pbx: Create pbx_ignorepat.c for management of 'struct ast_ignorepat'.
[asterisk/asterisk.git] / utils / ael_main.c
index f32e0b5..d2c815b 100644 (file)
@@ -1,19 +1,45 @@
-#include <sys/types.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <stdarg.h>
-#include <string.h>
+/*
+ * XXX this file probably need a fair amount of cleanup, at the very least:
+ *
+ * - documenting its purpose;
+ * - removing all unnecessary headers and other stuff from the sources
+ *   it was copied from;
+ * - fixing the formatting
+ */
+
+/*** MODULEINFO
+       <support_level>extended</support_level>
+ ***/
+
+#include "asterisk.h"
+
 #include <locale.h>
 #include <ctype.h>
-#include <errno.h>
 #include <regex.h>
 #include <limits.h>
 
-#include "asterisk/ast_expr.h"
+ASTERISK_REGISTER_FILE()
+
+#include "asterisk/backtrace.h"
 #include "asterisk/channel.h"
+#include "asterisk/ast_expr.h"
 #include "asterisk/module.h"
 #include "asterisk/app.h"
+#include "asterisk/lock.h"
+#include "asterisk/hashtab.h"
 #include "asterisk/ael_structs.h"
+#include "asterisk/extconf.h"
+
+int option_debug = 0;
+int option_verbose = 0;
+#if !defined(LOW_MEMORY)
+void __ast_register_file(const char *file) { }
+void __ast_unregister_file(const char *file) { }
+#endif
+
+/*** MODULEINFO
+       <depend>res_ael_share</depend>
+ ***/
 
 struct namelist
 {
@@ -22,7 +48,7 @@ struct namelist
        struct namelist *next;
 };
 
-struct ast_context 
+struct ast_context
 {
        int extension_count;
        char name[100];
@@ -54,8 +80,8 @@ void destroy_namelist(struct namelist *x)
        }
 }
 
-struct namelist *create_name(char *name);
-struct namelist *create_name(char *name)
+struct namelist *create_name(const char *name);
+struct namelist *create_name(const char *name)
 {
        struct namelist *x = calloc(1, sizeof(*x));
        if (!x)
@@ -71,19 +97,20 @@ struct namelist *globalvars_last;
 
 int conts=0, extens=0, priors=0;
 char last_exten[18000];
-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);
+static char config_dir[PATH_MAX];
+static char var_dir[PATH_MAX];
+const char *ast_config_AST_CONFIG_DIR = config_dir;
+const char *ast_config_AST_VAR_DIR = var_dir;
+
+void __ast_cli_register_multiple(void);
 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);
 struct ast_context * ast_context_create(void **extcontexts, const char *name, const char *registrar);
+struct ast_context * ast_context_find_or_create(void **extcontexts, void *tab, 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);
@@ -94,7 +121,7 @@ void ast_cli_unregister_multiple(void);
 void ast_context_destroy(void);
 void ast_log(int level, const char *file, int line, const char *function, const char *fmt, ...);
 char *ast_process_quotes_and_slashes(char *start, char find, char replace_with);
-void ast_verbose(const char *fmt, ...);
+void __ast_verbose(const char *file, int line, const char *func, int level, const char *fmt, ...);
 struct ast_app *pbx_findapp(const char *app);
 void filter_leading_space_from_exprs(char *str);
 void filter_newlines(char *str);
@@ -105,16 +132,63 @@ static int dump_extensions = 0;
 static int FIRST_TIME = 0;
 static FILE *dumpfile;
 
+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  ",
+                                                                  level, file, line, function);
+                                       vprintf(fmt, vars);
+                                               fflush(stdout);
+                                                       va_end(vars);
+}
+
+struct ast_exten *pbx_find_extension(struct ast_channel *chan,
+                                                                        struct ast_context *bypass,
+                                                                        struct pbx_find_info *q,
+                                                                        const char *context,
+                                                                        const char *exten,
+                                                                        int priority,
+                                                                        const char *label,
+                                                                        const char *callerid,
+                                                                        enum ext_match_t action);
+
+struct ast_exten *pbx_find_extension(struct ast_channel *chan,
+                                                                        struct ast_context *bypass,
+                                                                        struct pbx_find_info *q,
+                                                                        const char *context,
+                                                                        const char *exten,
+                                                                        int priority,
+                                                                        const char *label,
+                                                                        const char *callerid,
+                                                                        enum ext_match_t action)
+{
+       return localized_find_extension(bypass, q, context, exten, priority, label, callerid, action);
+}
+
 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)
