Mon May 14 04:42:55 2007

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

Generated on Mon May 14 04:42:55 2007 for Asterisk - the Open Source PBX by  doxygen 1.5.1