1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
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 /* Run the user supplied parser for an assignment */
35 static int next_assignment(
50 for (; t->parse || t->lvalue; t++) {
52 if (t->lvalue && !streq(lvalue, t->lvalue))
55 if (t->section && !section)
58 if (t->section && !streq(section, t->section))
64 return t->parse(filename, line, section, lvalue, rvalue, t->data, userdata);
67 /* Warn about unknown non-extension fields. */
68 if (!relaxed && !startswith(lvalue, "X-"))
69 log_info("[%s:%u] Unknown lvalue '%s' in section '%s'. Ignoring.", filename, line, lvalue, strna(section));
74 /* Parse a variable assignment line */
75 static int parse_line(const char *filename, unsigned line, char **section, const char* const * sections, const ConfigItem *t, bool relaxed, char *l, void *userdata) {
83 if (strchr(COMMENTS, *l))
86 if (startswith(l, ".include ")) {
90 if (!(fn = file_in_same_dir(filename, strstrip(l+9))))
93 r = config_parse(fn, NULL, sections, t, relaxed, userdata);
107 log_error("[%s:%u] Invalid section header.", filename, line);
111 if (!(n = strndup(l+1, k-2)))
114 if (!relaxed && sections && !strv_contains((char**) sections, n))
115 log_info("[%s:%u] Unknown section '%s'. Ignoring.", filename, line, n);
123 if (sections && (!*section || !strv_contains((char**) sections, *section)))
126 if (!(e = strchr(l, '='))) {
127 log_error("[%s:%u] Missing '='.", filename, line);
134 return next_assignment(filename, line, *section, t, relaxed, strstrip(l), strstrip(e), userdata);
137 /* Go through the file and parse each line */
138 int config_parse(const char *filename, FILE *f, const char* const * sections, const ConfigItem *t, bool relaxed, void *userdata) {
140 char *section = NULL;
143 char *continuation = NULL;
149 if (!(f = fopen(filename, "re"))) {
151 log_error("Failed to open configuration file '%s': %s", filename, strerror(-r));
159 char l[LINE_MAX], *p, *c = NULL, *e;
160 bool escaped = false;
162 if (!fgets(l, sizeof(l), f)) {
167 log_error("Failed to read configuration file '%s': %s", filename, strerror(-r));
174 if (!(c = strappend(continuation, l))) {
185 for (e = p; *e; e++) {
197 else if (!(continuation = strdup(l))) {
205 r = parse_line(filename, ++line, §ion, sections, t, relaxed, p, userdata);
224 int config_parse_int(
225 const char *filename,
241 if ((r = safe_atoi(rvalue, i)) < 0) {
242 log_error("[%s:%u] Failed to parse numeric value: %s", filename, line, rvalue);
249 int config_parse_unsigned(
250 const char *filename,
266 if ((r = safe_atou(rvalue, u)) < 0) {
267 log_error("[%s:%u] Failed to parse numeric value: %s", filename, line, rvalue);
274 int config_parse_size(
275 const char *filename,
292 if ((r = safe_atou(rvalue, &u)) < 0) {
293 log_error("[%s:%u] Failed to parse numeric value: %s", filename, line, rvalue);
301 int config_parse_bool(
302 const char *filename,
318 if ((k = parse_boolean(rvalue)) < 0) {
319 log_error("[%s:%u] Failed to parse boolean value: %s", filename, line, rvalue);
327 int config_parse_string(
328 const char *filename,
345 if (!(n = strdup(rvalue)))
356 int config_parse_path(
357 const char *filename,
373 if (!path_is_absolute(rvalue)) {
374 log_error("[%s:%u] Not an absolute path: %s", filename, line, rvalue);
378 if (!(n = strdup(rvalue)))
381 path_kill_slashes(n);
389 int config_parse_strv(
390 const char *filename,
410 k = strv_length(*sv);
411 FOREACH_WORD_QUOTED(w, l, rvalue, state)
414 if (!(n = new(char*, k+1)))
418 for (k = 0; (*sv)[k]; k++)
423 FOREACH_WORD_QUOTED(w, l, rvalue, state)
424 if (!(n[k++] = cunescape_length(w, l)))
441 int config_parse_path_strv(
442 const char *filename,
463 k = strv_length(*sv);
464 FOREACH_WORD_QUOTED(w, l, rvalue, state)
467 if (!(n = new(char*, k+1)))
472 for (; (*sv)[k]; k++)
475 FOREACH_WORD_QUOTED(w, l, rvalue, state) {
476 if (!(n[k] = cunescape_length(w, l))) {
481 if (!path_is_absolute(n[k])) {
482 log_error("[%s:%u] Not an absolute path: %s", filename, line, rvalue);
487 path_kill_slashes(n[k]);