Misc formatting cleanups
[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         struct ast_variable *v;
450 #ifdef PRESERVE_COMMENTS
451         struct ast_comment *com = NULL;
452 #endif  
453         int object;
454         /* Strip off lines using ; as comment */
455         c = strchr(buf, ';');
456         if (c) {
457                 *c = '\0';
458 #ifdef PRESERVE_COMMENTS
459                 c++;
460                 if (*c != '!')
461                         com = build_comment(c);
462 #endif                  
463         }
464         cur = strip(buf);
465         if (!ast_strlen_zero(cur)) {
466                 /* Actually parse the entry */
467                 if (cur[0] == '[') {
468                         /* A category header */
469                         c = strchr(cur, ']');
470                         if (c) {
471                                 *c = 0;
472                                 *_tmpc = malloc(sizeof(struct ast_category));
473                                 if (!*_tmpc) {
474                                         ast_destroy(tmp);
475                                         ast_log(LOG_WARNING,
476                                                 "Out of memory, line %d\n", lineno);
477                                         return -1;
478                                 }
479                                 memset(*_tmpc, 0, sizeof(struct ast_category));
480                                 strncpy((*_tmpc)->name, cur+1, sizeof((*_tmpc)->name) - 1);
481                                 (*_tmpc)->root =  NULL;
482 #ifdef PRESERVE_COMMENTS
483                                 (*_tmpc)->precomments = acs->root;
484                                 (*_tmpc)->sameline = com;
485 #endif                          
486                                 if (!tmp->prev)
487                                         tmp->root = *_tmpc;
488                                 else
489                                         tmp->prev->next = *_tmpc;
490
491                                 tmp->prev = *_tmpc;
492 #ifdef PRESERVE_COMMENTS
493                                 acs->root = NULL;
494                                 acs->prev = NULL;
495 #endif                          
496                                 *_last =  NULL;
497                         } else {
498                                 ast_log(LOG_WARNING, 
499                                         "parse error: no closing ']', line %d of %s\n", lineno, configfile);
500                         }
501                 } else if (cur[0] == '#') {
502                         /* A directive */
503                         cur++;
504                         c = cur;
505                         while(*c && (*c > 32)) c++;
506                         if (*c) {
507                                 *c = '\0';
508                                 c++;
509                                 /* Find real argument */
510                                 while(*c  && (*c < 33)) c++;
511                                 if (!*c)
512                                         c = NULL;
513                         } else 
514                                 c = NULL;
515                         if (!strcasecmp(cur, "include")) {
516                                 /* A #include */
517                                 if (c) {
518                                         while((*c == '<') || (*c == '>') || (*c == '\"')) c++;
519                                         /* Get rid of leading mess */
520                                         cur = c;
521                                         while (!ast_strlen_zero(cur)) {
522                                                 c = cur + strlen(cur) - 1;
523                                                 if ((*c == '>') || (*c == '<') || (*c == '\"'))
524                                                         *c = '\0';
525                                                 else
526                                                         break;
527                                         }
528                                         
529                                         if((c = strchr(cur,':'))) {
530                                                 *c = '\0';
531                                                 c++;
532                                                 arg = c;
533                                         }
534                                         
535                                         if (includelevel < MAX_INCLUDE_LEVEL) {
536                                                 if(arg && cur) {
537                                                         load_func = NULL;
538                                                         if(ast_cust_config_list)
539                                                                 reg = get_ast_cust_config(cur);
540                                                         if(reg && reg->func)
541                                                                 load_func = reg->func;
542                                                         if(load_func) { 
543                                                                 ast_log(LOG_NOTICE,"External Include '%s' via '%s' config engine\n",arg,cur);
544                                                                 load_func(arg,tmp, _tmpc, _last, includelevel
545 #ifdef PRESERVE_COMMENTS
546                                                                                   ,&acs
547 #endif
548                                                                                   );
549                                                         }
550                                                         else 
551                                                                 ast_log(LOG_WARNING,"Cant Find Registered Config Engine [%s] for [%s]\n",cur,arg);
552                                                 }
553                                                 else {
554                                                         __ast_load(cur, tmp, _tmpc, _last, includelevel + 1
555 #ifdef PRESERVE_COMMENTS
556                                                                            ,acs
557 #endif
558                                                                            );
559                                                 }
560                                         } else
561                                                 ast_log(LOG_WARNING, "Maximum Include level (%d) exceeded\n", includelevel);
562                                 } else
563                                         ast_log(LOG_WARNING, "Directive '#include' needs an argument (filename) at line %d of %s\n", lineno, configfile);
564                                 /* Strip off leading and trailing "'s and <>'s */
565                         }
566                         else 
567                                 ast_log(LOG_WARNING, "Unknown directive '%s' at line %d of %s\n", cur, lineno, configfile);
568                 } else {
569                         /* Just a line (variable = value) */
570                         if (!*_tmpc) {
571                                 ast_log(LOG_WARNING,
572                                         "parse error: No category context for line %d of %s\n", lineno, configfile);
573                                 ast_destroy(tmp);
574                                 return -1;
575                         }
576                         c = strchr(cur, '=');
577                         if (c) {
578                                 *c = 0;
579                                 c++;
580                                 /* Ignore > in => */
581                                 if (*c== '>') {
582                                         object = 1;
583                                         c++;
584                                 } else
585                                         object = 0;
586                                 v = malloc(sizeof(struct ast_variable));
587                                 if (v) {
588                                         memset(v, 0, sizeof(struct ast_variable));
589                                         v->next = NULL;
590                                         v->name = strdup(strip(cur));
591                                         v->value = strdup(strip(c));
592                                         v->lineno = lineno;
593                                         v->object = object;
594                                         /* Put and reset comments */
595 #ifdef PRESERVE_COMMENTS
596                                         v->precomments = acs->root;
597                                         v->sameline = com;
598                                         acs->prev = NULL;
599                                         acs->root = NULL;
600 #endif                                  
601                                         v->blanklines = 0;
602                                         if (*_last)
603                                                 (*_last)->next = v;
604                                         else
605                                                 (*_tmpc)->root = v;
606                                         *_last = v;
607                                 } else {
608                                         ast_destroy(tmp);
609                                         ast_log(LOG_WARNING, "Out of memory, line %d\n", lineno);
610                                         return -1;
611                                 }
612                         } else {
613                                 ast_log(LOG_WARNING, "No '=' (equal sign) in line %d of %s\n", lineno, configfile);
614                         }
615                                                                                                                 
616                 }
617         } else {
618                 /* store any comments if there are any */
619 #ifdef PRESERVE_COMMENTS
620                 if (com) {
621                         if (acs->prev)
622                                 acs->prev->next = com;
623                         else
624                                 acs->root = com;
625                         acs->prev = com;
626                 } else {
627                         if (*_last) 
628                                 (*_last)->blanklines++;
629                 }
630 #endif
631         }
632         return 0;
633 }
634
635 #ifdef PRESERVE_COMMENTS
636 static void dump_comments(FILE *f, struct ast_comment *comment)
637 {
638         while (comment) {
639                 fprintf(f, ";%s", comment->cmt);
640                 comment = comment->next;
641         }
642 }
643 #endif
644
645 int ast_save(char *configfile, struct ast_config *cfg, char *generator)
646 {
647         FILE *f;
648         char fn[256];
649         char date[256];
650         time_t t;
651         struct ast_variable *var;
652         struct ast_category *cat;
653         int blanklines = 0;
654         if (configfile[0] == '/') {
655                 strncpy(fn, configfile, sizeof(fn)-1);
656         } else {
657                 snprintf(fn, sizeof(fn), "%s/%s", AST_CONFIG_DIR, configfile);
658         }
659         time(&t);
660         strncpy(date, ctime(&t), sizeof(date));
661         if ((f = fopen(fn, "w"))) {
662                 if ((option_verbose > 1) && !option_debug)
663                         ast_verbose(  VERBOSE_PREFIX_2 "Saving '%s': ", fn);
664                 fprintf(f, ";!\n");
665                 fprintf(f, ";! Automatically generated configuration file\n");
666                 fprintf(f, ";! Filename: %s (%s)\n", configfile, fn);
667                 fprintf(f, ";! Generator: %s\n", generator);
668                 fprintf(f, ";! Creation Date: %s", date);
669                 fprintf(f, ";!\n");
670                 cat = cfg->root;
671                 while(cat) {
672 #ifdef PRESERVE_COMMENTS
673                         /* Dump any precomments */
674                         dump_comments(f, cat->precomments);
675 #endif
676                         /* Dump section with any appropriate comment */
677 #ifdef PRESERVE_COMMENTS
678                         if (cat->sameline) 
679                                 fprintf(f, "[%s]  ; %s\n", cat->name, cat->sameline->cmt);
680                         else
681 #endif
682                                 fprintf(f, "[%s]\n", cat->name);
683                         var = cat->root;
684                         while(var) {
685 #ifdef PRESERVE_COMMENTS
686                                 dump_comments(f, var->precomments);
687 #endif                          
688                                 if (var->sameline) 
689                                         fprintf(f, "%s %s %s  ; %s\n", var->name, (var->object ? "=>" : "="), var->value, var->sameline->cmt);
690                                 else    
691                                         fprintf(f, "%s %s %s\n", var->name, (var->object ? "=>" : "="), var->value);
692                                 if (var->blanklines) {
693                                         blanklines = var->blanklines;
694                                         while (blanklines) {
695                                                 fprintf(f, "\n");
696                                                 blanklines--;
697                                         }
698                                 }
699                                         
700                                 var = var->next;
701                         }
702 #if 0
703                         /* Put an empty line */
704                         fprintf(f, "\n");
705 #endif
706                         cat = cat->next;
707                 }
708 #ifdef PRESERVE_COMMENTS
709                 dump_comments(f, cfg->trailingcomments);
710 #endif          
711         } else {
712                 if (option_debug)
713                         printf("Unable to open for writing: %s\n", fn);
714                 else if (option_verbose > 1)
715                         printf( "Unable to write (%s)", strerror(errno));
716                 return -1;
717         }
718         fclose(f);
719         return 0;
720 }
721
722 static struct ast_config *__ast_load(char *configfile, struct ast_config *tmp, struct ast_category **_tmpc, struct ast_variable **_last, int includelevel
723 #ifdef PRESERVE_COMMENTS
724 , struct ast_comment_struct *acs
725 #endif
726 )
727 {
728         char fn[256];
729         char buf[8192];
730         FILE *f;
731         int lineno=0;
732         int master=0;
733         struct ast_config_reg *reg=NULL;
734         struct ast_config *(*load_func)(char *, struct ast_config *,struct ast_category **,struct ast_variable **,int
735 #ifdef PRESERVE_COMMENTS
736 ,struct ast_comment_struct *
737 #endif
738 );
739
740         load_func=NULL;
741         if (strcmp(configfile,config_conf_file) && strcmp(configfile,"asterisk.conf") && ast_cust_config_list) {
742                 if (global_load_func) {
743                         load_func = global_load_func;
744                 } else {
745                         reg = get_ast_cust_config_keyword(configfile);
746                         if (reg && reg->func) {
747                                 load_func = reg->func;
748                         } else {
749                                 reg = get_ast_cust_config_keyword("global");
750                                 if (reg && reg->func)
751                                         global_load_func = load_func = reg->func;
752                         }
753                 }
754
755                 if (load_func) {
756                         ast_log(LOG_NOTICE,"Loading Config %s via %s engine\n",configfile,reg && reg->name ? reg->name : "global");
757                         tmp = load_func(configfile,tmp, _tmpc, _last, includelevel
758 #ifdef PRESERVE_COMMENTS
759 ,&acs
760 #endif
761 );
762             
763                         if (tmp)
764                                 return tmp;
765                 }
766         }
767
768         if (configfile[0] == '/') {
769                 strncpy(fn, configfile, sizeof(fn)-1);
770         } else {
771                 snprintf(fn, sizeof(fn), "%s/%s", (char *)ast_config_AST_CONFIG_DIR, configfile);
772         }
773         if ((option_verbose > 1) && !option_debug) {
774                 ast_verbose(  VERBOSE_PREFIX_2 "Parsing '%s': ", fn);
775                 fflush(stdout);
776         }
777         if ((f = fopen(fn, "r"))) {
778                 if (option_debug)
779                         ast_log(LOG_DEBUG, "Parsing %s\n", fn);
780                 else if (option_verbose > 1)
781                         ast_verbose( "Found\n");
782                 if (!tmp) {
783                         tmp = malloc(sizeof(struct ast_config));
784                         if (tmp)
785                                 memset(tmp, 0, sizeof(struct ast_config));
786
787                         master = 1;
788                 }
789                 if (!tmp) {
790                         ast_log(LOG_WARNING, "Out of memory\n");
791                         fclose(f);
792                         return NULL;
793                 }
794                 while(!feof(f)) {
795                         lineno++;
796                         if (fgets(buf, sizeof(buf), f)) {
797                                 if (cfg_process(tmp, _tmpc, _last, buf, lineno, configfile, includelevel
798 #ifdef PRESERVE_COMMENTS
799                                 , acs
800 #endif
801                                 )) {
802                                         fclose(f);
803                                         return NULL;
804                                 }
805                         }
806                 }
807                 fclose(f);              
808         } else {
809                 if (option_debug)
810                         ast_log(LOG_DEBUG, "No file to parse: %s\n", fn);
811                 else if (option_verbose > 1)
812                         ast_verbose( "Not found (%s)\n", strerror(errno));
813         }
814 #ifdef PRESERVE_COMMENTS
815         if (master) {
816                 /* Keep trailing comments */
817                 tmp->trailingcomments = acs->root;
818                 acs->root = NULL;
819                 acs->prev = NULL;
820         }
821 #endif
822         return tmp;
823 }
824
825 struct ast_config_reg *get_ast_cust_config_keyword(char *name) 
826 {
827         struct ast_config_reg *reg,*ret=NULL;
828         int x=0;
829         ast_mutex_lock(&ast_cust_config_lock);
830         for (reg=ast_cust_config_list;reg && !ret;reg=reg->next) {
831                 for (x=0;x<reg->keycount && !ret ;x++) {
832                         if (!strcmp(reg->keywords[x],name))
833                                 ret=reg;
834                 }
835         }
836         ast_mutex_unlock(&ast_cust_config_lock);
837         return ret;
838 }
839
840 struct ast_config_reg *get_ast_cust_config(char *name) 
841 {
842         struct ast_config_reg *ptr=NULL;
843         ast_mutex_lock(&ast_cust_config_lock);
844         for (ptr=ast_cust_config_list;ptr;ptr=ptr->next) {
845                 if (!strcmp(name,ptr->name))
846                         break;
847         }
848         ast_mutex_unlock(&ast_cust_config_lock);
849         return ptr;
850 }
851
852 void ast_config_destroy_all(void) 
853 {
854         struct ast_config_reg *key;
855         ast_mutex_lock(&ast_cust_config_lock);
856         for (key=ast_cust_config_list;key;key=key->next) {
857                 ast_config_deregister(key);
858         }
859         ast_cust_config_list = NULL;
860         ast_mutex_unlock(&ast_cust_config_lock);
861 }
862
863 struct ast_config_reg *get_config_registrations(void) 
864 {
865         return ast_cust_config_list;
866 }
867
868 int ast_config_register(struct ast_config_reg *new) 
869 {
870         struct ast_config_reg *ptr;
871         ast_mutex_lock(&ast_cust_config_lock);
872         new->keycount = 0;
873         if (!ast_cust_config_list) {
874                 ast_cust_config_list = new;
875         } else {
876                 for(ptr=ast_cust_config_list;ptr->next;ptr=ptr->next);
877                         ptr->next = new;
878         }
879         ast_mutex_unlock(&ast_cust_config_lock);
880         ast_log(LOG_NOTICE,"Registered Config Engine %s\n",new->name);
881         return 1;
882 }
883
884 int ast_config_deregister(struct ast_config_reg *del) 
885 {
886         struct ast_config_reg *ptr=NULL,*last=NULL;
887         ast_mutex_lock(&ast_cust_config_lock);
888         for (ptr=ast_cust_config_list;ptr;ptr=ptr->next) {
889                 if (ptr == del) {
890                         if (last && ptr->next) {
891                                 last->next = ptr->next;
892                         } else if (last && ! ptr->next) {
893                                 last->next = NULL;
894                         } else if (!last && ptr->next) {
895                                 ast_cust_config_list = ptr->next;
896                         } else if (!last && !ptr->next) {
897                                 ast_cust_config_list = NULL;
898                         }
899                 }
900                 last = ptr;
901         }
902         ast_mutex_unlock(&ast_cust_config_lock);
903         return 0;
904 }
905
906 int ast_cust_config_active(void) {
907         return (ast_cust_config >0) ? 1 : 0;
908 }
909
910 struct ast_config *ast_load(char *configfile)
911 {
912         struct ast_category *tmpc=NULL;
913         struct ast_variable *last = NULL;
914
915
916 #ifdef PRESERVE_COMMENTS
917         struct ast_comment_struct acs = { NULL, NULL };
918 #endif  
919
920
921         return __ast_load(configfile, NULL, &tmpc, &last, 0 
922 #ifdef PRESERVE_COMMENTS
923         ,&acs
924 #endif
925         );
926 }
927
928 char *ast_category_browse(struct ast_config *config, char *prev)
929 {       
930         struct ast_category *cat;
931         if (!prev) {
932                 if (config->root)
933                         return config->root->name;
934                 else
935                         return NULL;
936         }
937         cat = config->root;
938         while(cat) {
939                 if (cat->name == prev) {
940                         if (cat->next)
941                                 return cat->next->name;
942                         else
943                                 return NULL;
944                 }
945                 cat = cat->next;
946         }
947         cat = config->root;
948         while(cat) {
949                 if (!strcasecmp(cat->name, prev)) {
950                         if (cat->next)
951                                 return cat->next->name;
952                         else
953                                 return NULL;
954                 }
955                 cat = cat->next;
956         }
957         return NULL;
958 }
959
960
961 struct ast_config *ast_new_config(void) 
962 {
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 {
973         struct ast_category *category;
974         category = malloc(sizeof(struct ast_category));
975         if (category) {
976                 memset(category,0,sizeof(struct ast_category));
977                 strncpy(category->name,name,sizeof(category->name));
978         }
979         return category;
980 }
981
982
983 struct ast_variable *ast_new_variable(char *name, char *value) 
984 {
985         struct ast_variable *variable;
986         variable = malloc(sizeof(struct ast_variable));
987         if (variable) {
988                 memset(variable,0,sizeof(struct ast_variable));
989                 variable->object=0;
990                 variable->name = malloc(strlen(name)+1);
991                 if (variable->name) {
992                         strcpy(variable->name,name);
993                         variable->value = malloc(strlen(value)+1);
994                         if (variable->value) {
995                                 strcpy(variable->value,value);
996                         } else {
997                                 free(variable->name);
998                                 variable->name = NULL;
999                         }
1000                 }
1001         }
1002         if (!variable->value) {
1003                 free(variable);
1004                 variable = NULL;
1005         }
1006                 
1007         return variable;
1008 }
1009
1010 int ast_cust_config_register(struct ast_config_reg *new) 
1011 {
1012         ast_config_register(new);
1013         read_ast_cust_config();
1014         return 1;
1015 }
1016 int ast_cust_config_deregister(struct ast_config_reg *new) 
1017 {
1018         ast_config_deregister(new);
1019         read_ast_cust_config();
1020         return 1;
1021 }
1022
1023 static void clear_cust_keywords(void) 
1024 {
1025         struct ast_config_reg *key;
1026         int x;
1027         ast_mutex_lock(&ast_cust_config_lock);
1028         for (key=get_config_registrations();key;key=key->next) {
1029                 for (x=0;x<key->keycount;x++) {
1030                         key->keywords[x][0] = '\0';
1031                 }
1032                 key->keycount=0;
1033         }
1034         ast_mutex_unlock(&ast_cust_config_lock);
1035 }
1036
1037 static int config_command(int fd, int argc, char **argv) 
1038 {
1039         struct ast_config_reg *key;
1040         int x;
1041         
1042         ast_cli(fd,"\n\n");
1043         ast_mutex_lock(&ast_cust_config_lock);
1044         for (key=get_config_registrations();key;key=key->next) {
1045                 ast_cli(fd,"\nConfig Engine: %s\n",key->name);
1046                 for (x=0;x<key->keycount;x++)
1047                         ast_cli(fd,"===>%s\n",key->keywords[x]);
1048         }
1049         ast_mutex_unlock(&ast_cust_config_lock);
1050         ast_cli(fd,"\n\n");
1051         
1052         return 0;
1053 }
1054
1055 static struct ast_cli_entry config_command_struct = {
1056   { "show","config","handles", NULL }, config_command,
1057   "Show Config Handles", NULL };
1058
1059 int register_config_cli() 
1060 {
1061         return ast_cli_register(&config_command_struct);
1062 }
1063
1064 int read_ast_cust_config(void) 
1065 {
1066         char *cfg = config_conf_file;
1067         struct ast_config *config;
1068         struct ast_variable *v;
1069         struct ast_config_reg *ptr;
1070         struct ast_config_reg *test = NULL;
1071
1072         clear_cust_keywords();
1073         config = ast_load(cfg);
1074         if (config) {
1075                 for (v = ast_variable_browse(config,"settings");v;v=v->next) {
1076                         
1077                         ptr = get_ast_cust_config(v->value);
1078                         if (ptr) {
1079                                 if (ptr->keycount >= CONFIG_KEYWORD_ARRAYLEN) {
1080                                         ast_log(LOG_WARNING,"Max Number of Bindings exceeded for %s->%s %d/%d\n",v->name,v->value,ptr->keycount,CONFIG_KEYWORD_ARRAYLEN);
1081                                 } else {
1082                                         if (strcmp(v->name,config_conf_file) && strcmp(v->name,"asterisk.conf")) {
1083                                                 if (!(test = get_ast_cust_config_keyword(v->name))) {
1084                                                         ast_log(LOG_NOTICE,"Binding: %s to %s\n",v->name,v->value);
1085                                                         strncpy(ptr->keywords[ptr->keycount],v->name,sizeof(ptr->keywords[ptr->keycount]));
1086                                                         ptr->keycount++;
1087                                                 }
1088                                         } else {
1089                                                 ast_log(LOG_WARNING,"Cannot bind %s, Permission Denied\n",v->name);
1090                                         }
1091                                 }
1092                         }
1093                 }
1094                 
1095                 ast_destroy(config);
1096         }
1097
1098         return 0;
1099 }