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