-
Notifications
You must be signed in to change notification settings - Fork 367
/
echosrv-conf.c
1330 lines (1144 loc) · 39.2 KB
/
echosrv-conf.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
/* Generated by conf2struct (https://www.rutschle.net/tech/conf2struct/README)
* on Sun Sep 8 23:10:29 2024.
# conf2struct: generate libconf parsers that read to structs
# Copyright (C) 2018-2024 Yves Rutschle
# 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.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS 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 COPYRIGHT HOLDER 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.
*/
#define _GNU_SOURCE
#include <string.h>
#ifdef LIBCONFIG
# include <libconfig.h>
#endif
#include <stdlib.h>
#include <stddef.h>
#include <stdio.h>
#include <stdarg.h>
#include <setjmp.h>
#include "echosrv-conf.h"
#include "argtable3.h"
#ifdef LIBPCRE
#define PCRE2_CODE_UNIT_WIDTH 8
#include <pcre2.h>
typedef struct {
PCRE2_SIZE rm_so;
PCRE2_SIZE rm_eo;
} regmatch_t;
#else
#include <regex.h>
#endif
/* This gets included in the output .c file */
/* Libconfig 1.4.9 is still used by major distributions
* (e.g. CentOS7) and had a different name for
* config_setting_lookup */
#if LIBCONFIG_VER_MAJOR == 1
#if LIBCONFIG_VER_MINOR == 4
#if LIBCONFIG_VER_REVISION == 9
#define config_setting_lookup config_lookup_from
#endif
#endif
#endif
/* config_type, lookup_fns, type2str are related, keep them together */
typedef enum {
CFG_BOOL,
CFG_INT,
CFG_INT64,
CFG_FLOAT,
CFG_STRING,
CFG_GROUP,
CFG_ARRAY,
CFG_LIST,
} config_type;
/* /config_type */
const char* type2str[] = {
"boolean",
"int",
"int64",
"float",
"string",
"group",
"array",
"list",
};
typedef union {
int def_bool;
int def_int;
long long def_int64;
double def_float;
char* def_string;
} any_val;
struct config_desc {
const char* name;
int type;
struct config_desc * sub_group; /* Table for compound types (list and group) */
void* arg_cl; /* command-line argument for this setting */
void* base_addr; /* Base of the structure (filled at runtime). Probably not useable for list elements */
size_t offset; /* Offset of setting in the structure */
size_t offset_len; /* Offset of *_len field, for arrays and lists */
size_t offset_present; /* offset of *_is_present field, for optional settings */
size_t size; /* Size of element, or size of group for groups and lists */
int array_type; /* type of array elements, when type == CFG_ARRAY */
int mandatory;
int optional;
any_val default_val;
};
#ifndef LIBCONFIG
/* Stubs in case you don't want libconfig */
typedef void config_setting_t;
typedef int config_t;
#define CONFIG_TRUE 1
#define CONFIG_FALSE 0
#define make_config_setting_lookup(type) \
int config_setting_lookup_##type(const config_setting_t* a, const char* b, void* c) { \
return 0; \
}
#define make_config_setting_get(type, ret_type) \
ret_type config_setting_get_##type(const config_setting_t* a) { \
return 0; \
}
enum {
CONFIG_TYPE_INT,
CONFIG_TYPE_BOOL,
CONFIG_TYPE_INT64,
CONFIG_TYPE_FLOAT,
CONFIG_TYPE_STRING
};
make_config_setting_lookup(bool);
make_config_setting_lookup(int);
make_config_setting_lookup(int64);
make_config_setting_lookup(float);
make_config_setting_lookup(string);
make_config_setting_get(bool, int);
make_config_setting_get(int, int);
make_config_setting_get(int64, long long int);
make_config_setting_get(float, double);
make_config_setting_get(string, char*);
config_setting_t* config_root_setting(config_t* c) {
return NULL;
}
config_setting_t* config_lookup(config_t* c, const char* b) {
return NULL;
}
void config_init(config_t* c) {
return;
}
char* config_setting_name(config_setting_t* c) {
return NULL;
}
int config_setting_is_list(config_setting_t* c) {
return 0;
}
int config_setting_is_array(config_setting_t* c) {
return 0;
}
int config_setting_is_scalar(config_setting_t* c) {
return 0;
}
int config_setting_index(const config_setting_t *setting) {
return 0;
}
config_setting_t* config_setting_lookup(config_setting_t* a, char* b) {
return NULL;
}
int config_setting_remove(config_setting_t* cfg, char* name) {
return 0;
}
int config_setting_type(config_setting_t* s) {
return -1;
}
int config_setting_length(config_setting_t* a) {
return 0;
}
config_setting_t* config_setting_get_elem(config_setting_t* a, int i) {
return NULL;
}
int config_read_file(config_t* a, const char* b) {
return CONFIG_TRUE;
}
int config_error_line(config_t* c) {
return 0;
}
char* config_error_text(config_t* c) {
return NULL;
}
#endif
static jmp_buf c2s_asprintf_fail;
static int c2s_asprintf(char **restrict strp, const char *restrict fmt, ...)
{
va_list ap;
va_start(ap,fmt);
int res = vasprintf(strp, fmt, ap);
va_end(ap);
if (res == -1) longjmp(c2s_asprintf_fail, res);
return res;
}
/* This is the same as config_setting_lookup_string() except
it allocates a new string which belongs to the caller */
static int myconfig_setting_lookup_stringcpy(
const config_setting_t* setting,
const char* name,
char** value)
{
const char* str;
*value = NULL;
if (config_setting_lookup_string(setting, name, &str) == CONFIG_TRUE) {
c2s_asprintf(value, "%s", str);
return CONFIG_TRUE;
} else {
return CONFIG_FALSE;
}
}
typedef int (*lookup_fn)(const config_setting_t*, const char*, void*);
lookup_fn lookup_fns[] = {
(lookup_fn)config_setting_lookup_bool,
(lookup_fn)config_setting_lookup_int,
(lookup_fn)config_setting_lookup_int64,
(lookup_fn)config_setting_lookup_float,
(lookup_fn)myconfig_setting_lookup_stringcpy,
NULL, /* CFG_GROUP */
NULL, /* CFG_ARRAY */
NULL, /* CFG_LIST */
};
/* Copy an any_val to arbitrary memory location */
/* 0: success
* <0: error */
static int any_valcpy(config_type type, void* target, any_val val)
{
switch(type) {
case CFG_BOOL:
*(int*)target = val.def_bool;
break;
case CFG_INT:
*(int*)target = val.def_int;
break;
case CFG_INT64:
*(long long*)target = val.def_int64;
break;
case CFG_FLOAT:
*(double*)target = val.def_float;
break;
case CFG_STRING:
*(char**)target = val.def_string;
break;
default:
fprintf(stderr, "Unknown type specification %d\n", type);
return -1;
}
return 1;
}
/* Copy the value of a setting to an arbitrary memory that
* must be large enough */
/* 0: success
* <0: error */
static int settingcpy(config_type type, void* target, const config_setting_t* setting)
{
any_val val;
char* str;
switch(type) {
case CFG_BOOL:
val.def_bool = config_setting_get_bool(setting);
*(int*)target = val.def_bool;
break;
case CFG_INT:
val.def_int = config_setting_get_int(setting);
*(int*)target = val.def_int;
break;
case CFG_INT64:
val.def_int64 = config_setting_get_int64(setting);
*(long long*)target = val.def_int64;
break;
case CFG_FLOAT:
val.def_float = config_setting_get_float(setting);
*(double*)target = val.def_int64;
break;
case CFG_STRING:
c2s_asprintf(&str, "%s", config_setting_get_string(setting));
val.def_string = str;
*(char**)target = val.def_string;
break;
default:
fprintf(stderr, "Unknown type specification %d\n", type);
return -1;
}
return 0;
}
/* Copy the value of a command line arg to arbitrary memory
* that must be large enough for the type */
/* 0: success
* <0: error */
static int clcpy(config_type type, void* target, const void* cl_arg)
{
any_val val;
char* str;
switch(type) {
case CFG_BOOL:
val.def_bool = (*(struct arg_lit**)cl_arg)->count;
*(int*)target = val.def_bool;
break;
case CFG_INT:
val.def_int = (*(struct arg_int**)cl_arg)->ival[0];
*(int*)target = val.def_int;
break;
case CFG_INT64:
val.def_int64 = (*(struct arg_int**)cl_arg)->ival[0];
*(long long*)target = val.def_int64;
break;
case CFG_FLOAT:
val.def_float = (*(struct arg_dbl**)cl_arg)->dval[0];
*(double*)target = val.def_float;
break;
case CFG_STRING:
c2s_asprintf(&str, "%s", (*(struct arg_str**)cl_arg)->sval[0]);
val.def_string = str;
*(char**)target = val.def_string;
break;
default:
fprintf(stderr, "Unknown type specification %d\n", type);
return -1;
}
return 0;
}
/* Copy the value of a string argument to arbitary memory
* location that must be large enough, converting on the way
* (i.e. CFG_INT gets atoi() and so on) */
/* 0: success
* <0: error */
static int stringcpy(config_type type, void* target, char* from)
{
any_val val;
switch(type) {
case CFG_BOOL:
val.def_bool = (*from != '0');
*(int*)target = val.def_bool;
break;
case CFG_INT:
val.def_int = strtol(from, NULL, 10);
*(int*)target = val.def_int;
break;
case CFG_INT64:
val.def_int64 = strtoll(from, NULL, 10);
*(long long*)target = val.def_int64;
break;
case CFG_FLOAT:
val.def_float = strtod(from, NULL);
*(double*)target = val.def_float;
break;
case CFG_STRING:
val.def_string = from;
*(char**)target = val.def_string;
break;
default:
fprintf(stderr, "Unknown type specification %d\n", type);
return -1;
}
return 0;
}
/* Element to describe the target of a compound element
* element: which config entry is being changed
* match: if >0, index in pmatch to set
* if 0, don't match but init with value
* value: constant if not matching */
struct compound_cl_target {
struct config_desc * element;
int match;
any_val value;
};
/* Element to describe one compound command line argument
* An argument is string that gets matched against a regex,
* then match-groups get evaluated to each targets[].
* For lists, base_entry points to the config_setting so we
* can append to it */
struct compound_cl_arg {
const char* regex;
struct arg_str** arg_cl; /* arg_str entry for this compound option */
struct config_desc * base_entry;
struct compound_cl_target* targets;
/* If override_desc is set, it points to the descriptor of the element in
the group which will be checked for override. Then, override_matchindex
indicates the command-line parameter match used to compare against
override_desc to know if this group is overridden. If override_matchindex
is 0, we don't match from the command-line but from a constant stored in
override_const instead */
struct config_desc * override_desc;
int override_matchindex;
char* override_const;
};
struct arg_file* echocfg_conffile;
struct arg_lit* echocfg_udp;
struct arg_str* echocfg_prefix;
struct arg_str* echocfg_listen_host;
struct arg_str* echocfg_listen_port;
struct arg_str* echocfg_listen;
struct arg_end* echocfg_end;
static struct config_desc table_echocfg_listen[] = {
{
/* name */ "host",
/* type */ CFG_STRING,
/* sub_group*/ NULL,
/* arg_cl */ & echocfg_listen_host,
/* base_addr */ NULL,
/* offset */ offsetof(struct echocfg_listen_item, host),
/* offset_len */ 0,
/* offset_present */ 0,
/* size */ sizeof(char*),
/* array_type */ -1,
/* mandatory */ 1,
/* optional */ 0,
/* default_val*/ .default_val.def_string = NULL
},
{
/* name */ "port",
/* type */ CFG_STRING,
/* sub_group*/ NULL,
/* arg_cl */ & echocfg_listen_port,
/* base_addr */ NULL,
/* offset */ offsetof(struct echocfg_listen_item, port),
/* offset_len */ 0,
/* offset_present */ 0,
/* size */ sizeof(char*),
/* array_type */ -1,
/* mandatory */ 1,
/* optional */ 0,
/* default_val*/ .default_val.def_string = NULL
},
{ 0 }
};
static struct config_desc table_echocfg[] = {
{
/* name */ "udp",
/* type */ CFG_BOOL,
/* sub_group*/ NULL,
/* arg_cl */ & echocfg_udp,
/* base_addr */ NULL,
/* offset */ offsetof(struct echocfg_item, udp),
/* offset_len */ 0,
/* offset_present */ 0,
/* size */ sizeof(int),
/* array_type */ -1,
/* mandatory */ 0,
/* optional */ 0,
/* default_val*/ .default_val.def_bool = 0
},
{
/* name */ "prefix",
/* type */ CFG_STRING,
/* sub_group*/ NULL,
/* arg_cl */ & echocfg_prefix,
/* base_addr */ NULL,
/* offset */ offsetof(struct echocfg_item, prefix),
/* offset_len */ 0,
/* offset_present */ 0,
/* size */ sizeof(char*),
/* array_type */ -1,
/* mandatory */ 1,
/* optional */ 0,
/* default_val*/ .default_val.def_string = NULL
},
{
/* name */ "listen",
/* type */ CFG_LIST,
/* sub_group*/ table_echocfg_listen,
/* arg_cl */ NULL,
/* base_addr */ NULL,
/* offset */ offsetof(struct echocfg_item, listen),
/* offset_len */ offsetof(struct echocfg_item, listen_len),
/* offset_present */ 0,
/* size */ sizeof(struct echocfg_listen_item),
/* array_type */ -1,
/* mandatory */ 1,
/* optional */ 0,
/* default_val*/ .default_val.def_int = 0
},
{ 0 }
};
static struct compound_cl_target echocfg_listen_targets [] = {
{ & table_echocfg_listen[0], 1, .value.def_string = "0" },
{ & table_echocfg_listen[1], 2, .value.def_string = "0" },
{ 0 }
};
static struct compound_cl_arg compound_cl_args[] = {
{ /* arg: listen */
.regex = "(.+):(\\w+)",
.arg_cl = & echocfg_listen,
.base_entry = & table_echocfg [2],
.targets = echocfg_listen_targets,
.override_desc = NULL,
.override_matchindex = 0,
.override_const = NULL,
},
{ 0 }
};
/* Enable debug to follow the parsing of tables */
#if 0
#define TRACE_READ(x) printf x
#define TRACE_READ_PRINT_SETTING 1
#else
#define TRACE_READ(x)
#define TRACE_READ_PRINT_SETTING 0
#endif
/* Enable debug to follow the parsing of compound options */
#if 0
#define TRACE_CMPD(x) printf x
#define TRACE_CMPD_PRINT_SETTING 1
#else
#define TRACE_CMPD(x)
#define TRACE_CMPD_PRINT_SETTING 0
#endif
static void print_setting(config_type type, void* val)
{
if (TRACE_READ_PRINT_SETTING || TRACE_CMPD_PRINT_SETTING) {
switch(type) {
case CFG_BOOL:
case CFG_INT:
printf("%d", *(int*)val);
break;
case CFG_INT64:
printf("%lld", *(long long*)val);
break;
case CFG_FLOAT:
printf("%f", *(double*)val);
break;
case CFG_STRING:
printf("`%s'", *(char**)val);
break;
case CFG_GROUP:
case CFG_LIST:
case CFG_ARRAY:
break;
}
}
}
/* Changes all dashes to underscores in a string of
* vice-versa */
static void strswap_ud(const char target, char* str)
{
char* c;
for (c = str; *c; c++)
if (*c == (target == '_' ? '-' : '_'))
*c = (target == '_' ? '_' : '-');
}
/* Same as config_setting_lookup() but looks up with dash or
* underscore so `my_setting` and `my-setting` match the same */
static config_setting_t* config_setting_lookup_ud(config_setting_t* cfg, struct config_desc* desc)
{
config_setting_t* setting;
char name[strlen(desc->name)+1];;
strcpy(name, desc->name);
strswap_ud('_', name);
setting = config_setting_lookup(cfg, name);
if (setting)
return setting;
strswap_ud('-', name);
setting = config_setting_lookup(cfg, name);
return setting;
}
static int lookup_typed_ud(config_setting_t* cfg, void* target, struct config_desc *desc)
{
lookup_fn lookup_fn = lookup_fns[desc->type];
char name[strlen(desc->name)+1];;
strcpy(name, desc->name);
strswap_ud('_', name);
if (lookup_fn(cfg, name, ((char*)target) + desc->offset) == CONFIG_TRUE)
return CONFIG_TRUE;
strswap_ud('-', name);
return lookup_fn(cfg, name, ((char*)target) + desc->offset);
}
/* Removes a setting, trying both underscores and dashes as
* name (so deleting 'my-setting' deletes both 'my_setting'
* and 'my-setting') */
static int setting_delete_ud(config_setting_t* cfg, struct config_desc *desc)
{
char name[strlen(desc->name)+1];;
strcpy(name, desc->name);
strswap_ud('_', name);
if (config_setting_remove(cfg, name) == CONFIG_TRUE)
return CONFIG_TRUE;
strswap_ud('-', name);
return config_setting_remove(cfg, name);
}
/* When traversing configuration, allocate memory for plural
* types, init for scalars */
static void read_block_init(void* target, config_setting_t* cfg, struct config_desc* desc)
{
size_t len = 0;
void* block;
config_setting_t* setting;
switch (desc->type) {
case CFG_LIST:
case CFG_ARRAY:
if (cfg) {
setting = config_setting_lookup_ud(cfg, desc);
if (setting)
len = config_setting_length(setting);
}
block = calloc(len, desc->size);
*(size_t*)(((char*)target) + desc->offset_len) = len;
*(void**)(((char*)target) + desc->offset) = block;
TRACE_READ((" sizing for %zu elems ", len));
break;
case CFG_GROUP:
block = calloc(1, desc->size);
*(void**)(((char*)target) + desc->offset) = block;
TRACE_READ((" sizing for %zu elems ", len));
break;
default:
/* scalar types: copy default */
memcpy(((char*)target) + desc->offset, &desc->default_val, desc->size);
TRACE_READ(("setting %s to default ", desc->name));
print_setting(desc->type,(char*)target + desc->offset);
break;
}
}
static int read_block(config_setting_t* cfg,
void* target,
struct config_desc* desc,
char** errmsg);
/* When traversing configuration, set value from config
* file, or command line
* return: 0 if not set, 1 if set somehow */
static int read_block_setval(void* target,
config_setting_t* cfg,
struct config_desc* desc,
char** errmsg)
{
int i;
size_t len = 0;
void* block;
int in_cfg = 0, in_cl = 0; /* Present in config file? present on command line? */
config_setting_t* setting = NULL;
switch (desc->type) {
case CFG_LIST:
if (cfg) {
setting = config_setting_lookup_ud(cfg, desc);
if (setting)
len = config_setting_length(setting);
block = *(void**)(((char*)target) + desc->offset);
for (i = 0; i < len; i++) {
config_setting_t* elem = config_setting_get_elem(setting, i);
if (!read_block(elem, (char*)block + desc->size * i, desc->sub_group, errmsg))
return 0;
}
}
break;
case CFG_ARRAY:
if (cfg) {
setting = config_setting_lookup_ud(cfg, desc);
if (setting)
len = config_setting_length(setting);
block = *(void**)(((char*)target) + desc->offset);
for (i = 0; i < len; i++) {
config_setting_t* elem = config_setting_get_elem(setting, i);
settingcpy(desc->array_type, (char*)block + desc->size * i, elem);
TRACE_READ(("[%d] = ", i));
print_setting(desc->array_type, (char*)block + desc->size *i); TRACE_READ(("\n"));
}
setting_delete_ud(cfg, desc);
}
break;
case CFG_GROUP:
if (cfg) setting = config_setting_lookup_ud(cfg, desc);
block = *(void**)(((char*)target) + desc->offset);
if (!read_block(setting, block, desc->sub_group, errmsg)) return 0;
break;
default: /* scalar types */
TRACE_READ((" `%s'", desc->name));
if (cfg && config_setting_lookup_ud(cfg, desc)) {
TRACE_READ((" in config file: "));
/* setting is present in cfg, look it up */
if (lookup_typed_ud(cfg, target, desc) != CONFIG_TRUE) {
TRACE_READ((" but wrong type (expected %s) ", type2str[desc->type]));
c2s_asprintf(errmsg, "Option \"%s\" wrong type, expected %s\n",
desc->name, type2str[desc->type]);
return 0;
}
print_setting(desc->type, (((char*)target) + desc->offset));
setting_delete_ud(cfg, desc);
in_cfg = 1;
} else {
TRACE_READ((" not in config file"));
}
if (desc->arg_cl && (*(struct arg_int**)desc->arg_cl)->count) {
clcpy(desc->type, ((char*)target) + desc->offset, desc->arg_cl);
TRACE_READ((", command line sets to "));
print_setting(desc->type, (((char*)target) + desc->offset));
in_cl = 1;
} else {
TRACE_READ((", not in command line"));
}
if (!(in_cfg || in_cl)) {
TRACE_READ(("\n"));
return 0;
}
TRACE_READ(("\n"));
break;
}
return 1;
}
/* Set *_is_present flag for target */
static void target_is_present(void* target, struct config_desc* desc, int val)
{
if (desc->optional) { /* _is_present only exists in target for optional settings */
TRACE_READ((" mark as set"));
*(int*)((char*)target + desc->offset_present) = val;
}
}
/* traverses the configuration; allocates memory if needed,
* set to default if exists,
* fill from configuration file if present, overrides or set from
* command line if present, verifies mandatory options have
* been set
* target: base address of the group being processed
*/
static int read_block(config_setting_t* cfg, void* target, struct config_desc* desc, char** errmsg)
{
int set;
for (; desc->name; desc++) {
TRACE_READ(("reading %s%s%s: ", desc->optional ? "optional " : "", desc->mandatory ? "mandatory " : "", desc->name));
desc->base_addr = target;
read_block_init(target, cfg, desc);
set = read_block_setval(target, cfg, desc, errmsg);
if (!set && desc->mandatory) {
c2s_asprintf(errmsg, "Mandatory option \"%s\" not found", desc->name);
return 0;
}
if (desc->optional) target_is_present(target, desc, set && desc->optional);
}
return 1;
}
/* Copy regex match into newly allocated string
* out: newly allocated string (caller has to free it)
* in: string into which the match was made
* pmatch: the match to extract */
static void pmatchcpy(char** out, const char* in, regmatch_t* pmatch)
{
int len = pmatch->rm_eo - pmatch->rm_so;
*out = calloc(len+1, 1);
memcpy(*out, in + pmatch->rm_so, len);
}
/* Processes a list of targets within one element, setting
* the values in the target setting
* target: where to put the data
* arg: CL arg containing the target fields
* clval: command line parameter
* pmatch: regex match array into clval
*/
static int set_target_fields(void* target_addr, struct compound_cl_arg* arg, const char* clval, regmatch_t* pmatch)
{
int pmatch_cnt = 1;
struct compound_cl_target* target;
for (target = arg->targets; target->element; target++) {
struct config_desc * element = target->element;
if (target->match) {
TRACE_CMPD((" match %d rm_so %d rm_eo %d type %d\n",
pmatch_cnt, pmatch[pmatch_cnt].rm_so, pmatch[pmatch_cnt].rm_eo, element->type ));
if (pmatch[pmatch_cnt].rm_so == -1) {
/* This should not happen as regexec() did
* match before, unless there is a
* discrepency between the regex and the
* number of backreferences */
return 0;
}
char* str;
pmatchcpy(&str, clval, &pmatch[pmatch_cnt]);
stringcpy(element->type, (char*)target_addr + element->offset, str);
TRACE_CMPD(("setting %p+%zu to : ", target_addr , element->offset));
print_setting(element->type , (char*)target_addr + element->offset);
TRACE_CMPD(("\n"));
/* str is temporary buffer for type conversion, except for strings which we
* need to keep around so don't free them */
if (element->type != CFG_STRING)
free(str);
pmatch_cnt++;
} else { /* don't use matching, set constant */
any_valcpy(element->type, (char*)target_addr + element->offset,
target->value);
}
}
return 1;
}
/* Goes over a list, finds if a group matches the specified string and overwrite
* it if it does. */
static int override_on_str(struct compound_cl_arg* arg, const char* str, regmatch_t* pmatch)
{
struct config_desc * desc = arg->base_entry;
void* list_base = *(void**)(desc->base_addr + desc->offset);
size_t list_len = *(size_t*)(desc->base_addr + desc->offset_len);
size_t elem_size = desc->size;
int i;
for (i = 0; i < list_len; i++) {
char* group_base = ((char*)list_base + i * elem_size);
char* cfg_member = *(char**)(group_base + arg->override_desc->offset);
if (!strcmp(str, cfg_member)) {
memset(group_base, 0, elem_size);
struct arg_str* arg_cl = *arg->arg_cl;
if (!set_target_fields(group_base, arg, arg_cl->sval[0], pmatch))
return 0;
return 1;
}
}
return 0;
}
/* Goes over a list and override group if needed */
static int override_elem(struct compound_cl_arg* arg, int arg_index, regmatch_t* pmatch)
{
char* str;
int allocated = 0;
int res;
if (arg->override_matchindex) {
struct arg_str* arg_cl = *arg->arg_cl;
pmatchcpy(&str, arg_cl->sval[arg_index], &pmatch[arg->override_matchindex]);
allocated = 1;
} else {
str = arg->override_const;
}
res = override_on_str(arg, str, pmatch);
if (allocated) free(str);
return res;
}
/* Add an argument to a list, overriding if required or
* appending otherwise */
static int add_arg_to_list(struct compound_cl_arg* arg, int arg_index, regmatch_t* pmatch)
{
struct config_desc * desc = arg->base_entry;
void* list_base = *(void**)(desc->base_addr + desc->offset);
size_t list_len = *(size_t*)(desc->base_addr + desc->offset_len);
size_t elem_size = desc->size;
/* are we overriding an existing group? */
if (arg->override_desc)
if (override_elem(arg, arg_index, pmatch))
return 1;
/* override not found or no override, append element and * zero it out */
list_len++;
list_base = realloc(list_base, list_len * elem_size);
*(size_t*)(desc->base_addr + desc->offset_len) = list_len;
*(void**)(desc->base_addr + desc->offset) = list_base;
memset(list_base + (list_len - 1) * elem_size, 0, elem_size);
struct arg_str* arg_cl = *arg->arg_cl;
if (!set_target_fields((char*)list_base + (list_len - 1) * elem_size, arg, arg_cl->sval[arg_index], pmatch)) {
return 0;
}
return 1;
}
/* TODO: pass pmatch size as parameter or something */
#define MAX_MATCH 10
#ifndef LIBPCRE
static int regcompmatch_posix( regmatch_t* pmatch,
struct compound_cl_arg* arg,
int arg_index,
char** errmsg)
{
char* regerr;
struct arg_str* arg_cl = *arg->arg_cl;
regex_t preg;
int res = regcomp(&preg, arg->regex, REG_EXTENDED);
if (res) {
int errlen = regerror(res, &preg, NULL, 0);
regerr = malloc(errlen);
regerror(res, &preg, regerr, errlen);
c2s_asprintf(errmsg, "compiling pattern /%s/:%s", arg->regex, regerr);
free(regerr);
return 0;