1 /*-*- Mode: C; c-basic-offset: 8 -*-*/
9 #include "conf-parser.h"
15 #define COMMENTS "#;\n"
16 #define NEWLINES "\n\r"
19 /* Run the user supplied parser for an assignment */
20 static int next_assignment(
34 for (; t->parse; t++) {
36 if (t->lvalue && !streq(lvalue, t->lvalue))
39 if (t->section && !section)
42 if (t->section && !streq(section, t->section))
45 return t->parse(filename, line, section, lvalue, rvalue, t->data, userdata);
48 log_info("[%s:%u] Unknown lvalue '%s' in section '%s'. Ignoring.", filename, line, lvalue, strna(section));
52 /* Returns non-zero when c is contained in s */
53 static int in_string(char c, const char *s) {
63 /* Remove all whitepsapce from the beginning and the end of *s. *s may
65 static char *strip(char *s) {
66 char *b = s+strspn(s, WHITESPACE);
70 if (!in_string(*e, WHITESPACE))
79 /* Parse a variable assignment line */
80 static int parse_line(const char *filename, unsigned line, char **section, const char* const * sections, const ConfigItem *t, char *l, void *userdata) {
83 b = l+strspn(l, WHITESPACE);
85 if ((c = strpbrk(b, NEWLINES)))
91 if (strchr(COMMENTS, *b))
94 if (startswith(b, ".include ")) {
95 char *path = NULL, *fn;
99 if (!is_path_absolute(fn)) {
102 if ((k = strrchr(filename, '/'))) {
105 if (!(dir = strndup(filename, k-filename)))
108 if (asprintf(&path, "%s/%s", dir, fn) < 0)
116 r = config_parse(fn, NULL, sections, t, userdata);
129 log_error("[%s:%u] Invalid section header.", filename, line);
133 if (!(n = strndup(b+1, k-2)))
137 const char * const * i;
139 STRV_FOREACH(i, sections)
157 if (!(e = strchr(b, '='))) {
158 log_error("[%s:%u] Missing '='.", filename, line);
165 return next_assignment(filename, line, *section, t, strip(b), strip(e), userdata);
168 /* Go through the file and parse each line */
169 int config_parse(const char *filename, FILE *f, const char* const * sections, const ConfigItem *t, void *userdata) {
171 char *section = NULL;
178 if (!(f = fopen(filename, "re"))) {
180 log_error("Failed to open configuration file '%s': %s", filename, strerror(-r));
188 if (!fgets(l, sizeof(l), f)) {
193 log_error("Failed to read configuration file '%s': %s", filename, strerror(-r));
197 if ((r = parse_line(filename, ++line, §ion, sections, t, l, userdata)) < 0)
212 int config_parse_int(
213 const char *filename,
229 if ((r = safe_atoi(rvalue, i)) < 0) {
230 log_error("[%s:%u] Failed to parse numeric value: %s", filename, line, rvalue);
237 int config_parse_unsigned(
238 const char *filename,
254 if ((r = safe_atou(rvalue, u)) < 0) {
255 log_error("[%s:%u] Failed to parse numeric value: %s", filename, line, rvalue);
262 int config_parse_size(
263 const char *filename,
280 if ((r = safe_atou(rvalue, &u)) < 0) {
281 log_error("[%s:%u] Failed to parse numeric value: %s", filename, line, rvalue);
289 int config_parse_bool(
290 const char *filename,
306 if ((k = parse_boolean(rvalue)) < 0) {
307 log_error("[%s:%u] Failed to parse boolean value: %s", filename, line, rvalue);
315 int config_parse_string(
316 const char *filename,
333 if (!(n = strdup(rvalue)))
344 int config_parse_path(
345 const char *filename,
361 if (*rvalue != '/') {
362 log_error("[%s:%u] Not an absolute path: %s", filename, line, rvalue);
366 if (!(n = strdup(rvalue)))
375 int config_parse_strv(
376 const char *filename,
396 k = strv_length(*sv);
397 FOREACH_WORD_QUOTED(w, l, rvalue, state)
400 if (!(n = new(char*, k+1)))
403 for (k = 0; (*sv)[k]; k++)
405 FOREACH_WORD_QUOTED(w, l, rvalue, state)
406 if (!(n[k++] = strndup(w, l)))