Thu Apr 28 2011 17:13:55

Asterisk developer's documentation


func_config.c File Reference

A function to retrieve variables from an Asterisk configuration file. More...

#include "asterisk.h"
#include "asterisk/module.h"
#include "asterisk/channel.h"
#include "asterisk/pbx.h"
#include "asterisk/app.h"
Include dependency graph for func_config.c:

Go to the source code of this file.

Data Structures

struct  config_item
struct  configs

Functions

static void __fini_configs (void)
static void __init_configs (void)
static void __reg_module (void)
static void __unreg_module (void)
static int config_function_read (struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
static int load_module (void)
static int unload_module (void)

Variables

static struct ast_module_info
__MODULE_INFO_SECTION 
__mod_info = { __MODULE_INFO_GLOBALS .name = AST_MODULE, .flags = AST_MODFLAG_DEFAULT , .description = "Asterisk configuration file variable access" , .key = ASTERISK_GPL_KEY , .buildopt_sum = AST_BUILDOPT_SUM, .load = load_module, .unload = unload_module, }
static struct ast_module_infoast_module_info = &__mod_info
static struct ast_custom_function config_function
static struct configs configs

Detailed Description

A function to retrieve variables from an Asterisk configuration file.

Author:
Russell Bryant <russell@digium.com>
Tilghman Lesher <func_config__200803@the-tilghman.com>

Definition in file func_config.c.


Function Documentation

static void __fini_configs ( void  ) [static]

Definition at line 62 of file func_config.c.

{
static void __init_configs ( void  ) [static]

Definition at line 62 of file func_config.c.

{
static void __reg_module ( void  ) [static]

Definition at line 203 of file func_config.c.

static void __unreg_module ( void  ) [static]

Definition at line 203 of file func_config.c.

static int config_function_read ( struct ast_channel chan,
const char *  cmd,
char *  data,
char *  buf,
size_t  len 
) [static]

Definition at line 64 of file func_config.c.

References AST_APP_ARG, ast_clear_flag, ast_config_destroy(), ast_config_load, ast_copy_string(), AST_DECLARE_APP_ARGS, ast_free, ast_log(), ast_malloc, AST_RWLIST_INSERT_TAIL, AST_RWLIST_RDLOCK, AST_RWLIST_TRAVERSE, AST_RWLIST_UNLOCK, AST_RWLIST_WRLOCK, AST_STANDARD_APP_ARGS, ast_strdupa, ast_strlen_zero(), ast_variable_retrieve(), CONFIG_FLAG_FILEUNCHANGED, CONFIG_STATUS_FILEINVALID, CONFIG_STATUS_FILEUNCHANGED, LOG_ERROR, parse(), and val.

{
   struct ast_config *cfg;
   struct ast_flags cfg_flags = { CONFIG_FLAG_FILEUNCHANGED };
   const char *val;
   char *parse;
   struct config_item *cur;
   AST_DECLARE_APP_ARGS(args,
      AST_APP_ARG(filename);
      AST_APP_ARG(category);
      AST_APP_ARG(variable);
      AST_APP_ARG(index);
   );

   if (ast_strlen_zero(data)) {
      ast_log(LOG_ERROR, "AST_CONFIG() requires an argument\n");
      return -1;
   }

   parse = ast_strdupa(data);
   AST_STANDARD_APP_ARGS(args, parse);

   if (ast_strlen_zero(args.filename)) {
      ast_log(LOG_ERROR, "AST_CONFIG() requires a filename\n");
      return -1;
   }

   if (ast_strlen_zero(args.category)) {
      ast_log(LOG_ERROR, "AST_CONFIG() requires a category\n");
      return -1;
   }
   
   if (ast_strlen_zero(args.variable)) {
      ast_log(LOG_ERROR, "AST_CONFIG() requires a variable\n");
      return -1;
   }

   if (!(cfg = ast_config_load(args.filename, cfg_flags)) || cfg == CONFIG_STATUS_FILEINVALID) {
      return -1;
   }

   if (cfg == CONFIG_STATUS_FILEUNCHANGED) {
      /* Retrieve cfg from list */
      AST_RWLIST_RDLOCK(&configs);
      AST_RWLIST_TRAVERSE(&configs, cur, entry) {
         if (!strcmp(cur->filename, args.filename)) {
            break;
         }
      }

      if (!cur) {
         /* At worst, we might leak an entry while upgrading locks */
         AST_RWLIST_UNLOCK(&configs);
         AST_RWLIST_WRLOCK(&configs);
         if (!(cur = ast_malloc(sizeof(*cur) + strlen(args.filename) + 1))) {
            AST_RWLIST_UNLOCK(&configs);
            return -1;
         }

         strcpy(cur->filename, args.filename);

         ast_clear_flag(&cfg_flags, CONFIG_FLAG_FILEUNCHANGED);
         if (!(cfg = ast_config_load(args.filename, cfg_flags)) || cfg == CONFIG_STATUS_FILEINVALID) {
            ast_free(cur);
            AST_RWLIST_UNLOCK(&configs);
            return -1;
         }

         cur->cfg = cfg;
         AST_RWLIST_INSERT_TAIL(&configs, cur, entry);
      }

      cfg = cur->cfg;
   } else {
      /* Replace cfg in list */
      AST_RWLIST_WRLOCK(&configs);
      AST_RWLIST_TRAVERSE(&configs, cur, entry) {
         if (!strcmp(cur->filename, args.filename)) {
            break;
         }
      }

      if (!cur) {
         if (!(cur = ast_malloc(sizeof(*cur) + strlen(args.filename) + 1))) {
            AST_RWLIST_UNLOCK(&configs);
            return -1;
         }

         strcpy(cur->filename, args.filename);
         cur->cfg = cfg;

         AST_RWLIST_INSERT_TAIL(&configs, cur, entry);
      } else {
         ast_config_destroy(cur->cfg);
         cur->cfg = cfg;
      }
   }

   if (!(val = ast_variable_retrieve(cfg, args.category, args.variable))) {
      ast_log(LOG_ERROR, "'%s' not found in [%s] of '%s'\n", args.variable, 
         args.category, args.filename);
      AST_RWLIST_UNLOCK(&configs);
      return -1;
   }

   ast_copy_string(buf, val, len);

   /* Unlock down here, so there's no chance the struct goes away while we're using it. */
   AST_RWLIST_UNLOCK(&configs);

   return 0;
}
static int load_module ( void  ) [static]

Definition at line 198 of file func_config.c.

References ast_custom_function_register.

static int unload_module ( void  ) [static]

Variable Documentation

struct ast_module_info __MODULE_INFO_SECTION __mod_info = { __MODULE_INFO_GLOBALS .name = AST_MODULE, .flags = AST_MODFLAG_DEFAULT , .description = "Asterisk configuration file variable access" , .key = ASTERISK_GPL_KEY , .buildopt_sum = AST_BUILDOPT_SUM, .load = load_module, .unload = unload_module, } [static]

Definition at line 203 of file func_config.c.

Definition at line 203 of file func_config.c.

Initial value:
 {
   .name = "AST_CONFIG",
   .read = config_function_read,
}

Definition at line 178 of file func_config.c.

struct configs configs [static]