2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 2008, Digium, Inc.
6 * Russell Bryant <russell@digium.com>
7 * Tilghman Lesher <func_config__200803@the-tilghman.com>
9 * See http://www.asterisk.org for more information about
10 * the Asterisk project. Please do not directly contact
11 * any of the maintainers of this project for assistance;
12 * the project provides a web site, mailing lists and IRC
13 * channels for your use.
15 * This program is free software, distributed under the terms of
16 * the GNU General Public License Version 2. See the LICENSE file
17 * at the top of the source tree.
22 * \brief A function to retrieve variables from an Asterisk configuration file
24 * \author Russell Bryant <russell@digium.com>
25 * \author Tilghman Lesher <func_config__200803@the-tilghman.com>
32 ASTERISK_FILE_VERSION(__FILE__
, "$Revision$")
34 #include "asterisk/module.h"
35 #include "asterisk/channel.h"
36 #include "asterisk/pbx.h"
37 #include "asterisk/app.h"
40 AST_RWLIST_ENTRY(config_item
) entry
;
41 struct ast_config
*cfg
;
45 static AST_RWLIST_HEAD_STATIC(configs
, config_item
);
47 static int config_function_read(struct ast_channel
*chan
, const char *cmd
, char *data
,
48 char *buf
, size_t len
)
50 struct ast_config
*cfg
;
51 struct ast_flags cfg_flags
= { CONFIG_FLAG_FILEUNCHANGED
};
54 struct config_item
*cur
;
55 AST_DECLARE_APP_ARGS(args
,
56 AST_APP_ARG(filename
);
57 AST_APP_ARG(category
);
58 AST_APP_ARG(variable
);
62 if (ast_strlen_zero(data
)) {
63 ast_log(LOG_ERROR
, "AST_CONFIG() requires an argument\n");
67 parse
= ast_strdupa(data
);
68 AST_STANDARD_APP_ARGS(args
, parse
);
70 if (ast_strlen_zero(args
.filename
)) {
71 ast_log(LOG_ERROR
, "AST_CONFIG() requires a filename\n");
75 if (ast_strlen_zero(args
.category
)) {
76 ast_log(LOG_ERROR
, "AST_CONFIG() requires a category\n");
80 if (ast_strlen_zero(args
.variable
)) {
81 ast_log(LOG_ERROR
, "AST_CONFIG() requires a variable\n");
85 if (!(cfg
= ast_config_load(args
.filename
, cfg_flags
))) {
89 if (cfg
== CONFIG_STATUS_FILEUNCHANGED
) {
90 /* Retrieve cfg from list */
91 AST_RWLIST_RDLOCK(&configs
);
92 AST_RWLIST_TRAVERSE(&configs
, cur
, entry
) {
93 if (!strcmp(cur
->filename
, args
.filename
)) {
99 /* At worst, we might leak an entry while upgrading locks */
100 AST_RWLIST_UNLOCK(&configs
);
101 AST_RWLIST_WRLOCK(&configs
);
102 if (!(cur
= ast_malloc(sizeof(*cur
) + strlen(args
.filename
) + 1))) {
103 AST_RWLIST_UNLOCK(&configs
);
107 strcpy(cur
->filename
, args
.filename
);
109 ast_clear_flag(&cfg_flags
, CONFIG_FLAG_FILEUNCHANGED
);
110 if (!(cfg
= ast_config_load(args
.filename
, cfg_flags
))) {
112 AST_RWLIST_UNLOCK(&configs
);
117 AST_RWLIST_INSERT_TAIL(&configs
, cur
, entry
);
122 /* Replace cfg in list */
123 AST_RWLIST_WRLOCK(&configs
);
124 AST_RWLIST_TRAVERSE(&configs
, cur
, entry
) {
125 if (!strcmp(cur
->filename
, args
.filename
)) {
131 if (!(cur
= ast_malloc(sizeof(*cur
) + strlen(args
.filename
) + 1))) {
132 AST_RWLIST_UNLOCK(&configs
);
136 strcpy(cur
->filename
, args
.filename
);
139 AST_RWLIST_INSERT_TAIL(&configs
, cur
, entry
);
141 ast_config_destroy(cur
->cfg
);
146 if (!(val
= ast_variable_retrieve(cfg
, args
.category
, args
.variable
))) {
147 ast_log(LOG_ERROR
, "'%s' not found in [%s] of '%s'\n", args
.variable
,
148 args
.category
, args
.filename
);
152 ast_copy_string(buf
, val
, len
);
154 /* Unlock down here, so there's no chance the struct goes away while we're using it. */
155 AST_RWLIST_UNLOCK(&configs
);
160 static struct ast_custom_function config_function
= {
161 .name
= "AST_CONFIG",
162 .syntax
= "AST_CONFIG(config_file,category,variable_name)",
163 .synopsis
= "Retrieve a variable from a configuration file",
165 " This function reads a variable from an Asterisk configuration file.\n"
167 .read
= config_function_read
,
170 static int unload_module(void)
172 struct config_item
*cur
;
173 int res
= ast_custom_function_unregister(&config_function
);
175 AST_RWLIST_WRLOCK(&configs
);
176 while ((cur
= AST_RWLIST_REMOVE_HEAD(&configs
, entry
))) {
177 ast_config_destroy(cur
->cfg
);
180 AST_RWLIST_UNLOCK(&configs
);
185 static int load_module(void)
187 return ast_custom_function_register(&config_function
);
190 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY
, "Asterisk configuration file variable access");