1 /* Copyright (c) 2006-2010, UNINETT AS.
2 * Copyright (c) 2010, UNINETT AS, NORDUnet A/S.
3 * Copyright (c) 2010-2012, NORDUnet A/S. */
4 /* See LICENSE for licensing information. */
12 #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 int pushgconfdata(struct gconffile **cf, const char *data) {
53 struct gconffile *newcf;
56 newcf = malloc(sizeof(struct gconffile) * 2);
59 memset(newcf, 0, sizeof(struct gconffile) * 2);
61 for (i = 0; (*cf)[i].data || (*cf)[i].path; i++);
62 newcf = realloc(*cf, sizeof(struct gconffile) * (i + 2));
65 memmove(newcf + 1, newcf, sizeof(struct gconffile) * (i + 1));
66 memset(newcf, 0, sizeof(struct gconffile));
73 FILE *pushgconffile(struct gconffile **cf, FILE *file, const char *description) {
75 struct gconffile *newcf;
79 debug(DBG_INFO, "could not read config from %s", description);
82 debug(DBG_DBG, "reading config from %s", description);
84 desc = stringcopy(description, 0);
89 newcf = malloc(sizeof(struct gconffile) * 2);
92 memset(newcf, 0, sizeof(struct gconffile) * 2);
94 for (i = 0; (*cf)[i].data || (*cf)[i].path; i++);
95 newcf = realloc(*cf, sizeof(struct gconffile) * (i + 2));
98 memmove(newcf + 1, newcf, sizeof(struct gconffile) * (i + 1));
99 memset(newcf, 0, sizeof(struct gconffile));
101 newcf[0].file = file;
102 newcf[0].path = desc;
109 debug(DBG_ERR, "malloc failed");
113 FILE *pushgconfpath(struct gconffile **cf, const char *path) {
116 f = fopen(path, "r");
117 return pushgconffile(cf, f, path);
120 FILE *pushgconfpaths(struct gconffile **cf, const char *cfgpath) {
124 char *path, *curfile = NULL, *dir;
126 /* if cfgpath is relative, make it relative to current config */
128 path = (char *)cfgpath;
130 /* dirname may modify its argument */
131 curfile = stringcopy((*cf)->path, 0);
133 debug(DBG_ERR, "malloc failed");
136 dir = dirname(curfile);
137 path = malloc(strlen(dir) + strlen(cfgpath) + 2);
139 debug(DBG_ERR, "malloc failed");
143 path[strlen(dir)] = '/';
144 strcpy(path + strlen(dir) + 1, cfgpath);
146 memset(&globbuf, 0, sizeof(glob_t));
147 if (glob(path, 0, NULL, &globbuf)) {
148 debug(DBG_WARN, "could not glob %s", path);
152 for (i = globbuf.gl_pathc - 1; i >= 0; i--) {
153 f = pushgconfpath(cf, globbuf.gl_pathv[i]);
167 int popgconf(struct gconffile **cf) {
172 for (i = 0; (*cf)[i].data || (*cf)[i].path; i++);
173 if (i && (*cf)[0].file) {
174 fclose((*cf)[0].file);
176 debug(DBG_DBG, "closing config file %s", (*cf)[0].path);
185 memmove(*cf, *cf + 1, sizeof(struct gconffile) * i);
189 void freegconfmstr(char **mstr) {
193 for (i = 0; mstr[i]; i++)
199 void freegconf(struct gconffile **cf) {
205 for (i = 0; (*cf)[i].data || (*cf)[i].path; i++) {
207 fclose((*cf)[i].file);
209 debug(DBG_DBG, "closing config file %s", (*cf)[i].path);
218 struct gconffile *openconfigfile(const char *file) {
219 struct gconffile *cf = NULL;
221 if (!pushgconfpath(&cf, file)) {
222 debug(DBG_ERR, "could not read config file %s\n%s", file, strerror(errno));
225 debug(DBG_DBG, "reading config file %s", file);
229 /* Parses config with following syntax:
232 * option-name = value
234 * option-name value {
235 * option-name [=] value
240 int getlinefromcf(struct gconffile *cf, char *line, const size_t size) {
247 return fgets(line, size, cf->file) ? 1 : 0;
252 for (i = pos; cf->data[i] && cf->data[i] != '\n'; i++);
253 if (cf->data[i] == '\n')
255 if (i - pos > size - 1)
257 memcpy(line, cf->data + pos, i - pos);
258 line[i - pos] = '\0';
265 int getconfigline(struct gconffile **cf, char *block, char **opt, char **val, int *conftype) {
274 if (!cf || !*cf || (!(*cf)->file && !(*cf)->data))
278 if (!getlinefromcf(*cf, line, 1024)) {
284 for (tcount = 0; tcount < 3; tcount++) {
285 s = strtokenquote(s, &tokens[tcount], " \t\r\n", "\"'", tcount ? NULL : "#");
287 debug(DBG_ERR, "Syntax error in line starting with: %s", line);
293 if (!tcount || **tokens == '#')
296 if (**tokens == '}') {
299 debug(DBG_ERR, "configuration error, found } with no matching {");
307 *opt = stringcopy(tokens[0], 0);
310 *val = stringcopy(tokens[1], 0);
313 *conftype = CONF_STR;
316 if (tokens[1][0] == '=' && tokens[1][1] == '\0') {
317 *opt = stringcopy(tokens[0], 0);
320 *val = stringcopy(tokens[2], 0);
323 *conftype = CONF_STR;
326 if (tokens[2][0] == '{' && tokens[2][1] == '\0') {
327 *opt = stringcopy(tokens[0], 0);
330 *val = stringcopy(tokens[1], 0);
333 *conftype = CONF_CBK;
339 debug(DBG_ERR, "configuration error in block %s, line starting with %s", block, tokens[0]);
341 debug(DBG_ERR, "configuration error, syntax error in line starting with %s", tokens[0]);
348 debug(DBG_ERR, "configuration error, option %s needs a non-empty value", *opt);
352 debug(DBG_ERR, "malloc failed");
361 uint8_t hexdigit2int(char d) {
362 if (d >= '0' && d <= '9')
364 if (d >= 'a' && d <= 'f')
366 if (d >= 'A' && d <= 'F')
371 void unhex(char *s) {
373 for (t = s; *t; s++) {
374 if (*t == '%' && isxdigit((int)t[1]) && isxdigit((int)t[2])) {
375 *s = 16 * hexdigit2int(t[1]) + hexdigit2int(t[2]);
383 typedef int (*t_fptr)(struct gconffile **, void *, char *, char *, char *);
385 /* returns 1 if ok, 0 on error */
386 /* caller must free returned values also on error */
387 int getgenericconfig(struct gconffile **cf, char *block, ...) {
389 char *opt = NULL, *val, *word, *optval, **str = NULL, ***mstr = NULL, **newmstr, *endptr;
391 long int *lint = NULL;
392 int type = 0, conftype = 0, n;
398 if (!getconfigline(cf, block, &opt, &val, &conftype))
403 if (conftype == CONF_STR && !strcasecmp(opt, "include")) {
404 if (!pushgconfpaths(cf, val)) {
405 debug(DBG_ERR, "failed to include config file %s", val);
413 while ((word = va_arg(ap, char *))) {
414 type = va_arg(ap, int);
417 str = va_arg(ap, char **);
422 mstr = va_arg(ap, char ***);
427 bln = va_arg(ap, uint8_t *);
432 lint = va_arg(ap, long int *);
437 cbk = va_arg(ap, t_fptr);
440 cbkarg = va_arg(ap, void *);
445 if (!strcasecmp(opt, word))
452 debug(DBG_ERR, "configuration error in block %s, unknown option %s", block, opt);
453 debug(DBG_ERR, "configuration error, unknown option %s", opt);
457 if (((type == CONF_STR || type == CONF_MSTR || type == CONF_BLN || type == CONF_LINT) && conftype != CONF_STR) ||
458 (type == CONF_CBK && conftype != CONF_CBK)) {
460 debug(DBG_ERR, "configuration error in block %s, wrong syntax for option %s", block, opt);
461 debug(DBG_ERR, "configuration error, wrong syntax for option %s", opt);
468 debug(DBG_ERR, "configuration error, option %s already set to %s", opt, *str);
476 for (n = 0; (*mstr)[n]; n++);
479 newmstr = realloc(*mstr, sizeof(char *) * (n + 2));
481 debug(DBG_ERR, "malloc failed");
486 newmstr[n + 1] = NULL;
490 if (!strcasecmp(val, "on"))
492 else if (!strcasecmp(val, "off"))
496 debug(DBG_ERR, "configuration error in block %s, value for option %s must be on or off, not %s", block, opt, val);
498 debug(DBG_ERR, "configuration error, value for option %s must be on or off, not %s", opt, val);
504 *lint = strtol(val, &endptr, 0);
505 if (*lint == LONG_MIN || *lint == LONG_MAX || !endptr || endptr == val || *endptr != '\0') {
507 debug(DBG_ERR, "configuration error in block %s, value for option %s must be an integer, not %s", block, opt, val);
509 debug(DBG_ERR, "configuration error, value for option %s must be an integer, not %s", opt, val);
514 optval = malloc(strlen(opt) + strlen(val) + 2);
516 debug(DBG_ERR, "malloc failed");
519 sprintf(optval, "%s %s", opt, val);
520 if (!cbk(cf, cbkarg, optval, opt, val)) {
531 debug(DBG_DBG, "getgenericconfig: block %s: %s = %s", block, opt, val);
533 debug(DBG_DBG, "getgenericconfig: %s = %s", opt, val);
534 if (type == CONF_BLN || type == CONF_LINT)
539 debug(DBG_ERR, "getgenericconfig: internal parameter error");
546 /* Local Variables: */
547 /* c-file-style: "stroustrup" */