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