1 /* Copyright (c) 2006-2010, UNINETT AS
2 * Copyright (c) 2010-2012, NORDUnet A/S */
3 /* See LICENSE for licensing information. */
11 #include <sys/types.h>
19 /* returns NULL on error, where to continue parsing if token and ok. E.g. "" will return token with empty string */
20 char *strtokenquote(char *s, char **token, char *del, char *quote, char *comment) {
23 if (!t || !token || !del)
25 while (*t && strchr(del, *t))
27 if (!*t || (comment && strchr(comment, *t))) {
29 return t + 1; /* needs to be non-NULL, but value doesn't matter */
31 if (quote && (q = strchr(quote, *t))) {
34 while (*t && *t != *q)
36 if (!*t || (t[1] && !strchr(del, t[1])))
44 while (*t && !strchr(del, *t))
50 int pushgconfdata(struct gconffile **cf, const char *data) {
52 struct gconffile *newcf;
55 newcf = malloc(sizeof(struct gconffile) * 2);
58 memset(newcf, 0, sizeof(struct gconffile) * 2);
60 for (i = 0; (*cf)[i].data || (*cf)[i].path; i++);
61 newcf = realloc(*cf, sizeof(struct gconffile) * (i + 2));
64 memmove(newcf + 1, newcf, sizeof(struct gconffile) * (i + 1));
65 memset(newcf, 0, sizeof(struct gconffile));
72 FILE *pushgconffile(struct gconffile **cf, FILE *file, const char *description) {
74 struct gconffile *newcf;
78 debug(DBG_INFO, "could not read config from %s", description);
81 debug(DBG_DBG, "reading config from %s", description);
83 desc = stringcopy(description, 0);
88 newcf = malloc(sizeof(struct gconffile) * 2);
91 memset(newcf, 0, sizeof(struct gconffile) * 2);
93 for (i = 0; (*cf)[i].data || (*cf)[i].path; i++);
94 newcf = realloc(*cf, sizeof(struct gconffile) * (i + 2));
97 memmove(newcf + 1, newcf, sizeof(struct gconffile) * (i + 1));
98 memset(newcf, 0, sizeof(struct gconffile));
100 newcf[0].file = file;
101 newcf[0].path = desc;
108 debug(DBG_ERR, "malloc failed");
112 FILE *pushgconfpath(struct gconffile **cf, const char *path) {
115 f = fopen(path, "r");
116 return pushgconffile(cf, f, path);
119 FILE *pushgconfpaths(struct gconffile **cf, const char *cfgpath) {
123 char *path, *curfile = NULL, *dir;
125 /* if cfgpath is relative, make it relative to current config */
127 path = (char *)cfgpath;
129 /* dirname may modify its argument */
130 curfile = stringcopy((*cf)->path, 0);
132 debug(DBG_ERR, "malloc failed");
135 dir = dirname(curfile);
136 path = malloc(strlen(dir) + strlen(cfgpath) + 2);
138 debug(DBG_ERR, "malloc failed");
142 path[strlen(dir)] = '/';
143 strcpy(path + strlen(dir) + 1, cfgpath);
145 memset(&globbuf, 0, sizeof(glob_t));
146 if (glob(path, 0, NULL, &globbuf)) {
147 debug(DBG_WARN, "could not glob %s", path);
151 for (i = globbuf.gl_pathc - 1; i >= 0; i--) {
152 f = pushgconfpath(cf, globbuf.gl_pathv[i]);
166 int popgconf(struct gconffile **cf) {
171 for (i = 0; (*cf)[i].data || (*cf)[i].path; i++);
172 if (i && (*cf)[0].file) {
173 fclose((*cf)[0].file);
175 debug(DBG_DBG, "closing config file %s", (*cf)[0].path);
184 memmove(*cf, *cf + 1, sizeof(struct gconffile) * i);
188 void freegconfmstr(char **mstr) {
192 for (i = 0; mstr[i]; i++)
198 void freegconf(struct gconffile **cf) {
204 for (i = 0; (*cf)[i].data || (*cf)[i].path; i++) {
206 fclose((*cf)[i].file);
208 debug(DBG_DBG, "closing config file %s", (*cf)[i].path);
217 struct gconffile *openconfigfile(const char *file) {
218 struct gconffile *cf = NULL;
220 if (!pushgconfpath(&cf, file)) {
221 debug(DBG_ERR, "could not read config file %s\n%s", file, strerror(errno));
224 debug(DBG_DBG, "reading config file %s", file);
228 /* Parses config with following syntax:
231 * option-name = value
233 * option-name value {
234 * option-name [=] value
239 int getlinefromcf(struct gconffile *cf, char *line, const size_t size) {
246 return fgets(line, size, cf->file) ? 1 : 0;
251 for (i = pos; cf->data[i] && cf->data[i] != '\n'; i++);
252 if (cf->data[i] == '\n')
254 if (i - pos > size - 1)
256 memcpy(line, cf->data + pos, i - pos);
257 line[i - pos] = '\0';
264 int getconfigline(struct gconffile **cf, char *block, char **opt, char **val, int *conftype) {
273 if (!cf || !*cf || (!(*cf)->file && !(*cf)->data))
277 if (!getlinefromcf(*cf, line, 1024)) {
283 for (tcount = 0; tcount < 3; tcount++) {
284 s = strtokenquote(s, &tokens[tcount], " \t\r\n", "\"'", tcount ? NULL : "#");
286 debug(DBG_ERR, "Syntax error in line starting with: %s", line);
292 if (!tcount || **tokens == '#')
295 if (**tokens == '}') {
298 debug(DBG_ERR, "configuration error, found } with no matching {");
306 *opt = stringcopy(tokens[0], 0);
309 *val = stringcopy(tokens[1], 0);
312 *conftype = CONF_STR;
315 if (tokens[1][0] == '=' && tokens[1][1] == '\0') {
316 *opt = stringcopy(tokens[0], 0);
319 *val = stringcopy(tokens[2], 0);
322 *conftype = CONF_STR;
325 if (tokens[2][0] == '{' && tokens[2][1] == '\0') {
326 *opt = stringcopy(tokens[0], 0);
329 *val = stringcopy(tokens[1], 0);
332 *conftype = CONF_CBK;
338 debug(DBG_ERR, "configuration error in block %s, line starting with %s", block, tokens[0]);
340 debug(DBG_ERR, "configuration error, syntax error in line starting with %s", tokens[0]);
347 debug(DBG_ERR, "configuration error, option %s needs a non-empty value", *opt);
351 debug(DBG_ERR, "malloc failed");
360 uint8_t hexdigit2int(char d) {
361 if (d >= '0' && d <= '9')
363 if (d >= 'a' && d <= 'f')
365 if (d >= 'A' && d <= 'F')
370 void unhex(char *s) {
372 for (t = s; *t; s++) {
373 if (*t == '%' && isxdigit((int)t[1]) && isxdigit((int)t[2])) {
374 *s = 16 * hexdigit2int(t[1]) + hexdigit2int(t[2]);
382 typedef int (*t_fptr)(struct gconffile **, void *, char *, char *, char *);
384 /* returns 1 if ok, 0 on error */
385 /* caller must free returned values also on error */
386 int getgenericconfig(struct gconffile **cf, char *block, ...) {
388 char *opt = NULL, *val, *word, *optval, **str = NULL, ***mstr = NULL, **newmstr, *endptr;
390 long int *lint = NULL;
391 int type = 0, conftype = 0, n;
397 if (!getconfigline(cf, block, &opt, &val, &conftype))
402 if (conftype == CONF_STR && !strcasecmp(opt, "include")) {
403 if (!pushgconfpaths(cf, val)) {
404 debug(DBG_ERR, "failed to include config file %s", val);
412 while ((word = va_arg(ap, char *))) {
413 type = va_arg(ap, int);
416 str = va_arg(ap, char **);
421 mstr = va_arg(ap, char ***);
426 bln = va_arg(ap, uint8_t *);
431 lint = va_arg(ap, long int *);
436 cbk = va_arg(ap, t_fptr);
439 cbkarg = va_arg(ap, void *);
444 if (!strcasecmp(opt, word))
451 debug(DBG_ERR, "configuration error in block %s, unknown option %s", block, opt);
452 debug(DBG_ERR, "configuration error, unknown option %s", opt);
456 if (((type == CONF_STR || type == CONF_MSTR || type == CONF_BLN || type == CONF_LINT) && conftype != CONF_STR) ||
457 (type == CONF_CBK && conftype != CONF_CBK)) {
459 debug(DBG_ERR, "configuration error in block %s, wrong syntax for option %s", block, opt);
460 debug(DBG_ERR, "configuration error, wrong syntax for option %s", opt);
467 debug(DBG_ERR, "configuration error, option %s already set to %s", opt, *str);
475 for (n = 0; (*mstr)[n]; n++);
478 newmstr = realloc(*mstr, sizeof(char *) * (n + 2));
480 debug(DBG_ERR, "malloc failed");
485 newmstr[n + 1] = NULL;
489 if (!strcasecmp(val, "on"))
491 else if (!strcasecmp(val, "off"))
495 debug(DBG_ERR, "configuration error in block %s, value for option %s must be on or off, not %s", block, opt, val);
497 debug(DBG_ERR, "configuration error, value for option %s must be on or off, not %s", opt, val);
503 *lint = strtol(val, &endptr, 0);
504 if (*lint == LONG_MIN || *lint == LONG_MAX || !endptr || endptr == val || *endptr != '\0') {
506 debug(DBG_ERR, "configuration error in block %s, value for option %s must be an integer, not %s", block, opt, val);
508 debug(DBG_ERR, "configuration error, value for option %s must be an integer, not %s", opt, val);
513 optval = malloc(strlen(opt) + strlen(val) + 2);
515 debug(DBG_ERR, "malloc failed");
518 sprintf(optval, "%s %s", opt, val);
519 if (!cbk(cf, cbkarg, optval, opt, val)) {
530 debug(DBG_DBG, "getgenericconfig: block %s: %s = %s", block, opt, val);
532 debug(DBG_DBG, "getgenericconfig: %s = %s", opt, val);
533 if (type == CONF_BLN || type == CONF_LINT)
538 debug(DBG_ERR, "getgenericconfig: internal parameter error");
545 /* Local Variables: */
546 /* c-file-style: "stroustrup" */