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>
20 /* returns NULL on error, where to continue parsing if token and ok. E.g. "" will return token with empty string */
21 char *strtokenquote(char *s, char **token, char *del, char *quote, char *comment) {
24 if (!t || !token || !del)
26 while (*t && strchr(del, *t))
28 if (!*t || (comment && strchr(comment, *t))) {
30 return t + 1; /* needs to be non-NULL, but value doesn't matter */
32 if (quote && (q = strchr(quote, *t))) {
35 while (*t && *t != *q)
37 if (!*t || (t[1] && !strchr(del, t[1])))
45 while (*t && !strchr(del, *t))
51 FILE *pushgconffile(struct gconffile **cf, const char *path) {
53 struct gconffile *newcf;
58 debug(DBG_INFO, "could not read config file %s", path);
61 debug(DBG_DBG, "opened config file %s", path);
63 newcf = malloc(sizeof(struct gconffile) * 2);
65 debugx(1, DBG_ERR, "malloc failed");
69 for (i = 0; (*cf)[i].path; i++);
70 newcf = realloc(*cf, sizeof(struct gconffile) * (i + 2));
72 debugx(1, DBG_ERR, "malloc failed");
73 memmove(newcf + 1, newcf, sizeof(struct gconffile) * (i + 1));
76 newcf[0].path = stringcopy(path, 0);
81 FILE *pushgconffiles(struct gconffile **cf, const char *cfgpath) {
85 char *path, *curfile = NULL, *dir;
87 /* if cfgpath is relative, make it relative to current config */
89 path = (char *)cfgpath;
91 /* dirname may modify its argument */
92 curfile = stringcopy((*cf)->path, 0);
94 debugx(1, DBG_ERR, "malloc failed");
95 dir = dirname(curfile);
96 path = malloc(strlen(dir) + strlen(cfgpath) + 2);
98 debugx(1, DBG_ERR, "malloc failed");
100 path[strlen(dir)] = '/';
101 strcpy(path + strlen(dir) + 1, cfgpath);
103 memset(&globbuf, 0, sizeof(glob_t));
104 if (glob(path, 0, NULL, &globbuf)) {
105 debug(DBG_INFO, "could not glob %s", path);
108 for (i = globbuf.gl_pathc - 1; i >= 0; i--) {
109 f = pushgconffile(cf, globbuf.gl_pathv[i]);
122 FILE *popgconffile(struct gconffile **cf) {
127 for (i = 0; (*cf)[i].path; i++);
128 if (i && (*cf)[0].file) {
129 fclose((*cf)[0].file);
130 debug(DBG_DBG, "closing config file %s", (*cf)[0].path);
137 memmove(*cf, *cf + 1, sizeof(struct gconffile) * i);
138 return (*cf)[0].file;
141 /* Parses config with following syntax:
144 * option-name = value
146 * option-name value {
147 * option-name [=] value
151 void getgenericconfig(struct gconffile **cf, char *block, ...) {
154 /* initialise lots of stuff to avoid stupid compiler warnings */
155 char *tokens[3], *s, *opt = NULL, *val = NULL, *word, *optval, **str = NULL, ***mstr = NULL;
157 int type = 0, tcount, conftype = 0, n;
158 void (*cbk)(struct gconffile **, char *, char *, char *) = NULL;
160 if (!cf || !*cf || !(*cf)->file)
163 if (!fgets(line, 1024, (*cf)->file)) {
164 if (popgconffile(cf))
169 for (tcount = 0; tcount < 3; tcount++) {
170 s = strtokenquote(s, &tokens[tcount], " \t\r\n", "\"'", tcount ? NULL : "#");
172 debugx(1, DBG_ERR, "Syntax error in line starting with: %s", line);
176 if (!tcount || **tokens == '#')
179 if (**tokens == '}') {
182 debugx(1, DBG_ERR, "configuration error, found } with no matching {");
192 if (tokens[1][0] == '=' && tokens[1][1] == '\0') {
198 if (tokens[2][0] == '{' && tokens[2][1] == '\0') {
207 debugx(1, DBG_ERR, "configuration error in block %s, line starting with %s", block, tokens[0]);
208 debugx(1, DBG_ERR, "configuration error, syntax error in line starting with %s", tokens[0]);
212 debugx(1, DBG_ERR, "configuration error, option %s needs a non-empty value", opt);
214 if (conftype == CONF_STR && !strcasecmp(opt, "include")) {
215 if (!pushgconffiles(cf, val))
216 debugx(1, DBG_ERR, "failed to include config file %s", val);
221 while ((word = va_arg(ap, char *))) {
222 type = va_arg(ap, int);
225 str = va_arg(ap, char **);
227 debugx(1, DBG_ERR, "getgenericconfig: internal parameter error");
230 mstr = va_arg(ap, char ***);
232 debugx(1, DBG_ERR, "getgenericconfig: internal parameter error");
235 bln = va_arg(ap, uint8_t *);
237 debugx(1, DBG_ERR, "getgenericconfig: internal parameter error");
240 cbk = va_arg(ap, void (*)(struct gconffile **, char *, char *, char *));
243 debugx(1, DBG_ERR, "getgenericconfig: internal parameter error");
245 if (!strcasecmp(opt, word))
252 debugx(1, DBG_ERR, "configuration error in block %s, unknown option %s", block, opt);
253 debugx(1, DBG_ERR, "configuration error, unknown option %s", opt);
256 if (((type == CONF_STR || type == CONF_MSTR || type == CONF_BLN) && conftype != CONF_STR) ||
257 (type == CONF_CBK && conftype != CONF_CBK)) {
259 debugx(1, DBG_ERR, "configuration error in block %s, wrong syntax for option %s", block, opt);
260 debugx(1, DBG_ERR, "configuration error, wrong syntax for option %s", opt);
266 debugx(1, DBG_ERR, "configuration error, option %s already set to %s", opt, *str);
267 *str = stringcopy(val, 0);
269 debugx(1, DBG_ERR, "malloc failed");
273 for (n = 0; (*mstr)[n]; n++);
276 *mstr = realloc(*mstr, sizeof(char *) * (n + 2));
278 debugx(1, DBG_ERR, "malloc failed");
279 (*mstr)[n] = stringcopy(val, 0);
280 (*mstr)[n + 1] = NULL;
283 if (!strcasecmp(val, "on"))
285 else if (!strcasecmp(val, "off"))
288 debugx(1, DBG_ERR, "configuration error in block %s, value for option %s must be on or off, not %s", block, opt, val);
290 debugx(1, DBG_ERR, "configuration error, value for option %s must be on or off, not %s", opt, val);
293 optval = malloc(strlen(opt) + strlen(val) + 2);
295 debugx(1, DBG_ERR, "malloc failed");
296 sprintf(optval, "%s %s", opt, val);
297 cbk(cf, optval, opt, val);
301 debugx(1, DBG_ERR, "getgenericconfig: internal parameter error");
304 debug(DBG_DBG, "getgenericconfig: block %s: %s = %s", block, opt, val);
306 debug(DBG_DBG, "getgenericconfig: %s = %s", opt, val);