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