Asterisk - The Open Source Telephony Project GIT-master-8924258
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Macros Modules Pages
func_config.c
Go to the documentation of this file.
1/*
2 * Asterisk -- An open source telephony toolkit.
3 *
4 * Copyright (C) 2008, Digium, Inc.
5 *
6 * Russell Bryant <russell@digium.com>
7 * Tilghman Lesher <func_config__200803@the-tilghman.com>
8 *
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.
14 *
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.
18 */
19
20/*! \file
21 *
22 * \brief A function to retrieve variables from an Asterisk configuration file
23 *
24 * \author Russell Bryant <russell@digium.com>
25 * \author Tilghman Lesher <func_config__200803@the-tilghman.com>
26 *
27 * \ingroup functions
28 */
29
30/*** MODULEINFO
31 <support_level>core</support_level>
32 ***/
33
34#include "asterisk.h"
35
36#include "asterisk/module.h"
37#include "asterisk/channel.h"
38#include "asterisk/pbx.h"
39#include "asterisk/app.h"
40
41/*** DOCUMENTATION
42 <function name="AST_CONFIG" language="en_US">
43 <since>
44 <version>1.6.1.0</version>
45 </since>
46 <synopsis>
47 Retrieve a variable from a configuration file.
48 </synopsis>
49 <syntax>
50 <parameter name="config_file" required="true" />
51 <parameter name="category" required="true" />
52 <parameter name="variable_name" required="true" />
53 <parameter name="index" required="false">
54 <para>If there are multiple variables with the same name, you can specify
55 <literal>0</literal> for the first item (default), <literal>-1</literal> for the last
56 item, or any other number for that specific item. <literal>-1</literal> is useful
57 when the variable is derived from a template and you want the effective value (the last
58 occurrence), not the value from the template (the first occurrence).</para>
59 </parameter>
60 </syntax>
61 <description>
62 <para>This function reads a variable from an Asterisk configuration file.</para>
63 </description>
64 </function>
65
66***/
67
70 struct ast_config *cfg;
71 char filename[0];
72};
73
75
76static int config_function_read(struct ast_channel *chan, const char *cmd, char *data,
77 char *buf, size_t len)
78{
79 struct ast_config *cfg;
80 struct ast_flags cfg_flags = { CONFIG_FLAG_FILEUNCHANGED };
81 char *parse;
82 struct config_item *cur;
83 int index = 0;
84 struct ast_variable *var;
85 struct ast_variable *found = NULL;
86 int ix = 0;
88 AST_APP_ARG(filename);
89 AST_APP_ARG(category);
90 AST_APP_ARG(variable);
91 AST_APP_ARG(index);
92 );
93
94 if (ast_strlen_zero(data)) {
95 ast_log(LOG_ERROR, "AST_CONFIG() requires an argument\n");
96 return -1;
97 }
98
99 parse = ast_strdupa(data);
101
102 if (ast_strlen_zero(args.filename)) {
103 ast_log(LOG_ERROR, "AST_CONFIG() requires a filename\n");
104 return -1;
105 }
106
107 if (ast_strlen_zero(args.category)) {
108 ast_log(LOG_ERROR, "AST_CONFIG() requires a category\n");
109 return -1;
110 }
111
112 if (ast_strlen_zero(args.variable)) {
113 ast_log(LOG_ERROR, "AST_CONFIG() requires a variable\n");
114 return -1;
115 }
116
117 if (!ast_strlen_zero(args.index)) {
118 if (!sscanf(args.index, "%d", &index)) {
119 ast_log(LOG_ERROR, "AST_CONFIG() index must be an integer\n");
120 return -1;
121 }
122 }
123
124 if (!(cfg = ast_config_load(args.filename, cfg_flags)) || cfg == CONFIG_STATUS_FILEINVALID) {
125 return -1;
126 }
127
128 if (cfg == CONFIG_STATUS_FILEUNCHANGED) {
129 /* Retrieve cfg from list */
131 AST_RWLIST_TRAVERSE(&configs, cur, entry) {
132 if (!strcmp(cur->filename, args.filename)) {
133 break;
134 }
135 }
136
137 if (!cur) {
138 /* At worst, we might leak an entry while upgrading locks */
141 if (!(cur = ast_calloc(1, sizeof(*cur) + strlen(args.filename) + 1))) {
143 return -1;
144 }
145
146 strcpy(cur->filename, args.filename);
147
149 if (!(cfg = ast_config_load(args.filename, cfg_flags)) || cfg == CONFIG_STATUS_FILEINVALID) {
150 ast_free(cur);
152 return -1;
153 }
154
155 cur->cfg = cfg;
156 AST_RWLIST_INSERT_TAIL(&configs, cur, entry);
157 }
158
159 cfg = cur->cfg;
160 } else {
161 /* Replace cfg in list */
163 AST_RWLIST_TRAVERSE(&configs, cur, entry) {
164 if (!strcmp(cur->filename, args.filename)) {
165 break;
166 }
167 }
168
169 if (!cur) {
170 if (!(cur = ast_calloc(1, sizeof(*cur) + strlen(args.filename) + 1))) {
172 return -1;
173 }
174
175 strcpy(cur->filename, args.filename);
176 cur->cfg = cfg;
177
178 AST_RWLIST_INSERT_TAIL(&configs, cur, entry);
179 } else {
180 ast_config_destroy(cur->cfg);
181 cur->cfg = cfg;
182 }
183 }
184
185 for (var = ast_category_root(cfg, args.category); var; var = var->next) {
186 if (strcasecmp(args.variable, var->name)) {
187 continue;
188 }
189 found = var;
190 if (index == -1) {
191 continue;
192 }
193 if (ix == index) {
194 break;
195 }
196 found = NULL;
197 ix++;
198 }
199
200 if (!found) {
201 ast_debug(1, "'%s' not found at index %d in [%s] of '%s'. Maximum index found: %d\n",
202 args.variable, index, args.category, args.filename, ix);
204 return -1;
205 }
206
207 ast_copy_string(buf, found->value, len);
208
209 /* Unlock down here, so there's no chance the struct goes away while we're using it. */
211
212 return 0;
213}
214
216 .name = "AST_CONFIG",
217 .read = config_function_read,
218};
219
220static int unload_module(void)
221{
222 struct config_item *current;
224
226 while ((current = AST_RWLIST_REMOVE_HEAD(&configs, entry))) {
229 }
231
232 return res;
233}
234
235static int load_module(void)
236{
238}
239
240AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Asterisk configuration file variable access");
#define var
Definition: ast_expr2f.c:605
Asterisk main include file. File version handling, generic pbx functions.
#define ast_free(a)
Definition: astmm.h:180
#define ast_strdupa(s)
duplicate a string in memory from the stack
Definition: astmm.h:298
#define ast_calloc(num, len)
A wrapper for calloc()
Definition: astmm.h:202
#define ast_log
Definition: astobj2.c:42
General Asterisk PBX channel definitions.
char buf[BUFSIZE]
Definition: eagi_proxy.c:66
static int config_function_read(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
Definition: func_config.c:76
static struct ast_custom_function config_function
Definition: func_config.c:215
static int load_module(void)
Definition: func_config.c:235
static int unload_module(void)
Definition: func_config.c:220
static int len(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t buflen)
Application convenience functions, designed to give consistent look and feel to Asterisk apps.
#define AST_APP_ARG(name)
Define an application argument.
#define AST_DECLARE_APP_ARGS(name, arglist)
Declare a structure to hold an application's arguments.
#define AST_STANDARD_APP_ARGS(args, parse)
Performs the 'standard' argument separation process for an application.
#define ast_config_load(filename, flags)
Load a config file.
@ CONFIG_FLAG_FILEUNCHANGED
#define CONFIG_STATUS_FILEUNCHANGED
#define CONFIG_STATUS_FILEINVALID
struct ast_variable * ast_category_root(struct ast_config *config, char *cat)
returns the root ast_variable of a config
Definition: main/config.c:1363
void ast_config_destroy(struct ast_config *cfg)
Destroys a config.
Definition: extconf.c:1289
#define ast_debug(level,...)
Log a DEBUG message.
#define LOG_ERROR
#define AST_RWLIST_RDLOCK(head)
Read locks a list.
Definition: linkedlists.h:78
#define AST_RWLIST_WRLOCK(head)
Write locks a list.
Definition: linkedlists.h:52
#define AST_RWLIST_UNLOCK(head)
Attempts to unlock a read/write based list.
Definition: linkedlists.h:151
#define AST_RWLIST_HEAD_STATIC(name, type)
Defines a structure to be used to hold a read/write list of specified type, statically initialized.
Definition: linkedlists.h:333
#define AST_RWLIST_REMOVE_HEAD
Definition: linkedlists.h:844
#define AST_RWLIST_TRAVERSE
Definition: linkedlists.h:494
#define AST_RWLIST_INSERT_TAIL
Definition: linkedlists.h:741
#define AST_RWLIST_ENTRY
Definition: linkedlists.h:415
size_t current
Definition: main/cli.c:113
Asterisk module definitions.
#define AST_MODULE_INFO_STANDARD(keystr, desc)
Definition: module.h:581
#define ASTERISK_GPL_KEY
The text the key() function should return.
Definition: module.h:46
Core PBX routines and definitions.
#define ast_custom_function_register(acf)
Register a custom function.
Definition: pbx.h:1559
int ast_custom_function_unregister(struct ast_custom_function *acf)
Unregister a custom function.
#define NULL
Definition: resample.c:96
static force_inline int attribute_pure ast_strlen_zero(const char *s)
Definition: strings.h:65
void ast_copy_string(char *dst, const char *src, size_t size)
Size-limited null-terminating string copy.
Definition: strings.h:425
Main Channel structure associated with a channel.
Data structure associated with a custom dialplan function.
Definition: pbx.h:118
const char * name
Definition: pbx.h:119
Structure used to handle boolean flags.
Definition: utils.h:199
Structure for variables, used for configurations and for channel variables.
const char * args
#define ast_clear_flag(p, flag)
Definition: utils.h:77