This commit adds the enhancemnt per bug 7606, adding
[asterisk/asterisk.git] / utils / ael_main.c
1 #include "asterisk/autoconfig.h"
2
3 #include <sys/types.h>
4 #include <stdio.h>
5 #include <stdlib.h>
6 #include <stdarg.h>
7 #include <string.h>
8 #include <locale.h>
9 #include <ctype.h>
10 #if !defined(SOLARIS) && !defined(__CYGWIN__)
11 #include <err.h>
12 #endif
13 #include <errno.h>
14 #include <regex.h>
15 #include <limits.h>
16
17 #include "asterisk/ast_expr.h"
18 #include "asterisk/module.h"
19 #include "asterisk/ael_structs.h"
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(char *name);
61 struct namelist *create_name(char *name)
62 {
63         struct namelist *x = (struct namelist *)calloc(sizeof(struct namelist),1);
64         strncpy(x->name,name,100);
65         return x;
66 }
67
68 struct ast_context *context_list;
69 struct ast_context *last_context;
70 struct namelist *globalvars;
71 struct namelist *globalvars_last;
72
73 int conts=0, extens=0, priors=0;
74 char last_exten[18000];
75 char ast_config_AST_CONFIG_DIR[PATH_MAX];
76 char ast_config_AST_VAR_DIR[PATH_MAX];
77
78 void ast_add_profile(void);
79 void ast_cli_register_multiple(void);
80 void ast_register_file_version(void);
81 void ast_unregister_file_version(void);
82 int ast_add_extension2(struct ast_context *con,
83                                            int replace, const char *extension, int priority, const char *label, const char *callerid,
84                                                 const char *application, void *data, void (*datad)(void *),
85                                            const char *registrar);
86 void pbx_builtin_setvar(void *chan, void *data);
87 struct ast_context * ast_context_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 no_comp = 0;
103 static int use_curr_dir = 0;
104 static int dump_extensions = 0;
105 static int FIRST_TIME = 0;
106 static FILE *dumpfile;
107
108 struct ast_app *pbx_findapp(const char *app)
109 {
110         return (struct ast_app*)1; /* so as not to trigger an error */
111 }
112
113 void ast_add_profile(void)
114 {
115         if (!no_comp)
116                 printf("Executed ast_add_profile();\n");
117 }
118
119 void ast_cli_register_multiple(void)
120 {
121         if(!no_comp)
122         printf("Executed ast_cli_register_multiple();\n");
123 }
124
125 void ast_register_file_version(void)
126 {
127         if(!no_comp)
128                 printf("Executed ast_register_file_version();\n");
129 }
130
131 void ast_unregister_file_version(void)
132 {
133         if(!no_comp)
134                 printf("Executed ast_unregister_file_version();\n");
135
136 }
137 int ast_add_extension2(struct ast_context *con,
138                         int replace, const char *extension, int priority, const char *label, const char *callerid,
139                         const char *application, void *data, void (*datad)(void *),
140                         const char *registrar)
141 {
142         priors++;
143         con->extension_count++;
144         if (strcmp(extension,last_exten) != 0) {
145                 extens++;
146                 strcpy(last_exten, extension);
147         }
148         if (!label) {
149                 label = "(null)";
150         }
151         if (!callerid) {
152                 callerid = "(null)";
153         }
154         if (!application) {
155                 application = "(null)";
156         }
157
158         if(!no_comp)
159                 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",
160                            con->name, replace, extension, priority, label, callerid, application, (data?(char*)data:"(null)"), registrar);
161
162         if( dump_extensions && dumpfile ) {
163                 struct namelist *n;
164
165                 if( FIRST_TIME ) {
166                         FIRST_TIME = 0;
167                         
168                         if( globalvars )
169                                 fprintf(dumpfile,"[globals]\n");
170                         
171                         for(n=globalvars;n;n=n->next) {
172                                 fprintf(dumpfile, "%s\n", n->name);
173                         }
174                 }
175                 
176                 /* print out each extension , possibly the context header also */
177                 if( con != last_context ) {
178                         fprintf(dumpfile,"\n\n[%s]\n", con->name);
179                         last_context = con;
180                         for(n=con->ignorepats;n;n=n->next) {
181                                 fprintf(dumpfile, "ignorepat => %s\n", n->name);
182                         }
183                         for(n=con->includes;n;n=n->next) {
184                                 fprintf(dumpfile, "include => %s\n", n->name);
185                         }
186                         for(n=con->switches;n;n=n->next) {
187                                 fprintf(dumpfile, "switch => %s/%s\n", n->name, n->name2);
188                         }
189                         for(n=con->eswitches;n;n=n->next) {
190                                 fprintf(dumpfile, "eswitch => %s/%s\n", n->name, n->name2);
191                         }
192                         
193                 }
194                 if( data ) {
195                         filter_newlines((char*)data);
196                         filter_leading_space_from_exprs((char*)data);
197
198                         if( strcmp(label,"(null)") != 0  )
199                                 fprintf(dumpfile,"exten => %s,%d(%s),%s(%s)\n", extension, priority, label, application, (char*)data);
200                         else
201                                 fprintf(dumpfile,"exten => %s,%d,%s(%s)\n", extension, priority, application, (char*)data);
202
203                 } else {
204
205                         if( strcmp(label,"(null)") != 0  )
206                                 fprintf(dumpfile,"exten => %s,%d(%s),%s\n", extension, priority, label, application);
207                         else
208                                 fprintf(dumpfile,"exten => %s,%d,%s\n", extension, priority, application);
209                 }
210         }
211         
212         /* since add_extension2 is responsible for the malloc'd data stuff */
213         if( data )
214                 free(data);
215         return 0;
216 }
217
218 void pbx_builtin_setvar(void *chan, void *data)
219 {
220         struct namelist *x = create_name((char*)data);
221         if(!no_comp)
222                 printf("Executed pbx_builtin_setvar(chan, data=%s);\n", (char*)data);
223
224         if( dump_extensions ) {
225                 x = create_name((char*)data);
226                 ADD_LAST(globalvars,x);
227         }
228 }
229         
230
231 struct ast_context * ast_context_create(void **extcontexts, const char *name, const char *registrar)
232 {
233         struct ast_context *x = (struct ast_context *)calloc(sizeof(struct ast_context),1);
234         x->next = context_list;
235         context_list = x;
236         if(!no_comp)
237                 printf("Executed ast_context_create(conts, name=%s, registrar=%s);\n", name, registrar);
238         conts++;
239         strncpy(x->name,name,100);
240         strncpy(x->registrar,registrar,100);
241         return x;
242 }
243
244 void ast_context_add_ignorepat2(struct ast_context *con, const char *value, const char *registrar)
245 {
246         if(!no_comp)
247                 printf("Executed ast_context_add_ignorepat2(con, value=%s, registrar=%s);\n", value, registrar);
248         if( dump_extensions ) {
249                 struct namelist *x;
250                 x = create_name((char*)value);
251                 ADD_LAST(con->ignorepats,x);
252         }
253 }
254
255 void ast_context_add_include2(struct ast_context *con, const char *value, const char *registrar)
256 {
257         if(!no_comp)
258                 printf("Executed ast_context_add_include2(con, value=%s, registrar=%s);\n", value, registrar);
259         if( dump_extensions ) {
260                 struct namelist *x;
261                 x = create_name((char*)value);
262                 ADD_LAST(con->includes,x);
263         }
264 }
265
266 void ast_context_add_switch2(struct ast_context *con, const char *value, const char *data, int eval, const char *registrar)
267 {
268         if(!no_comp)
269                 printf("Executed ast_context_add_switch2(con, value=%s, data=%s, eval=%d, registrar=%s);\n", value, data, eval, registrar);
270         if( dump_extensions ) {
271                 struct namelist *x;
272                 x = create_name((char*)value);
273                 strncpy(x->name2,data,100);
274                 if( eval ) {
275
276                         ADD_LAST(con->switches,x);
277
278                 } else {
279
280                         ADD_LAST(con->eswitches,x);
281                 }
282         }
283 }
284
285 void ast_merge_contexts_and_delete(void)
286 {
287         if(!no_comp)
288                 printf("Executed ast_merge_contexts_and_delete();\n");
289 }
290
291 void ast_context_verify_includes(void)
292 {
293         if(!no_comp)
294                 printf("Executed ast_context_verify_includes();\n");
295 }
296
297 struct ast_context * ast_walk_contexts(void)
298 {
299         if(!no_comp)
300                 printf("Executed ast_walk_contexts();\n");
301         return 0;
302 }
303
304 void ast_cli_unregister_multiple(void)
305 {
306         if(!no_comp)
307                 printf("Executed ast_cli_unregister_multiple();\n");
308 }
309
310 void ast_context_destroy(void)
311 {
312         printf("Executed ast_context_destroy();\n");
313 }
314
315 void ast_log(int level, const char *file, int line, const char *function, const char *fmt, ...)
316 {
317         va_list vars;
318         va_start(vars,fmt);
319
320         printf("LOG: lev:%d file:%s  line:%d func: %s  ",
321                    level, file, line, function);
322         vprintf(fmt, vars);
323         fflush(stdout);
324         va_end(vars);
325 }
326
327 void ast_verbose(const char *fmt, ...)
328 {
329         va_list vars;
330         va_start(vars,fmt);
331
332         printf("VERBOSE: ");
333         vprintf(fmt, vars);
334         fflush(stdout);
335         va_end(vars);
336 }
337
338 char *ast_process_quotes_and_slashes(char *start, char find, char replace_with)
339 {
340         char *dataPut = start;
341         int inEscape = 0;
342         int inQuotes = 0;
343
344         for (; *start; start++) {
345                 if (inEscape) {
346                         *dataPut++ = *start;       /* Always goes verbatim */
347                         inEscape = 0;
348                 } else {
349                         if (*start == '\\') {
350                                 inEscape = 1;      /* Do not copy \ into the data */
351                         } else if (*start == '\'') {
352                                 inQuotes = 1-inQuotes;   /* Do not copy ' into the data */
353                         } else {
354                                 /* Replace , with |, unless in quotes */
355                                 *dataPut++ = inQuotes ? *start : ((*start==find) ? replace_with : *start);
356                         }
357                 }
358         }
359         if (start != dataPut)
360                 *dataPut = 0;
361         return dataPut;
362 }
363
364 void filter_leading_space_from_exprs(char *str)
365 {
366         /*  Mainly for aesthetics */
367         char *t, *v, *u = str;
368         
369         while ( u && *u ) {
370
371                 if( *u == '$' && *(u+1) == '[' ) {
372                         t = u+2;
373                         while( *t == '\n' || *t == '\r' || *t == '\t' || *t == ' ' ) {
374                                 v = t;
375                                 while ( *v ) {
376                                         *v = *(v+1);
377                                         v++;
378                                 }
379                         }
380                 }
381                 
382                 u++;
383         }
384 }
385
386 void filter_newlines(char *str)
387 {
388         /* remove all newlines, returns  */
389         char *t=str;
390         while( t && *t ) {
391                 if( *t == '\n' || *t == '\r' ) {
392                         *t = ' '; /* just replace newlines and returns with spaces; they act as
393                                                  token separators, and just blindly removing them could be
394                                                  harmful. */
395                 }
396                 t++;
397         }
398 }
399
400
401 extern struct module_symbols mod_data;
402
403 int main(int argc, char **argv)
404 {
405         int i;
406         struct namelist *n;
407         struct ast_context *lp,*lp2;
408         
409         for(i=1;i<argc;i++) {
410                 if( argv[i][0] == '-' && argv[i][1] == 'n' )
411                         no_comp =1;
412                 if( argv[i][0] == '-' && argv[i][1] == 'd' )
413                         use_curr_dir =1;
414                 if( argv[i][0] == '-' && argv[i][1] == 'w' )
415                         dump_extensions =1;
416         }
417         
418                 
419         if( !no_comp )
420                 printf("\n(You can use the -n option if you aren't interested in seeing all the instructions generated by the compiler)\n\n");
421         if( !use_curr_dir )
422                 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");
423         if( !dump_extensions )
424                 printf("\n(You can use the -w option to dump extensions.conf format to extensions.conf.aeldump)\n");
425         
426         if( use_curr_dir ) {
427                 strcpy(ast_config_AST_CONFIG_DIR, ".");
428         }
429         else {
430                 strcpy(ast_config_AST_CONFIG_DIR, "/etc/asterisk");
431         }
432         strcpy(ast_config_AST_VAR_DIR, "/var/lib/asterisk");
433         
434         if( dump_extensions ) {
435                 dumpfile = fopen("extensions.conf.aeldump","w");
436                 if( !dumpfile ) {
437                         printf("\n\nSorry, cannot open extensions.conf.aeldump for writing! Correct the situation and try again!\n\n");
438                         exit(10);
439                 }
440                 
441         }
442
443         FIRST_TIME = 1;
444         
445         mod_data.load_module(0);
446         
447         ast_log(4, "ael2_parse", __LINE__, "main", "%d contexts, %d extensions, %d priorities\n", conts, extens, priors);
448
449         if( dump_extensions && dumpfile ) {
450         
451                 for( lp = context_list; lp; lp = lp->next ) { /* print out any contexts that didn't have any
452                                                                                                                  extensions in them */
453                         if( lp->extension_count == 0 ) {
454                                 
455                                 fprintf(dumpfile,"\n\n[%s]\n", lp->name);
456                                 
457                                 for(n=lp->ignorepats;n;n=n->next) {
458                                         fprintf(dumpfile, "ignorepat => %s\n", n->name);
459                                 }
460                                 for(n=lp->includes;n;n=n->next) {
461                                         fprintf(dumpfile, "include => %s\n", n->name);
462                                 }
463                                 for(n=lp->switches;n;n=n->next) {
464                                         fprintf(dumpfile, "switch => %s/%s\n", n->name, n->name2);
465                                 }
466                                 for(n=lp->eswitches;n;n=n->next) {
467                                         fprintf(dumpfile, "eswitch => %s/%s\n", n->name, n->name2);
468                                 }
469                         }
470                 }
471         }
472         
473         if( dump_extensions && dumpfile )
474                 fclose(dumpfile);
475         
476         for( lp = context_list; lp; lp = lp2 ) { /* free the ast_context structs */
477                 lp2 = lp->next;
478                 lp->next = 0;
479
480                 destroy_namelist(lp->includes);
481                 destroy_namelist(lp->ignorepats);
482                 destroy_namelist(lp->switches);
483                 destroy_namelist(lp->eswitches);
484
485                 free(lp);
486         }
487         
488     return 0;
489 }