add ExtenSpy variant of ChanSpy
[asterisk-bristuff.git] / config.c
blob2cf5385bfb1ad47293b967506bcae9557b28b2aa
1 /*
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.
19 /*! \file
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
29 #include "asterisk.h"
31 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
33 #include <stdio.h>
34 #include <unistd.h>
35 #include <stdlib.h>
36 #include <string.h>
37 #include <errno.h>
38 #include <time.h>
39 #include <sys/stat.h>
40 #define AST_INCLUDE_GLOB 1
41 #ifdef AST_INCLUDE_GLOB
42 #if defined(__Darwin__) || defined(__CYGWIN__)
43 #define GLOB_ABORTED GLOB_ABEND
44 #endif
45 # include <glob.h>
46 #endif
48 #include "asterisk/config.h"
49 #include "asterisk/cli.h"
50 #include "asterisk/lock.h"
51 #include "asterisk/options.h"
52 #include "asterisk/logger.h"
53 #include "asterisk/utils.h"
54 #include "asterisk/channel.h"
55 #include "asterisk/app.h"
57 #define MAX_NESTED_COMMENTS 128
58 #define COMMENT_START ";--"
59 #define COMMENT_END "--;"
60 #define COMMENT_META ';'
61 #define COMMENT_TAG '-'
63 static char *extconfig_conf = "extconfig.conf";
65 static struct ast_config_map {
66 struct ast_config_map *next;
67 char *name;
68 char *driver;
69 char *database;
70 char *table;
71 char stuff[0];
72 } *config_maps = NULL;
74 AST_MUTEX_DEFINE_STATIC(config_lock);
75 static struct ast_config_engine *config_engine_list;
77 #define MAX_INCLUDE_LEVEL 10
79 struct ast_comment {
80 struct ast_comment *next;
81 char cmt[0];
84 struct ast_category {
85 char name[80];
86 int ignored; /* do not let user of the config see this category */
87 struct ast_variable *root;
88 struct ast_variable *last;
89 struct ast_category *next;
92 struct ast_config {
93 struct ast_category *root;
94 struct ast_category *last;
95 struct ast_category *current;
96 struct ast_category *last_browse; /* used to cache the last category supplied via category_browse */
97 int include_level;
98 int max_include_level;
101 struct ast_variable *ast_variable_new(const char *name, const char *value)
103 struct ast_variable *variable;
104 int name_len = strlen(name) + 1;
106 if ((variable = ast_calloc(1, name_len + strlen(value) + 1 + sizeof(*variable)))) {
107 variable->name = variable->stuff;
108 variable->value = variable->stuff + name_len;
109 strcpy(variable->name,name);
110 strcpy(variable->value,value);
113 return variable;
116 void ast_variable_append(struct ast_category *category, struct ast_variable *variable)
118 if (!variable)
119 return;
120 if (category->last)
121 category->last->next = variable;
122 else
123 category->root = variable;
124 category->last = variable;
127 void ast_variables_destroy(struct ast_variable *v)
129 struct ast_variable *vn;
131 while(v) {
132 vn = v;
133 v = v->next;
134 free(vn);
138 struct ast_variable *ast_variable_browse(const struct ast_config *config, const char *category)
140 struct ast_category *cat = NULL;
142 if (category && config->last_browse && (config->last_browse->name == category))
143 cat = config->last_browse;
144 else
145 cat = ast_category_get(config, category);
147 return (cat) ? cat->root : NULL;
150 char *ast_variable_retrieve(const struct ast_config *config, const char *category, const char *variable)
152 struct ast_variable *v;
154 if (category) {
155 for (v = ast_variable_browse(config, category); v; v = v->next) {
156 if (!strcasecmp(variable, v->name))
157 return v->value;
159 } else {
160 struct ast_category *cat;
162 for (cat = config->root; cat; cat = cat->next)
163 for (v = cat->root; v; v = v->next)
164 if (!strcasecmp(variable, v->name))
165 return v->value;
168 return NULL;
171 static struct ast_variable *variable_clone(const struct ast_variable *old)
173 struct ast_variable *new = ast_variable_new(old->name, old->value);
175 if (new) {
176 new->lineno = old->lineno;
177 new->object = old->object;
178 new->blanklines = old->blanklines;
179 /* TODO: clone comments? */
182 return new;
185 static void move_variables(struct ast_category *old, struct ast_category *new)
187 struct ast_variable *var = old->root;
188 old->root = NULL;
189 #if 1
190 /* we can just move the entire list in a single op */
191 ast_variable_append(new, var);
192 #else
193 while (var) {
194 struct ast_variable *next = var->next;
195 var->next = NULL;
196 ast_variable_append(new, var);
197 var = next;
199 #endif
202 struct ast_category *ast_category_new(const char *name)
204 struct ast_category *category;
206 if ((category = ast_calloc(1, sizeof(*category))))
207 ast_copy_string(category->name, name, sizeof(category->name));
208 return category;
211 static struct ast_category *category_get(const struct ast_config *config, const char *category_name, int ignored)
213 struct ast_category *cat;
215 /* try exact match first, then case-insensitive match */
216 for (cat = config->root; cat; cat = cat->next) {
217 if (cat->name == category_name && (ignored || !cat->ignored))
218 return cat;
221 for (cat = config->root; cat; cat = cat->next) {
222 if (!strcasecmp(cat->name, category_name) && (ignored || !cat->ignored))
223 return cat;
226 return NULL;
229 struct ast_category *ast_category_get(const struct ast_config *config, const char *category_name)
231 return category_get(config, category_name, 0);
234 int ast_category_exist(const struct ast_config *config, const char *category_name)
236 return !!ast_category_get(config, category_name);
239 void ast_category_append(struct ast_config *config, struct ast_category *category)
241 if (config->last)
242 config->last->next = category;
243 else
244 config->root = category;
245 config->last = category;
246 config->current = category;
249 void ast_category_destroy(struct ast_category *cat)
251 ast_variables_destroy(cat->root);
252 free(cat);
255 static struct ast_category *next_available_category(struct ast_category *cat)
257 for (; cat && cat->ignored; cat = cat->next);
259 return cat;
262 char *ast_category_browse(struct ast_config *config, const char *prev)
264 struct ast_category *cat = NULL;
266 if (prev && config->last_browse && (config->last_browse->name == prev))
267 cat = config->last_browse->next;
268 else if (!prev && config->root)
269 cat = config->root;
270 else if (prev) {
271 for (cat = config->root; cat; cat = cat->next) {
272 if (cat->name == prev) {
273 cat = cat->next;
274 break;
277 if (!cat) {
278 for (cat = config->root; cat; cat = cat->next) {
279 if (!strcasecmp(cat->name, prev)) {
280 cat = cat->next;
281 break;
287 if (cat)
288 cat = next_available_category(cat);
290 config->last_browse = cat;
291 return (cat) ? cat->name : NULL;
294 struct ast_variable *ast_category_detach_variables(struct ast_category *cat)
296 struct ast_variable *v;
298 v = cat->root;
299 cat->root = NULL;
301 return v;
304 void ast_category_rename(struct ast_category *cat, const char *name)
306 ast_copy_string(cat->name, name, sizeof(cat->name));
309 static void inherit_category(struct ast_category *new, const struct ast_category *base)
311 struct ast_variable *var;
313 for (var = base->root; var; var = var->next)
314 ast_variable_append(new, variable_clone(var));
317 struct ast_config *ast_config_new(void)
319 struct ast_config *config;
321 if ((config = ast_calloc(1, sizeof(*config))))
322 config->max_include_level = MAX_INCLUDE_LEVEL;
323 return config;
326 int ast_variable_delete(struct ast_category *category, char *variable)
328 struct ast_variable *cur, *prev=NULL;
329 cur = category->root;
330 while (cur) {
331 if (cur->name == variable) {
332 if (prev) {
333 prev->next = cur->next;
334 if (cur == category->last)
335 category->last = prev;
336 } else {
337 category->root = cur->next;
338 if (cur == category->last)
339 category->last = NULL;
341 cur->next = NULL;
342 ast_variables_destroy(cur);
343 return 0;
345 prev = cur;
346 cur = cur->next;
349 prev = NULL;
350 cur = category->root;
351 while (cur) {
352 if (!strcasecmp(cur->name, variable)) {
353 if (prev) {
354 prev->next = cur->next;
355 if (cur == category->last)
356 category->last = prev;
357 } else {
358 category->root = cur->next;
359 if (cur == category->last)
360 category->last = NULL;
362 cur->next = NULL;
363 ast_variables_destroy(cur);
364 return 0;
366 prev = cur;
367 cur = cur->next;
369 return -1;
372 int ast_variable_update(struct ast_category *category, char *variable, char *value)
374 struct ast_variable *cur, *prev=NULL, *newer;
375 newer = ast_variable_new(variable, value);
376 if (!newer)
377 return -1;
378 cur = category->root;
379 while (cur) {
380 if (cur->name == variable) {
381 newer->next = cur->next;
382 if (prev)
383 prev->next = newer;
384 else
385 category->root = newer;
386 if (category->last == cur)
387 category->last = newer;
388 cur->next = NULL;
389 ast_variables_destroy(cur);
390 return 0;
392 prev = cur;
393 cur = cur->next;
396 prev = NULL;
397 cur = category->root;
398 while (cur) {
399 if (!strcasecmp(cur->name, variable)) {
400 newer->next = cur->next;
401 if (prev)
402 prev->next = newer;
403 else
404 category->root = newer;
405 if (category->last == cur)
406 category->last = newer;
407 cur->next = NULL;
408 ast_variables_destroy(cur);
409 return 0;
411 prev = cur;
412 cur = cur->next;
414 if (prev)
415 prev->next = newer;
416 else
417 category->root = newer;
418 return 0;
421 int ast_category_delete(struct ast_config *cfg, char *category)
423 struct ast_category *prev=NULL, *cat;
424 cat = cfg->root;
425 while(cat) {
426 if (cat->name == category) {
427 ast_variables_destroy(cat->root);
428 if (prev) {
429 prev->next = cat->next;
430 if (cat == cfg->last)
431 cfg->last = prev;
432 } else {
433 cfg->root = cat->next;
434 if (cat == cfg->last)
435 cfg->last = NULL;
437 free(cat);
438 return 0;
440 prev = cat;
441 cat = cat->next;
444 prev = NULL;
445 cat = cfg->root;
446 while(cat) {
447 if (!strcasecmp(cat->name, category)) {
448 ast_variables_destroy(cat->root);
449 if (prev) {
450 prev->next = cat->next;
451 if (cat == cfg->last)
452 cfg->last = prev;
453 } else {
454 cfg->root = cat->next;
455 if (cat == cfg->last)
456 cfg->last = NULL;
458 free(cat);
459 return 0;
461 prev = cat;
462 cat = cat->next;
464 return -1;
467 void ast_config_destroy(struct ast_config *cfg)
469 struct ast_category *cat, *catn;
471 if (!cfg)
472 return;
474 cat = cfg->root;
475 while(cat) {
476 ast_variables_destroy(cat->root);
477 catn = cat;
478 cat = cat->next;
479 free(catn);
481 free(cfg);
484 struct ast_category *ast_config_get_current_category(const struct ast_config *cfg)
486 return cfg->current;
489 void ast_config_set_current_category(struct ast_config *cfg, const struct ast_category *cat)
491 /* cast below is just to silence compiler warning about dropping "const" */
492 cfg->current = (struct ast_category *) cat;
495 static int process_text_line(struct ast_config *cfg, struct ast_category **cat, char *buf, int lineno, const char *configfile, int withcomments)
497 char *c;
498 char *cur = buf;
499 struct ast_variable *v;
500 char cmd[512], exec_file[512];
501 int object, do_exec, do_include;
503 /* Actually parse the entry */
504 if (cur[0] == '[') {
505 struct ast_category *newcat = NULL;
506 char *catname;
508 /* A category header */
509 c = strchr(cur, ']');
510 if (!c) {
511 ast_log(LOG_WARNING, "parse error: no closing ']', line %d of %s\n", lineno, configfile);
512 return -1;
514 *c++ = '\0';
515 cur++;
516 if (*c++ != '(')
517 c = NULL;
518 catname = cur;
519 if (!(*cat = newcat = ast_category_new(catname))) {
520 return -1;
522 /* If there are options or categories to inherit from, process them now */
523 if (c) {
524 if (!(cur = strchr(c, ')'))) {
525 ast_log(LOG_WARNING, "parse error: no closing ')', line %d of %s\n", lineno, configfile);
526 return -1;
528 *cur = '\0';
529 while ((cur = strsep(&c, ","))) {
530 if (!strcasecmp(cur, "!")) {
531 (*cat)->ignored = 1;
532 } else if (!strcasecmp(cur, "+")) {
533 *cat = category_get(cfg, catname, 1);
534 if (!*cat) {
535 ast_config_destroy(cfg);
536 if (newcat)
537 ast_category_destroy(newcat);
538 ast_log(LOG_WARNING, "Category addition requested, but category '%s' does not exist, line %d of %s\n", catname, lineno, configfile);
539 return -1;
541 if (newcat) {
542 move_variables(newcat, *cat);
543 ast_category_destroy(newcat);
544 newcat = NULL;
546 } else {
547 struct ast_category *base;
549 base = category_get(cfg, cur, 1);
550 if (!base) {
551 ast_log(LOG_WARNING, "Inheritance requested, but category '%s' does not exist, line %d of %s\n", cur, lineno, configfile);
552 return -1;
554 inherit_category(*cat, base);
558 if (newcat)
559 ast_category_append(cfg, *cat);
560 } else if (cur[0] == '#') {
561 /* A directive */
562 cur++;
563 c = cur;
564 while(*c && (*c > 32)) c++;
565 if (*c) {
566 *c = '\0';
567 /* Find real argument */
568 c = ast_skip_blanks(c + 1);
569 if (!*c)
570 c = NULL;
571 } else
572 c = NULL;
573 do_include = !strcasecmp(cur, "include");
574 if(!do_include)
575 do_exec = !strcasecmp(cur, "exec");
576 else
577 do_exec = 0;
578 if (do_exec && !ast_opt_exec_includes) {
579 ast_log(LOG_WARNING, "Cannot perform #exec unless execincludes option is enabled in asterisk.conf (options section)!\n");
580 do_exec = 0;
582 if (do_include || do_exec) {
583 if (c) {
584 /* Strip off leading and trailing "'s and <>'s */
585 while((*c == '<') || (*c == '>') || (*c == '\"')) c++;
586 /* Get rid of leading mess */
587 cur = c;
588 while (!ast_strlen_zero(cur)) {
589 c = cur + strlen(cur) - 1;
590 if ((*c == '>') || (*c == '<') || (*c == '\"'))
591 *c = '\0';
592 else
593 break;
595 /* #exec </path/to/executable>
596 We create a tmp file, then we #include it, then we delete it. */
597 if (do_exec) {
598 snprintf(exec_file, sizeof(exec_file), "/var/tmp/exec.%d.%ld", (int)time(NULL), (long)pthread_self());
599 snprintf(cmd, sizeof(cmd), "%s > %s 2>&1", cur, exec_file);
600 ast_safe_system(cmd);
601 cur = exec_file;
602 } else
603 exec_file[0] = '\0';
604 /* A #include */
605 do_include = ast_config_internal_load(cur, cfg, withcomments) ? 1 : 0;
606 if(!ast_strlen_zero(exec_file))
607 unlink(exec_file);
608 if(!do_include)
609 return 0;
611 } else {
612 ast_log(LOG_WARNING, "Directive '#%s' needs an argument (%s) at line %d of %s\n",
613 do_exec ? "exec" : "include",
614 do_exec ? "/path/to/executable" : "filename",
615 lineno,
616 configfile);
619 else
620 ast_log(LOG_WARNING, "Unknown directive '%s' at line %d of %s\n", cur, lineno, configfile);
621 } else {
622 /* Just a line (variable = value) */
623 if (!*cat) {
624 ast_log(LOG_WARNING,
625 "parse error: No category context for line %d of %s\n", lineno, configfile);
626 return -1;
628 c = strchr(cur, '=');
629 if (c) {
630 *c = 0;
631 c++;
632 /* Ignore > in => */
633 if (*c== '>') {
634 object = 1;
635 c++;
636 } else
637 object = 0;
638 if ((v = ast_variable_new(ast_strip(cur), ast_strip(c)))) {
639 v->lineno = lineno;
640 v->object = object;
641 /* Put and reset comments */
642 v->blanklines = 0;
643 ast_variable_append(*cat, v);
644 } else {
645 return -1;
647 } else {
648 ast_log(LOG_WARNING, "No '=' (equal sign) in line %d of %s\n", lineno, configfile);
652 return 0;
655 static struct ast_config *config_text_file_load(const char *database, const char *table, const char *filename, struct ast_config *cfg, int withcomments)
657 char fn[256];
658 char buf[8192];
659 char *new_buf, *comment_p, *process_buf;
660 FILE *f;
661 int lineno=0;
662 int comment = 0, nest[MAX_NESTED_COMMENTS];
663 struct ast_category *cat = NULL;
664 int count = 0;
665 struct stat statbuf;
667 cat = ast_config_get_current_category(cfg);
669 if (filename[0] == '/') {
670 ast_copy_string(fn, filename, sizeof(fn));
671 } else {
672 snprintf(fn, sizeof(fn), "%s/%s", (char *)ast_config_AST_CONFIG_DIR, filename);
675 #ifdef AST_INCLUDE_GLOB
677 int glob_ret;
678 glob_t globbuf;
679 globbuf.gl_offs = 0; /* initialize it to silence gcc */
680 #ifdef SOLARIS
681 glob_ret = glob(fn, GLOB_NOCHECK, NULL, &globbuf);
682 #else
683 glob_ret = glob(fn, GLOB_NOMAGIC|GLOB_BRACE, NULL, &globbuf);
684 #endif
685 if (glob_ret == GLOB_NOSPACE)
686 ast_log(LOG_WARNING,
687 "Glob Expansion of pattern '%s' failed: Not enough memory\n", fn);
688 else if (glob_ret == GLOB_ABORTED)
689 ast_log(LOG_WARNING,
690 "Glob Expansion of pattern '%s' failed: Read error\n", fn);
691 else {
692 /* loop over expanded files */
693 int i;
694 for (i=0; i<globbuf.gl_pathc; i++) {
695 ast_copy_string(fn, globbuf.gl_pathv[i], sizeof(fn));
696 #endif
697 do {
698 if (stat(fn, &statbuf))
699 continue;
701 if (!S_ISREG(statbuf.st_mode)) {
702 ast_log(LOG_WARNING, "'%s' is not a regular file, ignoring\n", fn);
703 continue;
705 if ((option_verbose > 1) && !option_debug) {
706 ast_verbose(VERBOSE_PREFIX_2 "Parsing '%s': ", fn);
707 fflush(stdout);
709 if (!(f = fopen(fn, "r"))) {
710 if (option_debug)
711 ast_log(LOG_DEBUG, "No file to parse: %s\n", fn);
712 else if (option_verbose > 1)
713 ast_verbose( "Not found (%s)\n", strerror(errno));
714 continue;
716 count++;
717 if (option_debug)
718 ast_log(LOG_DEBUG, "Parsing %s\n", fn);
719 else if (option_verbose > 1)
720 ast_verbose("Found\n");
721 while(!feof(f)) {
722 lineno++;
723 if (fgets(buf, sizeof(buf), f)) {
724 new_buf = buf;
725 if (comment)
726 process_buf = NULL;
727 else
728 process_buf = buf;
729 while ((comment_p = strchr(new_buf, COMMENT_META))) {
730 if ((comment_p > new_buf) && (*(comment_p-1) == '\\')) {
731 /* Yuck, gotta memmove */
732 memmove(comment_p - 1, comment_p, strlen(comment_p) + 1);
733 new_buf = comment_p;
734 } else if(comment_p[1] == COMMENT_TAG && comment_p[2] == COMMENT_TAG && (comment_p[3] != '-')) {
735 /* Meta-Comment start detected ";--" */
736 if (comment < MAX_NESTED_COMMENTS) {
737 *comment_p = '\0';
738 new_buf = comment_p + 3;
739 comment++;
740 nest[comment-1] = lineno;
741 } else {
742 ast_log(LOG_ERROR, "Maximum nest limit of %d reached.\n", MAX_NESTED_COMMENTS);
744 } else if ((comment_p >= new_buf + 2) &&
745 (*(comment_p - 1) == COMMENT_TAG) &&
746 (*(comment_p - 2) == COMMENT_TAG)) {
747 /* Meta-Comment end detected */
748 comment--;
749 new_buf = comment_p + 1;
750 if (!comment) {
751 /* Back to non-comment now */
752 if (process_buf) {
753 /* Actually have to move what's left over the top, then continue */
754 char *oldptr;
755 oldptr = process_buf + strlen(process_buf);
756 memmove(oldptr, new_buf, strlen(new_buf) + 1);
757 new_buf = oldptr;
758 } else
759 process_buf = new_buf;
761 } else {
762 if (!comment) {
763 /* If ; is found, and we are not nested in a comment,
764 we immediately stop all comment processing */
765 *comment_p = '\0';
766 new_buf = comment_p;
767 } else
768 new_buf = comment_p + 1;
771 if (process_buf) {
772 char *buf = ast_strip(process_buf);
773 if (!ast_strlen_zero(buf)) {
774 if (process_text_line(cfg, &cat, buf, lineno, filename, withcomments)) {
775 cfg = NULL;
776 break;
782 fclose(f);
783 } while(0);
784 if (comment) {
785 ast_log(LOG_WARNING,"Unterminated comment detected beginning on line %d\n", nest[comment]);
787 #ifdef AST_INCLUDE_GLOB
788 if (!cfg)
789 break;
791 globfree(&globbuf);
794 #endif
795 if (count == 0)
796 return NULL;
798 return cfg;
801 int config_text_file_save(const char *configfile, const struct ast_config *cfg, const char *generator)
803 FILE *f;
804 char fn[256];
805 char date[256]="";
806 time_t t;
807 struct ast_variable *var;
808 struct ast_category *cat;
809 int blanklines = 0;
811 if (configfile[0] == '/') {
812 ast_copy_string(fn, configfile, sizeof(fn));
813 } else {
814 snprintf(fn, sizeof(fn), "%s/%s", ast_config_AST_CONFIG_DIR, configfile);
816 time(&t);
817 ast_copy_string(date, ctime(&t), sizeof(date));
818 #ifdef __CYGWIN__
819 if ((f = fopen(fn, "w+"))) {
820 #else
821 if ((f = fopen(fn, "w"))) {
822 #endif
823 if ((option_verbose > 1) && !option_debug)
824 ast_verbose( VERBOSE_PREFIX_2 "Saving '%s': ", fn);
825 fprintf(f, ";!\n");
826 fprintf(f, ";! Automatically generated configuration file\n");
827 fprintf(f, ";! Filename: %s (%s)\n", configfile, fn);
828 fprintf(f, ";! Generator: %s\n", generator);
829 fprintf(f, ";! Creation Date: %s", date);
830 fprintf(f, ";!\n");
831 cat = cfg->root;
832 while(cat) {
833 /* Dump section with any appropriate comment */
834 fprintf(f, "\n[%s]\n", cat->name);
835 var = cat->root;
836 while(var) {
837 if (var->sameline)
838 fprintf(f, "%s %s %s ; %s\n", var->name, (var->object ? "=>" : "="), var->value, var->sameline->cmt);
839 else
840 fprintf(f, "%s %s %s\n", var->name, (var->object ? "=>" : "="), var->value);
841 if (var->blanklines) {
842 blanklines = var->blanklines;
843 while (blanklines--)
844 fprintf(f, "\n");
847 var = var->next;
849 #if 0
850 /* Put an empty line */
851 fprintf(f, "\n");
852 #endif
853 cat = cat->next;
855 if ((option_verbose > 1) && !option_debug)
856 ast_verbose("Saved\n");
857 } else {
858 if (option_debug)
859 printf("Unable to open for writing: %s\n", fn);
860 else if (option_verbose > 1)
861 printf( "Unable to write (%s)", strerror(errno));
862 return -1;
864 fclose(f);
865 return 0;
868 static void clear_config_maps(void)
870 struct ast_config_map *map;
872 ast_mutex_lock(&config_lock);
874 while (config_maps) {
875 map = config_maps;
876 config_maps = config_maps->next;
877 free(map);
880 ast_mutex_unlock(&config_lock);
883 static int append_mapping(char *name, char *driver, char *database, char *table)
885 struct ast_config_map *map;
886 int length;
888 length = sizeof(*map);
889 length += strlen(name) + 1;
890 length += strlen(driver) + 1;
891 length += strlen(database) + 1;
892 if (table)
893 length += strlen(table) + 1;
895 if (!(map = ast_calloc(1, length)))
896 return -1;
898 map->name = map->stuff;
899 strcpy(map->name, name);
900 map->driver = map->name + strlen(map->name) + 1;
901 strcpy(map->driver, driver);
902 map->database = map->driver + strlen(map->driver) + 1;
903 strcpy(map->database, database);
904 if (table) {
905 map->table = map->database + strlen(map->database) + 1;
906 strcpy(map->table, table);
908 map->next = config_maps;
910 if (option_verbose > 1)
911 ast_verbose(VERBOSE_PREFIX_2 "Binding %s to %s/%s/%s\n",
912 map->name, map->driver, map->database, map->table ? map->table : map->name);
914 config_maps = map;
915 return 0;
918 int read_config_maps(void)
920 struct ast_config *config, *configtmp;
921 struct ast_variable *v;
922 char *driver, *table, *database, *stringp;
924 clear_config_maps();
926 configtmp = ast_config_new();
927 configtmp->max_include_level = 1;
928 config = ast_config_internal_load(extconfig_conf, configtmp, 0);
929 if (!config) {
930 ast_config_destroy(configtmp);
931 return 0;
934 for (v = ast_variable_browse(config, "settings"); v; v = v->next) {
935 stringp = v->value;
936 driver = strsep(&stringp, ",");
938 /* check if the database text starts with a double quote */
939 if (*stringp == '"') {
940 stringp++;
941 database = strsep(&stringp, "\"");
942 strsep(&stringp, ",");
943 } else {
944 /* apparently this text has no quotes */
945 database = strsep(&stringp, ",");
948 table = strsep(&stringp, ",");
950 if (!strcmp(v->name, extconfig_conf)) {
951 ast_log(LOG_WARNING, "Cannot bind '%s'!\n", extconfig_conf);
952 continue;
955 if (!strcmp(v->name, "asterisk.conf")) {
956 ast_log(LOG_WARNING, "Cannot bind 'asterisk.conf'!\n");
957 continue;
960 if (!strcmp(v->name, "logger.conf")) {
961 ast_log(LOG_WARNING, "Cannot bind 'logger.conf'!\n");
962 continue;
965 if (!driver || !database)
966 continue;
967 if (!strcasecmp(v->name, "sipfriends")) {
968 ast_log(LOG_WARNING, "The 'sipfriends' table is obsolete, update your config to use sipusers and sippeers, though they can point to the same table.\n");
969 append_mapping("sipusers", driver, database, table ? table : "sipfriends");
970 append_mapping("sippeers", driver, database, table ? table : "sipfriends");
971 } else if (!strcasecmp(v->name, "iaxfriends")) {
972 ast_log(LOG_WARNING, "The 'iaxfriends' table is obsolete, update your config to use iaxusers and iaxpeers, though they can point to the same table.\n");
973 append_mapping("iaxusers", driver, database, table ? table : "iaxfriends");
974 append_mapping("iaxpeers", driver, database, table ? table : "iaxfriends");
975 } else
976 append_mapping(v->name, driver, database, table);
979 ast_config_destroy(config);
980 return 0;
983 int ast_config_engine_register(struct ast_config_engine *new)
985 struct ast_config_engine *ptr;
987 ast_mutex_lock(&config_lock);
989 if (!config_engine_list) {
990 config_engine_list = new;
991 } else {
992 for (ptr = config_engine_list; ptr->next; ptr=ptr->next);
993 ptr->next = new;
996 ast_mutex_unlock(&config_lock);
997 ast_log(LOG_NOTICE,"Registered Config Engine %s\n", new->name);
999 return 1;
1002 int ast_config_engine_deregister(struct ast_config_engine *del)
1004 struct ast_config_engine *ptr, *last=NULL;
1006 ast_mutex_lock(&config_lock);
1008 for (ptr = config_engine_list; ptr; ptr=ptr->next) {
1009 if (ptr == del) {
1010 if (last)
1011 last->next = ptr->next;
1012 else
1013 config_engine_list = ptr->next;
1014 break;
1016 last = ptr;
1019 ast_mutex_unlock(&config_lock);
1021 return 0;
1024 /*! \brief Find realtime engine for realtime family */
1025 static struct ast_config_engine *find_engine(const char *family, char *database, int dbsiz, char *table, int tabsiz)
1027 struct ast_config_engine *eng, *ret = NULL;
1028 struct ast_config_map *map;
1030 ast_mutex_lock(&config_lock);
1032 for (map = config_maps; map; map = map->next) {
1033 if (!strcasecmp(family, map->name)) {
1034 if (database)
1035 ast_copy_string(database, map->database, dbsiz);
1036 if (table)
1037 ast_copy_string(table, map->table ? map->table : family, tabsiz);
1038 break;
1042 /* Check if the required driver (engine) exist */
1043 if (map) {
1044 for (eng = config_engine_list; !ret && eng; eng = eng->next) {
1045 if (!strcasecmp(eng->name, map->driver))
1046 ret = eng;
1050 ast_mutex_unlock(&config_lock);
1052 /* if we found a mapping, but the engine is not available, then issue a warning */
1053 if (map && !ret)
1054 ast_log(LOG_WARNING, "Realtime mapping for '%s' found to engine '%s', but the engine is not available\n", map->name, map->driver);
1056 return ret;
1059 static struct ast_config_engine text_file_engine = {
1060 .name = "text",
1061 .load_func = config_text_file_load,
1064 struct ast_config *ast_config_internal_load(const char *filename, struct ast_config *cfg, int withcomments)
1066 char db[256];
1067 char table[256];
1068 struct ast_config_engine *loader = &text_file_engine;
1069 struct ast_config *result;
1071 if (cfg->include_level == cfg->max_include_level) {
1072 ast_log(LOG_WARNING, "Maximum Include level (%d) exceeded\n", cfg->max_include_level);
1073 return NULL;
1076 cfg->include_level++;
1078 if (strcmp(filename, extconfig_conf) && strcmp(filename, "asterisk.conf") && config_engine_list) {
1079 struct ast_config_engine *eng;
1081 eng = find_engine(filename, db, sizeof(db), table, sizeof(table));
1084 if (eng && eng->load_func) {
1085 loader = eng;
1086 } else {
1087 eng = find_engine("global", db, sizeof(db), table, sizeof(table));
1088 if (eng && eng->load_func)
1089 loader = eng;
1093 result = loader->load_func(db, table, filename, cfg, withcomments);
1095 if (result)
1096 result->include_level--;
1098 return result;
1101 struct ast_config *ast_config_load(const char *filename)
1103 struct ast_config *cfg;
1104 struct ast_config *result;
1106 cfg = ast_config_new();
1107 if (!cfg)
1108 return NULL;
1110 result = ast_config_internal_load(filename, cfg, 0);
1111 if (!result)
1112 ast_config_destroy(cfg);
1114 return result;
1117 struct ast_config *ast_config_load_with_comments(const char *filename)
1119 struct ast_config *cfg;
1120 struct ast_config *result;
1122 cfg = ast_config_new();
1123 if (!cfg)
1124 return NULL;
1126 result = ast_config_internal_load(filename, cfg, 1);
1127 if (!result)
1128 ast_config_destroy(cfg);
1130 return result;
1133 struct ast_variable *ast_load_realtime(const char *family, ...)
1135 struct ast_config_engine *eng;
1136 char db[256]="";
1137 char table[256]="";
1138 struct ast_variable *res=NULL;
1139 va_list ap;
1141 va_start(ap, family);
1142 eng = find_engine(family, db, sizeof(db), table, sizeof(table));
1143 if (eng && eng->realtime_func)
1144 res = eng->realtime_func(db, table, ap);
1145 va_end(ap);
1147 return res;
1150 /*! \brief Check if realtime engine is configured for family */
1151 int ast_check_realtime(const char *family)
1153 struct ast_config_engine *eng;
1155 eng = find_engine(family, NULL, 0, NULL, 0);
1156 if (eng)
1157 return 1;
1158 return 0;
1162 struct ast_config *ast_load_realtime_multientry(const char *family, ...)
1164 struct ast_config_engine *eng;
1165 char db[256]="";
1166 char table[256]="";
1167 struct ast_config *res=NULL;
1168 va_list ap;
1170 va_start(ap, family);
1171 eng = find_engine(family, db, sizeof(db), table, sizeof(table));
1172 if (eng && eng->realtime_multi_func)
1173 res = eng->realtime_multi_func(db, table, ap);
1174 va_end(ap);
1176 return res;
1179 int ast_update_realtime(const char *family, const char *keyfield, const char *lookup, ...)
1181 struct ast_config_engine *eng;
1182 int res = -1;
1183 char db[256]="";
1184 char table[256]="";
1185 va_list ap;
1187 va_start(ap, lookup);
1188 eng = find_engine(family, db, sizeof(db), table, sizeof(table));
1189 if (eng && eng->update_func)
1190 res = eng->update_func(db, table, keyfield, lookup, ap);
1191 va_end(ap);
1193 return res;
1196 static int config_command(int fd, int argc, char **argv)
1198 struct ast_config_engine *eng;
1199 struct ast_config_map *map;
1201 ast_mutex_lock(&config_lock);
1203 ast_cli(fd, "\n\n");
1204 for (eng = config_engine_list; eng; eng = eng->next) {
1205 ast_cli(fd, "\nConfig Engine: %s\n", eng->name);
1206 for (map = config_maps; map; map = map->next)
1207 if (!strcasecmp(map->driver, eng->name)) {
1208 ast_cli(fd, "===> %s (db=%s, table=%s)\n", map->name, map->database,
1209 map->table ? map->table : map->name);
1212 ast_cli(fd,"\n\n");
1214 ast_mutex_unlock(&config_lock);
1216 return 0;
1219 static char show_config_help[] =
1220 "Usage: show config mappings\n"
1221 " Shows the filenames to config engines.\n";
1223 static struct ast_cli_entry config_command_struct = {
1224 { "show", "config", "mappings", NULL }, config_command, "Show Config mappings (file names to config engines)", show_config_help, NULL
1227 int register_config_cli()
1229 return ast_cli_register(&config_command_struct);