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;
60 debug(DBG_INFO, "could not read config file %s", path);
63 debug(DBG_DBG, "opened config file %s", path);
65 pathcopy = stringcopy(path, 0);
70 newcf = malloc(sizeof(struct gconffile) * 2);
76 for (i = 0; (*cf)[i].path; i++);
77 newcf = realloc(*cf, sizeof(struct gconffile) * (i + 2));
80 memmove(newcf + 1, newcf, sizeof(struct gconffile) * (i + 1));
83 newcf[0].path = pathcopy;
90 debug(DBG_ERR, "malloc failed");
94 FILE *pushgconffiles(struct gconffile **cf, const char *cfgpath) {
98 char *path, *curfile = NULL, *dir;
100 /* if cfgpath is relative, make it relative to current config */
102 path = (char *)cfgpath;
104 /* dirname may modify its argument */
105 curfile = stringcopy((*cf)->path, 0);
107 debug(DBG_ERR, "malloc failed");
110 dir = dirname(curfile);
111 path = malloc(strlen(dir) + strlen(cfgpath) + 2);
113 debug(DBG_ERR, "malloc failed");
117 path[strlen(dir)] = '/';
118 strcpy(path + strlen(dir) + 1, cfgpath);
120 memset(&globbuf, 0, sizeof(glob_t));
121 if (glob(path, 0, NULL, &globbuf)) {
122 debug(DBG_INFO, "could not glob %s", path);
126 for (i = globbuf.gl_pathc - 1; i >= 0; i--) {
127 f = pushgconffile(cf, globbuf.gl_pathv[i]);
141 FILE *popgconffile(struct gconffile **cf) {
146 for (i = 0; (*cf)[i].path; i++);
147 if (i && (*cf)[0].file) {
148 fclose((*cf)[0].file);
149 debug(DBG_DBG, "closing config file %s", (*cf)[0].path);
157 memmove(*cf, *cf + 1, sizeof(struct gconffile) * i);
158 return (*cf)[0].file;
161 struct gconffile *openconfigfile(const char *file) {
162 struct gconffile *cf = NULL;
164 if (!pushgconffile(&cf, file)) {
165 debug(DBG_ERR, "could not read config file %s\n%s", file, strerror(errno));
168 debug(DBG_DBG, "reading config file %s", file);
172 /* Parses config with following syntax:
175 * option-name = value
177 * option-name value {
178 * option-name [=] value
183 int getconfigline(struct gconffile **cf, char *block, char **opt, char **val, int *conftype) {
192 if (!cf || !*cf || !(*cf)->file)
196 if (!fgets(line, 1024, (*cf)->file)) {
197 if (popgconffile(cf))
202 for (tcount = 0; tcount < 3; tcount++) {
203 s = strtokenquote(s, &tokens[tcount], " \t\r\n", "\"'", tcount ? NULL : "#");
205 debug(DBG_ERR, "Syntax error in line starting with: %s", line);
211 if (!tcount || **tokens == '#')
214 if (**tokens == '}') {
217 debug(DBG_ERR, "configuration error, found } with no matching {");
225 *opt = stringcopy(tokens[0], 0);
228 *val = stringcopy(tokens[1], 0);
231 *conftype = CONF_STR;
234 if (tokens[1][0] == '=' && tokens[1][1] == '\0') {
235 *opt = stringcopy(tokens[0], 0);
238 *val = stringcopy(tokens[2], 0);
241 *conftype = CONF_STR;
244 if (tokens[2][0] == '{' && tokens[2][1] == '\0') {
245 *opt = stringcopy(tokens[0], 0);
248 *val = stringcopy(tokens[1], 0);
251 *conftype = CONF_CBK;
257 debug(DBG_ERR, "configuration error in block %s, line starting with %s", block, tokens[0]);
259 debug(DBG_ERR, "configuration error, syntax error in line starting with %s", tokens[0]);
266 debug(DBG_ERR, "configuration error, option %s needs a non-empty value", *opt);
270 debug(DBG_ERR, "malloc failed");
279 /* returns 1 if ok, 0 on error */
280 /* caller must free returned values also on error */
281 int getgenericconfig(struct gconffile **cf, char *block, ...) {
283 char *opt = NULL, *val, *word, *optval, **str = NULL, ***mstr = NULL, **newmstr;
285 int type = 0, conftype = 0, n;
286 void (*cbk)(struct gconffile **, void *, char *, char *, char *) = NULL;
291 if (!getconfigline(cf, block, &opt, &val, &conftype))
296 if (conftype == CONF_STR && !strcasecmp(opt, "include")) {
297 if (!pushgconffiles(cf, val)) {
298 debug(DBG_ERR, "failed to include config file %s", val);
306 while ((word = va_arg(ap, char *))) {
307 type = va_arg(ap, int);
310 str = va_arg(ap, char **);
315 mstr = va_arg(ap, char ***);
320 bln = va_arg(ap, uint8_t *);
325 cbk = va_arg(ap, void (*)(struct gconffile **, void *, char *, char *, char *));
328 cbkarg = va_arg(ap, void *);
333 if (!strcasecmp(opt, word))
340 debug(DBG_ERR, "configuration error in block %s, unknown option %s", block, opt);
341 debug(DBG_ERR, "configuration error, unknown option %s", opt);
345 if (((type == CONF_STR || type == CONF_MSTR || type == CONF_BLN) && conftype != CONF_STR) ||
346 (type == CONF_CBK && conftype != CONF_CBK)) {
348 debug(DBG_ERR, "configuration error in block %s, wrong syntax for option %s", block, opt);
349 debug(DBG_ERR, "configuration error, wrong syntax for option %s", opt);
356 debug(DBG_ERR, "configuration error, option %s already set to %s", opt, *str);
363 for (n = 0; (*mstr)[n]; n++);
366 newmstr = realloc(*mstr, sizeof(char *) * (n + 2));
368 debug(DBG_ERR, "malloc failed");
372 newmstr[n + 1] = NULL;
376 if (!strcasecmp(val, "on"))
378 else if (!strcasecmp(val, "off"))
382 debug(DBG_ERR, "configuration error in block %s, value for option %s must be on or off, not %s", block, opt, val);
384 debug(DBG_ERR, "configuration error, value for option %s must be on or off, not %s", opt, val);
389 optval = malloc(strlen(opt) + strlen(val) + 2);
391 debug(DBG_ERR, "malloc failed");
394 sprintf(optval, "%s %s", opt, val);
395 cbk(cf, cbkarg, optval, opt, val);
403 debug(DBG_DBG, "getgenericconfig: block %s: %s = %s", block, opt, val);
405 debug(DBG_DBG, "getgenericconfig: %s = %s", opt, val);
406 if (type == CONF_BLN)
411 debug(DBG_ERR, "getgenericconfig: internal parameter error");