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