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