2004-09-17 19:45:44 +00:00
|
|
|
/* $Id$ */
|
|
|
|
|
|
|
|
|
|
/***
|
2006-06-19 21:53:48 +00:00
|
|
|
This file is part of PulseAudio.
|
2004-09-17 19:45:44 +00:00
|
|
|
|
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
|
2004-09-17 19:45:44 +00:00
|
|
|
by the Free Software Foundation; either version 2 of the License,
|
|
|
|
|
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 <assert.h>
|
|
|
|
|
#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>
|
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 */
|
2006-01-11 01:17:39 +00:00
|
|
|
static int next_assignment(const char *filename, unsigned line, const pa_config_item *t, const char *lvalue, const char *rvalue, void *userdata) {
|
2004-09-17 19:45:44 +00:00
|
|
|
assert(filename && t && lvalue && rvalue);
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2004-09-17 19:45:44 +00:00
|
|
|
for (; t->parse; t++)
|
|
|
|
|
if (!strcmp(lvalue, t->lvalue))
|
|
|
|
|
return t->parse(filename, line, lvalue, rvalue, t->data, userdata);
|
|
|
|
|
|
2006-08-18 21:38:40 +00:00
|
|
|
pa_log("[%s:%u] Unknown lvalue '%s'.", filename, line, lvalue);
|
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) {
|
|
|
|
|
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 */
|
2006-01-11 01:17:39 +00:00
|
|
|
static int parse_line(const char *filename, unsigned line, const pa_config_item *t, char *l, void *userdata) {
|
2004-09-17 19:45:44 +00:00
|
|
|
char *e, *c, *b = l+strspn(l, WHITESPACE);
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
|
|
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++;
|
|
|
|
|
|
|
|
|
|
return next_assignment(filename, line, t, strip(b), strip(e), userdata);
|
|
|
|
|
}
|
|
|
|
|
|
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;
|
2004-11-04 21:27:12 +00:00
|
|
|
int do_close = !f;
|
2004-09-17 19:45:44 +00:00
|
|
|
assert(filename && 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) {
|
|
|
|
|
r = 0;
|
|
|
|
|
goto finish;
|
|
|
|
|
}
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2006-08-18 21:38:40 +00:00
|
|
|
pa_log_warn("WARNING: failed to open configuration file '%s': %s",
|
2006-05-22 15:20:46 +00:00
|
|
|
filename, pa_cstrerror(errno));
|
2004-09-17 19:45:44 +00:00
|
|
|
goto finish;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
while (!feof(f)) {
|
|
|
|
|
char l[256];
|
|
|
|
|
if (!fgets(l, sizeof(l), f)) {
|
|
|
|
|
if (feof(f))
|
|
|
|
|
break;
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2006-08-18 21:38:40 +00:00
|
|
|
pa_log_warn("WARNING: failed to read configuration file '%s': %s",
|
2006-05-22 15:20:46 +00:00
|
|
|
filename, pa_cstrerror(errno));
|
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
|
|
|
if (parse_line(filename, ++line, t, l, userdata) < 0)
|
|
|
|
|
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:
|
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2006-01-11 01:17:39 +00:00
|
|
|
int pa_config_parse_int(const char *filename, unsigned line, const char *lvalue, const char *rvalue, void *data, PA_GCC_UNUSED void *userdata) {
|
2004-12-11 00:10:41 +00:00
|
|
|
int *i = data;
|
|
|
|
|
int32_t k;
|
2004-09-17 19:45:44 +00:00
|
|
|
assert(filename && lvalue && rvalue && 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
|
|
|
}
|
|
|
|
|
|
2006-01-11 01:17:39 +00:00
|
|
|
int pa_config_parse_bool(const char *filename, unsigned line, const char *lvalue, const char *rvalue, void *data, PA_GCC_UNUSED void *userdata) {
|
2004-09-17 19:45:44 +00:00
|
|
|
int *b = data, k;
|
|
|
|
|
assert(filename && lvalue && rvalue && 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
|
|
|
|
2004-09-17 19:45:44 +00:00
|
|
|
*b = k;
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2004-09-17 19:45:44 +00:00
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2006-01-11 01:17:39 +00:00
|
|
|
int pa_config_parse_string(const char *filename, PA_GCC_UNUSED unsigned line, const char *lvalue, const char *rvalue, void *data, PA_GCC_UNUSED void *userdata) {
|
2004-09-17 19:45:44 +00:00
|
|
|
char **s = data;
|
|
|
|
|
assert(filename && lvalue && rvalue && data);
|
|
|
|
|
|
|
|
|
|
pa_xfree(*s);
|
|
|
|
|
*s = *rvalue ? pa_xstrdup(rvalue) : NULL;
|
|
|
|
|
return 0;
|
|
|
|
|
}
|