845c32a0c7266662a62ba52a290465dfd6671520
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 parse_sample_format(const struct config_state
*cs
,
259 ao_sample_format
*ao
,
260 int nvec
, char **vec
) {
265 error(0, "%s:%d: wrong number of arguments", cs
->path
, cs
->line
);
268 if (xstrtol(&t
, p
, &p
, 0)) {
269 error(errno
, "%s:%d: converting bits-per-sample", cs
->path
, cs
->line
);
272 if (t
!= 8 && t
!= 16) {
273 error(0, "%s:%d: bad bite-per-sample (%ld)", cs
->path
, cs
->line
, t
);
276 if (ao
) ao
->bits
= t
;
278 case 'l': case 'L': t
= AO_FMT_LITTLE
; p
++; break;
279 case 'b': case 'B': t
= AO_FMT_BIG
; p
++; break;
280 default: t
= AO_FMT_NATIVE
; break;
282 if (ao
) ao
->byte_format
= t
;
284 error(errno
, "%s:%d: expected `/' after bits-per-sample",
289 if (xstrtol(&t
, p
, &p
, 0)) {
290 error(errno
, "%s:%d: converting sample-rate", cs
->path
, cs
->line
);
293 if (t
< 1 || t
> INT_MAX
) {
294 error(0, "%s:%d: silly sample-rate (%ld)", cs
->path
, cs
->line
, t
);
297 if (ao
) ao
->rate
= t
;
299 error(0, "%s:%d: expected `/' after sample-rate",
304 if (xstrtol(&t
, p
, &p
, 0)) {
305 error(errno
, "%s:%d: converting channels", cs
->path
, cs
->line
);
308 if (t
< 1 || t
> 8) {
309 error(0, "%s:%d: silly number (%ld) of channels", cs
->path
, cs
->line
, t
);
312 if (ao
) ao
->channels
= t
;
314 error(0, "%s:%d: junk after channels", cs
->path
, cs
->line
);
320 static int set_sample_format(const struct config_state
*cs
,
321 const struct conf
*whoami
,
322 int nvec
, char **vec
) {
323 return parse_sample_format(cs
, ADDRESS(cs
->config
, ao_sample_format
),
327 static int set_namepart(const struct config_state
*cs
,
328 const struct conf
*whoami
,
329 int nvec
, char **vec
) {
330 struct namepartlist
*npl
= ADDRESS(cs
->config
, struct namepartlist
);
337 error(0, "%s:%d: namepart needs at least 3 arguments", cs
->path
, cs
->line
);
341 error(0, "%s:%d: namepart needs at most 5 arguments", cs
->path
, cs
->line
);
344 reflags
= nvec
>= 5 ?
regsub_flags(vec
[4]) : 0;
345 if(!(re
= pcre_compile(vec
[1],
347 |regsub_compile_options(reflags
),
348 &errstr
, &erroffset
, 0))) {
349 error(0, "%s:%d: error compiling regexp /%s/: %s (offset %d)",
350 cs
->path
, cs
->line
, vec
[1], errstr
, erroffset
);
353 npl
->s
= xrealloc(npl
->s
, (npl
->n
+ 1) * sizeof (struct namepart
));
354 npl
->s
[npl
->n
].part
= xstrdup(vec
[0]);
355 npl
->s
[npl
->n
].re
= re
;
356 npl
->s
[npl
->n
].replace
= xstrdup(vec
[2]);
357 npl
->s
[npl
->n
].context
= xstrdup(vec
[3]);
358 npl
->s
[npl
->n
].reflags
= reflags
;
360 /* XXX a bit of a bodge; relies on there being very few parts. */
361 for(n
= 0; (n
< cs
->config
->nparts
362 && strcmp(cs
->config
->parts
[n
], vec
[0])); ++n
)
364 if(n
>= cs
->config
->nparts
) {
365 cs
->config
->parts
= xrealloc(cs
->config
->parts
,
366 (cs
->config
->nparts
+ 1) * sizeof (char *));
367 cs
->config
->parts
[cs
->config
->nparts
++] = xstrdup(vec
[0]);
372 static int set_transform(const struct config_state
*cs
,
373 const struct conf
*whoami
,
374 int nvec
, char **vec
) {
375 struct transformlist
*tl
= ADDRESS(cs
->config
, struct transformlist
);
382 error(0, "%s:%d: transform needs at least 3 arguments", cs
->path
, cs
->line
);
386 error(0, "%s:%d: transform needs at most 5 arguments", cs
->path
, cs
->line
);
389 reflags
= (nvec
>= 5 ?
regsub_flags(vec
[4]) : 0);
390 if(!(re
= pcre_compile(vec
[1],
392 |regsub_compile_options(reflags
),
393 &errstr
, &erroffset
, 0))) {
394 error(0, "%s:%d: error compiling regexp /%s/: %s (offset %d)",
395 cs
->path
, cs
->line
, vec
[1], errstr
, erroffset
);
398 tl
->t
= xrealloc(tl
->t
, (tl
->n
+ 1) * sizeof (struct namepart
));
399 tl
->t
[tl
->n
].type
= xstrdup(vec
[0]);
400 tl
->t
[tl
->n
].context
= xstrdup(vec
[3] ? vec
[3] : "*");
401 tl
->t
[tl
->n
].re
= re
;
402 tl
->t
[tl
->n
].replace
= xstrdup(vec
[2]);
403 tl
->t
[tl
->n
].flags
= reflags
;
410 static void free_none(struct config
attribute((unused
)) *c
,
411 const struct conf
attribute((unused
)) *whoami
) {
414 static void free_string(struct config
*c
,
415 const struct conf
*whoami
) {
416 xfree(VALUE(c
, char *));
419 static void free_stringlist(struct config
*c
,
420 const struct conf
*whoami
) {
422 struct stringlist
*sl
= ADDRESS(c
, struct stringlist
);
424 for(n
= 0; n
< sl
->n
; ++n
)
429 static void free_stringlistlist(struct config
*c
,
430 const struct conf
*whoami
) {
432 struct stringlistlist
*sll
= ADDRESS(c
, struct stringlistlist
);
433 struct stringlist
*sl
;
435 for(n
= 0; n
< sll
->n
; ++n
) {
437 for(m
= 0; m
< sl
->n
; ++m
)
444 static void free_collectionlist(struct config
*c
,
445 const struct conf
*whoami
) {
446 struct collectionlist
*cll
= ADDRESS(c
, struct collectionlist
);
447 struct collection
*cl
;
450 for(n
= 0; n
< cll
->n
; ++n
) {
459 static void free_namepartlist(struct config
*c
,
460 const struct conf
*whoami
) {
461 struct namepartlist
*npl
= ADDRESS(c
, struct namepartlist
);
465 for(n
= 0; n
< npl
->n
; ++n
) {
468 pcre_free(np
->re
); /* ...whatever pcre_free is set to. */
475 static void free_transformlist(struct config
*c
,
476 const struct conf
*whoami
) {
477 struct transformlist
*tl
= ADDRESS(c
, struct transformlist
);
481 for(n
= 0; n
< tl
->n
; ++n
) {
484 pcre_free(t
->re
); /* ...whatever pcre_free is set to. */
491 /* configuration types */
493 static const struct conftype
494 type_signal
= { set_signal
, free_none
},
495 type_collections
= { set_collections
, free_collectionlist
},
496 type_boolean
= { set_boolean
, free_none
},
497 type_string
= { set_string
, free_string
},
498 type_stringlist
= { set_stringlist
, free_stringlist
},
499 type_integer
= { set_integer
, free_none
},
500 type_stringlist_accum
= { set_stringlist_accum
, free_stringlistlist
},
501 type_string_accum
= { set_string_accum
, free_stringlist
},
502 type_sample_format
= { set_sample_format
, free_none
},
503 type_restrict
= { set_restrict
, free_none
},
504 type_namepart
= { set_namepart
, free_namepartlist
},
505 type_transform
= { set_transform
, free_transformlist
};
507 /* specific validation routine */
509 #define VALIDATE_FILE(test, what) do { \
513 for(n = 0; n < nvec; ++n) { \
514 if(stat(vec[n], &sb) < 0) { \
515 error(errno, "%s:%d: %s", cs->path, cs->line, vec[n]); \
518 if(!test(sb.st_mode)) { \
519 error(0, "%s:%d: %s is not a %s", \
520 cs->path, cs->line, vec[n], what); \
526 static int validate_isdir(const struct config_state
*cs
,
527 int nvec
, char **vec
) {
528 VALIDATE_FILE(S_ISDIR
, "directory");
532 static int validate_isreg(const struct config_state
*cs
,
533 int nvec
, char **vec
) {
534 VALIDATE_FILE(S_ISREG
, "regular file");
538 static int validate_ischr(const struct config_state
*cs
,
539 int nvec
, char **vec
) {
540 VALIDATE_FILE(S_ISCHR
, "character device");
544 static int validate_player(const struct config_state
*cs
,
546 char attribute((unused
)) **vec
) {
548 error(0, "%s:%d: should be at least 'player PATTERN MODULE'",
555 static int validate_allow(const struct config_state
*cs
,
557 char attribute((unused
)) **vec
) {
559 error(0, "%s:%d: must be 'allow NAME PASS'", cs
->path
, cs
->line
);
565 static int validate_non_negative(const struct config_state
*cs
,
566 int nvec
, char **vec
) {
570 error(0, "%s:%d: missing argument", cs
->path
, cs
->line
);
574 error(0, "%s:%d: too many arguments", cs
->path
, cs
->line
);
577 if(xstrtol(&n
, vec
[0], 0, 0)) {
578 error(0, "%s:%d: %s", cs
->path
, cs
->line
, strerror(errno
));
582 error(0, "%s:%d: must not be negative", cs
->path
, cs
->line
);
588 static int validate_positive(const struct config_state
*cs
,
589 int nvec
, char **vec
) {
593 error(0, "%s:%d: missing argument", cs
->path
, cs
->line
);
597 error(0, "%s:%d: too many arguments", cs
->path
, cs
->line
);
600 if(xstrtol(&n
, vec
[0], 0, 0)) {
601 error(0, "%s:%d: %s", cs
->path
, cs
->line
, strerror(errno
));
605 error(0, "%s:%d: must be positive", cs
->path
, cs
->line
);
611 static int validate_isauser(const struct config_state
*cs
,
612 int attribute((unused
)) nvec
,
616 if(!(pw
= getpwnam(vec
[0]))) {
617 error(0, "%s:%d: no such user as '%s'", cs
->path
, cs
->line
, vec
[0]);
623 static int validate_sample_format(const struct config_state
*cs
,
624 int attribute((unused
)) nvec
,
626 return parse_sample_format(cs
, 0, nvec
, vec
);
629 static int validate_channel(const struct config_state
*cs
,
630 int attribute((unused
)) nvec
,
632 if(mixer_channel(vec
[0]) == -1) {
633 error(0, "%s:%d: invalid channel '%s'", cs
->path
, cs
->line
, vec
[0]);
639 static int validate_any(const struct config_state
attribute((unused
)) *cs
,
640 int attribute((unused
)) nvec
,
641 char attribute((unused
)) **vec
) {
645 static int validate_url(const struct config_state
attribute((unused
)) *cs
,
646 int attribute((unused
)) nvec
,
650 /* absoluteURI = scheme ":" ( hier_part | opaque_part )
651 scheme = alpha *( alpha | digit | "+" | "-" | "." ) */
653 n
= strspn(s
, ("abcdefghijklmnopqrstuvwxyz"
654 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
657 error(0, "%s:%d: invalid url '%s'", cs
->path
, cs
->line
, vec
[0]);
660 if(!strncmp(s
, "http:", 5)
661 || !strncmp(s
, "https:", 6)) {
663 /* we only do a rather cursory check */
664 if(strncmp(s
, "//", 2)) {
665 error(0, "%s:%d: invalid url '%s'", cs
->path
, cs
->line
, vec
[0]);
672 static int validate_alias(const struct config_state
*cs
,
676 int in_brackets
= 0, c
;
679 error(0, "%s:%d: missing argument", cs
->path
, cs
->line
);
683 error(0, "%s:%d: too many arguments", cs
->path
, cs
->line
);
687 while((c
= (unsigned char)*s
++)) {
691 else if(!isalnum(c
)) {
692 error(0, "%s:%d: invalid part name in alias expansion in '%s'",
693 cs
->path
, cs
->line
, vec
[0]);
701 } else if(c
== '\\') {
702 if(!(c
= (unsigned char)*s
++)) {
703 error(0, "%s:%d: unterminated escape in alias expansion in '%s'",
704 cs
->path
, cs
->line
, vec
[0]);
706 } else if(c
!= '\\' && c
!= '{') {
707 error(0, "%s:%d: invalid escape in alias expansion in '%s'",
708 cs
->path
, cs
->line
, vec
[0]);
716 error(0, "%s:%d: unterminated part name in alias expansion in '%s'",
717 cs
->path
, cs
->line
, vec
[0]);
723 /* configuration table */
725 #define C(x) #x, offsetof(struct config, x)
726 #define C2(x,y) #x, offsetof(struct config, y)
728 static const struct conf conf
[] = {
729 { C(alias
), &type_string
, validate_alias
},
730 { C(allow
), &type_stringlist_accum
, validate_allow
},
731 { C(channel
), &type_string
, validate_channel
},
732 { C(checkpoint_kbyte
), &type_integer
, validate_non_negative
},
733 { C(checkpoint_min
), &type_integer
, validate_non_negative
},
734 { C(collection
), &type_collections
, validate_any
},
735 { C(connect
), &type_stringlist
, validate_any
},
736 { C(device
), &type_string
, validate_any
},
737 { C(gap
), &type_integer
, validate_non_negative
},
738 { C(history
), &type_integer
, validate_positive
},
739 { C(home
), &type_string
, validate_isdir
},
740 { C(listen
), &type_stringlist
, validate_any
},
741 { C(lock
), &type_boolean
, validate_any
},
742 { C(mixer
), &type_string
, validate_ischr
},
743 { C(namepart
), &type_namepart
, validate_any
},
744 { C2(nice
, nice_rescan
), &type_integer
, validate_non_negative
},
745 { C(nice_rescan
), &type_integer
, validate_non_negative
},
746 { C(nice_server
), &type_integer
, validate_any
},
747 { C(nice_speaker
), &type_integer
, validate_any
},
748 { C(password
), &type_string
, validate_any
},
749 { C(player
), &type_stringlist_accum
, validate_player
},
750 { C(plugins
), &type_string_accum
, validate_isdir
},
751 { C(prefsync
), &type_integer
, validate_positive
},
752 { C(queue_pad
), &type_integer
, validate_positive
},
753 { C(refresh
), &type_integer
, validate_positive
},
754 { C2(restrict
, restrictions
), &type_restrict
, validate_any
},
755 { C(sample_format
), &type_sample_format
, validate_sample_format
},
756 { C(scratch
), &type_string_accum
, validate_isreg
},
757 { C(signal
), &type_signal
, validate_any
},
758 { C(sox_generation
), &type_integer
, validate_non_negative
},
759 { C(speaker_command
), &type_string
, validate_any
},
760 { C(stopword
), &type_string_accum
, validate_any
},
761 { C(templates
), &type_string_accum
, validate_isdir
},
762 { C(transform
), &type_transform
, validate_any
},
763 { C(trust
), &type_string_accum
, validate_any
},
764 { C(url
), &type_string
, validate_url
},
765 { C(user
), &type_string
, validate_isauser
},
766 { C(username
), &type_string
, validate_any
},
769 /* find a configuration item's definition by key */
770 static const struct conf
*find(const char *key
) {
773 if((n
= TABLE_FIND(conf
, struct conf
, name
, key
)) < 0)
778 /* set a new configuration value */
779 static int config_set(const struct config_state
*cs
,
780 int nvec
, char **vec
) {
781 const struct conf
*which
;
783 D(("config_set %s", vec
[0]));
784 if(!(which
= find(vec
[0]))) {
785 error(0, "%s:%d: unknown configuration key '%s'",
786 cs
->path
, cs
->line
, vec
[0]);
789 return (which
->validate(cs
, nvec
- 1, vec
+ 1)
790 || which
->type
->set(cs
, which
, nvec
- 1, vec
+ 1));
793 static void config_error(const char *msg
, void *u
) {
794 const struct config_state
*cs
= u
;
796 error(0, "%s:%d: %s", cs
->path
, cs
->line
, msg
);
799 /* include a file by name */
800 static int config_include(struct config
*c
, const char *path
) {
802 char *buffer
, *inputbuffer
, **vec
;
804 struct config_state cs
;
809 D(("%s: reading configuration", path
));
810 if(!(fp
= fopen(path
, "r"))) {
811 error(errno
, "error opening %s", path
);
814 while(!inputline(path
, fp
, &inputbuffer
, '\n')) {
816 if(!(buffer
= mb2utf8(inputbuffer
))) {
817 error(errno
, "%s:%d: cannot convert to UTF-8", cs
.path
, cs
.line
);
823 if(!(vec
= split(buffer
, &n
, SPLIT_COMMENTS
|SPLIT_QUOTES
,
824 config_error
, &cs
))) {
830 if(!strcmp(vec
[0], "include")) {
832 error(0, "%s:%d: must be 'include PATH'", cs
.path
, cs
.line
);
835 config_include(c
, vec
[1]);
837 ret
|= config_set(&cs
, n
, vec
);
839 for(n
= 0; vec
[n
]; ++n
) xfree(vec
[n
]);
844 error(errno
, "error reading %s", path
);
851 /* make a new default config */
852 static struct config
*config_default(void) {
853 struct config
*c
= xmalloc(sizeof *c
);
857 /* Strings had better be xstrdup'd as they will get freed at some point. */
860 c
->home
= xstrdup(pkgstatedir
);
861 if(!(pw
= getpwuid(getuid())))
862 fatal(0, "cannot determine our username");
863 logname
= pw
->pw_name
;
864 c
->username
= xstrdup(logname
);
868 c
->alias
= xstrdup("{/artist}{/album}{/title}{ext}");
870 c
->device
= xstrdup("default");
872 c
->speaker_command
= 0;
873 c
->sample_format
.bits
= 16;
874 c
->sample_format
.rate
= 44100;
875 c
->sample_format
.channels
= 2;
876 c
->sample_format
.byte_format
= AO_FMT_NATIVE
;
881 static char *get_file(struct config
*c
, const char *name
) {
884 byte_xasprintf(&s
, "%s/%s", c
->home
, name
);
888 static void set_configfile(void) {
890 byte_xasprintf(&configfile
, "%s/config", pkgconfdir
);
893 /* free the config file */
894 static void config_free(struct config
*c
) {
898 for(n
= 0; n
< (int)(sizeof conf
/ sizeof *conf
); ++n
)
899 conf
[n
].type
->free(c
, &conf
[n
]);
900 for(n
= 0; n
< c
->nparts
; ++n
)
907 static void config_postdefaults(struct config
*c
) {
908 struct config_state cs
;
909 const struct conf
*whoami
;
912 static const char *namepart
[][4] = {
913 { "title", "/([0-9]+:)?([^/]+)\\.[a-zA-Z0-9]+$", "$2", "display" },
914 { "title", "/([^/]+)\\.[a-zA-Z0-9]+$", "$1", "sort" },
915 { "album", "/([^/]+)/[^/]+$", "$1", "*" },
916 { "artist", "/([^/]+)/[^/]+/[^/]+$", "$1", "*" },
917 { "ext", "(\\.[a-zA-Z0-9]+)$", "$1", "*" },
919 #define NNAMEPART (int)(sizeof namepart / sizeof *namepart)
921 static const char *transform
[][5] = {
922 { "track", "^.*/([0-9]+:)?([^/]+)\\.[a-zA-Z0-9]+$", "$2", "display", "" },
923 { "track", "^.*/([^/]+)\\.[a-zA-Z0-9]+$", "$1", "sort", "" },
924 { "dir", "^.*/([^/]+)$", "$1", "*", "" },
925 { "dir", "^(the) ([^/]*)", "$2, $1", "sort", "i", },
926 { "dir", "[[:punct:]]", "", "sort", "g", }
928 #define NTRANSFORM (int)(sizeof transform / sizeof *transform)
930 cs
.path
= "<internal>";
934 whoami
= find("namepart");
935 for(n
= 0; n
< NNAMEPART
; ++n
)
936 set_namepart(&cs
, whoami
, 4, (char **)namepart
[n
]);
938 if(!c
->transform
.n
) {
939 whoami
= find("transform");
940 for(n
= 0; n
< NTRANSFORM
; ++n
)
941 set_transform(&cs
, whoami
, 5, (char **)transform
[n
]);
945 /* re-read the config file */
952 c
= config_default();
953 if(config_include(c
, configfile
))
955 /* if we can read the private config file, do */
956 if((privconf
= config_private())
957 && access(privconf
, R_OK
) == 0
958 && config_include(c
, privconf
))
961 /* if there's a per-user system config file for this user, read it */
962 if(!(pw
= getpwuid(getuid())))
963 fatal(0, "cannot determine our username");
964 if((privconf
= config_usersysconf(pw
))
965 && access(privconf
, F_OK
) == 0
966 && config_include(c
, privconf
))
969 /* if we have a password file, read it */
970 if((privconf
= config_userconf(getenv("HOME"), pw
))
971 && access(privconf
, F_OK
) == 0
972 && config_include(c
, privconf
))
975 /* install default namepart and transform settings */
976 config_postdefaults(c
);
977 /* everything is good so we shall use the new config */
983 char *config_private(void) {
987 byte_xasprintf(&s
, "%s.private", configfile
);
991 char *config_userconf(const char *home
, const struct passwd
*pw
) {
994 byte_xasprintf(&s
, "%s/.disorder/passwd", home ? home
: pw
->pw_dir
);
998 char *config_usersysconf(const struct passwd
*pw
) {
1002 if(!strchr(pw
->pw_name
, '/')) {
1003 byte_xasprintf(&s
, "%s.%s", configfile
, pw
->pw_name
);
1009 char *config_get_file(const char *name
) {
1010 return get_file(config
, name
);