]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/conf-parser.c
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
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 /* Run the user supplied parser for an assignment */
35 static int next_assignment(
50 for (; t
->parse
|| t
->lvalue
; t
++) {
52 if (t
->lvalue
&& !streq(lvalue
, t
->lvalue
))
55 if (t
->section
&& !section
)
58 if (t
->section
&& !streq(section
, t
->section
))
64 return t
->parse(filename
, line
, section
, lvalue
, rvalue
, t
->data
, userdata
);
67 /* Warn about unknown non-extension fields. */
68 if (!relaxed
&& !startswith(lvalue
, "X-"))
69 log_info("[%s:%u] Unknown lvalue '%s' in section '%s'. Ignoring.", filename
, line
, lvalue
, strna(section
));
74 /* Parse a variable assignment line */
75 static int parse_line(const char *filename
, unsigned line
, char **section
, const char* const * sections
, const ConfigItem
*t
, bool relaxed
, char *l
, void *userdata
) {
83 if (strchr(COMMENTS
, *l
))
86 if (startswith(l
, ".include ")) {
90 if (!(fn
= file_in_same_dir(filename
, strstrip(l
+9))))
93 r
= config_parse(fn
, NULL
, sections
, t
, relaxed
, userdata
);
107 log_error("[%s:%u] Invalid section header.", filename
, line
);
111 if (!(n
= strndup(l
+1, k
-2)))
114 if (!relaxed
&& sections
&& !strv_contains((char**) sections
, n
))
115 log_info("[%s:%u] Unknown section '%s'. Ignoring.", filename
, line
, n
);
123 if (sections
&& (!*section
|| !strv_contains((char**) sections
, *section
)))
126 if (!(e
= strchr(l
, '='))) {
127 log_error("[%s:%u] Missing '='.", filename
, line
);
134 return next_assignment(filename
, line
, *section
, t
, relaxed
, strstrip(l
), strstrip(e
), userdata
);
137 /* Go through the file and parse each line */
138 int config_parse(const char *filename
, FILE *f
, const char* const * sections
, const ConfigItem
*t
, bool relaxed
, void *userdata
) {
140 char *section
= NULL
;
143 char *continuation
= NULL
;
149 if (!(f
= fopen(filename
, "re"))) {
151 log_error("Failed to open configuration file '%s': %s", filename
, strerror(-r
));
159 char l
[LINE_MAX
], *p
, *c
= NULL
, *e
;
160 bool escaped
= false;
162 if (!fgets(l
, sizeof(l
), f
)) {
167 log_error("Failed to read configuration file '%s': %s", filename
, strerror(-r
));
174 if (!(c
= strappend(continuation
, l
))) {
185 for (e
= p
; *e
; e
++) {
197 else if (!(continuation
= strdup(l
))) {
205 r
= parse_line(filename
, ++line
, §ion
, sections
, t
, relaxed
, p
, userdata
);
224 int config_parse_int(
225 const char *filename
,
241 if ((r
= safe_atoi(rvalue
, i
)) < 0) {
242 log_error("[%s:%u] Failed to parse numeric value: %s", filename
, line
, rvalue
);
249 int config_parse_uint64(
250 const char *filename
,
266 if ((r
= safe_atou64(rvalue
, u
)) < 0) {
267 log_error("[%s:%u] Failed to parse numeric value: %s", filename
, line
, rvalue
);
274 int config_parse_unsigned(
275 const char *filename
,
291 if ((r
= safe_atou(rvalue
, u
)) < 0) {
292 log_error("[%s:%u] Failed to parse numeric value: %s", filename
, line
, rvalue
);
299 int config_parse_size(
300 const char *filename
,
317 if ((r
= safe_atou(rvalue
, &u
)) < 0) {
318 log_error("[%s:%u] Failed to parse numeric value: %s", filename
, line
, rvalue
);
326 int config_parse_bool(
327 const char *filename
,
343 if ((k
= parse_boolean(rvalue
)) < 0) {
344 log_error("[%s:%u] Failed to parse boolean value: %s", filename
, line
, rvalue
);
352 int config_parse_string(
353 const char *filename
,
370 if (!(n
= strdup(rvalue
)))
381 int config_parse_path(
382 const char *filename
,
398 if (!path_is_absolute(rvalue
)) {
399 log_error("[%s:%u] Not an absolute path: %s", filename
, line
, rvalue
);
403 if (!(n
= strdup(rvalue
)))
406 path_kill_slashes(n
);
414 int config_parse_strv(
415 const char *filename
,
435 k
= strv_length(*sv
);
436 FOREACH_WORD_QUOTED(w
, l
, rvalue
, state
)
439 if (!(n
= new(char*, k
+1)))
443 for (k
= 0; (*sv
)[k
]; k
++)
448 FOREACH_WORD_QUOTED(w
, l
, rvalue
, state
)
449 if (!(n
[k
++] = cunescape_length(w
, l
)))
466 int config_parse_path_strv(
467 const char *filename
,
488 k
= strv_length(*sv
);
489 FOREACH_WORD_QUOTED(w
, l
, rvalue
, state
)
492 if (!(n
= new(char*, k
+1)))
497 for (; (*sv
)[k
]; k
++)
500 FOREACH_WORD_QUOTED(w
, l
, rvalue
, state
) {
501 if (!(n
[k
] = cunescape_length(w
, l
))) {
506 if (!path_is_absolute(n
[k
])) {
507 log_error("[%s:%u] Not an absolute path: %s", filename
, line
, rvalue
);
512 path_kill_slashes(n
[k
]);