X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?p=elogind.git;a=blobdiff_plain;f=conf-parser.c;h=5da55874085c2cc0604320d41eca77ca38e9135f;hp=3a7da7960ca84f5f5418f95603d27beed4702808;hb=16801e901e87f4ca0c83ea8c09df1ab3d344129e;hpb=42f4e3c4413ad35e3815f25211fee95d775488a7 diff --git a/conf-parser.c b/conf-parser.c index 3a7da7960..5da558740 100644 --- a/conf-parser.c +++ b/conf-parser.c @@ -1,5 +1,24 @@ /*-*- Mode: C; c-basic-offset: 8 -*-*/ +/*** + This file is part of systemd. + + Copyright 2010 Lennart Poettering + + systemd is free software; you can redistribute it and/or modify it + under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + systemd is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + General Public License for more details. + + You should have received a copy of the GNU General Public License + along with systemd; If not, see . +***/ + #include #include #include @@ -10,9 +29,10 @@ #include "util.h" #include "macro.h" #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 */ @@ -44,104 +64,53 @@ static int next_assignment( return t->parse(filename, line, section, lvalue, rvalue, t->data, userdata); } - fprintf(stderr, "[%s:%u] Unknown lvalue '%s' in section '%s'.\n", filename, line, lvalue, strna(section)); - return -EBADMSG; -} - -/* Returns non-zero when c is contained in s */ -static int in_string(char c, const char *s) { - assert(s); - - for (; *s; s++) - if (*s == c) - return 1; - + log_info("[%s:%u] Unknown lvalue '%s' in section '%s'. Ignoring.", filename, line, lvalue, strna(section)); return 0; } -/* Remove all whitepsapce from the beginning and the end of *s. *s may - * be modified. */ -static char *strip(char *s) { - char *b = s+strspn(s, WHITESPACE); - char *e, *l = NULL; - - for (e = b; *e; e++) - if (!in_string(*e, WHITESPACE)) - l = e; - - if (l) - *(l+1) = 0; - - return b; -} - /* 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 = l+strspn(l, WHITESPACE); + l = strstrip(l); - if ((c = strpbrk(b, COMMENTS))) - *c = 0; + if (!*l) + return 0; - if (!*b) + if (strchr(COMMENTS, *l)) return 0; - if (startswith(b, ".include ")) { - char *path = NULL, *fn; + if (startswith(l, ".include ")) { + char *fn; int r; - fn = strip(b+9); - if (!is_path_absolute(fn)) { - const char *k; - - if ((k = strrchr(filename, '/'))) { - char *dir; - - if (!(dir = strndup(filename, k-filename))) - return -ENOMEM; - - if (asprintf(&path, "%s/%s", dir, fn) < 0) - return -errno; + if (!(fn = file_in_same_dir(filename, strstrip(l+9)))) + return -ENOMEM; - fn = path; - free(dir); - } - } + r = config_parse(fn, NULL, sections, t, userdata); + free(fn); - r = config_parse(fn, sections, t, userdata); - free(path); return r; } - if (*b == '[') { + if (*l == '[') { size_t k; char *n; - k = strlen(b); + k = strlen(l); assert(k > 0); - if (b[k-1] != ']') { - fprintf(stderr, "[%s:%u] Invalid section header.\n", filename, line); + if (l[k-1] != ']') { + log_error("[%s:%u] Invalid section header.", filename, line); return -EBADMSG; } - if (!(n = strndup(b+1, k-2))) + if (!(n = strndup(l+1, k-2))) return -ENOMEM; - if (sections) { - const char * const * i; - bool good = false; - STRV_FOREACH(i, sections) - if (streq(*i, n)) { - good = true; - break; - } - - if (!good) { - free(n); - return -EBADMSG; - } + if (sections && !strv_contains((char**) sections, n)) { + free(n); + return -EBADMSG; } free(*section); @@ -150,31 +119,35 @@ static int parse_line(const char *filename, unsigned line, char **section, const return 0; } - if (!(e = strchr(b, '='))) { - fprintf(stderr, "[%s:%u] Missing '='.\n", filename, line); + if (!(e = strchr(l, '='))) { + log_error("[%s:%u] Missing '='.", filename, line); return -EBADMSG; } *e = 0; e++; - return next_assignment(filename, line, *section, t, strip(b), strip(e), userdata); + return next_assignment(filename, line, *section, t, strstrip(l), strstrip(e), userdata); } /* 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; + bool ours = false; assert(filename); assert(t); - if (!(f = fopen(filename, "re"))) { - r = -errno; - fprintf(stderr, "Failed to open configuration file '%s': %s\n", 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; + } + + ours = true; } while (!feof(f)) { @@ -185,7 +158,7 @@ int config_parse(const char *filename, const char* const * sections, const Confi break; r = -errno; - fprintf(stderr, "Failed to read configuration file '%s': %s\n", filename, strerror(-r)); + log_error("Failed to read configuration file '%s': %s", filename, strerror(-r)); goto finish; } @@ -198,7 +171,7 @@ int config_parse(const char *filename, const char* const * sections, const Confi finish: free(section); - if (f) + if (f && ours) fclose(f); return r; @@ -222,7 +195,7 @@ int config_parse_int( assert(data); if ((r = safe_atoi(rvalue, i)) < 0) { - fprintf(stderr, "[%s:%u] Failed to parse numeric value: %s\n", filename, line, rvalue); + log_error("[%s:%u] Failed to parse numeric value: %s", filename, line, rvalue); return r; } @@ -247,7 +220,7 @@ int config_parse_unsigned( assert(data); if ((r = safe_atou(rvalue, u)) < 0) { - fprintf(stderr, "[%s:%u] Failed to parse numeric value: %s\n", filename, line, rvalue); + log_error("[%s:%u] Failed to parse numeric value: %s", filename, line, rvalue); return r; } @@ -273,7 +246,7 @@ int config_parse_size( assert(data); if ((r = safe_atou(rvalue, &u)) < 0) { - fprintf(stderr, "[%s:%u] Failed to parse numeric value: %s\n", filename, line, rvalue); + log_error("[%s:%u] Failed to parse numeric value: %s", filename, line, rvalue); return r; } @@ -299,7 +272,7 @@ int config_parse_bool( assert(data); if ((k = parse_boolean(rvalue)) < 0) { - fprintf(stderr, "[%s:%u] Failed to parse boolean value: %s\n", filename, line, rvalue); + log_error("[%s:%u] Failed to parse boolean value: %s", filename, line, rvalue); return k; } @@ -336,6 +309,36 @@ int config_parse_string( 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, @@ -359,15 +362,19 @@ int config_parse_strv( 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))) return -ENOMEM; - for (k = 0; (*sv)[k]; k++) - n[k] = (*sv)[k]; - FOREACH_WORD(w, &l, rvalue, state) + if (*sv) + for (k = 0; (*sv)[k]; k++) + n[k] = (*sv)[k]; + else + k = 0; + + FOREACH_WORD_QUOTED(w, l, rvalue, state) if (!(n[k++] = strndup(w, l))) goto fail; @@ -380,6 +387,7 @@ int config_parse_strv( fail: for (; k > 0; k--) free(n[k-1]); + free(n); return -ENOMEM; }