-
Notifications
You must be signed in to change notification settings - Fork 110
/
Copy pathconfuse.c
2622 lines (2143 loc) · 53.7 KB
/
confuse.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
/*
* Copyright (c) 2002-2017 Martin Hedenfalk <martin@bzero.se>
*
* 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.
*/
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <sys/types.h>
#include <string.h>
#ifdef HAVE_STRINGS_H
# include <strings.h>
#endif
#include <stdlib.h>
#include <assert.h>
#include <errno.h>
#ifndef _WIN32
# include <pwd.h>
#endif
#ifdef HAVE_UNISTD_H
# include <unistd.h>
#endif
#include <ctype.h>
#ifdef HAVE_SYS_STAT_H
# include <sys/stat.h>
# ifndef S_ISREG
# define S_ISREG(mode) ((mode) & S_IFREG)
# endif
#endif
#include "compat.h"
#include "confuse.h"
#define is_set(f, x) (((f) & (x)) == (f))
#if defined(ENABLE_NLS) && defined(HAVE_GETTEXT)
# include <locale.h>
# include <libintl.h>
# define _(str) dgettext(PACKAGE, str)
#else
# define _(str) str
#endif
#define N_(str) str
const char confuse_version[] = PACKAGE_VERSION;
const char confuse_copyright[] = PACKAGE_STRING " by Martin Hedenfalk <martin@bzero.se>";
const char confuse_author[] = "Martin Hedenfalk <martin@bzero.se>";
char *cfg_yylval = 0;
extern int cfg_yylex(cfg_t *cfg);
extern void cfg_yylex_destroy(void);
extern int cfg_lexer_include(cfg_t *cfg, const char *fname);
extern void cfg_scan_fp_begin(FILE *fp);
extern void cfg_scan_fp_end(void);
static int cfg_parse_internal(cfg_t *cfg, int level, int force_state, cfg_opt_t *force_opt);
static void cfg_free_opt_array(cfg_opt_t *opts);
static int cfg_print_pff_indent(cfg_t *cfg, FILE *fp,
cfg_print_filter_func_t fb_pff, int indent);
#define STATE_CONTINUE 0
#define STATE_EOF -1
#define STATE_ERROR 1
#ifndef HAVE_FMEMOPEN
extern FILE *fmemopen(void *buf, size_t size, const char *type);
#endif
#ifndef HAVE_STRDUP
/*
* Copyright (c) 1988, 1993
* The Regents of the University of California. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
static char *strdup(const char *str)
{
size_t len;
char *dup;
len = strlen(str) + 1;
dup = calloc(len, sizeof(char));
if (!dup)
return NULL;
memcpy(dup, str, len);
return dup;
}
#endif
#ifndef HAVE_STRNDUP
static char *strndup(const char *s, size_t n)
{
char *r;
r = malloc(n + 1);
if (!r)
return NULL;
strncpy(r, s, n);
r[n] = 0;
return r;
}
#endif
#ifndef HAVE_STRCASECMP
int strcasecmp(const char *s1, const char *s2)
{
assert(s1);
assert(s2);
while (*s1) {
int c1 = tolower(*(const unsigned char *)s1);
int c2 = tolower(*(const unsigned char *)s2);
if (c1 < c2)
return -1;
if (c1 > c2)
return +1;
++s1;
++s2;
}
if (*s2 != 0)
return -1;
return 0;
}
#endif
static cfg_opt_t *cfg_getopt_leaf(cfg_t *cfg, const char *name)
{
unsigned int i;
for (i = 0; cfg->opts && cfg->opts[i].name; i++) {
if (is_set(CFGF_NOCASE, cfg->flags)) {
if (strcasecmp(cfg->opts[i].name, name) == 0)
return &cfg->opts[i];
} else {
if (strcmp(cfg->opts[i].name, name) == 0)
return &cfg->opts[i];
}
}
return NULL;
}
static char *parse_title(const char *name, size_t *len)
{
const char *escapes = "'\\";
char *title;
char *end;
char *ch;
if (*name != '\'') {
*len = strcspn(name, "|");
if (!*len)
return NULL;
return strndup(name, *len);
}
title = strdup(name + 1);
if (!title)
return NULL;
*len = 1;
ch = title;
end = title + strlen(title);
while (ch < end) {
size_t l = strcspn(ch, escapes);
*len += l + 1;
ch += l;
switch (*ch) {
case '\'':
*ch = 0;
return title;
case '\\':
if (!ch[1] || strcspn(ch + 1, escapes)) {
free(title);
return NULL;
}
memmove(ch, ch + 1, strlen(ch));
ch++;
(*len)++;
break;
default:
free(title);
return NULL;
}
}
free(title);
return NULL;
}
static long int cfg_opt_gettsecidx(cfg_opt_t *opt, const char *title)
{
unsigned int i, n;
n = cfg_opt_size(opt);
for (i = 0; i < n; i++) {
cfg_t *sec = cfg_opt_getnsec(opt, i);
if (!sec || !sec->title)
return -1;
if (is_set(CFGF_NOCASE, opt->flags)) {
if (strcasecmp(title, sec->title) == 0)
return i;
} else {
if (strcmp(title, sec->title) == 0)
return i;
}
}
return -1;
}
static cfg_opt_t *cfg_getopt_secidx(cfg_t *cfg, const char *name,
long int *index)
{
cfg_opt_t *opt = NULL;
cfg_t *sec = cfg;
if (!cfg || !cfg->name || !name || !*name) {
errno = EINVAL;
return NULL;
}
while (name && *name) {
char *secname;
size_t len = strcspn(name, "|=");
if (!index && name[len] == 0 /*len == strlen(name) */ )
/* no more subsections */
break;
if (len) {
char *title = NULL;
long int i = -1;
secname = strndup(name, len);
if (!secname)
return NULL;
do {
char *endptr;
opt = cfg_getopt_leaf(sec, secname);
if (!opt || opt->type != CFGT_SEC) {
opt = NULL;
break;
}
if (name[len] != '=') {
/* non-multi, and backwards compat */
i = 0;
break;
}
if (!is_set(CFGF_MULTI, opt->flags))
break;
name += len + 1;
title = parse_title(name, &len);
if (!title)
break;
if (is_set(CFGF_TITLE, opt->flags)) {
i = cfg_opt_gettsecidx(opt, title);
break;
}
i = strtol(title, &endptr, 0);
if (*endptr != '\0')
i = -1;
} while(0);
if (index)
*index = i;
sec = i >= 0 ? cfg_opt_getnsec(opt, i) : NULL;
if (!sec && !is_set(CFGF_IGNORE_UNKNOWN, cfg->flags)) {
if (opt && !is_set(CFGF_MULTI, opt->flags))
cfg_error(cfg, _("no such option '%s'"), secname);
else if (title)
cfg_error(cfg, _("no sub-section '%s' in '%s'"), title, secname);
else
cfg_error(cfg, _("no sub-section title/index for '%s'"), secname);
}
free(secname);
if (title)
free(title);
if (!sec)
return NULL;
}
name += len;
name += strspn(name, "|");
}
if (!index) {
opt = cfg_getopt_leaf(sec, name);
if (!opt && !is_set(CFGF_IGNORE_UNKNOWN, cfg->flags))
cfg_error(cfg, _("no such option '%s'"), name);
}
return opt;
}
DLLIMPORT cfg_opt_t *cfg_getnopt(cfg_t *cfg, unsigned int index)
{
unsigned int i;
if (!cfg)
return NULL;
for (i = 0; cfg->opts && cfg->opts[i].name; i++) {
if (i == index)
return &cfg->opts[i];
}
return NULL;
}
DLLIMPORT cfg_opt_t *cfg_getopt(cfg_t *cfg, const char *name)
{
return cfg_getopt_secidx(cfg, name, NULL);
}
DLLIMPORT const char *cfg_title(cfg_t *cfg)
{
if (cfg)
return cfg->title;
return NULL;
}
DLLIMPORT const char *cfg_name(cfg_t *cfg)
{
if (cfg)
return cfg->name;
return NULL;
}
DLLIMPORT const char *cfg_opt_name(cfg_opt_t *opt)
{
if (opt)
return opt->name;
return NULL;
}
DLLIMPORT unsigned int cfg_opt_size(cfg_opt_t *opt)
{
if (opt)
return opt->nvalues;
return 0;
}
DLLIMPORT unsigned int cfg_size(cfg_t *cfg, const char *name)
{
return cfg_opt_size(cfg_getopt(cfg, name));
}
DLLIMPORT char *cfg_opt_getcomment(cfg_opt_t *opt)
{
if (opt)
return opt->comment;
return NULL;
}
DLLIMPORT char *cfg_getcomment(cfg_t *cfg, const char *name)
{
return cfg_opt_getcomment(cfg_getopt(cfg, name));
}
DLLIMPORT signed long cfg_opt_getnint(cfg_opt_t *opt, unsigned int index)
{
if (!opt || opt->type != CFGT_INT) {
errno = EINVAL;
return 0;
}
if (opt->values && index < opt->nvalues)
return opt->values[index]->number;
if (opt->simple_value.number)
return *opt->simple_value.number;
return 0;
}
DLLIMPORT signed long cfg_getnint(cfg_t *cfg, const char *name, unsigned int index)
{
return cfg_opt_getnint(cfg_getopt(cfg, name), index);
}
DLLIMPORT signed long cfg_getint(cfg_t *cfg, const char *name)
{
return cfg_getnint(cfg, name, 0);
}
DLLIMPORT double cfg_opt_getnfloat(cfg_opt_t *opt, unsigned int index)
{
if (!opt || opt->type != CFGT_FLOAT) {
errno = EINVAL;
return 0;
}
if (opt->values && index < opt->nvalues)
return opt->values[index]->fpnumber;
if (opt->simple_value.fpnumber)
return *opt->simple_value.fpnumber;
return 0;
}
DLLIMPORT double cfg_getnfloat(cfg_t *cfg, const char *name, unsigned int index)
{
return cfg_opt_getnfloat(cfg_getopt(cfg, name), index);
}
DLLIMPORT double cfg_getfloat(cfg_t *cfg, const char *name)
{
return cfg_getnfloat(cfg, name, 0);
}
DLLIMPORT cfg_bool_t cfg_opt_getnbool(cfg_opt_t *opt, unsigned int index)
{
if (!opt || opt->type != CFGT_BOOL) {
errno = EINVAL;
return cfg_false;
}
if (opt->values && index < opt->nvalues)
return opt->values[index]->boolean;
if (opt->simple_value.boolean)
return *opt->simple_value.boolean;
return cfg_false;
}
DLLIMPORT cfg_bool_t cfg_getnbool(cfg_t *cfg, const char *name, unsigned int index)
{
return cfg_opt_getnbool(cfg_getopt(cfg, name), index);
}
DLLIMPORT cfg_bool_t cfg_getbool(cfg_t *cfg, const char *name)
{
return cfg_getnbool(cfg, name, 0);
}
DLLIMPORT char *cfg_opt_getnstr(cfg_opt_t *opt, unsigned int index)
{
if (!opt || opt->type != CFGT_STR) {
errno = EINVAL;
return NULL;
}
if (opt->values && index < opt->nvalues)
return opt->values[index]->string;
if (opt->simple_value.string)
return *opt->simple_value.string;
return NULL;
}
DLLIMPORT char *cfg_getnstr(cfg_t *cfg, const char *name, unsigned int index)
{
return cfg_opt_getnstr(cfg_getopt(cfg, name), index);
}
DLLIMPORT char *cfg_getstr(cfg_t *cfg, const char *name)
{
return cfg_getnstr(cfg, name, 0);
}
DLLIMPORT void *cfg_opt_getnptr(cfg_opt_t *opt, unsigned int index)
{
if (!opt || opt->type != CFGT_PTR) {
errno = EINVAL;
return NULL;
}
if (opt->values && index < opt->nvalues)
return opt->values[index]->ptr;
if (opt->simple_value.ptr)
return *opt->simple_value.ptr;
return NULL;
}
DLLIMPORT void *cfg_getnptr(cfg_t *cfg, const char *name, unsigned int index)
{
return cfg_opt_getnptr(cfg_getopt(cfg, name), index);
}
DLLIMPORT void *cfg_getptr(cfg_t *cfg, const char *name)
{
return cfg_getnptr(cfg, name, 0);
}
DLLIMPORT cfg_t *cfg_opt_getnsec(cfg_opt_t *opt, unsigned int index)
{
if (!opt || opt->type != CFGT_SEC) {
errno = EINVAL;
return NULL;
}
if (opt->values && index < opt->nvalues)
return opt->values[index]->section;
errno = ENOENT;
return NULL;
}
DLLIMPORT cfg_t *cfg_getnsec(cfg_t *cfg, const char *name, unsigned int index)
{
return cfg_opt_getnsec(cfg_getopt(cfg, name), index);
}
DLLIMPORT cfg_t *cfg_opt_gettsec(cfg_opt_t *opt, const char *title)
{
long int i;
if (!opt || !title) {
errno = EINVAL;
return NULL;
}
if (!is_set(CFGF_TITLE, opt->flags)) {
errno = EINVAL;
return NULL;
}
i = cfg_opt_gettsecidx(opt, title);
if (i >= 0)
return cfg_opt_getnsec(opt, i);
errno = ENOENT;
return NULL;
}
DLLIMPORT cfg_t *cfg_gettsec(cfg_t *cfg, const char *name, const char *title)
{
return cfg_opt_gettsec(cfg_getopt(cfg, name), title);
}
DLLIMPORT cfg_t *cfg_getsec(cfg_t *cfg, const char *name)
{
cfg_opt_t *opt;
long int index;
opt = cfg_getopt_secidx(cfg, name, &index);
return cfg_opt_getnsec(opt, index);
}
static cfg_value_t *cfg_addval(cfg_opt_t *opt)
{
void *ptr;
ptr = realloc(opt->values, (opt->nvalues + 1) * sizeof(cfg_value_t *));
if (!ptr)
return NULL;
opt->values = ptr;
opt->values[opt->nvalues] = calloc(1, sizeof(cfg_value_t));
if (!opt->values[opt->nvalues])
return NULL;
opt->flags |= CFGF_MODIFIED;
return opt->values[opt->nvalues++];
}
DLLIMPORT int cfg_numopts(cfg_opt_t *opts)
{
int n;
for (n = 0; opts && opts[n].name; n++)
/* do nothing */ ;
return n;
}
DLLIMPORT unsigned int cfg_num(cfg_t *cfg)
{
if (!cfg)
return 0;
return (unsigned int)cfg_numopts(cfg->opts);
}
static cfg_opt_t *cfg_dupopt_array(cfg_opt_t *opts)
{
int i;
cfg_opt_t *dupopts;
int n = cfg_numopts(opts);
dupopts = calloc(n + 1, sizeof(cfg_opt_t));
if (!dupopts)
return NULL;
memcpy(dupopts, opts, n * sizeof(cfg_opt_t));
for (i = 0; i < n; i++) {
/* Clear dynamic ptrs, protecting the original on failure */
dupopts[i].name = NULL;
dupopts[i].subopts = NULL;
dupopts[i].def.parsed = NULL;
dupopts[i].def.string = NULL;
dupopts[i].comment = NULL;
}
for (i = 0; i < n; i++) {
dupopts[i].name = strdup(opts[i].name);
if (!dupopts[i].name)
goto err;
if (opts[i].subopts) {
dupopts[i].subopts = cfg_dupopt_array(opts[i].subopts);
if (!dupopts[i].subopts)
goto err;
}
if (opts[i].def.parsed) {
dupopts[i].def.parsed = strdup(opts[i].def.parsed);
if (!dupopts[i].def.parsed)
goto err;
}
if (opts[i].def.string) {
dupopts[i].def.string = strdup(opts[i].def.string);
if (!dupopts[i].def.string)
goto err;
}
if (opts[i].comment) {
dupopts[i].comment = strdup(opts[i].comment);
if (!dupopts[i].comment)
goto err;
}
}
return dupopts;
err:
cfg_free_opt_array(dupopts);
return NULL;
}
DLLIMPORT int cfg_parse_boolean(const char *s)
{
if (!s) {
errno = EINVAL;
return CFG_FAIL;
}
if (strcasecmp(s, "true") == 0 || strcasecmp(s, "on") == 0 || strcasecmp(s, "yes") == 0)
return 1;
if (strcasecmp(s, "false") == 0 || strcasecmp(s, "off") == 0 || strcasecmp(s, "no") == 0)
return 0;
return CFG_FAIL;
}
static void cfg_init_defaults(cfg_t *cfg)
{
int i;
for (i = 0; cfg->opts && cfg->opts[i].name; i++) {
int j;
for (j = 0; j < i; ++j) {
if (is_set(CFGF_NOCASE, cfg->opts[i].flags | cfg->opts[j].flags)) {
if (strcasecmp(cfg->opts[i].name, cfg->opts[j].name))
continue;
} else {
if (strcmp(cfg->opts[i].name, cfg->opts[j].name))
continue;
}
/*
* There are two definitions of the same option name.
* What to do? It's a programming error and not an end
* user input error. Lets print a message and abort...
*/
cfg_error(cfg, _("duplicate option '%s' not allowed"),
cfg->opts[i].name);
break;
}
/* libConfuse doesn't handle default values for "simple" options */
if (cfg->opts[i].simple_value.ptr || is_set(CFGF_NODEFAULT, cfg->opts[i].flags))
continue;
if (cfg->opts[i].type != CFGT_SEC) {
cfg->opts[i].flags |= CFGF_DEFINIT;
if (is_set(CFGF_LIST, cfg->opts[i].flags) || cfg->opts[i].def.parsed) {
int xstate, ret = 0;
char *buf;
FILE *fp;
/* If it's a list, but no default value was given,
* keep the option uninitialized.
*/
buf = cfg->opts[i].def.parsed;
if (!buf || !buf[0])
continue;
/* setup scanning from the string specified for the
* "default" value, force the correct state and option
*/
if (is_set(CFGF_LIST, cfg->opts[i].flags))
/* lists must be surrounded by {braces} */
xstate = 3;
else if (cfg->opts[i].type == CFGT_FUNC)
xstate = 0;
else
xstate = 2;
fp = fmemopen(buf, strlen(buf), "r");
if (!fp) {
/*
* fmemopen() on older GLIBC versions do not accept zero
* length buffers for some reason. This is a workaround.
*/
if (strlen(buf) > 0)
ret = STATE_ERROR;
} else {
cfg_scan_fp_begin(fp);
do {
ret = cfg_parse_internal(cfg, 1, xstate, &cfg->opts[i]);
xstate = -1;
} while (ret == STATE_CONTINUE);
cfg_scan_fp_end();
fclose(fp);
}
if (ret == STATE_ERROR) {
/*
* If there was an error parsing the default string,
* the initialization of the default value could be
* inconsistent or empty. What to do? It's a
* programming error and not an end user input
* error. Lets print a message and abort...
*/
fprintf(stderr, "Parse error in default value '%s'"
" for option '%s'\n", cfg->opts[i].def.parsed, cfg->opts[i].name);
fprintf(stderr, "Check your initialization macros and the" " libConfuse documentation\n");
abort();
}
} else {
switch (cfg->opts[i].type) {
case CFGT_INT:
cfg_opt_setnint(&cfg->opts[i], cfg->opts[i].def.number, 0);
break;
case CFGT_FLOAT:
cfg_opt_setnfloat(&cfg->opts[i], cfg->opts[i].def.fpnumber, 0);
break;
case CFGT_BOOL:
cfg_opt_setnbool(&cfg->opts[i], cfg->opts[i].def.boolean, 0);
break;
case CFGT_STR:
cfg_opt_setnstr(&cfg->opts[i], cfg->opts[i].def.string, 0);
break;
case CFGT_FUNC:
case CFGT_PTR:
break;
default:
cfg_error(cfg, "internal error in cfg_init_defaults(%s)", cfg->opts[i].name);
break;
}
}
/* The default value should only be returned if no value
* is given in the configuration file, so we set the RESET
* flag here. When/If cfg_setopt() is called, the value(s)
* will be freed and the flag unset.
*/
cfg->opts[i].flags |= CFGF_RESET;
cfg->opts[i].flags &= ~CFGF_MODIFIED;
} else if (!is_set(CFGF_MULTI, cfg->opts[i].flags)) {
cfg_setopt(cfg, &cfg->opts[i], 0);
cfg->opts[i].flags |= CFGF_DEFINIT;
}
}
}
DLLIMPORT cfg_value_t *cfg_setopt(cfg_t *cfg, cfg_opt_t *opt, const char *value)
{
cfg_value_t *val = NULL;
int b;
const char *s;
double f;
long int i;
void *p;
char *endptr;
if (!cfg || !opt) {
errno = EINVAL;
return NULL;
}
if (opt->simple_value.ptr) {
if (opt->type == CFGT_SEC) {
errno = EINVAL;
return NULL;
}
val = (cfg_value_t *)opt->simple_value.ptr;
} else {
if (is_set(CFGF_RESET, opt->flags)) {
cfg_free_value(opt);
opt->flags &= ~CFGF_RESET;
}
if (opt->nvalues == 0 || is_set(CFGF_MULTI, opt->flags) || is_set(CFGF_LIST, opt->flags)) {
val = NULL;
if (opt->type == CFGT_SEC && is_set(CFGF_TITLE, opt->flags)) {
unsigned int i;
/* XXX: Check if there already is a section with the same title. */
/*
* Check there are either no sections at
* all, or a non-NULL section title.
*/
if (opt->nvalues != 0 && !value) {
errno = EINVAL;
return NULL;
}
for (i = 0; i < opt->nvalues && val == NULL; i++) {
cfg_t *sec = opt->values[i]->section;
if (is_set(CFGF_NOCASE, cfg->flags)) {
if (strcasecmp(value, sec->title) == 0)
val = opt->values[i];
} else {
if (strcmp(value, sec->title) == 0)
val = opt->values[i];
}
}
if (val && is_set(CFGF_NO_TITLE_DUPES, opt->flags)) {
cfg_error(cfg, _("found duplicate title '%s'"), value);
return NULL;
}
}
if (!val) {
val = cfg_addval(opt);
if (!val)
return NULL;
}
} else {
val = opt->values[0];
}
}
switch (opt->type) {
case CFGT_INT:
if (opt->parsecb) {
if ((*opt->parsecb) (cfg, opt, value, &i) != 0)
return NULL;
} else {
if (!value) {
errno = EINVAL;
return NULL;
}
i = strtol(value, &endptr, 0);
if (*endptr != '\0') {
cfg_error(cfg, _("invalid integer value for option '%s'"), opt->name);
return NULL;
}
if (errno == ERANGE) {
cfg_error(cfg, _("integer value for option '%s' is out of range"), opt->name);
return NULL;
}
}
val->number = i;
break;
case CFGT_FLOAT:
if (opt->parsecb) {
if ((*opt->parsecb) (cfg, opt, value, &f) != 0)
return NULL;
} else {
if (!value) {
errno = EINVAL;
return NULL;
}
f = strtod(value, &endptr);
if (*endptr != '\0') {
cfg_error(cfg, _("invalid floating point value for option '%s'"), opt->name);
return NULL;
}
if (errno == ERANGE) {
cfg_error(cfg, _("floating point value for option '%s' is out of range"), opt->name);
return NULL;
}
}
val->fpnumber = f;
break;
case CFGT_STR:
if (opt->parsecb) {
s = NULL;
if ((*opt->parsecb) (cfg, opt, value, &s) != 0)
return NULL;
} else {
s = value;
}
if (!s) {
errno = EINVAL;
return NULL;
}
free(val->string);
val->string = strdup(s);
if (!val->string)
return NULL;
break;
case CFGT_SEC:
if (is_set(CFGF_MULTI, opt->flags) || val->section == 0) {
if (val->section) {
val->section->path = NULL; /* Global search path */
cfg_free(val->section);
}
val->section = calloc(1, sizeof(cfg_t));
if (!val->section)
return NULL;
val->section->name = strdup(opt->name);
if (!val->section->name) {
free(val->section);
return NULL;
}
val->section->flags = cfg->flags;
val->section->filename = cfg->filename ? strdup(cfg->filename) : NULL;
if (cfg->filename && !val->section->filename) {
free(val->section->name);
free(val->section);
return NULL;
}
val->section->line = cfg->line;
val->section->errfunc = cfg->errfunc;
val->section->title = value ? strdup(value) : NULL;
if (value && !val->section->title) {
free(val->section->filename);
free(val->section->name);
free(val->section);
return NULL;
}