00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029 #include "asterisk.h"
00030
00031 #if !defined(STANDALONE)
00032 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 271484 $")
00033 #endif
00034
00035 #include <ctype.h>
00036 #include <regex.h>
00037 #include <sys/stat.h>
00038
00039 #ifdef STANDALONE
00040 #ifdef HAVE_MTX_PROFILE
00041 static int mtx_prof = -1;
00042 #endif
00043 #endif
00044 #include "asterisk/pbx.h"
00045 #include "asterisk/config.h"
00046 #include "asterisk/module.h"
00047 #include "asterisk/logger.h"
00048 #include "asterisk/cli.h"
00049 #include "asterisk/app.h"
00050 #include "asterisk/callerid.h"
00051 #include "asterisk/hashtab.h"
00052 #include "asterisk/ael_structs.h"
00053 #include "asterisk/pval.h"
00054 #ifdef AAL_ARGCHECK
00055 #include "asterisk/argdesc.h"
00056 #endif
00057
00058
00059
00060 #define DEBUG_READ (1 << 0)
00061 #define DEBUG_TOKENS (1 << 1)
00062 #define DEBUG_MACROS (1 << 2)
00063 #define DEBUG_CONTEXTS (1 << 3)
00064
00065 static char *config = "extensions.ael";
00066 static char *registrar = "pbx_ael";
00067 static int pbx_load_module(void);
00068
00069 #ifndef AAL_ARGCHECK
00070
00071
00072
00073
00074
00075 #endif
00076
00077 #ifdef AAL_ARGCHECK
00078 int option_matches_j( struct argdesc *should, pval *is, struct argapp *app);
00079 int option_matches( struct argdesc *should, pval *is, struct argapp *app);
00080 int ael_is_funcname(char *name);
00081 #endif
00082
00083 int check_app_args(pval *appcall, pval *arglist, struct argapp *app);
00084 void check_pval(pval *item, struct argapp *apps, int in_globals);
00085 void check_pval_item(pval *item, struct argapp *apps, int in_globals);
00086 void check_switch_expr(pval *item, struct argapp *apps);
00087 void ast_expr_register_extra_error_info(char *errmsg);
00088 void ast_expr_clear_extra_error_info(void);
00089 struct pval *find_macro(char *name);
00090 struct pval *find_context(char *name);
00091 struct pval *find_context(char *name);
00092 struct pval *find_macro(char *name);
00093 struct ael_priority *new_prio(void);
00094 struct ael_extension *new_exten(void);
00095 void destroy_extensions(struct ael_extension *exten);
00096 void set_priorities(struct ael_extension *exten);
00097 void add_extensions(struct ael_extension *exten);
00098 int ast_compile_ael2(struct ast_context **local_contexts, struct ast_hashtab *local_table, struct pval *root);
00099 void destroy_pval(pval *item);
00100 void destroy_pval_item(pval *item);
00101 int is_float(char *arg );
00102 int is_int(char *arg );
00103 int is_empty(char *arg);
00104
00105
00106
00107 static int aeldebug = 0;
00108
00109
00110
00111
00112
00113 static int pbx_load_module(void)
00114 {
00115 int errs=0, sem_err=0, sem_warn=0, sem_note=0;
00116 char *rfilename;
00117 struct ast_context *local_contexts=NULL, *con;
00118 struct ast_hashtab *local_table=NULL;
00119
00120 struct pval *parse_tree;
00121
00122 ast_log(LOG_NOTICE, "Starting AEL load process.\n");
00123 if (config[0] == '/')
00124 rfilename = (char *)config;
00125 else {
00126 rfilename = alloca(strlen(config) + strlen(ast_config_AST_CONFIG_DIR) + 2);
00127 sprintf(rfilename, "%s/%s", ast_config_AST_CONFIG_DIR, config);
00128 }
00129 if (access(rfilename,R_OK) != 0) {
00130 ast_log(LOG_NOTICE, "File %s not found; AEL declining load\n", rfilename);
00131 return AST_MODULE_LOAD_DECLINE;
00132 }
00133
00134 parse_tree = ael2_parse(rfilename, &errs);
00135 ast_log(LOG_NOTICE, "AEL load process: parsed config file name '%s'.\n", rfilename);
00136 ael2_semantic_check(parse_tree, &sem_err, &sem_warn, &sem_note);
00137 if (errs == 0 && sem_err == 0) {
00138 ast_log(LOG_NOTICE, "AEL load process: checked config file name '%s'.\n", rfilename);
00139 local_table = ast_hashtab_create(11, ast_hashtab_compare_contexts, ast_hashtab_resize_java, ast_hashtab_newsize_java, ast_hashtab_hash_contexts, 0);
00140 if (ast_compile_ael2(&local_contexts, local_table, parse_tree)) {
00141 ast_log(LOG_ERROR, "AEL compile failed! Aborting.\n");
00142 destroy_pval(parse_tree);
00143 return AST_MODULE_LOAD_DECLINE;
00144 }
00145 ast_log(LOG_NOTICE, "AEL load process: compiled config file name '%s'.\n", rfilename);
00146
00147 ast_merge_contexts_and_delete(&local_contexts, local_table, registrar);
00148 local_table = NULL;
00149 local_contexts = NULL;
00150 ast_log(LOG_NOTICE, "AEL load process: merged config file name '%s'.\n", rfilename);
00151 for (con = ast_walk_contexts(NULL); con; con = ast_walk_contexts(con))
00152 ast_context_verify_includes(con);
00153 ast_log(LOG_NOTICE, "AEL load process: verified config file name '%s'.\n", rfilename);
00154 } else {
00155 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);
00156 destroy_pval(parse_tree);
00157 return AST_MODULE_LOAD_DECLINE;
00158 }
00159 destroy_pval(parse_tree);
00160
00161 return AST_MODULE_LOAD_SUCCESS;
00162 }
00163
00164
00165 static char *handle_cli_ael_set_debug(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
00166 {
00167 switch (cmd) {
00168 case CLI_INIT:
00169 e->command = "ael set debug {read|tokens|macros|contexts|off}";
00170 e->usage =
00171 "Usage: ael set debug {read|tokens|macros|contexts|off}\n"
00172 " Enable AEL read, token, macro, or context debugging,\n"
00173 " or disable all AEL debugging messages. Note: this\n"
00174 " currently does nothing.\n";
00175 return NULL;
00176 case CLI_GENERATE:
00177 return NULL;
00178 }
00179
00180 if (a->argc != e->args)
00181 return CLI_SHOWUSAGE;
00182
00183 if (!strcasecmp(a->argv[3], "read"))
00184 aeldebug |= DEBUG_READ;
00185 else if (!strcasecmp(a->argv[3], "tokens"))
00186 aeldebug |= DEBUG_TOKENS;
00187 else if (!strcasecmp(a->argv[3], "macros"))
00188 aeldebug |= DEBUG_MACROS;
00189 else if (!strcasecmp(a->argv[3], "contexts"))
00190 aeldebug |= DEBUG_CONTEXTS;
00191 else if (!strcasecmp(a->argv[3], "off"))
00192 aeldebug = 0;
00193 else
00194 return CLI_SHOWUSAGE;
00195
00196 return CLI_SUCCESS;
00197 }
00198
00199 static char *handle_cli_ael_reload(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
00200 {
00201 switch (cmd) {
00202 case CLI_INIT:
00203 e->command = "ael reload";
00204 e->usage =
00205 "Usage: ael reload\n"
00206 " Reloads AEL configuration.\n";
00207 return NULL;
00208 case CLI_GENERATE:
00209 return NULL;
00210 }
00211
00212 if (a->argc != 2)
00213 return CLI_SHOWUSAGE;
00214
00215 return (pbx_load_module() ? CLI_FAILURE : CLI_SUCCESS);
00216 }
00217
00218 static struct ast_cli_entry cli_ael[] = {
00219 AST_CLI_DEFINE(handle_cli_ael_reload, "Reload AEL configuration"),
00220 AST_CLI_DEFINE(handle_cli_ael_set_debug, "Enable AEL debugging flags")
00221 };
00222
00223 static int unload_module(void)
00224 {
00225 ast_context_destroy(NULL, registrar);
00226 ast_cli_unregister_multiple(cli_ael, ARRAY_LEN(cli_ael));
00227 return 0;
00228 }
00229
00230 static int load_module(void)
00231 {
00232 ast_cli_register_multiple(cli_ael, ARRAY_LEN(cli_ael));
00233 return (pbx_load_module());
00234 }
00235
00236 static int reload(void)
00237 {
00238 return pbx_load_module();
00239 }
00240
00241 #ifdef STANDALONE
00242 #define AST_MODULE "ael"
00243 int ael_external_load_module(void);
00244 int ael_external_load_module(void)
00245 {
00246 pbx_load_module();
00247 return 1;
00248 }
00249 #endif
00250
00251 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "Asterisk Extension Language Compiler",
00252 .load = load_module,
00253 .unload = unload_module,
00254 .reload = reload,
00255 );
00256
00257 #ifdef AAL_ARGCHECK
00258 static char *ael_funclist[] =
00259 {
00260 "AGENT",
00261 "ARRAY",
00262 "BASE64_DECODE",
00263 "BASE64_ENCODE",
00264 "CALLERID",
00265 "CDR",
00266 "CHANNEL",
00267 "CHECKSIPDOMAIN",
00268 "CHECK_MD5",
00269 "CURL",
00270 "CUT",
00271 "DB",
00272 "DB_EXISTS",
00273 "DUNDILOOKUP",
00274 "ENUMLOOKUP",
00275 "ENV",
00276 "EVAL",
00277 "EXISTS",
00278 "FIELDQTY",
00279 "FILTER",
00280 "GROUP",
00281 "GROUP_COUNT",
00282 "GROUP_LIST",
00283 "GROUP_MATCH_COUNT",
00284 "IAXPEER",
00285 "IF",
00286 "IFTIME",
00287 "ISNULL",
00288 "KEYPADHASH",
00289 "LANGUAGE",
00290 "LEN",
00291 "MATH",
00292 "MD5",
00293 "MUSICCLASS",
00294 "QUEUEAGENTCOUNT",
00295 "QUEUE_MEMBER_COUNT",
00296 "QUEUE_MEMBER_LIST",
00297 "QUOTE",
00298 "RAND",
00299 "REGEX",
00300 "SET",
00301 "SHA1",
00302 "SIPCHANINFO",
00303 "SIPPEER",
00304 "SIP_HEADER",
00305 "SORT",
00306 "STAT",
00307 "STRFTIME",
00308 "STRPTIME",
00309 "TIMEOUT",
00310 "TXTCIDNAME",
00311 "URIDECODE",
00312 "URIENCODE",
00313 "VMCOUNT"
00314 };
00315
00316
00317 int ael_is_funcname(char *name)
00318 {
00319 int s,t;
00320 t = sizeof(ael_funclist)/sizeof(char*);
00321 s = 0;
00322 while ((s < t) && strcasecmp(name, ael_funclist[s]))
00323 s++;
00324 if ( s < t )
00325 return 1;
00326 else
00327 return 0;
00328 }
00329 #endif