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