2 * Copyright (C) 2007, 2008 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>
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 FILE *pushgconffile(struct gconffile **cf, const char *path) {
53 struct gconffile *newcf;
58 debug(DBG_INFO, "could not read config file %s", path);
61 debug(DBG_DBG, "opened config file %s", path);
63 newcf = malloc(sizeof(struct gconffile) * 2);
65 debugx(1, DBG_ERR, "malloc failed");
69 for (i = 0; (*cf)[i].path; i++);
70 newcf = realloc(*cf, sizeof(struct gconffile) * (i + 2));
72 debugx(1, DBG_ERR, "malloc failed");
73 memmove(newcf + 1, newcf, sizeof(struct gconffile) * (i + 1));
76 newcf[0].path = stringcopy(path, 0);
81 FILE *pushgconffiles(struct gconffile **cf, const char *cfgpath) {
85 char *path, *curfile = NULL, *dir;
87 /* if cfgpath is relative, make it relative to current config */
89 path = (char *)cfgpath;
91 /* dirname may modify its argument */
92 curfile = stringcopy((*cf)->path, 0);
94 debugx(1, DBG_ERR, "malloc failed");
95 dir = dirname(curfile);
96 path = malloc(strlen(dir) + strlen(cfgpath) + 2);
98 debugx(1, DBG_ERR, "malloc failed");
100 path[strlen(dir)] = '/';
101 strcpy(path + strlen(dir) + 1, cfgpath);
103 memset(&globbuf, 0, sizeof(glob_t));
104 if (glob(path, 0, NULL, &globbuf)) {
105 debug(DBG_INFO, "could not glob %s", path);
108 for (i = globbuf.gl_pathc - 1; i >= 0; i--) {
109 f = pushgconffile(cf, globbuf.gl_pathv[i]);
122 FILE *popgconffile(struct gconffile **cf) {
127 for (i = 0; (*cf)[i].path; i++);
128 if (i && (*cf)[0].file) {
129 fclose((*cf)[0].file);
130 debug(DBG_DBG, "closing config file %s", (*cf)[0].path);
137 memmove(*cf, *cf + 1, sizeof(struct gconffile) * i);
138 return (*cf)[0].file;
141 /* Parses config with following syntax:
144 * option-name = value
146 * option-name value {
147 * option-name [=] value
151 void getgenericconfig(struct gconffile **cf, char *block, ...) {
154 /* initialise lots of stuff to avoid stupid compiler warnings */
155 char *tokens[3], *s, *opt = NULL, *val = NULL, *word, *optval, **str = NULL, ***mstr = NULL;
156 int type = 0, tcount, conftype = 0, n;
157 void (*cbk)(struct gconffile **, char *, char *, char *) = NULL;
159 if (!cf || !*cf || !(*cf)->file)
162 if (!fgets(line, 1024, (*cf)->file)) {
163 if (popgconffile(cf))
168 for (tcount = 0; tcount < 3; tcount++) {
169 s = strtokenquote(s, &tokens[tcount], " \t\r\n", "\"'", tcount ? NULL : "#");
171 debugx(1, DBG_ERR, "Syntax error in line starting with: %s", line);
175 if (!tcount || **tokens == '#')
178 if (**tokens == '}') {
181 debugx(1, DBG_ERR, "configuration error, found } with no matching {");
191 if (tokens[1][0] == '=' && tokens[1][1] == '\0') {
197 if (tokens[2][0] == '{' && tokens[2][1] == '\0') {
206 debugx(1, DBG_ERR, "configuration error in block %s, line starting with %s", block, tokens[0]);
207 debugx(1, DBG_ERR, "configuration error, syntax error in line starting with %s", tokens[0]);
211 debugx(1, DBG_ERR, "configuration error, option %s needs a non-empty value", opt);
213 if (conftype == CONF_STR && !strcasecmp(opt, "include")) {
214 if (!pushgconffiles(cf, val))
215 debugx(1, DBG_ERR, "failed to include config file %s", val);
220 while ((word = va_arg(ap, char *))) {
221 type = va_arg(ap, int);
224 str = va_arg(ap, char **);
226 debugx(1, DBG_ERR, "getgenericconfig: internal parameter error");
229 mstr = va_arg(ap, char ***);
231 debugx(1, DBG_ERR, "getgenericconfig: internal parameter error");
234 cbk = va_arg(ap, void (*)(struct gconffile **, char *, char *, char *));
237 debugx(1, DBG_ERR, "getgenericconfig: internal parameter error");
239 if (!strcasecmp(opt, word))
246 debugx(1, DBG_ERR, "configuration error in block %s, unknown option %s", block, opt);
247 debugx(1, DBG_ERR, "configuration error, unknown option %s", opt);
250 if (((type == CONF_STR || type == CONF_MSTR) && conftype != CONF_STR) ||
251 (type == CONF_CBK && conftype != CONF_CBK)) {
253 debugx(1, DBG_ERR, "configuration error in block %s, wrong syntax for option %s", block, opt);
254 debugx(1, DBG_ERR, "configuration error, wrong syntax for option %s", opt);
260 debug(DBG_DBG, "getgenericconfig: block %s: %s = %s", block, opt, val);
262 debug(DBG_DBG, "getgenericconfig: %s = %s", opt, val);
264 debugx(1, DBG_ERR, "configuration error, option %s already set to %s", opt, *str);
265 *str = stringcopy(val, 0);
267 debugx(1, DBG_ERR, "malloc failed");
271 debug(DBG_DBG, "getgenericconfig: block %s: %s = %s", block, opt, val);
273 debug(DBG_DBG, "getgenericconfig: %s = %s", opt, val);
275 for (n = 0; (*mstr)[n]; n++);
278 *mstr = realloc(*mstr, sizeof(char *) * (n + 2));
280 debugx(1, DBG_ERR, "malloc failed");
281 (*mstr)[n] = stringcopy(val, 0);
282 (*mstr)[n + 1] = NULL;
285 optval = malloc(strlen(opt) + strlen(val) + 2);
287 debugx(1, DBG_ERR, "malloc failed");
288 sprintf(optval, "%s %s", opt, val);
289 cbk(cf, optval, opt, val);
293 debugx(1, DBG_ERR, "getgenericconfig: internal parameter error");