Typo. s/a/are/
[pulseaudio-mirror.git] / src / pulsecore / conf-parser.c
blob34b4d6fe21b5752feebd72d12930dd3a689c7696
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 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) {
52 pa_assert(filename);
53 pa_assert(t);
54 pa_assert(lvalue);
55 pa_assert(rvalue);
57 for (; t->parse; t++) {
59 if (t->lvalue && !pa_streq(lvalue, t->lvalue))
60 continue;
62 if (t->section && !section)
63 continue;
65 if (t->section && !pa_streq(section, t->section))
66 continue;
68 return t->parse(filename, line, section, lvalue, rvalue, t->data, userdata);
71 pa_log("[%s:%u] Unknown lvalue '%s' in section '%s'.", filename, line, lvalue, pa_strna(section));
73 return -1;
76 /* Returns non-zero when c is contained in s */
77 static int in_string(char c, const char *s) {
78 pa_assert(s);
80 for (; *s; s++)
81 if (*s == c)
82 return 1;
84 return 0;
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;
93 for (e = b; *e; e++)
94 if (!in_string(*e, WHITESPACE))
95 l = e;
97 if (l)
98 *(l+1) = 0;
100 return b;
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;
107 b = l+strspn(l, WHITESPACE);
109 if ((c = strpbrk(b, COMMENTS)))
110 *c = 0;
112 if (!*b)
113 return 0;
115 if (pa_startswith(b, ".include ")) {
116 char *path = NULL, *fn;
117 int r;
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);
129 r = pa_config_parse(fn, NULL, t, userdata);
130 pa_xfree(path);
131 return r;
134 if (*b == '[') {
135 size_t k;
137 k = strlen(b);
138 pa_assert(k > 0);
140 if (b[k-1] != ']') {
141 pa_log("[%s:%u] Invalid section header.", filename, line);
142 return -1;
145 pa_xfree(*section);
146 *section = pa_xstrndup(b+1, k-2);
147 return 0;
150 if (!(e = strchr(b, '='))) {
151 pa_log("[%s:%u] Missing '='.", filename, line);
152 return -1;
155 *e = 0;
156 e++;
158 return next_assignment(filename, line, *section, t, strip(b), strip(e), userdata);
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;
168 pa_assert(filename);
169 pa_assert(t);
171 if (!f && !(f = pa_fopen_cloexec(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;
178 pa_log_warn("Failed to open configuration file '%s': %s", filename, pa_cstrerror(errno));
179 goto finish;
182 while (!feof(f)) {
183 char l[4096];
185 if (!fgets(l, sizeof(l), f)) {
186 if (feof(f))
187 break;
189 pa_log_warn("Failed to read configuration file '%s': %s", filename, pa_cstrerror(errno));
190 goto finish;
193 if (parse_line(filename, ++line, &section, t, l, userdata) < 0)
194 goto finish;
197 r = 0;
199 finish:
200 pa_xfree(section);
202 if (do_close && f)
203 fclose(f);
205 return r;
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;
212 pa_assert(filename);
213 pa_assert(lvalue);
214 pa_assert(rvalue);
215 pa_assert(data);
217 if (pa_atoi(rvalue, &k) < 0) {
218 pa_log("[%s:%u] Failed to parse numeric value: %s", filename, line, rvalue);
219 return -1;
222 *i = (int) k;
223 return 0;
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;
230 pa_assert(filename);
231 pa_assert(lvalue);
232 pa_assert(rvalue);
233 pa_assert(data);
235 if (pa_atou(rvalue, &k) < 0) {
236 pa_log("[%s:%u] Failed to parse numeric value: %s", filename, line, rvalue);
237 return -1;
240 *u = (unsigned) k;
241 return 0;
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;
248 pa_assert(filename);
249 pa_assert(lvalue);
250 pa_assert(rvalue);
251 pa_assert(data);
253 if (pa_atou(rvalue, &k) < 0) {
254 pa_log("[%s:%u] Failed to parse numeric value: %s", filename, line, rvalue);
255 return -1;
258 *i = (size_t) k;
259 return 0;
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;
266 pa_assert(filename);
267 pa_assert(lvalue);
268 pa_assert(rvalue);
269 pa_assert(data);
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;
276 *b = !!k;
278 return 0;
281 int pa_config_parse_not_bool(
282 const char *filename, unsigned line,
283 const char *section,
284 const char *lvalue, const char *rvalue,
285 void *data, void *userdata) {
287 int k;
288 pa_bool_t *b = data;
290 pa_assert(filename);
291 pa_assert(lvalue);
292 pa_assert(rvalue);
293 pa_assert(data);
295 if ((k = pa_parse_boolean(rvalue)) < 0) {
296 pa_log("[%s:%u] Failed to parse boolean value: %s", filename, line, rvalue);
297 return -1;
300 *b = !k;
302 return 0;
305 int pa_config_parse_string(const char *filename, unsigned line, const char *section, const char *lvalue, const char *rvalue, void *data, void *userdata) {
306 char **s = data;
308 pa_assert(filename);
309 pa_assert(lvalue);
310 pa_assert(rvalue);
311 pa_assert(data);
313 pa_xfree(*s);
314 *s = *rvalue ? pa_xstrdup(rvalue) : NULL;
315 return 0;