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.
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
31 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
40 #define AST_INCLUDE_GLOB 1
41 #ifdef AST_INCLUDE_GLOB
42 #if defined(__Darwin__) || defined(__CYGWIN__)
43 #define GLOB_ABORTED GLOB_ABEND
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;
74 struct ast_comment *next;
80 static void CB_INIT(void)
82 if (!comment_buffer) {
83 comment_buffer = ast_malloc(CB_INCR);
86 comment_buffer[0] = 0;
87 comment_buffer_size = CB_INCR;
88 lline_buffer = ast_malloc(CB_INCR);
90 lline_buffer_size = CB_INCR;
92 comment_buffer[0] = 0;
97 static void CB_ADD(char *str)
99 int rem = comment_buffer_size - strlen(comment_buffer) - 1;
100 int siz = strlen(str);
102 comment_buffer = ast_realloc(comment_buffer, comment_buffer_size + CB_INCR + siz + 1);
105 comment_buffer_size += CB_INCR+siz+1;
107 strcat(comment_buffer,str);
110 static void CB_ADD_LEN(char *str, int len)
112 int cbl = strlen(comment_buffer) + 1;
113 int rem = comment_buffer_size - cbl;
115 comment_buffer = ast_realloc(comment_buffer, comment_buffer_size + CB_INCR + len + 1);
118 comment_buffer_size += CB_INCR+len+1;
120 strncat(comment_buffer,str,len);
121 comment_buffer[cbl+len-1] = 0;
124 static void LLB_ADD(char *str)
126 int rem = lline_buffer_size - strlen(lline_buffer) - 1;
127 int siz = strlen(str);
129 lline_buffer = ast_realloc(lline_buffer, lline_buffer_size + CB_INCR + siz + 1);
132 lline_buffer_size += CB_INCR + siz + 1;
134 strcat(lline_buffer,str);
137 static void CB_RESET(void )
139 comment_buffer[0] = 0;
145 static struct ast_comment *ALLOC_COMMENT(const char *buffer)
147 struct ast_comment *x = ast_calloc(1,sizeof(struct ast_comment)+strlen(buffer)+1);
148 strcpy(x->cmt, buffer);
153 static struct ast_config_map {
154 struct ast_config_map *next;
160 } *config_maps = NULL;
162 AST_MUTEX_DEFINE_STATIC(config_lock);
163 static struct ast_config_engine *config_engine_list;
165 #define MAX_INCLUDE_LEVEL 10
167 struct ast_category {
169 int ignored; /*!< do not let user of the config see this category */
170 struct ast_comment *precomments;
171 struct ast_comment *sameline;
172 struct ast_variable *root;
173 struct ast_variable *last;
174 struct ast_category *next;
178 struct ast_category *root;
179 struct ast_category *last;
180 struct ast_category *current;
181 struct ast_category *last_browse; /*!< used to cache the last category supplied via category_browse */
183 int max_include_level;
186 struct ast_variable *ast_variable_new(const char *name, const char *value)
188 struct ast_variable *variable;
189 int name_len = strlen(name) + 1;
191 if ((variable = ast_calloc(1, name_len + strlen(value) + 1 + sizeof(*variable)))) {
192 variable->name = variable->stuff;
193 variable->value = variable->stuff + name_len;
194 strcpy(variable->name,name);
195 strcpy(variable->value,value);
201 void ast_variable_append(struct ast_category *category, struct ast_variable *variable)
206 category->last->next = variable;
208 category->root = variable;
209 category->last = variable;
210 while (category->last->next)
211 category->last = category->last->next;
214 void ast_variables_destroy(struct ast_variable *v)
216 struct ast_variable *vn;
225 struct ast_variable *ast_variable_browse(const struct ast_config *config, const char *category)
227 struct ast_category *cat = NULL;
229 if (category && config->last_browse && (config->last_browse->name == category))
230 cat = config->last_browse;
232 cat = ast_category_get(config, category);
234 return (cat) ? cat->root : NULL;
237 const char *ast_config_option(struct ast_config *cfg, const char *cat, const char *var)
240 tmp = ast_variable_retrieve(cfg, cat, var);
242 tmp = ast_variable_retrieve(cfg, "general", var);
247 const char *ast_variable_retrieve(const struct ast_config *config, const char *category, const char *variable)
249 struct ast_variable *v;
252 for (v = ast_variable_browse(config, category); v; v = v->next) {
253 if (!strcasecmp(variable, v->name))
257 struct ast_category *cat;
259 for (cat = config->root; cat; cat = cat->next)
260 for (v = cat->root; v; v = v->next)
261 if (!strcasecmp(variable, v->name))
268 static struct ast_variable *variable_clone(const struct ast_variable *old)
270 struct ast_variable *new = ast_variable_new(old->name, old->value);
273 new->lineno = old->lineno;
274 new->object = old->object;
275 new->blanklines = old->blanklines;
276 /* TODO: clone comments? */
282 static void move_variables(struct ast_category *old, struct ast_category *new)
284 struct ast_variable *var = old->root;
287 /* we can just move the entire list in a single op */
288 ast_variable_append(new, var);
291 struct ast_variable *next = var->next;
293 ast_variable_append(new, var);
299 struct ast_category *ast_category_new(const char *name)
301 struct ast_category *category;
303 if ((category = ast_calloc(1, sizeof(*category))))
304 ast_copy_string(category->name, name, sizeof(category->name));
308 static struct ast_category *category_get(const struct ast_config *config, const char *category_name, int ignored)
310 struct ast_category *cat;
312 /* try exact match first, then case-insensitive match */
313 for (cat = config->root; cat; cat = cat->next) {
314 if (cat->name == category_name && (ignored || !cat->ignored))
318 for (cat = config->root; cat; cat = cat->next) {
319 if (!strcasecmp(cat->name, category_name) && (ignored || !cat->ignored))
326 struct ast_category *ast_category_get(const struct ast_config *config, const char *category_name)
328 return category_get(config, category_name, 0);
331 int ast_category_exist(const struct ast_config *config, const char *category_name)
333 return !!ast_category_get(config, category_name);
336 void ast_category_append(struct ast_config *config, struct ast_category *category)
339 config->last->next = category;
341 config->root = category;
342 config->last = category;
343 config->current = category;
346 void ast_category_destroy(struct ast_category *cat)
348 ast_variables_destroy(cat->root);
352 static struct ast_category *next_available_category(struct ast_category *cat)
354 for (; cat && cat->ignored; cat = cat->next);
359 char *ast_category_browse(struct ast_config *config, const char *prev)
361 struct ast_category *cat = NULL;
363 if (prev && config->last_browse && (config->last_browse->name == prev))
364 cat = config->last_browse->next;
365 else if (!prev && config->root)
368 for (cat = config->root; cat; cat = cat->next) {
369 if (cat->name == prev) {
375 for (cat = config->root; cat; cat = cat->next) {
376 if (!strcasecmp(cat->name, prev)) {
385 cat = next_available_category(cat);
387 config->last_browse = cat;
388 return (cat) ? cat->name : NULL;
391 struct ast_variable *ast_category_detach_variables(struct ast_category *cat)
393 struct ast_variable *v;
402 void ast_category_rename(struct ast_category *cat, const char *name)
404 ast_copy_string(cat->name, name, sizeof(cat->name));
407 static void inherit_category(struct ast_category *new, const struct ast_category *base)
409 struct ast_variable *var;
411 for (var = base->root; var; var = var->next)
412 ast_variable_append(new, variable_clone(var));
415 struct ast_config *ast_config_new(void)
417 struct ast_config *config;
419 if ((config = ast_calloc(1, sizeof(*config))))
420 config->max_include_level = MAX_INCLUDE_LEVEL;
424 int ast_variable_delete(struct ast_category *category, char *variable, char *match)
426 struct ast_variable *cur, *prev=NULL, *curn;
428 cur = category->root;
430 if (cur->name == variable) {
432 prev->next = cur->next;
433 if (cur == category->last)
434 category->last = prev;
436 category->root = cur->next;
437 if (cur == category->last)
438 category->last = NULL;
441 ast_variables_destroy(cur);
449 cur = category->root;
452 if (!strcasecmp(cur->name, variable) && (ast_strlen_zero(match) || !strcasecmp(cur->value, match))) {
454 prev->next = cur->next;
455 if (cur == category->last)
456 category->last = prev;
458 category->root = cur->next;
459 if (cur == category->last)
460 category->last = NULL;
463 ast_variables_destroy(cur);
473 int ast_variable_update(struct ast_category *category, char *variable, char *value, char *match)
475 struct ast_variable *cur, *prev=NULL, *newer;
476 newer = ast_variable_new(variable, value);
479 cur = category->root;
481 if (cur->name == variable) {
482 newer->next = cur->next;
483 newer->object = cur->object;
487 category->root = newer;
488 if (category->last == cur)
489 category->last = newer;
491 ast_variables_destroy(cur);
499 cur = category->root;
501 if (!strcasecmp(cur->name, variable) && (ast_strlen_zero(match) || !strcasecmp(cur->value, match))) {
502 newer->next = cur->next;
503 newer->object = cur->object;
507 category->root = newer;
508 if (category->last == cur)
509 category->last = newer;
511 ast_variables_destroy(cur);
520 category->root = newer;
524 int ast_category_delete(struct ast_config *cfg, char *category)
526 struct ast_category *prev=NULL, *cat;
529 if (cat->name == category) {
530 ast_variables_destroy(cat->root);
532 prev->next = cat->next;
533 if (cat == cfg->last)
536 cfg->root = cat->next;
537 if (cat == cfg->last)
550 if (!strcasecmp(cat->name, category)) {
551 ast_variables_destroy(cat->root);
553 prev->next = cat->next;
554 if (cat == cfg->last)
557 cfg->root = cat->next;
558 if (cat == cfg->last)
570 void ast_config_destroy(struct ast_config *cfg)
572 struct ast_category *cat, *catn;
579 ast_variables_destroy(cat->root);
587 struct ast_category *ast_config_get_current_category(const struct ast_config *cfg)
592 void ast_config_set_current_category(struct ast_config *cfg, const struct ast_category *cat)
594 /* cast below is just to silence compiler warning about dropping "const" */
595 cfg->current = (struct ast_category *) cat;
598 static int process_text_line(struct ast_config *cfg, struct ast_category **cat, char *buf, int lineno, const char *configfile, int withcomments)
602 struct ast_variable *v;
603 char cmd[512], exec_file[512];
604 int object, do_exec, do_include;
606 /* Actually parse the entry */
608 struct ast_category *newcat = NULL;
611 /* A category header */
612 c = strchr(cur, ']');
614 ast_log(LOG_WARNING, "parse error: no closing ']', line %d of %s\n", lineno, configfile);
622 if (!(*cat = newcat = ast_category_new(catname))) {
626 if (withcomments && comment_buffer && comment_buffer[0] ) {
627 newcat->precomments = ALLOC_COMMENT(comment_buffer);
629 if (withcomments && lline_buffer && lline_buffer[0] ) {
630 newcat->sameline = ALLOC_COMMENT(lline_buffer);
635 /* If there are options or categories to inherit from, process them now */
637 if (!(cur = strchr(c, ')'))) {
638 ast_log(LOG_WARNING, "parse error: no closing ')', line %d of %s\n", lineno, configfile);
642 while ((cur = strsep(&c, ","))) {
643 if (!strcasecmp(cur, "!")) {
645 } else if (!strcasecmp(cur, "+")) {
646 *cat = category_get(cfg, catname, 1);
648 ast_config_destroy(cfg);
650 ast_category_destroy(newcat);
651 ast_log(LOG_WARNING, "Category addition requested, but category '%s' does not exist, line %d of %s\n", catname, lineno, configfile);
655 move_variables(newcat, *cat);
656 ast_category_destroy(newcat);
660 struct ast_category *base;
662 base = category_get(cfg, cur, 1);
664 ast_log(LOG_WARNING, "Inheritance requested, but category '%s' does not exist, line %d of %s\n", cur, lineno, configfile);
667 inherit_category(*cat, base);
672 ast_category_append(cfg, *cat);
673 } else if (cur[0] == '#') {
677 while(*c && (*c > 32)) c++;
680 /* Find real argument */
681 c = ast_skip_blanks(c + 1);
686 do_include = !strcasecmp(cur, "include");
688 do_exec = !strcasecmp(cur, "exec");
691 if (do_exec && !ast_opt_exec_includes) {
692 ast_log(LOG_WARNING, "Cannot perform #exec unless execincludes option is enabled in asterisk.conf (options section)!\n");
695 if (do_include || do_exec) {
697 /* Strip off leading and trailing "'s and <>'s */
698 while((*c == '<') || (*c == '>') || (*c == '\"')) c++;
699 /* Get rid of leading mess */
701 while (!ast_strlen_zero(cur)) {
702 c = cur + strlen(cur) - 1;
703 if ((*c == '>') || (*c == '<') || (*c == '\"'))
708 /* #exec </path/to/executable>
709 We create a tmp file, then we #include it, then we delete it. */
711 snprintf(exec_file, sizeof(exec_file), "/var/tmp/exec.%d.%ld", (int)time(NULL), (long)pthread_self());
712 snprintf(cmd, sizeof(cmd), "%s > %s 2>&1", cur, exec_file);
713 ast_safe_system(cmd);
718 do_include = ast_config_internal_load(cur, cfg, withcomments) ? 1 : 0;
719 if(!ast_strlen_zero(exec_file))
725 ast_log(LOG_WARNING, "Directive '#%s' needs an argument (%s) at line %d of %s\n",
726 do_exec ? "exec" : "include",
727 do_exec ? "/path/to/executable" : "filename",
733 ast_log(LOG_WARNING, "Unknown directive '%s' at line %d of %s\n", cur, lineno, configfile);
735 /* Just a line (variable = value) */
738 "parse error: No category context for line %d of %s\n", lineno, configfile);
741 c = strchr(cur, '=');
751 if ((v = ast_variable_new(ast_strip(cur), ast_strip(c)))) {
754 /* Put and reset comments */
756 ast_variable_append(*cat, v);
758 if (withcomments && comment_buffer && comment_buffer[0] ) {
759 v->precomments = ALLOC_COMMENT(comment_buffer);
761 if (withcomments && lline_buffer && lline_buffer[0] ) {
762 v->sameline = ALLOC_COMMENT(lline_buffer);
771 ast_log(LOG_WARNING, "No '=' (equal sign) in line %d of %s\n", lineno, configfile);
777 static struct ast_config *config_text_file_load(const char *database, const char *table, const char *filename, struct ast_config *cfg, int withcomments)
781 char *new_buf, *comment_p, *process_buf;
784 int comment = 0, nest[MAX_NESTED_COMMENTS];
785 struct ast_category *cat = NULL;
789 cat = ast_config_get_current_category(cfg);
791 if (filename[0] == '/') {
792 ast_copy_string(fn, filename, sizeof(fn));
794 snprintf(fn, sizeof(fn), "%s/%s", (char *)ast_config_AST_CONFIG_DIR, filename);
801 #ifdef AST_INCLUDE_GLOB
805 globbuf.gl_offs = 0; /* initialize it to silence gcc */
807 glob_ret = glob(fn, GLOB_NOCHECK, NULL, &globbuf);
809 glob_ret = glob(fn, GLOB_NOMAGIC|GLOB_BRACE, NULL, &globbuf);
811 if (glob_ret == GLOB_NOSPACE)
813 "Glob Expansion of pattern '%s' failed: Not enough memory\n", fn);
814 else if (glob_ret == GLOB_ABORTED)
816 "Glob Expansion of pattern '%s' failed: Read error\n", fn);
818 /* loop over expanded files */
820 for (i=0; i<globbuf.gl_pathc; i++) {
821 ast_copy_string(fn, globbuf.gl_pathv[i], sizeof(fn));
824 if (stat(fn, &statbuf))
827 if (!S_ISREG(statbuf.st_mode)) {
828 ast_log(LOG_WARNING, "'%s' is not a regular file, ignoring\n", fn);
831 if (option_verbose > 1) {
832 ast_verbose(VERBOSE_PREFIX_2 "Parsing '%s': ", fn);
835 if (!(f = fopen(fn, "r"))) {
837 ast_log(LOG_DEBUG, "No file to parse: %s\n", fn);
838 if (option_verbose > 1)
839 ast_verbose( "Not found (%s)\n", strerror(errno));
844 ast_log(LOG_DEBUG, "Parsing %s\n", fn);
845 if (option_verbose > 1)
846 ast_verbose("Found\n");
849 if (fgets(buf, sizeof(buf), f)) {
850 if ( withcomments ) {
851 CB_ADD(lline_buffer); /* add the current lline buffer to the comment buffer */
852 lline_buffer[0] = 0; /* erase the lline buffer */
861 while ((comment_p = strchr(new_buf, COMMENT_META))) {
862 if ((comment_p > new_buf) && (*(comment_p-1) == '\\')) {
863 /* Yuck, gotta memmove */
864 memmove(comment_p - 1, comment_p, strlen(comment_p) + 1);
866 } else if(comment_p[1] == COMMENT_TAG && comment_p[2] == COMMENT_TAG && (comment_p[3] != '-')) {
867 /* Meta-Comment start detected ";--" */
868 if (comment < MAX_NESTED_COMMENTS) {
870 new_buf = comment_p + 3;
872 nest[comment-1] = lineno;
874 ast_log(LOG_ERROR, "Maximum nest limit of %d reached.\n", MAX_NESTED_COMMENTS);
876 } else if ((comment_p >= new_buf + 2) &&
877 (*(comment_p - 1) == COMMENT_TAG) &&
878 (*(comment_p - 2) == COMMENT_TAG)) {
879 /* Meta-Comment end detected */
881 new_buf = comment_p + 1;
883 /* Back to non-comment now */
885 /* Actually have to move what's left over the top, then continue */
887 oldptr = process_buf + strlen(process_buf);
888 if ( withcomments ) {
890 CB_ADD_LEN(oldptr+1,new_buf-oldptr-1);
893 memmove(oldptr, new_buf, strlen(new_buf) + 1);
896 process_buf = new_buf;
900 /* If ; is found, and we are not nested in a comment,
901 we immediately stop all comment processing */
902 if ( withcomments ) {
908 new_buf = comment_p + 1;
911 if( withcomments && comment && !process_buf )
913 CB_ADD(buf); /* the whole line is a comment, store it */
917 char *buf = ast_strip(process_buf);
918 if (!ast_strlen_zero(buf)) {
919 if (process_text_line(cfg, &cat, buf, lineno, filename, withcomments)) {
930 ast_log(LOG_WARNING,"Unterminated comment detected beginning on line %d\n", nest[comment]);
932 #ifdef AST_INCLUDE_GLOB
941 if (comment_buffer) {
942 free(comment_buffer);
946 comment_buffer_size=0;
956 int config_text_file_save(const char *configfile, const struct ast_config *cfg, const char *generator)
962 struct ast_variable *var;
963 struct ast_category *cat;
964 struct ast_comment *cmt;
967 if (configfile[0] == '/') {
968 ast_copy_string(fn, configfile, sizeof(fn));
970 snprintf(fn, sizeof(fn), "%s/%s", ast_config_AST_CONFIG_DIR, configfile);
973 ast_copy_string(date, ctime(&t), sizeof(date));
975 if ((f = fopen(fn, "w+"))) {
977 if ((f = fopen(fn, "w"))) {
979 if (option_verbose > 1)
980 ast_verbose(VERBOSE_PREFIX_2 "Saving '%s': ", fn);
982 fprintf(f, ";! Automatically generated configuration file\n");
983 if (strcmp(configfile, fn))
984 fprintf(f, ";! Filename: %s (%s)\n", configfile, fn);
986 fprintf(f, ";! Filename: %s\n", configfile);
987 fprintf(f, ";! Generator: %s\n", generator);
988 fprintf(f, ";! Creation Date: %s", date);
992 /* Dump section with any appropriate comment */
993 for (cmt = cat->precomments; cmt; cmt=cmt->next)
995 if (cmt->cmt[0] != ';' || cmt->cmt[1] != '!')
996 fprintf(f,"%s", cmt->cmt);
998 if (!cat->precomments)
1000 fprintf(f, "[%s]", cat->name);
1001 for(cmt = cat->sameline; cmt; cmt=cmt->next)
1003 fprintf(f,"%s", cmt->cmt);
1009 for (cmt = var->precomments; cmt; cmt=cmt->next)
1011 if (cmt->cmt[0] != ';' || cmt->cmt[1] != '!')
1012 fprintf(f,"%s", cmt->cmt);
1015 fprintf(f, "%s %s %s %s", var->name, (var->object ? "=>" : "="), var->value, var->sameline->cmt);
1017 fprintf(f, "%s %s %s\n", var->name, (var->object ? "=>" : "="), var->value);
1018 if (var->blanklines) {
1019 blanklines = var->blanklines;
1020 while (blanklines--)
1027 /* Put an empty line */
1032 if ((option_verbose > 1) && !option_debug)
1033 ast_verbose("Saved\n");
1036 ast_log(LOG_DEBUG, "Unable to open for writing: %s\n", fn);
1037 if (option_verbose > 1)
1038 ast_verbose(VERBOSE_PREFIX_2 "Unable to write (%s)", strerror(errno));
1045 static void clear_config_maps(void)
1047 struct ast_config_map *map;
1049 ast_mutex_lock(&config_lock);
1051 while (config_maps) {
1053 config_maps = config_maps->next;
1057 ast_mutex_unlock(&config_lock);
1060 static int append_mapping(char *name, char *driver, char *database, char *table)
1062 struct ast_config_map *map;
1065 length = sizeof(*map);
1066 length += strlen(name) + 1;
1067 length += strlen(driver) + 1;
1068 length += strlen(database) + 1;
1070 length += strlen(table) + 1;
1072 if (!(map = ast_calloc(1, length)))
1075 map->name = map->stuff;
1076 strcpy(map->name, name);
1077 map->driver = map->name + strlen(map->name) + 1;
1078 strcpy(map->driver, driver);
1079 map->database = map->driver + strlen(map->driver) + 1;
1080 strcpy(map->database, database);
1082 map->table = map->database + strlen(map->database) + 1;
1083 strcpy(map->table, table);
1085 map->next = config_maps;
1087 if (option_verbose > 1)
1088 ast_verbose(VERBOSE_PREFIX_2 "Binding %s to %s/%s/%s\n",
1089 map->name, map->driver, map->database, map->table ? map->table : map->name);
1095 int read_config_maps(void)
1097 struct ast_config *config, *configtmp;
1098 struct ast_variable *v;
1099 char *driver, *table, *database, *stringp;
1101 clear_config_maps();
1103 configtmp = ast_config_new();
1104 configtmp->max_include_level = 1;
1105 config = ast_config_internal_load(extconfig_conf, configtmp, 0);
1107 ast_config_destroy(configtmp);
1111 for (v = ast_variable_browse(config, "settings"); v; v = v->next) {
1113 driver = strsep(&stringp, ",");
1115 /* check if the database text starts with a double quote */
1116 if (*stringp == '"') {
1118 database = strsep(&stringp, "\"");
1119 strsep(&stringp, ",");
1121 /* apparently this text has no quotes */
1122 database = strsep(&stringp, ",");
1125 table = strsep(&stringp, ",");
1127 if (!strcmp(v->name, extconfig_conf)) {
1128 ast_log(LOG_WARNING, "Cannot bind '%s'!\n", extconfig_conf);
1132 if (!strcmp(v->name, "asterisk.conf")) {
1133 ast_log(LOG_WARNING, "Cannot bind 'asterisk.conf'!\n");
1137 if (!strcmp(v->name, "logger.conf")) {
1138 ast_log(LOG_WARNING, "Cannot bind 'logger.conf'!\n");
1142 if (!driver || !database)
1144 if (!strcasecmp(v->name, "sipfriends")) {
1145 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");
1146 append_mapping("sipusers", driver, database, table ? table : "sipfriends");
1147 append_mapping("sippeers", driver, database, table ? table : "sipfriends");
1148 } else if (!strcasecmp(v->name, "iaxfriends")) {
1149 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");
1150 append_mapping("iaxusers", driver, database, table ? table : "iaxfriends");
1151 append_mapping("iaxpeers", driver, database, table ? table : "iaxfriends");
1153 append_mapping(v->name, driver, database, table);
1156 ast_config_destroy(config);
1160 int ast_config_engine_register(struct ast_config_engine *new)
1162 struct ast_config_engine *ptr;
1164 ast_mutex_lock(&config_lock);
1166 if (!config_engine_list) {
1167 config_engine_list = new;
1169 for (ptr = config_engine_list; ptr->next; ptr=ptr->next);
1173 ast_mutex_unlock(&config_lock);
1174 ast_log(LOG_NOTICE,"Registered Config Engine %s\n", new->name);
1179 int ast_config_engine_deregister(struct ast_config_engine *del)
1181 struct ast_config_engine *ptr, *last=NULL;
1183 ast_mutex_lock(&config_lock);
1185 for (ptr = config_engine_list; ptr; ptr=ptr->next) {
1188 last->next = ptr->next;
1190 config_engine_list = ptr->next;
1196 ast_mutex_unlock(&config_lock);
1201 /*! \brief Find realtime engine for realtime family */
1202 static struct ast_config_engine *find_engine(const char *family, char *database, int dbsiz, char *table, int tabsiz)
1204 struct ast_config_engine *eng, *ret = NULL;
1205 struct ast_config_map *map;
1207 ast_mutex_lock(&config_lock);
1209 for (map = config_maps; map; map = map->next) {
1210 if (!strcasecmp(family, map->name)) {
1212 ast_copy_string(database, map->database, dbsiz);
1214 ast_copy_string(table, map->table ? map->table : family, tabsiz);
1219 /* Check if the required driver (engine) exist */
1221 for (eng = config_engine_list; !ret && eng; eng = eng->next) {
1222 if (!strcasecmp(eng->name, map->driver))
1227 ast_mutex_unlock(&config_lock);
1229 /* if we found a mapping, but the engine is not available, then issue a warning */
1231 ast_log(LOG_WARNING, "Realtime mapping for '%s' found to engine '%s', but the engine is not available\n", map->name, map->driver);
1236 static struct ast_config_engine text_file_engine = {
1238 .load_func = config_text_file_load,
1241 struct ast_config *ast_config_internal_load(const char *filename, struct ast_config *cfg, int withcomments)
1245 struct ast_config_engine *loader = &text_file_engine;
1246 struct ast_config *result;
1248 if (cfg->include_level == cfg->max_include_level) {
1249 ast_log(LOG_WARNING, "Maximum Include level (%d) exceeded\n", cfg->max_include_level);
1253 cfg->include_level++;
1255 if (strcmp(filename, extconfig_conf) && strcmp(filename, "asterisk.conf") && config_engine_list) {
1256 struct ast_config_engine *eng;
1258 eng = find_engine(filename, db, sizeof(db), table, sizeof(table));
1261 if (eng && eng->load_func) {
1264 eng = find_engine("global", db, sizeof(db), table, sizeof(table));
1265 if (eng && eng->load_func)
1270 result = loader->load_func(db, table, filename, cfg, withcomments);
1273 result->include_level--;
1278 struct ast_config *ast_config_load(const char *filename)
1280 struct ast_config *cfg;
1281 struct ast_config *result;
1283 cfg = ast_config_new();
1287 result = ast_config_internal_load(filename, cfg, 0);
1289 ast_config_destroy(cfg);
1294 struct ast_config *ast_config_load_with_comments(const char *filename)
1296 struct ast_config *cfg;
1297 struct ast_config *result;
1299 cfg = ast_config_new();
1303 result = ast_config_internal_load(filename, cfg, 1);
1305 ast_config_destroy(cfg);
1310 struct ast_variable *ast_load_realtime(const char *family, ...)
1312 struct ast_config_engine *eng;
1315 struct ast_variable *res=NULL;
1318 va_start(ap, family);
1319 eng = find_engine(family, db, sizeof(db), table, sizeof(table));
1320 if (eng && eng->realtime_func)
1321 res = eng->realtime_func(db, table, ap);
1327 /*! \brief Check if realtime engine is configured for family */
1328 int ast_check_realtime(const char *family)
1330 struct ast_config_engine *eng;
1332 eng = find_engine(family, NULL, 0, NULL, 0);
1339 struct ast_config *ast_load_realtime_multientry(const char *family, ...)
1341 struct ast_config_engine *eng;
1344 struct ast_config *res=NULL;
1347 va_start(ap, family);
1348 eng = find_engine(family, db, sizeof(db), table, sizeof(table));
1349 if (eng && eng->realtime_multi_func)
1350 res = eng->realtime_multi_func(db, table, ap);
1356 int ast_update_realtime(const char *family, const char *keyfield, const char *lookup, ...)
1358 struct ast_config_engine *eng;
1364 va_start(ap, lookup);
1365 eng = find_engine(family, db, sizeof(db), table, sizeof(table));
1366 if (eng && eng->update_func)
1367 res = eng->update_func(db, table, keyfield, lookup, ap);
1373 static int config_command(int fd, int argc, char **argv)
1375 struct ast_config_engine *eng;
1376 struct ast_config_map *map;
1378 ast_mutex_lock(&config_lock);
1380 ast_cli(fd, "\n\n");
1381 for (eng = config_engine_list; eng; eng = eng->next) {
1382 ast_cli(fd, "\nConfig Engine: %s\n", eng->name);
1383 for (map = config_maps; map; map = map->next)
1384 if (!strcasecmp(map->driver, eng->name)) {
1385 ast_cli(fd, "===> %s (db=%s, table=%s)\n", map->name, map->database,
1386 map->table ? map->table : map->name);
1391 ast_mutex_unlock(&config_lock);
1396 static char show_config_help[] =
1397 "Usage: core show config mappings\n"
1398 " Shows the filenames to config engines.\n";
1400 static struct ast_cli_entry cli_config[] = {
1401 { { "core", "show", "config", "mappings", NULL },
1402 config_command, "Display config mappings (file names to config engines)",
1406 int register_config_cli()
1408 ast_cli_register_multiple(cli_config, sizeof(cli_config) / sizeof(struct ast_cli_entry));