Debian Codegraph @

lib/usual/cfparser.c

raw symbols SCIP
1 /*
2 * Config file parser.
3 *
4 * Copyright (c) 2007-2009 Marko Kreen, Skype Technologies OÜ
5 *
6 * Permission to use, copy, modify, and/or distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
18
19 #include <usual/cfparser.h>
20
21 #ifdef HAVE_PWD_H
22 #include <pwd.h>
23 #endif
24
25 #include <usual/ctype.h>
26 #include <usual/fileutil.h>
27 #include <usual/logging.h>
28 #include <usual/time.h>
29 #include <usual/string.h>
30
31 #define MAX_INCLUDE 10
32
33 /*
34 * INI file parser.
35 */
36
37 static int count_lines(const char *s, const char *end)
38 {
39 int lineno = 1;
40 for (; s < end; s++) {
41 if (*s == '\n')
42 lineno++;
43 }
44 return lineno;
45 }
46
47 static bool parse_ini_file_internal(const char *fn, cf_handler_f user_handler, void *arg, int inclevel)
48 {
49 char *buf;
50 char *p, *key, *val;
51 int klen, vlen;
52 char o1, o2;
53 bool ok;
54
55 buf = load_file(fn, NULL);
56 if (buf == NULL) {
57 log_error("could not load file \"%s\": %s", fn, strerror(errno));
58 return false;
59 }
60
61 p = buf;
62 while (*p) {
63 /* space at the start of line - including empty lines */
64 while (*p && isspace(*p)) p++;
65
66 if (strncmp(p, "%include", 8) == 0 && p[8] != 0 && isblank(p[8])) {
67 if (inclevel >= MAX_INCLUDE) {
68 log_error("include nesting level too deep (%s:%d), stopping loading",
69 fn, count_lines(buf, p));
70 goto failed;
71 }
72 p += 8;
73 while (*p && isblank(*p)) p++;
74 /* now read value */
75 val = p;
76 while (*p && (*p != '\n'))
77 p++;
78 vlen = p - val;
79 /* eat space at end */
80 while (vlen > 0 && isspace(val[vlen - 1]))
81 vlen--;
82
83 /*
84 * val now has the name of the file to be included.
85 * Process it recursively.
86 */
87 o1 = val[vlen];
88 val[vlen] = 0;
89 log_debug("processing include: %s", val);
90 ok = parse_ini_file_internal(val, user_handler, arg, inclevel + 1);
91 val[vlen] = o1;
92 if (!ok) {
93 log_error("error processing include file in configuration (%s:%d), stopping loading", fn, count_lines(buf, p));
94 goto failed;
95 }
96 log_debug("returned to processing file %s", fn);
97 continue;
98 }
99
100 /* skip comment lines */
101 if (*p == '#' || *p == ';') {
102 while (*p && *p != '\n') p++;
103 continue;
104 }
105 /* got new section */
106 if (*p == '[') {
107 key = ++p;
108 while (*p && *p != ']' && *p != '\n') p++;
109 if (*p != ']')
110 goto syntax_error;
111 o1 = *p;
112 *p = 0;
113
114 log_debug("parse_ini_file: [%s]", key);
115 ok = user_handler(arg, true, key, NULL);
116 if (!ok) {
117 log_error("invalid section \"%s\" in configuration (%s:%d)",
118 key, fn, count_lines(buf, p));
119 goto failed;
120 }
121 *p++ = o1;
122 continue;
123 }
124
125 /* done? */
126 if (*p == 0)
127 break;
128
129 /* read key val */
130 key = p;
131 while (*p && (isalnum(*p) || strchr("_.-*", *p))) p++;
132 klen = p - key;
133
134 /* expect '=', skip it */
135 while (*p && (*p == ' ' || *p == '\t')) p++;
136 if (*p != '=') {
137 goto syntax_error;
138 } else
139 p++;
140 while (*p && (*p == ' ' || *p == '\t')) p++;
141
142 /* now read value */
143 val = p;
144 while (*p && (*p != '\n'))
145 p++;
146 vlen = p - val;
147 /* eat space at end */
148 while (vlen > 0 && isspace(val[vlen - 1]))
149 vlen--;
150
151 /* skip junk */
152 while (*p && isspace(*p)) p++;
153
154 /* our buf is r/w, so take it easy */
155 o1 = key[klen];
156 o2 = val[vlen];
157 key[klen] = 0;
158 val[vlen] = 0;
159
160 log_debug("parse_ini_file: '%s' = '%s'", key, val);
161
162 ok = user_handler(arg, false, key, val);
163
164 log_debug("parse_ini_file: '%s' = '%s' ok:%d", key, val, ok);
165
166 if (!ok)
167 log_error("invalid value \"%s\" for parameter %s in configuration (%s:%d)",
168 val, key, fn, count_lines(buf, p));
169
170 /* restore data, to keep count_lines() working */
171 key[klen] = o1;
172 val[vlen] = o2;
173
174 if (!ok)
175 goto failed;
176 }
177
178 free(buf);
179 return true;
180
181 syntax_error:
182 log_error("syntax error in configuration (%s:%d), stopping loading", fn, count_lines(buf, p));
183 failed:
184 free(buf);
185 return false;
186 }
187
188 bool parse_ini_file(const char *fn, cf_handler_f user_handler, void *arg)
189 {
190 return parse_ini_file_internal(fn, user_handler, arg, 0);
191 }
192
193 /*
194 * Config framework.
195 */
196
197 static void *get_dest(void *base, const struct CfKey *k)
198 {
199 char *dst;
200 if (k->flags & CF_VAL_REL) {
201 /* relative address requires base */
202 if (!base)
203 return NULL;
204 dst = (char *)base + k->key_ofs;
205 } else
206 dst = (char *)k->key_ofs;
207 return dst;
208 }
209
210 static const struct CfSect *find_sect(const struct CfContext *cf, const char *name)
211 {
212 const struct CfSect *s;
213 for (s = cf->sect_list; s->sect_name; s++) {
214 if (strcmp(s->sect_name, name) == 0)
215 return s;
216 if (strcmp(s->sect_name, "*") == 0)
217 return s;
218 }
219 return NULL;
220 }
221
222 static const struct CfKey *find_key(const struct CfSect *s, const char *key)
223 {
224 const struct CfKey *k;
225 for (k = s->key_list; k->key_name; k++) {
226 if (strcmp(k->key_name, key) == 0)
227 return k;
228 }
229 return NULL;
230 }
231
232 const char *cf_get(const struct CfContext *cf, const char *sect, const char *key,
233 char *buf, int buflen)
234 {
235 const struct CfSect *s;
236 const struct CfKey *k;
237 void *base, *p;
238 struct CfValue cv;
239
240 /* find section */
241 s = find_sect(cf, sect);
242 if (!s)
243 return NULL;
244
245 /* find section base */
246 base = cf->base;
247 if (s->base_lookup)
248 base = s->base_lookup(base, sect);
249
250 /* handle dynamic keys */
251 if (s->set_key) {
252 if (!s->get_key)
253 return NULL;
254 return s->get_key(base, key, buf, buflen);
255 }
256
257 /* get fixed key */
258 k = find_key(s, key);
259 if (!k || !k->op.getter)
260 return NULL;
261 p = get_dest(base, k);
262 if (!p)
263 return NULL;
264 cv.key_name = k->key_name;
265 cv.extra = k->op.op_extra;
266 cv.value_p = p;
267 cv.buf = buf;
268 cv.buflen = buflen;
269 return k->op.getter(&cv);
270 }
271
272 bool cf_set(const struct CfContext *cf, const char *sect, const char *key, const char *val)
273 {
274 const struct CfSect *s;
275 const struct CfKey *k;
276 void *base, *p;
277 struct CfValue cv;
278
279 /* find section */
280 s = find_sect(cf, sect);
281 if (!s) {
282 log_error("unknown section: %s", sect);
283 return false;
284 }
285
286 /* find section base */
287 base = cf->base;
288 if (s->base_lookup)
289 base = s->base_lookup(base, sect);
290
291 /* handle dynamic keys */
292 if (s->set_key)
293 return s->set_key(base, key, val);
294
295 /* set fixed key */
296 k = find_key(s, key);
297 if (!k) {
298 log_error("unknown parameter: %s/%s", sect, key);
299 return false;
300 }
301 if (!k->op.setter || (k->flags & CF_READONLY)) {
302 /* silently ignore */
303 return true;
304 }
305 if ((k->flags & CF_NO_RELOAD) && cf->loaded) {
306 /* silently ignore */
307 return true;
308 }
309 p = get_dest(base, k);
310 if (!p) {
311 log_error("bug - no base for relative key: %s/%s", sect, key);
312 return false;
313 }
314 cv.key_name = k->key_name;
315 cv.extra = k->op.op_extra;
316 cv.value_p = p;
317 cv.buf = NULL;
318 cv.buflen = 0;
319 return k->op.setter(&cv, val);
320 }
321
322 /*
323 * File loader
324 */
325
326 struct LoaderCtx {
327 const struct CfContext *cf;
328 char *cur_sect;
329 void *top_base;
330 bool got_main_sect;
331 };
332
333 static bool fill_defaults(struct LoaderCtx *ctx)
334 {
335 const struct CfKey *k;
336 const struct CfSect *s;
337
338 s = find_sect(ctx->cf, ctx->cur_sect);
339 if (!s)
340 goto fail;
341
342 if (s == ctx->cf->sect_list)
343 ctx->got_main_sect = true;
344
345 if (s->section_start) {
346 if (!s->section_start(ctx->top_base, ctx->cur_sect))
347 return false;
348 }
349
350 if (s->set_key)
351 return true;
352
353 for (k = s->key_list; k->key_name; k++) {
354 if (!k->def_value || (k->flags & CF_READONLY))
355 continue;
356 if ((k->flags & CF_NO_RELOAD) && ctx->cf->loaded)
357 continue;
358 if (!cf_set(ctx->cf, ctx->cur_sect, k->key_name, k->def_value))
359 goto fail;
360 }
361 return true;
362 fail:
363 log_error("fill_defaults fail");
364 return false;
365 }
366
367 static bool load_handler(void *arg, bool is_sect, const char *key, const char *val)
368 {
369 struct LoaderCtx *ctx = arg;
370
371 if (is_sect) {
372 free(ctx->cur_sect);
373 ctx->cur_sect = strdup(key);
374 if (!ctx->cur_sect)
375 return false;
376 return fill_defaults(ctx);
377 } else if (!ctx->cur_sect) {
378 log_error("load_init_file: value without section: %s", key);
379 return false;
380 } else {
381 return cf_set(ctx->cf, ctx->cur_sect, key, val);
382 }
383 }
384
385 bool cf_load_file(const struct CfContext *cf, const char *fn)
386 {
387 struct LoaderCtx ctx;
388 bool ok;
389 memset(&ctx, 0, sizeof(ctx));
390 ctx.cf = cf;
391
392 ok = parse_ini_file(fn, load_handler, &ctx);
393 free(ctx.cur_sect);
394 if (ok && !ctx.got_main_sect) {
395 log_error("load_init_file: main section missing from config file");
396 return false;
397 }
398 return ok;
399 }
400
401 /*
402 * Various value parsers.
403 */
404
405 bool cf_set_int(struct CfValue *cv, const char *value)
406 {
407 int *ptr = cv->value_p;
408 char *end;
409 long val;
410
411 errno = 0;
412 val = strtol(value, &end, 0);
413 if (end == value || *end != 0) {
414 /* reject partial parse */
415 if (!errno)
416 errno = EINVAL;
417 return false;
418 }
419 *ptr = val;
420 return true;
421 }
422
423 bool cf_set_uint(struct CfValue *cv, const char *value)
424 {
425 unsigned int *ptr = cv->value_p;
426 char *end;
427 unsigned long val;
428
429 errno = 0;
430 val = strtoul(value, &end, 0);
431 if (end == value || *end != 0) {
432 /* reject partial parse */
433 if (!errno)
434 errno = EINVAL;
435 return false;
436 }
437 *ptr = val;
438 return true;
439 }
440
441 bool cf_set_str(struct CfValue *cv, const char *value)
442 {
443 char **dst_p = cv->value_p;
444
445 char *tmp = strdup(value);
446 if (!tmp) {
447 log_error("cf_set_str: no mem");
448 return false;
449 }
450 free(*dst_p);
451 *dst_p = tmp;
452 return true;
453 }
454
455 bool cf_set_filename(struct CfValue *cv, const char *value)
456 {
457 char **dst_p = cv->value_p;
458 char *tmp, *home, *p;
459 int v_len, usr_len, home_len;
460 struct passwd *pw;
461
462 /* do we need to do tilde expansion */
463 if (value[0] != '~')
464 return cf_set_str(cv, value);
465
466 /* find username end */
467 v_len = strlen(value);
468 if ((p = memchr(value, '/', v_len)) == NULL)
469 usr_len = v_len - 1;
470 else
471 usr_len = (p - value) - 1;
472
473 if (usr_len) {
474 p = malloc(usr_len + 1);
475 if (!p)
476 return false;
477 memcpy(p, value + 1, usr_len);
478 p[usr_len] = 0;
479 pw = getpwnam(p);
480 free(p);
481 if (!pw)
482 goto fail;
483 home = pw->pw_dir;
484 } else {
485 home = getenv("HOME");
486 if (!home) {
487 pw = getpwuid(getuid());
488 if (!pw)
489 goto fail;
490 home = pw->pw_dir;
491 }
492 }
493 if (!home)
494 goto fail;
495
496 home_len = strlen(home);
497 tmp = malloc(v_len - usr_len + home_len);
498 if (!tmp)
499 return false;
500 memcpy(tmp, home, home_len);
501 memcpy(tmp + home_len, value + usr_len + 1, v_len - usr_len - 1);
502 tmp[v_len - 1 - usr_len + home_len] = 0;
503
504 log_debug("expanded '%s' -> '%s'", value, tmp);
505
506 free(*dst_p);
507 *dst_p = tmp;
508 return true;
509 fail:
510 log_error("cannot to expand filename: %s", value);
511 return false;
512 }
513
514 /* parse float with error checking. returns -1 if failed */
515 static double parse_time(const char *value)
516 {
517 double v;
518 char *endp = NULL;
519
520 errno = 0;
521 v = strtod_dot(value, &endp);
522 if (errno)
523 return -1;
524 if (*endp || endp == value || v < 0) {
525 errno = EINVAL;
526 return -1;
527 }
528 return v;
529 }
530
531 bool cf_set_time_usec(struct CfValue *cv, const char *value)
532 {
533 usec_t *ptr = cv->value_p;
534 double v = parse_time(value);
535 if (v < 0)
536 return false;
537 *ptr = (usec_t)(USEC * v);
538 return true;
539 }
540
541 bool cf_set_time_double(struct CfValue *cv, const char *value)
542 {
543 double *ptr = cv->value_p;
544 double v = parse_time(value);
545 if (v < 0)
546 return false;
547 *ptr = v;
548 return true;
549 }
550
551 /*
552 * Various value formatters.
553 */
554
555 const char *cf_get_str(struct CfValue *cv)
556 {
557 char **p = cv->value_p;
558 return *p;
559 }
560
561 const char *cf_get_int(struct CfValue *cv)
562 {
563 int *p = cv->value_p;
564 snprintf(cv->buf, cv->buflen, "%d", *p);
565 return cv->buf;
566 }
567
568 const char *cf_get_uint(struct CfValue *cv)
569 {
570 unsigned int *p = cv->value_p;
571 snprintf(cv->buf, cv->buflen, "%u", *p);
572 return cv->buf;
573 }
574
575 const char *cf_get_time_double(struct CfValue *cv)
576 {
577 double *p = cv->value_p;
578 snprintf(cv->buf, cv->buflen, "%g", *p);
579 return cv->buf;
580 }
581
582 const char *cf_get_time_usec(struct CfValue *cv)
583 {
584 struct CfValue tmp = *cv;
585 usec_t *p = cv->value_p;
586 double d = (double)(*p) / USEC;
587 tmp.value_p = &d;
588 return cf_get_time_double(&tmp);
589 }
590
591 /*
592 * str->int mapping
593 */
594
595 const char *cf_get_lookup(struct CfValue *cv)
596 {
597 int *p = cv->value_p;
598 const struct CfLookup *lk = cv->extra;
599 for (; lk->name; lk++) {
600 if (lk->value == *p)
601 return lk->name;
602 }
603 return "INVALID";
604 }
605
606 bool cf_set_lookup(struct CfValue *cv, const char *value)
607 {
608 int *p = cv->value_p;
609 const struct CfLookup *lk = cv->extra;
610 for (; lk->name; lk++) {
611 if (strcasecmp(lk->name, value) == 0) {
612 *p = lk->value;
613 return true;
614 }
615 }
616 return false;
617 }