2004-09-17 19:45:44 +00:00
|
|
|
/***
|
2006-06-19 21:53:48 +00:00
|
|
|
This file is part of PulseAudio.
|
2004-09-17 19:45:44 +00:00
|
|
|
|
2007-02-13 15:35:19 +00:00
|
|
|
Copyright 2004-2006 Lennart Poettering
|
|
|
|
|
|
2006-06-19 21:53:48 +00:00
|
|
|
PulseAudio is free software; you can redistribute it and/or modify
|
2004-11-14 14:58:54 +00:00
|
|
|
it under the terms of the GNU Lesser General Public License as published
|
2009-03-03 20:23:02 +00:00
|
|
|
by the Free Software Foundation; either version 2.1 of the License,
|
2004-09-17 19:45:44 +00:00
|
|
|
or (at your option) any later version.
|
|
|
|
|
|
2006-06-19 21:53:48 +00:00
|
|
|
PulseAudio is distributed in the hope that it will be useful, but
|
2004-09-17 19:45:44 +00:00
|
|
|
WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
|
General Public License for more details.
|
|
|
|
|
|
2004-11-14 14:58:54 +00:00
|
|
|
You should have received a copy of the GNU Lesser General Public License
|
2006-06-19 21:53:48 +00:00
|
|
|
along with PulseAudio; if not, write to the Free Software
|
2004-09-17 19:45:44 +00:00
|
|
|
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
|
|
|
|
|
USA.
|
|
|
|
|
***/
|
|
|
|
|
|
2006-01-10 17:51:06 +00:00
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
|
#include <config.h>
|
|
|
|
|
#endif
|
|
|
|
|
|
2004-09-17 19:45:44 +00:00
|
|
|
#include <string.h>
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <errno.h>
|
|
|
|
|
|
2006-06-19 21:53:48 +00:00
|
|
|
#include <pulse/xmalloc.h>
|
2006-05-17 16:34:18 +00:00
|
|
|
|
2006-06-19 21:53:48 +00:00
|
|
|
#include <pulsecore/core-error.h>
|
|
|
|
|
#include <pulsecore/log.h>
|
|
|
|
|
#include <pulsecore/core-util.h>
|
2007-10-28 19:13:50 +00:00
|
|
|
#include <pulsecore/macro.h>
|
2006-02-17 12:10:58 +00:00
|
|
|
|
2004-09-17 20:08:52 +00:00
|
|
|
#include "conf-parser.h"
|
2004-09-17 19:45:44 +00:00
|
|
|
|
|
|
|
|
#define WHITESPACE " \t\n"
|
|
|
|
|
#define COMMENTS "#;\n"
|
|
|
|
|
|
2004-11-21 21:31:28 +00:00
|
|
|
/* Run the user supplied parser for an assignment */
|
2012-04-13 14:48:26 +03:00
|
|
|
static int next_assignment(pa_config_parser_state *state) {
|
2012-04-13 14:48:25 +03:00
|
|
|
const pa_config_item *item;
|
2009-06-17 03:11:47 +02:00
|
|
|
|
2012-04-13 14:48:25 +03:00
|
|
|
pa_assert(state);
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2012-04-13 14:48:25 +03:00
|
|
|
for (item = state->item_table; item->parse; item++) {
|
2009-06-17 03:11:47 +02:00
|
|
|
|
2012-04-13 14:48:25 +03:00
|
|
|
if (item->lvalue && !pa_streq(state->lvalue, item->lvalue))
|
2009-06-17 03:11:47 +02:00
|
|
|
continue;
|
|
|
|
|
|
2012-04-13 14:48:25 +03:00
|
|
|
if (item->section && !state->section)
|
2009-06-17 03:11:47 +02:00
|
|
|
continue;
|
|
|
|
|
|
2012-04-13 14:48:25 +03:00
|
|
|
if (item->section && !pa_streq(state->section, item->section))
|
2009-06-17 03:11:47 +02:00
|
|
|
continue;
|
|
|
|
|
|
2012-04-13 14:48:26 +03:00
|
|
|
state->data = item->data;
|
|
|
|
|
|
|
|
|
|
return item->parse(state);
|
2009-06-17 03:11:47 +02:00
|
|
|
}
|
2004-09-17 19:45:44 +00:00
|
|
|
|
2012-04-13 14:48:25 +03:00
|
|
|
pa_log("[%s:%u] Unknown lvalue '%s' in section '%s'.", state->filename, state->lineno, state->lvalue, pa_strna(state->section));
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2004-09-17 19:45:44 +00:00
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
2004-11-21 21:31:28 +00:00
|
|
|
/* Parse a variable assignment line */
|
2012-04-13 14:48:26 +03:00
|
|
|
static int parse_line(pa_config_parser_state *state) {
|
2012-04-13 14:48:25 +03:00
|
|
|
char *c;
|
2009-02-05 04:11:26 +01:00
|
|
|
|
2012-04-13 14:48:25 +03:00
|
|
|
state->lvalue = state->buf + strspn(state->buf, WHITESPACE);
|
2004-09-17 19:45:44 +00:00
|
|
|
|
2012-04-13 14:48:25 +03:00
|
|
|
if ((c = strpbrk(state->lvalue, COMMENTS)))
|
2004-09-17 19:45:44 +00:00
|
|
|
*c = 0;
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2012-04-13 14:48:25 +03:00
|
|
|
if (!*state->lvalue)
|
2004-09-17 19:45:44 +00:00
|
|
|
return 0;
|
|
|
|
|
|
2012-04-13 14:48:25 +03:00
|
|
|
if (pa_startswith(state->lvalue, ".include ")) {
|
2009-09-08 23:51:39 +02:00
|
|
|
char *path = NULL, *fn;
|
2009-06-17 03:11:47 +02:00
|
|
|
int r;
|
|
|
|
|
|
2012-04-13 14:48:25 +03:00
|
|
|
fn = pa_strip(state->lvalue + 9);
|
2009-06-17 03:11:47 +02:00
|
|
|
if (!pa_is_path_absolute(fn)) {
|
|
|
|
|
const char *k;
|
2012-04-13 14:48:25 +03:00
|
|
|
if ((k = strrchr(state->filename, '/'))) {
|
|
|
|
|
char *dir = pa_xstrndup(state->filename, k - state->filename);
|
2009-06-17 03:11:47 +02:00
|
|
|
fn = path = pa_sprintf_malloc("%s" PA_PATH_SEP "%s", dir, fn);
|
|
|
|
|
pa_xfree(dir);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-04-13 14:48:25 +03:00
|
|
|
r = pa_config_parse(fn, NULL, state->item_table, state->userdata);
|
2009-06-17 03:11:47 +02:00
|
|
|
pa_xfree(path);
|
|
|
|
|
return r;
|
|
|
|
|
}
|
|
|
|
|
|
2012-04-13 14:48:25 +03:00
|
|
|
if (*state->lvalue == '[') {
|
2009-02-05 04:11:26 +01:00
|
|
|
size_t k;
|
|
|
|
|
|
2012-04-13 14:48:25 +03:00
|
|
|
k = strlen(state->lvalue);
|
2009-02-05 04:11:26 +01:00
|
|
|
pa_assert(k > 0);
|
|
|
|
|
|
2012-04-13 14:48:25 +03:00
|
|
|
if (state->lvalue[k-1] != ']') {
|
|
|
|
|
pa_log("[%s:%u] Invalid section header.", state->filename, state->lineno);
|
2009-02-05 04:11:26 +01:00
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
2012-04-13 14:48:25 +03:00
|
|
|
pa_xfree(state->section);
|
|
|
|
|
state->section = pa_xstrndup(state->lvalue + 1, k-2);
|
2009-02-05 04:11:26 +01:00
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2012-04-13 14:48:25 +03:00
|
|
|
if (!(state->rvalue = strchr(state->lvalue, '='))) {
|
|
|
|
|
pa_log("[%s:%u] Missing '='.", state->filename, state->lineno);
|
2004-09-17 19:45:44 +00:00
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
2012-04-13 14:48:25 +03:00
|
|
|
*state->rvalue = 0;
|
|
|
|
|
state->rvalue++;
|
2004-09-17 19:45:44 +00:00
|
|
|
|
2012-04-13 14:48:25 +03:00
|
|
|
state->lvalue = pa_strip(state->lvalue);
|
|
|
|
|
state->rvalue = pa_strip(state->rvalue);
|
|
|
|
|
|
|
|
|
|
return next_assignment(state);
|
2004-09-17 19:45:44 +00:00
|
|
|
}
|
|
|
|
|
|
2004-11-21 21:31:28 +00:00
|
|
|
/* Go through the file and parse each line */
|
2006-01-11 01:17:39 +00:00
|
|
|
int pa_config_parse(const char *filename, FILE *f, const pa_config_item *t, void *userdata) {
|
2004-09-17 19:45:44 +00:00
|
|
|
int r = -1;
|
2009-02-21 23:47:08 +01:00
|
|
|
pa_bool_t do_close = !f;
|
2012-04-13 14:48:26 +03:00
|
|
|
pa_config_parser_state state;
|
2007-10-28 19:13:50 +00:00
|
|
|
|
|
|
|
|
pa_assert(filename);
|
|
|
|
|
pa_assert(t);
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2009-10-30 04:54:19 +01:00
|
|
|
if (!f && !(f = pa_fopen_cloexec(filename, "r"))) {
|
2004-09-17 19:45:44 +00:00
|
|
|
if (errno == ENOENT) {
|
2009-06-17 03:11:47 +02:00
|
|
|
pa_log_debug("Failed to open configuration file '%s': %s", filename, pa_cstrerror(errno));
|
2004-09-17 19:45:44 +00:00
|
|
|
r = 0;
|
|
|
|
|
goto finish;
|
|
|
|
|
}
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2009-02-05 04:11:26 +01:00
|
|
|
pa_log_warn("Failed to open configuration file '%s': %s", filename, pa_cstrerror(errno));
|
2004-09-17 19:45:44 +00:00
|
|
|
goto finish;
|
|
|
|
|
}
|
|
|
|
|
|
2012-04-13 14:48:25 +03:00
|
|
|
pa_zero(state);
|
|
|
|
|
state.filename = filename;
|
|
|
|
|
state.item_table = t;
|
|
|
|
|
state.userdata = userdata;
|
2009-02-05 04:11:26 +01:00
|
|
|
|
2012-04-13 14:48:25 +03:00
|
|
|
while (!feof(f)) {
|
|
|
|
|
if (!fgets(state.buf, sizeof(state.buf), f)) {
|
2004-09-17 19:45:44 +00:00
|
|
|
if (feof(f))
|
|
|
|
|
break;
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2009-02-05 04:11:26 +01:00
|
|
|
pa_log_warn("Failed to read configuration file '%s': %s", filename, pa_cstrerror(errno));
|
2004-09-17 19:45:44 +00:00
|
|
|
goto finish;
|
|
|
|
|
}
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2012-04-13 14:48:25 +03:00
|
|
|
state.lineno++;
|
|
|
|
|
|
|
|
|
|
if (parse_line(&state) < 0)
|
2004-09-17 19:45:44 +00:00
|
|
|
goto finish;
|
|
|
|
|
}
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2004-09-17 19:45:44 +00:00
|
|
|
r = 0;
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2004-09-17 19:45:44 +00:00
|
|
|
finish:
|
2012-04-13 14:48:25 +03:00
|
|
|
pa_xfree(state.section);
|
2004-09-17 19:45:44 +00:00
|
|
|
|
2004-11-04 21:27:12 +00:00
|
|
|
if (do_close && f)
|
2004-09-17 19:45:44 +00:00
|
|
|
fclose(f);
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2004-09-17 19:45:44 +00:00
|
|
|
return r;
|
|
|
|
|
}
|
|
|
|
|
|
2012-04-13 14:48:26 +03:00
|
|
|
int pa_config_parse_int(pa_config_parser_state *state) {
|
|
|
|
|
int *i;
|
2004-12-11 00:10:41 +00:00
|
|
|
int32_t k;
|
2007-10-28 19:13:50 +00:00
|
|
|
|
2012-04-13 14:48:26 +03:00
|
|
|
pa_assert(state);
|
|
|
|
|
|
|
|
|
|
i = state->data;
|
2004-12-11 00:10:41 +00:00
|
|
|
|
2012-04-13 14:48:26 +03:00
|
|
|
if (pa_atoi(state->rvalue, &k) < 0) {
|
|
|
|
|
pa_log("[%s:%u] Failed to parse numeric value: %s", state->filename, state->lineno, state->rvalue);
|
2004-09-17 19:45:44 +00:00
|
|
|
return -1;
|
|
|
|
|
}
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2004-12-11 00:10:41 +00:00
|
|
|
*i = (int) k;
|
2007-01-04 13:43:45 +00:00
|
|
|
return 0;
|
2004-09-17 19:45:44 +00:00
|
|
|
}
|
|
|
|
|
|
2012-04-13 14:48:26 +03:00
|
|
|
int pa_config_parse_unsigned(pa_config_parser_state *state) {
|
|
|
|
|
unsigned *u;
|
2008-10-21 18:38:55 +02:00
|
|
|
uint32_t k;
|
|
|
|
|
|
2012-04-13 14:48:26 +03:00
|
|
|
pa_assert(state);
|
|
|
|
|
|
|
|
|
|
u = state->data;
|
2008-10-21 18:38:55 +02:00
|
|
|
|
2012-04-13 14:48:26 +03:00
|
|
|
if (pa_atou(state->rvalue, &k) < 0) {
|
|
|
|
|
pa_log("[%s:%u] Failed to parse numeric value: %s", state->filename, state->lineno, state->rvalue);
|
2008-10-21 18:38:55 +02:00
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
*u = (unsigned) k;
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2012-04-13 14:48:26 +03:00
|
|
|
int pa_config_parse_size(pa_config_parser_state *state) {
|
|
|
|
|
size_t *i;
|
2008-10-01 01:14:36 +02:00
|
|
|
uint32_t k;
|
|
|
|
|
|
2012-04-13 14:48:26 +03:00
|
|
|
pa_assert(state);
|
|
|
|
|
|
|
|
|
|
i = state->data;
|
2008-10-01 01:14:36 +02:00
|
|
|
|
2012-04-13 14:48:26 +03:00
|
|
|
if (pa_atou(state->rvalue, &k) < 0) {
|
|
|
|
|
pa_log("[%s:%u] Failed to parse numeric value: %s", state->filename, state->lineno, state->rvalue);
|
2008-10-01 01:14:36 +02:00
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
*i = (size_t) k;
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2012-04-13 14:48:26 +03:00
|
|
|
int pa_config_parse_bool(pa_config_parser_state *state) {
|
2007-11-01 00:31:59 +00:00
|
|
|
int k;
|
2012-04-13 14:48:26 +03:00
|
|
|
pa_bool_t *b;
|
2007-10-28 19:13:50 +00:00
|
|
|
|
2012-04-13 14:48:26 +03:00
|
|
|
pa_assert(state);
|
|
|
|
|
|
|
|
|
|
b = state->data;
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2012-04-13 14:48:26 +03:00
|
|
|
if ((k = pa_parse_boolean(state->rvalue)) < 0) {
|
|
|
|
|
pa_log("[%s:%u] Failed to parse boolean value: %s", state->filename, state->lineno, state->rvalue);
|
2004-09-17 19:45:44 +00:00
|
|
|
return -1;
|
|
|
|
|
}
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2007-11-01 00:31:59 +00:00
|
|
|
*b = !!k;
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2004-09-17 19:45:44 +00:00
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2012-04-13 14:48:26 +03:00
|
|
|
int pa_config_parse_not_bool(pa_config_parser_state *state) {
|
2009-08-12 20:14:31 +02:00
|
|
|
int k;
|
2012-04-13 14:48:26 +03:00
|
|
|
pa_bool_t *b;
|
2009-08-12 20:14:31 +02:00
|
|
|
|
2012-04-13 14:48:26 +03:00
|
|
|
pa_assert(state);
|
|
|
|
|
|
|
|
|
|
b = state->data;
|
2009-08-12 20:14:31 +02:00
|
|
|
|
2012-04-13 14:48:26 +03:00
|
|
|
if ((k = pa_parse_boolean(state->rvalue)) < 0) {
|
|
|
|
|
pa_log("[%s:%u] Failed to parse boolean value: %s", state->filename, state->lineno, state->rvalue);
|
2009-08-12 20:14:31 +02:00
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
*b = !k;
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2012-04-13 14:48:26 +03:00
|
|
|
int pa_config_parse_string(pa_config_parser_state *state) {
|
|
|
|
|
char **s;
|
2007-10-28 19:13:50 +00:00
|
|
|
|
2012-04-13 14:48:26 +03:00
|
|
|
pa_assert(state);
|
|
|
|
|
|
|
|
|
|
s = state->data;
|
2004-09-17 19:45:44 +00:00
|
|
|
|
|
|
|
|
pa_xfree(*s);
|
2012-04-13 14:48:26 +03:00
|
|
|
*s = *state->rvalue ? pa_xstrdup(state->rvalue) : NULL;
|
2004-09-17 19:45:44 +00:00
|
|
|
return 0;
|
|
|
|
|
}
|