2 * XXX this file probably need a fair amount of cleanup, at the very least:
4 * - documenting its purpose;
5 * - removing all unnecessary headers and other stuff from the sources
7 * - fixing the formatting
16 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
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"
26 <depend>res_ael_share</depend>
33 struct namelist *next;
41 struct namelist *includes;
42 struct namelist *ignorepats;
43 struct namelist *switches;
44 struct namelist *eswitches;
46 struct namelist *includes_last;
47 struct namelist *ignorepats_last;
48 struct namelist *switches_last;
49 struct namelist *eswitches_last;
51 struct ast_context *next;
54 #define ADD_LAST(headptr,memptr) if(!headptr){ headptr=(memptr); (headptr##_last)=(memptr);} else {(headptr##_last)->next = (memptr); (headptr##_last) = (memptr);}
56 void destroy_namelist(struct namelist *x);
57 void destroy_namelist(struct namelist *x)
59 struct namelist *z,*z2;
68 struct namelist *create_name(const char *name);
69 struct namelist *create_name(const char *name)
71 struct namelist *x = calloc(1, sizeof(*x));
74 strncpy(x->name, name, sizeof(x->name) - 1);
78 struct ast_context *context_list;
79 struct ast_context *last_context;
80 struct namelist *globalvars;
81 struct namelist *globalvars_last;
83 int conts=0, extens=0, priors=0;
84 char last_exten[18000];
85 char ast_config_AST_CONFIG_DIR[PATH_MAX];
86 char ast_config_AST_VAR_DIR[PATH_MAX];
88 void ast_cli_register_multiple(void);
89 int ast_add_extension2(struct ast_context *con,
90 int replace, const char *extension, int priority, const char *label, const char *callerid,
91 const char *application, void *data, void (*datad)(void *),
92 const char *registrar);
93 void pbx_builtin_setvar(void *chan, void *data);
94 struct ast_context * ast_context_create(void **extcontexts, const char *name, const char *registrar);
95 struct ast_context * ast_context_find_or_create(void **extcontexts, const char *name, const char *registrar);
96 void ast_context_add_ignorepat2(struct ast_context *con, const char *value, const char *registrar);
97 void ast_context_add_include2(struct ast_context *con, const char *value, const char *registrar);
98 void ast_context_add_switch2(struct ast_context *con, const char *value, const char *data, int eval, const char *registrar);
99 void ast_merge_contexts_and_delete(void);
100 void ast_context_verify_includes(void);
101 struct ast_context * ast_walk_contexts(void);
102 void ast_cli_unregister_multiple(void);
103 void ast_context_destroy(void);
104 void ast_log(int level, const char *file, int line, const char *function, const char *fmt, ...);
105 char *ast_process_quotes_and_slashes(char *start, char find, char replace_with);
106 void ast_verbose(const char *fmt, ...);
107 struct ast_app *pbx_findapp(const char *app);
108 void filter_leading_space_from_exprs(char *str);
109 void filter_newlines(char *str);
110 static int quiet = 0;
111 static int no_comp = 0;
112 static int use_curr_dir = 0;
113 static int dump_extensions = 0;
114 static int FIRST_TIME = 0;
115 static FILE *dumpfile;
117 void ast_log(int level, const char *file, int line, const char *function, const char *fmt, ...)
122 printf("LOG: lev:%d file:%s line:%d func: %s ",
123 level, file, line, function);
129 struct ast_exten *pbx_find_extension(struct ast_channel *chan,
130 struct ast_context *bypass,
131 struct pbx_find_info *q,
136 const char *callerid,
137 enum ext_match_t action);
139 struct ast_exten *pbx_find_extension(struct ast_channel *chan,
140 struct ast_context *bypass,
141 struct pbx_find_info *q,
146 const char *callerid,
147 enum ext_match_t action)
149 return localized_find_extension(bypass, q, context, exten, priority, label, callerid, action);
152 struct ast_app *pbx_findapp(const char *app)
154 return (struct ast_app*)1; /* so as not to trigger an error */
157 struct ast_custom_function *ast_custom_function_find(const char *name);
160 struct ast_custom_function *ast_custom_function_find(const char *name)
162 return 0; /* in "standalone" mode, functions are just not avail */
165 void ast_register_file_version(const char *file, const char *version)
169 void ast_unregister_file_version(const char *file)
173 int ast_add_profile(const char *x, uint64_t scale)
176 printf("Executed ast_add_profile();\n");
181 int ast_loader_register(int (*updater)(void))
186 int ast_loader_unregister(int (*updater)(void))
190 void ast_module_register(const struct ast_module_info *x)
194 void ast_module_unregister(const struct ast_module_info *x)
199 void ast_cli_register_multiple(void)
202 printf("Executed ast_cli_register_multiple();\n");
205 void pbx_substitute_variables_helper(struct ast_channel *c,const char *cp1,char *cp2,int count);
206 void pbx_substitute_variables_helper(struct ast_channel *c,const char *cp1,char *cp2,int count)
209 strncpy(cp2,cp1,AST_MAX_EXTENSION); /* Right now, this routine is ONLY being called for
210 a possible var substitution on extension names,
216 int ast_add_extension2(struct ast_context *con,
217 int replace, const char *extension, int priority, const char *label, const char *callerid,
218 const char *application, void *data, void (*datad)(void *),
219 const char *registrar)
222 con->extension_count++;
223 if (strcmp(extension,last_exten) != 0) {
225 strcpy(last_exten, extension);
234 application = "(null)";
238 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",
239 con->name, replace, extension, priority, label, callerid, application, (data?(char*)data:"(null)"), registrar);
241 if( dump_extensions && dumpfile ) {
243 char *data2,*data3=0;
250 fprintf(dumpfile,"[globals]\n");
252 for(n=globalvars;n;n=n->next) {
253 fprintf(dumpfile, "%s\n", n->name);
257 /* print out each extension , possibly the context header also */
258 if( con != last_context ) {
259 fprintf(dumpfile,"\n\n[%s]\n", con->name);
261 for(n=con->ignorepats;n;n=n->next) {
262 fprintf(dumpfile, "ignorepat => %s\n", n->name);
264 for(n=con->includes;n;n=n->next) {
265 fprintf(dumpfile, "include => %s\n", n->name);
267 for(n=con->switches;n;n=n->next) {
268 fprintf(dumpfile, "switch => %s/%s\n", n->name, n->name2);
270 for(n=con->eswitches;n;n=n->next) {
271 fprintf(dumpfile, "eswitch => %s/%s\n", n->name, n->name2);
276 filter_newlines((char*)data);
277 filter_leading_space_from_exprs((char*)data);
279 /* compiling turns commas into vertical bars in the app data, and also removes the backslash from before escaped commas;
280 we have to restore the escaping backslash in front of any commas; the vertical bars are OK to leave as-is */
281 for (data2 = data; *data2; data2++) {
283 commacount++; /* we need to know how much bigger the string will grow-- one backslash for each comma */
289 data2 = (char*)malloc(strlen(data)+commacount+1);
295 *d4++ = '\\'; /* put a backslash in front of each comma */
298 *d4++ = *d3++; /* or just copy the char */
300 *d4++ = 0; /* cap off the new string */
305 if( strcmp(label,"(null)") != 0 )
306 fprintf(dumpfile,"exten => %s,%d(%s),%s(%s)\n", extension, priority, label, application, (char*)data);
308 fprintf(dumpfile,"exten => %s,%d,%s(%s)\n", extension, priority, application, (char*)data);
313 data = data3; /* restore data to pre-messedup state */
318 if( strcmp(label,"(null)") != 0 )
319 fprintf(dumpfile,"exten => %s,%d(%s),%s\n", extension, priority, label, application);
321 fprintf(dumpfile,"exten => %s,%d,%s\n", extension, priority, application);
325 /* since add_extension2 is responsible for the malloc'd data stuff */
331 void pbx_builtin_setvar(void *chan, void *data)
333 struct namelist *x = create_name(data);
335 printf("Executed pbx_builtin_setvar(chan, data=%s);\n", (char*)data);
337 if( dump_extensions ) {
338 x = create_name(data);
339 ADD_LAST(globalvars,x);
344 struct ast_context * ast_context_create(void **extcontexts, const char *name, const char *registrar)
346 struct ast_context *x = calloc(1, sizeof(*x));
349 x->next = context_list;
352 printf("Executed ast_context_create(conts, name=%s, registrar=%s);\n", name, registrar);
354 strncpy(x->name, name, sizeof(x->name) - 1);
355 strncpy(x->registrar, registrar, sizeof(x->registrar) - 1);
359 struct ast_context * ast_context_find_or_create(void **extcontexts, const char *name, const char *registrar)
361 struct ast_context *x = calloc(1, sizeof(*x));
364 x->next = context_list;
367 printf("Executed ast_context_find_or_create(conts, name=%s, registrar=%s);\n", name, registrar);
369 strncpy(x->name, name, sizeof(x->name) - 1);
370 strncpy(x->registrar, registrar, sizeof(x->registrar) - 1);
374 void ast_context_add_ignorepat2(struct ast_context *con, const char *value, const char *registrar)
377 printf("Executed ast_context_add_ignorepat2(con, value=%s, registrar=%s);\n", value, registrar);
378 if( dump_extensions ) {
380 x = create_name(value);
381 ADD_LAST(con->ignorepats,x);
385 void ast_context_add_include2(struct ast_context *con, const char *value, const char *registrar)
388 printf("Executed ast_context_add_include2(con, value=%s, registrar=%s);\n", value, registrar);
389 if( dump_extensions ) {
391 x = create_name((char*)value);
392 ADD_LAST(con->includes,x);
396 void ast_context_add_switch2(struct ast_context *con, const char *value, const char *data, int eval, const char *registrar)
399 printf("Executed ast_context_add_switch2(con, value=%s, data=%s, eval=%d, registrar=%s);\n", value, data, eval, registrar);
400 if( dump_extensions ) {
402 x = create_name((char*)value);
403 strncpy(x->name2,data,100);
406 ADD_LAST(con->switches,x);
410 ADD_LAST(con->eswitches,x);
415 void ast_merge_contexts_and_delete(void)
418 printf("Executed ast_merge_contexts_and_delete();\n");
421 void ast_context_verify_includes(void)
424 printf("Executed ast_context_verify_includes();\n");
427 struct ast_context * ast_walk_contexts(void)
430 printf("Executed ast_walk_contexts();\n");
434 void ast_cli_unregister_multiple(void)
437 printf("Executed ast_cli_unregister_multiple();\n");
440 void ast_context_destroy(void)
443 printf("Executed ast_context_destroy();\n");
446 void filter_leading_space_from_exprs(char *str)
448 /* Mainly for aesthetics */
449 char *t, *v, *u = str;
453 if( *u == '$' && *(u+1) == '[' ) {
455 while( *t == '\n' || *t == '\r' || *t == '\t' || *t == ' ' ) {
468 void filter_newlines(char *str)
470 /* remove all newlines, returns */
473 if( *t == '\n' || *t == '\r' ) {
474 *t = ' '; /* just replace newlines and returns with spaces; they act as
475 token separators, and just blindly removing them could be
483 extern struct module_symbols mod_data;
484 int ael_external_load_module(void);
487 int main(int argc, char **argv)
491 struct ast_context *lp,*lp2;
493 for(i=1;i<argc;i++) {
494 if( argv[i][0] == '-' && argv[i][1] == 'n' )
496 if( argv[i][0] == '-' && argv[i][1] == 'q' ) {
500 if( argv[i][0] == '-' && argv[i][1] == 'd' )
502 if( argv[i][0] == '-' && argv[i][1] == 'w' )
507 printf("\n(If you find progress and other non-error messages irritating, you can use -q to suppress them)\n");
509 printf("\n(You can use the -n option if you aren't interested in seeing all the instructions generated by the compiler)\n\n");
511 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");
512 if( !dump_extensions )
513 printf("\n(You can use the -w option to dump extensions.conf format to extensions.conf.aeldump)\n");
517 strcpy(ast_config_AST_CONFIG_DIR, ".");
518 localized_use_local_dir();
521 strcpy(ast_config_AST_CONFIG_DIR, "/etc/asterisk");
522 localized_use_conf_dir();
524 strcpy(ast_config_AST_VAR_DIR, "/var/lib/asterisk");
526 if( dump_extensions ) {
527 dumpfile = fopen("extensions.conf.aeldump","w");
529 printf("\n\nSorry, cannot open extensions.conf.aeldump for writing! Correct the situation and try again!\n\n");
537 ael_external_load_module();
539 ast_log(4, "ael2_parse", __LINE__, "main", "%d contexts, %d extensions, %d priorities\n", conts, extens, priors);
541 if( dump_extensions && dumpfile ) {
543 for( lp = context_list; lp; lp = lp->next ) { /* print out any contexts that didn't have any
544 extensions in them */
545 if( lp->extension_count == 0 ) {
547 fprintf(dumpfile,"\n\n[%s]\n", lp->name);
549 for(n=lp->ignorepats;n;n=n->next) {
550 fprintf(dumpfile, "ignorepat => %s\n", n->name);
552 for(n=lp->includes;n;n=n->next) {
553 fprintf(dumpfile, "include => %s\n", n->name);
555 for(n=lp->switches;n;n=n->next) {
556 fprintf(dumpfile, "switch => %s/%s\n", n->name, n->name2);
558 for(n=lp->eswitches;n;n=n->next) {
559 fprintf(dumpfile, "eswitch => %s/%s\n", n->name, n->name2);
565 if( dump_extensions && dumpfile )
568 for( lp = context_list; lp; lp = lp2 ) { /* free the ast_context structs */
572 destroy_namelist(lp->includes);
573 destroy_namelist(lp->ignorepats);
574 destroy_namelist(lp->switches);
575 destroy_namelist(lp->eswitches);