forked from kamailio/kamailio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cfg_parser.c
1042 lines (893 loc) · 23.4 KB
/
cfg_parser.c
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
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Standalone Configuration File Parser
*
* Copyright (C) 2008 iptelorg GmbH
* Written by Jan Janak <[email protected]>
*
* This file is part of Kamailio, a free SIP server.
*
* Kamailio is free software; you can redistribute it and/or modify it under the
* terms of the GNU General Public License as published by the Free Software
* Foundation; either version 2 of the License, or (at your option) any later
* version.
*
* Kamailio is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
/*!
* \file
* \brief Kamailio core :: Standalone Configuration File Parser
* \author Jan Janak <[email protected]>
* \ingroup core
*
* Module: \ref core
*
* See \ref ConfigEngine
*
* \page ConfigEngine
* In file \ref cfg_parser.c
* Configuration examples
* - \ref ConfigExample1
* - \ref ConfigExample2
* - \ref ConfigExample3
* - \ref ConfigExample4
* - \ref ConfigExample5
* - \ref ConfigExample6
* - \ref ConfigExample7
* - \ref ConfigExample8
*
* <b>Run-time Allocated Destination Variables</b>
* - the destination variable pointers in arrays are assigned at compile time
* - The address of variables allocated on the heap (typically in dynamically allocated
* structures) is not know and thus we need to assign NULL initially and change the pointer
* at runtime.
* (provide an example).
*
* <b>Built-in parsing functions</b>
*
* *_val functions parse the whole option body (including =)
*/
/*! \page ConfigExample1 Configuration engine Example 1: Options without values
*
\verbatim
* str file = STR_STATIC_INIT("test.cfg");
* cfg_parser_t* parser;
* int feature_a = 0, feature_b = 0;
*
* cfg_option_t options[] = {
* {"enable_feature_a", .param = &feature_a, .val = 1},
* {"disable_feature_a", .param = &feature_a, .val = 0},
* {"feature_b", .param = &feature_b, .val = 1},
* {0}
* };
*
* if ((parser = cfg_parser_init(&file)) == NULL) {
* ERR("Error while creating parser\n");
* return -1;
* }
*
* cfg_set_options(parser, options);
*
* if (sr_cfg_parse(parser) < 0) {
* ERR("Error while parsing configuration file\n");
* cfg_parser_close(parser);
* return -1;
* }
*
* cfg_parser_close(parser);
\endverbatim
*/
/*! \page ConfigExample2 Configuration engine Example 2: Options with integer values
\verbatim
* cfg_option_t options[] = {
* {"max_number", .param = &max_number, .f = cfg_parse_int_val },
* {"extra_checks", .param = &extra_checks, .f = cfg_parse_bool_val},
* {0}
* };
\endverbatim
*/
/*! \page ConfigExample3 Configuration engine Example 3: Enum options
\verbatim
* int scope;
*
* cfg_option_t scopes[] = {
* {"base", .param = &scope, .val = 1},
* {"onelevel", .param = &scope, .val = 2},
* {"one", .param = &scope, .val = 3},
* {"subtree", .param = &scope, .val = 4},
* {"sub", .param = &scope, .val = 5},
* {"children", .param = &scope, .val = 6},
* {0}
* };
*
* cfg_option_t options[] = {
* {"scope", .param = scopes, .f = cfg_parse_enum_val},
* {0}
* };
\endverbatim
*/
/*! \page ConfigExample4 Configuration engine Example 4: Options with string values
\verbatim
* str filename = STR_NULL;
*
* cfg_option_t options[] = {
* {"filename", .param = &filename, .f = cfg_parse_str_val},
* {0}
* };
*
* - By default the function returns a pointer to an internal buffer which will be destroyed
* by a subsequent call
* - There are flags to tell the function to copy the resuting string in a pkg, shm, glibc,
* or static buffers
\endverbatim
*/
/*! \page ConfigExample5 Configuration engine Example 5: Custom value parsing
* TBD
*/
/*! \page ConfigExample6 Configuration engine Example 6: Parsing Sections
* TBD
*/
/*! \page ConfigExample7 Configuration engine Example 7: Default Options
* TBD
*/
/*! \page ConfigExample8 Configuration engine Example 8: Memory management of strings
*
* Data types with fixed size are easy, they can be copied into a pre-allocated memory
* buffer, strings cannot because their length is unknown.
*/
#include "cfg_parser.h"
#include "mem/mem.h"
#include "mem/shm_mem.h"
#include "dprint.h"
#include "trim.h"
#include "ut.h"
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <libgen.h>
/*! \brief The states of the lexical scanner */
enum st {
ST_S, /*!< Begin */
ST_A, /*!< Alphanumeric */
ST_AE, /*!< Alphanumeric escaped */
ST_Q, /*!< Quoted */
ST_QE, /*!< Quoted escaped */
ST_C, /*!< Comment */
ST_CE, /*!< Comment escaped */
ST_E, /*!< Escaped */
};
/*! \brief Test for alphanumeric characters */
#define IS_ALPHA(c) \
(((c) >= 'a' && (c) <= 'z') || \
((c) >= 'A' && (c) <= 'Z') || \
((c) >= '0' && (c) <= '9') || \
(c) == '_')
/*! \brief Test for delimiter characters */
#define IS_DELIM(c) \
((c) == '=' || \
(c) == ':' || \
(c) == ';' || \
(c) == '.' || \
(c) == ',' || \
(c) == '?' || \
(c) == '[' || \
(c) == ']' || \
(c) == '/' || \
(c) == '@' || \
(c) == '!' || \
(c) == '$' || \
(c) == '%' || \
(c) == '&' || \
(c) == '*' || \
(c) == '(' || \
(c) == ')' || \
(c) == '-' || \
(c) == '+' || \
(c) == '|' || \
(c) == '\'')
/*! \brief Whitespace characters */
#define IS_WHITESPACE(c) ((c) == ' ' || (c) == '\t' || (c) == '\r')
#define IS_QUOTE(c) ((c) == '\"') /* Quote characters */
#define IS_COMMENT(c) ((c) == '#') /* Characters that start comments */
#define IS_ESCAPE(c) ((c) == '\\') /* Escape characters */
#define IS_EOL(c) ((c) == '\n') /* End of line */
/*! \brief
* Append character to the value of current token
*/
#define PUSH(c) \
if (token->val.len >= MAX_TOKEN_LEN) { \
ERR("%s:%d:%d: Token too long\n", \
st->file, st->line, st->col); \
return -1; \
} \
if (token->val.len == 0) { \
token->start.line = st->line; \
token->start.col = st->col; \
} \
token->val.s[token->val.len++] = (c);
/*! \brief
* Return current token from the lexical analyzer
*/
#define RETURN(c) \
token->end.line = st->line; \
token->end.col = st->col; \
token->type = (c); \
print_token(token); \
return 0;
/*! \brief
* Get next character and update counters
*/
#define READ_CHAR \
c = fgetc(st->f); \
if (IS_EOL(c)) { \
st->line++; \
st->col = 0; \
} else { \
st->col++; \
}
cfg_option_t cfg_bool_values[] = {
{"yes", .val = 1},
{"true", .val = 1},
{"enable", .val = 1},
{"enabled", .val = 1},
{"1", .val = 1},
{"on", .val = 1},
{"no", .val = 0},
{"false", .val = 0},
{"disable", .val = 0},
{"disabled", .val = 0},
{"0", .val = 0},
{"off", .val = 0},
{0}
};
static void print_token(cfg_token_t* token)
{
#ifdef EXTRA_DEBUG
int i, j;
char* buf;
if ((buf = pkg_malloc(token->val.len * 2)) == NULL) {
LM_DBG("token(%d, '%.*s', <%d,%d>-<%d,%d>)\n",
token->type, STR_FMT(&token->val),
token->start.line, token->start.col,
token->end.line, token->end.col);
} else {
for(i = 0, j = 0; i < token->val.len; i++) {
switch(token->val.s[i]) {
case '\n': buf[j++] = '\\'; buf[j++] = 'n'; break;
case '\r': buf[j++] = '\\'; buf[j++] = 'r'; break;
case '\t': buf[j++] = '\\'; buf[j++] = 't'; break;
case '\0': buf[j++] = '\\'; buf[j++] = '0'; break;
case '\\': buf[j++] = '\\'; buf[j++] = '\\'; break;
default: buf[j++] = token->val.s[i];
}
}
LM_DBG("token(%d, '%.*s', <%d,%d>-<%d,%d>)\n",
token->type, j, buf,
token->start.line, token->start.col,
token->end.line, token->end.col);
pkg_free(buf);
}
#endif /* EXTRA_DEBUG */
}
int cfg_get_token(cfg_token_t* token, cfg_parser_t* st, unsigned int flags)
{
static int look_ahead = EOF;
int c;
enum st state;
state = ST_S;
token->val.s = token->buf;
token->val.len = 0;
if (look_ahead != EOF) {
c = look_ahead;
look_ahead = EOF;
} else {
READ_CHAR;
}
while(c != EOF) {
switch(state) {
case ST_S:
if (flags & CFG_EXTENDED_ALPHA) {
if (IS_WHITESPACE(c)) {
/* Do nothing */
} else if (IS_ALPHA(c) ||
IS_ESCAPE(c) ||
IS_DELIM(c)) {
PUSH(c);
state = ST_A;
} else if (IS_QUOTE(c)) {
state = ST_Q;
} else if (IS_COMMENT(c)) {
state = ST_C;
} else if (IS_EOL(c)) {
PUSH(c);
RETURN(c);
} else {
ERR("%s:%d:%d: Invalid character 0x%x\n",
st->file, st->line, st->col, c);
return -1;
}
} else {
if (IS_WHITESPACE(c)) {
/* Do nothing */
} else if (IS_ALPHA(c)) {
PUSH(c);
state = ST_A;
} else if (IS_QUOTE(c)) {
state = ST_Q;
} else if (IS_COMMENT(c)) {
state = ST_C;
} else if (IS_ESCAPE(c)) {
state = ST_E;
} else if (IS_DELIM(c) || IS_EOL(c)) {
PUSH(c);
RETURN(c);
} else {
ERR("%s:%d:%d: Invalid character 0x%x\n",
st->file, st->line, st->col, c);
return -1;
}
}
break;
case ST_A:
if (flags & CFG_EXTENDED_ALPHA) {
if (IS_ALPHA(c) ||
IS_DELIM(c) ||
IS_QUOTE(c)) {
PUSH(c);
} else if (IS_ESCAPE(c)) {
state = ST_AE;
} else if (IS_COMMENT(c) || IS_EOL(c) || IS_WHITESPACE(c)) {
look_ahead = c;
RETURN(CFG_TOKEN_ALPHA);
} else {
ERR("%s:%d:%d: Invalid character 0x%x\n",
st->file, st->line, st->col, c);
return -1;
}
} else {
if (IS_ALPHA(c)) {
PUSH(c);
} else if (IS_ESCAPE(c)) {
state = ST_AE;
} else if (IS_WHITESPACE(c) ||
IS_DELIM(c) ||
IS_QUOTE(c) ||
IS_COMMENT(c) ||
IS_EOL(c)) {
look_ahead = c;
RETURN(CFG_TOKEN_ALPHA);
} else {
ERR("%s:%d:%d: Invalid character 0x%x\n",
st->file, st->line, st->col, c);
return -1;
}
}
break;
case ST_AE:
if (IS_COMMENT(c) ||
IS_QUOTE(c) ||
IS_ESCAPE(c)) {
PUSH(c);
} else if (c == 'r') {
PUSH('\r');
} else if (c == 'n') {
PUSH('\n');
} else if (c == 't') {
PUSH('\t');
} else if (c == ' ') {
PUSH(' ');
} else if (IS_EOL(c)) {
/* Do nothing */
} else {
ERR("%s:%d:%d: Unsupported escape character 0x%x\n",
st->file, st->line, st->col, c);
return -1;
}
state = ST_A;
break;
case ST_Q:
if (IS_QUOTE(c)) {
RETURN(CFG_TOKEN_STRING);
} else if (IS_ESCAPE(c)) {
state = ST_QE;
break;
} else {
PUSH(c);
}
break;
case ST_QE:
if (IS_ESCAPE(c) ||
IS_QUOTE(c)) {
PUSH(c);
} else if (c == 'n') {
PUSH('\n');
} else if (c == 'r') {
PUSH('\r');
} else if (c == 't') {
PUSH('\t');
} else if (IS_EOL(c)) {
/* Do nothing */
} else {
ERR("%s:%d:%d: Unsupported escape character 0x%x\n",
st->file, st->line, st->col, c);
return -1;
}
state = ST_Q;
break;
case ST_C:
if (IS_ESCAPE(c)) {
state = ST_CE;
} else if (IS_EOL(c)) {
state = ST_S;
continue; /* Do not read a new char, return EOL */
} else {
/* Do nothing */
}
break;
case ST_CE:
state = ST_C;
break;
case ST_E:
if (IS_COMMENT(c) ||
IS_QUOTE(c) ||
IS_ESCAPE(c)) {
PUSH(c);
RETURN(c);
} else if (c == 'r') {
PUSH('\r');
RETURN('\r');
} else if (c == 'n') {
PUSH('\n');
RETURN('\n');
} else if (c == 't') {
PUSH('\t');
RETURN('\t');
} else if (c == ' ') {
PUSH(' ');
RETURN(' ');
} else if (IS_EOL(c)) {
/* Escped eol means no eol */
state = ST_S;
} else {
ERR("%s:%d:%d: Unsupported escape character 0x%x\n",
st->file, st->line, st->col, c);
return -1;
}
break;
}
READ_CHAR;
};
switch(state) {
case ST_S:
case ST_C:
case ST_CE:
return 1;
case ST_A:
RETURN(CFG_TOKEN_ALPHA);
case ST_Q:
ERR("%s:%d:%d: Premature end of file, missing closing quote in"
" string constant\n", st->file, st->line, st->col);
return -1;
case ST_QE:
case ST_E:
case ST_AE:
ERR("%s:%d:%d: Premature end of file, missing escaped character\n",
st->file, st->line, st->col);
return -1;
}
BUG("%s:%d:%d: Invalid state %d\n",
st->file, st->line, st->col, state);
return -1;
}
int cfg_parse_section(void* param, cfg_parser_t* st, unsigned int flags)
{
cfg_token_t t;
int ret;
ret = cfg_parse_str(param, st, flags);
if (ret < 0) return ret;
if (ret > 0) {
ERR("%s:%d:%d: Section name missing.\n",
st->file, st->line, st->col);
return ret;
}
ret = cfg_get_token(&t, st, flags);
if (ret < 0) goto error;
if (ret > 0) {
ERR("%s:%d:%d: Closing ']' missing\n", st->file, st->line, st->col);
goto error;
}
if (t.type != ']') {
ERR("%s:%d:%d: Syntax error, ']' expected\n",
st->file, t.start.line, t.start.col);
goto error;
}
if (cfg_eat_eol(st, flags)) goto error;
return 0;
error:
if (param && ((str*)param)->s) {
if (flags & CFG_STR_PKGMEM) {
pkg_free(((str*)param)->s);
((str*)param)->s = NULL;
} else if (flags & CFG_STR_SHMMEM) {
shm_free(((str*)param)->s);
((str*)param)->s = NULL;
} else if (flags & CFG_STR_MALLOC) {
free(((str*)param)->s);
((str*)param)->s = NULL;
}
}
return -1;
}
static char* get_base_name(str* filename)
{
char* tmp1, *tmp2, *res;
int len;
res = NULL;
if ((tmp1 = as_asciiz(filename)) == NULL) {
ERR("cfg_parser: No memory left\n");
goto error;
}
if ((tmp2 = basename(tmp1)) == NULL) {
ERR("cfg_parser: Error in basename\n");
goto error;
}
len = strlen(tmp2);
if ((res = pkg_malloc(len + 1)) == NULL) {
ERR("cfg_parser: No memory left");
goto error;
}
memcpy(res, tmp2, len + 1);
pkg_free(tmp1);
return res;
error:
if (tmp1) pkg_free(tmp1);
return NULL;
}
/** initialize the config parser.
* @param basedir - path to the config file name. If 0 the path
* (base directory) of the main ser.cfg file will be used, else
* basedir will be concatenated to the filename. It will be
* used only if filename is not an absolute path.
* @param filename - config filename (can include path elements).
* @return 0 on error, !=0 on success.
*/
cfg_parser_t* cfg_parser_init(str* basedir, str* filename)
{
cfg_parser_t* st;
char* pathname, *base, *abs_pathname;
abs_pathname = NULL;
pathname = filename->s;
st = NULL;
base = NULL;
/* if basedir == 0 or != "" get_abs_pathname */
if (basedir == 0 || basedir->len != 0) {
if ((abs_pathname = get_abs_pathname(basedir, filename)) == NULL) {
ERR("cfg_parser: Error while converting %.*s to absolute"
" pathname\n", STR_FMT(filename));
goto error;
}
pathname = abs_pathname;
}
if ((base = get_base_name(filename)) == NULL) goto error;
if ((st = (cfg_parser_t*)pkg_malloc(sizeof(*st))) == NULL) {
ERR("cfg_parser: No memory left\n");
goto error;
}
memset(st, '\0', sizeof(*st));
if ((st->f = fopen(pathname, "r")) == NULL) {
ERR("cfg_parser: Unable to open file '%s'\n", pathname);
goto error;
}
if (abs_pathname) pkg_free(abs_pathname);
st->file = base;
st->line = 1;
st->col = 0;
return st;
error:
if (st) {
if (st->f) fclose(st->f);
pkg_free(st);
}
if (base) pkg_free(base);
if (abs_pathname) pkg_free(abs_pathname);
return NULL;
}
void cfg_parser_close(cfg_parser_t* st)
{
if (!st) return;
if (st->f) fclose(st->f);
if (st->file) pkg_free(st->file);
pkg_free(st);
}
void cfg_section_parser(cfg_parser_t* st, cfg_func_f parser, void* param)
{
if (st == NULL) return;
st->section.parser = parser;
st->section.param = param;
}
void cfg_set_options(cfg_parser_t* st, cfg_option_t* options)
{
if (st) st->options = options;
}
static int process_option(cfg_parser_t* st, cfg_option_t* opt)
{
if (opt->f) {
/* We have a function so store it and pass opt->dst to it */
if (opt->f(opt->param, st, opt->flags) < 0) return -1;
} else {
/* We have no function, so if we have a pointer to some
* variable in opt->param then store the value of opt->i
* there, the variable is assumed to be of type i
*/
if (opt->param) *(int*)opt->param = opt->val;
}
return 0;
}
int sr_cfg_parse(cfg_parser_t* st)
{
int ret;
cfg_token_t t;
cfg_option_t* opt;
while(1) {
ret = cfg_get_token(&t, st, 0);
if (ret < 0) return ret;
if (ret > 0) break;
switch(t.type) {
case CFG_TOKEN_ALPHA:
/* Lookup the option name */
if ((opt = cfg_lookup_token(st->options, &t.val)) == NULL) {
ERR("%s:%d:%d: Unsupported option '%.*s'\n",
st->file, t.start.line, t.start.col, STR_FMT(&t.val));
return -1;
}
st->cur_opt = &t;
if (process_option(st, opt) < 0) { st->cur_opt = 0; return -1; }
st->cur_opt = 0;
break;
case '[':
if (st->section.parser == NULL) {
ERR("%s:%d:%d: Syntax error\n", st->file,
t.start.line, t.start.col);
return -1;
}
if (st->section.parser(st->section.param, st, 0) < 0) return -1;
break;
/* Empty line */
case '\n': continue;
default:
ERR("%s:%d:%d: Syntax error\n",
st->file, t.start.line, t.start.col);
return -1;
}
}
return 0;
}
cfg_option_t* cfg_lookup_token(cfg_option_t* table, str* token)
{
int len, i;
int (*cmp)(const char* s1, const char* s2, size_t n) = NULL;
if (table == NULL) return NULL;
for(i = 0; table[i].name; i++) {
len = strlen(table[i].name);
if (table[i].flags & CFG_PREFIX) {
if (token->len < len) continue;
} else {
if (token->len != len) continue;
}
if (table[i].flags & CFG_CASE_SENSITIVE) cmp = strncmp;
else cmp = strncasecmp;
if (cmp(token->s, table[i].name, len)) continue;
return table + i;
}
if (table[i].flags & CFG_DEFAULT) {
return table + i;
}
return NULL;
}
int cfg_eat_equal(cfg_parser_t* st, unsigned int flags)
{
cfg_token_t t;
int ret;
ret = cfg_get_token(&t, st, flags);
if (ret < 0) return ret;
if (ret > 0) {
ERR("%s:%d:%d: Delimiter '=' missing\n",
st->file, st->line, st->col);
return ret;
}
if (t.type != '=') {
ERR("%s:%d:%d: Syntax error, '=' expected\n",
st->file, t.start.line, t.start.col);
return -1;
}
return 0;
}
int cfg_eat_eol(cfg_parser_t* st, unsigned int flags)
{
cfg_token_t t;
int ret;
/* Skip EOL */
ret = cfg_get_token(&t, st, 0);
if (ret < 0) return ret;
if (ret > 0) return 0;
if (t.type != '\n') {
ERR("%s:%d:%d: End of line expected\n",
st->file, t.start.line, t.start.col);
return -1;
}
return 0;
}
int cfg_parse_enum(void* param, cfg_parser_t* st, unsigned int flags)
{
int ret;
cfg_token_t t;
cfg_option_t* values, *val;
values = (cfg_option_t*)param;
ret = cfg_get_token(&t, st, flags);
if (ret != 0) return ret;
if (t.type != CFG_TOKEN_ALPHA && t.type != CFG_TOKEN_STRING) {
ERR("%s:%d:%d: Invalid enum value '%.*s'\n",
st->file, t.start.line, t.start.col, STR_FMT(&t.val));
return -1;
}
if (values) {
if ((val = cfg_lookup_token(values, &t.val)) == NULL) {
ERR("%s:%d:%d Unsupported enum value '%.*s'\n",
st->file, t.start.line, t.start.col, STR_FMT(&t.val));
return -1;
}
return process_option(st, val);
} else {
return 0;
}
}
int cfg_parse_enum_opt(void* param, cfg_parser_t* st, unsigned int flags)
{
int ret;
if (cfg_eat_equal(st, flags)) return -1;
ret = cfg_parse_enum(param, st, CFG_EXTENDED_ALPHA | flags);
if (ret > 0) {
ERR("%s:%d:%d: Option value missing\n",
st->file, st->line, st->col);
return ret;
} else if (ret < 0) return ret;
if (cfg_eat_eol(st, flags)) return -1;
return 0;
}
int cfg_parse_str(void* param, cfg_parser_t* st, unsigned int flags)
{
str* val;
int ret;
char* buf;
cfg_token_t t;
ret = cfg_get_token(&t, st, flags);
if (ret != 0) return ret;
if (t.type != CFG_TOKEN_ALPHA && t.type != CFG_TOKEN_STRING) {
ERR("%s:%d:%d: Invalid string value '%.*s', a string expected.\n",
st->file, t.start.line, t.start.col, STR_FMT(&t.val));
return -1;
}
if (!param) return 0;
val = (str*)param;
if (flags & CFG_STR_STATIC) {
if (!val->s || val->len <= t.val.len) {
ERR("%s:%d:%d: Destination string buffer too small\n",
st->file, t.start.line, t.start.col);
return -1;
}
buf = val->s;
} else if (flags & CFG_STR_SHMMEM) {
if ((buf = shm_malloc(t.val.len + 1)) == NULL) {
ERR("%s:%d:%d: Out of shared memory\n", st->file,
t.start.line, t.start.col);
return -1;
}
if (val->s) shm_free(val->s);
} else if (flags & CFG_STR_MALLOC) {
if ((buf = malloc(t.val.len + 1)) == NULL) {
ERR("%s:%d:%d: Out of malloc memory\n", st->file,
t.start.line, t.start.col);
return -1;
}
if (val->s) free(val->s);
} else if (flags & CFG_STR_PKGMEM) {
if ((buf = pkg_malloc(t.val.len + 1)) == NULL) {
ERR("%s:%d:%d: Out of private memory\n", st->file,
t.start.line, t.start.col);
return -1;
}
if (val->s) pkg_free(val->s);
} else {
*val = t.val;
return 0;
}
memcpy(buf, t.val.s, t.val.len);
buf[t.val.len] = '\0';
val->s = buf;
val->len = t.val.len;
return 0;
}
int cfg_parse_str_opt(void* param, cfg_parser_t* st, unsigned int flags)
{
int ret;
if (cfg_eat_equal(st, flags)) return -1;
ret = cfg_parse_str(param, st, flags | CFG_EXTENDED_ALPHA);
if (ret > 0) {
ERR("%s:%d:%d: Option value missing\n",
st->file, st->line, st->col);
} else if (ret < 0) return ret;
if (cfg_eat_eol(st, flags)) return -1;
return 0;
}
int cfg_parse_int(void* param, cfg_parser_t* st, unsigned int flags)
{
int* val;
int ret, tmp;
cfg_token_t t;
val = (int*)param;
ret = cfg_get_token(&t, st, flags);
if (ret != 0) return ret;
if (t.type != CFG_TOKEN_ALPHA && t.type != CFG_TOKEN_STRING) {
ERR("%s:%d:%d: Invalid integer value '%.*s'\n",
st->file, t.start.line, t.start.col, STR_FMT(&t.val));
return -1;
}
if (str2sint(&t.val, &tmp) < 0) {
ERR("%s:%d:%d: Invalid integer value '%.*s'\n",
st->file, t.start.line, t.start.col, STR_FMT(&t.val));
return -1;
}
if (val) *val = tmp;
return 0;
}
int cfg_parse_int_opt(void* param, cfg_parser_t* st, unsigned int flags)
{
int ret;
if (cfg_eat_equal(st, flags)) return -1;
ret = cfg_parse_int(param, st, flags);
if (ret > 0) {
ERR("%s:%d:%d: Option value missing\n",
st->file, st->line, st->col);
} else if (ret < 0) return ret;
if (cfg_eat_eol(st, flags)) return -1;
return 0;
}