1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310
|
/*
* Asterisk -- An open source telephony toolkit.
*
* Copyright (C) 1999 - 2005, Digium, Inc.
*
* Mark Spencer <markster@digium.com>
*
* See http://www.asterisk.org for more information about
* the Asterisk project. Please do not directly contact
* any of the maintainers of this project for assistance;
* the project provides a web site, mailing lists and IRC
* channels for your use.
*
* This program is free software, distributed under the terms of
* the GNU General Public License Version 2. See the LICENSE file
* at the top of the source tree.
*/
/*! \file
*
* \brief Compile symbolic Asterisk Extension Logic into Asterisk extensions
*
*/
#include <sys/types.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <errno.h>
#include "asterisk.h"
ASTERISK_FILE_VERSION(__FILE__, "$Revision: 45134 $")
#include "asterisk/pbx.h"
#include "asterisk/config.h"
#include "asterisk/module.h"
#include "asterisk/logger.h"
#include "asterisk/cli.h"
#include "asterisk/callerid.h"
struct stringlink {
struct stringlink *next;
char data[0];
};
#define FILLIN_BREAK 1
#define FILLIN_CONTINUE 2
struct fillin {
struct fillin *next;
char exten[AST_MAX_EXTENSION];
int priority;
int type;
};
#ifdef __AST_DEBUG_MALLOC
static void FREE(void *ptr)
{
free(ptr);
}
#else
#define FREE free
#endif
#define DEBUG_READ (1 << 0)
#define DEBUG_TOKENS (1 << 1)
#define DEBUG_MACROS (1 << 2)
#define DEBUG_CONTEXTS (1 << 3)
static int aeldebug = 0;
static char *dtext = "Asterisk Extension Language Compiler";
static char *config = "extensions.ael";
static char *registrar = "pbx_ael";
static char *__grab_token(char *src, const char *filename, int lineno, int link)
{
char *c;
char *b;
char *a;
int level = 0;
char *ret;
#if 0
if (aeldebug || DEBUG_TOKENS)
ast_verbose("Searching for token in '%s'!\n", src);
#endif
c = src;
while(*c) {
if ((*c == '\\')) {
c++;
if (!*c)
c--;
} else {
if ((*c == '{') || (*c == '(')) {
level++;
} else if ((*c == '}') || (*c == ')')) {
if (level)
level--;
else
ast_log(LOG_WARNING, "Syntax error at line %d of '%s', too many closing braces!\n", lineno, filename);
} else if ((*c == ';') && !level) {
/* Got a token! */
*c = '\0';
b = c;
b--;
c++;
while((b > src) && (*b < 33)) {
*b = '\0';
b--;
}
a = ast_skip_blanks(src);
if (link) {
ret = malloc(strlen(a) + sizeof(struct stringlink) + 1);
if (ret)
strcpy(ret + sizeof(struct stringlink), a);
} else
ret = strdup(a);
/* Save remainder */
memmove(src, c, strlen(c) + 1);
return ret;
}
}
c++;
}
return NULL;
}
static char *grab_token(char *src, const char *filename, int lineno)
{
return __grab_token(src, filename, lineno, 0);
}
static struct stringlink *arg_parse(char *args, const char *filename, int lineno)
{
struct stringlink *cur, *prev=NULL, *root=NULL;
if (args) {
if (aeldebug & DEBUG_TOKENS)
ast_verbose("Parsing args '%s'!\n", args);
if (args[0] == '{') {
/* Strip mandatory '}' from end */
args[strlen(args) - 1] = '\0';
while ((cur = (struct stringlink *)__grab_token(args + 1, filename, lineno, 1))) {
cur->next = NULL;
if (prev)
prev->next = cur;
else
root = cur;
prev = cur;
}
} else if (*args) {
root = malloc(sizeof(struct stringlink) + strlen(args) + 1);
if (root) {
strcpy(root->data, args);
root->next = NULL;
}
}
}
return root;
}
static char *grab_else(char *args, const char *filename, int lineno)
{
char *ret = NULL;
int level=0;
char *c;
if (args) {
if (args[0] == '{') {
c = args;
while(*c) {
if (*c == '{')
level++;
else if (*c == '}') {
level--;
if (!level) {
c++;
while(*c && (*c < 33)) { *c = '\0'; c++; };
if (!strncasecmp(c, "else", 4) &&
((c[4] == '{') || (c[4] < 33))) {
/* Ladies and gentlemen, we have an else clause */
*c = '\0';
c += 4;
c = ast_skip_blanks(c);
ret = c;
if (aeldebug & DEBUG_TOKENS)
ast_verbose("Returning else clause '%s'\n", c);
}
break;
}
}
c++;
}
}
}
return ret;
}
static struct stringlink *param_parse(char *parms, const char *macro, const char *filename, int lineno)
{
char *s, *e;
struct stringlink *root = NULL, *prev=NULL, *cur;
if (!parms || !*parms)
return NULL;
if (*parms != '(') {
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);
return NULL;
}
s = parms + 1;
while(*s) {
s = ast_skip_blanks(s);
e = s;
while(*e && (*e != ')') && (*e != ',')) {
if (*e < 33)
*e = '\0';
e++;
}
if (*e) {
/* Strip token */
*e = '\0';
e++;
/* Skip over whitespace */
e = ast_skip_blanks(e);
/* Link */
cur = malloc(strlen(s) + sizeof(struct stringlink) + 1);
if (cur) {
cur->next = NULL;
strcpy(cur->data, s);
if (prev)
prev->next = cur;
else
root = cur;
prev = cur;
}
s = e;
}
}
return root;
}
static void arg_free(struct stringlink *cur)
{
struct stringlink *last;
while(cur) {
last = cur;
cur = cur->next;
free(last);
}
}
static void handle_globals(struct stringlink *vars)
{
while(vars) {
pbx_builtin_setvar(NULL, vars->data);
vars = vars->next;
}
}
static struct stringlink *split_token(char *token, const char *filename, int lineno)
{
char *args, *p;
struct stringlink *argv;
args = token;
while (*args && (*args > 32) && (*args != '{') && (*args != '(')) args++;
if (*args) {
p = args;
args = ast_skip_blanks(args);
if (*args != '(') {
*p = '\0';
} else {
while (*args && (*args != ')')) args++;
if (*args == ')') {
args++;
args = ast_skip_blanks(args);
}
}
if (!*args)
args = NULL;
} else args = NULL;
argv = arg_parse(args, filename, lineno);
if (args)
*args = '\0';
return argv;
}
static int matches_keyword(const char *data, const char *keyword)
{
char c;
if (!strncasecmp(data, keyword, strlen(keyword))) {
c = data[strlen(keyword)];
if ((c < 33) || (c == '(') || (c == '{'))
return 1;
}
return 0;
}
static struct stringlink *split_params(char *token, const char *filename, int lineno)
{
char *params;
struct stringlink *paramv;
params = token;
while(*params && (*params > 32) && (*params != '(')) params++;
if (*params) {
if (*params != '(') {
*params = '\0';
params++;
params = ast_skip_blanks(params);
}
if (!*params)
params = NULL;
} else params = NULL;
paramv = param_parse(params, token, filename, lineno);
if (params)
*params = '\0';
return paramv;
}
static const char *get_case(char *s, char **restout, int *pattern)
{
char *newcase=NULL;
char *rest=NULL;
if (!strncasecmp(s, "case", 4) && s[4] && ((s[4] < 33) || (s[4] == ':'))) {
newcase = s + 4;
newcase = ast_skip_blanks(newcase);
rest = newcase;
*pattern = 0;
} else if (!strncasecmp(s, "pattern", 7) && s[7] && ((s[7] < 33) || (s[7] == ':'))) {
newcase = s + 8;
newcase = ast_skip_blanks(newcase);
rest = newcase;
*pattern = 1;
} else if (!strncasecmp(s, "default", 7) && ((s[7] < 33) || (s[7] == ':'))) {
newcase = ".";
rest = s + 7;
rest = ast_skip_blanks(rest);
*pattern = 1;
}
if (rest) {
while (*rest && (*rest > 32) && (*rest != ':')) rest++;
if (*rest) {
*rest = 0;
rest++;
while (*rest && ((*rest == ':') || (*rest < 33))) rest++;
*restout = rest;
} else {
*restout = "";
}
} else
*restout = s;
if (aeldebug & DEBUG_TOKENS)
ast_verbose("GETCASE: newcase is '%s', rest = '%s'\n", newcase, *restout);
return newcase;
}
static void fillin_free(struct fillin *fillin)
{
struct fillin *cur, *next;
cur = fillin;
while(cur) {
next = cur->next;
free(cur);
cur = next;
}
}
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)
{
struct fillin *cur;
char *app;
char mdata[AST_MAX_EXTENSION + 20];
cur = fillin;
while(cur) {
if (cur->type == FILLIN_BREAK) {
if (breakexten && breakprio) {
app = "Goto";
snprintf(mdata, sizeof(mdata), "%s|%d", breakexten, breakprio);
} else {
app = "NoOp";
snprintf(mdata, sizeof(mdata), "Invalid break");
ast_log(LOG_NOTICE, "Ignoring inappropriate break around line %d of %s\n", lineno, filename);
}
if (ast_add_extension2(con, 0, cur->exten, cur->priority, NULL, NULL, app, strdup(mdata), FREE, registrar))
ast_log(LOG_WARNING, "Unable to add step at priority '%d' of break '%s'\n", cur->priority, cur->exten);
} else if (cur->type == FILLIN_CONTINUE) {
if (contexten && contprio) {
app = "Goto";
snprintf(mdata, sizeof(mdata), "%s|%d", contexten, contprio);
} else {
app = "NoOp";
snprintf(mdata, sizeof(mdata), "Invalid continue");
ast_log(LOG_NOTICE, "Ignoring inappropriate continue around line %d of %s\n", lineno, filename);
}
if (ast_add_extension2(con, 0, cur->exten, cur->priority, NULL, NULL, app, strdup(mdata), FREE, registrar))
ast_log(LOG_WARNING, "Unable to add step at priority '%d' of continue '%s'\n", cur->priority, cur->exten);
} else {
ast_log(LOG_WARNING, "Whoa, unknown fillin type '%d'\n", cur->type);
}
cur = cur->next;
}
}
static int match_assignment(char *variable, char **value)
{
char *c;
char *ws;
int inpar = 0;
c = variable;
while (*c) {
if(*c == ')' && (inpar > 0)) {
inpar--;
} else if(*c == '(' && (inpar >= 0)) {
inpar++;
} else if(*c == '=' && (inpar == 0)) {
break;
}
c++;
}
ws = c;
c = ast_skip_blanks(c);
if (*c == '=') {
*ws = '\0';
*c = '\0';
c++;
c = ast_skip_blanks(c);
*value = c;
return 1;
}
return 0;
}
static int matches_label(char *data, char **rest)
{
char last = 0;
char *start = data;
while (*data > 32) {
last = *data;
data++;
}
if (last != ':') {
data = ast_skip_blanks(data);
last = *data;
data++;
}
if (last == ':') {
*rest = data;
/* Go back and trim up the label */
while(*start && ((*start > 32) && (*start != ':'))) start++;
*start = '\0';
return 1;
}
return 0;
}
static char *argument_end(char *str)
{
int level=0;
while(*++str) {
switch(*str) {
case '(':
level++;
break;
case ')':
if(level)
level--;
else
return str;
break;
default:
break;
}
}
return NULL;
}
static void gen_match_to_pattern(const char *pattern, char *result)
{
/* the result will be a string that will be matched by pattern */
char *p=(char *)pattern, *t=result;
while (*p) {
if (*p == 'x' || *p == 'n' || *p == 'z' || *p == 'X' || *p == 'N' || *p == 'Z')
*t++ = '9';
else if (*p == '[') {
char *z = p+1;
while (*z != ']')
z++;
if (*(z+1)== ']')
z++;
*t++=*(p+1); /* use the first char in the set */
p = z;
} else {
*t++ = *p;
}
p++;
}
*t++ = 0; /* cap it off */
}
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);
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)
{
char *app;
char *args;
char *c;
char *margs=NULL;
char *oargs;
char *rest;
const char *curcase, *newcase;
struct stringlink *swargs, *cur;
int cpos;
int mlen;
int pattern = 0;
struct fillin *fillin;
data = ast_skip_blanks(data);
if (matches_label(data, &c)) {
*label = data;
data = c;
data = ast_skip_blanks(data);
}
if (ast_strlen_zero(data))
return 0;
if (matches_keyword(data, "switch")) {
fillin = NULL;
/* Switch */
args = data + strlen("switch");
while ((*args < 33) && (*args != '(')) args++;
if ((*args == '(') && (c = argument_end(args))) {
args++;
*c = '\0';
c++;
if (aeldebug & DEBUG_TOKENS)
ast_verbose("--SWITCH on : %s\n", args);
mlen = strlen(exten) + 128 + strlen(args) + strlen(name);
margs = alloca(mlen);
app = "Goto";
sprintf(margs, "sw-%d-%s|1", *pos, args);
ast_process_quotes_and_slashes(margs, ',', '|');
oargs = args;
args = margs;
if (ast_add_extension2(con, 0, exten, *pos, *label, NULL, app, strdup(args), FREE, registrar))
ast_log(LOG_WARNING, "Unable to add step at priority '%d' of %s '%s'\n", *pos, what, name);
else {
*label = NULL;
(*pos)++;
}
app = "NoOp";
sprintf(margs, "Finish switch-%d", *pos - 1);
if (ast_add_extension2(con, 0, exten, *pos, *label, NULL, app, strdup(args), FREE, registrar))
ast_log(LOG_WARNING, "Unable to add step at priority '%d' of %s '%s'\n", *pos, what, name);
else {
*label = NULL;
(*pos)++;
}
c = ast_skip_blanks(c);
if (aeldebug & DEBUG_TOKENS)
ast_verbose("ARG Parsing '%s'\n", c);
swargs = arg_parse(c, filename, lineno);
cur = swargs;
curcase = NULL;
while(cur) {
if ((newcase = get_case(cur->data, &rest, &pattern))) {
if (aeldebug & DEBUG_TOKENS)
ast_verbose("--NEWCASE: '%s'!\n", newcase);
if (curcase) {
char zbuf[256];
/* Handle fall through */
char tmp[strlen(newcase) + strlen(name) + 40];
gen_match_to_pattern(newcase,zbuf);
sprintf(tmp, "sw-%d-%s|%d", *pos - 2, zbuf, 1);
ast_add_extension2(con, 0, margs, cpos, NULL, NULL, "Goto", strdup(tmp), FREE, registrar);
}
curcase = newcase;
cpos = 1;
if (pattern)
snprintf(margs, mlen, "_sw-%d-%s", *pos - 2, curcase);
else
snprintf(margs, mlen, "sw-%d-%s", *pos - 2, curcase);
if (!strcasecmp(rest, "break")) {
char tmp[strlen(exten) + 10];
sprintf(tmp, "%s|%d", exten, *pos - 1);
ast_add_extension2(con, 0, exten, cpos, *label, NULL, "Goto", strdup(tmp), FREE, registrar);
curcase = NULL;
*label = NULL;
} else
build_step("switch", margs, filename, lineno, con, margs, &cpos, rest, &fillin, label);
} else if (curcase) {
if (aeldebug & DEBUG_TOKENS)
ast_verbose("Building statement from '%s'\n", rest);
if (!strcasecmp(rest, "break")) {
char tmp[strlen(exten) + 10];
sprintf(tmp, "%s|%d", exten, *pos - 1);
ast_add_extension2(con, 0, margs, cpos, *label, NULL, "Goto", strdup(tmp), FREE, registrar);
curcase = NULL;
*label = NULL;
} else
build_step("switch", margs, filename, lineno, con, margs, &cpos, rest, &fillin, label);
} else
ast_log(LOG_WARNING, "Unreachable code in switch at about line %d of %s\n", lineno, filename);
if (aeldebug & DEBUG_TOKENS)
ast_verbose("--SWARG: %s\n", cur->data);
cur = cur->next;
}
/* Can't do anything with these */
fillin_process(con, fillin, filename, lineno, NULL, 0, NULL, 0);
fillin_free(fillin);
arg_free(swargs);
} else
ast_log(LOG_WARNING, "Syntax error in switch declaration in %s around line %d!\n", filename, lineno);
} else if (matches_keyword(data, "if")) {
/* If... */
args = data + strlen("if");
while ((*args < 33) && (*args != '(')) args++;
if ((*args == '(') && (c = argument_end(args))) {
int ifblock;
int ifstart;
int elsestart;
int ifend;
int ifskip;
char *elses;
char *iflabel;
args++;
*c = '\0';
c++;
c = ast_skip_blanks(c);
if (aeldebug & DEBUG_TOKENS)
ast_verbose("--IF on : '%s' : '%s'\n", args, c);
mlen = strlen(exten) + 128 + strlen(args) + strlen(name);
margs = alloca(mlen);
/* Remember where the ifblock starts, and skip over */
ifblock = (*pos)++;
iflabel = *label;
*label = NULL;
/* Remember where the start of the ifblock is */
ifstart = *pos;
snprintf(margs, mlen, "if-%s-%d", name, ifblock);
/* Now process the block of the if */
if (aeldebug & DEBUG_TOKENS)
ast_verbose("Searching for elses in '%s'\n", c);
elses = grab_else(c, filename, lineno);
build_step("if", margs, filename, lineno, con, exten, pos, c, fillout, label);
if (elses) {
/* Reserve a goto to exit the if */
ifskip = *pos;
(*pos)++;
elsestart = *pos;
build_step("else", margs, filename, lineno, con, exten, pos, elses, fillout, label);
} else {
elsestart = *pos;
ifskip = 0;
}
ifend = *pos;
(*pos)++;
app = "NoOp";
snprintf(margs, mlen, "Finish if-%s-%d", name, ifblock);
if (ast_add_extension2(con, 0, exten, ifend, *label, NULL, app, strdup(margs), FREE, registrar))
ast_log(LOG_WARNING, "Unable to add step at priority '%d' of %s '%s'\n", *pos, what, name);
*label = NULL;
app = "GotoIf";
snprintf(margs, mlen, "$[ %s ]?%d:%d", args, ifstart, elsestart);
if (ast_add_extension2(con, 0, exten, ifblock, iflabel, NULL, app, strdup(margs), FREE, registrar))
ast_log(LOG_WARNING, "Unable to add step at priority '%d' of %s '%s'\n", *pos, what, name);
if (ifskip) {
/* Skip as appropriate around else clause */
snprintf(margs, mlen, "%d", ifend);
if (ast_add_extension2(con, 0, exten, ifskip, NULL, NULL, "Goto", strdup(margs), FREE, registrar))
ast_log(LOG_WARNING, "Unable to add step at priority '%d' of %s '%s'\n", *pos, what, name);
}
} else
ast_log(LOG_WARNING, "Syntax error in if declaration in %s around line %d!\n", filename, lineno);
} else if (matches_keyword(data, "while")) {
/* While... */
fillin = NULL;
args = data + strlen("while");
while ((*args < 33) && (*args != '(')) args++;
if ((*args == '(') && (c = argument_end(args))) {
int whileblock;
int whilestart;
int whileend;
char *whilelabel;
args++;
*c = '\0';
c++;
c = ast_skip_blanks(c);
if (aeldebug & DEBUG_TOKENS)
ast_verbose("--WHILE on : '%s' : '%s'\n", args, c);
mlen = strlen(exten) + 128 + strlen(args) + strlen(name);
margs = alloca(mlen);
/* Remember where to put the conditional, and keep its position */
whilestart = (*pos);
whilelabel = *label;
*label = NULL;
(*pos)++;
/* Remember where the whileblock starts */
whileblock = (*pos);
snprintf(margs, mlen, "while-%s-%d", name, whilestart);
build_step("while", margs, filename, lineno, con, exten, pos, c, &fillin, label);
/* Close the loop */
app = "Goto";
snprintf(margs, mlen, "%d", whilestart);
if (ast_add_extension2(con, 0, exten, (*pos)++, *label, NULL, app, strdup(margs), FREE, registrar))
ast_log(LOG_WARNING, "Unable to add step at priority '%d' of %s '%s'\n", *pos, what, name);
*label = NULL;
whileend = (*pos);
/* Place trailer */
app = "NoOp";
snprintf(margs, mlen, "Finish while-%s-%d", name, whilestart);
if (ast_add_extension2(con, 0, exten, (*pos)++, *label, NULL, app, strdup(margs), FREE, registrar))
ast_log(LOG_WARNING, "Unable to add step at priority '%d' of %s '%s'\n", *pos, what, name);
*label = NULL;
app = "GotoIf";
snprintf(margs, mlen, "$[ %s ]?%d:%d", args, whileblock, whileend);
if (ast_add_extension2(con, 0, exten, whilestart, whilelabel, NULL, app, strdup(margs), FREE, registrar))
ast_log(LOG_WARNING, "Unable to add step at priority '%d' of %s '%s'\n", *pos, what, name);
fillin_process(con, fillin, filename, lineno, exten, whileend, exten, whilestart);
fillin_free(fillin);
} else
ast_log(LOG_WARNING, "Syntax error in while declaration in %s around line %d!\n", filename, lineno);
} else if (matches_keyword(data, "jump")) {
char *p;
/* Jump... */
fillin = NULL;
args = data + strlen("jump");
args = ast_skip_blanks(args);
if (aeldebug & DEBUG_TOKENS)
ast_verbose("--JUMP to : '%s'\n", args);
p = strchr(args, ',');
if (p) {
*p = '\0';
p++;
} else
p = "1";
c = strchr(args, '@');
if (c) {
*c = '\0';
c++;
}
mlen = strlen(exten) + 128 + strlen(args) + strlen(name) + (c ? strlen(c) : 0);
margs = alloca(mlen);
if (c)
snprintf(margs, mlen, "%s|%s|%s", c,args, p);
else
snprintf(margs, mlen, "%s|%s", args, p);
app = "Goto";
if (ast_add_extension2(con, 0, exten, (*pos)++, *label, NULL, app, strdup(margs), FREE, registrar))
ast_log(LOG_WARNING, "Unable to add step at priority '%d' of %s '%s'\n", *pos, what, name);
*label = NULL;
} else if (matches_keyword(data, "goto")) {
/* Jump... */
fillin = NULL;
args = data + strlen("goto");
args = ast_skip_blanks(args);
if (aeldebug & DEBUG_TOKENS)
ast_verbose("--GOTO to : '%s'\n", args);
app = "Goto";
if (args[0] == '(' && args[strlen(args) - 1] == ')') {
args[0] = '\0';
args++;
args[strlen(args) - 1] = '\0';
}
if (ast_add_extension2(con, 0, exten, (*pos)++, *label, NULL, app, strdup(args), FREE, registrar))
ast_log(LOG_WARNING, "Unable to add step at priority '%d' of %s '%s'\n", *pos, what, name);
*label = NULL;
} else if (matches_keyword(data, "for")) {
/* While... */
fillin = NULL;
args = data + strlen("for");
while ((*args < 33) && (*args != '(')) args++;
if ((*args == '(') && (c = argument_end(args))) {
int forblock;
int forprep;
int forstart;
int forend;
struct stringlink *fields;
char *tmp;
char *forlabel = NULL;
args++;
*c = '\0';
c++;
c = ast_skip_blanks(c);
/* Parse arguments first */
tmp = alloca(strlen(args) + 10);
if (tmp) {
snprintf(tmp, strlen(args) + 10, "{%s;}", args);
fields = arg_parse(tmp, filename, lineno);
} else
fields = NULL;
if (fields && fields->next && fields->next->next) {
if (aeldebug & DEBUG_TOKENS)
ast_verbose("--FOR ('%s' ; '%s' ; '%s') : '%s'\n", fields->data, fields->next->data, fields->next->next->data, c);
mlen = strlen(exten) + 128 + strlen(args) + strlen(name);
margs = alloca(mlen);
forprep = *pos;
snprintf(margs, mlen, "for-%s-%d", name, forprep);
fillin = NULL;
build_step("while", margs, filename, lineno, con, exten, pos, fields->data, &fillin, label);
/* Remember where to put the conditional, and keep its position */
forstart = (*pos);
forlabel = *label;
(*pos)++;
*label = NULL;
/* Remember where the whileblock starts */
forblock = (*pos);
build_step("for", margs, filename, lineno, con, exten, pos, c, &fillin, label);
build_step("for", margs, filename, lineno, con, exten, pos, fields->next->next->data, &fillin, label);
/* Close the loop */
app = "Goto";
snprintf(margs, mlen, "%d", forstart);
if (ast_add_extension2(con, 0, exten, (*pos)++, *label, NULL, app, strdup(margs), FREE, registrar))
ast_log(LOG_WARNING, "Unable to add step at priority '%d' of %s '%s'\n", *pos, what, name);
*label = NULL;
forend = (*pos);
/* Place trailer */
app = "NoOp";
snprintf(margs, mlen, "Finish for-%s-%d", name, forprep);
if (ast_add_extension2(con, 0, exten, (*pos)++, *label, NULL, app, strdup(margs), FREE, registrar))
ast_log(LOG_WARNING, "Unable to add step at priority '%d' of %s '%s'\n", *pos, what, name);
*label = NULL;
app = "GotoIf";
snprintf(margs, mlen, "$[ %s ]?%d:%d", fields->next->data, forblock, forend);
if (ast_add_extension2(con, 0, exten, forstart, forlabel, NULL, app, strdup(margs), FREE, registrar))
ast_log(LOG_WARNING, "Unable to add step at priority '%d' of %s '%s'\n", forstart, what, name);
fillin_process(con, fillin, filename, lineno, exten, forend, exten, forstart);
fillin_free(fillin);
} else
ast_log(LOG_NOTICE, "Improper for declaration in %s around line %d!\n", filename, lineno);
arg_free(fields);
} else
ast_log(LOG_WARNING, "Syntax error in for declaration in %s around line %d!\n", filename, lineno);
} else if (!strcasecmp(data, "break") || !strcasecmp(data, "continue")) {
struct fillin *fi;
fi = malloc(sizeof(struct fillin));
if (fi) {
memset(fi, 0, sizeof(struct fillin));
if (!strcasecmp(data, "break"))
fi->type = FILLIN_BREAK;
else
fi->type = FILLIN_CONTINUE;
ast_copy_string(fi->exten, exten, sizeof(fi->exten));
fi->priority = (*pos)++;
fi->next = *fillout;
*fillout = fi;
}
} else if (match_assignment(data, &rest)) {
if (aeldebug & DEBUG_TOKENS)
ast_verbose("ASSIGN '%s' = '%s'\n", data, rest);
mlen = strlen(rest) + strlen(data) + 20;
margs = alloca(mlen);
snprintf(margs, mlen, "%s=$[ %s ]", data, rest);
app = "Set";
if (ast_add_extension2(con, 0, exten, *pos, *label, NULL, app, strdup(margs), FREE, registrar))
ast_log(LOG_WARNING, "Unable to add assignment at priority '%d' of %s '%s'\n", *pos, what, name);
else {
*label = NULL;
(*pos)++;
}
} else {
app = data;
args = app;
while (*args && (*args > 32) && (*args != '(')) args++;
if (*args != '(') {
while(*args && (*args != '(')) { *args = '\0'; args++; };
}
if (*args == '(') {
*args = '\0';
args++;
/* Got arguments, trim trailing ')' */
c = args + strlen(args) - 1;
while((c >= args) && (*c < 33) && (*c != ')')) { *c = '\0'; c--; };
if ((c >= args) && (*c == ')')) *c = '\0';
} else
args = "";
ast_process_quotes_and_slashes(args, ',', '|');
if (app[0] == '&') {
app++;
margs = alloca(strlen(args) + strlen(app) + 10);
sprintf(margs, "%s|%s", app, args);
args = margs;
app = "Macro";
}
if (aeldebug & DEBUG_TOKENS)
ast_verbose("-- APP: '%s', ARGS: '%s'\n", app, args);
if (ast_add_extension2(con, 0, exten, *pos, *label, NULL, app, strdup(args), FREE, registrar))
ast_log(LOG_WARNING, "Unable to add step at priority '%d' of %s '%s'\n", *pos, what, name);
else {
(*pos)++;
*label = NULL;
}
}
return 0;
}
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)
{
struct stringlink *args, *cur;
int res=0;
struct fillin *fillin=NULL;
int dropfill = 0;
char *labelin = NULL;
if (!fillout) {
fillout = &fillin;
dropfill = 1;
}
if (!label) {
label = &labelin;
};
args = arg_parse(data, filename, lineno);
cur = args;
while(cur) {
res |= __build_step(what, name, filename, lineno, con, exten, pos, cur->data, fillout, label);
cur = cur->next;
}
arg_free(args);
if (dropfill) {
fillin_process(con, fillin, filename, lineno, NULL, 0, NULL, 0);
fillin_free(fillin);
}
return res;
}
static int parse_catch(char *data, char **catch, char **rest)
{
/* Skip the word 'catch' */
data += 5;
data = ast_skip_blanks(data);
/* Here's the extension */
*catch = data;
if (!*data)
return 0;
while (*data && (*data > 32)) data++;
if (!*data)
return 0;
/* Trim any trailing spaces */
*data = '\0';
data++;
data = ast_skip_blanks(data);
if (!*data)
return 0;
*rest = data;
return 1;
}
static void handle_macro(struct ast_context **local_contexts, struct stringlink *vars, const char *filename, int lineno)
{
struct stringlink *argv;
struct stringlink *paramv;
struct stringlink *cur;
struct ast_context *con;
struct fillin *fillin;
char *catch, *rest;
char name[256];
int pos;
int cpos;
if (aeldebug & DEBUG_MACROS)
ast_verbose("Root macro def is '%s'\n", vars->data);
argv = split_token(vars->data, filename, lineno);
paramv = split_params(vars->data, filename, lineno);
if (aeldebug & DEBUG_MACROS)
ast_verbose("Found macro '%s'\n", vars->data);
snprintf(name, sizeof(name), "macro-%s", vars->data);
con = ast_context_create(local_contexts, name, registrar);
if (con) {
pos = 1;
cur = paramv;
while(cur) {
if (aeldebug & DEBUG_MACROS)
ast_verbose(" PARAM => '%s'\n", cur->data);
snprintf(name, sizeof(name), "%s=${ARG%d}", cur->data, pos);
if (ast_add_extension2(con, 0, "s", pos, NULL, NULL, "Set", strdup(name), FREE, registrar))
ast_log(LOG_WARNING, "Unable to add step at priority '%d' of macro '%s'\n", pos, vars->data);
else
pos++;
cur = cur->next;
}
cur = argv;
while(cur) {
if (aeldebug & DEBUG_MACROS)
ast_verbose(" STEP => '%s'\n", cur->data);
if (matches_keyword(cur->data, "catch")) {
if (aeldebug & DEBUG_MACROS)
ast_verbose("--CATCH: '%s'\n", cur->data);
if (parse_catch(cur->data, &catch, &rest)) {
cpos = 1;
build_step("catch", catch, filename, lineno, con, catch, &cpos, rest, NULL, NULL);
} else
ast_log(LOG_NOTICE, "Parse error for catch at about line %d of %s\n", lineno, filename);
} else {
fillin = NULL;
build_step("macro", vars->data, filename, lineno, con, "s", &pos, cur->data, NULL, NULL);
}
cur = cur->next;
}
} else
ast_log(LOG_WARNING, "Unable to create context '%s'\n", name);
arg_free(paramv);
arg_free(argv);
if (vars->next)
ast_log(LOG_NOTICE, "Ignoring excess tokens in macro definition around line %d of %s!\n", lineno, filename);
}
static int matches_extension(char *exten, char **extout)
{
char *c;
*extout = NULL;
c = exten;
while(*c && (*c > 32)) c++;
if (*c) {
*c = '\0';
c++;
c = ast_skip_blanks(c);
if (*c) {
if (*c == '=') {
*c = '\0';
c++;
if (*c == '>')
c++;
c = ast_skip_blanks(c);
*extout = c;
return 1;
}
}
}
return 0;
}
static void parse_keyword(char *s, char **o)
{
char *c;
c = s;
while((*c) && (*c > 32)) c++;
if (*c) {
*c = '\0';
c++;
c = ast_skip_blanks(c);
*o = c;
} else
*o = NULL;
}
static void handle_context(struct ast_context **local_contexts, struct stringlink *vars, const char *filename, int lineno)
{
struct stringlink *argv;
struct stringlink *cur2;
struct stringlink *argv2;
struct stringlink *cur;
struct ast_context *con;
char *rest;
char *c;
char name[256];
int pos;
if (aeldebug & DEBUG_CONTEXTS)
ast_verbose("Root context def is '%s'\n", vars->data);
argv = split_token(vars->data, filename, lineno);
if (aeldebug & DEBUG_CONTEXTS)
ast_verbose("Found context '%s'\n", vars->data);
snprintf(name, sizeof(name), "%s", vars->data);
con = ast_context_create(local_contexts, name, registrar);
if (con) {
cur = argv;
while(cur) {
if (matches_keyword(cur->data, "includes")) {
if (aeldebug & DEBUG_CONTEXTS)
ast_verbose("--INCLUDES: '%s'\n", cur->data);
parse_keyword(cur->data, &rest);
if (rest) {
argv2 = arg_parse(rest, filename, lineno);
cur2 = argv2;
while(cur2) {
ast_context_add_include2(con, cur2->data, registrar);
cur2 = cur2->next;
}
arg_free(argv2);
}
} else if (matches_keyword(cur->data, "ignorepat")) {
if (aeldebug & DEBUG_CONTEXTS)
ast_verbose("--IGNOREPAT: '%s'\n", cur->data);
parse_keyword(cur->data, &rest);
if (rest) {
argv2 = arg_parse(rest, filename, lineno);
cur2 = argv2;
while(cur2) {
ast_context_add_ignorepat2(con, cur2->data, registrar);
cur2 = cur2->next;
}
arg_free(argv2);
}
} else if (matches_keyword(cur->data, "switches") || matches_keyword(cur->data, "eswitches")) {
if (aeldebug & DEBUG_CONTEXTS)
ast_verbose("--[E]SWITCH: '%s'\n", cur->data);
parse_keyword(cur->data, &rest);
if (rest) {
argv2 = arg_parse(rest, filename, lineno);
cur2 = argv2;
while(cur2) {
c = strchr(cur2->data, '/');
if (c) {
*c = '\0';
c++;
} else
c = "";
ast_context_add_switch2(con, cur2->data, c, (cur->data[0] == 'e'), registrar);
cur2 = cur2->next;
}
arg_free(argv2);
}
} else if (matches_extension(cur->data, &rest)) {
if (aeldebug & DEBUG_CONTEXTS)
ast_verbose("Extension: '%s' => '%s'\n", cur->data, rest);
pos = 1;
build_step("extension", cur->data, filename, lineno, con, cur->data, &pos, rest, NULL, NULL);
}
cur = cur->next;
}
} else
ast_log(LOG_WARNING, "Unable to create context '%s'\n", name);
arg_free(argv);
if (vars->next)
ast_log(LOG_NOTICE, "Ignoring excess tokens in macro definition around line %d of %s!\n", lineno, filename);
}
static int handle_root_token(struct ast_context **local_contexts, char *token, int level, const char *filename, int lineno)
{
struct stringlink *argv, *cur;
argv = split_token(token, filename, lineno);
if (aeldebug & DEBUG_TOKENS) {
ast_verbose("Found root token '%s' at level %d (%s:%d)!\n", token, level, filename, lineno);
cur = argv;
while(cur) {
ast_verbose(" ARG => '%s'\n", cur->data);
cur = cur->next;
}
}
if (!strcasecmp(token, "globals")) {
handle_globals(argv);
} else if (!strcasecmp(token, "macro")) {
handle_macro(local_contexts, argv, filename, lineno);
} else if (!strcasecmp(token, "context")) {
handle_context(local_contexts, argv, filename, lineno);
} else {
ast_log(LOG_NOTICE, "Unknown root token '%s'\n", token);
}
arg_free(argv);
return 0;
}
static int ast_ael_compile(struct ast_context **local_contexts, const char *filename)
{
char *rfilename;
char *buf, *tbuf;
int bufsiz;
FILE *f;
char *c;
char *token;
int lineno=0;
if (filename[0] == '/')
rfilename = (char *)filename;
else {
rfilename = alloca(strlen(filename) + strlen(ast_config_AST_CONFIG_DIR) + 2);
sprintf(rfilename, "%s/%s", ast_config_AST_CONFIG_DIR, filename);
}
f = fopen(rfilename, "r");
if (!f) {
ast_log(LOG_WARNING, "Unable to open '%s': %s\n", rfilename, strerror(errno));
return -1;
}
buf = malloc(4096);
if (!buf) {
ast_log(LOG_WARNING, "Out of memory!\n");
fclose(f);
return -1;
}
buf[0] = 0;
bufsiz = 4096;
while(!feof(f)) {
if (bufsiz - strlen(buf) < 2048) {
bufsiz += 4096;
tbuf = realloc(buf, bufsiz);
if (tbuf) {
buf = tbuf;
} else {
free(buf);
ast_log(LOG_WARNING, "Out of memory!\n");
fclose(f);
}
}
if (fgets(buf + strlen(buf), bufsiz - strlen(buf), f)) {
lineno++;
while(*buf && buf[strlen(buf) - 1] < 33)
buf[strlen(buf) - 1] = '\0';
c = strstr(buf, "//");
if (c)
*c = '\0';
if (*buf) {
if (aeldebug & DEBUG_READ)
ast_verbose("Newly composed line '%s'\n", buf);
while((token = grab_token(buf, filename, lineno))) {
handle_root_token(local_contexts, token, 0, filename, lineno);
free(token);
}
}
}
};
free(buf);
fclose(f);
return 0;
}
static int pbx_load_module(void)
{
struct ast_context *local_contexts=NULL, *con;
ast_ael_compile(&local_contexts, config);
ast_merge_contexts_and_delete(&local_contexts, registrar);
for (con = ast_walk_contexts(NULL); con; con = ast_walk_contexts(con))
ast_context_verify_includes(con);
return 0;
}
/* CLI interface */
static int ael_debug_read(int fd, int argc, char *argv[])
{
aeldebug |= DEBUG_READ;
return 0;
}
static int ael_debug_tokens(int fd, int argc, char *argv[])
{
aeldebug |= DEBUG_TOKENS;
return 0;
}
static int ael_debug_macros(int fd, int argc, char *argv[])
{
aeldebug |= DEBUG_MACROS;
return 0;
}
static int ael_debug_contexts(int fd, int argc, char *argv[])
{
aeldebug |= DEBUG_CONTEXTS;
return 0;
}
static int ael_no_debug(int fd, int argc, char *argv[])
{
aeldebug = 0;
return 0;
}
static int ael_reload(int fd, int argc, char *argv[])
{
ast_context_destroy(NULL, registrar);
return (pbx_load_module());
}
static struct ast_cli_entry ael_cli[] = {
{ { "ael", "reload", NULL }, ael_reload, "Reload AEL configuration"},
{ { "ael", "debug", "read", NULL }, ael_debug_read, "Enable AEL read debug"},
{ { "ael", "debug", "tokens", NULL }, ael_debug_tokens, "Enable AEL tokens debug"},
{ { "ael", "debug", "macros", NULL }, ael_debug_macros, "Enable AEL macros debug"},
{ { "ael", "debug", "contexts", NULL }, ael_debug_contexts, "Enable AEL contexts debug"},
{ { "ael", "no", "debug", NULL }, ael_no_debug, "Disable AEL debug messages"},
};
/*
* Standard module functions ...
*/
int unload_module(void)
{
ast_context_destroy(NULL, registrar);
ast_cli_unregister_multiple(ael_cli, sizeof(ael_cli)/ sizeof(ael_cli[0]));
return 0;
}
int load_module(void)
{
ast_cli_register_multiple(ael_cli, sizeof(ael_cli)/ sizeof(ael_cli[0]));
return (pbx_load_module());
}
int reload(void)
{
ast_context_destroy(NULL, registrar);
return pbx_load_module();
}
int usecount(void)
{
return 0;
}
char *description(void)
{
return dtext;
}
char *key(void)
{
return ASTERISK_GPL_KEY;
}
|