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