forked from c-icap/c-icap-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cfg_lib.c
619 lines (523 loc) · 16.6 KB
/
cfg_lib.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
/*
* Copyright (C) 2004-2010 Christos Tsantilas
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This program 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301 USA.
*/
#include "common.h"
#include "c-icap.h"
#include <errno.h>
#include "cfg_param.h"
#include "mem.h"
#include "debug.h"
#include <util.h>
#include <ctype.h>
#if defined(HAVE_GNU_LIBC_VERSION_H)
#include <gnu/libc-version.h>
#endif
/*************************************************************************/
/* Memory managment for config parameters definitions and implementation */
#define ALLOCATOR_SIZE 65536
ci_mem_allocator_t *cfg_params_allocator = NULL;
void ci_cfg_lib_init()
{
cfg_params_allocator = ci_create_serial_allocator(ALLOCATOR_SIZE);
}
void ci_cfg_lib_reset()
{
cfg_params_allocator->reset(cfg_params_allocator);
}
void ci_cfg_lib_destroy()
{
ci_mem_allocator_destroy(cfg_params_allocator);
}
void *ci_cfg_alloc_mem(int size)
{
return cfg_params_allocator->alloc(cfg_params_allocator, size);
}
const char *ci_lib_version_string() {
return VERSION;
}
/*ci_conf_table functions*/
ci_conf_entry_t *ci_cfg_conf_table_allocate(int entries)
{
ci_conf_entry_t *table = (ci_conf_entry_t *)calloc(entries + 1, sizeof(ci_conf_entry_t));
return table;
}
void ci_cfg_conf_table_release(ci_conf_entry_t *table)
{
if (table)
free(table);
}
void ci_cfg_conf_table_push(ci_conf_entry_t *table, int entries, const char *name, void *data, int (*action)(const char *, const char **argv, void *), const char *msg )
{
int i;
if (!table)
return;
for(i = 0; i < entries; i++) {
if (table[i].name == NULL) {
table[i].name = name;
table[i].data = data;
table[i].action = action;
table[i].msg = msg;
return;
}
}
}
int ci_cfg_conf_table_configure(ci_conf_entry_t *table, const char *table_name, const char *varname, const char **argv)
{
ci_conf_entry_t *entry = NULL;
int i;
for (i = 0; table[i].name != NULL; i++) {
if (0 == strcmp(varname, table[i].name))
entry = &table[i];
}
if (!entry) {
ci_debug_printf(1, "Variable %s%s%s not found!\n", table_name, ((table_name && table_name[0]) ? "." : "" ),varname);
return 0;
}
return entry->action(entry->name, argv, entry->data);
}
/****************************************************************/
/* Command line options implementation, function and structures */
static char *wrap_msg(size_t first_line_pos, size_t ident_pos, int line_size, char *buf, size_t buf_size, const char *msg)
{
const char *s = msg;
char *be = buf + buf_size - 1;
char *b = buf;
size_t line_pos = first_line_pos;
for (b = buf; b < be && *s != '\0'; s++) {
if (*s == '\n')
*b = *s;
else if (isspace(*s)) {
// check if there is enough space in line for the next word:
size_t next_space;
for (next_space = 1; *(s + next_space) != '\0' && !isspace(*(s + next_space)); next_space++);
*b = ((next_space + line_pos) >= line_size) ? '\n' : ' ';
} else
*b = *s;
if (*b == '\n')
line_pos = 0;
else
line_pos++;
b++;
if (line_pos == 0) {
/*write spaces up to ident_pos*/
while (b < be && line_pos < ident_pos) *b = ' ', line_pos++, b++;
}
}
*b = '\0';
return buf;
}
#define max(x,y) ((x)>(y)?(x):(y))
#define min(x,y) ((x)>(y)?(y):(x))
void ci_args_usage(const char *progname, struct ci_options_entry *options)
{
int i;
printf("Usage :\n");
printf("%s", progname);
for (i = 0; options[i].name != NULL; i++) {
if (options[i].name[0] == '$')
printf(" [file1] [file2] ...");
else
printf(" [%s %s]", options[i].name,
(options[i].parameter == NULL ? "" : options[i].parameter));
}
printf("\nOptions:\n");
/*Find the maximum string length of option/parameter pairs:*/
const char *files_opt_name = "[file1] [file2] ...";
size_t max_len = 0;
for (i = 0; options[i].name != NULL; i++) {
const char *pname = (options[i].name[0] == '$' ? files_opt_name : options[i].name);
const char *pval = (options[i].parameter == NULL ? "" : options[i].parameter);
const size_t param_len = snprintf(NULL, 0, "%s %s", pname, pval);
if (max_len < param_len)
max_len = param_len;
}
const size_t screen_cols = ci_screen_columns();
size_t ident_pos = min(max_len, screen_cols/3) + 2 /*at the beginning*/ + 1 /*before message*/;
for (i = 0; options[i].name != NULL; i++) {
char param[128];
char msg[1024];
const char *pname = (options[i].name[0] == '$' ? files_opt_name : options[i].name);
const char *pval = (options[i].parameter == NULL ? "" : options[i].parameter);
snprintf(param, sizeof(param), " %s %s ", pname, pval);
size_t msg_start = max(ident_pos + 1, strlen(param) + 1);
printf("%-*s %s\n", (int)ident_pos, param,
wrap_msg(msg_start, ident_pos + 1, screen_cols, msg, sizeof(msg), options[i].msg));
}
}
struct ci_options_entry *search_options_table(const char *directive,
struct ci_options_entry *options)
{
int i;
const char *option_search;
if (directive[0] != '-')
option_search = "$$";
else
option_search = directive;
for (i = 0; options[i].name != NULL; i++) {
if (0 == strcmp(option_search, options[i].name))
return &options[i];
}
return NULL;
}
int ci_args_apply(int argc, char *argv[], struct ci_options_entry *options)
{
int i;
struct ci_options_entry *entry;
const char *act_args[2];
act_args[1] = NULL;
for (i = 1; i < argc; i++) {
if ((entry = search_options_table(argv[i], options)) == NULL)
return 0;
if (entry->parameter) {
if (++i >= argc)
return 0;
act_args[0] = argv[i];
if (!(*(entry->action)) (entry->name, act_args, entry->data))
return 0;
} else {
/*maybe is the "$$" directive ....*/
if (strcmp(entry->name, "$$") == 0) {
act_args[0] = argv[i];
if (!(*(entry->action)) (entry->name, act_args, entry->data))
return 0;
} else {
if (!(*(entry->action)) (entry->name, NULL, entry->data))
return 0;
}
}
}
return 1;
}
/****************************************************************************/
/*Various functions for setting parameters from command line or config file */
int ci_cfg_set_int(const char *directive, const char **argv, void *setdata)
{
int val = 0;
char *end;
if (setdata == NULL)
return 0;
if (argv == NULL || argv[0] == NULL) {
ci_debug_printf(1, "Missing arguments in directive:%s\n", directive);
return 0;
}
errno = 0;
val = strtoll(argv[0], &end, 10);
if ((val == 0 && errno != 0))
return 0;
*((int *) setdata) = val;
ci_debug_printf(2, "Setting parameter: %s=%d\n", directive, val);
return 1;
}
int ci_cfg_set_str(const char *directive, const char **argv, void *setdata)
{
if (setdata == NULL)
return 0;
if (argv == NULL || argv[0] == NULL) {
return 0;
}
const size_t str_size = strlen(argv[0]) + 1;
if (!(*((char **) setdata) = ci_cfg_alloc_mem(str_size))) {
return 0;
}
strncpy(*((char **) setdata), argv[0], str_size);
(*((char **) setdata))[str_size - 1] = '\0';
ci_debug_printf(2, "Setting parameter: %s=%s\n", directive, argv[0]);
return 1;
}
int ci_cfg_onoff(const char *directive, const char **argv, void *setdata)
{
if (setdata == NULL)
return 0;
if (argv == NULL || argv[0] == NULL) {
ci_debug_printf(1, "Missing arguments in directive:%s\n", directive);
return 0;
}
if (strcasecmp(argv[0], "on") == 0)
*((int *) setdata) = 1;
else if (strcasecmp(argv[0], "off") == 0)
*((int *) setdata) = 0;
else
return 0;
ci_debug_printf(2, "Setting parameter: %s=%d\n", directive,
*((int *) setdata));
return 1;
}
int ci_cfg_disable(const char *directive, const char **argv, void *setdata)
{
if (setdata == NULL)
return 0;
*((int *) setdata) = 0;
ci_debug_printf(2, "Disabling parameter %s\n", directive);
return 1;
}
int ci_cfg_enable(const char *directive, const char **argv, void *setdata)
{
if (setdata == NULL)
return 0;
*((int *) setdata) = 1;
ci_debug_printf(2, "Enabling parameter %s\n", directive);
return 1;
}
#define getUnits(suffix) ((suffix) == 'k' || (suffix) == 'K' ? 1024 : ((suffix) == 'm' || (suffix) == 'M' ? 1024 * 1024 : ((suffix) == 'g' || (suffix) == 'G' ? 1024 * 1024 * 1024 : 1)))
int ci_cfg_size_off(const char *directive, const char **argv, void *setdata)
{
ci_off_t val = 0;
char *end;
if (setdata == NULL)
return 0;
if (argv == NULL || argv[0] == NULL) {
ci_debug_printf(1, "Missing arguments in directive:%s\n", directive);
return 0;
}
errno = 0;
val = ci_strto_off_t(argv[0], &end, 10);
if ((val == 0 && errno != 0) || val < 0)
return 0;
val = val * getUnits(*end);
if (val > 0)
*((ci_off_t *) setdata) = val;
ci_debug_printf(2, "Setting parameter: %s=%" PRINTF_OFF_T "\n", directive,
(CAST_OFF_T) val);
return 1;
}
int ci_cfg_size_long(const char *directive, const char **argv, void *setdata)
{
long int val = 0;
char *end;
if (setdata == NULL)
return 0;
if (argv == NULL || argv[0] == NULL) {
ci_debug_printf(1, "Missing arguments in directive: %s\n", directive);
return 0;
}
errno = 0;
val = strtol(argv[0], &end, 10);
if ((val == 0 && errno != 0) || val < 0)
return 0;
val = val * getUnits(*end);
if (val > 0)
*((long int *) setdata) = val;
ci_debug_printf(2, "Setting parameter: %s=%ld\n", directive, val);
return 1;
}
int ci_cfg_size_longlong(const char *directive, const char **argv, void *setdata)
{
long long val = 0;
char *end;
if (setdata == NULL)
return 0;
if (argv == NULL || argv[0] == NULL) {
ci_debug_printf(1, "Missing arguments in directive: %s\n", directive);
return 0;
}
errno = 0;
val = strtoll(argv[0], &end, 10);
if ((val == 0 && errno != 0) || val < 0)
return 0;
val = val * getUnits(*end);
if (val > 0)
*((long int *) setdata) = val;
ci_debug_printf(2, "Setting parameter: %s=%lld\n", directive, val);
return 1;
}
int ci_cfg_size_size_t(const char *directive, const char **argv, void *setdata)
{
long long val = 0;
int ret = ci_cfg_size_longlong(directive, argv, &val);
if (!ret)
return 0;
*((size_t *) setdata) = (size_t) val;
return 1;
}
int ci_cfg_set_octal(const char *directive, const char **argv, void *setdata)
{
int val = 0;
char *end;
if (!setdata)
return 0;
if (argv == NULL || argv[0] == NULL) {
ci_debug_printf(1, "Missing arguments in directive:%s\n", directive);
return 0;
}
errno = 0;
val = strtoll(argv[0], &end, 8);
if ((val == 0 && errno != 0))
return 0;
*((int *) setdata) = val;
ci_debug_printf(2, "Setting parameter: %s=0%.3o\n", directive, val);
return 1;
}
int ci_cfg_set_float(const char *directive,const char **argv,void *setdata)
{
float val = 0;
char *end;
if (setdata == NULL)
return 0;
if (argv == NULL || argv[0] == NULL) {
ci_debug_printf(1, "Missing arguments in directive: %s\n", directive);
return 0;
}
errno = 0;
val = strtof(argv[0], &end);
if ((val == 0 && errno != 0) || val < 0)
return 0;
*((float *) setdata) = val;
ci_debug_printf(2, "Setting parameter: %s=%f\n", directive, (double)val);
return 1;
}
int ci_cfg_set_double(const char *directive,const char **argv,void *setdata)
{
double val = 0;
char *end;
if (setdata == NULL)
return 0;
if (argv == NULL || argv[0] == NULL) {
ci_debug_printf(1, "Missing arguments in directive: %s\n", directive);
return 0;
}
errno = 0;
val = strtod(argv[0], &end);
if (val == 0 && end == argv[0])
return 0;
*((double *) setdata) = val;
ci_debug_printf(2, "Setting parameter: %s=%f\n", directive, val);
return 1;
}
int ci_cfg_set_int_range(const char *directive, const char **argv, void *setdata)
{
if (!setdata)
return 0;
struct ci_cfg_int_range *range = (struct ci_cfg_int_range *)setdata;
if (!range->data)
return 0;
int tmpVal;
if (!ci_cfg_set_int(directive, argv, (void *)&tmpVal))
return 0;
if (tmpVal < range->start || tmpVal > range->end) {
ci_debug_printf(1, "Please use an integer value between %d and %d for directive '%s'\n", range->start, range->end, directive);
return 0;
}
*range->data = tmpVal;
return 1;
}
int ci_cfg_set_double_range(const char *directive, const char **argv, void *setdata)
{
if (!setdata)
return 0;
struct ci_cfg_double_range *range = (struct ci_cfg_double_range *)setdata;
if (!range->data)
return 0;
double tmpVal;
if (!ci_cfg_set_double(directive, argv, (void *)&tmpVal))
return 0;
if (tmpVal < range->start || tmpVal > range->end) {
ci_debug_printf(1, "Please use a double value between %f and %f for directive '%s'\n", range->start, range->end, directive);
return 0;
}
*range->data = tmpVal;
return 1;
}
int ci_cfg_set_str_set(const char *directive, const char **argv, void *setdata)
{
if (!setdata)
return 0;
struct ci_cfg_string_set *set = (struct ci_cfg_string_set *)setdata;
if (!set->data)
return 0;
char *tmpVal;
if (!ci_cfg_set_str(directive, argv, (void *)&tmpVal))
return 0;
int i, found;
for (i = 0, found = 0; i < set->set_length; ++i) {
if (set->set[i] && strcasecmp(tmpVal, set->set[i]) == 0) {
found = 1;
break;
}
}
if (!found) {
ci_debug_printf(1, "Not acceptable string value '%s' for directive '%s'\n", tmpVal, directive);
return 0;
}
*set->data = tmpVal;
return 1;
}
int ci_cfg_version(const char *directive, const char **argv, void *setdata)
{
if (setdata)
*((int *) setdata) = 1;
printf("%s\n", VERSION);
return 1;
}
int ci_cfg_build_info(const char *directive, const char **argv, void *setdata)
{
if (setdata)
*((int *) setdata) = 1;
printf("c-icap version: %s\n"
"Configure script options: %s\n"
"Configured for host: %s\n"
#if defined(__clang__) /* Clang also reports gcc-4.2.1*/
"Compiled with: clang version %s\n"
#define __SUBGCC
#endif
#if defined(__MINGW32__)
"Compiled with: mingw-w32-%d.%d\n"
#define __SUBGCC
#endif
#if defined(__MINGW64__)
"Compiled with: mingw-w64-%d.%d\n"
#define __SUBGCC
#endif
#if defined(__GNUC__)
#if defined(__SUBGCC)
"With extensions for: "
#else
"Compiled with: "
#endif
"gcc-%d.%d.%d\n"
#endif
#if defined(__GLIBC__)
"Compiled with: glibc-%d.%d\n"
#if defined(HAVE_GNU_LIBC_VERSION_H)
"Running with: glibc-%s %s\n"
#endif
#endif
"%s\n",
VERSION,
C_ICAP_CONFIGURE_OPTIONS,
C_ICAP_CONFIG_HOST_TYPE,
#if defined(__clang__)
__clang_version__,
#endif
#if defined(__MINGW32__)
__MINGW32_MAJOR_VERSION, __MINGW32_MINOR_VERSION,
#endif
#if defined(__MINGW64__)
__MINGW64_VERSION_MAJOR, __MINGW64_VERSION_MINOR,
#endif
#if defined(__GNUC__)
__GNUC__, __GNUC_MINOR__, __GNUC_PATCHLEVEL__,
#endif
#if defined(__GLIBC__)
__GLIBC__, __GLIBC_MINOR__,
#if defined(HAVE_GNU_LIBC_VERSION_H)
gnu_get_libc_version(), gnu_get_libc_release(),
#endif
#endif
"");
return 1;
}