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