add detection for timersub() and winsock.h/winsock2.h
[asterisk/asterisk.git] / pbx / pbx_ael.c
1 /* 
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 2006, Digium, Inc.
5  *
6  * Steve Murphy <murf@parsetree.com>
7  *
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.
13  *
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.
17  */
18
19 /*! \file
20  *
21  * \brief Compile symbolic Asterisk Extension Logic into Asterisk extensions, version 2.
22  * 
23  */
24
25 /*** MODULEINFO
26         <depend>res_ael_share</depend>
27  ***/
28
29 #include "asterisk.h"
30
31 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
32
33 #include <ctype.h>
34 #include <errno.h>
35 #include <regex.h>
36 #include <sys/stat.h>
37
38 #include "asterisk/pbx.h"
39 #include "asterisk/config.h"
40 #include "asterisk/module.h"
41 #include "asterisk/logger.h"
42 #include "asterisk/cli.h"
43 #include "asterisk/app.h"
44 #include "asterisk/callerid.h"
45 #include "asterisk/ael_structs.h"
46 #include "asterisk/pval.h"
47 #ifdef AAL_ARGCHECK
48 #include "asterisk/argdesc.h"
49 #endif
50
51 /* these functions are in ../ast_expr2.fl */
52
53 #define DEBUG_READ   (1 << 0)
54 #define DEBUG_TOKENS (1 << 1)
55 #define DEBUG_MACROS (1 << 2)
56 #define DEBUG_CONTEXTS (1 << 3)
57
58 static char *config = "extensions.ael";
59 static char *registrar = "pbx_ael";
60 static int pbx_load_module(void);
61
62 #ifndef AAL_ARGCHECK
63 /* for the time being, short circuit all the AAL related structures
64    without permanently removing the code; after/during the AAL 
65    development, this code can be properly re-instated 
66 */
67
68 #endif
69
70 #ifdef AAL_ARGCHECK
71 int option_matches_j( struct argdesc *should, pval *is, struct argapp *app);
72 int option_matches( struct argdesc *should, pval *is, struct argapp *app);
73 int ael_is_funcname(char *name);
74 #endif
75
76 int check_app_args(pval *appcall, pval *arglist, struct argapp *app);
77 void check_pval(pval *item, struct argapp *apps, int in_globals);
78 void check_pval_item(pval *item, struct argapp *apps, int in_globals);
79 void check_switch_expr(pval *item, struct argapp *apps);
80 void ast_expr_register_extra_error_info(char *errmsg);
81 void ast_expr_clear_extra_error_info(void);
82 struct pval *find_macro(char *name);
83 struct pval *find_context(char *name);
84 struct pval *find_context(char *name);
85 struct pval *find_macro(char *name);
86 struct ael_priority *new_prio(void);
87 struct ael_extension *new_exten(void);
88 void linkprio(struct ael_extension *exten, struct ael_priority *prio);
89 void destroy_extensions(struct ael_extension *exten);
90 void set_priorities(struct ael_extension *exten);
91 void add_extensions(struct ael_extension *exten);
92 void ast_compile_ael2(struct ast_context **local_contexts, struct pval *root);
93 void destroy_pval(pval *item);
94 void destroy_pval_item(pval *item);
95 int is_float(char *arg );
96 int is_int(char *arg );
97 int is_empty(char *arg);
98
99 /* static void substitute_commas(char *str); */
100
101 static int aeldebug = 0;
102
103 /* interface stuff */
104
105 /* if all the below are static, who cares if they are present? */
106
107 static int pbx_load_module(void)
108 {
109         int errs=0, sem_err=0, sem_warn=0, sem_note=0;
110         char *rfilename;
111         struct ast_context *local_contexts=NULL, *con;
112         struct pval *parse_tree;
113
114         ast_log(LOG_NOTICE, "Starting AEL load process.\n");
115         if (config[0] == '/')
116                 rfilename = (char *)config;
117         else {
118                 rfilename = alloca(strlen(config) + strlen(ast_config_AST_CONFIG_DIR) + 2);
119                 sprintf(rfilename, "%s/%s", ast_config_AST_CONFIG_DIR, config);
120         }
121         if (access(rfilename,R_OK) != 0) {
122                 ast_log(LOG_NOTICE, "File %s not found; AEL declining load\n", rfilename);
123                 return AST_MODULE_LOAD_DECLINE;
124         }
125         
126         parse_tree = ael2_parse(rfilename, &errs);
127         ast_log(LOG_NOTICE, "AEL load process: parsed config file name '%s'.\n", rfilename);
128         ael2_semantic_check(parse_tree, &sem_err, &sem_warn, &sem_note);
129         if (errs == 0 && sem_err == 0) {
130                 ast_log(LOG_NOTICE, "AEL load process: checked config file name '%s'.\n", rfilename);
131                 ast_compile_ael2(&local_contexts, parse_tree);
132                 ast_log(LOG_NOTICE, "AEL load process: compiled config file name '%s'.\n", rfilename);
133                 
134                 ast_merge_contexts_and_delete(&local_contexts, registrar);
135                 ast_log(LOG_NOTICE, "AEL load process: merged config file name '%s'.\n", rfilename);
136                 for (con = ast_walk_contexts(NULL); con; con = ast_walk_contexts(con))
137                         ast_context_verify_includes(con);
138                 ast_log(LOG_NOTICE, "AEL load process: verified config file name '%s'.\n", rfilename);
139         } else {
140                 ast_log(LOG_ERROR, "Sorry, but %d syntax errors and %d semantic errors were detected. It doesn't make sense to compile.\n", errs, sem_err);
141                 destroy_pval(parse_tree); /* free up the memory */
142                 return AST_MODULE_LOAD_DECLINE;
143         }
144         destroy_pval(parse_tree); /* free up the memory */
145         
146         return AST_MODULE_LOAD_SUCCESS;
147 }
148
149 /* CLI interface */
150 static char *handle_cli_ael_debug_multiple(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
151 {
152         switch (cmd) {
153         case CLI_INIT:
154                 e->command = "ael debug [read|tokens|macros|contexts|off]";
155                 e->usage =
156                         "Usage: ael debug [read|tokens|macros|contexts|off]\n"
157                         "       Enable AEL read, token, macro, or context debugging,\n"
158                         "       or disable all AEL debugging messages.  Note: this\n"
159                         "       currently does nothing.\n";
160                 return NULL;
161         case CLI_GENERATE:
162                 return NULL;
163         }
164
165         if (a->argc != 3)
166                 return CLI_SHOWUSAGE;
167
168         if (!strcasecmp(a->argv[2], "read"))
169                 aeldebug |= DEBUG_READ;
170         else if (!strcasecmp(a->argv[2], "tokens"))
171                 aeldebug |= DEBUG_TOKENS;
172         else if (!strcasecmp(a->argv[2], "macros"))
173                 aeldebug |= DEBUG_MACROS;
174         else if (!strcasecmp(a->argv[2], "contexts"))
175                 aeldebug |= DEBUG_CONTEXTS;
176         else if (!strcasecmp(a->argv[2], "off"))
177                 aeldebug = 0;
178         else
179                 return CLI_SHOWUSAGE;
180
181         return CLI_SUCCESS;
182 }
183
184 static char *handle_cli_ael_reload(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
185 {
186         switch (cmd) {
187         case CLI_INIT:
188                 e->command = "ael reload";
189                 e->usage =
190                         "Usage: ael reload\n"
191                         "       Reloads AEL configuration.\n";
192                 return NULL;
193         case CLI_GENERATE:
194                 return NULL;
195         }
196
197         if (a->argc != 2)
198                 return CLI_SHOWUSAGE;
199
200         return (pbx_load_module() ? CLI_FAILURE : CLI_SUCCESS);
201 }
202
203 static struct ast_cli_entry cli_ael[] = {
204         AST_CLI_DEFINE(handle_cli_ael_reload,         "Reload AEL configuration"),
205         AST_CLI_DEFINE(handle_cli_ael_debug_multiple, "Enable AEL debugging flags")
206 };
207
208 static int unload_module(void)
209 {
210         ast_context_destroy(NULL, registrar);
211         ast_cli_unregister_multiple(cli_ael, sizeof(cli_ael) / sizeof(struct ast_cli_entry));
212         return 0;
213 }
214
215 static int load_module(void)
216 {
217         ast_cli_register_multiple(cli_ael, sizeof(cli_ael) / sizeof(struct ast_cli_entry));
218         return (pbx_load_module());
219 }
220
221 static int reload(void)
222 {
223         return pbx_load_module();
224 }
225
226 #ifdef STANDALONE_AEL
227 #define AST_MODULE "ael"
228 int ael_external_load_module(void);
229 int ael_external_load_module(void)
230 {
231         pbx_load_module();
232         return 1;
233 }
234 #endif
235
236 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "Asterisk Extension Language Compiler",
237                 .load = load_module,
238                 .unload = unload_module,
239                 .reload = reload,
240                );
241
242 #ifdef AAL_ARGCHECK
243 static char *ael_funclist[] =
244 {
245         "AGENT",
246         "ARRAY",
247         "BASE64_DECODE",
248         "BASE64_ENCODE",
249         "CALLERID",
250         "CDR",
251         "CHANNEL",
252         "CHECKSIPDOMAIN",
253         "CHECK_MD5",
254         "CURL",
255         "CUT",
256         "DB",
257         "DB_EXISTS",
258         "DUNDILOOKUP",
259         "ENUMLOOKUP",
260         "ENV",
261         "EVAL",
262         "EXISTS",
263         "FIELDQTY",
264         "FILTER",
265         "GROUP",
266         "GROUP_COUNT",
267         "GROUP_LIST",
268         "GROUP_MATCH_COUNT",
269         "IAXPEER",
270         "IF",
271         "IFTIME",
272         "ISNULL",
273         "KEYPADHASH",
274         "LANGUAGE",
275         "LEN",
276         "MATH",
277         "MD5",
278         "MUSICCLASS",
279         "QUEUEAGENTCOUNT",
280         "QUEUE_MEMBER_COUNT",
281         "QUEUE_MEMBER_LIST",
282         "QUOTE",
283         "RAND",
284         "REGEX",
285         "SET",
286         "SHA1",
287         "SIPCHANINFO",
288         "SIPPEER",
289         "SIP_HEADER",
290         "SORT",
291         "STAT",
292         "STRFTIME",
293         "STRPTIME",
294         "TIMEOUT",
295         "TXTCIDNAME",
296         "URIDECODE",
297         "URIENCODE",
298         "VMCOUNT"
299 };
300
301
302 int ael_is_funcname(char *name)
303 {
304         int s,t;
305         t = sizeof(ael_funclist)/sizeof(char*);
306         s = 0;
307         while ((s < t) && strcasecmp(name, ael_funclist[s])) 
308                 s++;
309         if ( s < t )
310                 return 1;
311         else
312                 return 0;
313 }
314 #endif