2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 1999 - 2005, Digium, Inc.
6 * Mark Spencer <markster@digium.com>
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.
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.
21 * \brief Compile symbolic Asterisk Extension Logic into Asterisk extensions
25 #include <sys/types.h>
34 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
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/callerid.h"
44 struct stringlink *next;
48 #define FILLIN_BREAK 1
49 #define FILLIN_CONTINUE 2
53 char exten[AST_MAX_EXTENSION];
58 #ifdef __AST_DEBUG_MALLOC
59 static void FREE(void *ptr)
67 #define DEBUG_READ (1 << 0)
68 #define DEBUG_TOKENS (1 << 1)
69 #define DEBUG_MACROS (1 << 2)
70 #define DEBUG_CONTEXTS (1 << 3)
72 static int aeldebug = 0;
74 static char *dtext = "Asterisk Extension Language Compiler";
75 static char *config = "extensions.ael";
76 static char *registrar = "pbx_ael";
78 static char *__grab_token(char *src, const char *filename, int lineno, int link)
86 if (aeldebug || DEBUG_TOKENS)
87 ast_verbose("Searching for token in '%s'!\n", src);
96 if ((*c == '{') || (*c == '(')) {
98 } else if ((*c == '}') || (*c == ')')) {
102 ast_log(LOG_WARNING, "Syntax error at line %d of '%s', too many closing braces!\n", lineno, filename);
103 } else if ((*c == ';') && !level) {
109 while((b > src) && (*b < 33)) {
113 a = ast_skip_blanks(src);
115 ret = malloc(strlen(a) + sizeof(struct stringlink) + 1);
117 strcpy(ret + sizeof(struct stringlink), a);
121 memmove(src, c, strlen(c) + 1);
130 static char *grab_token(char *src, const char *filename, int lineno)
132 return __grab_token(src, filename, lineno, 0);
135 static struct stringlink *arg_parse(char *args, const char *filename, int lineno)
137 struct stringlink *cur, *prev=NULL, *root=NULL;
139 if (aeldebug & DEBUG_TOKENS)
140 ast_verbose("Parsing args '%s'!\n", args);
141 if (args[0] == '{') {
142 /* Strip mandatory '}' from end */
143 args[strlen(args) - 1] = '\0';
144 while ((cur = (struct stringlink *)__grab_token(args + 1, filename, lineno, 1))) {
153 root = malloc(sizeof(struct stringlink) + strlen(args) + 1);
155 strcpy(root->data, args);
163 static char *grab_else(char *args, const char *filename, int lineno)
169 if (args[0] == '{') {
174 else if (*c == '}') {
178 while(*c && (*c < 33)) { *c = '\0'; c++; };
179 if (!strncasecmp(c, "else", 4) &&
180 ((c[4] == '{') || (c[4] < 33))) {
181 /* Ladies and gentlemen, we have an else clause */
184 c = ast_skip_blanks(c);
186 if (aeldebug & DEBUG_TOKENS)
187 ast_verbose("Returning else clause '%s'\n", c);
199 static struct stringlink *param_parse(char *parms, const char *macro, const char *filename, int lineno)
202 struct stringlink *root = NULL, *prev=NULL, *cur;
203 if (!parms || !*parms)
206 ast_log(LOG_NOTICE, "Syntax error in parameter list for macro '%s' at about line %d of %s: Expecting '(' but got '%c'\n", macro, lineno, filename, *parms);
211 s = ast_skip_blanks(s);
213 while(*e && (*e != ')') && (*e != ',')) {
222 /* Skip over whitespace */
223 e = ast_skip_blanks(e);
225 cur = malloc(strlen(s) + sizeof(struct stringlink) + 1);
228 strcpy(cur->data, s);
241 static void arg_free(struct stringlink *cur)
243 struct stringlink *last;
251 static void handle_globals(struct stringlink *vars)
254 pbx_builtin_setvar(NULL, vars->data);
259 static struct stringlink *split_token(char *token, const char *filename, int lineno)
262 struct stringlink *argv;
264 while (*args && (*args > 32) && (*args != '{') && (*args != '(')) args++;
267 args = ast_skip_blanks(args);
271 while (*args && (*args != ')')) args++;
274 args = ast_skip_blanks(args);
280 argv = arg_parse(args, filename, lineno);
286 static int matches_keyword(const char *data, const char *keyword)
289 if (!strncasecmp(data, keyword, strlen(keyword))) {
290 c = data[strlen(keyword)];
291 if ((c < 33) || (c == '(') || (c == '{'))
297 static struct stringlink *split_params(char *token, const char *filename, int lineno)
300 struct stringlink *paramv;
302 while(*params && (*params > 32) && (*params != '(')) params++;
304 if (*params != '(') {
307 params = ast_skip_blanks(params);
311 } else params = NULL;
312 paramv = param_parse(params, token, filename, lineno);
318 static const char *get_case(char *s, char **restout, int *pattern)
322 if (!strncasecmp(s, "case", 4) && s[4] && ((s[4] < 33) || (s[4] == ':'))) {
324 newcase = ast_skip_blanks(newcase);
327 } else if (!strncasecmp(s, "pattern", 7) && s[7] && ((s[7] < 33) || (s[7] == ':'))) {
329 newcase = ast_skip_blanks(newcase);
332 } else if (!strncasecmp(s, "default", 7) && ((s[7] < 33) || (s[7] == ':'))) {
335 rest = ast_skip_blanks(rest);
340 while (*rest && (*rest > 32) && (*rest != ':')) rest++;
344 while (*rest && ((*rest == ':') || (*rest < 33))) rest++;
351 if (aeldebug & DEBUG_TOKENS)
352 ast_verbose("GETCASE: newcase is '%s', rest = '%s'\n", newcase, *restout);
356 static void fillin_free(struct fillin *fillin)
358 struct fillin *cur, *next;
367 static void fillin_process(struct ast_context *con, struct fillin *fillin, const char *filename, int lineno, const char *breakexten, int breakprio, const char *contexten, int contprio)
371 char mdata[AST_MAX_EXTENSION + 20];
374 if (cur->type == FILLIN_BREAK) {
375 if (breakexten && breakprio) {
377 snprintf(mdata, sizeof(mdata), "%s|%d", breakexten, breakprio);
380 snprintf(mdata, sizeof(mdata), "Invalid break");
381 ast_log(LOG_NOTICE, "Ignoring inappropriate break around line %d of %s\n", lineno, filename);
383 if (ast_add_extension2(con, 0, cur->exten, cur->priority, NULL, NULL, app, strdup(mdata), FREE, registrar))
384 ast_log(LOG_WARNING, "Unable to add step at priority '%d' of break '%s'\n", cur->priority, cur->exten);
385 } else if (cur->type == FILLIN_CONTINUE) {
386 if (contexten && contprio) {
388 snprintf(mdata, sizeof(mdata), "%s|%d", contexten, contprio);
391 snprintf(mdata, sizeof(mdata), "Invalid continue");
392 ast_log(LOG_NOTICE, "Ignoring inappropriate continue around line %d of %s\n", lineno, filename);
394 if (ast_add_extension2(con, 0, cur->exten, cur->priority, NULL, NULL, app, strdup(mdata), FREE, registrar))
395 ast_log(LOG_WARNING, "Unable to add step at priority '%d' of continue '%s'\n", cur->priority, cur->exten);
397 ast_log(LOG_WARNING, "Whoa, unknown fillin type '%d'\n", cur->type);
403 static int match_assignment(char *variable, char **value)
411 if(*c == ')' && (inpar > 0)) {
413 } else if(*c == '(' && (inpar >= 0)) {
415 } else if(*c == '=' && (inpar == 0)) {
421 c = ast_skip_blanks(c);
426 c = ast_skip_blanks(c);
433 static int matches_label(char *data, char **rest)
442 data = ast_skip_blanks(data);
448 /* Go back and trim up the label */
449 while(*start && ((*start > 32) && (*start != ':'))) start++;
456 static char *argument_end(char *str)
477 static int build_step(const char *what, const char *name, const char *filename, int lineno, struct ast_context *con, char *exten, int *pos, char *data, struct fillin **fillout, char **label);
478 static int __build_step(const char *what, const char *name, const char *filename, int lineno, struct ast_context *con, char *exten, int *pos, char *data, struct fillin **fillout, char **label)
486 const char *curcase, *newcase;
487 struct stringlink *swargs, *cur;
491 struct fillin *fillin;
493 data = ast_skip_blanks(data);
494 if (matches_label(data, &c)) {
497 data = ast_skip_blanks(data);
499 if (ast_strlen_zero(data))
501 if (matches_keyword(data, "switch")) {
504 args = data + strlen("switch");
505 while ((*args < 33) && (*args != '(')) args++;
506 if ((*args == '(') && (c = argument_end(args))) {
510 if (aeldebug & DEBUG_TOKENS)
511 ast_verbose("--SWITCH on : %s\n", args);
512 mlen = strlen(exten) + 128 + strlen(args) + strlen(name);
513 margs = alloca(mlen);
515 sprintf(margs, "sw-%d-%s|1", *pos, args);
516 ast_process_quotes_and_slashes(margs, ',', '|');
519 if (ast_add_extension2(con, 0, exten, *pos, *label, NULL, app, strdup(args), FREE, registrar))
520 ast_log(LOG_WARNING, "Unable to add step at priority '%d' of %s '%s'\n", *pos, what, name);
526 sprintf(margs, "Finish switch-%d", *pos - 1);
527 if (ast_add_extension2(con, 0, exten, *pos, *label, NULL, app, strdup(args), FREE, registrar))
528 ast_log(LOG_WARNING, "Unable to add step at priority '%d' of %s '%s'\n", *pos, what, name);
533 c = ast_skip_blanks(c);
534 if (aeldebug & DEBUG_TOKENS)
535 ast_verbose("ARG Parsing '%s'\n", c);
536 swargs = arg_parse(c, filename, lineno);
540 if ((newcase = get_case(cur->data, &rest, &pattern))) {
541 if (aeldebug & DEBUG_TOKENS)
542 ast_verbose("--NEWCASE: '%s'!\n", newcase);
544 /* Handle fall through */
545 char tmp[strlen(newcase) + strlen(name) + 40];
546 sprintf(tmp, "sw-%d-%s|%d", *pos - 2, newcase, 1);
547 ast_add_extension2(con, 0, margs, cpos, NULL, NULL, "Goto", strdup(tmp), FREE, registrar);
552 snprintf(margs, mlen, "_sw-%d-%s", *pos - 2, curcase);
554 snprintf(margs, mlen, "sw-%d-%s", *pos - 2, curcase);
555 if (!strcasecmp(rest, "break")) {
556 char tmp[strlen(exten) + 10];
557 sprintf(tmp, "%s|%d", exten, *pos - 1);
558 ast_add_extension2(con, 0, exten, cpos, *label, NULL, "Goto", strdup(tmp), FREE, registrar);
562 build_step("switch", margs, filename, lineno, con, margs, &cpos, rest, &fillin, label);
563 } else if (curcase) {
564 if (aeldebug & DEBUG_TOKENS)
565 ast_verbose("Building statement from '%s'\n", rest);
566 if (!strcasecmp(rest, "break")) {
567 char tmp[strlen(exten) + 10];
568 sprintf(tmp, "%s|%d", exten, *pos - 1);
569 ast_add_extension2(con, 0, margs, cpos, *label, NULL, "Goto", strdup(tmp), FREE, registrar);
573 build_step("switch", margs, filename, lineno, con, margs, &cpos, rest, &fillin, label);
575 ast_log(LOG_WARNING, "Unreachable code in switch at about line %d of %s\n", lineno, filename);
576 if (aeldebug & DEBUG_TOKENS)
577 ast_verbose("--SWARG: %s\n", cur->data);
580 /* Can't do anything with these */
581 fillin_process(con, fillin, filename, lineno, NULL, 0, NULL, 0);
585 ast_log(LOG_WARNING, "Syntax error in switch declaration in %s around line %d!\n", filename, lineno);
587 } else if (matches_keyword(data, "if")) {
589 args = data + strlen("if");
590 while ((*args < 33) && (*args != '(')) args++;
591 if ((*args == '(') && (c = argument_end(args))) {
602 c = ast_skip_blanks(c);
603 if (aeldebug & DEBUG_TOKENS)
604 ast_verbose("--IF on : '%s' : '%s'\n", args, c);
605 mlen = strlen(exten) + 128 + strlen(args) + strlen(name);
606 margs = alloca(mlen);
607 /* Remember where the ifblock starts, and skip over */
611 /* Remember where the start of the ifblock is */
613 snprintf(margs, mlen, "if-%s-%d", name, ifblock);
614 /* Now process the block of the if */
615 if (aeldebug & DEBUG_TOKENS)
616 ast_verbose("Searching for elses in '%s'\n", c);
617 elses = grab_else(c, filename, lineno);
618 build_step("if", margs, filename, lineno, con, exten, pos, c, fillout, label);
620 /* Reserve a goto to exit the if */
624 build_step("else", margs, filename, lineno, con, exten, pos, elses, fillout, label);
632 snprintf(margs, mlen, "Finish if-%s-%d", name, ifblock);
633 if (ast_add_extension2(con, 0, exten, ifend, *label, NULL, app, strdup(margs), FREE, registrar))
634 ast_log(LOG_WARNING, "Unable to add step at priority '%d' of %s '%s'\n", *pos, what, name);
637 snprintf(margs, mlen, "$[ %s ]?%d:%d", args, ifstart, elsestart);
638 if (ast_add_extension2(con, 0, exten, ifblock, iflabel, NULL, app, strdup(margs), FREE, registrar))
639 ast_log(LOG_WARNING, "Unable to add step at priority '%d' of %s '%s'\n", *pos, what, name);
641 /* Skip as appropriate around else clause */
642 snprintf(margs, mlen, "%d", ifend);
643 if (ast_add_extension2(con, 0, exten, ifskip, NULL, NULL, "Goto", strdup(margs), FREE, registrar))
644 ast_log(LOG_WARNING, "Unable to add step at priority '%d' of %s '%s'\n", *pos, what, name);
647 ast_log(LOG_WARNING, "Syntax error in if declaration in %s around line %d!\n", filename, lineno);
648 } else if (matches_keyword(data, "while")) {
651 args = data + strlen("while");
652 while ((*args < 33) && (*args != '(')) args++;
653 if ((*args == '(') && (c = argument_end(args))) {
661 c = ast_skip_blanks(c);
662 if (aeldebug & DEBUG_TOKENS)
663 ast_verbose("--WHILE on : '%s' : '%s'\n", args, c);
664 mlen = strlen(exten) + 128 + strlen(args) + strlen(name);
665 margs = alloca(mlen);
666 /* Remember where to put the conditional, and keep its position */
671 /* Remember where the whileblock starts */
673 snprintf(margs, mlen, "while-%s-%d", name, whilestart);
674 build_step("while", margs, filename, lineno, con, exten, pos, c, &fillin, label);
677 snprintf(margs, mlen, "%d", whilestart);
678 if (ast_add_extension2(con, 0, exten, (*pos)++, *label, NULL, app, strdup(margs), FREE, registrar))
679 ast_log(LOG_WARNING, "Unable to add step at priority '%d' of %s '%s'\n", *pos, what, name);
684 snprintf(margs, mlen, "Finish while-%s-%d", name, whilestart);
685 if (ast_add_extension2(con, 0, exten, (*pos)++, *label, NULL, app, strdup(margs), FREE, registrar))
686 ast_log(LOG_WARNING, "Unable to add step at priority '%d' of %s '%s'\n", *pos, what, name);
689 snprintf(margs, mlen, "$[ %s ]?%d:%d", args, whileblock, whileend);
690 if (ast_add_extension2(con, 0, exten, whilestart, whilelabel, NULL, app, strdup(margs), FREE, registrar))
691 ast_log(LOG_WARNING, "Unable to add step at priority '%d' of %s '%s'\n", *pos, what, name);
692 fillin_process(con, fillin, filename, lineno, exten, whileend, exten, whilestart);
695 ast_log(LOG_WARNING, "Syntax error in while declaration in %s around line %d!\n", filename, lineno);
696 } else if (matches_keyword(data, "jump")) {
700 args = data + strlen("jump");
701 args = ast_skip_blanks(args);
702 if (aeldebug & DEBUG_TOKENS)
703 ast_verbose("--JUMP to : '%s'\n", args);
704 p = strchr(args, ',');
710 c = strchr(args, '@');
715 mlen = strlen(exten) + 128 + strlen(args) + strlen(name) + (c ? strlen(c) : 0);
716 margs = alloca(mlen);
718 snprintf(margs, mlen, "%s|%s|%s", c,args, p);
720 snprintf(margs, mlen, "%s|%s", args, p);
722 if (ast_add_extension2(con, 0, exten, (*pos)++, *label, NULL, app, strdup(margs), FREE, registrar))
723 ast_log(LOG_WARNING, "Unable to add step at priority '%d' of %s '%s'\n", *pos, what, name);
725 } else if (matches_keyword(data, "goto")) {
728 args = data + strlen("goto");
729 args = ast_skip_blanks(args);
730 if (aeldebug & DEBUG_TOKENS)
731 ast_verbose("--GOTO to : '%s'\n", args);
733 if (ast_add_extension2(con, 0, exten, (*pos)++, *label, NULL, app, strdup(args), FREE, registrar))
734 ast_log(LOG_WARNING, "Unable to add step at priority '%d' of %s '%s'\n", *pos, what, name);
736 } else if (matches_keyword(data, "for")) {
739 args = data + strlen("for");
740 while ((*args < 33) && (*args != '(')) args++;
741 if ((*args == '(') && (c = argument_end(args))) {
746 struct stringlink *fields;
748 char *forlabel = NULL;
752 c = ast_skip_blanks(c);
753 /* Parse arguments first */
754 tmp = alloca(strlen(args) + 10);
756 snprintf(tmp, strlen(args) + 10, "{%s;}", args);
757 fields = arg_parse(tmp, filename, lineno);
760 if (fields && fields->next && fields->next->next) {
761 if (aeldebug & DEBUG_TOKENS)
762 ast_verbose("--FOR ('%s' ; '%s' ; '%s') : '%s'\n", fields->data, fields->next->data, fields->next->next->data, c);
763 mlen = strlen(exten) + 128 + strlen(args) + strlen(name);
764 margs = alloca(mlen);
766 snprintf(margs, mlen, "for-%s-%d", name, forprep);
768 build_step("while", margs, filename, lineno, con, exten, pos, fields->data, &fillin, label);
769 /* Remember where to put the conditional, and keep its position */
774 /* Remember where the whileblock starts */
776 build_step("for", margs, filename, lineno, con, exten, pos, fields->next->next->data, &fillin, label);
777 build_step("for", margs, filename, lineno, con, exten, pos, c, &fillin, label);
780 snprintf(margs, mlen, "%d", forstart);
781 if (ast_add_extension2(con, 0, exten, (*pos)++, *label, NULL, app, strdup(margs), FREE, registrar))
782 ast_log(LOG_WARNING, "Unable to add step at priority '%d' of %s '%s'\n", *pos, what, name);
787 snprintf(margs, mlen, "Finish for-%s-%d", name, forprep);
788 if (ast_add_extension2(con, 0, exten, (*pos)++, *label, NULL, app, strdup(margs), FREE, registrar))
789 ast_log(LOG_WARNING, "Unable to add step at priority '%d' of %s '%s'\n", *pos, what, name);
792 snprintf(margs, mlen, "$[ %s ]?%d:%d", fields->next->data, forblock, forend);
793 if (ast_add_extension2(con, 0, exten, forstart, forlabel, NULL, app, strdup(margs), FREE, registrar))
794 ast_log(LOG_WARNING, "Unable to add step at priority '%d' of %s '%s'\n", forstart, what, name);
795 fillin_process(con, fillin, filename, lineno, exten, forend, exten, forstart);
798 ast_log(LOG_NOTICE, "Improper for declaration in %s around line %d!\n", filename, lineno);
801 ast_log(LOG_WARNING, "Syntax error in for declaration in %s around line %d!\n", filename, lineno);
803 } else if (!strcasecmp(data, "break") || !strcasecmp(data, "continue")) {
805 fi = malloc(sizeof(struct fillin));
807 memset(fi, 0, sizeof(struct fillin));
808 if (!strcasecmp(data, "break"))
809 fi->type = FILLIN_BREAK;
811 fi->type = FILLIN_CONTINUE;
812 ast_copy_string(fi->exten, exten, sizeof(fi->exten));
813 fi->priority = (*pos)++;
817 } else if (match_assignment(data, &rest)) {
818 if (aeldebug & DEBUG_TOKENS)
819 ast_verbose("ASSIGN '%s' = '%s'\n", data, rest);
820 mlen = strlen(rest) + strlen(data) + 20;
821 margs = alloca(mlen);
822 snprintf(margs, mlen, "%s=$[ %s ]", data, rest);
824 if (ast_add_extension2(con, 0, exten, *pos, *label, NULL, app, strdup(margs), FREE, registrar))
825 ast_log(LOG_WARNING, "Unable to add assignment at priority '%d' of %s '%s'\n", *pos, what, name);
833 while (*args && (*args > 32) && (*args != '(')) args++;
835 while(*args && (*args != '(')) { *args = '\0'; args++; };
840 /* Got arguments, trim trailing ')' */
841 c = args + strlen(args) - 1;
842 while((c >= args) && (*c < 33) && (*c != ')')) { *c = '\0'; c--; };
843 if ((c >= args) && (*c == ')')) *c = '\0';
846 ast_process_quotes_and_slashes(args, ',', '|');
849 margs = alloca(strlen(args) + strlen(app) + 10);
850 sprintf(margs, "%s|%s", app, args);
854 if (aeldebug & DEBUG_TOKENS)
855 ast_verbose("-- APP: '%s', ARGS: '%s'\n", app, args);
856 if (ast_add_extension2(con, 0, exten, *pos, *label, NULL, app, strdup(args), FREE, registrar))
857 ast_log(LOG_WARNING, "Unable to add step at priority '%d' of %s '%s'\n", *pos, what, name);
866 static int build_step(const char *what, const char *name, const char *filename, int lineno, struct ast_context *con, char *exten, int *pos, char *data, struct fillin **fillout, char **label)
868 struct stringlink *args, *cur;
870 struct fillin *fillin=NULL;
872 char *labelin = NULL;
880 args = arg_parse(data, filename, lineno);
883 res |= __build_step(what, name, filename, lineno, con, exten, pos, cur->data, fillout, label);
888 fillin_process(con, fillin, filename, lineno, NULL, 0, NULL, 0);
894 static int parse_catch(char *data, char **catch, char **rest)
896 /* Skip the word 'catch' */
898 data = ast_skip_blanks(data);
899 /* Here's the extension */
903 while (*data && (*data > 32)) data++;
906 /* Trim any trailing spaces */
909 data = ast_skip_blanks(data);
916 static void handle_macro(struct ast_context **local_contexts, struct stringlink *vars, const char *filename, int lineno)
918 struct stringlink *argv;
919 struct stringlink *paramv;
920 struct stringlink *cur;
921 struct ast_context *con;
922 struct fillin *fillin;
928 if (aeldebug & DEBUG_MACROS)
929 ast_verbose("Root macro def is '%s'\n", vars->data);
930 argv = split_token(vars->data, filename, lineno);
931 paramv = split_params(vars->data, filename, lineno);
932 if (aeldebug & DEBUG_MACROS)
933 ast_verbose("Found macro '%s'\n", vars->data);
934 snprintf(name, sizeof(name), "macro-%s", vars->data);
935 con = ast_context_create(local_contexts, name, registrar);
940 if (aeldebug & DEBUG_MACROS)
941 ast_verbose(" PARAM => '%s'\n", cur->data);
942 snprintf(name, sizeof(name), "%s=${ARG%d}", cur->data, pos);
943 if (ast_add_extension2(con, 0, "s", pos, NULL, NULL, "Set", strdup(name), FREE, registrar))
944 ast_log(LOG_WARNING, "Unable to add step at priority '%d' of macro '%s'\n", pos, vars->data);
951 if (aeldebug & DEBUG_MACROS)
952 ast_verbose(" STEP => '%s'\n", cur->data);
953 if (matches_keyword(cur->data, "catch")) {
954 if (aeldebug & DEBUG_MACROS)
955 ast_verbose("--CATCH: '%s'\n", cur->data);
956 if (parse_catch(cur->data, &catch, &rest)) {
958 build_step("catch", catch, filename, lineno, con, catch, &cpos, rest, NULL, NULL);
960 ast_log(LOG_NOTICE, "Parse error for catch at about line %d of %s\n", lineno, filename);
963 build_step("macro", vars->data, filename, lineno, con, "s", &pos, cur->data, NULL, NULL);
968 ast_log(LOG_WARNING, "Unable to create context '%s'\n", name);
972 ast_log(LOG_NOTICE, "Ignoring excess tokens in macro definition around line %d of %s!\n", lineno, filename);
975 static int matches_extension(char *exten, char **extout)
980 while(*c && (*c > 32)) c++;
984 c = ast_skip_blanks(c);
991 c = ast_skip_blanks(c);
1000 static void parse_keyword(char *s, char **o)
1004 while((*c) && (*c > 32)) c++;
1008 c = ast_skip_blanks(c);
1014 static void handle_context(struct ast_context **local_contexts, struct stringlink *vars, const char *filename, int lineno)
1016 struct stringlink *argv;
1017 struct stringlink *cur2;
1018 struct stringlink *argv2;
1019 struct stringlink *cur;
1020 struct ast_context *con;
1026 if (aeldebug & DEBUG_CONTEXTS)
1027 ast_verbose("Root context def is '%s'\n", vars->data);
1028 argv = split_token(vars->data, filename, lineno);
1029 if (aeldebug & DEBUG_CONTEXTS)
1030 ast_verbose("Found context '%s'\n", vars->data);
1031 snprintf(name, sizeof(name), "%s", vars->data);
1032 con = ast_context_create(local_contexts, name, registrar);
1036 if (matches_keyword(cur->data, "includes")) {
1037 if (aeldebug & DEBUG_CONTEXTS)
1038 ast_verbose("--INCLUDES: '%s'\n", cur->data);
1039 parse_keyword(cur->data, &rest);
1041 argv2 = arg_parse(rest, filename, lineno);
1044 ast_context_add_include2(con, cur2->data, registrar);
1049 } else if (matches_keyword(cur->data, "ignorepat")) {
1050 if (aeldebug & DEBUG_CONTEXTS)
1051 ast_verbose("--IGNOREPAT: '%s'\n", cur->data);
1052 parse_keyword(cur->data, &rest);
1054 argv2 = arg_parse(rest, filename, lineno);
1057 ast_context_add_ignorepat2(con, cur2->data, registrar);
1062 } else if (matches_keyword(cur->data, "switches") || matches_keyword(cur->data, "eswitches")) {
1063 if (aeldebug & DEBUG_CONTEXTS)
1064 ast_verbose("--[E]SWITCH: '%s'\n", cur->data);
1065 parse_keyword(cur->data, &rest);
1067 argv2 = arg_parse(rest, filename, lineno);
1070 c = strchr(cur2->data, '/');
1076 ast_context_add_switch2(con, cur2->data, c, (cur->data[0] == 'e'), registrar);
1081 } else if (matches_extension(cur->data, &rest)) {
1082 if (aeldebug & DEBUG_CONTEXTS)
1083 ast_verbose("Extension: '%s' => '%s'\n", cur->data, rest);
1085 build_step("extension", cur->data, filename, lineno, con, cur->data, &pos, rest, NULL, NULL);
1090 ast_log(LOG_WARNING, "Unable to create context '%s'\n", name);
1093 ast_log(LOG_NOTICE, "Ignoring excess tokens in macro definition around line %d of %s!\n", lineno, filename);
1096 static int handle_root_token(struct ast_context **local_contexts, char *token, int level, const char *filename, int lineno)
1098 struct stringlink *argv, *cur;
1099 argv = split_token(token, filename, lineno);
1100 if (aeldebug & DEBUG_TOKENS) {
1101 ast_verbose("Found root token '%s' at level %d (%s:%d)!\n", token, level, filename, lineno);
1104 ast_verbose(" ARG => '%s'\n", cur->data);
1108 if (!strcasecmp(token, "globals")) {
1109 handle_globals(argv);
1110 } else if (!strcasecmp(token, "macro")) {
1111 handle_macro(local_contexts, argv, filename, lineno);
1112 } else if (!strcasecmp(token, "context")) {
1113 handle_context(local_contexts, argv, filename, lineno);
1115 ast_log(LOG_NOTICE, "Unknown root token '%s'\n", token);
1122 static int ast_ael_compile(struct ast_context **local_contexts, const char *filename)
1132 if (filename[0] == '/')
1133 rfilename = (char *)filename;
1135 rfilename = alloca(strlen(filename) + strlen(ast_config_AST_CONFIG_DIR) + 2);
1136 sprintf(rfilename, "%s/%s", ast_config_AST_CONFIG_DIR, filename);
1139 f = fopen(rfilename, "r");
1141 ast_log(LOG_WARNING, "Unable to open '%s': %s\n", rfilename, strerror(errno));
1146 ast_log(LOG_WARNING, "Out of memory!\n");
1153 if (bufsiz - strlen(buf) < 2048) {
1155 tbuf = realloc(buf, bufsiz);
1160 ast_log(LOG_WARNING, "Out of memory!\n");
1164 if (fgets(buf + strlen(buf), bufsiz - strlen(buf), f)) {
1166 while(*buf && buf[strlen(buf) - 1] < 33)
1167 buf[strlen(buf) - 1] = '\0';
1168 c = strstr(buf, "//");
1172 if (aeldebug & DEBUG_READ)
1173 ast_verbose("Newly composed line '%s'\n", buf);
1174 while((token = grab_token(buf, filename, lineno))) {
1175 handle_root_token(local_contexts, token, 0, filename, lineno);
1186 static int pbx_load_module(void)
1188 struct ast_context *local_contexts=NULL, *con;
1189 ast_ael_compile(&local_contexts, config);
1190 ast_merge_contexts_and_delete(&local_contexts, registrar);
1191 for (con = ast_walk_contexts(NULL); con; con = ast_walk_contexts(con))
1192 ast_context_verify_includes(con);
1198 static int ael_debug_read(int fd, int argc, char *argv[])
1200 aeldebug |= DEBUG_READ;
1204 static int ael_debug_tokens(int fd, int argc, char *argv[])
1206 aeldebug |= DEBUG_TOKENS;
1210 static int ael_debug_macros(int fd, int argc, char *argv[])
1212 aeldebug |= DEBUG_MACROS;
1216 static int ael_debug_contexts(int fd, int argc, char *argv[])
1218 aeldebug |= DEBUG_CONTEXTS;
1222 static int ael_no_debug(int fd, int argc, char *argv[])
1228 static int ael_reload(int fd, int argc, char *argv[])
1230 ast_context_destroy(NULL, registrar);
1231 return (pbx_load_module());
1234 static struct ast_cli_entry ael_cli[] = {
1235 { { "ael", "reload", NULL }, ael_reload, "Reload AEL configuration"},
1236 { { "ael", "debug", "read", NULL }, ael_debug_read, "Enable AEL read debug"},
1237 { { "ael", "debug", "tokens", NULL }, ael_debug_tokens, "Enable AEL tokens debug"},
1238 { { "ael", "debug", "macros", NULL }, ael_debug_macros, "Enable AEL macros debug"},
1239 { { "ael", "debug", "contexts", NULL }, ael_debug_contexts, "Enable AEL contexts debug"},
1240 { { "ael", "no", "debug", NULL }, ael_no_debug, "Disable AEL debug messages"},
1244 * Standard module functions ...
1246 int unload_module(void)
1248 ast_context_destroy(NULL, registrar);
1249 ast_cli_unregister_multiple(ael_cli, sizeof(ael_cli)/ sizeof(ael_cli[0]));
1254 int load_module(void)
1256 ast_cli_register_multiple(ael_cli, sizeof(ael_cli)/ sizeof(ael_cli[0]));
1257 return (pbx_load_module());
1262 ast_context_destroy(NULL, registrar);
1263 return pbx_load_module();
1271 char *description(void)
1278 return ASTERISK_GPL_KEY;