]>
git.ipfire.org Git - people/ms/systemd.git/blob - conf-parser.c
1 /*-*- Mode: C; c-basic-offset: 8 -*-*/
9 #include "conf-parser.h"
15 #define COMMENTS "#;\n"
18 /* Run the user supplied parser for an assignment */
19 static int next_assignment(
33 for (; t
->parse
; t
++) {
35 if (t
->lvalue
&& !streq(lvalue
, t
->lvalue
))
38 if (t
->section
&& !section
)
41 if (t
->section
&& !streq(section
, t
->section
))
44 return t
->parse(filename
, line
, section
, lvalue
, rvalue
, t
->data
, userdata
);
47 log_info("[%s:%u] Unknown lvalue '%s' in section '%s'. Ignoring.", filename
, line
, lvalue
, strna(section
));
51 /* Returns non-zero when c is contained in s */
52 static int in_string(char c
, const char *s
) {
62 /* Remove all whitepsapce from the beginning and the end of *s. *s may
64 static char *strip(char *s
) {
65 char *b
= s
+strspn(s
, WHITESPACE
);
69 if (!in_string(*e
, WHITESPACE
))
78 /* Parse a variable assignment line */
79 static int parse_line(const char *filename
, unsigned line
, char **section
, const char* const * sections
, const ConfigItem
*t
, char *l
, void *userdata
) {
82 b
= l
+strspn(l
, WHITESPACE
);
84 if ((c
= strpbrk(b
, COMMENTS
)))
90 if (startswith(b
, ".include ")) {
91 char *path
= NULL
, *fn
;
95 if (!is_path_absolute(fn
)) {
98 if ((k
= strrchr(filename
, '/'))) {
101 if (!(dir
= strndup(filename
, k
-filename
)))
104 if (asprintf(&path
, "%s/%s", dir
, fn
) < 0)
112 r
= config_parse(fn
, NULL
, sections
, t
, userdata
);
125 log_error("[%s:%u] Invalid section header.", filename
, line
);
129 if (!(n
= strndup(b
+1, k
-2)))
133 const char * const * i
;
135 STRV_FOREACH(i
, sections
)
153 if (!(e
= strchr(b
, '='))) {
154 log_error("[%s:%u] Missing '='.", filename
, line
);
161 return next_assignment(filename
, line
, *section
, t
, strip(b
), strip(e
), userdata
);
164 /* Go through the file and parse each line */
165 int config_parse(const char *filename
, FILE *f
, const char* const * sections
, const ConfigItem
*t
, void *userdata
) {
167 char *section
= NULL
;
174 if (!(f
= fopen(filename
, "re"))) {
176 log_error("Failed to open configuration file '%s': %s", filename
, strerror(-r
));
184 if (!fgets(l
, sizeof(l
), f
)) {
189 log_error("Failed to read configuration file '%s': %s", filename
, strerror(-r
));
193 if ((r
= parse_line(filename
, ++line
, §ion
, sections
, t
, l
, userdata
)) < 0)
208 int config_parse_int(
209 const char *filename
,
225 if ((r
= safe_atoi(rvalue
, i
)) < 0) {
226 log_error("[%s:%u] Failed to parse numeric value: %s", filename
, line
, rvalue
);
233 int config_parse_unsigned(
234 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
);
258 int config_parse_size(
259 const char *filename
,
276 if ((r
= safe_atou(rvalue
, &u
)) < 0) {
277 log_error("[%s:%u] Failed to parse numeric value: %s", filename
, line
, rvalue
);
285 int config_parse_bool(
286 const char *filename
,
302 if ((k
= parse_boolean(rvalue
)) < 0) {
303 log_error("[%s:%u] Failed to parse boolean value: %s", filename
, line
, rvalue
);
311 int config_parse_string(
312 const char *filename
,
329 if (!(n
= strdup(rvalue
)))
340 int config_parse_path(
341 const char *filename
,
357 if (*rvalue
!= '/') {
358 log_error("[%s:%u] Not an absolute path: %s", filename
, line
, rvalue
);
362 if (!(n
= strdup(rvalue
)))
371 int config_parse_strv(
372 const char *filename
,
392 k
= strv_length(*sv
);
393 FOREACH_WORD_QUOTED(w
, l
, rvalue
, state
)
396 if (!(n
= new(char*, k
+1)))
399 for (k
= 0; (*sv
)[k
]; k
++)
401 FOREACH_WORD_QUOTED(w
, l
, rvalue
, state
)
402 if (!(n
[k
++] = strndup(w
, l
)))