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