1
2
3
4
5
6
7
8
9
10
11
12
13
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
47
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
79
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
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
277
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
303
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
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
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
|
/*
* Config file parser.
*
* Copyright (c) 2007-2009 Marko Kreen, Skype Technologies OÜ
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <usual/cfparser.h>
#ifdef HAVE_PWD_H
#include <pwd.h>
#endif
#include <usual/ctype.h>
#include <usual/fileutil.h>
#include <usual/logging.h>
#include <usual/time.h>
#include <usual/string.h>
#define MAX_INCLUDE 10
/*
* INI file parser.
*/
static int count_lines(const char *s, const char *end)
{
int lineno = 1;
for (; s < end; s++) {
if (*s == '\n')
lineno++;
}
return lineno;
}
static bool parse_ini_file_internal(const char *fn, cf_handler_f user_handler, void *arg, int inclevel)
{
char *buf;
char *p, *key, *val;
int klen, vlen;
char o1, o2;
bool ok;
buf = load_file(fn, NULL);
if (buf == NULL)
return false;
p = buf;
while (*p) {
/* space at the start of line - including empty lines */
while (*p && isspace(*p)) p++;
if (strncmp(p, "%include", 8) == 0 && p[8] != 0 && isblank(p[8])) {
if (inclevel >= MAX_INCLUDE) {
log_error("include nesting level too deep (%s:%d), stopping loading",
fn, count_lines(buf, p));
goto failed;
}
p += 8;
while (*p && isblank(*p)) p++;
/* now read value */
val = p;
while (*p && (*p != '\n'))
p++;
vlen = p - val;
/* eat space at end */
while (vlen > 0 && isspace(val[vlen - 1]))
vlen--;
/*
* val now has the name of the file to be included.
* Process it recursively.
*/
o1 = val[vlen];
val[vlen] = 0;
log_debug("processing include: %s", val);
ok = parse_ini_file_internal(val, user_handler, arg, inclevel + 1);
val[vlen] = o1;
if (!ok)
goto failed;
log_debug("returned to processing file %s", fn);
continue;
}
/* skip comment lines */
if (*p == '#' || *p == ';') {
while (*p && *p != '\n') p++;
continue;
}
/* got new section */
if (*p == '[') {
key = ++p;
while (*p && *p != ']' && *p != '\n') p++;
if (*p != ']')
goto syntax_error;
o1 = *p;
*p = 0;
log_debug("parse_ini_file: [%s]", key);
ok = user_handler(arg, true, key, NULL);
*p++ = o1;
if (!ok)
goto failed;
continue;
}
/* done? */
if (*p == 0)
break;
/* read key val */
key = p;
while (*p && (isalnum(*p) || strchr("_.-*", *p))) p++;
klen = p - key;
/* expect '=', skip it */
while (*p && (*p == ' ' || *p == '\t')) p++;
if (*p != '=') {
goto syntax_error;
} else
p++;
while (*p && (*p == ' ' || *p == '\t')) p++;
/* now read value */
val = p;
while (*p && (*p != '\n'))
p++;
vlen = p - val;
/* eat space at end */
while (vlen > 0 && isspace(val[vlen - 1]))
vlen--;
/* skip junk */
while (*p && isspace(*p)) p++;
/* our buf is r/w, so take it easy */
o1 = key[klen];
o2 = val[vlen];
key[klen] = 0;
val[vlen] = 0;
log_debug("parse_ini_file: '%s' = '%s'", key, val);
ok = user_handler(arg, false, key, val);
log_debug("parse_ini_file: '%s' = '%s' ok:%d", key, val, ok);
/* restore data, to keep count_lines() working */
key[klen] = o1;
val[vlen] = o2;
if (!ok)
goto failed;
}
free(buf);
return true;
syntax_error:
log_error("syntax error in configuration (%s:%d), stopping loading", fn, count_lines(buf, p));
failed:
free(buf);
return false;
}
bool parse_ini_file(const char *fn, cf_handler_f user_handler, void *arg)
{
return parse_ini_file_internal(fn, user_handler, arg, 0);
}
/*
* Config framework.
*/
static void *get_dest(void *base, const struct CfKey *k)
{
char *dst;
if (k->flags & CF_VAL_REL) {
/* relative address requires base */
if (!base)
return NULL;
dst = (char *)base + k->key_ofs;
} else
dst = (char *)k->key_ofs;
return dst;
}
static const struct CfSect *find_sect(const struct CfContext *cf, const char *name)
{
const struct CfSect *s;
for (s = cf->sect_list; s->sect_name; s++) {
if (strcmp(s->sect_name, name) == 0)
return s;
if (strcmp(s->sect_name, "*") == 0)
return s;
}
return NULL;
}
static const struct CfKey *find_key(const struct CfSect *s, const char *key)
{
const struct CfKey *k;
for (k = s->key_list; k->key_name; k++) {
if (strcmp(k->key_name, key) == 0)
return k;
}
return k;
}
const char *cf_get(const struct CfContext *cf, const char *sect, const char *key,
char *buf, int buflen)
{
const struct CfSect *s;
const struct CfKey *k;
void *base, *p;
struct CfValue cv;
/* find section */
s = find_sect(cf, sect);
if (!s)
return NULL;
/* find section base */
base = cf->base;
if (s->base_lookup)
base = s->base_lookup(base, sect);
/* handle dynamic keys */
if (s->set_key) {
if (!s->get_key)
return NULL;
return s->get_key(base, key, buf, buflen);
}
/* get fixed key */
k = find_key(s, key);
if (!k || !k->op.getter)
return NULL;
p = get_dest(base, k);
if (!p)
return NULL;
cv.key_name = k->key_name;
cv.extra = k->op.op_extra;
cv.value_p = p;
cv.buf = buf;
cv.buflen = buflen;
return k->op.getter(&cv);
}
bool cf_set(const struct CfContext *cf, const char *sect, const char *key, const char *val)
{
const struct CfSect *s;
const struct CfKey *k;
void *base, *p;
struct CfValue cv;
/* find section */
s = find_sect(cf, sect);
if (!s) {
log_error("Unknown section: %s", sect);
return false;
}
/* find section base */
base = cf->base;
if (s->base_lookup)
base = s->base_lookup(base, sect);
/* handle dynamic keys */
if (s->set_key)
return s->set_key(base, key, val);
/* set fixed key */
k = find_key(s, key);
if (!k) {
log_error("Unknown parameter: %s/%s", sect, key);
return false;
}
if (!k->op.setter || (k->flags & CF_READONLY)) {
/* silently ignore */
return true;
}
if ((k->flags & CF_NO_RELOAD) && cf->loaded) {
/* silently ignore */
return true;
}
p = get_dest(base, k);
if (!p) {
log_error("Bug - no base for relative key: %s/%s", sect, key);
return false;
}
cv.key_name = k->key_name;
cv.extra = k->op.op_extra;
cv.value_p = p;
cv.buf = NULL;
cv.buflen = 0;
return k->op.setter(&cv, val);
}
/*
* File loader
*/
struct LoaderCtx {
const struct CfContext *cf;
const char *cur_sect;
void *top_base;
bool got_main_sect;
};
static bool fill_defaults(struct LoaderCtx *ctx)
{
const struct CfKey *k;
const struct CfSect *s;
s = find_sect(ctx->cf, ctx->cur_sect);
if (!s)
goto fail;
if (s == ctx->cf->sect_list)
ctx->got_main_sect = true;
if (s->section_start) {
if (!s->section_start(ctx->top_base, ctx->cur_sect))
return false;
}
if (s->set_key)
return true;
for (k = s->key_list; k->key_name; k++) {
if (!k->def_value || (k->flags & CF_READONLY))
continue;
if ((k->flags & CF_NO_RELOAD) && ctx->cf->loaded)
continue;
if (!cf_set(ctx->cf, ctx->cur_sect, k->key_name, k->def_value))
goto fail;
}
return true;
fail:
log_error("fill_defaults fail");
return false;
}
static bool load_handler(void *arg, bool is_sect, const char *key, const char *val)
{
struct LoaderCtx *ctx = arg;
if (is_sect) {
if (ctx->cur_sect)
free(ctx->cur_sect);
ctx->cur_sect = strdup(key);
if (!ctx->cur_sect)
return false;
return fill_defaults(ctx);
} else if (!ctx->cur_sect) {
log_error("load_init_file: value without section: %s", key);
return false;
} else {
return cf_set(ctx->cf, ctx->cur_sect, key, val);
}
}
bool cf_load_file(const struct CfContext *cf, const char *fn)
{
struct LoaderCtx ctx;
bool ok;
memset(&ctx, 0, sizeof(ctx));
ctx.cf = cf;
ok = parse_ini_file(fn, load_handler, &ctx);
if (ctx.cur_sect)
free(ctx.cur_sect);
if (ok && !ctx.got_main_sect) {
log_error("load_init_file: main section missing from config file");
return false;
}
return ok;
}
/*
* Various value parsers.
*/
bool cf_set_int(struct CfValue *cv, const char *value)
{
int *ptr = cv->value_p;
char *end;
long val;
errno = 0;
val = strtol(value, &end, 0);
if (end == value || *end != 0) {
/* reject partial parse */
if (!errno)
errno = EINVAL;
return false;
}
*ptr = val;
return true;
}
bool cf_set_uint(struct CfValue *cv, const char *value)
{
unsigned int *ptr = cv->value_p;
char *end;
unsigned long val;
errno = 0;
val = strtoul(value, &end, 0);
if (end == value || *end != 0) {
/* reject partial parse */
if (!errno)
errno = EINVAL;
return false;
}
*ptr = val;
return true;
}
bool cf_set_str(struct CfValue *cv, const char *value)
{
char **dst_p = cv->value_p;
char *tmp = strdup(value);
if (!tmp) {
log_error("cf_set_str: no mem");
return false;
}
if (*dst_p)
free(*dst_p);
*dst_p = tmp;
return true;
}
bool cf_set_filename(struct CfValue *cv, const char *value)
{
char **dst_p = cv->value_p;
char *tmp, *home, *p;
int v_len, usr_len, home_len;
struct passwd *pw;
/* do we need to do tilde expansion */
if (value[0] != '~')
return cf_set_str(cv, value);
/* find username end */
v_len = strlen(value);
if ((p = memchr(value, '/', v_len)) == NULL)
usr_len = v_len - 1;
else
usr_len = (p - value) - 1;
if (usr_len) {
p = malloc(usr_len + 1);
if (!p)
return false;
memcpy(p, value + 1, usr_len);
p[usr_len] = 0;
pw = getpwnam(p);
free(p);
if (!pw)
goto fail;
home = pw->pw_dir;
} else {
home = getenv("HOME");
if (!home) {
pw = getpwuid(getuid());
if (!pw)
goto fail;
home = pw->pw_dir;
}
}
if (!home)
goto fail;
home_len = strlen(home);
tmp = malloc(v_len - usr_len + home_len);
if (!tmp)
return false;
memcpy(tmp, home, home_len);
memcpy(tmp + home_len, value + usr_len + 1, v_len - usr_len - 1);
tmp[v_len - 1 - usr_len + home_len] = 0;
log_debug("expanded '%s' -> '%s'", value, tmp);
if (*dst_p)
free(*dst_p);
*dst_p = tmp;
return true;
fail:
log_error("cannot to expand filename: %s", value);
return false;
}
/* parse float with error checking. returns -1 if failed */
static double parse_time(const char *value)
{
double v;
char *endp = NULL;
errno = 0;
v = strtod_dot(value, &endp);
if (errno)
return -1;
if (*endp || endp == value || v < 0) {
errno = EINVAL;
return -1;
}
return v;
}
bool cf_set_time_usec(struct CfValue *cv, const char *value)
{
usec_t *ptr = cv->value_p;
double v = parse_time(value);
if (v < 0)
return false;
*ptr = (usec_t)(USEC * v);
return true;
}
bool cf_set_time_double(struct CfValue *cv, const char *value)
{
double *ptr = cv->value_p;
double v = parse_time(value);
if (v < 0)
return false;
*ptr = v;
return true;
}
/*
* Various value formatters.
*/
const char *cf_get_str(struct CfValue *cv)
{
char **p = cv->value_p;
return *p;
}
const char *cf_get_int(struct CfValue *cv)
{
int *p = cv->value_p;
snprintf(cv->buf, cv->buflen, "%d", *p);
return cv->buf;
}
const char *cf_get_uint(struct CfValue *cv)
{
unsigned int *p = cv->value_p;
snprintf(cv->buf, cv->buflen, "%u", *p);
return cv->buf;
}
const char *cf_get_time_double(struct CfValue *cv)
{
double *p = cv->value_p;
snprintf(cv->buf, cv->buflen, "%g", *p);
return cv->buf;
}
const char *cf_get_time_usec(struct CfValue *cv)
{
struct CfValue tmp = *cv;
usec_t *p = cv->value_p;
double d = (double)(*p) / USEC;
tmp.value_p = &d;
return cf_get_time_double(&tmp);
}
/*
* str->int mapping
*/
const char *cf_get_lookup(struct CfValue *cv)
{
int *p = cv->value_p;
const struct CfLookup *lk = cv->extra;
for (; lk->name; lk++) {
if (lk->value == *p)
return lk->name;
}
return "INVALID";
}
bool cf_set_lookup(struct CfValue *cv, const char *value)
{
int *p = cv->value_p;
const struct CfLookup *lk = cv->extra;
for (; lk->name; lk++) {
if (strcasecmp(lk->name, value) == 0) {
*p = lk->value;
return true;
}
}
return false;
}
|