alsa: Add a proplist to mappings
[pulseaudio-raopUDP/pulseaudio-raop-alac.git] / src / pulsecore / conf-parser.c
blob062fa8e5d09173f1869b5c593eee3b1d3fec1628
1 /***
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, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19 USA.
20 ***/
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
26 #include <string.h>
27 #include <stdio.h>
28 #include <errno.h>
30 #include <pulse/xmalloc.h>
32 #include <pulsecore/core-error.h>
33 #include <pulsecore/log.h>
34 #include <pulsecore/core-util.h>
35 #include <pulsecore/macro.h>
37 #include "conf-parser.h"
39 #define WHITESPACE " \t\n"
40 #define COMMENTS "#;\n"
42 /* Run the user supplied parser for an assignment */
43 static int normal_assignment(pa_config_parser_state *state) {
44 const pa_config_item *item;
46 pa_assert(state);
48 for (item = state->item_table; item->parse; item++) {
50 if (item->lvalue && !pa_streq(state->lvalue, item->lvalue))
51 continue;
53 if (item->section && !state->section)
54 continue;
56 if (item->section && !pa_streq(state->section, item->section))
57 continue;
59 state->data = item->data;
61 return item->parse(state);
64 pa_log("[%s:%u] Unknown lvalue '%s' in section '%s'.", state->filename, state->lineno, state->lvalue, pa_strna(state->section));
66 return -1;
69 /* Parse a proplist entry. */
70 static int proplist_assignment(pa_config_parser_state *state) {
71 pa_assert(state);
72 pa_assert(state->proplist);
74 if (pa_proplist_sets(state->proplist, state->lvalue, state->rvalue) < 0) {
75 pa_log("[%s:%u] Failed to parse a proplist entry: %s = %s", state->filename, state->lineno, state->lvalue, state->rvalue);
76 return -1;
79 return 0;
82 /* Parse a variable assignment line */
83 static int parse_line(pa_config_parser_state *state) {
84 char *c;
86 state->lvalue = state->buf + strspn(state->buf, WHITESPACE);
88 if ((c = strpbrk(state->lvalue, COMMENTS)))
89 *c = 0;
91 if (!*state->lvalue)
92 return 0;
94 if (pa_startswith(state->lvalue, ".include ")) {
95 char *path = NULL, *fn;
96 int r;
98 fn = pa_strip(state->lvalue + 9);
99 if (!pa_is_path_absolute(fn)) {
100 const char *k;
101 if ((k = strrchr(state->filename, '/'))) {
102 char *dir = pa_xstrndup(state->filename, k - state->filename);
103 fn = path = pa_sprintf_malloc("%s" PA_PATH_SEP "%s", dir, fn);
104 pa_xfree(dir);
108 r = pa_config_parse(fn, NULL, state->item_table, state->proplist, state->userdata);
109 pa_xfree(path);
110 return r;
113 if (*state->lvalue == '[') {
114 size_t k;
116 k = strlen(state->lvalue);
117 pa_assert(k > 0);
119 if (state->lvalue[k-1] != ']') {
120 pa_log("[%s:%u] Invalid section header.", state->filename, state->lineno);
121 return -1;
124 pa_xfree(state->section);
125 state->section = pa_xstrndup(state->lvalue + 1, k-2);
127 if (pa_streq(state->section, "Properties")) {
128 if (!state->proplist) {
129 pa_log("[%s:%u] \"Properties\" section is not allowed in this file.", state->filename, state->lineno);
130 return -1;
133 state->in_proplist = TRUE;
134 } else
135 state->in_proplist = FALSE;
137 return 0;
140 if (!(state->rvalue = strchr(state->lvalue, '='))) {
141 pa_log("[%s:%u] Missing '='.", state->filename, state->lineno);
142 return -1;
145 *state->rvalue = 0;
146 state->rvalue++;
148 state->lvalue = pa_strip(state->lvalue);
149 state->rvalue = pa_strip(state->rvalue);
151 if (state->in_proplist)
152 return proplist_assignment(state);
153 else
154 return normal_assignment(state);
157 /* Go through the file and parse each line */
158 int pa_config_parse(const char *filename, FILE *f, const pa_config_item *t, pa_proplist *proplist, void *userdata) {
159 int r = -1;
160 pa_bool_t do_close = !f;
161 pa_config_parser_state state;
163 pa_assert(filename);
164 pa_assert(t);
166 pa_zero(state);
168 if (!f && !(f = pa_fopen_cloexec(filename, "r"))) {
169 if (errno == ENOENT) {
170 pa_log_debug("Failed to open configuration file '%s': %s", filename, pa_cstrerror(errno));
171 r = 0;
172 goto finish;
175 pa_log_warn("Failed to open configuration file '%s': %s", filename, pa_cstrerror(errno));
176 goto finish;
179 state.filename = filename;
180 state.item_table = t;
181 state.userdata = userdata;
183 if (proplist)
184 state.proplist = pa_proplist_new();
186 while (!feof(f)) {
187 if (!fgets(state.buf, sizeof(state.buf), f)) {
188 if (feof(f))
189 break;
191 pa_log_warn("Failed to read configuration file '%s': %s", filename, pa_cstrerror(errno));
192 goto finish;
195 state.lineno++;
197 if (parse_line(&state) < 0)
198 goto finish;
201 if (proplist)
202 pa_proplist_update(proplist, PA_UPDATE_REPLACE, state.proplist);
204 r = 0;
206 finish:
207 if (state.proplist)
208 pa_proplist_free(state.proplist);
210 pa_xfree(state.section);
212 if (do_close && f)
213 fclose(f);
215 return r;
218 int pa_config_parse_int(pa_config_parser_state *state) {
219 int *i;
220 int32_t k;
222 pa_assert(state);
224 i = state->data;
226 if (pa_atoi(state->rvalue, &k) < 0) {
227 pa_log("[%s:%u] Failed to parse numeric value: %s", state->filename, state->lineno, state->rvalue);
228 return -1;
231 *i = (int) k;
232 return 0;
235 int pa_config_parse_unsigned(pa_config_parser_state *state) {
236 unsigned *u;
237 uint32_t k;
239 pa_assert(state);
241 u = state->data;
243 if (pa_atou(state->rvalue, &k) < 0) {
244 pa_log("[%s:%u] Failed to parse numeric value: %s", state->filename, state->lineno, state->rvalue);
245 return -1;
248 *u = (unsigned) k;
249 return 0;
252 int pa_config_parse_size(pa_config_parser_state *state) {
253 size_t *i;
254 uint32_t k;
256 pa_assert(state);
258 i = state->data;
260 if (pa_atou(state->rvalue, &k) < 0) {
261 pa_log("[%s:%u] Failed to parse numeric value: %s", state->filename, state->lineno, state->rvalue);
262 return -1;
265 *i = (size_t) k;
266 return 0;
269 int pa_config_parse_bool(pa_config_parser_state *state) {
270 int k;
271 pa_bool_t *b;
273 pa_assert(state);
275 b = state->data;
277 if ((k = pa_parse_boolean(state->rvalue)) < 0) {
278 pa_log("[%s:%u] Failed to parse boolean value: %s", state->filename, state->lineno, state->rvalue);
279 return -1;
282 *b = !!k;
284 return 0;
287 int pa_config_parse_not_bool(pa_config_parser_state *state) {
288 int k;
289 pa_bool_t *b;
291 pa_assert(state);
293 b = state->data;
295 if ((k = pa_parse_boolean(state->rvalue)) < 0) {
296 pa_log("[%s:%u] Failed to parse boolean value: %s", state->filename, state->lineno, state->rvalue);
297 return -1;
300 *b = !k;
302 return 0;
305 int pa_config_parse_string(pa_config_parser_state *state) {
306 char **s;
308 pa_assert(state);
310 s = state->data;
312 pa_xfree(*s);
313 *s = *state->rvalue ? pa_xstrdup(state->rvalue) : NULL;
314 return 0;