76562c3812c3140f743687487136b28a644aed21
[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 void pbx_substitute_variables_helper(struct ast_channel *c,const char *cp1,char *cp2,int count);
168 void pbx_substitute_variables_helper(struct ast_channel *c,const char *cp1,char *cp2,int count)
169 {
170         if (cp1 && *cp1)
171                 strncpy(cp2,cp1,AST_MAX_EXTENSION); /* Right now, this routine is ONLY being called for 
172                                                                                            a possible var substitution on extension names,
173                                                                                            so....! */
174         else
175                 *cp2 = 0;
176 }
177
178 int ast_add_extension2(struct ast_context *con,
179                         int replace, const char *extension, int priority, const char *label, const char *callerid,
180                         const char *application, void *data, void (*datad)(void *),
181                         const char *registrar)
182 {
183         priors++;
184         con->extension_count++;
185         if (strcmp(extension,last_exten) != 0) {
186                 extens++;
187                 strcpy(last_exten, extension);
188         }
189         if (!label) {
190                 label = "(null)";
191         }
192         if (!callerid) {
193                 callerid = "(null)";
194         }
195         if (!application) {
196                 application = "(null)";
197         }
198
199         if(!no_comp)
200                 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",
201                            con->name, replace, extension, priority, label, callerid, application, (data?(char*)data:"(null)"), registrar);
202
203         if( dump_extensions && dumpfile ) {
204                 struct namelist *n;
205                 char *data2,*data3=0;
206                 int commacount = 0;
207
208                 if( FIRST_TIME ) {
209                         FIRST_TIME = 0;
210                         
211                         if( globalvars )
212                                 fprintf(dumpfile,"[globals]\n");
213                         
214                         for(n=globalvars;n;n=n->next) {
215                                 fprintf(dumpfile, "%s\n", n->name);
216                         }
217                 }
218                 
219                 /* print out each extension , possibly the context header also */
220                 if( con != last_context ) {
221                         fprintf(dumpfile,"\n\n[%s]\n", con->name);
222                         last_context = con;
223                         for(n=con->ignorepats;n;n=n->next) {
224                                 fprintf(dumpfile, "ignorepat => %s\n", n->name);
225                         }
226                         for(n=con->includes;n;n=n->next) {
227                                 fprintf(dumpfile, "include => %s\n", n->name);
228                         }
229                         for(n=con->switches;n;n=n->next) {
230                                 fprintf(dumpfile, "switch => %s/%s\n", n->name, n->name2);
231                         }
232                         for(n=con->eswitches;n;n=n->next) {
233                                 fprintf(dumpfile, "eswitch => %s/%s\n", n->name, n->name2);
234                         }
235                         
236                 }
237                 if( data ) {
238                         filter_newlines((char*)data);
239                         filter_leading_space_from_exprs((char*)data);
240
241                         /* compiling turns commas into vertical bars in the app data, and also removes the backslash from before escaped commas;
242                            we have to restore the escaping backslash in front of any commas; the vertical bars are OK to leave as-is */
243                         for (data2 = data; *data2; data2++) {
244                                 if (*data2 == ',')
245                                         commacount++;  /* we need to know how much bigger the string will grow-- one backslash for each comma  */
246                         }
247                         if (commacount) 
248                         {
249                                 char *d3,*d4;
250                                 
251                                 data2 = (char*)malloc(strlen(data)+commacount+1);
252                                 data3 = data;
253                                 d3 = data;
254                                 d4 = data2;
255                                 while (*d3) {
256                                         if (*d3 == ',') {
257                                                 *d4++ = '\\'; /* put a backslash in front of each comma */
258                                                 *d4++ = *d3++;
259                                         } else
260                                                 *d4++ = *d3++;  /* or just copy the char */
261                                 }
262                                 *d4++ = 0;  /* cap off the new string */
263                                 data = data2;
264                         } else
265                                 data2 = 0;
266                         
267                         if( strcmp(label,"(null)") != 0  )
268                                 fprintf(dumpfile,"exten => %s,%d(%s),%s(%s)\n", extension, priority, label, application, (char*)data);
269                         else
270                                 fprintf(dumpfile,"exten => %s,%d,%s(%s)\n", extension, priority, application, (char*)data);
271
272                         if (data2) {
273                                 free(data2);
274                                 data2 = 0;
275                                 data = data3; /* restore data to pre-messedup state */
276                         }
277
278                 } else {
279
280                         if( strcmp(label,"(null)") != 0  )
281                                 fprintf(dumpfile,"exten => %s,%d(%s),%s\n", extension, priority, label, application);
282                         else
283                                 fprintf(dumpfile,"exten => %s,%d,%s\n", extension, priority, application);
284                 }
285         }
286         
287         /* since add_extension2 is responsible for the malloc'd data stuff */
288         if( data )
289                 free(data);
290         return 0;
291 }
292
293 void pbx_builtin_setvar(void *chan, void *data)
294 {
295         struct namelist *x = create_name((char*)data);
296         if(!no_comp)
297                 printf("Executed pbx_builtin_setvar(chan, data=%s);\n", (char*)data);
298
299         if( dump_extensions ) {
300                 x = create_name((char*)data);
301                 ADD_LAST(globalvars,x);
302         }
303 }
304         
305
306 struct ast_context * ast_context_create(void **extcontexts, const char *name, const char *registrar)
307 {
308         struct ast_context *x = calloc(1, sizeof(*x));
309         if (!x)
310                 return NULL;
311         x->next = context_list;
312         context_list = x;
313         if (!no_comp)
314                 printf("Executed ast_context_create(conts, name=%s, registrar=%s);\n", name, registrar);
315         conts++;
316         strncpy(x->name, name, sizeof(x->name) - 1);
317         strncpy(x->registrar, registrar, sizeof(x->registrar) - 1);
318         return x;
319 }
320
321 void ast_context_add_ignorepat2(struct ast_context *con, const char *value, const char *registrar)
322 {
323         if(!no_comp)
324                 printf("Executed ast_context_add_ignorepat2(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->ignorepats,x);
329         }
330 }
331
332 void ast_context_add_include2(struct ast_context *con, const char *value, const char *registrar)
333 {
334         if(!no_comp)
335                 printf("Executed ast_context_add_include2(con, value=%s, registrar=%s);\n", value, registrar);
336         if( dump_extensions ) {
337                 struct namelist *x;
338                 x = create_name((char*)value);
339                 ADD_LAST(con->includes,x);
340         }
341 }
342
343 void ast_context_add_switch2(struct ast_context *con, const char *value, const char *data, int eval, const char *registrar)
344 {
345         if(!no_comp)
346                 printf("Executed ast_context_add_switch2(con, value=%s, data=%s, eval=%d, registrar=%s);\n", value, data, eval, registrar);
347         if( dump_extensions ) {
348                 struct namelist *x;
349                 x = create_name((char*)value);
350                 strncpy(x->name2,data,100);
351                 if( eval ) {
352
353                         ADD_LAST(con->switches,x);
354
355                 } else {
356
357                         ADD_LAST(con->eswitches,x);
358                 }
359         }
360 }
361
362 void ast_merge_contexts_and_delete(void)
363 {
364         if(!no_comp)
365                 printf("Executed ast_merge_contexts_and_delete();\n");
366 }
367
368 void ast_context_verify_includes(void)
369 {
370         if(!no_comp)
371                 printf("Executed ast_context_verify_includes();\n");
372 }
373
374 struct ast_context * ast_walk_contexts(void)
375 {
376         if(!no_comp)
377                 printf("Executed ast_walk_contexts();\n");
378         return 0;
379 }
380
381 void ast_cli_unregister_multiple(void)
382 {
383         if(!no_comp)
384                 printf("Executed ast_cli_unregister_multiple();\n");
385 }
386
387 void ast_context_destroy(void)
388 {
389         if( !no_comp)
390                 printf("Executed ast_context_destroy();\n");
391 }
392
393 void ast_log(int level, const char *file, int line, const char *function, const char *fmt, ...)
394 {
395         va_list vars;
396         va_start(vars,fmt);
397         if( !quiet || level > 2 ) {
398             printf("LOG: lev:%d file:%s  line:%d func: %s  ",
399                    level, file, line, function);
400             vprintf(fmt, vars);
401             fflush(stdout);
402             va_end(vars);
403         }
404 }
405
406 void ast_verbose(const char *fmt, ...)
407 {
408         va_list vars;
409         va_start(vars,fmt);
410
411         printf("VERBOSE: ");
412         vprintf(fmt, vars);
413         fflush(stdout);
414         va_end(vars);
415 }
416
417 char *ast_process_quotes_and_slashes(char *start, char find, char replace_with)
418 {
419         char *dataPut = start;
420         int inEscape = 0;
421         int inQuotes = 0;
422
423         for (; *start; start++) {
424                 if (inEscape) {
425                         *dataPut++ = *start;       /* Always goes verbatim */
426                         inEscape = 0;
427                 } else {
428                         if (*start == '\\') {
429                                 inEscape = 1;      /* Do not copy \ into the data */
430                         } else if (*start == '\'') {
431                                 inQuotes = 1-inQuotes;   /* Do not copy ' into the data */
432                         } else {
433                                 /* Replace , with |, unless in quotes */
434                                 *dataPut++ = inQuotes ? *start : ((*start==find) ? replace_with : *start);
435                         }
436                 }
437         }
438         if (start != dataPut)
439                 *dataPut = 0;
440         return dataPut;
441 }
442
443 void filter_leading_space_from_exprs(char *str)
444 {
445         /*  Mainly for aesthetics */
446         char *t, *v, *u = str;
447         
448         while ( u && *u ) {
449
450                 if( *u == '$' && *(u+1) == '[' ) {
451                         t = u+2;
452                         while( *t == '\n' || *t == '\r' || *t == '\t' || *t == ' ' ) {
453                                 v = t;
454                                 while ( *v ) {
455                                         *v = *(v+1);
456                                         v++;
457                                 }
458                         }
459                 }
460                 
461                 u++;
462         }
463 }
464
465 void filter_newlines(char *str)
466 {
467         /* remove all newlines, returns  */
468         char *t=str;
469         while( t && *t ) {
470                 if( *t == '\n' || *t == '\r' ) {
471                         *t = ' '; /* just replace newlines and returns with spaces; they act as
472                                                  token separators, and just blindly removing them could be
473                                                  harmful. */
474                 }
475                 t++;
476         }
477 }
478
479
480 extern struct module_symbols mod_data;
481 extern int ael_external_load_module(void);
482
483 int main(int argc, char **argv)
484 {
485         int i;
486         struct namelist *n;
487         struct ast_context *lp,*lp2;
488         
489         for(i=1;i<argc;i++) {
490                 if( argv[i][0] == '-' && argv[i][1] == 'n' )
491                         no_comp =1;
492                 if( argv[i][0] == '-' && argv[i][1] == 'q' ) {
493                         quiet = 1;
494                         no_comp =1;
495                 }
496                 if( argv[i][0] == '-' && argv[i][1] == 'd' )
497                         use_curr_dir =1;
498                 if( argv[i][0] == '-' && argv[i][1] == 'w' )
499                         dump_extensions =1;
500         }
501         
502         if( !quiet ) {
503                 printf("\n(If you find progress and other non-error messages irritating, you can use -q to suppress them)\n");
504                 if( !no_comp )
505                         printf("\n(You can use the -n option if you aren't interested in seeing all the instructions generated by the compiler)\n\n");
506                 if( !use_curr_dir )
507                         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");
508                 if( !dump_extensions )
509                         printf("\n(You can use the -w option to dump extensions.conf format to extensions.conf.aeldump)\n");
510         }
511
512         if( use_curr_dir ) {
513                 strcpy(ast_config_AST_CONFIG_DIR, ".");
514         }
515         else {
516                 strcpy(ast_config_AST_CONFIG_DIR, "/etc/asterisk");
517         }
518         strcpy(ast_config_AST_VAR_DIR, "/var/lib/asterisk");
519         
520         if( dump_extensions ) {
521                 dumpfile = fopen("extensions.conf.aeldump","w");
522                 if( !dumpfile ) {
523                         printf("\n\nSorry, cannot open extensions.conf.aeldump for writing! Correct the situation and try again!\n\n");
524                         exit(10);
525                 }
526                 
527         }
528
529         FIRST_TIME = 1;
530         
531         ael_external_load_module();
532         
533         ast_log(4, "ael2_parse", __LINE__, "main", "%d contexts, %d extensions, %d priorities\n", conts, extens, priors);
534
535         if( dump_extensions && dumpfile ) {
536         
537                 for( lp = context_list; lp; lp = lp->next ) { /* print out any contexts that didn't have any
538                                                                                                                  extensions in them */
539                         if( lp->extension_count == 0 ) {
540                                 
541                                 fprintf(dumpfile,"\n\n[%s]\n", lp->name);
542                                 
543                                 for(n=lp->ignorepats;n;n=n->next) {
544                                         fprintf(dumpfile, "ignorepat => %s\n", n->name);
545                                 }
546                                 for(n=lp->includes;n;n=n->next) {
547                                         fprintf(dumpfile, "include => %s\n", n->name);
548                                 }
549                                 for(n=lp->switches;n;n=n->next) {
550                                         fprintf(dumpfile, "switch => %s/%s\n", n->name, n->name2);
551                                 }
552                                 for(n=lp->eswitches;n;n=n->next) {
553                                         fprintf(dumpfile, "eswitch => %s/%s\n", n->name, n->name2);
554                                 }
555                         }
556                 }
557         }
558         
559         if( dump_extensions && dumpfile )
560                 fclose(dumpfile);
561         
562         for( lp = context_list; lp; lp = lp2 ) { /* free the ast_context structs */
563                 lp2 = lp->next;
564                 lp->next = 0;
565
566                 destroy_namelist(lp->includes);
567                 destroy_namelist(lp->ignorepats);
568                 destroy_namelist(lp->switches);
569                 destroy_namelist(lp->eswitches);
570
571                 free(lp);
572         }
573         
574     return 0;
575 }