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