2 * This file is part of DisOrder.
3 * Copyright (C) 2004, 2005, 2006 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 set_namepart(const struct config_state *cs,
259 const struct conf *whoami,
260 int nvec, char **vec) {
261 struct namepartlist *npl = ADDRESS(cs->config, struct namepartlist);
268 error(0, "%s:%d: namepart needs at least 3 arguments", cs->path, cs->line);
272 error(0, "%s:%d: namepart needs at most 5 arguments", cs->path, cs->line);
275 reflags = nvec >= 5 ? regsub_flags(vec[4]) : 0;
276 if(!(re = pcre_compile(vec[1],
278 |regsub_compile_options(reflags),
279 &errstr, &erroffset, 0))) {
280 error(0, "%s:%d: error compiling regexp /%s/: %s (offset %d)",
281 cs->path, cs->line, vec[1], errstr, erroffset);
284 npl->s = xrealloc(npl->s, (npl->n + 1) * sizeof (struct namepart));
285 npl->s[npl->n].part = xstrdup(vec[0]);
286 npl->s[npl->n].re = re;
287 npl->s[npl->n].replace = xstrdup(vec[2]);
288 npl->s[npl->n].context = xstrdup(vec[3]);
289 npl->s[npl->n].reflags = reflags;
291 /* XXX a bit of a bodge; relies on there being very few parts. */
292 for(n = 0; (n < cs->config->nparts
293 && strcmp(cs->config->parts[n], vec[0])); ++n)
295 if(n >= cs->config->nparts) {
296 cs->config->parts = xrealloc(cs->config->parts,
297 (cs->config->nparts + 1) * sizeof (char *));
298 cs->config->parts[cs->config->nparts++] = xstrdup(vec[0]);
303 static int set_transform(const struct config_state *cs,
304 const struct conf *whoami,
305 int nvec, char **vec) {
306 struct transformlist *tl = ADDRESS(cs->config, struct transformlist);
313 error(0, "%s:%d: transform needs at least 3 arguments", cs->path, cs->line);
317 error(0, "%s:%d: transform needs at most 5 arguments", cs->path, cs->line);
320 reflags = (nvec >= 5 ? regsub_flags(vec[4]) : 0);
321 if(!(re = pcre_compile(vec[1],
323 |regsub_compile_options(reflags),
324 &errstr, &erroffset, 0))) {
325 error(0, "%s:%d: error compiling regexp /%s/: %s (offset %d)",
326 cs->path, cs->line, vec[1], errstr, erroffset);
329 tl->t = xrealloc(tl->t, (tl->n + 1) * sizeof (struct namepart));
330 tl->t[tl->n].type = xstrdup(vec[0]);
331 tl->t[tl->n].context = xstrdup(vec[3] ? vec[3] : "*");
332 tl->t[tl->n].re = re;
333 tl->t[tl->n].replace = xstrdup(vec[2]);
334 tl->t[tl->n].flags = reflags;
341 static void free_none(struct config attribute((unused)) *c,
342 const struct conf attribute((unused)) *whoami) {
345 static void free_string(struct config *c,
346 const struct conf *whoami) {
347 xfree(VALUE(c, char *));
350 static void free_stringlist(struct config *c,
351 const struct conf *whoami) {
353 struct stringlist *sl = ADDRESS(c, struct stringlist);
355 for(n = 0; n < sl->n; ++n)
360 static void free_stringlistlist(struct config *c,
361 const struct conf *whoami) {
363 struct stringlistlist *sll = ADDRESS(c, struct stringlistlist);
364 struct stringlist *sl;
366 for(n = 0; n < sll->n; ++n) {
368 for(m = 0; m < sl->n; ++m)
375 static void free_collectionlist(struct config *c,
376 const struct conf *whoami) {
377 struct collectionlist *cll = ADDRESS(c, struct collectionlist);
378 struct collection *cl;
381 for(n = 0; n < cll->n; ++n) {
390 static void free_namepartlist(struct config *c,
391 const struct conf *whoami) {
392 struct namepartlist *npl = ADDRESS(c, struct namepartlist);
396 for(n = 0; n < npl->n; ++n) {
399 pcre_free(np->re); /* ...whatever pcre_free is set to. */
406 static void free_transformlist(struct config *c,
407 const struct conf *whoami) {
408 struct transformlist *tl = ADDRESS(c, struct transformlist);
412 for(n = 0; n < tl->n; ++n) {
415 pcre_free(t->re); /* ...whatever pcre_free is set to. */
422 /* configuration types */
424 static const struct conftype
425 type_signal = { set_signal, free_none },
426 type_collections = { set_collections, free_collectionlist },
427 type_boolean = { set_boolean, free_none },
428 type_string = { set_string, free_string },
429 type_stringlist = { set_stringlist, free_stringlist },
430 type_integer = { set_integer, free_none },
431 type_stringlist_accum = { set_stringlist_accum, free_stringlistlist },
432 type_string_accum = { set_string_accum, free_stringlist },
433 type_restrict = { set_restrict, free_none },
434 type_namepart = { set_namepart, free_namepartlist },
435 type_transform = { set_transform, free_transformlist };
437 /* specific validation routine */
439 #define VALIDATE_FILE(test, what) do { \
443 for(n = 0; n < nvec; ++n) { \
444 if(stat(vec[n], &sb) < 0) { \
445 error(errno, "%s:%d: %s", cs->path, cs->line, vec[n]); \
448 if(!test(sb.st_mode)) { \
449 error(0, "%s:%d: %s is not a %s", \
450 cs->path, cs->line, vec[n], what); \
456 static int validate_isdir(const struct config_state *cs,
457 int nvec, char **vec) {
458 VALIDATE_FILE(S_ISDIR, "directory");
462 static int validate_isreg(const struct config_state *cs,
463 int nvec, char **vec) {
464 VALIDATE_FILE(S_ISREG, "regular file");
468 static int validate_ischr(const struct config_state *cs,
469 int nvec, char **vec) {
470 VALIDATE_FILE(S_ISCHR, "character device");
474 static int validate_player(const struct config_state *cs,
476 char attribute((unused)) **vec) {
478 error(0, "%s:%d: should be at least 'player PATTERN MODULE'",
485 static int validate_allow(const struct config_state *cs,
487 char attribute((unused)) **vec) {
489 error(0, "%s:%d: must be 'allow NAME PASS'", cs->path, cs->line);
495 static int validate_non_negative(const struct config_state *cs,
496 int nvec, char **vec) {
500 error(0, "%s:%d: missing argument", cs->path, cs->line);
504 error(0, "%s:%d: too many arguments", cs->path, cs->line);
507 if(xstrtol(&n, vec[0], 0, 0)) {
508 error(0, "%s:%d: %s", cs->path, cs->line, strerror(errno));
512 error(0, "%s:%d: must not be negative", cs->path, cs->line);
518 static int validate_positive(const struct config_state *cs,
519 int nvec, char **vec) {
523 error(0, "%s:%d: missing argument", cs->path, cs->line);
527 error(0, "%s:%d: too many arguments", cs->path, cs->line);
530 if(xstrtol(&n, vec[0], 0, 0)) {
531 error(0, "%s:%d: %s", cs->path, cs->line, strerror(errno));
535 error(0, "%s:%d: must be positive", cs->path, cs->line);
541 static int validate_isauser(const struct config_state *cs,
542 int attribute((unused)) nvec,
546 if(!(pw = getpwnam(vec[0]))) {
547 error(0, "%s:%d: no such user as '%s'", cs->path, cs->line, vec[0]);
553 static int validate_channel(const struct config_state *cs,
554 int attribute((unused)) nvec,
556 if(mixer_channel(vec[0]) == -1) {
557 error(0, "%s:%d: invalid channel '%s'", cs->path, cs->line, vec[0]);
563 static int validate_any(const struct config_state attribute((unused)) *cs,
564 int attribute((unused)) nvec,
565 char attribute((unused)) **vec) {
569 static int validate_url(const struct config_state attribute((unused)) *cs,
570 int attribute((unused)) nvec,
574 /* absoluteURI = scheme ":" ( hier_part | opaque_part )
575 scheme = alpha *( alpha | digit | "+" | "-" | "." ) */
577 n = strspn(s, ("abcdefghijklmnopqrstuvwxyz"
578 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
581 error(0, "%s:%d: invalid url '%s'", cs->path, cs->line, vec[0]);
584 if(!strncmp(s, "http:", 5)
585 || !strncmp(s, "https:", 6)) {
587 /* we only do a rather cursory check */
588 if(strncmp(s, "//", 2)) {
589 error(0, "%s:%d: invalid url '%s'", cs->path, cs->line, vec[0]);
596 static int validate_alias(const struct config_state *cs,
600 int in_brackets = 0, c;
603 error(0, "%s:%d: missing argument", cs->path, cs->line);
607 error(0, "%s:%d: too many arguments", cs->path, cs->line);
611 while((c = (unsigned char)*s++)) {
615 else if(!isalnum(c)) {
616 error(0, "%s:%d: invalid part name in alias expansion in '%s'",
617 cs->path, cs->line, vec[0]);
625 } else if(c == '\\') {
626 if(!(c = (unsigned char)*s++)) {
627 error(0, "%s:%d: unterminated escape in alias expansion in '%s'",
628 cs->path, cs->line, vec[0]);
630 } else if(c != '\\' && c != '{') {
631 error(0, "%s:%d: invalid escape in alias expansion in '%s'",
632 cs->path, cs->line, vec[0]);
640 error(0, "%s:%d: unterminated part name in alias expansion in '%s'",
641 cs->path, cs->line, vec[0]);
647 /* configuration table */
649 #define C(x) #x, offsetof(struct config, x)
650 #define C2(x,y) #x, offsetof(struct config, y)
652 static const struct conf conf[] = {
653 { C(alias), &type_string, validate_alias },
654 { C(allow), &type_stringlist_accum, validate_allow },
655 { C(channel), &type_string, validate_channel },
656 { C(checkpoint_kbyte), &type_integer, validate_non_negative },
657 { C(checkpoint_min), &type_integer, validate_non_negative },
658 { C(collection), &type_collections, validate_any },
659 { C(connect), &type_stringlist, validate_any },
660 { C(device), &type_string, validate_any },
661 { C(gap), &type_integer, validate_non_negative },
662 { C(history), &type_integer, validate_positive },
663 { C(home), &type_string, validate_isdir },
664 { C(listen), &type_stringlist, validate_any },
665 { C(lock), &type_boolean, validate_any },
666 { C(mixer), &type_string, validate_ischr },
667 { C(namepart), &type_namepart, validate_any },
668 { C2(nice, nice_rescan), &type_integer, validate_non_negative },
669 { C(nice_rescan), &type_integer, validate_non_negative },
670 { C(nice_server), &type_integer, validate_any },
671 { C(nice_speaker), &type_integer, validate_any },
672 { C(password), &type_string, validate_any },
673 { C(player), &type_stringlist_accum, validate_player },
674 { C(plugins), &type_string_accum, validate_isdir },
675 { C(prefsync), &type_integer, validate_positive },
676 { C(refresh), &type_integer, validate_positive },
677 { C2(restrict, restrictions), &type_restrict, validate_any },
678 { C(scratch), &type_string_accum, validate_isreg },
679 { C(signal), &type_signal, validate_any },
680 { C(stopword), &type_string_accum, validate_any },
681 { C(templates), &type_string_accum, validate_isdir },
682 { C(transform), &type_transform, validate_any },
683 { C(trust), &type_string_accum, validate_any },
684 { C(url), &type_string, validate_url },
685 { C(user), &type_string, validate_isauser },
686 { C(username), &type_string, validate_any },
689 /* find a configuration item's definition by key */
690 static const struct conf *find(const char *key) {
693 if((n = TABLE_FIND(conf, struct conf, name, key)) < 0)
698 /* set a new configuration value */
699 static int config_set(const struct config_state *cs,
700 int nvec, char **vec) {
701 const struct conf *which;
703 D(("config_set %s", vec[0]));
704 if(!(which = find(vec[0]))) {
705 error(0, "%s:%d: unknown configuration key '%s'",
706 cs->path, cs->line, vec[0]);
709 return (which->validate(cs, nvec - 1, vec + 1)
710 || which->type->set(cs, which, nvec - 1, vec + 1));
713 static void config_error(const char *msg, void *u) {
714 const struct config_state *cs = u;
716 error(0, "%s:%d: %s", cs->path, cs->line, msg);
719 /* include a file by name */
720 static int config_include(struct config *c, const char *path) {
722 char *buffer, *inputbuffer, **vec;
724 struct config_state cs;
729 D(("%s: reading configuration", path));
730 if(!(fp = fopen(path, "r"))) {
731 error(errno, "error opening %s", path);
734 while(!inputline(path, fp, &inputbuffer, '\n')) {
736 if(!(buffer = mb2utf8(inputbuffer))) {
737 error(errno, "%s:%d: cannot convert to UTF-8", cs.path, cs.line);
743 if(!(vec = split(buffer, &n, SPLIT_COMMENTS|SPLIT_QUOTES,
744 config_error, &cs))) {
750 if(!strcmp(vec[0], "include")) {
752 error(0, "%s:%d: must be 'include PATH'", cs.path, cs.line);
755 config_include(c, vec[1]);
757 ret |= config_set(&cs, n, vec);
759 for(n = 0; vec[n]; ++n) xfree(vec[n]);
764 error(errno, "error reading %s", path);
771 /* make a new default config */
772 static struct config *config_default(void) {
773 struct config *c = xmalloc(sizeof *c);
777 /* Strings had better be xstrdup'd as they will get freed at some point. */
780 c->home = xstrdup(pkgstatedir);
781 if(!(pw = getpwuid(getuid())))
782 fatal(0, "cannot determine our username");
783 logname = pw->pw_name;
784 c->username = xstrdup(logname);
788 c->alias = xstrdup("{/artist}{/album}{/title}{ext}");
790 c->device = xstrdup("default");
795 static char *get_file(struct config *c, const char *name) {
798 byte_xasprintf(&s, "%s/%s", c->home, name);
802 static void set_configfile(void) {
804 byte_xasprintf(&configfile, "%s/config", pkgconfdir);
807 /* free the config file */
808 static void config_free(struct config *c) {
812 for(n = 0; n < (int)(sizeof conf / sizeof *conf); ++n)
813 conf[n].type->free(c, &conf[n]);
814 for(n = 0; n < c->nparts; ++n)
821 static void config_postdefaults(struct config *c) {
822 struct config_state cs;
823 const struct conf *whoami;
826 static const char *namepart[][4] = {
827 { "title", "/([0-9]+:)?([^/]+)\\.[a-zA-Z0-9]+$", "$2", "display" },
828 { "title", "/([^/]+)\\.[a-zA-Z0-9]+$", "$1", "sort" },
829 { "album", "/([^/]+)/[^/]+$", "$1", "*" },
830 { "artist", "/([^/]+)/[^/]+/[^/]+$", "$1", "*" },
831 { "ext", "(\\.[a-zA-Z0-9]+)$", "$1", "*" },
833 #define NNAMEPART (int)(sizeof namepart / sizeof *namepart)
835 static const char *transform[][5] = {
836 { "track", "^.*/([0-9]+:)?([^/]+)\\.[a-zA-Z0-9]+$", "$2", "display", "" },
837 { "track", "^.*/([^/]+)\\.[a-zA-Z0-9]+$", "$1", "sort", "" },
838 { "dir", "^.*/([^/]+)$", "$1", "*", "" },
839 { "dir", "^(the) ([^/]*)", "$2, $1", "sort", "i", },
840 { "dir", "[[:punct:]]", "", "sort", "g", }
842 #define NTRANSFORM (int)(sizeof transform / sizeof *transform)
844 cs.path = "<internal>";
848 whoami = find("namepart");
849 for(n = 0; n < NNAMEPART; ++n)
850 set_namepart(&cs, whoami, 4, (char **)namepart[n]);
852 if(!c->transform.n) {
853 whoami = find("transform");
854 for(n = 0; n < NTRANSFORM; ++n)
855 set_transform(&cs, whoami, 5, (char **)transform[n]);
859 /* re-read the config file */
866 c = config_default();
867 if(config_include(c, configfile))
869 /* if we can read the private config file, do */
870 if((privconf = config_private())
871 && access(privconf, R_OK) == 0
872 && config_include(c, privconf))
875 /* if there's a per-user system config file for this user, read it */
876 if(!(pw = getpwuid(getuid())))
877 fatal(0, "cannot determine our username");
878 if((privconf = config_usersysconf(pw))
879 && access(privconf, F_OK) == 0
880 && config_include(c, privconf))
883 /* if we have a password file, read it */
884 if((privconf = config_userconf(getenv("HOME"), pw))
885 && access(privconf, F_OK) == 0
886 && config_include(c, privconf))
889 /* install default namepart and transform settings */
890 config_postdefaults(c);
891 /* everything is good so we shall use the new config */
897 char *config_private(void) {
901 byte_xasprintf(&s, "%s.private", configfile);
905 char *config_userconf(const char *home, const struct passwd *pw) {
908 byte_xasprintf(&s, "%s/.disorder/passwd", home ? home : pw->pw_dir);
912 char *config_usersysconf(const struct passwd *pw ) {
916 if(!strchr(pw->pw_name, '/')) {
917 byte_xasprintf(&s, "%s.%s", configfile, pw->pw_name);
923 char *config_get_file(const char *name) {
924 return get_file(config, name);
934 /* arch-tag:ede19ed49dca6136ba864ed0a4988b34 */