2 * This file is part of DisOrder.
3 * Copyright (C) 2004, 2005, 2006, 2007 Richard Kettlewell
4 * Portions copyright (C) 2007 Mark Wooding
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
21 /** @file lib/configuration.c
22 * @brief Configuration file support
32 #include <sys/types.h>
43 #include "configuration.h"
49 #include "inputline.h"
58 /** @brief Path to config file
60 * set_configfile() sets the deafult if it is null.
64 /** @brief Read user configuration
66 * If clear, the user-specific configuration is not read.
68 int config_per_user
= 1;
70 /** @brief Config file parser state */
72 /** @brief Filename */
74 /** @brief Line number */
76 /** @brief Configuration object under construction */
77 struct config
*config
;
80 /** @brief Current configuration */
81 struct config
*config
;
83 /** @brief One configuration item */
85 /** @brief Name as it appears in the config file */
87 /** @brief Offset in @ref config structure */
89 /** @brief Pointer to item type */
90 const struct conftype
*type
;
91 /** @brief Pointer to item-specific validation routine */
92 int (*validate
)(const struct config_state
*cs
,
93 int nvec
, char **vec
);
96 /** @brief Type of a configuration item */
98 /** @brief Pointer to function to set item */
99 int (*set
)(const struct config_state
*cs
,
100 const struct conf
*whoami
,
101 int nvec
, char **vec
);
102 /** @brief Pointer to function to free item */
103 void (*free
)(struct config
*c
, const struct conf
*whoami
);
106 /** @brief Compute the address of an item */
107 #define ADDRESS(C, TYPE) ((TYPE *)((char *)(C) + whoami->offset))
108 /** @brief Return the value of an item */
109 #define VALUE(C, TYPE) (*ADDRESS(C, TYPE))
111 static int set_signal(const struct config_state
*cs
,
112 const struct conf
*whoami
,
113 int nvec
, char **vec
) {
117 error(0, "%s:%d: '%s' requires one argument",
118 cs
->path
, cs
->line
, whoami
->name
);
121 if((n
= find_signal(vec
[0])) == -1) {
122 error(0, "%s:%d: unknown signal '%s'",
123 cs
->path
, cs
->line
, vec
[0]);
126 VALUE(cs
->config
, int) = n
;
130 static int set_collections(const struct config_state
*cs
,
131 const struct conf
*whoami
,
132 int nvec
, char **vec
) {
133 struct collectionlist
*cl
;
134 const char *root
, *encoding
, *module
;
153 error(0, "%s:%d: '%s' requires at least one argument",
154 cs
->path
, cs
->line
, whoami
->name
);
157 error(0, "%s:%d: '%s' requires at most three arguments",
158 cs
->path
, cs
->line
, whoami
->name
);
161 /* Sanity check root */
163 error(0, "%s:%d: collection root must start with '/'",
167 if(root
[1] && root
[strlen(root
)-1] == '/') {
168 error(0, "%s:%d: collection root must not end with '/'",
176 encoding
= nl_langinfo(CODESET
);
177 cl
= ADDRESS(cs
->config
, struct collectionlist
);
179 cl
->s
= xrealloc(cl
->s
, cl
->n
* sizeof (struct collection
));
180 cl
->s
[cl
->n
- 1].module
= xstrdup(module
);
181 cl
->s
[cl
->n
- 1].encoding
= xstrdup(encoding
);
182 cl
->s
[cl
->n
- 1].root
= xstrdup(root
);
186 static int set_boolean(const struct config_state
*cs
,
187 const struct conf
*whoami
,
188 int nvec
, char **vec
) {
192 error(0, "%s:%d: '%s' takes only one argument",
193 cs
->path
, cs
->line
, whoami
->name
);
196 if(!strcmp(vec
[0], "yes")) state
= 1;
197 else if(!strcmp(vec
[0], "no")) state
= 0;
199 error(0, "%s:%d: argument to '%s' must be 'yes' or 'no'",
200 cs
->path
, cs
->line
, whoami
->name
);
203 VALUE(cs
->config
, int) = state
;
207 static int set_string(const struct config_state
*cs
,
208 const struct conf
*whoami
,
209 int nvec
, char **vec
) {
211 error(0, "%s:%d: '%s' takes only one argument",
212 cs
->path
, cs
->line
, whoami
->name
);
215 VALUE(cs
->config
, char *) = xstrdup(vec
[0]);
219 static int set_stringlist(const struct config_state
*cs
,
220 const struct conf
*whoami
,
221 int nvec
, char **vec
) {
223 struct stringlist
*sl
;
225 sl
= ADDRESS(cs
->config
, struct stringlist
);
227 for(n
= 0; n
< nvec
; ++n
) {
229 sl
->s
= xrealloc(sl
->s
, (sl
->n
* sizeof (char *)));
230 sl
->s
[sl
->n
- 1] = xstrdup(vec
[n
]);
235 static int set_integer(const struct config_state
*cs
,
236 const struct conf
*whoami
,
237 int nvec
, char **vec
) {
241 error(0, "%s:%d: '%s' takes only one argument",
242 cs
->path
, cs
->line
, whoami
->name
);
245 if(xstrtol(ADDRESS(cs
->config
, long), vec
[0], &e
, 0)) {
246 error(errno
, "%s:%d: converting integer", cs
->path
, cs
->line
);
250 error(0, "%s:%d: invalid integer syntax", cs
->path
, cs
->line
);
256 static int set_stringlist_accum(const struct config_state
*cs
,
257 const struct conf
*whoami
,
258 int nvec
, char **vec
) {
260 struct stringlist
*s
;
261 struct stringlistlist
*sll
;
263 sll
= ADDRESS(cs
->config
, struct stringlistlist
);
269 sll
->s
= xrealloc(sll
->s
, (sll
->n
* sizeof (struct stringlist
)));
270 s
= &sll
->s
[sll
->n
- 1];
272 s
->s
= xmalloc((nvec
+ 1) * sizeof (char *));
273 for(n
= 0; n
< nvec
; ++n
)
274 s
->s
[n
] = xstrdup(vec
[n
]);
278 static int set_string_accum(const struct config_state
*cs
,
279 const struct conf
*whoami
,
280 int nvec
, char **vec
) {
282 struct stringlist
*sl
;
284 sl
= ADDRESS(cs
->config
, struct stringlist
);
289 for(n
= 0; n
< nvec
; ++n
) {
291 sl
->s
= xrealloc(sl
->s
, (sl
->n
* sizeof (char *)));
292 sl
->s
[sl
->n
- 1] = xstrdup(vec
[n
]);
297 static int set_restrict(const struct config_state
*cs
,
298 const struct conf
*whoami
,
299 int nvec
, char **vec
) {
303 static const struct restriction
{
307 { "remove", RESTRICT_REMOVE
},
308 { "scratch", RESTRICT_SCRATCH
},
309 { "move", RESTRICT_MOVE
},
312 for(n
= 0; n
< nvec
; ++n
) {
313 if((i
= TABLE_FIND(restrictions
, struct restriction
, name
, vec
[n
])) < 0) {
314 error(0, "%s:%d: invalid restriction '%s'",
315 cs
->path
, cs
->line
, vec
[n
]);
318 r
|= restrictions
[i
].bit
;
320 VALUE(cs
->config
, unsigned) = r
;
324 static int parse_sample_format(const struct config_state
*cs
,
325 struct stream_header
*format
,
326 int nvec
, char **vec
) {
331 error(0, "%s:%d: wrong number of arguments", cs
->path
, cs
->line
);
334 if(xstrtol(&t
, p
, &p
, 0)) {
335 error(errno
, "%s:%d: converting bits-per-sample", cs
->path
, cs
->line
);
338 if(t
!= 8 && t
!= 16) {
339 error(0, "%s:%d: bad bite-per-sample (%ld)", cs
->path
, cs
->line
, t
);
342 if(format
) format
->bits
= t
;
344 case 'l': case 'L': t
= ENDIAN_LITTLE
; p
++; break;
345 case 'b': case 'B': t
= ENDIAN_BIG
; p
++; break;
346 default: t
= ENDIAN_NATIVE
; break;
348 if(format
) format
->endian
= t
;
350 error(errno
, "%s:%d: expected `/' after bits-per-sample",
355 if(xstrtol(&t
, p
, &p
, 0)) {
356 error(errno
, "%s:%d: converting sample-rate", cs
->path
, cs
->line
);
359 if(t
< 1 || t
> INT_MAX
) {
360 error(0, "%s:%d: silly sample-rate (%ld)", cs
->path
, cs
->line
, t
);
363 if(format
) format
->rate
= t
;
365 error(0, "%s:%d: expected `/' after sample-rate",
370 if(xstrtol(&t
, p
, &p
, 0)) {
371 error(errno
, "%s:%d: converting channels", cs
->path
, cs
->line
);
375 error(0, "%s:%d: silly number (%ld) of channels", cs
->path
, cs
->line
, t
);
378 if(format
) format
->channels
= t
;
380 error(0, "%s:%d: junk after channels", cs
->path
, cs
->line
);
386 static int set_sample_format(const struct config_state
*cs
,
387 const struct conf
*whoami
,
388 int nvec
, char **vec
) {
389 return parse_sample_format(cs
, ADDRESS(cs
->config
, struct stream_header
),
393 static int set_namepart(const struct config_state
*cs
,
394 const struct conf
*whoami
,
395 int nvec
, char **vec
) {
396 struct namepartlist
*npl
= ADDRESS(cs
->config
, struct namepartlist
);
403 error(0, "%s:%d: namepart needs at least 3 arguments", cs
->path
, cs
->line
);
407 error(0, "%s:%d: namepart needs at most 5 arguments", cs
->path
, cs
->line
);
410 reflags
= nvec
>= 5 ?
regsub_flags(vec
[4]) : 0;
411 if(!(re
= pcre_compile(vec
[1],
413 |regsub_compile_options(reflags
),
414 &errstr
, &erroffset
, 0))) {
415 error(0, "%s:%d: error compiling regexp /%s/: %s (offset %d)",
416 cs
->path
, cs
->line
, vec
[1], errstr
, erroffset
);
419 npl
->s
= xrealloc(npl
->s
, (npl
->n
+ 1) * sizeof (struct namepart
));
420 npl
->s
[npl
->n
].part
= xstrdup(vec
[0]);
421 npl
->s
[npl
->n
].re
= re
;
422 npl
->s
[npl
->n
].replace
= xstrdup(vec
[2]);
423 npl
->s
[npl
->n
].context
= xstrdup(vec
[3]);
424 npl
->s
[npl
->n
].reflags
= reflags
;
426 /* XXX a bit of a bodge; relies on there being very few parts. */
427 for(n
= 0; (n
< cs
->config
->nparts
428 && strcmp(cs
->config
->parts
[n
], vec
[0])); ++n
)
430 if(n
>= cs
->config
->nparts
) {
431 cs
->config
->parts
= xrealloc(cs
->config
->parts
,
432 (cs
->config
->nparts
+ 1) * sizeof (char *));
433 cs
->config
->parts
[cs
->config
->nparts
++] = xstrdup(vec
[0]);
438 static int set_transform(const struct config_state
*cs
,
439 const struct conf
*whoami
,
440 int nvec
, char **vec
) {
441 struct transformlist
*tl
= ADDRESS(cs
->config
, struct transformlist
);
448 error(0, "%s:%d: transform needs at least 3 arguments", cs
->path
, cs
->line
);
452 error(0, "%s:%d: transform needs at most 5 arguments", cs
->path
, cs
->line
);
455 reflags
= (nvec
>= 5 ?
regsub_flags(vec
[4]) : 0);
456 if(!(re
= pcre_compile(vec
[1],
458 |regsub_compile_options(reflags
),
459 &errstr
, &erroffset
, 0))) {
460 error(0, "%s:%d: error compiling regexp /%s/: %s (offset %d)",
461 cs
->path
, cs
->line
, vec
[1], errstr
, erroffset
);
464 tl
->t
= xrealloc(tl
->t
, (tl
->n
+ 1) * sizeof (struct namepart
));
465 tl
->t
[tl
->n
].type
= xstrdup(vec
[0]);
466 tl
->t
[tl
->n
].context
= xstrdup(vec
[3] ? vec
[3] : "*");
467 tl
->t
[tl
->n
].re
= re
;
468 tl
->t
[tl
->n
].replace
= xstrdup(vec
[2]);
469 tl
->t
[tl
->n
].flags
= reflags
;
474 static int set_backend(const struct config_state
*cs
,
475 const struct conf
*whoami
,
476 int nvec
, char **vec
) {
477 int *const valuep
= ADDRESS(cs
->config
, int);
480 error(0, "%s:%d: '%s' requires one argument",
481 cs
->path
, cs
->line
, whoami
->name
);
484 if(!strcmp(vec
[0], "alsa")) {
485 #if HAVE_ALSA_ASOUNDLIB_H
486 *valuep
= BACKEND_ALSA
;
488 error(0, "%s:%d: ALSA is not available on this platform",
492 } else if(!strcmp(vec
[0], "command"))
493 *valuep
= BACKEND_COMMAND
;
494 else if(!strcmp(vec
[0], "network"))
495 *valuep
= BACKEND_NETWORK
;
496 else if(!strcmp(vec
[0], "coreaudio")) {
497 #if HAVE_COREAUDIO_AUDIOHARDWARE_H
498 *valuep
= BACKEND_COREAUDIO
;
500 error(0, "%s:%d: Core Audio is not available on this platform",
504 } else if(!strcmp(vec
[0], "oss")) {
505 #if HAVE_SYS_SOUNDCARD_H
506 *valuep
= BACKEND_OSS
;
508 error(0, "%s:%d: OSS is not available on this platform",
513 error(0, "%s:%d: invalid '%s' value '%s'",
514 cs
->path
, cs
->line
, whoami
->name
, vec
[0]);
520 static int set_rights(const struct config_state
*cs
,
521 const struct conf
*whoami
,
522 int nvec
, char **vec
) {
524 error(0, "%s:%d: '%s' requires one argument",
525 cs
->path
, cs
->line
, whoami
->name
);
528 if(parse_rights(vec
[0], 0, 1)) {
529 error(0, "%s:%d: invalid rights string '%s'",
530 cs
->path
, cs
->line
, vec
[0]);
533 *ADDRESS(cs
->config
, char *) = vec
[0];
539 static void free_none(struct config
attribute((unused
)) *c
,
540 const struct conf
attribute((unused
)) *whoami
) {
543 static void free_string(struct config
*c
,
544 const struct conf
*whoami
) {
545 xfree(VALUE(c
, char *));
548 static void free_stringlist(struct config
*c
,
549 const struct conf
*whoami
) {
551 struct stringlist
*sl
= ADDRESS(c
, struct stringlist
);
553 for(n
= 0; n
< sl
->n
; ++n
)
558 static void free_stringlistlist(struct config
*c
,
559 const struct conf
*whoami
) {
561 struct stringlistlist
*sll
= ADDRESS(c
, struct stringlistlist
);
562 struct stringlist
*sl
;
564 for(n
= 0; n
< sll
->n
; ++n
) {
566 for(m
= 0; m
< sl
->n
; ++m
)
573 static void free_collectionlist(struct config
*c
,
574 const struct conf
*whoami
) {
575 struct collectionlist
*cll
= ADDRESS(c
, struct collectionlist
);
576 struct collection
*cl
;
579 for(n
= 0; n
< cll
->n
; ++n
) {
588 static void free_namepartlist(struct config
*c
,
589 const struct conf
*whoami
) {
590 struct namepartlist
*npl
= ADDRESS(c
, struct namepartlist
);
594 for(n
= 0; n
< npl
->n
; ++n
) {
597 pcre_free(np
->re
); /* ...whatever pcre_free is set to. */
604 static void free_transformlist(struct config
*c
,
605 const struct conf
*whoami
) {
606 struct transformlist
*tl
= ADDRESS(c
, struct transformlist
);
610 for(n
= 0; n
< tl
->n
; ++n
) {
613 pcre_free(t
->re
); /* ...whatever pcre_free is set to. */
620 /* configuration types */
622 static const struct conftype
623 type_signal
= { set_signal
, free_none
},
624 type_collections
= { set_collections
, free_collectionlist
},
625 type_boolean
= { set_boolean
, free_none
},
626 type_string
= { set_string
, free_string
},
627 type_stringlist
= { set_stringlist
, free_stringlist
},
628 type_integer
= { set_integer
, free_none
},
629 type_stringlist_accum
= { set_stringlist_accum
, free_stringlistlist
},
630 type_string_accum
= { set_string_accum
, free_stringlist
},
631 type_sample_format
= { set_sample_format
, free_none
},
632 type_restrict
= { set_restrict
, free_none
},
633 type_namepart
= { set_namepart
, free_namepartlist
},
634 type_transform
= { set_transform
, free_transformlist
},
635 type_rights
= { set_rights
, free_none
},
636 type_backend
= { set_backend
, free_none
};
638 /* specific validation routine */
640 #define VALIDATE_FILE(test, what) do { \
644 for(n = 0; n < nvec; ++n) { \
645 if(stat(vec[n], &sb) < 0) { \
646 error(errno, "%s:%d: %s", cs->path, cs->line, vec[n]); \
649 if(!test(sb.st_mode)) { \
650 error(0, "%s:%d: %s is not a %s", \
651 cs->path, cs->line, vec[n], what); \
657 static int validate_isabspath(const struct config_state
*cs
,
658 int nvec
, char **vec
) {
661 for(n
= 0; n
< nvec
; ++n
)
662 if(vec
[n
][0] != '/') {
663 error(errno
, "%s:%d: %s: not an absolute path",
664 cs
->path
, cs
->line
, vec
[n
]);
670 static int validate_isdir(const struct config_state
*cs
,
671 int nvec
, char **vec
) {
672 VALIDATE_FILE(S_ISDIR
, "directory");
676 static int validate_isreg(const struct config_state
*cs
,
677 int nvec
, char **vec
) {
678 VALIDATE_FILE(S_ISREG
, "regular file");
682 static int validate_player(const struct config_state
*cs
,
684 char attribute((unused
)) **vec
) {
686 error(0, "%s:%d: should be at least 'player PATTERN MODULE'",
693 static int validate_tracklength(const struct config_state
*cs
,
695 char attribute((unused
)) **vec
) {
697 error(0, "%s:%d: should be at least 'tracklength PATTERN MODULE'",
704 static int validate_allow(const struct config_state
*cs
,
706 char attribute((unused
)) **vec
) {
708 error(0, "%s:%d: must be 'allow NAME PASS'", cs
->path
, cs
->line
);
714 static int validate_non_negative(const struct config_state
*cs
,
715 int nvec
, char **vec
) {
719 error(0, "%s:%d: missing argument", cs
->path
, cs
->line
);
723 error(0, "%s:%d: too many arguments", cs
->path
, cs
->line
);
726 if(xstrtol(&n
, vec
[0], 0, 0)) {
727 error(0, "%s:%d: %s", cs
->path
, cs
->line
, strerror(errno
));
731 error(0, "%s:%d: must not be negative", cs
->path
, cs
->line
);
737 static int validate_positive(const struct config_state
*cs
,
738 int nvec
, char **vec
) {
742 error(0, "%s:%d: missing argument", cs
->path
, cs
->line
);
746 error(0, "%s:%d: too many arguments", cs
->path
, cs
->line
);
749 if(xstrtol(&n
, vec
[0], 0, 0)) {
750 error(0, "%s:%d: %s", cs
->path
, cs
->line
, strerror(errno
));
754 error(0, "%s:%d: must be positive", cs
->path
, cs
->line
);
760 static int validate_isauser(const struct config_state
*cs
,
761 int attribute((unused
)) nvec
,
765 if(!(pw
= getpwnam(vec
[0]))) {
766 error(0, "%s:%d: no such user as '%s'", cs
->path
, cs
->line
, vec
[0]);
772 static int validate_sample_format(const struct config_state
*cs
,
773 int attribute((unused
)) nvec
,
775 return parse_sample_format(cs
, 0, nvec
, vec
);
778 static int validate_any(const struct config_state
attribute((unused
)) *cs
,
779 int attribute((unused
)) nvec
,
780 char attribute((unused
)) **vec
) {
784 static int validate_url(const struct config_state
attribute((unused
)) *cs
,
785 int attribute((unused
)) nvec
,
789 /* absoluteURI = scheme ":" ( hier_part | opaque_part )
790 scheme = alpha *( alpha | digit | "+" | "-" | "." ) */
792 n
= strspn(s
, ("abcdefghijklmnopqrstuvwxyz"
793 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
796 error(0, "%s:%d: invalid url '%s'", cs
->path
, cs
->line
, vec
[0]);
799 if(!strncmp(s
, "http:", 5)
800 || !strncmp(s
, "https:", 6)) {
802 /* we only do a rather cursory check */
803 if(strncmp(s
, "//", 2)) {
804 error(0, "%s:%d: invalid url '%s'", cs
->path
, cs
->line
, vec
[0]);
811 static int validate_alias(const struct config_state
*cs
,
815 int in_brackets
= 0, c
;
818 error(0, "%s:%d: missing argument", cs
->path
, cs
->line
);
822 error(0, "%s:%d: too many arguments", cs
->path
, cs
->line
);
826 while((c
= (unsigned char)*s
++)) {
830 else if(!isalnum(c
)) {
831 error(0, "%s:%d: invalid part name in alias expansion in '%s'",
832 cs
->path
, cs
->line
, vec
[0]);
840 } else if(c
== '\\') {
841 if(!(c
= (unsigned char)*s
++)) {
842 error(0, "%s:%d: unterminated escape in alias expansion in '%s'",
843 cs
->path
, cs
->line
, vec
[0]);
845 } else if(c
!= '\\' && c
!= '{') {
846 error(0, "%s:%d: invalid escape in alias expansion in '%s'",
847 cs
->path
, cs
->line
, vec
[0]);
855 error(0, "%s:%d: unterminated part name in alias expansion in '%s'",
856 cs
->path
, cs
->line
, vec
[0]);
862 static int validate_addrport(const struct config_state
attribute((unused
)) *cs
,
864 char attribute((unused
)) **vec
) {
867 error(0, "%s:%d: missing address",
871 error(0, "%s:%d: missing port name/number",
877 error(0, "%s:%d: expected ADDRESS PORT",
883 static int validate_port(const struct config_state
attribute((unused
)) *cs
,
885 char attribute((unused
)) **vec
) {
888 error(0, "%s:%d: missing address",
895 error(0, "%s:%d: expected [ADDRESS] PORT",
901 static int validate_algo(const struct config_state
attribute((unused
)) *cs
,
905 error(0, "%s:%d: invalid algorithm specification", cs
->path
, cs
->line
);
908 if(!valid_authhash(vec
[0])) {
909 error(0, "%s:%d: unsuported algorithm '%s'", cs
->path
, cs
->line
, vec
[0]);
915 /** @brief Item name and and offset */
916 #define C(x) #x, offsetof(struct config, x)
917 /** @brief Item name and and offset */
918 #define C2(x,y) #x, offsetof(struct config, y)
920 /** @brief All configuration items */
921 static const struct conf conf
[] = {
922 { C(alias
), &type_string
, validate_alias
},
923 { C(allow
), &type_stringlist_accum
, validate_allow
},
924 { C(api
), &type_backend
, validate_any
},
925 { C(authorization_algorithm
), &type_string
, validate_algo
},
926 { C(broadcast
), &type_stringlist
, validate_addrport
},
927 { C(broadcast_from
), &type_stringlist
, validate_addrport
},
928 { C(channel
), &type_string
, validate_any
},
929 { C(checkpoint_kbyte
), &type_integer
, validate_non_negative
},
930 { C(checkpoint_min
), &type_integer
, validate_non_negative
},
931 { C(collection
), &type_collections
, validate_any
},
932 { C(connect
), &type_stringlist
, validate_addrport
},
933 { C(cookie_login_lifetime
), &type_integer
, validate_positive
},
934 { C(cookie_key_lifetime
), &type_integer
, validate_positive
},
935 { C(dbversion
), &type_integer
, validate_positive
},
936 { C(default_rights
), &type_rights
, validate_any
},
937 { C(device
), &type_string
, validate_any
},
938 { C(gap
), &type_integer
, validate_non_negative
},
939 { C(history
), &type_integer
, validate_positive
},
940 { C(home
), &type_string
, validate_isabspath
},
941 { C(listen
), &type_stringlist
, validate_port
},
942 { C(lock
), &type_boolean
, validate_any
},
943 { C(mail_sender
), &type_string
, validate_any
},
944 { C(mixer
), &type_string
, validate_any
},
945 { C(multicast_loop
), &type_boolean
, validate_any
},
946 { C(multicast_ttl
), &type_integer
, validate_non_negative
},
947 { C(namepart
), &type_namepart
, validate_any
},
948 { C2(nice
, nice_rescan
), &type_integer
, validate_non_negative
},
949 { C(nice_rescan
), &type_integer
, validate_non_negative
},
950 { C(nice_server
), &type_integer
, validate_any
},
951 { C(nice_speaker
), &type_integer
, validate_any
},
952 { C(noticed_history
), &type_integer
, validate_positive
},
953 { C(password
), &type_string
, validate_any
},
954 { C(player
), &type_stringlist_accum
, validate_player
},
955 { C(plugins
), &type_string_accum
, validate_isdir
},
956 { C(prefsync
), &type_integer
, validate_positive
},
957 { C(queue_pad
), &type_integer
, validate_positive
},
958 { C(refresh
), &type_integer
, validate_positive
},
959 { C2(restrict
, restrictions
), &type_restrict
, validate_any
},
960 { C(sample_format
), &type_sample_format
, validate_sample_format
},
961 { C(scratch
), &type_string_accum
, validate_isreg
},
962 { C(short_display
), &type_integer
, validate_positive
},
963 { C(signal
), &type_signal
, validate_any
},
964 { C(smtp_server
), &type_string
, validate_any
},
965 { C(sox_generation
), &type_integer
, validate_non_negative
},
966 { C2(speaker_backend
, api
), &type_backend
, validate_any
},
967 { C(speaker_command
), &type_string
, validate_any
},
968 { C(stopword
), &type_string_accum
, validate_any
},
969 { C(templates
), &type_string_accum
, validate_isdir
},
970 { C(tracklength
), &type_stringlist_accum
, validate_tracklength
},
971 { C(transform
), &type_transform
, validate_any
},
972 { C(trust
), &type_string_accum
, validate_any
},
973 { C(url
), &type_string
, validate_url
},
974 { C(user
), &type_string
, validate_isauser
},
975 { C(username
), &type_string
, validate_any
},
978 /** @brief Find a configuration item's definition by key */
979 static const struct conf
*find(const char *key
) {
982 if((n
= TABLE_FIND(conf
, struct conf
, name
, key
)) < 0)
987 /** @brief Set a new configuration value */
988 static int config_set(const struct config_state
*cs
,
989 int nvec
, char **vec
) {
990 const struct conf
*which
;
992 D(("config_set %s", vec
[0]));
993 if(!(which
= find(vec
[0]))) {
994 error(0, "%s:%d: unknown configuration key '%s'",
995 cs
->path
, cs
->line
, vec
[0]);
998 return (which
->validate(cs
, nvec
- 1, vec
+ 1)
999 || which
->type
->set(cs
, which
, nvec
- 1, vec
+ 1));
1002 /** @brief Error callback used by config_include() */
1003 static void config_error(const char *msg
, void *u
) {
1004 const struct config_state
*cs
= u
;
1006 error(0, "%s:%d: %s", cs
->path
, cs
->line
, msg
);
1009 /** @brief Include a file by name */
1010 static int config_include(struct config
*c
, const char *path
) {
1012 char *buffer
, *inputbuffer
, **vec
;
1014 struct config_state cs
;
1019 D(("%s: reading configuration", path
));
1020 if(!(fp
= fopen(path
, "r"))) {
1021 error(errno
, "error opening %s", path
);
1024 while(!inputline(path
, fp
, &inputbuffer
, '\n')) {
1026 if(!(buffer
= mb2utf8(inputbuffer
))) {
1027 error(errno
, "%s:%d: cannot convert to UTF-8", cs
.path
, cs
.line
);
1033 if(!(vec
= split(buffer
, &n
, SPLIT_COMMENTS
|SPLIT_QUOTES
,
1034 config_error
, &cs
))) {
1040 if(!strcmp(vec
[0], "include")) {
1042 error(0, "%s:%d: must be 'include PATH'", cs
.path
, cs
.line
);
1045 config_include(c
, vec
[1]);
1047 ret
|= config_set(&cs
, n
, vec
);
1049 for(n
= 0; vec
[n
]; ++n
) xfree(vec
[n
]);
1054 error(errno
, "error reading %s", path
);
1061 static const char *const default_stopwords
[] = {
1120 #define NDEFAULT_STOPWORDS (sizeof default_stopwords / sizeof *default_stopwords)
1122 /** @brief Make a new default configuration */
1123 static struct config
*config_default(void) {
1124 struct config
*c
= xmalloc(sizeof *c
);
1125 const char *logname
;
1127 struct config_state cs
;
1129 cs
.path
= "<internal>";
1132 /* Strings had better be xstrdup'd as they will get freed at some point. */
1135 c
->home
= xstrdup(pkgstatedir
);
1136 if(!(pw
= getpwuid(getuid())))
1137 fatal(0, "cannot determine our username");
1138 logname
= pw
->pw_name
;
1139 c
->username
= xstrdup(logname
);
1142 c
->signal
= SIGKILL
;
1143 c
->alias
= xstrdup("{/artist}{/album}{/title}{ext}");
1145 c
->device
= xstrdup("default");
1146 c
->nice_rescan
= 10;
1147 c
->speaker_command
= 0;
1148 c
->sample_format
.bits
= 16;
1149 c
->sample_format
.rate
= 44100;
1150 c
->sample_format
.channels
= 2;
1151 c
->sample_format
.endian
= ENDIAN_NATIVE
;
1154 c
->multicast_ttl
= 1;
1155 c
->multicast_loop
= 1;
1156 c
->authorization_algorithm
= xstrdup("sha1");
1157 c
->noticed_history
= 31;
1158 c
->short_display
= 32;
1162 c
->cookie_login_lifetime
= 86400;
1163 c
->cookie_key_lifetime
= 86400 * 7;
1164 c
->smtp_server
= xstrdup("127.0.0.1");
1165 if(config_set(&cs
, (int)NDEFAULT_STOPWORDS
, (char **)default_stopwords
))
1170 static char *get_file(struct config
*c
, const char *name
) {
1173 byte_xasprintf(&s
, "%s/%s", c
->home
, name
);
1177 /** @brief Set the default configuration file */
1178 static void set_configfile(void) {
1180 byte_xasprintf(&configfile
, "%s/config", pkgconfdir
);
1183 /** @brief Free a configuration object */
1184 static void config_free(struct config
*c
) {
1188 for(n
= 0; n
< (int)(sizeof conf
/ sizeof *conf
); ++n
)
1189 conf
[n
].type
->free(c
, &conf
[n
]);
1190 for(n
= 0; n
< c
->nparts
; ++n
)
1197 /** @brief Set post-parse defaults */
1198 static void config_postdefaults(struct config
*c
,
1200 struct config_state cs
;
1201 const struct conf
*whoami
;
1204 static const char *namepart
[][4] = {
1205 { "title", "/([0-9]+ *[-:] *)?([^/]+)\\.[a-zA-Z0-9]+$", "$2", "display" },
1206 { "title", "/([^/]+)\\.[a-zA-Z0-9]+$", "$1", "sort" },
1207 { "album", "/([^/]+)/[^/]+$", "$1", "*" },
1208 { "artist", "/([^/]+)/[^/]+/[^/]+$", "$1", "*" },
1209 { "ext", "(\\.[a-zA-Z0-9]+)$", "$1", "*" },
1211 #define NNAMEPART (int)(sizeof namepart / sizeof *namepart)
1213 static const char *transform
[][5] = {
1214 { "track", "^.*/([0-9]+ *[-:] *)?([^/]+)\\.[a-zA-Z0-9]+$", "$2", "display", "" },
1215 { "track", "^.*/([^/]+)\\.[a-zA-Z0-9]+$", "$1", "sort", "" },
1216 { "dir", "^.*/([^/]+)$", "$1", "*", "" },
1217 { "dir", "^(the) ([^/]*)", "$2, $1", "sort", "i", },
1218 { "dir", "[[:punct:]]", "", "sort", "g", }
1220 #define NTRANSFORM (int)(sizeof transform / sizeof *transform)
1222 cs
.path
= "<internal>";
1225 if(!c
->namepart
.n
) {
1226 whoami
= find("namepart");
1227 for(n
= 0; n
< NNAMEPART
; ++n
)
1228 set_namepart(&cs
, whoami
, 4, (char **)namepart
[n
]);
1230 if(!c
->transform
.n
) {
1231 whoami
= find("transform");
1232 for(n
= 0; n
< NTRANSFORM
; ++n
)
1233 set_transform(&cs
, whoami
, 5, (char **)transform
[n
]);
1236 if(c
->speaker_command
)
1237 c
->api
= BACKEND_COMMAND
;
1238 else if(c
->broadcast
.n
)
1239 c
->api
= BACKEND_NETWORK
;
1241 #if HAVE_ALSA_ASOUNDLIB_H
1242 c
->api
= BACKEND_ALSA
;
1243 #elif HAVE_SYS_SOUNDCARD_H
1244 c
->api
= BACKEND_OSS
;
1245 #elif HAVE_COREAUDIO_AUDIOHARDWARE_H
1246 c
->api
= BACKEND_COREAUDIO
;
1248 c
->api
= BACKEND_COMMAND
;
1253 if(c
->api
== BACKEND_COMMAND
&& !c
->speaker_command
)
1254 fatal(0, "'api command' but speaker_command is not set");
1255 if(c
->api
== BACKEND_NETWORK
&& !c
->broadcast
.n
)
1256 fatal(0, "'api network' but broadcast is not set");
1258 /* Override sample format */
1260 case BACKEND_NETWORK
:
1261 c
->sample_format
.rate
= 44100;
1262 c
->sample_format
.channels
= 2;
1263 c
->sample_format
.bits
= 16;
1264 c
->sample_format
.endian
= ENDIAN_BIG
;
1266 case BACKEND_COREAUDIO
:
1267 c
->sample_format
.rate
= 44100;
1268 c
->sample_format
.channels
= 2;
1269 c
->sample_format
.bits
= 16;
1270 c
->sample_format
.endian
= ENDIAN_NATIVE
;
1273 if(!c
->default_rights
) {
1274 rights_type r
= RIGHTS__MASK
& ~(RIGHT_ADMIN
|RIGHT_REGISTER
1276 |RIGHT_SCRATCH__MASK
1277 |RIGHT_REMOVE__MASK
);
1278 /* The idea is to approximate the meaning of the old 'restrict' directive
1279 * in the default rights if they are not overridden. */
1280 if(c
->restrictions
& RESTRICT_SCRATCH
)
1281 r
|= RIGHT_SCRATCH_MINE
|RIGHT_SCRATCH_RANDOM
;
1283 r
|= RIGHT_SCRATCH_ANY
;
1284 if(!(c
->restrictions
& RESTRICT_MOVE
))
1285 r
|= RIGHT_MOVE_ANY
;
1286 if(c
->restrictions
& RESTRICT_REMOVE
)
1287 r
|= RIGHT_REMOVE_MINE
;
1289 r
|= RIGHT_REMOVE_ANY
;
1290 c
->default_rights
= rights_string(r
);
1294 /** @brief (Re-)read the config file
1295 * @param server If set, do extra checking
1297 int config_read(int server
) {
1303 c
= config_default();
1304 /* standalone Disobedience installs might not have a global config file */
1305 if(access(configfile
, F_OK
) == 0)
1306 if(config_include(c
, configfile
))
1308 /* if we can read the private config file, do */
1309 if((privconf
= config_private())
1310 && access(privconf
, R_OK
) == 0
1311 && config_include(c
, privconf
))
1314 /* if there's a per-user system config file for this user, read it */
1315 if(config_per_user
) {
1316 if(!(pw
= getpwuid(getuid())))
1317 fatal(0, "cannot determine our username");
1318 if((privconf
= config_usersysconf(pw
))
1319 && access(privconf
, F_OK
) == 0
1320 && config_include(c
, privconf
))
1323 /* if we have a password file, read it */
1324 if((privconf
= config_userconf(getenv("HOME"), pw
))
1325 && access(privconf
, F_OK
) == 0
1326 && config_include(c
, privconf
))
1330 /* install default namepart and transform settings */
1331 config_postdefaults(c
, server
);
1332 /* everything is good so we shall use the new config */
1333 config_free(config
);
1334 /* warn about obsolete directives */
1336 error(0, "'restrict' will be removed in a future version");
1338 error(0, "'allow' will be removed in a future version");
1340 error(0, "'trust' will be removed in a future version");
1345 /** @brief Return the path to the private configuration file */
1346 char *config_private(void) {
1350 byte_xasprintf(&s
, "%s.private", configfile
);
1354 /** @brief Return the path to user's personal configuration file */
1355 char *config_userconf(const char *home
, const struct passwd
*pw
) {
1358 if(!home
&& !pw
&& !(pw
= getpwuid(getuid())))
1359 fatal(0, "cannot determine our username");
1360 byte_xasprintf(&s
, "%s/.disorder/passwd", home ? home
: pw
->pw_dir
);
1364 /** @brief Return the path to user-specific system configuration */
1365 char *config_usersysconf(const struct passwd
*pw
) {
1369 if(!strchr(pw
->pw_name
, '/')) {
1370 byte_xasprintf(&s
, "%s.%s", configfile
, pw
->pw_name
);
1376 char *config_get_file(const char *name
) {
1377 return get_file(config
, name
);