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.
15 #include <sys/types.h>
22 /* returns NULL on error, where to continue parsing if token and ok. E.g. "" will return token with empty string */
23 char *strtokenquote(char *s, char **token, char *del, char *quote, char *comment) {
26 if (!t || !token || !del)
28 while (*t && strchr(del, *t))
30 if (!*t || (comment && strchr(comment, *t))) {
32 return t + 1; /* needs to be non-NULL, but value doesn't matter */
34 if (quote && (q = strchr(quote, *t))) {
37 while (*t && *t != *q)
39 if (!*t || (t[1] && !strchr(del, t[1])))
47 while (*t && !strchr(del, *t))
53 int pushgconfdata(struct gconffile **cf, const char *data) {
55 struct gconffile *newcf;
58 newcf = malloc(sizeof(struct gconffile) * 2);
61 memset(newcf, 0, sizeof(struct gconffile) * 2);
63 for (i = 0; (*cf)[i].data || (*cf)[i].path; i++);
64 newcf = realloc(*cf, sizeof(struct gconffile) * (i + 2));
67 memmove(newcf + 1, newcf, sizeof(struct gconffile) * (i + 1));
68 memset(newcf, 0, sizeof(struct gconffile));
75 FILE *pushgconffile(struct gconffile **cf, FILE *file, const char *description) {
77 struct gconffile *newcf;
81 debug(DBG_INFO, "could not read config from %s", description);
84 debug(DBG_DBG, "reading config from %s", description);
86 desc = stringcopy(description, 0);
91 newcf = malloc(sizeof(struct gconffile) * 2);
94 memset(newcf, 0, sizeof(struct gconffile) * 2);
96 for (i = 0; (*cf)[i].data || (*cf)[i].path; i++);
97 newcf = realloc(*cf, sizeof(struct gconffile) * (i + 2));
100 memmove(newcf + 1, newcf, sizeof(struct gconffile) * (i + 1));
101 memset(newcf, 0, sizeof(struct gconffile));
103 newcf[0].file = file;
104 newcf[0].path = desc;
111 debug(DBG_ERR, "malloc failed");
115 FILE *pushgconfpath(struct gconffile **cf, const char *path) {
118 f = fopen(path, "r");
119 return pushgconffile(cf, f, path);
122 FILE *pushgconfpaths(struct gconffile **cf, const char *cfgpath) {
126 char *path, *curfile = NULL, *dir;
128 /* if cfgpath is relative, make it relative to current config */
130 path = (char *)cfgpath;
132 /* dirname may modify its argument */
133 curfile = stringcopy((*cf)->path, 0);
135 debug(DBG_ERR, "malloc failed");
138 dir = dirname(curfile);
139 path = malloc(strlen(dir) + strlen(cfgpath) + 2);
141 debug(DBG_ERR, "malloc failed");
145 path[strlen(dir)] = '/';
146 strcpy(path + strlen(dir) + 1, cfgpath);
148 memset(&globbuf, 0, sizeof(glob_t));
149 if (glob(path, 0, NULL, &globbuf)) {
150 debug(DBG_INFO, "could not glob %s", path);
154 for (i = globbuf.gl_pathc - 1; i >= 0; i--) {
155 f = pushgconfpath(cf, globbuf.gl_pathv[i]);
169 int popgconf(struct gconffile **cf) {
174 for (i = 0; (*cf)[i].data || (*cf)[i].path; i++);
175 if (i && (*cf)[0].file) {
176 fclose((*cf)[0].file);
178 debug(DBG_DBG, "closing config file %s", (*cf)[0].path);
187 memmove(*cf, *cf + 1, sizeof(struct gconffile) * i);
191 void freegconf(struct gconffile **cf) {
197 for (i = 0; (*cf)[i].data || (*cf)[i].path; i++) {
199 fclose((*cf)[i].file);
201 debug(DBG_DBG, "closing config file %s", (*cf)[i].path);
210 struct gconffile *openconfigfile(const char *file) {
211 struct gconffile *cf = NULL;
213 if (!pushgconfpath(&cf, file)) {
214 debug(DBG_ERR, "could not read config file %s\n%s", file, strerror(errno));
217 debug(DBG_DBG, "reading config file %s", file);
221 /* Parses config with following syntax:
224 * option-name = value
226 * option-name value {
227 * option-name [=] value
232 int getlinefromcf(struct gconffile *cf, char *line, const size_t size) {
239 return fgets(line, size, cf->file) ? 1 : 0;
244 for (i = pos; cf->data[i] && cf->data[i] != '\n'; i++);
245 if (cf->data[i] == '\n')
247 if (i - pos > size - 1)
249 memcpy(line, cf->data + pos, i - pos);
250 line[i - pos] = '\0';
257 int getconfigline(struct gconffile **cf, char *block, char **opt, char **val, int *conftype) {
266 if (!cf || !*cf || (!(*cf)->file && !(*cf)->data))
270 if (!getlinefromcf(*cf, line, 1024)) {
276 for (tcount = 0; tcount < 3; tcount++) {
277 s = strtokenquote(s, &tokens[tcount], " \t\r\n", "\"'", tcount ? NULL : "#");
279 debug(DBG_ERR, "Syntax error in line starting with: %s", line);
285 if (!tcount || **tokens == '#')
288 if (**tokens == '}') {
291 debug(DBG_ERR, "configuration error, found } with no matching {");
299 *opt = stringcopy(tokens[0], 0);
302 *val = stringcopy(tokens[1], 0);
305 *conftype = CONF_STR;
308 if (tokens[1][0] == '=' && tokens[1][1] == '\0') {
309 *opt = stringcopy(tokens[0], 0);
312 *val = stringcopy(tokens[2], 0);
315 *conftype = CONF_STR;
318 if (tokens[2][0] == '{' && tokens[2][1] == '\0') {
319 *opt = stringcopy(tokens[0], 0);
322 *val = stringcopy(tokens[1], 0);
325 *conftype = CONF_CBK;
331 debug(DBG_ERR, "configuration error in block %s, line starting with %s", block, tokens[0]);
333 debug(DBG_ERR, "configuration error, syntax error in line starting with %s", tokens[0]);
340 debug(DBG_ERR, "configuration error, option %s needs a non-empty value", *opt);
344 debug(DBG_ERR, "malloc failed");
353 /* returns 1 if ok, 0 on error */
354 /* caller must free returned values also on error */
355 int getgenericconfig(struct gconffile **cf, char *block, ...) {
357 char *opt = NULL, *val, *word, *optval, **str = NULL, ***mstr = NULL, **newmstr, *endptr;
359 long int *lint = NULL;
360 int type = 0, conftype = 0, n;
361 int (*cbk)(struct gconffile **, void *, char *, char *, char *) = NULL;
366 if (!getconfigline(cf, block, &opt, &val, &conftype))
371 if (conftype == CONF_STR && !strcasecmp(opt, "include")) {
372 if (!pushgconfpaths(cf, val)) {
373 debug(DBG_ERR, "failed to include config file %s", val);
381 while ((word = va_arg(ap, char *))) {
382 type = va_arg(ap, int);
385 str = va_arg(ap, char **);
390 mstr = va_arg(ap, char ***);
395 bln = va_arg(ap, uint8_t *);
400 lint = va_arg(ap, long int *);
405 cbk = va_arg(ap, int (*)(struct gconffile **, void *, char *, char *, char *));
408 cbkarg = va_arg(ap, void *);
413 if (!strcasecmp(opt, word))
420 debug(DBG_ERR, "configuration error in block %s, unknown option %s", block, opt);
421 debug(DBG_ERR, "configuration error, unknown option %s", opt);
425 if (((type == CONF_STR || type == CONF_MSTR || type == CONF_BLN || type == CONF_LINT) && conftype != CONF_STR) ||
426 (type == CONF_CBK && conftype != CONF_CBK)) {
428 debug(DBG_ERR, "configuration error in block %s, wrong syntax for option %s", block, opt);
429 debug(DBG_ERR, "configuration error, wrong syntax for option %s", opt);
436 debug(DBG_ERR, "configuration error, option %s already set to %s", opt, *str);
443 for (n = 0; (*mstr)[n]; n++);
446 newmstr = realloc(*mstr, sizeof(char *) * (n + 2));
448 debug(DBG_ERR, "malloc failed");
452 newmstr[n + 1] = NULL;
456 if (!strcasecmp(val, "on"))
458 else if (!strcasecmp(val, "off"))
462 debug(DBG_ERR, "configuration error in block %s, value for option %s must be on or off, not %s", block, opt, val);
464 debug(DBG_ERR, "configuration error, value for option %s must be on or off, not %s", opt, val);
470 *lint = strtol(val, &endptr, 0);
471 if (*lint == LONG_MIN || *lint == LONG_MAX || !endptr || endptr == val || *endptr != '\0') {
473 debug(DBG_ERR, "configuration error in block %s, value for option %s must be an integer, not %s", block, opt, val);
475 debug(DBG_ERR, "configuration error, value for option %s must be an integer, not %s", opt, val);
480 optval = malloc(strlen(opt) + strlen(val) + 2);
482 debug(DBG_ERR, "malloc failed");
485 sprintf(optval, "%s %s", opt, val);
486 if (!cbk(cf, cbkarg, optval, opt, val)) {
497 debug(DBG_DBG, "getgenericconfig: block %s: %s = %s", block, opt, val);
499 debug(DBG_DBG, "getgenericconfig: %s = %s", opt, val);
500 if (type == CONF_BLN || type == CONF_LINT)
505 debug(DBG_ERR, "getgenericconfig: internal parameter error");