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