Merged revisions 114051 via svnmerge from
[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 void ast_register_file_version(const char *file, const char *version)
172 {
173 }
174
175 void ast_unregister_file_version(const char *file);
176 void ast_unregister_file_version(const char *file)
177 {
178 }
179
180 #if !defined(LOW_MEMORY)
181 int ast_add_profile(const char *x, uint64_t scale)
182 {
183         if (!no_comp)
184                 printf("Executed ast_add_profile();\n");
185
186         return 0;
187 }
188 #endif
189
190 int ast_loader_register(int (*updater)(void))
191 {
192         return 1;
193 }
194
195 int ast_loader_unregister(int (*updater)(void))
196 {
197         return 1;
198 }
199 void ast_module_register(const struct ast_module_info *x)
200 {
201 }
202
203 void ast_module_unregister(const struct ast_module_info *x)
204 {
205 }
206
207
208 void ast_cli_register_multiple(void)
209 {
210         if(!no_comp)
211                 printf("Executed ast_cli_register_multiple();\n");
212 }
213
214 void pbx_substitute_variables_helper(struct ast_channel *c,const char *cp1,char *cp2,int count);
215 void pbx_substitute_variables_helper(struct ast_channel *c,const char *cp1,char *cp2,int count)
216 {
217         if (cp1 && *cp1)
218                 strncpy(cp2,cp1,AST_MAX_EXTENSION); /* Right now, this routine is ONLY being called for 
219                                                                                            a possible var substitution on extension names,
220                                                                                            so....! */
221         else
222                 *cp2 = 0;
223 }
224
225 int ast_add_extension2(struct ast_context *con,
226                         int replace, const char *extension, int priority, const char *label, const char *callerid,
227                         const char *application, void *data, void (*datad)(void *),
228                         const char *registrar)
229 {
230         priors++;
231         con->extension_count++;
232         if (strcmp(extension,last_exten) != 0) {
233                 extens++;
234                 strcpy(last_exten, extension);
235         }
236         if (!label) {
237                 label = "(null)";
238         }
239         if (!callerid) {
240                 callerid = "(null)";
241         }
242         if (!application) {
243                 application = "(null)";
244         }
245
246         if(!no_comp)
247                 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",
248                            con->name, replace, extension, priority, label, callerid, application, (data?(char*)data:"(null)"), registrar);
249
250         if( dump_extensions && dumpfile ) {
251                 struct namelist *n;
252                 char *data2,*data3=0;
253                 int commacount = 0;
254
255                 if( FIRST_TIME ) {
256                         FIRST_TIME = 0;
257                         
258                         if( globalvars )
259                                 fprintf(dumpfile,"[globals]\n");
260                         
261                         for(n=globalvars;n;n=n->next) {
262                                 fprintf(dumpfile, "%s\n", n->name);
263                         }
264                 }
265                 
266                 /* print out each extension , possibly the context header also */
267                 if( con != last_context ) {
268                         fprintf(dumpfile,"\n\n[%s]\n", con->name);
269                         last_context = con;
270                         for(n=con->ignorepats;n;n=n->next) {
271                                 fprintf(dumpfile, "ignorepat => %s\n", n->name);
272                         }
273                         for(n=con->includes;n;n=n->next) {
274                                 fprintf(dumpfile, "include => %s\n", n->name);
275                         }
276                         for(n=con->switches;n;n=n->next) {
277                                 fprintf(dumpfile, "switch => %s/%s\n", n->name, n->name2);
278                         }
279                         for(n=con->eswitches;n;n=n->next) {
280                                 fprintf(dumpfile, "eswitch => %s/%s\n", n->name, n->name2);
281                         }
282                         
283                 }
284                 if( data ) {
285                         filter_newlines((char*)data);
286                         filter_leading_space_from_exprs((char*)data);
287
288                         /* compiling turns commas into vertical bars in the app data, and also removes the backslash from before escaped commas;
289                            we have to restore the escaping backslash in front of any commas; the vertical bars are OK to leave as-is */
290                         for (data2 = data; *data2; data2++) {
291                                 if (*data2 == ',')
292                                         commacount++;  /* we need to know how much bigger the string will grow-- one backslash for each comma  */
293                         }
294                         if (commacount) 
295                         {
296                                 char *d3,*d4;
297                                 
298                                 data2 = (char*)malloc(strlen(data)+commacount+1);
299                                 data3 = data;
300                                 d3 = data;
301                                 d4 = data2;
302                                 while (*d3) {
303                                         if (*d3 == ',') {
304                                                 *d4++ = '\\'; /* put a backslash in front of each comma */
305                                                 *d4++ = *d3++;
306                                         } else
307                                                 *d4++ = *d3++;  /* or just copy the char */
308                                 }
309                                 *d4++ = 0;  /* cap off the new string */
310                                 data = data2;
311                         } else
312                                 data2 = 0;
313                         
314                         if( strcmp(label,"(null)") != 0  )
315                                 fprintf(dumpfile,"exten => %s,%d(%s),%s(%s)\n", extension, priority, label, application, (char*)data);
316                         else
317                                 fprintf(dumpfile,"exten => %s,%d,%s(%s)\n", extension, priority, application, (char*)data);
318
319                         if (data2) {
320                                 free(data2);
321                                 data2 = 0;
322                                 data = data3; /* restore data to pre-messedup state */
323                         }
324
325                 } else {
326
327                         if( strcmp(label,"(null)") != 0  )
328                                 fprintf(dumpfile,"exten => %s,%d(%s),%s\n", extension, priority, label, application);
329                         else
330                                 fprintf(dumpfile,"exten => %s,%d,%s\n", extension, priority, application);
331                 }
332         }
333         
334         /* since add_extension2 is responsible for the malloc'd data stuff */
335         if( data )
336                 free(data);
337         return 0;
338 }
339
340 void pbx_builtin_setvar(void *chan, void *data)
341 {
342         struct namelist *x = create_name(data);
343         if(!no_comp)
344                 printf("Executed pbx_builtin_setvar(chan, data=%s);\n", (char*)data);
345
346         if( dump_extensions ) {
347                 x = create_name(data);
348                 ADD_LAST(globalvars,x);
349         }
350 }
351         
352
353 struct ast_context * ast_context_create(void **extcontexts, const char *name, const char *registrar)
354 {
355         struct ast_context *x = calloc(1, sizeof(*x));
356         if (!x)
357                 return NULL;
358         x->next = context_list;
359         context_list = x;
360         if (!no_comp)
361                 printf("Executed ast_context_create(conts, name=%s, registrar=%s);\n", name, registrar);
362         conts++;
363         strncpy(x->name, name, sizeof(x->name) - 1);
364         strncpy(x->registrar, registrar, sizeof(x->registrar) - 1);
365         return x;
366 }
367
368 struct ast_context * ast_context_find_or_create(void **extcontexts, void *tab, const char *name, const char *registrar)
369 {
370         struct ast_context *x = calloc(1, sizeof(*x));
371         if (!x)
372                 return NULL;
373         x->next = context_list;
374         context_list = x;
375         if (!no_comp)
376                 printf("Executed ast_context_find_or_create(conts, name=%s, registrar=%s);\n", name, registrar);
377         conts++;
378         strncpy(x->name, name, sizeof(x->name) - 1);
379         strncpy(x->registrar, registrar, sizeof(x->registrar) - 1);
380         return x;
381 }
382
383 void ast_context_add_ignorepat2(struct ast_context *con, const char *value, const char *registrar)
384 {
385         if(!no_comp)
386                 printf("Executed ast_context_add_ignorepat2(con, value=%s, registrar=%s);\n", value, registrar);
387         if( dump_extensions ) {
388                 struct namelist *x;
389                 x = create_name(value);
390                 ADD_LAST(con->ignorepats,x);
391         }
392 }
393
394 void ast_context_add_include2(struct ast_context *con, const char *value, const char *registrar)
395 {
396         if(!no_comp)
397                 printf("Executed ast_context_add_include2(con, value=%s, registrar=%s);\n", value, registrar);
398         if( dump_extensions ) {
399                 struct namelist *x;
400                 x = create_name((char*)value);
401                 ADD_LAST(con->includes,x);
402         }
403 }
404
405 void ast_context_add_switch2(struct ast_context *con, const char *value, const char *data, int eval, const char *registrar)
406 {
407         if(!no_comp)
408                 printf("Executed ast_context_add_switch2(con, value=%s, data=%s, eval=%d, registrar=%s);\n", value, data, eval, registrar);
409         if( dump_extensions ) {
410                 struct namelist *x;
411                 x = create_name((char*)value);
412                 strncpy(x->name2,data,100);
413                 if( eval ) {
414
415                         ADD_LAST(con->switches,x);
416
417                 } else {
418
419                         ADD_LAST(con->eswitches,x);
420                 }
421         }
422 }
423
424 void ast_merge_contexts_and_delete(void)
425 {
426         if(!no_comp)
427                 printf("Executed ast_merge_contexts_and_delete();\n");
428 }
429
430 void ast_context_verify_includes(void)
431 {
432         if(!no_comp)
433                 printf("Executed ast_context_verify_includes();\n");
434 }
435
436 struct ast_context * ast_walk_contexts(void)
437 {
438         if(!no_comp)
439                 printf("Executed ast_walk_contexts();\n");
440         return 0;
441 }
442
443 void ast_cli_unregister_multiple(void)
444 {
445         if(!no_comp)
446                 printf("Executed ast_cli_unregister_multiple();\n");
447 }
448
449 void ast_context_destroy(void)
450 {
451         if( !no_comp)
452                 printf("Executed ast_context_destroy();\n");
453 }
454
455 void filter_leading_space_from_exprs(char *str)
456 {
457         /*  Mainly for aesthetics */
458         char *t, *v, *u = str;
459         
460         while ( u && *u ) {
461
462                 if( *u == '$' && *(u+1) == '[' ) {
463                         t = u+2;
464                         while( *t == '\n' || *t == '\r' || *t == '\t' || *t == ' ' ) {
465                                 v = t;
466                                 while ( *v ) {
467                                         *v = *(v+1);
468                                         v++;
469                                 }
470                         }
471                 }
472                 
473                 u++;
474         }
475 }
476
477 void filter_newlines(char *str)
478 {
479         /* remove all newlines, returns  */
480         char *t=str;
481         while( t && *t ) {
482                 if( *t == '\n' || *t == '\r' ) {
483                         *t = ' '; /* just replace newlines and returns with spaces; they act as
484                                                  token separators, and just blindly removing them could be
485                                                  harmful. */
486                 }
487                 t++;
488         }
489 }
490
491
492 extern struct module_symbols mod_data;
493 int ael_external_load_module(void);
494
495
496 int main(int argc, char **argv)
497 {
498         int i;
499         struct namelist *n;
500         struct ast_context *lp,*lp2;
501         
502         for(i=1;i<argc;i++) {
503                 if( argv[i][0] == '-' && argv[i][1] == 'n' )
504                         no_comp =1;
505                 if( argv[i][0] == '-' && argv[i][1] == 'q' ) {
506                         quiet = 1;
507                         no_comp =1;
508                 }
509                 if( argv[i][0] == '-' && argv[i][1] == 'd' )
510                         use_curr_dir =1;
511                 if( argv[i][0] == '-' && argv[i][1] == 'w' )
512                         dump_extensions =1;
513         }
514         
515         if( !quiet ) {
516                 printf("\n(If you find progress and other non-error messages irritating, you can use -q to suppress them)\n");
517                 if( !no_comp )
518                         printf("\n(You can use the -n option if you aren't interested in seeing all the instructions generated by the compiler)\n\n");
519                 if( !use_curr_dir )
520                         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");
521                 if( !dump_extensions )
522                         printf("\n(You can use the -w option to dump extensions.conf format to extensions.conf.aeldump)\n");
523         }
524
525         if( use_curr_dir ) {
526                 strcpy(config_dir, ".");
527                 localized_use_local_dir();
528         }
529         else {
530                 strcpy(config_dir, "/etc/asterisk");
531                 localized_use_conf_dir();
532         }
533         strcpy(var_dir, "/var/lib/asterisk");
534         
535         if( dump_extensions ) {
536                 dumpfile = fopen("extensions.conf.aeldump","w");
537                 if( !dumpfile ) {
538                         printf("\n\nSorry, cannot open extensions.conf.aeldump for writing! Correct the situation and try again!\n\n");
539                         exit(10);
540                 }
541                 
542         }
543
544         FIRST_TIME = 1;
545         
546         ael_external_load_module();
547         
548         ast_log(4, "ael2_parse", __LINE__, "main", "%d contexts, %d extensions, %d priorities\n", conts, extens, priors);
549
550         if( dump_extensions && dumpfile ) {
551         
552                 for( lp = context_list; lp; lp = lp->next ) { /* print out any contexts that didn't have any
553                                                                                                                  extensions in them */
554                         if( lp->extension_count == 0 ) {
555                                 
556                                 fprintf(dumpfile,"\n\n[%s]\n", lp->name);
557                                 
558                                 for(n=lp->ignorepats;n;n=n->next) {
559                                         fprintf(dumpfile, "ignorepat => %s\n", n->name);
560                                 }
561                                 for(n=lp->includes;n;n=n->next) {
562                                         fprintf(dumpfile, "include => %s\n", n->name);
563                                 }
564                                 for(n=lp->switches;n;n=n->next) {
565                                         fprintf(dumpfile, "switch => %s/%s\n", n->name, n->name2);
566                                 }
567                                 for(n=lp->eswitches;n;n=n->next) {
568                                         fprintf(dumpfile, "eswitch => %s/%s\n", n->name, n->name2);
569                                 }
570                         }
571                 }
572         }
573         
574         if( dump_extensions && dumpfile )
575                 fclose(dumpfile);
576         
577         for( lp = context_list; lp; lp = lp2 ) { /* free the ast_context structs */
578                 lp2 = lp->next;
579                 lp->next = 0;
580
581                 destroy_namelist(lp->includes);
582                 destroy_namelist(lp->ignorepats);
583                 destroy_namelist(lp->switches);
584                 destroy_namelist(lp->eswitches);
585
586                 free(lp);
587         }
588         
589     return 0;
590 }
591
592 int ast_hashtab_compare_contexts(const void *ah_a, const void *ah_b);
593
594 int ast_hashtab_compare_contexts(const void *ah_a, const void *ah_b)
595 {
596         return 0;
597 }
598
599 unsigned int ast_hashtab_hash_contexts(const void *obj);
600
601 unsigned int ast_hashtab_hash_contexts(const void *obj)
602 {
603         return 0;
604 }
605
606 #ifdef DEBUG_THREADS
607 #if !defined(LOW_MEMORY)
608 void ast_mark_lock_acquired(void *lock_addr)
609 {
610 }
611
612 void ast_remove_lock_info(void *lock_addr)
613 {
614 }
615
616 void ast_store_lock_info(enum ast_lock_type type, const char *filename,
617         int line_num, const char *func, const char *lock_name, void *lock_addr)
618 {
619 }
620 #endif
621 #endif