2
This file is part of PulseAudio.
4
Copyright 2004-2006 Lennart Poettering
6
PulseAudio is free software; you can redistribute it and/or modify
7
it under the terms of the GNU Lesser General Public License as published
8
by the Free Software Foundation; either version 2.1 of the License,
9
or (at your option) any later version.
11
PulseAudio is distributed in the hope that it will be useful, but
12
WITHOUT ANY WARRANTY; without even the implied warranty of
13
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14
General Public License for more details.
16
You should have received a copy of the GNU Lesser General Public License
17
along with PulseAudio; if not, see <http://www.gnu.org/licenses/>.
28
#include <pulse/xmalloc.h>
30
#include <pulsecore/core-error.h>
31
#include <pulsecore/log.h>
32
#include <pulsecore/core-util.h>
33
#include <pulsecore/macro.h>
35
#include "conf-parser.h"
37
#define WHITESPACE " \t\n"
38
#define COMMENTS "#;\n"
40
/* Run the user supplied parser for an assignment */
41
static int normal_assignment(pa_config_parser_state *state) {
42
const pa_config_item *item;
46
for (item = state->item_table; item->parse; item++) {
48
if (item->lvalue && !pa_streq(state->lvalue, item->lvalue))
51
if (item->section && !state->section)
54
if (item->section && !pa_streq(state->section, item->section))
57
state->data = item->data;
59
return item->parse(state);
62
pa_log("[%s:%u] Unknown lvalue '%s' in section '%s'.", state->filename, state->lineno, state->lvalue, pa_strna(state->section));
67
/* Parse a proplist entry. */
68
static int proplist_assignment(pa_config_parser_state *state) {
70
pa_assert(state->proplist);
72
if (pa_proplist_sets(state->proplist, state->lvalue, state->rvalue) < 0) {
73
pa_log("[%s:%u] Failed to parse a proplist entry: %s = %s", state->filename, state->lineno, state->lvalue, state->rvalue);
80
/* Parse a variable assignment line */
81
static int parse_line(pa_config_parser_state *state) {
84
state->lvalue = state->buf + strspn(state->buf, WHITESPACE);
86
if ((c = strpbrk(state->lvalue, COMMENTS)))
92
if (pa_startswith(state->lvalue, ".include ")) {
93
char *path = NULL, *fn;
96
fn = pa_strip(state->lvalue + 9);
97
if (!pa_is_path_absolute(fn)) {
99
if ((k = strrchr(state->filename, '/'))) {
100
char *dir = pa_xstrndup(state->filename, k - state->filename);
101
fn = path = pa_sprintf_malloc("%s" PA_PATH_SEP "%s", dir, fn);
106
r = pa_config_parse(fn, NULL, state->item_table, state->proplist, state->userdata);
111
if (*state->lvalue == '[') {
114
k = strlen(state->lvalue);
117
if (state->lvalue[k-1] != ']') {
118
pa_log("[%s:%u] Invalid section header.", state->filename, state->lineno);
122
pa_xfree(state->section);
123
state->section = pa_xstrndup(state->lvalue + 1, k-2);
125
if (pa_streq(state->section, "Properties")) {
126
if (!state->proplist) {
127
pa_log("[%s:%u] \"Properties\" section is not allowed in this file.", state->filename, state->lineno);
131
state->in_proplist = true;
133
state->in_proplist = false;
138
if (!(state->rvalue = strchr(state->lvalue, '='))) {
139
pa_log("[%s:%u] Missing '='.", state->filename, state->lineno);
146
state->lvalue = pa_strip(state->lvalue);
147
state->rvalue = pa_strip(state->rvalue);
149
if (state->in_proplist)
150
return proplist_assignment(state);
152
return normal_assignment(state);
155
/* Go through the file and parse each line */
156
int pa_config_parse(const char *filename, FILE *f, const pa_config_item *t, pa_proplist *proplist, void *userdata) {
159
pa_config_parser_state state;
166
if (!f && !(f = pa_fopen_cloexec(filename, "r"))) {
167
if (errno == ENOENT) {
168
pa_log_debug("Failed to open configuration file '%s': %s", filename, pa_cstrerror(errno));
173
pa_log_warn("Failed to open configuration file '%s': %s", filename, pa_cstrerror(errno));
176
pa_log_debug("Parsing configuration file '%s'", filename);
178
state.filename = filename;
179
state.item_table = t;
180
state.userdata = userdata;
183
state.proplist = pa_proplist_new();
186
if (!fgets(state.buf, sizeof(state.buf), f)) {
190
pa_log_warn("Failed to read configuration file '%s': %s", filename, pa_cstrerror(errno));
196
if (parse_line(&state) < 0)
201
pa_proplist_update(proplist, PA_UPDATE_REPLACE, state.proplist);
207
pa_proplist_free(state.proplist);
209
pa_xfree(state.section);
217
int pa_config_parse_int(pa_config_parser_state *state) {
225
if (pa_atoi(state->rvalue, &k) < 0) {
226
pa_log("[%s:%u] Failed to parse numeric value: %s", state->filename, state->lineno, state->rvalue);
234
int pa_config_parse_unsigned(pa_config_parser_state *state) {
242
if (pa_atou(state->rvalue, &k) < 0) {
243
pa_log("[%s:%u] Failed to parse numeric value: %s", state->filename, state->lineno, state->rvalue);
251
int pa_config_parse_size(pa_config_parser_state *state) {
259
if (pa_atou(state->rvalue, &k) < 0) {
260
pa_log("[%s:%u] Failed to parse numeric value: %s", state->filename, state->lineno, state->rvalue);
268
int pa_config_parse_bool(pa_config_parser_state *state) {
276
if ((k = pa_parse_boolean(state->rvalue)) < 0) {
277
pa_log("[%s:%u] Failed to parse boolean value: %s", state->filename, state->lineno, state->rvalue);
286
int pa_config_parse_not_bool(pa_config_parser_state *state) {
294
if ((k = pa_parse_boolean(state->rvalue)) < 0) {
295
pa_log("[%s:%u] Failed to parse boolean value: %s", state->filename, state->lineno, state->rvalue);
304
int pa_config_parse_string(pa_config_parser_state *state) {
312
*s = *state->rvalue ? pa_xstrdup(state->rvalue) : NULL;