2 * This file is part of DisOrder.
3 * Copyright (C) 2004, 2005, 2006, 2007 Richard Kettlewell
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
20 /** @file lib/configuration.c
21 * @brief Configuration file support
31 #include <sys/types.h>
41 #include "configuration.h"
47 #include "inputline.h"
56 /** @brief Path to config file
58 * set_configfile() sets the deafult if it is null.
62 /** @brief Config file parser state */
64 /** @brief Filename */
66 /** @brief Line number */
68 /** @brief Configuration object under construction */
69 struct config
*config
;
72 /** @brief Current configuration */
73 struct config
*config
;
75 /** @brief One configuration item */
77 /** @brief Name as it appears in the config file */
79 /** @brief Offset in @ref config structure */
81 /** @brief Pointer to item type */
82 const struct conftype
*type
;
83 /** @brief Pointer to item-specific validation routine */
84 int (*validate
)(const struct config_state
*cs
,
85 int nvec
, char **vec
);
88 /** @brief Type of a configuration item */
90 /** @brief Pointer to function to set item */
91 int (*set
)(const struct config_state
*cs
,
92 const struct conf
*whoami
,
93 int nvec
, char **vec
);
94 /** @brief Pointer to function to free item */
95 void (*free
)(struct config
*c
, const struct conf
*whoami
);
98 /** @brief Compute the address of an item */
99 #define ADDRESS(C, TYPE) ((TYPE *)((char *)(C) + whoami->offset))
100 /** @brief Return the value of an item */
101 #define VALUE(C, TYPE) (*ADDRESS(C, TYPE))
103 static int set_signal(const struct config_state
*cs
,
104 const struct conf
*whoami
,
105 int nvec
, char **vec
) {
109 error(0, "%s:%d: '%s' requires one argument",
110 cs
->path
, cs
->line
, whoami
->name
);
113 if((n
= find_signal(vec
[0])) == -1) {
114 error(0, "%s:%d: unknown signal '%s'",
115 cs
->path
, cs
->line
, vec
[0]);
118 VALUE(cs
->config
, int) = n
;
122 static int set_collections(const struct config_state
*cs
,
123 const struct conf
*whoami
,
124 int nvec
, char **vec
) {
125 struct collectionlist
*cl
;
128 error(0, "%s:%d: '%s' requires three arguments",
129 cs
->path
, cs
->line
, whoami
->name
);
132 if(vec
[2][0] != '/') {
133 error(0, "%s:%d: collection root must start with '/'",
137 if(vec
[2][1] && vec
[2][strlen(vec
[2])-1] == '/') {
138 error(0, "%s:%d: collection root must not end with '/'",
142 cl
= ADDRESS(cs
->config
, struct collectionlist
);
144 cl
->s
= xrealloc(cl
->s
, cl
->n
* sizeof (struct collection
));
145 cl
->s
[cl
->n
- 1].module
= xstrdup(vec
[0]);
146 cl
->s
[cl
->n
- 1].encoding
= xstrdup(vec
[1]);
147 cl
->s
[cl
->n
- 1].root
= xstrdup(vec
[2]);
151 static int set_boolean(const struct config_state
*cs
,
152 const struct conf
*whoami
,
153 int nvec
, char **vec
) {
157 error(0, "%s:%d: '%s' takes only one argument",
158 cs
->path
, cs
->line
, whoami
->name
);
161 if(!strcmp(vec
[0], "yes")) state
= 1;
162 else if(!strcmp(vec
[0], "no")) state
= 0;
164 error(0, "%s:%d: argument to '%s' must be 'yes' or 'no'",
165 cs
->path
, cs
->line
, whoami
->name
);
168 VALUE(cs
->config
, int) = state
;
172 static int set_string(const struct config_state
*cs
,
173 const struct conf
*whoami
,
174 int nvec
, char **vec
) {
176 error(0, "%s:%d: '%s' takes only one argument",
177 cs
->path
, cs
->line
, whoami
->name
);
180 VALUE(cs
->config
, char *) = xstrdup(vec
[0]);
184 static int set_stringlist(const struct config_state
*cs
,
185 const struct conf
*whoami
,
186 int nvec
, char **vec
) {
188 struct stringlist
*sl
;
190 sl
= ADDRESS(cs
->config
, struct stringlist
);
192 for(n
= 0; n
< nvec
; ++n
) {
194 sl
->s
= xrealloc(sl
->s
, (sl
->n
* sizeof (char *)));
195 sl
->s
[sl
->n
- 1] = xstrdup(vec
[n
]);
200 static int set_integer(const struct config_state
*cs
,
201 const struct conf
*whoami
,
202 int nvec
, char **vec
) {
206 error(0, "%s:%d: '%s' takes only one argument",
207 cs
->path
, cs
->line
, whoami
->name
);
210 if(xstrtol(ADDRESS(cs
->config
, long), vec
[0], &e
, 0)) {
211 error(errno
, "%s:%d: converting integer", cs
->path
, cs
->line
);
215 error(0, "%s:%d: invalid integer syntax", cs
->path
, cs
->line
);
221 static int set_stringlist_accum(const struct config_state
*cs
,
222 const struct conf
*whoami
,
223 int nvec
, char **vec
) {
225 struct stringlist
*s
;
226 struct stringlistlist
*sll
;
228 sll
= ADDRESS(cs
->config
, struct stringlistlist
);
234 sll
->s
= xrealloc(sll
->s
, (sll
->n
* sizeof (struct stringlist
)));
235 s
= &sll
->s
[sll
->n
- 1];
237 s
->s
= xmalloc((nvec
+ 1) * sizeof (char *));
238 for(n
= 0; n
< nvec
; ++n
)
239 s
->s
[n
] = xstrdup(vec
[n
]);
243 static int set_string_accum(const struct config_state
*cs
,
244 const struct conf
*whoami
,
245 int nvec
, char **vec
) {
247 struct stringlist
*sl
;
249 sl
= ADDRESS(cs
->config
, struct stringlist
);
254 for(n
= 0; n
< nvec
; ++n
) {
256 sl
->s
= xrealloc(sl
->s
, (sl
->n
* sizeof (char *)));
257 sl
->s
[sl
->n
- 1] = xstrdup(vec
[n
]);
262 static int set_restrict(const struct config_state
*cs
,
263 const struct conf
*whoami
,
264 int nvec
, char **vec
) {
268 static const struct restriction
{
272 { "remove", RESTRICT_REMOVE
},
273 { "scratch", RESTRICT_SCRATCH
},
274 { "move", RESTRICT_MOVE
},
277 for(n
= 0; n
< nvec
; ++n
) {
278 if((i
= TABLE_FIND(restrictions
, struct restriction
, name
, vec
[n
])) < 0) {
279 error(0, "%s:%d: invalid restriction '%s'",
280 cs
->path
, cs
->line
, vec
[n
]);
283 r
|= restrictions
[i
].bit
;
285 VALUE(cs
->config
, unsigned) = r
;
289 static int parse_sample_format(const struct config_state
*cs
,
290 struct stream_header
*format
,
291 int nvec
, char **vec
) {
296 error(0, "%s:%d: wrong number of arguments", cs
->path
, cs
->line
);
299 if(xstrtol(&t
, p
, &p
, 0)) {
300 error(errno
, "%s:%d: converting bits-per-sample", cs
->path
, cs
->line
);
303 if(t
!= 8 && t
!= 16) {
304 error(0, "%s:%d: bad bite-per-sample (%ld)", cs
->path
, cs
->line
, t
);
307 if(format
) format
->bits
= t
;
309 case 'l': case 'L': t
= ENDIAN_LITTLE
; p
++; break;
310 case 'b': case 'B': t
= ENDIAN_BIG
; p
++; break;
311 default: t
= ENDIAN_NATIVE
; break;
313 if(format
) format
->endian
= t
;
315 error(errno
, "%s:%d: expected `/' after bits-per-sample",
320 if(xstrtol(&t
, p
, &p
, 0)) {
321 error(errno
, "%s:%d: converting sample-rate", cs
->path
, cs
->line
);
324 if(t
< 1 || t
> INT_MAX
) {
325 error(0, "%s:%d: silly sample-rate (%ld)", cs
->path
, cs
->line
, t
);
328 if(format
) format
->rate
= t
;
330 error(0, "%s:%d: expected `/' after sample-rate",
335 if(xstrtol(&t
, p
, &p
, 0)) {
336 error(errno
, "%s:%d: converting channels", cs
->path
, cs
->line
);
340 error(0, "%s:%d: silly number (%ld) of channels", cs
->path
, cs
->line
, t
);
343 if(format
) format
->channels
= t
;
345 error(0, "%s:%d: junk after channels", cs
->path
, cs
->line
);
351 static int set_sample_format(const struct config_state
*cs
,
352 const struct conf
*whoami
,
353 int nvec
, char **vec
) {
354 return parse_sample_format(cs
, ADDRESS(cs
->config
, struct stream_header
),
358 static int set_namepart(const struct config_state
*cs
,
359 const struct conf
*whoami
,
360 int nvec
, char **vec
) {
361 struct namepartlist
*npl
= ADDRESS(cs
->config
, struct namepartlist
);
368 error(0, "%s:%d: namepart needs at least 3 arguments", cs
->path
, cs
->line
);
372 error(0, "%s:%d: namepart needs at most 5 arguments", cs
->path
, cs
->line
);
375 reflags
= nvec
>= 5 ?
regsub_flags(vec
[4]) : 0;
376 if(!(re
= pcre_compile(vec
[1],
378 |regsub_compile_options(reflags
),
379 &errstr
, &erroffset
, 0))) {
380 error(0, "%s:%d: error compiling regexp /%s/: %s (offset %d)",
381 cs
->path
, cs
->line
, vec
[1], errstr
, erroffset
);
384 npl
->s
= xrealloc(npl
->s
, (npl
->n
+ 1) * sizeof (struct namepart
));
385 npl
->s
[npl
->n
].part
= xstrdup(vec
[0]);
386 npl
->s
[npl
->n
].re
= re
;
387 npl
->s
[npl
->n
].replace
= xstrdup(vec
[2]);
388 npl
->s
[npl
->n
].context
= xstrdup(vec
[3]);
389 npl
->s
[npl
->n
].reflags
= reflags
;
391 /* XXX a bit of a bodge; relies on there being very few parts. */
392 for(n
= 0; (n
< cs
->config
->nparts
393 && strcmp(cs
->config
->parts
[n
], vec
[0])); ++n
)
395 if(n
>= cs
->config
->nparts
) {
396 cs
->config
->parts
= xrealloc(cs
->config
->parts
,
397 (cs
->config
->nparts
+ 1) * sizeof (char *));
398 cs
->config
->parts
[cs
->config
->nparts
++] = xstrdup(vec
[0]);
403 static int set_transform(const struct config_state
*cs
,
404 const struct conf
*whoami
,
405 int nvec
, char **vec
) {
406 struct transformlist
*tl
= ADDRESS(cs
->config
, struct transformlist
);
413 error(0, "%s:%d: transform needs at least 3 arguments", cs
->path
, cs
->line
);
417 error(0, "%s:%d: transform needs at most 5 arguments", cs
->path
, cs
->line
);
420 reflags
= (nvec
>= 5 ?
regsub_flags(vec
[4]) : 0);
421 if(!(re
= pcre_compile(vec
[1],
423 |regsub_compile_options(reflags
),
424 &errstr
, &erroffset
, 0))) {
425 error(0, "%s:%d: error compiling regexp /%s/: %s (offset %d)",
426 cs
->path
, cs
->line
, vec
[1], errstr
, erroffset
);
429 tl
->t
= xrealloc(tl
->t
, (tl
->n
+ 1) * sizeof (struct namepart
));
430 tl
->t
[tl
->n
].type
= xstrdup(vec
[0]);
431 tl
->t
[tl
->n
].context
= xstrdup(vec
[3] ? vec
[3] : "*");
432 tl
->t
[tl
->n
].re
= re
;
433 tl
->t
[tl
->n
].replace
= xstrdup(vec
[2]);
434 tl
->t
[tl
->n
].flags
= reflags
;
439 static int set_backend(const struct config_state
*cs
,
440 const struct conf
*whoami
,
441 int nvec
, char **vec
) {
442 int *const valuep
= ADDRESS(cs
->config
, int);
445 error(0, "%s:%d: '%s' requires one argument",
446 cs
->path
, cs
->line
, whoami
->name
);
449 if(!strcmp(vec
[0], "alsa")) {
450 #if HAVE_ALSA_ASOUNDLIB_H
451 *valuep
= BACKEND_ALSA
;
453 error(0, "%s:%d: ALSA is not available on this platform",
457 } else if(!strcmp(vec
[0], "command"))
458 *valuep
= BACKEND_COMMAND
;
459 else if(!strcmp(vec
[0], "network"))
460 *valuep
= BACKEND_NETWORK
;
461 else if(!strcmp(vec
[0], "coreaudio")) {
462 #if HAVE_COREAUDIO_AUDIOHARDWARE_H
463 *valuep
= BACKEND_COREAUDIO
;
465 error(0, "%s:%d: Core Audio is not available on this platform",
469 } else if(!strcmp(vec
[0], "oss")) {
470 #if HAVE_SYS_SOUNDCARD_H
471 *valuep
= BACKEND_OSS
;
473 error(0, "%s:%d: OSS is not available on this platform",
478 error(0, "%s:%d: invalid '%s' value '%s'",
479 cs
->path
, cs
->line
, whoami
->name
, vec
[0]);
487 static void free_none(struct config
attribute((unused
)) *c
,
488 const struct conf
attribute((unused
)) *whoami
) {
491 static void free_string(struct config
*c
,
492 const struct conf
*whoami
) {
493 xfree(VALUE(c
, char *));
496 static void free_stringlist(struct config
*c
,
497 const struct conf
*whoami
) {
499 struct stringlist
*sl
= ADDRESS(c
, struct stringlist
);
501 for(n
= 0; n
< sl
->n
; ++n
)
506 static void free_stringlistlist(struct config
*c
,
507 const struct conf
*whoami
) {
509 struct stringlistlist
*sll
= ADDRESS(c
, struct stringlistlist
);
510 struct stringlist
*sl
;
512 for(n
= 0; n
< sll
->n
; ++n
) {
514 for(m
= 0; m
< sl
->n
; ++m
)
521 static void free_collectionlist(struct config
*c
,
522 const struct conf
*whoami
) {
523 struct collectionlist
*cll
= ADDRESS(c
, struct collectionlist
);
524 struct collection
*cl
;
527 for(n
= 0; n
< cll
->n
; ++n
) {
536 static void free_namepartlist(struct config
*c
,
537 const struct conf
*whoami
) {
538 struct namepartlist
*npl
= ADDRESS(c
, struct namepartlist
);
542 for(n
= 0; n
< npl
->n
; ++n
) {
545 pcre_free(np
->re
); /* ...whatever pcre_free is set to. */
552 static void free_transformlist(struct config
*c
,
553 const struct conf
*whoami
) {
554 struct transformlist
*tl
= ADDRESS(c
, struct transformlist
);
558 for(n
= 0; n
< tl
->n
; ++n
) {
561 pcre_free(t
->re
); /* ...whatever pcre_free is set to. */
568 /* configuration types */
570 static const struct conftype
571 type_signal
= { set_signal
, free_none
},
572 type_collections
= { set_collections
, free_collectionlist
},
573 type_boolean
= { set_boolean
, free_none
},
574 type_string
= { set_string
, free_string
},
575 type_stringlist
= { set_stringlist
, free_stringlist
},
576 type_integer
= { set_integer
, free_none
},
577 type_stringlist_accum
= { set_stringlist_accum
, free_stringlistlist
},
578 type_string_accum
= { set_string_accum
, free_stringlist
},
579 type_sample_format
= { set_sample_format
, free_none
},
580 type_restrict
= { set_restrict
, free_none
},
581 type_namepart
= { set_namepart
, free_namepartlist
},
582 type_transform
= { set_transform
, free_transformlist
},
583 type_backend
= { set_backend
, free_none
};
585 /* specific validation routine */
587 #define VALIDATE_FILE(test, what) do { \
591 for(n = 0; n < nvec; ++n) { \
592 if(stat(vec[n], &sb) < 0) { \
593 error(errno, "%s:%d: %s", cs->path, cs->line, vec[n]); \
596 if(!test(sb.st_mode)) { \
597 error(0, "%s:%d: %s is not a %s", \
598 cs->path, cs->line, vec[n], what); \
604 static int validate_isabspath(const struct config_state
*cs
,
605 int nvec
, char **vec
) {
608 for(n
= 0; n
< nvec
; ++n
)
609 if(vec
[n
][0] != '/') {
610 error(errno
, "%s:%d: %s: not an absolute path",
611 cs
->path
, cs
->line
, vec
[n
]);
617 static int validate_isdir(const struct config_state
*cs
,
618 int nvec
, char **vec
) {
619 VALIDATE_FILE(S_ISDIR
, "directory");
623 static int validate_isreg(const struct config_state
*cs
,
624 int nvec
, char **vec
) {
625 VALIDATE_FILE(S_ISREG
, "regular file");
629 static int validate_ischr(const struct config_state
*cs
,
630 int nvec
, char **vec
) {
631 VALIDATE_FILE(S_ISCHR
, "character device");
635 static int validate_player(const struct config_state
*cs
,
637 char attribute((unused
)) **vec
) {
639 error(0, "%s:%d: should be at least 'player PATTERN MODULE'",
646 static int validate_tracklength(const struct config_state
*cs
,
648 char attribute((unused
)) **vec
) {
650 error(0, "%s:%d: should be at least 'tracklength PATTERN MODULE'",
657 static int validate_allow(const struct config_state
*cs
,
659 char attribute((unused
)) **vec
) {
661 error(0, "%s:%d: must be 'allow NAME PASS'", cs
->path
, cs
->line
);
667 static int validate_non_negative(const struct config_state
*cs
,
668 int nvec
, char **vec
) {
672 error(0, "%s:%d: missing argument", cs
->path
, cs
->line
);
676 error(0, "%s:%d: too many arguments", cs
->path
, cs
->line
);
679 if(xstrtol(&n
, vec
[0], 0, 0)) {
680 error(0, "%s:%d: %s", cs
->path
, cs
->line
, strerror(errno
));
684 error(0, "%s:%d: must not be negative", cs
->path
, cs
->line
);
690 static int validate_positive(const struct config_state
*cs
,
691 int nvec
, char **vec
) {
695 error(0, "%s:%d: missing argument", cs
->path
, cs
->line
);
699 error(0, "%s:%d: too many arguments", cs
->path
, cs
->line
);
702 if(xstrtol(&n
, vec
[0], 0, 0)) {
703 error(0, "%s:%d: %s", cs
->path
, cs
->line
, strerror(errno
));
707 error(0, "%s:%d: must be positive", cs
->path
, cs
->line
);
713 static int validate_isauser(const struct config_state
*cs
,
714 int attribute((unused
)) nvec
,
718 if(!(pw
= getpwnam(vec
[0]))) {
719 error(0, "%s:%d: no such user as '%s'", cs
->path
, cs
->line
, vec
[0]);
725 static int validate_sample_format(const struct config_state
*cs
,
726 int attribute((unused
)) nvec
,
728 return parse_sample_format(cs
, 0, nvec
, vec
);
731 static int validate_channel(const struct config_state
*cs
,
732 int attribute((unused
)) nvec
,
734 if(mixer_channel(vec
[0]) == -1) {
735 error(0, "%s:%d: invalid channel '%s'", cs
->path
, cs
->line
, vec
[0]);
741 static int validate_any(const struct config_state
attribute((unused
)) *cs
,
742 int attribute((unused
)) nvec
,
743 char attribute((unused
)) **vec
) {
747 static int validate_url(const struct config_state
attribute((unused
)) *cs
,
748 int attribute((unused
)) nvec
,
752 /* absoluteURI = scheme ":" ( hier_part | opaque_part )
753 scheme = alpha *( alpha | digit | "+" | "-" | "." ) */
755 n
= strspn(s
, ("abcdefghijklmnopqrstuvwxyz"
756 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
759 error(0, "%s:%d: invalid url '%s'", cs
->path
, cs
->line
, vec
[0]);
762 if(!strncmp(s
, "http:", 5)
763 || !strncmp(s
, "https:", 6)) {
765 /* we only do a rather cursory check */
766 if(strncmp(s
, "//", 2)) {
767 error(0, "%s:%d: invalid url '%s'", cs
->path
, cs
->line
, vec
[0]);
774 static int validate_alias(const struct config_state
*cs
,
778 int in_brackets
= 0, c
;
781 error(0, "%s:%d: missing argument", cs
->path
, cs
->line
);
785 error(0, "%s:%d: too many arguments", cs
->path
, cs
->line
);
789 while((c
= (unsigned char)*s
++)) {
793 else if(!isalnum(c
)) {
794 error(0, "%s:%d: invalid part name in alias expansion in '%s'",
795 cs
->path
, cs
->line
, vec
[0]);
803 } else if(c
== '\\') {
804 if(!(c
= (unsigned char)*s
++)) {
805 error(0, "%s:%d: unterminated escape in alias expansion in '%s'",
806 cs
->path
, cs
->line
, vec
[0]);
808 } else if(c
!= '\\' && c
!= '{') {
809 error(0, "%s:%d: invalid escape in alias expansion in '%s'",
810 cs
->path
, cs
->line
, vec
[0]);
818 error(0, "%s:%d: unterminated part name in alias expansion in '%s'",
819 cs
->path
, cs
->line
, vec
[0]);
825 static int validate_addrport(const struct config_state
attribute((unused
)) *cs
,
827 char attribute((unused
)) **vec
) {
830 error(0, "%s:%d: missing address",
834 error(0, "%s:%d: missing port name/number",
840 error(0, "%s:%d: expected ADDRESS PORT",
846 static int validate_port(const struct config_state
attribute((unused
)) *cs
,
848 char attribute((unused
)) **vec
) {
851 error(0, "%s:%d: missing address",
858 error(0, "%s:%d: expected [ADDRESS] PORT",
864 static int validate_algo(const struct config_state
attribute((unused
)) *cs
,
868 error(0, "%s:%d: invalid algorithm specification", cs
->path
, cs
->line
);
871 if(!valid_authhash(vec
[0])) {
872 error(0, "%s:%d: unsuported algorithm '%s'", cs
->path
, cs
->line
, vec
[0]);
878 /** @brief Item name and and offset */
879 #define C(x) #x, offsetof(struct config, x)
880 /** @brief Item name and and offset */
881 #define C2(x,y) #x, offsetof(struct config, y)
883 /** @brief All configuration items */
884 static const struct conf conf
[] = {
885 { C(alias
), &type_string
, validate_alias
},
886 { C(allow
), &type_stringlist_accum
, validate_allow
},
887 { C(authorization_algorithm
), &type_string
, validate_algo
},
888 { C(broadcast
), &type_stringlist
, validate_addrport
},
889 { C(broadcast_from
), &type_stringlist
, validate_addrport
},
890 { C(channel
), &type_string
, validate_channel
},
891 { C(checkpoint_kbyte
), &type_integer
, validate_non_negative
},
892 { C(checkpoint_min
), &type_integer
, validate_non_negative
},
893 { C(collection
), &type_collections
, validate_any
},
894 { C(connect
), &type_stringlist
, validate_addrport
},
895 { C(dbversion
), &type_integer
, validate_positive
},
896 { C(device
), &type_string
, validate_any
},
897 { C(gap
), &type_integer
, validate_non_negative
},
898 { C(history
), &type_integer
, validate_positive
},
899 { C(home
), &type_string
, validate_isabspath
},
900 { C(listen
), &type_stringlist
, validate_port
},
901 { C(lock
), &type_boolean
, validate_any
},
902 { C(mixer
), &type_string
, validate_ischr
},
903 { C(multicast_ttl
), &type_integer
, validate_non_negative
},
904 { C(namepart
), &type_namepart
, validate_any
},
905 { C2(nice
, nice_rescan
), &type_integer
, validate_non_negative
},
906 { C(nice_rescan
), &type_integer
, validate_non_negative
},
907 { C(nice_server
), &type_integer
, validate_any
},
908 { C(nice_speaker
), &type_integer
, validate_any
},
909 { C(noticed_history
), &type_integer
, validate_positive
},
910 { C(password
), &type_string
, validate_any
},
911 { C(player
), &type_stringlist_accum
, validate_player
},
912 { C(plugins
), &type_string_accum
, validate_isdir
},
913 { C(prefsync
), &type_integer
, validate_positive
},
914 { C(queue_pad
), &type_integer
, validate_positive
},
915 { C(refresh
), &type_integer
, validate_positive
},
916 { C2(restrict
, restrictions
), &type_restrict
, validate_any
},
917 { C(sample_format
), &type_sample_format
, validate_sample_format
},
918 { C(scratch
), &type_string_accum
, validate_isreg
},
919 { C(short_display
), &type_integer
, validate_positive
},
920 { C(signal
), &type_signal
, validate_any
},
921 { C(sox_generation
), &type_integer
, validate_non_negative
},
922 { C(speaker_backend
), &type_backend
, validate_any
},
923 { C(speaker_command
), &type_string
, validate_any
},
924 { C(stopword
), &type_string_accum
, validate_any
},
925 { C(templates
), &type_string_accum
, validate_isdir
},
926 { C(tracklength
), &type_stringlist_accum
, validate_tracklength
},
927 { C(transform
), &type_transform
, validate_any
},
928 { C(trust
), &type_string_accum
, validate_any
},
929 { C(url
), &type_string
, validate_url
},
930 { C(user
), &type_string
, validate_isauser
},
931 { C(username
), &type_string
, validate_any
},
934 /** @brief Find a configuration item's definition by key */
935 static const struct conf
*find(const char *key
) {
938 if((n
= TABLE_FIND(conf
, struct conf
, name
, key
)) < 0)
943 /** @brief Set a new configuration value */
944 static int config_set(const struct config_state
*cs
,
945 int nvec
, char **vec
) {
946 const struct conf
*which
;
948 D(("config_set %s", vec
[0]));
949 if(!(which
= find(vec
[0]))) {
950 error(0, "%s:%d: unknown configuration key '%s'",
951 cs
->path
, cs
->line
, vec
[0]);
954 return (which
->validate(cs
, nvec
- 1, vec
+ 1)
955 || which
->type
->set(cs
, which
, nvec
- 1, vec
+ 1));
958 /** @brief Error callback used by config_include() */
959 static void config_error(const char *msg
, void *u
) {
960 const struct config_state
*cs
= u
;
962 error(0, "%s:%d: %s", cs
->path
, cs
->line
, msg
);
965 /** @brief Include a file by name */
966 static int config_include(struct config
*c
, const char *path
) {
968 char *buffer
, *inputbuffer
, **vec
;
970 struct config_state cs
;
975 D(("%s: reading configuration", path
));
976 if(!(fp
= fopen(path
, "r"))) {
977 error(errno
, "error opening %s", path
);
980 while(!inputline(path
, fp
, &inputbuffer
, '\n')) {
982 if(!(buffer
= mb2utf8(inputbuffer
))) {
983 error(errno
, "%s:%d: cannot convert to UTF-8", cs
.path
, cs
.line
);
989 if(!(vec
= split(buffer
, &n
, SPLIT_COMMENTS
|SPLIT_QUOTES
,
990 config_error
, &cs
))) {
996 if(!strcmp(vec
[0], "include")) {
998 error(0, "%s:%d: must be 'include PATH'", cs
.path
, cs
.line
);
1001 config_include(c
, vec
[1]);
1003 ret
|= config_set(&cs
, n
, vec
);
1005 for(n
= 0; vec
[n
]; ++n
) xfree(vec
[n
]);
1010 error(errno
, "error reading %s", path
);
1017 /** @brief Make a new default configuration */
1018 static struct config
*config_default(void) {
1019 struct config
*c
= xmalloc(sizeof *c
);
1020 const char *logname
;
1023 /* Strings had better be xstrdup'd as they will get freed at some point. */
1026 c
->home
= xstrdup(pkgstatedir
);
1027 if(!(pw
= getpwuid(getuid())))
1028 fatal(0, "cannot determine our username");
1029 logname
= pw
->pw_name
;
1030 c
->username
= xstrdup(logname
);
1033 c
->signal
= SIGKILL
;
1034 c
->alias
= xstrdup("{/artist}{/album}{/title}{ext}");
1036 c
->device
= xstrdup("default");
1037 c
->nice_rescan
= 10;
1038 c
->speaker_command
= 0;
1039 c
->sample_format
.bits
= 16;
1040 c
->sample_format
.rate
= 44100;
1041 c
->sample_format
.channels
= 2;
1042 c
->sample_format
.endian
= ENDIAN_NATIVE
;
1044 c
->speaker_backend
= -1;
1045 c
->multicast_ttl
= 1;
1046 c
->authorization_algorithm
= xstrdup("sha1");
1047 c
->noticed_history
= 31;
1048 c
->short_display
= 32;
1049 c
->mixer
= xstrdup("/dev/mixer");
1050 c
->channel
= xstrdup("pcm");
1055 static char *get_file(struct config
*c
, const char *name
) {
1058 byte_xasprintf(&s
, "%s/%s", c
->home
, name
);
1062 /** @brief Set the default configuration file */
1063 static void set_configfile(void) {
1065 byte_xasprintf(&configfile
, "%s/config", pkgconfdir
);
1068 /** @brief Free a configuration object */
1069 static void config_free(struct config
*c
) {
1073 for(n
= 0; n
< (int)(sizeof conf
/ sizeof *conf
); ++n
)
1074 conf
[n
].type
->free(c
, &conf
[n
]);
1075 for(n
= 0; n
< c
->nparts
; ++n
)
1082 /** @brief Set post-parse defaults */
1083 static void config_postdefaults(struct config
*c
,
1085 struct config_state cs
;
1086 const struct conf
*whoami
;
1089 static const char *namepart
[][4] = {
1090 { "title", "/([0-9]+ *[-:] *)?([^/]+)\\.[a-zA-Z0-9]+$", "$2", "display" },
1091 { "title", "/([^/]+)\\.[a-zA-Z0-9]+$", "$1", "sort" },
1092 { "album", "/([^/]+)/[^/]+$", "$1", "*" },
1093 { "artist", "/([^/]+)/[^/]+/[^/]+$", "$1", "*" },
1094 { "ext", "(\\.[a-zA-Z0-9]+)$", "$1", "*" },
1096 #define NNAMEPART (int)(sizeof namepart / sizeof *namepart)
1098 static const char *transform
[][5] = {
1099 { "track", "^.*/([0-9]+ *[-:] *)?([^/]+)\\.[a-zA-Z0-9]+$", "$2", "display", "" },
1100 { "track", "^.*/([^/]+)\\.[a-zA-Z0-9]+$", "$1", "sort", "" },
1101 { "dir", "^.*/([^/]+)$", "$1", "*", "" },
1102 { "dir", "^(the) ([^/]*)", "$2, $1", "sort", "i", },
1103 { "dir", "[[:punct:]]", "", "sort", "g", }
1105 #define NTRANSFORM (int)(sizeof transform / sizeof *transform)
1107 cs
.path
= "<internal>";
1110 if(!c
->namepart
.n
) {
1111 whoami
= find("namepart");
1112 for(n
= 0; n
< NNAMEPART
; ++n
)
1113 set_namepart(&cs
, whoami
, 4, (char **)namepart
[n
]);
1115 if(!c
->transform
.n
) {
1116 whoami
= find("transform");
1117 for(n
= 0; n
< NTRANSFORM
; ++n
)
1118 set_transform(&cs
, whoami
, 5, (char **)transform
[n
]);
1120 if(c
->speaker_backend
== -1) {
1121 if(c
->speaker_command
)
1122 c
->speaker_backend
= BACKEND_COMMAND
;
1123 else if(c
->broadcast
.n
)
1124 c
->speaker_backend
= BACKEND_NETWORK
;
1126 #if HAVE_ALSA_ASOUNDLIB_H
1127 c
->speaker_backend
= BACKEND_ALSA
;
1128 #elif HAVE_SYS_SOUNDCARD_H
1129 c
->speaker_backend
= BACKEND_OSS
;
1130 #elif HAVE_COREAUDIO_AUDIOHARDWARE_H
1131 c
->speaker_backend
= BACKEND_COREAUDIO
;
1133 c
->speaker_backend
= BACKEND_COMMAND
;
1138 if(c
->speaker_backend
== BACKEND_COMMAND
&& !c
->speaker_command
)
1139 fatal(0, "speaker_backend is command but speaker_command is not set");
1140 if(c
->speaker_backend
== BACKEND_NETWORK
&& !c
->broadcast
.n
)
1141 fatal(0, "speaker_backend is network but broadcast is not set");
1143 /* Override sample format */
1144 switch(c
->speaker_backend
) {
1145 case BACKEND_NETWORK
:
1146 c
->sample_format
.rate
= 44100;
1147 c
->sample_format
.channels
= 2;
1148 c
->sample_format
.bits
= 16;
1149 c
->sample_format
.endian
= ENDIAN_BIG
;
1151 case BACKEND_COREAUDIO
:
1152 c
->sample_format
.rate
= 44100;
1153 c
->sample_format
.channels
= 2;
1154 c
->sample_format
.bits
= 16;
1155 c
->sample_format
.endian
= ENDIAN_NATIVE
;
1160 /** @brief (Re-)read the config file
1161 * @param server If set, do extra checking
1163 int config_read(int server
) {
1169 c
= config_default();
1170 /* standalone Disobedience installs might not have a global config file */
1171 if(access(configfile
, F_OK
) == 0)
1172 if(config_include(c
, configfile
))
1174 /* if we can read the private config file, do */
1175 if((privconf
= config_private())
1176 && access(privconf
, R_OK
) == 0
1177 && config_include(c
, privconf
))
1180 /* if there's a per-user system config file for this user, read it */
1181 if(!(pw
= getpwuid(getuid())))
1182 fatal(0, "cannot determine our username");
1183 if((privconf
= config_usersysconf(pw
))
1184 && access(privconf
, F_OK
) == 0
1185 && config_include(c
, privconf
))
1188 /* if we have a password file, read it */
1189 if((privconf
= config_userconf(getenv("HOME"), pw
))
1190 && access(privconf
, F_OK
) == 0
1191 && config_include(c
, privconf
))
1194 /* install default namepart and transform settings */
1195 config_postdefaults(c
, server
);
1196 /* everything is good so we shall use the new config */
1197 config_free(config
);
1202 /** @brief Return the path to the private configuration file */
1203 char *config_private(void) {
1207 byte_xasprintf(&s
, "%s.private", configfile
);
1211 /** @brief Return the path to user's personal configuration file */
1212 char *config_userconf(const char *home
, const struct passwd
*pw
) {
1215 if(!home
&& !pw
&& !(pw
= getpwuid(getuid())))
1216 fatal(0, "cannot determine our username");
1217 byte_xasprintf(&s
, "%s/.disorder/passwd", home ? home
: pw
->pw_dir
);
1221 /** @brief Return the path to user-specific system configuration */
1222 char *config_usersysconf(const struct passwd
*pw
) {
1226 if(!strchr(pw
->pw_name
, '/')) {
1227 byte_xasprintf(&s
, "%s.%s", configfile
, pw
->pw_name
);
1233 char *config_get_file(const char *name
) {
1234 return get_file(config
, name
);