X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=blobdiff_plain;f=conf-parser.c;h=6994211b1095ace6e014320b46971ae1d2a4b6c2;hb=07b0b134d3076fe223d6e15959b6081a74b56792;hp=0388a24bcc315a5cc556986371ef6a7f5e8cbf23;hpb=1ea86b1803ed7bbb55b31bd9cb780a638a20b2a6;p=elogind.git
diff --git a/conf-parser.c b/conf-parser.c
index 0388a24bc..6994211b1 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
@@ -13,7 +32,6 @@
#include "log.h"
#define COMMENTS "#;\n"
-#define NEWLINES "\n\r"
#define LINE_MAX 4096
/* Run the user supplied parser for an assignment */
@@ -45,107 +63,57 @@ static int next_assignment(
return t->parse(filename, line, section, lvalue, rvalue, t->data, userdata);
}
- 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 */
-static int in_string(char c, const char *s) {
- assert(s);
-
- for (; *s; s++)
- if (*s == c)
- return 1;
+ /* Warn about unknown non-extension fields. */
+ if (!startswith(lvalue, "X-"))
+ 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, *b, *c;
-
- b = l+strspn(l, WHITESPACE);
+ char *e;
- if ((c = strpbrk(b, NEWLINES)))
- *c = 0;
+ l = strstrip(l);
- if (!*b)
+ if (!*l)
return 0;
- if (strchr(COMMENTS, *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;
-
- fn = path;
- free(dir);
- }
- }
+ if (!(fn = file_in_same_dir(filename, strstrip(l+9))))
+ return -ENOMEM;
r = config_parse(fn, NULL, sections, t, userdata);
- free(path);
+ free(fn);
+
return r;
}
- if (*b == '[') {
+ if (*l == '[') {
size_t k;
char *n;
- k = strlen(b);
+ k = strlen(l);
assert(k > 0);
- if (b[k-1] != ']') {
+ 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)) {
+ log_error("[%s:%u] Unknown section '%s'.", filename, line, n);
+ free(n);
+ return -EBADMSG;
}
free(*section);
@@ -154,7 +122,7 @@ static int parse_line(const char *filename, unsigned line, char **section, const
return 0;
}
- if (!(e = strchr(b, '='))) {
+ if (!(e = strchr(l, '='))) {
log_error("[%s:%u] Missing '='.", filename, line);
return -EBADMSG;
}
@@ -162,7 +130,7 @@ static int parse_line(const char *filename, unsigned line, char **section, const
*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 */
@@ -170,6 +138,7 @@ int config_parse(const char *filename, FILE *f, const char* const * sections, co
unsigned line = 0;
char *section = NULL;
int r;
+ bool ours = false;
assert(filename);
assert(t);
@@ -180,6 +149,8 @@ int config_parse(const char *filename, FILE *f, const char* const * sections, co
log_error("Failed to open configuration file '%s': %s", filename, strerror(-r));
goto finish;
}
+
+ ours = true;
}
while (!feof(f)) {
@@ -203,7 +174,7 @@ int config_parse(const char *filename, FILE *f, const char* const * sections, co
finish:
free(section);
- if (f)
+ if (f && ours)
fclose(f);
return r;
@@ -358,7 +329,7 @@ int config_parse_path(
assert(rvalue);
assert(data);
- if (*rvalue != '/') {
+ if (!path_is_absolute(rvalue)) {
log_error("[%s:%u] Not an absolute path: %s", filename, line, rvalue);
return -EINVAL;
}
@@ -400,8 +371,12 @@ int config_parse_strv(
if (!(n = new(char*, k+1)))
return -ENOMEM;
- for (k = 0; (*sv)[k]; k++)
- n[k] = (*sv)[k];
+ 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;
@@ -419,3 +394,67 @@ fail:
return -ENOMEM;
}
+
+int config_parse_path_strv(
+ const char *filename,
+ unsigned line,
+ const char *section,
+ const char *lvalue,
+ const char *rvalue,
+ void *data,
+ void *userdata) {
+
+ char*** sv = data;
+ char **n;
+ char *w;
+ unsigned k;
+ size_t l;
+ char *state;
+ int r;
+
+ assert(filename);
+ assert(lvalue);
+ assert(rvalue);
+ assert(data);
+
+ k = strv_length(*sv);
+ FOREACH_WORD_QUOTED(w, l, rvalue, state)
+ k++;
+
+ if (!(n = new(char*, k+1)))
+ return -ENOMEM;
+
+ k = 0;
+ if (*sv)
+ for (; (*sv)[k]; k++)
+ n[k] = (*sv)[k];
+
+ FOREACH_WORD_QUOTED(w, l, rvalue, state) {
+ if (!(n[k] = strndup(w, l))) {
+ r = -ENOMEM;
+ goto fail;
+ }
+
+ if (!path_is_absolute(n[k])) {
+ log_error("[%s:%u] Not an absolute path: %s", filename, line, rvalue);
+ r = -EINVAL;
+ goto fail;
+ }
+
+ k++;
+ }
+
+ n[k] = NULL;
+ free(*sv);
+ *sv = n;
+
+ return 0;
+
+fail:
+ free(n[k]);
+ for (; k > 0; k--)
+ free(n[k-1]);
+ free(n);
+
+ return r;
+}