2 * This file is part of DisOrder.
3 * Copyright (C) 2004, 2005, 2006, 2007 Richard Kettlewell
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.
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.
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
20 /** @file lib/configuration.c
21 * @brief Configuration file support
31 #include <sys/types.h>
41 #include "configuration.h"
47 #include "inputline.h"
56 /** @brief Path to config file
58 * set_configfile() sets the deafult if it is null.
62 /** @brief Config file parser state */
64 /** @brief Filename */
66 /** @brief Line number */
68 /** @brief Configuration object under construction */
69 struct config *config;
72 /** @brief Current configuration */
73 struct config *config;
75 /** @brief One configuration item */
77 /** @brief Name as it appears in the config file */
79 /** @brief Offset in @ref config structure */
81 /** @brief Pointer to item type */
82 const struct conftype *type;
83 /** @brief Pointer to item-specific validation routine */
84 int (*validate)(const struct config_state *cs,
85 int nvec, char **vec);
88 /** @brief Type of a configuration item */
90 /** @brief Pointer to function to set item */
91 int (*set)(const struct config_state *cs,
92 const struct conf *whoami,
93 int nvec, char **vec);
94 /** @brief Pointer to function to free item */
95 void (*free)(struct config *c, const struct conf *whoami);
98 /** @brief Compute the address of an item */
99 #define ADDRESS(C, TYPE) ((TYPE *)((char *)(C) + whoami->offset))
100 /** @brief Return the value of an item */
101 #define VALUE(C, TYPE) (*ADDRESS(C, TYPE))
103 static int set_signal(const struct config_state *cs,
104 const struct conf *whoami,
105 int nvec, char **vec) {
109 error(0, "%s:%d: '%s' requires one argument",
110 cs->path, cs->line, whoami->name);
113 if((n = find_signal(vec[0])) == -1) {
114 error(0, "%s:%d: unknown signal '%s'",
115 cs->path, cs->line, vec[0]);
118 VALUE(cs->config, int) = n;
122 static int set_collections(const struct config_state *cs,
123 const struct conf *whoami,
124 int nvec, char **vec) {
125 struct collectionlist *cl;
128 error(0, "%s:%d: '%s' requires three arguments",
129 cs->path, cs->line, whoami->name);
132 if(vec[2][0] != '/') {
133 error(0, "%s:%d: collection root must start with '/'",
137 if(vec[2][1] && vec[2][strlen(vec[2])-1] == '/') {
138 error(0, "%s:%d: collection root must not end with '/'",
142 cl = ADDRESS(cs->config, struct collectionlist);
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]);
151 static int set_boolean(const struct config_state *cs,
152 const struct conf *whoami,
153 int nvec, char **vec) {
157 error(0, "%s:%d: '%s' takes only one argument",
158 cs->path, cs->line, whoami->name);
161 if(!strcmp(vec[0], "yes")) state = 1;
162 else if(!strcmp(vec[0], "no")) state = 0;
164 error(0, "%s:%d: argument to '%s' must be 'yes' or 'no'",
165 cs->path, cs->line, whoami->name);
168 VALUE(cs->config, int) = state;
172 static int set_string(const struct config_state *cs,
173 const struct conf *whoami,
174 int nvec, char **vec) {
176 error(0, "%s:%d: '%s' takes only one argument",
177 cs->path, cs->line, whoami->name);
180 VALUE(cs->config, char *) = xstrdup(vec[0]);
184 static int set_stringlist(const struct config_state *cs,
185 const struct conf *whoami,
186 int nvec, char **vec) {
188 struct stringlist *sl;
190 sl = ADDRESS(cs->config, struct stringlist);
192 for(n = 0; n < nvec; ++n) {
194 sl->s = xrealloc(sl->s, (sl->n * sizeof (char *)));
195 sl->s[sl->n - 1] = xstrdup(vec[n]);
200 static int set_integer(const struct config_state *cs,
201 const struct conf *whoami,
202 int nvec, char **vec) {
206 error(0, "%s:%d: '%s' takes only one argument",
207 cs->path, cs->line, whoami->name);
210 if(xstrtol(ADDRESS(cs->config, long), vec[0], &e, 0)) {
211 error(errno, "%s:%d: converting integer", cs->path, cs->line);
215 error(0, "%s:%d: invalid integer syntax", cs->path, cs->line);
221 static int set_stringlist_accum(const struct config_state *cs,
222 const struct conf *whoami,
223 int nvec, char **vec) {
225 struct stringlist *s;
226 struct stringlistlist *sll;
228 sll = ADDRESS(cs->config, struct stringlistlist);
230 sll->s = xrealloc(sll->s, (sll->n * sizeof (struct stringlist)));
231 s = &sll->s[sll->n - 1];
233 s->s = xmalloc((nvec + 1) * sizeof (char *));
234 for(n = 0; n < nvec; ++n)
235 s->s[n] = xstrdup(vec[n]);
239 static int set_string_accum(const struct config_state *cs,
240 const struct conf *whoami,
241 int nvec, char **vec) {
243 struct stringlist *sl;
245 sl = ADDRESS(cs->config, struct stringlist);
246 for(n = 0; n < nvec; ++n) {
248 sl->s = xrealloc(sl->s, (sl->n * sizeof (char *)));
249 sl->s[sl->n - 1] = xstrdup(vec[n]);
254 static int set_restrict(const struct config_state *cs,
255 const struct conf *whoami,
256 int nvec, char **vec) {
260 static const struct restriction {
264 { "remove", RESTRICT_REMOVE },
265 { "scratch", RESTRICT_SCRATCH },
266 { "move", RESTRICT_MOVE },
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]);
275 r |= restrictions[i].bit;
277 VALUE(cs->config, unsigned) = r;
281 static int parse_sample_format(const struct config_state *cs,
282 struct stream_header *format,
283 int nvec, char **vec) {
288 error(0, "%s:%d: wrong number of arguments", cs->path, cs->line);
291 if(xstrtol(&t, p, &p, 0)) {
292 error(errno, "%s:%d: converting bits-per-sample", cs->path, cs->line);
295 if(t != 8 && t != 16) {
296 error(0, "%s:%d: bad bite-per-sample (%ld)", cs->path, cs->line, t);
299 if(format) format->bits = t;
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;
305 if(format) format->endian = t;
307 error(errno, "%s:%d: expected `/' after bits-per-sample",
312 if(xstrtol(&t, p, &p, 0)) {
313 error(errno, "%s:%d: converting sample-rate", cs->path, cs->line);
316 if(t < 1 || t > INT_MAX) {
317 error(0, "%s:%d: silly sample-rate (%ld)", cs->path, cs->line, t);
320 if(format) format->rate = t;
322 error(0, "%s:%d: expected `/' after sample-rate",
327 if(xstrtol(&t, p, &p, 0)) {
328 error(errno, "%s:%d: converting channels", cs->path, cs->line);
332 error(0, "%s:%d: silly number (%ld) of channels", cs->path, cs->line, t);
335 if(format) format->channels = t;
337 error(0, "%s:%d: junk after channels", cs->path, cs->line);
343 static int set_sample_format(const struct config_state *cs,
344 const struct conf *whoami,
345 int nvec, char **vec) {
346 return parse_sample_format(cs, ADDRESS(cs->config, struct stream_header),
350 static int set_namepart(const struct config_state *cs,
351 const struct conf *whoami,
352 int nvec, char **vec) {
353 struct namepartlist *npl = ADDRESS(cs->config, struct namepartlist);
360 error(0, "%s:%d: namepart needs at least 3 arguments", cs->path, cs->line);
364 error(0, "%s:%d: namepart needs at most 5 arguments", cs->path, cs->line);
367 reflags = nvec >= 5 ? regsub_flags(vec[4]) : 0;
368 if(!(re = pcre_compile(vec[1],
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);
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;
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)
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]);
395 static int set_transform(const struct config_state *cs,
396 const struct conf *whoami,
397 int nvec, char **vec) {
398 struct transformlist *tl = ADDRESS(cs->config, struct transformlist);
405 error(0, "%s:%d: transform needs at least 3 arguments", cs->path, cs->line);
409 error(0, "%s:%d: transform needs at most 5 arguments", cs->path, cs->line);
412 reflags = (nvec >= 5 ? regsub_flags(vec[4]) : 0);
413 if(!(re = pcre_compile(vec[1],
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);
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;
431 static int set_backend(const struct config_state *cs,
432 const struct conf *whoami,
433 int nvec, char **vec) {
434 int *const valuep = ADDRESS(cs->config, int);
437 error(0, "%s:%d: '%s' requires one argument",
438 cs->path, cs->line, whoami->name);
441 if(!strcmp(vec[0], "alsa")) {
442 #if HAVE_ALSA_ASOUNDLIB_H
443 *valuep = BACKEND_ALSA;
445 error(0, "%s:%d: ALSA is not available on this platform",
449 } else if(!strcmp(vec[0], "command"))
450 *valuep = BACKEND_COMMAND;
451 else if(!strcmp(vec[0], "network"))
452 *valuep = BACKEND_NETWORK;
453 else if(!strcmp(vec[0], "coreaudio")) {
454 #if HAVE_COREAUDIO_AUDIOHARDWARE_H
455 *valuep = BACKEND_COREAUDIO;
457 error(0, "%s:%d: Core Audio is not available on this platform",
461 } else if(!strcmp(vec[0], "oss")) {
462 #if HAVE_SYS_SOUNDCARD_H
463 *valuep = BACKEND_OSS;
465 error(0, "%s:%d: OSS is not available on this platform",
470 error(0, "%s:%d: invalid '%s' value '%s'",
471 cs->path, cs->line, whoami->name, vec[0]);
479 static void free_none(struct config attribute((unused)) *c,
480 const struct conf attribute((unused)) *whoami) {
483 static void free_string(struct config *c,
484 const struct conf *whoami) {
485 xfree(VALUE(c, char *));
488 static void free_stringlist(struct config *c,
489 const struct conf *whoami) {
491 struct stringlist *sl = ADDRESS(c, struct stringlist);
493 for(n = 0; n < sl->n; ++n)
498 static void free_stringlistlist(struct config *c,
499 const struct conf *whoami) {
501 struct stringlistlist *sll = ADDRESS(c, struct stringlistlist);
502 struct stringlist *sl;
504 for(n = 0; n < sll->n; ++n) {
506 for(m = 0; m < sl->n; ++m)
513 static void free_collectionlist(struct config *c,
514 const struct conf *whoami) {
515 struct collectionlist *cll = ADDRESS(c, struct collectionlist);
516 struct collection *cl;
519 for(n = 0; n < cll->n; ++n) {
528 static void free_namepartlist(struct config *c,
529 const struct conf *whoami) {
530 struct namepartlist *npl = ADDRESS(c, struct namepartlist);
534 for(n = 0; n < npl->n; ++n) {
537 pcre_free(np->re); /* ...whatever pcre_free is set to. */
544 static void free_transformlist(struct config *c,
545 const struct conf *whoami) {
546 struct transformlist *tl = ADDRESS(c, struct transformlist);
550 for(n = 0; n < tl->n; ++n) {
553 pcre_free(t->re); /* ...whatever pcre_free is set to. */
560 /* configuration types */
562 static const struct conftype
563 type_signal = { set_signal, free_none },
564 type_collections = { set_collections, free_collectionlist },
565 type_boolean = { set_boolean, free_none },
566 type_string = { set_string, free_string },
567 type_stringlist = { set_stringlist, free_stringlist },
568 type_integer = { set_integer, free_none },
569 type_stringlist_accum = { set_stringlist_accum, free_stringlistlist },
570 type_string_accum = { set_string_accum, free_stringlist },
571 type_sample_format = { set_sample_format, free_none },
572 type_restrict = { set_restrict, free_none },
573 type_namepart = { set_namepart, free_namepartlist },
574 type_transform = { set_transform, free_transformlist },
575 type_backend = { set_backend, free_none };
577 /* specific validation routine */
579 #define VALIDATE_FILE(test, what) do { \
583 for(n = 0; n < nvec; ++n) { \
584 if(stat(vec[n], &sb) < 0) { \
585 error(errno, "%s:%d: %s", cs->path, cs->line, vec[n]); \
588 if(!test(sb.st_mode)) { \
589 error(0, "%s:%d: %s is not a %s", \
590 cs->path, cs->line, vec[n], what); \
596 static int validate_isabspath(const struct config_state *cs,
597 int nvec, char **vec) {
600 for(n = 0; n < nvec; ++n)
601 if(vec[n][0] != '/') {
602 error(errno, "%s:%d: %s: not an absolute path",
603 cs->path, cs->line, vec[n]);
609 static int validate_isdir(const struct config_state *cs,
610 int nvec, char **vec) {
611 VALIDATE_FILE(S_ISDIR, "directory");
615 static int validate_isreg(const struct config_state *cs,
616 int nvec, char **vec) {
617 VALIDATE_FILE(S_ISREG, "regular file");
621 static int validate_ischr(const struct config_state *cs,
622 int nvec, char **vec) {
623 VALIDATE_FILE(S_ISCHR, "character device");
627 static int validate_player(const struct config_state *cs,
629 char attribute((unused)) **vec) {
631 error(0, "%s:%d: should be at least 'player PATTERN MODULE'",
638 static int validate_allow(const struct config_state *cs,
640 char attribute((unused)) **vec) {
642 error(0, "%s:%d: must be 'allow NAME PASS'", cs->path, cs->line);
648 static int validate_non_negative(const struct config_state *cs,
649 int nvec, char **vec) {
653 error(0, "%s:%d: missing argument", cs->path, cs->line);
657 error(0, "%s:%d: too many arguments", cs->path, cs->line);
660 if(xstrtol(&n, vec[0], 0, 0)) {
661 error(0, "%s:%d: %s", cs->path, cs->line, strerror(errno));
665 error(0, "%s:%d: must not be negative", cs->path, cs->line);
671 static int validate_positive(const struct config_state *cs,
672 int nvec, char **vec) {
676 error(0, "%s:%d: missing argument", cs->path, cs->line);
680 error(0, "%s:%d: too many arguments", cs->path, cs->line);
683 if(xstrtol(&n, vec[0], 0, 0)) {
684 error(0, "%s:%d: %s", cs->path, cs->line, strerror(errno));
688 error(0, "%s:%d: must be positive", cs->path, cs->line);
694 static int validate_isauser(const struct config_state *cs,
695 int attribute((unused)) nvec,
699 if(!(pw = getpwnam(vec[0]))) {
700 error(0, "%s:%d: no such user as '%s'", cs->path, cs->line, vec[0]);
706 static int validate_sample_format(const struct config_state *cs,
707 int attribute((unused)) nvec,
709 return parse_sample_format(cs, 0, nvec, vec);
712 static int validate_channel(const struct config_state *cs,
713 int attribute((unused)) nvec,
715 if(mixer_channel(vec[0]) == -1) {
716 error(0, "%s:%d: invalid channel '%s'", cs->path, cs->line, vec[0]);
722 static int validate_any(const struct config_state attribute((unused)) *cs,
723 int attribute((unused)) nvec,
724 char attribute((unused)) **vec) {
728 static int validate_url(const struct config_state attribute((unused)) *cs,
729 int attribute((unused)) nvec,
733 /* absoluteURI = scheme ":" ( hier_part | opaque_part )
734 scheme = alpha *( alpha | digit | "+" | "-" | "." ) */
736 n = strspn(s, ("abcdefghijklmnopqrstuvwxyz"
737 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
740 error(0, "%s:%d: invalid url '%s'", cs->path, cs->line, vec[0]);
743 if(!strncmp(s, "http:", 5)
744 || !strncmp(s, "https:", 6)) {
746 /* we only do a rather cursory check */
747 if(strncmp(s, "//", 2)) {
748 error(0, "%s:%d: invalid url '%s'", cs->path, cs->line, vec[0]);
755 static int validate_alias(const struct config_state *cs,
759 int in_brackets = 0, c;
762 error(0, "%s:%d: missing argument", cs->path, cs->line);
766 error(0, "%s:%d: too many arguments", cs->path, cs->line);
770 while((c = (unsigned char)*s++)) {
774 else if(!isalnum(c)) {
775 error(0, "%s:%d: invalid part name in alias expansion in '%s'",
776 cs->path, cs->line, vec[0]);
784 } else if(c == '\\') {
785 if(!(c = (unsigned char)*s++)) {
786 error(0, "%s:%d: unterminated escape in alias expansion in '%s'",
787 cs->path, cs->line, vec[0]);
789 } else if(c != '\\' && c != '{') {
790 error(0, "%s:%d: invalid escape in alias expansion in '%s'",
791 cs->path, cs->line, vec[0]);
799 error(0, "%s:%d: unterminated part name in alias expansion in '%s'",
800 cs->path, cs->line, vec[0]);
806 static int validate_addrport(const struct config_state attribute((unused)) *cs,
808 char attribute((unused)) **vec) {
811 error(0, "%s:%d: missing address",
815 error(0, "%s:%d: missing port name/number",
821 error(0, "%s:%d: expected ADDRESS PORT",
827 static int validate_port(const struct config_state attribute((unused)) *cs,
829 char attribute((unused)) **vec) {
832 error(0, "%s:%d: missing address",
839 error(0, "%s:%d: expected [ADDRESS] PORT",
845 static int validate_algo(const struct config_state attribute((unused)) *cs,
849 error(0, "%s:%d: invalid algorithm specification", cs->path, cs->line);
852 if(!valid_authhash(vec[0])) {
853 error(0, "%s:%d: unsuported algorithm '%s'", cs->path, cs->line, vec[0]);
859 /** @brief Item name and and offset */
860 #define C(x) #x, offsetof(struct config, x)
861 /** @brief Item name and and offset */
862 #define C2(x,y) #x, offsetof(struct config, y)
864 /** @brief All configuration items */
865 static const struct conf conf[] = {
866 { C(alias), &type_string, validate_alias },
867 { C(allow), &type_stringlist_accum, validate_allow },
868 { C(authorization_algorithm), &type_string, validate_algo },
869 { C(broadcast), &type_stringlist, validate_addrport },
870 { C(broadcast_from), &type_stringlist, validate_addrport },
871 { C(channel), &type_string, validate_channel },
872 { C(checkpoint_kbyte), &type_integer, validate_non_negative },
873 { C(checkpoint_min), &type_integer, validate_non_negative },
874 { C(collection), &type_collections, validate_any },
875 { C(connect), &type_stringlist, validate_addrport },
876 { C(device), &type_string, validate_any },
877 { C(gap), &type_integer, validate_non_negative },
878 { C(history), &type_integer, validate_positive },
879 { C(home), &type_string, validate_isabspath },
880 { C(listen), &type_stringlist, validate_port },
881 { C(lock), &type_boolean, validate_any },
882 { C(mixer), &type_string, validate_ischr },
883 { C(multicast_ttl), &type_integer, validate_non_negative },
884 { C(namepart), &type_namepart, validate_any },
885 { C2(nice, nice_rescan), &type_integer, validate_non_negative },
886 { C(nice_rescan), &type_integer, validate_non_negative },
887 { C(nice_server), &type_integer, validate_any },
888 { C(nice_speaker), &type_integer, validate_any },
889 { C(noticed_history), &type_integer, validate_positive },
890 { C(password), &type_string, validate_any },
891 { C(player), &type_stringlist_accum, validate_player },
892 { C(plugins), &type_string_accum, validate_isdir },
893 { C(prefsync), &type_integer, validate_positive },
894 { C(queue_pad), &type_integer, validate_positive },
895 { C(refresh), &type_integer, validate_positive },
896 { C2(restrict, restrictions), &type_restrict, validate_any },
897 { C(sample_format), &type_sample_format, validate_sample_format },
898 { C(scratch), &type_string_accum, validate_isreg },
899 { C(signal), &type_signal, validate_any },
900 { C(sox_generation), &type_integer, validate_non_negative },
901 { C(speaker_backend), &type_backend, validate_any },
902 { C(speaker_command), &type_string, validate_any },
903 { C(stopword), &type_string_accum, validate_any },
904 { C(templates), &type_string_accum, validate_isdir },
905 { C(transform), &type_transform, validate_any },
906 { C(trust), &type_string_accum, validate_any },
907 { C(url), &type_string, validate_url },
908 { C(user), &type_string, validate_isauser },
909 { C(username), &type_string, validate_any },
912 /** @brief Find a configuration item's definition by key */
913 static const struct conf *find(const char *key) {
916 if((n = TABLE_FIND(conf, struct conf, name, key)) < 0)
921 /** @brief Set a new configuration value */
922 static int config_set(const struct config_state *cs,
923 int nvec, char **vec) {
924 const struct conf *which;
926 D(("config_set %s", vec[0]));
927 if(!(which = find(vec[0]))) {
928 error(0, "%s:%d: unknown configuration key '%s'",
929 cs->path, cs->line, vec[0]);
932 return (which->validate(cs, nvec - 1, vec + 1)
933 || which->type->set(cs, which, nvec - 1, vec + 1));
936 /** @brief Error callback used by config_include() */
937 static void config_error(const char *msg, void *u) {
938 const struct config_state *cs = u;
940 error(0, "%s:%d: %s", cs->path, cs->line, msg);
943 /** @brief Include a file by name */
944 static int config_include(struct config *c, const char *path) {
946 char *buffer, *inputbuffer, **vec;
948 struct config_state cs;
953 D(("%s: reading configuration", path));
954 if(!(fp = fopen(path, "r"))) {
955 error(errno, "error opening %s", path);
958 while(!inputline(path, fp, &inputbuffer, '\n')) {
960 if(!(buffer = mb2utf8(inputbuffer))) {
961 error(errno, "%s:%d: cannot convert to UTF-8", cs.path, cs.line);
967 if(!(vec = split(buffer, &n, SPLIT_COMMENTS|SPLIT_QUOTES,
968 config_error, &cs))) {
974 if(!strcmp(vec[0], "include")) {
976 error(0, "%s:%d: must be 'include PATH'", cs.path, cs.line);
979 config_include(c, vec[1]);
981 ret |= config_set(&cs, n, vec);
983 for(n = 0; vec[n]; ++n) xfree(vec[n]);
988 error(errno, "error reading %s", path);
995 /** @brief Make a new default configuration */
996 static struct config *config_default(void) {
997 struct config *c = xmalloc(sizeof *c);
1001 /* Strings had better be xstrdup'd as they will get freed at some point. */
1004 c->home = xstrdup(pkgstatedir);
1005 if(!(pw = getpwuid(getuid())))
1006 fatal(0, "cannot determine our username");
1007 logname = pw->pw_name;
1008 c->username = xstrdup(logname);
1011 c->signal = SIGKILL;
1012 c->alias = xstrdup("{/artist}{/album}{/title}{ext}");
1014 c->device = xstrdup("default");
1015 c->nice_rescan = 10;
1016 c->speaker_command = 0;
1017 c->sample_format.bits = 16;
1018 c->sample_format.rate = 44100;
1019 c->sample_format.channels = 2;
1020 c->sample_format.endian = ENDIAN_NATIVE;
1022 c->speaker_backend = -1;
1023 c->multicast_ttl = 1;
1024 c->authorization_algorithm = xstrdup("sha1");
1025 c->noticed_history = 31;
1029 static char *get_file(struct config *c, const char *name) {
1032 byte_xasprintf(&s, "%s/%s", c->home, name);
1036 /** @brief Set the default configuration file */
1037 static void set_configfile(void) {
1039 byte_xasprintf(&configfile, "%s/config", pkgconfdir);
1042 /** @brief Free a configuration object */
1043 static void config_free(struct config *c) {
1047 for(n = 0; n < (int)(sizeof conf / sizeof *conf); ++n)
1048 conf[n].type->free(c, &conf[n]);
1049 for(n = 0; n < c->nparts; ++n)
1056 /** @brief Set post-parse defaults */
1057 static void config_postdefaults(struct config *c,
1059 struct config_state cs;
1060 const struct conf *whoami;
1063 static const char *namepart[][4] = {
1064 { "title", "/([0-9]+ *[-:] *)?([^/]+)\\.[a-zA-Z0-9]+$", "$2", "display" },
1065 { "title", "/([^/]+)\\.[a-zA-Z0-9]+$", "$1", "sort" },
1066 { "album", "/([^/]+)/[^/]+$", "$1", "*" },
1067 { "artist", "/([^/]+)/[^/]+/[^/]+$", "$1", "*" },
1068 { "ext", "(\\.[a-zA-Z0-9]+)$", "$1", "*" },
1070 #define NNAMEPART (int)(sizeof namepart / sizeof *namepart)
1072 static const char *transform[][5] = {
1073 { "track", "^.*/([0-9]+ *[-:] *)?([^/]+)\\.[a-zA-Z0-9]+$", "$2", "display", "" },
1074 { "track", "^.*/([^/]+)\\.[a-zA-Z0-9]+$", "$1", "sort", "" },
1075 { "dir", "^.*/([^/]+)$", "$1", "*", "" },
1076 { "dir", "^(the) ([^/]*)", "$2, $1", "sort", "i", },
1077 { "dir", "[[:punct:]]", "", "sort", "g", }
1079 #define NTRANSFORM (int)(sizeof transform / sizeof *transform)
1081 cs.path = "<internal>";
1084 if(!c->namepart.n) {
1085 whoami = find("namepart");
1086 for(n = 0; n < NNAMEPART; ++n)
1087 set_namepart(&cs, whoami, 4, (char **)namepart[n]);
1089 if(!c->transform.n) {
1090 whoami = find("transform");
1091 for(n = 0; n < NTRANSFORM; ++n)
1092 set_transform(&cs, whoami, 5, (char **)transform[n]);
1094 if(c->speaker_backend == -1) {
1095 if(c->speaker_command)
1096 c->speaker_backend = BACKEND_COMMAND;
1097 else if(c->broadcast.n)
1098 c->speaker_backend = BACKEND_NETWORK;
1100 #if HAVE_ALSA_ASOUNDLIB_H
1101 c->speaker_backend = BACKEND_ALSA;
1102 #elif HAVE_SYS_SOUNDCARD_H
1103 c->speaker_backend = BACKEND_OSS;
1104 #elif HAVE_COREAUDIO_AUDIOHARDWARE_H
1105 c->speaker_backend = BACKEND_COREAUDIO;
1107 c->speaker_backend = BACKEND_COMMAND;
1112 if(c->speaker_backend == BACKEND_COMMAND && !c->speaker_command)
1113 fatal(0, "speaker_backend is command but speaker_command is not set");
1114 if(c->speaker_backend == BACKEND_NETWORK && !c->broadcast.n)
1115 fatal(0, "speaker_backend is network but broadcast is not set");
1117 /* Override sample format */
1118 switch(c->speaker_backend) {
1119 case BACKEND_NETWORK:
1120 c->sample_format.rate = 44100;
1121 c->sample_format.channels = 2;
1122 c->sample_format.bits = 16;
1123 c->sample_format.endian = ENDIAN_BIG;
1125 case BACKEND_COREAUDIO:
1126 c->sample_format.rate = 44100;
1127 c->sample_format.channels = 2;
1128 c->sample_format.bits = 16;
1129 c->sample_format.endian = ENDIAN_NATIVE;
1134 /** @brief (Re-)read the config file
1135 * @param server If set, do extra checking
1137 int config_read(int server) {
1143 c = config_default();
1144 /* standalone Disobedience installs might not have a global config file */
1145 if(access(configfile, F_OK) == 0)
1146 if(config_include(c, configfile))
1148 /* if we can read the private config file, do */
1149 if((privconf = config_private())
1150 && access(privconf, R_OK) == 0
1151 && config_include(c, privconf))
1154 /* if there's a per-user system config file for this user, read it */
1155 if(!(pw = getpwuid(getuid())))
1156 fatal(0, "cannot determine our username");
1157 if((privconf = config_usersysconf(pw))
1158 && access(privconf, F_OK) == 0
1159 && config_include(c, privconf))
1162 /* if we have a password file, read it */
1163 if((privconf = config_userconf(getenv("HOME"), pw))
1164 && access(privconf, F_OK) == 0
1165 && config_include(c, privconf))
1168 /* install default namepart and transform settings */
1169 config_postdefaults(c, server);
1170 /* everything is good so we shall use the new config */
1171 config_free(config);
1176 /** @brief Return the path to the private configuration file */
1177 char *config_private(void) {
1181 byte_xasprintf(&s, "%s.private", configfile);
1185 /** @brief Return the path to user's personal configuration file */
1186 char *config_userconf(const char *home, const struct passwd *pw) {
1189 byte_xasprintf(&s, "%s/.disorder/passwd", home ? home : pw->pw_dir);
1193 /** @brief Return the path to user-specific system configuration */
1194 char *config_usersysconf(const struct passwd *pw) {
1198 if(!strchr(pw->pw_name, '/')) {
1199 byte_xasprintf(&s, "%s.%s", configfile, pw->pw_name);
1205 char *config_get_file(const char *name) {
1206 return get_file(config, name);