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(
53 for (; t->parse; t++) {
55 if (t->lvalue && !streq(lvalue, t->lvalue))
58 if (t->section && !section)
61 if (t->section && !streq(section, t->section))
67 return t->parse(filename, line, section, lvalue, rvalue, t->data, userdata);
70 /* Warn about unknown non-extension fields. */
71 if (!relaxed && !startswith(lvalue, "X-"))
72 log_info("[%s:%u] Unknown lvalue '%s' in section '%s'. Ignoring.", filename, line, lvalue, strna(section));
77 /* Parse a variable assignment line */
78 static int parse_line(const char *filename, unsigned line, char **section, const char* const * sections, const ConfigItem *t, bool relaxed, char *l, void *userdata) {
86 if (strchr(COMMENTS, *l))
89 if (startswith(l, ".include ")) {
93 if (!(fn = file_in_same_dir(filename, strstrip(l+9))))
96 r = config_parse(fn, NULL, sections, t, relaxed, userdata);
110 log_error("[%s:%u] Invalid section header.", filename, line);
114 if (!(n = strndup(l+1, k-2)))
117 if (!relaxed && sections && !strv_contains((char**) sections, n))
118 log_info("[%s:%u] Unknown section '%s'. Ignoring.", filename, line, n);
126 if (sections && !strv_contains((char**) sections, *section))
129 if (!(e = strchr(l, '='))) {
130 log_error("[%s:%u] Missing '='.", filename, line);
137 return next_assignment(filename, line, *section, t, relaxed, strstrip(l), strstrip(e), userdata);
140 /* Go through the file and parse each line */
141 int config_parse(const char *filename, FILE *f, const char* const * sections, const ConfigItem *t, bool relaxed, void *userdata) {
143 char *section = NULL;
151 if (!(f = fopen(filename, "re"))) {
153 log_error("Failed to open configuration file '%s': %s", filename, strerror(-r));
163 if (!fgets(l, sizeof(l), f)) {
168 log_error("Failed to read configuration file '%s': %s", filename, strerror(-r));
172 if ((r = parse_line(filename, ++line, §ion, sections, t, relaxed, l, userdata)) < 0)
187 int config_parse_int(
188 const char *filename,
204 if ((r = safe_atoi(rvalue, i)) < 0) {
205 log_error("[%s:%u] Failed to parse numeric value: %s", filename, line, rvalue);
212 int config_parse_unsigned(
213 const char *filename,
229 if ((r = safe_atou(rvalue, u)) < 0) {
230 log_error("[%s:%u] Failed to parse numeric value: %s", filename, line, rvalue);
237 int config_parse_size(
238 const char *filename,
255 if ((r = safe_atou(rvalue, &u)) < 0) {
256 log_error("[%s:%u] Failed to parse numeric value: %s", filename, line, rvalue);
264 int config_parse_bool(
265 const char *filename,
281 if ((k = parse_boolean(rvalue)) < 0) {
282 log_error("[%s:%u] Failed to parse boolean value: %s", filename, line, rvalue);
290 int config_parse_string(
291 const char *filename,
308 if (!(n = strdup(rvalue)))
319 int config_parse_path(
320 const char *filename,
336 if (!path_is_absolute(rvalue)) {
337 log_error("[%s:%u] Not an absolute path: %s", filename, line, rvalue);
341 if (!(n = strdup(rvalue)))
344 path_kill_slashes(n);
352 int config_parse_strv(
353 const char *filename,
373 k = strv_length(*sv);
374 FOREACH_WORD_QUOTED(w, l, rvalue, state)
377 if (!(n = new(char*, k+1)))
381 for (k = 0; (*sv)[k]; k++)
386 FOREACH_WORD_QUOTED(w, l, rvalue, state)
387 if (!(n[k++] = strndup(w, l)))
404 int config_parse_path_strv(
405 const char *filename,
426 k = strv_length(*sv);
427 FOREACH_WORD_QUOTED(w, l, rvalue, state)
430 if (!(n = new(char*, k+1)))
435 for (; (*sv)[k]; k++)
438 FOREACH_WORD_QUOTED(w, l, rvalue, state) {
439 if (!(n[k] = strndup(w, l))) {
444 if (!path_is_absolute(n[k])) {
445 log_error("[%s:%u] Not an absolute path: %s", filename, line, rvalue);
450 path_kill_slashes(n[k]);