Main Page | Alphabetical List | Data Structures | File List | Data Fields | Globals

config.c File Reference

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <time.h>
#include <asterisk/config.h>
#include <asterisk/options.h>
#include <asterisk/logger.h>
#include "asterisk.h"
#include "astconf.h"

Go to the source code of this file.

Data Structures

struct  ast_category
struct  ast_config

Defines

#define MAX_INCLUDE_LEVEL   10

Functions

void ast_destroy (struct ast_config *ast)
 Removes a config.

int ast_true (char *s)
 Make sure something is true.

int ast_false (char *s)
 Make sure something is false.

ast_variableast_variable_browse (struct ast_config *config, char *category)
 Goes through variables.

char * ast_variable_retrieve (struct ast_config *config, char *category, char *value)
 Gets a variable.

int ast_category_exist (struct ast_config *config, char *category_name)
 Check for category duplicates.

int ast_save (char *configfile, struct ast_config *cfg, char *generator)
ast_configast_load (char *configfile)
 Load a config file.

char * ast_category_browse (struct ast_config *config, char *prev)
 Goes through categories.


Define Documentation

#define MAX_INCLUDE_LEVEL   10
 

Definition at line 26 of file config.c.


Function Documentation

char* ast_category_browse struct ast_config config,
char *  prev
 

Goes through categories.

Parameters:
config Which config file you wish to "browse"
prev A pointer to a previous category. This funtion is kind of non-intuitive in it's use. To begin, one passes NULL as the second arguement. It will return a pointer to the string of the first category in the file. From here on after, one must then pass the previous usage's return value as the second pointer, and it will return a pointer to the category name afterwards. Note: If you manually strcpy a string into a character array and pass it thinking it will return your category, it will not; the comparisons are not done doing strcmp, they are done by checking whether the value of the string POINTER is the same. Returns a category on success, or NULL on failure/no-more-categories

Definition at line 787 of file config.c.

References ast_category::name, ast_category::next, and ast_config::root.

00788 {  
00789    struct ast_category *cat;
00790    if (!prev) {
00791       if (config->root)
00792          return config->root->name;
00793       else
00794          return NULL;
00795    }
00796    cat = config->root;
00797    while(cat) {
00798       if (cat->name == prev) {
00799          if (cat->next)
00800             return cat->next->name;
00801          else
00802             return NULL;
00803       }
00804       cat = cat->next;
00805    }
00806    cat = config->root;
00807    while(cat) {
00808       if (!strcasecmp(cat->name, prev)) {
00809          if (cat->next)
00810             return cat->next->name;
00811          else
00812             return NULL;
00813       }
00814       cat = cat->next;
00815    }
00816    return NULL;
00817 }

int ast_category_exist struct ast_config config,
char *  category_name
 

Check for category duplicates.

Parameters:
config which config to use
category_name name of the category you're looking for This will search through the categories within a given config file and search for a match. The passed category_name can be a regular string (as opposed to a pointer of an existent string, lol) Browse config structure and check for category duplicity Return non-zero if found

Definition at line 411 of file config.c.

References ast_category::next, and ast_config::root.

00412 {
00413    struct ast_category *category = NULL;
00414 
00415    category = config->root;
00416 
00417    while(category) {
00418       if (!strcasecmp(category->name,category_name)) 
00419          return 1;
00420       category = category->next;
00421    } 
00422 
00423    return 0;
00424 }

void ast_destroy struct ast_config config  ) 
 

Removes a config.

Parameters:
config config data structure associated with the config. Free memory associated with a given config Returns nothing

Definition at line 81 of file config.c.

References free, ast_config::root, and ast_category::root.

Referenced by ast_enum_init(), ast_load_resource(), ast_rtp_reload(), init_manager(), and load_modules().

00082 {
00083    struct ast_category *cat, *catn;
00084    struct ast_variable *v, *vn;
00085 
00086    if (!ast)
00087       return;
00088 
00089    cat = ast->root;
00090    while(cat) {
00091       v = cat->root;
00092       while(v) {
00093          vn = v;
00094          free(v->name);
00095          free(v->value);
00096 #ifdef PRESERVE_COMMENTS
00097          free_comments(v->precomments);
00098          free_comments(v->sameline);
00099 #endif         
00100          v = v->next;
00101          free(vn);
00102       }
00103       catn = cat;
00104 #ifdef PRESERVE_COMMENTS
00105       free_comments(cat->precomments);
00106       free_comments(cat->sameline);
00107 #endif      
00108       cat = cat->next;
00109       free(catn);
00110    }
00111 #ifdef PRESERVE_COMMENTS
00112    free_comments(ast->trailingcomments);
00113 #endif   
00114    free(ast);
00115 }