+struct ast_custom_function *ast_custom_function_find(const char *name);
+
+
+struct ast_custom_function *ast_custom_function_find(const char *name)
+{
+       return 0; /* in "standalone" mode, functions are just not avail */
+}
+
+#if !defined(LOW_MEMORY)
+int ast_add_profile(const char *x, uint64_t scale)
 {
        if (!no_comp)
                printf("Executed ast_add_profile();\n");
+
+       return 0;
 }
+#endif
 
 int ast_loader_register(int (*updater)(void))
 {
@@ -134,26 +208,23 @@ void ast_module_unregister(const struct ast_module_info *x)
 }
 
 
-void ast_cli_register_multiple(void)
+void __ast_cli_register_multiple(void)
 {
        if(!no_comp)
                printf("Executed ast_cli_register_multiple();\n");
 }
 
-void ast_register_file_version(void)
+void pbx_substitute_variables_helper(struct ast_channel *c,const char *cp1,char *cp2,int count);
+void pbx_substitute_variables_helper(struct ast_channel *c,const char *cp1,char *cp2,int count)
 {
-       /* 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 */
+       if (cp1 && *cp1)
+               strncpy(cp2,cp1,AST_MAX_EXTENSION); /* Right now, this routine is ONLY being called for
+                                                                                          a possible var substitution on extension names,
+                                                                                          so....! */
+       else
+               *cp2 = 0;
 }
 
-void ast_unregister_file_version(void)
-{
-       /* 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(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 *),
@@ -181,20 +252,18 @@ int ast_add_extension2(struct ast_context *con,
 
        if( dump_extensions && dumpfile ) {
                struct namelist *n;
-               char *data2,*data3=0;
-               int commacount = 0;
 
                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);
@@ -211,49 +280,21 @@ int ast_add_extension2(struct ast_context *con,
                        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);
+                       /* in previous versions, commas were converted to '|' to separate
+                          args in app calls, but now, commas are used. There used to be
+                          code here to insert backslashes (escapes) before any commas
+                          that may have been embedded in the app args. This code is no more. */
 
-                       /* compiling turns commas into vertical bars in the app data, and also removes the backslash from before escaped commas;
-                          we have to restore the escaping backslash in front of any commas; the vertical bars are OK to leave as-is */
-                       for (data2 = data; *data2; data2++) {
-                               if (*data2 == ',')
-                                       commacount++;  /* we need to know how much bigger the string will grow-- one backslash for each comma  */
-                       }
-                       if (commacount) 
-                       {
-                               char *d3,*d4;
-                               
-                               data2 = (char*)malloc(strlen(data)+commacount+1);
-                               data3 = data;
-                               d3 = data;
-                               d4 = data2;
-                               while (*d3) {
-                                       if (*d3 == ',') {
-                                               *d4++ = '\\'; /* put a backslash in front of each comma */
-                                               *d4++ = *d3++;
-                                       } else
-                                               *d4++ = *d3++;  /* or just copy the char */
-                               }
-                               *d4++ = 0;  /* cap off the new string */
-                               data = data2;
-                       } else
-                               data2 = 0;
-                       
                        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);
 
-                       if (data2) {
-                               free(data2);
-                               data2 = 0;
-                               data = data3; /* restore data to pre-messedup state */
-                       }
-
                } else {
 
                        if( strcmp(label,"(null)") != 0  )
@@ -262,25 +303,24 @@ int ast_add_extension2(struct ast_context *con,
                                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);
+       free(data);
        return 0;
 }
 
 void pbx_builtin_setvar(void *chan, void *data)
 {
-       struct namelist *x = create_name((char*)data);
+       struct namelist *x = create_name(data);
        if(!no_comp)
                printf("Executed pbx_builtin_setvar(chan, data=%s);\n", (char*)data);
 
        if( dump_extensions ) {
-               x = create_name((char*)data);
+               x = create_name(data);
                ADD_LAST(globalvars,x);
        }
 }
