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 static struct ast_config_map
{
66 struct ast_config_map
*next
;
72 } *config_maps
= NULL
;
74 AST_MUTEX_DEFINE_STATIC(config_lock
);
75 static struct ast_config_engine
*config_engine_list
;
77 #define MAX_INCLUDE_LEVEL 10
80 struct ast_comment
*next
;
86 int ignored
; /* do not let user of the config see this category */
87 struct ast_variable
*root
;
88 struct ast_variable
*last
;
89 struct ast_category
*next
;
93 struct ast_category
*root
;
94 struct ast_category
*last
;
95 struct ast_category
*current
;
96 struct ast_category
*last_browse
; /* used to cache the last category supplied via category_browse */
98 int max_include_level
;
101 struct ast_variable
*ast_variable_new(const char *name
, const char *value
)
103 struct ast_variable
*variable
;
104 int name_len
= strlen(name
) + 1;
106 if ((variable
= ast_calloc(1, name_len
+ strlen(value
) + 1 + sizeof(*variable
)))) {
107 variable
->name
= variable
->stuff
;
108 variable
->value
= variable
->stuff
+ name_len
;
109 strcpy(variable
->name
,name
);
110 strcpy(variable
->value
,value
);
116 void ast_variable_append(struct ast_category
*category
, struct ast_variable
*variable
)
121 category
->last
->next
= variable
;
123 category
->root
= variable
;
124 category
->last
= variable
;
127 void ast_variables_destroy(struct ast_variable
*v
)
129 struct ast_variable
*vn
;
138 struct ast_variable
*ast_variable_browse(const struct ast_config
*config
, const char *category
)
140 struct ast_category
*cat
= NULL
;
142 if (category
&& config
->last_browse
&& (config
->last_browse
->name
== category
))
143 cat
= config
->last_browse
;
145 cat
= ast_category_get(config
, category
);
147 return (cat
) ? cat
->root
: NULL
;
150 char *ast_variable_retrieve(const struct ast_config
*config
, const char *category
, const char *variable
)
152 struct ast_variable
*v
;
155 for (v
= ast_variable_browse(config
, category
); v
; v
= v
->next
) {
156 if (!strcasecmp(variable
, v
->name
))
160 struct ast_category
*cat
;
162 for (cat
= config
->root
; cat
; cat
= cat
->next
)
163 for (v
= cat
->root
; v
; v
= v
->next
)
164 if (!strcasecmp(variable
, v
->name
))
171 static struct ast_variable
*variable_clone(const struct ast_variable
*old
)
173 struct ast_variable
*new = ast_variable_new(old
->name
, old
->value
);
176 new->lineno
= old
->lineno
;
177 new->object
= old
->object
;
178 new->blanklines
= old
->blanklines
;
179 /* TODO: clone comments? */
185 static void move_variables(struct ast_category
*old
, struct ast_category
*new)
187 struct ast_variable
*var
= old
->root
;
190 /* we can just move the entire list in a single op */
191 ast_variable_append(new, var
);
194 struct ast_variable
*next
= var
->next
;
196 ast_variable_append(new, var
);
202 struct ast_category
*ast_category_new(const char *name
)
204 struct ast_category
*category
;
206 if ((category
= ast_calloc(1, sizeof(*category
))))
207 ast_copy_string(category
->name
, name
, sizeof(category
->name
));
211 static struct ast_category
*category_get(const struct ast_config
*config
, const char *category_name
, int ignored
)
213 struct ast_category
*cat
;
215 /* try exact match first, then case-insensitive match */
216 for (cat
= config
->root
; cat
; cat
= cat
->next
) {
217 if (cat
->name
== category_name
&& (ignored
|| !cat
->ignored
))
221 for (cat
= config
->root
; cat
; cat
= cat
->next
) {
222 if (!strcasecmp(cat
->name
, category_name
) && (ignored
|| !cat
->ignored
))
229 struct ast_category
*ast_category_get(const struct ast_config
*config
, const char *category_name
)
231 return category_get(config
, category_name
, 0);
234 int ast_category_exist(const struct ast_config
*config
, const char *category_name
)
236 return !!ast_category_get(config
, category_name
);
239 void ast_category_append(struct ast_config
*config
, struct ast_category
*category
)
242 config
->last
->next
= category
;
244 config
->root
= category
;
245 config
->last
= category
;
246 config
->current
= category
;
249 void ast_category_destroy(struct ast_category
*cat
)
251 ast_variables_destroy(cat
->root
);
255 static struct ast_category
*next_available_category(struct ast_category
*cat
)
257 for (; cat
&& cat
->ignored
; cat
= cat
->next
);
262 char *ast_category_browse(struct ast_config
*config
, const char *prev
)
264 struct ast_category
*cat
= NULL
;
266 if (prev
&& config
->last_browse
&& (config
->last_browse
->name
== prev
))
267 cat
= config
->last_browse
->next
;
268 else if (!prev
&& config
->root
)
271 for (cat
= config
->root
; cat
; cat
= cat
->next
) {
272 if (cat
->name
== prev
) {
278 for (cat
= config
->root
; cat
; cat
= cat
->next
) {
279 if (!strcasecmp(cat
->name
, prev
)) {
288 cat
= next_available_category(cat
);
290 config
->last_browse
= cat
;
291 return (cat
) ? cat
->name
: NULL
;
294 struct ast_variable
*ast_category_detach_variables(struct ast_category
*cat
)
296 struct ast_variable
*v
;
304 void ast_category_rename(struct ast_category
*cat
, const char *name
)
306 ast_copy_string(cat
->name
, name
, sizeof(cat
->name
));
309 static void inherit_category(struct ast_category
*new, const struct ast_category
*base
)
311 struct ast_variable
*var
;
313 for (var
= base
->root
; var
; var
= var
->next
)
314 ast_variable_append(new, variable_clone(var
));
317 struct ast_config
*ast_config_new(void)
319 struct ast_config
*config
;
321 if ((config
= ast_calloc(1, sizeof(*config
))))
322 config
->max_include_level
= MAX_INCLUDE_LEVEL
;
326 int ast_variable_delete(struct ast_category
*category
, char *variable
)
328 struct ast_variable
*cur
, *prev
=NULL
;
329 cur
= category
->root
;
331 if (cur
->name
== variable
) {
333 prev
->next
= cur
->next
;
334 if (cur
== category
->last
)
335 category
->last
= prev
;
337 category
->root
= cur
->next
;
338 if (cur
== category
->last
)
339 category
->last
= NULL
;
342 ast_variables_destroy(cur
);
350 cur
= category
->root
;
352 if (!strcasecmp(cur
->name
, variable
)) {
354 prev
->next
= cur
->next
;
355 if (cur
== category
->last
)
356 category
->last
= prev
;
358 category
->root
= cur
->next
;
359 if (cur
== category
->last
)
360 category
->last
= NULL
;
363 ast_variables_destroy(cur
);
372 int ast_variable_update(struct ast_category
*category
, char *variable
, char *value
)
374 struct ast_variable
*cur
, *prev
=NULL
, *newer
;
375 newer
= ast_variable_new(variable
, value
);
378 cur
= category
->root
;
380 if (cur
->name
== variable
) {
381 newer
->next
= cur
->next
;
385 category
->root
= newer
;
386 if (category
->last
== cur
)
387 category
->last
= newer
;
389 ast_variables_destroy(cur
);
397 cur
= category
->root
;
399 if (!strcasecmp(cur
->name
, variable
)) {
400 newer
->next
= cur
->next
;
404 category
->root
= newer
;
405 if (category
->last
== cur
)
406 category
->last
= newer
;
408 ast_variables_destroy(cur
);
417 category
->root
= newer
;
421 int ast_category_delete(struct ast_config
*cfg
, char *category
)
423 struct ast_category
*prev
=NULL
, *cat
;
426 if (cat
->name
== category
) {
427 ast_variables_destroy(cat
->root
);
429 prev
->next
= cat
->next
;
430 if (cat
== cfg
->last
)
433 cfg
->root
= cat
->next
;
434 if (cat
== cfg
->last
)
447 if (!strcasecmp(cat
->name
, category
)) {
448 ast_variables_destroy(cat
->root
);
450 prev
->next
= cat
->next
;
451 if (cat
== cfg
->last
)
454 cfg
->root
= cat
->next
;
455 if (cat
== cfg
->last
)
467 void ast_config_destroy(struct ast_config
*cfg
)
469 struct ast_category
*cat
, *catn
;
476 ast_variables_destroy(cat
->root
);
484 struct ast_category
*ast_config_get_current_category(const struct ast_config
*cfg
)
489 void ast_config_set_current_category(struct ast_config
*cfg
, const struct ast_category
*cat
)
491 /* cast below is just to silence compiler warning about dropping "const" */
492 cfg
->current
= (struct ast_category
*) cat
;
495 static int process_text_line(struct ast_config
*cfg
, struct ast_category
**cat
, char *buf
, int lineno
, const char *configfile
, int withcomments
)
499 struct ast_variable
*v
;
500 char cmd
[512], exec_file
[512];
501 int object
, do_exec
, do_include
;
503 /* Actually parse the entry */
505 struct ast_category
*newcat
= NULL
;
508 /* A category header */
509 c
= strchr(cur
, ']');
511 ast_log(LOG_WARNING
, "parse error: no closing ']', line %d of %s\n", lineno
, configfile
);
519 if (!(*cat
= newcat
= ast_category_new(catname
))) {
522 /* If there are options or categories to inherit from, process them now */
524 if (!(cur
= strchr(c
, ')'))) {
525 ast_log(LOG_WARNING
, "parse error: no closing ')', line %d of %s\n", lineno
, configfile
);
529 while ((cur
= strsep(&c
, ","))) {
530 if (!strcasecmp(cur
, "!")) {
532 } else if (!strcasecmp(cur
, "+")) {
533 *cat
= category_get(cfg
, catname
, 1);
535 ast_config_destroy(cfg
);
537 ast_category_destroy(newcat
);
538 ast_log(LOG_WARNING
, "Category addition requested, but category '%s' does not exist, line %d of %s\n", catname
, lineno
, configfile
);
542 move_variables(newcat
, *cat
);
543 ast_category_destroy(newcat
);
547 struct ast_category
*base
;
549 base
= category_get(cfg
, cur
, 1);
551 ast_log(LOG_WARNING
, "Inheritance requested, but category '%s' does not exist, line %d of %s\n", cur
, lineno
, configfile
);
554 inherit_category(*cat
, base
);
559 ast_category_append(cfg
, *cat
);
560 } else if (cur
[0] == '#') {
564 while(*c
&& (*c
> 32)) c
++;
567 /* Find real argument */
568 c
= ast_skip_blanks(c
+ 1);
573 do_include
= !strcasecmp(cur
, "include");
575 do_exec
= !strcasecmp(cur
, "exec");
578 if (do_exec
&& !ast_opt_exec_includes
) {
579 ast_log(LOG_WARNING
, "Cannot perform #exec unless execincludes option is enabled in asterisk.conf (options section)!\n");
582 if (do_include
|| do_exec
) {
584 /* Strip off leading and trailing "'s and <>'s */
585 while((*c
== '<') || (*c
== '>') || (*c
== '\"')) c
++;
586 /* Get rid of leading mess */
588 while (!ast_strlen_zero(cur
)) {
589 c
= cur
+ strlen(cur
) - 1;
590 if ((*c
== '>') || (*c
== '<') || (*c
== '\"'))
595 /* #exec </path/to/executable>
596 We create a tmp file, then we #include it, then we delete it. */
598 snprintf(exec_file
, sizeof(exec_file
), "/var/tmp/exec.%d.%ld", (int)time(NULL
), (long)pthread_self());
599 snprintf(cmd
, sizeof(cmd
), "%s > %s 2>&1", cur
, exec_file
);
600 ast_safe_system(cmd
);
605 do_include
= ast_config_internal_load(cur
, cfg
, withcomments
) ? 1 : 0;
606 if(!ast_strlen_zero(exec_file
))
612 ast_log(LOG_WARNING
, "Directive '#%s' needs an argument (%s) at line %d of %s\n",
613 do_exec
? "exec" : "include",
614 do_exec
? "/path/to/executable" : "filename",
620 ast_log(LOG_WARNING
, "Unknown directive '%s' at line %d of %s\n", cur
, lineno
, configfile
);
622 /* Just a line (variable = value) */
625 "parse error: No category context for line %d of %s\n", lineno
, configfile
);
628 c
= strchr(cur
, '=');
638 if ((v
= ast_variable_new(ast_strip(cur
), ast_strip(c
)))) {
641 /* Put and reset comments */
643 ast_variable_append(*cat
, v
);
648 ast_log(LOG_WARNING
, "No '=' (equal sign) in line %d of %s\n", lineno
, configfile
);
655 static struct ast_config
*config_text_file_load(const char *database
, const char *table
, const char *filename
, struct ast_config
*cfg
, int withcomments
)
659 char *new_buf
, *comment_p
, *process_buf
;
662 int comment
= 0, nest
[MAX_NESTED_COMMENTS
];
663 struct ast_category
*cat
= NULL
;
667 cat
= ast_config_get_current_category(cfg
);
669 if (filename
[0] == '/') {
670 ast_copy_string(fn
, filename
, sizeof(fn
));
672 snprintf(fn
, sizeof(fn
), "%s/%s", (char *)ast_config_AST_CONFIG_DIR
, filename
);
675 #ifdef AST_INCLUDE_GLOB
679 globbuf
.gl_offs
= 0; /* initialize it to silence gcc */
681 glob_ret
= glob(fn
, GLOB_NOCHECK
, NULL
, &globbuf
);
683 glob_ret
= glob(fn
, GLOB_NOMAGIC
|GLOB_BRACE
, NULL
, &globbuf
);
685 if (glob_ret
== GLOB_NOSPACE
)
687 "Glob Expansion of pattern '%s' failed: Not enough memory\n", fn
);
688 else if (glob_ret
== GLOB_ABORTED
)
690 "Glob Expansion of pattern '%s' failed: Read error\n", fn
);
692 /* loop over expanded files */
694 for (i
=0; i
<globbuf
.gl_pathc
; i
++) {
695 ast_copy_string(fn
, globbuf
.gl_pathv
[i
], sizeof(fn
));
698 if (stat(fn
, &statbuf
))
701 if (!S_ISREG(statbuf
.st_mode
)) {
702 ast_log(LOG_WARNING
, "'%s' is not a regular file, ignoring\n", fn
);
705 if ((option_verbose
> 1) && !option_debug
) {
706 ast_verbose(VERBOSE_PREFIX_2
"Parsing '%s': ", fn
);
709 if (!(f
= fopen(fn
, "r"))) {
711 ast_log(LOG_DEBUG
, "No file to parse: %s\n", fn
);
712 else if (option_verbose
> 1)
713 ast_verbose( "Not found (%s)\n", strerror(errno
));
718 ast_log(LOG_DEBUG
, "Parsing %s\n", fn
);
719 else if (option_verbose
> 1)
720 ast_verbose("Found\n");
723 if (fgets(buf
, sizeof(buf
), f
)) {
729 while ((comment_p
= strchr(new_buf
, COMMENT_META
))) {
730 if ((comment_p
> new_buf
) && (*(comment_p
-1) == '\\')) {
731 /* Yuck, gotta memmove */
732 memmove(comment_p
- 1, comment_p
, strlen(comment_p
) + 1);
734 } else if(comment_p
[1] == COMMENT_TAG
&& comment_p
[2] == COMMENT_TAG
&& (comment_p
[3] != '-')) {
735 /* Meta-Comment start detected ";--" */
736 if (comment
< MAX_NESTED_COMMENTS
) {
738 new_buf
= comment_p
+ 3;
740 nest
[comment
-1] = lineno
;
742 ast_log(LOG_ERROR
, "Maximum nest limit of %d reached.\n", MAX_NESTED_COMMENTS
);
744 } else if ((comment_p
>= new_buf
+ 2) &&
745 (*(comment_p
- 1) == COMMENT_TAG
) &&
746 (*(comment_p
- 2) == COMMENT_TAG
)) {
747 /* Meta-Comment end detected */
749 new_buf
= comment_p
+ 1;
751 /* Back to non-comment now */
753 /* Actually have to move what's left over the top, then continue */
755 oldptr
= process_buf
+ strlen(process_buf
);
756 memmove(oldptr
, new_buf
, strlen(new_buf
) + 1);
759 process_buf
= new_buf
;
763 /* If ; is found, and we are not nested in a comment,
764 we immediately stop all comment processing */
768 new_buf
= comment_p
+ 1;
772 char *buf
= ast_strip(process_buf
);
773 if (!ast_strlen_zero(buf
)) {
774 if (process_text_line(cfg
, &cat
, buf
, lineno
, filename
, withcomments
)) {
785 ast_log(LOG_WARNING
,"Unterminated comment detected beginning on line %d\n", nest
[comment
]);
787 #ifdef AST_INCLUDE_GLOB
801 int config_text_file_save(const char *configfile
, const struct ast_config
*cfg
, const char *generator
)
807 struct ast_variable
*var
;
808 struct ast_category
*cat
;
811 if (configfile
[0] == '/') {
812 ast_copy_string(fn
, configfile
, sizeof(fn
));
814 snprintf(fn
, sizeof(fn
), "%s/%s", ast_config_AST_CONFIG_DIR
, configfile
);
817 ast_copy_string(date
, ctime(&t
), sizeof(date
));
819 if ((f
= fopen(fn
, "w+"))) {
821 if ((f
= fopen(fn
, "w"))) {
823 if ((option_verbose
> 1) && !option_debug
)
824 ast_verbose( VERBOSE_PREFIX_2
"Saving '%s': ", fn
);
826 fprintf(f
, ";! Automatically generated configuration file\n");
827 fprintf(f
, ";! Filename: %s (%s)\n", configfile
, fn
);
828 fprintf(f
, ";! Generator: %s\n", generator
);
829 fprintf(f
, ";! Creation Date: %s", date
);
833 /* Dump section with any appropriate comment */
834 fprintf(f
, "\n[%s]\n", cat
->name
);
838 fprintf(f
, "%s %s %s ; %s\n", var
->name
, (var
->object
? "=>" : "="), var
->value
, var
->sameline
->cmt
);
840 fprintf(f
, "%s %s %s\n", var
->name
, (var
->object
? "=>" : "="), var
->value
);
841 if (var
->blanklines
) {
842 blanklines
= var
->blanklines
;
850 /* Put an empty line */
855 if ((option_verbose
> 1) && !option_debug
)
856 ast_verbose("Saved\n");
859 printf("Unable to open for writing: %s\n", fn
);
860 else if (option_verbose
> 1)
861 printf( "Unable to write (%s)", strerror(errno
));
868 static void clear_config_maps(void)
870 struct ast_config_map
*map
;
872 ast_mutex_lock(&config_lock
);
874 while (config_maps
) {
876 config_maps
= config_maps
->next
;
880 ast_mutex_unlock(&config_lock
);
883 static int append_mapping(char *name
, char *driver
, char *database
, char *table
)
885 struct ast_config_map
*map
;
888 length
= sizeof(*map
);
889 length
+= strlen(name
) + 1;
890 length
+= strlen(driver
) + 1;
891 length
+= strlen(database
) + 1;
893 length
+= strlen(table
) + 1;
895 if (!(map
= ast_calloc(1, length
)))
898 map
->name
= map
->stuff
;
899 strcpy(map
->name
, name
);
900 map
->driver
= map
->name
+ strlen(map
->name
) + 1;
901 strcpy(map
->driver
, driver
);
902 map
->database
= map
->driver
+ strlen(map
->driver
) + 1;
903 strcpy(map
->database
, database
);
905 map
->table
= map
->database
+ strlen(map
->database
) + 1;
906 strcpy(map
->table
, table
);
908 map
->next
= config_maps
;
910 if (option_verbose
> 1)
911 ast_verbose(VERBOSE_PREFIX_2
"Binding %s to %s/%s/%s\n",
912 map
->name
, map
->driver
, map
->database
, map
->table
? map
->table
: map
->name
);
918 int read_config_maps(void)
920 struct ast_config
*config
, *configtmp
;
921 struct ast_variable
*v
;
922 char *driver
, *table
, *database
, *stringp
;
926 configtmp
= ast_config_new();
927 configtmp
->max_include_level
= 1;
928 config
= ast_config_internal_load(extconfig_conf
, configtmp
, 0);
930 ast_config_destroy(configtmp
);
934 for (v
= ast_variable_browse(config
, "settings"); v
; v
= v
->next
) {
936 driver
= strsep(&stringp
, ",");
938 /* check if the database text starts with a double quote */
939 if (*stringp
== '"') {
941 database
= strsep(&stringp
, "\"");
942 strsep(&stringp
, ",");
944 /* apparently this text has no quotes */
945 database
= strsep(&stringp
, ",");
948 table
= strsep(&stringp
, ",");
950 if (!strcmp(v
->name
, extconfig_conf
)) {
951 ast_log(LOG_WARNING
, "Cannot bind '%s'!\n", extconfig_conf
);
955 if (!strcmp(v
->name
, "asterisk.conf")) {
956 ast_log(LOG_WARNING
, "Cannot bind 'asterisk.conf'!\n");
960 if (!strcmp(v
->name
, "logger.conf")) {
961 ast_log(LOG_WARNING
, "Cannot bind 'logger.conf'!\n");
965 if (!driver
|| !database
)
967 if (!strcasecmp(v
->name
, "sipfriends")) {
968 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");
969 append_mapping("sipusers", driver
, database
, table
? table
: "sipfriends");
970 append_mapping("sippeers", driver
, database
, table
? table
: "sipfriends");
971 } else if (!strcasecmp(v
->name
, "iaxfriends")) {
972 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");
973 append_mapping("iaxusers", driver
, database
, table
? table
: "iaxfriends");
974 append_mapping("iaxpeers", driver
, database
, table
? table
: "iaxfriends");
976 append_mapping(v
->name
, driver
, database
, table
);
979 ast_config_destroy(config
);
983 int ast_config_engine_register(struct ast_config_engine
*new)
985 struct ast_config_engine
*ptr
;
987 ast_mutex_lock(&config_lock
);
989 if (!config_engine_list
) {
990 config_engine_list
= new;
992 for (ptr
= config_engine_list
; ptr
->next
; ptr
=ptr
->next
);
996 ast_mutex_unlock(&config_lock
);
997 ast_log(LOG_NOTICE
,"Registered Config Engine %s\n", new->name
);
1002 int ast_config_engine_deregister(struct ast_config_engine
*del
)
1004 struct ast_config_engine
*ptr
, *last
=NULL
;
1006 ast_mutex_lock(&config_lock
);
1008 for (ptr
= config_engine_list
; ptr
; ptr
=ptr
->next
) {
1011 last
->next
= ptr
->next
;
1013 config_engine_list
= ptr
->next
;
1019 ast_mutex_unlock(&config_lock
);
1024 /*! \brief Find realtime engine for realtime family */
1025 static struct ast_config_engine
*find_engine(const char *family
, char *database
, int dbsiz
, char *table
, int tabsiz
)
1027 struct ast_config_engine
*eng
, *ret
= NULL
;
1028 struct ast_config_map
*map
;
1030 ast_mutex_lock(&config_lock
);
1032 for (map
= config_maps
; map
; map
= map
->next
) {
1033 if (!strcasecmp(family
, map
->name
)) {
1035 ast_copy_string(database
, map
->database
, dbsiz
);
1037 ast_copy_string(table
, map
->table
? map
->table
: family
, tabsiz
);
1042 /* Check if the required driver (engine) exist */
1044 for (eng
= config_engine_list
; !ret
&& eng
; eng
= eng
->next
) {
1045 if (!strcasecmp(eng
->name
, map
->driver
))
1050 ast_mutex_unlock(&config_lock
);
1052 /* if we found a mapping, but the engine is not available, then issue a warning */
1054 ast_log(LOG_WARNING
, "Realtime mapping for '%s' found to engine '%s', but the engine is not available\n", map
->name
, map
->driver
);
1059 static struct ast_config_engine text_file_engine
= {
1061 .load_func
= config_text_file_load
,
1064 struct ast_config
*ast_config_internal_load(const char *filename
, struct ast_config
*cfg
, int withcomments
)
1068 struct ast_config_engine
*loader
= &text_file_engine
;
1069 struct ast_config
*result
;
1071 if (cfg
->include_level
== cfg
->max_include_level
) {
1072 ast_log(LOG_WARNING
, "Maximum Include level (%d) exceeded\n", cfg
->max_include_level
);
1076 cfg
->include_level
++;
1078 if (strcmp(filename
, extconfig_conf
) && strcmp(filename
, "asterisk.conf") && config_engine_list
) {
1079 struct ast_config_engine
*eng
;
1081 eng
= find_engine(filename
, db
, sizeof(db
), table
, sizeof(table
));
1084 if (eng
&& eng
->load_func
) {
1087 eng
= find_engine("global", db
, sizeof(db
), table
, sizeof(table
));
1088 if (eng
&& eng
->load_func
)
1093 result
= loader
->load_func(db
, table
, filename
, cfg
, withcomments
);
1096 result
->include_level
--;
1101 struct ast_config
*ast_config_load(const char *filename
)
1103 struct ast_config
*cfg
;
1104 struct ast_config
*result
;
1106 cfg
= ast_config_new();
1110 result
= ast_config_internal_load(filename
, cfg
, 0);
1112 ast_config_destroy(cfg
);
1117 struct ast_config
*ast_config_load_with_comments(const char *filename
)
1119 struct ast_config
*cfg
;
1120 struct ast_config
*result
;
1122 cfg
= ast_config_new();
1126 result
= ast_config_internal_load(filename
, cfg
, 1);
1128 ast_config_destroy(cfg
);
1133 struct ast_variable
*ast_load_realtime(const char *family
, ...)
1135 struct ast_config_engine
*eng
;
1138 struct ast_variable
*res
=NULL
;
1141 va_start(ap
, family
);
1142 eng
= find_engine(family
, db
, sizeof(db
), table
, sizeof(table
));
1143 if (eng
&& eng
->realtime_func
)
1144 res
= eng
->realtime_func(db
, table
, ap
);
1150 /*! \brief Check if realtime engine is configured for family */
1151 int ast_check_realtime(const char *family
)
1153 struct ast_config_engine
*eng
;
1155 eng
= find_engine(family
, NULL
, 0, NULL
, 0);
1162 struct ast_config
*ast_load_realtime_multientry(const char *family
, ...)
1164 struct ast_config_engine
*eng
;
1167 struct ast_config
*res
=NULL
;
1170 va_start(ap
, family
);
1171 eng
= find_engine(family
, db
, sizeof(db
), table
, sizeof(table
));
1172 if (eng
&& eng
->realtime_multi_func
)
1173 res
= eng
->realtime_multi_func(db
, table
, ap
);
1179 int ast_update_realtime(const char *family
, const char *keyfield
, const char *lookup
, ...)
1181 struct ast_config_engine
*eng
;
1187 va_start(ap
, lookup
);
1188 eng
= find_engine(family
, db
, sizeof(db
), table
, sizeof(table
));
1189 if (eng
&& eng
->update_func
)
1190 res
= eng
->update_func(db
, table
, keyfield
, lookup
, ap
);
1196 static int config_command(int fd
, int argc
, char **argv
)
1198 struct ast_config_engine
*eng
;
1199 struct ast_config_map
*map
;
1201 ast_mutex_lock(&config_lock
);
1203 ast_cli(fd
, "\n\n");
1204 for (eng
= config_engine_list
; eng
; eng
= eng
->next
) {
1205 ast_cli(fd
, "\nConfig Engine: %s\n", eng
->name
);
1206 for (map
= config_maps
; map
; map
= map
->next
)
1207 if (!strcasecmp(map
->driver
, eng
->name
)) {
1208 ast_cli(fd
, "===> %s (db=%s, table=%s)\n", map
->name
, map
->database
,
1209 map
->table
? map
->table
: map
->name
);
1214 ast_mutex_unlock(&config_lock
);
1219 static char show_config_help
[] =
1220 "Usage: show config mappings\n"
1221 " Shows the filenames to config engines.\n";
1223 static struct ast_cli_entry config_command_struct
= {
1224 { "show", "config", "mappings", NULL
}, config_command
, "Show Config mappings (file names to config engines)", show_config_help
, NULL
1227 int register_config_cli()
1229 return ast_cli_register(&config_command_struct
);