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";
66 /*! \brief Structure to keep comments for rewriting configuration files */
68 struct ast_comment
*next
;
74 static void CB_INIT(char **comment_buffer
, int *comment_buffer_size
, char **lline_buffer
, int *lline_buffer_size
)
76 if (!(*comment_buffer
)) {
77 *comment_buffer
= ast_malloc(CB_INCR
);
78 if (!(*comment_buffer
))
80 (*comment_buffer
)[0] = 0;
81 *comment_buffer_size
= CB_INCR
;
82 *lline_buffer
= ast_malloc(CB_INCR
);
85 (*lline_buffer
)[0] = 0;
86 *lline_buffer_size
= CB_INCR
;
88 (*comment_buffer
)[0] = 0;
89 (*lline_buffer
)[0] = 0;
93 static void CB_ADD(char **comment_buffer
, int *comment_buffer_size
, char *str
)
95 int rem
= *comment_buffer_size
- strlen(*comment_buffer
) - 1;
96 int siz
= strlen(str
);
98 *comment_buffer
= ast_realloc(*comment_buffer
, *comment_buffer_size
+ CB_INCR
+ siz
+ 1);
99 if (!(*comment_buffer
))
101 *comment_buffer_size
+= CB_INCR
+siz
+1;
103 strcat(*comment_buffer
,str
);
106 static void CB_ADD_LEN(char **comment_buffer
, int *comment_buffer_size
, char *str
, int len
)
108 int cbl
= strlen(*comment_buffer
) + 1;
109 int rem
= *comment_buffer_size
- cbl
;
111 *comment_buffer
= ast_realloc(*comment_buffer
, *comment_buffer_size
+ CB_INCR
+ len
+ 1);
112 if (!(*comment_buffer
))
114 *comment_buffer_size
+= CB_INCR
+len
+1;
116 strncat(*comment_buffer
,str
,len
);
117 (*comment_buffer
)[cbl
+len
-1] = 0;
120 static void LLB_ADD(char **lline_buffer
, int *lline_buffer_size
, char *str
)
122 int rem
= *lline_buffer_size
- strlen(*lline_buffer
) - 1;
123 int siz
= strlen(str
);
125 *lline_buffer
= ast_realloc(*lline_buffer
, *lline_buffer_size
+ CB_INCR
+ siz
+ 1);
126 if (!(*lline_buffer
))
128 *lline_buffer_size
+= CB_INCR
+ siz
+ 1;
130 strcat(*lline_buffer
,str
);
133 static void CB_RESET(char **comment_buffer
, char **lline_buffer
)
135 (*comment_buffer
)[0] = 0;
136 (*lline_buffer
)[0] = 0;
140 static struct ast_comment
*ALLOC_COMMENT(const char *buffer
)
142 struct ast_comment
*x
= ast_calloc(1,sizeof(struct ast_comment
)+strlen(buffer
)+1);
143 strcpy(x
->cmt
, buffer
);
148 static struct ast_config_map
{
149 struct ast_config_map
*next
;
155 } *config_maps
= NULL
;
157 AST_MUTEX_DEFINE_STATIC(config_lock
);
158 static struct ast_config_engine
*config_engine_list
;
160 #define MAX_INCLUDE_LEVEL 10
162 struct ast_category_template_instance
{
163 char name
[80]; /* redundant? */
164 const struct ast_category
*inst
;
165 AST_LIST_ENTRY(ast_category_template_instance
) next
;
168 struct ast_category
{
170 int ignored
; /*!< do not let user of the config see this category */
172 AST_LIST_HEAD_NOLOCK(template_instance_list
, ast_category_template_instance
) template_instances
;
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 static void ast_destroy_comments(struct ast_category
*cat
)
352 struct ast_comment
*n
, *p
;
353 for (p
=cat
->precomments
; p
; p
=n
) {
357 for (p
=cat
->sameline
; p
; p
=n
) {
361 cat
->precomments
= NULL
;
362 cat
->sameline
= NULL
;
365 static void ast_destroy_template_list(struct ast_category
*cat
)
367 struct ast_category_template_instance
*x
;
368 AST_LIST_TRAVERSE_SAFE_BEGIN(&cat
->template_instances
, x
, next
) {
369 AST_LIST_REMOVE_CURRENT(&cat
->template_instances
, next
);
372 AST_LIST_TRAVERSE_SAFE_END
;
375 void ast_category_destroy(struct ast_category
*cat
)
377 ast_variables_destroy(cat
->root
);
378 ast_destroy_comments(cat
);
379 ast_destroy_template_list(cat
);
383 static struct ast_category
*next_available_category(struct ast_category
*cat
)
385 for (; cat
&& cat
->ignored
; cat
= cat
->next
);
390 struct ast_variable
*ast_category_root(struct ast_config
*config
, char *cat
)
392 struct ast_category
*category
= ast_category_get(config
, cat
);
394 return category
->root
;
398 char *ast_category_browse(struct ast_config
*config
, const char *prev
)
400 struct ast_category
*cat
= NULL
;
402 if (prev
&& config
->last_browse
&& (config
->last_browse
->name
== prev
))
403 cat
= config
->last_browse
->next
;
404 else if (!prev
&& config
->root
)
407 for (cat
= config
->root
; cat
; cat
= cat
->next
) {
408 if (cat
->name
== prev
) {
414 for (cat
= config
->root
; cat
; cat
= cat
->next
) {
415 if (!strcasecmp(cat
->name
, prev
)) {
424 cat
= next_available_category(cat
);
426 config
->last_browse
= cat
;
427 return (cat
) ? cat
->name
: NULL
;
430 struct ast_variable
*ast_category_detach_variables(struct ast_category
*cat
)
432 struct ast_variable
*v
;
441 void ast_category_rename(struct ast_category
*cat
, const char *name
)
443 ast_copy_string(cat
->name
, name
, sizeof(cat
->name
));
446 static void inherit_category(struct ast_category
*new, const struct ast_category
*base
)
448 struct ast_variable
*var
;
449 struct ast_category_template_instance
*x
= ast_calloc(1,sizeof(struct ast_category_template_instance
));
450 strcpy(x
->name
, base
->name
);
452 AST_LIST_INSERT_TAIL(&new->template_instances
, x
, next
);
453 for (var
= base
->root
; var
; var
= var
->next
)
454 ast_variable_append(new, variable_clone(var
));
457 struct ast_config
*ast_config_new(void)
459 struct ast_config
*config
;
461 if ((config
= ast_calloc(1, sizeof(*config
))))
462 config
->max_include_level
= MAX_INCLUDE_LEVEL
;
466 int ast_variable_delete(struct ast_category
*category
, char *variable
, char *match
)
468 struct ast_variable
*cur
, *prev
=NULL
, *curn
;
470 cur
= category
->root
;
472 if (cur
->name
== variable
) {
474 prev
->next
= cur
->next
;
475 if (cur
== category
->last
)
476 category
->last
= prev
;
478 category
->root
= cur
->next
;
479 if (cur
== category
->last
)
480 category
->last
= NULL
;
483 ast_variables_destroy(cur
);
491 cur
= category
->root
;
494 if (!strcasecmp(cur
->name
, variable
) && (ast_strlen_zero(match
) || !strcasecmp(cur
->value
, match
))) {
496 prev
->next
= cur
->next
;
497 if (cur
== category
->last
)
498 category
->last
= prev
;
500 category
->root
= cur
->next
;
501 if (cur
== category
->last
)
502 category
->last
= NULL
;
505 ast_variables_destroy(cur
);
515 int ast_variable_update(struct ast_category
*category
, const char *variable
,
516 const char *value
, const char *match
, unsigned int object
)
518 struct ast_variable
*cur
, *prev
=NULL
, *newer
;
520 if (!(newer
= ast_variable_new(variable
, value
)))
523 newer
->object
= object
;
525 for (cur
= category
->root
; cur
; prev
= cur
, cur
= cur
->next
) {
526 if (strcasecmp(cur
->name
, variable
) ||
527 (!ast_strlen_zero(match
) && strcasecmp(cur
->value
, match
)))
530 newer
->next
= cur
->next
;
531 newer
->object
= cur
->object
|| object
;
535 category
->root
= newer
;
536 if (category
->last
== cur
)
537 category
->last
= newer
;
540 ast_variables_destroy(cur
);
548 category
->root
= newer
;
553 int ast_category_delete(struct ast_config
*cfg
, char *category
)
555 struct ast_category
*prev
=NULL
, *cat
;
558 if (cat
->name
== category
) {
560 prev
->next
= cat
->next
;
561 if (cat
== cfg
->last
)
564 cfg
->root
= cat
->next
;
565 if (cat
== cfg
->last
)
568 ast_category_destroy(cat
);
578 if (!strcasecmp(cat
->name
, category
)) {
580 prev
->next
= cat
->next
;
581 if (cat
== cfg
->last
)
584 cfg
->root
= cat
->next
;
585 if (cat
== cfg
->last
)
588 ast_category_destroy(cat
);
597 void ast_config_destroy(struct ast_config
*cfg
)
599 struct ast_category
*cat
, *catn
;
608 ast_category_destroy(catn
);
613 struct ast_category
*ast_config_get_current_category(const struct ast_config
*cfg
)
618 void ast_config_set_current_category(struct ast_config
*cfg
, const struct ast_category
*cat
)
620 /* cast below is just to silence compiler warning about dropping "const" */
621 cfg
->current
= (struct ast_category
*) cat
;
624 static int process_text_line(struct ast_config
*cfg
, struct ast_category
**cat
, char *buf
, int lineno
, const char *configfile
, int withcomments
,
625 char **comment_buffer
, int *comment_buffer_size
, char **lline_buffer
, int *lline_buffer_size
)
629 struct ast_variable
*v
;
630 char cmd
[512], exec_file
[512];
631 int object
, do_exec
, do_include
;
633 /* Actually parse the entry */
635 struct ast_category
*newcat
= NULL
;
638 /* A category header */
639 c
= strchr(cur
, ']');
641 ast_log(LOG_WARNING
, "parse error: no closing ']', line %d of %s\n", lineno
, configfile
);
649 if (!(*cat
= newcat
= ast_category_new(catname
))) {
653 if (withcomments
&& *comment_buffer
&& (*comment_buffer
)[0] ) {
654 newcat
->precomments
= ALLOC_COMMENT(*comment_buffer
);
656 if (withcomments
&& *lline_buffer
&& (*lline_buffer
)[0] ) {
657 newcat
->sameline
= ALLOC_COMMENT(*lline_buffer
);
660 CB_RESET(comment_buffer
, lline_buffer
);
662 /* If there are options or categories to inherit from, process them now */
664 if (!(cur
= strchr(c
, ')'))) {
665 ast_log(LOG_WARNING
, "parse error: no closing ')', line %d of %s\n", lineno
, configfile
);
669 while ((cur
= strsep(&c
, ","))) {
670 if (!strcasecmp(cur
, "!")) {
672 } else if (!strcasecmp(cur
, "+")) {
673 *cat
= category_get(cfg
, catname
, 1);
676 ast_category_destroy(newcat
);
677 ast_log(LOG_WARNING
, "Category addition requested, but category '%s' does not exist, line %d of %s\n", catname
, lineno
, configfile
);
681 move_variables(newcat
, *cat
);
682 ast_category_destroy(newcat
);
686 struct ast_category
*base
;
688 base
= category_get(cfg
, cur
, 1);
690 ast_log(LOG_WARNING
, "Inheritance requested, but category '%s' does not exist, line %d of %s\n", cur
, lineno
, configfile
);
693 inherit_category(*cat
, base
);
698 ast_category_append(cfg
, *cat
);
699 } else if (cur
[0] == '#') {
703 while(*c
&& (*c
> 32)) c
++;
706 /* Find real argument */
707 c
= ast_skip_blanks(c
+ 1);
712 do_include
= !strcasecmp(cur
, "include");
714 do_exec
= !strcasecmp(cur
, "exec");
717 if (do_exec
&& !ast_opt_exec_includes
) {
718 ast_log(LOG_WARNING
, "Cannot perform #exec unless execincludes option is enabled in asterisk.conf (options section)!\n");
721 if (do_include
|| do_exec
) {
723 /* Strip off leading and trailing "'s and <>'s */
724 while((*c
== '<') || (*c
== '>') || (*c
== '\"')) c
++;
725 /* Get rid of leading mess */
727 while (!ast_strlen_zero(cur
)) {
728 c
= cur
+ strlen(cur
) - 1;
729 if ((*c
== '>') || (*c
== '<') || (*c
== '\"'))
734 /* #exec </path/to/executable>
735 We create a tmp file, then we #include it, then we delete it. */
737 snprintf(exec_file
, sizeof(exec_file
), "/var/tmp/exec.%d.%ld", (int)time(NULL
), (long)pthread_self());
738 snprintf(cmd
, sizeof(cmd
), "%s > %s 2>&1", cur
, exec_file
);
739 ast_safe_system(cmd
);
744 do_include
= ast_config_internal_load(cur
, cfg
, withcomments
) ? 1 : 0;
745 if(!ast_strlen_zero(exec_file
))
748 ast_log(LOG_ERROR
, "*********************************************************\n");
749 ast_log(LOG_ERROR
, "*********** YOU SHOULD REALLY READ THIS ERROR ***********\n");
750 ast_log(LOG_ERROR
, "Future versions of Asterisk will treat a #include of a "
751 "file that does not exist as an error, and will fail to "
752 "load that configuration file. Please ensure that the "
753 "file '%s' exists, even if it is empty.\n", cur
);
754 ast_log(LOG_ERROR
, "*********** YOU SHOULD REALLY READ THIS ERROR ***********\n");
755 ast_log(LOG_ERROR
, "*********************************************************\n");
760 ast_log(LOG_WARNING
, "Directive '#%s' needs an argument (%s) at line %d of %s\n",
761 do_exec
? "exec" : "include",
762 do_exec
? "/path/to/executable" : "filename",
768 ast_log(LOG_WARNING
, "Unknown directive '#%s' at line %d of %s\n", cur
, lineno
, configfile
);
770 /* Just a line (variable = value) */
773 "parse error: No category context for line %d of %s\n", lineno
, configfile
);
776 c
= strchr(cur
, '=');
786 if ((v
= ast_variable_new(ast_strip(cur
), ast_strip(c
)))) {
789 /* Put and reset comments */
791 ast_variable_append(*cat
, v
);
793 if (withcomments
&& *comment_buffer
&& (*comment_buffer
)[0] ) {
794 v
->precomments
= ALLOC_COMMENT(*comment_buffer
);
796 if (withcomments
&& *lline_buffer
&& (*lline_buffer
)[0] ) {
797 v
->sameline
= ALLOC_COMMENT(*lline_buffer
);
800 CB_RESET(comment_buffer
, lline_buffer
);
806 ast_log(LOG_WARNING
, "No '=' (equal sign) in line %d of %s\n", lineno
, configfile
);
812 static struct ast_config
*config_text_file_load(const char *database
, const char *table
, const char *filename
, struct ast_config
*cfg
, int withcomments
)
815 #if defined(LOW_MEMORY)
820 char *new_buf
, *comment_p
, *process_buf
;
823 int comment
= 0, nest
[MAX_NESTED_COMMENTS
];
824 struct ast_category
*cat
= NULL
;
827 /*! Growable string buffer */
828 char *comment_buffer
=0; /*!< this will be a comment collector.*/
829 int comment_buffer_size
=0; /*!< the amount of storage so far alloc'd for the comment_buffer */
831 char *lline_buffer
=0; /*!< A buffer for stuff behind the ; */
832 int lline_buffer_size
=0;
835 cat
= ast_config_get_current_category(cfg
);
837 if (filename
[0] == '/') {
838 ast_copy_string(fn
, filename
, sizeof(fn
));
840 snprintf(fn
, sizeof(fn
), "%s/%s", (char *)ast_config_AST_CONFIG_DIR
, filename
);
844 CB_INIT(&comment_buffer
, &comment_buffer_size
, &lline_buffer
, &lline_buffer_size
);
845 if (!lline_buffer
|| !comment_buffer
) {
846 ast_log(LOG_ERROR
, "Failed to initialize the comment buffer!\n");
850 #ifdef AST_INCLUDE_GLOB
854 globbuf
.gl_offs
= 0; /* initialize it to silence gcc */
856 glob_ret
= glob(fn
, GLOB_NOCHECK
, NULL
, &globbuf
);
858 glob_ret
= glob(fn
, GLOB_NOMAGIC
|GLOB_BRACE
, NULL
, &globbuf
);
860 if (glob_ret
== GLOB_NOSPACE
)
862 "Glob Expansion of pattern '%s' failed: Not enough memory\n", fn
);
863 else if (glob_ret
== GLOB_ABORTED
)
865 "Glob Expansion of pattern '%s' failed: Read error\n", fn
);
867 /* loop over expanded files */
869 for (i
=0; i
<globbuf
.gl_pathc
; i
++) {
870 ast_copy_string(fn
, globbuf
.gl_pathv
[i
], sizeof(fn
));
873 if (stat(fn
, &statbuf
))
876 if (!S_ISREG(statbuf
.st_mode
)) {
877 ast_log(LOG_WARNING
, "'%s' is not a regular file, ignoring\n", fn
);
880 if (option_verbose
> 1) {
881 ast_verbose(VERBOSE_PREFIX_2
"Parsing '%s': ", fn
);
884 if (!(f
= fopen(fn
, "r"))) {
886 ast_log(LOG_DEBUG
, "No file to parse: %s\n", fn
);
887 if (option_verbose
> 1)
888 ast_verbose( "Not found (%s)\n", strerror(errno
));
893 ast_log(LOG_DEBUG
, "Parsing %s\n", fn
);
894 if (option_verbose
> 1)
895 ast_verbose("Found\n");
898 if (fgets(buf
, sizeof(buf
), f
)) {
899 if ( withcomments
) {
900 CB_ADD(&comment_buffer
, &comment_buffer_size
, lline_buffer
); /* add the current lline buffer to the comment buffer */
901 lline_buffer
[0] = 0; /* erase the lline buffer */
910 while ((comment_p
= strchr(new_buf
, COMMENT_META
))) {
911 if ((comment_p
> new_buf
) && (*(comment_p
-1) == '\\')) {
912 /* Escaped semicolons aren't comments. */
913 new_buf
= comment_p
+ 1;
914 } else if(comment_p
[1] == COMMENT_TAG
&& comment_p
[2] == COMMENT_TAG
&& (comment_p
[3] != '-')) {
915 /* Meta-Comment start detected ";--" */
916 if (comment
< MAX_NESTED_COMMENTS
) {
918 new_buf
= comment_p
+ 3;
920 nest
[comment
-1] = lineno
;
922 ast_log(LOG_ERROR
, "Maximum nest limit of %d reached.\n", MAX_NESTED_COMMENTS
);
924 } else if ((comment_p
>= new_buf
+ 2) &&
925 (*(comment_p
- 1) == COMMENT_TAG
) &&
926 (*(comment_p
- 2) == COMMENT_TAG
)) {
927 /* Meta-Comment end detected */
929 new_buf
= comment_p
+ 1;
931 /* Back to non-comment now */
933 /* Actually have to move what's left over the top, then continue */
935 oldptr
= process_buf
+ strlen(process_buf
);
936 if ( withcomments
) {
937 CB_ADD(&comment_buffer
, &comment_buffer_size
, ";");
938 CB_ADD_LEN(&comment_buffer
, &comment_buffer_size
, oldptr
+1, new_buf
-oldptr
-1);
941 memmove(oldptr
, new_buf
, strlen(new_buf
) + 1);
944 process_buf
= new_buf
;
948 /* If ; is found, and we are not nested in a comment,
949 we immediately stop all comment processing */
950 if ( withcomments
) {
951 LLB_ADD(&lline_buffer
, &lline_buffer_size
, comment_p
);
956 new_buf
= comment_p
+ 1;
959 if( withcomments
&& comment
&& !process_buf
)
961 CB_ADD(&comment_buffer
, &comment_buffer_size
, buf
); /* the whole line is a comment, store it */
965 char *buf
= ast_strip(process_buf
);
966 if (!ast_strlen_zero(buf
)) {
967 if (process_text_line(cfg
, &cat
, buf
, lineno
, fn
, withcomments
, &comment_buffer
, &comment_buffer_size
, &lline_buffer
, &lline_buffer_size
)) {
978 ast_log(LOG_WARNING
,"Unterminated comment detected beginning on line %d\n", nest
[comment
- 1]);
980 #ifdef AST_INCLUDE_GLOB
989 if (cfg
&& cfg
->include_level
== 1 && withcomments
&& comment_buffer
) {
990 free(comment_buffer
);
992 comment_buffer
= NULL
;
994 comment_buffer_size
= 0;
995 lline_buffer_size
= 0;
1004 int config_text_file_save(const char *configfile
, const struct ast_config
*cfg
, const char *generator
)
1008 char fn
[256], fntmp
[256];
1011 struct ast_variable
*var
;
1012 struct ast_category
*cat
;
1013 struct ast_comment
*cmt
;
1016 int stat_result
= 0;
1018 if (configfile
[0] == '/') {
1019 snprintf(fntmp
, sizeof(fntmp
), "%s.XXXXXX", configfile
);
1020 ast_copy_string(fn
, configfile
, sizeof(fn
));
1022 snprintf(fntmp
, sizeof(fntmp
), "%s/%s.XXXXXX", ast_config_AST_CONFIG_DIR
, configfile
);
1023 snprintf(fn
, sizeof(fn
), "%s/%s", ast_config_AST_CONFIG_DIR
, configfile
);
1026 ast_copy_string(date
, ctime(&t
), sizeof(date
));
1027 if ((fd
= mkstemp(fntmp
)) > 0 && (f
= fdopen(fd
, "w")) != NULL
) {
1028 if (option_verbose
> 1)
1029 ast_verbose(VERBOSE_PREFIX_2
"Saving '%s': ", fn
);
1031 fprintf(f
, ";! Automatically generated configuration file\n");
1032 if (strcmp(configfile
, fn
))
1033 fprintf(f
, ";! Filename: %s (%s)\n", configfile
, fn
);
1035 fprintf(f
, ";! Filename: %s\n", configfile
);
1036 fprintf(f
, ";! Generator: %s\n", generator
);
1037 fprintf(f
, ";! Creation Date: %s", date
);
1041 /* Dump section with any appropriate comment */
1042 for (cmt
= cat
->precomments
; cmt
; cmt
=cmt
->next
)
1044 if (cmt
->cmt
[0] != ';' || cmt
->cmt
[1] != '!')
1045 fprintf(f
,"%s", cmt
->cmt
);
1047 if (!cat
->precomments
)
1049 fprintf(f
, "[%s]", cat
->name
);
1050 if (cat
->ignored
|| !AST_LIST_EMPTY(&cat
->template_instances
)) {
1055 if (cat
->ignored
&& !AST_LIST_EMPTY(&cat
->template_instances
)) {
1058 if (!AST_LIST_EMPTY(&cat
->template_instances
)) {
1059 struct ast_category_template_instance
*x
;
1060 AST_LIST_TRAVERSE(&cat
->template_instances
, x
, next
) {
1061 fprintf(f
,"%s",x
->name
);
1062 if (x
!= AST_LIST_LAST(&cat
->template_instances
))
1068 for(cmt
= cat
->sameline
; cmt
; cmt
=cmt
->next
)
1070 fprintf(f
,"%s", cmt
->cmt
);
1076 struct ast_category_template_instance
*x
;
1077 struct ast_variable
*v2
;
1079 AST_LIST_TRAVERSE(&cat
->template_instances
, x
, next
) {
1081 for (v2
= x
->inst
->root
; v2
; v2
= v2
->next
) {
1082 if (!strcasecmp(var
->name
, v2
->name
))
1085 if (v2
&& v2
->value
&& !strcmp(v2
->value
, var
->value
)) {
1094 for (cmt
= var
->precomments
; cmt
; cmt
=cmt
->next
)
1096 if (cmt
->cmt
[0] != ';' || cmt
->cmt
[1] != '!')
1097 fprintf(f
,"%s", cmt
->cmt
);
1100 fprintf(f
, "%s %s %s %s", var
->name
, (var
->object
? "=>" : "="), var
->value
, var
->sameline
->cmt
);
1102 fprintf(f
, "%s %s %s\n", var
->name
, (var
->object
? "=>" : "="), var
->value
);
1103 if (var
->blanklines
) {
1104 blanklines
= var
->blanklines
;
1105 while (blanklines
--)
1112 /* Put an empty line */
1117 if ((option_verbose
> 1) && !option_debug
)
1118 ast_verbose("Saved\n");
1121 ast_log(LOG_DEBUG
, "Unable to open for writing: %s (%s)\n", fn
, strerror(errno
));
1122 if (option_verbose
> 1)
1123 ast_verbose(VERBOSE_PREFIX_2
"Unable to write %s (%s)", fn
, strerror(errno
));
1128 if (!(stat_result
= stat(fn
, &s
))) {
1129 fchmod(fd
, s
.st_mode
);
1132 if ((!stat_result
&& unlink(fn
)) || link(fntmp
, fn
)) {
1134 ast_log(LOG_DEBUG
, "Unable to open for writing: %s (%s)\n", fn
, strerror(errno
));
1135 if (option_verbose
> 1)
1136 ast_verbose(VERBOSE_PREFIX_2
"Unable to write %s (%s)", fn
, strerror(errno
));
1144 static void clear_config_maps(void)
1146 struct ast_config_map
*map
;
1148 ast_mutex_lock(&config_lock
);
1150 while (config_maps
) {
1152 config_maps
= config_maps
->next
;
1156 ast_mutex_unlock(&config_lock
);
1159 static int append_mapping(char *name
, char *driver
, char *database
, char *table
)
1161 struct ast_config_map
*map
;
1164 length
= sizeof(*map
);
1165 length
+= strlen(name
) + 1;
1166 length
+= strlen(driver
) + 1;
1167 length
+= strlen(database
) + 1;
1169 length
+= strlen(table
) + 1;
1171 if (!(map
= ast_calloc(1, length
)))
1174 map
->name
= map
->stuff
;
1175 strcpy(map
->name
, name
);
1176 map
->driver
= map
->name
+ strlen(map
->name
) + 1;
1177 strcpy(map
->driver
, driver
);
1178 map
->database
= map
->driver
+ strlen(map
->driver
) + 1;
1179 strcpy(map
->database
, database
);
1181 map
->table
= map
->database
+ strlen(map
->database
) + 1;
1182 strcpy(map
->table
, table
);
1184 map
->next
= config_maps
;
1186 if (option_verbose
> 1)
1187 ast_verbose(VERBOSE_PREFIX_2
"Binding %s to %s/%s/%s\n",
1188 map
->name
, map
->driver
, map
->database
, map
->table
? map
->table
: map
->name
);
1194 int read_config_maps(void)
1196 struct ast_config
*config
, *configtmp
;
1197 struct ast_variable
*v
;
1198 char *driver
, *table
, *database
, *stringp
, *tmp
;
1200 clear_config_maps();
1202 configtmp
= ast_config_new();
1203 configtmp
->max_include_level
= 1;
1204 config
= ast_config_internal_load(extconfig_conf
, configtmp
, 0);
1206 ast_config_destroy(configtmp
);
1210 for (v
= ast_variable_browse(config
, "settings"); v
; v
= v
->next
) {
1212 driver
= strsep(&stringp
, ",");
1214 if ((tmp
= strchr(stringp
, '\"')))
1217 /* check if the database text starts with a double quote */
1218 if (*stringp
== '"') {
1220 database
= strsep(&stringp
, "\"");
1221 strsep(&stringp
, ",");
1223 /* apparently this text has no quotes */
1224 database
= strsep(&stringp
, ",");
1227 table
= strsep(&stringp
, ",");
1229 if (!strcmp(v
->name
, extconfig_conf
)) {
1230 ast_log(LOG_WARNING
, "Cannot bind '%s'!\n", extconfig_conf
);
1234 if (!strcmp(v
->name
, "asterisk.conf")) {
1235 ast_log(LOG_WARNING
, "Cannot bind 'asterisk.conf'!\n");
1239 if (!strcmp(v
->name
, "logger.conf")) {
1240 ast_log(LOG_WARNING
, "Cannot bind 'logger.conf'!\n");
1244 if (!driver
|| !database
)
1246 if (!strcasecmp(v
->name
, "sipfriends")) {
1247 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");
1248 append_mapping("sipusers", driver
, database
, table
? table
: "sipfriends");
1249 append_mapping("sippeers", driver
, database
, table
? table
: "sipfriends");
1250 } else if (!strcasecmp(v
->name
, "iaxfriends")) {
1251 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");
1252 append_mapping("iaxusers", driver
, database
, table
? table
: "iaxfriends");
1253 append_mapping("iaxpeers", driver
, database
, table
? table
: "iaxfriends");
1255 append_mapping(v
->name
, driver
, database
, table
);
1258 ast_config_destroy(config
);
1262 int ast_config_engine_register(struct ast_config_engine
*new)
1264 struct ast_config_engine
*ptr
;
1266 ast_mutex_lock(&config_lock
);
1268 if (!config_engine_list
) {
1269 config_engine_list
= new;
1271 for (ptr
= config_engine_list
; ptr
->next
; ptr
=ptr
->next
);
1275 ast_mutex_unlock(&config_lock
);
1276 ast_log(LOG_NOTICE
,"Registered Config Engine %s\n", new->name
);
1281 int ast_config_engine_deregister(struct ast_config_engine
*del
)
1283 struct ast_config_engine
*ptr
, *last
=NULL
;
1285 ast_mutex_lock(&config_lock
);
1287 for (ptr
= config_engine_list
; ptr
; ptr
=ptr
->next
) {
1290 last
->next
= ptr
->next
;
1292 config_engine_list
= ptr
->next
;
1298 ast_mutex_unlock(&config_lock
);
1303 /*! \brief Find realtime engine for realtime family */
1304 static struct ast_config_engine
*find_engine(const char *family
, char *database
, int dbsiz
, char *table
, int tabsiz
)
1306 struct ast_config_engine
*eng
, *ret
= NULL
;
1307 struct ast_config_map
*map
;
1309 ast_mutex_lock(&config_lock
);
1311 for (map
= config_maps
; map
; map
= map
->next
) {
1312 if (!strcasecmp(family
, map
->name
)) {
1314 ast_copy_string(database
, map
->database
, dbsiz
);
1316 ast_copy_string(table
, map
->table
? map
->table
: family
, tabsiz
);
1321 /* Check if the required driver (engine) exist */
1323 for (eng
= config_engine_list
; !ret
&& eng
; eng
= eng
->next
) {
1324 if (!strcasecmp(eng
->name
, map
->driver
))
1329 ast_mutex_unlock(&config_lock
);
1331 /* if we found a mapping, but the engine is not available, then issue a warning */
1333 ast_log(LOG_WARNING
, "Realtime mapping for '%s' found to engine '%s', but the engine is not available\n", map
->name
, map
->driver
);
1338 static struct ast_config_engine text_file_engine
= {
1340 .load_func
= config_text_file_load
,
1343 struct ast_config
*ast_config_internal_load(const char *filename
, struct ast_config
*cfg
, int withcomments
)
1347 struct ast_config_engine
*loader
= &text_file_engine
;
1348 struct ast_config
*result
;
1350 /* The config file itself bumps include_level by 1 */
1351 if (cfg
->max_include_level
> 0 && cfg
->include_level
== cfg
->max_include_level
+ 1) {
1352 ast_log(LOG_WARNING
, "Maximum Include level (%d) exceeded\n", cfg
->max_include_level
);
1356 cfg
->include_level
++;
1358 if (strcmp(filename
, extconfig_conf
) && strcmp(filename
, "asterisk.conf") && config_engine_list
) {
1359 struct ast_config_engine
*eng
;
1361 eng
= find_engine(filename
, db
, sizeof(db
), table
, sizeof(table
));
1364 if (eng
&& eng
->load_func
) {
1367 eng
= find_engine("global", db
, sizeof(db
), table
, sizeof(table
));
1368 if (eng
&& eng
->load_func
)
1373 result
= loader
->load_func(db
, table
, filename
, cfg
, withcomments
);
1376 result
->include_level
--;
1378 cfg
->include_level
--;
1383 struct ast_config
*ast_config_load(const char *filename
)
1385 struct ast_config
*cfg
;
1386 struct ast_config
*result
;
1388 cfg
= ast_config_new();
1392 result
= ast_config_internal_load(filename
, cfg
, 0);
1394 ast_config_destroy(cfg
);
1399 struct ast_config
*ast_config_load_with_comments(const char *filename
)
1401 struct ast_config
*cfg
;
1402 struct ast_config
*result
;
1404 cfg
= ast_config_new();
1408 result
= ast_config_internal_load(filename
, cfg
, 1);
1410 ast_config_destroy(cfg
);
1415 struct ast_variable
*ast_load_realtime(const char *family
, ...)
1417 struct ast_config_engine
*eng
;
1420 struct ast_variable
*res
=NULL
;
1423 va_start(ap
, family
);
1424 eng
= find_engine(family
, db
, sizeof(db
), table
, sizeof(table
));
1425 if (eng
&& eng
->realtime_func
)
1426 res
= eng
->realtime_func(db
, table
, ap
);
1432 /*! \brief Check if realtime engine is configured for family */
1433 int ast_check_realtime(const char *family
)
1435 struct ast_config_engine
*eng
;
1437 eng
= find_engine(family
, NULL
, 0, NULL
, 0);
1444 struct ast_config
*ast_load_realtime_multientry(const char *family
, ...)
1446 struct ast_config_engine
*eng
;
1449 struct ast_config
*res
=NULL
;
1452 va_start(ap
, family
);
1453 eng
= find_engine(family
, db
, sizeof(db
), table
, sizeof(table
));
1454 if (eng
&& eng
->realtime_multi_func
)
1455 res
= eng
->realtime_multi_func(db
, table
, ap
);
1461 int ast_update_realtime(const char *family
, const char *keyfield
, const char *lookup
, ...)
1463 struct ast_config_engine
*eng
;
1469 va_start(ap
, lookup
);
1470 eng
= find_engine(family
, db
, sizeof(db
), table
, sizeof(table
));
1471 if (eng
&& eng
->update_func
)
1472 res
= eng
->update_func(db
, table
, keyfield
, lookup
, ap
);
1478 static int config_command(int fd
, int argc
, char **argv
)
1480 struct ast_config_engine
*eng
;
1481 struct ast_config_map
*map
;
1483 ast_mutex_lock(&config_lock
);
1485 ast_cli(fd
, "\n\n");
1486 for (eng
= config_engine_list
; eng
; eng
= eng
->next
) {
1487 ast_cli(fd
, "\nConfig Engine: %s\n", eng
->name
);
1488 for (map
= config_maps
; map
; map
= map
->next
)
1489 if (!strcasecmp(map
->driver
, eng
->name
)) {
1490 ast_cli(fd
, "===> %s (db=%s, table=%s)\n", map
->name
, map
->database
,
1491 map
->table
? map
->table
: map
->name
);
1496 ast_mutex_unlock(&config_lock
);
1501 static char show_config_help
[] =
1502 "Usage: core show config mappings\n"
1503 " Shows the filenames to config engines.\n";
1505 static struct ast_cli_entry cli_show_config_mappings_deprecated
= {
1506 { "show", "config", "mappings", NULL
},
1507 config_command
, NULL
,
1510 static struct ast_cli_entry cli_config
[] = {
1511 { { "core", "show", "config", "mappings", NULL
},
1512 config_command
, "Display config mappings (file names to config engines)",
1513 show_config_help
, NULL
, &cli_show_config_mappings_deprecated
},
1516 int register_config_cli()
1518 ast_cli_register_multiple(cli_config
, sizeof(cli_config
) / sizeof(struct ast_cli_entry
));