-       
+
 
 struct ast_context * ast_context_create(void **extcontexts, const char *name, const char *registrar)
 {
@@ -297,13 +337,28 @@ struct ast_context * ast_context_create(void **extcontexts, const char *name, co
        return x;
 }
 
+struct ast_context * ast_context_find_or_create(void **extcontexts, void *tab, const char *name, const char *registrar)
+{
+       struct ast_context *x = calloc(1, sizeof(*x));
+       if (!x)
+               return NULL;
+       x->next = context_list;
+       context_list = x;
+       if (!no_comp)
+               printf("Executed ast_context_find_or_create(conts, name=%s, registrar=%s);\n", name, registrar);
+       conts++;
+       strncpy(x->name, name, sizeof(x->name) - 1);
+       strncpy(x->registrar, registrar, sizeof(x->registrar) - 1);
+       return x;
+}
+
 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);
        if( dump_extensions ) {
                struct namelist *x;
-               x = create_name((char*)value);
+               x = create_name(value);
                ADD_LAST(con->ignorepats,x);
        }
 }
@@ -369,61 +424,41 @@ void ast_context_destroy(void)
                printf("Executed ast_context_destroy();\n");
 }
 
-void ast_log(int level, const char *file, int line, const char *function, const char *fmt, ...)
+const char *ast_get_context_name(struct ast_context *con);
+const char *ast_get_context_name(struct ast_context *con)
 {
-        va_list vars;
-        va_start(vars,fmt);
-       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);
-       }
+       return con ? con->name : NULL;
 }
 
-void ast_verbose(const char *fmt, ...)
+struct ast_exten *ast_walk_context_extensions(struct ast_context *con, struct ast_exten *exten);
+struct ast_exten *ast_walk_context_extensions(struct ast_context *con, struct ast_exten *exten)
 {
-        va_list vars;
-        va_start(vars,fmt);
+       return NULL;
+}
 
-        printf("VERBOSE: ");
-        vprintf(fmt, vars);
-        fflush(stdout);
-        va_end(vars);
+struct ast_include *ast_walk_context_includes(struct ast_context *con, struct ast_include *inc);
+struct ast_include *ast_walk_context_includes(struct ast_context *con, struct ast_include *inc)
+{
+       return NULL;
 }
 
-char *ast_process_quotes_and_slashes(char *start, char find, char replace_with)
+struct ast_ignorepat *ast_walk_context_ignorepats(struct ast_context *con, struct ast_ignorepat *ip);
+struct ast_ignorepat *ast_walk_context_ignorepats(struct ast_context *con, struct ast_ignorepat *ip)
 {
-        char *dataPut = start;
-        int inEscape = 0;
-        int inQuotes = 0;
+       return NULL;
+}
 
-        for (; *start; start++) {
-                if (inEscape) {
-                        *dataPut++ = *start;       /* Always goes verbatim */
-                        inEscape = 0;
-                } else {
-                        if (*start == '\\') {
-                                inEscape = 1;      /* Do not copy \ into the data */
-                        } else if (*start == '\'') {
-                                inQuotes = 1-inQuotes;   /* Do not copy ' into the data */
-                        } else {
-                                /* Replace , with |, unless in quotes */
-                                *dataPut++ = inQuotes ? *start : ((*start==find) ? replace_with : *start);
-                        }
-                }
-        }
-        if (start != dataPut)
-                *dataPut = 0;
-        return dataPut;
+struct ast_sw *ast_walk_context_switches(struct ast_context *con, struct ast_sw *sw);
+struct ast_sw *ast_walk_context_switches(struct ast_context *con, struct ast_sw *sw)
+{
+       return NULL;
 }
 
 void filter_leading_space_from_exprs(char *str)
 {
        /*  Mainly for aesthetics */
        char *t, *v, *u = str;
-       
+
        while ( u && *u ) {
 
                if( *u == '$' && *(u+1) == '[' ) {
@@ -436,7 +471,7 @@ void filter_leading_space_from_exprs(char *str)
                                }
                        }
                }