int ast_false char *  val  ) 
 

Make sure something is false.

Determine falseness of a boolean value. This function checks to see whether a string passed to it is an indication of a negatirve value. It checks to see if the string is "no", "false", "n", "f", and "0". Returns 0 if the value of s is a NULL pointer, 0 on "truth", and -1 on falsehood.

Definition at line 131 of file config.c.

References s.

00132 {
00133    if (!s)
00134       return 0;
00135    /* Determine if this is a false value */
00136    if (!strcasecmp(s, "no") ||
00137        !strcasecmp(s, "false") ||
00138       !strcasecmp(s, "n") ||
00139       !strcasecmp(s, "f") ||
00140       !strcasecmp(s, "0"))
00141          return -1;
00142    return 0;
00143 }

struct ast_config* ast_load char *  configfile  ) 
 

Load a config file.

Parameters:
configfile path of file to open. If no preceding '/' character, path is considered relative to AST_CONFIG_DIR Create a config structure from a given configuration file. Returns NULL on error, or an ast_config data structure on success

Definition at line 773 of file config.c.

Referenced by ast_enum_init(), ast_load_resource(), ast_rtp_reload(), init_manager(), and load_modules().

00774 {
00775    struct ast_category *tmpc=NULL;
00776    struct ast_variable *last = NULL;
00777 #ifdef PRESERVE_COMMENTS
00778    struct ast_comment_struct acs = { NULL, NULL };
00779 #endif   
00780    return __ast_load(configfile, NULL, &tmpc, &last, 0 
00781 #ifdef PRESERVE_COMMENTS
00782    ,&acs
00783 #endif
00784    );
00785 }

int ast_save char *  configfile,
struct ast_config cfg,
char *  generator
 

Definition at line 626 of file config.c.

References AST_CONFIG_DIR, ast_verbose(), option_debug, option_verbose, ast_config::root, and VERBOSE_PREFIX_2.

00627 {
00628    FILE *f;
00629    char fn[256];
00630    char date[256];
00631    time_t t;
00632    struct ast_variable *var;
00633    struct ast_category *cat;
00634    int blanklines = 0;
00635    if (configfile[0] == '/') {
00636       strncpy(fn, configfile, sizeof(fn)-1);
00637    } else {
00638       snprintf(fn, sizeof(fn), "%s/%s", AST_CONFIG_DIR, configfile);
00639    }
00640    time(&t);
00641    strncpy(date, ctime(&t), sizeof(date));
00642    if ((f = fopen(fn, "w"))) {
00643       if ((option_verbose > 1) && !option_debug)
00644          ast_verbose(  VERBOSE_PREFIX_2 "Saving '%s': ", fn);
00645       fprintf(f, ";!\n");
00646       fprintf(f, ";! Automatically generated configuration file\n");
00647       fprintf(f, ";! Filename: %s (%s)\n", configfile, fn);
00648       fprintf(f, ";! Generator: %s\n", generator);
00649       fprintf(f, ";! Creation Date: %s", date);
00650       fprintf(f, ";!\n");
00651       cat = cfg->root;
00652       while(cat) {
00653 #ifdef PRESERVE_COMMENTS
00654          /* Dump any precomments */
00655          dump_comments(f, cat->precomments);
00656 #endif
00657          /* Dump section with any appropriate comment */
00658 #ifdef PRESERVE_COMMENTS
00659          if (cat->sameline) 
00660             fprintf(f, "[%s]  ; %s\n", cat->name, cat->sameline->cmt);
00661          else
00662 #endif
00663             fprintf(f, "[%s]\n", cat->name);
00664          var = cat->root;
00665          while(var) {
00666 #ifdef PRESERVE_COMMENTS
00667             dump_comments(f, var->precomments);
00668 #endif            
00669             if (var->sameline) 
00670                fprintf(f, "%s %s %s  ; %s\n", var->name, (var->object ? "=>" : "="), var->value, var->sameline->cmt);
00671             else  
00672                fprintf(f, "%s %s %s\n", var->name, (var->object ? "=>" : "="), var->value);
00673             if (var->blanklines) {
00674                blanklines = var->blanklines;
00675                while (blanklines) {
00676                   fprintf(f, "\n");
00677                   blanklines--;
00678                }
00679             }
00680                
00681             var = var->next;
00682          }
00683 #if 0
00684          /* Put an empty line */
00685          fprintf(f, "\n");
00686 #endif
00687          cat = cat->next;
00688       }
00689 #ifdef PRESERVE_COMMENTS
00690       dump_comments(f, cfg->trailingcomments);
00691 #endif      
00692    } else {
00693       if (option_debug)
00694          printf("Unable to open for writing: %s\n", fn);
00695       else if (option_verbose > 1)
00696          printf( "Unable to write (%s)", strerror(errno));
00697       return -1;
00698    }
00699    fclose(f);
00700    return 0;
00701 }

