X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?p=elogind.git;a=blobdiff_plain;f=src%2Fconf-parser.c;h=aac64b29a3811c734ecc121f5b4ecaa7f6178ee7;hp=20f7641b13c5323e1b4600c2501f028ef3d7a1f1;hb=1a6f4df6c9437ed631080b7e006f666326063d36;hpb=01f78473b104d28db0fa813414092bc6358ae521 diff --git a/src/conf-parser.c b/src/conf-parser.c index 20f7641b1..aac64b29a 100644 --- a/src/conf-parser.c +++ b/src/conf-parser.c @@ -1,4 +1,4 @@ -/*-*- Mode: C; c-basic-offset: 8 -*-*/ +/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/ /*** This file is part of systemd. @@ -31,15 +31,13 @@ #include "strv.h" #include "log.h" -#define COMMENTS "#;\n" -#define LINE_MAX 4096 - /* Run the user supplied parser for an assignment */ static int next_assignment( const char *filename, unsigned line, const char *section, const ConfigItem *t, + bool relaxed, const char *lvalue, const char *rvalue, void *userdata) { @@ -49,7 +47,7 @@ static int next_assignment( assert(lvalue); assert(rvalue); - for (; t->parse; t++) { + for (; t->parse || t->lvalue; t++) { if (t->lvalue && !streq(lvalue, t->lvalue)) continue; @@ -60,18 +58,21 @@ static int next_assignment( if (t->section && !streq(section, t->section)) continue; + if (!t->parse) + return 0; + return t->parse(filename, line, section, lvalue, rvalue, t->data, userdata); } /* Warn about unknown non-extension fields. */ - if (!startswith(lvalue, "X-")) + if (!relaxed && !startswith(lvalue, "X-")) log_info("[%s:%u] Unknown lvalue '%s' in section '%s'. Ignoring.", filename, line, lvalue, strna(section)); return 0; } /* 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) { +static int parse_line(const char *filename, unsigned line, char **section, const char* const * sections, const ConfigItem *t, bool relaxed, char *l, void *userdata) { char *e; l = strstrip(l); @@ -89,7 +90,7 @@ static int parse_line(const char *filename, unsigned line, char **section, const if (!(fn = file_in_same_dir(filename, strstrip(l+9)))) return -ENOMEM; - r = config_parse(fn, NULL, sections, t, userdata); + r = config_parse(fn, NULL, sections, t, relaxed, userdata); free(fn); return r; @@ -110,11 +111,8 @@ static int parse_line(const char *filename, unsigned line, char **section, const if (!(n = strndup(l+1, k-2))) return -ENOMEM; - if (sections && !strv_contains((char**) sections, n)) { - log_error("[%s:%u] Unknown section '%s'.", filename, line, n); - free(n); - return -EBADMSG; - } + if (!relaxed && sections && !strv_contains((char**) sections, n)) + log_info("[%s:%u] Unknown section '%s'. Ignoring.", filename, line, n); free(*section); *section = n; @@ -122,6 +120,9 @@ static int parse_line(const char *filename, unsigned line, char **section, const return 0; } + if (sections && (!*section || !strv_contains((char**) sections, *section))) + return 0; + if (!(e = strchr(l, '='))) { log_error("[%s:%u] Missing '='.", filename, line); return -EBADMSG; @@ -130,15 +131,16 @@ static int parse_line(const char *filename, unsigned line, char **section, const *e = 0; e++; - return next_assignment(filename, line, *section, t, strstrip(l), strstrip(e), userdata); + return next_assignment(filename, line, *section, t, relaxed, strstrip(l), strstrip(e), userdata); } /* Go through the file and parse each line */ -int config_parse(const char *filename, FILE *f, const char* const * sections, const ConfigItem *t, void *userdata) { +int config_parse(const char *filename, FILE *f, const char* const * sections, const ConfigItem *t, bool relaxed, void *userdata) { unsigned line = 0; char *section = NULL; int r; bool ours = false; + char *continuation = NULL; assert(filename); assert(t); @@ -154,7 +156,8 @@ int config_parse(const char *filename, FILE *f, const char* const * sections, co } while (!feof(f)) { - char l[LINE_MAX]; + char l[LINE_MAX], *p, *c = NULL, *e; + bool escaped = false; if (!fgets(l, sizeof(l), f)) { if (feof(f)) @@ -165,7 +168,44 @@ int config_parse(const char *filename, FILE *f, const char* const * sections, co goto finish; } - if ((r = parse_line(filename, ++line, §ion, sections, t, l, userdata)) < 0) + truncate_nl(l); + + if (continuation) { + if (!(c = strappend(continuation, l))) { + r = -ENOMEM; + goto finish; + } + + free(continuation); + continuation = NULL; + p = c; + } else + p = l; + + for (e = p; *e; e++) { + if (escaped) + escaped = false; + else if (*e == '\\') + escaped = true; + } + + if (escaped) { + *(e-1) = ' '; + + if (c) + continuation = c; + else if (!(continuation = strdup(l))) { + r = -ENOMEM; + goto finish; + } + + continue; + } + + r = parse_line(filename, ++line, §ion, sections, t, relaxed, p, userdata); + free(c); + + if (r < 0) goto finish; } @@ -173,6 +213,7 @@ int config_parse(const char *filename, FILE *f, const char* const * sections, co finish: free(section); + free(continuation); if (f && ours) fclose(f); @@ -205,6 +246,31 @@ int config_parse_int( return 0; } +int config_parse_uint64( + const char *filename, + unsigned line, + const char *section, + const char *lvalue, + const char *rvalue, + void *data, + void *userdata) { + + uint64_t *u = data; + int r; + + assert(filename); + assert(lvalue); + assert(rvalue); + assert(data); + + if ((r = safe_atou64(rvalue, u)) < 0) { + log_error("[%s:%u] Failed to parse numeric value: %s", filename, line, rvalue); + return r; + } + + return 0; +} + int config_parse_unsigned( const char *filename, unsigned line, @@ -380,7 +446,7 @@ int config_parse_strv( k = 0; FOREACH_WORD_QUOTED(w, l, rvalue, state) - if (!(n[k++] = strndup(w, l))) + if (!(n[k++] = cunescape_length(w, l))) goto fail; n[k] = NULL; @@ -432,7 +498,7 @@ int config_parse_path_strv( n[k] = (*sv)[k]; FOREACH_WORD_QUOTED(w, l, rvalue, state) { - if (!(n[k] = strndup(w, l))) { + if (!(n[k] = cunescape_length(w, l))) { r = -ENOMEM; goto fail; }