Thu Oct 8 00:59:02 2009

Asterisk developer's documentation


config.c

Go to the documentation of this file.
00001 /*
00002  * Asterisk -- An open source telephony toolkit.
00003  *
00004  * Copyright (C) 1999 - 2005, Digium, Inc.
00005  *
00006  * Mark Spencer <markster@digium.com>
00007  *
00008  * See http://www.asterisk.org for more information about
00009  * the Asterisk project. Please do not directly contact
00010  * any of the maintainers of this project for assistance;
00011  * the project provides a web site, mailing lists and IRC
00012  * channels for your use.
00013  *
00014  * This program is free software, distributed under the terms of
00015  * the GNU General Public License Version 2. See the LICENSE file
00016  * at the top of the source tree.
00017  */
00018 
00019 /*! \file
00020  *
00021  * \brief Configuration File Parser
00022  *
00023  * \author Mark Spencer <markster@digium.com>
00024  *
00025  * Includes the Asterisk Realtime API - ARA
00026  * See doc/realtime.txt and doc/extconfig.txt
00027  */
00028 
00029 #include "asterisk.h"
00030 
00031 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 189601 $")
00032 
00033 #include <stdio.h>
00034 #include <unistd.h>
00035 #include <stdlib.h>
00036 #include <string.h>
00037 #include <errno.h>
00038 #include <time.h>
00039 #include <sys/stat.h>
00040 #define AST_INCLUDE_GLOB 1
00041 #ifdef AST_INCLUDE_GLOB
00042 # include <glob.h>
00043 #endif
00044 
00045 #include "asterisk/config.h"
00046 #include "asterisk/cli.h"
00047 #include "asterisk/lock.h"
00048 #include "asterisk/options.h"
00049 #include "asterisk/logger.h"
00050 #include "asterisk/utils.h"
00051 #include "asterisk/channel.h"
00052 #include "asterisk/app.h"
00053 
00054 #define MAX_NESTED_COMMENTS 128
00055 #define COMMENT_START ";--"
00056 #define COMMENT_END "--;"
00057 #define COMMENT_META ';'
00058 #define COMMENT_TAG '-'
00059 
00060 static char *extconfig_conf = "extconfig.conf";
00061 
00062 
00063 /*! \brief Structure to keep comments for rewriting configuration files */
00064 struct ast_comment {
00065    struct ast_comment *next;
00066    char cmt[0];
00067 };
00068 
00069 #define CB_INCR 250
00070 
00071 static void CB_INIT(char **comment_buffer, int *comment_buffer_size, char **lline_buffer, int *lline_buffer_size)
00072 {
00073    if (!(*comment_buffer)) {
00074       *comment_buffer = ast_malloc(CB_INCR);
00075       if (!(*comment_buffer))
00076          return;
00077       (*comment_buffer)[0] = 0;
00078       *comment_buffer_size = CB_INCR;
00079       *lline_buffer = ast_malloc(CB_INCR);
00080       if (!(*lline_buffer))
00081          return;
00082       (*lline_buffer)[0] = 0;
00083       *lline_buffer_size = CB_INCR;
00084    } else {
00085       (*comment_buffer)[0] = 0;
00086       (*lline_buffer)[0] = 0;
00087    }
00088 }
00089 
00090 static void  CB_ADD(char **comment_buffer, int *comment_buffer_size, char *str)
00091 {
00092    int rem = *comment_buffer_size - strlen(*comment_buffer) - 1;
00093    int siz = strlen(str);
00094    if (rem < siz+1) {
00095       *comment_buffer = ast_realloc(*comment_buffer, *comment_buffer_size + CB_INCR + siz + 1);
00096       if (!(*comment_buffer))
00097          return;
00098       *comment_buffer_size += CB_INCR+siz+1;
00099    }
00100    strcat(*comment_buffer,str);
00101 }
00102 
00103 static void  CB_ADD_LEN(char **comment_buffer, int *comment_buffer_size, char *str, int len)
00104 {
00105    int cbl = strlen(*comment_buffer) + 1;
00106    int rem = *comment_buffer_size - cbl;
00107    if (rem < len+1) {
00108       *comment_buffer = ast_realloc(*comment_buffer, *comment_buffer_size + CB_INCR + len + 1);
00109       if (!(*comment_buffer))
00110          return;
00111       *comment_buffer_size += CB_INCR+len+1;
00112    }
00113    strncat(*comment_buffer,str,len);
00114    (*comment_buffer)[cbl+len-1] = 0;
00115 }
00116 
00117 static void  LLB_ADD(char **lline_buffer, int *lline_buffer_size, char *str)
00118 {
00119    int rem = *lline_buffer_size - strlen(*lline_buffer) - 1;
00120    int siz = strlen(str);
00121    if (rem < siz+1) {
00122       *lline_buffer = ast_realloc(*lline_buffer, *lline_buffer_size + CB_INCR + siz + 1);
00123       if (!(*lline_buffer)) 
00124          return;
00125       *lline_buffer_size += CB_INCR + siz + 1;
00126    }
00127    strcat(*lline_buffer,str);
00128 }
00129 
00130 static void CB_RESET(char **comment_buffer, char **lline_buffer)  
00131 { 
00132    (*comment_buffer)[0] = 0; 
00133    (*lline_buffer)[0] = 0;
00134 }
00135 
00136 
00137 static struct ast_comment *ALLOC_COMMENT(const char *buffer)
00138 { 
00139    struct ast_comment *x = ast_calloc(1,sizeof(struct ast_comment)+strlen(buffer)+1);
00140    strcpy(x->cmt, buffer);
00141    return x;
00142 }
00143 
00144 
00145 static struct ast_config_map {
00146    struct ast_config_map *next;
00147    char *name;
00148    char *driver;
00149    char *database;
00150    char *table;
00151    char stuff[0];
00152 } *config_maps = NULL;
00153 
00154 AST_MUTEX_DEFINE_STATIC(config_lock);
00155 static struct ast_config_engine *config_engine_list;
00156 
00157 #define MAX_INCLUDE_LEVEL 10
00158 
00159 struct ast_category_template_instance {
00160    char name[80]; /* redundant? */
00161    const struct ast_category *inst;
00162    AST_LIST_ENTRY(ast_category_template_instance) next;
00163 };
00164 
00165 struct ast_category {
00166    char name[80];
00167    int ignored;         /*!< do not let user of the config see this category */
00168    int include_level;   
00169    AST_LIST_HEAD_NOLOCK(template_instance_list, ast_category_template_instance) template_instances;
00170    struct ast_comment *precomments;
00171    struct ast_comment *sameline;
00172    struct ast_variable *root;
00173    struct ast_variable *last;
00174    struct ast_category *next;
00175 };
00176 
00177 struct ast_config {
00178    struct ast_category *root;
00179    struct ast_category *last;
00180    struct ast_category *current;
00181    struct ast_category *last_browse;      /*!< used to cache the last category supplied via category_browse */
00182    int include_level;
00183    int max_include_level;
00184 };
00185 
00186 struct ast_variable *ast_variable_new(const char *name, const char *value) 
00187 {
00188    struct ast_variable *variable;
00189    int name_len = strlen(name) + 1; 
00190 
00191    if ((variable = ast_calloc(1, name_len + strlen(value) + 1 + sizeof(*variable)))) {
00192       variable->name = variable->stuff;
00193       variable->value = variable->stuff + name_len;      
00194       strcpy(variable->name,name);
00195       strcpy(variable->value,value);
00196    }
00197 
00198    return variable;
00199 }
00200 
00201 void ast_variable_append(struct ast_category *category, struct ast_variable *variable)
00202 {
00203    if (!variable)
00204       return;
00205    if (category->last)
00206       category->last->next = variable;
00207    else
00208       category->root = variable;
00209    category->last = variable;
00210    while (category->last->next)
00211       category->last = category->last->next;
00212 }
00213 
00214 void ast_variables_destroy(struct ast_variable *v)
00215 {
00216    struct ast_variable *vn;
00217 
00218    while(v) {
00219       vn = v;
00220       v = v->next;
00221       free(vn);
00222    }
00223 }
00224 
00225 struct ast_variable *ast_variable_browse(const struct ast_config *config, const char *category)
00226 {
00227    struct ast_category *cat = NULL;
00228 
00229    if (category && config->last_browse && (config->last_browse->name == category))
00230       cat = config->last_browse;
00231    else
00232       cat = ast_category_get(config, category);
00233 
00234    return (cat) ? cat->root : NULL;
00235 }
00236 
00237 const char *ast_config_option(struct ast_config *cfg, const char *cat, const char *var)
00238 {
00239    const char *tmp;
00240    tmp = ast_variable_retrieve(cfg, cat, var);
00241    if (!tmp)
00242       tmp = ast_variable_retrieve(cfg, "general", var);
00243    return tmp;
00244 }
00245 
00246 
00247 struct ast_variable *ast_variable_next(struct ast_config *config, char *category,struct ast_variable *prev)
00248 {
00249    struct ast_variable *v = NULL;
00250    v = ast_variable_browse(config, category);
00251    if(v) {
00252      if(prev) {
00253       while (v) {
00254         if (v == prev)
00255          return v->next;
00256         v=v->next;
00257       }
00258      } else {
00259       return v;
00260      }
00261    }
00262    return NULL;
00263 }
00264 
00265 
00266 const char *ast_variable_retrieve(const struct ast_config *config, const char *category, const char *variable)
00267 {
00268    struct ast_variable *v;
00269 
00270    if (category) {
00271       for (v = ast_variable_browse(config, category); v; v = v->next) {
00272          if (!strcasecmp(variable, v->name))
00273             return v->value;
00274       }
00275    } else {
00276       struct ast_category *cat;
00277 
00278       for (cat = config->root; cat; cat = cat->next)
00279          for (v = cat->root; v; v = v->next)
00280             if (!strcasecmp(variable, v->name))
00281                return v->value;
00282    }
00283 
00284    return NULL;
00285 }
00286 
00287 static struct ast_variable *variable_clone(const struct ast_variable *old)
00288 {
00289    struct ast_variable *new = ast_variable_new(old->name, old->value);
00290 
00291    if (new) {
00292       new->lineno = old->lineno;
00293       new->object = old->object;
00294       new->blanklines = old->blanklines;
00295       /* TODO: clone comments? */
00296    }
00297 
00298    return new;
00299 }
00300  
00301 static void move_variables(struct ast_category *old, struct ast_category *new)
00302 {
00303    struct ast_variable *var = old->root;
00304    old->root = NULL;
00305 #if 1
00306    /* we can just move the entire list in a single op */
00307    ast_variable_append(new, var);
00308 #else
00309    while (var) {
00310       struct ast_variable *next = var->next;
00311       var->next = NULL;
00312       ast_variable_append(new, var);
00313       var = next;
00314    }
00315 #endif
00316 }
00317 
00318 struct ast_category *ast_category_new(const char *name) 
00319 {
00320    struct ast_category *category;
00321 
00322    if ((category = ast_calloc(1, sizeof(*category))))
00323       ast_copy_string(category->name, name, sizeof(category->name));
00324    return category;
00325 }
00326 
00327 static struct ast_category *category_get(const struct ast_config *config, const char *category_name, int ignored)
00328 {
00329    struct ast_category *cat;
00330 
00331    /* try exact match first, then case-insensitive match */
00332    for (cat = config->root; cat; cat = cat->next) {
00333       if (cat->name == category_name && (ignored || !cat->ignored))
00334          return cat;
00335    }
00336 
00337    for (cat = config->root; cat; cat = cat->next) {
00338       if (!strcasecmp(cat->name, category_name) && (ignored || !cat->ignored))
00339          return cat;
00340    }
00341 
00342    return NULL;
00343 }
00344 
00345 struct ast_category *ast_category_get(const struct ast_config *config, const char *category_name)
00346 {
00347    return category_get(config, category_name, 0);
00348 }
00349 
00350 int ast_category_exist(const struct ast_config *config, const char *category_name)
00351 {
00352    return !!ast_category_get(config, category_name);
00353 }
00354 
00355 void ast_category_append(struct ast_config *config, struct ast_category *category)
00356 {
00357    if (config->last)
00358       config->last->next = category;
00359    else
00360       config->root = category;
00361    category->include_level = config->include_level;
00362    config->last = category;
00363    config->current = category;
00364 }
00365 
00366 static void ast_destroy_comments(struct ast_category *cat)
00367 {
00368    struct ast_comment *n, *p;
00369    for (p=cat->precomments; p; p=n) {
00370       n = p->next;
00371       free(p);
00372    }
00373    for (p=cat->sameline; p; p=n) {
00374       n = p->next;
00375       free(p);
00376    }
00377    cat->precomments = NULL;
00378    cat->sameline = NULL;
00379 }
00380 
00381 static void ast_destroy_template_list(struct ast_category *cat)
00382 {
00383    struct ast_category_template_instance *x;
00384    AST_LIST_TRAVERSE_SAFE_BEGIN(&cat->template_instances, x, next) {
00385       AST_LIST_REMOVE_CURRENT(&cat->template_instances, next);
00386       free(x);
00387    }
00388    AST_LIST_TRAVERSE_SAFE_END;
00389 }
00390 
00391 void ast_category_destroy(struct ast_category *cat)
00392 {
00393    ast_variables_destroy(cat->root);
00394    ast_destroy_comments(cat);
00395    ast_destroy_template_list(cat);
00396    free(cat);
00397 }
00398 
00399 static struct ast_category *next_available_category(struct ast_category *cat)
00400 {
00401    for (; cat && cat->ignored; cat = cat->next);
00402 
00403    return cat;
00404 }
00405 
00406 struct ast_variable *ast_category_root(struct ast_config *config, char *cat)
00407 {
00408    struct ast_category *category = ast_category_get(config, cat);
00409    if (category)
00410       return category->root;
00411    return NULL;
00412 }
00413 
00414 char *ast_category_browse(struct ast_config *config, const char *prev)
00415 {  
00416    struct ast_category *cat = NULL;
00417 
00418    if (prev && config->last_browse && (config->last_browse->name == prev))
00419       cat = config->last_browse->next;
00420    else if (!prev && config->root)
00421       cat = config->root;
00422    else if (prev) {
00423       for (cat = config->root; cat; cat = cat->next) {
00424          if (cat->name == prev) {
00425             cat = cat->next;
00426             break;
00427          }
00428       }
00429       if (!cat) {
00430          for (cat = config->root; cat; cat = cat->next) {
00431             if (!strcasecmp(cat->name, prev)) {
00432                cat = cat->next;
00433                break;
00434             }
00435          }
00436       }
00437    }
00438    
00439    if (cat)
00440       cat = next_available_category(cat);
00441 
00442    config->last_browse = cat;
00443    return (cat) ? cat->name : NULL;
00444 }
00445 
00446 struct ast_variable *ast_category_detach_variables(struct ast_category *cat)
00447 {
00448    struct ast_variable *v;
00449 
00450    v = cat->root;
00451    cat->root = NULL;
00452    cat->last = NULL;
00453 
00454    return v;
00455 }
00456 
00457 void ast_category_rename(struct ast_category *cat, const char *name)
00458 {
00459    ast_copy_string(cat->name, name, sizeof(cat->name));
00460 }
00461 
00462 static void inherit_category(struct ast_category *new, const struct ast_category *base)
00463 {
00464    struct ast_variable *var;
00465    struct ast_category_template_instance *x = ast_calloc(1,sizeof(struct ast_category_template_instance));
00466    strcpy(x->name, base->name);
00467    x->inst = base;
00468    AST_LIST_INSERT_TAIL(&new->template_instances, x, next);
00469    for (var = base->root; var; var = var->next)
00470       ast_variable_append(new, variable_clone(var));
00471 }
00472 
00473 struct ast_config *ast_config_new(void) 
00474 {
00475    struct ast_config *config;
00476 
00477    if ((config = ast_calloc(1, sizeof(*config))))
00478       config->max_include_level = MAX_INCLUDE_LEVEL;
00479    return config;
00480 }
00481 
00482 int ast_variable_delete(struct ast_category *category, char *variable, char *match)
00483 {
00484    struct ast_variable *cur, *prev=NULL, *curn;
00485    int res = -1;
00486    cur = category->root;
00487    while (cur) {
00488       if (cur->name == variable) {
00489          if (prev) {
00490             prev->next = cur->next;
00491             if (cur == category->last)
00492                category->last = prev;
00493          } else {
00494             category->root = cur->next;
00495             if (cur == category->last)
00496                category->last = NULL;
00497          }
00498          cur->next = NULL;
00499          ast_variables_destroy(cur);
00500          return 0;
00501       }
00502       prev = cur;
00503       cur = cur->next;
00504    }
00505 
00506    prev = NULL;
00507    cur = category->root;
00508    while (cur) {
00509       curn = cur->next;
00510       if (!strcasecmp(cur->name, variable) && (ast_strlen_zero(match) || !strcasecmp(cur->value, match))) {
00511          if (prev) {
00512             prev->next = cur->next;
00513             if (cur == category->last)
00514                category->last = prev;
00515          } else {
00516             category->root = cur->next;
00517             if (cur == category->last)
00518                category->last = NULL;
00519          }
00520          cur->next = NULL;
00521          ast_variables_destroy(cur);
00522          res = 0;
00523       } else
00524          prev = cur;
00525 
00526       cur = curn;
00527    }
00528    return res;
00529 }
00530 
00531 int ast_variable_update(struct ast_category *category, const char *variable, 
00532    const char *value, const char *match, unsigned int object)
00533 {
00534    struct ast_variable *cur, *prev=NULL, *newer;
00535 
00536    if (!(newer = ast_variable_new(variable, value)))
00537       return -1;
00538    
00539    newer->object = object;
00540 
00541    for (cur = category->root; cur; prev = cur, cur = cur->next) {
00542       if (strcasecmp(cur->name, variable) ||
00543          (!ast_strlen_zero(match) && strcasecmp(cur->value, match)))
00544          continue;
00545 
00546       newer->next = cur->next;
00547       newer->object = cur->object || object;
00548       if (prev)
00549          prev->next = newer;
00550       else
00551          category->root = newer;
00552       if (category->last == cur)
00553          category->last = newer;
00554 
00555       cur->next = NULL;
00556       ast_variables_destroy(cur);
00557 
00558       return 0;
00559    }
00560 
00561    if (prev)
00562       prev->next = newer;
00563    else
00564       category->root = newer;
00565 
00566    return 0;
00567 }
00568 
00569 int ast_category_delete(struct ast_config *cfg, char *category)
00570 {
00571    struct ast_category *prev=NULL, *cat;
00572    cat = cfg->root;
00573    while(cat) {
00574       if (cat->name == category) {
00575          if (prev) {
00576             prev->next = cat->next;
00577             if (cat == cfg->last)
00578                cfg->last = prev;
00579          } else {
00580             cfg->root = cat->next;
00581             if (cat == cfg->last)
00582                cfg->last = NULL;
00583          }
00584          ast_category_destroy(cat);
00585          return 0;
00586       }
00587       prev = cat;
00588       cat = cat->next;
00589    }
00590 
00591    prev = NULL;
00592    cat = cfg->root;
00593    while(cat) {
00594       if (!strcasecmp(cat->name, category)) {
00595          if (prev) {
00596             prev->next = cat->next;
00597             if (cat == cfg->last)
00598                cfg->last = prev;
00599          } else {
00600             cfg->root = cat->next;
00601             if (cat == cfg->last)
00602                cfg->last = NULL;
00603          }
00604          ast_category_destroy(cat);
00605          return 0;
00606       }
00607       prev = cat;
00608       cat = cat->next;
00609    }
00610    return -1;
00611 }
00612 
00613 void ast_config_destroy(struct ast_config *cfg)
00614 {
00615    struct ast_category *cat, *catn;
00616 
00617    if (!cfg)
00618       return;
00619 
00620    cat = cfg->root;
00621    while(cat) {
00622       catn = cat;
00623       cat = cat->next;
00624       ast_category_destroy(catn);
00625    }
00626    free(cfg);
00627 }
00628 
00629 struct ast_category *ast_config_get_current_category(const struct ast_config *cfg)
00630 {
00631    return cfg->current;
00632 }
00633 
00634 void ast_config_set_current_category(struct ast_config *cfg, const struct ast_category *cat)
00635 {
00636    /* cast below is just to silence compiler warning about dropping "const" */
00637    cfg->current = (struct ast_category *) cat;
00638 }
00639 
00640 static int process_text_line(struct ast_config *cfg, struct ast_category **cat, char *buf, int lineno, const char *configfile, int withcomments,
00641             char **comment_buffer, int *comment_buffer_size, char **lline_buffer, int *lline_buffer_size)
00642 {
00643    char *c;
00644    char *cur = buf;
00645    struct ast_variable *v;
00646    char cmd[512], exec_file[512];
00647    int object, do_exec, do_include;
00648 
00649    /* Actually parse the entry */
00650    if (cur[0] == '[') {
00651       struct ast_category *newcat = NULL;
00652       char *catname;
00653 
00654       /* A category header */
00655       c = strchr(cur, ']');
00656       if (!c) {
00657          ast_log(LOG_WARNING, "parse error: no closing ']', line %d of %s\n", lineno, configfile);
00658          return -1;
00659       }
00660       *c++ = '\0';
00661       cur++;
00662       if (*c++ != '(')
00663          c = NULL;
00664       catname = cur;
00665       if (!(*cat = newcat = ast_category_new(catname))) {
00666          return -1;
00667       }
00668       /* add comments */
00669       if (withcomments && *comment_buffer && (*comment_buffer)[0] ) {
00670          newcat->precomments = ALLOC_COMMENT(*comment_buffer);
00671       }
00672       if (withcomments && *lline_buffer && (*lline_buffer)[0] ) {
00673          newcat->sameline = ALLOC_COMMENT(*lline_buffer);
00674       }
00675       if( withcomments )
00676          CB_RESET(comment_buffer, lline_buffer);
00677       
00678       /* If there are options or categories to inherit from, process them now */
00679       if (c) {
00680          if (!(cur = strchr(c, ')'))) {
00681             ast_log(LOG_WARNING, "parse error: no closing ')', line %d of %s\n", lineno, configfile);
00682             return -1;
00683          }
00684          *cur = '\0';
00685          while ((cur = strsep(&c, ","))) {
00686             if (!strcasecmp(cur, "!")) {
00687                (*cat)->ignored = 1;
00688             } else if (!strcasecmp(cur, "+")) {
00689                *cat = category_get(cfg, catname, 1);
00690                if (!(*cat)) {
00691                   if (newcat)
00692                      ast_category_destroy(newcat);
00693                   ast_log(LOG_WARNING, "Category addition requested, but category '%s' does not exist, line %d of %s\n", catname, lineno, configfile);
00694                   return -1;
00695                }
00696                if (newcat) {
00697                   move_variables(newcat, *cat);
00698                   ast_category_destroy(newcat);
00699                   newcat = NULL;
00700                }
00701             } else {
00702                struct ast_category *base;
00703             
00704                base = category_get(cfg, cur, 1);
00705                if (!base) {
00706                   ast_log(LOG_WARNING, "Inheritance requested, but category '%s' does not exist, line %d of %s\n", cur, lineno, configfile);
00707                   return -1;
00708                }
00709                inherit_category(*cat, base);
00710             }
00711          }
00712       }
00713       if (newcat)
00714          ast_category_append(cfg, *cat);
00715    } else if (cur[0] == '#') {
00716       /* A directive */
00717       cur++;
00718       c = cur;
00719       while(*c && (*c > 32)) c++;
00720       if (*c) {
00721          *c = '\0';
00722          /* Find real argument */
00723          c = ast_skip_blanks(c + 1);
00724          if (!(*c))
00725             c = NULL;
00726       } else 
00727          c = NULL;
00728       do_include = !strcasecmp(cur, "include");
00729       if(!do_include)
00730          do_exec = !strcasecmp(cur, "exec");
00731       else
00732          do_exec = 0;
00733       if (do_exec && !ast_opt_exec_includes) {
00734          ast_log(LOG_WARNING, "Cannot perform #exec unless execincludes option is enabled in asterisk.conf (options section)!\n");
00735          do_exec = 0;
00736       }
00737       if (do_include || do_exec) {
00738          if (c) {
00739             /* Strip off leading and trailing "'s and <>'s */
00740             while((*c == '<') || (*c == '>') || (*c == '\"')) c++;
00741             /* Get rid of leading mess */
00742             cur = c;
00743             while (!ast_strlen_zero(cur)) {
00744                c = cur + strlen(cur) - 1;
00745                if ((*c == '>') || (*c == '<') || (*c == '\"'))
00746                   *c = '\0';
00747                else
00748                   break;
00749             }
00750             /* #exec </path/to/executable>
00751                We create a tmp file, then we #include it, then we delete it. */
00752             if (do_exec) { 
00753                snprintf(exec_file, sizeof(exec_file), "/var/tmp/exec.%d.%ld", (int)time(NULL), (long)pthread_self());
00754                snprintf(cmd, sizeof(cmd), "%s > %s 2>&1", cur, exec_file);
00755                ast_safe_system(cmd);
00756                cur = exec_file;
00757             } else
00758                exec_file[0] = '\0';
00759             /* A #include */
00760             do_include = ast_config_internal_load(cur, cfg, withcomments) ? 1 : 0;
00761             if(!ast_strlen_zero(exec_file))
00762                unlink(exec_file);
00763             if (!do_include) {
00764                ast_log(LOG_ERROR, "*********************************************************\n");
00765                ast_log(LOG_ERROR, "*********** YOU SHOULD REALLY READ THIS ERROR ***********\n");
00766                ast_log(LOG_ERROR, "Future versions of Asterisk will treat a #include of a "
00767                                   "file that does not exist as an error, and will fail to "
00768                                   "load that configuration file.  Please ensure that the "
00769                                   "file '%s' exists, even if it is empty.\n", cur);
00770                ast_log(LOG_ERROR, "*********** YOU SHOULD REALLY READ THIS ERROR ***********\n");
00771                ast_log(LOG_ERROR, "*********************************************************\n");
00772                return 0;
00773             }
00774 
00775          } else {
00776             ast_log(LOG_WARNING, "Directive '#%s' needs an argument (%s) at line %d of %s\n", 
00777                   do_exec ? "exec" : "include",
00778                   do_exec ? "/path/to/executable" : "filename",
00779                   lineno,
00780                   configfile);
00781          }
00782       }
00783       else 
00784          ast_log(LOG_WARNING, "Unknown directive '#%s' at line %d of %s\n", cur, lineno, configfile);
00785    } else {
00786       /* Just a line (variable = value) */
00787       if (!(*cat)) {
00788          ast_log(LOG_WARNING,
00789             "parse error: No category context for line %d of %s\n", lineno, configfile);
00790          return -1;
00791       }
00792       c = strchr(cur, '=');
00793       if (c) {
00794          *c = 0;
00795          c++;
00796          /* Ignore > in => */
00797          if (*c== '>') {
00798             object = 1;
00799             c++;
00800          } else
00801             object = 0;
00802          if ((v = ast_variable_new(ast_strip(cur), ast_strip(c)))) {
00803             v->lineno = lineno;
00804             v->object = object;
00805             /* Put and reset comments */
00806             v->blanklines = 0;
00807             ast_variable_append(*cat, v);
00808             /* add comments */
00809             if (withcomments && *comment_buffer && (*comment_buffer)[0] ) {
00810                v->precomments = ALLOC_COMMENT(*comment_buffer);
00811             }
00812             if (withcomments && *lline_buffer && (*lline_buffer)[0] ) {
00813                v->sameline = ALLOC_COMMENT(*lline_buffer);
00814             }
00815             if( withcomments )
00816                CB_RESET(comment_buffer, lline_buffer);
00817             
00818          } else {
00819             return -1;
00820          }
00821       } else {
00822          ast_log(LOG_WARNING, "No '=' (equal sign) in line %d of %s\n", lineno, configfile);
00823       }
00824    }
00825    return 0;
00826 }
00827 
00828 static struct ast_config *config_text_file_load(const char *database, const char *table, const char *filename, struct ast_config *cfg, int withcomments)
00829 {
00830    char fn[256];
00831 #if defined(LOW_MEMORY)
00832    char buf[512];
00833 #else
00834    char buf[8192];
00835 #endif
00836    char *new_buf, *comment_p, *process_buf;
00837    FILE *f;
00838    int lineno=0;
00839    int comment = 0, nest[MAX_NESTED_COMMENTS];
00840    struct ast_category *cat = NULL;
00841    int count = 0;
00842    struct stat statbuf;
00843    /*! Growable string buffer */
00844    char *comment_buffer=0;   /*!< this will be a comment collector.*/
00845    int   comment_buffer_size=0;  /*!< the amount of storage so far alloc'd for the comment_buffer */
00846 
00847    char *lline_buffer=0;    /*!< A buffer for stuff behind the ; */
00848    int  lline_buffer_size=0;
00849 
00850    
00851    cat = ast_config_get_current_category(cfg);
00852 
00853    if (filename[0] == '/') {
00854       ast_copy_string(fn, filename, sizeof(fn));
00855    } else {
00856       snprintf(fn, sizeof(fn), "%s/%s", (char *)ast_config_AST_CONFIG_DIR, filename);
00857    }
00858 
00859    if (withcomments) {
00860       CB_INIT(&comment_buffer, &comment_buffer_size, &lline_buffer, &lline_buffer_size);
00861       if (!lline_buffer || !comment_buffer) {
00862          ast_log(LOG_ERROR, "Failed to initialize the comment buffer!\n");
00863          return NULL;
00864       }
00865    }
00866 #ifdef AST_INCLUDE_GLOB
00867    {
00868       int glob_ret;
00869       glob_t globbuf;
00870       globbuf.gl_offs = 0; /* initialize it to silence gcc */
00871       glob_ret = glob(fn, MY_GLOB_FLAGS, NULL, &globbuf);
00872       if (glob_ret == GLOB_NOSPACE)
00873          ast_log(LOG_WARNING,
00874             "Glob Expansion of pattern '%s' failed: Not enough memory\n", fn);
00875       else if (glob_ret  == GLOB_ABORTED)
00876          ast_log(LOG_WARNING,
00877             "Glob Expansion of pattern '%s' failed: Read error\n", fn);
00878       else  {
00879          /* loop over expanded files */
00880          int i;
00881          for (i=0; i<globbuf.gl_pathc; i++) {
00882             ast_copy_string(fn, globbuf.gl_pathv[i], sizeof(fn));
00883 #endif
00884    do {
00885       if (stat(fn, &statbuf))
00886          continue;
00887 
00888       if (!S_ISREG(statbuf.st_mode)) {
00889          ast_log(LOG_WARNING, "'%s' is not a regular file, ignoring\n", fn);
00890          continue;
00891       }
00892       if (option_verbose > 1) {
00893          ast_verbose(VERBOSE_PREFIX_2 "Parsing '%s': ", fn);
00894          fflush(stdout);
00895       }
00896       if (!(f = fopen(fn, "r"))) {
00897          if (option_debug)
00898             ast_log(LOG_DEBUG, "No file to parse: %s\n", fn);
00899          if (option_verbose > 1)
00900             ast_verbose( "Not found (%s)\n", strerror(errno));
00901          continue;
00902       }
00903       count++;
00904       if (option_debug)
00905          ast_log(LOG_DEBUG, "Parsing %s\n", fn);
00906       if (option_verbose > 1)
00907          ast_verbose("Found\n");
00908       while(!feof(f)) {
00909          lineno++;
00910          if (fgets(buf, sizeof(buf), f)) {
00911             if ( withcomments ) {
00912                CB_ADD(&comment_buffer, &comment_buffer_size, lline_buffer);       /* add the current lline buffer to the comment buffer */
00913                lline_buffer[0] = 0;        /* erase the lline buffer */
00914             }
00915             
00916             new_buf = buf;
00917             if (comment) 
00918                process_buf = NULL;
00919             else
00920                process_buf = buf;
00921             
00922             while ((comment_p = strchr(new_buf, COMMENT_META))) {
00923                if ((comment_p > new_buf) && (*(comment_p-1) == '\\')) {
00924                   /* Escaped semicolons aren't comments. */
00925                   new_buf = comment_p + 1;
00926                } else if(comment_p[1] == COMMENT_TAG && comment_p[2] == COMMENT_TAG && (comment_p[3] != '-')) {
00927                   /* Meta-Comment start detected ";--" */
00928                   if (comment < MAX_NESTED_COMMENTS) {
00929                      *comment_p = '\0';
00930                      new_buf = comment_p + 3;
00931                      comment++;
00932                      nest[comment-1] = lineno;
00933                   } else {
00934                      ast_log(LOG_ERROR, "Maximum nest limit of %d reached.\n", MAX_NESTED_COMMENTS);
00935                   }
00936                } else if ((comment_p >= new_buf + 2) &&
00937                      (*(comment_p - 1) == COMMENT_TAG) &&
00938                      (*(comment_p - 2) == COMMENT_TAG)) {
00939                   /* Meta-Comment end detected */
00940                   comment--;
00941                   new_buf = comment_p + 1;
00942                   if (!comment) {
00943                      /* Back to non-comment now */
00944                      if (process_buf) {
00945                         /* Actually have to move what's left over the top, then continue */
00946                         char *oldptr;
00947                         oldptr = process_buf + strlen(process_buf);
00948                         if ( withcomments ) {
00949                            CB_ADD(&comment_buffer, &comment_buffer_size, ";");
00950                            CB_ADD_LEN(&comment_buffer, &comment_buffer_size, oldptr+1, new_buf-oldptr-1);
00951                         }
00952                         
00953                         memmove(oldptr, new_buf, strlen(new_buf) + 1);
00954                         new_buf = oldptr;
00955                      } else
00956                         process_buf = new_buf;
00957                   }
00958                } else {
00959                   if (!comment) {
00960                      /* If ; is found, and we are not nested in a comment, 
00961                         we immediately stop all comment processing */
00962                      if ( withcomments ) {
00963                         LLB_ADD(&lline_buffer, &lline_buffer_size, comment_p);
00964                      }
00965                      *comment_p = '\0'; 
00966                      new_buf = comment_p;
00967                   } else
00968                      new_buf = comment_p + 1;
00969                }
00970             }
00971             if( withcomments && comment && !process_buf )
00972             {
00973                CB_ADD(&comment_buffer, &comment_buffer_size, buf);  /* the whole line is a comment, store it */
00974             }
00975             
00976             if (process_buf) {
00977                char *buf = ast_strip(process_buf);
00978                if (!ast_strlen_zero(buf)) {
00979                   if (process_text_line(cfg, &cat, buf, lineno, fn, withcomments, &comment_buffer, &comment_buffer_size, &lline_buffer, &lline_buffer_size)) {
00980                      cfg = NULL;
00981                      break;
00982                   }
00983                }
00984             }
00985          }
00986       }
00987       fclose(f);     
00988    } while(0);
00989    if (comment) {
00990       ast_log(LOG_WARNING,"Unterminated comment detected beginning on line %d\n", nest[comment - 1]);
00991    }
00992 #ifdef AST_INCLUDE_GLOB
00993                if (!cfg)
00994                   break;
00995             }
00996             globfree(&globbuf);
00997          }
00998       }
00999 #endif
01000 
01001    if (cfg && cfg->include_level == 1 && withcomments && comment_buffer) {
01002       free(comment_buffer);
01003       free(lline_buffer);
01004       comment_buffer = NULL;
01005       lline_buffer = NULL;
01006       comment_buffer_size = 0;
01007       lline_buffer_size = 0;
01008    }
01009    
01010    if (count == 0)
01011       return NULL;
01012 
01013    return cfg;
01014 }
01015 
01016 int config_text_file_save(const char *configfile, const struct ast_config *cfg, const char *generator)
01017 {
01018    FILE *f = NULL;
01019    int fd = -1;
01020    char fn[256], fntmp[256];
01021    char date[256]="";
01022    time_t t;
01023    struct ast_variable *var;
01024    struct ast_category *cat;
01025    struct ast_comment *cmt;
01026    struct stat s;
01027    int blanklines = 0;
01028    int stat_result = 0;
01029 
01030    if (configfile[0] == '/') {
01031       snprintf(fntmp, sizeof(fntmp), "%s.XXXXXX", configfile);
01032       ast_copy_string(fn, configfile, sizeof(fn));
01033    } else {
01034       snprintf(fntmp, sizeof(fntmp), "%s/%s.XXXXXX", ast_config_AST_CONFIG_DIR, configfile);
01035       snprintf(fn, sizeof(fn), "%s/%s", ast_config_AST_CONFIG_DIR, configfile);
01036    }
01037    time(&t);
01038    ast_copy_string(date, ctime(&t), sizeof(date));
01039    if ((fd = mkstemp(fntmp)) > 0 && (f = fdopen(fd, "w")) != NULL) {
01040       if (option_verbose > 1)
01041          ast_verbose(VERBOSE_PREFIX_2 "Saving '%s': ", fn);
01042       fprintf(f, ";!\n");
01043       fprintf(f, ";! Automatically generated configuration file\n");
01044       if (strcmp(configfile, fn))
01045          fprintf(f, ";! Filename: %s (%s)\n", configfile, fn);
01046       else
01047          fprintf(f, ";! Filename: %s\n", configfile);
01048       fprintf(f, ";! Generator: %s\n", generator);
01049       fprintf(f, ";! Creation Date: %s", date);
01050       fprintf(f, ";!\n");
01051       cat = cfg->root;
01052       while(cat) {
01053          /* Dump section with any appropriate comment */
01054          for (cmt = cat->precomments; cmt; cmt=cmt->next)
01055          {
01056             if (cmt->cmt[0] != ';' || cmt->cmt[1] != '!')
01057                fprintf(f,"%s", cmt->cmt);
01058          }
01059          if (!cat->precomments)
01060             fprintf(f,"\n");
01061          fprintf(f, "[%s]", cat->name);
01062          if (cat->ignored || !AST_LIST_EMPTY(&cat->template_instances)) {
01063             fprintf(f, "(");
01064             if (cat->ignored) {
01065                fprintf(f, "!");
01066             }
01067             if (cat->ignored && !AST_LIST_EMPTY(&cat->template_instances)) {
01068                fprintf(f, ",");
01069             }
01070             if (!AST_LIST_EMPTY(&cat->template_instances)) {
01071                struct ast_category_template_instance *x;
01072                AST_LIST_TRAVERSE(&cat->template_instances, x, next) {
01073                   fprintf(f,"%s",x->name);
01074                   if (x != AST_LIST_LAST(&cat->template_instances))
01075                      fprintf(f,",");
01076                }
01077             }
01078             fprintf(f, ")");
01079          }
01080          for(cmt = cat->sameline; cmt; cmt=cmt->next)
01081          {
01082             fprintf(f,"%s", cmt->cmt);
01083          }
01084          if (!cat->sameline)
01085             fprintf(f,"\n");
01086          var = cat->root;
01087          while(var) {
01088             struct ast_category_template_instance *x;
01089             struct ast_variable *v2;
01090             int found = 0;
01091             AST_LIST_TRAVERSE(&cat->template_instances, x, next) {
01092                
01093                for (v2 = x->inst->root; v2; v2 = v2->next) {
01094                   if (!strcasecmp(var->name, v2->name))
01095                      break;
01096                }
01097                if (v2 && v2->value && !strcmp(v2->value, var->value)) {
01098                   found = 1;
01099                   break;
01100                }
01101             }
01102             if (found) {
01103                var = var->next;
01104                continue;
01105             }
01106             for (cmt = var->precomments; cmt; cmt=cmt->next)
01107             {
01108                if (cmt->cmt[0] != ';' || cmt->cmt[1] != '!')
01109                   fprintf(f,"%s", cmt->cmt);
01110             }
01111             if (var->sameline) 
01112                fprintf(f, "%s %s %s  %s", var->name, (var->object ? "=>" : "="), var->value, var->sameline->cmt);
01113             else  
01114                fprintf(f, "%s %s %s\n", var->name, (var->object ? "=>" : "="), var->value);
01115             if (var->blanklines) {
01116                blanklines = var->blanklines;
01117                while (blanklines--)
01118                   fprintf(f, "\n");
01119             }
01120                
01121             var = var->next;
01122          }
01123 #if 0
01124          /* Put an empty line */
01125          fprintf(f, "\n");
01126 #endif
01127          cat = cat->next;
01128       }
01129       if ((option_verbose > 1) && !option_debug)
01130          ast_verbose("Saved\n");
01131    } else {
01132       if (option_debug)
01133          ast_log(LOG_DEBUG, "Unable to open for writing: %s (%s)\n", fn, strerror(errno));
01134       if (option_verbose > 1)
01135          ast_verbose(VERBOSE_PREFIX_2 "Unable to write %s (%s)", fn, strerror(errno));
01136       if (fd > -1)
01137          close(fd);
01138       return -1;
01139    }
01140    if (!(stat_result = stat(fn, &s))) {
01141       fchmod(fd, s.st_mode);
01142    }
01143    fclose(f);
01144    if ((!stat_result && unlink(fn)) || link(fntmp, fn)) {
01145       if (option_debug)
01146          ast_log(LOG_DEBUG, "Unable to open for writing: %s (%s)\n", fn, strerror(errno));
01147       if (option_verbose > 1)
01148          ast_verbose(VERBOSE_PREFIX_2 "Unable to write %s (%s)", fn, strerror(errno));
01149       unlink(fntmp);
01150       return -1;
01151    }
01152    unlink(fntmp);
01153    return 0;
01154 }
01155 
01156 static void clear_config_maps(void) 
01157 {
01158    struct ast_config_map *map;
01159 
01160    ast_mutex_lock(&config_lock);
01161 
01162    while (config_maps) {
01163       map = config_maps;
01164       config_maps = config_maps->next;
01165       free(map);
01166    }
01167       
01168    ast_mutex_unlock(&config_lock);
01169 }
01170 
01171 static int append_mapping(char *name, char *driver, char *database, char *table)
01172 {
01173    struct ast_config_map *map;
01174    int length;
01175 
01176    length = sizeof(*map);
01177    length += strlen(name) + 1;
01178    length += strlen(driver) + 1;
01179    length += strlen(database) + 1;
01180    if (table)
01181       length += strlen(table) + 1;
01182 
01183    if (!(map = ast_calloc(1, length)))
01184       return -1;
01185 
01186    map->name = map->stuff;
01187    strcpy(map->name, name);
01188    map->driver = map->name + strlen(map->name) + 1;
01189    strcpy(map->driver, driver);
01190    map->database = map->driver + strlen(map->driver) + 1;
01191    strcpy(map->database, database);
01192    if (table) {
01193       map->table = map->database + strlen(map->database) + 1;
01194       strcpy(map->table, table);
01195    }
01196    map->next = config_maps;
01197 
01198    if (option_verbose > 1)
01199       ast_verbose(VERBOSE_PREFIX_2 "Binding %s to %s/%s/%s\n",
01200              map->name, map->driver, map->database, map->table ? map->table : map->name);
01201 
01202    config_maps = map;
01203    return 0;
01204 }
01205 
01206 int read_config_maps(void) 
01207 {
01208    struct ast_config *config, *configtmp;
01209    struct ast_variable *v;
01210    char *driver, *table, *database, *stringp, *tmp;
01211 
01212    clear_config_maps();
01213 
01214    configtmp = ast_config_new();
01215    configtmp->max_include_level = 1;
01216    config = ast_config_internal_load(extconfig_conf, configtmp, 0);
01217    if (!config) {
01218       ast_config_destroy(configtmp);
01219       return 0;
01220    }
01221 
01222    for (v = ast_variable_browse(config, "settings"); v; v = v->next) {
01223       stringp = v->value;
01224       driver = strsep(&stringp, ",");
01225 
01226       if ((tmp = strchr(stringp, '\"')))
01227          stringp = tmp;
01228 
01229       /* check if the database text starts with a double quote */
01230       if (*stringp == '"') {
01231          stringp++;
01232          database = strsep(&stringp, "\"");
01233          strsep(&stringp, ",");
01234       } else {
01235          /* apparently this text has no quotes */
01236          database = strsep(&stringp, ",");
01237       }
01238 
01239       table = strsep(&stringp, ",");
01240 
01241       if (!strcmp(v->name, extconfig_conf)) {
01242          ast_log(LOG_WARNING, "Cannot bind '%s'!\n", extconfig_conf);
01243          continue;
01244       }
01245 
01246       if (!strcmp(v->name, "asterisk.conf")) {
01247          ast_log(LOG_WARNING, "Cannot bind 'asterisk.conf'!\n");
01248          continue;
01249       }
01250 
01251       if (!strcmp(v->name, "logger.conf")) {
01252          ast_log(LOG_WARNING, "Cannot bind 'logger.conf'!\n");
01253          continue;
01254       }
01255 
01256       if (!driver || !database)
01257          continue;
01258       if (!strcasecmp(v->name, "sipfriends")) {
01259          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");
01260          append_mapping("sipusers", driver, database, table ? table : "sipfriends");
01261          append_mapping("sippeers", driver, database, table ? table : "sipfriends");
01262       } else if (!strcasecmp(v->name, "iaxfriends")) {
01263          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");
01264          append_mapping("iaxusers", driver, database, table ? table : "iaxfriends");
01265          append_mapping("iaxpeers", driver, database, table ? table : "iaxfriends");
01266       } else 
01267          append_mapping(v->name, driver, database, table);
01268    }
01269       
01270    ast_config_destroy(config);
01271    return 0;
01272 }
01273 
01274 int ast_config_engine_register(struct ast_config_engine *new) 
01275 {
01276    struct ast_config_engine *ptr;
01277 
01278    ast_mutex_lock(&config_lock);
01279 
01280    if (!config_engine_list) {
01281       config_engine_list = new;
01282    } else {
01283       for (ptr = config_engine_list; ptr->next; ptr=ptr->next);
01284       ptr->next = new;
01285    }
01286 
01287    ast_mutex_unlock(&config_lock);
01288    ast_log(LOG_NOTICE,"Registered Config Engine %s\n", new->name);
01289 
01290    return 1;
01291 }
01292 
01293 int ast_config_engine_deregister(struct ast_config_engine *del) 
01294 {
01295    struct ast_config_engine *ptr, *last=NULL;
01296 
01297    ast_mutex_lock(&config_lock);
01298 
01299    for (ptr = config_engine_list; ptr; ptr=ptr->next) {
01300       if (ptr == del) {
01301          if (last)
01302             last->next = ptr->next;
01303          else
01304             config_engine_list = ptr->next;
01305          break;
01306       }
01307       last = ptr;
01308    }
01309 
01310    ast_mutex_unlock(&config_lock);
01311 
01312    return 0;
01313 }
01314 
01315 /*! \brief Find realtime engine for realtime family */
01316 static struct ast_config_engine *find_engine(const char *family, char *database, int dbsiz, char *table, int tabsiz) 
01317 {
01318    struct ast_config_engine *eng, *ret = NULL;
01319    struct ast_config_map *map;
01320 
01321    ast_mutex_lock(&config_lock);
01322 
01323    for (map = config_maps; map; map = map->next) {
01324       if (!strcasecmp(family, map->name)) {
01325          if (database)
01326             ast_copy_string(database, map->database, dbsiz);
01327          if (table)
01328             ast_copy_string(table, map->table ? map->table : family, tabsiz);
01329          break;
01330       }
01331    }
01332 
01333    /* Check if the required driver (engine) exist */
01334    if (map) {
01335       for (eng = config_engine_list; !ret && eng; eng = eng->next) {
01336          if (!strcasecmp(eng->name, map->driver))
01337             ret = eng;
01338       }
01339    }
01340 
01341    ast_mutex_unlock(&config_lock);
01342    
01343    /* if we found a mapping, but the engine is not available, then issue a warning */
01344    if (map && !ret)
01345       ast_log(LOG_WARNING, "Realtime mapping for '%s' found to engine '%s', but the engine is not available\n", map->name, map->driver);
01346 
01347    return ret;
01348 }
01349 
01350 static struct ast_config_engine text_file_engine = {
01351    .name = "text",
01352    .load_func = config_text_file_load,
01353 };
01354 
01355 struct ast_config *ast_config_internal_load(const char *filename, struct ast_config *cfg, int withcomments)
01356 {
01357    char db[256];
01358    char table[256];
01359    struct ast_config_engine *loader = &text_file_engine;
01360    struct ast_config *result; 
01361 
01362    /* The config file itself bumps include_level by 1 */
01363    if (cfg->max_include_level > 0 && cfg->include_level == cfg->max_include_level + 1) {
01364       ast_log(LOG_WARNING, "Maximum Include level (%d) exceeded\n", cfg->max_include_level);
01365       return NULL;
01366    }
01367 
01368    cfg->include_level++;
01369 
01370    if (strcmp(filename, extconfig_conf) && strcmp(filename, "asterisk.conf") && config_engine_list) {
01371       struct ast_config_engine *eng;
01372 
01373       eng = find_engine(filename, db, sizeof(db), table, sizeof(table));
01374 
01375 
01376       if (eng && eng->load_func) {
01377          loader = eng;
01378       } else {
01379          eng = find_engine("global", db, sizeof(db), table, sizeof(table));
01380          if (eng && eng->load_func)
01381             loader = eng;
01382       }
01383    }
01384 
01385    result = loader->load_func(db, table, filename, cfg, withcomments);
01386 
01387    if (result)
01388       result->include_level--;
01389    else
01390       cfg->include_level--;
01391 
01392    return result;
01393 }
01394 
01395 struct ast_config *ast_config_load(const char *filename)
01396 {
01397    struct ast_config *cfg;
01398    struct ast_config *result;
01399 
01400    cfg = ast_config_new();
01401    if (!cfg)
01402       return NULL;
01403 
01404    result = ast_config_internal_load(filename, cfg, 0);
01405    if (!result)
01406       ast_config_destroy(cfg);
01407 
01408    return result;
01409 }
01410 
01411 struct ast_config *ast_config_load_with_comments(const char *filename)
01412 {
01413    struct ast_config *cfg;
01414    struct ast_config *result;
01415 
01416    cfg = ast_config_new();
01417    if (!cfg)
01418       return NULL;
01419 
01420    result = ast_config_internal_load(filename, cfg, 1);
01421    if (!result)
01422       ast_config_destroy(cfg);
01423 
01424    return result;
01425 }
01426 
01427 struct ast_variable *ast_load_realtime(const char *family, ...)
01428 {
01429    struct ast_config_engine *eng;
01430    char db[256]="";
01431    char table[256]="";
01432    struct ast_variable *res=NULL;
01433    va_list ap;
01434 
01435    va_start(ap, family);
01436    eng = find_engine(family, db, sizeof(db), table, sizeof(table));
01437    if (eng && eng->realtime_func) 
01438       res = eng->realtime_func(db, table, ap);
01439    va_end(ap);
01440 
01441    return res;
01442 }
01443 
01444 /*! \brief Check if realtime engine is configured for family */
01445 int ast_check_realtime(const char *family)
01446 {
01447    struct ast_config_engine *eng;
01448 
01449    eng = find_engine(family, NULL, 0, NULL, 0);
01450    if (eng)
01451       return 1;
01452    return 0;
01453 
01454 }
01455 
01456 /*! \brief Check if there's any realtime engines loaded */
01457 int ast_realtime_enabled(void)
01458 {
01459    return config_maps ? 1 : 0;
01460 }
01461 
01462 struct ast_config *ast_load_realtime_multientry(const char *family, ...)
01463 {
01464    struct ast_config_engine *eng;
01465    char db[256]="";
01466    char table[256]="";
01467    struct ast_config *res=NULL;
01468    va_list ap;
01469 
01470    va_start(ap, family);
01471    eng = find_engine(family, db, sizeof(db), table, sizeof(table));
01472    if (eng && eng->realtime_multi_func) 
01473       res = eng->realtime_multi_func(db, table, ap);
01474    va_end(ap);
01475 
01476    return res;
01477 }
01478 
01479 int ast_update_realtime(const char *family, const char *keyfield, const char *lookup, ...)
01480 {
01481    struct ast_config_engine *eng;
01482    int res = -1;
01483    char db[256]="";
01484    char table[256]="";
01485    va_list ap;
01486 
01487    va_start(ap, lookup);
01488    eng = find_engine(family, db, sizeof(db), table, sizeof(table));
01489    if (eng && eng->update_func) 
01490       res = eng->update_func(db, table, keyfield, lookup, ap);
01491    va_end(ap);
01492 
01493    return res;
01494 }
01495 
01496 static int config_command(int fd, int argc, char **argv) 
01497 {
01498    struct ast_config_engine *eng;
01499    struct ast_config_map *map;
01500    
01501    ast_mutex_lock(&config_lock);
01502 
01503    ast_cli(fd, "\n\n");
01504    for (eng = config_engine_list; eng; eng = eng->next) {
01505       ast_cli(fd, "\nConfig Engine: %s\n", eng->name);
01506       for (map = config_maps; map; map = map->next)
01507          if (!strcasecmp(map->driver, eng->name)) {
01508             ast_cli(fd, "===> %s (db=%s, table=%s)\n", map->name, map->database,
01509                map->table ? map->table : map->name);
01510          }
01511    }
01512    ast_cli(fd,"\n\n");
01513    
01514    ast_mutex_unlock(&config_lock);
01515 
01516    return 0;
01517 }
01518 
01519 static char show_config_help[] =
01520    "Usage: core show config mappings\n"
01521    "  Shows the filenames to config engines.\n";
01522 
01523 static struct ast_cli_entry cli_show_config_mappings_deprecated = {
01524    { "show", "config", "mappings", NULL },
01525    config_command, NULL,
01526    NULL };
01527 
01528 static struct ast_cli_entry cli_config[] = {
01529    { { "core", "show", "config", "mappings", NULL },
01530    config_command, "Display config mappings (file names to config engines)",
01531    show_config_help, NULL, &cli_show_config_mappings_deprecated },
01532 };
01533 
01534 int register_config_cli() 
01535 {
01536    ast_cli_register_multiple(cli_config, sizeof(cli_config) / sizeof(struct ast_cli_entry));
01537    return 0;
01538 }

Generated on Thu Oct 8 00:59:02 2009 for Asterisk - the Open Source PBX by  doxygen 1.4.7