Merge "stasis: Allow filtering by formatter"
[asterisk/asterisk.git] / funcs / func_config.c
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                 <synopsis>
44                         Retrieve a variable from a configuration file.
45                 </synopsis>
46                 <syntax>
47                         <parameter name="config_file" required="true" />
48                         <parameter name="category" required="true" />
49                         <parameter name="variable_name" required="true" />
50                         <parameter name="index" required="false">
51                                 <para>If there are multiple variables with the same name, you can specify
52                                 <literal>0</literal> for the first item (default), <literal>-1</literal> for the last
53                                 item, or any other number for that specific item.  <literal>-1</literal> is useful
54                                 when the variable is derived from a template and you want the effective value (the last
55                                 occurrence), not the value from the template (the first occurrence).</para>
56                         </parameter>
57                 </syntax>
58                 <description>
59                         <para>This function reads a variable from an Asterisk configuration file.</para>
60                 </description>
61         </function>
62
63 ***/
64
65 struct config_item {
66         AST_RWLIST_ENTRY(config_item) entry;
67         struct ast_config *cfg;
68         char filename[0];
69 };
70
71 static AST_RWLIST_HEAD_STATIC(configs, config_item);
72
73 static int config_function_read(struct ast_channel *chan, const char *cmd, char *data,
74         char *buf, size_t len)
75 {
76         struct ast_config *cfg;
77         struct ast_flags cfg_flags = { CONFIG_FLAG_FILEUNCHANGED };
78         char *parse;
79         struct config_item *cur;
80         int index = 0;
81         struct ast_variable *var;
82         struct ast_variable *found = NULL;
83         int ix = 0;
84         AST_DECLARE_APP_ARGS(args,
85                 AST_APP_ARG(filename);
86                 AST_APP_ARG(category);
87                 AST_APP_ARG(variable);
88                 AST_APP_ARG(index);
89         );
90
91         if (ast_strlen_zero(data)) {
92                 ast_log(LOG_ERROR, "AST_CONFIG() requires an argument\n");
93                 return -1;
94         }
95
96         parse = ast_strdupa(data);
97         AST_STANDARD_APP_ARGS(args, parse);
98
99         if (ast_strlen_zero(args.filename)) {
100                 ast_log(LOG_ERROR, "AST_CONFIG() requires a filename\n");
101                 return -1;
102         }
103
104         if (ast_strlen_zero(args.category)) {
105                 ast_log(LOG_ERROR, "AST_CONFIG() requires a category\n");
106                 return -1;
107         }
108
109         if (ast_strlen_zero(args.variable)) {
110                 ast_log(LOG_ERROR, "AST_CONFIG() requires a variable\n");
111                 return -1;
112         }
113
114         if (!ast_strlen_zero(args.index)) {
115                 if (!sscanf(args.index, "%d", &index)) {
116                         ast_log(LOG_ERROR, "AST_CONFIG() index must be an integer\n");
117                         return -1;
118                 }
119         }
120
121         if (!(cfg = ast_config_load(args.filename, cfg_flags)) || cfg == CONFIG_STATUS_FILEINVALID) {
122                 return -1;
123         }
124
125         if (cfg == CONFIG_STATUS_FILEUNCHANGED) {
126                 /* Retrieve cfg from list */
127                 AST_RWLIST_RDLOCK(&configs);
128                 AST_RWLIST_TRAVERSE(&configs, cur, entry) {
129                         if (!strcmp(cur->filename, args.filename)) {
130                                 break;
131                         }
132                 }
133
134                 if (!cur) {
135                         /* At worst, we might leak an entry while upgrading locks */
136                         AST_RWLIST_UNLOCK(&configs);
137                         AST_RWLIST_WRLOCK(&configs);
138                         if (!(cur = ast_calloc(1, sizeof(*cur) + strlen(args.filename) + 1))) {
139                                 AST_RWLIST_UNLOCK(&configs);
140                                 return -1;
141                         }
142
143                         strcpy(cur->filename, args.filename);
144
145                         ast_clear_flag(&cfg_flags, CONFIG_FLAG_FILEUNCHANGED);
146                         if (!(cfg = ast_config_load(args.filename, cfg_flags)) || cfg == CONFIG_STATUS_FILEINVALID) {
147                                 ast_free(cur);
148                                 AST_RWLIST_UNLOCK(&configs);
149                                 return -1;
150                         }
151
152                         cur->cfg = cfg;
153                         AST_RWLIST_INSERT_TAIL(&configs, cur, entry);
154                 }
155
156                 cfg = cur->cfg;
157         } else {
158                 /* Replace cfg in list */
159                 AST_RWLIST_WRLOCK(&configs);
160                 AST_RWLIST_TRAVERSE(&configs, cur, entry) {
161                         if (!strcmp(cur->filename, args.filename)) {
162                                 break;
163                         }
164                 }
165
166                 if (!cur) {
167                         if (!(cur = ast_calloc(1, sizeof(*cur) + strlen(args.filename) + 1))) {
168                                 AST_RWLIST_UNLOCK(&configs);
169                                 return -1;
170                         }
171
172                         strcpy(cur->filename, args.filename);
173                         cur->cfg = cfg;
174
175                         AST_RWLIST_INSERT_TAIL(&configs, cur, entry);
176                 } else {
177                         ast_config_destroy(cur->cfg);
178                         cur->cfg = cfg;
179                 }
180         }
181
182         for (var = ast_category_root(cfg, args.category); var; var = var->next) {
183                 if (strcasecmp(args.variable, var->name)) {
184                         continue;
185                 }
186                 found = var;
187                 if (index == -1) {
188                         continue;
189                 }
190                 if (ix == index) {
191                         break;
192                 }
193                 found = NULL;
194                 ix++;
195         }
196
197         if (!found) {
198                 ast_debug(1, "'%s' not found at index %d in [%s] of '%s'.  Maximum index found: %d\n",
199                         args.variable, index, args.category, args.filename, ix);
200                 AST_RWLIST_UNLOCK(&configs);
201                 return -1;
202         }
203
204         ast_copy_string(buf, found->value, len);
205
206         /* Unlock down here, so there's no chance the struct goes away while we're using it. */
207         AST_RWLIST_UNLOCK(&configs);
208
209         return 0;
210 }
211
212 static struct ast_custom_function config_function = {
213         .name = "AST_CONFIG",
214         .read = config_function_read,
215 };
216
217 static int unload_module(void)
218 {
219         struct config_item *current;
220         int res = ast_custom_function_unregister(&config_function);
221
222         AST_RWLIST_WRLOCK(&configs);
223         while ((current = AST_RWLIST_REMOVE_HEAD(&configs, entry))) {
224                 ast_config_destroy(current->cfg);
225                 ast_free(current);
226         }
227         AST_RWLIST_UNLOCK(&configs);
228
229         return res;
230 }
231
232 static int load_module(void)
233 {
234         return ast_custom_function_register(&config_function);
235 }
236
237 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Asterisk configuration file variable access");