Don't keep repeating the warning over and over when the end of the call is reached...
[asterisk-bristuff.git] / main / config.c
blobf0c4fe0df562d68b6724ae641cebe80de185d381
1 /*
2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 1999 - 2005, Digium, Inc.
6 * Mark Spencer <markster@digium.com>
8 * See http://www.asterisk.org for more information about
9 * the Asterisk project. Please do not directly contact
10 * any of the maintainers of this project for assistance;
11 * the project provides a web site, mailing lists and IRC
12 * channels for your use.
14 * This program is free software, distributed under the terms of
15 * the GNU General Public License Version 2. See the LICENSE file
16 * at the top of the source tree.
19 /*! \file
21 * \brief Configuration File Parser
23 * \author Mark Spencer <markster@digium.com>
25 * Includes the Asterisk Realtime API - ARA
26 * See doc/realtime.txt and doc/extconfig.txt
29 #include "asterisk.h"
31 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
33 #include <stdio.h>
34 #include <unistd.h>
35 #include <stdlib.h>
36 #include <string.h>
37 #include <errno.h>
38 #include <time.h>
39 #include <sys/stat.h>
40 #define AST_INCLUDE_GLOB 1
41 #ifdef AST_INCLUDE_GLOB
42 #if defined(__Darwin__) || defined(__CYGWIN__)
43 #define GLOB_ABORTED GLOB_ABEND
44 #endif
45 # include <glob.h>
46 #endif
48 #include "asterisk/config.h"
49 #include "asterisk/cli.h"
50 #include "asterisk/lock.h"
51 #include "asterisk/options.h"
52 #include "asterisk/logger.h"
53 #include "asterisk/utils.h"
54 #include "asterisk/channel.h"
55 #include "asterisk/app.h"
57 #define MAX_NESTED_COMMENTS 128
58 #define COMMENT_START ";--"
59 #define COMMENT_END "--;"
60 #define COMMENT_META ';'
61 #define COMMENT_TAG '-'
63 static char *extconfig_conf = "extconfig.conf";
65 /*! Growable string buffer */
66 static char *comment_buffer; /*!< this will be a comment collector.*/
67 static int comment_buffer_size; /*!< the amount of storage so far alloc'd for the comment_buffer */
69 static char *lline_buffer; /*!< A buffer for stuff behind the ; */
70 static int lline_buffer_size;
73 struct ast_comment {
74 struct ast_comment *next;
75 char cmt[0];
78 #define CB_INCR 250
80 static void CB_INIT(void)
82 if (!comment_buffer) {
83 comment_buffer = ast_malloc(CB_INCR);
84 if (!comment_buffer)
85 return;
86 comment_buffer[0] = 0;
87 comment_buffer_size = CB_INCR;
88 lline_buffer = ast_malloc(CB_INCR);
89 if (!lline_buffer)
90 return;
91 lline_buffer[0] = 0;
92 lline_buffer_size = CB_INCR;
93 } else {
94 comment_buffer[0] = 0;
95 lline_buffer[0] = 0;
99 static void CB_ADD(char *str)
101 int rem = comment_buffer_size - strlen(comment_buffer) - 1;
102 int siz = strlen(str);
103 if (rem < siz+1) {
104 comment_buffer = ast_realloc(comment_buffer, comment_buffer_size + CB_INCR + siz + 1);
105 if (!comment_buffer)
106 return;
107 comment_buffer_size += CB_INCR+siz+1;
109 strcat(comment_buffer,str);
112 static void CB_ADD_LEN(char *str, int len)
114 int cbl = strlen(comment_buffer) + 1;
115 int rem = comment_buffer_size - cbl;
116 if (rem < len+1) {
117 comment_buffer = ast_realloc(comment_buffer, comment_buffer_size + CB_INCR + len + 1);
118 if (!comment_buffer)
119 return;
120 comment_buffer_size += CB_INCR+len+1;
122 strncat(comment_buffer,str,len);
123 comment_buffer[cbl+len-1] = 0;
126 static void LLB_ADD(char *str)
128 int rem = lline_buffer_size - strlen(lline_buffer) - 1;
129 int siz = strlen(str);
130 if (rem < siz+1) {
131 lline_buffer = ast_realloc(lline_buffer, lline_buffer_size + CB_INCR + siz + 1);
132 if (!lline_buffer)
133 return;
134 lline_buffer_size += CB_INCR + siz + 1;
136 strcat(lline_buffer,str);
139 static void CB_RESET(void )
141 comment_buffer[0] = 0;
142 lline_buffer[0] = 0;
147 static struct ast_comment *ALLOC_COMMENT(const char *buffer)
149 struct ast_comment *x = ast_calloc(1,sizeof(struct ast_comment)+strlen(buffer)+1);
150 strcpy(x->cmt, buffer);
151 return x;
155 static struct ast_config_map {
156 struct ast_config_map *next;
157 char *name;
158 char *driver;
159 char *database;
160 char *table;
161 char stuff[0];
162 } *config_maps = NULL;
164 AST_MUTEX_DEFINE_STATIC(config_lock);
165 static struct ast_config_engine *config_engine_list;
167 #define MAX_INCLUDE_LEVEL 10
169 struct ast_category {
170 char name[80];
171 int ignored; /*!< do not let user of the config see this category */
172 int include_level;
173 struct ast_comment *precomments;
174 struct ast_comment *sameline;
175 struct ast_variable *root;
176 struct ast_variable *last;
177 struct ast_category *next;
180 struct ast_config {
181 struct ast_category *root;
182 struct ast_category *last;
183 struct ast_category *current;
184 struct ast_category *last_browse; /*!< used to cache the last category supplied via category_browse */
185 int include_level;
186 int max_include_level;
189 struct ast_variable *ast_variable_new(const char *name, const char *value)
191 struct ast_variable *variable;
192 int name_len = strlen(name) + 1;
194 if ((variable = ast_calloc(1, name_len + strlen(value) + 1 + sizeof(*variable)))) {
195 variable->name = variable->stuff;
196 variable->value = variable->stuff + name_len;
197 strcpy(variable->name,name);
198 strcpy(variable->value,value);
201 return variable;
204 void ast_variable_append(struct ast_category *category, struct ast_variable *variable)
206 if (!variable)
207 return;
208 if (category->last)
209 category->last->next = variable;
210 else
211 category->root = variable;
212 category->last = variable;
213 while (category->last->next)
214 category->last = category->last->next;
217 void ast_variables_destroy(struct ast_variable *v)
219 struct ast_variable *vn;
221 while(v) {
222 vn = v;
223 v = v->next;
224 free(vn);
228 struct ast_variable *ast_variable_browse(const struct ast_config *config, const char *category)
230 struct ast_category *cat = NULL;
232 if (category && config->last_browse && (config->last_browse->name == category))
233 cat = config->last_browse;
234 else
235 cat = ast_category_get(config, category);
237 return (cat) ? cat->root : NULL;
240 const char *ast_config_option(struct ast_config *cfg, const char *cat, const char *var)
242 const char *tmp;
243 tmp = ast_variable_retrieve(cfg, cat, var);
244 if (!tmp)
245 tmp = ast_variable_retrieve(cfg, "general", var);
246 return tmp;
250 const char *ast_variable_retrieve(const struct ast_config *config, const char *category, const char *variable)
252 struct ast_variable *v;
254 if (category) {
255 for (v = ast_variable_browse(config, category); v; v = v->next) {
256 if (!strcasecmp(variable, v->name))
257 return v->value;
259 } else {
260 struct ast_category *cat;
262 for (cat = config->root; cat; cat = cat->next)
263 for (v = cat->root; v; v = v->next)
264 if (!strcasecmp(variable, v->name))
265 return v->value;
268 return NULL;
271 static struct ast_variable *variable_clone(const struct ast_variable *old)
273 struct ast_variable *new = ast_variable_new(old->name, old->value);
275 if (new) {
276 new->lineno = old->lineno;
277 new->object = old->object;
278 new->blanklines = old->blanklines;
279 /* TODO: clone comments? */
282 return new;
285 static void move_variables(struct ast_category *old, struct ast_category *new)
287 struct ast_variable *var = old->root;
288 old->root = NULL;
289 #if 1
290 /* we can just move the entire list in a single op */
291 ast_variable_append(new, var);
292 #else
293 while (var) {
294 struct ast_variable *next = var->next;
295 var->next = NULL;
296 ast_variable_append(new, var);
297 var = next;
299 #endif
302 struct ast_category *ast_category_new(const char *name)
304 struct ast_category *category;
306 if ((category = ast_calloc(1, sizeof(*category))))
307 ast_copy_string(category->name, name, sizeof(category->name));
308 return category;
311 static struct ast_category *category_get(const struct ast_config *config, const char *category_name, int ignored)
313 struct ast_category *cat;
315 /* try exact match first, then case-insensitive match */
316 for (cat = config->root; cat; cat = cat->next) {
317 if (cat->name == category_name && (ignored || !cat->ignored))
318 return cat;
321 for (cat = config->root; cat; cat = cat->next) {
322 if (!strcasecmp(cat->name, category_name) && (ignored || !cat->ignored))
323 return cat;
326 return NULL;
329 struct ast_category *ast_category_get(const struct ast_config *config, const char *category_name)
331 return category_get(config, category_name, 0);
334 int ast_category_exist(const struct ast_config *config, const char *category_name)
336 return !!ast_category_get(config, category_name);
339 void ast_category_append(struct ast_config *config, struct ast_category *category)
341 if (config->last)
342 config->last->next = category;
343 else
344 config->root = category;
345 category->include_level = config->include_level;
346 config->last = category;
347 config->current = category;
350 void ast_category_destroy(struct ast_category *cat)
352 ast_variables_destroy(cat->root);
353 free(cat);
356 static struct ast_category *next_available_category(struct ast_category *cat)
358 for (; cat && cat->ignored; cat = cat->next);
360 return cat;
363 char *ast_category_browse(struct ast_config *config, const char *prev)
365 struct ast_category *cat = NULL;
367 if (prev && config->last_browse && (config->last_browse->name == prev))
368 cat = config->last_browse->next;
369 else if (!prev && config->root)
370 cat = config->root;
371 else if (prev) {
372 for (cat = config->root; cat; cat = cat->next) {
373 if (cat->name == prev) {
374 cat = cat->next;
375 break;
378 if (!cat) {
379 for (cat = config->root; cat; cat = cat->next) {
380 if (!strcasecmp(cat->name, prev)) {
381 cat = cat->next;
382 break;
388 if (cat)
389 cat = next_available_category(cat);
391 config->last_browse = cat;
392 return (cat) ? cat->name : NULL;
395 struct ast_variable *ast_category_detach_variables(struct ast_category *cat)
397 struct ast_variable *v;
399 v = cat->root;
400 cat->root = NULL;
401 cat->last = NULL;
403 return v;
406 void ast_category_rename(struct ast_category *cat, const char *name)
408 ast_copy_string(cat->name, name, sizeof(cat->name));
411 static void inherit_category(struct ast_category *new, const struct ast_category *base)
413 struct ast_variable *var;
415 for (var = base->root; var; var = var->next)
416 ast_variable_append(new, variable_clone(var));
419 struct ast_config *ast_config_new(void)
421 struct ast_config *config;
423 if ((config = ast_calloc(1, sizeof(*config))))
424 config->max_include_level = MAX_INCLUDE_LEVEL;
425 return config;
428 int ast_variable_delete(struct ast_category *category, char *variable, char *match)
430 struct ast_variable *cur, *prev=NULL, *curn;
431 int res = -1;
432 cur = category->root;
433 while (cur) {
434 if (cur->name == variable) {
435 if (prev) {
436 prev->next = cur->next;
437 if (cur == category->last)
438 category->last = prev;
439 } else {
440 category->root = cur->next;
441 if (cur == category->last)
442 category->last = NULL;
444 cur->next = NULL;
445 ast_variables_destroy(cur);
446 return 0;
448 prev = cur;
449 cur = cur->next;
452 prev = NULL;
453 cur = category->root;
454 while (cur) {
455 curn = cur->next;
456 if (!strcasecmp(cur->name, variable) && (ast_strlen_zero(match) || !strcasecmp(cur->value, match))) {
457 if (prev) {
458 prev->next = cur->next;
459 if (cur == category->last)
460 category->last = prev;
461 } else {
462 category->root = cur->next;
463 if (cur == category->last)
464 category->last = NULL;
466 cur->next = NULL;
467 ast_variables_destroy(cur);
468 res = 0;
469 } else
470 prev = cur;
472 cur = curn;
474 return res;
477 int ast_variable_update(struct ast_category *category, char *variable, char *value, char *match)
479 struct ast_variable *cur, *prev=NULL, *newer;
480 newer = ast_variable_new(variable, value);
481 if (!newer)
482 return -1;
483 cur = category->root;
484 while (cur) {
485 if (cur->name == variable) {
486 newer->next = cur->next;
487 newer->object = cur->object;
488 if (prev)
489 prev->next = newer;
490 else
491 category->root = newer;
492 if (category->last == cur)
493 category->last = newer;
494 cur->next = NULL;
495 ast_variables_destroy(cur);
496 return 0;
498 prev = cur;
499 cur = cur->next;
502 prev = NULL;
503 cur = category->root;
504 while (cur) {
505 if (!strcasecmp(cur->name, variable) && (ast_strlen_zero(match) || !strcasecmp(cur->value, match))) {
506 newer->next = cur->next;
507 newer->object = cur->object;
508 if (prev)
509 prev->next = newer;
510 else
511 category->root = newer;
512 if (category->last == cur)
513 category->last = newer;
514 cur->next = NULL;
515 ast_variables_destroy(cur);
516 return 0;
518 prev = cur;
519 cur = cur->next;
521 if (prev)
522 prev->next = newer;
523 else
524 category->root = newer;
525 return 0;
528 int ast_category_delete(struct ast_config *cfg, char *category)
530 struct ast_category *prev=NULL, *cat;
531 cat = cfg->root;
532 while(cat) {
533 if (cat->name == category) {
534 ast_variables_destroy(cat->root);
535 if (prev) {
536 prev->next = cat->next;
537 if (cat == cfg->last)
538 cfg->last = prev;
539 } else {
540 cfg->root = cat->next;
541 if (cat == cfg->last)
542 cfg->last = NULL;
544 free(cat);
545 return 0;
547 prev = cat;
548 cat = cat->next;
551 prev = NULL;
552 cat = cfg->root;
553 while(cat) {
554 if (!strcasecmp(cat->name, category)) {
555 ast_variables_destroy(cat->root);
556 if (prev) {
557 prev->next = cat->next;
558 if (cat == cfg->last)
559 cfg->last = prev;
560 } else {
561 cfg->root = cat->next;
562 if (cat == cfg->last)
563 cfg->last = NULL;
565 free(cat);
566 return 0;
568 prev = cat;
569 cat = cat->next;
571 return -1;
574 void ast_config_destroy(struct ast_config *cfg)
576 struct ast_category *cat, *catn;
578 if (!cfg)
579 return;
581 cat = cfg->root;
582 while(cat) {
583 ast_variables_destroy(cat->root);
584 catn = cat;
585 cat = cat->next;
586 free(catn);
588 free(cfg);
591 struct ast_category *ast_config_get_current_category(const struct ast_config *cfg)
593 return cfg->current;
596 void ast_config_set_current_category(struct ast_config *cfg, const struct ast_category *cat)
598 /* cast below is just to silence compiler warning about dropping "const" */
599 cfg->current = (struct ast_category *) cat;
602 static int process_text_line(struct ast_config *cfg, struct ast_category **cat, char *buf, int lineno, const char *configfile, int withcomments)
604 char *c;
605 char *cur = buf;
606 struct ast_variable *v;
607 char cmd[512], exec_file[512];
608 int object, do_exec, do_include;
610 /* Actually parse the entry */
611 if (cur[0] == '[') {
612 struct ast_category *newcat = NULL;
613 char *catname;
615 /* A category header */
616 c = strchr(cur, ']');
617 if (!c) {
618 ast_log(LOG_WARNING, "parse error: no closing ']', line %d of %s\n", lineno, configfile);
619 return -1;
621 *c++ = '\0';
622 cur++;
623 if (*c++ != '(')
624 c = NULL;
625 catname = cur;
626 if (!(*cat = newcat = ast_category_new(catname))) {
627 return -1;
629 /* add comments */
630 if (withcomments && comment_buffer && comment_buffer[0] ) {
631 newcat->precomments = ALLOC_COMMENT(comment_buffer);
633 if (withcomments && lline_buffer && lline_buffer[0] ) {
634 newcat->sameline = ALLOC_COMMENT(lline_buffer);
636 if( withcomments )
637 CB_RESET();
639 /* If there are options or categories to inherit from, process them now */
640 if (c) {
641 if (!(cur = strchr(c, ')'))) {
642 ast_log(LOG_WARNING, "parse error: no closing ')', line %d of %s\n", lineno, configfile);
643 return -1;
645 *cur = '\0';
646 while ((cur = strsep(&c, ","))) {
647 if (!strcasecmp(cur, "!")) {
648 (*cat)->ignored = 1;
649 } else if (!strcasecmp(cur, "+")) {
650 *cat = category_get(cfg, catname, 1);
651 if (!*cat) {
652 ast_config_destroy(cfg);
653 if (newcat)
654 ast_category_destroy(newcat);
655 ast_log(LOG_WARNING, "Category addition requested, but category '%s' does not exist, line %d of %s\n", catname, lineno, configfile);
656 return -1;
658 if (newcat) {
659 move_variables(newcat, *cat);
660 ast_category_destroy(newcat);
661 newcat = NULL;
663 } else {
664 struct ast_category *base;
666 base = category_get(cfg, cur, 1);
667 if (!base) {
668 ast_log(LOG_WARNING, "Inheritance requested, but category '%s' does not exist, line %d of %s\n", cur, lineno, configfile);
669 return -1;
671 inherit_category(*cat, base);
675 if (newcat)
676 ast_category_append(cfg, *cat);
677 } else if (cur[0] == '#') {
678 /* A directive */
679 cur++;
680 c = cur;
681 while(*c && (*c > 32)) c++;
682 if (*c) {
683 *c = '\0';
684 /* Find real argument */
685 c = ast_skip_blanks(c + 1);
686 if (!*c)
687 c = NULL;
688 } else
689 c = NULL;
690 do_include = !strcasecmp(cur, "include");
691 if(!do_include)
692 do_exec = !strcasecmp(cur, "exec");
693 else
694 do_exec = 0;
695 if (do_exec && !ast_opt_exec_includes) {
696 ast_log(LOG_WARNING, "Cannot perform #exec unless execincludes option is enabled in asterisk.conf (options section)!\n");
697 do_exec = 0;
699 if (do_include || do_exec) {
700 if (c) {
701 /* Strip off leading and trailing "'s and <>'s */
702 while((*c == '<') || (*c == '>') || (*c == '\"')) c++;
703 /* Get rid of leading mess */
704 cur = c;
705 while (!ast_strlen_zero(cur)) {
706 c = cur + strlen(cur) - 1;
707 if ((*c == '>') || (*c == '<') || (*c == '\"'))
708 *c = '\0';
709 else
710 break;
712 /* #exec </path/to/executable>
713 We create a tmp file, then we #include it, then we delete it. */
714 if (do_exec) {
715 snprintf(exec_file, sizeof(exec_file), "/var/tmp/exec.%d.%ld", (int)time(NULL), (long)pthread_self());
716 snprintf(cmd, sizeof(cmd), "%s > %s 2>&1", cur, exec_file);
717 ast_safe_system(cmd);
718 cur = exec_file;
719 } else
720 exec_file[0] = '\0';
721 /* A #include */
722 do_include = ast_config_internal_load(cur, cfg, withcomments) ? 1 : 0;
723 if(!ast_strlen_zero(exec_file))
724 unlink(exec_file);
725 if(!do_include)
726 return 0;
728 } else {
729 ast_log(LOG_WARNING, "Directive '#%s' needs an argument (%s) at line %d of %s\n",
730 do_exec ? "exec" : "include",
731 do_exec ? "/path/to/executable" : "filename",
732 lineno,
733 configfile);
736 else
737 ast_log(LOG_WARNING, "Unknown directive '%s' at line %d of %s\n", cur, lineno, configfile);
738 } else {
739 /* Just a line (variable = value) */
740 if (!*cat) {
741 ast_log(LOG_WARNING,
742 "parse error: No category context for line %d of %s\n", lineno, configfile);
743 return -1;
745 c = strchr(cur, '=');
746 if (c) {
747 *c = 0;
748 c++;
749 /* Ignore > in => */
750 if (*c== '>') {
751 object = 1;
752 c++;
753 } else
754 object = 0;
755 if ((v = ast_variable_new(ast_strip(cur), ast_strip(c)))) {
756 v->lineno = lineno;
757 v->object = object;
758 /* Put and reset comments */
759 v->blanklines = 0;
760 ast_variable_append(*cat, v);
761 /* add comments */
762 if (withcomments && comment_buffer && comment_buffer[0] ) {
763 v->precomments = ALLOC_COMMENT(comment_buffer);
765 if (withcomments && lline_buffer && lline_buffer[0] ) {
766 v->sameline = ALLOC_COMMENT(lline_buffer);
768 if( withcomments )
769 CB_RESET();
771 } else {
772 return -1;
774 } else {
775 ast_log(LOG_WARNING, "No '=' (equal sign) in line %d of %s\n", lineno, configfile);
778 return 0;
781 static struct ast_config *config_text_file_load(const char *database, const char *table, const char *filename, struct ast_config *cfg, int withcomments)
783 char fn[256];
784 char buf[8192];
785 char *new_buf, *comment_p, *process_buf;
786 FILE *f;
787 int lineno=0;
788 int comment = 0, nest[MAX_NESTED_COMMENTS];
789 struct ast_category *cat = NULL;
790 int count = 0;
791 struct stat statbuf;
793 cat = ast_config_get_current_category(cfg);
795 if (filename[0] == '/') {
796 ast_copy_string(fn, filename, sizeof(fn));
797 } else {
798 snprintf(fn, sizeof(fn), "%s/%s", (char *)ast_config_AST_CONFIG_DIR, filename);
801 if (withcomments) {
802 CB_INIT();
803 if (!lline_buffer || !comment_buffer) {
804 ast_log(LOG_ERROR, "Failed to initialize the comment buffer!\n");
805 return NULL;
808 #ifdef AST_INCLUDE_GLOB
810 int glob_ret;
811 glob_t globbuf;
812 globbuf.gl_offs = 0; /* initialize it to silence gcc */
813 #ifdef SOLARIS
814 glob_ret = glob(fn, GLOB_NOCHECK, NULL, &globbuf);
815 #else
816 glob_ret = glob(fn, GLOB_NOMAGIC|GLOB_BRACE, NULL, &globbuf);
817 #endif
818 if (glob_ret == GLOB_NOSPACE)
819 ast_log(LOG_WARNING,
820 "Glob Expansion of pattern '%s' failed: Not enough memory\n", fn);
821 else if (glob_ret == GLOB_ABORTED)
822 ast_log(LOG_WARNING,
823 "Glob Expansion of pattern '%s' failed: Read error\n", fn);
824 else {
825 /* loop over expanded files */
826 int i;
827 for (i=0; i<globbuf.gl_pathc; i++) {
828 ast_copy_string(fn, globbuf.gl_pathv[i], sizeof(fn));
829 #endif
830 do {
831 if (stat(fn, &statbuf))
832 continue;
834 if (!S_ISREG(statbuf.st_mode)) {
835 ast_log(LOG_WARNING, "'%s' is not a regular file, ignoring\n", fn);
836 continue;
838 if (option_verbose > 1) {
839 ast_verbose(VERBOSE_PREFIX_2 "Parsing '%s': ", fn);
840 fflush(stdout);
842 if (!(f = fopen(fn, "r"))) {
843 if (option_debug)
844 ast_log(LOG_DEBUG, "No file to parse: %s\n", fn);
845 if (option_verbose > 1)
846 ast_verbose( "Not found (%s)\n", strerror(errno));
847 continue;
849 count++;
850 if (option_debug)
851 ast_log(LOG_DEBUG, "Parsing %s\n", fn);
852 if (option_verbose > 1)
853 ast_verbose("Found\n");
854 while(!feof(f)) {
855 lineno++;
856 if (fgets(buf, sizeof(buf), f)) {
857 if ( withcomments ) {
858 ast_log(LOG_NOTICE, "moo\n");
859 CB_ADD(lline_buffer); /* add the current lline buffer to the comment buffer */
860 lline_buffer[0] = 0; /* erase the lline buffer */
863 new_buf = buf;
864 if (comment)
865 process_buf = NULL;
866 else
867 process_buf = buf;
869 while ((comment_p = strchr(new_buf, COMMENT_META))) {
870 if ((comment_p > new_buf) && (*(comment_p-1) == '\\')) {
871 /* Yuck, gotta memmove */
872 memmove(comment_p - 1, comment_p, strlen(comment_p) + 1);
873 new_buf = comment_p;
874 } else if(comment_p[1] == COMMENT_TAG && comment_p[2] == COMMENT_TAG && (comment_p[3] != '-')) {
875 /* Meta-Comment start detected ";--" */
876 if (comment < MAX_NESTED_COMMENTS) {
877 *comment_p = '\0';
878 new_buf = comment_p + 3;
879 comment++;
880 nest[comment-1] = lineno;
881 } else {
882 ast_log(LOG_ERROR, "Maximum nest limit of %d reached.\n", MAX_NESTED_COMMENTS);
884 } else if ((comment_p >= new_buf + 2) &&
885 (*(comment_p - 1) == COMMENT_TAG) &&
886 (*(comment_p - 2) == COMMENT_TAG)) {
887 /* Meta-Comment end detected */
888 comment--;
889 new_buf = comment_p + 1;
890 if (!comment) {
891 /* Back to non-comment now */
892 if (process_buf) {
893 /* Actually have to move what's left over the top, then continue */
894 char *oldptr;
895 oldptr = process_buf + strlen(process_buf);
896 if ( withcomments ) {
897 CB_ADD(";");
898 CB_ADD_LEN(oldptr+1,new_buf-oldptr-1);
901 memmove(oldptr, new_buf, strlen(new_buf) + 1);
902 new_buf = oldptr;
903 } else
904 process_buf = new_buf;
906 } else {
907 if (!comment) {
908 /* If ; is found, and we are not nested in a comment,
909 we immediately stop all comment processing */
910 if ( withcomments ) {
911 LLB_ADD(comment_p);
913 *comment_p = '\0';
914 new_buf = comment_p;
915 } else
916 new_buf = comment_p + 1;
919 if( withcomments && comment && !process_buf )
921 CB_ADD(buf); /* the whole line is a comment, store it */
924 if (process_buf) {
925 char *buf = ast_strip(process_buf);
926 if (!ast_strlen_zero(buf)) {
927 if (process_text_line(cfg, &cat, buf, lineno, filename, withcomments)) {
928 cfg = NULL;
929 break;
935 fclose(f);
936 } while(0);
937 if (comment) {
938 ast_log(LOG_WARNING,"Unterminated comment detected beginning on line %d\n", nest[comment]);
940 #ifdef AST_INCLUDE_GLOB
941 if (!cfg)
942 break;
944 globfree(&globbuf);
947 #endif
949 if (cfg->include_level == 1 && withcomments && comment_buffer) {
950 free(comment_buffer);
951 free(lline_buffer);
952 comment_buffer = NULL;
953 lline_buffer = NULL;
954 comment_buffer_size = 0;
955 lline_buffer_size = 0;
958 if (count == 0)
959 return NULL;
961 return cfg;
964 int config_text_file_save(const char *configfile, const struct ast_config *cfg, const char *generator)
966 FILE *f;
967 char fn[256];
968 char date[256]="";
969 time_t t;
970 struct ast_variable *var;
971 struct ast_category *cat;
972 struct ast_comment *cmt;
973 int blanklines = 0;
975 if (configfile[0] == '/') {
976 ast_copy_string(fn, configfile, sizeof(fn));
977 } else {
978 snprintf(fn, sizeof(fn), "%s/%s", ast_config_AST_CONFIG_DIR, configfile);
980 time(&t);
981 ast_copy_string(date, ctime(&t), sizeof(date));
982 #ifdef __CYGWIN__
983 if ((f = fopen(fn, "w+"))) {
984 #else
985 if ((f = fopen(fn, "w"))) {
986 #endif
987 if (option_verbose > 1)
988 ast_verbose(VERBOSE_PREFIX_2 "Saving '%s': ", fn);
989 fprintf(f, ";!\n");
990 fprintf(f, ";! Automatically generated configuration file\n");
991 if (strcmp(configfile, fn))
992 fprintf(f, ";! Filename: %s (%s)\n", configfile, fn);
993 else
994 fprintf(f, ";! Filename: %s\n", configfile);
995 fprintf(f, ";! Generator: %s\n", generator);
996 fprintf(f, ";! Creation Date: %s", date);
997 fprintf(f, ";!\n");
998 cat = cfg->root;
999 while(cat) {
1000 /* Dump section with any appropriate comment */
1001 for (cmt = cat->precomments; cmt; cmt=cmt->next)
1003 if (cmt->cmt[0] != ';' || cmt->cmt[1] != '!')
1004 fprintf(f,"%s", cmt->cmt);
1006 if (!cat->precomments)
1007 fprintf(f,"\n");
1008 fprintf(f, "[%s]", cat->name);
1009 for(cmt = cat->sameline; cmt; cmt=cmt->next)
1011 fprintf(f,"%s", cmt->cmt);
1013 if (!cat->sameline)
1014 fprintf(f,"\n");
1015 var = cat->root;
1016 while(var) {
1017 for (cmt = var->precomments; cmt; cmt=cmt->next)
1019 if (cmt->cmt[0] != ';' || cmt->cmt[1] != '!')
1020 fprintf(f,"%s", cmt->cmt);
1022 if (var->sameline)
1023 fprintf(f, "%s %s %s %s", var->name, (var->object ? "=>" : "="), var->value, var->sameline->cmt);
1024 else
1025 fprintf(f, "%s %s %s\n", var->name, (var->object ? "=>" : "="), var->value);
1026 if (var->blanklines) {
1027 blanklines = var->blanklines;
1028 while (blanklines--)
1029 fprintf(f, "\n");
1032 var = var->next;
1034 #if 0
1035 /* Put an empty line */
1036 fprintf(f, "\n");
1037 #endif
1038 cat = cat->next;
1040 if ((option_verbose > 1) && !option_debug)
1041 ast_verbose("Saved\n");
1042 } else {
1043 if (option_debug)
1044 ast_log(LOG_DEBUG, "Unable to open for writing: %s\n", fn);
1045 if (option_verbose > 1)
1046 ast_verbose(VERBOSE_PREFIX_2 "Unable to write (%s)", strerror(errno));
1047 return -1;
1049 fclose(f);
1050 return 0;
1053 static void clear_config_maps(void)
1055 struct ast_config_map *map;
1057 ast_mutex_lock(&config_lock);
1059 while (config_maps) {
1060 map = config_maps;
1061 config_maps = config_maps->next;
1062 free(map);
1065 ast_mutex_unlock(&config_lock);
1068 static int append_mapping(char *name, char *driver, char *database, char *table)
1070 struct ast_config_map *map;
1071 int length;
1073 length = sizeof(*map);
1074 length += strlen(name) + 1;
1075 length += strlen(driver) + 1;
1076 length += strlen(database) + 1;
1077 if (table)
1078 length += strlen(table) + 1;
1080 if (!(map = ast_calloc(1, length)))
1081 return -1;
1083 map->name = map->stuff;
1084 strcpy(map->name, name);
1085 map->driver = map->name + strlen(map->name) + 1;
1086 strcpy(map->driver, driver);
1087 map->database = map->driver + strlen(map->driver) + 1;
1088 strcpy(map->database, database);
1089 if (table) {
1090 map->table = map->database + strlen(map->database) + 1;
1091 strcpy(map->table, table);
1093 map->next = config_maps;
1095 if (option_verbose > 1)
1096 ast_verbose(VERBOSE_PREFIX_2 "Binding %s to %s/%s/%s\n",
1097 map->name, map->driver, map->database, map->table ? map->table : map->name);
1099 config_maps = map;
1100 return 0;
1103 int read_config_maps(void)
1105 struct ast_config *config, *configtmp;
1106 struct ast_variable *v;
1107 char *driver, *table, *database, *stringp;
1109 clear_config_maps();
1111 configtmp = ast_config_new();
1112 configtmp->max_include_level = 1;
1113 config = ast_config_internal_load(extconfig_conf, configtmp, 0);
1114 if (!config) {
1115 ast_config_destroy(configtmp);
1116 return 0;
1119 for (v = ast_variable_browse(config, "settings"); v; v = v->next) {
1120 stringp = v->value;
1121 driver = strsep(&stringp, ",");
1123 /* check if the database text starts with a double quote */
1124 if (*stringp == '"') {
1125 stringp++;
1126 database = strsep(&stringp, "\"");
1127 strsep(&stringp, ",");
1128 } else {
1129 /* apparently this text has no quotes */
1130 database = strsep(&stringp, ",");
1133 table = strsep(&stringp, ",");
1135 if (!strcmp(v->name, extconfig_conf)) {
1136 ast_log(LOG_WARNING, "Cannot bind '%s'!\n", extconfig_conf);
1137 continue;
1140 if (!strcmp(v->name, "asterisk.conf")) {
1141 ast_log(LOG_WARNING, "Cannot bind 'asterisk.conf'!\n");
1142 continue;
1145 if (!strcmp(v->name, "logger.conf")) {
1146 ast_log(LOG_WARNING, "Cannot bind 'logger.conf'!\n");
1147 continue;
1150 if (!driver || !database)
1151 continue;
1152 if (!strcasecmp(v->name, "sipfriends")) {
1153 ast_log(LOG_WARNING, "The 'sipfriends' table is obsolete, update your config to use sipusers and sippeers, though they can point to the same table.\n");
1154 append_mapping("sipusers", driver, database, table ? table : "sipfriends");
1155 append_mapping("sippeers", driver, database, table ? table : "sipfriends");
1156 } else if (!strcasecmp(v->name, "iaxfriends")) {
1157 ast_log(LOG_WARNING, "The 'iaxfriends' table is obsolete, update your config to use iaxusers and iaxpeers, though they can point to the same table.\n");
1158 append_mapping("iaxusers", driver, database, table ? table : "iaxfriends");
1159 append_mapping("iaxpeers", driver, database, table ? table : "iaxfriends");
1160 } else
1161 append_mapping(v->name, driver, database, table);
1164 ast_config_destroy(config);
1165 return 0;
1168 int ast_config_engine_register(struct ast_config_engine *new)
1170 struct ast_config_engine *ptr;
1172 ast_mutex_lock(&config_lock);
1174 if (!config_engine_list) {
1175 config_engine_list = new;
1176 } else {
1177 for (ptr = config_engine_list; ptr->next; ptr=ptr->next);
1178 ptr->next = new;
1181 ast_mutex_unlock(&config_lock);
1182 ast_log(LOG_NOTICE,"Registered Config Engine %s\n", new->name);
1184 return 1;
1187 int ast_config_engine_deregister(struct ast_config_engine *del)
1189 struct ast_config_engine *ptr, *last=NULL;
1191 ast_mutex_lock(&config_lock);
1193 for (ptr = config_engine_list; ptr; ptr=ptr->next) {
1194 if (ptr == del) {
1195 if (last)
1196 last->next = ptr->next;
1197 else
1198 config_engine_list = ptr->next;
1199 break;
1201 last = ptr;
1204 ast_mutex_unlock(&config_lock);
1206 return 0;
1209 /*! \brief Find realtime engine for realtime family */
1210 static struct ast_config_engine *find_engine(const char *family, char *database, int dbsiz, char *table, int tabsiz)
1212 struct ast_config_engine *eng, *ret = NULL;
1213 struct ast_config_map *map;
1215 ast_mutex_lock(&config_lock);
1217 for (map = config_maps; map; map = map->next) {
1218 if (!strcasecmp(family, map->name)) {
1219 if (database)
1220 ast_copy_string(database, map->database, dbsiz);
1221 if (table)
1222 ast_copy_string(table, map->table ? map->table : family, tabsiz);
1223 break;
1227 /* Check if the required driver (engine) exist */
1228 if (map) {
1229 for (eng = config_engine_list; !ret && eng; eng = eng->next) {
1230 if (!strcasecmp(eng->name, map->driver))
1231 ret = eng;
1235 ast_mutex_unlock(&config_lock);
1237 /* if we found a mapping, but the engine is not available, then issue a warning */
1238 if (map && !ret)
1239 ast_log(LOG_WARNING, "Realtime mapping for '%s' found to engine '%s', but the engine is not available\n", map->name, map->driver);
1241 return ret;
1244 static struct ast_config_engine text_file_engine = {
1245 .name = "text",
1246 .load_func = config_text_file_load,
1249 struct ast_config *ast_config_internal_load(const char *filename, struct ast_config *cfg, int withcomments)
1251 char db[256];
1252 char table[256];
1253 struct ast_config_engine *loader = &text_file_engine;
1254 struct ast_config *result;
1256 if (cfg->include_level == cfg->max_include_level) {
1257 ast_log(LOG_WARNING, "Maximum Include level (%d) exceeded\n", cfg->max_include_level);
1258 return NULL;
1261 cfg->include_level++;
1263 if (strcmp(filename, extconfig_conf) && strcmp(filename, "asterisk.conf") && config_engine_list) {
1264 struct ast_config_engine *eng;
1266 eng = find_engine(filename, db, sizeof(db), table, sizeof(table));
1269 if (eng && eng->load_func) {
1270 loader = eng;
1271 } else {
1272 eng = find_engine("global", db, sizeof(db), table, sizeof(table));
1273 if (eng && eng->load_func)
1274 loader = eng;
1278 result = loader->load_func(db, table, filename, cfg, withcomments);
1280 if (result)
1281 result->include_level--;
1283 return result;
1286 struct ast_config *ast_config_load(const char *filename)
1288 struct ast_config *cfg;
1289 struct ast_config *result;
1291 cfg = ast_config_new();
1292 if (!cfg)
1293 return NULL;
1295 result = ast_config_internal_load(filename, cfg, 0);
1296 if (!result)
1297 ast_config_destroy(cfg);
1299 return result;
1302 struct ast_config *ast_config_load_with_comments(const char *filename)
1304 struct ast_config *cfg;
1305 struct ast_config *result;
1307 cfg = ast_config_new();
1308 if (!cfg)
1309 return NULL;
1311 result = ast_config_internal_load(filename, cfg, 1);
1312 if (!result)
1313 ast_config_destroy(cfg);
1315 return result;
1318 struct ast_variable *ast_load_realtime(const char *family, ...)
1320 struct ast_config_engine *eng;
1321 char db[256]="";
1322 char table[256]="";
1323 struct ast_variable *res=NULL;
1324 va_list ap;
1326 va_start(ap, family);
1327 eng = find_engine(family, db, sizeof(db), table, sizeof(table));
1328 if (eng && eng->realtime_func)
1329 res = eng->realtime_func(db, table, ap);
1330 va_end(ap);
1332 return res;
1335 /*! \brief Check if realtime engine is configured for family */
1336 int ast_check_realtime(const char *family)
1338 struct ast_config_engine *eng;
1340 eng = find_engine(family, NULL, 0, NULL, 0);
1341 if (eng)
1342 return 1;
1343 return 0;
1347 struct ast_config *ast_load_realtime_multientry(const char *family, ...)
1349 struct ast_config_engine *eng;
1350 char db[256]="";
1351 char table[256]="";
1352 struct ast_config *res=NULL;
1353 va_list ap;
1355 va_start(ap, family);
1356 eng = find_engine(family, db, sizeof(db), table, sizeof(table));
1357 if (eng && eng->realtime_multi_func)
1358 res = eng->realtime_multi_func(db, table, ap);
1359 va_end(ap);
1361 return res;
1364 int ast_update_realtime(const char *family, const char *keyfield, const char *lookup, ...)
1366 struct ast_config_engine *eng;
1367 int res = -1;
1368 char db[256]="";
1369 char table[256]="";
1370 va_list ap;
1372 va_start(ap, lookup);
1373 eng = find_engine(family, db, sizeof(db), table, sizeof(table));
1374 if (eng && eng->update_func)
1375 res = eng->update_func(db, table, keyfield, lookup, ap);
1376 va_end(ap);
1378 return res;
1381 static int config_command(int fd, int argc, char **argv)
1383 struct ast_config_engine *eng;
1384 struct ast_config_map *map;
1386 ast_mutex_lock(&config_lock);
1388 ast_cli(fd, "\n\n");
1389 for (eng = config_engine_list; eng; eng = eng->next) {
1390 ast_cli(fd, "\nConfig Engine: %s\n", eng->name);
1391 for (map = config_maps; map; map = map->next)
1392 if (!strcasecmp(map->driver, eng->name)) {
1393 ast_cli(fd, "===> %s (db=%s, table=%s)\n", map->name, map->database,
1394 map->table ? map->table : map->name);
1397 ast_cli(fd,"\n\n");
1399 ast_mutex_unlock(&config_lock);
1401 return 0;
1404 static char show_config_help[] =
1405 "Usage: core show config mappings\n"
1406 " Shows the filenames to config engines.\n";
1408 static struct ast_cli_entry cli_show_config_mappings_deprecated = {
1409 { "show", "config", "mappings", NULL },
1410 config_command, NULL,
1411 NULL };
1413 static struct ast_cli_entry cli_config[] = {
1414 { { "core", "show", "config", "mappings", NULL },
1415 config_command, "Display config mappings (file names to config engines)",
1416 show_config_help, NULL, &cli_show_config_mappings_deprecated },
1419 int register_config_cli()
1421 ast_cli_register_multiple(cli_config, sizeof(cli_config) / sizeof(struct ast_cli_entry));
1422 return 0;