Branch data Line data Source code
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 : 0 : static int normal_assignment(pa_config_parser_state *state) {
44 : : const pa_config_item *item;
45 : :
46 [ # # ]: 0 : pa_assert(state);
47 : :
48 [ # # ]: 0 : for (item = state->item_table; item->parse; item++) {
49 : :
50 [ # # ][ # # ]: 0 : if (item->lvalue && !pa_streq(state->lvalue, item->lvalue))
51 : 0 : continue;
52 : :
53 [ # # ][ # # ]: 0 : if (item->section && !state->section)
54 : 0 : continue;
55 : :
56 [ # # ][ # # ]: 0 : if (item->section && !pa_streq(state->section, item->section))
57 : 0 : continue;
58 : :
59 : 0 : state->data = item->data;
60 : :
61 : 0 : return item->parse(state);
62 : : }
63 : :
64 : 0 : pa_log("[%s:%u] Unknown lvalue '%s' in section '%s'.", state->filename, state->lineno, state->lvalue, pa_strna(state->section));
65 : :
66 : 0 : return -1;
67 : : }
68 : :
69 : : /* Parse a proplist entry. */
70 : 0 : static int proplist_assignment(pa_config_parser_state *state) {
71 [ # # ]: 0 : pa_assert(state);
72 [ # # ]: 0 : pa_assert(state->proplist);
73 : :
74 [ # # ]: 0 : if (pa_proplist_sets(state->proplist, state->lvalue, state->rvalue) < 0) {
75 : 0 : pa_log("[%s:%u] Failed to parse a proplist entry: %s = %s", state->filename, state->lineno, state->lvalue, state->rvalue);
76 : 0 : return -1;
77 : : }
78 : :
79 : : return 0;
80 : : }
81 : :
82 : : /* Parse a variable assignment line */
83 : 0 : static int parse_line(pa_config_parser_state *state) {
84 : : char *c;
85 : :
86 [ # # ][ # # ]: 0 : state->lvalue = state->buf + strspn(state->buf, WHITESPACE);
[ # # ][ # # ]
87 : :
88 [ # # ][ # # ]: 0 : if ((c = strpbrk(state->lvalue, COMMENTS)))
[ # # ][ # # ]
[ # # ]
89 : 0 : *c = 0;
90 : :
91 [ # # ]: 0 : if (!*state->lvalue)
92 : : return 0;
93 : :
94 [ # # ]: 0 : if (pa_startswith(state->lvalue, ".include ")) {
95 : 0 : char *path = NULL, *fn;
96 : : int r;
97 : :
98 : 0 : fn = pa_strip(state->lvalue + 9);
99 [ # # ]: 0 : if (!pa_is_path_absolute(fn)) {
100 : : const char *k;
101 [ # # ]: 0 : if ((k = strrchr(state->filename, '/'))) {
102 : 0 : char *dir = pa_xstrndup(state->filename, k - state->filename);
103 : 0 : fn = path = pa_sprintf_malloc("%s" PA_PATH_SEP "%s", dir, fn);
104 : 0 : pa_xfree(dir);
105 : : }
106 : : }
107 : :
108 : 0 : r = pa_config_parse(fn, NULL, state->item_table, state->proplist, state->userdata);
109 : 0 : pa_xfree(path);
110 : 0 : return r;
111 : : }
112 : :
113 [ # # ]: 0 : if (*state->lvalue == '[') {
114 : : size_t k;
115 : :
116 : 0 : k = strlen(state->lvalue);
117 [ # # ]: 0 : pa_assert(k > 0);
118 : :
119 [ # # ]: 0 : if (state->lvalue[k-1] != ']') {
120 : 0 : pa_log("[%s:%u] Invalid section header.", state->filename, state->lineno);
121 : 0 : return -1;
122 : : }
123 : :
124 : 0 : pa_xfree(state->section);
125 : 0 : state->section = pa_xstrndup(state->lvalue + 1, k-2);
126 : :
127 [ # # ]: 0 : if (pa_streq(state->section, "Properties")) {
128 [ # # ]: 0 : if (!state->proplist) {
129 : 0 : pa_log("[%s:%u] \"Properties\" section is not allowed in this file.", state->filename, state->lineno);
130 : 0 : return -1;
131 : : }
132 : :
133 : 0 : state->in_proplist = TRUE;
134 : : } else
135 : 0 : state->in_proplist = FALSE;
136 : :
137 : : return 0;
138 : : }
139 : :
140 [ # # ]: 0 : if (!(state->rvalue = strchr(state->lvalue, '='))) {
141 : 0 : pa_log("[%s:%u] Missing '='.", state->filename, state->lineno);
142 : 0 : return -1;
143 : : }
144 : :
145 : 0 : *state->rvalue = 0;
146 : 0 : state->rvalue++;
147 : :
148 : 0 : state->lvalue = pa_strip(state->lvalue);
149 : 0 : state->rvalue = pa_strip(state->rvalue);
150 : :
151 [ # # ]: 0 : if (state->in_proplist)
152 : 0 : return proplist_assignment(state);
153 : : else
154 : 0 : return normal_assignment(state);
155 : : }
156 : :
157 : : /* Go through the file and parse each line */
158 : 0 : int pa_config_parse(const char *filename, FILE *f, const pa_config_item *t, pa_proplist *proplist, void *userdata) {
159 : 0 : int r = -1;
160 : 0 : pa_bool_t do_close = !f;
161 : : pa_config_parser_state state;
162 : :
163 [ # # ]: 0 : pa_assert(filename);
164 [ # # ]: 0 : pa_assert(t);
165 : :
166 : : pa_zero(state);
167 : :
168 [ # # ][ # # ]: 0 : if (!f && !(f = pa_fopen_cloexec(filename, "r"))) {
169 [ # # ]: 0 : if (errno == ENOENT) {
170 : 0 : pa_log_debug("Failed to open configuration file '%s': %s", filename, pa_cstrerror(errno));
171 : 0 : r = 0;
172 : 0 : goto finish;
173 : : }
174 : :
175 : 0 : pa_log_warn("Failed to open configuration file '%s': %s", filename, pa_cstrerror(errno));
176 : 0 : goto finish;
177 : : }
178 : :
179 : 0 : state.filename = filename;
180 : 0 : state.item_table = t;
181 : 0 : state.userdata = userdata;
182 : :
183 [ # # ]: 0 : if (proplist)
184 : 0 : state.proplist = pa_proplist_new();
185 : :
186 [ # # ]: 0 : while (!feof(f)) {
187 [ # # ]: 0 : if (!fgets(state.buf, sizeof(state.buf), f)) {
188 [ # # ]: 0 : if (feof(f))
189 : : break;
190 : :
191 : 0 : pa_log_warn("Failed to read configuration file '%s': %s", filename, pa_cstrerror(errno));
192 : 0 : goto finish;
193 : : }
194 : :
195 : 0 : state.lineno++;
196 : :
197 [ # # ]: 0 : if (parse_line(&state) < 0)
198 : : goto finish;
199 : : }
200 : :
201 [ # # ]: 0 : if (proplist)
202 : 0 : pa_proplist_update(proplist, PA_UPDATE_REPLACE, state.proplist);
203 : :
204 : : r = 0;
205 : :
206 : : finish:
207 [ # # ]: 0 : if (state.proplist)
208 : 0 : pa_proplist_free(state.proplist);
209 : :
210 : 0 : pa_xfree(state.section);
211 : :
212 [ # # ][ # # ]: 0 : if (do_close && f)
213 : 0 : fclose(f);
214 : :
215 : 0 : return r;
216 : : }
217 : :
218 : 0 : int pa_config_parse_int(pa_config_parser_state *state) {
219 : : int *i;
220 : : int32_t k;
221 : :
222 [ # # ]: 0 : pa_assert(state);
223 : :
224 : 0 : i = state->data;
225 : :
226 [ # # ]: 0 : if (pa_atoi(state->rvalue, &k) < 0) {
227 : 0 : pa_log("[%s:%u] Failed to parse numeric value: %s", state->filename, state->lineno, state->rvalue);
228 : 0 : return -1;
229 : : }
230 : :
231 : 0 : *i = (int) k;
232 : 0 : return 0;
233 : : }
234 : :
235 : 0 : int pa_config_parse_unsigned(pa_config_parser_state *state) {
236 : : unsigned *u;
237 : : uint32_t k;
238 : :
239 [ # # ]: 0 : pa_assert(state);
240 : :
241 : 0 : u = state->data;
242 : :
243 [ # # ]: 0 : if (pa_atou(state->rvalue, &k) < 0) {
244 : 0 : pa_log("[%s:%u] Failed to parse numeric value: %s", state->filename, state->lineno, state->rvalue);
245 : 0 : return -1;
246 : : }
247 : :
248 : 0 : *u = (unsigned) k;
249 : 0 : return 0;
250 : : }
251 : :
252 : 0 : int pa_config_parse_size(pa_config_parser_state *state) {
253 : : size_t *i;
254 : : uint32_t k;
255 : :
256 [ # # ]: 0 : pa_assert(state);
257 : :
258 : 0 : i = state->data;
259 : :
260 [ # # ]: 0 : if (pa_atou(state->rvalue, &k) < 0) {
261 : 0 : pa_log("[%s:%u] Failed to parse numeric value: %s", state->filename, state->lineno, state->rvalue);
262 : 0 : return -1;
263 : : }
264 : :
265 : 0 : *i = (size_t) k;
266 : 0 : return 0;
267 : : }
268 : :
269 : 0 : int pa_config_parse_bool(pa_config_parser_state *state) {
270 : : int k;
271 : : pa_bool_t *b;
272 : :
273 [ # # ]: 0 : pa_assert(state);
274 : :
275 : 0 : b = state->data;
276 : :
277 [ # # ]: 0 : if ((k = pa_parse_boolean(state->rvalue)) < 0) {
278 : 0 : pa_log("[%s:%u] Failed to parse boolean value: %s", state->filename, state->lineno, state->rvalue);
279 : 0 : return -1;
280 : : }
281 : :
282 : 0 : *b = !!k;
283 : :
284 : 0 : return 0;
285 : : }
286 : :
287 : 0 : int pa_config_parse_not_bool(pa_config_parser_state *state) {
288 : : int k;
289 : : pa_bool_t *b;
290 : :
291 [ # # ]: 0 : pa_assert(state);
292 : :
293 : 0 : b = state->data;
294 : :
295 [ # # ]: 0 : if ((k = pa_parse_boolean(state->rvalue)) < 0) {
296 : 0 : pa_log("[%s:%u] Failed to parse boolean value: %s", state->filename, state->lineno, state->rvalue);
297 : 0 : return -1;
298 : : }
299 : :
300 : 0 : *b = !k;
301 : :
302 : 0 : return 0;
303 : : }
304 : :
305 : 0 : int pa_config_parse_string(pa_config_parser_state *state) {
306 : : char **s;
307 : :
308 [ # # ]: 0 : pa_assert(state);
309 : :
310 : 0 : s = state->data;
311 : :
312 : 0 : pa_xfree(*s);
313 [ # # ]: 0 : *s = *state->rvalue ? pa_xstrdup(state->rvalue) : NULL;
314 : 0 : return 0;
315 : : }
|