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/utils.h>
#include "asterisk.h"
#include "astconf.h"

Include dependency graph for config.c:

Include dependency graph

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 27 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 412 of file config.c.

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

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

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 82 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().

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

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 132 of file config.c.

References s.

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

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 627 of file config.c.

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

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

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 118 of file config.c.

References s.

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

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

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 146 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().

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

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 164 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().

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


Generated on Sat Jun 12 16:41:11 2004 for Asterisk by doxygen 1.3.7