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 int pushgconfdata(struct gconffile **cf, const char *data) {
54 struct gconffile *newcf;
57 newcf = malloc(sizeof(struct gconffile) * 2);
60 memset(newcf, 0, sizeof(struct gconffile) * 2);
62 for (i = 0; (*cf)[i].data || (*cf)[i].path; i++);
63 newcf = realloc(*cf, sizeof(struct gconffile) * (i + 2));
66 memmove(newcf + 1, newcf, sizeof(struct gconffile) * (i + 1));
67 memset(newcf, 0, sizeof(struct gconffile));
74 FILE *pushgconffile(struct gconffile **cf, FILE *file, const char *description) {
76 struct gconffile *newcf;
80 debug(DBG_INFO, "could not read config from %s", description);
83 debug(DBG_DBG, "reading config from %s", description);
85 desc = stringcopy(description, 0);
90 newcf = malloc(sizeof(struct gconffile) * 2);
93 memset(newcf, 0, sizeof(struct gconffile) * 2);
95 for (i = 0; (*cf)[i].data || (*cf)[i].path; i++);
96 newcf = realloc(*cf, sizeof(struct gconffile) * (i + 2));
99 memmove(newcf + 1, newcf, sizeof(struct gconffile) * (i + 1));
100 memset(newcf, 0, sizeof(struct gconffile));
102 newcf[0].file = file;
103 newcf[0].path = desc;
110 debug(DBG_ERR, "malloc failed");
114 FILE *pushgconfpath(struct gconffile **cf, const char *path) {
117 f = fopen(path, "r");
118 return pushgconffile(cf, f, path);
121 FILE *pushgconfpaths(struct gconffile **cf, const char *cfgpath) {
125 char *path, *curfile = NULL, *dir;
127 /* if cfgpath is relative, make it relative to current config */
129 path = (char *)cfgpath;
131 /* dirname may modify its argument */
132 curfile = stringcopy((*cf)->path, 0);
134 debug(DBG_ERR, "malloc failed");
137 dir = dirname(curfile);
138 path = malloc(strlen(dir) + strlen(cfgpath) + 2);
140 debug(DBG_ERR, "malloc failed");
144 path[strlen(dir)] = '/';
145 strcpy(path + strlen(dir) + 1, cfgpath);
147 memset(&globbuf, 0, sizeof(glob_t));
148 if (glob(path, 0, NULL, &globbuf)) {
149 debug(DBG_INFO, "could not glob %s", path);
153 for (i = globbuf.gl_pathc - 1; i >= 0; i--) {
154 f = pushgconfpath(cf, globbuf.gl_pathv[i]);
168 int popgconf(struct gconffile **cf) {
173 for (i = 0; (*cf)[i].data || (*cf)[i].path; i++);
174 if (i && (*cf)[0].file) {
175 fclose((*cf)[0].file);
177 debug(DBG_DBG, "closing config file %s", (*cf)[0].path);
186 memmove(*cf, *cf + 1, sizeof(struct gconffile) * i);
190 void freegconf(struct gconffile **cf) {
196 for (i = 0; (*cf)[i].data || (*cf)[i].path; i++) {
198 fclose((*cf)[i].file);
200 debug(DBG_DBG, "closing config file %s", (*cf)[i].path);
209 struct gconffile *openconfigfile(const char *file) {
210 struct gconffile *cf = NULL;
212 if (!pushgconfpath(&cf, file)) {
213 debug(DBG_ERR, "could not read config file %s\n%s", file, strerror(errno));
216 debug(DBG_DBG, "reading config file %s", file);
220 /* Parses config with following syntax:
223 * option-name = value
225 * option-name value {
226 * option-name [=] value
231 int getlinefromcf(struct gconffile *cf, char *line, const size_t size) {
238 return fgets(line, size, cf->file) ? 1 : 0;
243 for (i = pos; cf->data[i] && cf->data[i] != '\n'; i++);
244 if (cf->data[i] == '\n')
246 if (i - pos > size - 1)
248 memcpy(line, cf->data + pos, i - pos);
249 line[i - pos] = '\0';
256 int getconfigline(struct gconffile **cf, char *block, char **opt, char **val, int *conftype) {
265 if (!cf || !*cf || (!(*cf)->file && !(*cf)->data))
269 if (!getlinefromcf(*cf, line, 1024)) {
275 for (tcount = 0; tcount < 3; tcount++) {
276 s = strtokenquote(s, &tokens[tcount], " \t\r\n", "\"'", tcount ? NULL : "#");
278 debug(DBG_ERR, "Syntax error in line starting with: %s", line);
284 if (!tcount || **tokens == '#')
287 if (**tokens == '}') {
290 debug(DBG_ERR, "configuration error, found } with no matching {");
298 *opt = stringcopy(tokens[0], 0);
301 *val = stringcopy(tokens[1], 0);
304 *conftype = CONF_STR;
307 if (tokens[1][0] == '=' && tokens[1][1] == '\0') {
308 *opt = stringcopy(tokens[0], 0);
311 *val = stringcopy(tokens[2], 0);
314 *conftype = CONF_STR;
317 if (tokens[2][0] == '{' && tokens[2][1] == '\0') {
318 *opt = stringcopy(tokens[0], 0);
321 *val = stringcopy(tokens[1], 0);
324 *conftype = CONF_CBK;
330 debug(DBG_ERR, "configuration error in block %s, line starting with %s", block, tokens[0]);
332 debug(DBG_ERR, "configuration error, syntax error in line starting with %s", tokens[0]);
339 debug(DBG_ERR, "configuration error, option %s needs a non-empty value", *opt);
343 debug(DBG_ERR, "malloc failed");
352 /* returns 1 if ok, 0 on error */
353 /* caller must free returned values also on error */
354 int getgenericconfig(struct gconffile **cf, char *block, ...) {
356 char *opt = NULL, *val, *word, *optval, **str = NULL, ***mstr = NULL, **newmstr;
358 int type = 0, conftype = 0, n;
359 int (*cbk)(struct gconffile **, void *, char *, char *, char *) = NULL;
364 if (!getconfigline(cf, block, &opt, &val, &conftype))
369 if (conftype == CONF_STR && !strcasecmp(opt, "include")) {
370 if (!pushgconfpaths(cf, val)) {
371 debug(DBG_ERR, "failed to include config file %s", val);
379 while ((word = va_arg(ap, char *))) {
380 type = va_arg(ap, int);
383 str = va_arg(ap, char **);
388 mstr = va_arg(ap, char ***);
393 bln = va_arg(ap, uint8_t *);
398 cbk = va_arg(ap, int (*)(struct gconffile **, void *, char *, char *, char *));
401 cbkarg = va_arg(ap, void *);
406 if (!strcasecmp(opt, word))
413 debug(DBG_ERR, "configuration error in block %s, unknown option %s", block, opt);
414 debug(DBG_ERR, "configuration error, unknown option %s", opt);
418 if (((type == CONF_STR || type == CONF_MSTR || type == CONF_BLN) && conftype != CONF_STR) ||
419 (type == CONF_CBK && conftype != CONF_CBK)) {
421 debug(DBG_ERR, "configuration error in block %s, wrong syntax for option %s", block, opt);
422 debug(DBG_ERR, "configuration error, wrong syntax for option %s", opt);
429 debug(DBG_ERR, "configuration error, option %s already set to %s", opt, *str);
436 for (n = 0; (*mstr)[n]; n++);
439 newmstr = realloc(*mstr, sizeof(char *) * (n + 2));
441 debug(DBG_ERR, "malloc failed");
445 newmstr[n + 1] = NULL;
449 if (!strcasecmp(val, "on"))
451 else if (!strcasecmp(val, "off"))
455 debug(DBG_ERR, "configuration error in block %s, value for option %s must be on or off, not %s", block, opt, val);
457 debug(DBG_ERR, "configuration error, value for option %s must be on or off, not %s", opt, val);
462 optval = malloc(strlen(opt) + strlen(val) + 2);
464 debug(DBG_ERR, "malloc failed");
467 sprintf(optval, "%s %s", opt, val);
468 if (!cbk(cf, cbkarg, optval, opt, val)) {
479 debug(DBG_DBG, "getgenericconfig: block %s: %s = %s", block, opt, val);
481 debug(DBG_DBG, "getgenericconfig: %s = %s", opt, val);
482 if (type == CONF_BLN)
487 debug(DBG_ERR, "getgenericconfig: internal parameter error");