Sat Aug 6 00:39:28 2011

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

Generated on Sat Aug 6 00:39:28 2011 for Asterisk - the Open Source PBX by  doxygen 1.4.7