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
);
92 lline_buffer_size
= CB_INCR
;
94 comment_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
);
104 comment_buffer
= ast_realloc(comment_buffer
, comment_buffer_size
+ CB_INCR
+ siz
+ 1);
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
;
117 comment_buffer
= ast_realloc(comment_buffer
, comment_buffer_size
+ CB_INCR
+ len
+ 1);
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
);
131 lline_buffer
= ast_realloc(lline_buffer
, lline_buffer_size
+ CB_INCR
+ siz
+ 1);
134 lline_buffer_size
+= CB_INCR
+ siz
+ 1;
136 strcat(lline_buffer
,str
);
139 static void CB_RESET(void )
141 comment_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
);
155 static struct ast_config_map
{
156 struct ast_config_map
*next
;
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
{
171 int ignored
; /*!< do not let user of the config see this category */
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
;
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 */
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
);
204 void ast_variable_append(struct ast_category
*category
, struct ast_variable
*variable
)
209 category
->last
->next
= variable
;
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
;
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
;
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
)
243 tmp
= ast_variable_retrieve(cfg
, cat
, var
);
245 tmp
= ast_variable_retrieve(cfg
, "general", var
);
250 const char *ast_variable_retrieve(const struct ast_config
*config
, const char *category
, const char *variable
)
252 struct ast_variable
*v
;
255 for (v
= ast_variable_browse(config
, category
); v
; v
= v
->next
) {
256 if (!strcasecmp(variable
, v
->name
))
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
))
271 static struct ast_variable
*variable_clone(const struct ast_variable
*old
)
273 struct ast_variable
*new = ast_variable_new(old
->name
, old
->value
);
276 new->lineno
= old
->lineno
;
277 new->object
= old
->object
;
278 new->blanklines
= old
->blanklines
;
279 /* TODO: clone comments? */
285 static void move_variables(struct ast_category
*old
, struct ast_category
*new)
287 struct ast_variable
*var
= old
->root
;
290 /* we can just move the entire list in a single op */
291 ast_variable_append(new, var
);
294 struct ast_variable
*next
= var
->next
;
296 ast_variable_append(new, var
);
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
));
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
))
321 for (cat
= config
->root
; cat
; cat
= cat
->next
) {
322 if (!strcasecmp(cat
->name
, category_name
) && (ignored
|| !cat
->ignored
))
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
)
342 config
->last
->next
= category
;
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
);
356 static struct ast_category
*next_available_category(struct ast_category
*cat
)
358 for (; cat
&& cat
->ignored
; cat
= cat
->next
);
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
)
372 for (cat
= config
->root
; cat
; cat
= cat
->next
) {
373 if (cat
->name
== prev
) {
379 for (cat
= config
->root
; cat
; cat
= cat
->next
) {
380 if (!strcasecmp(cat
->name
, prev
)) {
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
;
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
;
428 int ast_variable_delete(struct ast_category
*category
, char *variable
, char *match
)
430 struct ast_variable
*cur
, *prev
=NULL
, *curn
;
432 cur
= category
->root
;
434 if (cur
->name
== variable
) {
436 prev
->next
= cur
->next
;
437 if (cur
== category
->last
)
438 category
->last
= prev
;
440 category
->root
= cur
->next
;
441 if (cur
== category
->last
)
442 category
->last
= NULL
;
445 ast_variables_destroy(cur
);
453 cur
= category
->root
;
456 if (!strcasecmp(cur
->name
, variable
) && (ast_strlen_zero(match
) || !strcasecmp(cur
->value
, match
))) {
458 prev
->next
= cur
->next
;
459 if (cur
== category
->last
)
460 category
->last
= prev
;
462 category
->root
= cur
->next
;
463 if (cur
== category
->last
)
464 category
->last
= NULL
;
467 ast_variables_destroy(cur
);
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
);
483 cur
= category
->root
;
485 if (cur
->name
== variable
) {
486 newer
->next
= cur
->next
;
487 newer
->object
= cur
->object
;
491 category
->root
= newer
;
492 if (category
->last
== cur
)
493 category
->last
= newer
;
495 ast_variables_destroy(cur
);
503 cur
= category
->root
;
505 if (!strcasecmp(cur
->name
, variable
) && (ast_strlen_zero(match
) || !strcasecmp(cur
->value
, match
))) {
506 newer
->next
= cur
->next
;
507 newer
->object
= cur
->object
;
511 category
->root
= newer
;
512 if (category
->last
== cur
)
513 category
->last
= newer
;
515 ast_variables_destroy(cur
);
524 category
->root
= newer
;
528 int ast_category_delete(struct ast_config
*cfg
, char *category
)
530 struct ast_category
*prev
=NULL
, *cat
;
533 if (cat
->name
== category
) {
534 ast_variables_destroy(cat
->root
);
536 prev
->next
= cat
->next
;
537 if (cat
== cfg
->last
)
540 cfg
->root
= cat
->next
;
541 if (cat
== cfg
->last
)
554 if (!strcasecmp(cat
->name
, category
)) {
555 ast_variables_destroy(cat
->root
);
557 prev
->next
= cat
->next
;
558 if (cat
== cfg
->last
)
561 cfg
->root
= cat
->next
;
562 if (cat
== cfg
->last
)
574 void ast_config_destroy(struct ast_config
*cfg
)
576 struct ast_category
*cat
, *catn
;
583 ast_variables_destroy(cat
->root
);
591 struct ast_category
*ast_config_get_current_category(const struct ast_config
*cfg
)
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
)
606 struct ast_variable
*v
;
607 char cmd
[512], exec_file
[512];
608 int object
, do_exec
, do_include
;
610 /* Actually parse the entry */
612 struct ast_category
*newcat
= NULL
;
615 /* A category header */
616 c
= strchr(cur
, ']');
618 ast_log(LOG_WARNING
, "parse error: no closing ']', line %d of %s\n", lineno
, configfile
);
626 if (!(*cat
= newcat
= ast_category_new(catname
))) {
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
);
639 /* If there are options or categories to inherit from, process them now */
641 if (!(cur
= strchr(c
, ')'))) {
642 ast_log(LOG_WARNING
, "parse error: no closing ')', line %d of %s\n", lineno
, configfile
);
646 while ((cur
= strsep(&c
, ","))) {
647 if (!strcasecmp(cur
, "!")) {
649 } else if (!strcasecmp(cur
, "+")) {
650 *cat
= category_get(cfg
, catname
, 1);
652 ast_config_destroy(cfg
);
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
);
659 move_variables(newcat
, *cat
);
660 ast_category_destroy(newcat
);
664 struct ast_category
*base
;
666 base
= category_get(cfg
, cur
, 1);
668 ast_log(LOG_WARNING
, "Inheritance requested, but category '%s' does not exist, line %d of %s\n", cur
, lineno
, configfile
);
671 inherit_category(*cat
, base
);
676 ast_category_append(cfg
, *cat
);
677 } else if (cur
[0] == '#') {
681 while(*c
&& (*c
> 32)) c
++;
684 /* Find real argument */
685 c
= ast_skip_blanks(c
+ 1);
690 do_include
= !strcasecmp(cur
, "include");
692 do_exec
= !strcasecmp(cur
, "exec");
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");
699 if (do_include
|| do_exec
) {
701 /* Strip off leading and trailing "'s and <>'s */
702 while((*c
== '<') || (*c
== '>') || (*c
== '\"')) c
++;
703 /* Get rid of leading mess */
705 while (!ast_strlen_zero(cur
)) {
706 c
= cur
+ strlen(cur
) - 1;
707 if ((*c
== '>') || (*c
== '<') || (*c
== '\"'))
712 /* #exec </path/to/executable>
713 We create a tmp file, then we #include it, then we delete it. */
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
);
722 do_include
= ast_config_internal_load(cur
, cfg
, withcomments
) ? 1 : 0;
723 if(!ast_strlen_zero(exec_file
))
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",
737 ast_log(LOG_WARNING
, "Unknown directive '%s' at line %d of %s\n", cur
, lineno
, configfile
);
739 /* Just a line (variable = value) */
742 "parse error: No category context for line %d of %s\n", lineno
, configfile
);
745 c
= strchr(cur
, '=');
755 if ((v
= ast_variable_new(ast_strip(cur
), ast_strip(c
)))) {
758 /* Put and reset comments */
760 ast_variable_append(*cat
, v
);
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
);
775 ast_log(LOG_WARNING
, "No '=' (equal sign) in line %d of %s\n", lineno
, configfile
);
781 static struct ast_config
*config_text_file_load(const char *database
, const char *table
, const char *filename
, struct ast_config
*cfg
, int withcomments
)
785 char *new_buf
, *comment_p
, *process_buf
;
788 int comment
= 0, nest
[MAX_NESTED_COMMENTS
];
789 struct ast_category
*cat
= NULL
;
793 cat
= ast_config_get_current_category(cfg
);
795 if (filename
[0] == '/') {
796 ast_copy_string(fn
, filename
, sizeof(fn
));
798 snprintf(fn
, sizeof(fn
), "%s/%s", (char *)ast_config_AST_CONFIG_DIR
, filename
);
803 if (!lline_buffer
|| !comment_buffer
) {
804 ast_log(LOG_ERROR
, "Failed to initialize the comment buffer!\n");
808 #ifdef AST_INCLUDE_GLOB
812 globbuf
.gl_offs
= 0; /* initialize it to silence gcc */
814 glob_ret
= glob(fn
, GLOB_NOCHECK
, NULL
, &globbuf
);
816 glob_ret
= glob(fn
, GLOB_NOMAGIC
|GLOB_BRACE
, NULL
, &globbuf
);
818 if (glob_ret
== GLOB_NOSPACE
)
820 "Glob Expansion of pattern '%s' failed: Not enough memory\n", fn
);
821 else if (glob_ret
== GLOB_ABORTED
)
823 "Glob Expansion of pattern '%s' failed: Read error\n", fn
);
825 /* loop over expanded files */
827 for (i
=0; i
<globbuf
.gl_pathc
; i
++) {
828 ast_copy_string(fn
, globbuf
.gl_pathv
[i
], sizeof(fn
));
831 if (stat(fn
, &statbuf
))
834 if (!S_ISREG(statbuf
.st_mode
)) {
835 ast_log(LOG_WARNING
, "'%s' is not a regular file, ignoring\n", fn
);
838 if (option_verbose
> 1) {
839 ast_verbose(VERBOSE_PREFIX_2
"Parsing '%s': ", fn
);
842 if (!(f
= fopen(fn
, "r"))) {
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
));
851 ast_log(LOG_DEBUG
, "Parsing %s\n", fn
);
852 if (option_verbose
> 1)
853 ast_verbose("Found\n");
856 if (fgets(buf
, sizeof(buf
), f
)) {
857 if ( withcomments
) {
858 CB_ADD(lline_buffer
); /* add the current lline buffer to the comment buffer */
859 lline_buffer
[0] = 0; /* erase the lline buffer */
868 while ((comment_p
= strchr(new_buf
, COMMENT_META
))) {
869 if ((comment_p
> new_buf
) && (*(comment_p
-1) == '\\')) {
870 /* Yuck, gotta memmove */
871 memmove(comment_p
- 1, comment_p
, strlen(comment_p
) + 1);
873 } else if(comment_p
[1] == COMMENT_TAG
&& comment_p
[2] == COMMENT_TAG
&& (comment_p
[3] != '-')) {
874 /* Meta-Comment start detected ";--" */
875 if (comment
< MAX_NESTED_COMMENTS
) {
877 new_buf
= comment_p
+ 3;
879 nest
[comment
-1] = lineno
;
881 ast_log(LOG_ERROR
, "Maximum nest limit of %d reached.\n", MAX_NESTED_COMMENTS
);
883 } else if ((comment_p
>= new_buf
+ 2) &&
884 (*(comment_p
- 1) == COMMENT_TAG
) &&
885 (*(comment_p
- 2) == COMMENT_TAG
)) {
886 /* Meta-Comment end detected */
888 new_buf
= comment_p
+ 1;
890 /* Back to non-comment now */
892 /* Actually have to move what's left over the top, then continue */
894 oldptr
= process_buf
+ strlen(process_buf
);
895 if ( withcomments
) {
897 CB_ADD_LEN(oldptr
+1,new_buf
-oldptr
-1);
900 memmove(oldptr
, new_buf
, strlen(new_buf
) + 1);
903 process_buf
= new_buf
;
907 /* If ; is found, and we are not nested in a comment,
908 we immediately stop all comment processing */
909 if ( withcomments
) {
915 new_buf
= comment_p
+ 1;
918 if( withcomments
&& comment
&& !process_buf
)
920 CB_ADD(buf
); /* the whole line is a comment, store it */
924 char *buf
= ast_strip(process_buf
);
925 if (!ast_strlen_zero(buf
)) {
926 if (process_text_line(cfg
, &cat
, buf
, lineno
, fn
, withcomments
)) {
937 ast_log(LOG_WARNING
,"Unterminated comment detected beginning on line %d\n", nest
[comment
]);
939 #ifdef AST_INCLUDE_GLOB
948 if (cfg
&& cfg
->include_level
== 1 && withcomments
&& comment_buffer
) {
949 free(comment_buffer
);
951 comment_buffer
= NULL
;
953 comment_buffer_size
= 0;
954 lline_buffer_size
= 0;
963 int config_text_file_save(const char *configfile
, const struct ast_config
*cfg
, const char *generator
)
969 struct ast_variable
*var
;
970 struct ast_category
*cat
;
971 struct ast_comment
*cmt
;
974 if (configfile
[0] == '/') {
975 ast_copy_string(fn
, configfile
, sizeof(fn
));
977 snprintf(fn
, sizeof(fn
), "%s/%s", ast_config_AST_CONFIG_DIR
, configfile
);
980 ast_copy_string(date
, ctime(&t
), sizeof(date
));
982 if ((f
= fopen(fn
, "w+"))) {
984 if ((f
= fopen(fn
, "w"))) {
986 if (option_verbose
> 1)
987 ast_verbose(VERBOSE_PREFIX_2
"Saving '%s': ", fn
);
989 fprintf(f
, ";! Automatically generated configuration file\n");
990 if (strcmp(configfile
, fn
))
991 fprintf(f
, ";! Filename: %s (%s)\n", configfile
, fn
);
993 fprintf(f
, ";! Filename: %s\n", configfile
);
994 fprintf(f
, ";! Generator: %s\n", generator
);
995 fprintf(f
, ";! Creation Date: %s", date
);
999 /* Dump section with any appropriate comment */
1000 for (cmt
= cat
->precomments
; cmt
; cmt
=cmt
->next
)
1002 if (cmt
->cmt
[0] != ';' || cmt
->cmt
[1] != '!')
1003 fprintf(f
,"%s", cmt
->cmt
);
1005 if (!cat
->precomments
)
1007 fprintf(f
, "[%s]", cat
->name
);
1008 for(cmt
= cat
->sameline
; cmt
; cmt
=cmt
->next
)
1010 fprintf(f
,"%s", cmt
->cmt
);
1016 for (cmt
= var
->precomments
; cmt
; cmt
=cmt
->next
)
1018 if (cmt
->cmt
[0] != ';' || cmt
->cmt
[1] != '!')
1019 fprintf(f
,"%s", cmt
->cmt
);
1022 fprintf(f
, "%s %s %s %s", var
->name
, (var
->object
? "=>" : "="), var
->value
, var
->sameline
->cmt
);
1024 fprintf(f
, "%s %s %s\n", var
->name
, (var
->object
? "=>" : "="), var
->value
);
1025 if (var
->blanklines
) {
1026 blanklines
= var
->blanklines
;
1027 while (blanklines
--)
1034 /* Put an empty line */
1039 if ((option_verbose
> 1) && !option_debug
)
1040 ast_verbose("Saved\n");
1043 ast_log(LOG_DEBUG
, "Unable to open for writing: %s\n", fn
);
1044 if (option_verbose
> 1)
1045 ast_verbose(VERBOSE_PREFIX_2
"Unable to write (%s)", strerror(errno
));
1052 static void clear_config_maps(void)
1054 struct ast_config_map
*map
;
1056 ast_mutex_lock(&config_lock
);
1058 while (config_maps
) {
1060 config_maps
= config_maps
->next
;
1064 ast_mutex_unlock(&config_lock
);
1067 static int append_mapping(char *name
, char *driver
, char *database
, char *table
)
1069 struct ast_config_map
*map
;
1072 length
= sizeof(*map
);
1073 length
+= strlen(name
) + 1;
1074 length
+= strlen(driver
) + 1;
1075 length
+= strlen(database
) + 1;
1077 length
+= strlen(table
) + 1;
1079 if (!(map
= ast_calloc(1, length
)))
1082 map
->name
= map
->stuff
;
1083 strcpy(map
->name
, name
);
1084 map
->driver
= map
->name
+ strlen(map
->name
) + 1;
1085 strcpy(map
->driver
, driver
);
1086 map
->database
= map
->driver
+ strlen(map
->driver
) + 1;
1087 strcpy(map
->database
, database
);
1089 map
->table
= map
->database
+ strlen(map
->database
) + 1;
1090 strcpy(map
->table
, table
);
1092 map
->next
= config_maps
;
1094 if (option_verbose
> 1)
1095 ast_verbose(VERBOSE_PREFIX_2
"Binding %s to %s/%s/%s\n",
1096 map
->name
, map
->driver
, map
->database
, map
->table
? map
->table
: map
->name
);
1102 int read_config_maps(void)
1104 struct ast_config
*config
, *configtmp
;
1105 struct ast_variable
*v
;
1106 char *driver
, *table
, *database
, *stringp
, *tmp
;
1108 clear_config_maps();
1110 configtmp
= ast_config_new();
1111 configtmp
->max_include_level
= 1;
1112 config
= ast_config_internal_load(extconfig_conf
, configtmp
, 0);
1114 ast_config_destroy(configtmp
);
1118 for (v
= ast_variable_browse(config
, "settings"); v
; v
= v
->next
) {
1120 driver
= strsep(&stringp
, ",");
1122 if ((tmp
= strchr(stringp
, '\"')))
1125 /* check if the database text starts with a double quote */
1126 if (*stringp
== '"') {
1128 database
= strsep(&stringp
, "\"");
1129 strsep(&stringp
, ",");
1131 /* apparently this text has no quotes */
1132 database
= strsep(&stringp
, ",");
1135 table
= strsep(&stringp
, ",");
1137 if (!strcmp(v
->name
, extconfig_conf
)) {
1138 ast_log(LOG_WARNING
, "Cannot bind '%s'!\n", extconfig_conf
);
1142 if (!strcmp(v
->name
, "asterisk.conf")) {
1143 ast_log(LOG_WARNING
, "Cannot bind 'asterisk.conf'!\n");
1147 if (!strcmp(v
->name
, "logger.conf")) {
1148 ast_log(LOG_WARNING
, "Cannot bind 'logger.conf'!\n");
1152 if (!driver
|| !database
)
1154 if (!strcasecmp(v
->name
, "sipfriends")) {
1155 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");
1156 append_mapping("sipusers", driver
, database
, table
? table
: "sipfriends");
1157 append_mapping("sippeers", driver
, database
, table
? table
: "sipfriends");
1158 } else if (!strcasecmp(v
->name
, "iaxfriends")) {
1159 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");
1160 append_mapping("iaxusers", driver
, database
, table
? table
: "iaxfriends");
1161 append_mapping("iaxpeers", driver
, database
, table
? table
: "iaxfriends");
1163 append_mapping(v
->name
, driver
, database
, table
);
1166 ast_config_destroy(config
);
1170 int ast_config_engine_register(struct ast_config_engine
*new)
1172 struct ast_config_engine
*ptr
;
1174 ast_mutex_lock(&config_lock
);
1176 if (!config_engine_list
) {
1177 config_engine_list
= new;
1179 for (ptr
= config_engine_list
; ptr
->next
; ptr
=ptr
->next
);
1183 ast_mutex_unlock(&config_lock
);
1184 ast_log(LOG_NOTICE
,"Registered Config Engine %s\n", new->name
);
1189 int ast_config_engine_deregister(struct ast_config_engine
*del
)
1191 struct ast_config_engine
*ptr
, *last
=NULL
;
1193 ast_mutex_lock(&config_lock
);
1195 for (ptr
= config_engine_list
; ptr
; ptr
=ptr
->next
) {
1198 last
->next
= ptr
->next
;
1200 config_engine_list
= ptr
->next
;
1206 ast_mutex_unlock(&config_lock
);
1211 /*! \brief Find realtime engine for realtime family */
1212 static struct ast_config_engine
*find_engine(const char *family
, char *database
, int dbsiz
, char *table
, int tabsiz
)
1214 struct ast_config_engine
*eng
, *ret
= NULL
;
1215 struct ast_config_map
*map
;
1217 ast_mutex_lock(&config_lock
);
1219 for (map
= config_maps
; map
; map
= map
->next
) {
1220 if (!strcasecmp(family
, map
->name
)) {
1222 ast_copy_string(database
, map
->database
, dbsiz
);
1224 ast_copy_string(table
, map
->table
? map
->table
: family
, tabsiz
);
1229 /* Check if the required driver (engine) exist */
1231 for (eng
= config_engine_list
; !ret
&& eng
; eng
= eng
->next
) {
1232 if (!strcasecmp(eng
->name
, map
->driver
))
1237 ast_mutex_unlock(&config_lock
);
1239 /* if we found a mapping, but the engine is not available, then issue a warning */
1241 ast_log(LOG_WARNING
, "Realtime mapping for '%s' found to engine '%s', but the engine is not available\n", map
->name
, map
->driver
);
1246 static struct ast_config_engine text_file_engine
= {
1248 .load_func
= config_text_file_load
,
1251 struct ast_config
*ast_config_internal_load(const char *filename
, struct ast_config
*cfg
, int withcomments
)
1255 struct ast_config_engine
*loader
= &text_file_engine
;
1256 struct ast_config
*result
;
1258 if (cfg
->include_level
== cfg
->max_include_level
) {
1259 ast_log(LOG_WARNING
, "Maximum Include level (%d) exceeded\n", cfg
->max_include_level
);
1263 cfg
->include_level
++;
1265 if (strcmp(filename
, extconfig_conf
) && strcmp(filename
, "asterisk.conf") && config_engine_list
) {
1266 struct ast_config_engine
*eng
;
1268 eng
= find_engine(filename
, db
, sizeof(db
), table
, sizeof(table
));
1271 if (eng
&& eng
->load_func
) {
1274 eng
= find_engine("global", db
, sizeof(db
), table
, sizeof(table
));
1275 if (eng
&& eng
->load_func
)
1280 result
= loader
->load_func(db
, table
, filename
, cfg
, withcomments
);
1283 result
->include_level
--;
1288 struct ast_config
*ast_config_load(const char *filename
)
1290 struct ast_config
*cfg
;
1291 struct ast_config
*result
;
1293 cfg
= ast_config_new();
1297 result
= ast_config_internal_load(filename
, cfg
, 0);
1299 ast_config_destroy(cfg
);
1304 struct ast_config
*ast_config_load_with_comments(const char *filename
)
1306 struct ast_config
*cfg
;
1307 struct ast_config
*result
;
1309 cfg
= ast_config_new();
1313 result
= ast_config_internal_load(filename
, cfg
, 1);
1315 ast_config_destroy(cfg
);
1320 struct ast_variable
*ast_load_realtime(const char *family
, ...)
1322 struct ast_config_engine
*eng
;
1325 struct ast_variable
*res
=NULL
;
1328 va_start(ap
, family
);
1329 eng
= find_engine(family
, db
, sizeof(db
), table
, sizeof(table
));
1330 if (eng
&& eng
->realtime_func
)
1331 res
= eng
->realtime_func(db
, table
, ap
);
1337 /*! \brief Check if realtime engine is configured for family */
1338 int ast_check_realtime(const char *family
)
1340 struct ast_config_engine
*eng
;
1342 eng
= find_engine(family
, NULL
, 0, NULL
, 0);
1349 struct ast_config
*ast_load_realtime_multientry(const char *family
, ...)
1351 struct ast_config_engine
*eng
;
1354 struct ast_config
*res
=NULL
;
1357 va_start(ap
, family
);
1358 eng
= find_engine(family
, db
, sizeof(db
), table
, sizeof(table
));
1359 if (eng
&& eng
->realtime_multi_func
)
1360 res
= eng
->realtime_multi_func(db
, table
, ap
);
1366 int ast_update_realtime(const char *family
, const char *keyfield
, const char *lookup
, ...)
1368 struct ast_config_engine
*eng
;
1374 va_start(ap
, lookup
);
1375 eng
= find_engine(family
, db
, sizeof(db
), table
, sizeof(table
));
1376 if (eng
&& eng
->update_func
)
1377 res
= eng
->update_func(db
, table
, keyfield
, lookup
, ap
);
1383 static int config_command(int fd
, int argc
, char **argv
)
1385 struct ast_config_engine
*eng
;
1386 struct ast_config_map
*map
;
1388 ast_mutex_lock(&config_lock
);
1390 ast_cli(fd
, "\n\n");
1391 for (eng
= config_engine_list
; eng
; eng
= eng
->next
) {
1392 ast_cli(fd
, "\nConfig Engine: %s\n", eng
->name
);
1393 for (map
= config_maps
; map
; map
= map
->next
)
1394 if (!strcasecmp(map
->driver
, eng
->name
)) {
1395 ast_cli(fd
, "===> %s (db=%s, table=%s)\n", map
->name
, map
->database
,
1396 map
->table
? map
->table
: map
->name
);
1401 ast_mutex_unlock(&config_lock
);
1406 static char show_config_help
[] =
1407 "Usage: core show config mappings\n"
1408 " Shows the filenames to config engines.\n";
1410 static struct ast_cli_entry cli_show_config_mappings_deprecated
= {
1411 { "show", "config", "mappings", NULL
},
1412 config_command
, NULL
,
1415 static struct ast_cli_entry cli_config
[] = {
1416 { { "core", "show", "config", "mappings", NULL
},
1417 config_command
, "Display config mappings (file names to config engines)",
1418 show_config_help
, NULL
, &cli_show_config_mappings_deprecated
},
1421 int register_config_cli()
1423 ast_cli_register_multiple(cli_config
, sizeof(cli_config
) / sizeof(struct ast_cli_entry
));