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