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.
14 #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 FILE *pushgconffile(struct gconffile **cf, const char *path) {
52 struct gconffile *newcf;
57 debug(DBG_INFO, "could not read config file %s", path);
60 debug(DBG_DBG, "opened config file %s", path);
62 newcf = malloc(sizeof(struct gconffile) * 2);
64 debugx(1, DBG_ERR, "malloc failed");
68 for (i = 0; (*cf)[i].path; i++);
69 newcf = realloc(*cf, sizeof(struct gconffile) * (i + 2));
71 debugx(1, DBG_ERR, "malloc failed");
72 memmove(newcf + 1, newcf, sizeof(struct gconffile) * (i + 1));
75 newcf[0].path = stringcopy(path, 0);
80 FILE *pushgconffiles(struct gconffile **cf, const char *path) {
85 memset(&globbuf, 0, sizeof(glob_t));
86 if (glob(path, 0, NULL, &globbuf)) {
87 debug(DBG_INFO, "could not glob %s", path);
90 for (i = globbuf.gl_pathc - 1; i >= 0; i--) {
91 f = pushgconffile(cf, globbuf.gl_pathv[i]);
99 FILE *popgconffile(struct gconffile **cf) {
104 for (i = 0; (*cf)[i].path; i++);
105 if (i && (*cf)[0].file) {
106 fclose((*cf)[0].file);
107 debug(DBG_DBG, "closing config file %s", (*cf)[0].path);
114 memmove(*cf, *cf + 1, sizeof(struct gconffile) * i);
115 return (*cf)[0].file;
118 /* Parses config with following syntax:
121 * option-name = value
123 * option-name value {
124 * option-name [=] value
128 void getgenericconfig(struct gconffile **cf, char *block, ...) {
131 /* initialise lots of stuff to avoid stupid compiler warnings */
132 char *tokens[3], *s, *opt = NULL, *val = NULL, *word, *optval, **str = NULL, ***mstr = NULL;
133 int type = 0, tcount, conftype = 0, n;
134 void (*cbk)(struct gconffile **, char *, char *, char *) = NULL;
136 if (!cf || !*cf || !(*cf)->file)
139 if (!fgets(line, 1024, (*cf)->file)) {
140 if (popgconffile(cf))
145 for (tcount = 0; tcount < 3; tcount++) {
146 s = strtokenquote(s, &tokens[tcount], " \t\r\n", "\"'", tcount ? NULL : "#");
148 debugx(1, DBG_ERR, "Syntax error in line starting with: %s", line);
152 if (!tcount || **tokens == '#')
155 if (**tokens == '}') {
158 debugx(1, DBG_ERR, "configuration error, found } with no matching {");
168 if (tokens[1][0] == '=' && tokens[1][1] == '\0') {
174 if (tokens[2][0] == '{' && tokens[2][1] == '\0') {
183 debugx(1, DBG_ERR, "configuration error in block %s, line starting with %s", block, tokens[0]);
184 debugx(1, DBG_ERR, "configuration error, syntax error in line starting with %s", tokens[0]);
188 debugx(1, DBG_ERR, "configuration error, option %s needs a non-empty value", opt);
190 if (conftype == CONF_STR && !strcasecmp(opt, "include")) {
191 if (!pushgconffiles(cf, val))
192 debugx(1, DBG_ERR, "failed to include config file %s", val);
197 while ((word = va_arg(ap, char *))) {
198 type = va_arg(ap, int);
201 str = va_arg(ap, char **);
203 debugx(1, DBG_ERR, "getgeneralconfig: internal parameter error");
206 mstr = va_arg(ap, char ***);
208 debugx(1, DBG_ERR, "getgeneralconfig: internal parameter error");
211 cbk = va_arg(ap, void (*)(struct gconffile **, char *, char *, char *));
214 debugx(1, DBG_ERR, "getgeneralconfig: internal parameter error");
216 if (!strcasecmp(opt, word))
223 debugx(1, DBG_ERR, "configuration error in block %s, unknown option %s", block, opt);
224 debugx(1, DBG_ERR, "configuration error, unknown option %s", opt);
227 if (((type == CONF_STR || type == CONF_MSTR) && conftype != CONF_STR) ||
228 (type == CONF_CBK && conftype != CONF_CBK)) {
230 debugx(1, DBG_ERR, "configuration error in block %s, wrong syntax for option %s", block, opt);
231 debugx(1, DBG_ERR, "configuration error, wrong syntax for option %s", opt);
237 debug(DBG_DBG, "getgeneralconfig: block %s: %s = %s", block, opt, val);
239 debug(DBG_DBG, "getgeneralconfig: %s = %s", opt, val);
241 debugx(1, DBG_ERR, "configuration error, option %s already set to %s", opt, *str);
242 *str = stringcopy(val, 0);
244 debugx(1, DBG_ERR, "malloc failed");
248 debug(DBG_DBG, "getgeneralconfig: block %s: %s = %s", block, opt, val);
250 debug(DBG_DBG, "getgeneralconfig: %s = %s", opt, val);
252 for (n = 0; (*mstr)[n]; n++);
255 *mstr = realloc(*mstr, sizeof(char *) * (n + 2));
257 debugx(1, DBG_ERR, "malloc failed");
258 (*mstr)[n] = stringcopy(val, 0);
259 (*mstr)[n + 1] = NULL;
262 optval = malloc(strlen(opt) + strlen(val) + 2);
264 debugx(1, DBG_ERR, "malloc failed");
265 sprintf(optval, "%s %s", opt, val);
266 cbk(cf, optval, opt, val);
270 debugx(1, DBG_ERR, "getgeneralconfig: internal parameter error");