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>
42 #include "configuration.h"
48 #include "inputline.h"
57 /** @brief Path to config file
59 * set_configfile() sets the deafult if it is null.
63 /** @brief Read user configuration
65 * If clear, the user-specific configuration is not read.
67 int config_per_user
= 1;
69 /** @brief Config file parser state */
71 /** @brief Filename */
73 /** @brief Line number */
75 /** @brief Configuration object under construction */
76 struct config
*config
;
79 /** @brief Current configuration */
80 struct config
*config
;
82 /** @brief One configuration item */
84 /** @brief Name as it appears in the config file */
86 /** @brief Offset in @ref config structure */
88 /** @brief Pointer to item type */
89 const struct conftype
*type
;
90 /** @brief Pointer to item-specific validation routine */
91 int (*validate
)(const struct config_state
*cs
,
92 int nvec
, char **vec
);
95 /** @brief Type of a configuration item */
97 /** @brief Pointer to function to set item */
98 int (*set
)(const struct config_state
*cs
,
99 const struct conf
*whoami
,
100 int nvec
, char **vec
);
101 /** @brief Pointer to function to free item */
102 void (*free
)(struct config
*c
, const struct conf
*whoami
);
105 /** @brief Compute the address of an item */
106 #define ADDRESS(C, TYPE) ((TYPE *)((char *)(C) + whoami->offset))
107 /** @brief Return the value of an item */
108 #define VALUE(C, TYPE) (*ADDRESS(C, TYPE))
110 static int set_signal(const struct config_state
*cs
,
111 const struct conf
*whoami
,
112 int nvec
, char **vec
) {
116 error(0, "%s:%d: '%s' requires one argument",
117 cs
->path
, cs
->line
, whoami
->name
);
120 if((n
= find_signal(vec
[0])) == -1) {
121 error(0, "%s:%d: unknown signal '%s'",
122 cs
->path
, cs
->line
, vec
[0]);
125 VALUE(cs
->config
, int) = n
;
129 static int set_collections(const struct config_state
*cs
,
130 const struct conf
*whoami
,
131 int nvec
, char **vec
) {
132 struct collectionlist
*cl
;
135 error(0, "%s:%d: '%s' requires three arguments",
136 cs
->path
, cs
->line
, whoami
->name
);
139 if(vec
[2][0] != '/') {
140 error(0, "%s:%d: collection root must start with '/'",
144 if(vec
[2][1] && vec
[2][strlen(vec
[2])-1] == '/') {
145 error(0, "%s:%d: collection root must not end with '/'",
149 cl
= ADDRESS(cs
->config
, struct collectionlist
);
151 cl
->s
= xrealloc(cl
->s
, cl
->n
* sizeof (struct collection
));
152 cl
->s
[cl
->n
- 1].module
= xstrdup(vec
[0]);
153 cl
->s
[cl
->n
- 1].encoding
= xstrdup(vec
[1]);
154 cl
->s
[cl
->n
- 1].root
= xstrdup(vec
[2]);
158 static int set_boolean(const struct config_state
*cs
,
159 const struct conf
*whoami
,
160 int nvec
, char **vec
) {
164 error(0, "%s:%d: '%s' takes only one argument",
165 cs
->path
, cs
->line
, whoami
->name
);
168 if(!strcmp(vec
[0], "yes")) state
= 1;
169 else if(!strcmp(vec
[0], "no")) state
= 0;
171 error(0, "%s:%d: argument to '%s' must be 'yes' or 'no'",
172 cs
->path
, cs
->line
, whoami
->name
);
175 VALUE(cs
->config
, int) = state
;
179 static int set_string(const struct config_state
*cs
,
180 const struct conf
*whoami
,
181 int nvec
, char **vec
) {
183 error(0, "%s:%d: '%s' takes only one argument",
184 cs
->path
, cs
->line
, whoami
->name
);
187 VALUE(cs
->config
, char *) = xstrdup(vec
[0]);
191 static int set_stringlist(const struct config_state
*cs
,
192 const struct conf
*whoami
,
193 int nvec
, char **vec
) {
195 struct stringlist
*sl
;
197 sl
= ADDRESS(cs
->config
, struct stringlist
);
199 for(n
= 0; n
< nvec
; ++n
) {
201 sl
->s
= xrealloc(sl
->s
, (sl
->n
* sizeof (char *)));
202 sl
->s
[sl
->n
- 1] = xstrdup(vec
[n
]);
207 static int set_integer(const struct config_state
*cs
,
208 const struct conf
*whoami
,
209 int nvec
, char **vec
) {
213 error(0, "%s:%d: '%s' takes only one argument",
214 cs
->path
, cs
->line
, whoami
->name
);
217 if(xstrtol(ADDRESS(cs
->config
, long), vec
[0], &e
, 0)) {
218 error(errno
, "%s:%d: converting integer", cs
->path
, cs
->line
);
222 error(0, "%s:%d: invalid integer syntax", cs
->path
, cs
->line
);
228 static int set_stringlist_accum(const struct config_state
*cs
,
229 const struct conf
*whoami
,
230 int nvec
, char **vec
) {
232 struct stringlist
*s
;
233 struct stringlistlist
*sll
;
235 sll
= ADDRESS(cs
->config
, struct stringlistlist
);
241 sll
->s
= xrealloc(sll
->s
, (sll
->n
* sizeof (struct stringlist
)));
242 s
= &sll
->s
[sll
->n
- 1];
244 s
->s
= xmalloc((nvec
+ 1) * sizeof (char *));
245 for(n
= 0; n
< nvec
; ++n
)
246 s
->s
[n
] = xstrdup(vec
[n
]);
250 static int set_string_accum(const struct config_state
*cs
,
251 const struct conf
*whoami
,
252 int nvec
, char **vec
) {
254 struct stringlist
*sl
;
256 sl
= ADDRESS(cs
->config
, struct stringlist
);
261 for(n
= 0; n
< nvec
; ++n
) {
263 sl
->s
= xrealloc(sl
->s
, (sl
->n
* sizeof (char *)));
264 sl
->s
[sl
->n
- 1] = xstrdup(vec
[n
]);
269 static int set_restrict(const struct config_state
*cs
,
270 const struct conf
*whoami
,
271 int nvec
, char **vec
) {
275 static const struct restriction
{
279 { "remove", RESTRICT_REMOVE
},
280 { "scratch", RESTRICT_SCRATCH
},
281 { "move", RESTRICT_MOVE
},
284 for(n
= 0; n
< nvec
; ++n
) {
285 if((i
= TABLE_FIND(restrictions
, struct restriction
, name
, vec
[n
])) < 0) {
286 error(0, "%s:%d: invalid restriction '%s'",
287 cs
->path
, cs
->line
, vec
[n
]);
290 r
|= restrictions
[i
].bit
;
292 VALUE(cs
->config
, unsigned) = r
;
296 static int parse_sample_format(const struct config_state
*cs
,
297 struct stream_header
*format
,
298 int nvec
, char **vec
) {
303 error(0, "%s:%d: wrong number of arguments", cs
->path
, cs
->line
);
306 if(xstrtol(&t
, p
, &p
, 0)) {
307 error(errno
, "%s:%d: converting bits-per-sample", cs
->path
, cs
->line
);
310 if(t
!= 8 && t
!= 16) {
311 error(0, "%s:%d: bad bite-per-sample (%ld)", cs
->path
, cs
->line
, t
);
314 if(format
) format
->bits
= t
;
316 case 'l': case 'L': t
= ENDIAN_LITTLE
; p
++; break;
317 case 'b': case 'B': t
= ENDIAN_BIG
; p
++; break;
318 default: t
= ENDIAN_NATIVE
; break;
320 if(format
) format
->endian
= t
;
322 error(errno
, "%s:%d: expected `/' after bits-per-sample",
327 if(xstrtol(&t
, p
, &p
, 0)) {
328 error(errno
, "%s:%d: converting sample-rate", cs
->path
, cs
->line
);
331 if(t
< 1 || t
> INT_MAX
) {
332 error(0, "%s:%d: silly sample-rate (%ld)", cs
->path
, cs
->line
, t
);
335 if(format
) format
->rate
= t
;
337 error(0, "%s:%d: expected `/' after sample-rate",
342 if(xstrtol(&t
, p
, &p
, 0)) {
343 error(errno
, "%s:%d: converting channels", cs
->path
, cs
->line
);
347 error(0, "%s:%d: silly number (%ld) of channels", cs
->path
, cs
->line
, t
);
350 if(format
) format
->channels
= t
;
352 error(0, "%s:%d: junk after channels", cs
->path
, cs
->line
);
358 static int set_sample_format(const struct config_state
*cs
,
359 const struct conf
*whoami
,
360 int nvec
, char **vec
) {
361 return parse_sample_format(cs
, ADDRESS(cs
->config
, struct stream_header
),
365 static int set_namepart(const struct config_state
*cs
,
366 const struct conf
*whoami
,
367 int nvec
, char **vec
) {
368 struct namepartlist
*npl
= ADDRESS(cs
->config
, struct namepartlist
);
375 error(0, "%s:%d: namepart needs at least 3 arguments", cs
->path
, cs
->line
);
379 error(0, "%s:%d: namepart needs at most 5 arguments", cs
->path
, cs
->line
);
382 reflags
= nvec
>= 5 ?
regsub_flags(vec
[4]) : 0;
383 if(!(re
= pcre_compile(vec
[1],
385 |regsub_compile_options(reflags
),
386 &errstr
, &erroffset
, 0))) {
387 error(0, "%s:%d: error compiling regexp /%s/: %s (offset %d)",
388 cs
->path
, cs
->line
, vec
[1], errstr
, erroffset
);
391 npl
->s
= xrealloc(npl
->s
, (npl
->n
+ 1) * sizeof (struct namepart
));
392 npl
->s
[npl
->n
].part
= xstrdup(vec
[0]);
393 npl
->s
[npl
->n
].re
= re
;
394 npl
->s
[npl
->n
].replace
= xstrdup(vec
[2]);
395 npl
->s
[npl
->n
].context
= xstrdup(vec
[3]);
396 npl
->s
[npl
->n
].reflags
= reflags
;
398 /* XXX a bit of a bodge; relies on there being very few parts. */
399 for(n
= 0; (n
< cs
->config
->nparts
400 && strcmp(cs
->config
->parts
[n
], vec
[0])); ++n
)
402 if(n
>= cs
->config
->nparts
) {
403 cs
->config
->parts
= xrealloc(cs
->config
->parts
,
404 (cs
->config
->nparts
+ 1) * sizeof (char *));
405 cs
->config
->parts
[cs
->config
->nparts
++] = xstrdup(vec
[0]);
410 static int set_transform(const struct config_state
*cs
,
411 const struct conf
*whoami
,
412 int nvec
, char **vec
) {
413 struct transformlist
*tl
= ADDRESS(cs
->config
, struct transformlist
);
420 error(0, "%s:%d: transform needs at least 3 arguments", cs
->path
, cs
->line
);
424 error(0, "%s:%d: transform needs at most 5 arguments", cs
->path
, cs
->line
);
427 reflags
= (nvec
>= 5 ?
regsub_flags(vec
[4]) : 0);
428 if(!(re
= pcre_compile(vec
[1],
430 |regsub_compile_options(reflags
),
431 &errstr
, &erroffset
, 0))) {
432 error(0, "%s:%d: error compiling regexp /%s/: %s (offset %d)",
433 cs
->path
, cs
->line
, vec
[1], errstr
, erroffset
);
436 tl
->t
= xrealloc(tl
->t
, (tl
->n
+ 1) * sizeof (struct namepart
));
437 tl
->t
[tl
->n
].type
= xstrdup(vec
[0]);
438 tl
->t
[tl
->n
].context
= xstrdup(vec
[3] ? vec
[3] : "*");
439 tl
->t
[tl
->n
].re
= re
;
440 tl
->t
[tl
->n
].replace
= xstrdup(vec
[2]);
441 tl
->t
[tl
->n
].flags
= reflags
;
446 static int set_backend(const struct config_state
*cs
,
447 const struct conf
*whoami
,
448 int nvec
, char **vec
) {
449 int *const valuep
= ADDRESS(cs
->config
, int);
452 error(0, "%s:%d: '%s' requires one argument",
453 cs
->path
, cs
->line
, whoami
->name
);
456 if(!strcmp(vec
[0], "alsa")) {
457 #if HAVE_ALSA_ASOUNDLIB_H
458 *valuep
= BACKEND_ALSA
;
460 error(0, "%s:%d: ALSA is not available on this platform",
464 } else if(!strcmp(vec
[0], "command"))
465 *valuep
= BACKEND_COMMAND
;
466 else if(!strcmp(vec
[0], "network"))
467 *valuep
= BACKEND_NETWORK
;
468 else if(!strcmp(vec
[0], "coreaudio")) {
469 #if HAVE_COREAUDIO_AUDIOHARDWARE_H
470 *valuep
= BACKEND_COREAUDIO
;
472 error(0, "%s:%d: Core Audio is not available on this platform",
476 } else if(!strcmp(vec
[0], "oss")) {
477 #if HAVE_SYS_SOUNDCARD_H
478 *valuep
= BACKEND_OSS
;
480 error(0, "%s:%d: OSS is not available on this platform",
485 error(0, "%s:%d: invalid '%s' value '%s'",
486 cs
->path
, cs
->line
, whoami
->name
, vec
[0]);
494 static void free_none(struct config
attribute((unused
)) *c
,
495 const struct conf
attribute((unused
)) *whoami
) {
498 static void free_string(struct config
*c
,
499 const struct conf
*whoami
) {
500 xfree(VALUE(c
, char *));
503 static void free_stringlist(struct config
*c
,
504 const struct conf
*whoami
) {
506 struct stringlist
*sl
= ADDRESS(c
, struct stringlist
);
508 for(n
= 0; n
< sl
->n
; ++n
)
513 static void free_stringlistlist(struct config
*c
,
514 const struct conf
*whoami
) {
516 struct stringlistlist
*sll
= ADDRESS(c
, struct stringlistlist
);
517 struct stringlist
*sl
;
519 for(n
= 0; n
< sll
->n
; ++n
) {
521 for(m
= 0; m
< sl
->n
; ++m
)
528 static void free_collectionlist(struct config
*c
,
529 const struct conf
*whoami
) {
530 struct collectionlist
*cll
= ADDRESS(c
, struct collectionlist
);
531 struct collection
*cl
;
534 for(n
= 0; n
< cll
->n
; ++n
) {
543 static void free_namepartlist(struct config
*c
,
544 const struct conf
*whoami
) {
545 struct namepartlist
*npl
= ADDRESS(c
, struct namepartlist
);
549 for(n
= 0; n
< npl
->n
; ++n
) {
552 pcre_free(np
->re
); /* ...whatever pcre_free is set to. */
559 static void free_transformlist(struct config
*c
,
560 const struct conf
*whoami
) {
561 struct transformlist
*tl
= ADDRESS(c
, struct transformlist
);
565 for(n
= 0; n
< tl
->n
; ++n
) {
568 pcre_free(t
->re
); /* ...whatever pcre_free is set to. */
575 /* configuration types */
577 static const struct conftype
578 type_signal
= { set_signal
, free_none
},
579 type_collections
= { set_collections
, free_collectionlist
},
580 type_boolean
= { set_boolean
, free_none
},
581 type_string
= { set_string
, free_string
},
582 type_stringlist
= { set_stringlist
, free_stringlist
},
583 type_integer
= { set_integer
, free_none
},
584 type_stringlist_accum
= { set_stringlist_accum
, free_stringlistlist
},
585 type_string_accum
= { set_string_accum
, free_stringlist
},
586 type_sample_format
= { set_sample_format
, free_none
},
587 type_restrict
= { set_restrict
, free_none
},
588 type_namepart
= { set_namepart
, free_namepartlist
},
589 type_transform
= { set_transform
, free_transformlist
},
590 type_backend
= { set_backend
, free_none
};
592 /* specific validation routine */
594 #define VALIDATE_FILE(test, what) do { \
598 for(n = 0; n < nvec; ++n) { \
599 if(stat(vec[n], &sb) < 0) { \
600 error(errno, "%s:%d: %s", cs->path, cs->line, vec[n]); \
603 if(!test(sb.st_mode)) { \
604 error(0, "%s:%d: %s is not a %s", \
605 cs->path, cs->line, vec[n], what); \
611 static int validate_isabspath(const struct config_state
*cs
,
612 int nvec
, char **vec
) {
615 for(n
= 0; n
< nvec
; ++n
)
616 if(vec
[n
][0] != '/') {
617 error(errno
, "%s:%d: %s: not an absolute path",
618 cs
->path
, cs
->line
, vec
[n
]);
624 static int validate_isdir(const struct config_state
*cs
,
625 int nvec
, char **vec
) {
626 VALIDATE_FILE(S_ISDIR
, "directory");
630 static int validate_isreg(const struct config_state
*cs
,
631 int nvec
, char **vec
) {
632 VALIDATE_FILE(S_ISREG
, "regular file");
636 static int validate_ischr(const struct config_state
*cs
,
637 int nvec
, char **vec
) {
638 VALIDATE_FILE(S_ISCHR
, "character device");
642 static int validate_player(const struct config_state
*cs
,
644 char attribute((unused
)) **vec
) {
646 error(0, "%s:%d: should be at least 'player PATTERN MODULE'",
653 static int validate_tracklength(const struct config_state
*cs
,
655 char attribute((unused
)) **vec
) {
657 error(0, "%s:%d: should be at least 'tracklength PATTERN MODULE'",
664 static int validate_allow(const struct config_state
*cs
,
666 char attribute((unused
)) **vec
) {
668 error(0, "%s:%d: must be 'allow NAME PASS'", cs
->path
, cs
->line
);
674 static int validate_non_negative(const struct config_state
*cs
,
675 int nvec
, char **vec
) {
679 error(0, "%s:%d: missing argument", cs
->path
, cs
->line
);
683 error(0, "%s:%d: too many arguments", cs
->path
, cs
->line
);
686 if(xstrtol(&n
, vec
[0], 0, 0)) {
687 error(0, "%s:%d: %s", cs
->path
, cs
->line
, strerror(errno
));
691 error(0, "%s:%d: must not be negative", cs
->path
, cs
->line
);
697 static int validate_positive(const struct config_state
*cs
,
698 int nvec
, char **vec
) {
702 error(0, "%s:%d: missing argument", cs
->path
, cs
->line
);
706 error(0, "%s:%d: too many arguments", cs
->path
, cs
->line
);
709 if(xstrtol(&n
, vec
[0], 0, 0)) {
710 error(0, "%s:%d: %s", cs
->path
, cs
->line
, strerror(errno
));
714 error(0, "%s:%d: must be positive", cs
->path
, cs
->line
);
720 static int validate_isauser(const struct config_state
*cs
,
721 int attribute((unused
)) nvec
,
725 if(!(pw
= getpwnam(vec
[0]))) {
726 error(0, "%s:%d: no such user as '%s'", cs
->path
, cs
->line
, vec
[0]);
732 static int validate_sample_format(const struct config_state
*cs
,
733 int attribute((unused
)) nvec
,
735 return parse_sample_format(cs
, 0, nvec
, vec
);
738 static int validate_channel(const struct config_state
*cs
,
739 int attribute((unused
)) nvec
,
741 if(mixer_channel(vec
[0]) == -1) {
742 error(0, "%s:%d: invalid channel '%s'", cs
->path
, cs
->line
, vec
[0]);
748 static int validate_any(const struct config_state
attribute((unused
)) *cs
,
749 int attribute((unused
)) nvec
,
750 char attribute((unused
)) **vec
) {
754 static int validate_url(const struct config_state
attribute((unused
)) *cs
,
755 int attribute((unused
)) nvec
,
759 /* absoluteURI = scheme ":" ( hier_part | opaque_part )
760 scheme = alpha *( alpha | digit | "+" | "-" | "." ) */
762 n
= strspn(s
, ("abcdefghijklmnopqrstuvwxyz"
763 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
766 error(0, "%s:%d: invalid url '%s'", cs
->path
, cs
->line
, vec
[0]);
769 if(!strncmp(s
, "http:", 5)
770 || !strncmp(s
, "https:", 6)) {
772 /* we only do a rather cursory check */
773 if(strncmp(s
, "//", 2)) {
774 error(0, "%s:%d: invalid url '%s'", cs
->path
, cs
->line
, vec
[0]);
781 static int validate_alias(const struct config_state
*cs
,
785 int in_brackets
= 0, c
;
788 error(0, "%s:%d: missing argument", cs
->path
, cs
->line
);
792 error(0, "%s:%d: too many arguments", cs
->path
, cs
->line
);
796 while((c
= (unsigned char)*s
++)) {
800 else if(!isalnum(c
)) {
801 error(0, "%s:%d: invalid part name in alias expansion in '%s'",
802 cs
->path
, cs
->line
, vec
[0]);
810 } else if(c
== '\\') {
811 if(!(c
= (unsigned char)*s
++)) {
812 error(0, "%s:%d: unterminated escape in alias expansion in '%s'",
813 cs
->path
, cs
->line
, vec
[0]);
815 } else if(c
!= '\\' && c
!= '{') {
816 error(0, "%s:%d: invalid escape in alias expansion in '%s'",
817 cs
->path
, cs
->line
, vec
[0]);
825 error(0, "%s:%d: unterminated part name in alias expansion in '%s'",
826 cs
->path
, cs
->line
, vec
[0]);
832 static int validate_addrport(const struct config_state
attribute((unused
)) *cs
,
834 char attribute((unused
)) **vec
) {
837 error(0, "%s:%d: missing address",
841 error(0, "%s:%d: missing port name/number",
847 error(0, "%s:%d: expected ADDRESS PORT",
853 static int validate_port(const struct config_state
attribute((unused
)) *cs
,
855 char attribute((unused
)) **vec
) {
858 error(0, "%s:%d: missing address",
865 error(0, "%s:%d: expected [ADDRESS] PORT",
871 static int validate_algo(const struct config_state
attribute((unused
)) *cs
,
875 error(0, "%s:%d: invalid algorithm specification", cs
->path
, cs
->line
);
878 if(!valid_authhash(vec
[0])) {
879 error(0, "%s:%d: unsuported algorithm '%s'", cs
->path
, cs
->line
, vec
[0]);
885 /** @brief Item name and and offset */
886 #define C(x) #x, offsetof(struct config, x)
887 /** @brief Item name and and offset */
888 #define C2(x,y) #x, offsetof(struct config, y)
890 /** @brief All configuration items */
891 static const struct conf conf
[] = {
892 { C(alias
), &type_string
, validate_alias
},
893 { C(allow
), &type_stringlist_accum
, validate_allow
},
894 { C(authorization_algorithm
), &type_string
, validate_algo
},
895 { C(broadcast
), &type_stringlist
, validate_addrport
},
896 { C(broadcast_from
), &type_stringlist
, validate_addrport
},
897 { C(channel
), &type_string
, validate_channel
},
898 { C(checkpoint_kbyte
), &type_integer
, validate_non_negative
},
899 { C(checkpoint_min
), &type_integer
, validate_non_negative
},
900 { C(collection
), &type_collections
, validate_any
},
901 { C(connect
), &type_stringlist
, validate_addrport
},
902 { C(cookie_login_lifetime
), &type_integer
, validate_positive
},
903 { C(cookie_key_lifetime
), &type_integer
, validate_positive
},
904 { C(dbversion
), &type_integer
, validate_positive
},
905 { C(device
), &type_string
, validate_any
},
906 { C(gap
), &type_integer
, validate_non_negative
},
907 { C(history
), &type_integer
, validate_positive
},
908 { C(home
), &type_string
, validate_isabspath
},
909 { C(listen
), &type_stringlist
, validate_port
},
910 { C(lock
), &type_boolean
, validate_any
},
911 { C(mixer
), &type_string
, validate_ischr
},
912 { C(multicast_loop
), &type_boolean
, validate_any
},
913 { C(multicast_ttl
), &type_integer
, validate_non_negative
},
914 { C(namepart
), &type_namepart
, validate_any
},
915 { C2(nice
, nice_rescan
), &type_integer
, validate_non_negative
},
916 { C(nice_rescan
), &type_integer
, validate_non_negative
},
917 { C(nice_server
), &type_integer
, validate_any
},
918 { C(nice_speaker
), &type_integer
, validate_any
},
919 { C(noticed_history
), &type_integer
, validate_positive
},
920 { C(password
), &type_string
, validate_any
},
921 { C(player
), &type_stringlist_accum
, validate_player
},
922 { C(plugins
), &type_string_accum
, validate_isdir
},
923 { C(prefsync
), &type_integer
, validate_positive
},
924 { C(queue_pad
), &type_integer
, validate_positive
},
925 { C(refresh
), &type_integer
, validate_positive
},
926 { C2(restrict
, restrictions
), &type_restrict
, validate_any
},
927 { C(sample_format
), &type_sample_format
, validate_sample_format
},
928 { C(scratch
), &type_string_accum
, validate_isreg
},
929 { C(short_display
), &type_integer
, validate_positive
},
930 { C(signal
), &type_signal
, validate_any
},
931 { C(sox_generation
), &type_integer
, validate_non_negative
},
932 { C(speaker_backend
), &type_backend
, validate_any
},
933 { C(speaker_command
), &type_string
, validate_any
},
934 { C(stopword
), &type_string_accum
, validate_any
},
935 { C(templates
), &type_string_accum
, validate_isdir
},
936 { C(tracklength
), &type_stringlist_accum
, validate_tracklength
},
937 { C(transform
), &type_transform
, validate_any
},
938 { C(trust
), &type_string_accum
, validate_any
},
939 { C(url
), &type_string
, validate_url
},
940 { C(user
), &type_string
, validate_isauser
},
941 { C(username
), &type_string
, validate_any
},
944 /** @brief Find a configuration item's definition by key */
945 static const struct conf
*find(const char *key
) {
948 if((n
= TABLE_FIND(conf
, struct conf
, name
, key
)) < 0)
953 /** @brief Set a new configuration value */
954 static int config_set(const struct config_state
*cs
,
955 int nvec
, char **vec
) {
956 const struct conf
*which
;
958 D(("config_set %s", vec
[0]));
959 if(!(which
= find(vec
[0]))) {
960 error(0, "%s:%d: unknown configuration key '%s'",
961 cs
->path
, cs
->line
, vec
[0]);
964 return (which
->validate(cs
, nvec
- 1, vec
+ 1)
965 || which
->type
->set(cs
, which
, nvec
- 1, vec
+ 1));
968 /** @brief Error callback used by config_include() */
969 static void config_error(const char *msg
, void *u
) {
970 const struct config_state
*cs
= u
;
972 error(0, "%s:%d: %s", cs
->path
, cs
->line
, msg
);
975 /** @brief Include a file by name */
976 static int config_include(struct config
*c
, const char *path
) {
978 char *buffer
, *inputbuffer
, **vec
;
980 struct config_state cs
;
985 D(("%s: reading configuration", path
));
986 if(!(fp
= fopen(path
, "r"))) {
987 error(errno
, "error opening %s", path
);
990 while(!inputline(path
, fp
, &inputbuffer
, '\n')) {
992 if(!(buffer
= mb2utf8(inputbuffer
))) {
993 error(errno
, "%s:%d: cannot convert to UTF-8", cs
.path
, cs
.line
);
999 if(!(vec
= split(buffer
, &n
, SPLIT_COMMENTS
|SPLIT_QUOTES
,
1000 config_error
, &cs
))) {
1006 if(!strcmp(vec
[0], "include")) {
1008 error(0, "%s:%d: must be 'include PATH'", cs
.path
, cs
.line
);
1011 config_include(c
, vec
[1]);
1013 ret
|= config_set(&cs
, n
, vec
);
1015 for(n
= 0; vec
[n
]; ++n
) xfree(vec
[n
]);
1020 error(errno
, "error reading %s", path
);
1027 /** @brief Make a new default configuration */
1028 static struct config
*config_default(void) {
1029 struct config
*c
= xmalloc(sizeof *c
);
1030 const char *logname
;
1033 /* Strings had better be xstrdup'd as they will get freed at some point. */
1036 c
->home
= xstrdup(pkgstatedir
);
1037 if(!(pw
= getpwuid(getuid())))
1038 fatal(0, "cannot determine our username");
1039 logname
= pw
->pw_name
;
1040 c
->username
= xstrdup(logname
);
1043 c
->signal
= SIGKILL
;
1044 c
->alias
= xstrdup("{/artist}{/album}{/title}{ext}");
1046 c
->device
= xstrdup("default");
1047 c
->nice_rescan
= 10;
1048 c
->speaker_command
= 0;
1049 c
->sample_format
.bits
= 16;
1050 c
->sample_format
.rate
= 44100;
1051 c
->sample_format
.channels
= 2;
1052 c
->sample_format
.endian
= ENDIAN_NATIVE
;
1054 c
->speaker_backend
= -1;
1055 c
->multicast_ttl
= 1;
1056 c
->multicast_loop
= 1;
1057 c
->authorization_algorithm
= xstrdup("sha1");
1058 c
->noticed_history
= 31;
1059 c
->short_display
= 32;
1060 c
->mixer
= xstrdup("/dev/mixer");
1061 c
->channel
= xstrdup("pcm");
1063 c
->cookie_login_lifetime
= 86400;
1064 c
->cookie_key_lifetime
= 86400 * 7;
1068 static char *get_file(struct config
*c
, const char *name
) {
1071 byte_xasprintf(&s
, "%s/%s", c
->home
, name
);
1075 /** @brief Set the default configuration file */
1076 static void set_configfile(void) {
1078 byte_xasprintf(&configfile
, "%s/config", pkgconfdir
);
1081 /** @brief Free a configuration object */
1082 static void config_free(struct config
*c
) {
1086 for(n
= 0; n
< (int)(sizeof conf
/ sizeof *conf
); ++n
)
1087 conf
[n
].type
->free(c
, &conf
[n
]);
1088 for(n
= 0; n
< c
->nparts
; ++n
)
1095 /** @brief Set post-parse defaults */
1096 static void config_postdefaults(struct config
*c
,
1098 struct config_state cs
;
1099 const struct conf
*whoami
;
1102 static const char *namepart
[][4] = {
1103 { "title", "/([0-9]+ *[-:] *)?([^/]+)\\.[a-zA-Z0-9]+$", "$2", "display" },
1104 { "title", "/([^/]+)\\.[a-zA-Z0-9]+$", "$1", "sort" },
1105 { "album", "/([^/]+)/[^/]+$", "$1", "*" },
1106 { "artist", "/([^/]+)/[^/]+/[^/]+$", "$1", "*" },
1107 { "ext", "(\\.[a-zA-Z0-9]+)$", "$1", "*" },
1109 #define NNAMEPART (int)(sizeof namepart / sizeof *namepart)
1111 static const char *transform
[][5] = {
1112 { "track", "^.*/([0-9]+ *[-:] *)?([^/]+)\\.[a-zA-Z0-9]+$", "$2", "display", "" },
1113 { "track", "^.*/([^/]+)\\.[a-zA-Z0-9]+$", "$1", "sort", "" },
1114 { "dir", "^.*/([^/]+)$", "$1", "*", "" },
1115 { "dir", "^(the) ([^/]*)", "$2, $1", "sort", "i", },
1116 { "dir", "[[:punct:]]", "", "sort", "g", }
1118 #define NTRANSFORM (int)(sizeof transform / sizeof *transform)
1120 cs
.path
= "<internal>";
1123 if(!c
->namepart
.n
) {
1124 whoami
= find("namepart");
1125 for(n
= 0; n
< NNAMEPART
; ++n
)
1126 set_namepart(&cs
, whoami
, 4, (char **)namepart
[n
]);
1128 if(!c
->transform
.n
) {
1129 whoami
= find("transform");
1130 for(n
= 0; n
< NTRANSFORM
; ++n
)
1131 set_transform(&cs
, whoami
, 5, (char **)transform
[n
]);
1133 if(c
->speaker_backend
== -1) {
1134 if(c
->speaker_command
)
1135 c
->speaker_backend
= BACKEND_COMMAND
;
1136 else if(c
->broadcast
.n
)
1137 c
->speaker_backend
= BACKEND_NETWORK
;
1139 #if HAVE_ALSA_ASOUNDLIB_H
1140 c
->speaker_backend
= BACKEND_ALSA
;
1141 #elif HAVE_SYS_SOUNDCARD_H
1142 c
->speaker_backend
= BACKEND_OSS
;
1143 #elif HAVE_COREAUDIO_AUDIOHARDWARE_H
1144 c
->speaker_backend
= BACKEND_COREAUDIO
;
1146 c
->speaker_backend
= BACKEND_COMMAND
;
1151 if(c
->speaker_backend
== BACKEND_COMMAND
&& !c
->speaker_command
)
1152 fatal(0, "speaker_backend is command but speaker_command is not set");
1153 if(c
->speaker_backend
== BACKEND_NETWORK
&& !c
->broadcast
.n
)
1154 fatal(0, "speaker_backend is network but broadcast is not set");
1156 /* Override sample format */
1157 switch(c
->speaker_backend
) {
1158 case BACKEND_NETWORK
:
1159 c
->sample_format
.rate
= 44100;
1160 c
->sample_format
.channels
= 2;
1161 c
->sample_format
.bits
= 16;
1162 c
->sample_format
.endian
= ENDIAN_BIG
;
1164 case BACKEND_COREAUDIO
:
1165 c
->sample_format
.rate
= 44100;
1166 c
->sample_format
.channels
= 2;
1167 c
->sample_format
.bits
= 16;
1168 c
->sample_format
.endian
= ENDIAN_NATIVE
;
1173 /** @brief (Re-)read the config file
1174 * @param server If set, do extra checking
1176 int config_read(int server
) {
1182 c
= config_default();
1183 /* standalone Disobedience installs might not have a global config file */
1184 if(access(configfile
, F_OK
) == 0)
1185 if(config_include(c
, configfile
))
1187 /* if we can read the private config file, do */
1188 if((privconf
= config_private())
1189 && access(privconf
, R_OK
) == 0
1190 && config_include(c
, privconf
))
1193 /* if there's a per-user system config file for this user, read it */
1194 if(config_per_user
) {
1195 if(!(pw
= getpwuid(getuid())))
1196 fatal(0, "cannot determine our username");
1197 if((privconf
= config_usersysconf(pw
))
1198 && access(privconf
, F_OK
) == 0
1199 && config_include(c
, privconf
))
1202 /* if we have a password file, read it */
1203 if((privconf
= config_userconf(getenv("HOME"), pw
))
1204 && access(privconf
, F_OK
) == 0
1205 && config_include(c
, privconf
))
1209 /* install default namepart and transform settings */
1210 config_postdefaults(c
, server
);
1211 /* everything is good so we shall use the new config */
1212 config_free(config
);
1217 /** @brief Return the path to the private configuration file */
1218 char *config_private(void) {
1222 byte_xasprintf(&s
, "%s.private", configfile
);
1226 /** @brief Return the path to user's personal configuration file */
1227 char *config_userconf(const char *home
, const struct passwd
*pw
) {
1230 if(!home
&& !pw
&& !(pw
= getpwuid(getuid())))
1231 fatal(0, "cannot determine our username");
1232 byte_xasprintf(&s
, "%s/.disorder/passwd", home ? home
: pw
->pw_dir
);
1236 /** @brief Return the path to user-specific system configuration */
1237 char *config_usersysconf(const struct passwd
*pw
) {
1241 if(!strchr(pw
->pw_name
, '/')) {
1242 byte_xasprintf(&s
, "%s.%s", configfile
, pw
->pw_name
);
1248 char *config_get_file(const char *name
) {
1249 return get_file(config
, name
);