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);
59 debug(DBG_DBG, "opened config file %s", path);
61 newcf = malloc(sizeof(struct gconffile) * 2);
63 debugx(1, DBG_ERR, "malloc failed");
67 for (i = 0; (*cf)[i].path; i++);
68 newcf = realloc(*cf, sizeof(struct gconffile) * (i + 2));
70 debugx(1, DBG_ERR, "malloc failed");
71 memmove(newcf + 1, newcf, sizeof(struct gconffile) * (i + 1));
74 newcf[0].path = stringcopy(path, 0);
79 FILE *popgconffile(struct gconffile **cf) {
84 for (i = 0; (*cf)[i].path; i++);
85 if (i && (*cf)[0].file) {
86 fclose((*cf)[0].file);
87 debug(DBG_DBG, "closing config file %s", (*cf)[0].path);
94 memmove(*cf, *cf + 1, sizeof(struct gconffile) * i);
98 /* Parses config with following syntax:
101 * option-name = value
103 * option-name value {
104 * option-name [=] value
108 void getgenericconfig(struct gconffile **cf, char *block, ...) {
111 /* initialise lots of stuff to avoid stupid compiler warnings */
112 char *tokens[3], *s, *opt = NULL, *val = NULL, *word, *optval, **str = NULL, ***mstr = NULL;
113 int type = 0, tcount, conftype = 0, n;
115 void (*cbk)(struct gconffile **, char *, char *, char *) = NULL;
117 if (!cf || !*cf || !(*cf)->file)
120 if (!fgets(line, 1024, (*cf)->file)) {
121 if (popgconffile(cf))
126 for (tcount = 0; tcount < 3; tcount++) {
127 s = strtokenquote(s, &tokens[tcount], " \t\r\n", "\"'", tcount ? NULL : "#");
129 debugx(1, DBG_ERR, "Syntax error in line starting with: %s", line);
133 if (!tcount || **tokens == '#')
136 if (**tokens == '}') {
139 debugx(1, DBG_ERR, "configuration error, found } with no matching {");
149 if (tokens[1][0] == '=' && tokens[1][1] == '\0') {
155 if (tokens[2][0] == '{' && tokens[2][1] == '\0') {
164 debugx(1, DBG_ERR, "configuration error in block %s, line starting with %s", block, tokens[0]);
165 debugx(1, DBG_ERR, "configuration error, syntax error in line starting with %s", tokens[0]);
169 debugx(1, DBG_ERR, "configuration error, option %s needs a non-empty value", opt);
171 if (conftype == CONF_STR && !strcasecmp(opt, "include")) {
172 pushgconffile(cf, val);
177 while ((word = va_arg(ap, char *))) {
178 type = va_arg(ap, int);
181 str = va_arg(ap, char **);
183 debugx(1, DBG_ERR, "getgeneralconfig: internal parameter error");
186 mstr = va_arg(ap, char ***);
188 debugx(1, DBG_ERR, "getgeneralconfig: internal parameter error");
191 cbk = va_arg(ap, void (*)(struct gconffile **, char *, char *, char *));
194 debugx(1, DBG_ERR, "getgeneralconfig: internal parameter error");
196 if (!strcasecmp(opt, word))
203 debugx(1, DBG_ERR, "configuration error in block %s, unknown option %s", block, opt);
204 debugx(1, DBG_ERR, "configuration error, unknown option %s", opt);
207 if (((type == CONF_STR || type == CONF_MSTR) && conftype != CONF_STR) ||
208 (type == CONF_CBK && conftype != CONF_CBK)) {
210 debugx(1, DBG_ERR, "configuration error in block %s, wrong syntax for option %s", block, opt);
211 debugx(1, DBG_ERR, "configuration error, wrong syntax for option %s", opt);
217 debug(DBG_DBG, "getgeneralconfig: block %s: %s = %s", block, opt, val);
219 debug(DBG_DBG, "getgeneralconfig: %s = %s", opt, val);
221 debugx(1, DBG_ERR, "configuration error, option %s already set to %s", opt, *str);
222 *str = stringcopy(val, 0);
224 debugx(1, DBG_ERR, "malloc failed");
228 debug(DBG_DBG, "getgeneralconfig: block %s: %s = %s", block, opt, val);
230 debug(DBG_DBG, "getgeneralconfig: %s = %s", opt, val);
232 for (n = 0; (*mstr)[n]; n++);
235 *mstr = realloc(*mstr, sizeof(char *) * (n + 2));
237 debugx(1, DBG_ERR, "malloc failed");
238 (*mstr)[n] = stringcopy(val, 0);
239 (*mstr)[n + 1] = NULL;
242 optval = malloc(strlen(opt) + strlen(val) + 2);
244 debugx(1, DBG_ERR, "malloc failed");
245 sprintf(optval, "%s %s", opt, val);
246 cbk(cf, optval, opt, val);
250 debugx(1, DBG_ERR, "getgeneralconfig: internal parameter error");