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 */
|
2009-06-17 03:11:47 +02:00
|
|
|
static int next_assignment(
|
|
|
|
|
const char *filename,
|
|
|
|
|
unsigned line,
|
|
|
|
|
const char *section,
|
|
|
|
|
const pa_config_item *t,
|
|
|
|
|
const char *lvalue,
|
|
|
|
|
const char *rvalue,
|
|
|
|
|
void *userdata) {
|
|
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
pa_assert(filename);
|
|
|
|
|
pa_assert(t);
|
|
|
|
|
pa_assert(lvalue);
|
|
|
|
|
pa_assert(rvalue);
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2009-06-17 03:11:47 +02:00
|
|
|
for (; t->parse; t++) {
|
|
|
|
|
|
|
|
|
|
if (t->lvalue && !pa_streq(lvalue, t->lvalue))
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
if (t->section && !section)
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
if (t->section && !pa_streq(section, t->section))
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
return t->parse(filename, line, section, lvalue, rvalue, t->data, userdata);
|
|
|
|
|
}
|
2004-09-17 19:45:44 +00:00
|
|
|
|
2009-06-17 03:11:47 +02:00
|
|
|
pa_log("[%s:%u] Unknown lvalue '%s' in section '%s'.", filename, line, lvalue, pa_strna(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
|
|
|
/* Returns non-zero when c is contained in s */
|
2004-09-17 19:45:44 +00:00
|
|
|
static int in_string(char c, const char *s) {
|
2007-10-28 19:13:50 +00:00
|
|
|
pa_assert(s);
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2004-09-17 19:45:44 +00:00
|
|
|
for (; *s; s++)
|
|
|
|
|
if (*s == c)
|
|
|
|
|
return 1;
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2004-09-17 19:45:44 +00:00
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2004-11-21 21:31:28 +00:00
|
|
|
/* Remove all whitepsapce from the beginning and the end of *s. *s may
|
|
|
|
|
* be modified. */
|
2004-09-17 19:45:44 +00:00
|
|
|
static char *strip(char *s) {
|
|
|
|
|
char *b = s+strspn(s, WHITESPACE);
|
|
|
|
|
char *e, *l = NULL;
|
|
|
|
|
|
|
|
|
|
for (e = b; *e; e++)
|
|
|
|
|
if (!in_string(*e, WHITESPACE))
|
|
|
|
|
l = e;
|
|
|
|
|
|
|
|
|
|
if (l)
|
|
|
|
|
*(l+1) = 0;
|
|
|
|
|
|
|
|
|
|
return b;
|
|
|
|
|
}
|
|
|
|
|
|
2004-11-21 21:31:28 +00:00
|
|
|
/* Parse a variable assignment line */
|
2009-02-05 04:11:26 +01:00
|
|
|
static int parse_line(const char *filename, unsigned line, char **section, const pa_config_item *t, char *l, void *userdata) {
|
|
|
|
|
char *e, *c, *b;
|
|
|
|
|
|
|
|
|
|
b = l+strspn(l, WHITESPACE);
|
2004-09-17 19:45:44 +00:00
|
|
|
|
|
|
|
|
if ((c = strpbrk(b, COMMENTS)))
|
|
|
|
|
*c = 0;
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2004-09-17 19:45:44 +00:00
|
|
|
if (!*b)
|
|
|
|
|
return 0;
|
|
|
|
|
|
2009-06-17 03:11:47 +02:00
|
|
|
if (pa_startswith(b, ".include ")) {
|
|
|
|
|
char *path, *fn;
|
|
|
|
|
int r;
|
|
|
|
|
|
|
|
|
|
fn = strip(b+9);
|
|
|
|
|
if (!pa_is_path_absolute(fn)) {
|
|
|
|
|
const char *k;
|
|
|
|
|
if ((k = strrchr(filename, '/'))) {
|
|
|
|
|
char *dir = pa_xstrndup(filename, k-filename);
|
|
|
|
|
fn = path = pa_sprintf_malloc("%s" PA_PATH_SEP "%s", dir, fn);
|
|
|
|
|
pa_xfree(dir);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
r = pa_config_parse(fn, NULL, t, userdata);
|
|
|
|
|
pa_xfree(path);
|
|
|
|
|
return r;
|
|
|
|
|
}
|
|
|
|
|
|
2009-02-05 04:11:26 +01:00
|
|
|
if (*b == '[') {
|
|
|
|
|
size_t k;
|
|
|
|
|
|
|
|
|
|
k = strlen(b);
|
|
|
|
|
pa_assert(k > 0);
|
|
|
|
|
|
|
|
|
|
if (b[k-1] != ']') {
|
|
|
|
|
pa_log("[%s:%u] Invalid section header.", filename, line);
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pa_xfree(*section);
|
|
|
|
|
*section = pa_xstrndup(b+1, k-2);
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2004-09-17 19:45:44 +00:00
|
|
|
if (!(e = strchr(b, '='))) {
|
2006-08-18 21:38:40 +00:00
|
|
|
pa_log("[%s:%u] Missing '='.", filename, line);
|
2004-09-17 19:45:44 +00:00
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
*e = 0;
|
|
|
|
|
e++;
|
|
|
|
|
|
2009-02-05 04:11:26 +01:00
|
|
|
return next_assignment(filename, line, *section, t, strip(b), strip(e), userdata);
|
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;
|
|
|
|
|
unsigned line = 0;
|
2009-02-21 23:47:08 +01:00
|
|
|
pa_bool_t do_close = !f;
|
2009-02-05 04:11:26 +01:00
|
|
|
char *section = NULL;
|
2007-10-28 19:13:50 +00:00
|
|
|
|
|
|
|
|
pa_assert(filename);
|
|
|
|
|
pa_assert(t);
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2004-11-04 21:27:12 +00:00
|
|
|
if (!f && !(f = fopen(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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
while (!feof(f)) {
|
2009-02-21 23:47:08 +01:00
|
|
|
char l[4096];
|
2009-02-05 04:11:26 +01:00
|
|
|
|
2004-09-17 19:45:44 +00:00
|
|
|
if (!fgets(l, sizeof(l), f)) {
|
|
|
|
|
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
|
|
|
|
2009-02-05 04:11:26 +01:00
|
|
|
if (parse_line(filename, ++line, §ion, t, l, userdata) < 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:
|
2009-02-05 04:11:26 +01:00
|
|
|
pa_xfree(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;
|
|
|
|
|
}
|
|
|
|
|
|
2009-02-05 04:11:26 +01:00
|
|
|
int pa_config_parse_int(const char *filename, unsigned line, const char *section, const char *lvalue, const char *rvalue, void *data, void *userdata) {
|
2004-12-11 00:10:41 +00:00
|
|
|
int *i = data;
|
|
|
|
|
int32_t k;
|
2007-10-28 19:13:50 +00:00
|
|
|
|
|
|
|
|
pa_assert(filename);
|
|
|
|
|
pa_assert(lvalue);
|
|
|
|
|
pa_assert(rvalue);
|
|
|
|
|
pa_assert(data);
|
2004-12-11 00:10:41 +00:00
|
|
|
|
|
|
|
|
if (pa_atoi(rvalue, &k) < 0) {
|
2006-08-18 21:38:40 +00:00
|
|
|
pa_log("[%s:%u] Failed to parse numeric value: %s", filename, line, 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
|
|
|
}
|
|
|
|
|
|
2009-02-05 04:11:26 +01:00
|
|
|
int pa_config_parse_unsigned(const char *filename, unsigned line, const char *section, const char *lvalue, const char *rvalue, void *data, void *userdata) {
|
2008-10-21 18:38:55 +02:00
|
|
|
unsigned *u = data;
|
|
|
|
|
uint32_t k;
|
|
|
|
|
|
|
|
|
|
pa_assert(filename);
|
|
|
|
|
pa_assert(lvalue);
|
|
|
|
|
pa_assert(rvalue);
|
|
|
|
|
pa_assert(data);
|
|
|
|
|
|
|
|
|
|
if (pa_atou(rvalue, &k) < 0) {
|
|
|
|
|
pa_log("[%s:%u] Failed to parse numeric value: %s", filename, line, rvalue);
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
*u = (unsigned) k;
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2009-02-05 04:11:26 +01:00
|
|
|
int pa_config_parse_size(const char *filename, unsigned line, const char *section, const char *lvalue, const char *rvalue, void *data, void *userdata) {
|
2008-10-01 01:14:36 +02:00
|
|
|
size_t *i = data;
|
|
|
|
|
uint32_t k;
|
|
|
|
|
|
|
|
|
|
pa_assert(filename);
|
|
|
|
|
pa_assert(lvalue);
|
|
|
|
|
pa_assert(rvalue);
|
|
|
|
|
pa_assert(data);
|
|
|
|
|
|
|
|
|
|
if (pa_atou(rvalue, &k) < 0) {
|
|
|
|
|
pa_log("[%s:%u] Failed to parse numeric value: %s", filename, line, rvalue);
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
*i = (size_t) k;
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2009-02-05 04:11:26 +01:00
|
|
|
int pa_config_parse_bool(const char *filename, unsigned line, const char *section, const char *lvalue, const char *rvalue, void *data, void *userdata) {
|
2007-11-01 00:31:59 +00:00
|
|
|
int k;
|
|
|
|
|
pa_bool_t *b = data;
|
2007-10-28 19:13:50 +00:00
|
|
|
|
|
|
|
|
pa_assert(filename);
|
|
|
|
|
pa_assert(lvalue);
|
|
|
|
|
pa_assert(rvalue);
|
|
|
|
|
pa_assert(data);
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2004-09-17 19:45:44 +00:00
|
|
|
if ((k = pa_parse_boolean(rvalue)) < 0) {
|
2006-08-18 21:38:40 +00:00
|
|
|
pa_log("[%s:%u] Failed to parse boolean value: %s", filename, line, 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;
|
|
|
|
|
}
|
|
|
|
|
|
2009-02-05 04:11:26 +01:00
|
|
|
int pa_config_parse_string(const char *filename, unsigned line, const char *section, const char *lvalue, const char *rvalue, void *data, void *userdata) {
|
2004-09-17 19:45:44 +00:00
|
|
|
char **s = data;
|
2007-10-28 19:13:50 +00:00
|
|
|
|
|
|
|
|
pa_assert(filename);
|
|
|
|
|
pa_assert(lvalue);
|
|
|
|
|
pa_assert(rvalue);
|
|
|
|
|
pa_assert(data);
|
2004-09-17 19:45:44 +00:00
|
|
|
|
|
|
|
|
pa_xfree(*s);
|
|
|
|
|
*s = *rvalue ? pa_xstrdup(rvalue) : NULL;
|
|
|
|
|
return 0;
|
|
|
|
|
}
|