Start untangling header inclusion in a way that does not affect
[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/ast_expr.h"
11 #include "asterisk/module.h"
12 #include "asterisk/app.h"
13 #include "asterisk/ael_structs.h"
14 #include "asterisk/extconf.h"
15
16 /*** MODULEINFO
17         <depend>res_ael_share</depend>
18  ***/
19
20 struct namelist
21 {
22         char name[100];
23         char name2[100];
24         struct namelist *next;
25 };
26
27 struct ast_context 
28 {
29         int extension_count;
30         char name[100];
31         char registrar[100];
32         struct namelist *includes;
33         struct namelist *ignorepats;
34         struct namelist *switches;
35         struct namelist *eswitches;
36
37         struct namelist *includes_last;
38         struct namelist *ignorepats_last;
39         struct namelist *switches_last;
40         struct namelist *eswitches_last;
41
42         struct ast_context *next;
43 };
44
45 #define ADD_LAST(headptr,memptr) if(!headptr){ headptr=(memptr); (headptr##_last)=(memptr);} else {(headptr##_last)->next = (memptr); (headptr##_last) = (memptr);}
46
47 void destroy_namelist(struct namelist *x);
48 void destroy_namelist(struct namelist *x)
49 {
50         struct namelist *z,*z2;
51         for(z=x; z; z = z2)
52         {
53                 z2 = z->next;
54                 z->next = 0;
55                 free(z);
56         }
57 }
58
59 struct namelist *create_name(const char *name);
60 struct namelist *create_name(const char *name)
61 {
62         struct namelist *x = calloc(1, sizeof(*x));
63         if (!x)
64                 return NULL;
65         strncpy(x->name, name, sizeof(x->name) - 1);
66         return x;
67 }
68
69 struct ast_context *context_list;
70 struct ast_context *last_context;
71 struct namelist *globalvars;
72 struct namelist *globalvars_last;
73
74 int conts=0, extens=0, priors=0;
75 char last_exten[18000];
76 char ast_config_AST_CONFIG_DIR[PATH_MAX];
77 char ast_config_AST_VAR_DIR[PATH_MAX];
78
79 void ast_add_profile(void);
80 void ast_cli_register_multiple(void);
81 void ast_register_file_version(void);
82 void ast_unregister_file_version(void);
83 int ast_add_extension2(struct ast_context *con,
84                                            int replace, const char *extension, int priority, const char *label, const char *callerid,
85                                                 const char *application, void *data, void (*datad)(void *),
86                                            const char *registrar);
87 void pbx_builtin_setvar(void *chan, void *data);
88 struct ast_context * ast_context_create(void **extcontexts, const char *name, const char *registrar);
89 struct ast_context * ast_context_find_or_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(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(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 struct ast_context * ast_context_find_or_create(void **extcontexts, const char *name, const char *registrar)
359 {
360         struct ast_context *x = calloc(1, sizeof(*x));
361         if (!x)
362                 return NULL;
363         x->next = context_list;
364         context_list = x;
365         if (!no_comp)
366                 printf("Executed ast_context_find_or_create(conts, name=%s, registrar=%s);\n", name, registrar);
367         conts++;
368         strncpy(x->name, name, sizeof(x->name) - 1);
369         strncpy(x->registrar, registrar, sizeof(x->registrar) - 1);
370         return x;
371 }
372
373 void ast_context_add_ignorepat2(struct ast_context *con, const char *value, const char *registrar)
374 {
375         if(!no_comp)
376                 printf("Executed ast_context_add_ignorepat2(con, value=%s, registrar=%s);\n", value, registrar);
377         if( dump_extensions ) {
378                 struct namelist *x;
379                 x = create_name(value);
380                 ADD_LAST(con->ignorepats,x);
381         }
382 }
383
384 void ast_context_add_include2(struct ast_context *con, const char *value, const char *registrar)
385 {
386         if(!no_comp)
387                 printf("Executed ast_context_add_include2(con, value=%s, registrar=%s);\n", value, registrar);
388         if( dump_extensions ) {
389                 struct namelist *x;
390                 x = create_name((char*)value);
391                 ADD_LAST(con->includes,x);
392         }
393 }
394
395 void ast_context_add_switch2(struct ast_context *con, const char *value, const char *data, int eval, const char *registrar)
396 {
397         if(!no_comp)
398                 printf("Executed ast_context_add_switch2(con, value=%s, data=%s, eval=%d, registrar=%s);\n", value, data, eval, registrar);
399         if( dump_extensions ) {
400                 struct namelist *x;
401                 x = create_name((char*)value);
402                 strncpy(x->name2,data,100);
403                 if( eval ) {
404
405                         ADD_LAST(con->switches,x);
406
407                 } else {
408
409                         ADD_LAST(con->eswitches,x);
410                 }
411         }
412 }
413
414 void ast_merge_contexts_and_delete(void)
415 {
416         if(!no_comp)
417                 printf("Executed ast_merge_contexts_and_delete();\n");
418 }
419
420 void ast_context_verify_includes(void)
421 {
422         if(!no_comp)
423                 printf("Executed ast_context_verify_includes();\n");
424 }
425
426 struct ast_context * ast_walk_contexts(void)
427 {
428         if(!no_comp)
429                 printf("Executed ast_walk_contexts();\n");
430         return 0;
431 }
432
433 void ast_cli_unregister_multiple(void)
434 {
435         if(!no_comp)
436                 printf("Executed ast_cli_unregister_multiple();\n");
437 }
438
439 void ast_context_destroy(void)
440 {
441         if( !no_comp)
442                 printf("Executed ast_context_destroy();\n");
443 }
444
445 void filter_leading_space_from_exprs(char *str)
446 {
447         /*  Mainly for aesthetics */
448         char *t, *v, *u = str;
449         
450         while ( u && *u ) {
451
452                 if( *u == '$' && *(u+1) == '[' ) {
453                         t = u+2;
454                         while( *t == '\n' || *t == '\r' || *t == '\t' || *t == ' ' ) {
455                                 v = t;
456                                 while ( *v ) {
457                                         *v = *(v+1);
458                                         v++;
459                                 }
460                         }
461                 }
462                 
463                 u++;
464         }
465 }
466
467 void filter_newlines(char *str)
468 {
469         /* remove all newlines, returns  */
470         char *t=str;
471         while( t && *t ) {
472                 if( *t == '\n' || *t == '\r' ) {
473                         *t = ' '; /* just replace newlines and returns with spaces; they act as
474                                                  token separators, and just blindly removing them could be
475                                                  harmful. */
476                 }
477                 t++;
478         }
479 }
480
481
482 extern struct module_symbols mod_data;
483 int ael_external_load_module(void);
484
485
486 int main(int argc, char **argv)
487 {
488         int i;
489         struct namelist *n;
490         struct ast_context *lp,*lp2;
491         
492         for(i=1;i<argc;i++) {
493                 if( argv[i][0] == '-' && argv[i][1] == 'n' )
494                         no_comp =1;
495                 if( argv[i][0] == '-' && argv[i][1] == 'q' ) {
496                         quiet = 1;
497                         no_comp =1;
498                 }
499                 if( argv[i][0] == '-' && argv[i][1] == 'd' )
500                         use_curr_dir =1;
501                 if( argv[i][0] == '-' && argv[i][1] == 'w' )
502                         dump_extensions =1;
503         }
504         
505         if( !quiet ) {
506                 printf("\n(If you find progress and other non-error messages irritating, you can use -q to suppress them)\n");
507                 if( !no_comp )
508                         printf("\n(You can use the -n option if you aren't interested in seeing all the instructions generated by the compiler)\n\n");
509                 if( !use_curr_dir )
510                         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");
511                 if( !dump_extensions )
512                         printf("\n(You can use the -w option to dump extensions.conf format to extensions.conf.aeldump)\n");
513         }
514
515         if( use_curr_dir ) {
516                 strcpy(ast_config_AST_CONFIG_DIR, ".");
517                 localized_use_local_dir();
518         }
519         else {
520                 strcpy(ast_config_AST_CONFIG_DIR, "/etc/asterisk");
521                 localized_use_conf_dir();
522         }
523         strcpy(ast_config_AST_VAR_DIR, "/var/lib/asterisk");
524         
525         if( dump_extensions ) {
526                 dumpfile = fopen("extensions.conf.aeldump","w");
527                 if( !dumpfile ) {
528                         printf("\n\nSorry, cannot open extensions.conf.aeldump for writing! Correct the situation and try again!\n\n");
529                         exit(10);
530                 }
531                 
532         }
533
534         FIRST_TIME = 1;
535         
536         ael_external_load_module();
537         
538         ast_log(4, "ael2_parse", __LINE__, "main", "%d contexts, %d extensions, %d priorities\n", conts, extens, priors);
539
540         if( dump_extensions && dumpfile ) {
541         
542                 for( lp = context_list; lp; lp = lp->next ) { /* print out any contexts that didn't have any
543                                                                                                                  extensions in them */
544                         if( lp->extension_count == 0 ) {
545                                 
546                                 fprintf(dumpfile,"\n\n[%s]\n", lp->name);
547                                 
548                                 for(n=lp->ignorepats;n;n=n->next) {
549                                         fprintf(dumpfile, "ignorepat => %s\n", n->name);
550                                 }
551                                 for(n=lp->includes;n;n=n->next) {
552                                         fprintf(dumpfile, "include => %s\n", n->name);
553                                 }
554                                 for(n=lp->switches;n;n=n->next) {
555                                         fprintf(dumpfile, "switch => %s/%s\n", n->name, n->name2);
556                                 }
557                                 for(n=lp->eswitches;n;n=n->next) {
558                                         fprintf(dumpfile, "eswitch => %s/%s\n", n->name, n->name2);
559                                 }
560                         }
561                 }
562         }
563         
564         if( dump_extensions && dumpfile )
565                 fclose(dumpfile);
566         
567         for( lp = context_list; lp; lp = lp2 ) { /* free the ast_context structs */
568                 lp2 = lp->next;
569                 lp->next = 0;
570
571                 destroy_namelist(lp->includes);
572                 destroy_namelist(lp->ignorepats);
573                 destroy_namelist(lp->switches);
574                 destroy_namelist(lp->eswitches);
575
576                 free(lp);
577         }
578         
579     return 0;
580 }