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