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 log_info("[%s:%u] Unknown lvalue '%s' in section '%s'. Ignoring.", filename, line, lvalue, strna(section));
70 /* Parse a variable assignment line */
71 static int parse_line(const char *filename, unsigned line, char **section, const char* const * sections, const ConfigItem *t, char *l, void *userdata) {
79 if (strchr(COMMENTS, *l))
82 if (startswith(l, ".include ")) {
86 if (!(fn = file_in_same_dir(filename, strstrip(l+9))))
89 r = config_parse(fn, NULL, sections, t, userdata);
103 log_error("[%s:%u] Invalid section header.", filename, line);
107 if (!(n = strndup(l+1, k-2)))
110 if (sections && !strv_contains((char**) sections, n)) {
121 if (!(e = strchr(l, '='))) {
122 log_error("[%s:%u] Missing '='.", filename, line);
129 return next_assignment(filename, line, *section, t, strstrip(l), strstrip(e), userdata);
132 /* Go through the file and parse each line */
133 int config_parse(const char *filename, FILE *f, const char* const * sections, const ConfigItem *t, void *userdata) {
135 char *section = NULL;
143 if (!(f = fopen(filename, "re"))) {
145 log_error("Failed to open configuration file '%s': %s", filename, strerror(-r));
155 if (!fgets(l, sizeof(l), f)) {
160 log_error("Failed to read configuration file '%s': %s", filename, strerror(-r));
164 if ((r = parse_line(filename, ++line, §ion, sections, t, l, userdata)) < 0)
179 int config_parse_int(
180 const char *filename,
196 if ((r = safe_atoi(rvalue, i)) < 0) {
197 log_error("[%s:%u] Failed to parse numeric value: %s", filename, line, rvalue);
204 int config_parse_unsigned(
205 const char *filename,
221 if ((r = safe_atou(rvalue, u)) < 0) {
222 log_error("[%s:%u] Failed to parse numeric value: %s", filename, line, rvalue);
229 int config_parse_size(
230 const char *filename,
247 if ((r = safe_atou(rvalue, &u)) < 0) {
248 log_error("[%s:%u] Failed to parse numeric value: %s", filename, line, rvalue);
256 int config_parse_bool(
257 const char *filename,
273 if ((k = parse_boolean(rvalue)) < 0) {
274 log_error("[%s:%u] Failed to parse boolean value: %s", filename, line, rvalue);
282 int config_parse_string(
283 const char *filename,
300 if (!(n = strdup(rvalue)))
311 int config_parse_path(
312 const char *filename,
328 if (*rvalue != '/') {
329 log_error("[%s:%u] Not an absolute path: %s", filename, line, rvalue);
333 if (!(n = strdup(rvalue)))
342 int config_parse_strv(
343 const char *filename,
363 k = strv_length(*sv);
364 FOREACH_WORD_QUOTED(w, l, rvalue, state)
367 if (!(n = new(char*, k+1)))
371 for (k = 0; (*sv)[k]; k++)
376 FOREACH_WORD_QUOTED(w, l, rvalue, state)
377 if (!(n[k++] = strndup(w, l)))