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 FILE *pushgconffile(struct gconffile **cf, const char *path) {
51 struct gconffile *newcf;
56 debug(DBG_INFO, "could not read config file %s", path);
60 newcf = malloc(sizeof(struct gconffile) * 2);
62 debugx(1, DBG_ERR, "malloc failed");
65 for (i = 0; (*cf)[i].path; i++);
66 newcf = realloc(*cf, sizeof(struct gconffile) * (i + 2));
68 debugx(1, DBG_ERR, "malloc failed");
69 memmove(newcf + 1, newcf, sizeof(struct gconffile) * (i + 1));
72 newcf[0].path = stringcopy(path, 0);
77 FILE *popgconffile(struct gconffile **cf) {
82 for (i = 0; (*cf)[i].path; i++);
83 if (i && (*cf)[0].file)
84 fclose((*cf)[0].file);
90 memmove(*cf, *cf + 1, sizeof(struct gconffile) * i);
94 /* Parses config with following syntax:
100 * option-name [=] value
104 void getgenericconfig(FILE *f, char *block, ...) {
107 /* initialise lots of stuff to avoid stupid compiler warnings */
108 char *tokens[3], *s, *opt = NULL, *val = NULL, *word, *optval, **str = NULL, ***mstr = NULL;
109 int type = 0, tcount, conftype = 0, n;
110 void (*cbk)(FILE *, char *, char *, char *) = NULL;
112 while (fgets(line, 1024, f)) {
114 for (tcount = 0; tcount < 3; tcount++) {
115 s = strtokenquote(s, &tokens[tcount], " \t\r\n", "\"'", tcount ? NULL : "#");
117 debugx(1, DBG_ERR, "Syntax error in line starting with: %s", line);
121 if (!tcount || **tokens == '#')
124 if (**tokens == '}') {
127 debugx(1, DBG_ERR, "configuration error, found } with no matching {");
137 if (tokens[1][0] == '=' && tokens[1][1] == '\0') {
143 if (tokens[2][0] == '{' && tokens[2][1] == '\0') {
152 debugx(1, DBG_ERR, "configuration error in block %s, line starting with %s", block, tokens[0]);
153 debugx(1, DBG_ERR, "configuration error, syntax error in line starting with %s", tokens[0]);
157 debugx(1, DBG_ERR, "configuration error, option %s needs a non-empty value", opt);
160 while ((word = va_arg(ap, char *))) {
161 type = va_arg(ap, int);
164 str = va_arg(ap, char **);
166 debugx(1, DBG_ERR, "getgeneralconfig: internal parameter error");
169 mstr = va_arg(ap, char ***);
171 debugx(1, DBG_ERR, "getgeneralconfig: internal parameter error");
174 cbk = va_arg(ap, void (*)(FILE *, char *, char *, char *));
177 debugx(1, DBG_ERR, "getgeneralconfig: internal parameter error");
179 if (!strcasecmp(opt, word))
186 debugx(1, DBG_ERR, "configuration error in block %s, unknown option %s", block, opt);
187 debugx(1, DBG_ERR, "configuration error, unknown option %s", opt);
190 if (((type == CONF_STR || type == CONF_MSTR) && conftype != CONF_STR) ||
191 (type == CONF_CBK && conftype != CONF_CBK)) {
193 debugx(1, DBG_ERR, "configuration error in block %s, wrong syntax for option %s", block, opt);
194 debugx(1, DBG_ERR, "configuration error, wrong syntax for option %s", opt);
200 debug(DBG_DBG, "getgeneralconfig: block %s: %s = %s", block, opt, val);
202 debug(DBG_DBG, "getgeneralconfig: %s = %s", opt, val);
204 debugx(1, DBG_ERR, "configuration error, option %s already set to %s", opt, *str);
205 *str = stringcopy(val, 0);
207 debugx(1, DBG_ERR, "malloc failed");
211 debug(DBG_DBG, "getgeneralconfig: block %s: %s = %s", block, opt, val);
213 debug(DBG_DBG, "getgeneralconfig: %s = %s", opt, val);
215 for (n = 0; (*mstr)[n]; n++);
218 *mstr = realloc(*mstr, sizeof(char *) * (n + 2));
220 debugx(1, DBG_ERR, "malloc failed");
221 (*mstr)[n] = stringcopy(val, 0);
222 (*mstr)[n + 1] = NULL;
225 optval = malloc(strlen(opt) + strlen(val) + 2);
227 debugx(1, DBG_ERR, "malloc failed");
228 sprintf(optval, "%s %s", opt, val);
229 cbk(f, optval, opt, val);
233 debugx(1, DBG_ERR, "getgeneralconfig: internal parameter error");