Asterisk - The Open Source Telephony Project  21.4.1
utils/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"
44 #include "asterisk/ael_structs.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 
77 static char *config = "extensions.ael";
78 static char *registrar = "pbx_ael";
79 static 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
90 int option_matches_j( struct argdesc *should, pval *is, struct argapp *app);
91 int option_matches( struct argdesc *should, pval *is, struct argapp *app);
92 int ael_is_funcname(char *name);
93 #endif
94 
95 int check_app_args(pval *appcall, pval *arglist, struct argapp *app);
96 void check_pval(pval *item, struct argapp *apps, int in_globals);
97 void check_pval_item(pval *item, struct argapp *apps, int in_globals);
98 void check_switch_expr(pval *item, struct argapp *apps);
99 void ast_expr_register_extra_error_info(char *errmsg);
100 void ast_expr_clear_extra_error_info(void);
101 struct pval *find_context(char *name);
102 struct pval *find_context(char *name);
103 struct ael_priority *new_prio(void);
104 struct ael_extension *new_exten(void);
105 void destroy_extensions(struct ael_extension *exten);
106 void set_priorities(struct ael_extension *exten);
107 void add_extensions(struct ael_extension *exten);
108 int ast_compile_ael2(struct ast_context **local_contexts, struct ast_hashtab *local_table, struct pval *root);
109 void destroy_pval(pval *item);
110 void destroy_pval_item(pval *item);
111 int is_float(char *arg );
112 int is_int(char *arg );
113 int is_empty(char *arg);
114 
115 /* static void substitute_commas(char *str); */
116 
117 static int aeldebug = 0;
118 
119 /* interface stuff */
120 
121 #ifndef STANDALONE
122 static char *aelsub = "AELSub";
123 
124 static 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");
129  AST_APP_ARG(name);
130  AST_APP_ARG(args);
131  );
132 
133  if (gosub) {
134  AST_STANDARD_RAW_ARGS(args, data);
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 
144 static 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;
149  struct ast_hashtab *local_table=NULL;
150 
151  struct pval *parse_tree;
152 
153  ast_log(LOG_DEBUG, "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_log(LOG_DEBUG, "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_log(LOG_DEBUG, "AEL load process: checked config file name '%s'.\n", rfilename);
170  local_table = ast_hashtab_create(11, ast_hashtab_compare_contexts, ast_hashtab_resize_java, ast_hashtab_newsize_java, ast_hashtab_hash_contexts, 0);
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_log(LOG_DEBUG, "AEL load process: compiled config file name '%s'.\n", rfilename);
177 
178  ast_merge_contexts_and_delete(&local_contexts, local_table, registrar);
179  local_table = NULL; /* it's the dialplan global now */
180  local_contexts = NULL;
181  ast_log(LOG_DEBUG, "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_log(LOG_DEBUG, "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 */
196 static 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"))
215  aeldebug |= DEBUG_READ;
216  else if (!strcasecmp(a->argv[3], "tokens"))
217  aeldebug |= DEBUG_TOKENS;
218  else if (!strcasecmp(a->argv[3], "contexts"))
219  aeldebug |= DEBUG_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 
228 static 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 */
246  return ast_module_reload("pbx_ael") == AST_MODULE_RELOAD_SUCCESS ? CLI_SUCCESS : CLI_FAILURE;
247 #else
248  /* Lock-Protected reload not needed (and not available) when running standalone (Example: via aelparse cli tool). No reload contention is possible */
249  return (pbx_load_module() ? CLI_FAILURE : CLI_SUCCESS);
250 #endif
251 }
252 
253 static 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 
258 static int unload_module(void)
259 {
260  ast_context_destroy(NULL, registrar);
261  ast_cli_unregister_multiple(cli_ael, ARRAY_LEN(cli_ael));
262 #ifndef STANDALONE
264 #endif
265  return 0;
266 }
267 
268 static int load_module(void)
269 {
270  ast_cli_register_multiple(cli_ael, ARRAY_LEN(cli_ael));
271 #ifndef STANDALONE
272  ast_register_application_xml(aelsub, aelsub_exec);
273 #endif
274  return (pbx_load_module());
275 }
276 
277 static 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"
285 int ael_external_load_module(void);
286 int ael_external_load_module(void)
287 {
288  pbx_load_module();
289  return 1;
290 }
291 #endif
292 
293 AST_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",
299 );
300 
301 #ifdef AAL_ARGCHECK
302 static 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 
361 int 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 ast_hashtab_compare_contexts(const void *ah_a, const void *ah_b)
hashtable functions for contexts
Definition: ael_main.c:589
Main Channel structure associated with a channel.
Asterisk main include file. File version handling, generic pbx functions.
int ast_hashtab_newsize_java(struct ast_hashtab *tab)
Create a prime number roughly 2x the current table size.
Definition: main/hashtab.c:127
int pbx_exec(struct ast_channel *c, struct ast_app *app, const char *data)
Execute an application.
Definition: pbx_app.c:471
CallerID (and other GR30) management and generation Includes code and algorithms from the Zapata libr...
int ast_cli_unregister_multiple(struct ast_cli_entry *e, int len)
Unregister multiple commands.
Definition: clicompat.c:30
Registered applications container.
Definition: pbx_app.c:67
enum ast_module_reload_result ast_module_reload(const char *name)
Reload asterisk modules.
Definition: loader.c:1721
descriptor for a cli entry.
Definition: cli.h:171
Generic (perhaps overly so) hashtable implementation Hash Table support in Asterisk.
#define ast_cli_register_multiple(e, len)
Register multiple commands.
Definition: cli.h:265
int ast_unregister_application(const char *app)
Unregister an application.
Definition: pbx_app.c:392
Definition: pval.h:110
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: main/hashtab.c:84
int args
This gets set in ast_cli_register()
Definition: cli.h:185
Configuration File Parser.
#define ast_strdupa(s)
duplicate a string in memory from the stack
Definition: astmm.h:298
Definition: pval.h:48
Core PBX routines and definitions.
#define ast_alloca(size)
call __builtin_alloca to ensure we get gcc builtin semantics
Definition: astmm.h:288
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
char * command
Definition: cli.h:186
static struct ast_context * find_context(const char *context)
lookup for a context with a given name,
Definition: pbx.c:4800
Structures for AEL - the Asterisk extension language.
Support for logging to various files, console and syslog Configuration in file logger.conf.
Module has failed to load, may be in an inconsistent state.
Definition: module.h:78
const char * usage
Definition: cli.h:177
Standard Command Line Interface.
ast_app: A registered application
Definition: pbx_app.c:45
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
#define ast_hashtab_create(initial_buckets, compare, resize, newsize, hash, do_locking)
Create the hashtable list.
Definition: hashtab.h:254
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
#define ASTERISK_GPL_KEY
The text the key() function should return.
Definition: module.h:46
Asterisk module definitions.
#define AST_DECLARE_APP_ARGS(name, arglist)
Declare a structure to hold an application's arguments.
Application convenience functions, designed to give consistent look and feel to Asterisk apps...
ast_context: An extension context
Definition: pbx.c:284
#define ast_register_application_xml(app, execute)
Register an application using XML documentation.
Definition: module.h:640
#define AST_APP_ARG(name)
Define an application argument.