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/config_pvt.h>
#include <asterisk/cli.h>
#include <asterisk/lock.h>
#include <asterisk/options.h>
#include <asterisk/logger.h>
#include <asterisk/utils.h>
#include "asterisk.h"
#include "astconf.h"

Go to the source code of this file.

Functions

 AST_MUTEX_DEFINE_STATIC (ast_cust_config_lock)
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_config_regget_ast_cust_config_keyword (char *name)
ast_config_regget_ast_cust_config (char *name)
void ast_config_destroy_all (void)
ast_config_regget_config_registrations (void)
int ast_config_register (struct ast_config_reg *new)
int ast_config_deregister (struct ast_config_reg *del)
int ast_cust_config_active (void)
ast_configast_load (char *configfile)
 Load a config file.
char * ast_category_browse (struct ast_config *config, char *prev)
 Goes through categories.
ast_configast_new_config (void)
ast_categoryast_new_category (char *name)
ast_variableast_new_variable (char *name, char *value)
int ast_cust_config_register (struct ast_config_reg *new)
int ast_cust_config_deregister (struct ast_config_reg *new)
int register_config_cli ()
int read_ast_cust_config (void)

Variables

ast_config *(* global_load_func )(char *, struct ast_config *, struct ast_category **, struct ast_variable **, int)


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

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

00929 { 00930 struct ast_category *cat; 00931 if (!prev) { 00932 if (config->root) 00933 return config->root->name; 00934 else 00935 return NULL; 00936 } 00937 cat = config->root; 00938 while(cat) { 00939 if (cat->name == prev) { 00940 if (cat->next) 00941 return cat->next->name; 00942 else 00943 return NULL; 00944 } 00945 cat = cat->next; 00946 } 00947 cat = config->root; 00948 while(cat) { 00949 if (!strcasecmp(cat->name, prev)) { 00950 if (cat->next) 00951 return cat->next->name; 00952 else 00953 return NULL; 00954 } 00955 cat = cat->next; 00956 } 00957 return NULL; 00958 }

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

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

00398 { 00399 struct ast_category *category = NULL; 00400 00401 category = config->root; 00402 00403 while(category) { 00404 if (!strcasecmp(category->name,category_name)) 00405 return 1; 00406 category = category->next; 00407 } 00408 00409 return 0; 00410 }

int ast_config_deregister struct ast_config_reg del  ) 
 

Definition at line 884 of file config.c.

References ast_mutex_lock, ast_mutex_unlock, and ast_config_reg::next.

Referenced by ast_config_destroy_all(), and ast_cust_config_deregister().

00885 { 00886 struct ast_config_reg *ptr=NULL,*last=NULL; 00887 ast_mutex_lock(&ast_cust_config_lock); 00888 for (ptr=ast_cust_config_list;ptr;ptr=ptr->next) { 00889 if (ptr == del) { 00890 if (last && ptr->next) { 00891 last->next = ptr->next; 00892 } else if (last && ! ptr->next) { 00893 last->next = NULL; 00894 } else if (!last && ptr->next) { 00895 ast_cust_config_list = ptr->next; 00896 } else if (!last && !ptr->next) { 00897 ast_cust_config_list = NULL; 00898 } 00899 } 00900 last = ptr; 00901 } 00902 ast_mutex_unlock(&ast_cust_config_lock); 00903 return 0; 00904 }

void ast_config_destroy_all void   ) 
 

Definition at line 852 of file config.c.

References ast_config_deregister(), ast_mutex_lock, ast_mutex_unlock, and key().

00853 { 00854 struct ast_config_reg *key; 00855 ast_mutex_lock(&ast_cust_config_lock); 00856 for (key=ast_cust_config_list;key;key=key->next) { 00857 ast_config_deregister(key); 00858 } 00859 ast_cust_config_list = NULL; 00860 ast_mutex_unlock(&ast_cust_config_lock); 00861 }

int ast_config_register struct ast_config_reg new  ) 
 

Definition at line 868 of file config.c.

References ast_log(), ast_mutex_lock, ast_mutex_unlock, LOG_NOTICE, ast_config_reg::name, and ast_config_reg::next.

Referenced by ast_cust_config_register().

00869 { 00870 struct ast_config_reg *ptr; 00871 ast_mutex_lock(&ast_cust_config_lock); 00872 new->keycount = 0; 00873 if (!ast_cust_config_list) { 00874 ast_cust_config_list = new; 00875 } else { 00876 for(ptr=ast_cust_config_list;ptr->next;ptr=ptr->next); 00877 ptr->next = new; 00878 } 00879 ast_mutex_unlock(&ast_cust_config_lock); 00880 ast_log(LOG_NOTICE,"Registered Config Engine %s\n",new->name); 00881 return 1; 00882 }

int ast_cust_config_active void   ) 
 

Definition at line 906 of file config.c.

