2 * Copyright (C) 2007, 2008 Stig Venaas <venaas@uninett.no>
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
14 #include <sys/types.h>
21 /* returns NULL on error, where to continue parsing if token and ok. E.g. "" will return token with empty string */
22 char *strtokenquote(char *s, char **token, char *del, char *quote, char *comment) {
25 if (!t || !token || !del)
27 while (*t && strchr(del, *t))
29 if (!*t || (comment && strchr(comment, *t))) {
31 return t + 1; /* needs to be non-NULL, but value doesn't matter */
33 if (quote && (q = strchr(quote, *t))) {
36 while (*t && *t != *q)
38 if (!*t || (t[1] && !strchr(del, t[1])))
46 while (*t && !strchr(del, *t))
52 FILE *pushgconffile(struct gconffile **cf, const char *path) {
54 struct gconffile *newcf;
59 debug(DBG_INFO, "could not read config file %s", path);
62 debug(DBG_DBG, "opened config file %s", path);
64 newcf = malloc(sizeof(struct gconffile) * 2);
66 debugx(1, DBG_ERR, "malloc failed");
70 for (i = 0; (*cf)[i].path; i++);
71 newcf = realloc(*cf, sizeof(struct gconffile) * (i + 2));
73 debugx(1, DBG_ERR, "malloc failed");
74 memmove(newcf + 1, newcf, sizeof(struct gconffile) * (i + 1));
77 newcf[0].path = stringcopy(path, 0);
82 FILE *pushgconffiles(struct gconffile **cf, const char *cfgpath) {
86 char *path, *curfile = NULL, *dir;
88 /* if cfgpath is relative, make it relative to current config */
90 path = (char *)cfgpath;
92 /* dirname may modify its argument */
93 curfile = stringcopy((*cf)->path, 0);
95 debugx(1, DBG_ERR, "malloc failed");
96 dir = dirname(curfile);
97 path = malloc(strlen(dir) + strlen(cfgpath) + 2);
99 debugx(1, DBG_ERR, "malloc failed");
101 path[strlen(dir)] = '/';
102 strcpy(path + strlen(dir) + 1, cfgpath);
104 memset(&globbuf, 0, sizeof(glob_t));
105 if (glob(path, 0, NULL, &globbuf)) {
106 debug(DBG_INFO, "could not glob %s", path);
109 for (i = globbuf.gl_pathc - 1; i >= 0; i--) {
110 f = pushgconffile(cf, globbuf.gl_pathv[i]);
123 FILE *popgconffile(struct gconffile **cf) {
128 for (i = 0; (*cf)[i].path; i++);
129 if (i && (*cf)[0].file) {
130 fclose((*cf)[0].file);
131 debug(DBG_DBG, "closing config file %s", (*cf)[0].path);
138 memmove(*cf, *cf + 1, sizeof(struct gconffile) * i);
139 return (*cf)[0].file;
142 struct gconffile *openconfigfile(const char *file) {
143 struct gconffile *cf = NULL;
145 if (!pushgconffile(&cf, file))
146 debugx(1, DBG_ERR, "could not read config file %s\n%s", file, strerror(errno));
147 debug(DBG_DBG, "reading config file %s", file);
151 /* Parses config with following syntax:
154 * option-name = value
156 * option-name value {
157 * option-name [=] value
162 void getconfigline(struct gconffile **cf, char *block, char **opt, char **val, int *conftype) {
171 if (!cf || !*cf || !(*cf)->file)
175 if (!fgets(line, 1024, (*cf)->file)) {
176 if (popgconffile(cf))
181 for (tcount = 0; tcount < 3; tcount++) {
182 s = strtokenquote(s, &tokens[tcount], " \t\r\n", "\"'", tcount ? NULL : "#");
184 debugx(1, DBG_ERR, "Syntax error in line starting with: %s", line);
188 if (!tcount || **tokens == '#')
191 if (**tokens == '}') {
194 debugx(1, DBG_ERR, "configuration error, found } with no matching {");
203 *conftype = CONF_STR;
206 if (tokens[1][0] == '=' && tokens[1][1] == '\0') {
209 *conftype = CONF_STR;
212 if (tokens[2][0] == '{' && tokens[2][1] == '\0') {
215 *conftype = CONF_CBK;
221 debugx(1, DBG_ERR, "configuration error in block %s, line starting with %s", block, tokens[0]);
222 debugx(1, DBG_ERR, "configuration error, syntax error in line starting with %s", tokens[0]);
226 debugx(1, DBG_ERR, "configuration error, option %s needs a non-empty value", *opt);
230 void getgenericconfig(struct gconffile **cf, char *block, ...) {
232 char *opt, *val, *word, *optval, **str = NULL, ***mstr = NULL;
234 int type = 0, conftype = 0, n;
235 void (*cbk)(struct gconffile **, char *, char *, char *) = NULL;
238 getconfigline(cf, block, &opt, &val, &conftype);
242 if (conftype == CONF_STR && !strcasecmp(opt, "include")) {
243 if (!pushgconffiles(cf, val))
244 debugx(1, DBG_ERR, "failed to include config file %s", val);
249 while ((word = va_arg(ap, char *))) {
250 type = va_arg(ap, int);
253 str = va_arg(ap, char **);
255 debugx(1, DBG_ERR, "getgenericconfig: internal parameter error");
258 mstr = va_arg(ap, char ***);
260 debugx(1, DBG_ERR, "getgenericconfig: internal parameter error");
263 bln = va_arg(ap, uint8_t *);
265 debugx(1, DBG_ERR, "getgenericconfig: internal parameter error");
268 cbk = va_arg(ap, void (*)(struct gconffile **, char *, char *, char *));
271 debugx(1, DBG_ERR, "getgenericconfig: internal parameter error");
273 if (!strcasecmp(opt, word))
280 debugx(1, DBG_ERR, "configuration error in block %s, unknown option %s", block, opt);
281 debugx(1, DBG_ERR, "configuration error, unknown option %s", opt);
284 if (((type == CONF_STR || type == CONF_MSTR || type == CONF_BLN) && conftype != CONF_STR) ||
285 (type == CONF_CBK && conftype != CONF_CBK)) {
287 debugx(1, DBG_ERR, "configuration error in block %s, wrong syntax for option %s", block, opt);
288 debugx(1, DBG_ERR, "configuration error, wrong syntax for option %s", opt);
294 debugx(1, DBG_ERR, "configuration error, option %s already set to %s", opt, *str);
295 *str = stringcopy(val, 0);
297 debugx(1, DBG_ERR, "malloc failed");
301 for (n = 0; (*mstr)[n]; n++);
304 *mstr = realloc(*mstr, sizeof(char *) * (n + 2));
306 debugx(1, DBG_ERR, "malloc failed");
307 (*mstr)[n] = stringcopy(val, 0);
308 (*mstr)[n + 1] = NULL;
311 if (!strcasecmp(val, "on"))
313 else if (!strcasecmp(val, "off"))
316 debugx(1, DBG_ERR, "configuration error in block %s, value for option %s must be on or off, not %s", block, opt, val);
318 debugx(1, DBG_ERR, "configuration error, value for option %s must be on or off, not %s", opt, val);
321 optval = malloc(strlen(opt) + strlen(val) + 2);
323 debugx(1, DBG_ERR, "malloc failed");
324 sprintf(optval, "%s %s", opt, val);
325 cbk(cf, optval, opt, val);
329 debugx(1, DBG_ERR, "getgenericconfig: internal parameter error");
332 debug(DBG_DBG, "getgenericconfig: block %s: %s = %s", block, opt, val);
334 debug(DBG_DBG, "getgenericconfig: %s = %s", opt, val);