1 /*-*- Mode: C; c-basic-offset: 8 -*-*/
4 This file is part of systemd.
6 Copyright 2010 Lennart Poettering
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
28 #include "conf-parser.h"
34 #define COMMENTS "#;\n"
37 /* Run the user supplied parser for an assignment */
38 static int next_assignment(
52 for (; t->parse; t++) {
54 if (t->lvalue && !streq(lvalue, t->lvalue))
57 if (t->section && !section)
60 if (t->section && !streq(section, t->section))
63 return t->parse(filename, line, section, lvalue, rvalue, t->data, userdata);
66 /* Warn about unknown non-extension fields. */
67 if (!startswith(lvalue, "X-"))
68 log_info("[%s:%u] Unknown lvalue '%s' in section '%s'. Ignoring.", filename, line, lvalue, strna(section));
73 /* Parse a variable assignment line */
74 static int parse_line(const char *filename, unsigned line, char **section, const char* const * sections, const ConfigItem *t, char *l, void *userdata) {
82 if (strchr(COMMENTS, *l))
85 if (startswith(l, ".include ")) {
89 if (!(fn = file_in_same_dir(filename, strstrip(l+9))))
92 r = config_parse(fn, NULL, sections, t, userdata);
106 log_error("[%s:%u] Invalid section header.", filename, line);
110 if (!(n = strndup(l+1, k-2)))
113 if (sections && !strv_contains((char**) sections, n)) {
114 log_error("[%s:%u] Unknown section '%s'.", filename, line, n);
125 if (!(e = strchr(l, '='))) {
126 log_error("[%s:%u] Missing '='.", filename, line);
133 return next_assignment(filename, line, *section, t, strstrip(l), strstrip(e), userdata);
136 /* Go through the file and parse each line */
137 int config_parse(const char *filename, FILE *f, const char* const * sections, const ConfigItem *t, void *userdata) {
139 char *section = NULL;
147 if (!(f = fopen(filename, "re"))) {
149 log_error("Failed to open configuration file '%s': %s", filename, strerror(-r));
159 if (!fgets(l, sizeof(l), f)) {
164 log_error("Failed to read configuration file '%s': %s", filename, strerror(-r));
168 if ((r = parse_line(filename, ++line, §ion, sections, t, l, userdata)) < 0)
183 int config_parse_int(
184 const char *filename,
200 if ((r = safe_atoi(rvalue, i)) < 0) {
201 log_error("[%s:%u] Failed to parse numeric value: %s", filename, line, rvalue);
208 int config_parse_unsigned(
209 const char *filename,
225 if ((r = safe_atou(rvalue, u)) < 0) {
226 log_error("[%s:%u] Failed to parse numeric value: %s", filename, line, rvalue);
233 int config_parse_size(
234 const char *filename,
251 if ((r = safe_atou(rvalue, &u)) < 0) {
252 log_error("[%s:%u] Failed to parse numeric value: %s", filename, line, rvalue);
260 int config_parse_bool(
261 const char *filename,
277 if ((k = parse_boolean(rvalue)) < 0) {
278 log_error("[%s:%u] Failed to parse boolean value: %s", filename, line, rvalue);
286 int config_parse_string(
287 const char *filename,
304 if (!(n = strdup(rvalue)))
315 int config_parse_path(
316 const char *filename,
332 if (!path_is_absolute(rvalue)) {
333 log_error("[%s:%u] Not an absolute path: %s", filename, line, rvalue);
337 if (!(n = strdup(rvalue)))
346 int config_parse_strv(
347 const char *filename,
367 k = strv_length(*sv);
368 FOREACH_WORD_QUOTED(w, l, rvalue, state)
371 if (!(n = new(char*, k+1)))
375 for (k = 0; (*sv)[k]; k++)
380 FOREACH_WORD_QUOTED(w, l, rvalue, state)
381 if (!(n[k++] = strndup(w, l)))
398 int config_parse_path_strv(
399 const char *filename,
420 k = strv_length(*sv);
421 FOREACH_WORD_QUOTED(w, l, rvalue, state)
424 if (!(n = new(char*, k+1)))
429 for (; (*sv)[k]; k++)
432 FOREACH_WORD_QUOTED(w, l, rvalue, state) {
433 if (!(n[k] = strndup(w, l))) {
438 if (!path_is_absolute(n[k])) {
439 log_error("[%s:%u] Not an absolute path: %s", filename, line, rvalue);