00906 { 00907 return (ast_cust_config >0) ? 1 : 0; 00908 }

int ast_cust_config_deregister struct ast_config_reg new  ) 
 

Definition at line 1016 of file config.c.

References ast_config_deregister(), and read_ast_cust_config().

01017 { 01018 ast_config_deregister(new); 01019 read_ast_cust_config(); 01020 return 1; 01021 }

int ast_cust_config_register struct ast_config_reg new  ) 
 

Definition at line 1010 of file config.c.

References ast_config_register(), and read_ast_cust_config().

01011 { 01012 ast_config_register(new); 01013 read_ast_cust_config(); 01014 return 1; 01015 }

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 67 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(), load_modules(), and read_ast_cust_config().

00068 { 00069 struct ast_category *cat, *catn; 00070 struct ast_variable *v, *vn; 00071 00072 if (!ast) 00073 return; 00074 00075 cat = ast->root; 00076 while(cat) { 00077 v = cat->root; 00078 while(v) { 00079 vn = v; 00080 free(v->name); 00081 free(v->value); 00082 #ifdef PRESERVE_COMMENTS 00083 free_comments(v->precomments); 00084 free_comments(v->sameline); 00085 #endif 00086 v = v->next; 00087 free(vn); 00088 } 00089 catn = cat; 00090 #ifdef PRESERVE_COMMENTS 00091 free_comments(cat->precomments); 00092 free_comments(cat->sameline); 00093 #endif 00094 cat = cat->next; 00095 free(catn); 00096 } 00097 #ifdef PRESERVE_COMMENTS 00098 free_comments(ast->trailingcomments); 00099 #endif 00100 free(ast); 00101 }

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

References s.

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

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

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

00911 { 00912 struct ast_category *tmpc=NULL; 00913 struct ast_variable *last = NULL; 00914 00915 00916 #ifdef PRESERVE_COMMENTS 00917 struct ast_comment_struct acs = { NULL, NULL }; 00918 #endif 00919 00920 00921 return __ast_load(configfile, NULL, &tmpc, &last, 0 00922 #ifdef PRESERVE_COMMENTS 00923 ,&acs 00924 #endif 00925 ); 00926 }

AST_MUTEX_DEFINE_STATIC ast_cust_config_lock   ) 
 

struct ast_category* ast_new_category char *  name  ) 
 

Definition at line 971 of file config.c.

References malloc.

00972 { 00973 struct ast_category *category; 00974 category = malloc(sizeof(struct ast_category)); 00975 if (category) { 00976 memset(category,0,sizeof(struct ast_category)); 00977 strncpy(category->name,name,sizeof(category->name) - 1); 00978 } 00979 return category; 00980 }

struct ast_config* ast_new_config void   ) 
 

Definition at line 961 of file config.c.

References malloc.

00962 { 00963 struct ast_config *config; 00964 config = malloc(sizeof(struct ast_config)); 00965 memset(config,0,sizeof(struct ast_config)); 00966 return config; 00967 }

struct ast_variable* ast_new_variable char *  name,
char *  value
 

Definition at line 983 of file config.c.

References free, and malloc.

00984 { 00985 struct ast_variable *variable; 00986 variable = malloc(sizeof(struct ast_variable)); 00987 if (variable) { 00988 memset(variable,0,sizeof(struct ast_variable)); 00989 variable->object=0; 00990 variable->name = malloc(strlen(name)+1); 00991 if (variable->name) { 00992 strcpy(variable->name,name); 00993 variable->value = malloc(strlen(value)+1); 00994 if (variable->value) { 00995 strcpy(variable->value,value); 00996 } else { 00997 free(variable->name); 00998 variable->name = NULL; 00999 } 01000 } 01001 } 01002 if (!variable->value) { 01003 free(variable); 01004 variable = NULL; 01005 } 01006 01007 return variable; 01008 }

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

Definition at line 645 of file config.c.

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

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

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

References s.

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

00104 { 00105 if (!s) 00106 return 0; 00107 /* Determine if this is a true value */ 00108 if (!strcasecmp(s, "yes") || 00109 !strcasecmp(s, "true") || 00110 !strcasecmp(s, "y") || 00111 !strcasecmp(s, "t") || 00112 !strcasecmp(s, "1")) 00113 return -1; 00114 return 0; 00115 }

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

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

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

00132 { 00133 struct ast_category *cat; 00134 cat = config->root; 00135 while(cat) { 00136 if (cat->name == category) 00137 return cat->root; 00138 cat = cat->next; 00139 } 00140 cat = config->root; 00141 while(cat) { 00142 if (!strcasecmp(cat->name, category)) 00143 return cat->root; 00144 cat = cat->next; 00145 } 00146 return NULL; 00147 }

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

00150 { 00151 struct ast_variable *v; 00152 if (category) { 00153 v = ast_variable_browse(config, category); 00154 while (v) { 00155 if (value == v->name) 00156 return v->value; 00157 v=v->next; 00158 } 00159 v = ast_variable_browse(config, category); 00160 while (v) { 00161 if (!strcasecmp(value, v->name)) 00162 return v->value; 00163 v=v->next; 00164 } 00165 } else { 00166 struct ast_category *cat; 00167 cat = config->root; 00168 while(cat) { 00169 v = cat->root; 00170 while (v) { 00171 if (!strcasecmp(value, v->name)) 00172 return v->value; 00173 v=v->next; 00174 } 00175 cat = cat->next; 00176 } 00177 } 00178 return NULL; 00179 }

struct ast_config_reg* get_ast_cust_config char *  name  ) 
 

