]>
git.ipfire.org Git - thirdparty/systemd.git/blob - conf-parser.c
1 /*-*- Mode: C; c-basic-offset: 8 -*-*/
9 #include "conf-parser.h"
13 #define WHITESPACE " \t\n"
14 #define COMMENTS "#;\n"
17 /* Run the user supplied parser for an assignment */
18 static int next_assignment(
32 for (; t
->parse
; t
++) {
34 if (t
->lvalue
&& !streq(lvalue
, t
->lvalue
))
37 if (t
->section
&& !section
)
40 if (t
->section
&& !streq(section
, t
->section
))
43 return t
->parse(filename
, line
, section
, lvalue
, rvalue
, t
->data
, userdata
);
46 fprintf(stderr
, "[%s:%u] Unknown lvalue '%s' in section '%s'.", filename
, line
, lvalue
, strna(section
));
50 /* Returns non-zero when c is contained in s */
51 static int in_string(char c
, const char *s
) {
61 /* Remove all whitepsapce from the beginning and the end of *s. *s may
63 static char *strip(char *s
) {
64 char *b
= s
+strspn(s
, WHITESPACE
);
68 if (!in_string(*e
, WHITESPACE
))
77 /* Parse a variable assignment line */
78 static int parse_line(const char *filename
, unsigned line
, char **section
, const ConfigItem
*t
, char *l
, void *userdata
) {
81 b
= l
+strspn(l
, WHITESPACE
);
83 if ((c
= strpbrk(b
, COMMENTS
)))
89 if (startswith(b
, ".include ")) {
90 char *path
= NULL
, *fn
;
94 if (!is_path_absolute(fn
)) {
97 if ((k
= strrchr(filename
, '/'))) {
100 if (!(dir
= strndup(filename
, k
-filename
)))
103 if (asprintf(&path
, "%s/%s", dir
, fn
) < 0)
111 r
= config_parse(fn
, t
, userdata
);
124 fprintf(stderr
, "[%s:%u] Invalid section header.", filename
, line
);
128 if (!(n
= strndup(b
+1, k
-2)))
137 if (!(e
= strchr(b
, '='))) {
138 fprintf(stderr
, "[%s:%u] Missing '='.", filename
, line
);
145 return next_assignment(filename
, line
, *section
, t
, strip(b
), strip(e
), userdata
);
148 /* Go through the file and parse each line */
149 int config_parse(const char *filename
, const ConfigItem
*t
, void *userdata
) {
151 char *section
= NULL
;
158 if (!(f
= fopen(filename
, "re"))) {
160 fprintf(stderr
, "Failed to open configuration file '%s': %s", filename
, strerror(-r
));
167 if (!fgets(l
, sizeof(l
), f
)) {
172 fprintf(stderr
, "Failed to read configuration file '%s': %s", filename
, strerror(-r
));
176 if ((r
= parse_line(filename
, ++line
, §ion
, t
, l
, userdata
)) < 0)
191 int config_parse_int(
192 const char *filename
,
208 if ((r
= safe_atoi(rvalue
, i
)) < 0) {
209 fprintf(stderr
, "[%s:%u] Failed to parse numeric value: %s", filename
, line
, rvalue
);
216 int config_parse_unsigned(
217 const char *filename
,
233 if ((r
= safe_atou(rvalue
, u
)) < 0) {
234 fprintf(stderr
, "[%s:%u] Failed to parse numeric value: %s", filename
, line
, rvalue
);
241 int config_parse_size(
242 const char *filename
,
259 if ((r
= safe_atou(rvalue
, &u
)) < 0) {
260 fprintf(stderr
, "[%s:%u] Failed to parse numeric value: %s", filename
, line
, rvalue
);
268 int config_parse_bool(
269 const char *filename
,
285 if ((k
= parse_boolean(rvalue
)) < 0) {
286 fprintf(stderr
, "[%s:%u] Failed to parse boolean value: %s", filename
, line
, rvalue
);
294 int config_parse_string(
295 const char *filename
,
312 if (!(n
= strdup(rvalue
)))