core audio support in speaker
[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")) {
442#if API_ALSA
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;
460#endif
461 } else {
e83d0967
RK
462 error(0, "%s:%d: invalid '%s' value '%s'",
463 cs->path, cs->line, whoami->name, vec[0]);
464 return -1;
465 }
466 return 0;
467}
468
460b9539 469/* free functions */
470
471static void free_none(struct config attribute((unused)) *c,
472 const struct conf attribute((unused)) *whoami) {
473}
474
475static void free_string(struct config *c,
476 const struct conf *whoami) {
477 xfree(VALUE(c, char *));
478}
479
480static void free_stringlist(struct config *c,
481 const struct conf *whoami) {
482 int n;
483 struct stringlist *sl = ADDRESS(c, struct stringlist);
484
485 for(n = 0; n < sl->n; ++n)
486 xfree(sl->s[n]);
487 xfree(sl->s);
488}
489
490static void free_stringlistlist(struct config *c,
491 const struct conf *whoami) {
492 int n, m;
493 struct stringlistlist *sll = ADDRESS(c, struct stringlistlist);
494 struct stringlist *sl;
495
496 for(n = 0; n < sll->n; ++n) {
497 sl = &sll->s[n];
498 for(m = 0; m < sl->n; ++m)
499 xfree(sl->s[m]);
500 xfree(sl->s);
501 }
502 xfree(sll->s);
503}
504
505static void free_collectionlist(struct config *c,
506 const struct conf *whoami) {
507 struct collectionlist *cll = ADDRESS(c, struct collectionlist);
508 struct collection *cl;
509 int n;
510
511 for(n = 0; n < cll->n; ++n) {
512 cl = &cll->s[n];
513 xfree(cl->module);
514 xfree(cl->encoding);
515 xfree(cl->root);
516 }
517 xfree(cll->s);
518}
519
520static void free_namepartlist(struct config *c,
521 const struct conf *whoami) {
522 struct namepartlist *npl = ADDRESS(c, struct namepartlist);
523 struct namepart *np;
524 int n;
525
526 for(n = 0; n < npl->n; ++n) {
527 np = &npl->s[n];
528 xfree(np->part);
529 pcre_free(np->re); /* ...whatever pcre_free is set to. */
530 xfree(np->replace);
531 xfree(np->context);
532 }
533 xfree(npl->s);
534}
535
536static void free_transformlist(struct config *c,
537 const struct conf *whoami) {
538 struct transformlist *tl = ADDRESS(c, struct transformlist);
539 struct transform *t;
540 int n;
541
542 for(n = 0; n < tl->n; ++n) {
543 t = &tl->t[n];
544 xfree(t->type);
545 pcre_free(t->re); /* ...whatever pcre_free is set to. */
546 xfree(t->replace);
547 xfree(t->context);
548 }
549 xfree(tl->t);
550}
551
552/* configuration types */
553
554static const struct conftype
555 type_signal = { set_signal, free_none },
556 type_collections = { set_collections, free_collectionlist },
557 type_boolean = { set_boolean, free_none },
558 type_string = { set_string, free_string },
559 type_stringlist = { set_stringlist, free_stringlist },
560 type_integer = { set_integer, free_none },
561 type_stringlist_accum = { set_stringlist_accum, free_stringlistlist },
562 type_string_accum = { set_string_accum, free_stringlist },
9d5da576 563 type_sample_format = { set_sample_format, free_none },
460b9539 564 type_restrict = { set_restrict, free_none },
565 type_namepart = { set_namepart, free_namepartlist },
e83d0967
RK
566 type_transform = { set_transform, free_transformlist },
567 type_backend = { set_backend, free_none };
460b9539 568
569/* specific validation routine */
570
571#define VALIDATE_FILE(test, what) do { \
572 struct stat sb; \
573 int n; \
574 \
575 for(n = 0; n < nvec; ++n) { \
576 if(stat(vec[n], &sb) < 0) { \
577 error(errno, "%s:%d: %s", cs->path, cs->line, vec[n]); \
578 return -1; \
579 } \
580 if(!test(sb.st_mode)) { \
581 error(0, "%s:%d: %s is not a %s", \
582 cs->path, cs->line, vec[n], what); \
583 return -1; \
584 } \
585 } \
586} while(0)
587
588static int validate_isdir(const struct config_state *cs,
589 int nvec, char **vec) {
590 VALIDATE_FILE(S_ISDIR, "directory");
591 return 0;
592}
593
594static int validate_isreg(const struct config_state *cs,
595 int nvec, char **vec) {
596 VALIDATE_FILE(S_ISREG, "regular file");
597 return 0;
598}
599
600static int validate_ischr(const struct config_state *cs,
601 int nvec, char **vec) {
602 VALIDATE_FILE(S_ISCHR, "character device");
603 return 0;
604}
605
606static int validate_player(const struct config_state *cs,
607 int nvec,
608 char attribute((unused)) **vec) {
609 if(nvec < 2) {
610 error(0, "%s:%d: should be at least 'player PATTERN MODULE'",
611 cs->path, cs->line);
612 return -1;
613 }
614 return 0;
615}
616
617static int validate_allow(const struct config_state *cs,
618 int nvec,
619 char attribute((unused)) **vec) {
620 if(nvec != 2) {
621 error(0, "%s:%d: must be 'allow NAME PASS'", cs->path, cs->line);
622 return -1;
623 }
624 return 0;
625}
626
627static int validate_non_negative(const struct config_state *cs,
628 int nvec, char **vec) {
629 long n;
630
631 if(nvec < 1) {
632 error(0, "%s:%d: missing argument", cs->path, cs->line);
633 return -1;
634 }
635 if(nvec > 1) {
636 error(0, "%s:%d: too many arguments", cs->path, cs->line);
637 return -1;
638 }
639 if(xstrtol(&n, vec[0], 0, 0)) {
640 error(0, "%s:%d: %s", cs->path, cs->line, strerror(errno));
641 return -1;
642 }
643 if(n < 0) {
644 error(0, "%s:%d: must not be negative", cs->path, cs->line);
645 return -1;
646 }
647 return 0;
648}
649
650static int validate_positive(const struct config_state *cs,
651 int nvec, char **vec) {
652 long n;
653
654 if(nvec < 1) {
655 error(0, "%s:%d: missing argument", cs->path, cs->line);
656 return -1;
657 }
658 if(nvec > 1) {
659 error(0, "%s:%d: too many arguments", cs->path, cs->line);
660 return -1;
661 }
662 if(xstrtol(&n, vec[0], 0, 0)) {
663 error(0, "%s:%d: %s", cs->path, cs->line, strerror(errno));
664 return -1;
665 }
666 if(n <= 0) {
667 error(0, "%s:%d: must be positive", cs->path, cs->line);
668 return -1;
669 }
670 return 0;
671}
672
673static int validate_isauser(const struct config_state *cs,
674 int attribute((unused)) nvec,
675 char **vec) {
676 struct passwd *pw;
677
678 if(!(pw = getpwnam(vec[0]))) {
679 error(0, "%s:%d: no such user as '%s'", cs->path, cs->line, vec[0]);
680 return -1;
681 }
682 return 0;
683}
684
9d5da576 685static int validate_sample_format(const struct config_state *cs,
686 int attribute((unused)) nvec,
687 char **vec) {
688 return parse_sample_format(cs, 0, nvec, vec);
689}
690
460b9539 691static int validate_channel(const struct config_state *cs,
692 int attribute((unused)) nvec,
693 char **vec) {
694 if(mixer_channel(vec[0]) == -1) {
695 error(0, "%s:%d: invalid channel '%s'", cs->path, cs->line, vec[0]);
696 return -1;
697 }
698 return 0;
699}
700
701static int validate_any(const struct config_state attribute((unused)) *cs,
702 int attribute((unused)) nvec,
703 char attribute((unused)) **vec) {
704 return 0;
705}
706
707static int validate_url(const struct config_state attribute((unused)) *cs,
708 int attribute((unused)) nvec,
709 char **vec) {
710 const char *s;
711 int n;
712 /* absoluteURI = scheme ":" ( hier_part | opaque_part )
713 scheme = alpha *( alpha | digit | "+" | "-" | "." ) */
714 s = vec[0];
715 n = strspn(s, ("abcdefghijklmnopqrstuvwxyz"
716 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
717 "0123456789"));
718 if(s[n] != ':') {
719 error(0, "%s:%d: invalid url '%s'", cs->path, cs->line, vec[0]);
720 return -1;
721 }
722 if(!strncmp(s, "http:", 5)
723 || !strncmp(s, "https:", 6)) {
724 s += n + 1;
725 /* we only do a rather cursory check */
726 if(strncmp(s, "//", 2)) {
727 error(0, "%s:%d: invalid url '%s'", cs->path, cs->line, vec[0]);
728 return -1;
729 }
730 }
731 return 0;
732}
733
734static int validate_alias(const struct config_state *cs,
735 int nvec,
736 char **vec) {
737 const char *s;
738 int in_brackets = 0, c;
739
740 if(nvec < 1) {
741 error(0, "%s:%d: missing argument", cs->path, cs->line);
742 return -1;
743 }
744 if(nvec > 1) {
745 error(0, "%s:%d: too many arguments", cs->path, cs->line);
746 return -1;
747 }
748 s = vec[0];
749 while((c = (unsigned char)*s++)) {
750 if(in_brackets) {
751 if(c == '}')
752 in_brackets = 0;
753 else if(!isalnum(c)) {
754 error(0, "%s:%d: invalid part name in alias expansion in '%s'",
755 cs->path, cs->line, vec[0]);
756 return -1;
757 }
758 } else {
759 if(c == '{') {
760 in_brackets = 1;
761 if(*s == '/')
762 ++s;
763 } else if(c == '\\') {
764 if(!(c = (unsigned char)*s++)) {
765 error(0, "%s:%d: unterminated escape in alias expansion in '%s'",
766 cs->path, cs->line, vec[0]);
767 return -1;
768 } else if(c != '\\' && c != '{') {
769 error(0, "%s:%d: invalid escape in alias expansion in '%s'",
770 cs->path, cs->line, vec[0]);
771 return -1;
772 }
773 }
774 }
775 ++s;
776 }
777 if(in_brackets) {
778 error(0, "%s:%d: unterminated part name in alias expansion in '%s'",
779 cs->path, cs->line, vec[0]);
780 return -1;
781 }
782 return 0;
783}
784
e83d0967
RK
785static int validate_addrport(const struct config_state attribute((unused)) *cs,
786 int nvec,
787 char attribute((unused)) **vec) {
788 switch(nvec) {
789 case 0:
790 error(0, "%s:%d: missing address",
791 cs->path, cs->line);
792 return -1;
793 case 1:
794 error(0, "%s:%d: missing port name/number",
795 cs->path, cs->line);
796 return -1;
797 case 2:
798 return 0;
799 default:
800 error(0, "%s:%d: expected ADDRESS PORT",
801 cs->path, cs->line);
802 return -1;
803 }
804}
805
ccf0aafa 806static int validate_port(const struct config_state attribute((unused)) *cs,
e83d0967
RK
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 case 2:
816 return 0;
817 default:
ccf0aafa 818 error(0, "%s:%d: expected [ADDRESS] PORT",
e83d0967
RK
819 cs->path, cs->line);
820 return -1;
821 }
822}
823
637fdea3
RK
824static int validate_algo(const struct config_state attribute((unused)) *cs,
825 int nvec,
826 char **vec) {
827 if(nvec != 1) {
828 error(0, "%s:%d: invalid algorithm specification", cs->path, cs->line);
829 return -1;
830 }
831 if(!valid_authhash(vec[0])) {
832 error(0, "%s:%d: unsuported algorithm '%s'", cs->path, cs->line, vec[0]);
833 return -1;
834 }
835 return 0;
836}
837
3f3bb97b 838/** @brief Item name and and offset */
460b9539 839#define C(x) #x, offsetof(struct config, x)
3f3bb97b 840/** @brief Item name and and offset */
460b9539 841#define C2(x,y) #x, offsetof(struct config, y)
842
3f3bb97b 843/** @brief All configuration items */
460b9539 844static const struct conf conf[] = {
845 { C(alias), &type_string, validate_alias },
846 { C(allow), &type_stringlist_accum, validate_allow },
637fdea3 847 { C(authorization_algorithm), &type_string, validate_algo },
e83d0967 848 { C(broadcast), &type_stringlist, validate_addrport },
ccf0aafa 849 { C(broadcast_from), &type_stringlist, validate_addrport },
460b9539 850 { C(channel), &type_string, validate_channel },
851 { C(checkpoint_kbyte), &type_integer, validate_non_negative },
852 { C(checkpoint_min), &type_integer, validate_non_negative },
853 { C(collection), &type_collections, validate_any },
e83d0967 854 { C(connect), &type_stringlist, validate_addrport },
460b9539 855 { C(device), &type_string, validate_any },
856 { C(gap), &type_integer, validate_non_negative },
857 { C(history), &type_integer, validate_positive },
858 { C(home), &type_string, validate_isdir },
ccf0aafa 859 { C(listen), &type_stringlist, validate_port },
460b9539 860 { C(lock), &type_boolean, validate_any },
861 { C(mixer), &type_string, validate_ischr },
23205f9c 862 { C(multicast_ttl), &type_integer, validate_non_negative },
460b9539 863 { C(namepart), &type_namepart, validate_any },
864 { C2(nice, nice_rescan), &type_integer, validate_non_negative },
865 { C(nice_rescan), &type_integer, validate_non_negative },
866 { C(nice_server), &type_integer, validate_any },
867 { C(nice_speaker), &type_integer, validate_any },
2a10b70b 868 { C(noticed_history), &type_integer, validate_positive },
460b9539 869 { C(password), &type_string, validate_any },
870 { C(player), &type_stringlist_accum, validate_player },
871 { C(plugins), &type_string_accum, validate_isdir },
872 { C(prefsync), &type_integer, validate_positive },
459d4402 873 { C(queue_pad), &type_integer, validate_positive },
460b9539 874 { C(refresh), &type_integer, validate_positive },
875 { C2(restrict, restrictions), &type_restrict, validate_any },
9d5da576 876 { C(sample_format), &type_sample_format, validate_sample_format },
460b9539 877 { C(scratch), &type_string_accum, validate_isreg },
878 { C(signal), &type_signal, validate_any },
5330d674 879 { C(sox_generation), &type_integer, validate_non_negative },
e83d0967 880 { C(speaker_backend), &type_backend, validate_any },
9d5da576 881 { C(speaker_command), &type_string, validate_any },
460b9539 882 { C(stopword), &type_string_accum, validate_any },
883 { C(templates), &type_string_accum, validate_isdir },
884 { C(transform), &type_transform, validate_any },
885 { C(trust), &type_string_accum, validate_any },
886 { C(url), &type_string, validate_url },
887 { C(user), &type_string, validate_isauser },
888 { C(username), &type_string, validate_any },
889};
890
3f3bb97b 891/** @brief Find a configuration item's definition by key */
460b9539 892static const struct conf *find(const char *key) {
893 int n;
894
895 if((n = TABLE_FIND(conf, struct conf, name, key)) < 0)
896 return 0;
897 return &conf[n];
898}
899
3f3bb97b 900/** @brief Set a new configuration value */
460b9539 901static int config_set(const struct config_state *cs,
902 int nvec, char **vec) {
903 const struct conf *which;
904
905 D(("config_set %s", vec[0]));
906 if(!(which = find(vec[0]))) {
907 error(0, "%s:%d: unknown configuration key '%s'",
908 cs->path, cs->line, vec[0]);
909 return -1;
910 }
911 return (which->validate(cs, nvec - 1, vec + 1)
912 || which->type->set(cs, which, nvec - 1, vec + 1));
913}
914
0e4472a0 915/** @brief Error callback used by config_include() */
460b9539 916static void config_error(const char *msg, void *u) {
917 const struct config_state *cs = u;
918
919 error(0, "%s:%d: %s", cs->path, cs->line, msg);
920}
921
3f3bb97b 922/** @brief Include a file by name */
460b9539 923static int config_include(struct config *c, const char *path) {
924 FILE *fp;
925 char *buffer, *inputbuffer, **vec;
926 int n, ret = 0;
927 struct config_state cs;
928
929 cs.path = path;
930 cs.line = 0;
931 cs.config = c;
932 D(("%s: reading configuration", path));
933 if(!(fp = fopen(path, "r"))) {
934 error(errno, "error opening %s", path);
935 return -1;
936 }
937 while(!inputline(path, fp, &inputbuffer, '\n')) {
938 ++cs.line;
939 if(!(buffer = mb2utf8(inputbuffer))) {
940 error(errno, "%s:%d: cannot convert to UTF-8", cs.path, cs.line);
941 ret = -1;
942 xfree(inputbuffer);
943 continue;
944 }
945 xfree(inputbuffer);
946 if(!(vec = split(buffer, &n, SPLIT_COMMENTS|SPLIT_QUOTES,
947 config_error, &cs))) {
948 ret = -1;
949 xfree(buffer);
950 continue;
951 }
952 if(n) {
953 if(!strcmp(vec[0], "include")) {
954 if(n != 2) {
955 error(0, "%s:%d: must be 'include PATH'", cs.path, cs.line);
956 ret = -1;
957 } else
958 config_include(c, vec[1]);
959 } else
960 ret |= config_set(&cs, n, vec);
961 }
962 for(n = 0; vec[n]; ++n) xfree(vec[n]);
963 xfree(vec);
964 xfree(buffer);
965 }
966 if(ferror(fp)) {
967 error(errno, "error reading %s", path);
968 ret = -1;
969 }
970 fclose(fp);
971 return ret;
972}
973
3f3bb97b 974/** @brief Make a new default configuration */
460b9539 975static struct config *config_default(void) {
976 struct config *c = xmalloc(sizeof *c);
977 const char *logname;
978 struct passwd *pw;
979
980 /* Strings had better be xstrdup'd as they will get freed at some point. */
981 c->gap = 2;
982 c->history = 60;
983 c->home = xstrdup(pkgstatedir);
984 if(!(pw = getpwuid(getuid())))
985 fatal(0, "cannot determine our username");
986 logname = pw->pw_name;
987 c->username = xstrdup(logname);
988 c->refresh = 15;
989 c->prefsync = 3600;
990 c->signal = SIGKILL;
991 c->alias = xstrdup("{/artist}{/album}{/title}{ext}");
992 c->lock = 1;
993 c->device = xstrdup("default");
994 c->nice_rescan = 10;
9d5da576 995 c->speaker_command = 0;
996 c->sample_format.bits = 16;
997 c->sample_format.rate = 44100;
998 c->sample_format.channels = 2;
6d2d327c 999 c->sample_format.endian = ENDIAN_NATIVE;
459d4402 1000 c->queue_pad = 10;
e83d0967 1001 c->speaker_backend = -1;
23205f9c 1002 c->multicast_ttl = 1;
637fdea3 1003 c->authorization_algorithm = xstrdup("sha1");
2a10b70b 1004 c->noticed_history = 31;
460b9539 1005 return c;
1006}
1007
1008static char *get_file(struct config *c, const char *name) {
1009 char *s;
1010
1011 byte_xasprintf(&s, "%s/%s", c->home, name);
1012 return s;
1013}
1014
3f3bb97b 1015/** @brief Set the default configuration file */
460b9539 1016static void set_configfile(void) {
1017 if(!configfile)
1018 byte_xasprintf(&configfile, "%s/config", pkgconfdir);
1019}
1020
3f3bb97b 1021/** @brief Free a configuration object */
460b9539 1022static void config_free(struct config *c) {
1023 int n;
1024
1025 if(c) {
1026 for(n = 0; n < (int)(sizeof conf / sizeof *conf); ++n)
1027 conf[n].type->free(c, &conf[n]);
1028 for(n = 0; n < c->nparts; ++n)
1029 xfree(c->parts[n]);
1030 xfree(c->parts);
1031 xfree(c);
1032 }
1033}
1034
3f3bb97b 1035/** @brief Set post-parse defaults */
c00fce3a
RK
1036static void config_postdefaults(struct config *c,
1037 int server) {
460b9539 1038 struct config_state cs;
1039 const struct conf *whoami;
1040 int n;
1041
1042 static const char *namepart[][4] = {
88608992 1043 { "title", "/([0-9]+ *[-:] *)?([^/]+)\\.[a-zA-Z0-9]+$", "$2", "display" },
460b9539 1044 { "title", "/([^/]+)\\.[a-zA-Z0-9]+$", "$1", "sort" },
1045 { "album", "/([^/]+)/[^/]+$", "$1", "*" },
1046 { "artist", "/([^/]+)/[^/]+/[^/]+$", "$1", "*" },
1047 { "ext", "(\\.[a-zA-Z0-9]+)$", "$1", "*" },
1048 };
1049#define NNAMEPART (int)(sizeof namepart / sizeof *namepart)
1050
1051 static const char *transform[][5] = {
88608992 1052 { "track", "^.*/([0-9]+ *[-:] *)?([^/]+)\\.[a-zA-Z0-9]+$", "$2", "display", "" },
460b9539 1053 { "track", "^.*/([^/]+)\\.[a-zA-Z0-9]+$", "$1", "sort", "" },
1054 { "dir", "^.*/([^/]+)$", "$1", "*", "" },
1055 { "dir", "^(the) ([^/]*)", "$2, $1", "sort", "i", },
1056 { "dir", "[[:punct:]]", "", "sort", "g", }
1057 };
1058#define NTRANSFORM (int)(sizeof transform / sizeof *transform)
1059
1060 cs.path = "<internal>";
1061 cs.line = 0;
1062 cs.config = c;
1063 if(!c->namepart.n) {
1064 whoami = find("namepart");
1065 for(n = 0; n < NNAMEPART; ++n)
1066 set_namepart(&cs, whoami, 4, (char **)namepart[n]);
1067 }
1068 if(!c->transform.n) {
1069 whoami = find("transform");
1070 for(n = 0; n < NTRANSFORM; ++n)
1071 set_transform(&cs, whoami, 5, (char **)transform[n]);
1072 }
e83d0967
RK
1073 if(c->speaker_backend == -1) {
1074 if(c->speaker_command)
1075 c->speaker_backend = BACKEND_COMMAND;
1076 else if(c->broadcast.n)
1077 c->speaker_backend = BACKEND_NETWORK;
1078 else {
1079#if API_ALSA
1080 c->speaker_backend = BACKEND_ALSA;
937be4c0
RK
1081#elif HAVE_COREAUDIO_AUDIOHARDWARE_H
1082 c->speaker_backend = BACKEND_COREAUDIO;
e83d0967
RK
1083#else
1084 c->speaker_backend = BACKEND_COMMAND;
1085#endif
1086 }
1087 }
c00fce3a
RK
1088 if(server) {
1089 if(c->speaker_backend == BACKEND_COMMAND && !c->speaker_command)
1090 fatal(0, "speaker_backend is command but speaker_command is not set");
1091 if(c->speaker_backend == BACKEND_NETWORK && !c->broadcast.n)
1092 fatal(0, "speaker_backend is network but broadcast is not set");
1093 }
937be4c0 1094 if(c->speaker_backend == BACKEND_NETWORK) {
6d2d327c
RK
1095 /* Override sample format */
1096 c->sample_format.rate = 44100;
1097 c->sample_format.channels = 2;
1098 c->sample_format.bits = 16;
1099 c->sample_format.endian = ENDIAN_BIG;
1100 }
937be4c0
RK
1101 if(c->speaker_backend == BACKEND_COREAUDIO) {
1102 /* Override sample format */
1103 c->sample_format.rate = 44100;
1104 c->sample_format.channels = 2;
1105 c->sample_format.bits = 16;
1106 c->sample_format.endian = ENDIAN_NATIVE;
1107 }
460b9539 1108}
1109
c00fce3a
RK
1110/** @brief (Re-)read the config file
1111 * @param server If set, do extra checking
1112 */
1113int config_read(int server) {
460b9539 1114 struct config *c;
1115 char *privconf;
1116 struct passwd *pw;
1117
1118 set_configfile();
1119 c = config_default();
9ade2319 1120 /* standalone Disobedience installs might not have a global config file */
1121 if(access(configfile, F_OK) == 0)
1122 if(config_include(c, configfile))
1123 return -1;
460b9539 1124 /* if we can read the private config file, do */
1125 if((privconf = config_private())
1126 && access(privconf, R_OK) == 0
1127 && config_include(c, privconf))
1128 return -1;
1129 xfree(privconf);
1130 /* if there's a per-user system config file for this user, read it */
1131 if(!(pw = getpwuid(getuid())))
1132 fatal(0, "cannot determine our username");
1133 if((privconf = config_usersysconf(pw))
1134 && access(privconf, F_OK) == 0
1135 && config_include(c, privconf))
1136 return -1;
1137 xfree(privconf);
1138 /* if we have a password file, read it */
1139 if((privconf = config_userconf(getenv("HOME"), pw))
1140 && access(privconf, F_OK) == 0
1141 && config_include(c, privconf))
1142 return -1;
1143 xfree(privconf);
1144 /* install default namepart and transform settings */
c00fce3a 1145 config_postdefaults(c, server);
460b9539 1146 /* everything is good so we shall use the new config */
1147 config_free(config);
1148 config = c;
1149 return 0;
1150}
1151
3f3bb97b 1152/** @brief Return the path to the private configuration file */
460b9539 1153char *config_private(void) {
1154 char *s;
1155
1156 set_configfile();
1157 byte_xasprintf(&s, "%s.private", configfile);
1158 return s;
1159}
1160
3f3bb97b 1161/** @brief Return the path to user's personal configuration file */
460b9539 1162char *config_userconf(const char *home, const struct passwd *pw) {
1163 char *s;
1164
1165 byte_xasprintf(&s, "%s/.disorder/passwd", home ? home : pw->pw_dir);
1166 return s;
1167}
1168
3f3bb97b
RK
1169/** @brief Return the path to user-specific system configuration */
1170char *config_usersysconf(const struct passwd *pw) {
460b9539 1171 char *s;
1172
1173 set_configfile();
1174 if(!strchr(pw->pw_name, '/')) {
1175 byte_xasprintf(&s, "%s.%s", configfile, pw->pw_name);
1176 return s;
1177 } else
1178 return 0;
1179}
1180
1181char *config_get_file(const char *name) {
1182 return get_file(config, name);
1183}
1184
1185/*
1186Local Variables:
1187c-basic-offset:2
1188comment-column:40
1189fill-column:79
1190End:
1191*/