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"
35 #define NEWLINES "\n\r"
38 /* Run the user supplied parser for an assignment */
39 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))
64 return t->parse(filename, line, section, lvalue, rvalue, t->data, userdata);
67 log_info("[%s:%u] Unknown lvalue '%s' in section '%s'. Ignoring.", filename, line, lvalue, strna(section));
71 /* Parse a variable assignment line */
72 static int parse_line(const char *filename, unsigned line, char **section, const char* const * sections, const ConfigItem *t, char *l, void *userdata) {
80 if (strchr(COMMENTS, *l))
83 if (startswith(l, ".include ")) {
87 if (!(fn = file_in_same_dir(filename, strstrip(l+9))))
90 r = config_parse(fn, NULL, sections, t, userdata);
104 log_error("[%s:%u] Invalid section header.", filename, line);
108 if (!(n = strndup(l+1, k-2)))
111 if (sections && !strv_contains((char**) sections, n)) {
122 if (!(e = strchr(l, '='))) {
123 log_error("[%s:%u] Missing '='.", filename, line);
130 return next_assignment(filename, line, *section, t, strstrip(l), strstrip(e), userdata);
133 /* Go through the file and parse each line */
134 int config_parse(const char *filename, FILE *f, const char* const * sections, const ConfigItem *t, void *userdata) {
136 char *section = NULL;
144 if (!(f = fopen(filename, "re"))) {
146 log_error("Failed to open configuration file '%s': %s", filename, strerror(-r));
156 if (!fgets(l, sizeof(l), f)) {
161 log_error("Failed to read configuration file '%s': %s", filename, strerror(-r));
165 if ((r = parse_line(filename, ++line, §ion, sections, t, l, userdata)) < 0)
180 int config_parse_int(
181 const char *filename,
197 if ((r = safe_atoi(rvalue, i)) < 0) {
198 log_error("[%s:%u] Failed to parse numeric value: %s", filename, line, rvalue);
205 int config_parse_unsigned(
206 const char *filename,
222 if ((r = safe_atou(rvalue, u)) < 0) {
223 log_error("[%s:%u] Failed to parse numeric value: %s", filename, line, rvalue);
230 int config_parse_size(
231 const char *filename,
248 if ((r = safe_atou(rvalue, &u)) < 0) {
249 log_error("[%s:%u] Failed to parse numeric value: %s", filename, line, rvalue);
257 int config_parse_bool(
258 const char *filename,
274 if ((k = parse_boolean(rvalue)) < 0) {
275 log_error("[%s:%u] Failed to parse boolean value: %s", filename, line, rvalue);
283 int config_parse_string(
284 const char *filename,
301 if (!(n = strdup(rvalue)))
312 int config_parse_path(
313 const char *filename,
329 if (*rvalue != '/') {
330 log_error("[%s:%u] Not an absolute path: %s", filename, line, rvalue);
334 if (!(n = strdup(rvalue)))
343 int config_parse_strv(
344 const char *filename,
364 k = strv_length(*sv);
365 FOREACH_WORD_QUOTED(w, l, rvalue, state)
368 if (!(n = new(char*, k+1)))
372 for (k = 0; (*sv)[k]; k++)
377 FOREACH_WORD_QUOTED(w, l, rvalue, state)
378 if (!(n[k++] = strndup(w, l)))