Commit | Line | Data |
---|---|---|
460b9539 | 1 | /* |
2 | * This file is part of DisOrder. | |
e83d0967 | 3 | * Copyright (C) 2004, 2005, 2006, 2007 Richard Kettlewell |
460b9539 | 4 | * |
5 | * This program is free software; you can redistribute it and/or modify | |
6 | * it under the terms of the GNU General Public License as published by | |
7 | * the Free Software Foundation; either version 2 of the License, or | |
8 | * (at your option) any later version. | |
9 | * | |
10 | * This program is distributed in the hope that it will be useful, but | |
11 | * WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
13 | * General Public License for more details. | |
14 | * | |
15 | * You should have received a copy of the GNU General Public License | |
16 | * along with this program; if not, write to the Free Software | |
17 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 | |
18 | * USA | |
19 | */ | |
0e4472a0 | 20 | /** @file lib/configuration.c |
21 | * @brief Configuration file support | |
22 | */ | |
460b9539 | 23 | |
24 | #include <config.h> | |
25 | #include "types.h" | |
26 | ||
27 | #include <stdio.h> | |
28 | #include <string.h> | |
29 | #include <stdlib.h> | |
30 | #include <errno.h> | |
31 | #include <sys/types.h> | |
32 | #include <sys/stat.h> | |
33 | #include <unistd.h> | |
34 | #include <ctype.h> | |
35 | #include <stddef.h> | |
36 | #include <pwd.h> | |
37 | #include <langinfo.h> | |
38 | #include <pcre.h> | |
39 | #include <signal.h> | |
40 | ||
41 | #include "configuration.h" | |
42 | #include "mem.h" | |
43 | #include "log.h" | |
44 | #include "split.h" | |
45 | #include "syscalls.h" | |
46 | #include "table.h" | |
47 | #include "inputline.h" | |
48 | #include "charset.h" | |
49 | #include "defs.h" | |
50 | #include "mixer.h" | |
51 | #include "printf.h" | |
52 | #include "regsub.h" | |
53 | #include "signame.h" | |
637fdea3 | 54 | #include "authhash.h" |
460b9539 | 55 | |
3f3bb97b RK |
56 | /** @brief Path to config file |
57 | * | |
58 | * set_configfile() sets the deafult if it is null. | |
59 | */ | |
460b9539 | 60 | char *configfile; |
61 | ||
3f3bb97b | 62 | /** @brief Config file parser state */ |
460b9539 | 63 | struct config_state { |
3f3bb97b | 64 | /** @brief Filename */ |
460b9539 | 65 | const char *path; |
3f3bb97b | 66 | /** @brief Line number */ |
460b9539 | 67 | int line; |
3f3bb97b | 68 | /** @brief Configuration object under construction */ |
460b9539 | 69 | struct config *config; |
70 | }; | |
71 | ||
3f3bb97b | 72 | /** @brief Current configuration */ |
460b9539 | 73 | struct config *config; |
74 | ||
3f3bb97b | 75 | /** @brief One configuration item */ |
460b9539 | 76 | struct conf { |
3f3bb97b | 77 | /** @brief Name as it appears in the config file */ |
460b9539 | 78 | const char *name; |
3f3bb97b | 79 | /** @brief Offset in @ref config structure */ |
460b9539 | 80 | size_t offset; |
3f3bb97b | 81 | /** @brief Pointer to item type */ |
460b9539 | 82 | const struct conftype *type; |
3f3bb97b | 83 | /** @brief Pointer to item-specific validation routine */ |
460b9539 | 84 | int (*validate)(const struct config_state *cs, |
85 | int nvec, char **vec); | |
86 | }; | |
87 | ||
3f3bb97b | 88 | /** @brief Type of a configuration item */ |
460b9539 | 89 | struct conftype { |
3f3bb97b | 90 | /** @brief Pointer to function to set item */ |
460b9539 | 91 | int (*set)(const struct config_state *cs, |
92 | const struct conf *whoami, | |
93 | int nvec, char **vec); | |
3f3bb97b | 94 | /** @brief Pointer to function to free item */ |
460b9539 | 95 | void (*free)(struct config *c, const struct conf *whoami); |
96 | }; | |
97 | ||
3f3bb97b | 98 | /** @brief Compute the address of an item */ |
460b9539 | 99 | #define ADDRESS(C, TYPE) ((TYPE *)((char *)(C) + whoami->offset)) |
3f3bb97b | 100 | /** @brief Return the value of an item */ |
460b9539 | 101 | #define VALUE(C, TYPE) (*ADDRESS(C, TYPE)) |
102 | ||
103 | static int set_signal(const struct config_state *cs, | |
104 | const struct conf *whoami, | |
105 | int nvec, char **vec) { | |
106 | int n; | |
107 | ||
108 | if(nvec != 1) { | |
109 | error(0, "%s:%d: '%s' requires one argument", | |
110 | cs->path, cs->line, whoami->name); | |
111 | return -1; | |
112 | } | |
113 | if((n = find_signal(vec[0])) == -1) { | |
114 | error(0, "%s:%d: unknown signal '%s'", | |
115 | cs->path, cs->line, vec[0]); | |
116 | return -1; | |
117 | } | |
118 | VALUE(cs->config, int) = n; | |
119 | return 0; | |
120 | } | |
121 | ||
122 | static int set_collections(const struct config_state *cs, | |
123 | const struct conf *whoami, | |
124 | int nvec, char **vec) { | |
125 | struct collectionlist *cl; | |
126 | ||
127 | if(nvec != 3) { | |
128 | error(0, "%s:%d: '%s' requires three arguments", | |
129 | cs->path, cs->line, whoami->name); | |
130 | return -1; | |
131 | } | |
132 | if(vec[2][0] != '/') { | |
133 | error(0, "%s:%d: collection root must start with '/'", | |
134 | cs->path, cs->line); | |
135 | return -1; | |
136 | } | |
137 | if(vec[2][1] && vec[2][strlen(vec[2])-1] == '/') { | |
138 | error(0, "%s:%d: collection root must not end with '/'", | |
139 | cs->path, cs->line); | |
140 | return -1; | |
141 | } | |
142 | cl = ADDRESS(cs->config, struct collectionlist); | |
143 | ++cl->n; | |
144 | cl->s = xrealloc(cl->s, cl->n * sizeof (struct collection)); | |
145 | cl->s[cl->n - 1].module = xstrdup(vec[0]); | |
146 | cl->s[cl->n - 1].encoding = xstrdup(vec[1]); | |
147 | cl->s[cl->n - 1].root = xstrdup(vec[2]); | |
148 | return 0; | |
149 | } | |
150 | ||
151 | static int set_boolean(const struct config_state *cs, | |
152 | const struct conf *whoami, | |
153 | int nvec, char **vec) { | |
154 | int state; | |
155 | ||
156 | if(nvec != 1) { | |
157 | error(0, "%s:%d: '%s' takes only one argument", | |
158 | cs->path, cs->line, whoami->name); | |
159 | return -1; | |
160 | } | |
161 | if(!strcmp(vec[0], "yes")) state = 1; | |
162 | else if(!strcmp(vec[0], "no")) state = 0; | |
163 | else { | |
164 | error(0, "%s:%d: argument to '%s' must be 'yes' or 'no'", | |
165 | cs->path, cs->line, whoami->name); | |
166 | return -1; | |
167 | } | |
168 | VALUE(cs->config, int) = state; | |
169 | return 0; | |
170 | } | |
171 | ||
172 | static int set_string(const struct config_state *cs, | |
173 | const struct conf *whoami, | |
174 | int nvec, char **vec) { | |
175 | if(nvec != 1) { | |
176 | error(0, "%s:%d: '%s' takes only one argument", | |
177 | cs->path, cs->line, whoami->name); | |
178 | return -1; | |
179 | } | |
180 | VALUE(cs->config, char *) = xstrdup(vec[0]); | |
181 | return 0; | |
182 | } | |
183 | ||
184 | static int set_stringlist(const struct config_state *cs, | |
185 | const struct conf *whoami, | |
186 | int nvec, char **vec) { | |
187 | int n; | |
188 | struct stringlist *sl; | |
189 | ||
190 | sl = ADDRESS(cs->config, struct stringlist); | |
191 | sl->n = 0; | |
192 | for(n = 0; n < nvec; ++n) { | |
193 | sl->n++; | |
194 | sl->s = xrealloc(sl->s, (sl->n * sizeof (char *))); | |
195 | sl->s[sl->n - 1] = xstrdup(vec[n]); | |
196 | } | |
197 | return 0; | |
198 | } | |
199 | ||
200 | static int set_integer(const struct config_state *cs, | |
201 | const struct conf *whoami, | |
202 | int nvec, char **vec) { | |
203 | char *e; | |
204 | ||
205 | if(nvec != 1) { | |
206 | error(0, "%s:%d: '%s' takes only one argument", | |
207 | cs->path, cs->line, whoami->name); | |
208 | return -1; | |
209 | } | |
210 | if(xstrtol(ADDRESS(cs->config, long), vec[0], &e, 0)) { | |
211 | error(errno, "%s:%d: converting integer", cs->path, cs->line); | |
212 | return -1; | |
213 | } | |
214 | if(*e) { | |
215 | error(0, "%s:%d: invalid integer syntax", cs->path, cs->line); | |
216 | return -1; | |
217 | } | |
218 | return 0; | |
219 | } | |
220 | ||
221 | static int set_stringlist_accum(const struct config_state *cs, | |
222 | const struct conf *whoami, | |
223 | int nvec, char **vec) { | |
224 | int n; | |
225 | struct stringlist *s; | |
226 | struct stringlistlist *sll; | |
227 | ||
228 | sll = ADDRESS(cs->config, struct stringlistlist); | |
229 | sll->n++; | |
230 | sll->s = xrealloc(sll->s, (sll->n * sizeof (struct stringlist))); | |
231 | s = &sll->s[sll->n - 1]; | |
232 | s->n = nvec; | |
233 | s->s = xmalloc((nvec + 1) * sizeof (char *)); | |
234 | for(n = 0; n < nvec; ++n) | |
235 | s->s[n] = xstrdup(vec[n]); | |
236 | return 0; | |
237 | } | |
238 | ||
239 | static int set_string_accum(const struct config_state *cs, | |
240 | const struct conf *whoami, | |
241 | int nvec, char **vec) { | |
242 | int n; | |
243 | struct stringlist *sl; | |
244 | ||
245 | sl = ADDRESS(cs->config, struct stringlist); | |
246 | for(n = 0; n < nvec; ++n) { | |
247 | sl->n++; | |
248 | sl->s = xrealloc(sl->s, (sl->n * sizeof (char *))); | |
249 | sl->s[sl->n - 1] = xstrdup(vec[n]); | |
250 | } | |
251 | return 0; | |
252 | } | |
253 | ||
254 | static int set_restrict(const struct config_state *cs, | |
255 | const struct conf *whoami, | |
256 | int nvec, char **vec) { | |
257 | unsigned r = 0; | |
258 | int n, i; | |
259 | ||
260 | static const struct restriction { | |
261 | const char *name; | |
262 | unsigned bit; | |
263 | } restrictions[] = { | |
264 | { "remove", RESTRICT_REMOVE }, | |
265 | { "scratch", RESTRICT_SCRATCH }, | |
266 | { "move", RESTRICT_MOVE }, | |
267 | }; | |
268 | ||
269 | for(n = 0; n < nvec; ++n) { | |
270 | if((i = TABLE_FIND(restrictions, struct restriction, name, vec[n])) < 0) { | |
271 | error(0, "%s:%d: invalid restriction '%s'", | |
272 | cs->path, cs->line, vec[n]); | |
273 | return -1; | |
274 | } | |
275 | r |= restrictions[i].bit; | |
276 | } | |
277 | VALUE(cs->config, unsigned) = r; | |
278 | return 0; | |
279 | } | |
280 | ||
9d5da576 | 281 | static int parse_sample_format(const struct config_state *cs, |
6d2d327c | 282 | struct stream_header *format, |
9d5da576 | 283 | int nvec, char **vec) { |
284 | char *p = vec[0]; | |
285 | long t; | |
286 | ||
6d2d327c | 287 | if(nvec != 1) { |
9d5da576 | 288 | error(0, "%s:%d: wrong number of arguments", cs->path, cs->line); |
289 | return -1; | |
290 | } | |
6d2d327c | 291 | if(xstrtol(&t, p, &p, 0)) { |
9d5da576 | 292 | error(errno, "%s:%d: converting bits-per-sample", cs->path, cs->line); |
293 | return -1; | |
294 | } | |
6d2d327c | 295 | if(t != 8 && t != 16) { |
9d5da576 | 296 | error(0, "%s:%d: bad bite-per-sample (%ld)", cs->path, cs->line, t); |
297 | return -1; | |
298 | } | |
6d2d327c | 299 | if(format) format->bits = t; |
9d5da576 | 300 | switch (*p) { |
6d2d327c RK |
301 | case 'l': case 'L': t = ENDIAN_LITTLE; p++; break; |
302 | case 'b': case 'B': t = ENDIAN_BIG; p++; break; | |
303 | default: t = ENDIAN_NATIVE; break; | |
9d5da576 | 304 | } |
6d2d327c RK |
305 | if(format) format->endian = t; |
306 | if(*p != '/') { | |
9d5da576 | 307 | error(errno, "%s:%d: expected `/' after bits-per-sample", |
308 | cs->path, cs->line); | |
309 | return -1; | |
310 | } | |
311 | p++; | |
6d2d327c | 312 | if(xstrtol(&t, p, &p, 0)) { |
9d5da576 | 313 | error(errno, "%s:%d: converting sample-rate", cs->path, cs->line); |
314 | return -1; | |
315 | } | |
6d2d327c | 316 | if(t < 1 || t > INT_MAX) { |
9d5da576 | 317 | error(0, "%s:%d: silly sample-rate (%ld)", cs->path, cs->line, t); |
318 | return -1; | |
319 | } | |
6d2d327c RK |
320 | if(format) format->rate = t; |
321 | if(*p != '/') { | |
9d5da576 | 322 | error(0, "%s:%d: expected `/' after sample-rate", |
323 | cs->path, cs->line); | |
324 | return -1; | |
325 | } | |
326 | p++; | |
6d2d327c | 327 | if(xstrtol(&t, p, &p, 0)) { |
9d5da576 | 328 | error(errno, "%s:%d: converting channels", cs->path, cs->line); |
329 | return -1; | |
330 | } | |
6d2d327c | 331 | if(t < 1 || t > 8) { |
9d5da576 | 332 | error(0, "%s:%d: silly number (%ld) of channels", cs->path, cs->line, t); |
333 | return -1; | |
334 | } | |
6d2d327c RK |
335 | if(format) format->channels = t; |
336 | if(*p) { | |
9d5da576 | 337 | error(0, "%s:%d: junk after channels", cs->path, cs->line); |
338 | return -1; | |
339 | } | |
340 | return 0; | |
341 | } | |
342 | ||
343 | static int set_sample_format(const struct config_state *cs, | |
344 | const struct conf *whoami, | |
345 | int nvec, char **vec) { | |
6d2d327c | 346 | return parse_sample_format(cs, ADDRESS(cs->config, struct stream_header), |
9d5da576 | 347 | nvec, vec); |
348 | } | |
349 | ||
460b9539 | 350 | static int set_namepart(const struct config_state *cs, |
351 | const struct conf *whoami, | |
352 | int nvec, char **vec) { | |
353 | struct namepartlist *npl = ADDRESS(cs->config, struct namepartlist); | |
354 | unsigned reflags; | |
355 | const char *errstr; | |
356 | int erroffset, n; | |
357 | pcre *re; | |
358 | ||
359 | if(nvec < 3) { | |
360 | error(0, "%s:%d: namepart needs at least 3 arguments", cs->path, cs->line); | |
361 | return -1; | |
362 | } | |
363 | if(nvec > 5) { | |
364 | error(0, "%s:%d: namepart needs at most 5 arguments", cs->path, cs->line); | |
365 | return -1; | |
366 | } | |
367 | reflags = nvec >= 5 ? regsub_flags(vec[4]) : 0; | |
368 | if(!(re = pcre_compile(vec[1], | |
369 | PCRE_UTF8 | |
370 | |regsub_compile_options(reflags), | |
371 | &errstr, &erroffset, 0))) { | |
372 | error(0, "%s:%d: error compiling regexp /%s/: %s (offset %d)", | |
373 | cs->path, cs->line, vec[1], errstr, erroffset); | |
374 | return -1; | |
375 | } | |
376 | npl->s = xrealloc(npl->s, (npl->n + 1) * sizeof (struct namepart)); | |
377 | npl->s[npl->n].part = xstrdup(vec[0]); | |
378 | npl->s[npl->n].re = re; | |
379 | npl->s[npl->n].replace = xstrdup(vec[2]); | |
380 | npl->s[npl->n].context = xstrdup(vec[3]); | |
381 | npl->s[npl->n].reflags = reflags; | |
382 | ++npl->n; | |
383 | /* XXX a bit of a bodge; relies on there being very few parts. */ | |
384 | for(n = 0; (n < cs->config->nparts | |
385 | && strcmp(cs->config->parts[n], vec[0])); ++n) | |
386 | ; | |
387 | if(n >= cs->config->nparts) { | |
388 | cs->config->parts = xrealloc(cs->config->parts, | |
389 | (cs->config->nparts + 1) * sizeof (char *)); | |
390 | cs->config->parts[cs->config->nparts++] = xstrdup(vec[0]); | |
391 | } | |
392 | return 0; | |
393 | } | |
394 | ||
395 | static int set_transform(const struct config_state *cs, | |
396 | const struct conf *whoami, | |
397 | int nvec, char **vec) { | |
398 | struct transformlist *tl = ADDRESS(cs->config, struct transformlist); | |
399 | pcre *re; | |
400 | unsigned reflags; | |
401 | const char *errstr; | |
402 | int erroffset; | |
403 | ||
404 | if(nvec < 3) { | |
405 | error(0, "%s:%d: transform needs at least 3 arguments", cs->path, cs->line); | |
406 | return -1; | |
407 | } | |
408 | if(nvec > 5) { | |
409 | error(0, "%s:%d: transform needs at most 5 arguments", cs->path, cs->line); | |
410 | return -1; | |
411 | } | |
412 | reflags = (nvec >= 5 ? regsub_flags(vec[4]) : 0); | |
413 | if(!(re = pcre_compile(vec[1], | |
414 | PCRE_UTF8 | |
415 | |regsub_compile_options(reflags), | |
416 | &errstr, &erroffset, 0))) { | |
417 | error(0, "%s:%d: error compiling regexp /%s/: %s (offset %d)", | |
418 | cs->path, cs->line, vec[1], errstr, erroffset); | |
419 | return -1; | |
420 | } | |
421 | tl->t = xrealloc(tl->t, (tl->n + 1) * sizeof (struct namepart)); | |
422 | tl->t[tl->n].type = xstrdup(vec[0]); | |
423 | tl->t[tl->n].context = xstrdup(vec[3] ? vec[3] : "*"); | |
424 | tl->t[tl->n].re = re; | |
425 | tl->t[tl->n].replace = xstrdup(vec[2]); | |
426 | tl->t[tl->n].flags = reflags; | |
427 | ++tl->n; | |
428 | return 0; | |
429 | } | |
430 | ||
e83d0967 RK |
431 | static int set_backend(const struct config_state *cs, |
432 | const struct conf *whoami, | |
433 | int nvec, char **vec) { | |
434 | int *const valuep = ADDRESS(cs->config, int); | |
435 | ||
436 | if(nvec != 1) { | |
437 | error(0, "%s:%d: '%s' requires one argument", | |
438 | cs->path, cs->line, whoami->name); | |
439 | return -1; | |
440 | } | |
441 | if(!strcmp(vec[0], "alsa")) { | |
146e86fb | 442 | #if HAVE_ALSA_ASOUNDLIB_H |
e83d0967 RK |
443 | *valuep = BACKEND_ALSA; |
444 | #else | |
445 | error(0, "%s:%d: ALSA is not available on this platform", | |
446 | cs->path, cs->line); | |
447 | return -1; | |
448 | #endif | |
449 | } else if(!strcmp(vec[0], "command")) | |
450 | *valuep = BACKEND_COMMAND; | |
451 | else if(!strcmp(vec[0], "network")) | |
452 | *valuep = BACKEND_NETWORK; | |
937be4c0 RK |
453 | else if(!strcmp(vec[0], "coreaudio")) { |
454 | #if HAVE_COREAUDIO_AUDIOHARDWARE_H | |
455 | *valuep = BACKEND_COREAUDIO; | |
456 | #else | |
457 | error(0, "%s:%d: Core Audio is not available on this platform", | |
458 | cs->path, cs->line); | |
459 | return -1; | |
e99d42b1 | 460 | #endif |
461 | } else if(!strcmp(vec[0], "oss")) { | |
462 | #if HAVE_SYS_SOUNDCARD_H | |
463 | *valuep = BACKEND_OSS; | |
464 | #else | |
465 | error(0, "%s:%d: OSS is not available on this platform", | |
466 | cs->path, cs->line); | |
467 | return -1; | |
937be4c0 RK |
468 | #endif |
469 | } else { | |
e83d0967 RK |
470 | error(0, "%s:%d: invalid '%s' value '%s'", |
471 | cs->path, cs->line, whoami->name, vec[0]); | |
472 | return -1; | |
473 | } | |
474 | return 0; | |
475 | } | |
476 | ||
460b9539 | 477 | /* free functions */ |
478 | ||
479 | static void free_none(struct config attribute((unused)) *c, | |
480 | const struct conf attribute((unused)) *whoami) { | |
481 | } | |
482 | ||
483 | static void free_string(struct config *c, | |
484 | const struct conf *whoami) { | |
485 | xfree(VALUE(c, char *)); | |
486 | } | |
487 | ||
488 | static void free_stringlist(struct config *c, | |
489 | const struct conf *whoami) { | |
490 | int n; | |
491 | struct stringlist *sl = ADDRESS(c, struct stringlist); | |
492 | ||
493 | for(n = 0; n < sl->n; ++n) | |
494 | xfree(sl->s[n]); | |
495 | xfree(sl->s); | |
496 | } | |
497 | ||
498 | static void free_stringlistlist(struct config *c, | |
499 | const struct conf *whoami) { | |
500 | int n, m; | |
501 | struct stringlistlist *sll = ADDRESS(c, struct stringlistlist); | |
502 | struct stringlist *sl; | |
503 | ||
504 | for(n = 0; n < sll->n; ++n) { | |
505 | sl = &sll->s[n]; | |
506 | for(m = 0; m < sl->n; ++m) | |
507 | xfree(sl->s[m]); | |
508 | xfree(sl->s); | |
509 | } | |
510 | xfree(sll->s); | |
511 | } | |
512 | ||
513 | static void free_collectionlist(struct config *c, | |
514 | const struct conf *whoami) { | |
515 | struct collectionlist *cll = ADDRESS(c, struct collectionlist); | |
516 | struct collection *cl; | |
517 | int n; | |
518 | ||
519 | for(n = 0; n < cll->n; ++n) { | |
520 | cl = &cll->s[n]; | |
521 | xfree(cl->module); | |
522 | xfree(cl->encoding); | |
523 | xfree(cl->root); | |
524 | } | |
525 | xfree(cll->s); | |
526 | } | |
527 | ||
528 | static void free_namepartlist(struct config *c, | |
529 | const struct conf *whoami) { | |
530 | struct namepartlist *npl = ADDRESS(c, struct namepartlist); | |
531 | struct namepart *np; | |
532 | int n; | |
533 | ||
534 | for(n = 0; n < npl->n; ++n) { | |
535 | np = &npl->s[n]; | |
536 | xfree(np->part); | |
537 | pcre_free(np->re); /* ...whatever pcre_free is set to. */ | |
538 | xfree(np->replace); | |
539 | xfree(np->context); | |
540 | } | |
541 | xfree(npl->s); | |
542 | } | |
543 | ||
544 | static void free_transformlist(struct config *c, | |
545 | const struct conf *whoami) { | |
546 | struct transformlist *tl = ADDRESS(c, struct transformlist); | |
547 | struct transform *t; | |
548 | int n; | |
549 | ||
550 | for(n = 0; n < tl->n; ++n) { | |
551 | t = &tl->t[n]; | |
552 | xfree(t->type); | |
553 | pcre_free(t->re); /* ...whatever pcre_free is set to. */ | |
554 | xfree(t->replace); | |
555 | xfree(t->context); | |
556 | } | |
557 | xfree(tl->t); | |
558 | } | |
559 | ||
560 | /* configuration types */ | |
561 | ||
562 | static const struct conftype | |
563 | type_signal = { set_signal, free_none }, | |
564 | type_collections = { set_collections, free_collectionlist }, | |
565 | type_boolean = { set_boolean, free_none }, | |
566 | type_string = { set_string, free_string }, | |
567 | type_stringlist = { set_stringlist, free_stringlist }, | |
568 | type_integer = { set_integer, free_none }, | |
569 | type_stringlist_accum = { set_stringlist_accum, free_stringlistlist }, | |
570 | type_string_accum = { set_string_accum, free_stringlist }, | |
9d5da576 | 571 | type_sample_format = { set_sample_format, free_none }, |
460b9539 | 572 | type_restrict = { set_restrict, free_none }, |
573 | type_namepart = { set_namepart, free_namepartlist }, | |
e83d0967 RK |
574 | type_transform = { set_transform, free_transformlist }, |
575 | type_backend = { set_backend, free_none }; | |
460b9539 | 576 | |
577 | /* specific validation routine */ | |
578 | ||
579 | #define VALIDATE_FILE(test, what) do { \ | |
580 | struct stat sb; \ | |
581 | int n; \ | |
582 | \ | |
583 | for(n = 0; n < nvec; ++n) { \ | |
584 | if(stat(vec[n], &sb) < 0) { \ | |
585 | error(errno, "%s:%d: %s", cs->path, cs->line, vec[n]); \ | |
586 | return -1; \ | |
587 | } \ | |
588 | if(!test(sb.st_mode)) { \ | |
589 | error(0, "%s:%d: %s is not a %s", \ | |
590 | cs->path, cs->line, vec[n], what); \ | |
591 | return -1; \ | |
592 | } \ | |
593 | } \ | |
594 | } while(0) | |
595 | ||
659d87e8 RK |
596 | static int validate_isabspath(const struct config_state *cs, |
597 | int nvec, char **vec) { | |
598 | int n; | |
599 | ||
600 | for(n = 0; n < nvec; ++n) | |
601 | if(vec[n][0] != '/') { | |
602 | error(errno, "%s:%d: %s: not an absolute path", | |
603 | cs->path, cs->line, vec[n]); | |
604 | return -1; | |
605 | } | |
606 | return 0; | |
607 | } | |
608 | ||
460b9539 | 609 | static int validate_isdir(const struct config_state *cs, |
610 | int nvec, char **vec) { | |
611 | VALIDATE_FILE(S_ISDIR, "directory"); | |
612 | return 0; | |
613 | } | |
614 | ||
615 | static int validate_isreg(const struct config_state *cs, | |
616 | int nvec, char **vec) { | |
617 | VALIDATE_FILE(S_ISREG, "regular file"); | |
618 | return 0; | |
619 | } | |
620 | ||
621 | static int validate_ischr(const struct config_state *cs, | |
622 | int nvec, char **vec) { | |
623 | VALIDATE_FILE(S_ISCHR, "character device"); | |
624 | return 0; | |
625 | } | |
626 | ||
627 | static int validate_player(const struct config_state *cs, | |
628 | int nvec, | |
629 | char attribute((unused)) **vec) { | |
630 | if(nvec < 2) { | |
631 | error(0, "%s:%d: should be at least 'player PATTERN MODULE'", | |
632 | cs->path, cs->line); | |
633 | return -1; | |
634 | } | |
635 | return 0; | |
636 | } | |
637 | ||
62dc3748 RK |
638 | static int validate_tracklength(const struct config_state *cs, |
639 | int nvec, | |
640 | char attribute((unused)) **vec) { | |
641 | if(nvec < 2) { | |
642 | error(0, "%s:%d: should be at least 'tracklength PATTERN MODULE'", | |
643 | cs->path, cs->line); | |
644 | return -1; | |
645 | } | |
646 | return 0; | |
647 | } | |
648 | ||
460b9539 | 649 | static int validate_allow(const struct config_state *cs, |
650 | int nvec, | |
651 | char attribute((unused)) **vec) { | |
652 | if(nvec != 2) { | |
653 | error(0, "%s:%d: must be 'allow NAME PASS'", cs->path, cs->line); | |
654 | return -1; | |
655 | } | |
656 | return 0; | |
657 | } | |
658 | ||
659 | static int validate_non_negative(const struct config_state *cs, | |
660 | int nvec, char **vec) { | |
661 | long n; | |
662 | ||
663 | if(nvec < 1) { | |
664 | error(0, "%s:%d: missing argument", cs->path, cs->line); | |
665 | return -1; | |
666 | } | |
667 | if(nvec > 1) { | |
668 | error(0, "%s:%d: too many arguments", cs->path, cs->line); | |
669 | return -1; | |
670 | } | |
671 | if(xstrtol(&n, vec[0], 0, 0)) { | |
672 | error(0, "%s:%d: %s", cs->path, cs->line, strerror(errno)); | |
673 | return -1; | |
674 | } | |
675 | if(n < 0) { | |
676 | error(0, "%s:%d: must not be negative", cs->path, cs->line); | |
677 | return -1; | |
678 | } | |
679 | return 0; | |
680 | } | |
681 | ||
682 | static int validate_positive(const struct config_state *cs, | |
683 | int nvec, char **vec) { | |
684 | long n; | |
685 | ||
686 | if(nvec < 1) { | |
687 | error(0, "%s:%d: missing argument", cs->path, cs->line); | |
688 | return -1; | |
689 | } | |
690 | if(nvec > 1) { | |
691 | error(0, "%s:%d: too many arguments", cs->path, cs->line); | |
692 | return -1; | |
693 | } | |
694 | if(xstrtol(&n, vec[0], 0, 0)) { | |
695 | error(0, "%s:%d: %s", cs->path, cs->line, strerror(errno)); | |
696 | return -1; | |
697 | } | |
698 | if(n <= 0) { | |
699 | error(0, "%s:%d: must be positive", cs->path, cs->line); | |
700 | return -1; | |
701 | } | |
702 | return 0; | |
703 | } | |
704 | ||
705 | static int validate_isauser(const struct config_state *cs, | |
706 | int attribute((unused)) nvec, | |
707 | char **vec) { | |
708 | struct passwd *pw; | |
709 | ||
710 | if(!(pw = getpwnam(vec[0]))) { | |
711 | error(0, "%s:%d: no such user as '%s'", cs->path, cs->line, vec[0]); | |
712 | return -1; | |
713 | } | |
714 | return 0; | |
715 | } | |
716 | ||
9d5da576 | 717 | static int validate_sample_format(const struct config_state *cs, |
718 | int attribute((unused)) nvec, | |
719 | char **vec) { | |
720 | return parse_sample_format(cs, 0, nvec, vec); | |
721 | } | |
722 | ||
460b9539 | 723 | static int validate_channel(const struct config_state *cs, |
724 | int attribute((unused)) nvec, | |
725 | char **vec) { | |
726 | if(mixer_channel(vec[0]) == -1) { | |
727 | error(0, "%s:%d: invalid channel '%s'", cs->path, cs->line, vec[0]); | |
728 | return -1; | |
729 | } | |
730 | return 0; | |
731 | } | |
732 | ||
733 | static int validate_any(const struct config_state attribute((unused)) *cs, | |
734 | int attribute((unused)) nvec, | |
735 | char attribute((unused)) **vec) { | |
736 | return 0; | |
737 | } | |
738 | ||
739 | static int validate_url(const struct config_state attribute((unused)) *cs, | |
740 | int attribute((unused)) nvec, | |
741 | char **vec) { | |
742 | const char *s; | |
743 | int n; | |
744 | /* absoluteURI = scheme ":" ( hier_part | opaque_part ) | |
745 | scheme = alpha *( alpha | digit | "+" | "-" | "." ) */ | |
746 | s = vec[0]; | |
747 | n = strspn(s, ("abcdefghijklmnopqrstuvwxyz" | |
748 | "ABCDEFGHIJKLMNOPQRSTUVWXYZ" | |
749 | "0123456789")); | |
750 | if(s[n] != ':') { | |
751 | error(0, "%s:%d: invalid url '%s'", cs->path, cs->line, vec[0]); | |
752 | return -1; | |
753 | } | |
754 | if(!strncmp(s, "http:", 5) | |
755 | || !strncmp(s, "https:", 6)) { | |
756 | s += n + 1; | |
757 | /* we only do a rather cursory check */ | |
758 | if(strncmp(s, "//", 2)) { | |
759 | error(0, "%s:%d: invalid url '%s'", cs->path, cs->line, vec[0]); | |
760 | return -1; | |
761 | } | |
762 | } | |
763 | return 0; | |
764 | } | |
765 | ||
766 | static int validate_alias(const struct config_state *cs, | |
767 | int nvec, | |
768 | char **vec) { | |
769 | const char *s; | |
770 | int in_brackets = 0, c; | |
771 | ||
772 | if(nvec < 1) { | |
773 | error(0, "%s:%d: missing argument", cs->path, cs->line); | |
774 | return -1; | |
775 | } | |
776 | if(nvec > 1) { | |
777 | error(0, "%s:%d: too many arguments", cs->path, cs->line); | |
778 | return -1; | |
779 | } | |
780 | s = vec[0]; | |
781 | while((c = (unsigned char)*s++)) { | |
782 | if(in_brackets) { | |
783 | if(c == '}') | |
784 | in_brackets = 0; | |
785 | else if(!isalnum(c)) { | |
786 | error(0, "%s:%d: invalid part name in alias expansion in '%s'", | |
787 | cs->path, cs->line, vec[0]); | |
788 | return -1; | |
789 | } | |
790 | } else { | |
791 | if(c == '{') { | |
792 | in_brackets = 1; | |
793 | if(*s == '/') | |
794 | ++s; | |
795 | } else if(c == '\\') { | |
796 | if(!(c = (unsigned char)*s++)) { | |
797 | error(0, "%s:%d: unterminated escape in alias expansion in '%s'", | |
798 | cs->path, cs->line, vec[0]); | |
799 | return -1; | |
800 | } else if(c != '\\' && c != '{') { | |
801 | error(0, "%s:%d: invalid escape in alias expansion in '%s'", | |
802 | cs->path, cs->line, vec[0]); | |
803 | return -1; | |
804 | } | |
805 | } | |
806 | } | |
807 | ++s; | |
808 | } | |
809 | if(in_brackets) { | |
810 | error(0, "%s:%d: unterminated part name in alias expansion in '%s'", | |
811 | cs->path, cs->line, vec[0]); | |
812 | return -1; | |
813 | } | |
814 | return 0; | |
815 | } | |
816 | ||
e83d0967 RK |
817 | static int validate_addrport(const struct config_state attribute((unused)) *cs, |
818 | int nvec, | |
819 | char attribute((unused)) **vec) { | |
820 | switch(nvec) { | |
821 | case 0: | |
822 | error(0, "%s:%d: missing address", | |
823 | cs->path, cs->line); | |
824 | return -1; | |
825 | case 1: | |
826 | error(0, "%s:%d: missing port name/number", | |
827 | cs->path, cs->line); | |
828 | return -1; | |
829 | case 2: | |
830 | return 0; | |
831 | default: | |
832 | error(0, "%s:%d: expected ADDRESS PORT", | |
833 | cs->path, cs->line); | |
834 | return -1; | |
835 | } | |
836 | } | |
837 | ||
ccf0aafa | 838 | static int validate_port(const struct config_state attribute((unused)) *cs, |
e83d0967 RK |
839 | int nvec, |
840 | char attribute((unused)) **vec) { | |
841 | switch(nvec) { | |
842 | case 0: | |
843 | error(0, "%s:%d: missing address", | |
844 | cs->path, cs->line); | |
845 | return -1; | |
846 | case 1: | |
847 | case 2: | |
848 | return 0; | |
849 | default: | |
ccf0aafa | 850 | error(0, "%s:%d: expected [ADDRESS] PORT", |
e83d0967 RK |
851 | cs->path, cs->line); |
852 | return -1; | |
853 | } | |
854 | } | |
855 | ||
637fdea3 RK |
856 | static int validate_algo(const struct config_state attribute((unused)) *cs, |
857 | int nvec, | |
858 | char **vec) { | |
859 | if(nvec != 1) { | |
860 | error(0, "%s:%d: invalid algorithm specification", cs->path, cs->line); | |
861 | return -1; | |
862 | } | |
863 | if(!valid_authhash(vec[0])) { | |
864 | error(0, "%s:%d: unsuported algorithm '%s'", cs->path, cs->line, vec[0]); | |
865 | return -1; | |
866 | } | |
867 | return 0; | |
868 | } | |
869 | ||
3f3bb97b | 870 | /** @brief Item name and and offset */ |
460b9539 | 871 | #define C(x) #x, offsetof(struct config, x) |
3f3bb97b | 872 | /** @brief Item name and and offset */ |
460b9539 | 873 | #define C2(x,y) #x, offsetof(struct config, y) |
874 | ||
3f3bb97b | 875 | /** @brief All configuration items */ |
460b9539 | 876 | static const struct conf conf[] = { |
877 | { C(alias), &type_string, validate_alias }, | |
878 | { C(allow), &type_stringlist_accum, validate_allow }, | |
637fdea3 | 879 | { C(authorization_algorithm), &type_string, validate_algo }, |
e83d0967 | 880 | { C(broadcast), &type_stringlist, validate_addrport }, |
ccf0aafa | 881 | { C(broadcast_from), &type_stringlist, validate_addrport }, |
460b9539 | 882 | { C(channel), &type_string, validate_channel }, |
883 | { C(checkpoint_kbyte), &type_integer, validate_non_negative }, | |
884 | { C(checkpoint_min), &type_integer, validate_non_negative }, | |
885 | { C(collection), &type_collections, validate_any }, | |
e83d0967 | 886 | { C(connect), &type_stringlist, validate_addrport }, |
460b9539 | 887 | { C(device), &type_string, validate_any }, |
888 | { C(gap), &type_integer, validate_non_negative }, | |
889 | { C(history), &type_integer, validate_positive }, | |
659d87e8 | 890 | { C(home), &type_string, validate_isabspath }, |
ccf0aafa | 891 | { C(listen), &type_stringlist, validate_port }, |
460b9539 | 892 | { C(lock), &type_boolean, validate_any }, |
893 | { C(mixer), &type_string, validate_ischr }, | |
23205f9c | 894 | { C(multicast_ttl), &type_integer, validate_non_negative }, |
460b9539 | 895 | { C(namepart), &type_namepart, validate_any }, |
896 | { C2(nice, nice_rescan), &type_integer, validate_non_negative }, | |
897 | { C(nice_rescan), &type_integer, validate_non_negative }, | |
898 | { C(nice_server), &type_integer, validate_any }, | |
899 | { C(nice_speaker), &type_integer, validate_any }, | |
2a10b70b | 900 | { C(noticed_history), &type_integer, validate_positive }, |
460b9539 | 901 | { C(password), &type_string, validate_any }, |
902 | { C(player), &type_stringlist_accum, validate_player }, | |
903 | { C(plugins), &type_string_accum, validate_isdir }, | |
904 | { C(prefsync), &type_integer, validate_positive }, | |
459d4402 | 905 | { C(queue_pad), &type_integer, validate_positive }, |
460b9539 | 906 | { C(refresh), &type_integer, validate_positive }, |
907 | { C2(restrict, restrictions), &type_restrict, validate_any }, | |
9d5da576 | 908 | { C(sample_format), &type_sample_format, validate_sample_format }, |
460b9539 | 909 | { C(scratch), &type_string_accum, validate_isreg }, |
61507e3c | 910 | { C(short_display), &type_integer, validate_positive }, |
460b9539 | 911 | { C(signal), &type_signal, validate_any }, |
5330d674 | 912 | { C(sox_generation), &type_integer, validate_non_negative }, |
e83d0967 | 913 | { C(speaker_backend), &type_backend, validate_any }, |
9d5da576 | 914 | { C(speaker_command), &type_string, validate_any }, |
460b9539 | 915 | { C(stopword), &type_string_accum, validate_any }, |
916 | { C(templates), &type_string_accum, validate_isdir }, | |
62dc3748 | 917 | { C(tracklength), &type_stringlist_accum, validate_tracklength }, |
460b9539 | 918 | { C(transform), &type_transform, validate_any }, |
919 | { C(trust), &type_string_accum, validate_any }, | |
920 | { C(url), &type_string, validate_url }, | |
921 | { C(user), &type_string, validate_isauser }, | |
922 | { C(username), &type_string, validate_any }, | |
923 | }; | |
924 | ||
3f3bb97b | 925 | /** @brief Find a configuration item's definition by key */ |
460b9539 | 926 | static const struct conf *find(const char *key) { |
927 | int n; | |
928 | ||
929 | if((n = TABLE_FIND(conf, struct conf, name, key)) < 0) | |
930 | return 0; | |
931 | return &conf[n]; | |
932 | } | |
933 | ||
3f3bb97b | 934 | /** @brief Set a new configuration value */ |
460b9539 | 935 | static int config_set(const struct config_state *cs, |
936 | int nvec, char **vec) { | |
937 | const struct conf *which; | |
938 | ||
939 | D(("config_set %s", vec[0])); | |
940 | if(!(which = find(vec[0]))) { | |
941 | error(0, "%s:%d: unknown configuration key '%s'", | |
942 | cs->path, cs->line, vec[0]); | |
943 | return -1; | |
944 | } | |
945 | return (which->validate(cs, nvec - 1, vec + 1) | |
946 | || which->type->set(cs, which, nvec - 1, vec + 1)); | |
947 | } | |
948 | ||
0e4472a0 | 949 | /** @brief Error callback used by config_include() */ |
460b9539 | 950 | static void config_error(const char *msg, void *u) { |
951 | const struct config_state *cs = u; | |
952 | ||
953 | error(0, "%s:%d: %s", cs->path, cs->line, msg); | |
954 | } | |
955 | ||
3f3bb97b | 956 | /** @brief Include a file by name */ |
460b9539 | 957 | static int config_include(struct config *c, const char *path) { |
958 | FILE *fp; | |
959 | char *buffer, *inputbuffer, **vec; | |
960 | int n, ret = 0; | |
961 | struct config_state cs; | |
962 | ||
963 | cs.path = path; | |
964 | cs.line = 0; | |
965 | cs.config = c; | |
966 | D(("%s: reading configuration", path)); | |
967 | if(!(fp = fopen(path, "r"))) { | |
968 | error(errno, "error opening %s", path); | |
969 | return -1; | |
970 | } | |
971 | while(!inputline(path, fp, &inputbuffer, '\n')) { | |
972 | ++cs.line; | |
973 | if(!(buffer = mb2utf8(inputbuffer))) { | |
974 | error(errno, "%s:%d: cannot convert to UTF-8", cs.path, cs.line); | |
975 | ret = -1; | |
976 | xfree(inputbuffer); | |
977 | continue; | |
978 | } | |
979 | xfree(inputbuffer); | |
980 | if(!(vec = split(buffer, &n, SPLIT_COMMENTS|SPLIT_QUOTES, | |
981 | config_error, &cs))) { | |
982 | ret = -1; | |
983 | xfree(buffer); | |
984 | continue; | |
985 | } | |
986 | if(n) { | |
987 | if(!strcmp(vec[0], "include")) { | |
988 | if(n != 2) { | |
989 | error(0, "%s:%d: must be 'include PATH'", cs.path, cs.line); | |
990 | ret = -1; | |
991 | } else | |
992 | config_include(c, vec[1]); | |
993 | } else | |
994 | ret |= config_set(&cs, n, vec); | |
995 | } | |
996 | for(n = 0; vec[n]; ++n) xfree(vec[n]); | |
997 | xfree(vec); | |
998 | xfree(buffer); | |
999 | } | |
1000 | if(ferror(fp)) { | |
1001 | error(errno, "error reading %s", path); | |
1002 | ret = -1; | |
1003 | } | |
1004 | fclose(fp); | |
1005 | return ret; | |
1006 | } | |
1007 | ||
3f3bb97b | 1008 | /** @brief Make a new default configuration */ |
460b9539 | 1009 | static struct config *config_default(void) { |
1010 | struct config *c = xmalloc(sizeof *c); | |
1011 | const char *logname; | |
1012 | struct passwd *pw; | |
1013 | ||
1014 | /* Strings had better be xstrdup'd as they will get freed at some point. */ | |
1015 | c->gap = 2; | |
1016 | c->history = 60; | |
1017 | c->home = xstrdup(pkgstatedir); | |
1018 | if(!(pw = getpwuid(getuid()))) | |
1019 | fatal(0, "cannot determine our username"); | |
1020 | logname = pw->pw_name; | |
1021 | c->username = xstrdup(logname); | |
1022 | c->refresh = 15; | |
1023 | c->prefsync = 3600; | |
1024 | c->signal = SIGKILL; | |
1025 | c->alias = xstrdup("{/artist}{/album}{/title}{ext}"); | |
1026 | c->lock = 1; | |
1027 | c->device = xstrdup("default"); | |
1028 | c->nice_rescan = 10; | |
9d5da576 | 1029 | c->speaker_command = 0; |
1030 | c->sample_format.bits = 16; | |
1031 | c->sample_format.rate = 44100; | |
1032 | c->sample_format.channels = 2; | |
6d2d327c | 1033 | c->sample_format.endian = ENDIAN_NATIVE; |
459d4402 | 1034 | c->queue_pad = 10; |
e83d0967 | 1035 | c->speaker_backend = -1; |
23205f9c | 1036 | c->multicast_ttl = 1; |
637fdea3 | 1037 | c->authorization_algorithm = xstrdup("sha1"); |
2a10b70b | 1038 | c->noticed_history = 31; |
61507e3c | 1039 | c->short_display = 32; |
321f7536 RK |
1040 | c->mixer = xstrdup("/dev/mixer"); |
1041 | c->channel = xstrdup("pcm"); | |
460b9539 | 1042 | return c; |
1043 | } | |
1044 | ||
1045 | static char *get_file(struct config *c, const char *name) { | |
1046 | char *s; | |
1047 | ||
1048 | byte_xasprintf(&s, "%s/%s", c->home, name); | |
1049 | return s; | |
1050 | } | |
1051 | ||
3f3bb97b | 1052 | /** @brief Set the default configuration file */ |
460b9539 | 1053 | static void set_configfile(void) { |
1054 | if(!configfile) | |
1055 | byte_xasprintf(&configfile, "%s/config", pkgconfdir); | |
1056 | } | |
1057 | ||
3f3bb97b | 1058 | /** @brief Free a configuration object */ |
460b9539 | 1059 | static void config_free(struct config *c) { |
1060 | int n; | |
1061 | ||
1062 | if(c) { | |
1063 | for(n = 0; n < (int)(sizeof conf / sizeof *conf); ++n) | |
1064 | conf[n].type->free(c, &conf[n]); | |
1065 | for(n = 0; n < c->nparts; ++n) | |
1066 | xfree(c->parts[n]); | |
1067 | xfree(c->parts); | |
1068 | xfree(c); | |
1069 | } | |
1070 | } | |
1071 | ||
3f3bb97b | 1072 | /** @brief Set post-parse defaults */ |
c00fce3a RK |
1073 | static void config_postdefaults(struct config *c, |
1074 | int server) { | |
460b9539 | 1075 | struct config_state cs; |
1076 | const struct conf *whoami; | |
1077 | int n; | |
1078 | ||
1079 | static const char *namepart[][4] = { | |
88608992 | 1080 | { "title", "/([0-9]+ *[-:] *)?([^/]+)\\.[a-zA-Z0-9]+$", "$2", "display" }, |
460b9539 | 1081 | { "title", "/([^/]+)\\.[a-zA-Z0-9]+$", "$1", "sort" }, |
1082 | { "album", "/([^/]+)/[^/]+$", "$1", "*" }, | |
1083 | { "artist", "/([^/]+)/[^/]+/[^/]+$", "$1", "*" }, | |
1084 | { "ext", "(\\.[a-zA-Z0-9]+)$", "$1", "*" }, | |
1085 | }; | |
1086 | #define NNAMEPART (int)(sizeof namepart / sizeof *namepart) | |
1087 | ||
1088 | static const char *transform[][5] = { | |
88608992 | 1089 | { "track", "^.*/([0-9]+ *[-:] *)?([^/]+)\\.[a-zA-Z0-9]+$", "$2", "display", "" }, |
460b9539 | 1090 | { "track", "^.*/([^/]+)\\.[a-zA-Z0-9]+$", "$1", "sort", "" }, |
1091 | { "dir", "^.*/([^/]+)$", "$1", "*", "" }, | |
1092 | { "dir", "^(the) ([^/]*)", "$2, $1", "sort", "i", }, | |
1093 | { "dir", "[[:punct:]]", "", "sort", "g", } | |
1094 | }; | |
1095 | #define NTRANSFORM (int)(sizeof transform / sizeof *transform) | |
1096 | ||
1097 | cs.path = "<internal>"; | |
1098 | cs.line = 0; | |
1099 | cs.config = c; | |
1100 | if(!c->namepart.n) { | |
1101 | whoami = find("namepart"); | |
1102 | for(n = 0; n < NNAMEPART; ++n) | |
1103 | set_namepart(&cs, whoami, 4, (char **)namepart[n]); | |
1104 | } | |
1105 | if(!c->transform.n) { | |
1106 | whoami = find("transform"); | |
1107 | for(n = 0; n < NTRANSFORM; ++n) | |
1108 | set_transform(&cs, whoami, 5, (char **)transform[n]); | |
1109 | } | |
e83d0967 RK |
1110 | if(c->speaker_backend == -1) { |
1111 | if(c->speaker_command) | |
1112 | c->speaker_backend = BACKEND_COMMAND; | |
1113 | else if(c->broadcast.n) | |
1114 | c->speaker_backend = BACKEND_NETWORK; | |
1115 | else { | |
146e86fb | 1116 | #if HAVE_ALSA_ASOUNDLIB_H |
e83d0967 | 1117 | c->speaker_backend = BACKEND_ALSA; |
146e86fb | 1118 | #elif HAVE_SYS_SOUNDCARD_H |
1119 | c->speaker_backend = BACKEND_OSS; | |
937be4c0 RK |
1120 | #elif HAVE_COREAUDIO_AUDIOHARDWARE_H |
1121 | c->speaker_backend = BACKEND_COREAUDIO; | |
e83d0967 RK |
1122 | #else |
1123 | c->speaker_backend = BACKEND_COMMAND; | |
1124 | #endif | |
1125 | } | |
1126 | } | |
c00fce3a RK |
1127 | if(server) { |
1128 | if(c->speaker_backend == BACKEND_COMMAND && !c->speaker_command) | |
1129 | fatal(0, "speaker_backend is command but speaker_command is not set"); | |
1130 | if(c->speaker_backend == BACKEND_NETWORK && !c->broadcast.n) | |
1131 | fatal(0, "speaker_backend is network but broadcast is not set"); | |
1132 | } | |
e99d42b1 | 1133 | /* Override sample format */ |
1134 | switch(c->speaker_backend) { | |
1135 | case BACKEND_NETWORK: | |
6d2d327c RK |
1136 | c->sample_format.rate = 44100; |
1137 | c->sample_format.channels = 2; | |
1138 | c->sample_format.bits = 16; | |
1139 | c->sample_format.endian = ENDIAN_BIG; | |
e99d42b1 | 1140 | break; |
1141 | case BACKEND_COREAUDIO: | |
937be4c0 RK |
1142 | c->sample_format.rate = 44100; |
1143 | c->sample_format.channels = 2; | |
1144 | c->sample_format.bits = 16; | |
1145 | c->sample_format.endian = ENDIAN_NATIVE; | |
e99d42b1 | 1146 | break; |
1147 | } | |
460b9539 | 1148 | } |
1149 | ||
c00fce3a RK |
1150 | /** @brief (Re-)read the config file |
1151 | * @param server If set, do extra checking | |
1152 | */ | |
1153 | int config_read(int server) { | |
460b9539 | 1154 | struct config *c; |
1155 | char *privconf; | |
1156 | struct passwd *pw; | |
1157 | ||
1158 | set_configfile(); | |
1159 | c = config_default(); | |
9ade2319 | 1160 | /* standalone Disobedience installs might not have a global config file */ |
1161 | if(access(configfile, F_OK) == 0) | |
1162 | if(config_include(c, configfile)) | |
1163 | return -1; | |
460b9539 | 1164 | /* if we can read the private config file, do */ |
1165 | if((privconf = config_private()) | |
1166 | && access(privconf, R_OK) == 0 | |
1167 | && config_include(c, privconf)) | |
1168 | return -1; | |
1169 | xfree(privconf); | |
1170 | /* if there's a per-user system config file for this user, read it */ | |
1171 | if(!(pw = getpwuid(getuid()))) | |
1172 | fatal(0, "cannot determine our username"); | |
1173 | if((privconf = config_usersysconf(pw)) | |
1174 | && access(privconf, F_OK) == 0 | |
1175 | && config_include(c, privconf)) | |
1176 | return -1; | |
1177 | xfree(privconf); | |
1178 | /* if we have a password file, read it */ | |
1179 | if((privconf = config_userconf(getenv("HOME"), pw)) | |
1180 | && access(privconf, F_OK) == 0 | |
1181 | && config_include(c, privconf)) | |
1182 | return -1; | |
1183 | xfree(privconf); | |
1184 | /* install default namepart and transform settings */ | |
c00fce3a | 1185 | config_postdefaults(c, server); |
460b9539 | 1186 | /* everything is good so we shall use the new config */ |
1187 | config_free(config); | |
1188 | config = c; | |
1189 | return 0; | |
1190 | } | |
1191 | ||
3f3bb97b | 1192 | /** @brief Return the path to the private configuration file */ |
460b9539 | 1193 | char *config_private(void) { |
1194 | char *s; | |
1195 | ||
1196 | set_configfile(); | |
1197 | byte_xasprintf(&s, "%s.private", configfile); | |
1198 | return s; | |
1199 | } | |
1200 | ||
3f3bb97b | 1201 | /** @brief Return the path to user's personal configuration file */ |
460b9539 | 1202 | char *config_userconf(const char *home, const struct passwd *pw) { |
1203 | char *s; | |
1204 | ||
73f1b9f3 RK |
1205 | if(!home && !pw && !(pw = getpwuid(getuid()))) |
1206 | fatal(0, "cannot determine our username"); | |
460b9539 | 1207 | byte_xasprintf(&s, "%s/.disorder/passwd", home ? home : pw->pw_dir); |
1208 | return s; | |
1209 | } | |
1210 | ||
3f3bb97b RK |
1211 | /** @brief Return the path to user-specific system configuration */ |
1212 | char *config_usersysconf(const struct passwd *pw) { | |
460b9539 | 1213 | char *s; |
1214 | ||
1215 | set_configfile(); | |
1216 | if(!strchr(pw->pw_name, '/')) { | |
1217 | byte_xasprintf(&s, "%s.%s", configfile, pw->pw_name); | |
1218 | return s; | |
1219 | } else | |
1220 | return 0; | |
1221 | } | |
1222 | ||
1223 | char *config_get_file(const char *name) { | |
1224 | return get_file(config, name); | |
1225 | } | |
1226 | ||
1227 | /* | |
1228 | Local Variables: | |
1229 | c-basic-offset:2 | |
1230 | comment-column:40 | |
1231 | fill-column:79 | |
1232 | End: | |
1233 | */ |