int ast_true char *  val  ) 
 

Make sure something is true.

Determine affermativeness of a boolean value. This function checks to see whether a string passed to it is an indication of an affirmitave value. It checks to see if the string is "yes", "true", "y", "t", and "1". Returns 0 if the value of s is a NULL pointer, 0 on "truth", and -1 on falsehood.

Definition at line 117 of file config.c.

References s.

Referenced by ast_load_resource(), init_manager(), and load_modules().

00118 {
00119    if (!s)
00120       return 0;
00121    /* Determine if this is a true value */
00122    if (!strcasecmp(s, "yes") ||
00123        !strcasecmp(s, "true") ||
00124       !strcasecmp(s, "y") ||
00125       !strcasecmp(s, "t") ||
00126       !strcasecmp(s, "1"))
00127          return -1;
00128    return 0;
00129 }

struct ast_variable* ast_variable_browse struct ast_config config,
char *  category
 

Goes through variables.

Somewhat similar in intent as the ast_category_browse. The category MUST be an actual pointer to an actual category (such as one obtained by using ast_category_browse()). List variables of config file Returns ast_variable list on success, or NULL on failure

Definition at line 145 of file config.c.

References ast_variable::next, ast_config::root, and ast_category::root.

Referenced by ast_enum_init(), ast_variable_retrieve(), and load_modules().

00146 {
00147    struct ast_category *cat;
00148    cat = config->root;
00149    while(cat) {
00150       if (cat->name == category)
00151          return cat->root;
00152       cat = cat->next;
00153    }
00154    cat = config->root;
00155    while(cat) {
00156       if (!strcasecmp(cat->name, category))
00157          return cat->root;
00158       cat = cat->next;
00159    }
00160    return NULL;
00161 }

char* ast_variable_retrieve struct ast_config config,
char *  category,
char *  value
 

Gets a variable.

Parameters:
config which (opened) config to use
category category under which the variable lies (must be a pointer to the category, such as one given by ast_category_browse)
value which variable you wish to get the data for Goes through a given config file in the given category and searches for the given variable Returns the variable value on success, or NULL if unable to find it. Retrieve a specific variable

Definition at line 163 of file config.c.

References ast_variable_browse(), ast_config::root, ast_category::root, and ast_variable::value.

Referenced by ast_load_resource(), ast_rtp_reload(), init_manager(), and load_modules().

00164 {
00165    struct ast_variable *v;
00166    if (category) {
00167       v = ast_variable_browse(config, category);
00168       while (v) {
00169          if (value == v->name)
00170             return v->value;
00171          v=v->next;
00172       }
00173       v = ast_variable_browse(config, category);
00174       while (v) {
00175          if (!strcasecmp(value, v->name))
00176             return v->value;
00177          v=v->next;
00178       }
00179    } else {
00180       struct ast_category *cat;
00181       cat = config->root;
00182       while(cat) {
00183          v = cat->root;
00184          while (v) {
00185             if (!strcasecmp(value, v->name))
00186                return v->value;
00187             v=v->next;
00188          }
00189          cat = cat->next;
00190       }
00191    }
00192    return NULL;
00193 }


Generated on Sun Apr 18 23:34:02 2004 for Asterisk by doxygen 1.3.6-20040222