-
Notifications
You must be signed in to change notification settings - Fork 932
/
config.c
4251 lines (3676 loc) · 110 KB
/
config.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
/*
* Asterisk -- An open source telephony toolkit.
*
* Copyright (C) 1999 - 2010, Digium, Inc.
*
* Mark Spencer <markster@digium.com>
*
* See http://www.asterisk.org for more information about
* the Asterisk project. Please do not directly contact
* any of the maintainers of this project for assistance;
* the project provides a web site, mailing lists and IRC
* channels for your use.
*
* This program is free software, distributed under the terms of
* the GNU General Public License Version 2. See the LICENSE file
* at the top of the source tree.
*/
/*! \file
*
* \brief Configuration File Parser
*
* \author Mark Spencer <markster@digium.com>
*
* Includes the Asterisk Realtime API - ARA
* See http://wiki.asterisk.org
*/
/*** MODULEINFO
<support_level>core</support_level>
***/
/* This maintains the original "module reload extconfig" CLI command instead
* of replacing it with "module reload config". */
#undef AST_MODULE
#define AST_MODULE "extconfig"
#include "asterisk.h"
#include "asterisk/paths.h" /* use ast_config_AST_CONFIG_DIR */
#include "asterisk/network.h" /* we do some sockaddr manipulation here */
#include <string.h>
#include <libgen.h>
#include <time.h>
#include <sys/stat.h>
#include <math.h> /* HUGE_VAL */
#include <regex.h>
#include "asterisk/config.h"
#include "asterisk/cli.h"
#include "asterisk/lock.h"
#include "asterisk/utils.h"
#include "asterisk/channel.h"
#include "asterisk/app.h"
#include "asterisk/astobj2.h"
#include "asterisk/strings.h" /* for the ast_str_*() API */
#include "asterisk/netsock2.h"
#include "asterisk/module.h"
#define MAX_NESTED_COMMENTS 128
#define COMMENT_START ";--"
#define COMMENT_END "--;"
#define COMMENT_META ';'
#define COMMENT_TAG '-'
/*!
* Define the minimum filename space to reserve for each
* ast_variable in case the filename is renamed later by
* ast_include_rename().
*/
#define MIN_VARIABLE_FNAME_SPACE 40
static char *extconfig_conf = "extconfig.conf";
static struct ao2_container *cfg_hooks;
static void config_hook_exec(const char *filename, const char *module, const struct ast_config *cfg);
static inline struct ast_variable *variable_list_switch(struct ast_variable *l1, struct ast_variable *l2);
static int does_category_match(struct ast_category *cat, const char *category_name,
const char *match, char sep);
/*! \brief Structure to keep comments for rewriting configuration files */
struct ast_comment {
struct ast_comment *next;
/*! Comment body allocated after struct. */
char cmt[0];
};
/*! \brief Hold the mtime for config files, so if we don't need to reread our config, don't. */
struct cache_file_include {
AST_LIST_ENTRY(cache_file_include) list;
/*! Filename or wildcard pattern as specified by the including file. */
char include[0];
};
struct cache_file_mtime {
AST_LIST_ENTRY(cache_file_mtime) list;
AST_LIST_HEAD_NOLOCK(includes, cache_file_include) includes;
unsigned int has_exec:1;
/*! stat() file size */
unsigned long stat_size;
/*! stat() file modtime nanoseconds */
unsigned long stat_mtime_nsec;
/*! stat() file modtime seconds since epoc */
time_t stat_mtime;
/*! String stuffed in filename[] after the filename string. */
const char *who_asked;
/*! Filename and who_asked stuffed after it. */
char filename[0];
};
/*! Cached file mtime list. */
static AST_LIST_HEAD_STATIC(cfmtime_head, cache_file_mtime);
static int init_appendbuf(void *data)
{
struct ast_str **str = data;
*str = ast_str_create(16);
return *str ? 0 : -1;
}
AST_THREADSTORAGE_CUSTOM(appendbuf, init_appendbuf, ast_free_ptr);
/* comment buffers are better implemented using the ast_str_*() API */
#define CB_SIZE 250 /* initial size of comment buffers */
static void CB_ADD(struct ast_str **cb, const char *str)
{
ast_str_append(cb, 0, "%s", str);
}
static void CB_ADD_LEN(struct ast_str **cb, const char *str, int len)
{
char *s = ast_alloca(len + 1);
memcpy(s, str, len);
s[len] = '\0';
ast_str_append(cb, 0, "%s", s);
}
static void CB_RESET(struct ast_str *cb, struct ast_str *llb)
{
if (cb) {
ast_str_reset(cb);
}
if (llb) {
ast_str_reset(llb);
}
}
static struct ast_comment *ALLOC_COMMENT(struct ast_str *buffer)
{
struct ast_comment *x = NULL;
if (!buffer || !ast_str_strlen(buffer)) {
return NULL;
}
if ((x = ast_calloc(1, sizeof(*x) + ast_str_strlen(buffer) + 1))) {
strcpy(x->cmt, ast_str_buffer(buffer)); /* SAFE */
}
return x;
}
/* I need to keep track of each config file, and all its inclusions,
so that we can track blank lines in each */
struct inclfile {
char *fname;
int lineno;
};
static int hash_string(const void *obj, const int flags)
{
char *str = ((struct inclfile *) obj)->fname;
int total;
for (total = 0; *str; str++) {
unsigned int tmp = total;
total <<= 1; /* multiply by 2 */
total += tmp; /* multiply by 3 */
total <<= 2; /* multiply by 12 */
total += tmp; /* multiply by 13 */
total += ((unsigned int) (*str));
}
if (total < 0) {
total = -total;
}
return total;
}
static int hashtab_compare_strings(void *a, void *b, int flags)
{
const struct inclfile *ae = a, *be = b;
return !strcmp(ae->fname, be->fname) ? CMP_MATCH | CMP_STOP : 0;
}
static struct ast_config_map {
struct ast_config_map *next;
int priority;
/*! Stored in stuff[] at struct end. */
const char *name;
/*! Stored in stuff[] at struct end. */
const char *driver;
/*! Stored in stuff[] at struct end. */
const char *database;
/*! Stored in stuff[] at struct end. */
const char *table;
/*! Contents of name, driver, database, and table in that order stuffed here. */
char stuff[0];
} *config_maps = NULL;
AST_MUTEX_DEFINE_STATIC(config_lock);
static struct ast_config_engine *config_engine_list;
#define MAX_INCLUDE_LEVEL 10
struct ast_category_template_instance {
char name[80]; /* redundant? */
const struct ast_category *inst;
AST_LIST_ENTRY(ast_category_template_instance) next;
};
struct ast_category {
char name[80];
int ignored; /*!< do not let user of the config see this category -- set by (!) after the category decl; a template */
int include_level;
/*!
* \brief The file name from whence this declaration was read
* \note Will never be NULL
*/
char *file;
int lineno;
AST_LIST_HEAD_NOLOCK(template_instance_list, ast_category_template_instance) template_instances;
struct ast_comment *precomments;
struct ast_comment *sameline;
struct ast_comment *trailing; /*!< the last object in the list will get assigned any trailing comments when EOF is hit */
/*! First category variable in the list. */
struct ast_variable *root;
/*! Last category variable in the list. */
struct ast_variable *last;
/*! Previous node in the list. */
struct ast_category *prev;
/*! Next node in the list. */
struct ast_category *next;
};
struct ast_config {
/*! First config category in the list. */
struct ast_category *root;
/*! Last config category in the list. */
struct ast_category *last;
struct ast_category *current;
struct ast_category *last_browse; /*!< used to cache the last category supplied via category_browse */
int include_level;
int max_include_level;
struct ast_config_include *includes; /*!< a list of inclusions, which should describe the entire tree */
};
struct ast_config_include {
/*!
* \brief file name in which the include occurs
* \note Will never be NULL
*/
char *include_location_file;
int include_location_lineno; /*!< lineno where include occurred */
int exec; /*!< set to non-zero if its a #exec statement */
/*!
* \brief if it's an exec, you'll have both the /var/tmp to read, and the original script
* \note Will never be NULL if exec is non-zero
*/
char *exec_file;
/*!
* \brief file name included
* \note Will never be NULL
*/
char *included_file;
int inclusion_count; /*!< if the file is included more than once, a running count thereof -- but, worry not,
we explode the instances and will include those-- so all entries will be unique */
int output; /*!< a flag to indicate if the inclusion has been output */
struct ast_config_include *next; /*!< ptr to next inclusion in the list */
};
static void ast_variable_destroy(struct ast_variable *doomed);
static void ast_includes_destroy(struct ast_config_include *incls);
struct ast_variable *_ast_variable_new(const char *name, const char *value, const char *filename, const char *file, const char *func, int lineno)
{
struct ast_variable *variable;
int name_len = strlen(name) + 1;
int val_len = strlen(value) + 1;
int fn_len = strlen(filename) + 1;
/* Ensure a minimum length in case the filename is changed later. */
if (fn_len < MIN_VARIABLE_FNAME_SPACE) {
fn_len = MIN_VARIABLE_FNAME_SPACE;
}
variable = __ast_calloc(1, fn_len + name_len + val_len + sizeof(*variable),
file, lineno, func);
if (variable) {
char *dst = variable->stuff; /* writable space starts here */
/* Put file first so ast_include_rename() can calculate space available. */
variable->file = strcpy(dst, filename);
dst += fn_len;
variable->name = strcpy(dst, name);
dst += name_len;
variable->value = strcpy(dst, value);
}
return variable;
}
/*!
* \internal
* \brief Move the contents from the source to the destination variable.
*
* \param dst_var Destination variable node
* \param src_var Source variable node
*/
static void ast_variable_move(struct ast_variable *dst_var, struct ast_variable *src_var)
{
dst_var->lineno = src_var->lineno;
dst_var->object = src_var->object;
dst_var->blanklines = src_var->blanklines;
dst_var->precomments = src_var->precomments;
src_var->precomments = NULL;
dst_var->sameline = src_var->sameline;
src_var->sameline = NULL;
dst_var->trailing = src_var->trailing;
src_var->trailing = NULL;
}
struct ast_config_include *ast_include_new(struct ast_config *conf, const char *from_file, const char *included_file, int is_exec, const char *exec_file, int from_lineno, char *real_included_file_name, int real_included_file_name_size)
{
/* a file should be included ONCE. Otherwise, if one of the instances is changed,
* then all be changed. -- how do we know to include it? -- Handling modified
* instances is possible, I'd have
* to create a new master for each instance. */
struct ast_config_include *inc;
struct stat statbuf;
inc = ast_include_find(conf, included_file);
if (inc) {
do {
inc->inclusion_count++;
snprintf(real_included_file_name, real_included_file_name_size, "%s~~%d", included_file, inc->inclusion_count);
} while (stat(real_included_file_name, &statbuf) == 0);
ast_log(LOG_WARNING,"'%s', line %d: Same File included more than once! This data will be saved in %s if saved back to disk.\n", from_file, from_lineno, real_included_file_name);
} else
*real_included_file_name = 0;
inc = ast_calloc(1,sizeof(struct ast_config_include));
if (!inc) {
return NULL;
}
inc->include_location_file = ast_strdup(from_file);
inc->include_location_lineno = from_lineno;
if (!ast_strlen_zero(real_included_file_name))
inc->included_file = ast_strdup(real_included_file_name);
else
inc->included_file = ast_strdup(included_file);
inc->exec = is_exec;
if (is_exec)
inc->exec_file = ast_strdup(exec_file);
if (!inc->include_location_file
|| !inc->included_file
|| (is_exec && !inc->exec_file)) {
ast_includes_destroy(inc);
return NULL;
}
/* attach this new struct to the conf struct */
inc->next = conf->includes;
conf->includes = inc;
return inc;
}
void ast_include_rename(struct ast_config *conf, const char *from_file, const char *to_file)
{
struct ast_config_include *incl;
struct ast_category *cat;
char *str;
int from_len = strlen(from_file);
int to_len = strlen(to_file);
if (strcmp(from_file, to_file) == 0) /* no use wasting time if the name is the same */
return;
/* the manager code allows you to read in one config file, then
* write it back out under a different name. But, the new arrangement
* ties output lines to the file name. So, before you try to write
* the config file to disk, better riffle thru the data and make sure
* the file names are changed.
*/
/* file names are on categories, includes (of course), and on variables. So,
* traverse all this and swap names */
for (incl = conf->includes; incl; incl=incl->next) {
if (strcmp(incl->include_location_file,from_file) == 0) {
if (from_len >= to_len)
strcpy(incl->include_location_file, to_file);
else {
/* Keep the old filename if the allocation fails. */
str = ast_strdup(to_file);
if (str) {
ast_free(incl->include_location_file);
incl->include_location_file = str;
}
}
}
}
for (cat = conf->root; cat; cat = cat->next) {
struct ast_variable **prev;
struct ast_variable *v;
struct ast_variable *new_var;
if (strcmp(cat->file,from_file) == 0) {
if (from_len >= to_len)
strcpy(cat->file, to_file);
else {
/* Keep the old filename if the allocation fails. */
str = ast_strdup(to_file);
if (str) {
ast_free(cat->file);
cat->file = str;
}
}
}
for (prev = &cat->root, v = cat->root; v; prev = &v->next, v = v->next) {
if (strcmp(v->file, from_file)) {
continue;
}
/*
* Calculate actual space available. The file string is
* intentionally stuffed before the name string just so we can
* do this.
*/
if (to_len < v->name - v->file) {
/* The new name will fit in the available space. */
str = (char *) v->file;/* Stupid compiler complains about discarding qualifiers even though I used a cast. */
strcpy(str, to_file);/* SAFE */
continue;
}
/* Keep the old filename if the allocation fails. */
new_var = ast_variable_new(v->name, v->value, to_file);
if (!new_var) {
continue;
}
/* Move items from the old list node to the replacement node. */
ast_variable_move(new_var, v);
/* Replace the old node in the list with the new node. */
new_var->next = v->next;
if (cat->last == v) {
cat->last = new_var;
}
*prev = new_var;
ast_variable_destroy(v);
v = new_var;
}
}
}
struct ast_config_include *ast_include_find(struct ast_config *conf, const char *included_file)
{
struct ast_config_include *x;
for (x=conf->includes;x;x=x->next) {
if (strcmp(x->included_file,included_file) == 0)
return x;
}
return 0;
}
void ast_variable_append(struct ast_category *category, struct ast_variable *variable)
{
if (!variable)
return;
if (category->last)
category->last->next = variable;
else
category->root = variable;
category->last = variable;
while (category->last->next)
category->last = category->last->next;
}
void ast_variable_insert(struct ast_category *category, struct ast_variable *variable, const char *line)
{
struct ast_variable *cur = category->root;
int lineno;
int insertline;
if (!variable || sscanf(line, "%30d", &insertline) != 1) {
return;
}
if (!insertline) {
variable->next = category->root;
category->root = variable;
} else {
for (lineno = 1; lineno < insertline; lineno++) {
cur = cur->next;
if (!cur->next) {
break;
}
}
variable->next = cur->next;
cur->next = variable;
}
}
static void ast_comment_destroy(struct ast_comment **comment)
{
struct ast_comment *n, *p;
for (p = *comment; p; p = n) {
n = p->next;
ast_free(p);
}
*comment = NULL;
}
static void ast_variable_destroy(struct ast_variable *doomed)
{
ast_comment_destroy(&doomed->precomments);
ast_comment_destroy(&doomed->sameline);
ast_comment_destroy(&doomed->trailing);
ast_free(doomed);
}
struct ast_variable *ast_variables_dup(struct ast_variable *var)
{
struct ast_variable *cloned;
struct ast_variable *tmp;
if (!(cloned = ast_variable_new(var->name, var->value, var->file))) {
return NULL;
}
tmp = cloned;
while ((var = var->next)) {
if (!(tmp->next = ast_variable_new(var->name, var->value, var->file))) {
ast_variables_destroy(cloned);
return NULL;
}
tmp = tmp->next;
}
return cloned;
}
struct ast_variable *ast_variables_reverse(struct ast_variable *var)
{
struct ast_variable *var1, *var2;
var1 = var;
if (!var1 || !var1->next) {
return var1;
}
var2 = var1->next;
var1->next = NULL;
while (var2) {
struct ast_variable *next = var2->next;
var2->next = var1;
var1 = var2;
var2 = next;
}
return var1;
}
void ast_variables_destroy(struct ast_variable *var)
{
struct ast_variable *vn;
while (var) {
vn = var;
var = var->next;
ast_variable_destroy(vn);
}
}
struct ast_variable *ast_variable_browse(const struct ast_config *config, const char *category)
{
struct ast_category *cat;
if (config->last_browse && (config->last_browse->name == category)) {
cat = config->last_browse;
} else {
cat = ast_category_get(config, category, NULL);
}
return (cat) ? cat->root : NULL;
}
static inline struct ast_variable *variable_list_switch(struct ast_variable *l1, struct ast_variable *l2)
{
l1->next = l2->next;
l2->next = l1;
return l2;
}
struct ast_variable *ast_variable_list_sort(struct ast_variable *start)
{
struct ast_variable *p, *q;
struct ast_variable top;
int changed = 1;
memset(&top, 0, sizeof(top));
top.next = start;
if (start != NULL && start->next != NULL) {
while (changed) {
changed = 0;
q = ⊤
p = top.next;
while (p->next != NULL) {
if (p->next != NULL && strcmp(p->name, p->next->name) > 0) {
q->next = variable_list_switch(p, p->next);
changed = 1;
}
q = p;
if (p->next != NULL)
p = p->next;
}
}
}
return top.next;
}
struct ast_variable *ast_variable_list_append_hint(struct ast_variable **head, struct ast_variable *search_hint, struct ast_variable *newvar)
{
struct ast_variable *curr;
ast_assert(head != NULL);
if (!*head) {
*head = newvar;
} else {
if (search_hint == NULL) {
search_hint = *head;
}
for (curr = search_hint; curr->next; curr = curr->next);
curr->next = newvar;
}
for (curr = newvar; curr->next; curr = curr->next);
return curr;
}
int ast_variable_list_replace(struct ast_variable **head, struct ast_variable *replacement)
{
struct ast_variable *v, **prev = head;
for (v = *head; v; prev = &v->next, v = v->next) {
if (!strcmp(v->name, replacement->name)) {
replacement->next = v->next;
*prev = replacement;
ast_free(v);
return 0;
}
}
return -1;
}
int ast_variable_list_replace_variable(struct ast_variable **head, struct ast_variable *old,
struct ast_variable *new)
{
struct ast_variable *v, **prev = head;
for (v = *head; v; prev = &v->next, v = v->next) {
if (v == old) {
new->next = v->next;
*prev = new;
ast_free(v);
return 0;
}
}
return -1;
}
struct ast_str *ast_variable_list_join(const struct ast_variable *head, const char *item_separator,
const char *name_value_separator, const char *quote_char, struct ast_str **str)
{
struct ast_variable *var = (struct ast_variable *)head;
struct ast_str *local_str = NULL;
if (str == NULL || *str == NULL) {
local_str = ast_str_create(AST_MAX_USER_FIELD);
if (!local_str) {
return NULL;
}
} else {
local_str = *str;
}
for (; var; var = var->next) {
ast_str_append(&local_str, 0, "%s%s%s%s%s%s", var->name, name_value_separator, S_OR(quote_char, ""),
var->value, S_OR(quote_char, ""), var->next ? item_separator : "");
}
if (str != NULL) {
*str = local_str;
}
return local_str;
}
struct ast_variable *ast_variable_list_from_string(const char *input, const char *item_separator,
const char *name_value_separator)
{
char item_sep;
char nv_sep;
struct ast_variable *new_list = NULL;
struct ast_variable *new_var = NULL;
char *item_string;
char *item;
char *item_name;
char *item_value;
if (ast_strlen_zero(input)) {
return NULL;
}
item_sep = ast_strlen_zero(item_separator) ? ',' : item_separator[0];
nv_sep = ast_strlen_zero(name_value_separator) ? '=' : name_value_separator[0];
item_string = ast_strip(ast_strdupa(input));
while ((item = ast_strsep(&item_string, item_sep, AST_STRSEP_ALL))) {
item_name = ast_strsep(&item, nv_sep, AST_STRSEP_ALL);
item_value = ast_strsep(&item, nv_sep, AST_STRSEP_ALL);
new_var = ast_variable_new(item_name, item_value, "");
if (!new_var) {
ast_variables_destroy(new_list);
return NULL;
}
ast_variable_list_append(&new_list, new_var);
}
return new_list;
}
const char *ast_config_option(struct ast_config *cfg, const char *cat, const char *var)
{
const char *tmp;
tmp = ast_variable_retrieve(cfg, cat, var);
if (!tmp) {
tmp = ast_variable_retrieve(cfg, "general", var);
}
return tmp;
}
const char *ast_variable_retrieve(struct ast_config *config, const char *category, const char *variable)
{
struct ast_variable *v;
if (category) {
for (v = ast_variable_browse(config, category); v; v = v->next) {
if (!strcasecmp(variable, v->name)) {
return v->value;
}
}
} else {
struct ast_category *cat;
for (cat = config->root; cat; cat = cat->next) {
for (v = cat->root; v; v = v->next) {
if (!strcasecmp(variable, v->name)) {
return v->value;
}
}
}
}
return NULL;
}
const char *ast_variable_retrieve_filtered(struct ast_config *config,
const char *category, const char *variable, const char *filter)
{
struct ast_category *cat = NULL;
const char *value;
while ((cat = ast_category_browse_filtered(config, category, cat, filter))) {
value = ast_variable_find(cat, variable);
if (value) {
return value;
}
}
return NULL;
}
const char *ast_variable_find(const struct ast_category *category, const char *variable)
{
return ast_variable_find_in_list(category->root, variable);
}
const struct ast_variable *ast_variable_find_variable_in_list(const struct ast_variable *list, const char *variable_name)
{
const struct ast_variable *v;
for (v = list; v; v = v->next) {
if (!strcasecmp(variable_name, v->name)) {
return v;
}
}
return NULL;
}
int ast_variables_match(const struct ast_variable *left, const struct ast_variable *right)
{
char *op;
if (left == right) {
return 1;
}
if (!(left && right)) {
return 0;
}
op = strrchr(right->name, ' ');
if (op) {
op++;
}
return ast_strings_match(left->value, op ? ast_strdupa(op) : NULL, right->value);
}
int ast_variable_lists_match(const struct ast_variable *left, const struct ast_variable *right, int exact_match)
{
const struct ast_variable *field;
int right_count = 0;
int left_count = 0;
if (left == right) {
return 1;
}
if (!(left && right)) {
return 0;
}
for (field = right; field; field = field->next) {
char *space = strrchr(field->name, ' ');
const struct ast_variable *old;
char * name = (char *)field->name;
if (space) {
name = ast_strdup(field->name);
if (!name) {
return 0;
}
name[space - field->name] = '\0';
}
old = ast_variable_find_variable_in_list(left, name);
if (name != field->name) {
ast_free(name);
}
if (exact_match) {
if (!old || strcmp(old->value, field->value)) {
return 0;
}
} else {
if (!ast_variables_match(old, field)) {
return 0;
}
}
right_count++;
}
if (exact_match) {
for (field = left; field; field = field->next) {
left_count++;
}
if (right_count != left_count) {
return 0;
}
}
return 1;
}
const char *ast_variable_find_in_list(const struct ast_variable *list, const char *variable)
{
const struct ast_variable *v;
for (v = list; v; v = v->next) {
if (!strcasecmp(variable, v->name)) {
return v->value;
}
}
return NULL;
}
const char *ast_variable_find_last_in_list(const struct ast_variable *list, const char *variable)
{
const struct ast_variable *v;
const char *found = NULL;
for (v = list; v; v = v->next) {
if (!strcasecmp(variable, v->name)) {
found = v->value;
}
}
return found;
}
static struct ast_variable *variable_clone(const struct ast_variable *old)
{
struct ast_variable *new = ast_variable_new(old->name, old->value, old->file);
if (new) {
new->lineno = old->lineno;
new->object = old->object;
new->blanklines = old->blanklines;
/* TODO: clone comments? */
}
return new;
}
static void move_variables(struct ast_category *old, struct ast_category *new)
{
struct ast_variable *var = old->root;
old->root = NULL;
/* we can just move the entire list in a single op */
ast_variable_append(new, var);
}
/*! \brief Returns true if ALL of the regex expressions and category name match.
* Both can be NULL (I.E. no predicate) which results in a true return;
*/
static int does_category_match(struct ast_category *cat, const char *category_name,
const char *match, char sep)
{
char *dupmatch;
char *nvp = NULL;
int match_found = 0, match_expressions = 0;
int template_ok = 0;
/* Only match on category name if it's not a NULL or empty string */
if (!ast_strlen_zero(category_name) && strcasecmp(cat->name, category_name)) {
return 0;
}
/* If match is NULL or empty, automatically match if not a template */
if (ast_strlen_zero(match)) {
return !cat->ignored;
}
dupmatch = ast_strdupa(match);
while ((nvp = ast_strsep(&dupmatch, sep, AST_STRSEP_STRIP))) {
struct ast_variable *v;
char *match_name;
char *match_value = NULL;
char *regerr;
int rc;
regex_t r_name, r_value;
match_expressions++;
match_name = ast_strsep(&nvp, '=', AST_STRSEP_STRIP);
match_value = ast_strsep(&nvp, '=', AST_STRSEP_STRIP);
/* an empty match value is OK. A NULL match value (no =) is NOT. */
if (match_value == NULL) {
break;
}
if (!strcmp("TEMPLATES", match_name)) {
if (!strcasecmp("include", match_value)) {
if (cat->ignored) {
template_ok = 1;
}
match_found++;
} else if (!strcasecmp("restrict", match_value)) {
if (cat->ignored) {