1999-10-24 18:00:20 +00:00
|
|
|
/*
|
|
|
|
* Asterisk -- A telephony toolkit for Linux.
|
|
|
|
*
|
|
|
|
* Configuration File Parser
|
|
|
|
*
|
2004-10-02 17:16:36 +00:00
|
|
|
* Copyright (C) 1999-2004, Digium, Inc.
|
1999-10-24 18:00:20 +00:00
|
|
|
*
|
2004-10-02 17:16:36 +00:00
|
|
|
* Mark Spencer <markster@digium.com>
|
1999-10-24 18:00:20 +00:00
|
|
|
*
|
|
|
|
* This program is free software, distributed under the terms of
|
|
|
|
* the GNU General Public License
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
2003-03-09 06:00:18 +00:00
|
|
|
#include <time.h>
|
1999-10-24 18:00:20 +00:00
|
|
|
#include <asterisk/config.h>
|
2004-06-11 00:12:35 +00:00
|
|
|
#include <asterisk/config_pvt.h>
|
|
|
|
#include <asterisk/cli.h>
|
|
|
|
#include <asterisk/lock.h>
|
1999-10-24 18:00:20 +00:00
|
|
|
#include <asterisk/options.h>
|
|
|
|
#include <asterisk/logger.h>
|
2004-05-04 14:54:42 +00:00
|
|
|
#include <asterisk/utils.h>
|
1999-10-24 18:00:20 +00:00
|
|
|
#include "asterisk.h"
|
2003-01-30 15:03:20 +00:00
|
|
|
#include "astconf.h"
|
1999-10-24 18:00:20 +00:00
|
|
|
|
2002-03-05 23:57:31 +00:00
|
|
|
|
2004-06-11 00:12:35 +00:00
|
|
|
static int ast_cust_config=0;
|
2004-10-05 06:46:11 +00:00
|
|
|
struct ast_config *(*global_load_func)(const char *dbname, const char *table, const char *, struct ast_config *,struct ast_category **,struct ast_variable **,int);
|
|
|
|
|
|
|
|
static struct ast_config_map {
|
|
|
|
struct ast_config_map *next;
|
|
|
|
char *name;
|
|
|
|
char *driver;
|
|
|
|
char *database;
|
|
|
|
char *table;
|
|
|
|
char stuff[0];
|
|
|
|
} *maps = NULL;
|
2004-06-11 00:12:35 +00:00
|
|
|
|
|
|
|
AST_MUTEX_DEFINE_STATIC(ast_cust_config_lock);
|
|
|
|
static struct ast_config_reg *ast_cust_config_list;
|
|
|
|
static char *config_conf_file = "extconfig.conf";
|
1999-10-24 18:00:20 +00:00
|
|
|
|
2004-10-05 06:46:11 +00:00
|
|
|
void ast_destroy_realtime(struct ast_variable *v)
|
1999-10-24 18:00:20 +00:00
|
|
|
{
|
2004-10-05 06:46:11 +00:00
|
|
|
struct ast_variable *vn;
|
|
|
|
while(v) {
|
|
|
|
vn = v;
|
|
|
|
v = v->next;
|
|
|
|
free(vn);
|
2003-03-09 06:00:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1999-10-24 18:00:20 +00:00
|
|
|
void ast_destroy(struct ast_config *ast)
|
|
|
|
{
|
|
|
|
struct ast_category *cat, *catn;
|
|
|
|
|
2001-03-06 17:32:03 +00:00
|
|
|
if (!ast)
|
|
|
|
return;
|
|
|
|
|
1999-10-24 18:00:20 +00:00
|
|
|
cat = ast->root;
|
|
|
|
while(cat) {
|
2004-10-05 06:46:11 +00:00
|
|
|
ast_destroy_realtime(cat->root);
|
1999-10-24 18:00:20 +00:00
|
|
|
catn = cat;
|
|
|
|
cat = cat->next;
|
|
|
|
free(catn);
|
|
|
|
}
|
|
|
|
free(ast);
|
|
|
|
}
|
|
|
|
|
2004-10-08 05:36:23 +00:00
|
|
|
int ast_true(const char *s)
|
1999-10-24 18:00:20 +00:00
|
|
|
{
|
2000-01-06 10:19:32 +00:00
|
|
|
if (!s)
|
|
|
|
return 0;
|
1999-10-24 18:00:20 +00:00
|
|
|
/* Determine if this is a true value */
|
|
|
|
if (!strcasecmp(s, "yes") ||
|
|
|
|
!strcasecmp(s, "true") ||
|
|
|
|
!strcasecmp(s, "y") ||
|
|
|
|
!strcasecmp(s, "t") ||
|
2004-08-08 22:22:30 +00:00
|
|
|
!strcasecmp(s, "1") ||
|
|
|
|
!strcasecmp(s, "on"))
|
1999-10-24 18:00:20 +00:00
|
|
|
return -1;
|
2004-03-02 07:57:06 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2004-10-08 05:36:23 +00:00
|
|
|
int ast_false(const char *s)
|
2004-03-02 07:57:06 +00:00
|
|
|
{
|
|
|
|
if (!s)
|
|
|
|
return 0;
|
|
|
|
/* Determine if this is a false value */
|
|
|
|
if (!strcasecmp(s, "no") ||
|
|
|
|
!strcasecmp(s, "false") ||
|
|
|
|
!strcasecmp(s, "n") ||
|
|
|
|
!strcasecmp(s, "f") ||
|
2004-08-08 22:22:30 +00:00
|
|
|
!strcasecmp(s, "0") ||
|
|
|
|
!strcasecmp(s, "off"))
|
2004-03-02 07:57:06 +00:00
|
|
|
return -1;
|
1999-10-24 18:00:20 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct ast_variable *ast_variable_browse(struct ast_config *config, char *category)
|
|
|
|
{
|
|
|
|
struct ast_category *cat;
|
|
|
|
cat = config->root;
|
2001-10-09 14:54:42 +00:00
|
|
|
while(cat) {
|
|
|
|
if (cat->name == category)
|
|
|
|
return cat->root;
|
|
|
|
cat = cat->next;
|
|
|
|
}
|
|
|
|
cat = config->root;
|
1999-10-24 18:00:20 +00:00
|
|
|
while(cat) {
|
|
|
|
if (!strcasecmp(cat->name, category))
|
|
|
|
return cat->root;
|
|
|
|
cat = cat->next;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
char *ast_variable_retrieve(struct ast_config *config, char *category, char *value)
|
|
|
|
{
|
|
|
|
struct ast_variable *v;
|
1999-12-11 20:09:45 +00:00
|
|
|
if (category) {
|
2001-10-09 14:54:42 +00:00
|
|
|
v = ast_variable_browse(config, category);
|
|
|
|
while (v) {
|
|
|
|
if (value == v->name)
|
|
|
|
return v->value;
|
|
|
|
v=v->next;
|
|
|
|
}
|
1999-12-11 20:09:45 +00:00
|
|
|
v = ast_variable_browse(config, category);
|
|
|
|
while (v) {
|
|
|
|
if (!strcasecmp(value, v->name))
|
|
|
|
return v->value;
|
|
|
|
v=v->next;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
struct ast_category *cat;
|
|
|
|
cat = config->root;
|
|
|
|
while(cat) {
|
|
|
|
v = cat->root;
|
|
|
|
while (v) {
|
|
|
|
if (!strcasecmp(value, v->name))
|
|
|
|
return v->value;
|
|
|
|
v=v->next;
|
|
|
|
}
|
|
|
|
cat = cat->next;
|
|
|
|
}
|
1999-10-24 18:00:20 +00:00
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2004-10-05 06:46:11 +00:00
|
|
|
int ast_category_exist(struct ast_config *config, char *category_name)
|
2003-03-09 06:00:18 +00:00
|
|
|
{
|
2004-10-05 06:46:11 +00:00
|
|
|
struct ast_category *category = NULL;
|
2003-03-09 06:00:18 +00:00
|
|
|
|
2004-10-05 06:46:11 +00:00
|
|
|
category = config->root;
|
|
|
|
|
|
|
|
while(category) {
|
|
|
|
if (!strcasecmp(category->name,category_name))
|
|
|
|
return 1;
|
|
|
|
category = category->next;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
2003-03-09 06:00:18 +00:00
|
|
|
}
|
|
|
|
|
2004-10-05 06:46:11 +00:00
|
|
|
|
|
|
|
static struct ast_config_reg *get_ast_cust_config_keyword(const char *name, char *database, int dbsiz, char *table, int tabsiz)
|
2003-03-09 06:00:18 +00:00
|
|
|
{
|
2004-10-05 06:46:11 +00:00
|
|
|
struct ast_config_reg *reg,*ret=NULL;
|
|
|
|
struct ast_config_map *map;
|
|
|
|
|
|
|
|
ast_mutex_lock(&ast_cust_config_lock);
|
|
|
|
map = maps;
|
|
|
|
while(map) {
|
|
|
|
if (!strcasecmp(name, map->name)) {
|
|
|
|
strncpy(database, map->database, dbsiz - 1);
|
|
|
|
if (map->table)
|
|
|
|
strncpy(table, map->table, tabsiz - 1);
|
|
|
|
else
|
|
|
|
strncpy(table, name, tabsiz - 1);
|
2003-03-09 06:00:18 +00:00
|
|
|
break;
|
|
|
|
}
|
2004-10-05 06:46:11 +00:00
|
|
|
map = map->next;
|
2003-03-09 06:00:18 +00:00
|
|
|
}
|
2004-10-05 06:46:11 +00:00
|
|
|
if (map) {
|
|
|
|
for (reg=ast_cust_config_list;reg && !ret;reg=reg->next) {
|
|
|
|
if (!strcmp(reg->name,map->driver))
|
|
|
|
ret=reg;
|
2003-03-09 06:00:18 +00:00
|
|
|
}
|
|
|
|
}
|
2004-10-05 06:46:11 +00:00
|
|
|
ast_mutex_unlock(&ast_cust_config_lock);
|
|
|
|
return ret;
|
2003-03-09 06:00:18 +00:00
|
|
|
}
|
|
|
|
|
2004-10-05 06:46:11 +00:00
|
|
|
void ast_config_destroy_all(void)
|
2003-03-09 06:00:18 +00:00
|
|
|
{
|
2004-10-05 06:46:11 +00:00
|
|
|
struct ast_config_reg *key;
|
|
|
|
ast_mutex_lock(&ast_cust_config_lock);
|
|
|
|
for (key=ast_cust_config_list;key;key=key->next) {
|
|
|
|
ast_config_deregister(key);
|
2003-03-09 06:00:18 +00:00
|
|
|
}
|
2004-10-05 06:46:11 +00:00
|
|
|
ast_cust_config_list = NULL;
|
|
|
|
ast_mutex_unlock(&ast_cust_config_lock);
|
2003-03-09 06:00:18 +00:00
|
|
|
}
|
|
|
|
|
2004-10-05 06:46:11 +00:00
|
|
|
static struct ast_config_reg *get_config_registrations(void)
|
2001-04-23 16:50:12 +00:00
|
|
|
{
|
2004-10-05 06:46:11 +00:00
|
|
|
return ast_cust_config_list;
|
2001-04-23 16:50:12 +00:00
|
|
|
}
|
|
|
|
|
2003-04-28 19:58:43 +00:00
|
|
|
|
2004-10-05 06:46:11 +00:00
|
|
|
static struct ast_config *__ast_load(const char *configfile, struct ast_config *tmp, struct ast_category **_tmpc, struct ast_variable **_last, int includelevel);
|
2003-03-09 06:00:18 +00:00
|
|
|
|
2004-10-05 06:46:11 +00:00
|
|
|
static int cfg_process(struct ast_config *tmp, struct ast_category **_tmpc, struct ast_variable **_last, char *buf, int lineno, const char *configfile, int includelevel )
|
2002-03-05 23:57:31 +00:00
|
|
|
{
|
|
|
|
char *c;
|
|
|
|
char *cur;
|
2004-06-11 00:12:35 +00:00
|
|
|
char *arg=NULL;
|
2002-03-05 23:57:31 +00:00
|
|
|
struct ast_variable *v;
|
2003-03-09 06:00:18 +00:00
|
|
|
int object;
|
2002-03-05 23:57:31 +00:00
|
|
|
/* Strip off lines using ; as comment */
|
|
|
|
c = strchr(buf, ';');
|
2004-09-03 15:36:13 +00:00
|
|
|
while (c) {
|
|
|
|
if ((c == buf) || (*(c-1) != '\\')) {
|
|
|
|
*c = '\0';
|
|
|
|
} else {
|
|
|
|
*(c-1) = ';';
|
|
|
|
memmove(c, c + 1, strlen(c + 1));
|
|
|
|
}
|
|
|
|
c = strchr(c + 1, ';');
|
2003-03-09 06:00:18 +00:00
|
|
|
}
|
2004-10-05 06:46:11 +00:00
|
|
|
cur = ast_strip(buf);
|
2004-05-04 14:54:42 +00:00
|
|
|
if (!ast_strlen_zero(cur)) {
|
2002-03-05 23:57:31 +00:00
|
|
|
/* Actually parse the entry */
|
|
|
|
if (cur[0] == '[') {
|
|
|
|
/* A category header */
|
|
|
|
c = strchr(cur, ']');
|
|
|
|
if (c) {
|
|
|
|
*c = 0;
|
|
|
|
*_tmpc = malloc(sizeof(struct ast_category));
|
|
|
|
if (!*_tmpc) {
|
|
|
|
ast_destroy(tmp);
|
|
|
|
ast_log(LOG_WARNING,
|
|
|
|
"Out of memory, line %d\n", lineno);
|
|
|
|
return -1;
|
|
|
|
}
|
2002-04-22 15:14:18 +00:00
|
|
|
memset(*_tmpc, 0, sizeof(struct ast_category));
|
2002-03-05 23:57:31 +00:00
|
|
|
strncpy((*_tmpc)->name, cur+1, sizeof((*_tmpc)->name) - 1);
|
|
|
|
(*_tmpc)->root = NULL;
|
2003-03-09 06:00:18 +00:00
|
|
|
if (!tmp->prev)
|
|
|
|
tmp->root = *_tmpc;
|
|
|
|
else
|
|
|
|
tmp->prev->next = *_tmpc;
|
|
|
|
|
|
|
|
tmp->prev = *_tmpc;
|
2002-03-05 23:57:31 +00:00
|
|
|
*_last = NULL;
|
|
|
|
} else {
|
|
|
|
ast_log(LOG_WARNING,
|
|
|
|
"parse error: no closing ']', line %d of %s\n", lineno, configfile);
|
|
|
|
}
|
|
|
|
} else if (cur[0] == '#') {
|
|
|
|
/* A directive */
|
|
|
|
cur++;
|
|
|
|
c = cur;
|
|
|
|
while(*c && (*c > 32)) c++;
|
|
|
|
if (*c) {
|
|
|
|
*c = '\0';
|
|
|
|
c++;
|
|
|
|
/* Find real argument */
|
|
|
|
while(*c && (*c < 33)) c++;
|
|
|
|
if (!*c)
|
|
|
|
c = NULL;
|
|
|
|
} else
|
|
|
|
c = NULL;
|
|
|
|
if (!strcasecmp(cur, "include")) {
|
|
|
|
/* A #include */
|
|
|
|
if (c) {
|
|
|
|
while((*c == '<') || (*c == '>') || (*c == '\"')) c++;
|
|
|
|
/* Get rid of leading mess */
|
|
|
|
cur = c;
|
2004-06-17 04:53:55 +00:00
|
|
|
while (!ast_strlen_zero(cur)) {
|
2002-03-05 23:57:31 +00:00
|
|
|
c = cur + strlen(cur) - 1;
|
|
|
|
if ((*c == '>') || (*c == '<') || (*c == '\"'))
|
|
|
|
*c = '\0';
|
|
|
|
else
|
|
|
|
break;
|
|
|
|
}
|
2004-06-11 00:12:35 +00:00
|
|
|
|
|
|
|
if((c = strchr(cur,':'))) {
|
|
|
|
*c = '\0';
|
2004-06-12 14:34:15 +00:00
|
|
|
c++;
|
2004-06-11 00:12:35 +00:00
|
|
|
arg = c;
|
|
|
|
}
|
|
|
|
|
2002-03-05 23:57:31 +00:00
|
|
|
if (includelevel < MAX_INCLUDE_LEVEL) {
|
2004-06-11 00:12:35 +00:00
|
|
|
if(arg && cur) {
|
2004-10-05 06:46:11 +00:00
|
|
|
ast_log(LOG_WARNING, "Including files with explicit config engine no longer permitted. Please use extconfig.conf to specify all mappings\n");
|
|
|
|
} else {
|
|
|
|
__ast_load(cur, tmp, _tmpc, _last, includelevel + 1);
|
2004-06-11 00:12:35 +00:00
|
|
|
}
|
|
|
|
} else
|
2002-03-05 23:57:31 +00:00
|
|
|
ast_log(LOG_WARNING, "Maximum Include level (%d) exceeded\n", includelevel);
|
|
|
|
} else
|
|
|
|
ast_log(LOG_WARNING, "Directive '#include' needs an argument (filename) at line %d of %s\n", lineno, configfile);
|
|
|
|
/* Strip off leading and trailing "'s and <>'s */
|
2004-06-11 00:12:35 +00:00
|
|
|
}
|
|
|
|
else
|
2002-03-05 23:57:31 +00:00
|
|
|
ast_log(LOG_WARNING, "Unknown directive '%s' at line %d of %s\n", cur, lineno, configfile);
|
|
|
|
} else {
|
|
|
|
/* Just a line (variable = value) */
|
|
|
|
if (!*_tmpc) {
|
|
|
|
ast_log(LOG_WARNING,
|
|
|
|
"parse error: No category context for line %d of %s\n", lineno, configfile);
|
2003-01-30 15:03:20 +00:00
|
|
|
ast_destroy(tmp);
|
|
|
|
return -1;
|
2002-03-05 23:57:31 +00:00
|
|
|
}
|
|
|
|
c = strchr(cur, '=');
|
|
|
|
if (c) {
|
|
|
|
*c = 0;
|
|
|
|
c++;
|
|
|
|
/* Ignore > in => */
|
2003-03-09 06:00:18 +00:00
|
|
|
if (*c== '>') {
|
|
|
|
object = 1;
|
2002-03-05 23:57:31 +00:00
|
|
|
c++;
|
2003-03-09 06:00:18 +00:00
|
|
|
} else
|
|
|
|
object = 0;
|
2004-10-05 06:46:11 +00:00
|
|
|
v = ast_new_variable(ast_strip(cur), ast_strip(c));
|
2002-03-05 23:57:31 +00:00
|
|
|
if (v) {
|
|
|
|
v->next = NULL;
|
|
|
|
v->lineno = lineno;
|
2003-03-09 06:00:18 +00:00
|
|
|
v->object = object;
|
|
|
|
/* Put and reset comments */
|
2003-04-28 19:58:43 +00:00
|
|
|
v->blanklines = 0;
|
2003-03-09 06:00:18 +00:00
|
|
|
if (*_last)
|
2002-03-05 23:57:31 +00:00
|
|
|
(*_last)->next = v;
|
|
|
|
else
|
|
|
|
(*_tmpc)->root = v;
|
|
|
|
*_last = v;
|
|
|
|
} else {
|
|
|
|
ast_destroy(tmp);
|
|
|
|
ast_log(LOG_WARNING, "Out of memory, line %d\n", lineno);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
ast_log(LOG_WARNING, "No '=' (equal sign) in line %d of %s\n", lineno, configfile);
|
|
|
|
}
|
|
|
|
|
2003-03-09 06:00:18 +00:00
|
|
|
}
|
2002-03-05 23:57:31 +00:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2003-03-09 06:00:18 +00:00
|
|
|
int ast_save(char *configfile, struct ast_config *cfg, char *generator)
|
|
|
|
{
|
|
|
|
FILE *f;
|
|
|
|
char fn[256];
|
2004-07-09 10:08:09 +00:00
|
|
|
char date[256]="";
|
2003-03-09 06:00:18 +00:00
|
|
|
time_t t;
|
|
|
|
struct ast_variable *var;
|
|
|
|
struct ast_category *cat;
|
|
|
|
int blanklines = 0;
|
|
|
|
if (configfile[0] == '/') {
|
|
|
|
strncpy(fn, configfile, sizeof(fn)-1);
|
|
|
|
} else {
|
|
|
|
snprintf(fn, sizeof(fn), "%s/%s", AST_CONFIG_DIR, configfile);
|
|
|
|
}
|
|
|
|
time(&t);
|
2004-07-09 10:08:09 +00:00
|
|
|
strncpy(date, ctime(&t), sizeof(date) - 1);
|
2003-03-09 06:00:18 +00:00
|
|
|
if ((f = fopen(fn, "w"))) {
|
|
|
|
if ((option_verbose > 1) && !option_debug)
|
|
|
|
ast_verbose( VERBOSE_PREFIX_2 "Saving '%s': ", fn);
|
|
|
|
fprintf(f, ";!\n");
|
|
|
|
fprintf(f, ";! Automatically generated configuration file\n");
|
|
|
|
fprintf(f, ";! Filename: %s (%s)\n", configfile, fn);
|
|
|
|
fprintf(f, ";! Generator: %s\n", generator);
|
|
|
|
fprintf(f, ";! Creation Date: %s", date);
|
|
|
|
fprintf(f, ";!\n");
|
|
|
|
cat = cfg->root;
|
|
|
|
while(cat) {
|
|
|
|
/* Dump section with any appropriate comment */
|
2004-10-05 06:46:11 +00:00
|
|
|
fprintf(f, "[%s]\n", cat->name);
|
2003-03-09 06:00:18 +00:00
|
|
|
var = cat->root;
|
|
|
|
while(var) {
|
|
|
|
if (var->sameline)
|
2003-04-25 20:33:27 +00:00
|
|
|
fprintf(f, "%s %s %s ; %s\n", var->name, (var->object ? "=>" : "="), var->value, var->sameline->cmt);
|
2003-03-09 06:00:18 +00:00
|
|
|
else
|
|
|
|
fprintf(f, "%s %s %s\n", var->name, (var->object ? "=>" : "="), var->value);
|
|
|
|
if (var->blanklines) {
|
|
|
|
blanklines = var->blanklines;
|
|
|
|
while (blanklines) {
|
|
|
|
fprintf(f, "\n");
|
|
|
|
blanklines--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var = var->next;
|
|
|
|
}
|
|
|
|
#if 0
|
|
|
|
/* Put an empty line */
|
|
|
|
fprintf(f, "\n");
|
|
|
|
#endif
|
|
|
|
cat = cat->next;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (option_debug)
|
|
|
|
printf("Unable to open for writing: %s\n", fn);
|
|
|
|
else if (option_verbose > 1)
|
|
|
|
printf( "Unable to write (%s)", strerror(errno));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
fclose(f);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2004-10-05 06:46:11 +00:00
|
|
|
|
2004-10-07 19:57:50 +00:00
|
|
|
struct ast_variable *ast_load_realtime(const char *family, ...)
|
2004-10-05 06:46:11 +00:00
|
|
|
{
|
|
|
|
struct ast_config_reg *reg;
|
|
|
|
char db[256]="";
|
|
|
|
char table[256]="";
|
2004-10-07 19:57:50 +00:00
|
|
|
struct ast_variable *res=NULL;
|
|
|
|
va_list ap;
|
|
|
|
va_start(ap, family);
|
2004-10-05 06:46:11 +00:00
|
|
|
reg = get_ast_cust_config_keyword(family, db, sizeof(db), table, sizeof(table));
|
|
|
|
if (reg && reg->realtime_func)
|
2004-10-07 19:57:50 +00:00
|
|
|
res = reg->realtime_func(db, table, ap);
|
|
|
|
va_end(ap);
|
|
|
|
return res;
|
2004-10-05 06:46:11 +00:00
|
|
|
}
|
|
|
|
|
2004-10-08 18:20:49 +00:00
|
|
|
struct ast_config *ast_load_realtime_multientry(const char *family, ...)
|
|
|
|
{
|
|
|
|
struct ast_config_reg *reg;
|
|
|
|
char db[256]="";
|
|
|
|
char table[256]="";
|
|
|
|
struct ast_config *res=NULL;
|
|
|
|
va_list ap;
|
|
|
|
va_start(ap, family);
|
|
|
|
reg = get_ast_cust_config_keyword(family, db, sizeof(db), table, sizeof(table));
|
|
|
|
if (reg && reg->realtime_multi_func)
|
|
|
|
res = reg->realtime_multi_func(db, table, ap);
|
|
|
|
va_end(ap);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2004-10-05 06:46:11 +00:00
|
|
|
int ast_update_realtime(const char *family, const char *keyfield, const char *lookup, ...)
|
|
|
|
{
|
|
|
|
struct ast_config_reg *reg;
|
|
|
|
int res = -1;
|
|
|
|
char db[256]="";
|
|
|
|
char table[256]="";
|
|
|
|
va_list ap;
|
|
|
|
va_start(ap, lookup);
|
|
|
|
reg = get_ast_cust_config_keyword(family, db, sizeof(db), table, sizeof(table));
|
|
|
|
if (reg && reg->update_func)
|
|
|
|
res = reg->update_func(db, table, keyfield, lookup, ap);
|
|
|
|
va_end(ap);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct ast_config *__ast_load(const char *configfile, struct ast_config *tmp, struct ast_category **_tmpc, struct ast_variable **_last, int includelevel)
|
1999-10-24 18:00:20 +00:00
|
|
|
{
|
|
|
|
char fn[256];
|
2004-06-12 14:34:15 +00:00
|
|
|
char buf[8192];
|
2004-10-05 06:46:11 +00:00
|
|
|
char db[256];
|
|
|
|
char table[256];
|
1999-10-24 18:00:20 +00:00
|
|
|
FILE *f;
|
|
|
|
int lineno=0;
|
2003-03-09 06:00:18 +00:00
|
|
|
int master=0;
|
2004-06-11 00:12:35 +00:00
|
|
|
struct ast_config_reg *reg=NULL;
|
2004-10-05 06:46:11 +00:00
|
|
|
struct ast_config *(*load_func)(const char *database, const char *table, const char *, struct ast_config *,struct ast_category **,struct ast_variable **,int);
|
2004-06-11 00:12:35 +00:00
|
|
|
|
|
|
|
load_func=NULL;
|
2004-06-17 04:53:55 +00:00
|
|
|
if (strcmp(configfile,config_conf_file) && strcmp(configfile,"asterisk.conf") && ast_cust_config_list) {
|
|
|
|
if (global_load_func) {
|
|
|
|
load_func = global_load_func;
|
|
|
|
} else {
|
2004-10-05 06:46:11 +00:00
|
|
|
reg = get_ast_cust_config_keyword(configfile, db, sizeof(db), table, sizeof(table));
|
|
|
|
if (reg && reg->static_func) {
|
|
|
|
load_func = reg->static_func;
|
2004-06-17 04:53:55 +00:00
|
|
|
} else {
|
2004-10-05 06:46:11 +00:00
|
|
|
reg = get_ast_cust_config_keyword(configfile, db, sizeof(db), table, sizeof(table));
|
|
|
|
if (reg && reg->static_func)
|
|
|
|
global_load_func = load_func = reg->static_func;
|
2004-06-17 04:53:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (load_func) {
|
|
|
|
ast_log(LOG_NOTICE,"Loading Config %s via %s engine\n",configfile,reg && reg->name ? reg->name : "global");
|
2004-10-05 06:46:11 +00:00
|
|
|
tmp = load_func(db, table, configfile,tmp, _tmpc, _last, includelevel);
|
2004-06-11 00:12:35 +00:00
|
|
|
|
2004-06-17 04:53:55 +00:00
|
|
|
if (tmp)
|
|
|
|
return tmp;
|
|
|
|
}
|
2004-06-11 00:12:35 +00:00
|
|
|
}
|
|
|
|
|
1999-10-24 18:00:20 +00:00
|
|
|
if (configfile[0] == '/') {
|
2001-11-02 01:22:37 +00:00
|
|
|
strncpy(fn, configfile, sizeof(fn)-1);
|
1999-10-24 18:00:20 +00:00
|
|
|
} else {
|
2003-01-30 15:03:20 +00:00
|
|
|
snprintf(fn, sizeof(fn), "%s/%s", (char *)ast_config_AST_CONFIG_DIR, configfile);
|
1999-10-24 18:00:20 +00:00
|
|
|
}
|
|
|
|
if ((option_verbose > 1) && !option_debug) {
|
|
|
|
ast_verbose( VERBOSE_PREFIX_2 "Parsing '%s': ", fn);
|
|
|
|
fflush(stdout);
|
|
|
|
}
|
|
|
|
if ((f = fopen(fn, "r"))) {
|
|
|
|
if (option_debug)
|
|
|
|
ast_log(LOG_DEBUG, "Parsing %s\n", fn);
|
|
|
|
else if (option_verbose > 1)
|
|
|
|
ast_verbose( "Found\n");
|
2002-03-05 23:57:31 +00:00
|
|
|
if (!tmp) {
|
|
|
|
tmp = malloc(sizeof(struct ast_config));
|
2002-04-22 15:14:18 +00:00
|
|
|
if (tmp)
|
|
|
|
memset(tmp, 0, sizeof(struct ast_config));
|
2003-03-09 06:00:18 +00:00
|
|
|
|
|
|
|
master = 1;
|
2002-03-05 23:57:31 +00:00
|
|
|
}
|
1999-10-24 18:00:20 +00:00
|
|
|
if (!tmp) {
|
|
|
|
ast_log(LOG_WARNING, "Out of memory\n");
|
|
|
|
fclose(f);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
while(!feof(f)) {
|
|
|
|
lineno++;
|
2004-05-20 07:52:07 +00:00
|
|
|
if (fgets(buf, sizeof(buf), f)) {
|
2004-10-05 06:46:11 +00:00
|
|
|
if (cfg_process(tmp, _tmpc, _last, buf, lineno, configfile, includelevel)) {
|
2002-03-05 23:57:31 +00:00
|
|
|
fclose(f);
|
|
|
|
return NULL;
|
1999-10-24 18:00:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fclose(f);
|
|
|
|
} else {
|
|
|
|
if (option_debug)
|
|
|
|
ast_log(LOG_DEBUG, "No file to parse: %s\n", fn);
|
|
|
|
else if (option_verbose > 1)
|
2003-07-01 04:08:25 +00:00
|
|
|
ast_verbose( "Not found (%s)\n", strerror(errno));
|
1999-10-24 18:00:20 +00:00
|
|
|
}
|
|
|
|
return tmp;
|
|
|
|
}
|
|
|
|
|
2004-06-17 04:53:55 +00:00
|
|
|
int ast_config_register(struct ast_config_reg *new)
|
|
|
|
{
|
2004-06-11 00:12:35 +00:00
|
|
|
struct ast_config_reg *ptr;
|
|
|
|
ast_mutex_lock(&ast_cust_config_lock);
|
2004-06-17 04:53:55 +00:00
|
|
|
if (!ast_cust_config_list) {
|
2004-06-11 00:12:35 +00:00
|
|
|
ast_cust_config_list = new;
|
2004-06-17 04:53:55 +00:00
|
|
|
} else {
|
2004-06-11 00:12:35 +00:00
|
|
|
for(ptr=ast_cust_config_list;ptr->next;ptr=ptr->next);
|
2004-06-17 04:53:55 +00:00
|
|
|
ptr->next = new;
|
2004-06-11 00:12:35 +00:00
|
|
|
}
|
|
|
|
ast_mutex_unlock(&ast_cust_config_lock);
|
|
|
|
ast_log(LOG_NOTICE,"Registered Config Engine %s\n",new->name);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2004-06-17 04:53:55 +00:00
|
|
|
int ast_config_deregister(struct ast_config_reg *del)
|
|
|
|
{
|
|
|
|
struct ast_config_reg *ptr=NULL,*last=NULL;
|
|
|
|
ast_mutex_lock(&ast_cust_config_lock);
|
|
|
|
for (ptr=ast_cust_config_list;ptr;ptr=ptr->next) {
|
|
|
|
if (ptr == del) {
|
|
|
|
if (last && ptr->next) {
|
|
|
|
last->next = ptr->next;
|
|
|
|
} else if (last && ! ptr->next) {
|
|
|
|
last->next = NULL;
|
|
|
|
} else if (!last && ptr->next) {
|
|
|
|
ast_cust_config_list = ptr->next;
|
|
|
|
} else if (!last && !ptr->next) {
|
|
|
|
ast_cust_config_list = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
last = ptr;
|
|
|
|
}
|
|
|
|
ast_mutex_unlock(&ast_cust_config_lock);
|
|
|
|
return 0;
|
2004-06-11 00:12:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int ast_cust_config_active(void) {
|
|
|
|
return (ast_cust_config >0) ? 1 : 0;
|
|
|
|
}
|
|
|
|
|
2002-03-05 23:57:31 +00:00
|
|
|
struct ast_config *ast_load(char *configfile)
|
|
|
|
{
|
|
|
|
struct ast_category *tmpc=NULL;
|
|
|
|
struct ast_variable *last = NULL;
|
2004-06-11 00:12:35 +00:00
|
|
|
|
2004-10-05 06:46:11 +00:00
|
|
|
return __ast_load(configfile, NULL, &tmpc, &last, 0);
|
2002-03-05 23:57:31 +00:00
|
|
|
}
|
|
|
|
|
1999-10-24 18:00:20 +00:00
|
|
|
char *ast_category_browse(struct ast_config *config, char *prev)
|
|
|
|
{
|
|
|
|
struct ast_category *cat;
|
|
|
|
if (!prev) {
|
|
|
|
if (config->root)
|
|
|
|
return config->root->name;
|
|
|
|
else
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
cat = config->root;
|
2001-10-09 14:54:42 +00:00
|
|
|
while(cat) {
|
|
|
|
if (cat->name == prev) {
|
|
|
|
if (cat->next)
|
|
|
|
return cat->next->name;
|
|
|
|
else
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
cat = cat->next;
|
|
|
|
}
|
|
|
|
cat = config->root;
|
1999-10-24 18:00:20 +00:00
|
|
|
while(cat) {
|
|
|
|
if (!strcasecmp(cat->name, prev)) {
|
|
|
|
if (cat->next)
|
|
|
|
return cat->next->name;
|
|
|
|
else
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
cat = cat->next;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
2004-06-11 00:12:35 +00:00
|
|
|
|
|
|
|
|
2004-06-17 04:53:55 +00:00
|
|
|
struct ast_config *ast_new_config(void)
|
|
|
|
{
|
2004-06-11 00:12:35 +00:00
|
|
|
struct ast_config *config;
|
|
|
|
config = malloc(sizeof(struct ast_config));
|
|
|
|
memset(config,0,sizeof(struct ast_config));
|
|
|
|
return config;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2004-06-17 04:53:55 +00:00
|
|
|
struct ast_category *ast_new_category(char *name)
|
|
|
|
{
|
2004-06-11 00:12:35 +00:00
|
|
|
struct ast_category *category;
|
|
|
|
category = malloc(sizeof(struct ast_category));
|
2004-06-17 04:53:55 +00:00
|
|
|
if (category) {
|
2004-06-11 00:12:35 +00:00
|
|
|
memset(category,0,sizeof(struct ast_category));
|
2004-07-09 10:08:09 +00:00
|
|
|
strncpy(category->name,name,sizeof(category->name) - 1);
|
2004-06-11 00:12:35 +00:00
|
|
|
}
|
|
|
|
return category;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-06-17 04:53:55 +00:00
|
|
|
struct ast_variable *ast_new_variable(char *name, char *value)
|
|
|
|
{
|
2004-06-11 00:12:35 +00:00
|
|
|
struct ast_variable *variable;
|
2004-10-05 06:46:11 +00:00
|
|
|
int length = strlen(name) + strlen(value) + 2 + sizeof(struct ast_variable);
|
|
|
|
variable = malloc(length);
|
2004-06-17 04:53:55 +00:00
|
|
|
if (variable) {
|
2004-10-05 06:46:11 +00:00
|
|
|
memset(variable, 0, length);
|
|
|
|
variable->name = variable->stuff;
|
|
|
|
variable->value = variable->stuff + strlen(name) + 1;
|
2004-06-11 00:12:35 +00:00
|
|
|
variable->object=0;
|
2004-10-05 06:46:11 +00:00
|
|
|
strcpy(variable->name,name);
|
|
|
|
strcpy(variable->value,value);
|
2004-06-11 00:12:35 +00:00
|
|
|
}
|
|
|
|
return variable;
|
|
|
|
}
|
|
|
|
|
2004-06-17 04:53:55 +00:00
|
|
|
int ast_cust_config_register(struct ast_config_reg *new)
|
|
|
|
{
|
2004-06-11 00:12:35 +00:00
|
|
|
ast_config_register(new);
|
|
|
|
read_ast_cust_config();
|
|
|
|
return 1;
|
|
|
|
}
|
2004-06-17 04:53:55 +00:00
|
|
|
int ast_cust_config_deregister(struct ast_config_reg *new)
|
|
|
|
{
|
2004-06-11 00:12:35 +00:00
|
|
|
ast_config_deregister(new);
|
|
|
|
read_ast_cust_config();
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2004-06-17 04:53:55 +00:00
|
|
|
static void clear_cust_keywords(void)
|
|
|
|
{
|
2004-10-05 06:46:11 +00:00
|
|
|
struct ast_config_map *map, *prev;
|
2004-06-11 00:12:35 +00:00
|
|
|
ast_mutex_lock(&ast_cust_config_lock);
|
2004-10-05 06:46:11 +00:00
|
|
|
map = maps;
|
|
|
|
while(map) {
|
|
|
|
prev = map;
|
|
|
|
map = map->next;
|
|
|
|
free(prev);
|
2004-06-11 00:12:35 +00:00
|
|
|
}
|
2004-10-05 06:46:11 +00:00
|
|
|
maps = NULL;
|
2004-06-11 00:12:35 +00:00
|
|
|
ast_mutex_unlock(&ast_cust_config_lock);
|
|
|
|
}
|
|
|
|
|
2004-06-17 04:53:55 +00:00
|
|
|
static int config_command(int fd, int argc, char **argv)
|
|
|
|
{
|
2004-06-11 00:12:35 +00:00
|
|
|
struct ast_config_reg *key;
|
2004-10-05 06:46:11 +00:00
|
|
|
struct ast_config_map *map;
|
2004-06-11 00:12:35 +00:00
|
|
|
|
|
|
|
ast_cli(fd,"\n\n");
|
|
|
|
ast_mutex_lock(&ast_cust_config_lock);
|
2004-06-17 04:53:55 +00:00
|
|
|
for (key=get_config_registrations();key;key=key->next) {
|
2004-06-11 00:12:35 +00:00
|
|
|
ast_cli(fd,"\nConfig Engine: %s\n",key->name);
|
2004-10-05 06:46:11 +00:00
|
|
|
map = maps;
|
|
|
|
while(map) {
|
|
|
|
if (!strcasecmp(map->driver, key->name))
|
|
|
|
ast_cli(fd,"===> %s (db=%s, table=%s)\n",map->name, map->database, map->table ? map->table : map->name);
|
|
|
|
map = map->next;
|
|
|
|
}
|
2004-06-11 00:12:35 +00:00
|
|
|
}
|
|
|
|
ast_mutex_unlock(&ast_cust_config_lock);
|
|
|
|
ast_cli(fd,"\n\n");
|
|
|
|
|
2004-06-17 04:53:55 +00:00
|
|
|
return 0;
|
2004-06-11 00:12:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static struct ast_cli_entry config_command_struct = {
|
|
|
|
{ "show","config","handles", NULL }, config_command,
|
|
|
|
"Show Config Handles", NULL };
|
|
|
|
|
2004-06-17 04:53:55 +00:00
|
|
|
int register_config_cli()
|
|
|
|
{
|
2004-06-11 00:12:35 +00:00
|
|
|
return ast_cli_register(&config_command_struct);
|
|
|
|
}
|
|
|
|
|
2004-06-17 04:53:55 +00:00
|
|
|
int read_ast_cust_config(void)
|
|
|
|
{
|
2004-06-11 00:12:35 +00:00
|
|
|
char *cfg = config_conf_file;
|
|
|
|
struct ast_config *config;
|
|
|
|
struct ast_variable *v;
|
2004-10-05 06:46:11 +00:00
|
|
|
struct ast_config_map *map;
|
|
|
|
int length;
|
|
|
|
char *driver, *table, *database, *stringp;
|
2004-06-17 04:53:55 +00:00
|
|
|
|
2004-06-11 00:12:35 +00:00
|
|
|
clear_cust_keywords();
|
|
|
|
config = ast_load(cfg);
|
2004-06-17 04:53:55 +00:00
|
|
|
if (config) {
|
|
|
|
for (v = ast_variable_browse(config,"settings");v;v=v->next) {
|
2004-10-05 06:46:11 +00:00
|
|
|
stringp = v->value;
|
|
|
|
driver = strsep(&stringp, ",");
|
|
|
|
database = strsep(&stringp, ",");
|
|
|
|
table = strsep(&stringp, ",");
|
2004-06-11 00:12:35 +00:00
|
|
|
|
2004-10-05 06:46:11 +00:00
|
|
|
if (!strcmp(v->name,config_conf_file) || !strcmp(v->name,"asterisk.conf")) {
|
|
|
|
ast_log(LOG_WARNING, "Cannot bind asterisk.conf or extconfig.conf!\n");
|
|
|
|
} else if (driver && database) {
|
|
|
|
length = sizeof(struct ast_config_map);
|
|
|
|
length += strlen(v->name) + 1;
|
|
|
|
length += strlen(driver) + 1;
|
|
|
|
length += strlen(database) + 1;
|
|
|
|
if (table)
|
|
|
|
length += strlen(table) + 1;
|
|
|
|
map = malloc(length);
|
|
|
|
if (map) {
|
|
|
|
memset(map, 0, length);
|
|
|
|
map->name = map->stuff;
|
|
|
|
strcpy(map->name, v->name);
|
|
|
|
map->driver = map->name + strlen(map->name) + 1;
|
|
|
|
strcpy(map->driver, driver);
|
|
|
|
map->database = map->driver + strlen(map->driver) + 1;
|
|
|
|
strcpy(map->database, database);
|
|
|
|
if (table) {
|
|
|
|
map->table = map->database + strlen(map->database) + 1;
|
|
|
|
strcpy(map->table, table);
|
|
|
|
} else
|
|
|
|
map->table = NULL;
|
|
|
|
map->next = maps;
|
|
|
|
if (option_verbose > 1)
|
|
|
|
ast_verbose(VERBOSE_PREFIX_2 "Binding %s to %s/%s/%s\n",map->name,map->driver, map->database, map->table ? map->table : map->name);
|
|
|
|
maps = map;
|
2004-06-11 00:12:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ast_destroy(config);
|
|
|
|
}
|
2004-06-17 04:53:55 +00:00
|
|
|
|
2004-06-11 00:12:35 +00:00
|
|
|
return 0;
|
|
|
|
}
|