Add support for globbing (bug #2825)
[asterisk/asterisk.git] / config.c
1 /*
2  * Asterisk -- A telephony toolkit for Linux.
3  *
4  * Configuration File Parser
5  * 
6  * Copyright (C) 1999-2004, Digium, Inc.
7  *
8  * Mark Spencer <markster@digium.com>
9  *
10  * This program is free software, distributed under the terms of
11  * the GNU General Public License
12  */
13
14 #include <stdio.h>
15 #include <unistd.h>
16 #include <stdlib.h>
17 #include <string.h>
18 #include <errno.h>
19 #include <time.h>
20 #define AST_INCLUDE_GLOB 1
21 #ifdef AST_INCLUDE_GLOB
22 # include <glob.h>
23 #endif
24 #include <asterisk/config.h>
25 #include <asterisk/config_pvt.h>
26 #include <asterisk/cli.h>
27 #include <asterisk/lock.h>
28 #include <asterisk/options.h>
29 #include <asterisk/logger.h>
30 #include <asterisk/utils.h>
31 #include "asterisk.h"
32 #include "astconf.h"
33
34 #define MAX_NESTED_COMMENTS 128
35 #define COMMENT_START ";--"
36 #define COMMENT_END "--;"
37 #define COMMENT_META ';'
38 #define COMMENT_TAG '-'
39
40 static int ast_cust_config=0;
41 struct ast_config *(*global_load_func)(const char *dbname, const char *table, const char *, struct ast_config *,struct ast_category **,struct ast_variable **,int);
42
43 static struct ast_config_map {
44         struct ast_config_map *next;
45         char *name;
46         char *driver;
47         char *database;
48         char *table;
49         char stuff[0];
50 } *maps = NULL;
51
52 AST_MUTEX_DEFINE_STATIC(ast_cust_config_lock);
53 static struct ast_config_reg *ast_cust_config_list;
54 static char *config_conf_file = "extconfig.conf";
55
56 void ast_destroy_realtime(struct ast_variable *v)
57 {
58         struct ast_variable *vn;
59         while(v) {
60                 vn = v;
61                 v = v->next;
62                 free(vn);
63         }
64 }
65
66 void ast_category_destroy(struct ast_category *cat)
67 {
68         ast_destroy_realtime(cat->root);
69         free(cat);
70 }
71
72 void ast_destroy(struct ast_config *ast)
73 {
74         struct ast_category *cat, *catn;
75
76         if (!ast)
77                 return;
78
79         cat = ast->root;
80         while(cat) {
81                 ast_destroy_realtime(cat->root);
82                 catn = cat;
83                 cat = cat->next;
84                 free(catn);
85         }
86         free(ast);
87 }
88
89 int ast_true(const char *s)
90 {
91         if (!s)
92                 return 0;
93         /* Determine if this is a true value */
94         if (!strcasecmp(s, "yes") ||
95             !strcasecmp(s, "true") ||
96                 !strcasecmp(s, "y") ||
97                 !strcasecmp(s, "t") ||
98                 !strcasecmp(s, "1") ||
99                 !strcasecmp(s, "on"))
100                         return -1;
101         return 0;
102 }
103
104 int ast_false(const char *s)
105 {
106         if (!s)
107                 return 0;
108         /* Determine if this is a false value */
109         if (!strcasecmp(s, "no") ||
110             !strcasecmp(s, "false") ||
111                 !strcasecmp(s, "n") ||
112                 !strcasecmp(s, "f") ||
113                 !strcasecmp(s, "0") ||
114                 !strcasecmp(s, "off"))
115                         return -1;
116         return 0;
117 }
118
119 struct ast_variable *ast_variable_browse(struct ast_config *config, char *category)
120 {
121         struct ast_category *cat;
122         cat = config->root;
123         while(cat) {
124                 if (cat->name == category)
125                         return cat->root;
126                 cat = cat->next;
127         }
128         cat = config->root;
129         while(cat) {
130                 if (!strcasecmp(cat->name, category))
131                         return cat->root;
132                 cat = cat->next;
133         }
134         return NULL;
135 }
136
137 char *ast_variable_retrieve(struct ast_config *config, char *category, char *value)
138 {
139         struct ast_variable *v;
140         if (category) {
141                 v = ast_variable_browse(config, category);
142                 while (v) {
143                         if (value == v->name)
144                                 return v->value;
145                         v=v->next;
146                 }
147                 v = ast_variable_browse(config, category);
148                 while (v) {
149                         if (!strcasecmp(value, v->name))
150                                 return v->value;
151                         v=v->next;
152                 }
153         } else {
154                 struct ast_category *cat;
155                 cat = config->root;
156                 while(cat) {
157                         v = cat->root;
158                         while (v) {
159                                 if (!strcasecmp(value, v->name))
160                                         return v->value;
161                                 v=v->next;
162                         }
163                         cat = cat->next;
164                 }
165         }
166         return NULL;
167 }
168
169 int ast_category_exist(struct ast_config *config, char *category_name)
170 {
171         struct ast_category *category = NULL;
172
173         category = config->root;
174
175         while(category) {
176                 if (!strcasecmp(category->name,category_name)) 
177                         return 1;
178                 category = category->next;
179         } 
180
181         return 0;
182 }
183
184
185 static struct ast_config_reg *get_ast_cust_config_keyword(const char *name, char *database, int dbsiz, char *table, int tabsiz) 
186 {
187         struct ast_config_reg *reg,*ret=NULL;
188         struct ast_config_map *map;
189
190         ast_mutex_lock(&ast_cust_config_lock);
191         map = maps;
192         while(map) {
193                 if (!strcasecmp(name, map->name)) {
194                         strncpy(database, map->database, dbsiz - 1);
195                         if (map->table)
196                                 strncpy(table, map->table, tabsiz - 1);
197                         else
198                                 strncpy(table, name, tabsiz - 1);
199                         break;
200                 }
201                 map = map->next;
202         }
203         if (map) {
204                 for (reg=ast_cust_config_list;reg && !ret;reg=reg->next) {
205                         if (!strcmp(reg->name,map->driver))
206                                 ret=reg;
207                 }
208         }
209         ast_mutex_unlock(&ast_cust_config_lock);
210         return ret;
211 }
212
213 void ast_config_destroy_all(void) 
214 {
215         struct ast_config_reg *key;
216         ast_mutex_lock(&ast_cust_config_lock);
217         for (key=ast_cust_config_list;key;key=key->next) {
218                 ast_config_deregister(key);
219         }
220         ast_cust_config_list = NULL;
221         ast_mutex_unlock(&ast_cust_config_lock);
222 }
223
224 static struct ast_config_reg *get_config_registrations(void) 
225 {
226         return ast_cust_config_list;
227 }
228
229
230 static struct ast_config *__ast_load(const char *configfile, struct ast_config *tmp, struct ast_category **_tmpc, struct ast_variable **_last, int includelevel);
231
232 static int cfg_process(struct ast_config *tmp, struct ast_category **_tmpc, struct ast_variable **_last, char *buf, int lineno, const char *configfile, int includelevel )
233 {
234         char *c;
235         char *cur;
236         char *arg=NULL;
237         struct ast_variable *v;
238         int object;
239         cur = ast_strip(buf);
240         if (!ast_strlen_zero(cur)) {
241                 /* Actually parse the entry */
242                 if (cur[0] == '[') {
243                         /* A category header */
244                         c = strchr(cur, ']');
245                         if (c) {
246                                 *c = 0;
247                                 *_tmpc = malloc(sizeof(struct ast_category));
248                                 if (!*_tmpc) {
249                                         ast_destroy(tmp);
250                                         ast_log(LOG_WARNING,
251                                                 "Out of memory, line %d\n", lineno);
252                                         return -1;
253                                 }
254                                 memset(*_tmpc, 0, sizeof(struct ast_category));
255                                 strncpy((*_tmpc)->name, cur+1, sizeof((*_tmpc)->name) - 1);
256                                 (*_tmpc)->root =  NULL;
257                                 if (!tmp->prev)
258                                         tmp->root = *_tmpc;
259                                 else
260                                         tmp->prev->next = *_tmpc;
261
262                                 tmp->prev = *_tmpc;
263                                 *_last =  NULL;
264                         } else {
265                                 ast_log(LOG_WARNING, 
266                                         "parse error: no closing ']', line %d of %s\n", lineno, configfile);
267                         }
268                 } else if (cur[0] == '#') {
269                         /* A directive */
270                         cur++;
271                         c = cur;
272                         while(*c && (*c > 32)) c++;
273                         if (*c) {
274                                 *c = '\0';
275                                 c++;
276                                 /* Find real argument */
277                                 while(*c  && (*c < 33)) c++;
278                                 if (!*c)
279                                         c = NULL;
280                         } else 
281                                 c = NULL;
282                         if (!strcasecmp(cur, "include")) {
283                                 /* A #include */
284                                 if (c) {
285                                         while((*c == '<') || (*c == '>') || (*c == '\"')) c++;
286                                         /* Get rid of leading mess */
287                                         cur = c;
288                                         while (!ast_strlen_zero(cur)) {
289                                                 c = cur + strlen(cur) - 1;
290                                                 if ((*c == '>') || (*c == '<') || (*c == '\"'))
291                                                         *c = '\0';
292                                                 else
293                                                         break;
294                                         }
295                                         
296                                         if((c = strchr(cur,':'))) {
297                                                 *c = '\0';
298                                                 c++;
299                                                 arg = c;
300                                         }
301                                         
302                                         if (includelevel < MAX_INCLUDE_LEVEL) {
303                                                 if(arg && cur) {
304                                                         ast_log(LOG_WARNING, "Including files with explicit config engine no longer permitted.  Please use extconfig.conf to specify all mappings\n");
305                                                 } else {
306                                                         if (!__ast_load(cur, tmp, _tmpc, _last, includelevel + 1))
307                                                                 return -1;
308                                                 }
309                                         } else
310                                                 ast_log(LOG_WARNING, "Maximum Include level (%d) exceeded\n", includelevel);
311                                 } else
312                                         ast_log(LOG_WARNING, "Directive '#include' needs an argument (filename) at line %d of %s\n", lineno, configfile);
313                                 /* Strip off leading and trailing "'s and <>'s */
314                         }
315                         else 
316                                 ast_log(LOG_WARNING, "Unknown directive '%s' at line %d of %s\n", cur, lineno, configfile);
317                 } else {
318                         /* Just a line (variable = value) */
319                         if (!*_tmpc) {
320                                 ast_log(LOG_WARNING,
321                                         "parse error: No category context for line %d of %s\n", lineno, configfile);
322                                 ast_destroy(tmp);
323                                 return -1;
324                         }
325                         c = strchr(cur, '=');
326                         if (c) {
327                                 *c = 0;
328                                 c++;
329                                 /* Ignore > in => */
330                                 if (*c== '>') {
331                                         object = 1;
332                                         c++;
333                                 } else
334                                         object = 0;
335                                 v = ast_new_variable(ast_strip(cur), ast_strip(c));
336                                 if (v) {
337                                         v->next = NULL;
338                                         v->lineno = lineno;
339                                         v->object = object;
340                                         /* Put and reset comments */
341                                         v->blanklines = 0;
342                                         if (*_last)
343                                                 (*_last)->next = v;
344                                         else
345                                                 (*_tmpc)->root = v;
346                                         *_last = v;
347                                 } else {
348                                         ast_destroy(tmp);
349                                         ast_log(LOG_WARNING, "Out of memory, line %d\n", lineno);
350                                         return -1;
351                                 }
352                         } else {
353                                 ast_log(LOG_WARNING, "No '=' (equal sign) in line %d of %s\n", lineno, configfile);
354                         }
355                                                                                                                 
356                 }
357         }
358         return 0;
359 }
360
361 int ast_save(char *configfile, struct ast_config *cfg, char *generator)
362 {
363         FILE *f;
364         char fn[256];
365         char date[256]="";
366         time_t t;
367         struct ast_variable *var;
368         struct ast_category *cat;
369         int blanklines = 0;
370         if (configfile[0] == '/') {
371                 strncpy(fn, configfile, sizeof(fn)-1);
372         } else {
373                 snprintf(fn, sizeof(fn), "%s/%s", AST_CONFIG_DIR, configfile);
374         }
375         time(&t);
376         strncpy(date, ctime(&t), sizeof(date) - 1);
377         if ((f = fopen(fn, "w"))) {
378                 if ((option_verbose > 1) && !option_debug)
379                         ast_verbose(  VERBOSE_PREFIX_2 "Saving '%s': ", fn);
380                 fprintf(f, ";!\n");
381                 fprintf(f, ";! Automatically generated configuration file\n");
382                 fprintf(f, ";! Filename: %s (%s)\n", configfile, fn);
383                 fprintf(f, ";! Generator: %s\n", generator);
384                 fprintf(f, ";! Creation Date: %s", date);
385                 fprintf(f, ";!\n");
386                 cat = cfg->root;
387                 while(cat) {
388                         /* Dump section with any appropriate comment */
389                         fprintf(f, "[%s]\n", cat->name);
390                         var = cat->root;
391                         while(var) {
392                                 if (var->sameline) 
393                                         fprintf(f, "%s %s %s  ; %s\n", var->name, (var->object ? "=>" : "="), var->value, var->sameline->cmt);
394                                 else    
395                                         fprintf(f, "%s %s %s\n", var->name, (var->object ? "=>" : "="), var->value);
396                                 if (var->blanklines) {
397                                         blanklines = var->blanklines;
398                                         while (blanklines) {
399                                                 fprintf(f, "\n");
400                                                 blanklines--;
401                                         }
402                                 }
403                                         
404                                 var = var->next;
405                         }
406 #if 0
407                         /* Put an empty line */
408                         fprintf(f, "\n");
409 #endif
410                         cat = cat->next;
411                 }
412         } else {
413                 if (option_debug)
414                         printf("Unable to open for writing: %s\n", fn);
415                 else if (option_verbose > 1)
416                         printf( "Unable to write (%s)", strerror(errno));
417                 return -1;
418         }
419         fclose(f);
420         return 0;
421 }
422
423
424 struct ast_variable *ast_load_realtime(const char *family, ...)
425 {
426         struct ast_config_reg *reg;
427         char db[256]="";
428         char table[256]="";
429         struct ast_variable *res=NULL;
430         va_list ap;
431         va_start(ap, family);
432         reg = get_ast_cust_config_keyword(family, db, sizeof(db), table, sizeof(table));
433         if (reg && reg->realtime_func) 
434                 res = reg->realtime_func(db, table, ap);
435         va_end(ap);
436         return res;
437 }
438
439 struct ast_config *ast_load_realtime_multientry(const char *family, ...)
440 {
441         struct ast_config_reg *reg;
442         char db[256]="";
443         char table[256]="";
444         struct ast_config *res=NULL;
445         va_list ap;
446         va_start(ap, family);
447         reg = get_ast_cust_config_keyword(family, db, sizeof(db), table, sizeof(table));
448         if (reg && reg->realtime_multi_func) 
449                 res = reg->realtime_multi_func(db, table, ap);
450         va_end(ap);
451         return res;
452 }
453
454 int ast_update_realtime(const char *family, const char *keyfield, const char *lookup, ...)
455 {
456         struct ast_config_reg *reg;
457         int res = -1;
458         char db[256]="";
459         char table[256]="";
460         va_list ap;
461         va_start(ap, lookup);
462         reg = get_ast_cust_config_keyword(family, db, sizeof(db), table, sizeof(table));
463         if (reg && reg->update_func) 
464                 res = reg->update_func(db, table, keyfield, lookup, ap);
465         va_end(ap);
466         return res;
467 }
468
469 static struct ast_config *__ast_load(const char *configfile, struct ast_config *tmp, struct ast_category **_tmpc, struct ast_variable **_last, int includelevel)
470 {
471         char fn[256];
472         char buf[8192];
473         char db[256];
474         char table[256];
475         FILE *f;
476         int lineno=0;
477         int master=0;
478         int comment = 0, nest[MAX_NESTED_COMMENTS];
479         
480         struct ast_config_reg *reg=NULL;
481         struct ast_config *(*load_func)(const char *database, const char *table, const char *, struct ast_config *,struct ast_category **,struct ast_variable **,int);
482         char *comment_p, *process_buf, *new_buf=NULL;
483
484         load_func=NULL;
485         if (strcmp(configfile,config_conf_file) && strcmp(configfile,"asterisk.conf") && ast_cust_config_list) {
486                 if (global_load_func) {
487                         load_func = global_load_func;
488                 } else {
489                         reg = get_ast_cust_config_keyword(configfile, db, sizeof(db), table, sizeof(table));
490                         if (reg && reg->static_func) {
491                                 load_func = reg->static_func;
492                         } else {
493                                 reg = get_ast_cust_config_keyword(configfile, db, sizeof(db), table, sizeof(table));
494                                 if (reg && reg->static_func)
495                                         global_load_func = load_func = reg->static_func;
496                         }
497                 }
498
499                 if (load_func) {
500                         ast_log(LOG_NOTICE,"Loading Config %s via %s engine\n",configfile,reg && reg->name ? reg->name : "global");
501                         if((tmp = load_func(db, table, configfile,tmp, _tmpc, _last, includelevel)))
502                                 return tmp;
503                 }
504         }
505
506         if (configfile[0] == '/') {
507                 strncpy(fn, configfile, sizeof(fn)-1);
508         } else {
509                 snprintf(fn, sizeof(fn), "%s/%s", (char *)ast_config_AST_CONFIG_DIR, configfile);
510         }
511 #ifdef AST_INCLUDE_GLOB
512         {
513                 int glob_ret;
514                 glob_t globbuf;
515                 globbuf.gl_offs = 0;    /* initialize it to silence gcc */
516                 glob_ret = glob(fn, GLOB_NOMATCH|GLOB_BRACE, NULL, &globbuf);
517                 if (glob_ret == GLOB_NOSPACE)
518                         ast_log(LOG_WARNING,
519                                 "Glob Expansion of pattern '%s' failed: Not enough memory\n", fn);
520                 else if (glob_ret  == GLOB_ABORTED)
521                         ast_log(LOG_WARNING,
522                                 "Glob Expansion of pattern '%s' failed: Read error\n", fn);
523                 else  {
524                         /* loop over expanded files */
525                         int i;
526                         for (i=0; i<globbuf.gl_pathc; i++) {
527                                 strncpy(fn, globbuf.gl_pathv[i], sizeof(fn)-1);
528 #endif
529         if ((option_verbose > 1) && !option_debug) {
530                 ast_verbose(  VERBOSE_PREFIX_2 "Parsing '%s': ", fn);
531                 fflush(stdout);
532         }
533         if ((f = fopen(fn, "r"))) {
534                 if (option_debug)
535                         ast_log(LOG_DEBUG, "Parsing %s\n", fn);
536                 else if (option_verbose > 2)
537                         ast_verbose( "Found\n");
538                 if (!tmp) {
539                         if((tmp = malloc(sizeof(struct ast_config)))) {
540                                 memset(tmp, 0, sizeof(struct ast_config));
541                                 master = 1;
542                         }
543                 }
544                 if (tmp) {
545                         while(!feof(f)) {
546                                 lineno++;
547                                 if (fgets(buf, sizeof(buf), f)) {
548                                         new_buf = buf;
549                                         if (comment)
550                                                 process_buf = NULL;
551                                         else
552                                                 process_buf = buf;
553                                         while ((comment_p = strchr(new_buf, COMMENT_META))) {
554                                                 if ((comment_p > new_buf) && (*(comment_p-1) == '\\')) {
555                                                         /* Yuck, gotta memmove */
556                                                         memmove(comment_p - 1, comment_p, strlen(comment_p) + 1);
557                                                         new_buf = comment_p;
558                                                 } else if(comment_p[1] == COMMENT_TAG && comment_p[2] == COMMENT_TAG && (comment_p[3] != '-')) {
559                                                         /* Meta-Comment start detected ";--" */
560                                                         if (comment < MAX_NESTED_COMMENTS) {
561                                                                 *comment_p = '\0';
562                                                                 new_buf = comment_p + 3;
563                                                                 comment++;
564                                                                 nest[comment-1] = lineno;
565                                                         } else {
566                                                                 ast_log(LOG_ERROR, "Maximum nest limit of %d reached.\n", MAX_NESTED_COMMENTS);
567                                                         }
568                                                 } else if ((comment_p >= new_buf + 2) &&
569                                                                    (*(comment_p - 1) == COMMENT_TAG) &&
570                                                                    (*(comment_p - 2) == COMMENT_TAG)) {
571                                                         /* Meta-Comment end detected */
572                                                         comment--;
573                                                         new_buf = comment_p + 1;
574                                                         if (!comment) {
575                                                                 /* Back to non-comment now */
576                                                                 if (process_buf) {
577                                                                         /* Actually have to move what's left over the top, then continue */
578                                                                         char *oldptr;
579                                                                         oldptr = process_buf + strlen(process_buf);
580                                                                         memmove(oldptr, new_buf, strlen(new_buf) + 1);
581                                                                         new_buf = oldptr;
582                                                                 } else
583                                                                         process_buf = new_buf;
584                                                         }
585                                                 } else {
586                                                         if (!comment) {
587                                                                 /* If ; is found, and we are not nested in a comment, 
588                                                                    we immediately stop all comment processing */
589                                                                 *comment_p = '\0'; 
590                                                                 new_buf = comment_p;
591                                                         } else
592                                                                 new_buf = comment_p + 1;
593                                                 }
594                                         }
595                                         if (process_buf && cfg_process(tmp, _tmpc, _last, process_buf, lineno, fn, includelevel)) {
596                                                 tmp = NULL;
597                                                 break;
598                                         }
599                                 }
600                         }
601                 } else 
602                         ast_log(LOG_WARNING, "Out of memory\n");
603                 
604                 fclose(f);              
605         } else { /* can't open file */
606                 if (option_debug)
607                         ast_log(LOG_DEBUG, "No file to parse: %s\n", fn);
608                 else if (option_verbose > 2)
609                         ast_verbose( "Not found (%s)\n", strerror(errno));
610         }
611         if (comment) {
612                 ast_log(LOG_WARNING,"Unterminated comment detected beginning on line %d\n", nest[comment]);
613         }
614 #ifdef AST_INCLUDE_GLOB
615                                         if (!tmp)
616                                                 break;
617                                 }
618                                 globfree(&globbuf);
619                         }
620                 }
621 #endif
622
623         return tmp;
624 }
625
626 int ast_config_register(struct ast_config_reg *new) 
627 {
628         struct ast_config_reg *ptr;
629         ast_mutex_lock(&ast_cust_config_lock);
630         if (!ast_cust_config_list) {
631                 ast_cust_config_list = new;
632         } else {
633                 for(ptr=ast_cust_config_list;ptr->next;ptr=ptr->next);
634                         ptr->next = new;
635         }
636         ast_mutex_unlock(&ast_cust_config_lock);
637         ast_log(LOG_NOTICE,"Registered Config Engine %s\n",new->name);
638         return 1;
639 }
640
641 int ast_config_deregister(struct ast_config_reg *del) 
642 {
643         struct ast_config_reg *ptr=NULL,*last=NULL;
644         ast_mutex_lock(&ast_cust_config_lock);
645         for (ptr=ast_cust_config_list;ptr;ptr=ptr->next) {
646                 if (ptr == del) {
647                         if (last && ptr->next) {
648                                 last->next = ptr->next;
649                         } else if (last && ! ptr->next) {
650                                 last->next = NULL;
651                         } else if (!last && ptr->next) {
652                                 ast_cust_config_list = ptr->next;
653                         } else if (!last && !ptr->next) {
654                                 ast_cust_config_list = NULL;
655                         }
656                 }
657                 last = ptr;
658         }
659         ast_mutex_unlock(&ast_cust_config_lock);
660         return 0;
661 }
662
663 int ast_cust_config_active(void) {
664         return (ast_cust_config >0) ? 1 : 0;
665 }
666
667 struct ast_config *ast_load(char *configfile)
668 {
669         struct ast_category *tmpc=NULL;
670         struct ast_variable *last = NULL;
671
672         return __ast_load(configfile, NULL, &tmpc, &last, 0);
673 }
674
675 void ast_category_append(struct ast_config *config, struct ast_category *cat)
676 {
677         struct ast_category *prev = NULL;
678         cat->next = NULL;
679         if (config->root) {
680                 prev = config->root;
681                 while(prev->next) prev = prev->next;
682                 prev->next = cat;
683         } else
684                 config->root = cat;
685 }
686
687 char *ast_category_browse(struct ast_config *config, char *prev)
688 {       
689         struct ast_category *cat;
690         if (!prev) {
691                 if (config->root)
692                         return config->root->name;
693                 else
694                         return NULL;
695         }
696         cat = config->root;
697         while(cat) {
698                 if (cat->name == prev) {
699                         if (cat->next)
700                                 return cat->next->name;
701                         else
702                                 return NULL;
703                 }
704                 cat = cat->next;
705         }
706         cat = config->root;
707         while(cat) {
708                 if (!strcasecmp(cat->name, prev)) {
709                         if (cat->next)
710                                 return cat->next->name;
711                         else
712                                 return NULL;
713                 }
714                 cat = cat->next;
715         }
716         return NULL;
717 }
718
719
720 struct ast_config *ast_new_config(void) 
721 {
722         struct ast_config *config;
723         config = malloc(sizeof(struct ast_config));
724         memset(config,0,sizeof(struct ast_config));
725         return config;
726 }
727
728
729
730 struct ast_category *ast_new_category(char *name) 
731 {
732         struct ast_category *category;
733         category = malloc(sizeof(struct ast_category));
734         if (category) {
735                 memset(category,0,sizeof(struct ast_category));
736                 strncpy(category->name,name,sizeof(category->name) - 1);
737         }
738         return category;
739 }
740
741
742 struct ast_variable *ast_new_variable(char *name, char *value) 
743 {
744         struct ast_variable *variable;
745         int length = strlen(name) + strlen(value) + 2 + sizeof(struct ast_variable);
746         variable = malloc(length);
747         if (variable) {
748                 memset(variable, 0, length);
749                 variable->name = variable->stuff;
750                 variable->value = variable->stuff + strlen(name) + 1;           
751                 variable->object=0;
752                 strcpy(variable->name,name);
753                 strcpy(variable->value,value);
754         }
755         return variable;
756 }
757
758 int ast_cust_config_register(struct ast_config_reg *new) 
759 {
760         ast_config_register(new);
761         read_ast_cust_config();
762         return 1;
763 }
764 int ast_cust_config_deregister(struct ast_config_reg *new) 
765 {
766         ast_config_deregister(new);
767         read_ast_cust_config();
768         return 1;
769 }
770
771 static void clear_cust_keywords(void) 
772 {
773         struct ast_config_map *map, *prev;
774         ast_mutex_lock(&ast_cust_config_lock);
775         map = maps;
776         while(map) {
777                 prev = map;
778                 map = map->next;
779                 free(prev);
780         }
781         maps = NULL;
782         ast_mutex_unlock(&ast_cust_config_lock);
783 }
784
785 static int config_command(int fd, int argc, char **argv) 
786 {
787         struct ast_config_reg *key;
788         struct ast_config_map *map;
789         
790         ast_cli(fd,"\n\n");
791         ast_mutex_lock(&ast_cust_config_lock);
792         for (key=get_config_registrations();key;key=key->next) {
793                 ast_cli(fd,"\nConfig Engine: %s\n",key->name);
794                 map = maps;
795                 while(map) {
796                         if (!strcasecmp(map->driver, key->name))
797                                 ast_cli(fd,"===> %s (db=%s, table=%s)\n",map->name, map->database, map->table ? map->table : map->name);
798                         map = map->next;
799                 }
800         }
801         ast_mutex_unlock(&ast_cust_config_lock);
802         ast_cli(fd,"\n\n");
803         
804         return 0;
805 }
806
807 static struct ast_cli_entry config_command_struct = {
808   { "show","config","handles", NULL }, config_command,
809   "Show Config Handles", NULL };
810
811 int register_config_cli() 
812 {
813         return ast_cli_register(&config_command_struct);
814 }
815
816 int read_ast_cust_config(void) 
817 {
818         char *cfg = config_conf_file;
819         struct ast_config *config;
820         struct ast_variable *v;
821         struct ast_config_map *map;
822         int length;
823         char *driver, *table, *database, *stringp;
824
825         clear_cust_keywords();
826         config = ast_load(cfg);
827         if (config) {
828                 for (v = ast_variable_browse(config,"settings");v;v=v->next) {
829                         stringp = v->value;
830                         driver = strsep(&stringp, ",");
831                         database = strsep(&stringp, ",");
832                         table = strsep(&stringp, ",");
833                         
834                         if (!strcmp(v->name,config_conf_file) || !strcmp(v->name,"asterisk.conf")) {
835                                 ast_log(LOG_WARNING, "Cannot bind asterisk.conf or extconfig.conf!\n");
836                         } else if (driver && database) {
837                                 length = sizeof(struct ast_config_map);
838                                 length += strlen(v->name) + 1;
839                                 length += strlen(driver) + 1;
840                                 length += strlen(database) + 1;
841                                 if (table)
842                                         length += strlen(table) + 1;
843                                 map = malloc(length);
844                                 if (map) {
845                                         memset(map, 0, length);
846                                         map->name = map->stuff;
847                                         strcpy(map->name, v->name);
848                                         map->driver = map->name + strlen(map->name) + 1;
849                                         strcpy(map->driver, driver);
850                                         map->database = map->driver + strlen(map->driver) + 1;
851                                         strcpy(map->database, database);
852                                         if (table) {
853                                                 map->table = map->database + strlen(map->database) + 1;
854                                                 strcpy(map->table, table);
855                                         } else
856                                                 map->table = NULL;
857                                         map->next = maps;
858                                         if (option_verbose > 1)
859                                                 ast_verbose(VERBOSE_PREFIX_2 "Binding %s to %s/%s/%s\n",map->name,map->driver, map->database, map->table ? map->table : map->name);
860                                         maps = map;
861                                 }
862                         }
863                 }
864                 
865                 ast_destroy(config);
866         }
867
868         return 0;
869 }