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 char *mystringcopyx(const char *s) {
25 debugx(1, DBG_ERR, "malloc failed");
29 /* returns NULL on error, where to continue parsing if token and ok. E.g. "" will return token with empty string */
30 char *strtokenquote(char *s, char **token, char *del, char *quote, char *comment) {
33 if (!t || !token || !del)
35 while (*t && strchr(del, *t))
37 if (!*t || (comment && strchr(comment, *t))) {
39 return t + 1; /* needs to be non-NULL, but value doesn't matter */
41 if (quote && (q = strchr(quote, *t))) {
44 while (*t && *t != *q)
46 if (!*t || (t[1] && !strchr(del, t[1])))
54 while (*t && !strchr(del, *t))
60 FILE *pushgconffile(struct gconffile **cf, const char *path) {
62 struct gconffile *newcf;
67 debug(DBG_INFO, "could not read config file %s", path);
70 debug(DBG_DBG, "opened config file %s", path);
72 newcf = malloc(sizeof(struct gconffile) * 2);
74 debugx(1, DBG_ERR, "malloc failed");
78 for (i = 0; (*cf)[i].path; i++);
79 newcf = realloc(*cf, sizeof(struct gconffile) * (i + 2));
81 debugx(1, DBG_ERR, "malloc failed");
82 memmove(newcf + 1, newcf, sizeof(struct gconffile) * (i + 1));
85 newcf[0].path = mystringcopyx(path);
90 FILE *pushgconffiles(struct gconffile **cf, const char *cfgpath) {
94 char *path, *curfile = NULL, *dir;
96 /* if cfgpath is relative, make it relative to current config */
98 path = (char *)cfgpath;
100 /* dirname may modify its argument */
101 curfile = mystringcopyx((*cf)->path);
102 dir = dirname(curfile);
103 path = malloc(strlen(dir) + strlen(cfgpath) + 2);
105 debugx(1, DBG_ERR, "malloc failed");
107 path[strlen(dir)] = '/';
108 strcpy(path + strlen(dir) + 1, cfgpath);
110 memset(&globbuf, 0, sizeof(glob_t));
111 if (glob(path, 0, NULL, &globbuf)) {
112 debug(DBG_INFO, "could not glob %s", path);
115 for (i = globbuf.gl_pathc - 1; i >= 0; i--) {
116 f = pushgconffile(cf, globbuf.gl_pathv[i]);
129 FILE *popgconffile(struct gconffile **cf) {
134 for (i = 0; (*cf)[i].path; i++);
135 if (i && (*cf)[0].file) {
136 fclose((*cf)[0].file);
137 debug(DBG_DBG, "closing config file %s", (*cf)[0].path);
145 memmove(*cf, *cf + 1, sizeof(struct gconffile) * i);
146 return (*cf)[0].file;
149 struct gconffile *openconfigfile(const char *file) {
150 struct gconffile *cf = NULL;
152 if (!pushgconffile(&cf, file))
153 debugx(1, DBG_ERR, "could not read config file %s\n%s", file, strerror(errno));
154 debug(DBG_DBG, "reading config file %s", file);
158 /* Parses config with following syntax:
161 * option-name = value
163 * option-name value {
164 * option-name [=] value
169 void getconfigline(struct gconffile **cf, char *block, char **opt, char **val, int *conftype) {
178 if (!cf || !*cf || !(*cf)->file)
182 if (!fgets(line, 1024, (*cf)->file)) {
183 if (popgconffile(cf))
188 for (tcount = 0; tcount < 3; tcount++) {
189 s = strtokenquote(s, &tokens[tcount], " \t\r\n", "\"'", tcount ? NULL : "#");
191 debugx(1, DBG_ERR, "Syntax error in line starting with: %s", line);
195 if (!tcount || **tokens == '#')
198 if (**tokens == '}') {
201 debugx(1, DBG_ERR, "configuration error, found } with no matching {");
208 *opt = mystringcopyx(tokens[0]);
209 *val = mystringcopyx(tokens[1]);
210 *conftype = CONF_STR;
213 if (tokens[1][0] == '=' && tokens[1][1] == '\0') {
214 *opt = mystringcopyx(tokens[0]);
215 *val = mystringcopyx(tokens[2]);
216 *conftype = CONF_STR;
219 if (tokens[2][0] == '{' && tokens[2][1] == '\0') {
220 *opt = mystringcopyx(tokens[0]);
221 *val = mystringcopyx(tokens[1]);
222 *conftype = CONF_CBK;
228 debugx(1, DBG_ERR, "configuration error in block %s, line starting with %s", block, tokens[0]);
229 debugx(1, DBG_ERR, "configuration error, syntax error in line starting with %s", tokens[0]);
233 debugx(1, DBG_ERR, "configuration error, option %s needs a non-empty value", *opt);
237 void getgenericconfig(struct gconffile **cf, char *block, ...) {
239 char *opt = NULL, *val, *word, *optval, **str = NULL, ***mstr = NULL;
241 int type = 0, conftype = 0, n;
242 void (*cbk)(struct gconffile **, char *, char *, char *) = NULL;
246 getconfigline(cf, block, &opt, &val, &conftype);
250 if (conftype == CONF_STR && !strcasecmp(opt, "include")) {
251 if (!pushgconffiles(cf, val))
252 debugx(1, DBG_ERR, "failed to include config file %s", val);
258 while ((word = va_arg(ap, char *))) {
259 type = va_arg(ap, int);
262 str = va_arg(ap, char **);
264 debugx(1, DBG_ERR, "getgenericconfig: internal parameter error");
267 mstr = va_arg(ap, char ***);
269 debugx(1, DBG_ERR, "getgenericconfig: internal parameter error");
272 bln = va_arg(ap, uint8_t *);
274 debugx(1, DBG_ERR, "getgenericconfig: internal parameter error");
277 cbk = va_arg(ap, void (*)(struct gconffile **, char *, char *, char *));
280 debugx(1, DBG_ERR, "getgenericconfig: internal parameter error");
282 if (!strcasecmp(opt, word))
289 debugx(1, DBG_ERR, "configuration error in block %s, unknown option %s", block, opt);
290 debugx(1, DBG_ERR, "configuration error, unknown option %s", opt);
293 if (((type == CONF_STR || type == CONF_MSTR || type == CONF_BLN) && conftype != CONF_STR) ||
294 (type == CONF_CBK && conftype != CONF_CBK)) {
296 debugx(1, DBG_ERR, "configuration error in block %s, wrong syntax for option %s", block, opt);
297 debugx(1, DBG_ERR, "configuration error, wrong syntax for option %s", opt);
303 debugx(1, DBG_ERR, "configuration error, option %s already set to %s", opt, *str);
308 for (n = 0; (*mstr)[n]; n++);
311 *mstr = realloc(*mstr, sizeof(char *) * (n + 2));
313 debugx(1, DBG_ERR, "malloc failed");
315 (*mstr)[n + 1] = NULL;
318 if (!strcasecmp(val, "on"))
320 else if (!strcasecmp(val, "off"))
323 debugx(1, DBG_ERR, "configuration error in block %s, value for option %s must be on or off, not %s", block, opt, val);
325 debugx(1, DBG_ERR, "configuration error, value for option %s must be on or off, not %s", opt, val);
328 optval = malloc(strlen(opt) + strlen(val) + 2);
330 debugx(1, DBG_ERR, "malloc failed");
331 sprintf(optval, "%s %s", opt, val);
332 cbk(cf, optval, opt, val);
337 debugx(1, DBG_ERR, "getgenericconfig: internal parameter error");
340 debug(DBG_DBG, "getgenericconfig: block %s: %s = %s", block, opt, val);
342 debug(DBG_DBG, "getgenericconfig: %s = %s", opt, val);
343 if (type == CONF_BLN)