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"
36 /* Run the user supplied parser for an assignment */
37 static int next_assignment(
52 for (; t->parse || t->lvalue; t++) {
54 if (t->lvalue && !streq(lvalue, t->lvalue))
57 if (t->section && !section)
60 if (t->section && !streq(section, t->section))
66 return t->parse(filename, line, section, lvalue, rvalue, t->data, userdata);
69 /* Warn about unknown non-extension fields. */
70 if (!relaxed && !startswith(lvalue, "X-"))
71 log_info("[%s:%u] Unknown lvalue '%s' in section '%s'. Ignoring.", filename, line, lvalue, strna(section));
76 /* Parse a variable assignment line */
77 static int parse_line(const char *filename, unsigned line, char **section, const char* const * sections, const ConfigItem *t, bool relaxed, char *l, void *userdata) {
85 if (strchr(COMMENTS, *l))
88 if (startswith(l, ".include ")) {
92 if (!(fn = file_in_same_dir(filename, strstrip(l+9))))
95 r = config_parse(fn, NULL, sections, t, relaxed, userdata);
109 log_error("[%s:%u] Invalid section header.", filename, line);
113 if (!(n = strndup(l+1, k-2)))
116 if (!relaxed && sections && !strv_contains((char**) sections, n))
117 log_info("[%s:%u] Unknown section '%s'. Ignoring.", filename, line, n);
125 if (sections && !strv_contains((char**) sections, *section))
128 if (!(e = strchr(l, '='))) {
129 log_error("[%s:%u] Missing '='.", filename, line);
136 return next_assignment(filename, line, *section, t, relaxed, strstrip(l), strstrip(e), userdata);
139 /* Go through the file and parse each line */
140 int config_parse(const char *filename, FILE *f, const char* const * sections, const ConfigItem *t, bool relaxed, void *userdata) {
142 char *section = NULL;
145 char *continuation = NULL;
151 if (!(f = fopen(filename, "re"))) {
153 log_error("Failed to open configuration file '%s': %s", filename, strerror(-r));
161 char l[LINE_MAX], *p, *c = NULL, *e;
162 bool escaped = false;
164 if (!fgets(l, sizeof(l), f)) {
169 log_error("Failed to read configuration file '%s': %s", filename, strerror(-r));
176 if (!(c = strappend(continuation, l))) {
187 for (e = p; *e; e++) {
199 else if (!(continuation = strdup(l))) {
207 r = parse_line(filename, ++line, §ion, sections, t, relaxed, p, userdata);
226 int config_parse_int(
227 const char *filename,
243 if ((r = safe_atoi(rvalue, i)) < 0) {
244 log_error("[%s:%u] Failed to parse numeric value: %s", filename, line, rvalue);
251 int config_parse_unsigned(
252 const char *filename,
268 if ((r = safe_atou(rvalue, u)) < 0) {
269 log_error("[%s:%u] Failed to parse numeric value: %s", filename, line, rvalue);
276 int config_parse_size(
277 const char *filename,
294 if ((r = safe_atou(rvalue, &u)) < 0) {
295 log_error("[%s:%u] Failed to parse numeric value: %s", filename, line, rvalue);
303 int config_parse_bool(
304 const char *filename,
320 if ((k = parse_boolean(rvalue)) < 0) {
321 log_error("[%s:%u] Failed to parse boolean value: %s", filename, line, rvalue);
329 int config_parse_string(
330 const char *filename,
347 if (!(n = strdup(rvalue)))
358 int config_parse_path(
359 const char *filename,
375 if (!path_is_absolute(rvalue)) {
376 log_error("[%s:%u] Not an absolute path: %s", filename, line, rvalue);
380 if (!(n = strdup(rvalue)))
383 path_kill_slashes(n);
391 int config_parse_strv(
392 const char *filename,
412 k = strv_length(*sv);
413 FOREACH_WORD_QUOTED(w, l, rvalue, state)
416 if (!(n = new(char*, k+1)))
420 for (k = 0; (*sv)[k]; k++)
425 FOREACH_WORD_QUOTED(w, l, rvalue, state)
426 if (!(n[k++] = cunescape_length(w, l)))
443 int config_parse_path_strv(
444 const char *filename,
465 k = strv_length(*sv);
466 FOREACH_WORD_QUOTED(w, l, rvalue, state)
469 if (!(n = new(char*, k+1)))
474 for (; (*sv)[k]; k++)
477 FOREACH_WORD_QUOTED(w, l, rvalue, state) {
478 if (!(n[k] = cunescape_length(w, l))) {
483 if (!path_is_absolute(n[k])) {
484 log_error("[%s:%u] Not an absolute path: %s", filename, line, rvalue);
489 path_kill_slashes(n[k]);