]> code.delx.au - pulseaudio/blob - src/pulsecore/conf-parser.c
remap: Change remapping function argument type from void to int16_t / float as approp...
[pulseaudio] / src / pulsecore / conf-parser.c
1 /***
2 This file is part of PulseAudio.
3
4 Copyright 2004-2006 Lennart Poettering
5
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.
10
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.
15
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 ***/
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <string.h>
27 #include <stdio.h>
28 #include <errno.h>
29
30 #include <pulse/xmalloc.h>
31
32 #include <pulsecore/core-error.h>
33 #include <pulsecore/log.h>
34 #include <pulsecore/core-util.h>
35 #include <pulsecore/macro.h>
36
37 #include "conf-parser.h"
38
39 #define WHITESPACE " \t\n"
40 #define COMMENTS "#;\n"
41
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;
45
46 pa_assert(state);
47
48 for (item = state->item_table; item->parse; item++) {
49
50 if (item->lvalue && !pa_streq(state->lvalue, item->lvalue))
51 continue;
52
53 if (item->section && !state->section)
54 continue;
55
56 if (item->section && !pa_streq(state->section, item->section))
57 continue;
58
59 state->data = item->data;
60
61 return item->parse(state);
62 }
63
64 pa_log("[%s:%u] Unknown lvalue '%s' in section '%s'.", state->filename, state->lineno, state->lvalue, pa_strna(state->section));
65
66 return -1;
67 }
68
69 /* Parse a proplist entry. */
70 static int proplist_assignment(pa_config_parser_state *state) {
71 pa_assert(state);
72 pa_assert(state->proplist);
73
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;
77 }
78
79 return 0;
80 }
81
82 /* Parse a variable assignment line */
83 static int parse_line(pa_config_parser_state *state) {
84 char *c;
85
86 state->lvalue = state->buf + strspn(state->buf, WHITESPACE);
87
88 if ((c = strpbrk(state->lvalue, COMMENTS)))
89 *c = 0;
90
91 if (!*state->lvalue)
92 return 0;
93
94 if (pa_startswith(state->lvalue, ".include ")) {
95 char *path = NULL, *fn;
96 int r;
97
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);
105 }
106 }
107
108 r = pa_config_parse(fn, NULL, state->item_table, state->proplist, state->userdata);
109 pa_xfree(path);
110 return r;
111 }
112
113 if (*state->lvalue == '[') {
114 size_t k;
115
116 k = strlen(state->lvalue);
117 pa_assert(k > 0);
118
119 if (state->lvalue[k-1] != ']') {
120 pa_log("[%s:%u] Invalid section header.", state->filename, state->lineno);
121 return -1;
122 }
123
124 pa_xfree(state->section);
125 state->section = pa_xstrndup(state->lvalue + 1, k-2);
126
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;
131 }
132
133 state->in_proplist = true;
134 } else
135 state->in_proplist = false;
136
137 return 0;
138 }
139
140 if (!(state->rvalue = strchr(state->lvalue, '='))) {
141 pa_log("[%s:%u] Missing '='.", state->filename, state->lineno);
142 return -1;
143 }
144
145 *state->rvalue = 0;
146 state->rvalue++;
147
148 state->lvalue = pa_strip(state->lvalue);
149 state->rvalue = pa_strip(state->rvalue);
150
151 if (state->in_proplist)
152 return proplist_assignment(state);
153 else
154 return normal_assignment(state);
155 }
156
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 bool do_close = !f;
161 pa_config_parser_state state;
162
163 pa_assert(filename);
164 pa_assert(t);
165
166 pa_zero(state);
167
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;
173 }
174
175 pa_log_warn("Failed to open configuration file '%s': %s", filename, pa_cstrerror(errno));
176 goto finish;
177 }
178
179 state.filename = filename;
180 state.item_table = t;
181 state.userdata = userdata;
182
183 if (proplist)
184 state.proplist = pa_proplist_new();
185
186 while (!feof(f)) {
187 if (!fgets(state.buf, sizeof(state.buf), f)) {
188 if (feof(f))
189 break;
190
191 pa_log_warn("Failed to read configuration file '%s': %s", filename, pa_cstrerror(errno));
192 goto finish;
193 }
194
195 state.lineno++;
196
197 if (parse_line(&state) < 0)
198 goto finish;
199 }
200
201 if (proplist)
202 pa_proplist_update(proplist, PA_UPDATE_REPLACE, state.proplist);
203
204 r = 0;
205
206 finish:
207 if (state.proplist)
208 pa_proplist_free(state.proplist);
209
210 pa_xfree(state.section);
211
212 if (do_close && f)
213 fclose(f);
214
215 return r;
216 }
217
218 int pa_config_parse_int(pa_config_parser_state *state) {
219 int *i;
220 int32_t k;
221
222 pa_assert(state);
223
224 i = state->data;
225
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;
229 }
230
231 *i = (int) k;
232 return 0;
233 }
234
235 int pa_config_parse_unsigned(pa_config_parser_state *state) {
236 unsigned *u;
237 uint32_t k;
238
239 pa_assert(state);
240
241 u = state->data;
242
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;
246 }
247
248 *u = (unsigned) k;
249 return 0;
250 }
251
252 int pa_config_parse_size(pa_config_parser_state *state) {
253 size_t *i;
254 uint32_t k;
255
256 pa_assert(state);
257
258 i = state->data;
259
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;
263 }
264
265 *i = (size_t) k;
266 return 0;
267 }
268
269 int pa_config_parse_bool(pa_config_parser_state *state) {
270 int k;
271 bool *b;
272
273 pa_assert(state);
274
275 b = state->data;
276
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;
280 }
281
282 *b = !!k;
283
284 return 0;
285 }
286
287 int pa_config_parse_not_bool(pa_config_parser_state *state) {
288 int k;
289 bool *b;
290
291 pa_assert(state);
292
293 b = state->data;
294
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;
298 }
299
300 *b = !k;
301
302 return 0;
303 }
304
305 int pa_config_parse_string(pa_config_parser_state *state) {
306 char **s;
307
308 pa_assert(state);
309
310 s = state->data;
311
312 pa_xfree(*s);
313 *s = *state->rvalue ? pa_xstrdup(state->rvalue) : NULL;
314 return 0;
315 }