-               
+
                u++;
        }
 }
@@ -457,14 +492,15 @@ void filter_newlines(char *str)
 
 
 extern struct module_symbols mod_data;
-extern int ael_external_load_module(void);
+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++) {
                if( argv[i][0] == '-' && argv[i][1] == 'n' )
                        no_comp =1;
@@ -477,7 +513,7 @@ int main(int argc, char **argv)
                if( argv[i][0] == '-' && argv[i][1] == 'w' )
                        dump_extensions =1;
        }
-       
+
        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 )
@@ -489,36 +525,38 @@ int main(int argc, char **argv)
        }
 
        if( use_curr_dir ) {
-               strcpy(ast_config_AST_CONFIG_DIR, ".");
+               strcpy(config_dir, ".");
+               localized_use_local_dir();
        }
        else {
-               strcpy(ast_config_AST_CONFIG_DIR, "/etc/asterisk");
+               strcpy(config_dir, "/etc/asterisk");
+               localized_use_conf_dir();
        }
-       strcpy(ast_config_AST_VAR_DIR, "/var/lib/asterisk");
-       
+       strcpy(var_dir, "/var/lib/asterisk");
+
        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;
-       
+
        ael_external_load_module();
-       
+
        ast_log(4, "ael2_parse", __LINE__, "main", "%d contexts, %d extensions, %d priorities\n", conts, extens, priors);
 
        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);
                                }
@@ -534,10 +572,10 @@ int main(int argc, char **argv)
                        }
                }
        }
-       
+
        if( dump_extensions && dumpfile )
                fclose(dumpfile);
-       
+
        for( lp = context_list; lp; lp = lp2 ) { /* free the ast_context structs */
                lp2 = lp->next;
                lp->next = 0;
@@ -549,6 +587,70 @@ int main(int argc, char **argv)
 
                free(lp);
        }
-       
+
     return 0;
 }
+
+int ast_hashtab_compare_contexts(const void *ah_a, const void *ah_b);
+
+int ast_hashtab_compare_contexts(const void *ah_a, const void *ah_b)
+{
+       return 0;
+}
+
+unsigned int ast_hashtab_hash_contexts(const void *obj);
+
+unsigned int ast_hashtab_hash_contexts(const void *obj)
+{
+       return 0;
+}
+
+#ifdef DEBUG_THREADS
+#if !defined(LOW_MEMORY)
+void ast_mark_lock_acquired(void *lock_addr)
+{
+}
+#ifdef HAVE_BKTR
+void ast_remove_lock_info(void *lock_addr, struct ast_bt *bt)
+{
+}
+
+void ast_store_lock_info(enum ast_lock_type type, const char *filename,
+       int line_num, const char *func, const char *lock_name, void *lock_addr, struct ast_bt *bt)
+{
+}
+
+int __ast_bt_get_addresses(struct ast_bt *bt)
+{
+       return 0;
+}
+
+char **__ast_bt_get_symbols(void **addresses, size_t num_frames)
+{
+       char **foo = calloc(num_frames, sizeof(char *) + 1);
+       if (foo) {
+               int i;
+               for (i = 0; i < num_frames; i++) {
+                       foo[i] = (char *) foo + sizeof(char *) * num_frames;
+               }
+       }
+       return foo;
+}
+#else
+void ast_remove_lock_info(void *lock_addr)
+{
+}
+
+void ast_store_lock_info(enum ast_lock_type type, const char *filename,
+       int line_num, const char *func, const char *lock_name, void *lock_addr)
+{
+}
+#endif /* HAVE_BKTR */
+void ast_suspend_lock_info(void *lock_addr)
+{
+}
+void ast_restore_lock_info(void *lock_addr)
+{
+}
+#endif /* !defined(LOW_MEMORY) */
+#endif /* DEBUG_THREADS */