pbx_ael.c 35 KB

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