conf-parser.c 10.1 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
/*-*- Mode: C; c-basic-offset: 8 -*-*/

#include <string.h>
#include <stdio.h>
#include <errno.h>
#include <assert.h>
#include <stdlib.h>

#include "conf-parser.h"
#include "util.h"
#include "macro.h"
12
#include "strv.h"
13
#include "log.h"
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46

#define COMMENTS "#;\n"
#define LINE_MAX 4096

/* Run the user supplied parser for an assignment */
static int next_assignment(
                const char *filename,
                unsigned line,
                const char *section,
                const ConfigItem *t,
                const char *lvalue,
                const char *rvalue,
                void *userdata) {

        assert(filename);
        assert(t);
        assert(lvalue);
        assert(rvalue);

        for (; t->parse; t++) {

                if (t->lvalue && !streq(lvalue, t->lvalue))
                        continue;

                if (t->section && !section)
                        continue;

                if (t->section && !streq(section, t->section))
                        continue;

                return t->parse(filename, line, section, lvalue, rvalue, t->data, userdata);
        }

47
        log_error("[%s:%u] Unknown lvalue '%s' in section '%s'.", filename, line, lvalue, strna(section));
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
        return -EBADMSG;
}

/* Returns non-zero when c is contained in s */
static int in_string(char c, const char *s) {
        assert(s);

        for (; *s; s++)
                if (*s == c)
                        return 1;

        return 0;
}

/* Remove all whitepsapce from the beginning and the end of *s. *s may
 * be modified. */
static char *strip(char *s) {
        char *b = s+strspn(s, WHITESPACE);
        char *e, *l = NULL;

        for (e = b; *e; e++)
                if (!in_string(*e, WHITESPACE))
                        l = e;

        if (l)
                *(l+1) = 0;

        return b;
}

/* 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) {
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
        char *e, *c, *b;

        b = l+strspn(l, WHITESPACE);

        if ((c = strpbrk(b, COMMENTS)))
                *c = 0;

        if (!*b)
                return 0;

        if (startswith(b, ".include ")) {
                char *path = NULL, *fn;
                int r;

                fn = strip(b+9);
                if (!is_path_absolute(fn)) {
                        const char *k;

                        if ((k = strrchr(filename, '/'))) {
                                char *dir;

                                if (!(dir = strndup(filename, k-filename)))
                                        return -ENOMEM;

                                if (asprintf(&path, "%s/%s", dir, fn) < 0)
                                        return -errno;

                                fn = path;
                                free(dir);
                        }
                }

Lennart Poettering's avatar
Lennart Poettering committed
112
                r = config_parse(fn, NULL, sections, t, userdata);
113
114
115
116
117
118
119
120
121
122
123
124
                free(path);
                return r;
        }

        if (*b == '[') {
                size_t k;
                char *n;

                k = strlen(b);
                assert(k > 0);

                if (b[k-1] != ']') {
125
                        log_error("[%s:%u] Invalid section header.", filename, line);
126
127
128
129
130
131
                        return -EBADMSG;
                }

                if (!(n = strndup(b+1, k-2)))
                        return -ENOMEM;

132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
                if (sections) {
                        const char * const * i;
                        bool good = false;
                        STRV_FOREACH(i, sections)
                                if (streq(*i, n)) {
                                        good = true;
                                        break;
                                }

                        if (!good) {
                                free(n);
                                return -EBADMSG;
                        }
                }

147
148
149
150
151
152
153
                free(*section);
                *section = n;

                return 0;
        }

        if (!(e = strchr(b, '='))) {
154
                log_error("[%s:%u] Missing '='.", filename, line);
155
156
157
158
159
160
161
162
163
164
                return -EBADMSG;
        }

        *e = 0;
        e++;

        return next_assignment(filename, line, *section, t, strip(b), strip(e), userdata);
}

/* Go through the file and parse each line */
Lennart Poettering's avatar
Lennart Poettering committed
165
int config_parse(const char *filename, FILE *f, const char* const * sections, const ConfigItem *t, void *userdata) {
166
167
168
169
170
171
172
        unsigned line = 0;
        char *section = NULL;
        int r;

        assert(filename);
        assert(t);

Lennart Poettering's avatar
Lennart Poettering committed
173
174
175
176
177
178
        if (!f) {
                if (!(f = fopen(filename, "re"))) {
                        r = -errno;
                        log_error("Failed to open configuration file '%s': %s", filename, strerror(-r));
                        goto finish;
                }
179
180
181
182
183
184
185
186
187
188
        }

        while (!feof(f)) {
                char l[LINE_MAX];

                if (!fgets(l, sizeof(l), f)) {
                        if (feof(f))
                                break;

                        r = -errno;
189
                        log_error("Failed to read configuration file '%s': %s", filename, strerror(-r));
190
191
192
                        goto finish;
                }

193
                if ((r = parse_line(filename, ++line, &section, sections, t, l, userdata)) < 0)
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
                        goto finish;
        }

        r = 0;

finish:
        free(section);

        if (f)
                fclose(f);

        return r;
}

