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, const char *path) {
76 struct gconffile *newcf;
82 debug(DBG_INFO, "could not read config file %s", path);
85 debug(DBG_DBG, "opened config file %s", path);
87 pathcopy = stringcopy(path, 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));
105 newcf[0].path = pathcopy;
112 debug(DBG_ERR, "malloc failed");
116 FILE *pushgconffiles(struct gconffile **cf, const char *cfgpath) {
120 char *path, *curfile = NULL, *dir;
122 /* if cfgpath is relative, make it relative to current config */
124 path = (char *)cfgpath;
126 /* dirname may modify its argument */
127 curfile = stringcopy((*cf)->path, 0);
129 debug(DBG_ERR, "malloc failed");
132 dir = dirname(curfile);
133 path = malloc(strlen(dir) + strlen(cfgpath) + 2);
135 debug(DBG_ERR, "malloc failed");
139 path[strlen(dir)] = '/';
140 strcpy(path + strlen(dir) + 1, cfgpath);
142 memset(&globbuf, 0, sizeof(glob_t));
143 if (glob(path, 0, NULL, &globbuf)) {
144 debug(DBG_INFO, "could not glob %s", path);
148 for (i = globbuf.gl_pathc - 1; i >= 0; i--) {
149 f = pushgconffile(cf, globbuf.gl_pathv[i]);
163 int popgconffile(struct gconffile **cf) {
168 for (i = 0; (*cf)[i].data || (*cf)[i].path; i++);
169 if (i && (*cf)[0].file) {
170 fclose((*cf)[0].file);
172 debug(DBG_DBG, "closing config file %s", (*cf)[0].path);
181 memmove(*cf, *cf + 1, sizeof(struct gconffile) * i);
185 void freegconf(struct gconffile **cf) {
191 for (i = 0; (*cf)[i].data || (*cf)[i].path; i++) {
193 fclose((*cf)[i].file);
195 debug(DBG_DBG, "closing config file %s", (*cf)[i].path);
204 struct gconffile *openconfigfile(const char *file) {
205 struct gconffile *cf = NULL;
207 if (!pushgconffile(&cf, file)) {
208 debug(DBG_ERR, "could not read config file %s\n%s", file, strerror(errno));
211 debug(DBG_DBG, "reading config file %s", file);
215 /* Parses config with following syntax:
218 * option-name = value
220 * option-name value {
221 * option-name [=] value
226 int getlinefromcf(struct gconffile *cf, char *line, const size_t size) {
233 return fgets(line, size, cf->file) ? 1 : 0;
238 for (i = pos; cf->data[i] && cf->data[i] != '\n'; i++);
239 if (cf->data[i] == '\n')
241 if (i - pos > size - 1)
243 memcpy(line, cf->data + pos, i - pos);
244 line[i - pos] = '\0';
251 int getconfigline(struct gconffile **cf, char *block, char **opt, char **val, int *conftype) {
260 if (!cf || !*cf || (!(*cf)->file && !(*cf)->data))
264 if (!getlinefromcf(*cf, line, 1024)) {
265 if (popgconffile(cf))
270 for (tcount = 0; tcount < 3; tcount++) {
271 s = strtokenquote(s, &tokens[tcount], " \t\r\n", "\"'", tcount ? NULL : "#");
273 debug(DBG_ERR, "Syntax error in line starting with: %s", line);
279 if (!tcount || **tokens == '#')
282 if (**tokens == '}') {
285 debug(DBG_ERR, "configuration error, found } with no matching {");
293 *opt = stringcopy(tokens[0], 0);
296 *val = stringcopy(tokens[1], 0);
299 *conftype = CONF_STR;
302 if (tokens[1][0] == '=' && tokens[1][1] == '\0') {
303 *opt = stringcopy(tokens[0], 0);
306 *val = stringcopy(tokens[2], 0);
309 *conftype = CONF_STR;
312 if (tokens[2][0] == '{' && tokens[2][1] == '\0') {
313 *opt = stringcopy(tokens[0], 0);
316 *val = stringcopy(tokens[1], 0);
319 *conftype = CONF_CBK;
325 debug(DBG_ERR, "configuration error in block %s, line starting with %s", block, tokens[0]);
327 debug(DBG_ERR, "configuration error, syntax error in line starting with %s", tokens[0]);
334 debug(DBG_ERR, "configuration error, option %s needs a non-empty value", *opt);
338 debug(DBG_ERR, "malloc failed");
347 /* returns 1 if ok, 0 on error */
348 /* caller must free returned values also on error */
349 int getgenericconfig(struct gconffile **cf, char *block, ...) {
351 char *opt = NULL, *val, *word, *optval, **str = NULL, ***mstr = NULL, **newmstr;
353 int type = 0, conftype = 0, n;
354 void (*cbk)(struct gconffile **, void *, char *, char *, char *) = NULL;
359 if (!getconfigline(cf, block, &opt, &val, &conftype))
364 if (conftype == CONF_STR && !strcasecmp(opt, "include")) {
365 if (!pushgconffiles(cf, val)) {
366 debug(DBG_ERR, "failed to include config file %s", val);
374 while ((word = va_arg(ap, char *))) {
375 type = va_arg(ap, int);
378 str = va_arg(ap, char **);
383 mstr = va_arg(ap, char ***);
388 bln = va_arg(ap, uint8_t *);
393 cbk = va_arg(ap, void (*)(struct gconffile **, void *, char *, char *, char *));
396 cbkarg = va_arg(ap, void *);
401 if (!strcasecmp(opt, word))
408 debug(DBG_ERR, "configuration error in block %s, unknown option %s", block, opt);
409 debug(DBG_ERR, "configuration error, unknown option %s", opt);
413 if (((type == CONF_STR || type == CONF_MSTR || type == CONF_BLN) && conftype != CONF_STR) ||
414 (type == CONF_CBK && conftype != CONF_CBK)) {
416 debug(DBG_ERR, "configuration error in block %s, wrong syntax for option %s", block, opt);
417 debug(DBG_ERR, "configuration error, wrong syntax for option %s", opt);
424 debug(DBG_ERR, "configuration error, option %s already set to %s", opt, *str);
431 for (n = 0; (*mstr)[n]; n++);
434 newmstr = realloc(*mstr, sizeof(char *) * (n + 2));
436 debug(DBG_ERR, "malloc failed");
440 newmstr[n + 1] = NULL;
444 if (!strcasecmp(val, "on"))
446 else if (!strcasecmp(val, "off"))
450 debug(DBG_ERR, "configuration error in block %s, value for option %s must be on or off, not %s", block, opt, val);
452 debug(DBG_ERR, "configuration error, value for option %s must be on or off, not %s", opt, val);
457 optval = malloc(strlen(opt) + strlen(val) + 2);
459 debug(DBG_ERR, "malloc failed");
462 sprintf(optval, "%s %s", opt, val);
463 cbk(cf, cbkarg, optval, opt, val);
471 debug(DBG_DBG, "getgenericconfig: block %s: %s = %s", block, opt, val);
473 debug(DBG_DBG, "getgenericconfig: %s = %s", opt, val);
474 if (type == CONF_BLN)
479 debug(DBG_ERR, "getgenericconfig: internal parameter error");