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
28 #include <sys/types.h>
38 #include "configuration.h"
44 #include "inputline.h"
57 struct config *config;
60 struct config *config;
65 const struct conftype *type;
66 int (*validate)(const struct config_state *cs,
67 int nvec, char **vec);
71 int (*set)(const struct config_state *cs,
72 const struct conf *whoami,
73 int nvec, char **vec);
74 void (*free)(struct config *c, const struct conf *whoami);
77 #define ADDRESS(C, TYPE) ((TYPE *)((char *)(C) + whoami->offset))
78 #define VALUE(C, TYPE) (*ADDRESS(C, TYPE))
80 static int set_signal(const struct config_state *cs,
81 const struct conf *whoami,
82 int nvec, char **vec) {
86 error(0, "%s:%d: '%s' requires one argument",
87 cs->path, cs->line, whoami->name);
90 if((n = find_signal(vec[0])) == -1) {
91 error(0, "%s:%d: unknown signal '%s'",
92 cs->path, cs->line, vec[0]);
95 VALUE(cs->config, int) = n;
99 static int set_collections(const struct config_state *cs,
100 const struct conf *whoami,
101 int nvec, char **vec) {
102 struct collectionlist *cl;
105 error(0, "%s:%d: '%s' requires three arguments",
106 cs->path, cs->line, whoami->name);
109 if(vec[2][0] != '/') {
110 error(0, "%s:%d: collection root must start with '/'",
114 if(vec[2][1] && vec[2][strlen(vec[2])-1] == '/') {
115 error(0, "%s:%d: collection root must not end with '/'",
119 cl = ADDRESS(cs->config, struct collectionlist);
121 cl->s = xrealloc(cl->s, cl->n * sizeof (struct collection));
122 cl->s[cl->n - 1].module = xstrdup(vec[0]);
123 cl->s[cl->n - 1].encoding = xstrdup(vec[1]);
124 cl->s[cl->n - 1].root = xstrdup(vec[2]);
128 static int set_boolean(const struct config_state *cs,
129 const struct conf *whoami,
130 int nvec, char **vec) {
134 error(0, "%s:%d: '%s' takes only one argument",
135 cs->path, cs->line, whoami->name);
138 if(!strcmp(vec[0], "yes")) state = 1;
139 else if(!strcmp(vec[0], "no")) state = 0;
141 error(0, "%s:%d: argument to '%s' must be 'yes' or 'no'",
142 cs->path, cs->line, whoami->name);
145 VALUE(cs->config, int) = state;
149 static int set_string(const struct config_state *cs,
150 const struct conf *whoami,
151 int nvec, char **vec) {
153 error(0, "%s:%d: '%s' takes only one argument",
154 cs->path, cs->line, whoami->name);
157 VALUE(cs->config, char *) = xstrdup(vec[0]);
161 static int set_stringlist(const struct config_state *cs,
162 const struct conf *whoami,
163 int nvec, char **vec) {
165 struct stringlist *sl;
167 sl = ADDRESS(cs->config, struct stringlist);
169 for(n = 0; n < nvec; ++n) {
171 sl->s = xrealloc(sl->s, (sl->n * sizeof (char *)));
172 sl->s[sl->n - 1] = xstrdup(vec[n]);
177 static int set_integer(const struct config_state *cs,
178 const struct conf *whoami,
179 int nvec, char **vec) {
183 error(0, "%s:%d: '%s' takes only one argument",
184 cs->path, cs->line, whoami->name);
187 if(xstrtol(ADDRESS(cs->config, long), vec[0], &e, 0)) {
188 error(errno, "%s:%d: converting integer", cs->path, cs->line);
192 error(0, "%s:%d: invalid integer syntax", cs->path, cs->line);
198 static int set_stringlist_accum(const struct config_state *cs,
199 const struct conf *whoami,
200 int nvec, char **vec) {
202 struct stringlist *s;
203 struct stringlistlist *sll;
205 sll = ADDRESS(cs->config, struct stringlistlist);
207 sll->s = xrealloc(sll->s, (sll->n * sizeof (struct stringlist)));
208 s = &sll->s[sll->n - 1];
210 s->s = xmalloc((nvec + 1) * sizeof (char *));
211 for(n = 0; n < nvec; ++n)
212 s->s[n] = xstrdup(vec[n]);
216 static int set_string_accum(const struct config_state *cs,
217 const struct conf *whoami,
218 int nvec, char **vec) {
220 struct stringlist *sl;
222 sl = ADDRESS(cs->config, struct stringlist);
223 for(n = 0; n < nvec; ++n) {
225 sl->s = xrealloc(sl->s, (sl->n * sizeof (char *)));
226 sl->s[sl->n - 1] = xstrdup(vec[n]);
231 static int set_restrict(const struct config_state *cs,
232 const struct conf *whoami,
233 int nvec, char **vec) {
237 static const struct restriction {
241 { "remove", RESTRICT_REMOVE },
242 { "scratch", RESTRICT_SCRATCH },
243 { "move", RESTRICT_MOVE },
246 for(n = 0; n < nvec; ++n) {
247 if((i = TABLE_FIND(restrictions, struct restriction, name, vec[n])) < 0) {
248 error(0, "%s:%d: invalid restriction '%s'",
249 cs->path, cs->line, vec[n]);
252 r |= restrictions[i].bit;
254 VALUE(cs->config, unsigned) = r;
258 static int parse_sample_format(const struct config_state *cs,
259 ao_sample_format *ao,
260 int nvec, char **vec) {
265 error(0, "%s:%d: wrong number of arguments", cs->path, cs->line);
268 if (xstrtol(&t, p, &p, 0)) {
269 error(errno, "%s:%d: converting bits-per-sample", cs->path, cs->line);
272 if (t != 8 && t != 16) {
273 error(0, "%s:%d: bad bite-per-sample (%ld)", cs->path, cs->line, t);
276 if (ao) ao->bits = t;
278 case 'l': case 'L': t = AO_FMT_LITTLE; p++; break;
279 case 'b': case 'B': t = AO_FMT_BIG; p++; break;
280 default: t = AO_FMT_NATIVE; break;
282 if (ao) ao->byte_format = t;
284 error(errno, "%s:%d: expected `/' after bits-per-sample",
289 if (xstrtol(&t, p, &p, 0)) {
290 error(errno, "%s:%d: converting sample-rate", cs->path, cs->line);
293 if (t < 1 || t > INT_MAX) {
294 error(0, "%s:%d: silly sample-rate (%ld)", cs->path, cs->line, t);
297 if (ao) ao->rate = t;
299 error(0, "%s:%d: expected `/' after sample-rate",
304 if (xstrtol(&t, p, &p, 0)) {
305 error(errno, "%s:%d: converting channels", cs->path, cs->line);
308 if (t < 1 || t > 8) {
309 error(0, "%s:%d: silly number (%ld) of channels", cs->path, cs->line, t);
312 if (ao) ao->channels = t;
314 error(0, "%s:%d: junk after channels", cs->path, cs->line);
320 static int set_sample_format(const struct config_state *cs,
321 const struct conf *whoami,
322 int nvec, char **vec) {
323 return parse_sample_format(cs, ADDRESS(cs->config, ao_sample_format),
327 static int set_namepart(const struct config_state *cs,
328 const struct conf *whoami,
329 int nvec, char **vec) {
330 struct namepartlist *npl = ADDRESS(cs->config, struct namepartlist);
337 error(0, "%s:%d: namepart needs at least 3 arguments", cs->path, cs->line);
341 error(0, "%s:%d: namepart needs at most 5 arguments", cs->path, cs->line);
344 reflags = nvec >= 5 ? regsub_flags(vec[4]) : 0;
345 if(!(re = pcre_compile(vec[1],
347 |regsub_compile_options(reflags),
348 &errstr, &erroffset, 0))) {
349 error(0, "%s:%d: error compiling regexp /%s/: %s (offset %d)",
350 cs->path, cs->line, vec[1], errstr, erroffset);
353 npl->s = xrealloc(npl->s, (npl->n + 1) * sizeof (struct namepart));
354 npl->s[npl->n].part = xstrdup(vec[0]);
355 npl->s[npl->n].re = re;
356 npl->s[npl->n].replace = xstrdup(vec[2]);
357 npl->s[npl->n].context = xstrdup(vec[3]);
358 npl->s[npl->n].reflags = reflags;
360 /* XXX a bit of a bodge; relies on there being very few parts. */
361 for(n = 0; (n < cs->config->nparts
362 && strcmp(cs->config->parts[n], vec[0])); ++n)
364 if(n >= cs->config->nparts) {
365 cs->config->parts = xrealloc(cs->config->parts,
366 (cs->config->nparts + 1) * sizeof (char *));
367 cs->config->parts[cs->config->nparts++] = xstrdup(vec[0]);
372 static int set_transform(const struct config_state *cs,
373 const struct conf *whoami,
374 int nvec, char **vec) {
375 struct transformlist *tl = ADDRESS(cs->config, struct transformlist);
382 error(0, "%s:%d: transform needs at least 3 arguments", cs->path, cs->line);
386 error(0, "%s:%d: transform needs at most 5 arguments", cs->path, cs->line);
389 reflags = (nvec >= 5 ? regsub_flags(vec[4]) : 0);
390 if(!(re = pcre_compile(vec[1],
392 |regsub_compile_options(reflags),
393 &errstr, &erroffset, 0))) {
394 error(0, "%s:%d: error compiling regexp /%s/: %s (offset %d)",
395 cs->path, cs->line, vec[1], errstr, erroffset);
398 tl->t = xrealloc(tl->t, (tl->n + 1) * sizeof (struct namepart));
399 tl->t[tl->n].type = xstrdup(vec[0]);
400 tl->t[tl->n].context = xstrdup(vec[3] ? vec[3] : "*");
401 tl->t[tl->n].re = re;
402 tl->t[tl->n].replace = xstrdup(vec[2]);
403 tl->t[tl->n].flags = reflags;
408 static int set_backend(const struct config_state *cs,
409 const struct conf *whoami,
410 int nvec, char **vec) {
411 int *const valuep = ADDRESS(cs->config, int);
414 error(0, "%s:%d: '%s' requires one argument",
415 cs->path, cs->line, whoami->name);
418 if(!strcmp(vec[0], "alsa")) {
420 *valuep = BACKEND_ALSA;
422 error(0, "%s:%d: ALSA is not available on this platform",
426 } else if(!strcmp(vec[0], "command"))
427 *valuep = BACKEND_COMMAND;
428 else if(!strcmp(vec[0], "network"))
429 *valuep = BACKEND_NETWORK;
431 error(0, "%s:%d: invalid '%s' value '%s'",
432 cs->path, cs->line, whoami->name, vec[0]);
440 static void free_none(struct config attribute((unused)) *c,
441 const struct conf attribute((unused)) *whoami) {
444 static void free_string(struct config *c,
445 const struct conf *whoami) {
446 xfree(VALUE(c, char *));
449 static void free_stringlist(struct config *c,
450 const struct conf *whoami) {
452 struct stringlist *sl = ADDRESS(c, struct stringlist);
454 for(n = 0; n < sl->n; ++n)
459 static void free_stringlistlist(struct config *c,
460 const struct conf *whoami) {
462 struct stringlistlist *sll = ADDRESS(c, struct stringlistlist);
463 struct stringlist *sl;
465 for(n = 0; n < sll->n; ++n) {
467 for(m = 0; m < sl->n; ++m)
474 static void free_collectionlist(struct config *c,
475 const struct conf *whoami) {
476 struct collectionlist *cll = ADDRESS(c, struct collectionlist);
477 struct collection *cl;
480 for(n = 0; n < cll->n; ++n) {
489 static void free_namepartlist(struct config *c,
490 const struct conf *whoami) {
491 struct namepartlist *npl = ADDRESS(c, struct namepartlist);
495 for(n = 0; n < npl->n; ++n) {
498 pcre_free(np->re); /* ...whatever pcre_free is set to. */
505 static void free_transformlist(struct config *c,
506 const struct conf *whoami) {
507 struct transformlist *tl = ADDRESS(c, struct transformlist);
511 for(n = 0; n < tl->n; ++n) {
514 pcre_free(t->re); /* ...whatever pcre_free is set to. */
521 /* configuration types */
523 static const struct conftype
524 type_signal = { set_signal, free_none },
525 type_collections = { set_collections, free_collectionlist },
526 type_boolean = { set_boolean, free_none },
527 type_string = { set_string, free_string },
528 type_stringlist = { set_stringlist, free_stringlist },
529 type_integer = { set_integer, free_none },
530 type_stringlist_accum = { set_stringlist_accum, free_stringlistlist },
531 type_string_accum = { set_string_accum, free_stringlist },
532 type_sample_format = { set_sample_format, free_none },
533 type_restrict = { set_restrict, free_none },
534 type_namepart = { set_namepart, free_namepartlist },
535 type_transform = { set_transform, free_transformlist },
536 type_backend = { set_backend, free_none };
538 /* specific validation routine */
540 #define VALIDATE_FILE(test, what) do { \
544 for(n = 0; n < nvec; ++n) { \
545 if(stat(vec[n], &sb) < 0) { \
546 error(errno, "%s:%d: %s", cs->path, cs->line, vec[n]); \
549 if(!test(sb.st_mode)) { \
550 error(0, "%s:%d: %s is not a %s", \
551 cs->path, cs->line, vec[n], what); \
557 static int validate_isdir(const struct config_state *cs,
558 int nvec, char **vec) {
559 VALIDATE_FILE(S_ISDIR, "directory");
563 static int validate_isreg(const struct config_state *cs,
564 int nvec, char **vec) {
565 VALIDATE_FILE(S_ISREG, "regular file");
569 static int validate_ischr(const struct config_state *cs,
570 int nvec, char **vec) {
571 VALIDATE_FILE(S_ISCHR, "character device");
575 static int validate_player(const struct config_state *cs,
577 char attribute((unused)) **vec) {
579 error(0, "%s:%d: should be at least 'player PATTERN MODULE'",
586 static int validate_allow(const struct config_state *cs,
588 char attribute((unused)) **vec) {
590 error(0, "%s:%d: must be 'allow NAME PASS'", cs->path, cs->line);
596 static int validate_non_negative(const struct config_state *cs,
597 int nvec, char **vec) {
601 error(0, "%s:%d: missing argument", cs->path, cs->line);
605 error(0, "%s:%d: too many arguments", cs->path, cs->line);
608 if(xstrtol(&n, vec[0], 0, 0)) {
609 error(0, "%s:%d: %s", cs->path, cs->line, strerror(errno));
613 error(0, "%s:%d: must not be negative", cs->path, cs->line);
619 static int validate_positive(const struct config_state *cs,
620 int nvec, char **vec) {
624 error(0, "%s:%d: missing argument", cs->path, cs->line);
628 error(0, "%s:%d: too many arguments", cs->path, cs->line);
631 if(xstrtol(&n, vec[0], 0, 0)) {
632 error(0, "%s:%d: %s", cs->path, cs->line, strerror(errno));
636 error(0, "%s:%d: must be positive", cs->path, cs->line);
642 static int validate_isauser(const struct config_state *cs,
643 int attribute((unused)) nvec,
647 if(!(pw = getpwnam(vec[0]))) {
648 error(0, "%s:%d: no such user as '%s'", cs->path, cs->line, vec[0]);
654 static int validate_sample_format(const struct config_state *cs,
655 int attribute((unused)) nvec,
657 return parse_sample_format(cs, 0, nvec, vec);
660 static int validate_channel(const struct config_state *cs,
661 int attribute((unused)) nvec,
663 if(mixer_channel(vec[0]) == -1) {
664 error(0, "%s:%d: invalid channel '%s'", cs->path, cs->line, vec[0]);
670 static int validate_any(const struct config_state attribute((unused)) *cs,
671 int attribute((unused)) nvec,
672 char attribute((unused)) **vec) {
676 static int validate_url(const struct config_state attribute((unused)) *cs,
677 int attribute((unused)) nvec,
681 /* absoluteURI = scheme ":" ( hier_part | opaque_part )
682 scheme = alpha *( alpha | digit | "+" | "-" | "." ) */
684 n = strspn(s, ("abcdefghijklmnopqrstuvwxyz"
685 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
688 error(0, "%s:%d: invalid url '%s'", cs->path, cs->line, vec[0]);
691 if(!strncmp(s, "http:", 5)
692 || !strncmp(s, "https:", 6)) {
694 /* we only do a rather cursory check */
695 if(strncmp(s, "//", 2)) {
696 error(0, "%s:%d: invalid url '%s'", cs->path, cs->line, vec[0]);
703 static int validate_alias(const struct config_state *cs,
707 int in_brackets = 0, c;
710 error(0, "%s:%d: missing argument", cs->path, cs->line);
714 error(0, "%s:%d: too many arguments", cs->path, cs->line);
718 while((c = (unsigned char)*s++)) {
722 else if(!isalnum(c)) {
723 error(0, "%s:%d: invalid part name in alias expansion in '%s'",
724 cs->path, cs->line, vec[0]);
732 } else if(c == '\\') {
733 if(!(c = (unsigned char)*s++)) {
734 error(0, "%s:%d: unterminated escape in alias expansion in '%s'",
735 cs->path, cs->line, vec[0]);
737 } else if(c != '\\' && c != '{') {
738 error(0, "%s:%d: invalid escape in alias expansion in '%s'",
739 cs->path, cs->line, vec[0]);
747 error(0, "%s:%d: unterminated part name in alias expansion in '%s'",
748 cs->path, cs->line, vec[0]);
754 static int validate_addrport(const struct config_state attribute((unused)) *cs,
756 char attribute((unused)) **vec) {
759 error(0, "%s:%d: missing address",
763 error(0, "%s:%d: missing port name/number",
769 error(0, "%s:%d: expected ADDRESS PORT",
775 static int validate_address(const struct config_state attribute((unused)) *cs,
777 char attribute((unused)) **vec) {
780 error(0, "%s:%d: missing address",
787 error(0, "%s:%d: expected ADDRESS PORT",
793 /* configuration table */
795 #define C(x) #x, offsetof(struct config, x)
796 #define C2(x,y) #x, offsetof(struct config, y)
798 static const struct conf conf[] = {
799 { C(alias), &type_string, validate_alias },
800 { C(allow), &type_stringlist_accum, validate_allow },
801 { C(broadcast), &type_stringlist, validate_addrport },
802 { C(broadcast_from), &type_stringlist, validate_address },
803 { C(channel), &type_string, validate_channel },
804 { C(checkpoint_kbyte), &type_integer, validate_non_negative },
805 { C(checkpoint_min), &type_integer, validate_non_negative },
806 { C(collection), &type_collections, validate_any },
807 { C(connect), &type_stringlist, validate_addrport },
808 { C(device), &type_string, validate_any },
809 { C(gap), &type_integer, validate_non_negative },
810 { C(history), &type_integer, validate_positive },
811 { C(home), &type_string, validate_isdir },
812 { C(listen), &type_stringlist, validate_addrport },
813 { C(lock), &type_boolean, validate_any },
814 { C(mixer), &type_string, validate_ischr },
815 { C(namepart), &type_namepart, validate_any },
816 { C2(nice, nice_rescan), &type_integer, validate_non_negative },
817 { C(nice_rescan), &type_integer, validate_non_negative },
818 { C(nice_server), &type_integer, validate_any },
819 { C(nice_speaker), &type_integer, validate_any },
820 { C(password), &type_string, validate_any },
821 { C(player), &type_stringlist_accum, validate_player },
822 { C(plugins), &type_string_accum, validate_isdir },
823 { C(prefsync), &type_integer, validate_positive },
824 { C(queue_pad), &type_integer, validate_positive },
825 { C(refresh), &type_integer, validate_positive },
826 { C2(restrict, restrictions), &type_restrict, validate_any },
827 { C(sample_format), &type_sample_format, validate_sample_format },
828 { C(scratch), &type_string_accum, validate_isreg },
829 { C(signal), &type_signal, validate_any },
830 { C(sox_generation), &type_integer, validate_non_negative },
831 { C(speaker_backend), &type_backend, validate_any },
832 { C(speaker_command), &type_string, validate_any },
833 { C(stopword), &type_string_accum, validate_any },
834 { C(templates), &type_string_accum, validate_isdir },
835 { C(transform), &type_transform, validate_any },
836 { C(trust), &type_string_accum, validate_any },
837 { C(url), &type_string, validate_url },
838 { C(user), &type_string, validate_isauser },
839 { C(username), &type_string, validate_any },
842 /* find a configuration item's definition by key */
843 static const struct conf *find(const char *key) {
846 if((n = TABLE_FIND(conf, struct conf, name, key)) < 0)
851 /* set a new configuration value */
852 static int config_set(const struct config_state *cs,
853 int nvec, char **vec) {
854 const struct conf *which;
856 D(("config_set %s", vec[0]));
857 if(!(which = find(vec[0]))) {
858 error(0, "%s:%d: unknown configuration key '%s'",
859 cs->path, cs->line, vec[0]);
862 return (which->validate(cs, nvec - 1, vec + 1)
863 || which->type->set(cs, which, nvec - 1, vec + 1));
866 static void config_error(const char *msg, void *u) {
867 const struct config_state *cs = u;
869 error(0, "%s:%d: %s", cs->path, cs->line, msg);
872 /* include a file by name */
873 static int config_include(struct config *c, const char *path) {
875 char *buffer, *inputbuffer, **vec;
877 struct config_state cs;
882 D(("%s: reading configuration", path));
883 if(!(fp = fopen(path, "r"))) {
884 error(errno, "error opening %s", path);
887 while(!inputline(path, fp, &inputbuffer, '\n')) {
889 if(!(buffer = mb2utf8(inputbuffer))) {
890 error(errno, "%s:%d: cannot convert to UTF-8", cs.path, cs.line);
896 if(!(vec = split(buffer, &n, SPLIT_COMMENTS|SPLIT_QUOTES,
897 config_error, &cs))) {
903 if(!strcmp(vec[0], "include")) {
905 error(0, "%s:%d: must be 'include PATH'", cs.path, cs.line);
908 config_include(c, vec[1]);
910 ret |= config_set(&cs, n, vec);
912 for(n = 0; vec[n]; ++n) xfree(vec[n]);
917 error(errno, "error reading %s", path);
924 /* make a new default config */
925 static struct config *config_default(void) {
926 struct config *c = xmalloc(sizeof *c);
930 /* Strings had better be xstrdup'd as they will get freed at some point. */
933 c->home = xstrdup(pkgstatedir);
934 if(!(pw = getpwuid(getuid())))
935 fatal(0, "cannot determine our username");
936 logname = pw->pw_name;
937 c->username = xstrdup(logname);
941 c->alias = xstrdup("{/artist}{/album}{/title}{ext}");
943 c->device = xstrdup("default");
945 c->speaker_command = 0;
946 c->sample_format.bits = 16;
947 c->sample_format.rate = 44100;
948 c->sample_format.channels = 2;
949 c->sample_format.byte_format = AO_FMT_NATIVE;
951 c->speaker_backend = -1;
955 static char *get_file(struct config *c, const char *name) {
958 byte_xasprintf(&s, "%s/%s", c->home, name);
962 static void set_configfile(void) {
964 byte_xasprintf(&configfile, "%s/config", pkgconfdir);
967 /* free the config file */
968 static void config_free(struct config *c) {
972 for(n = 0; n < (int)(sizeof conf / sizeof *conf); ++n)
973 conf[n].type->free(c, &conf[n]);
974 for(n = 0; n < c->nparts; ++n)
981 static void config_postdefaults(struct config *c) {
982 struct config_state cs;
983 const struct conf *whoami;
986 static const char *namepart[][4] = {
987 { "title", "/([0-9]+:)?([^/]+)\\.[a-zA-Z0-9]+$", "$2", "display" },
988 { "title", "/([^/]+)\\.[a-zA-Z0-9]+$", "$1", "sort" },
989 { "album", "/([^/]+)/[^/]+$", "$1", "*" },
990 { "artist", "/([^/]+)/[^/]+/[^/]+$", "$1", "*" },
991 { "ext", "(\\.[a-zA-Z0-9]+)$", "$1", "*" },
993 #define NNAMEPART (int)(sizeof namepart / sizeof *namepart)
995 static const char *transform[][5] = {
996 { "track", "^.*/([0-9]+:)?([^/]+)\\.[a-zA-Z0-9]+$", "$2", "display", "" },
997 { "track", "^.*/([^/]+)\\.[a-zA-Z0-9]+$", "$1", "sort", "" },
998 { "dir", "^.*/([^/]+)$", "$1", "*", "" },
999 { "dir", "^(the) ([^/]*)", "$2, $1", "sort", "i", },
1000 { "dir", "[[:punct:]]", "", "sort", "g", }
1002 #define NTRANSFORM (int)(sizeof transform / sizeof *transform)
1004 cs.path = "<internal>";
1007 if(!c->namepart.n) {
1008 whoami = find("namepart");
1009 for(n = 0; n < NNAMEPART; ++n)
1010 set_namepart(&cs, whoami, 4, (char **)namepart[n]);
1012 if(!c->transform.n) {
1013 whoami = find("transform");
1014 for(n = 0; n < NTRANSFORM; ++n)
1015 set_transform(&cs, whoami, 5, (char **)transform[n]);
1017 if(c->speaker_backend == -1) {
1018 if(c->speaker_command)
1019 c->speaker_backend = BACKEND_COMMAND;
1020 else if(c->broadcast.n)
1021 c->speaker_backend = BACKEND_NETWORK;
1024 c->speaker_backend = BACKEND_ALSA;
1026 c->speaker_backend = BACKEND_COMMAND;
1030 if(c->speaker_backend == BACKEND_COMMAND && !c->speaker_command)
1031 fatal(0, "speaker_backend is command but speaker_command is not set");
1032 if(c->speaker_backend == BACKEND_NETWORK && !c->broadcast.n)
1033 fatal(0, "speaker_backend is network but broadcast is not set");
1036 /* re-read the config file */
1043 c = config_default();
1044 if(config_include(c, configfile))
1046 /* if we can read the private config file, do */
1047 if((privconf = config_private())
1048 && access(privconf, R_OK) == 0
1049 && config_include(c, privconf))
1052 /* if there's a per-user system config file for this user, read it */
1053 if(!(pw = getpwuid(getuid())))
1054 fatal(0, "cannot determine our username");
1055 if((privconf = config_usersysconf(pw))
1056 && access(privconf, F_OK) == 0
1057 && config_include(c, privconf))
1060 /* if we have a password file, read it */
1061 if((privconf = config_userconf(getenv("HOME"), pw))
1062 && access(privconf, F_OK) == 0
1063 && config_include(c, privconf))
1066 /* install default namepart and transform settings */
1067 config_postdefaults(c);
1068 /* everything is good so we shall use the new config */
1069 config_free(config);
1074 char *config_private(void) {
1078 byte_xasprintf(&s, "%s.private", configfile);
1082 char *config_userconf(const char *home, const struct passwd *pw) {
1085 byte_xasprintf(&s, "%s/.disorder/passwd", home ? home : pw->pw_dir);
1089 char *config_usersysconf(const struct passwd *pw ) {
1093 if(!strchr(pw->pw_name, '/')) {
1094 byte_xasprintf(&s, "%s.%s", configfile, pw->pw_name);
1100 char *config_get_file(const char *name) {
1101 return get_file(config, name);