when a PRI call must be moved to a different B channel at the request of the other...
[asterisk-bristuff.git] / utils / ael_main.c
blob6c8f3d39055da1bd54f7a9ea76b63f24110ac072
1 #include <sys/types.h>
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <stdarg.h>
5 #include <string.h>
6 #include <locale.h>
7 #include <ctype.h>
8 #include <errno.h>
9 #include <regex.h>
10 #include <limits.h>
12 #include "asterisk/ast_expr.h"
13 #include "asterisk/channel.h"
14 #include "asterisk/module.h"
15 #include "asterisk/app.h"
16 #include "asterisk/ael_structs.h"
18 /*** MODULEINFO
19 <depend>pbx_ael</depend>
20 ***/
22 struct namelist
24 char name[100];
25 char name2[100];
26 struct namelist *next;
29 struct ast_context
31 int extension_count;
32 char name[100];
33 char registrar[100];
34 struct namelist *includes;
35 struct namelist *ignorepats;
36 struct namelist *switches;
37 struct namelist *eswitches;
39 struct namelist *includes_last;
40 struct namelist *ignorepats_last;
41 struct namelist *switches_last;
42 struct namelist *eswitches_last;
44 struct ast_context *next;
47 #define ADD_LAST(headptr,memptr) if(!headptr){ headptr=(memptr); (headptr##_last)=(memptr);} else {(headptr##_last)->next = (memptr); (headptr##_last) = (memptr);}
49 void destroy_namelist(struct namelist *x);
50 void destroy_namelist(struct namelist *x)
52 struct namelist *z,*z2;
53 for(z=x; z; z = z2)
55 z2 = z->next;
56 z->next = 0;
57 free(z);
61 struct namelist *create_name(const char *name);
62 struct namelist *create_name(const char *name)
64 struct namelist *x = calloc(1, sizeof(*x));
65 if (!x)
66 return NULL;
67 strncpy(x->name, name, sizeof(x->name) - 1);
68 return x;
71 struct ast_context *context_list;
72 struct ast_context *last_context;
73 struct namelist *globalvars;
74 struct namelist *globalvars_last;
76 int conts=0, extens=0, priors=0;
77 char last_exten[18000];
78 char ast_config_AST_CONFIG_DIR[PATH_MAX];
79 char ast_config_AST_VAR_DIR[PATH_MAX];
81 void ast_add_profile(void);
82 void ast_cli_register_multiple(void);
83 void ast_register_file_version(void);
84 void ast_unregister_file_version(void);
85 int ast_add_extension2(struct ast_context *con,
86 int replace, const char *extension, int priority, const char *label, const char *callerid,
87 const char *application, void *data, void (*datad)(void *),
88 const char *registrar);
89 void pbx_builtin_setvar(void *chan, void *data);
90 struct ast_context * ast_context_create(void **extcontexts, const char *name, const char *registrar);
91 struct ast_context * ast_context_find_or_create(void **extcontexts, const char *name, const char *registrar);
92 void ast_context_add_ignorepat2(struct ast_context *con, const char *value, const char *registrar);
93 void ast_context_add_include2(struct ast_context *con, const char *value, const char *registrar);
94 void ast_context_add_switch2(struct ast_context *con, const char *value, const char *data, int eval, const char *registrar);
95 void ast_merge_contexts_and_delete(void);
96 void ast_context_verify_includes(void);
97 struct ast_context * ast_walk_contexts(void);
98 void ast_cli_unregister_multiple(void);
99 void ast_context_destroy(void);
100 void ast_log(int level, const char *file, int line, const char *function, const char *fmt, ...);
101 char *ast_process_quotes_and_slashes(char *start, char find, char replace_with);
102 void ast_verbose(const char *fmt, ...);
103 struct ast_app *pbx_findapp(const char *app);
104 void filter_leading_space_from_exprs(char *str);
105 void filter_newlines(char *str);
106 static int quiet = 0;
107 static int no_comp = 0;
108 static int use_curr_dir = 0;
109 static int dump_extensions = 0;
110 static int FIRST_TIME = 0;
111 static FILE *dumpfile;
113 struct ast_app *pbx_findapp(const char *app)
115 return (struct ast_app*)1; /* so as not to trigger an error */
118 void ast_add_profile(void)
120 if (!no_comp)
121 printf("Executed ast_add_profile();\n");
124 int ast_loader_register(int (*updater)(void))
126 return 1;
129 int ast_loader_unregister(int (*updater)(void))
131 return 1;
133 void ast_module_register(const struct ast_module_info *x)
137 void ast_module_unregister(const struct ast_module_info *x)
142 void ast_cli_register_multiple(void)
144 if(!no_comp)
145 printf("Executed ast_cli_register_multiple();\n");
148 void ast_register_file_version(void)
150 /* if(!no_comp)
151 printf("Executed ast_register_file_version();\n"); */
152 /* I'm erasing this, because I don't think anyone really ever needs to see it anyway */
155 void ast_unregister_file_version(void)
157 /* if(!no_comp)
158 printf("Executed ast_unregister_file_version();\n"); */
159 /* I'm erasing this, because I don't think anyone really ever needs to see it anyway */
162 void pbx_substitute_variables_helper(struct ast_channel *c,const char *cp1,char *cp2,int count);
163 void pbx_substitute_variables_helper(struct ast_channel *c,const char *cp1,char *cp2,int count)
165 if (cp1 && *cp1)
166 strncpy(cp2,cp1,AST_MAX_EXTENSION); /* Right now, this routine is ONLY being called for
167 a possible var substitution on extension names,
168 so....! */
169 else
170 *cp2 = 0;
173 int ast_add_extension2(struct ast_context *con,
174 int replace, const char *extension, int priority, const char *label, const char *callerid,
175 const char *application, void *data, void (*datad)(void *),
176 const char *registrar)
178 priors++;
179 con->extension_count++;
180 if (strcmp(extension,last_exten) != 0) {
181 extens++;
182 strcpy(last_exten, extension);
184 if (!label) {
185 label = "(null)";
187 if (!callerid) {
188 callerid = "(null)";
190 if (!application) {
191 application = "(null)";
194 if(!no_comp)
195 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",
196 con->name, replace, extension, priority, label, callerid, application, (data?(char*)data:"(null)"), registrar);
198 if( dump_extensions && dumpfile ) {
199 struct namelist *n;
200 char *data2,*data3=0;
201 int commacount = 0;
203 if( FIRST_TIME ) {
204 FIRST_TIME = 0;
206 if( globalvars )
207 fprintf(dumpfile,"[globals]\n");
209 for(n=globalvars;n;n=n->next) {
210 fprintf(dumpfile, "%s\n", n->name);
214 /* print out each extension , possibly the context header also */
215 if( con != last_context ) {
216 fprintf(dumpfile,"\n\n[%s]\n", con->name);
217 last_context = con;
218 for(n=con->ignorepats;n;n=n->next) {
219 fprintf(dumpfile, "ignorepat => %s\n", n->name);
221 for(n=con->includes;n;n=n->next) {
222 fprintf(dumpfile, "include => %s\n", n->name);
224 for(n=con->switches;n;n=n->next) {
225 fprintf(dumpfile, "switch => %s/%s\n", n->name, n->name2);
227 for(n=con->eswitches;n;n=n->next) {
228 fprintf(dumpfile, "eswitch => %s/%s\n", n->name, n->name2);
232 if( data ) {
233 filter_newlines((char*)data);
234 filter_leading_space_from_exprs((char*)data);
236 /* compiling turns commas into vertical bars in the app data, and also removes the backslash from before escaped commas;
237 we have to restore the escaping backslash in front of any commas; the vertical bars are OK to leave as-is */
238 for (data2 = data; *data2; data2++) {
239 if (*data2 == ',')
240 commacount++; /* we need to know how much bigger the string will grow-- one backslash for each comma */
242 if (commacount)
244 char *d3,*d4;
246 data2 = (char*)malloc(strlen(data)+commacount+1);
247 data3 = data;
248 d3 = data;
249 d4 = data2;
250 while (*d3) {
251 if (*d3 == ',') {
252 *d4++ = '\\'; /* put a backslash in front of each comma */
253 *d4++ = *d3++;
254 } else
255 *d4++ = *d3++; /* or just copy the char */
257 *d4++ = 0; /* cap off the new string */
258 data = data2;
259 } else
260 data2 = 0;
262 if( strcmp(label,"(null)") != 0 )
263 fprintf(dumpfile,"exten => %s,%d(%s),%s(%s)\n", extension, priority, label, application, (char*)data);
264 else
265 fprintf(dumpfile,"exten => %s,%d,%s(%s)\n", extension, priority, application, (char*)data);
267 if (data2) {
268 free(data2);
269 data2 = 0;
270 data = data3; /* restore data to pre-messedup state */
273 } else {
275 if( strcmp(label,"(null)") != 0 )
276 fprintf(dumpfile,"exten => %s,%d(%s),%s\n", extension, priority, label, application);
277 else
278 fprintf(dumpfile,"exten => %s,%d,%s\n", extension, priority, application);
282 /* since add_extension2 is responsible for the malloc'd data stuff */
283 if( data )
284 free(data);
285 return 0;
288 void pbx_builtin_setvar(void *chan, void *data)
290 struct namelist *x = create_name(data);
291 if(!no_comp)
292 printf("Executed pbx_builtin_setvar(chan, data=%s);\n", (char*)data);
294 if( dump_extensions ) {
295 x = create_name(data);
296 ADD_LAST(globalvars,x);
301 struct ast_context * ast_context_create(void **extcontexts, const char *name, const char *registrar)
303 struct ast_context *x = calloc(1, sizeof(*x));
304 if (!x)
305 return NULL;
306 x->next = context_list;
307 context_list = x;
308 if (!no_comp)
309 printf("Executed ast_context_create(conts, name=%s, registrar=%s);\n", name, registrar);
310 conts++;
311 strncpy(x->name, name, sizeof(x->name) - 1);
312 strncpy(x->registrar, registrar, sizeof(x->registrar) - 1);
313 return x;
316 struct ast_context * ast_context_find_or_create(void **extcontexts, const char *name, const char *registrar)
318 struct ast_context *x = calloc(1, sizeof(*x));
319 if (!x)
320 return NULL;
321 x->next = context_list;
322 context_list = x;
323 if (!no_comp)
324 printf("Executed ast_context_find_or_create(conts, name=%s, registrar=%s);\n", name, registrar);
325 conts++;
326 strncpy(x->name, name, sizeof(x->name) - 1);
327 strncpy(x->registrar, registrar, sizeof(x->registrar) - 1);
328 return x;
331 void ast_context_add_ignorepat2(struct ast_context *con, const char *value, const char *registrar)
333 if(!no_comp)
334 printf("Executed ast_context_add_ignorepat2(con, value=%s, registrar=%s);\n", value, registrar);
335 if( dump_extensions ) {
336 struct namelist *x;
337 x = create_name(value);
338 ADD_LAST(con->ignorepats,x);
342 void ast_context_add_include2(struct ast_context *con, const char *value, const char *registrar)
344 if(!no_comp)
345 printf("Executed ast_context_add_include2(con, value=%s, registrar=%s);\n", value, registrar);
346 if( dump_extensions ) {
347 struct namelist *x;
348 x = create_name((char*)value);
349 ADD_LAST(con->includes,x);
353 void ast_context_add_switch2(struct ast_context *con, const char *value, const char *data, int eval, const char *registrar)
355 if(!no_comp)
356 printf("Executed ast_context_add_switch2(con, value=%s, data=%s, eval=%d, registrar=%s);\n", value, data, eval, registrar);
357 if( dump_extensions ) {
358 struct namelist *x;
359 x = create_name((char*)value);
360 strncpy(x->name2,data,100);
361 if( eval ) {
363 ADD_LAST(con->switches,x);
365 } else {
367 ADD_LAST(con->eswitches,x);
372 void ast_merge_contexts_and_delete(void)
374 if(!no_comp)
375 printf("Executed ast_merge_contexts_and_delete();\n");
378 void ast_context_verify_includes(void)
380 if(!no_comp)
381 printf("Executed ast_context_verify_includes();\n");
384 struct ast_context * ast_walk_contexts(void)
386 if(!no_comp)
387 printf("Executed ast_walk_contexts();\n");
388 return 0;
391 void ast_cli_unregister_multiple(void)
393 if(!no_comp)
394 printf("Executed ast_cli_unregister_multiple();\n");
397 void ast_context_destroy(void)
399 if( !no_comp)
400 printf("Executed ast_context_destroy();\n");
403 void ast_log(int level, const char *file, int line, const char *function, const char *fmt, ...)
405 va_list vars;
406 va_start(vars,fmt);
407 if( !quiet || level > 2 ) {
408 printf("LOG: lev:%d file:%s line:%d func: %s ",
409 level, file, line, function);
410 vprintf(fmt, vars);
411 fflush(stdout);
412 va_end(vars);
416 void ast_verbose(const char *fmt, ...)
418 va_list vars;
419 va_start(vars,fmt);
421 printf("VERBOSE: ");
422 vprintf(fmt, vars);
423 fflush(stdout);
424 va_end(vars);
427 char *ast_process_quotes_and_slashes(char *start, char find, char replace_with)
429 char *dataPut = start;
430 int inEscape = 0;
431 int inQuotes = 0;
433 for (; *start; start++) {
434 if (inEscape) {
435 *dataPut++ = *start; /* Always goes verbatim */
436 inEscape = 0;
437 } else {
438 if (*start == '\\') {
439 inEscape = 1; /* Do not copy \ into the data */
440 } else if (*start == '\'') {
441 inQuotes = 1-inQuotes; /* Do not copy ' into the data */
442 } else {
443 /* Replace , with |, unless in quotes */
444 *dataPut++ = inQuotes ? *start : ((*start==find) ? replace_with : *start);
448 if (start != dataPut)
449 *dataPut = 0;
450 return dataPut;
453 void filter_leading_space_from_exprs(char *str)
455 /* Mainly for aesthetics */
456 char *t, *v, *u = str;
458 while ( u && *u ) {
460 if( *u == '$' && *(u+1) == '[' ) {
461 t = u+2;
462 while( *t == '\n' || *t == '\r' || *t == '\t' || *t == ' ' ) {
463 v = t;
464 while ( *v ) {
465 *v = *(v+1);
466 v++;
471 u++;
475 void filter_newlines(char *str)
477 /* remove all newlines, returns */
478 char *t=str;
479 while( t && *t ) {
480 if( *t == '\n' || *t == '\r' ) {
481 *t = ' '; /* just replace newlines and returns with spaces; they act as
482 token separators, and just blindly removing them could be
483 harmful. */
485 t++;
490 extern struct module_symbols mod_data;
491 extern int ael_external_load_module(void);
493 int main(int argc, char **argv)
495 int i;
496 struct namelist *n;
497 struct ast_context *lp,*lp2;
499 for(i=1;i<argc;i++) {
500 if( argv[i][0] == '-' && argv[i][1] == 'n' )
501 no_comp =1;
502 if( argv[i][0] == '-' && argv[i][1] == 'q' ) {
503 quiet = 1;
504 no_comp =1;
506 if( argv[i][0] == '-' && argv[i][1] == 'd' )
507 use_curr_dir =1;
508 if( argv[i][0] == '-' && argv[i][1] == 'w' )
509 dump_extensions =1;
512 if( !quiet ) {
513 printf("\n(If you find progress and other non-error messages irritating, you can use -q to suppress them)\n");
514 if( !no_comp )
515 printf("\n(You can use the -n option if you aren't interested in seeing all the instructions generated by the compiler)\n\n");
516 if( !use_curr_dir )
517 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");
518 if( !dump_extensions )
519 printf("\n(You can use the -w option to dump extensions.conf format to extensions.conf.aeldump)\n");
522 if( use_curr_dir ) {
523 strcpy(ast_config_AST_CONFIG_DIR, ".");
525 else {
526 strcpy(ast_config_AST_CONFIG_DIR, "/etc/asterisk");
528 strcpy(ast_config_AST_VAR_DIR, "/var/lib/asterisk");
530 if( dump_extensions ) {
531 dumpfile = fopen("extensions.conf.aeldump","w");
532 if( !dumpfile ) {
533 printf("\n\nSorry, cannot open extensions.conf.aeldump for writing! Correct the situation and try again!\n\n");
534 exit(10);
539 FIRST_TIME = 1;
541 ael_external_load_module();
543 ast_log(4, "ael2_parse", __LINE__, "main", "%d contexts, %d extensions, %d priorities\n", conts, extens, priors);
545 if( dump_extensions && dumpfile ) {
547 for( lp = context_list; lp; lp = lp->next ) { /* print out any contexts that didn't have any
548 extensions in them */
549 if( lp->extension_count == 0 ) {
551 fprintf(dumpfile,"\n\n[%s]\n", lp->name);
553 for(n=lp->ignorepats;n;n=n->next) {
554 fprintf(dumpfile, "ignorepat => %s\n", n->name);
556 for(n=lp->includes;n;n=n->next) {
557 fprintf(dumpfile, "include => %s\n", n->name);
559 for(n=lp->switches;n;n=n->next) {
560 fprintf(dumpfile, "switch => %s/%s\n", n->name, n->name2);
562 for(n=lp->eswitches;n;n=n->next) {
563 fprintf(dumpfile, "eswitch => %s/%s\n", n->name, n->name2);
569 if( dump_extensions && dumpfile )
570 fclose(dumpfile);
572 for( lp = context_list; lp; lp = lp2 ) { /* free the ast_context structs */
573 lp2 = lp->next;
574 lp->next = 0;
576 destroy_namelist(lp->includes);
577 destroy_namelist(lp->ignorepats);
578 destroy_namelist(lp->switches);
579 destroy_namelist(lp->eswitches);
581 free(lp);
584 return 0;