int config_parse_int(
                const char *filename,
                unsigned line,
                const char *section,
                const char *lvalue,
                const char *rvalue,
                void *data,
                void *userdata) {

        int *i = data;
        int r;

        assert(filename);
        assert(lvalue);
        assert(rvalue);
        assert(data);

        if ((r = safe_atoi(rvalue, i)) < 0) {
226
                log_error("[%s:%u] Failed to parse numeric value: %s", filename, line, rvalue);
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
                return r;
        }

        return 0;
}

int config_parse_unsigned(
                const char *filename,
                unsigned line,
                const char *section,
                const char *lvalue,
                const char *rvalue,
                void *data,
                void *userdata) {

        unsigned *u = data;
        int r;

        assert(filename);
        assert(lvalue);
        assert(rvalue);
        assert(data);

        if ((r = safe_atou(rvalue, u)) < 0) {
251
                log_error("[%s:%u] Failed to parse numeric value: %s", filename, line, rvalue);
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
                return r;
        }

        return 0;
}

int config_parse_size(
                const char *filename,
                unsigned line,
                const char *section,
                const char *lvalue,
                const char *rvalue,
                void *data,
                void *userdata) {

        size_t *sz = data;
        unsigned u;
        int r;

        assert(filename);
        assert(lvalue);
        assert(rvalue);
        assert(data);

        if ((r = safe_atou(rvalue, &u)) < 0) {
277
                log_error("[%s:%u] Failed to parse numeric value: %s", filename, line, rvalue);
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
                return r;
        }

        *sz = (size_t) u;
        return 0;
}

int config_parse_bool(
                const char *filename,
                unsigned line,
                const char *section,
                const char *lvalue,
                const char *rvalue,
                void *data,
                void *userdata) {

        int k;
        bool *b = data;

        assert(filename);
        assert(lvalue);
        assert(rvalue);
        assert(data);

        if ((k = parse_boolean(rvalue)) < 0) {
303
                log_error("[%s:%u] Failed to parse boolean value: %s", filename, line, rvalue);
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
                return k;
        }

        *b = !!k;
        return 0;
}

int config_parse_string(
                const char *filename,
                unsigned line,
                const char *section,
                const char *lvalue,
                const char *rvalue,
                void *data,
                void *userdata) {

        char **s = data;
        char *n;

        assert(filename);
        assert(lvalue);
        assert(rvalue);
        assert(data);

        if (*rvalue) {
                if (!(n = strdup(rvalue)))
                        return -ENOMEM;
        } else
                n = NULL;

        free(*s);
        *s = n;

        return 0;
}
339

340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
int config_parse_path(
                const char *filename,
                unsigned line,
                const char *section,
                const char *lvalue,
                const char *rvalue,
                void *data,
                void *userdata) {

        char **s = data;
        char *n;

        assert(filename);
        assert(lvalue);
        assert(rvalue);
        assert(data);

        if (*rvalue != '/') {
                log_error("[%s:%u] Not an absolute path: %s", filename, line, rvalue);
                return -EINVAL;
        }

        if (!(n = strdup(rvalue)))
                return -ENOMEM;

        free(*s);
        *s = n;

        return 0;
}
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392

int config_parse_strv(
                const char *filename,
                unsigned line,
                const char *section,
                const char *lvalue,
                const char *rvalue,
                void *data,
                void *userdata) {

        char*** sv = data;
        char **n;
        char *w;
        unsigned k;
        size_t l;
        char *state;

        assert(filename);
        assert(lvalue);
        assert(rvalue);
        assert(data);

        k = strv_length(*sv);
393
        FOREACH_WORD_QUOTED(w, l, rvalue, state)
394
395
396
397
398
399
400
                k++;

        if (!(n = new(char*, k+1)))
                return -ENOMEM;

        for (k = 0; (*sv)[k]; k++)
                n[k] = (*sv)[k];
401
        FOREACH_WORD_QUOTED(w, l, rvalue, state)
402
403
404
405
406
407
408
409
410
411
412
413
                if (!(n[k++] = strndup(w, l)))
                        goto fail;

        n[k] = NULL;
        free(*sv);
        *sv = n;

        return 0;

fail:
        for (; k > 0; k--)
                free(n[k-1]);
414
        free(n);
415
416
417

        return -ENOMEM;
}