modify http://svn.digium.com/view/asterisk?view=rev&rev=93603
[asterisk/asterisk.git] / utils / ael_main.c
1 /*
2  * XXX this file probably need a fair amount of cleanup, at the very least:
3  *
4  * - documenting its purpose;
5  * - removing all unnecessary headers and other stuff from the sources
6  *   it was copied from;
7  * - fixing the formatting
8  */
9 #include "asterisk.h"
10
11 #include <locale.h>
12 #include <ctype.h>
13 #include <regex.h>
14 #include <limits.h>
15
16 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
17
18 #include "asterisk/channel.h"
19 #include "asterisk/ast_expr.h"
20 #include "asterisk/module.h"
21 #include "asterisk/app.h"
22 #include "asterisk/ael_structs.h"
23 #include "asterisk/extconf.h"
24
25 /*** MODULEINFO
26         <depend>res_ael_share</depend>
27  ***/
28
29 struct namelist
30 {
31         char name[100];
32         char name2[100];
33         struct namelist *next;
34 };
35
36 struct ast_context 
37 {
38         int extension_count;
39         char name[100];
40         char registrar[100];
41         struct namelist *includes;
42         struct namelist *ignorepats;
43         struct namelist *switches;
44         struct namelist *eswitches;
45
46         struct namelist *includes_last;
47         struct namelist *ignorepats_last;
48         struct namelist *switches_last;
49         struct namelist *eswitches_last;
50
51         struct ast_context *next;
52 };
53
54 #define ADD_LAST(headptr,memptr) if(!headptr){ headptr=(memptr); (headptr##_last)=(memptr);} else {(headptr##_last)->next = (memptr); (headptr##_last) = (memptr);}
55
56 void destroy_namelist(struct namelist *x);
57 void destroy_namelist(struct namelist *x)
58 {
59         struct namelist *z,*z2;
60         for(z=x; z; z = z2)
61         {
62                 z2 = z->next;
63                 z->next = 0;
64                 free(z);
65         }
66 }
67
68 struct namelist *create_name(const char *name);
69 struct namelist *create_name(const char *name)
70 {
71         struct namelist *x = calloc(1, sizeof(*x));
72         if (!x)
73                 return NULL;
74         strncpy(x->name, name, sizeof(x->name) - 1);
75         return x;
76 }
77
78 struct ast_context *context_list;
79 struct ast_context *last_context;
80 struct namelist *globalvars;
81 struct namelist *globalvars_last;
82
83 int conts=0, extens=0, priors=0;
84 char last_exten[18000];
85
86 static char config_dir[PATH_MAX];
87 static char var_dir[PATH_MAX];
88 const char *ast_config_AST_CONFIG_DIR = config_dir;
89 const char *ast_config_AST_VAR_DIR = var_dir;
90
91 void ast_cli_register_multiple(void);
92 int ast_add_extension2(struct ast_context *con,
93                                            int replace, const char *extension, int priority, const char *label, const char *callerid,
94                                                 const char *application, void *data, void (*datad)(void *),
95                                            const char *registrar);
96 void pbx_builtin_setvar(void *chan, void *data);
97 struct ast_context * ast_context_create(void **extcontexts, const char *name, const char *registrar);
98 struct ast_context * ast_context_find_or_create(void **extcontexts, const char *name, const char *registrar);
99 void ast_context_add_ignorepat2(struct ast_context *con, const char *value, const char *registrar);
100 void ast_context_add_include2(struct ast_context *con, const char *value, const char *registrar);
101 void ast_context_add_switch2(struct ast_context *con, const char *value, const char *data, int eval, const char *registrar);
102 void ast_merge_contexts_and_delete(void);
103 void ast_context_verify_includes(void);
104 struct ast_context * ast_walk_contexts(void);
105 void ast_cli_unregister_multiple(void);
106 void ast_context_destroy(void);
107 void ast_log(int level, const char *file, int line, const char *function, const char *fmt, ...);
108 char *ast_process_quotes_and_slashes(char *start, char find, char replace_with);
109 void ast_verbose(const char *fmt, ...);
110 struct ast_app *pbx_findapp(const char *app);
111 void filter_leading_space_from_exprs(char *str);
112 void filter_newlines(char *str);
113 static int quiet = 0;
114 static int no_comp = 0;
115 static int use_curr_dir = 0;
116 static int dump_extensions = 0;
117 static int FIRST_TIME = 0;
118 static FILE *dumpfile;
119
120 void ast_log(int level, const char *file, int line, const char *function, const char *fmt, ...)
121 {
122                 va_list vars;
123                         va_start(vars,fmt);
124                         
125                                 printf("LOG: lev:%d file:%s  line:%d func: %s  ",
126                                                                    level, file, line, function);
127                                         vprintf(fmt, vars);
128                                                 fflush(stdout);
129                                                         va_end(vars);
130 }
131
132 struct ast_exten *pbx_find_extension(struct ast_channel *chan,
133                                                                          struct ast_context *bypass,
134                                                                          struct pbx_find_info *q,
135                                                                          const char *context, 
136                                                                          const char *exten, 
137                                                                          int priority,
138                                                                          const char *label, 
139                                                                          const char *callerid, 
140                                                                          enum ext_match_t action);
141
142 struct ast_exten *pbx_find_extension(struct ast_channel *chan,
143                                                                          struct ast_context *bypass,
144                                                                          struct pbx_find_info *q,
145                                                                          const char *context, 
146                                                                          const char *exten, 
147                                                                          int priority,
148                                                                          const char *label, 
149                                                                          const char *callerid, 
150                                                                          enum ext_match_t action)
151 {
152         return localized_find_extension(bypass, q, context, exten, priority, label, callerid, action);
153 }
154
155 struct ast_app *pbx_findapp(const char *app)
156 {
157         return (struct ast_app*)1; /* so as not to trigger an error */
158 }
159
160 struct ast_custom_function *ast_custom_function_find(const char *name);
161
162
163 struct ast_custom_function *ast_custom_function_find(const char *name)
164 {
165         return 0; /* in "standalone" mode, functions are just not avail */
166 }
167
168 void ast_register_file_version(const char *file, const char *version)
169 {
170 }
171
172 void ast_unregister_file_version(const char *file)
173 {
174 }
175
176 int ast_add_profile(const char *x, uint64_t scale)
177 {
178         if (!no_comp)
179                 printf("Executed ast_add_profile();\n");
180
181         return 0;
182 }
183
184 int ast_loader_register(int (*updater)(void))
185 {
186         return 1;
187 }
188
189 int ast_loader_unregister(int (*updater)(void))
190 {
191         return 1;
192 }
193 void ast_module_register(const struct ast_module_info *x)
194 {
195 }
196
197 void ast_module_unregister(const struct ast_module_info *x)
198 {
199 }
200
201
202 void ast_cli_register_multiple(void)
203 {
204         if(!no_comp)
205                 printf("Executed ast_cli_register_multiple();\n");
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(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(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 struct ast_context * ast_context_find_or_create(void **extcontexts, const char *name, const char *registrar)
363 {
364         struct ast_context *x = calloc(1, sizeof(*x));
365         if (!x)
366                 return NULL;
367         x->next = context_list;
368         context_list = x;
369         if (!no_comp)
370                 printf("Executed ast_context_find_or_create(conts, name=%s, registrar=%s);\n", name, registrar);
371         conts++;
372         strncpy(x->name, name, sizeof(x->name) - 1);
373         strncpy(x->registrar, registrar, sizeof(x->registrar) - 1);
374         return x;
375 }
376
377 void ast_context_add_ignorepat2(struct ast_context *con, const char *value, const char *registrar)
378 {
379         if(!no_comp)
380                 printf("Executed ast_context_add_ignorepat2(con, value=%s, registrar=%s);\n", value, registrar);
381         if( dump_extensions ) {
382                 struct namelist *x;
383                 x = create_name(value);
384                 ADD_LAST(con->ignorepats,x);
385         }
386 }
387
388 void ast_context_add_include2(struct ast_context *con, const char *value, const char *registrar)
389 {
390         if(!no_comp)
391                 printf("Executed ast_context_add_include2(con, value=%s, registrar=%s);\n", value, registrar);
392         if( dump_extensions ) {
393                 struct namelist *x;
394                 x = create_name((char*)value);
395                 ADD_LAST(con->includes,x);
396         }
397 }
398
399 void ast_context_add_switch2(struct ast_context *con, const char *value, const char *data, int eval, const char *registrar)
400 {
401         if(!no_comp)
402                 printf("Executed ast_context_add_switch2(con, value=%s, data=%s, eval=%d, registrar=%s);\n", value, data, eval, registrar);
403         if( dump_extensions ) {
404                 struct namelist *x;
405                 x = create_name((char*)value);
406                 strncpy(x->name2,data,100);
407                 if( eval ) {
408
409                         ADD_LAST(con->switches,x);
410
411                 } else {
412
413                         ADD_LAST(con->eswitches,x);
414                 }
415         }
416 }
417
418 void ast_merge_contexts_and_delete(void)
419 {
420         if(!no_comp)
421                 printf("Executed ast_merge_contexts_and_delete();\n");
422 }
423
424 void ast_context_verify_includes(void)
425 {
426         if(!no_comp)
427                 printf("Executed ast_context_verify_includes();\n");
428 }
429
430 struct ast_context * ast_walk_contexts(void)
431 {
432         if(!no_comp)
433                 printf("Executed ast_walk_contexts();\n");
434         return 0;
435 }
436
437 void ast_cli_unregister_multiple(void)
438 {
439         if(!no_comp)
440                 printf("Executed ast_cli_unregister_multiple();\n");
441 }
442
443 void ast_context_destroy(void)
444 {
445         if( !no_comp)
446                 printf("Executed ast_context_destroy();\n");
447 }
448
449 void filter_leading_space_from_exprs(char *str)
450 {
451         /*  Mainly for aesthetics */
452         char *t, *v, *u = str;
453         
454         while ( u && *u ) {
455
456                 if( *u == '$' && *(u+1) == '[' ) {
457                         t = u+2;
458                         while( *t == '\n' || *t == '\r' || *t == '\t' || *t == ' ' ) {
459                                 v = t;
460                                 while ( *v ) {
461                                         *v = *(v+1);
462                                         v++;
463                                 }
464                         }
465                 }
466                 
467                 u++;
468         }
469 }
470
471 void filter_newlines(char *str)
472 {
473         /* remove all newlines, returns  */
474         char *t=str;
475         while( t && *t ) {
476                 if( *t == '\n' || *t == '\r' ) {
477                         *t = ' '; /* just replace newlines and returns with spaces; they act as
478                                                  token separators, and just blindly removing them could be
479                                                  harmful. */
480                 }
481                 t++;
482         }
483 }
484
485
486 extern struct module_symbols mod_data;
487 int ael_external_load_module(void);
488
489
490 int main(int argc, char **argv)
491 {
492         int i;
493         struct namelist *n;
494         struct ast_context *lp,*lp2;
495         
496         for(i=1;i<argc;i++) {
497                 if( argv[i][0] == '-' && argv[i][1] == 'n' )
498                         no_comp =1;
499                 if( argv[i][0] == '-' && argv[i][1] == 'q' ) {
500                         quiet = 1;
501                         no_comp =1;
502                 }
503                 if( argv[i][0] == '-' && argv[i][1] == 'd' )
504                         use_curr_dir =1;
505                 if( argv[i][0] == '-' && argv[i][1] == 'w' )
506                         dump_extensions =1;
507         }
508         
509         if( !quiet ) {
510                 printf("\n(If you find progress and other non-error messages irritating, you can use -q to suppress them)\n");
511                 if( !no_comp )
512                         printf("\n(You can use the -n option if you aren't interested in seeing all the instructions generated by the compiler)\n\n");
513                 if( !use_curr_dir )
514                         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");
515                 if( !dump_extensions )
516                         printf("\n(You can use the -w option to dump extensions.conf format to extensions.conf.aeldump)\n");
517         }
518
519         if( use_curr_dir ) {
520                 strcpy(config_dir, ".");
521                 localized_use_local_dir();
522         }
523         else {
524                 strcpy(config_dir, "/etc/asterisk");
525                 localized_use_conf_dir();
526         }
527         strcpy(var_dir, "/var/lib/asterisk");
528         
529         if( dump_extensions ) {
530                 dumpfile = fopen("extensions.conf.aeldump","w");
531                 if( !dumpfile ) {
532                         printf("\n\nSorry, cannot open extensions.conf.aeldump for writing! Correct the situation and try again!\n\n");
533                         exit(10);
534                 }
535                 
536         }
537
538         FIRST_TIME = 1;
539         
540         ael_external_load_module();
541         
542         ast_log(4, "ael2_parse", __LINE__, "main", "%d contexts, %d extensions, %d priorities\n", conts, extens, priors);
543
544         if( dump_extensions && dumpfile ) {
545         
546                 for( lp = context_list; lp; lp = lp->next ) { /* print out any contexts that didn't have any
547                                                                                                                  extensions in them */
548                         if( lp->extension_count == 0 ) {
549                                 
550                                 fprintf(dumpfile,"\n\n[%s]\n", lp->name);
551                                 
552                                 for(n=lp->ignorepats;n;n=n->next) {
553                                         fprintf(dumpfile, "ignorepat => %s\n", n->name);
554                                 }
555                                 for(n=lp->includes;n;n=n->next) {
556                                         fprintf(dumpfile, "include => %s\n", n->name);
557                                 }
558                                 for(n=lp->switches;n;n=n->next) {
559                                         fprintf(dumpfile, "switch => %s/%s\n", n->name, n->name2);
560                                 }
561                                 for(n=lp->eswitches;n;n=n->next) {
562                                         fprintf(dumpfile, "eswitch => %s/%s\n", n->name, n->name2);
563                                 }
564                         }
565                 }
566         }
567         
568         if( dump_extensions && dumpfile )
569                 fclose(dumpfile);
570         
571         for( lp = context_list; lp; lp = lp2 ) { /* free the ast_context structs */
572                 lp2 = lp->next;
573                 lp->next = 0;
574
575                 destroy_namelist(lp->includes);
576                 destroy_namelist(lp->ignorepats);
577                 destroy_namelist(lp->switches);
578                 destroy_namelist(lp->eswitches);
579
580                 free(lp);
581         }
582         
583     return 0;
584 }