2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 2008, Digium, Inc.
6 * Russell Bryant <russell@digium.com>
8 * See http://www.asterisk.org for more information about
9 * the Asterisk project. Please do not directly contact
10 * any of the maintainers of this project for assistance;
11 * the project provides a web site, mailing lists and IRC
12 * channels for your use.
14 * This program is free software, distributed under the terms of
15 * the GNU General Public License Version 2. See the LICENSE file
16 * at the top of the source tree.
21 * \brief A function to retrieve variables from an Asterisk configuration file
23 * \author Russell Bryant <russell@digium.com>
30 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
32 #include "asterisk/module.h"
33 #include "asterisk/channel.h"
34 #include "asterisk/pbx.h"
35 #include "asterisk/app.h"
37 static int config_function_read(struct ast_channel *chan, const char *cmd, char *data,
38 char *buf, size_t len)
40 struct ast_config *cfg;
41 struct ast_flags cfg_flags = { 0 };
44 AST_DECLARE_APP_ARGS(args,
45 AST_APP_ARG(filename);
46 AST_APP_ARG(category);
47 AST_APP_ARG(variable);
51 if (ast_strlen_zero(data)) {
52 ast_log(LOG_ERROR, "AST_CONFIG() requires an argument\n");
56 parse = ast_strdupa(data);
57 AST_STANDARD_APP_ARGS(args, parse);
59 if (ast_strlen_zero(args.filename)) {
60 ast_log(LOG_ERROR, "AST_CONFIG() requires a filename\n");
64 if (ast_strlen_zero(args.category)) {
65 ast_log(LOG_ERROR, "AST_CONFIG() requires a category\n");
69 if (ast_strlen_zero(args.variable)) {
70 ast_log(LOG_ERROR, "AST_CONFIG() requires a variable\n");
74 if (!(cfg = ast_config_load(args.filename, cfg_flags))) {
78 if (!(val = ast_variable_retrieve(cfg, args.category, args.variable))) {
79 ast_log(LOG_ERROR, "'%s' not found in [%s] of '%s'\n", args.variable,
80 args.category, args.filename);
84 ast_copy_string(buf, val, len);
86 ast_config_destroy(cfg);
91 static struct ast_custom_function config_function = {
93 .syntax = "AST_CONFIG(config_file,category,variable_name[,index])",
94 .synopsis = "Retrieve a variable from a configuration file",
96 " This function reads a variable from an Asterisk configuration file.\n"
97 "The optional index parameter would be used in the case that a variable\n"
98 "exists more than once in a category. The index is zero-based, so an\n"
99 "index of 0 returns the first instance of the variable. Also, if the\n"
100 "word \"count\" in the index field, the number of instances of that\n"
101 "variable will be returned.\n"
103 .read = config_function_read,
106 static int unload_module(void)
108 return ast_custom_function_unregister(&config_function);
111 static int load_module(void)
113 return ast_custom_function_register(&config_function);
116 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Asterisk configuration file variable access");