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.
15 #include <sys/types.h>
23 char *mystringcopyx(const char *s) {
27 debugx(1, DBG_ERR, "malloc failed");
31 /* returns NULL on error, where to continue parsing if token and ok. E.g. "" will return token with empty string */
32 char *strtokenquote(char *s, char **token, char *del, char *quote, char *comment) {
35 if (!t || !token || !del)
37 while (*t && strchr(del, *t))
39 if (!*t || (comment && strchr(comment, *t))) {
41 return t + 1; /* needs to be non-NULL, but value doesn't matter */
43 if (quote && (q = strchr(quote, *t))) {
46 while (*t && *t != *q)
48 if (!*t || (t[1] && !strchr(del, t[1])))
56 while (*t && !strchr(del, *t))
62 FILE *pushgconffile(struct gconffile **cf, const char *path) {
64 struct gconffile *newcf;
69 debug(DBG_INFO, "could not read config file %s", path);
72 debug(DBG_DBG, "opened config file %s", path);
74 newcf = malloc(sizeof(struct gconffile) * 2);
76 debugx(1, DBG_ERR, "malloc failed");
80 for (i = 0; (*cf)[i].path; i++);
81 newcf = realloc(*cf, sizeof(struct gconffile) * (i + 2));
83 debugx(1, DBG_ERR, "malloc failed");
84 memmove(newcf + 1, newcf, sizeof(struct gconffile) * (i + 1));
87 newcf[0].path = mystringcopyx(path);
92 FILE *pushgconffiles(struct gconffile **cf, const char *cfgpath) {
96 char *path, *curfile = NULL, *dir;
98 /* if cfgpath is relative, make it relative to current config */
100 path = (char *)cfgpath;
102 /* dirname may modify its argument */
103 curfile = mystringcopyx((*cf)->path);
104 dir = dirname(curfile);
105 path = malloc(strlen(dir) + strlen(cfgpath) + 2);
107 debugx(1, DBG_ERR, "malloc failed");
109 path[strlen(dir)] = '/';
110 strcpy(path + strlen(dir) + 1, cfgpath);
112 memset(&globbuf, 0, sizeof(glob_t));
113 if (glob(path, 0, NULL, &globbuf))
114 debug(DBG_INFO, "could not glob %s", path);
116 for (i = globbuf.gl_pathc - 1; i >= 0; i--) {
117 f = pushgconffile(cf, globbuf.gl_pathv[i]);
130 FILE *popgconffile(struct gconffile **cf) {
135 for (i = 0; (*cf)[i].path; i++);
136 if (i && (*cf)[0].file) {
137 fclose((*cf)[0].file);
138 debug(DBG_DBG, "closing config file %s", (*cf)[0].path);
146 memmove(*cf, *cf + 1, sizeof(struct gconffile) * i);
147 return (*cf)[0].file;
150 struct gconffile *openconfigfile(const char *file) {
151 struct gconffile *cf = NULL;
153 if (!pushgconffile(&cf, file))
154 debugx(1, DBG_ERR, "could not read config file %s\n%s", file, strerror(errno));
155 debug(DBG_DBG, "reading config file %s", file);
159 /* Parses config with following syntax:
162 * option-name = value
164 * option-name value {
165 * option-name [=] value
170 void getconfigline(struct gconffile **cf, char *block, char **opt, char **val, int *conftype) {
179 if (!cf || !*cf || !(*cf)->file)
183 if (!fgets(line, 1024, (*cf)->file)) {
184 if (popgconffile(cf))
189 for (tcount = 0; tcount < 3; tcount++) {
190 s = strtokenquote(s, &tokens[tcount], " \t\r\n", "\"'", tcount ? NULL : "#");
192 debugx(1, DBG_ERR, "Syntax error in line starting with: %s", line);
196 if (!tcount || **tokens == '#')
199 if (**tokens == '}') {
202 debugx(1, DBG_ERR, "configuration error, found } with no matching {");
209 *opt = mystringcopyx(tokens[0]);
210 *val = mystringcopyx(tokens[1]);
211 *conftype = CONF_STR;
214 if (tokens[1][0] == '=' && tokens[1][1] == '\0') {
215 *opt = mystringcopyx(tokens[0]);
216 *val = mystringcopyx(tokens[2]);
217 *conftype = CONF_STR;
220 if (tokens[2][0] == '{' && tokens[2][1] == '\0') {
221 *opt = mystringcopyx(tokens[0]);
222 *val = mystringcopyx(tokens[1]);
223 *conftype = CONF_CBK;
229 debugx(1, DBG_ERR, "configuration error in block %s, line starting with %s", block, tokens[0]);
230 debugx(1, DBG_ERR, "configuration error, syntax error in line starting with %s", tokens[0]);
234 debugx(1, DBG_ERR, "configuration error, option %s needs a non-empty value", *opt);
238 uint8_t hexdigit2int(char d) {
239 if (d >= '0' && d <= '9')
241 if (d >= 'a' && d <= 'f')
243 if (d >= 'A' && d <= 'F')
248 void unhex(char *s) {
250 for (t = s; *t; s++) {
251 if (*t == '%' && isxdigit(t[1]) && isxdigit(t[2])) {
252 *s = 16 * hexdigit2int(t[1]) + hexdigit2int(t[2]);
260 void getgenericconfig(struct gconffile **cf, char *block, ...) {
262 char *opt = NULL, *val, *word, *optval, **str = NULL, ***mstr = NULL, *endptr;
264 long int *lint = NULL;
265 int type = 0, conftype = 0, n;
266 void (*cbk)(struct gconffile **, char *, char *, char *) = NULL;
270 getconfigline(cf, block, &opt, &val, &conftype);
274 if (conftype == CONF_STR && !strcasecmp(opt, "include")) {
275 if (!pushgconffiles(cf, val))
276 debugx(1, DBG_ERR, "failed to include config file %s", val);
282 while ((word = va_arg(ap, char *))) {
283 type = va_arg(ap, int);
286 str = va_arg(ap, char **);
288 debugx(1, DBG_ERR, "getgenericconfig: internal parameter error");
291 mstr = va_arg(ap, char ***);
293 debugx(1, DBG_ERR, "getgenericconfig: internal parameter error");
296 bln = va_arg(ap, uint8_t *);
298 debugx(1, DBG_ERR, "getgenericconfig: internal parameter error");
301 lint = va_arg(ap, long int *);
303 debugx(1, DBG_ERR, "getgenericconfig: internal parameter error");
306 cbk = va_arg(ap, void (*)(struct gconffile **, char *, char *, char *));
309 debugx(1, DBG_ERR, "getgenericconfig: internal parameter error");
311 if (!strcasecmp(opt, word))
318 debugx(1, DBG_ERR, "configuration error in block %s, unknown option %s", block, opt);
319 debugx(1, DBG_ERR, "configuration error, unknown option %s", opt);
322 if (((type == CONF_STR || type == CONF_MSTR || type == CONF_BLN || type == CONF_LINT) && conftype != CONF_STR) ||
323 (type == CONF_CBK && conftype != CONF_CBK)) {
325 debugx(1, DBG_ERR, "configuration error in block %s, wrong syntax for option %s", block, opt);
326 debugx(1, DBG_ERR, "configuration error, wrong syntax for option %s", opt);
332 debugx(1, DBG_ERR, "configuration error, option %s already set to %s", opt, *str);
338 for (n = 0; (*mstr)[n]; n++);
341 *mstr = realloc(*mstr, sizeof(char *) * (n + 2));
343 debugx(1, DBG_ERR, "malloc failed");
346 (*mstr)[n + 1] = NULL;
349 if (!strcasecmp(val, "on"))
351 else if (!strcasecmp(val, "off"))
354 debugx(1, DBG_ERR, "configuration error in block %s, value for option %s must be on or off, not %s", block, opt, val);
356 debugx(1, DBG_ERR, "configuration error, value for option %s must be on or off, not %s", opt, val);
360 *lint = strtol(val, &endptr, 0);
361 if (*lint == LONG_MIN || *lint == LONG_MAX || !endptr || endptr == val || *endptr != '\0') {
363 debugx(1, DBG_ERR, "configuration error in block %s, value for option %s must be an integer, not %s", block, opt, val);
365 debugx(1, DBG_ERR, "configuration error, value for option %s must be an integer, not %s", opt, val);
369 optval = malloc(strlen(opt) + strlen(val) + 2);
371 debugx(1, DBG_ERR, "malloc failed");
372 sprintf(optval, "%s %s", opt, val);
373 cbk(cf, optval, opt, val);
378 debugx(1, DBG_ERR, "getgenericconfig: internal parameter error");
381 debug(DBG_DBG, "getgenericconfig: block %s: %s = %s", block, opt, val);
383 debug(DBG_DBG, "getgenericconfig: %s = %s", opt, val);
384 if (type == CONF_BLN || type == CONF_LINT)