via 10206, I have added an option (e) to Dial to allow the h exten to get run on...
[asterisk/asterisk.git] / utils / ael_main.c
1 #include <sys/types.h>
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <stdarg.h>
5 #include <string.h>
6 #include <locale.h>
7 #include <ctype.h>
8 #include <errno.h>
9 #include <regex.h>
10 #include <limits.h>
11
12 #include "asterisk/compat.h"
13 #include "asterisk/ast_expr.h"
14 #include "asterisk/channel.h"
15 #include "asterisk/module.h"
16 #include "asterisk/app.h"
17 #include "asterisk/ael_structs.h"
18
19 struct namelist
20 {
21         char name[100];
22         char name2[100];
23         struct namelist *next;
24 };
25
26 struct ast_context 
27 {
28         int extension_count;
29         char name[100];
30         char registrar[100];
31         struct namelist *includes;
32         struct namelist *ignorepats;
33         struct namelist *switches;
34         struct namelist *eswitches;
35
36         struct namelist *includes_last;
37         struct namelist *ignorepats_last;
38         struct namelist *switches_last;
39         struct namelist *eswitches_last;
40
41         struct ast_context *next;
42 };
43
44 #define ADD_LAST(headptr,memptr) if(!headptr){ headptr=(memptr); (headptr##_last)=(memptr);} else {(headptr##_last)->next = (memptr); (headptr##_last) = (memptr);}
45
46 void destroy_namelist(struct namelist *x);
47 void destroy_namelist(struct namelist *x)
48 {
49         struct namelist *z,*z2;
50         for(z=x; z; z = z2)
51         {
52                 z2 = z->next;
53                 z->next = 0;
54                 free(z);
55         }
56 }
57
58 struct namelist *create_name(char *name);
59 struct namelist *create_name(char *name)
60 {
61         struct namelist *x = calloc(1, sizeof(*x));
62         if (!x)
63                 return NULL;
64         strncpy(x->name, name, sizeof(x->name) - 1);
65         return x;
66 }
67
68 struct ast_context *context_list;
69 struct ast_context *last_context;
70 struct namelist *globalvars;
71 struct namelist *globalvars_last;
72
73 int conts=0, extens=0, priors=0;
74 char last_exten[18000];
75 char ast_config_AST_CONFIG_DIR[PATH_MAX];
76 char ast_config_AST_VAR_DIR[PATH_MAX];
77
78 void ast_add_profile(void);
79 void ast_cli_register_multiple(void);
80 void ast_register_file_version(void);
81 void ast_unregister_file_version(void);
82 int ast_add_extension2(struct ast_context *con,
83                                            int replace, const char *extension, int priority, const char *label, const char *callerid,
84                                                 const char *application, void *data, void (*datad)(void *),
85                                            const char *registrar);
86 void pbx_builtin_setvar(void *chan, void *data);
87 struct ast_context * ast_context_create(void **extcontexts, const char *name, const char *registrar);
88 void ast_context_add_ignorepat2(struct ast_context *con, const char *value, const char *registrar);
89 void ast_context_add_include2(struct ast_context *con, const char *value, const char *registrar);
90 void ast_context_add_switch2(struct ast_context *con, const char *value, const char *data, int eval, const char *registrar);
91 void ast_merge_contexts_and_delete(void);
92 void ast_context_verify_includes(void);
93 struct ast_context * ast_walk_contexts(void);
94 void ast_cli_unregister_multiple(void);
95 void ast_context_destroy(void);
96 void ast_log(int level, const char *file, int line, const char *function, const char *fmt, ...);
97 char *ast_process_quotes_and_slashes(char *start, char find, char replace_with);
98 void ast_verbose(const char *fmt, ...);
99 struct ast_app *pbx_findapp(const char *app);
100 void filter_leading_space_from_exprs(char *str);
101 void filter_newlines(char *str);
102 static int quiet = 0;
103 static int no_comp = 0;
104 static int use_curr_dir = 0;
105 static int dump_extensions = 0;
106 static int FIRST_TIME = 0;
107 static FILE *dumpfile;
108
109 struct ast_app *pbx_findapp(const char *app)
110 {
111         return (struct ast_app*)1; /* so as not to trigger an error */
112 }
113
114 struct ast_custom_function *ast_custom_function_find(const char *name);
115
116
117 struct ast_custom_function *ast_custom_function_find(const char *name)
118 {
119         return 0; /* in "standalone" mode, functions are just not avail */
120 }
121
122
123 void ast_add_profile(void)
124 {
125         if (!no_comp)
126                 printf("Executed ast_add_profile();\n");
127 }
128
129 int ast_loader_register(int (*updater)(void))
130 {
131         return 1;
132 }
133
134 int ast_loader_unregister(int (*updater)(void))
135 {
136         return 1;
137 }
138 void ast_module_register(const struct ast_module_info *x)
139 {
140 }
141
142 void ast_module_unregister(const struct ast_module_info *x)
143 {
144 }
145
146
147 void ast_cli_register_multiple(void)
148 {
149         if(!no_comp)
150                 printf("Executed ast_cli_register_multiple();\n");
151 }
152
153 void ast_register_file_version(void)
154 {
155         /* if(!no_comp)
156                 printf("Executed ast_register_file_version();\n"); */
157         /* I'm erasing this, because I don't think anyone really ever needs to see it anyway */
158 }
159
160 void ast_unregister_file_version(void)
161 {
162         /* if(!no_comp)
163                 printf("Executed ast_unregister_file_version();\n"); */
164         /* I'm erasing this, because I don't think anyone really ever needs to see it anyway */
165
166 }
167 int ast_add_extension2(struct ast_context *con,
168                         int replace, const char *extension, int priority, const char *label, const char *callerid,
169                         const char *application, void *data, void (*datad)(void *),
170                         const char *registrar)
171 {
172         priors++;
173         con->extension_count++;
174         if (strcmp(extension,last_exten) != 0) {
175                 extens++;
176                 strcpy(last_exten, extension);
177         }
178         if (!label) {
179                 label = "(null)";
180         }
181         if (!callerid) {
182                 callerid = "(null)";
183         }
184         if (!application) {
185                 application = "(null)";
186         }
187
188         if(!no_comp)
189                 printf("Executed ast_add_extension2(context=%s, rep=%d, exten=%s, priority=%d, label=%s, callerid=%s, appl=%s, data=%s, FREE, registrar=%s);\n",
190                            con->name, replace, extension, priority, label, callerid, application, (data?(char*)data:"(null)"), registrar);
191
192         if( dump_extensions && dumpfile ) {
193                 struct namelist *n;
194                 char *data2,*data3=0;
195                 int commacount = 0;
196
197                 if( FIRST_TIME ) {
198                         FIRST_TIME = 0;
199                         
200                         if( globalvars )
201                                 fprintf(dumpfile,"[globals]\n");
202                         
203                         for(n=globalvars;n;n=n->next) {
204                                 fprintf(dumpfile, "%s\n", n->name);
205                         }
206                 }
207                 
208                 /* print out each extension , possibly the context header also */
209                 if( con != last_context ) {
210                         fprintf(dumpfile,"\n\n[%s]\n", con->name);
211                         last_context = con;
212                         for(n=con->ignorepats;n;n=n->next) {
213                                 fprintf(dumpfile, "ignorepat => %s\n", n->name);
214                         }
215                         for(n=con->includes;n;n=n->next) {
216                                 fprintf(dumpfile, "include => %s\n", n->name);
217                         }
218                         for(n=con->switches;n;n=n->next) {
219                                 fprintf(dumpfile, "switch => %s/%s\n", n->name, n->name2);
220                         }
221                         for(n=con->eswitches;n;n=n->next) {
222                                 fprintf(dumpfile, "eswitch => %s/%s\n", n->name, n->name2);
223                         }
224                         
225                 }
226                 if( data ) {
227                         filter_newlines((char*)data);
228                         filter_leading_space_from_exprs((char*)data);
229
230                         /* compiling turns commas into vertical bars in the app data, and also removes the backslash from before escaped commas;
231                            we have to restore the escaping backslash in front of any commas; the vertical bars are OK to leave as-is */
232                         for (data2 = data; *data2; data2++) {
233                                 if (*data2 == ',')
234                                         commacount++;  /* we need to know how much bigger the string will grow-- one backslash for each comma  */
235                         }
236                         if (commacount) 
237                         {
238                                 char *d3,*d4;
239                                 
240                                 data2 = (char*)malloc(strlen(data)+commacount+1);
241                                 data3 = data;
242                                 d3 = data;
243                                 d4 = data2;
244                                 while (*d3) {
245                                         if (*d3 == ',') {
246                                                 *d4++ = '\\'; /* put a backslash in front of each comma */
247                                                 *d4++ = *d3++;
248                                         } else
249                                                 *d4++ = *d3++;  /* or just copy the char */
250                                 }
251                                 *d4++ = 0;  /* cap off the new string */
252                                 data = data2;
253                         } else
254                                 data2 = 0;
255                         
256                         if( strcmp(label,"(null)") != 0  )
257                                 fprintf(dumpfile,"exten => %s,%d(%s),%s(%s)\n", extension, priority, label, application, (char*)data);
258                         else
259                                 fprintf(dumpfile,"exten => %s,%d,%s(%s)\n", extension, priority, application, (char*)data);
260
261                         if (data2) {
262                                 free(data2);
263                                 data2 = 0;
264                                 data = data3; /* restore data to pre-messedup state */
265                         }
266
267                 } else {
268
269                         if( strcmp(label,"(null)") != 0  )
270                                 fprintf(dumpfile,"exten => %s,%d(%s),%s\n", extension, priority, label, application);
271                         else
272                                 fprintf(dumpfile,"exten => %s,%d,%s\n", extension, priority, application);
273                 }
274         }
275         
276         /* since add_extension2 is responsible for the malloc'd data stuff */
277         if( data )
278                 free(data);
279         return 0;
280 }
281
282 void pbx_builtin_setvar(void *chan, void *data)
283 {
284         struct namelist *x = create_name((char*)data);
285         if(!no_comp)
286                 printf("Executed pbx_builtin_setvar(chan, data=%s);\n", (char*)data);
287
288         if( dump_extensions ) {
289                 x = create_name((char*)data);
290                 ADD_LAST(globalvars,x);
291         }
292 }
293         
294
295 struct ast_context * ast_context_create(void **extcontexts, const char *name, const char *registrar)
296 {
297         struct ast_context *x = calloc(1, sizeof(*x));
298         if (!x)
299                 return NULL;
300         x->next = context_list;
301         context_list = x;
302         if (!no_comp)
303                 printf("Executed ast_context_create(conts, name=%s, registrar=%s);\n", name, registrar);
304         conts++;
305         strncpy(x->name, name, sizeof(x->name) - 1);
306         strncpy(x->registrar, registrar, sizeof(x->registrar) - 1);
307         return x;
308 }
309
310 void ast_context_add_ignorepat2(struct ast_context *con, const char *value, const char *registrar)
311 {
312         if(!no_comp)
313                 printf("Executed ast_context_add_ignorepat2(con, value=%s, registrar=%s);\n", value, registrar);
314         if( dump_extensions ) {
315                 struct namelist *x;
316                 x = create_name((char*)value);
317                 ADD_LAST(con->ignorepats,x);
318         }
319 }
320
321 void ast_context_add_include2(struct ast_context *con, const char *value, const char *registrar)
322 {
323         if(!no_comp)
324                 printf("Executed ast_context_add_include2(con, value=%s, registrar=%s);\n", value, registrar);
325         if( dump_extensions ) {
326                 struct namelist *x;
327                 x = create_name((char*)value);
328                 ADD_LAST(con->includes,x);
329         }
330 }
331
332 void ast_context_add_switch2(struct ast_context *con, const char *value, const char *data, int eval, const char *registrar)
333 {
334         if(!no_comp)
335                 printf("Executed ast_context_add_switch2(con, value=%s, data=%s, eval=%d, registrar=%s);\n", value, data, eval, registrar);
336         if( dump_extensions ) {
337                 struct namelist *x;
338                 x = create_name((char*)value);
339                 strncpy(x->name2,data,100);
340                 if( eval ) {
341
342                         ADD_LAST(con->switches,x);
343
344                 } else {
345
346                         ADD_LAST(con->eswitches,x);
347                 }
348         }
349 }
350
351 void ast_merge_contexts_and_delete(void)
352 {
353         if(!no_comp)
354                 printf("Executed ast_merge_contexts_and_delete();\n");
355 }
356
357 void ast_context_verify_includes(void)
358 {
359         if(!no_comp)
360                 printf("Executed ast_context_verify_includes();\n");
361 }
362
363 struct ast_context * ast_walk_contexts(void)
364 {
365         if(!no_comp)
366                 printf("Executed ast_walk_contexts();\n");
367         return 0;
368 }
369
370 void ast_cli_unregister_multiple(void)
371 {
372         if(!no_comp)
373                 printf("Executed ast_cli_unregister_multiple();\n");
374 }
375
376 void ast_context_destroy(void)
377 {
378         if( !no_comp)
379                 printf("Executed ast_context_destroy();\n");
380 }
381
382 void ast_log(int level, const char *file, int line, const char *function, const char *fmt, ...)
383 {
384         va_list vars;
385         va_start(vars,fmt);
386         if( !quiet || level > 2 ) {
387             printf("LOG: lev:%d file:%s  line:%d func: %s  ",
388                    level, file, line, function);
389             vprintf(fmt, vars);
390             fflush(stdout);
391             va_end(vars);
392         }
393 }
394
395 void ast_verbose(const char *fmt, ...)
396 {
397         va_list vars;
398         va_start(vars,fmt);
399
400         printf("VERBOSE: ");
401         vprintf(fmt, vars);
402         fflush(stdout);
403         va_end(vars);
404 }
405
406 char *ast_process_quotes_and_slashes(char *start, char find, char replace_with)
407 {
408         char *dataPut = start;
409         int inEscape = 0;
410         int inQuotes = 0;
411
412         for (; *start; start++) {
413                 if (inEscape) {
414                         *dataPut++ = *start;       /* Always goes verbatim */
415                         inEscape = 0;
416                 } else {
417                         if (*start == '\\') {
418                                 inEscape = 1;      /* Do not copy \ into the data */
419                         } else if (*start == '\'') {
420                                 inQuotes = 1-inQuotes;   /* Do not copy ' into the data */
421                         } else {
422                                 /* Replace , with |, unless in quotes */
423                                 *dataPut++ = inQuotes ? *start : ((*start==find) ? replace_with : *start);
424                         }
425                 }
426         }
427         if (start != dataPut)
428                 *dataPut = 0;
429         return dataPut;
430 }
431
432 void filter_leading_space_from_exprs(char *str)
433 {
434         /*  Mainly for aesthetics */
435         char *t, *v, *u = str;
436         
437         while ( u && *u ) {
438
439                 if( *u == '$' && *(u+1) == '[' ) {
440                         t = u+2;
441                         while( *t == '\n' || *t == '\r' || *t == '\t' || *t == ' ' ) {
442                                 v = t;
443                                 while ( *v ) {
444                                         *v = *(v+1);
445                                         v++;
446                                 }
447                         }
448                 }
449                 
450                 u++;
451         }
452 }
453
454 void filter_newlines(char *str)
455 {
456         /* remove all newlines, returns  */
457         char *t=str;
458         while( t && *t ) {
459                 if( *t == '\n' || *t == '\r' ) {
460                         *t = ' '; /* just replace newlines and returns with spaces; they act as
461                                                  token separators, and just blindly removing them could be
462                                                  harmful. */
463                 }
464                 t++;
465         }
466 }
467
468
469 extern struct module_symbols mod_data;
470 extern int ael_external_load_module(void);
471
472 int main(int argc, char **argv)
473 {
474         int i;
475         struct namelist *n;
476         struct ast_context *lp,*lp2;
477         
478         for(i=1;i<argc;i++) {
479                 if( argv[i][0] == '-' && argv[i][1] == 'n' )
480                         no_comp =1;
481                 if( argv[i][0] == '-' && argv[i][1] == 'q' ) {
482                         quiet = 1;
483                         no_comp =1;
484                 }
485                 if( argv[i][0] == '-' && argv[i][1] == 'd' )
486                         use_curr_dir =1;
487                 if( argv[i][0] == '-' && argv[i][1] == 'w' )
488                         dump_extensions =1;
489         }
490         
491         if( !quiet ) {
492                 printf("\n(If you find progress and other non-error messages irritating, you can use -q to suppress them)\n");
493                 if( !no_comp )
494                         printf("\n(You can use the -n option if you aren't interested in seeing all the instructions generated by the compiler)\n\n");
495                 if( !use_curr_dir )
496                         printf("\n(You can use the -d option if you want to use the current working directory as the CONFIG_DIR. I will look in this dir for extensions.ael* and its included files)\n\n");
497                 if( !dump_extensions )
498                         printf("\n(You can use the -w option to dump extensions.conf format to extensions.conf.aeldump)\n");
499         }
500
501         if( use_curr_dir ) {
502                 strcpy(ast_config_AST_CONFIG_DIR, ".");
503         }
504         else {
505                 strcpy(ast_config_AST_CONFIG_DIR, "/etc/asterisk");
506         }
507         strcpy(ast_config_AST_VAR_DIR, "/var/lib/asterisk");
508         
509         if( dump_extensions ) {
510                 dumpfile = fopen("extensions.conf.aeldump","w");
511                 if( !dumpfile ) {
512                         printf("\n\nSorry, cannot open extensions.conf.aeldump for writing! Correct the situation and try again!\n\n");
513                         exit(10);
514                 }
515                 
516         }
517
518         FIRST_TIME = 1;
519         
520         ael_external_load_module();
521         
522         ast_log(4, "ael2_parse", __LINE__, "main", "%d contexts, %d extensions, %d priorities\n", conts, extens, priors);
523
524         if( dump_extensions && dumpfile ) {
525         
526                 for( lp = context_list; lp; lp = lp->next ) { /* print out any contexts that didn't have any
527                                                                                                                  extensions in them */
528                         if( lp->extension_count == 0 ) {
529                                 
530                                 fprintf(dumpfile,"\n\n[%s]\n", lp->name);
531                                 
532                                 for(n=lp->ignorepats;n;n=n->next) {
533                                         fprintf(dumpfile, "ignorepat => %s\n", n->name);
534                                 }
535                                 for(n=lp->includes;n;n=n->next) {
536                                         fprintf(dumpfile, "include => %s\n", n->name);
537                                 }
538                                 for(n=lp->switches;n;n=n->next) {
539                                         fprintf(dumpfile, "switch => %s/%s\n", n->name, n->name2);
540                                 }
541                                 for(n=lp->eswitches;n;n=n->next) {
542                                         fprintf(dumpfile, "eswitch => %s/%s\n", n->name, n->name2);
543                                 }
544                         }
545                 }
546         }
547         
548         if( dump_extensions && dumpfile )
549                 fclose(dumpfile);
550         
551         for( lp = context_list; lp; lp = lp2 ) { /* free the ast_context structs */
552                 lp2 = lp->next;
553                 lp->next = 0;
554
555                 destroy_namelist(lp->includes);
556                 destroy_namelist(lp->ignorepats);
557                 destroy_namelist(lp->switches);
558                 destroy_namelist(lp->eswitches);
559
560                 free(lp);
561         }
562         
563     return 0;
564 }