be a little more consistent with our variable usage
[asterisk/asterisk.git] / utils / ael_main.c
index a511f61..225e23b 100644 (file)
@@ -1,6 +1,9 @@
+#include "asterisk/autoconfig.h"
+
 #include <sys/types.h>
 #include <stdio.h>
 #include <stdlib.h>
+#include <stdarg.h>
 #include <string.h>
 #include <locale.h>
 #include <ctype.h>
 #include <regex.h>
 #include <limits.h>
 
-/* ast_copy_string */
-#define AST_API_MODULE
-#include "asterisk/strings.h"
-
-/* ensure that _ast_calloc works */
-#define AST_API_MODULE 
-#include "asterisk/utils.h"
-
 #include "asterisk/ast_expr.h"
-#include "asterisk/logger.h"
-#include "asterisk/module.h"
 #include "asterisk/channel.h"
+#include "asterisk/module.h"
 #include "asterisk/app.h"
 #include "asterisk/ael_structs.h"
 
-#define AST_CONFIG_MAX_PATH 255
+struct namelist
+{
+       char name[100];
+       char name2[100];
+       struct namelist *next;
+};
+
+struct ast_context 
+{
+       int extension_count;
+       char name[100];
+       char registrar[100];
+       struct namelist *includes;
+       struct namelist *ignorepats;
+       struct namelist *switches;
+       struct namelist *eswitches;
+
+       struct namelist *includes_last;
+       struct namelist *ignorepats_last;
+       struct namelist *switches_last;
+       struct namelist *eswitches_last;
+
+       struct ast_context *next;
+};
+
+#define ADD_LAST(headptr,memptr) if(!headptr){ headptr=(memptr); (headptr##_last)=(memptr);} else {(headptr##_last)->next = (memptr); (headptr##_last) = (memptr);}
+
+void destroy_namelist(struct namelist *x);
+void destroy_namelist(struct namelist *x)
+{
+       struct namelist *z,*z2;
+       for(z=x; z; z = z2)
+       {
+               z2 = z->next;
+               z->next = 0;
+               free(z);
+       }
+}
+
+struct namelist *create_name(char *name);
+struct namelist *create_name(char *name)
+{
+       struct namelist *x = (struct namelist *)calloc(sizeof(struct namelist),1);
+       strncpy(x->name,name,100);
+       return x;
+}
+
+struct ast_context *context_list;
+struct ast_context *last_context;
+struct namelist *globalvars;
+struct namelist *globalvars_last;
 
 int conts=0, extens=0, priors=0;
 char last_exten[18000];
-char ast_config_AST_CONFIG_DIR[AST_CONFIG_MAX_PATH];
-char ast_config_AST_VAR_DIR[AST_CONFIG_MAX_PATH];
+char ast_config_AST_CONFIG_DIR[PATH_MAX];
+char ast_config_AST_VAR_DIR[PATH_MAX];
 
+void ast_add_profile(void);
 void ast_cli_register_multiple(void);
 void ast_register_file_version(void);
 void ast_unregister_file_version(void);
