1 /* Copyright (c) 2007-2008, UNINETT AS */
2 /* See LICENSE for licensing information. */
10 #include <sys/types.h>
18 /* returns NULL on error, where to continue parsing if token and ok. E.g. "" will return token with empty string */
19 char *strtokenquote(char *s, char **token, char *del, char *quote, char *comment) {
22 if (!t || !token || !del)
24 while (*t && strchr(del, *t))
26 if (!*t || (comment && strchr(comment, *t))) {
28 return t + 1; /* needs to be non-NULL, but value doesn't matter */
30 if (quote && (q = strchr(quote, *t))) {
33 while (*t && *t != *q)
35 if (!*t || (t[1] && !strchr(del, t[1])))
43 while (*t && !strchr(del, *t))
49 int pushgconfdata(struct gconffile **cf, const char *data) {
51 struct gconffile *newcf;
54 newcf = malloc(sizeof(struct gconffile) * 2);
57 memset(newcf, 0, sizeof(struct gconffile) * 2);
59 for (i = 0; (*cf)[i].data || (*cf)[i].path; i++);
60 newcf = realloc(*cf, sizeof(struct gconffile) * (i + 2));
63 memmove(newcf + 1, newcf, sizeof(struct gconffile) * (i + 1));
64 memset(newcf, 0, sizeof(struct gconffile));
71 FILE *pushgconffile(struct gconffile **cf, FILE *file, const char *description) {
73 struct gconffile *newcf;
77 debug(DBG_INFO, "could not read config from %s", description);
80 debug(DBG_DBG, "reading config from %s", description);
82 desc = stringcopy(description, 0);
87 newcf = malloc(sizeof(struct gconffile) * 2);
90 memset(newcf, 0, sizeof(struct gconffile) * 2);
92 for (i = 0; (*cf)[i].data || (*cf)[i].path; i++);
93 newcf = realloc(*cf, sizeof(struct gconffile) * (i + 2));
96 memmove(newcf + 1, newcf, sizeof(struct gconffile) * (i + 1));
97 memset(newcf, 0, sizeof(struct gconffile));
100 newcf[0].path = desc;
107 debug(DBG_ERR, "malloc failed");
111 FILE *pushgconfpath(struct gconffile **cf, const char *path) {
114 f = fopen(path, "r");
115 return pushgconffile(cf, f, path);
118 FILE *pushgconfpaths(struct gconffile **cf, const char *cfgpath) {
122 char *path, *curfile = NULL, *dir;
124 /* if cfgpath is relative, make it relative to current config */
126 path = (char *)cfgpath;
128 /* dirname may modify its argument */
129 curfile = stringcopy((*cf)->path, 0);
131 debug(DBG_ERR, "malloc failed");
134 dir = dirname(curfile);
135 path = malloc(strlen(dir) + strlen(cfgpath) + 2);
137 debug(DBG_ERR, "malloc failed");
141 path[strlen(dir)] = '/';
142 strcpy(path + strlen(dir) + 1, cfgpath);
144 memset(&globbuf, 0, sizeof(glob_t));
145 if (glob(path, 0, NULL, &globbuf)) {
146 debug(DBG_WARN, "could not glob %s", path);
150 for (i = globbuf.gl_pathc - 1; i >= 0; i--) {
151 f = pushgconfpath(cf, globbuf.gl_pathv[i]);
165 int popgconf(struct gconffile **cf) {
170 for (i = 0; (*cf)[i].data || (*cf)[i].path; i++);
171 if (i && (*cf)[0].file) {
172 fclose((*cf)[0].file);
174 debug(DBG_DBG, "closing config file %s", (*cf)[0].path);
183 memmove(*cf, *cf + 1, sizeof(struct gconffile) * i);
187 void freegconfmstr(char **mstr) {
191 for (i = 0; mstr[i]; i++)
197 void freegconf(struct gconffile **cf) {
203 for (i = 0; (*cf)[i].data || (*cf)[i].path; i++) {
205 fclose((*cf)[i].file);
207 debug(DBG_DBG, "closing config file %s", (*cf)[i].path);
216 struct gconffile *openconfigfile(const char *file) {
217 struct gconffile *cf = NULL;
219 if (!pushgconfpath(&cf, file)) {
220 debug(DBG_ERR, "could not read config file %s\n%s", file, strerror(errno));
223 debug(DBG_DBG, "reading config file %s", file);
227 /* Parses config with following syntax:
230 * option-name = value
232 * option-name value {
233 * option-name [=] value
238 int getlinefromcf(struct gconffile *cf, char *line, const size_t size) {
245 return fgets(line, size, cf->file) ? 1 : 0;
250 for (i = pos; cf->data[i] && cf->data[i] != '\n'; i++);
251 if (cf->data[i] == '\n')
253 if (i - pos > size - 1)
255 memcpy(line, cf->data + pos, i - pos);
256 line[i - pos] = '\0';
263 int getconfigline(struct gconffile **cf, char *block, char **opt, char **val, int *conftype) {
272 if (!cf || !*cf || (!(*cf)->file && !(*cf)->data))
276 if (!getlinefromcf(*cf, line, 1024)) {
282 for (tcount = 0; tcount < 3; tcount++) {
283 s = strtokenquote(s, &tokens[tcount], " \t\r\n", "\"'", tcount ? NULL : "#");
285 debug(DBG_ERR, "Syntax error in line starting with: %s", line);
291 if (!tcount || **tokens == '#')
294 if (**tokens == '}') {
297 debug(DBG_ERR, "configuration error, found } with no matching {");
305 *opt = stringcopy(tokens[0], 0);
308 *val = stringcopy(tokens[1], 0);
311 *conftype = CONF_STR;
314 if (tokens[1][0] == '=' && tokens[1][1] == '\0') {
315 *opt = stringcopy(tokens[0], 0);
318 *val = stringcopy(tokens[2], 0);
321 *conftype = CONF_STR;
324 if (tokens[2][0] == '{' && tokens[2][1] == '\0') {
325 *opt = stringcopy(tokens[0], 0);
328 *val = stringcopy(tokens[1], 0);
331 *conftype = CONF_CBK;
337 debug(DBG_ERR, "configuration error in block %s, line starting with %s", block, tokens[0]);
339 debug(DBG_ERR, "configuration error, syntax error in line starting with %s", tokens[0]);
346 debug(DBG_ERR, "configuration error, option %s needs a non-empty value", *opt);
350 debug(DBG_ERR, "malloc failed");
359 uint8_t hexdigit2int(char d) {
360 if (d >= '0' && d <= '9')
362 if (d >= 'a' && d <= 'f')
364 if (d >= 'A' && d <= 'F')
369 void unhex(char *s) {
371 for (t = s; *t; s++) {
372 if (*t == '%' && isxdigit((int)t[1]) && isxdigit((int)t[2])) {
373 *s = 16 * hexdigit2int(t[1]) + hexdigit2int(t[2]);
381 typedef int (*t_fptr)(struct gconffile **, void *, char *, char *, char *);
383 /* returns 1 if ok, 0 on error */
384 /* caller must free returned values also on error */
385 int getgenericconfig(struct gconffile **cf, char *block, ...) {
387 char *opt = NULL, *val, *word, *optval, **str = NULL, ***mstr = NULL, **newmstr, *endptr;
389 long int *lint = NULL;
390 int type = 0, conftype = 0, n;
396 if (!getconfigline(cf, block, &opt, &val, &conftype))
401 if (conftype == CONF_STR && !strcasecmp(opt, "include")) {
402 if (!pushgconfpaths(cf, val)) {
403 debug(DBG_ERR, "failed to include config file %s", val);
411 while ((word = va_arg(ap, char *))) {
412 type = va_arg(ap, int);
415 str = va_arg(ap, char **);
420 mstr = va_arg(ap, char ***);
425 bln = va_arg(ap, uint8_t *);
430 lint = va_arg(ap, long int *);
435 cbk = va_arg(ap, t_fptr);
438 cbkarg = va_arg(ap, void *);
443 if (!strcasecmp(opt, word))
450 debug(DBG_ERR, "configuration error in block %s, unknown option %s", block, opt);
451 debug(DBG_ERR, "configuration error, unknown option %s", opt);
455 if (((type == CONF_STR || type == CONF_MSTR || type == CONF_BLN || type == CONF_LINT) && conftype != CONF_STR) ||
456 (type == CONF_CBK && conftype != CONF_CBK)) {
458 debug(DBG_ERR, "configuration error in block %s, wrong syntax for option %s", block, opt);
459 debug(DBG_ERR, "configuration error, wrong syntax for option %s", opt);
466 debug(DBG_ERR, "configuration error, option %s already set to %s", opt, *str);
474 for (n = 0; (*mstr)[n]; n++);
477 newmstr = realloc(*mstr, sizeof(char *) * (n + 2));
479 debug(DBG_ERR, "malloc failed");
484 newmstr[n + 1] = NULL;
488 if (!strcasecmp(val, "on"))
490 else if (!strcasecmp(val, "off"))
494 debug(DBG_ERR, "configuration error in block %s, value for option %s must be on or off, not %s", block, opt, val);
496 debug(DBG_ERR, "configuration error, value for option %s must be on or off, not %s", opt, val);
502 *lint = strtol(val, &endptr, 0);
503 if (*lint == LONG_MIN || *lint == LONG_MAX || !endptr || endptr == val || *endptr != '\0') {
505 debug(DBG_ERR, "configuration error in block %s, value for option %s must be an integer, not %s", block, opt, val);
507 debug(DBG_ERR, "configuration error, value for option %s must be an integer, not %s", opt, val);
512 optval = malloc(strlen(opt) + strlen(val) + 2);
514 debug(DBG_ERR, "malloc failed");
517 sprintf(optval, "%s %s", opt, val);
518 if (!cbk(cf, cbkarg, optval, opt, val)) {
529 debug(DBG_DBG, "getgenericconfig: block %s: %s = %s", block, opt, val);
531 debug(DBG_DBG, "getgenericconfig: %s = %s", opt, val);
532 if (type == CONF_BLN || type == CONF_LINT)
537 debug(DBG_ERR, "getgenericconfig: internal parameter error");
544 /* Local Variables: */
545 /* c-file-style: "stroustrup" */