Cleanup formatting in config.c
[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 #endif
632         }
633         return 0;
634 }
635
636 #ifdef PRESERVE_COMMENTS
637 static void dump_comments(FILE *f, struct ast_comment *comment)
638 {
639         while (comment) {
640                 fprintf(f, ";%s", comment->cmt);
641                 comment = comment->next;
642         }
643 }
644 #endif
645
646 int ast_save(char *configfile, struct ast_config *cfg, char *generator)
647 {
648         FILE *f;
649         char fn[256];
650         char date[256];
651         time_t t;
652         struct ast_variable *var;
653         struct ast_category *cat;
654         int blanklines = 0;
655         if (configfile[0] == '/') {
656                 strncpy(fn, configfile, sizeof(fn)-1);
657         } else {
658                 snprintf(fn, sizeof(fn), "%s/%s", AST_CONFIG_DIR, configfile);
659         }
660         time(&t);
661         strncpy(date, ctime(&t), sizeof(date));
662         if ((f = fopen(fn, "w"))) {
663                 if ((option_verbose > 1) && !option_debug)
664                         ast_verbose(  VERBOSE_PREFIX_2 "Saving '%s': ", fn);
665                 fprintf(f, ";!\n");
666                 fprintf(f, ";! Automatically generated configuration file\n");
667                 fprintf(f, ";! Filename: %s (%s)\n", configfile, fn);
668                 fprintf(f, ";! Generator: %s\n", generator);
669                 fprintf(f, ";! Creation Date: %s", date);
670                 fprintf(f, ";!\n");
671                 cat = cfg->root;
672                 while(cat) {
673 #ifdef PRESERVE_COMMENTS
674                         /* Dump any precomments */
675                         dump_comments(f, cat->precomments);
676 #endif
677                         /* Dump section with any appropriate comment */
678 #ifdef PRESERVE_COMMENTS
679                         if (cat->sameline) 
680                                 fprintf(f, "[%s]  ; %s\n", cat->name, cat->sameline->cmt);
681                         else
682 #endif
683                                 fprintf(f, "[%s]\n", cat->name);
684                         var = cat->root;
685                         while(var) {
686 #ifdef PRESERVE_COMMENTS
687                                 dump_comments(f, var->precomments);
688 #endif                          
689                                 if (var->sameline) 
690                                         fprintf(f, "%s %s %s  ; %s\n", var->name, (var->object ? "=>" : "="), var->value, var->sameline->cmt);
691                                 else    
692                                         fprintf(f, "%s %s %s\n", var->name, (var->object ? "=>" : "="), var->value);
693                                 if (var->blanklines) {
694                                         blanklines = var->blanklines;
695                                         while (blanklines) {
696                                                 fprintf(f, "\n");
697                                                 blanklines--;
698                                         }
699                                 }
700                                         
701                                 var = var->next;
702                         }
703 #if 0
704                         /* Put an empty line */
705                         fprintf(f, "\n");
706 #endif
707                         cat = cat->next;
708                 }
709 #ifdef PRESERVE_COMMENTS
710                 dump_comments(f, cfg->trailingcomments);
711 #endif          
712         } else {
713                 if (option_debug)
714                         printf("Unable to open for writing: %s\n", fn);
715                 else if (option_verbose > 1)
716                         printf( "Unable to write (%s)", strerror(errno));
717                 return -1;
718         }
719         fclose(f);
720         return 0;
721 }
722
723 static struct ast_config *__ast_load(char *configfile, struct ast_config *tmp, struct ast_category **_tmpc, struct ast_variable **_last, int includelevel
724 #ifdef PRESERVE_COMMENTS
725 , struct ast_comment_struct *acs
726 #endif
727 )
728 {
729         char fn[256];
730         char buf[8192];
731         FILE *f;
732         int lineno=0;
733         int master=0;
734         struct ast_config_reg *reg=NULL;
735         struct ast_config *(*load_func)(char *, struct ast_config *,struct ast_category **,struct ast_variable **,int
736 #ifdef PRESERVE_COMMENTS
737 ,struct ast_comment_struct *
738 #endif
739 );
740
741         load_func=NULL;
742         if (strcmp(configfile,config_conf_file) && strcmp(configfile,"asterisk.conf") && ast_cust_config_list) {
743                 if (global_load_func) {
744                         load_func = global_load_func;
745                 } else {
746                         reg = get_ast_cust_config_keyword(configfile);
747                         if (reg && reg->func) {
748                                 load_func = reg->func;
749                         } else {
750                                 reg = get_ast_cust_config_keyword("global");
751                                 if (reg && reg->func)
752                                         global_load_func = load_func = reg->func;
753                         }
754                 }
755
756                 if (load_func) {
757                         ast_log(LOG_NOTICE,"Loading Config %s via %s engine\n",configfile,reg && reg->name ? reg->name : "global");
758                         tmp = load_func(configfile,tmp, _tmpc, _last, includelevel
759 #ifdef PRESERVE_COMMENTS
760 ,&acs
761 #endif
762 );
763             
764                         if (tmp)
765                                 return tmp;
766                 }
767         }
768
769         if (configfile[0] == '/') {
770                 strncpy(fn, configfile, sizeof(fn)-1);
771         } else {
772                 snprintf(fn, sizeof(fn), "%s/%s", (char *)ast_config_AST_CONFIG_DIR, configfile);
773         }
774         if ((option_verbose > 1) && !option_debug) {
775                 ast_verbose(  VERBOSE_PREFIX_2 "Parsing '%s': ", fn);
776                 fflush(stdout);
777         }
778         if ((f = fopen(fn, "r"))) {
779                 if (option_debug)
780                         ast_log(LOG_DEBUG, "Parsing %s\n", fn);
781                 else if (option_verbose > 1)
782                         ast_verbose( "Found\n");
783                 if (!tmp) {
784                         tmp = malloc(sizeof(struct ast_config));
785                         if (tmp)
786                                 memset(tmp, 0, sizeof(struct ast_config));
787
788                         master = 1;
789                 }
790                 if (!tmp) {
791                         ast_log(LOG_WARNING, "Out of memory\n");
792                         fclose(f);
793                         return NULL;
794                 }
795                 while(!feof(f)) {
796                         lineno++;
797                         if (fgets(buf, sizeof(buf), f)) {
798                                 if (cfg_process(tmp, _tmpc, _last, buf, lineno, configfile, includelevel
799 #ifdef PRESERVE_COMMENTS
800                                 , acs
801 #endif
802                                 )) {
803                                         fclose(f);
804                                         return NULL;
805                                 }
806                         }
807                 }
808                 fclose(f);              
809         } else {
810                 if (option_debug)
811                         ast_log(LOG_DEBUG, "No file to parse: %s\n", fn);
812                 else if (option_verbose > 1)
813                         ast_verbose( "Not found (%s)\n", strerror(errno));
814         }
815 #ifdef PRESERVE_COMMENTS
816         if (master) {
817                 /* Keep trailing comments */
818                 tmp->trailingcomments = acs->root;
819                 acs->root = NULL;
820                 acs->prev = NULL;
821         }
822 #endif
823         return tmp;
824 }
825
826 struct ast_config_reg *get_ast_cust_config_keyword(char *name) 
827 {
828         struct ast_config_reg *reg,*ret=NULL;
829         int x=0;
830         ast_mutex_lock(&ast_cust_config_lock);
831         for (reg=ast_cust_config_list;reg && !ret;reg=reg->next) {
832                 for (x=0;x<reg->keycount && !ret ;x++) {
833                         if (!strcmp(reg->keywords[x],name))
834                                 ret=reg;
835                 }
836         }
837         ast_mutex_unlock(&ast_cust_config_lock);
838         return ret;
839 }
840
841 struct ast_config_reg *get_ast_cust_config(char *name) 
842 {
843         struct ast_config_reg *ptr=NULL;
844         ast_mutex_lock(&ast_cust_config_lock);
845         for (ptr=ast_cust_config_list;ptr;ptr=ptr->next) {
846                 if (!strcmp(name,ptr->name))
847                         break;
848         }
849         ast_mutex_unlock(&ast_cust_config_lock);
850         return ptr;
851 }
852
853 void ast_config_destroy_all(void) 
854 {
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 {
866         return ast_cust_config_list;
867 }
868
869 int ast_config_register(struct ast_config_reg *new) 
870 {
871         struct ast_config_reg *ptr;
872         ast_mutex_lock(&ast_cust_config_lock);
873         new->keycount = 0;
874         if (!ast_cust_config_list) {
875                 ast_cust_config_list = new;
876         } else {
877                 for(ptr=ast_cust_config_list;ptr->next;ptr=ptr->next);
878                         ptr->next = new;
879         }
880         ast_mutex_unlock(&ast_cust_config_lock);
881         ast_log(LOG_NOTICE,"Registered Config Engine %s\n",new->name);
882         return 1;
883 }
884
885 int ast_config_deregister(struct ast_config_reg *del) 
886 {
887         struct ast_config_reg *ptr=NULL,*last=NULL;
888         ast_mutex_lock(&ast_cust_config_lock);
889         for (ptr=ast_cust_config_list;ptr;ptr=ptr->next) {
890                 if (ptr == del) {
891                         if (last && ptr->next) {
892                                 last->next = ptr->next;
893                         } else if (last && ! ptr->next) {
894                                 last->next = NULL;
895                         } else if (!last && ptr->next) {
896                                 ast_cust_config_list = ptr->next;
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 {
964         struct ast_config *config;
965         config = malloc(sizeof(struct ast_config));
966         memset(config,0,sizeof(struct ast_config));
967         return config;
968 }
969
970
971
972 struct ast_category *ast_new_category(char *name) 
973 {
974         struct ast_category *category;
975         category = malloc(sizeof(struct ast_category));
976         if (category) {
977                 memset(category,0,sizeof(struct ast_category));
978                 strncpy(category->name,name,sizeof(category->name));
979         }
980         return category;
981 }
982
983
984 struct ast_variable *ast_new_variable(char *name, char *value) 
985 {
986         struct ast_variable *variable;
987         variable = malloc(sizeof(struct ast_variable));
988         if (variable) {
989                 memset(variable,0,sizeof(struct ast_variable));
990                 variable->object=0;
991                 variable->name = malloc(strlen(name)+1);
992                 if (variable->name) {
993                         strcpy(variable->name,name);
994                         variable->value = malloc(strlen(value)+1);
995                         if (variable->value) {
996                                 strcpy(variable->value,value);
997                         } else {
998                                 free(variable->name);
999                                 variable->name = NULL;
1000                         }
1001                 }
1002         }
1003         if (!variable->value) {
1004                 free(variable);
1005                 variable = NULL;
1006         }
1007                 
1008         return variable;
1009 }
1010
1011 int ast_cust_config_register(struct ast_config_reg *new) 
1012 {
1013         ast_config_register(new);
1014         read_ast_cust_config();
1015         return 1;
1016 }
1017 int ast_cust_config_deregister(struct ast_config_reg *new) 
1018 {
1019         ast_config_deregister(new);
1020         read_ast_cust_config();
1021         return 1;
1022 }
1023
1024 static void clear_cust_keywords(void) 
1025 {
1026         struct ast_config_reg *key;
1027         int x;
1028         ast_mutex_lock(&ast_cust_config_lock);
1029         for (key=get_config_registrations();key;key=key->next) {
1030                 for (x=0;x<key->keycount;x++) {
1031                         key->keywords[x][0] = '\0';
1032                 }
1033                 key->keycount=0;
1034         }
1035         ast_mutex_unlock(&ast_cust_config_lock);
1036 }
1037
1038 static int config_command(int fd, int argc, char **argv) 
1039 {
1040         struct ast_config_reg *key;
1041         int x;
1042         
1043         ast_cli(fd,"\n\n");
1044         ast_mutex_lock(&ast_cust_config_lock);
1045         for (key=get_config_registrations();key;key=key->next) {
1046                 ast_cli(fd,"\nConfig Engine: %s\n",key->name);
1047                 for (x=0;x<key->keycount;x++)
1048                         ast_cli(fd,"===>%s\n",key->keywords[x]);
1049         }
1050         ast_mutex_unlock(&ast_cust_config_lock);
1051         ast_cli(fd,"\n\n");
1052         
1053         return 0;
1054 }
1055
1056 static struct ast_cli_entry config_command_struct = {
1057   { "show","config","handles", NULL }, config_command,
1058   "Show Config Handles", NULL };
1059
1060 int register_config_cli() 
1061 {
1062         return ast_cli_register(&config_command_struct);
1063 }
1064
1065 int read_ast_cust_config(void) 
1066 {
1067         char *cfg = config_conf_file;
1068         struct ast_config *config;
1069         struct ast_variable *v;
1070         struct ast_config_reg *ptr;
1071         struct ast_config_reg *test = NULL;
1072
1073         clear_cust_keywords();
1074         config = ast_load(cfg);
1075         if (config) {
1076                 for (v = ast_variable_browse(config,"settings");v;v=v->next) {
1077                         
1078                         ptr = get_ast_cust_config(v->value);
1079                         if (ptr) {
1080                                 if (ptr->keycount >= CONFIG_KEYWORD_ARRAYLEN) {
1081                                         ast_log(LOG_WARNING,"Max Number of Bindings exceeded for %s->%s %d/%d\n",v->name,v->value,ptr->keycount,CONFIG_KEYWORD_ARRAYLEN);
1082                                 } else {
1083                                         if (strcmp(v->name,config_conf_file) && strcmp(v->name,"asterisk.conf")) {
1084                                                 if (!(test = get_ast_cust_config_keyword(v->name))) {
1085                                                         ast_log(LOG_NOTICE,"Binding: %s to %s\n",v->name,v->value);
1086                                                         strncpy(ptr->keywords[ptr->keycount],v->name,sizeof(ptr->keywords[ptr->keycount]));
1087                                                         ptr->keycount++;
1088                                                 }
1089                                         } else {
1090                                                 ast_log(LOG_WARNING,"Cannot bind %s, Permission Denied\n",v->name);
1091                                         }
1092                                 }
1093                         }
1094                 }
1095                 
1096                 ast_destroy(config);
1097         }
1098
1099         return 0;
1100 }