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/>.
29 #include <pulse/xmalloc.h>
31 #include <pulsecore/core-error.h>
32 #include <pulsecore/log.h>
33 #include <pulsecore/core-util.h>
34 #include <pulsecore/macro.h>
36 #include "conf-parser.h"
38 #define WHITESPACE " \t\n"
39 #define COMMENTS "#;\n"
41 /* Run the user supplied parser for an assignment */
42 static int normal_assignment(pa_config_parser_state *state) {
43 const pa_config_item *item;
47 for (item = state->item_table; item->parse; item++) {
49 if (item->lvalue && !pa_streq(state->lvalue, item->lvalue))
52 if (item->section && !state->section)
55 if (item->section && !pa_streq(state->section, item->section))
58 state->data = item->data;
60 return item->parse(state);
63 pa_log("[%s:%u] Unknown lvalue '%s' in section '%s'.", state->filename, state->lineno, state->lvalue, pa_strna(state->section));
68 /* Parse a proplist entry. */
69 static int proplist_assignment(pa_config_parser_state *state) {
71 pa_assert(state->proplist);
73 if (pa_proplist_sets(state->proplist, state->lvalue, state->rvalue) < 0) {
74 pa_log("[%s:%u] Failed to parse a proplist entry: %s = %s", state->filename, state->lineno, state->lvalue, state->rvalue);
81 /* Parse a variable assignment line */
82 static int parse_line(pa_config_parser_state *state) {
85 state->lvalue = state->buf + strspn(state->buf, WHITESPACE);
87 if ((c = strpbrk(state->lvalue, COMMENTS)))
93 if (pa_startswith(state->lvalue, ".include ")) {
94 char *path = NULL, *fn;
97 fn = pa_strip(state->lvalue + 9);
98 if (!pa_is_path_absolute(fn)) {
100 if ((k = strrchr(state->filename, '/'))) {
101 char *dir = pa_xstrndup(state->filename, k - state->filename);
102 fn = path = pa_sprintf_malloc("%s" PA_PATH_SEP "%s", dir, fn);
107 r = pa_config_parse(fn, NULL, state->item_table, state->proplist, false, state->userdata);
112 if (*state->lvalue == '[') {
115 k = strlen(state->lvalue);
118 if (state->lvalue[k-1] != ']') {
119 pa_log("[%s:%u] Invalid section header.", state->filename, state->lineno);
123 pa_xfree(state->section);
124 state->section = pa_xstrndup(state->lvalue + 1, k-2);
126 if (pa_streq(state->section, "Properties")) {
127 if (!state->proplist) {
128 pa_log("[%s:%u] \"Properties\" section is not allowed in this file.", state->filename, state->lineno);
132 state->in_proplist = true;
134 state->in_proplist = false;
139 if (!(state->rvalue = strchr(state->lvalue, '='))) {
140 pa_log("[%s:%u] Missing '='.", state->filename, state->lineno);
147 state->lvalue = pa_strip(state->lvalue);
148 state->rvalue = pa_strip(state->rvalue);
150 if (state->in_proplist)
151 return proplist_assignment(state);
153 return normal_assignment(state);
157 static int conf_filter(const struct dirent *entry) {
158 return pa_endswith(entry->d_name, ".conf");
162 /* Go through the file and parse each line */
163 int pa_config_parse(const char *filename, FILE *f, const pa_config_item *t, pa_proplist *proplist, bool use_dot_d,
167 pa_config_parser_state state;
174 if (!f && !(f = pa_fopen_cloexec(filename, "r"))) {
175 if (errno == ENOENT) {
176 pa_log_debug("Failed to open configuration file '%s': %s", filename, pa_cstrerror(errno));
181 pa_log_warn("Failed to open configuration file '%s': %s", filename, pa_cstrerror(errno));
184 pa_log_debug("Parsing configuration file '%s'", filename);
186 state.filename = filename;
187 state.item_table = t;
188 state.userdata = userdata;
191 state.proplist = pa_proplist_new();
194 if (!fgets(state.buf, sizeof(state.buf), f)) {
198 pa_log_warn("Failed to read configuration file '%s': %s", filename, pa_cstrerror(errno));
204 if (parse_line(&state) < 0)
209 pa_proplist_update(proplist, PA_UPDATE_REPLACE, state.proplist);
215 pa_proplist_free(state.proplist);
217 pa_xfree(state.section);
224 char *dir_name = pa_sprintf_malloc("%s.d", filename);
225 char *pattern = pa_sprintf_malloc("%s\\*.conf", dir_name);
229 fh = FindFirstFile(pattern, &wfd);
230 if (fh != INVALID_HANDLE_VALUE) {
232 if (!(wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) {
233 char *filename2 = pa_sprintf_malloc("%s\\%s", dir_name, wfd.cFileName);
234 pa_config_parse(filename2, NULL, t, proplist, false, userdata);
237 } while (FindNextFile(fh, &wfd));
240 DWORD err = GetLastError();
242 if (err == ERROR_PATH_NOT_FOUND) {
243 pa_log_debug("Pattern %s did not match any files, ignoring.", pattern);
246 DWORD fret = FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
247 NULL, err, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPTSTR)&msgbuf, 0, NULL);
250 pa_log_warn("FindFirstFile(%s) failed with error %ld (%s), ignoring.", pattern, err, (char*)msgbuf);
253 pa_log_warn("FindFirstFile(%s) failed with error %ld, ignoring.", pattern, err);
254 pa_log_warn("FormatMessage failed with error %lu", GetLastError());
264 struct dirent **entries = NULL;
266 dir_name = pa_sprintf_malloc("%s.d", filename);
268 n = scandir(dir_name, &entries, conf_filter, alphasort);
272 for (i = 0; i < n; i++) {
275 filename2 = pa_sprintf_malloc("%s" PA_PATH_SEP "%s", dir_name, entries[i]->d_name);
276 pa_config_parse(filename2, NULL, t, proplist, false, userdata);
285 pa_log_debug("%s does not exist, ignoring.", dir_name);
287 pa_log_warn("scandir(\"%s\") failed: %s", dir_name, pa_cstrerror(errno));
297 int pa_config_parse_int(pa_config_parser_state *state) {
305 if (pa_atoi(state->rvalue, &k) < 0) {
306 pa_log("[%s:%u] Failed to parse numeric value: %s", state->filename, state->lineno, state->rvalue);
314 int pa_config_parse_unsigned(pa_config_parser_state *state) {
322 if (pa_atou(state->rvalue, &k) < 0) {
323 pa_log("[%s:%u] Failed to parse numeric value: %s", state->filename, state->lineno, state->rvalue);
331 int pa_config_parse_size(pa_config_parser_state *state) {
339 if (pa_atou(state->rvalue, &k) < 0) {
340 pa_log("[%s:%u] Failed to parse numeric value: %s", state->filename, state->lineno, state->rvalue);
348 int pa_config_parse_bool(pa_config_parser_state *state) {
356 if ((k = pa_parse_boolean(state->rvalue)) < 0) {
357 pa_log("[%s:%u] Failed to parse boolean value: %s", state->filename, state->lineno, state->rvalue);
366 int pa_config_parse_not_bool(pa_config_parser_state *state) {
374 if ((k = pa_parse_boolean(state->rvalue)) < 0) {
375 pa_log("[%s:%u] Failed to parse boolean value: %s", state->filename, state->lineno, state->rvalue);
384 int pa_config_parse_string(pa_config_parser_state *state) {
392 *s = *state->rvalue ? pa_xstrdup(state->rvalue) : NULL;