]> code.delx.au - pulseaudio/blob - src/pulsecore/conf-parser.c
6b0e1d5670812e010c3eb087ccdedfef30bf1c05
[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 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(const char *filename, unsigned line, const pa_config_item *t, const char *lvalue, const char *rvalue, void *userdata) {
44 pa_assert(filename);
45 pa_assert(t);
46 pa_assert(lvalue);
47 pa_assert(rvalue);
48
49 for (; t->parse; t++)
50 if (!strcmp(lvalue, t->lvalue))
51 return t->parse(filename, line, lvalue, rvalue, t->data, userdata);
52
53 pa_log("[%s:%u] Unknown lvalue '%s'.", filename, line, lvalue);
54
55 return -1;
56 }
57
58 /* Returns non-zero when c is contained in s */
59 static int in_string(char c, const char *s) {
60 pa_assert(s);
61
62 for (; *s; s++)
63 if (*s == c)
64 return 1;
65
66 return 0;
67 }
68
69 /* Remove all whitepsapce from the beginning and the end of *s. *s may
70 * be modified. */
71 static char *strip(char *s) {
72 char *b = s+strspn(s, WHITESPACE);
73 char *e, *l = NULL;
74
75 for (e = b; *e; e++)
76 if (!in_string(*e, WHITESPACE))
77 l = e;
78
79 if (l)
80 *(l+1) = 0;
81
82 return b;
83 }
84
85 /* Parse a variable assignment line */
86 static int parse_line(const char *filename, unsigned line, const pa_config_item *t, char *l, void *userdata) {
87 char *e, *c, *b = l+strspn(l, WHITESPACE);
88
89 if ((c = strpbrk(b, COMMENTS)))
90 *c = 0;
91
92 if (!*b)
93 return 0;
94
95 if (!(e = strchr(b, '='))) {
96 pa_log("[%s:%u] Missing '='.", filename, line);
97 return -1;
98 }
99
100 *e = 0;
101 e++;
102
103 return next_assignment(filename, line, t, strip(b), strip(e), userdata);
104 }
105
106 /* Go through the file and parse each line */
107 int pa_config_parse(const char *filename, FILE *f, const pa_config_item *t, void *userdata) {
108 int r = -1;
109 unsigned line = 0;
110 int do_close = !f;
111
112 pa_assert(filename);
113 pa_assert(t);
114
115 if (!f && !(f = fopen(filename, "r"))) {
116 if (errno == ENOENT) {
117 r = 0;
118 goto finish;
119 }
120
121 pa_log_warn("Failed to open configuration file '%s': %s",
122 filename, pa_cstrerror(errno));
123 goto finish;
124 }
125
126 while (!feof(f)) {
127 char l[256];
128 if (!fgets(l, sizeof(l), f)) {
129 if (feof(f))
130 break;
131
132 pa_log_warn("Failed to read configuration file '%s': %s",
133 filename, pa_cstrerror(errno));
134 goto finish;
135 }
136
137 if (parse_line(filename, ++line, t, l, userdata) < 0)
138 goto finish;
139 }
140
141 r = 0;
142
143 finish:
144
145 if (do_close && f)
146 fclose(f);
147
148 return r;
149 }
150
151 int pa_config_parse_int(const char *filename, unsigned line, const char *lvalue, const char *rvalue, void *data, void *userdata) {
152 int *i = data;
153 int32_t k;
154
155 pa_assert(filename);
156 pa_assert(lvalue);
157 pa_assert(rvalue);
158 pa_assert(data);
159
160 if (pa_atoi(rvalue, &k) < 0) {
161 pa_log("[%s:%u] Failed to parse numeric value: %s", filename, line, rvalue);
162 return -1;
163 }
164
165 *i = (int) k;
166 return 0;
167 }
168
169 int pa_config_parse_bool(const char *filename, unsigned line, const char *lvalue, const char *rvalue, void *data, void *userdata) {
170 int k;
171 pa_bool_t *b = data;
172
173 pa_assert(filename);
174 pa_assert(lvalue);
175 pa_assert(rvalue);
176 pa_assert(data);
177
178 if ((k = pa_parse_boolean(rvalue)) < 0) {
179 pa_log("[%s:%u] Failed to parse boolean value: %s", filename, line, rvalue);
180 return -1;
181 }
182
183 *b = !!k;
184
185 return 0;
186 }
187
188 int pa_config_parse_string(const char *filename, unsigned line, const char *lvalue, const char *rvalue, void *data, void *userdata) {
189 char **s = data;
190
191 pa_assert(filename);
192 pa_assert(lvalue);
193 pa_assert(rvalue);
194 pa_assert(data);
195
196 pa_xfree(*s);
197 *s = *rvalue ? pa_xstrdup(rvalue) : NULL;
198 return 0;
199 }