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"
22 struct namelist *next;
30 struct namelist *includes;
31 struct namelist *ignorepats;
32 struct namelist *switches;
33 struct namelist *eswitches;
35 struct namelist *includes_last;
36 struct namelist *ignorepats_last;
37 struct namelist *switches_last;
38 struct namelist *eswitches_last;
40 struct ast_context *next;
43 #define ADD_LAST(headptr,memptr) if(!headptr){ headptr=(memptr); (headptr##_last)=(memptr);} else {(headptr##_last)->next = (memptr); (headptr##_last) = (memptr);}
45 void destroy_namelist(struct namelist *x);
46 void destroy_namelist(struct namelist *x)
48 struct namelist *z,*z2;
57 struct namelist *create_name(char *name);
58 struct namelist *create_name(char *name)
60 struct namelist *x = calloc(1, sizeof(*x));
63 strncpy(x->name, name, sizeof(x->name) - 1);
67 struct ast_context *context_list;
68 struct ast_context *last_context;
69 struct namelist *globalvars;
70 struct namelist *globalvars_last;
72 int conts=0, extens=0, priors=0;
73 char last_exten[18000];
74 char ast_config_AST_CONFIG_DIR[PATH_MAX];
75 char ast_config_AST_VAR_DIR[PATH_MAX];
77 void ast_add_profile(void);
78 void ast_cli_register_multiple(void);
79 void ast_register_file_version(void);
80 void ast_unregister_file_version(void);
81 int ast_add_extension2(struct ast_context *con,
82 int replace, const char *extension, int priority, const char *label, const char *callerid,
83 const char *application, void *data, void (*datad)(void *),
84 const char *registrar);
85 void pbx_builtin_setvar(void *chan, void *data);
86 struct ast_context * ast_context_create(void **extcontexts, const char *name, const char *registrar);
87 void ast_context_add_ignorepat2(struct ast_context *con, const char *value, const char *registrar);
88 void ast_context_add_include2(struct ast_context *con, const char *value, const char *registrar);
89 void ast_context_add_switch2(struct ast_context *con, const char *value, const char *data, int eval, const char *registrar);
90 void ast_merge_contexts_and_delete(void);
91 void ast_context_verify_includes(void);
92 struct ast_context * ast_walk_contexts(void);
93 void ast_cli_unregister_multiple(void);
94 void ast_context_destroy(void);
95 void ast_log(int level, const char *file, int line, const char *function, const char *fmt, ...);
96 char *ast_process_quotes_and_slashes(char *start, char find, char replace_with);
97 void ast_verbose(const char *fmt, ...);
98 struct ast_app *pbx_findapp(const char *app);
99 void filter_leading_space_from_exprs(char *str);
100 void filter_newlines(char *str);
101 static int quiet = 0;
102 static int no_comp = 0;
103 static int use_curr_dir = 0;
104 static int dump_extensions = 0;
105 static int FIRST_TIME = 0;
106 static FILE *dumpfile;
108 struct ast_app *pbx_findapp(const char *app)
110 return (struct ast_app*)1; /* so as not to trigger an error */
113 void ast_add_profile(void)
116 printf("Executed ast_add_profile();\n");
119 int ast_loader_register(int (*updater)(void))
124 int ast_loader_unregister(int (*updater)(void))
128 void ast_module_register(const struct ast_module_info *x)
132 void ast_module_unregister(const struct ast_module_info *x)
137 void ast_cli_register_multiple(void)
140 printf("Executed ast_cli_register_multiple();\n");
143 void ast_register_file_version(void)
146 printf("Executed ast_register_file_version();\n"); */
147 /* I'm erasing this, because I don't think anyone really ever needs to see it anyway */
150 void ast_unregister_file_version(void)
153 printf("Executed ast_unregister_file_version();\n"); */
154 /* I'm erasing this, because I don't think anyone really ever needs to see it anyway */
157 int ast_add_extension2(struct ast_context *con,
158 int replace, const char *extension, int priority, const char *label, const char *callerid,
159 const char *application, void *data, void (*datad)(void *),
160 const char *registrar)
163 con->extension_count++;
164 if (strcmp(extension,last_exten) != 0) {
166 strcpy(last_exten, extension);
175 application = "(null)";
179 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",
180 con->name, replace, extension, priority, label, callerid, application, (data?(char*)data:"(null)"), registrar);
182 if( dump_extensions && dumpfile ) {
189 fprintf(dumpfile,"[globals]\n");
191 for(n=globalvars;n;n=n->next) {
192 fprintf(dumpfile, "%s\n", n->name);
196 /* print out each extension , possibly the context header also */
197 if( con != last_context ) {
198 fprintf(dumpfile,"\n\n[%s]\n", con->name);
200 for(n=con->ignorepats;n;n=n->next) {
201 fprintf(dumpfile, "ignorepat => %s\n", n->name);
203 for(n=con->includes;n;n=n->next) {
204 fprintf(dumpfile, "include => %s\n", n->name);
206 for(n=con->switches;n;n=n->next) {
207 fprintf(dumpfile, "switch => %s/%s\n", n->name, n->name2);
209 for(n=con->eswitches;n;n=n->next) {
210 fprintf(dumpfile, "eswitch => %s/%s\n", n->name, n->name2);
215 filter_newlines((char*)data);
216 filter_leading_space_from_exprs((char*)data);
218 if( strcmp(label,"(null)") != 0 )
219 fprintf(dumpfile,"exten => %s,%d(%s),%s(%s)\n", extension, priority, label, application, (char*)data);
221 fprintf(dumpfile,"exten => %s,%d,%s(%s)\n", extension, priority, application, (char*)data);
225 if( strcmp(label,"(null)") != 0 )
226 fprintf(dumpfile,"exten => %s,%d(%s),%s\n", extension, priority, label, application);
228 fprintf(dumpfile,"exten => %s,%d,%s\n", extension, priority, application);
232 /* since add_extension2 is responsible for the malloc'd data stuff */
238 void pbx_builtin_setvar(void *chan, void *data)
240 struct namelist *x = create_name((char*)data);
242 printf("Executed pbx_builtin_setvar(chan, data=%s);\n", (char*)data);
244 if( dump_extensions ) {
245 x = create_name((char*)data);
246 ADD_LAST(globalvars,x);
251 struct ast_context * ast_context_create(void **extcontexts, const char *name, const char *registrar)
253 struct ast_context *x = calloc(1, sizeof(*x));
256 x->next = context_list;
259 printf("Executed ast_context_create(conts, name=%s, registrar=%s);\n", name, registrar);
261 strncpy(x->name, name, sizeof(x->name) - 1);
262 strncpy(x->registrar, registrar, sizeof(x->registrar) - 1);
266 void ast_context_add_ignorepat2(struct ast_context *con, const char *value, const char *registrar)
269 printf("Executed ast_context_add_ignorepat2(con, value=%s, registrar=%s);\n", value, registrar);
270 if( dump_extensions ) {
272 x = create_name((char*)value);
273 ADD_LAST(con->ignorepats,x);
277 void ast_context_add_include2(struct ast_context *con, const char *value, const char *registrar)
280 printf("Executed ast_context_add_include2(con, value=%s, registrar=%s);\n", value, registrar);
281 if( dump_extensions ) {
283 x = create_name((char*)value);
284 ADD_LAST(con->includes,x);
288 void ast_context_add_switch2(struct ast_context *con, const char *value, const char *data, int eval, const char *registrar)
291 printf("Executed ast_context_add_switch2(con, value=%s, data=%s, eval=%d, registrar=%s);\n", value, data, eval, registrar);
292 if( dump_extensions ) {
294 x = create_name((char*)value);
295 strncpy(x->name2,data,100);
298 ADD_LAST(con->switches,x);
302 ADD_LAST(con->eswitches,x);
307 void ast_merge_contexts_and_delete(void)
310 printf("Executed ast_merge_contexts_and_delete();\n");
313 void ast_context_verify_includes(void)
316 printf("Executed ast_context_verify_includes();\n");
319 struct ast_context * ast_walk_contexts(void)
322 printf("Executed ast_walk_contexts();\n");
326 void ast_cli_unregister_multiple(void)
329 printf("Executed ast_cli_unregister_multiple();\n");
332 void ast_context_destroy(void)
335 printf("Executed ast_context_destroy();\n");
338 void ast_log(int level, const char *file, int line, const char *function, const char *fmt, ...)
342 if( !quiet || level > 2 ) {
343 printf("LOG: lev:%d file:%s line:%d func: %s ",
344 level, file, line, function);
351 void ast_verbose(const char *fmt, ...)
362 char *ast_process_quotes_and_slashes(char *start, char find, char replace_with)
364 char *dataPut = start;
368 for (; *start; start++) {
370 *dataPut++ = *start; /* Always goes verbatim */
373 if (*start == '\\') {
374 inEscape = 1; /* Do not copy \ into the data */
375 } else if (*start == '\'') {
376 inQuotes = 1-inQuotes; /* Do not copy ' into the data */
378 /* Replace , with |, unless in quotes */
379 *dataPut++ = inQuotes ? *start : ((*start==find) ? replace_with : *start);
383 if (start != dataPut)
388 void filter_leading_space_from_exprs(char *str)
390 /* Mainly for aesthetics */
391 char *t, *v, *u = str;
395 if( *u == '$' && *(u+1) == '[' ) {
397 while( *t == '\n' || *t == '\r' || *t == '\t' || *t == ' ' ) {
410 void filter_newlines(char *str)
412 /* remove all newlines, returns */
415 if( *t == '\n' || *t == '\r' ) {
416 *t = ' '; /* just replace newlines and returns with spaces; they act as
417 token separators, and just blindly removing them could be
425 extern struct module_symbols mod_data;
426 extern int ael_external_load_module(void);
428 int main(int argc, char **argv)
432 struct ast_context *lp,*lp2;
434 for(i=1;i<argc;i++) {
435 if( argv[i][0] == '-' && argv[i][1] == 'n' )
437 if( argv[i][0] == '-' && argv[i][1] == 'q' ) {
441 if( argv[i][0] == '-' && argv[i][1] == 'd' )
443 if( argv[i][0] == '-' && argv[i][1] == 'w' )
448 printf("\n(If you find progress and other non-error messages irritating, you can use -q to suppress them)\n");
450 printf("\n(You can use the -n option if you aren't interested in seeing all the instructions generated by the compiler)\n\n");
452 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");
453 if( !dump_extensions )
454 printf("\n(You can use the -w option to dump extensions.conf format to extensions.conf.aeldump)\n");
458 strcpy(ast_config_AST_CONFIG_DIR, ".");
461 strcpy(ast_config_AST_CONFIG_DIR, "/etc/asterisk");
463 strcpy(ast_config_AST_VAR_DIR, "/var/lib/asterisk");
465 if( dump_extensions ) {
466 dumpfile = fopen("extensions.conf.aeldump","w");
468 printf("\n\nSorry, cannot open extensions.conf.aeldump for writing! Correct the situation and try again!\n\n");
476 ael_external_load_module();
478 ast_log(4, "ael2_parse", __LINE__, "main", "%d contexts, %d extensions, %d priorities\n", conts, extens, priors);
480 if( dump_extensions && dumpfile ) {
482 for( lp = context_list; lp; lp = lp->next ) { /* print out any contexts that didn't have any
483 extensions in them */
484 if( lp->extension_count == 0 ) {
486 fprintf(dumpfile,"\n\n[%s]\n", lp->name);
488 for(n=lp->ignorepats;n;n=n->next) {
489 fprintf(dumpfile, "ignorepat => %s\n", n->name);
491 for(n=lp->includes;n;n=n->next) {
492 fprintf(dumpfile, "include => %s\n", n->name);
494 for(n=lp->switches;n;n=n->next) {
495 fprintf(dumpfile, "switch => %s/%s\n", n->name, n->name2);
497 for(n=lp->eswitches;n;n=n->next) {
498 fprintf(dumpfile, "eswitch => %s/%s\n", n->name, n->name2);
504 if( dump_extensions && dumpfile )
507 for( lp = context_list; lp; lp = lp2 ) { /* free the ast_context structs */
511 destroy_namelist(lp->includes);
512 destroy_namelist(lp->ignorepats);
513 destroy_namelist(lp->switches);
514 destroy_namelist(lp->eswitches);