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 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 */
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);
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
) {
878 new_buf
= comment_p
+ 3;
880 nest
[comment
-1] = lineno
;
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 */
889 new_buf
= comment_p
+ 1;
891 /* Back to non-comment now */
893 /* Actually have to move what's left over the top, then continue */
895 oldptr
= process_buf
+ strlen(process_buf
);
896 if ( withcomments
) {
898 CB_ADD_LEN(oldptr
+1,new_buf
-oldptr
-1);
901 memmove(oldptr
, new_buf
, strlen(new_buf
) + 1);
904 process_buf
= new_buf
;
908 /* If ; is found, and we are not nested in a comment,
909 we immediately stop all comment processing */
910 if ( withcomments
) {
916 new_buf
= comment_p
+ 1;
919 if( withcomments
&& comment
&& !process_buf
)
921 CB_ADD(buf
); /* the whole line is a comment, store it */
925 char *buf
= ast_strip(process_buf
);
926 if (!ast_strlen_zero(buf
)) {
927 if (process_text_line(cfg
, &cat
, buf
, lineno
, filename
, withcomments
)) {
938 ast_log(LOG_WARNING
,"Unterminated comment detected beginning on line %d\n", nest
[comment
]);
940 #ifdef AST_INCLUDE_GLOB
949 if (cfg
->include_level
== 1 && withcomments
&& comment_buffer
) {
950 free(comment_buffer
);
952 comment_buffer
= NULL
;
954 comment_buffer_size
= 0;
955 lline_buffer_size
= 0;
964 int config_text_file_save(const char *configfile
, const struct ast_config
*cfg
, const char *generator
)
970 struct ast_variable
*var
;
971 struct ast_category
*cat
;
972 struct ast_comment
*cmt
;
975 if (configfile
[0] == '/') {
976 ast_copy_string(fn
, configfile
, sizeof(fn
));
978 snprintf(fn
, sizeof(fn
), "%s/%s", ast_config_AST_CONFIG_DIR
, configfile
);
981 ast_copy_string(date
, ctime(&t
), sizeof(date
));
983 if ((f
= fopen(fn
, "w+"))) {
985 if ((f
= fopen(fn
, "w"))) {
987 if (option_verbose
> 1)
988 ast_verbose(VERBOSE_PREFIX_2
"Saving '%s': ", fn
);
990 fprintf(f
, ";! Automatically generated configuration file\n");
991 if (strcmp(configfile
, fn
))
992 fprintf(f
, ";! Filename: %s (%s)\n", configfile
, fn
);
994 fprintf(f
, ";! Filename: %s\n", configfile
);
995 fprintf(f
, ";! Generator: %s\n", generator
);
996 fprintf(f
, ";! Creation Date: %s", date
);
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
)
1008 fprintf(f
, "[%s]", cat
->name
);
1009 for(cmt
= cat
->sameline
; cmt
; cmt
=cmt
->next
)
1011 fprintf(f
,"%s", cmt
->cmt
);
1017 for (cmt
= var
->precomments
; cmt
; cmt
=cmt
->next
)
1019 if (cmt
->cmt
[0] != ';' || cmt
->cmt
[1] != '!')
1020 fprintf(f
,"%s", cmt
->cmt
);
1023 fprintf(f
, "%s %s %s %s", var
->name
, (var
->object
? "=>" : "="), var
->value
, var
->sameline
->cmt
);
1025 fprintf(f
, "%s %s %s\n", var
->name
, (var
->object
? "=>" : "="), var
->value
);
1026 if (var
->blanklines
) {
1027 blanklines
= var
->blanklines
;
1028 while (blanklines
--)
1035 /* Put an empty line */
1040 if ((option_verbose
> 1) && !option_debug
)
1041 ast_verbose("Saved\n");
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
));
1053 static void clear_config_maps(void)
1055 struct ast_config_map
*map
;
1057 ast_mutex_lock(&config_lock
);
1059 while (config_maps
) {
1061 config_maps
= config_maps
->next
;
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
;
1073 length
= sizeof(*map
);
1074 length
+= strlen(name
) + 1;
1075 length
+= strlen(driver
) + 1;
1076 length
+= strlen(database
) + 1;
1078 length
+= strlen(table
) + 1;
1080 if (!(map
= ast_calloc(1, length
)))
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
);
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
);
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);
1115 ast_config_destroy(configtmp
);
1119 for (v
= ast_variable_browse(config
, "settings"); v
; v
= v
->next
) {
1121 driver
= strsep(&stringp
, ",");
1123 /* check if the database text starts with a double quote */
1124 if (*stringp
== '"') {
1126 database
= strsep(&stringp
, "\"");
1127 strsep(&stringp
, ",");
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
);
1140 if (!strcmp(v
->name
, "asterisk.conf")) {
1141 ast_log(LOG_WARNING
, "Cannot bind 'asterisk.conf'!\n");
1145 if (!strcmp(v
->name
, "logger.conf")) {
1146 ast_log(LOG_WARNING
, "Cannot bind 'logger.conf'!\n");
1150 if (!driver
|| !database
)
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");
1161 append_mapping(v
->name
, driver
, database
, table
);
1164 ast_config_destroy(config
);
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;
1177 for (ptr
= config_engine_list
; ptr
->next
; ptr
=ptr
->next
);
1181 ast_mutex_unlock(&config_lock
);
1182 ast_log(LOG_NOTICE
,"Registered Config Engine %s\n", new->name
);
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
) {
1196 last
->next
= ptr
->next
;
1198 config_engine_list
= ptr
->next
;
1204 ast_mutex_unlock(&config_lock
);
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
)) {
1220 ast_copy_string(database
, map
->database
, dbsiz
);
1222 ast_copy_string(table
, map
->table
? map
->table
: family
, tabsiz
);
1227 /* Check if the required driver (engine) exist */
1229 for (eng
= config_engine_list
; !ret
&& eng
; eng
= eng
->next
) {
1230 if (!strcasecmp(eng
->name
, map
->driver
))
1235 ast_mutex_unlock(&config_lock
);
1237 /* if we found a mapping, but the engine is not available, then issue a warning */
1239 ast_log(LOG_WARNING
, "Realtime mapping for '%s' found to engine '%s', but the engine is not available\n", map
->name
, map
->driver
);
1244 static struct ast_config_engine text_file_engine
= {
1246 .load_func
= config_text_file_load
,
1249 struct ast_config
*ast_config_internal_load(const char *filename
, struct ast_config
*cfg
, int withcomments
)
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
);
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
) {
1272 eng
= find_engine("global", db
, sizeof(db
), table
, sizeof(table
));
1273 if (eng
&& eng
->load_func
)
1278 result
= loader
->load_func(db
, table
, filename
, cfg
, withcomments
);
1281 result
->include_level
--;
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();
1295 result
= ast_config_internal_load(filename
, cfg
, 0);
1297 ast_config_destroy(cfg
);
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();
1311 result
= ast_config_internal_load(filename
, cfg
, 1);
1313 ast_config_destroy(cfg
);
1318 struct ast_variable
*ast_load_realtime(const char *family
, ...)
1320 struct ast_config_engine
*eng
;
1323 struct ast_variable
*res
=NULL
;
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
);
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);
1347 struct ast_config
*ast_load_realtime_multientry(const char *family
, ...)
1349 struct ast_config_engine
*eng
;
1352 struct ast_config
*res
=NULL
;
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
);
1364 int ast_update_realtime(const char *family
, const char *keyfield
, const char *lookup
, ...)
1366 struct ast_config_engine
*eng
;
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
);
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
);
1399 ast_mutex_unlock(&config_lock
);
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
,
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
));