#include "strv.h"
#include "log.h"
-#define WHITESPACE " \t\n"
#define COMMENTS "#;\n"
+#define NEWLINES "\n\r"
#define LINE_MAX 4096
/* Run the user supplied parser for an assignment */
return t->parse(filename, line, section, lvalue, rvalue, t->data, userdata);
}
- log_error("[%s:%u] Unknown lvalue '%s' in section '%s'.", filename, line, lvalue, strna(section));
- return -EBADMSG;
+ log_info("[%s:%u] Unknown lvalue '%s' in section '%s'. Ignoring.", filename, line, lvalue, strna(section));
+ return 0;
}
/* Returns non-zero when c is contained in s */
/* Parse a variable assignment line */
static int parse_line(const char *filename, unsigned line, char **section, const char* const * sections, const ConfigItem *t, char *l, void *userdata) {
- char *e, *c, *b;
+ char *e, *b, *c;
b = l+strspn(l, WHITESPACE);
- if ((c = strpbrk(b, COMMENTS)))
+ if ((c = strpbrk(b, NEWLINES)))
*c = 0;
if (!*b)
return 0;
+ if (strchr(COMMENTS, *b))
+ return 0;
+
if (startswith(b, ".include ")) {
char *path = NULL, *fn;
int r;
}
}
- r = config_parse(fn, sections, t, userdata);
+ r = config_parse(fn, NULL, sections, t, userdata);
free(path);
return r;
}
}
/* Go through the file and parse each line */
-int config_parse(const char *filename, const char* const * sections, const ConfigItem *t, void *userdata) {
+int config_parse(const char *filename, FILE *f, const char* const * sections, const ConfigItem *t, void *userdata) {
unsigned line = 0;
char *section = NULL;
- FILE *f;
int r;
assert(filename);
assert(t);
- if (!(f = fopen(filename, "re"))) {
- r = -errno;
- log_error("Failed to open configuration file '%s': %s", filename, strerror(-r));
- goto finish;
+ if (!f) {
+ if (!(f = fopen(filename, "re"))) {
+ r = -errno;
+ log_error("Failed to open configuration file '%s': %s", filename, strerror(-r));
+ goto finish;
+ }
}
while (!feof(f)) {
return 0;
}
+int config_parse_path(
+ const char *filename,
+ unsigned line,
+ const char *section,
+ const char *lvalue,
+ const char *rvalue,
+ void *data,
+ void *userdata) {
+
+ char **s = data;
+ char *n;
+
+ assert(filename);
+ assert(lvalue);
+ assert(rvalue);
+ assert(data);
+
+ if (*rvalue != '/') {
+ log_error("[%s:%u] Not an absolute path: %s", filename, line, rvalue);
+ return -EINVAL;
+ }
+
+ if (!(n = strdup(rvalue)))
+ return -ENOMEM;
+
+ free(*s);
+ *s = n;
+
+ return 0;
+}
int config_parse_strv(
const char *filename,
assert(data);
k = strv_length(*sv);
- FOREACH_WORD(w, &l, rvalue, state)
+ FOREACH_WORD_QUOTED(w, l, rvalue, state)
k++;
if (!(n = new(char*, k+1)))
for (k = 0; (*sv)[k]; k++)
n[k] = (*sv)[k];
- FOREACH_WORD(w, &l, rvalue, state)
+ FOREACH_WORD_QUOTED(w, l, rvalue, state)
if (!(n[k++] = strndup(w, l)))
goto fail;
fail:
for (; k > 0; k--)
free(n[k-1]);
+ free(n);
return -ENOMEM;
}