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>
23 /* returns NULL on error, where to continue parsing if token and ok. E.g. "" will return token with empty string */
24 char *strtokenquote(char *s, char **token, char *del, char *quote, char *comment) {
27 if (!t || !token || !del)
29 while (*t && strchr(del, *t))
31 if (!*t || (comment && strchr(comment, *t))) {
33 return t + 1; /* needs to be non-NULL, but value doesn't matter */
35 if (quote && (q = strchr(quote, *t))) {
38 while (*t && *t != *q)
40 if (!*t || (t[1] && !strchr(del, t[1])))
48 while (*t && !strchr(del, *t))
54 int pushgconfdata(struct gconffile **cf, const char *data) {
56 struct gconffile *newcf;
59 newcf = malloc(sizeof(struct gconffile) * 2);
62 memset(newcf, 0, sizeof(struct gconffile) * 2);
64 for (i = 0; (*cf)[i].data || (*cf)[i].path; i++);
65 newcf = realloc(*cf, sizeof(struct gconffile) * (i + 2));
68 memmove(newcf + 1, newcf, sizeof(struct gconffile) * (i + 1));
69 memset(newcf, 0, sizeof(struct gconffile));
76 FILE *pushgconffile(struct gconffile **cf, FILE *file, const char *description) {
78 struct gconffile *newcf;
82 debug(DBG_INFO, "could not read config from %s", description);
85 debug(DBG_DBG, "reading config from %s", description);
87 desc = stringcopy(description, 0);
92 newcf = malloc(sizeof(struct gconffile) * 2);
95 memset(newcf, 0, sizeof(struct gconffile) * 2);
97 for (i = 0; (*cf)[i].data || (*cf)[i].path; i++);
98 newcf = realloc(*cf, sizeof(struct gconffile) * (i + 2));
101 memmove(newcf + 1, newcf, sizeof(struct gconffile) * (i + 1));
102 memset(newcf, 0, sizeof(struct gconffile));
104 newcf[0].file = file;
105 newcf[0].path = desc;
112 debug(DBG_ERR, "malloc failed");
116 FILE *pushgconfpath(struct gconffile **cf, const char *path) {
119 f = fopen(path, "r");
120 return pushgconffile(cf, f, path);
123 FILE *pushgconfpaths(struct gconffile **cf, const char *cfgpath) {
127 char *path, *curfile = NULL, *dir;
129 /* if cfgpath is relative, make it relative to current config */
131 path = (char *)cfgpath;
133 /* dirname may modify its argument */
134 curfile = stringcopy((*cf)->path, 0);
136 debug(DBG_ERR, "malloc failed");
139 dir = dirname(curfile);
140 path = malloc(strlen(dir) + strlen(cfgpath) + 2);
142 debug(DBG_ERR, "malloc failed");
146 path[strlen(dir)] = '/';
147 strcpy(path + strlen(dir) + 1, cfgpath);
149 memset(&globbuf, 0, sizeof(glob_t));
150 if (glob(path, 0, NULL, &globbuf)) {
151 debug(DBG_INFO, "could not glob %s", path);
155 for (i = globbuf.gl_pathc - 1; i >= 0; i--) {
156 f = pushgconfpath(cf, globbuf.gl_pathv[i]);
170 int popgconf(struct gconffile **cf) {
175 for (i = 0; (*cf)[i].data || (*cf)[i].path; i++);
176 if (i && (*cf)[0].file) {
177 fclose((*cf)[0].file);
179 debug(DBG_DBG, "closing config file %s", (*cf)[0].path);
188 memmove(*cf, *cf + 1, sizeof(struct gconffile) * i);
192 void freegconf(struct gconffile **cf) {
198 for (i = 0; (*cf)[i].data || (*cf)[i].path; i++) {
200 fclose((*cf)[i].file);
202 debug(DBG_DBG, "closing config file %s", (*cf)[i].path);
211 struct gconffile *openconfigfile(const char *file) {
212 struct gconffile *cf = NULL;
214 if (!pushgconfpath(&cf, file)) {
215 debug(DBG_ERR, "could not read config file %s\n%s", file, strerror(errno));
218 debug(DBG_DBG, "reading config file %s", file);
222 /* Parses config with following syntax:
225 * option-name = value
227 * option-name value {
228 * option-name [=] value
233 int getlinefromcf(struct gconffile *cf, char *line, const size_t size) {
240 return fgets(line, size, cf->file) ? 1 : 0;
245 for (i = pos; cf->data[i] && cf->data[i] != '\n'; i++);
246 if (cf->data[i] == '\n')
248 if (i - pos > size - 1)
250 memcpy(line, cf->data + pos, i - pos);
251 line[i - pos] = '\0';
258 int getconfigline(struct gconffile **cf, char *block, char **opt, char **val, int *conftype) {
267 if (!cf || !*cf || (!(*cf)->file && !(*cf)->data))
271 if (!getlinefromcf(*cf, line, 1024)) {
277 for (tcount = 0; tcount < 3; tcount++) {
278 s = strtokenquote(s, &tokens[tcount], " \t\r\n", "\"'", tcount ? NULL : "#");
280 debug(DBG_ERR, "Syntax error in line starting with: %s", line);
286 if (!tcount || **tokens == '#')
289 if (**tokens == '}') {
292 debug(DBG_ERR, "configuration error, found } with no matching {");
300 *opt = stringcopy(tokens[0], 0);
303 *val = stringcopy(tokens[1], 0);
306 *conftype = CONF_STR;
309 if (tokens[1][0] == '=' && tokens[1][1] == '\0') {
310 *opt = stringcopy(tokens[0], 0);
313 *val = stringcopy(tokens[2], 0);
316 *conftype = CONF_STR;
319 if (tokens[2][0] == '{' && tokens[2][1] == '\0') {
320 *opt = stringcopy(tokens[0], 0);
323 *val = stringcopy(tokens[1], 0);
326 *conftype = CONF_CBK;
332 debug(DBG_ERR, "configuration error in block %s, line starting with %s", block, tokens[0]);
334 debug(DBG_ERR, "configuration error, syntax error in line starting with %s", tokens[0]);
341 debug(DBG_ERR, "configuration error, option %s needs a non-empty value", *opt);
345 debug(DBG_ERR, "malloc failed");
354 uint8_t hexdigit2int(char d) {
355 if (d >= '0' && d <= '9')
357 if (d >= 'a' && d <= 'f')
359 if (d >= 'A' && d <= 'F')
364 void unhex(char *s) {
366 for (t = s; *t; s++) {
367 if (*t == '%' && isxdigit(t[1]) && isxdigit(t[2])) {
368 *s = 16 * hexdigit2int(t[1]) + hexdigit2int(t[2]);
376 /* returns 1 if ok, 0 on error */
377 /* caller must free returned values also on error */
378 int getgenericconfig(struct gconffile **cf, char *block, ...) {
380 char *opt = NULL, *val, *word, *optval, **str = NULL, ***mstr = NULL, **newmstr, *endptr;
382 long int *lint = NULL;
383 int type = 0, conftype = 0, n;
384 int (*cbk)(struct gconffile **, void *, char *, char *, char *) = NULL;
389 if (!getconfigline(cf, block, &opt, &val, &conftype))
394 if (conftype == CONF_STR && !strcasecmp(opt, "include")) {
395 if (!pushgconfpaths(cf, val)) {
396 debug(DBG_ERR, "failed to include config file %s", val);
404 while ((word = va_arg(ap, char *))) {
405 type = va_arg(ap, int);
408 str = va_arg(ap, char **);
413 mstr = va_arg(ap, char ***);
418 bln = va_arg(ap, uint8_t *);
423 lint = va_arg(ap, long int *);
428 cbk = va_arg(ap, int (*)(struct gconffile **, void *, char *, char *, char *));
431 cbkarg = va_arg(ap, void *);
436 if (!strcasecmp(opt, word))
443 debug(DBG_ERR, "configuration error in block %s, unknown option %s", block, opt);
444 debug(DBG_ERR, "configuration error, unknown option %s", opt);
448 if (((type == CONF_STR || type == CONF_MSTR || type == CONF_BLN || type == CONF_LINT) && conftype != CONF_STR) ||
449 (type == CONF_CBK && conftype != CONF_CBK)) {
451 debug(DBG_ERR, "configuration error in block %s, wrong syntax for option %s", block, opt);
452 debug(DBG_ERR, "configuration error, wrong syntax for option %s", opt);
459 debug(DBG_ERR, "configuration error, option %s already set to %s", opt, *str);
467 for (n = 0; (*mstr)[n]; n++);
470 newmstr = realloc(*mstr, sizeof(char *) * (n + 2));
472 debug(DBG_ERR, "malloc failed");
477 newmstr[n + 1] = NULL;
481 if (!strcasecmp(val, "on"))
483 else if (!strcasecmp(val, "off"))
487 debug(DBG_ERR, "configuration error in block %s, value for option %s must be on or off, not %s", block, opt, val);
489 debug(DBG_ERR, "configuration error, value for option %s must be on or off, not %s", opt, val);
495 *lint = strtol(val, &endptr, 0);
496 if (*lint == LONG_MIN || *lint == LONG_MAX || !endptr || endptr == val || *endptr != '\0') {
498 debug(DBG_ERR, "configuration error in block %s, value for option %s must be an integer, not %s", block, opt, val);
500 debug(DBG_ERR, "configuration error, value for option %s must be an integer, not %s", opt, val);
505 optval = malloc(strlen(opt) + strlen(val) + 2);
507 debug(DBG_ERR, "malloc failed");
510 sprintf(optval, "%s %s", opt, val);
511 if (!cbk(cf, cbkarg, optval, opt, val)) {
522 debug(DBG_DBG, "getgenericconfig: block %s: %s = %s", block, opt, val);
524 debug(DBG_DBG, "getgenericconfig: %s = %s", opt, val);
525 if (type == CONF_BLN || type == CONF_LINT)
530 debug(DBG_ERR, "getgenericconfig: internal parameter error");