1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
|
/***
This file is part of PulseAudio.
Copyright 2004-2006 Lennart Poettering
PulseAudio is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published
by the Free Software Foundation; either version 2.1 of the License,
or (at your option) any later version.
PulseAudio is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with PulseAudio; if not, see <http://www.gnu.org/licenses/>.
***/
#include "config.h"
#include <dirent.h>
#include <string.h>
#include <stdio.h>
#include <errno.h>
#include "compat.h"
#include "conf-parser.h"
#define WHITESPACE " \t\n"
#define COMMENTS "#;\n"
/* Run the user supplied parser for an assignment */
static int normal_assignment(pa_config_parser_state *state) {
const pa_config_item *item;
pa_assert(state);
for (item = state->item_table; item->parse; item++) {
if (item->lvalue && !pa_streq(state->lvalue, item->lvalue))
continue;
if (item->section && !state->section)
continue;
if (item->section && !pa_streq(state->section, item->section))
continue;
state->data = item->data;
return item->parse(state);
}
pa_log("[%s:%u] Unknown lvalue '%s' in section '%s'.", state->filename, state->lineno, state->lvalue, pa_strna(state->section));
return -1;
}
/* Parse a proplist entry. */
static int proplist_assignment(pa_config_parser_state *state) {
pa_assert(state);
pa_assert(state->proplist);
if (pa_proplist_sets(state->proplist, state->lvalue, state->rvalue) < 0) {
pa_log("[%s:%u] Failed to parse a proplist entry: %s = %s", state->filename, state->lineno, state->lvalue, state->rvalue);
return -1;
}
return 0;
}
/* Parse a variable assignment line */
static int parse_line(pa_config_parser_state *state) {
char *c;
state->lvalue = state->buf + strspn(state->buf, WHITESPACE);
if ((c = strpbrk(state->lvalue, COMMENTS)))
*c = 0;
if (!*state->lvalue)
return 0;
if (pa_startswith(state->lvalue, ".include ")) {
char *path = NULL, *fn;
int r;
fn = pa_strip(state->lvalue + 9);
if (!pa_is_path_absolute(fn)) {
const char *k;
if ((k = strrchr(state->filename, '/'))) {
char *dir = pa_xstrndup(state->filename, k - state->filename);
fn = path = pa_sprintf_malloc("%s" PA_PATH_SEP "%s", dir, fn);
pa_xfree(dir);
}
}
r = pa_config_parse(fn, NULL, state->item_table, state->proplist, false, state->userdata);
pa_xfree(path);
return r;
}
if (*state->lvalue == '[') {
size_t k;
k = strlen(state->lvalue);
pa_assert(k > 0);
if (state->lvalue[k-1] != ']') {
pa_log("[%s:%u] Invalid section header.", state->filename, state->lineno);
return -1;
}
pa_xfree(state->section);
state->section = pa_xstrndup(state->lvalue + 1, k-2);
if (pa_streq(state->section, "Properties")) {
if (!state->proplist) {
pa_log("[%s:%u] \"Properties\" section is not allowed in this file.", state->filename, state->lineno);
return -1;
}
state->in_proplist = true;
} else
state->in_proplist = false;
return 0;
}
if (!(state->rvalue = strchr(state->lvalue, '='))) {
pa_log("[%s:%u] Missing '='.", state->filename, state->lineno);
return -1;
}
*state->rvalue = 0;
state->rvalue++;
state->lvalue = pa_strip(state->lvalue);
state->rvalue = pa_strip(state->rvalue);
if (state->in_proplist)
return proplist_assignment(state);
else
return normal_assignment(state);
}
#ifndef OS_IS_WIN32
static int conf_filter(const struct dirent *entry) {
return pa_endswith(entry->d_name, ".conf");
}
#endif
/* Go through the file and parse each line */
int pa_config_parse(const char *filename, FILE *f, const pa_config_item *t, pa_proplist *proplist, bool use_dot_d,
void *userdata) {
int r = -1;
bool do_close = !f;
pa_config_parser_state state;
pa_assert(filename);
pa_assert(t);
pa_zero(state);
if (!f && !(f = pa_fopen_cloexec(filename, "r"))) {
if (errno == ENOENT) {
pa_log_debug("Failed to open configuration file '%s': %s", filename, pa_cstrerror(errno));
r = 0;
goto finish;
}
pa_log_warn("Failed to open configuration file '%s': %s", filename, pa_cstrerror(errno));
goto finish;
}
pa_log_debug("Parsing configuration file '%s'", filename);
state.filename = filename;
state.item_table = t;
state.userdata = userdata;
if (proplist)
state.proplist = pa_proplist_new();
while (!feof(f)) {
if (!fgets(state.buf, sizeof(state.buf), f)) {
if (feof(f))
break;
pa_log_warn("Failed to read configuration file '%s': %s", filename, pa_cstrerror(errno));
goto finish;
}
state.lineno++;
if (parse_line(&state) < 0)
goto finish;
}
if (proplist)
pa_proplist_update(proplist, PA_UPDATE_REPLACE, state.proplist);
r = 0;
finish:
if (state.proplist)
pa_proplist_free(state.proplist);
pa_xfree(state.section);
if (do_close && f)
fclose(f);
if (use_dot_d) {
#ifdef OS_IS_WIN32
char *dir_name = pa_sprintf_malloc("%s.d", filename);
char *pattern = pa_sprintf_malloc("%s\\*.conf", dir_name);
HANDLE fh;
WIN32_FIND_DATA wfd;
fh = FindFirstFile(pattern, &wfd);
if (fh != INVALID_HANDLE_VALUE) {
do {
if (!(wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) {
char *filename2 = pa_sprintf_malloc("%s\\%s", dir_name, wfd.cFileName);
pa_config_parse(filename2, NULL, t, proplist, false, userdata);
pa_xfree(filename2);
}
} while (FindNextFile(fh, &wfd));
FindClose(fh);
} else {
DWORD err = GetLastError();
if (err == ERROR_PATH_NOT_FOUND) {
pa_log_debug("Pattern %s did not match any files, ignoring.", pattern);
} else {
LPVOID msgbuf;
DWORD fret = FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
NULL, err, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPTSTR)&msgbuf, 0, NULL);
if (fret != 0) {
pa_log_warn("FindFirstFile(%s) failed with error %ld (%s), ignoring.", pattern, err, (char*)msgbuf);
LocalFree(msgbuf);
} else {
pa_log_warn("FindFirstFile(%s) failed with error %ld, ignoring.", pattern, err);
pa_log_warn("FormatMessage failed with error %ld", GetLastError());
}
}
}
pa_xfree(pattern);
pa_xfree(dir_name);
#else
char *dir_name;
int n;
struct dirent **entries = NULL;
dir_name = pa_sprintf_malloc("%s.d", filename);
n = scandir(dir_name, &entries, conf_filter, alphasort);
if (n >= 0) {
int i;
for (i = 0; i < n; i++) {
char *filename2;
filename2 = pa_sprintf_malloc("%s" PA_PATH_SEP "%s", dir_name, entries[i]->d_name);
pa_config_parse(filename2, NULL, t, proplist, false, userdata);
pa_xfree(filename2);
free(entries[i]);
}
free(entries);
} else {
if (errno == ENOENT)
pa_log_debug("%s does not exist, ignoring.", dir_name);
else
pa_log_warn("scandir(\"%s\") failed: %s", dir_name, pa_cstrerror(errno));
}
pa_xfree(dir_name);
#endif
}
return r;
}
int pa_config_parse_int(pa_config_parser_state *state) {
int *i;
int32_t k;
pa_assert(state);
i = state->data;
if (pa_atoi(state->rvalue, &k) < 0) {
pa_log("[%s:%u] Failed to parse numeric value: %s", state->filename, state->lineno, state->rvalue);
return -1;
}
*i = (int) k;
return 0;
}
int pa_config_parse_unsigned(pa_config_parser_state *state) {
unsigned *u;
uint32_t k;
pa_assert(state);
u = state->data;
if (pa_atou(state->rvalue, &k) < 0) {
pa_log("[%s:%u] Failed to parse numeric value: %s", state->filename, state->lineno, state->rvalue);
return -1;
}
*u = (unsigned) k;
return 0;
}
int pa_config_parse_size(pa_config_parser_state *state) {
size_t *i;
uint32_t k;
pa_assert(state);
i = state->data;
if (pa_atou(state->rvalue, &k) < 0) {
pa_log("[%s:%u] Failed to parse numeric value: %s", state->filename, state->lineno, state->rvalue);
return -1;
}
*i = (size_t) k;
return 0;
}
int pa_config_parse_bool(pa_config_parser_state *state) {
int k;
bool *b;
pa_assert(state);
b = state->data;
if ((k = pa_parse_boolean(state->rvalue)) < 0) {
pa_log("[%s:%u] Failed to parse boolean value: %s", state->filename, state->lineno, state->rvalue);
return -1;
}
*b = !!k;
return 0;
}
int pa_config_parse_not_bool(pa_config_parser_state *state) {
int k;
bool *b;
pa_assert(state);
b = state->data;
if ((k = pa_parse_boolean(state->rvalue)) < 0) {
pa_log("[%s:%u] Failed to parse boolean value: %s", state->filename, state->lineno, state->rvalue);
return -1;
}
*b = !k;
return 0;
}
int pa_config_parse_string(pa_config_parser_state *state) {
char **s;
pa_assert(state);
s = state->data;
pa_xfree(*s);
*s = *state->rvalue ? pa_xstrdup(state->rvalue) : NULL;
return 0;
}
|