-int ast_add_extension2(void *con,
+int ast_add_extension2(struct ast_context *con,
                                           int replace, const char *extension, int priority, const char *label, const char *callerid,
                                                const char *application, void *data, void (*datad)(void *),
                                           const char *registrar);
 void pbx_builtin_setvar(void *chan, void *data);
-void ast_context_create(void **extcontexts, const char *name, const char *registrar);
-void ast_context_add_ignorepat2(void *con, const char *value, const char *registrar);
-void ast_context_add_include2(void *con, const char *value, const char *registrar);
-void ast_context_add_switch2(void *con, const char *value, const char *data, int eval, const char *registrar);
+struct ast_context * ast_context_create(void **extcontexts, const char *name, const char *registrar);
+void ast_context_add_ignorepat2(struct ast_context *con, const char *value, const char *registrar);
+void ast_context_add_include2(struct ast_context *con, const char *value, const char *registrar);
+void ast_context_add_switch2(struct ast_context *con, const char *value, const char *data, int eval, const char *registrar);
 void ast_merge_contexts_and_delete(void);
 void ast_context_verify_includes(void);
 struct ast_context * ast_walk_contexts(void);
@@ -54,39 +99,71 @@ void ast_log(int level, const char *file, int line, const char *function, const
 char *ast_process_quotes_and_slashes(char *start, char find, char replace_with);
 void ast_verbose(const char *fmt, ...);
 struct ast_app *pbx_findapp(const char *app);
+void filter_leading_space_from_exprs(char *str);
+void filter_newlines(char *str);
+static int quiet = 0;
 static int no_comp = 0;
 static int use_curr_dir = 0;
-
+static int dump_extensions = 0;
+static int FIRST_TIME = 0;
+static FILE *dumpfile;
 
 struct ast_app *pbx_findapp(const char *app)
 {
        return (struct ast_app*)1; /* so as not to trigger an error */
 }
 
+void ast_add_profile(void)
+{
+       if (!no_comp)
+               printf("Executed ast_add_profile();\n");
+}
+
+int ast_loader_register(int (*updater)(void))
+{
+       return 1;
+}
+
+int ast_loader_unregister(int (*updater)(void))
+{
+       return 1;
+}
+void ast_module_register(const struct ast_module_info *x)
+{
+}
+
+void ast_module_unregister(const struct ast_module_info *x)
+{
+}
+
+
 void ast_cli_register_multiple(void)
 {
        if(!no_comp)
-        printf("Executed ast_cli_register_multiple();\n");
+               printf("Executed ast_cli_register_multiple();\n");
 }
 
 void ast_register_file_version(void)
 {
-       if(!no_comp)
-       printf("Executed ast_register_file_version();\n");
+       /* if(!no_comp)
+               printf("Executed ast_register_file_version();\n"); */
+       /* I'm erasing this, because I don't think anyone really ever needs to see it anyway */
 }
 
 void ast_unregister_file_version(void)
 {
-       if(!no_comp)
-       printf("Executed ast_unregister_file_version();\n");
+       /* if(!no_comp)
+               printf("Executed ast_unregister_file_version();\n"); */
+       /* I'm erasing this, because I don't think anyone really ever needs to see it anyway */
 
 }
-int ast_add_extension2(void *con,
-                                               int replace, const char *extension, int priority, const char *label, const char *callerid,
-                                               const char *application, void *data, void (*datad)(void *),
-                                               const char *registrar)
+int ast_add_extension2(struct ast_context *con,
+                       int replace, const char *extension, int priority, const char *label, const char *callerid,
+                       const char *application, void *data, void (*datad)(void *),
+                       const char *registrar)
 {
        priors++;
+       con->extension_count++;
        if (strcmp(extension,last_exten) != 0) {
                extens++;
                strcpy(last_exten, extension);
@@ -100,10 +177,61 @@ int ast_add_extension2(void *con,
        if (!application) {
                application = "(null)";
        }
-       if(!no_comp)
-       printf("Executed ast_add_extension2(con, rep=%d, exten=%s, priority=%d, label=%s, callerid=%s, appl=%s, data=%s, FREE, registrar=%s);\n",
-               replace, extension, priority, label, callerid, application, (data?(char*)data:"(null)"), registrar);
 
+       if(!no_comp)
+               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",
+                          con->name, replace, extension, priority, label, callerid, application, (data?(char*)data:"(null)"), registrar);
+
+       if( dump_extensions && dumpfile ) {
+               struct namelist *n;
+
+               if( FIRST_TIME ) {
+                       FIRST_TIME = 0;
+                       
+                       if( globalvars )
+                               fprintf(dumpfile,"[globals]\n");
+                       
+                       for(n=globalvars;n;n=n->next) {
+                               fprintf(dumpfile, "%s\n", n->name);
+                       }
+               }
+               
+               /* print out each extension , possibly the context header also */
+               if( con != last_context ) {
+                       fprintf(dumpfile,"\n\n[%s]\n", con->name);
+                       last_context = con;
+                       for(n=con->ignorepats;n;n=n->next) {
+                               fprintf(dumpfile, "ignorepat => %s\n", n->name);
+                       }
+                       for(n=con->includes;n;n=n->next) {
+                               fprintf(dumpfile, "include => %s\n", n->name);
+                       }
+                       for(n=con->switches;n;n=n->next) {
+                               fprintf(dumpfile, "switch => %s/%s\n", n->name, n->name2);
+                       }
+                       for(n=con->eswitches;n;n=n->next) {
+                               fprintf(dumpfile, "eswitch => %s/%s\n", n->name, n->name2);
+                       }
+                       
+               }
+               if( data ) {
+                       filter_newlines((char*)data);
+                       filter_leading_space_from_exprs((char*)data);
+
+                       if( strcmp(label,"(null)") != 0  )
+                               fprintf(dumpfile,"exten => %s,%d(%s),%s(%s)\n", extension, priority, label, application, (char*)data);
+                       else
+                               fprintf(dumpfile,"exten => %s,%d,%s(%s)\n", extension, priority, application, (char*)data);
+
+               } else {
+
+                       if( strcmp(label,"(null)") != 0  )
+                               fprintf(dumpfile,"exten => %s,%d(%s),%s\n", extension, priority, label, application);
+                       else
+                               fprintf(dumpfile,"exten => %s,%d,%s\n", extension, priority, application);
+               }
+       }
+       
        /* since add_extension2 is responsible for the malloc'd data stuff */
        if( data )
                free(data);
@@ -112,76 +240,113 @@ int ast_add_extension2(void *con,
 
 void pbx_builtin_setvar(void *chan, void *data)
 {
+       struct namelist *x = create_name((char*)data);
        if(!no_comp)
-       printf("Executed pbx_builtin_setvar(chan, data=%s);\n", (char*)data);
+               printf("Executed pbx_builtin_setvar(chan, data=%s);\n", (char*)data);
+
+       if( dump_extensions ) {
+               x = create_name((char*)data);
+               ADD_LAST(globalvars,x);
+       }
 }
        
 
-void ast_context_create(void **extcontexts, const char *name, const char *registrar)
+struct ast_context * ast_context_create(void **extcontexts, const char *name, const char *registrar)
 {
+       struct ast_context *x = (struct ast_context *)calloc(sizeof(struct ast_context),1);
+       x->next = context_list;
+       context_list = x;
        if(!no_comp)
-       printf("Executed ast_context_create(conts, name=%s, registrar=%s);\n", name, registrar);
+               printf("Executed ast_context_create(conts, name=%s, registrar=%s);\n", name, registrar);
        conts++;
+       strncpy(x->name,name,100);
+       strncpy(x->registrar,registrar,100);
+       return x;
 }
 
-void ast_context_add_ignorepat2(void *con, const char *value, const char *registrar)
+void ast_context_add_ignorepat2(struct ast_context *con, const char *value, const char *registrar)
 {
        if(!no_comp)
-       printf("Executed ast_context_add_ignorepat2(con, value=%s, registrar=%s);\n", value, registrar);
+               printf("Executed ast_context_add_ignorepat2(con, value=%s, registrar=%s);\n", value, registrar);
+       if( dump_extensions ) {
+               struct namelist *x;
+               x = create_name((char*)value);
+               ADD_LAST(con->ignorepats,x);
+       }
 }
 
-void ast_context_add_include2(void *con, const char *value, const char *registrar)
+void ast_context_add_include2(struct ast_context *con, const char *value, const char *registrar)
 {
        if(!no_comp)
-       printf("Executed ast_context_add_include2(con, value=%s, registrar=%s);\n", value, registrar);
+               printf("Executed ast_context_add_include2(con, value=%s, registrar=%s);\n", value, registrar);
+       if( dump_extensions ) {
+               struct namelist *x;
+               x = create_name((char*)value);
+               ADD_LAST(con->includes,x);
+       }
 }
 
-void ast_context_add_switch2(void *con, const char *value, const char *data, int eval, const char *registrar)
+void ast_context_add_switch2(struct ast_context *con, const char *value, const char *data, int eval, const char *registrar)
 {
        if(!no_comp)
-       printf("Executed ast_context_add_switch2(con, value=%s, data=%s, eval=%d, registrar=%s);\n", value, data, eval, registrar);
+               printf("Executed ast_context_add_switch2(con, value=%s, data=%s, eval=%d, registrar=%s);\n", value, data, eval, registrar);
+       if( dump_extensions ) {
+               struct namelist *x;
+               x = create_name((char*)value);
+               strncpy(x->name2,data,100);
+               if( eval ) {
+
+                       ADD_LAST(con->switches,x);
+
+               } else {
+
+                       ADD_LAST(con->eswitches,x);
+               }
+       }
 }
 
 void ast_merge_contexts_and_delete(void)
 {
        if(!no_comp)
-       printf("Executed ast_merge_contexts_and_delete();\n");
+               printf("Executed ast_merge_contexts_and_delete();\n");
 }
 
 void ast_context_verify_includes(void)
 {
        if(!no_comp)
-       printf("Executed ast_context_verify_includes();\n");
+               printf("Executed ast_context_verify_includes();\n");
 }
 
 struct ast_context * ast_walk_contexts(void)
 {
        if(!no_comp)
-       printf("Executed ast_walk_contexts();\n");
+               printf("Executed ast_walk_contexts();\n");
        return 0;
 }
 
 void ast_cli_unregister_multiple(void)
 {
        if(!no_comp)
-       printf("Executed ast_cli_unregister_multiple();\n");
+               printf("Executed ast_cli_unregister_multiple();\n");
 }
 
 void ast_context_destroy(void)
 {
-       printf("Executed ast_context_destroy();\n");
+       if( !no_comp)
+               printf("Executed ast_context_destroy();\n");
 }
 
 void ast_log(int level, const char *file, int line, const char *function, const char *fmt, ...)
 {
         va_list vars;
         va_start(vars,fmt);
-
-        printf("LOG: lev:%d file:%s  line:%d func: %s  ",
+       if( !quiet || level > 2 ) {
+           printf("LOG: lev:%d file:%s  line:%d func: %s  ",
                    level, file, line, function);
-        vprintf(fmt, vars);
-        fflush(stdout);
-        va_end(vars);
+            vprintf(fmt, vars);
+            fflush(stdout);
+            va_end(vars);
+       }
 }
 
 void ast_verbose(const char *fmt, ...)
@@ -221,79 +386,136 @@ char *ast_process_quotes_and_slashes(char *start, char find, char replace_with)
         return dataPut;
 }
 
-extern struct module_symbols mod_data;
+void filter_leading_space_from_exprs(char *str)
+{
+       /*  Mainly for aesthetics */
+       char *t, *v, *u = str;
+       
+       while ( u && *u ) {
+
+               if( *u == '$' && *(u+1) == '[' ) {
+                       t = u+2;
+                       while( *t == '\n' || *t == '\r' || *t == '\t' || *t == ' ' ) {
+                               v = t;
+                               while ( *v ) {
+                                       *v = *(v+1);
+                                       v++;
+                               }
+                       }
+               }
+               
+               u++;
+       }
+}
 
+void filter_newlines(char *str)
+{
+       /* remove all newlines, returns  */
+       char *t=str;
+       while( t && *t ) {
+               if( *t == '\n' || *t == '\r' ) {
+                       *t = ' '; /* just replace newlines and returns with spaces; they act as
+                                                token separators, and just blindly removing them could be
+                                                harmful. */
+               }
+               t++;
+       }
+}
+
+
+extern struct module_symbols mod_data;
+extern int ael_external_load_module(void);
 
 int main(int argc, char **argv)
 {
        int i;
+       struct namelist *n;
+       struct ast_context *lp,*lp2;
        
-       for(i=1;i<argc;i++)
-       {
+       for(i=1;i<argc;i++) {
                if( argv[i][0] == '-' && argv[i][1] == 'n' )
                        no_comp =1;
+               if( argv[i][0] == '-' && argv[i][1] == 'q' ) {
+                       quiet = 1;
+                       no_comp =1;
+               }
                if( argv[i][0] == '-' && argv[i][1] == 'd' )
                        use_curr_dir =1;
+               if( argv[i][0] == '-' && argv[i][1] == 'w' )
+                       dump_extensions =1;
        }
        
-               
-       if( !no_comp )
-               printf("\n(You can use the -n option if you aren't interested in seeing all the instructions generated by the compiler)\n\n");
-       if( !use_curr_dir )
-               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");
-       
-       if( use_curr_dir )
-       {
+       if( !quiet ) {
+               printf("\n(If you find progress and other non-error messages irritating, you can use -q to suppress them)\n");
+               if( !no_comp )
+                       printf("\n(You can use the -n option if you aren't interested in seeing all the instructions generated by the compiler)\n\n");
+               if( !use_curr_dir )
+                       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");
+               if( !dump_extensions )
+                       printf("\n(You can use the -w option to dump extensions.conf format to extensions.conf.aeldump)\n");
+       }
+
+       if( use_curr_dir ) {
                strcpy(ast_config_AST_CONFIG_DIR, ".");
        }
-       else
-       {
+       else {
                strcpy(ast_config_AST_CONFIG_DIR, "/etc/asterisk");
        }
        strcpy(ast_config_AST_VAR_DIR, "/var/lib/asterisk");
        
-       mod_data.load_module(0);
+       if( dump_extensions ) {
+               dumpfile = fopen("extensions.conf.aeldump","w");
+               if( !dumpfile ) {
+                       printf("\n\nSorry, cannot open extensions.conf.aeldump for writing! Correct the situation and try again!\n\n");
+                       exit(10);
+               }
+               
+       }
+
+       FIRST_TIME = 1;
        
-       ast_log(4, "ael2_parse", __LINE__, "main", "%d contexts, %d extensions, %d priorities\n", conts, extens, priors);
+       ael_external_load_module();
        
-    return 0;
-}
-
-/*
- * XXX the code below is replicated here from utils.c, because
- * the #define AST_API_MODULE references functions that are
- * not available on all platforms.
- * We hit the problem with strndup (which in turn uses strnlen),
- * but it is possible that there are more of these issues.
- *
- * When utils.c is properly split and functions become available
- * through a library, this file will just link to the library and
- * the problem will go away together with the code below.
- */
-#ifndef HAVE_STRNLEN
-size_t strnlen(const char *s, size_t n)
-{
-        size_t len;   
-
-        for (len=0; len < n; len++)
-                if (s[len] == '\0')
-                        break;
-
-        return len;
-}
-#endif /* !HAVE_STRNLEN */
+       ast_log(4, "ael2_parse", __LINE__, "main", "%d contexts, %d extensions, %d priorities\n", conts, extens, priors);
 
-#if !defined(HAVE_STRNDUP) && !defined(__AST_DEBUG_MALLOC)
-char *strndup(const char *s, size_t n)
-{
-        size_t len = strnlen(s, n);
-        char *new = malloc(len + 1);
+       if( dump_extensions && dumpfile ) {
+       
+               for( lp = context_list; lp; lp = lp->next ) { /* print out any contexts that didn't have any
+                                                                                                                extensions in them */
+                       if( lp->extension_count == 0 ) {
+                               
+                               fprintf(dumpfile,"\n\n[%s]\n", lp->name);
+                               
+                               for(n=lp->ignorepats;n;n=n->next) {
+                                       fprintf(dumpfile, "ignorepat => %s\n", n->name);
+                               }
+                               for(n=lp->includes;n;n=n->next) {
+                                       fprintf(dumpfile, "include => %s\n", n->name);
+                               }
+                               for(n=lp->switches;n;n=n->next) {
+                                       fprintf(dumpfile, "switch => %s/%s\n", n->name, n->name2);
+                               }
+                               for(n=lp->eswitches;n;n=n->next) {
+                                       fprintf(dumpfile, "eswitch => %s/%s\n", n->name, n->name2);
+                               }
+                       }
+               }
+       }
+       
+       if( dump_extensions && dumpfile )
+               fclose(dumpfile);
+       
+       for( lp = context_list; lp; lp = lp2 ) { /* free the ast_context structs */
+               lp2 = lp->next;
+               lp->next = 0;
 
-        if (!new)
-                return NULL;
+               destroy_namelist(lp->includes);
+               destroy_namelist(lp->ignorepats);
+               destroy_namelist(lp->switches);
+               destroy_namelist(lp->eswitches);
 
-        new[len] = '\0';
-        return memcpy(new, s, len);
+               free(lp);
+       }
+       
+    return 0;
 }
-#endif /* !defined(HAVE_STRNDUP) && !defined(__AST_DEBUG_MALLOC) */
-