2 * Copyright (C) 2007 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.
13 #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 /* Parses config with following syntax:
55 * option-name [=] value
59 void getgenericconfig(FILE *f, char *block, ...) {
62 /* initialise lots of stuff to avoid stupid compiler warnings */
63 char *tokens[3], *s, *opt = NULL, *val = NULL, *word, *optval, **str = NULL, ***mstr = NULL;
64 int type = 0, tcount, conftype = 0, n;
65 void (*cbk)(FILE *, char *, char *, char *) = NULL;
67 while (fgets(line, 1024, f)) {
69 for (tcount = 0; tcount < 3; tcount++) {
70 s = strtokenquote(s, &tokens[tcount], " \t\r\n", "\"'", tcount ? NULL : "#");
72 debugx(1, DBG_ERR, "Syntax error in line starting with: %s", line);
76 if (!tcount || **tokens == '#')
79 if (**tokens == '}') {
82 debugx(1, DBG_ERR, "configuration error, found } with no matching {");
92 if (tokens[1][0] == '=' && tokens[1][1] == '\0') {
98 if (tokens[2][0] == '{' && tokens[2][1] == '\0') {
107 debugx(1, DBG_ERR, "configuration error in block %s, line starting with %s", block, tokens[0]);
108 debugx(1, DBG_ERR, "configuration error, syntax error in line starting with %s", tokens[0]);
112 debugx(1, DBG_ERR, "configuration error, option %s needs a non-empty value", opt);
115 while ((word = va_arg(ap, char *))) {
116 type = va_arg(ap, int);
119 str = va_arg(ap, char **);
121 debugx(1, DBG_ERR, "getgeneralconfig: internal parameter error");
124 mstr = va_arg(ap, char ***);
126 debugx(1, DBG_ERR, "getgeneralconfig: internal parameter error");
129 cbk = va_arg(ap, void (*)(FILE *, char *, char *, char *));
132 debugx(1, DBG_ERR, "getgeneralconfig: internal parameter error");
134 if (!strcasecmp(opt, word))
141 debugx(1, DBG_ERR, "configuration error in block %s, unknown option %s", block, opt);
142 debugx(1, DBG_ERR, "configuration error, unknown option %s", opt);
145 if (((type == CONF_STR || type == CONF_MSTR) && conftype != CONF_STR) ||
146 (type == CONF_CBK && conftype != CONF_CBK)) {
148 debugx(1, DBG_ERR, "configuration error in block %s, wrong syntax for option %s", block, opt);
149 debugx(1, DBG_ERR, "configuration error, wrong syntax for option %s", opt);
155 debug(DBG_DBG, "getgeneralconfig: block %s: %s = %s", block, opt, val);
157 debug(DBG_DBG, "getgeneralconfig: %s = %s", opt, val);
159 debugx(1, DBG_ERR, "configuration error, option %s already set to %s", opt, *str);
160 *str = stringcopy(val, 0);
162 debugx(1, DBG_ERR, "malloc failed");
166 debug(DBG_DBG, "getgeneralconfig: block %s: %s = %s", block, opt, val);
168 debug(DBG_DBG, "getgeneralconfig: %s = %s", opt, val);
170 for (n = 0; (*mstr)[n]; n++);
173 *mstr = realloc(*mstr, sizeof(char *) * (n + 2));
175 debugx(1, DBG_ERR, "malloc failed");
176 (*mstr)[n] = stringcopy(val, 0);
177 (*mstr)[n + 1] = NULL;
180 optval = malloc(strlen(opt) + strlen(val) + 2);
182 debugx(1, DBG_ERR, "malloc failed");
183 sprintf(optval, "%s %s", opt, val);
184 cbk(f, optval, opt, val);
188 debugx(1, DBG_ERR, "getgeneralconfig: internal parameter error");