fix a miscalled arg in the #include support in external config handles
[asterisk/asterisk.git] / config.c
1 /*
2  * Asterisk -- A telephony toolkit for Linux.
3  *
4  * Configuration File Parser
5  * 
6  * Copyright (C) 1999, Mark Spencer
7  *
8  * Mark Spencer <markster@linux-support.net>
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 #include <asterisk/config.h>
21 #include <asterisk/config_pvt.h>
22 #include <asterisk/cli.h>
23 #include <asterisk/lock.h>
24 #include <asterisk/options.h>
25 #include <asterisk/logger.h>
26 #include <asterisk/utils.h>
27 #include "asterisk.h"
28 #include "astconf.h"
29
30
31 static int ast_cust_config=0;
32 struct ast_config *(*global_load_func)(char *, struct ast_config *,struct ast_category **,struct ast_variable **,int
33 #ifdef PRESERVE_COMMENTS                                                                                                                                     
34 ,struct ast_comment_struct *
35 #endif
36 );
37
38 AST_MUTEX_DEFINE_STATIC(ast_cust_config_lock);
39 static struct ast_config_reg *ast_cust_config_list;
40 static char *config_conf_file = "extconfig.conf";
41
42 static char *strip(char *buf)
43 {
44         char *start;
45         /* Strip off trailing whitespace, returns, etc */
46         while(!ast_strlen_zero(buf) && (buf[strlen(buf)-1]<33))
47                 buf[strlen(buf)-1] = '\0';
48         start = buf;
49         /* Strip off leading whitespace, returns, etc */
50         while(*start && (*start < 33))
51                 *start++ = '\0';
52         return start;
53 }
54
55 #ifdef PRESERVE_COMMENTS
56 static void free_comments(struct ast_comment *com)
57 {
58         struct ast_comment *l;
59         while (com) {
60                 l = com;
61                 com = com->next;
62                 free(l);
63         }
64 }
65 #endif
66
67 void ast_destroy(struct ast_config *ast)
68 {
69         struct ast_category *cat, *catn;
70         struct ast_variable *v, *vn;
71
72         if (!ast)
73                 return;
74
75         cat = ast->root;
76         while(cat) {
77                 v = cat->root;
78                 while(v) {
79                         vn = v;
80                         free(v->name);
81                         free(v->value);
82 #ifdef PRESERVE_COMMENTS
83                         free_comments(v->precomments);
84                         free_comments(v->sameline);
85 #endif                  
86                         v = v->next;
87                         free(vn);
88                 }
89                 catn = cat;
90 #ifdef PRESERVE_COMMENTS
91                 free_comments(cat->precomments);
92                 free_comments(cat->sameline);
93 #endif          
94                 cat = cat->next;
95                 free(catn);
96         }
97 #ifdef PRESERVE_COMMENTS
98         free_comments(ast->trailingcomments);
99 #endif  
100         free(ast);
101 }
102
103 int ast_true(char *s)
104 {
105         if (!s)
106                 return 0;
107         /* Determine if this is a true value */
108         if (!strcasecmp(s, "yes") ||
109             !strcasecmp(s, "true") ||
110                 !strcasecmp(s, "y") ||
111                 !strcasecmp(s, "t") ||
112                 !strcasecmp(s, "1"))
113                         return -1;
114         return 0;
115 }
116
117 int ast_false(char *s)
118 {
119         if (!s)
120                 return 0;
121         /* Determine if this is a false value */
122         if (!strcasecmp(s, "no") ||
123             !strcasecmp(s, "false") ||
124                 !strcasecmp(s, "n") ||
125                 !strcasecmp(s, "f") ||
126                 !strcasecmp(s, "0"))
127                         return -1;
128         return 0;
129 }
130
131 struct ast_variable *ast_variable_browse(struct ast_config *config, char *category)
132 {
133         struct ast_category *cat;
134         cat = config->root;
135         while(cat) {
136                 if (cat->name == category)
137                         return cat->root;
138                 cat = cat->next;
139         }
140         cat = config->root;
141         while(cat) {
142                 if (!strcasecmp(cat->name, category))
143                         return cat->root;
144                 cat = cat->next;
145         }
146         return NULL;
147 }
148
149 char *ast_variable_retrieve(struct ast_config *config, char *category, char *value)
150 {
151         struct ast_variable *v;
152         if (category) {
153                 v = ast_variable_browse(config, category);
154                 while (v) {
155                         if (value == v->name)
156                                 return v->value;
157                         v=v->next;
158                 }
159                 v = ast_variable_browse(config, category);
160                 while (v) {
161                         if (!strcasecmp(value, v->name))
162                                 return v->value;
163                         v=v->next;
164                 }
165         } else {
166                 struct ast_category *cat;
167                 cat = config->root;
168                 while(cat) {
169                         v = cat->root;
170                         while (v) {
171                                 if (!strcasecmp(value, v->name))
172                                         return v->value;
173                                 v=v->next;
174                         }
175                         cat = cat->next;
176                 }
177         }
178         return NULL;
179 }
180
181 #ifdef PRESERVE_COMMENTS
182 int ast_variable_delete(struct ast_config *cfg, char *category, char *variable, char *value)
183 {
184         struct ast_variable *v, *pv, *bv, *bpv;
185         struct ast_category *cat;
186         cat = cfg->root;
187         while(cat) {
188                 if (cat->name == category) {
189                         break;
190                 }
191                 cat = cat->next;
192         }
193         if (!cat) {
194                 cat = cfg->root;
195                 while(cat) {
196                         if (!strcasecmp(cat->name, category)) {
197                                 break;
198                         }
199                         cat = cat->next;
200                 }
201         }
202         if (!cat)
203                 return -1;
204         v = cat->root;
205         pv = NULL;
206         while (v) {
207                 if ((variable == v->name) && (!value || !strcmp(v->value, value)))
208                         break;
209                 pv = v;
210                 v=v->next;
211         }
212         if (!v) {
213                 /* Get the last one that looks like it */
214                 bv = NULL;
215                 bpv = NULL;
216                 v = cat->root;
217                 pv = NULL;
218                 while (v) {
219                         if (!strcasecmp(variable, v->name) && (!value || !strcmp(v->value, value))) {
220                                 bv = v;
221                                 bpv = pv;
222                         }
223                         pv = v;
224                         v=v->next;
225                 }
226                 v = bv;
227         }
228
229         if (v) {
230                 /* Unlink from original position */
231                 if (pv) 
232                         pv->next = v->next;
233                 else
234                         cat->root = v->next;
235                 v->next = NULL;
236                 free(v->name);
237                 if (v->value)
238                         free(v->value);
239                 free_comments(v->sameline);
240                 free_comments(v->precomments);
241                 return 0;
242         }
243         return -1;
244 }
245
246 int ast_category_delete(struct ast_config *cfg, char *category)
247 {
248         struct ast_variable *v, *pv;
249         struct ast_category *cat, *cprev;
250         cat = cfg->root;
251         cprev = NULL;
252         while(cat) {
253                 if (cat->name == category) {
254                         break;
255                 }
256                 cprev = cat;
257                 cat = cat->next;
258         }
259         if (!cat) {
260                 cat = cfg->root;
261                 cprev = NULL;
262                 while(cat) {
263                         if (!strcasecmp(cat->name, category)) {
264                                 break;
265                         }
266                         cprev = cat;
267                         cat = cat->next;
268                 }
269         }
270         if (!cat)
271                 return -1;
272         /* Unlink it */
273         if (cprev)
274                 cprev->next = cat->next;
275         else
276                 cfg->root = cat->next;
277         v = cat->root;
278         while (v) {
279                 pv = v;
280                 v=v->next;
281                 if (pv->value)
282                         free(pv->value);
283                 if (pv->name)
284                         free(pv->name);
285                 free_comments(pv->sameline);
286                 free_comments(pv->precomments);
287                 free(pv);
288         }
289         free_comments(cat->sameline);
290         free_comments(cat->precomments);
291         free(cat);
292         return 0;
293 }
294
295 struct ast_variable *ast_variable_append_modify(struct ast_config *config, char *category, char *variable, char *value, int newcat, int newvar, int move)
296 {
297         struct ast_variable *v, *pv=NULL, *bv, *bpv;
298         struct ast_category *cat, *pcat;
299         cat = config->root;
300         if (!newcat) {
301                 while(cat) {
302                         if (cat->name == category) {
303                                 break;
304                         }
305                         cat = cat->next;
306                 }
307                 if (!cat) {
308                         cat = config->root;
309                         while(cat) {
310                                 if (!strcasecmp(cat->name, category)) {
311                                         break;
312                                 }
313                                 cat = cat->next;
314                         }
315                 }
316         }
317         if (!cat) {
318                 cat = malloc(sizeof(struct ast_category));
319                 if (!cat)
320                         return NULL;
321                 memset(cat, 0, sizeof(struct ast_category));
322                 strncpy(cat->name, category, sizeof(cat->name));
323                 if (config->root) {
324                         /* Put us at the end */
325                         pcat = config->root;
326                         while(pcat->next)
327                                 pcat = pcat->next;
328                         pcat->next = cat;
329                 } else {
330                         /* We're the first one */
331                         config->root = cat;
332                 }
333                         
334         }
335         if (!newvar) {
336                 v = cat->root;
337                 pv = NULL;
338                 while (v) {
339                         if (variable == v->name)
340                                 break;
341                         pv = v;
342                         v=v->next;
343                 }
344                 if (!v) {
345                         /* Get the last one that looks like it */
346                         bv = NULL;
347                         bpv = NULL;
348                         v = cat->root;
349                         pv = NULL;
350                         while (v) {
351                                 if (!strcasecmp(variable, v->name)) {
352                                         bv = v;
353                                         bpv = pv;
354                                 }
355                                 pv = v;
356                                 v=v->next;
357                         }
358                         v = bv;
359                 }
360         } else v = NULL;
361         if (v && move) {
362                 /* Unlink from original position */
363                 if (pv) 
364                         pv->next = v->next;
365                 else
366                         cat->root = v->next;
367                 v->next = NULL;
368         }
369         if (!v) {
370                 v = malloc(sizeof(struct ast_variable));
371                 if (!v)
372                         return NULL;
373                 memset(v, 0, sizeof(struct ast_variable));
374                 v->name = strdup(variable);
375                 move = 1;
376         }
377         if (v->value)
378                 free(v->value);
379         if (value)
380                 v->value = strdup(value);
381         else
382                 v->value = strdup("");
383         if (move) {
384                 if (cat->root) {
385                         pv = cat->root;
386                         while (pv->next) 
387                                 pv = pv->next;
388                         pv->next = v;
389                 } else {
390                         cat->root = v;
391                 }
392         }
393         return v;
394 }
395 #endif          
396
397 int ast_category_exist(struct ast_config *config, char *category_name)
398 {
399         struct ast_category *category = NULL;
400
401         category = config->root;
402
403         while(category) {
404                 if (!strcasecmp(category->name,category_name)) 
405                         return 1;
406                 category = category->next;
407         } 
408
409         return 0;
410 }
411
412 #ifdef PRESERVE_COMMENTS
413 static struct ast_comment *build_comment(char *cmt)
414 {
415         struct ast_comment *c;
416         int len = strlen(cmt) + 1;
417         c = malloc(sizeof(struct ast_comment) + len);
418         if (c) {
419                 /* Memset the header */
420                 memset(c, 0, sizeof(struct ast_comment));
421                 /* Copy the rest */
422                 strcpy(c->cmt, cmt);
423         }
424         return c;
425 }
426 #endif
427
428 static struct ast_config *__ast_load(char *configfile, struct ast_config *tmp, struct ast_category **_tmpc, struct ast_variable **_last, int includelevel
429 #ifdef PRESERVE_COMMENTS
430 , struct ast_comment_struct *acs
431 #endif
432 );
433
434 static int cfg_process(struct ast_config *tmp, struct ast_category **_tmpc, struct ast_variable **_last, char *buf, int lineno, char *configfile, int includelevel 
435 #ifdef PRESERVE_COMMENTS
436 ,struct ast_comment_struct *acs
437 #endif
438 )
439 {
440         char *c;
441         char *cur;
442         char *arg=NULL;
443         struct ast_config_reg *reg=NULL;
444     struct ast_config *(*load_func)(char *, struct ast_config *,struct ast_category **,struct ast_variable **,int
445 #ifdef PRESERVE_COMMENTS
446 ,struct ast_comment_struct *
447 #endif
448 );
449         
450         struct ast_variable *v;
451 #ifdef PRESERVE_COMMENTS
452         struct ast_comment *com = NULL;
453 #endif  
454         int object;
455         /* Strip off lines using ; as comment */
456         c = strchr(buf, ';');
457         if (c) {
458                 *c = '\0';
459 #ifdef PRESERVE_COMMENTS
460                 c++;
461                 if (*c != '!')
462                         com = build_comment(c);
463 #endif                  
464         }
465         cur = strip(buf);
466         if (!ast_strlen_zero(cur)) {
467                 /* Actually parse the entry */
468                 if (cur[0] == '[') {
469                         /* A category header */
470                         c = strchr(cur, ']');
471                         if (c) {
472                                 *c = 0;
473                                 *_tmpc = malloc(sizeof(struct ast_category));
474                                 if (!*_tmpc) {
475                                         ast_destroy(tmp);
476                                         ast_log(LOG_WARNING,
477                                                 "Out of memory, line %d\n", lineno);
478                                         return -1;
479                                 }
480                                 memset(*_tmpc, 0, sizeof(struct ast_category));
481                                 strncpy((*_tmpc)->name, cur+1, sizeof((*_tmpc)->name) - 1);
482                                 (*_tmpc)->root =  NULL;
483 #ifdef PRESERVE_COMMENTS
484                                 (*_tmpc)->precomments = acs->root;
485                                 (*_tmpc)->sameline = com;
486 #endif                          
487                                 if (!tmp->prev)
488                                         tmp->root = *_tmpc;
489                                 else
490                                         tmp->prev->next = *_tmpc;
491
492                                 tmp->prev = *_tmpc;
493 #ifdef PRESERVE_COMMENTS
494                                 acs->root = NULL;
495                                 acs->prev = NULL;
496 #endif                          
497                                 *_last =  NULL;
498                         } else {
499                                 ast_log(LOG_WARNING, 
500                                         "parse error: no closing ']', line %d of %s\n", lineno, configfile);
501                         }
502                 } else if (cur[0] == '#') {
503                         /* A directive */
504                         cur++;
505                         c = cur;
506                         while(*c && (*c > 32)) c++;
507                         if (*c) {
508                                 *c = '\0';
509                                 c++;
510                                 /* Find real argument */
511                                 while(*c  && (*c < 33)) c++;
512                                 if (!*c)
513                                         c = NULL;
514                         } else 
515                                 c = NULL;
516                         if (!strcasecmp(cur, "include")) {
517                                 /* A #include */
518                                 if (c) {
519                                         while((*c == '<') || (*c == '>') || (*c == '\"')) c++;
520                                         /* Get rid of leading mess */
521                                         cur = c;
522                                         while(!ast_strlen_zero(cur)) {
523                                                 c = cur + strlen(cur) - 1;
524                                                 if ((*c == '>') || (*c == '<') || (*c == '\"'))
525                                                         *c = '\0';
526                                                 else
527                                                         break;
528                                         }
529                                         
530                                         if((c = strchr(cur,':'))) {
531                                                 *c = '\0';
532                                                 c++;
533                                                 arg = c;
534                                         }
535                                         
536                                         if (includelevel < MAX_INCLUDE_LEVEL) {
537                                                 if(arg && cur) {
538                                                         load_func = NULL;
539                                                         if(ast_cust_config_list)
540                                                                 reg = get_ast_cust_config(cur);
541                                                         if(reg && reg->func)
542                                                                 load_func = reg->func;
543                                                         if(load_func) { 
544                                                                 ast_log(LOG_NOTICE,"External Include '%s' via '%s' config engine\n",arg,cur);
545                                                                 load_func(arg,tmp, _tmpc, _last, includelevel
546 #ifdef PRESERVE_COMMENTS
547                                                                                   ,&acs
548 #endif
549                                                                                   );
550                                                         }
551                                                         else 
552                                                                 ast_log(LOG_WARNING,"Cant Find Registered Config Engine [%s] for [%s]\n",cur,arg);
553                                                 }
554                                                 else {
555                                                         __ast_load(cur, tmp, _tmpc, _last, includelevel + 1
556 #ifdef PRESERVE_COMMENTS
557                                                                            ,acs
558 #endif
559                                                                            );
560                                                 }
561                                         } else
562                                                 ast_log(LOG_WARNING, "Maximum Include level (%d) exceeded\n", includelevel);
563                                 } else
564                                         ast_log(LOG_WARNING, "Directive '#include' needs an argument (filename) at line %d of %s\n", lineno, configfile);
565                                 /* Strip off leading and trailing "'s and <>'s */
566                         }
567                         else 
568                                 ast_log(LOG_WARNING, "Unknown directive '%s' at line %d of %s\n", cur, lineno, configfile);
569                 } else {
570                         /* Just a line (variable = value) */
571                         if (!*_tmpc) {
572                                 ast_log(LOG_WARNING,
573                                         "parse error: No category context for line %d of %s\n", lineno, configfile);
574                                 ast_destroy(tmp);
575                                 return -1;
576                         }
577                         c = strchr(cur, '=');
578                         if (c) {
579                                 *c = 0;
580                                 c++;
581                                 /* Ignore > in => */
582                                 if (*c== '>') {
583                                         object = 1;
584                                         c++;
585                                 } else
586                                         object = 0;
587                                 v = malloc(sizeof(struct ast_variable));
588                                 if (v) {
589                                         memset(v, 0, sizeof(struct ast_variable));
590                                         v->next = NULL;
591                                         v->name = strdup(strip(cur));
592                                         v->value = strdup(strip(c));
593                                         v->lineno = lineno;
594                                         v->object = object;
595                                         /* Put and reset comments */
596 #ifdef PRESERVE_COMMENTS
597                                         v->precomments = acs->root;
598                                         v->sameline = com;
599                                         acs->prev = NULL;
600                                         acs->root = NULL;
601 #endif                                  
602                                         v->blanklines = 0;
603                                         if (*_last)
604                                                 (*_last)->next = v;
605                                         else
606                                                 (*_tmpc)->root = v;
607                                         *_last = v;
608                                 } else {
609                                         ast_destroy(tmp);
610                                         ast_log(LOG_WARNING, "Out of memory, line %d\n", lineno);
611                                         return -1;
612                                 }
613                         } else {
614                                 ast_log(LOG_WARNING, "No '=' (equal sign) in line %d of %s\n", lineno, configfile);
615                         }
616                                                                                                                 
617                 }
618         } else {
619                 /* store any comments if there are any */
620 #ifdef PRESERVE_COMMENTS
621                 if (com) {
622                         if (acs->prev)
623                                 acs->prev->next = com;
624                         else
625                                 acs->root = com;
626                         acs->prev = com;
627                 } else {
628                 if (*_last) 
629                         (*_last)->blanklines++;
630
631                 }
632 #endif
633         }
634         return 0;
635 }
636
637 #ifdef PRESERVE_COMMENTS
638 static void dump_comments(FILE *f, struct ast_comment *comment)
639 {
640         while (comment) {
641                 fprintf(f, ";%s", comment->cmt);
642                 comment = comment->next;
643         }
644 }
645 #endif
646
647 int ast_save(char *configfile, struct ast_config *cfg, char *generator)
648 {
649         FILE *f;
650         char fn[256];
651         char date[256];
652         time_t t;
653         struct ast_variable *var;
654         struct ast_category *cat;
655         int blanklines = 0;
656         if (configfile[0] == '/') {
657                 strncpy(fn, configfile, sizeof(fn)-1);
658         } else {
659                 snprintf(fn, sizeof(fn), "%s/%s", AST_CONFIG_DIR, configfile);
660         }
661         time(&t);
662         strncpy(date, ctime(&t), sizeof(date));
663         if ((f = fopen(fn, "w"))) {
664                 if ((option_verbose > 1) && !option_debug)
665                         ast_verbose(  VERBOSE_PREFIX_2 "Saving '%s': ", fn);
666                 fprintf(f, ";!\n");
667                 fprintf(f, ";! Automatically generated configuration file\n");
668                 fprintf(f, ";! Filename: %s (%s)\n", configfile, fn);
669                 fprintf(f, ";! Generator: %s\n", generator);
670                 fprintf(f, ";! Creation Date: %s", date);
671                 fprintf(f, ";!\n");
672                 cat = cfg->root;
673                 while(cat) {
674 #ifdef PRESERVE_COMMENTS
675                         /* Dump any precomments */
676                         dump_comments(f, cat->precomments);
677 #endif
678                         /* Dump section with any appropriate comment */
679 #ifdef PRESERVE_COMMENTS
680                         if (cat->sameline) 
681                                 fprintf(f, "[%s]  ; %s\n", cat->name, cat->sameline->cmt);
682                         else
683 #endif
684                                 fprintf(f, "[%s]\n", cat->name);
685                         var = cat->root;
686                         while(var) {
687 #ifdef PRESERVE_COMMENTS
688                                 dump_comments(f, var->precomments);
689 #endif                          
690                                 if (var->sameline) 
691                                         fprintf(f, "%s %s %s  ; %s\n", var->name, (var->object ? "=>" : "="), var->value, var->sameline->cmt);
692                                 else    
693                                         fprintf(f, "%s %s %s\n", var->name, (var->object ? "=>" : "="), var->value);
694                                 if (var->blanklines) {
695                                         blanklines = var->blanklines;
696                                         while (blanklines) {
697                                                 fprintf(f, "\n");
698                                                 blanklines--;
699                                         }
700                                 }
701                                         
702                                 var = var->next;
703                         }
704 #if 0
705                         /* Put an empty line */
706                         fprintf(f, "\n");
707 #endif
708                         cat = cat->next;
709                 }
710 #ifdef PRESERVE_COMMENTS
711                 dump_comments(f, cfg->trailingcomments);
712 #endif          
713         } else {
714                 if (option_debug)
715                         printf("Unable to open for writing: %s\n", fn);
716                 else if (option_verbose > 1)
717                         printf( "Unable to write (%s)", strerror(errno));
718                 return -1;
719         }
720         fclose(f);
721         return 0;
722 }
723
724 static struct ast_config *__ast_load(char *configfile, struct ast_config *tmp, struct ast_category **_tmpc, struct ast_variable **_last, int includelevel
725 #ifdef PRESERVE_COMMENTS
726 , struct ast_comment_struct *acs
727 #endif
728 )
729 {
730         char fn[256];
731         char buf[8192];
732         FILE *f;
733         int lineno=0;
734         int master=0;
735         struct ast_config_reg *reg=NULL;
736         struct ast_config *(*load_func)(char *, struct ast_config *,struct ast_category **,struct ast_variable **,int
737 #ifdef PRESERVE_COMMENTS
738 ,struct ast_comment_struct *
739 #endif
740 );
741
742
743         load_func=NULL;
744         if(strcmp(configfile,config_conf_file) && strcmp(configfile,"asterisk.conf") && ast_cust_config_list) {
745           if(global_load_func)
746             load_func = global_load_func;
747           else {
748             reg = get_ast_cust_config_keyword(configfile);
749             if(reg && reg->func)
750               load_func = reg->func;
751             else {
752               reg = get_ast_cust_config_keyword("global");
753               if(reg && reg->func)
754                 global_load_func = load_func = reg->func;
755             }
756           }
757           
758           if(load_func) {
759             ast_log(LOG_NOTICE,"Loading Config %s via %s engine\n",configfile,reg && reg->name ? reg->name : "global");
760             tmp = load_func(configfile,tmp, _tmpc, _last, includelevel
761 #ifdef PRESERVE_COMMENTS
762 ,&acs
763 #endif
764 );
765             
766             if(tmp)
767               return tmp;
768           }
769         }
770
771
772
773
774         if (configfile[0] == '/') {
775                 strncpy(fn, configfile, sizeof(fn)-1);
776         } else {
777                 snprintf(fn, sizeof(fn), "%s/%s", (char *)ast_config_AST_CONFIG_DIR, configfile);
778         }
779         if ((option_verbose > 1) && !option_debug) {
780                 ast_verbose(  VERBOSE_PREFIX_2 "Parsing '%s': ", fn);
781                 fflush(stdout);
782         }
783         if ((f = fopen(fn, "r"))) {
784                 if (option_debug)
785                         ast_log(LOG_DEBUG, "Parsing %s\n", fn);
786                 else if (option_verbose > 1)
787                         ast_verbose( "Found\n");
788                 if (!tmp) {
789                         tmp = malloc(sizeof(struct ast_config));
790                         if (tmp)
791                                 memset(tmp, 0, sizeof(struct ast_config));
792
793                         master = 1;
794                 }
795                 if (!tmp) {
796                         ast_log(LOG_WARNING, "Out of memory\n");
797                         fclose(f);
798                         return NULL;
799                 }
800                 while(!feof(f)) {
801                         lineno++;
802                         if (fgets(buf, sizeof(buf), f)) {
803                                 if (cfg_process(tmp, _tmpc, _last, buf, lineno, configfile, includelevel
804 #ifdef PRESERVE_COMMENTS
805                                 , acs
806 #endif
807                                 )) {
808                                         fclose(f);
809                                         return NULL;
810                                 }
811                         }
812                 }
813                 fclose(f);              
814         } else {
815                 if (option_debug)
816                         ast_log(LOG_DEBUG, "No file to parse: %s\n", fn);
817                 else if (option_verbose > 1)
818                         ast_verbose( "Not found (%s)\n", strerror(errno));
819         }
820 #ifdef PRESERVE_COMMENTS
821         if (master) {
822                 /* Keep trailing comments */
823                 tmp->trailingcomments = acs->root;
824                 acs->root = NULL;
825                 acs->prev = NULL;
826         }
827 #endif
828         return tmp;
829 }
830
831 struct ast_config_reg *get_ast_cust_config_keyword(char *name) {
832         struct ast_config_reg *reg,*ret=NULL;
833         int x=0;
834         ast_mutex_lock(&ast_cust_config_lock);
835         for(reg=ast_cust_config_list;reg && !ret;reg=reg->next)
836                 for(x=0;x<reg->keycount && !ret ;x++) 
837                         if(!strcmp(reg->keywords[x],name))
838                                 ret=reg;
839         ast_mutex_unlock(&ast_cust_config_lock);
840         return ret;
841 }
842
843 struct ast_config_reg *get_ast_cust_config(char *name) {
844         struct ast_config_reg *ptr=NULL;
845         ast_mutex_lock(&ast_cust_config_lock);
846         for(ptr=ast_cust_config_list;ptr;ptr=ptr->next) {
847                 if(!strcmp(name,ptr->name))
848                         break;
849         }
850         ast_mutex_unlock(&ast_cust_config_lock);
851         return ptr;
852 }
853
854 void ast_config_destroy_all(void) {
855         struct ast_config_reg *key;
856         ast_mutex_lock(&ast_cust_config_lock);
857         for(key=ast_cust_config_list;key;key=key->next) {
858                 ast_config_deregister(key);
859         }
860         ast_cust_config_list = NULL;
861         ast_mutex_unlock(&ast_cust_config_lock);
862 }
863
864 struct ast_config_reg *get_config_registrations(void) {
865         return ast_cust_config_list;
866 }
867
868 int ast_config_register(struct ast_config_reg *new) {
869         struct ast_config_reg *ptr;
870         ast_mutex_lock(&ast_cust_config_lock);
871         new->keycount = 0;
872         if(!ast_cust_config_list)
873                 ast_cust_config_list = new;
874         else {
875                 for(ptr=ast_cust_config_list;ptr->next;ptr=ptr->next);
876                 ptr->next = new;
877         }
878         ast_mutex_unlock(&ast_cust_config_lock);
879         ast_log(LOG_NOTICE,"Registered Config Engine %s\n",new->name);
880         return 1;
881 }
882
883 int ast_config_deregister(struct ast_config_reg *del) {
884   struct ast_config_reg *ptr=NULL,*last=NULL;
885   ast_mutex_lock(&ast_cust_config_lock);
886   for(ptr=ast_cust_config_list;ptr;ptr=ptr->next) {
887           if(ptr == del) {
888                   if(last && ptr->next) {
889                           last->next = ptr->next;
890                   }
891                   else if(last && ! ptr->next) {
892                           last->next = NULL;
893                   }
894                   else if(! last && ptr->next) {
895                           ast_cust_config_list = ptr->next;
896                   }
897                   else if(! last && ! ptr->next) {
898                           ast_cust_config_list = NULL;
899                   }
900           }
901           last = ptr;
902   }
903   ast_mutex_unlock(&ast_cust_config_lock);
904   return 0;
905 }
906
907 int ast_cust_config_active(void) {
908         return (ast_cust_config >0) ? 1 : 0;
909 }
910
911 struct ast_config *ast_load(char *configfile)
912 {
913         struct ast_category *tmpc=NULL;
914         struct ast_variable *last = NULL;
915
916
917 #ifdef PRESERVE_COMMENTS
918         struct ast_comment_struct acs = { NULL, NULL };
919 #endif  
920
921
922         return __ast_load(configfile, NULL, &tmpc, &last, 0 
923 #ifdef PRESERVE_COMMENTS
924         ,&acs
925 #endif
926         );
927 }
928
929 char *ast_category_browse(struct ast_config *config, char *prev)
930 {       
931         struct ast_category *cat;
932         if (!prev) {
933                 if (config->root)
934                         return config->root->name;
935                 else
936                         return NULL;
937         }
938         cat = config->root;
939         while(cat) {
940                 if (cat->name == prev) {
941                         if (cat->next)
942                                 return cat->next->name;
943                         else
944                                 return NULL;
945                 }
946                 cat = cat->next;
947         }
948         cat = config->root;
949         while(cat) {
950                 if (!strcasecmp(cat->name, prev)) {
951                         if (cat->next)
952                                 return cat->next->name;
953                         else
954                                 return NULL;
955                 }
956                 cat = cat->next;
957         }
958         return NULL;
959 }
960
961
962 struct ast_config *ast_new_config(void) {
963         struct ast_config *config;
964         config = malloc(sizeof(struct ast_config));
965         memset(config,0,sizeof(struct ast_config));
966         return config;
967 }
968
969
970
971 struct ast_category *ast_new_category(char *name) {
972         struct ast_category *category;
973         category = malloc(sizeof(struct ast_category));
974         if(category) {
975                 memset(category,0,sizeof(struct ast_category));
976                 strncpy(category->name,name,sizeof(category->name));
977         }
978         return category;
979 }
980
981
982 struct ast_variable *ast_new_variable(char *name,char *value) {
983         struct ast_variable *variable;
984         variable = malloc(sizeof(struct ast_variable));
985         if(variable) {
986                 memset(variable,0,sizeof(struct ast_variable));
987                 variable->object=0;
988                 variable->name = malloc(strlen(name)+1);
989                 if(variable->name) {
990                         strcpy(variable->name,name);
991                         variable->value = malloc(strlen(value)+1);
992                         if(variable->value) {
993                                 strcpy(variable->value,value);
994                         }
995                         else {
996                                 free(variable->name);
997                                 variable->name = NULL;
998                         }
999                 }
1000         }
1001         if(!variable->value) {
1002                 free(variable);
1003                 variable = NULL;
1004         }
1005                 
1006         return variable;
1007 }
1008
1009 int ast_cust_config_register(struct ast_config_reg *new) {
1010         ast_config_register(new);
1011         read_ast_cust_config();
1012         return 1;
1013 }
1014 int ast_cust_config_deregister(struct ast_config_reg *new) {
1015         ast_config_deregister(new);
1016         read_ast_cust_config();
1017         return 1;
1018 }
1019
1020 static void clear_cust_keywords(void) {
1021         struct ast_config_reg *key;
1022         int x;
1023         ast_mutex_lock(&ast_cust_config_lock);
1024         for(key=get_config_registrations();key;key=key->next) {
1025                 for(x=0;x<key->keycount;x++) {
1026                         key->keywords[x][0] = '\0';
1027                 }
1028                 key->keycount=0;
1029         }
1030         ast_mutex_unlock(&ast_cust_config_lock);
1031 }
1032
1033 static int config_command(int fd, int argc, char **argv) {
1034         struct ast_config_reg *key;
1035         int x;
1036         
1037         ast_cli(fd,"\n\n");
1038         ast_mutex_lock(&ast_cust_config_lock);
1039         for(key=get_config_registrations();key;key=key->next) {
1040                 ast_cli(fd,"\nConfig Engine: %s\n",key->name);
1041                 for(x=0;x<key->keycount;x++)
1042                         ast_cli(fd,"===>%s\n",key->keywords[x]);
1043         }
1044         ast_mutex_unlock(&ast_cust_config_lock);
1045         ast_cli(fd,"\n\n");
1046         
1047   return 0;
1048 }
1049
1050 static struct ast_cli_entry config_command_struct = {
1051   { "show","config","handles", NULL }, config_command,
1052   "Show Config Handles", NULL };
1053
1054 int register_config_cli() {
1055         return ast_cli_register(&config_command_struct);
1056 }
1057
1058 int read_ast_cust_config(void) {
1059         char *cfg = config_conf_file;
1060         struct ast_config *config;
1061         struct ast_variable *v;
1062         struct ast_config_reg *ptr;
1063         struct ast_config_reg *test = NULL;
1064         clear_cust_keywords();
1065         config = ast_load(cfg);
1066         if(config) {
1067                 for(v = ast_variable_browse(config,"settings");v;v=v->next) {
1068                         
1069                         ptr = get_ast_cust_config(v->value);
1070                         if(ptr) {
1071                                 if(ptr->keycount >= CONFIG_KEYWORD_ARRAYLEN) {
1072                                         ast_log(LOG_WARNING,"Max Number of Bindings exceeded for %s->%s %d/%d\n",v->name,v->value,ptr->keycount,CONFIG_KEYWORD_ARRAYLEN);
1073                                 }
1074                                 else {
1075                                         if(strcmp(v->name,config_conf_file) && strcmp(v->name,"asterisk.conf")) {
1076                                                 if(!(test = get_ast_cust_config_keyword(v->name))) {
1077                                                         ast_log(LOG_NOTICE,"Binding: %s to %s\n",v->name,v->value);
1078                                                         strncpy(ptr->keywords[ptr->keycount],v->name,sizeof(ptr->keywords[ptr->keycount]));
1079                                                         ptr->keycount++;
1080                                                 }
1081                                         }
1082                                         else {
1083                                                 ast_log(LOG_WARNING,"Cannot bind %s, Permission Denied\n",v->name);
1084                                         }
1085                                 }
1086                         }
1087                 }
1088                 
1089                 ast_destroy(config);
1090         }
1091         else 
1092                 ast_log(LOG_WARNING,"config loader has no config file so nevermind.\n");
1093         
1094         return 0;
1095 }