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 /* Warn about unknown non-extension fields. */
67 if (!startswith(lvalue, "X-"))
68 log_info("[%s:%u] Unknown lvalue '%s' in section '%s'. Ignoring.", filename, line, lvalue, strna(section));
73 /* Parse a variable assignment line */
74 static int parse_line(const char *filename, unsigned line, char **section, const char* const * sections, const ConfigItem *t, char *l, void *userdata) {
82 if (strchr(COMMENTS, *l))
85 if (startswith(l, ".include ")) {
89 if (!(fn = file_in_same_dir(filename, strstrip(l+9))))
92 r = config_parse(fn, NULL, sections, t, userdata);
106 log_error("[%s:%u] Invalid section header.", filename, line);
110 if (!(n = strndup(l+1, k-2)))
113 if (sections && !strv_contains((char**) sections, n)) {
124 if (!(e = strchr(l, '='))) {
125 log_error("[%s:%u] Missing '='.", filename, line);
132 return next_assignment(filename, line, *section, t, strstrip(l), strstrip(e), userdata);
135 /* Go through the file and parse each line */
136 int config_parse(const char *filename, FILE *f, const char* const * sections, const ConfigItem *t, void *userdata) {
138 char *section = NULL;
146 if (!(f = fopen(filename, "re"))) {
148 log_error("Failed to open configuration file '%s': %s", filename, strerror(-r));
158 if (!fgets(l, sizeof(l), f)) {
163 log_error("Failed to read configuration file '%s': %s", filename, strerror(-r));
167 if ((r = parse_line(filename, ++line, §ion, sections, t, l, userdata)) < 0)
182 int config_parse_int(
183 const char *filename,
199 if ((r = safe_atoi(rvalue, i)) < 0) {
200 log_error("[%s:%u] Failed to parse numeric value: %s", filename, line, rvalue);
207 int config_parse_unsigned(
208 const char *filename,
224 if ((r = safe_atou(rvalue, u)) < 0) {
225 log_error("[%s:%u] Failed to parse numeric value: %s", filename, line, rvalue);
232 int config_parse_size(
233 const char *filename,
250 if ((r = safe_atou(rvalue, &u)) < 0) {
251 log_error("[%s:%u] Failed to parse numeric value: %s", filename, line, rvalue);
259 int config_parse_bool(
260 const char *filename,
276 if ((k = parse_boolean(rvalue)) < 0) {
277 log_error("[%s:%u] Failed to parse boolean value: %s", filename, line, rvalue);
285 int config_parse_string(
286 const char *filename,
303 if (!(n = strdup(rvalue)))
314 int config_parse_path(
315 const char *filename,
331 if (*rvalue != '/') {
332 log_error("[%s:%u] Not an absolute path: %s", filename, line, rvalue);
336 if (!(n = strdup(rvalue)))
345 int config_parse_strv(
346 const char *filename,
366 k = strv_length(*sv);
367 FOREACH_WORD_QUOTED(w, l, rvalue, state)
370 if (!(n = new(char*, k+1)))
374 for (k = 0; (*sv)[k]; k++)
379 FOREACH_WORD_QUOTED(w, l, rvalue, state)
380 if (!(n[k++] = strndup(w, l)))