Asterisk - The Open Source Telephony Project GIT-master-a358458
pbx_ael.c
Go to the documentation of this file.
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 <support_level>extended</support_level>
28 ***/
29
30#include "asterisk.h"
31
32#include <ctype.h>
33#include <regex.h>
34#include <sys/stat.h>
35
36#include "asterisk/pbx.h"
37#include "asterisk/config.h"
38#include "asterisk/module.h"
39#include "asterisk/logger.h"
40#include "asterisk/cli.h"
41#include "asterisk/app.h"
42#include "asterisk/callerid.h"
43#include "asterisk/hashtab.h"
45#include "asterisk/pval.h"
46#ifdef AAL_ARGCHECK
47#include "asterisk/argdesc.h"
48#endif
49
50/*** DOCUMENTATION
51 <application name="AELSub" language="en_US">
52 <synopsis>
53 Launch subroutine built with AEL
54 </synopsis>
55 <syntax>
56 <parameter name="routine" required="true">
57 <para>Named subroutine to execute.</para>
58 </parameter>
59 <parameter name="args" required="false" />
60 </syntax>
61 <description>
62 <para>Execute the named subroutine, defined in AEL, from another dialplan
63 language, such as extensions.conf, Realtime extensions, or Lua.</para>
64 <para>The purpose of this application is to provide a sane entry point into
65 AEL subroutines, the implementation of which may change from time to time.</para>
66 </description>
67 </application>
68 ***/
69
70/* these functions are in ../ast_expr2.fl */
71
72#define DEBUG_READ (1 << 0)
73#define DEBUG_TOKENS (1 << 1)
74#define DEBUG_MACROS (1 << 2)
75#define DEBUG_CONTEXTS (1 << 3)
76
77static char *config = "extensions.ael";
78static char *registrar = "pbx_ael";
79static int pbx_load_module(void);
80
81#ifndef AAL_ARGCHECK
82/* for the time being, short circuit all the AAL related structures
83 without permanently removing the code; after/during the AAL
84 development, this code can be properly re-instated
85*/
86
87#endif
88
89#ifdef AAL_ARGCHECK
90int option_matches_j( struct argdesc *should, pval *is, struct argapp *app);
91int option_matches( struct argdesc *should, pval *is, struct argapp *app);
92int ael_is_funcname(char *name);
93#endif
94
95int check_app_args(pval *appcall, pval *arglist, struct argapp *app);
96void check_pval(pval *item, struct argapp *apps, int in_globals);
97void check_pval_item(pval *item, struct argapp *apps, int in_globals);
98void check_switch_expr(pval *item, struct argapp *apps);
99void ast_expr_register_extra_error_info(char *errmsg);
101struct pval *find_context(char *name);
102struct pval *find_context(char *name);
103struct ael_priority *new_prio(void);
104struct ael_extension *new_exten(void);
105void destroy_extensions(struct ael_extension *exten);
106void set_priorities(struct ael_extension *exten);
107void add_extensions(struct ael_extension *exten);
108int ast_compile_ael2(struct ast_context **local_contexts, struct ast_hashtab *local_table, struct pval *root);
109void destroy_pval(pval *item);
111int is_float(char *arg );
112int is_int(char *arg );
113int is_empty(char *arg);
114
115/* static void substitute_commas(char *str); */
116
117static int aeldebug = 0;
118
119/* interface stuff */
120
121#ifndef STANDALONE
122static char *aelsub = "AELSub";
123
124static int aelsub_exec(struct ast_channel *chan, const char *vdata)
125{
126 char buf[256], *data = ast_strdupa(vdata);
127 struct ast_app *gosub = pbx_findapp("Gosub");
131 );
132
133 if (gosub) {
135 snprintf(buf, sizeof(buf), "%s,~~s~~,1(%s)", args.name, args.args);
136 return pbx_exec(chan, gosub, buf);
137 }
138 return -1;
139}
140#endif
141
142/* if all the below are static, who cares if they are present? */
143
144static int pbx_load_module(void)
145{
146 int errs=0, sem_err=0, sem_warn=0, sem_note=0;
147 char *rfilename;
148 struct ast_context *local_contexts=NULL, *con;
150
151 struct pval *parse_tree;
152
153 ast_debug(1, "Starting AEL load process.\n");
154 if (config[0] == '/')
155 rfilename = (char *)config;
156 else {
157 rfilename = ast_alloca(strlen(config) + strlen(ast_config_AST_CONFIG_DIR) + 2);
158 sprintf(rfilename, "%s/%s", ast_config_AST_CONFIG_DIR, config);
159 }
160 if (access(rfilename,R_OK) != 0) {
161 ast_log(LOG_NOTICE, "File %s not found; AEL declining load\n", rfilename);
163 }
164
165 parse_tree = ael2_parse(rfilename, &errs);
166 ast_debug(1, "AEL load process: parsed config file name '%s'.\n", rfilename);
167 ael2_semantic_check(parse_tree, &sem_err, &sem_warn, &sem_note);
168 if (errs == 0 && sem_err == 0) {
169 ast_debug(1, "AEL load process: checked config file name '%s'.\n", rfilename);
171 if (ast_compile_ael2(&local_contexts, local_table, parse_tree)) {
172 ast_log(LOG_ERROR, "AEL compile failed! Aborting.\n");
173 destroy_pval(parse_tree); /* free up the memory */
175 }
176 ast_debug(1, "AEL load process: compiled config file name '%s'.\n", rfilename);
177
179 local_table = NULL; /* it's the dialplan global now */
181 ast_debug(1, "AEL load process: merged config file name '%s'.\n", rfilename);
182 for (con = ast_walk_contexts(NULL); con; con = ast_walk_contexts(con))
184 ast_debug(1, "AEL load process: verified config file name '%s'.\n", rfilename);
185 } else {
186 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);
187 destroy_pval(parse_tree); /* free up the memory */
189 }
190 destroy_pval(parse_tree); /* free up the memory */
191
193}
194
195/* CLI interface */
196static char *handle_cli_ael_set_debug(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
197{
198 switch (cmd) {
199 case CLI_INIT:
200 e->command = "ael set debug {read|tokens|contexts|off}";
201 e->usage =
202 "Usage: ael set debug {read|tokens|contexts|off}\n"
203 " Enable AEL read, token, or context debugging,\n"
204 " or disable all AEL debugging messages. Note: this\n"
205 " currently does nothing.\n";
206 return NULL;
207 case CLI_GENERATE:
208 return NULL;
209 }
210
211 if (a->argc != e->args)
212 return CLI_SHOWUSAGE;
213
214 if (!strcasecmp(a->argv[3], "read"))
216 else if (!strcasecmp(a->argv[3], "tokens"))
218 else if (!strcasecmp(a->argv[3], "contexts"))
220 else if (!strcasecmp(a->argv[3], "off"))
221 aeldebug = 0;
222 else
223 return CLI_SHOWUSAGE;
224
225 return CLI_SUCCESS;
226}
227
228static char *handle_cli_ael_reload(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
229{
230 switch (cmd) {
231 case CLI_INIT:
232 e->command = "ael reload";
233 e->usage =
234 "Usage: ael reload\n"
235 " Reloads AEL configuration.\n";
236 return NULL;
237 case CLI_GENERATE:
238 return NULL;
239 }
240
241 if (a->argc != 2)
242 return CLI_SHOWUSAGE;
243
244#ifndef STANDALONE
245 /* Lock-Protected reload. It is VERY BAD to have simultaneous ael load_module() executing at the same time */
247#else
248 /* Lock-Protected reload not needed (and not available) when running standalone (Example: via aelparse cli tool). No reload contention is possible */
250#endif
251}
252
253static struct ast_cli_entry cli_ael[] = {
254 AST_CLI_DEFINE(handle_cli_ael_reload, "Reload AEL configuration"),
255 AST_CLI_DEFINE(handle_cli_ael_set_debug, "Enable AEL debugging flags")
256};
257
258static int unload_module(void)
259{
262#ifndef STANDALONE
264#endif
265 return 0;
266}
267
268static int load_module(void)
269{
271#ifndef STANDALONE
273#endif
274 return (pbx_load_module());
275}
276
277static int reload(void)
278{
279 /* Lock-Protected reload not needed because we're already being called from ast_module_reload() */
280 return pbx_load_module();
281}
282
283#ifdef STANDALONE
284#define AST_MODULE "ael"
287{
289 return 1;
290}
291#endif
292
293AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "Asterisk Extension Language Compiler",
294 .support_level = AST_MODULE_SUPPORT_EXTENDED,
295 .load = load_module,
296 .unload = unload_module,
297 .reload = reload,
298 .requires = "res_ael_share",
300
301#ifdef AAL_ARGCHECK
302static const char * const ael_funclist[] =
303{
304 "AGENT",
305 "ARRAY",
306 "BASE64_DECODE",
307 "BASE64_ENCODE",
308 "CALLERID",
309 "CDR",
310 "CHANNEL",
311 "CHECKSIPDOMAIN",
312 "CHECK_MD5",
313 "CURL",
314 "CUT",
315 "DB",
316 "DB_EXISTS",
317 "DUNDILOOKUP",
318 "ENUMLOOKUP",
319 "ENV",
320 "EVAL",
321 "EXISTS",
322 "FIELDQTY",
323 "FILTER",
324 "GROUP",
325 "GROUP_COUNT",
326 "GROUP_LIST",
327 "GROUP_MATCH_COUNT",
328 "IAXPEER",
329 "IF",
330 "IFTIME",
331 "ISNULL",
332 "KEYPADHASH",
333 "LANGUAGE",
334 "LEN",
335 "MATH",
336 "MD5",
337 "MUSICCLASS",
338 "QUEUEAGENTCOUNT",
339 "QUEUE_MEMBER_COUNT",
340 "QUEUE_MEMBER_LIST",
341 "QUOTE",
342 "RAND",
343 "REGEX",
344 "SET",
345 "SHA1",
346 "SIPCHANINFO",
347 "SIPPEER",
348 "SIP_HEADER",
349 "SORT",
350 "STAT",
351 "STRFTIME",
352 "STRPTIME",
353 "TIMEOUT",
354 "TXTCIDNAME",
355 "URIDECODE",
356 "URIENCODE",
357 "VMCOUNT"
358};
359
360
361int ael_is_funcname(char *name)
362{
363 int s,t;
364 t = sizeof(ael_funclist)/sizeof(char*);
365 s = 0;
366 while ((s < t) && strcasecmp(name, ael_funclist[s]))
367 s++;
368 if ( s < t )
369 return 1;
370 else
371 return 0;
372}
373#endif
int ael_external_load_module(void)
Structures for AEL - the Asterisk extension language.
struct pval * ael2_parse(char *fname, int *errs)
Definition: ael_lex.c:3344
void ael2_semantic_check(pval *item, int *errs, int *warns, int *notes)
Definition: pval.c:2885
static const char app[]
Definition: app_adsiprog.c:56
Asterisk main include file. File version handling, generic pbx functions.
#define ast_alloca(size)
call __builtin_alloca to ensure we get gcc builtin semantics
Definition: astmm.h:288
#define ast_strdupa(s)
duplicate a string in memory from the stack
Definition: astmm.h:298
#define ast_log
Definition: astobj2.c:42
CallerID (and other GR30) management and generation Includes code and algorithms from the Zapata libr...
Standard Command Line Interface.
#define CLI_SHOWUSAGE
Definition: cli.h:45
#define CLI_SUCCESS
Definition: cli.h:44
int ast_cli_unregister_multiple(struct ast_cli_entry *e, int len)
Unregister multiple commands.
Definition: clicompat.c:30
#define AST_CLI_DEFINE(fn, txt,...)
Definition: cli.h:197
@ CLI_INIT
Definition: cli.h:152
@ CLI_GENERATE
Definition: cli.h:153
#define CLI_FAILURE
Definition: cli.h:46
#define ast_cli_register_multiple(e, len)
Register multiple commands.
Definition: cli.h:265
char buf[BUFSIZE]
Definition: eagi_proxy.c:66
static const char name[]
Definition: format_mp3.c:68
Generic (perhaps overly so) hashtable implementation Hash Table support in Asterisk.
int ast_hashtab_newsize_java(struct ast_hashtab *tab)
Create a prime number roughly 2x the current table size.
Definition: hashtab.c:127
#define ast_hashtab_create(initial_buckets, compare, resize, newsize, hash, do_locking)
Create the hashtable list.
Definition: hashtab.h:254
int ast_hashtab_resize_java(struct ast_hashtab *tab)
Determines if a table resize should occur using the Java algorithm (if the table load factor is 75% o...
Definition: hashtab.c:84
Application convenience functions, designed to give consistent look and feel to Asterisk apps.
#define AST_APP_ARG(name)
Define an application argument.
#define AST_STANDARD_RAW_ARGS(args, parse)
#define AST_DECLARE_APP_ARGS(name, arglist)
Declare a structure to hold an application's arguments.
Configuration File Parser.
Support for logging to various files, console and syslog Configuration in file logger....
#define ast_debug(level,...)
Log a DEBUG message.
#define LOG_ERROR
#define LOG_NOTICE
Asterisk module definitions.
@ AST_MODFLAG_DEFAULT
Definition: module.h:315
#define AST_MODULE_INFO(keystr, flags_to_set, desc, fields...)
Definition: module.h:543
@ AST_MODULE_SUPPORT_EXTENDED
Definition: module.h:122
#define ASTERISK_GPL_KEY
The text the key() function should return.
Definition: module.h:46
@ AST_MODULE_RELOAD_SUCCESS
Definition: module.h:110
int ast_unregister_application(const char *app)
Unregister an application.
Definition: pbx_app.c:392
@ AST_MODULE_LOAD_SUCCESS
Definition: module.h:70
@ AST_MODULE_LOAD_DECLINE
Module has failed to load, may be in an inconsistent state.
Definition: module.h:78
enum ast_module_reload_result ast_module_reload(const char *name)
Reload asterisk modules.
Definition: loader.c:1567
#define ast_register_application_xml(app, execute)
Register an application using XML documentation.
Definition: module.h:626
const char * ast_config_AST_CONFIG_DIR
Definition: options.c:151
Core PBX routines and definitions.
struct ast_context * ast_walk_contexts(struct ast_context *con)
Definition: extconf.c:4024
int ast_hashtab_compare_contexts(const void *ah_a, const void *ah_b)
hashtable functions for contexts
Definition: ael_main.c:589
unsigned int ast_hashtab_hash_contexts(const void *obj)
Definition: ael_main.c:596
int pbx_exec(struct ast_channel *c, struct ast_app *app, const char *data)
Execute an application.
Definition: pbx_app.c:471
void ast_merge_contexts_and_delete(struct ast_context **extcontexts, struct ast_hashtab *exttable, const char *registrar)
Merge the temporary contexts into a global contexts list and delete from the global list the ones tha...
Definition: pbx.c:6426
void ast_context_destroy(struct ast_context *con, const char *registrar)
Destroy a context (matches the specified context or ANY context if NULL)
Definition: pbx.c:8221
int ast_context_verify_includes(struct ast_context *con)
Verifies includes in an ast_contect structure.
Definition: pbx.c:8732
struct ast_app * pbx_findapp(const char *app)
Look up an application.
Definition: ael_main.c:165
int ast_compile_ael2(struct ast_context **local_contexts, struct ast_hashtab *local_table, struct pval *root)
Definition: pval.c:4413
void check_switch_expr(pval *item, struct argapp *apps)
Definition: pval.c:2184
struct ael_priority * new_prio(void)
Definition: pval.c:2924
void destroy_pval_item(pval *item)
Definition: pval.c:4672
int is_empty(char *arg)
Definition: pval.c:1981
void check_pval_item(pval *item, struct argapp *apps, int in_globals)
Definition: pval.c:2357
#define DEBUG_CONTEXTS
Definition: pbx_ael.c:75
static char * aelsub
Definition: pbx_ael.c:122
#define DEBUG_TOKENS
Definition: pbx_ael.c:73
int check_app_args(pval *appcall, pval *arglist, struct argapp *app)
Definition: pval.c:2130
static int pbx_load_module(void)
Definition: pbx_ael.c:144
int is_float(char *arg)
Definition: pval.c:1963
static struct ast_cli_entry cli_ael[]
Definition: pbx_ael.c:253
void set_priorities(struct ael_extension *exten)
Definition: pval.c:4187
static char * config
Definition: pbx_ael.c:77
void ast_expr_clear_extra_error_info(void)
Definition: ast_expr2f.c:2469
static int aelsub_exec(struct ast_channel *chan, const char *vdata)
Definition: pbx_ael.c:124
void check_pval(pval *item, struct argapp *apps, int in_globals)
Definition: pval.c:2865
struct ael_extension * new_exten(void)
Definition: pval.c:2930
struct pval * find_context(char *name)
Definition: pval.c:1953
void destroy_pval(pval *item)
Definition: pval.c:4940
void destroy_extensions(struct ael_extension *exten)
Definition: pval.c:2978
static char * handle_cli_ael_set_debug(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
Definition: pbx_ael.c:196
#define DEBUG_READ
Definition: pbx_ael.c:72
static int aeldebug
Definition: pbx_ael.c:117
static char * registrar
Definition: pbx_ael.c:78
static int load_module(void)
Definition: pbx_ael.c:268
void add_extensions(struct ael_extension *exten)
Definition: pval.c:4213
static int unload_module(void)
Definition: pbx_ael.c:258
static int reload(void)
Definition: pbx_ael.c:277
static char * handle_cli_ael_reload(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
Definition: pbx_ael.c:228
int is_int(char *arg)
Definition: pval.c:1972
void ast_expr_register_extra_error_info(char *errmsg)
Definition: ast_expr2f.c:2463
static struct ast_context * local_contexts
Definition: pbx_config.c:113
static struct ast_hashtab * local_table
Definition: pbx_config.c:114
static int errs
Definition: pval.c:65
#define NULL
Definition: resample.c:96
Registered applications container.
Definition: pbx_app.c:67
Definition: pval.h:111
ast_app: A registered application
Definition: pbx_app.c:45
Main Channel structure associated with a channel.
descriptor for a cli entry.
Definition: cli.h:171
int args
This gets set in ast_cli_register()
Definition: cli.h:185
char * command
Definition: cli.h:186
const char * usage
Definition: cli.h:177
ast_context: An extension context
Definition: pbx.c:284
Definition: pval.h:49
static struct aco_type item
Definition: test_config.c:1463
const char * args
static struct test_val a
#define ARRAY_LEN(a)
Definition: utils.h:666