chiark / gitweb /
doxygen; chatty logging in hope of catching a bug
[disorder] / lib / configuration.c
CommitLineData
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 60char *configfile;
61
3f3bb97b 62/** @brief Config file parser state */
460b9539 63struct 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 73struct config *config;
74
3f3bb97b 75/** @brief One configuration item */
460b9539 76struct 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 89struct 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
103static 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
122static 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
151static 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
172static 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
184static 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
200static 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
221static 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
239static 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
254static 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 281static 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
343static 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 350static 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
395static 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
431static 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
479static void free_none(struct config attribute((unused)) *c,
480 const struct conf attribute((unused)) *whoami) {
481}
482
483static void free_string(struct config *c,
484 const struct conf *whoami) {
485 xfree(VALUE(c, char *));
486}
487
488static 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
498static 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
513static 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
528static 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
544static 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
562static 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
596static 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 609static int validate_isdir(const struct config_state *cs,
610 int nvec, char **vec) {
611 VALIDATE_FILE(S_ISDIR, "directory");
612 return 0;
613}
614
615static 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
621static 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
627static 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
638static int validate_allow(const struct config_state *cs,
639 int nvec,
640 char attribute((unused)) **vec) {
641 if(nvec != 2) {
642 error(0, "%s:%d: must be 'allow NAME PASS'", cs->path, cs->line);
643 return -1;
644 }
645 return 0;
646}
647
648static int validate_non_negative(const struct config_state *cs,
649 int nvec, char **vec) {
650 long n;
651
652 if(nvec < 1) {
653 error(0, "%s:%d: missing argument", cs->path, cs->line);
654 return -1;
655 }
656 if(nvec > 1) {
657 error(0, "%s:%d: too many arguments", cs->path, cs->line);
658 return -1;
659 }
660 if(xstrtol(&n, vec[0], 0, 0)) {
661 error(0, "%s:%d: %s", cs->path, cs->line, strerror(errno));
662 return -1;
663 }
664 if(n < 0) {
665 error(0, "%s:%d: must not be negative", cs->path, cs->line);
666 return -1;
667 }
668 return 0;
669}
670
671static int validate_positive(const struct config_state *cs,
672 int nvec, char **vec) {
673 long n;
674
675 if(nvec < 1) {
676 error(0, "%s:%d: missing argument", cs->path, cs->line);
677 return -1;
678 }
679 if(nvec > 1) {
680 error(0, "%s:%d: too many arguments", cs->path, cs->line);
681 return -1;
682 }
683 if(xstrtol(&n, vec[0], 0, 0)) {
684 error(0, "%s:%d: %s", cs->path, cs->line, strerror(errno));
685 return -1;
686 }
687 if(n <= 0) {
688 error(0, "%s:%d: must be positive", cs->path, cs->line);
689 return -1;
690 }
691 return 0;
692}
693
694static int validate_isauser(const struct config_state *cs,
695 int attribute((unused)) nvec,
696 char **vec) {
697 struct passwd *pw;
698
699 if(!(pw = getpwnam(vec[0]))) {
700 error(0, "%s:%d: no such user as '%s'", cs->path, cs->line, vec[0]);
701 return -1;
702 }
703 return 0;
704}
705
9d5da576 706static int validate_sample_format(const struct config_state *cs,
707 int attribute((unused)) nvec,
708 char **vec) {
709 return parse_sample_format(cs, 0, nvec, vec);
710}
711
460b9539 712static int validate_channel(const struct config_state *cs,
713 int attribute((unused)) nvec,
714 char **vec) {
715 if(mixer_channel(vec[0]) == -1) {
716 error(0, "%s:%d: invalid channel '%s'", cs->path, cs->line, vec[0]);
717 return -1;
718 }
719 return 0;
720}
721
722static int validate_any(const struct config_state attribute((unused)) *cs,
723 int attribute((unused)) nvec,
724 char attribute((unused)) **vec) {
725 return 0;
726}
727
728static int validate_url(const struct config_state attribute((unused)) *cs,
729 int attribute((unused)) nvec,
730 char **vec) {
731 const char *s;
732 int n;
733 /* absoluteURI = scheme ":" ( hier_part | opaque_part )
734 scheme = alpha *( alpha | digit | "+" | "-" | "." ) */
735 s = vec[0];
736 n = strspn(s, ("abcdefghijklmnopqrstuvwxyz"
737 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
738 "0123456789"));
739 if(s[n] != ':') {
740 error(0, "%s:%d: invalid url '%s'", cs->path, cs->line, vec[0]);
741 return -1;
742 }
743 if(!strncmp(s, "http:", 5)
744 || !strncmp(s, "https:", 6)) {
745 s += n + 1;
746 /* we only do a rather cursory check */
747 if(strncmp(s, "//", 2)) {
748 error(0, "%s:%d: invalid url '%s'", cs->path, cs->line, vec[0]);
749 return -1;
750 }
751 }
752 return 0;
753}
754
755static int validate_alias(const struct config_state *cs,
756 int nvec,
757 char **vec) {
758 const char *s;
759 int in_brackets = 0, c;
760
761 if(nvec < 1) {
762 error(0, "%s:%d: missing argument", cs->path, cs->line);
763 return -1;
764 }
765 if(nvec > 1) {
766 error(0, "%s:%d: too many arguments", cs->path, cs->line);
767 return -1;
768 }
769 s = vec[0];
770 while((c = (unsigned char)*s++)) {
771 if(in_brackets) {
772 if(c == '}')
773 in_brackets = 0;
774 else if(!isalnum(c)) {
775 error(0, "%s:%d: invalid part name in alias expansion in '%s'",
776 cs->path, cs->line, vec[0]);
777 return -1;
778 }
779 } else {
780 if(c == '{') {
781 in_brackets = 1;
782 if(*s == '/')
783 ++s;
784 } else if(c == '\\') {
785 if(!(c = (unsigned char)*s++)) {
786 error(0, "%s:%d: unterminated escape in alias expansion in '%s'",
787 cs->path, cs->line, vec[0]);
788 return -1;
789 } else if(c != '\\' && c != '{') {
790 error(0, "%s:%d: invalid escape in alias expansion in '%s'",
791 cs->path, cs->line, vec[0]);
792 return -1;
793 }
794 }
795 }
796 ++s;
797 }
798 if(in_brackets) {
799 error(0, "%s:%d: unterminated part name in alias expansion in '%s'",
800 cs->path, cs->line, vec[0]);
801 return -1;
802 }
803 return 0;
804}
805
e83d0967
RK
806static int validate_addrport(const struct config_state attribute((unused)) *cs,
807 int nvec,
808 char attribute((unused)) **vec) {
809 switch(nvec) {
810 case 0:
811 error(0, "%s:%d: missing address",
812 cs->path, cs->line);
813 return -1;
814 case 1:
815 error(0, "%s:%d: missing port name/number",
816 cs->path, cs->line);
817 return -1;
818 case 2:
819 return 0;
820 default:
821 error(0, "%s:%d: expected ADDRESS PORT",
822 cs->path, cs->line);
823 return -1;
824 }
825}
826
ccf0aafa 827static int validate_port(const struct config_state attribute((unused)) *cs,
e83d0967
RK
828 int nvec,
829 char attribute((unused)) **vec) {
830 switch(nvec) {
831 case 0:
832 error(0, "%s:%d: missing address",
833 cs->path, cs->line);
834 return -1;
835 case 1:
836 case 2:
837 return 0;
838 default:
ccf0aafa 839 error(0, "%s:%d: expected [ADDRESS] PORT",
e83d0967
RK
840 cs->path, cs->line);
841 return -1;
842 }
843}
844
637fdea3
RK
845static int validate_algo(const struct config_state attribute((unused)) *cs,
846 int nvec,
847 char **vec) {
848 if(nvec != 1) {
849 error(0, "%s:%d: invalid algorithm specification", cs->path, cs->line);
850 return -1;
851 }
852 if(!valid_authhash(vec[0])) {
853 error(0, "%s:%d: unsuported algorithm '%s'", cs->path, cs->line, vec[0]);
854 return -1;
855 }
856 return 0;
857}
858
3f3bb97b 859/** @brief Item name and and offset */
460b9539 860#define C(x) #x, offsetof(struct config, x)
3f3bb97b 861/** @brief Item name and and offset */
460b9539 862#define C2(x,y) #x, offsetof(struct config, y)
863
3f3bb97b 864/** @brief All configuration items */
460b9539 865static const struct conf conf[] = {
866 { C(alias), &type_string, validate_alias },
867 { C(allow), &type_stringlist_accum, validate_allow },
637fdea3 868 { C(authorization_algorithm), &type_string, validate_algo },
e83d0967 869 { C(broadcast), &type_stringlist, validate_addrport },
ccf0aafa 870 { C(broadcast_from), &type_stringlist, validate_addrport },
460b9539 871 { C(channel), &type_string, validate_channel },
872 { C(checkpoint_kbyte), &type_integer, validate_non_negative },
873 { C(checkpoint_min), &type_integer, validate_non_negative },
874 { C(collection), &type_collections, validate_any },
e83d0967 875 { C(connect), &type_stringlist, validate_addrport },
460b9539 876 { C(device), &type_string, validate_any },
877 { C(gap), &type_integer, validate_non_negative },
878 { C(history), &type_integer, validate_positive },
659d87e8 879 { C(home), &type_string, validate_isabspath },
ccf0aafa 880 { C(listen), &type_stringlist, validate_port },
460b9539 881 { C(lock), &type_boolean, validate_any },
882 { C(mixer), &type_string, validate_ischr },
23205f9c 883 { C(multicast_ttl), &type_integer, validate_non_negative },
460b9539 884 { C(namepart), &type_namepart, validate_any },
885 { C2(nice, nice_rescan), &type_integer, validate_non_negative },
886 { C(nice_rescan), &type_integer, validate_non_negative },
887 { C(nice_server), &type_integer, validate_any },
888 { C(nice_speaker), &type_integer, validate_any },
2a10b70b 889 { C(noticed_history), &type_integer, validate_positive },
460b9539 890 { C(password), &type_string, validate_any },
891 { C(player), &type_stringlist_accum, validate_player },
892 { C(plugins), &type_string_accum, validate_isdir },
893 { C(prefsync), &type_integer, validate_positive },
459d4402 894 { C(queue_pad), &type_integer, validate_positive },
460b9539 895 { C(refresh), &type_integer, validate_positive },
896 { C2(restrict, restrictions), &type_restrict, validate_any },
9d5da576 897 { C(sample_format), &type_sample_format, validate_sample_format },
460b9539 898 { C(scratch), &type_string_accum, validate_isreg },
899 { C(signal), &type_signal, validate_any },
5330d674 900 { C(sox_generation), &type_integer, validate_non_negative },
e83d0967 901 { C(speaker_backend), &type_backend, validate_any },
9d5da576 902 { C(speaker_command), &type_string, validate_any },
460b9539 903 { C(stopword), &type_string_accum, validate_any },
904 { C(templates), &type_string_accum, validate_isdir },
905 { C(transform), &type_transform, validate_any },
906 { C(trust), &type_string_accum, validate_any },
907 { C(url), &type_string, validate_url },
908 { C(user), &type_string, validate_isauser },
909 { C(username), &type_string, validate_any },
910};
911
3f3bb97b 912/** @brief Find a configuration item's definition by key */
460b9539 913static const struct conf *find(const char *key) {
914 int n;
915
916 if((n = TABLE_FIND(conf, struct conf, name, key)) < 0)
917 return 0;
918 return &conf[n];
919}
920
3f3bb97b 921/** @brief Set a new configuration value */
460b9539 922static int config_set(const struct config_state *cs,
923 int nvec, char **vec) {
924 const struct conf *which;
925
926 D(("config_set %s", vec[0]));
927 if(!(which = find(vec[0]))) {
928 error(0, "%s:%d: unknown configuration key '%s'",
929 cs->path, cs->line, vec[0]);
930 return -1;
931 }
932 return (which->validate(cs, nvec - 1, vec + 1)
933 || which->type->set(cs, which, nvec - 1, vec + 1));
934}
935
0e4472a0 936/** @brief Error callback used by config_include() */
460b9539 937static void config_error(const char *msg, void *u) {
938 const struct config_state *cs = u;
939
940 error(0, "%s:%d: %s", cs->path, cs->line, msg);
941}
942
3f3bb97b 943/** @brief Include a file by name */
460b9539 944static int config_include(struct config *c, const char *path) {
945 FILE *fp;
946 char *buffer, *inputbuffer, **vec;
947 int n, ret = 0;
948 struct config_state cs;
949
950 cs.path = path;
951 cs.line = 0;
952 cs.config = c;
953 D(("%s: reading configuration", path));
954 if(!(fp = fopen(path, "r"))) {
955 error(errno, "error opening %s", path);
956 return -1;
957 }
958 while(!inputline(path, fp, &inputbuffer, '\n')) {
959 ++cs.line;
960 if(!(buffer = mb2utf8(inputbuffer))) {
961 error(errno, "%s:%d: cannot convert to UTF-8", cs.path, cs.line);
962 ret = -1;
963 xfree(inputbuffer);
964 continue;
965 }
966 xfree(inputbuffer);
967 if(!(vec = split(buffer, &n, SPLIT_COMMENTS|SPLIT_QUOTES,
968 config_error, &cs))) {
969 ret = -1;
970 xfree(buffer);
971 continue;
972 }
973 if(n) {
974 if(!strcmp(vec[0], "include")) {
975 if(n != 2) {
976 error(0, "%s:%d: must be 'include PATH'", cs.path, cs.line);
977 ret = -1;
978 } else
979 config_include(c, vec[1]);
980 } else
981 ret |= config_set(&cs, n, vec);
982 }
983 for(n = 0; vec[n]; ++n) xfree(vec[n]);
984 xfree(vec);
985 xfree(buffer);
986 }
987 if(ferror(fp)) {
988 error(errno, "error reading %s", path);
989 ret = -1;
990 }
991 fclose(fp);
992 return ret;
993}
994
3f3bb97b 995/** @brief Make a new default configuration */
460b9539 996static struct config *config_default(void) {
997 struct config *c = xmalloc(sizeof *c);
998 const char *logname;
999 struct passwd *pw;
1000
1001 /* Strings had better be xstrdup'd as they will get freed at some point. */
1002 c->gap = 2;
1003 c->history = 60;
1004 c->home = xstrdup(pkgstatedir);
1005 if(!(pw = getpwuid(getuid())))
1006 fatal(0, "cannot determine our username");
1007 logname = pw->pw_name;
1008 c->username = xstrdup(logname);
1009 c->refresh = 15;
1010 c->prefsync = 3600;
1011 c->signal = SIGKILL;
1012 c->alias = xstrdup("{/artist}{/album}{/title}{ext}");
1013 c->lock = 1;
1014 c->device = xstrdup("default");
1015 c->nice_rescan = 10;
9d5da576 1016 c->speaker_command = 0;
1017 c->sample_format.bits = 16;
1018 c->sample_format.rate = 44100;
1019 c->sample_format.channels = 2;
6d2d327c 1020 c->sample_format.endian = ENDIAN_NATIVE;
459d4402 1021 c->queue_pad = 10;
e83d0967 1022 c->speaker_backend = -1;
23205f9c 1023 c->multicast_ttl = 1;
637fdea3 1024 c->authorization_algorithm = xstrdup("sha1");
2a10b70b 1025 c->noticed_history = 31;
460b9539 1026 return c;
1027}
1028
1029static char *get_file(struct config *c, const char *name) {
1030 char *s;
1031
1032 byte_xasprintf(&s, "%s/%s", c->home, name);
1033 return s;
1034}
1035
3f3bb97b 1036/** @brief Set the default configuration file */
460b9539 1037static void set_configfile(void) {
1038 if(!configfile)
1039 byte_xasprintf(&configfile, "%s/config", pkgconfdir);
1040}
1041
3f3bb97b 1042/** @brief Free a configuration object */
460b9539 1043static void config_free(struct config *c) {
1044 int n;
1045
1046 if(c) {
1047 for(n = 0; n < (int)(sizeof conf / sizeof *conf); ++n)
1048 conf[n].type->free(c, &conf[n]);
1049 for(n = 0; n < c->nparts; ++n)
1050 xfree(c->parts[n]);
1051 xfree(c->parts);
1052 xfree(c);
1053 }
1054}
1055
3f3bb97b 1056/** @brief Set post-parse defaults */
c00fce3a
RK
1057static void config_postdefaults(struct config *c,
1058 int server) {
460b9539 1059 struct config_state cs;
1060 const struct conf *whoami;
1061 int n;
1062
1063 static const char *namepart[][4] = {
88608992 1064 { "title", "/([0-9]+ *[-:] *)?([^/]+)\\.[a-zA-Z0-9]+$", "$2", "display" },
460b9539 1065 { "title", "/([^/]+)\\.[a-zA-Z0-9]+$", "$1", "sort" },
1066 { "album", "/([^/]+)/[^/]+$", "$1", "*" },
1067 { "artist", "/([^/]+)/[^/]+/[^/]+$", "$1", "*" },
1068 { "ext", "(\\.[a-zA-Z0-9]+)$", "$1", "*" },
1069 };
1070#define NNAMEPART (int)(sizeof namepart / sizeof *namepart)
1071
1072 static const char *transform[][5] = {
88608992 1073 { "track", "^.*/([0-9]+ *[-:] *)?([^/]+)\\.[a-zA-Z0-9]+$", "$2", "display", "" },
460b9539 1074 { "track", "^.*/([^/]+)\\.[a-zA-Z0-9]+$", "$1", "sort", "" },
1075 { "dir", "^.*/([^/]+)$", "$1", "*", "" },
1076 { "dir", "^(the) ([^/]*)", "$2, $1", "sort", "i", },
1077 { "dir", "[[:punct:]]", "", "sort", "g", }
1078 };
1079#define NTRANSFORM (int)(sizeof transform / sizeof *transform)
1080
1081 cs.path = "<internal>";
1082 cs.line = 0;
1083 cs.config = c;
1084 if(!c->namepart.n) {
1085 whoami = find("namepart");
1086 for(n = 0; n < NNAMEPART; ++n)
1087 set_namepart(&cs, whoami, 4, (char **)namepart[n]);
1088 }
1089 if(!c->transform.n) {
1090 whoami = find("transform");
1091 for(n = 0; n < NTRANSFORM; ++n)
1092 set_transform(&cs, whoami, 5, (char **)transform[n]);
1093 }
e83d0967
RK
1094 if(c->speaker_backend == -1) {
1095 if(c->speaker_command)
1096 c->speaker_backend = BACKEND_COMMAND;
1097 else if(c->broadcast.n)
1098 c->speaker_backend = BACKEND_NETWORK;
1099 else {
146e86fb 1100#if HAVE_ALSA_ASOUNDLIB_H
e83d0967 1101 c->speaker_backend = BACKEND_ALSA;
146e86fb 1102#elif HAVE_SYS_SOUNDCARD_H
1103 c->speaker_backend = BACKEND_OSS;
937be4c0
RK
1104#elif HAVE_COREAUDIO_AUDIOHARDWARE_H
1105 c->speaker_backend = BACKEND_COREAUDIO;
e83d0967
RK
1106#else
1107 c->speaker_backend = BACKEND_COMMAND;
1108#endif
1109 }
1110 }
c00fce3a
RK
1111 if(server) {
1112 if(c->speaker_backend == BACKEND_COMMAND && !c->speaker_command)
1113 fatal(0, "speaker_backend is command but speaker_command is not set");
1114 if(c->speaker_backend == BACKEND_NETWORK && !c->broadcast.n)
1115 fatal(0, "speaker_backend is network but broadcast is not set");
1116 }
e99d42b1 1117 /* Override sample format */
1118 switch(c->speaker_backend) {
1119 case BACKEND_NETWORK:
6d2d327c
RK
1120 c->sample_format.rate = 44100;
1121 c->sample_format.channels = 2;
1122 c->sample_format.bits = 16;
1123 c->sample_format.endian = ENDIAN_BIG;
e99d42b1 1124 break;
1125 case BACKEND_COREAUDIO:
937be4c0
RK
1126 c->sample_format.rate = 44100;
1127 c->sample_format.channels = 2;
1128 c->sample_format.bits = 16;
1129 c->sample_format.endian = ENDIAN_NATIVE;
e99d42b1 1130 break;
1131 }
460b9539 1132}
1133
c00fce3a
RK
1134/** @brief (Re-)read the config file
1135 * @param server If set, do extra checking
1136 */
1137int config_read(int server) {
460b9539 1138 struct config *c;
1139 char *privconf;
1140 struct passwd *pw;
1141
1142 set_configfile();
1143 c = config_default();
9ade2319 1144 /* standalone Disobedience installs might not have a global config file */
1145 if(access(configfile, F_OK) == 0)
1146 if(config_include(c, configfile))
1147 return -1;
460b9539 1148 /* if we can read the private config file, do */
1149 if((privconf = config_private())
1150 && access(privconf, R_OK) == 0
1151 && config_include(c, privconf))
1152 return -1;
1153 xfree(privconf);
1154 /* if there's a per-user system config file for this user, read it */
1155 if(!(pw = getpwuid(getuid())))
1156 fatal(0, "cannot determine our username");
1157 if((privconf = config_usersysconf(pw))
1158 && access(privconf, F_OK) == 0
1159 && config_include(c, privconf))
1160 return -1;
1161 xfree(privconf);
1162 /* if we have a password file, read it */
1163 if((privconf = config_userconf(getenv("HOME"), pw))
1164 && access(privconf, F_OK) == 0
1165 && config_include(c, privconf))
1166 return -1;
1167 xfree(privconf);
1168 /* install default namepart and transform settings */
c00fce3a 1169 config_postdefaults(c, server);
460b9539 1170 /* everything is good so we shall use the new config */
1171 config_free(config);
1172 config = c;
1173 return 0;
1174}
1175
3f3bb97b 1176/** @brief Return the path to the private configuration file */
460b9539 1177char *config_private(void) {
1178 char *s;
1179
1180 set_configfile();
1181 byte_xasprintf(&s, "%s.private", configfile);
1182 return s;
1183}
1184
3f3bb97b 1185/** @brief Return the path to user's personal configuration file */
460b9539 1186char *config_userconf(const char *home, const struct passwd *pw) {
1187 char *s;
1188
73f1b9f3
RK
1189 if(!home && !pw && !(pw = getpwuid(getuid())))
1190 fatal(0, "cannot determine our username");
460b9539 1191 byte_xasprintf(&s, "%s/.disorder/passwd", home ? home : pw->pw_dir);
1192 return s;
1193}
1194
3f3bb97b
RK
1195/** @brief Return the path to user-specific system configuration */
1196char *config_usersysconf(const struct passwd *pw) {
460b9539 1197 char *s;
1198
1199 set_configfile();
1200 if(!strchr(pw->pw_name, '/')) {
1201 byte_xasprintf(&s, "%s.%s", configfile, pw->pw_name);
1202 return s;
1203 } else
1204 return 0;
1205}
1206
1207char *config_get_file(const char *name) {
1208 return get_file(config, name);
1209}
1210
1211/*
1212Local Variables:
1213c-basic-offset:2
1214comment-column:40
1215fill-column:79
1216End:
1217*/