]> code.delx.au - pulseaudio/blob - src/pulsecore/conf-parser.c
conf-parser: Remove redundant feof() call
[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 pa_bool_t 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 pa_log_warn("Failed to read configuration file '%s': %s", filename, pa_cstrerror(errno));
189 goto finish;
190 }
191
192 state.lineno++;
193
194 if (parse_line(&state) < 0)
195 goto finish;
196 }
197
198 if (proplist)
199 pa_proplist_update(proplist, PA_UPDATE_REPLACE, state.proplist);
200
201 r = 0;
202
203 finish:
204 if (state.proplist)
205 pa_proplist_free(state.proplist);
206
207 pa_xfree(state.section);
208
209 if (do_close && f)
210 fclose(f);
211
212 return r;
213 }
214
215 int pa_config_parse_int(pa_config_parser_state *state) {
216 int *i;
217 int32_t k;
218
219 pa_assert(state);
220
221 i = state->data;
222
223 if (pa_atoi(state->rvalue, &k) < 0) {
224 pa_log("[%s:%u] Failed to parse numeric value: %s", state->filename, state->lineno, state->rvalue);
225 return -1;
226 }
227
228 *i = (int) k;
229 return 0;
230 }
231
232 int pa_config_parse_unsigned(pa_config_parser_state *state) {
233 unsigned *u;
234 uint32_t k;
235
236 pa_assert(state);
237
238 u = state->data;
239
240 if (pa_atou(state->rvalue, &k) < 0) {
241 pa_log("[%s:%u] Failed to parse numeric value: %s", state->filename, state->lineno, state->rvalue);
242 return -1;
243 }
244
245 *u = (unsigned) k;
246 return 0;
247 }
248
249 int pa_config_parse_size(pa_config_parser_state *state) {
250 size_t *i;
251 uint32_t k;
252
253 pa_assert(state);
254
255 i = state->data;
256
257 if (pa_atou(state->rvalue, &k) < 0) {
258 pa_log("[%s:%u] Failed to parse numeric value: %s", state->filename, state->lineno, state->rvalue);
259 return -1;
260 }
261
262 *i = (size_t) k;
263 return 0;
264 }
265
266 int pa_config_parse_bool(pa_config_parser_state *state) {
267 int k;
268 pa_bool_t *b;
269
270 pa_assert(state);
271
272 b = state->data;
273
274 if ((k = pa_parse_boolean(state->rvalue)) < 0) {
275 pa_log("[%s:%u] Failed to parse boolean value: %s", state->filename, state->lineno, state->rvalue);
276 return -1;
277 }
278
279 *b = !!k;
280
281 return 0;
282 }
283
284 int pa_config_parse_not_bool(pa_config_parser_state *state) {
285 int k;
286 pa_bool_t *b;
287
288 pa_assert(state);
289
290 b = state->data;
291
292 if ((k = pa_parse_boolean(state->rvalue)) < 0) {
293 pa_log("[%s:%u] Failed to parse boolean value: %s", state->filename, state->lineno, state->rvalue);
294 return -1;
295 }
296
297 *b = !k;
298
299 return 0;
300 }
301
302 int pa_config_parse_string(pa_config_parser_state *state) {
303 char **s;
304
305 pa_assert(state);
306
307 s = state->data;
308
309 pa_xfree(*s);
310 *s = *state->rvalue ? pa_xstrdup(state->rvalue) : NULL;
311 return 0;
312 }