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