Definition at line 840 of file config.c.

References ast_mutex_lock, ast_mutex_unlock, and ast_config_reg::next.

Referenced by read_ast_cust_config().

00841 { 00842 struct ast_config_reg *ptr=NULL; 00843 ast_mutex_lock(&ast_cust_config_lock); 00844 for (ptr=ast_cust_config_list;ptr;ptr=ptr->next) { 00845 if (!strcmp(name,ptr->name)) 00846 break; 00847 } 00848 ast_mutex_unlock(&ast_cust_config_lock); 00849 return ptr; 00850 }

struct ast_config_reg* get_ast_cust_config_keyword char *  name  ) 
 

Definition at line 825 of file config.c.

References ast_mutex_lock, ast_mutex_unlock, and ast_config_reg::next.

Referenced by read_ast_cust_config().

00826 { 00827 struct ast_config_reg *reg,*ret=NULL; 00828 int x=0; 00829 ast_mutex_lock(&ast_cust_config_lock); 00830 for (reg=ast_cust_config_list;reg && !ret;reg=reg->next) { 00831 for (x=0;x<reg->keycount && !ret ;x++) { 00832 if (!strcmp(reg->keywords[x],name)) 00833 ret=reg; 00834 } 00835 } 00836 ast_mutex_unlock(&ast_cust_config_lock); 00837 return ret; 00838 }

struct ast_config_reg* get_config_registrations void   ) 
 

Definition at line 863 of file config.c.

00864 { 00865 return ast_cust_config_list; 00866 }

int read_ast_cust_config void   ) 
 

Definition at line 1064 of file config.c.

References ast_destroy(), ast_load(), ast_log(), ast_variable_browse(), CONFIG_KEYWORD_ARRAYLEN, get_ast_cust_config(), get_ast_cust_config_keyword(), LOG_NOTICE, and LOG_WARNING.

Referenced by ast_cust_config_deregister(), ast_cust_config_register(), ast_module_reload(), and main().

01065 { 01066 char *cfg = config_conf_file; 01067 struct ast_config *config; 01068 struct ast_variable *v; 01069 struct ast_config_reg *ptr; 01070 struct ast_config_reg *test = NULL; 01071 01072 clear_cust_keywords(); 01073 config = ast_load(cfg); 01074 if (config) { 01075 for (v = ast_variable_browse(config,"settings");v;v=v->next) { 01076 01077 ptr = get_ast_cust_config(v->value); 01078 if (ptr) { 01079 if (ptr->keycount >= CONFIG_KEYWORD_ARRAYLEN) { 01080 ast_log(LOG_WARNING,"Max Number of Bindings exceeded for %s->%s %d/%d\n",v->name,v->value,ptr->keycount,CONFIG_KEYWORD_ARRAYLEN); 01081 } else { 01082 if (strcmp(v->name,config_conf_file) && strcmp(v->name,"asterisk.conf")) { 01083 if (!(test = get_ast_cust_config_keyword(v->name))) { 01084 ast_log(LOG_NOTICE,"Binding: %s to %s\n",v->name,v->value); 01085 strncpy(ptr->keywords[ptr->keycount],v->name,sizeof(ptr->keywords[ptr->keycount]) - 1); 01086 ptr->keywords[ptr->keycount][sizeof(ptr->keywords[ptr->keycount])-1] = '\0'; 01087 ptr->keycount++; 01088 } 01089 } else { 01090 ast_log(LOG_WARNING,"Cannot bind %s, Permission Denied\n",v->name); 01091 } 01092 } 01093 } 01094 } 01095 01096 ast_destroy(config); 01097 } 01098 01099 return 0; 01100 }

int register_config_cli void   ) 
 

Definition at line 1059 of file config.c.

References ast_cli_register().

Referenced by main().

01060 { 01061 return ast_cli_register(&config_command_struct); 01062 }


Variable Documentation

struct ast_config*(* global_load_func)(char *, struct ast_config *, struct ast_category **, struct ast_variable **, int)
 

Definition at line 32 of file config.c.


Generated on Tue Aug 17 16:13:55 2004 for Asterisk by doxygen 1.3.8