]> git.ipfire.org Git - thirdparty/freeswitch.git/commitdiff
rearrange for build
authorAnthony Minessale <anthony.minessale@gmail.com>
Tue, 15 Dec 2009 00:49:47 +0000 (00:49 +0000)
committerAnthony Minessale <anthony.minessale@gmail.com>
Tue, 15 Dec 2009 00:49:47 +0000 (00:49 +0000)
git-svn-id: http://svn.freeswitch.org/svn/freeswitch/trunk@15961 d0543943-73ff-0310-b7d9-9358b9ac24b2

src/switch_console.c

index 5cec15f80ee056d494f7f24addf2527e0c349f5e..342b14b48994d52f95dba3e5c5899226b689c93a 100644 (file)
 #define CMD_BUFLEN 1024;
 
 #ifdef SWITCH_HAVE_LIBEDIT
+#include <histedit.h>
+
+static EditLine *el;
+static History *myhistory;
+static HistEvent ev;
+static char *hfile = NULL;
+
+
 /*
  * store a strdup() of the string configured in XML
  * bound to each of the 12 function key
@@ -83,6 +91,19 @@ static switch_status_t console_xml_config(void)
 
        return SWITCH_STATUS_SUCCESS;
 }
+#else
+
+#define CC_NORM         0
+#define CC_NEWLINE      1
+#define CC_EOF          2
+#define CC_ARGHACK      3
+#define CC_REFRESH      4
+#define CC_CURSOR       5
+#define CC_ERROR        6
+#define CC_FATAL        7
+#define CC_REDISPLAY    8
+#define CC_REFRESH_BEEP 9
+
 #endif
 
 SWITCH_DECLARE_NONSTD(switch_status_t) switch_console_stream_raw_write(switch_stream_handle_t *handle, uint8_t *data, switch_size_t datalen)
@@ -364,145 +385,6 @@ SWITCH_DECLARE(void) switch_console_printf(switch_text_channel_t channel, const
 static char hostname[256] = "";
 static int32_t running = 1;
 
-#ifdef SWITCH_HAVE_LIBEDIT
-#include <histedit.h>
-static char prompt_str[512] = "";
-
-/*
- * If a fnkey is configured then process the command
- */
-static unsigned char console_fnkey_pressed(int i)
-{
-       char *c, *cmd;
-
-       assert((i > 0) && (i <= 12));
-
-       c = console_fnkeys[i - 1];
-
-       /* This new line is necessary to avoid output to begin after the ">" of the CLI's prompt */
-       switch_log_printf(SWITCH_CHANNEL_LOG_CLEAN, SWITCH_LOG_CONSOLE, "\n");
-
-       if (c == NULL) {
-               switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CONSOLE, "FUNCTION KEY F%d IS NOT BOUND, please edit switch.conf XML file\n", i);
-               return CC_REDISPLAY;
-       }
-
-       cmd = strdup(c);
-       switch_console_process(cmd, 0);
-       free(cmd);
-
-       return CC_REDISPLAY;
-}
-
-static unsigned char console_f1key(EditLine * el, int ch)
-{
-       return console_fnkey_pressed(1);
-}
-static unsigned char console_f2key(EditLine * el, int ch)
-{
-       return console_fnkey_pressed(2);
-}
-static unsigned char console_f3key(EditLine * el, int ch)
-{
-       return console_fnkey_pressed(3);
-}
-static unsigned char console_f4key(EditLine * el, int ch)
-{
-       return console_fnkey_pressed(4);
-}
-static unsigned char console_f5key(EditLine * el, int ch)
-{
-       return console_fnkey_pressed(5);
-}
-static unsigned char console_f6key(EditLine * el, int ch)
-{
-       return console_fnkey_pressed(6);
-}
-static unsigned char console_f7key(EditLine * el, int ch)
-{
-       return console_fnkey_pressed(7);
-}
-static unsigned char console_f8key(EditLine * el, int ch)
-{
-       return console_fnkey_pressed(8);
-}
-static unsigned char console_f9key(EditLine * el, int ch)
-{
-       return console_fnkey_pressed(9);
-}
-static unsigned char console_f10key(EditLine * el, int ch)
-{
-       return console_fnkey_pressed(10);
-}
-static unsigned char console_f11key(EditLine * el, int ch)
-{
-       return console_fnkey_pressed(11);
-}
-static unsigned char console_f12key(EditLine * el, int ch)
-{
-       return console_fnkey_pressed(12);
-}
-
-
-char *prompt(EditLine * e)
-{
-       if (*prompt_str == '\0') {
-               switch_snprintf(prompt_str, sizeof(prompt_str), "freeswitch@%s> ", hostname);
-       }
-
-       return prompt_str;
-}
-
-static EditLine *el;
-static History *myhistory;
-static HistEvent ev;
-static char *hfile = NULL;
-
-static void *SWITCH_THREAD_FUNC console_thread(switch_thread_t *thread, void *obj)
-{
-       int count;
-       const char *line;
-       switch_memory_pool_t *pool = (switch_memory_pool_t *) obj;
-
-       while (running) {
-               int32_t arg = 0;
-
-               if (getppid() == 1) {
-                       switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING, "We've become an orphan, no more console for us.\n");
-                       break;
-               }
-               
-               switch_core_session_ctl(SCSC_CHECK_RUNNING, &arg);
-               if (!arg) {
-                       break;
-               }
-
-               line = el_gets(el, &count);
-
-               if (count > 1) {
-                       if (!zstr(line)) {
-                               char *cmd = strdup(line);
-                               char *p;
-                               const LineInfo *lf = el_line(el);
-                               char *foo = (char *) lf->buffer;
-                               if ((p = strrchr(cmd, '\r')) || (p = strrchr(cmd, '\n'))) {
-                                       *p = '\0';
-                               }
-                               assert(cmd != NULL);
-                               history(myhistory, &ev, H_ENTER, line);
-                               running = switch_console_process(cmd, 0);
-                               el_deletestr(el, strlen(foo) + 1);
-                               memset(foo, 0, strlen(foo));
-                               free(cmd);
-                       }
-               }
-               switch_cond_next();
-       }
-
-       switch_core_destroy_memory_pool(&pool);
-       switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Editline thread exiting\n");
-       return NULL;
-}
 
 
 struct helper {
@@ -654,6 +536,12 @@ SWITCH_DECLARE(unsigned char) switch_console_complete(const char *line, const ch
        unsigned char ret = CC_REDISPLAY;
        int pos = 0;
 
+#ifndef SWITCH_HAVE_LIBEDIT
+       if (!stream) {
+               return CC_ERROR;
+       }
+#endif
+
        switch_core_db_handle(&db);
        
        if (!zstr(cursor) && !zstr(line)) {
@@ -785,6 +673,7 @@ SWITCH_DECLARE(unsigned char) switch_console_complete(const char *line, const ch
                }
        }
 
+#ifdef SWITCH_HAVE_LIBEDIT
        if (h.out) {
                if (h.hits == 1 && !zstr(h.last)) {
                        el_deletestr(el, h.len);
@@ -795,6 +684,7 @@ SWITCH_DECLARE(unsigned char) switch_console_complete(const char *line, const ch
                        el_insertstr(el, h.partial);
                }
        }
+#endif
 
   end:
 
@@ -810,245 +700,151 @@ SWITCH_DECLARE(unsigned char) switch_console_complete(const char *line, const ch
        return (ret);
 }
 
-static unsigned char complete(EditLine * el, int ch)
-{
-       const LineInfo *lf = el_line(el);
-
-       return switch_console_complete(lf->buffer, lf->cursor, switch_core_get_console(), NULL);
-}
 
-static struct {
-       switch_hash_t *func_hash;
-       switch_mutex_t *func_mutex;
-} globals;
 
-SWITCH_DECLARE(switch_status_t) switch_console_init(switch_memory_pool_t *pool)
-{
-       switch_mutex_init(&globals.func_mutex, SWITCH_MUTEX_NESTED, pool);
-       switch_core_hash_init(&globals.func_hash, pool);
-       switch_console_add_complete_func("::console::list_uuid", switch_console_list_uuid);
-       return SWITCH_STATUS_SUCCESS;
-}
 
-SWITCH_DECLARE(switch_status_t) switch_console_shutdown(void)
-{
-       return switch_core_hash_destroy(&globals.func_hash);
-}
+#ifdef SWITCH_HAVE_LIBEDIT
+static char prompt_str[512] = "";
 
-SWITCH_DECLARE(switch_status_t) switch_console_add_complete_func(const char *name, switch_console_complete_callback_t cb)
+/*
+ * If a fnkey is configured then process the command
+ */
+static unsigned char console_fnkey_pressed(int i)
 {
-       switch_status_t status;
-       
-       switch_mutex_lock(globals.func_mutex);
-       status = switch_core_hash_insert(globals.func_hash, name, (void *)(intptr_t)cb);
-       switch_mutex_unlock(globals.func_mutex);
+       char *c, *cmd;
 
-       return status;
-}
+       assert((i > 0) && (i <= 12));
 
-SWITCH_DECLARE(switch_status_t) switch_console_del_complete_func(const char *name)
-{
-       switch_status_t status;
-       
-       switch_mutex_lock(globals.func_mutex);
-       status = switch_core_hash_insert(globals.func_hash, name, NULL);
-       switch_mutex_unlock(globals.func_mutex);
+       c = console_fnkeys[i - 1];
 
-    return status;
-}
+       /* This new line is necessary to avoid output to begin after the ">" of the CLI's prompt */
+       switch_log_printf(SWITCH_CHANNEL_LOG_CLEAN, SWITCH_LOG_CONSOLE, "\n");
 
-SWITCH_DECLARE(void) switch_console_free_matches(switch_console_callback_match_t **matches)
-{
-       switch_console_callback_match_t *my_match = *matches;
-       switch_console_callback_match_node_t *m, *cur;
+       if (c == NULL) {
+               switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CONSOLE, "FUNCTION KEY F%d IS NOT BOUND, please edit switch.conf XML file\n", i);
+               return CC_REDISPLAY;
+       }
 
-       /* Don't play with matches */
-       *matches = NULL;
+       cmd = strdup(c);
+       switch_console_process(cmd, 0);
+       free(cmd);
 
-       m = my_match->head;
-       while(m) {
-               cur = m;
-               m = m->next;
-               free(cur->val);
-               free(cur);
-       }
+       return CC_REDISPLAY;
 }
 
-SWITCH_DECLARE(void) switch_console_push_match(switch_console_callback_match_t **matches, const char *new_val)
+static unsigned char console_f1key(EditLine * el, int ch)
 {
-       switch_console_callback_match_node_t *match;
-
-       if (!*matches) {
-               switch_zmalloc(*matches, sizeof(**matches));
-       }
-
-       switch_zmalloc(match, sizeof(*match));
-       match->val = strdup(new_val);
-
-       if ((*matches)->head) {
-               (*matches)->end->next = match;
-       } else {
-               (*matches)->head = match;
-       }
-
-       (*matches)->end = match;
+       return console_fnkey_pressed(1);
 }
-
-SWITCH_DECLARE(switch_status_t) switch_console_run_complete_func(const char *func, const char *line, const char *last_word,
-                                                                                                                                switch_console_callback_match_t **matches)
+static unsigned char console_f2key(EditLine * el, int ch)
 {
-       switch_console_complete_callback_t cb;
-       switch_status_t status = SWITCH_STATUS_FALSE;
-
-       switch_mutex_lock(globals.func_mutex);
-       if ((cb = (switch_console_complete_callback_t)(intptr_t)switch_core_hash_find(globals.func_hash, func))) {
-               status = cb(line, last_word, matches);
-       }
-       switch_mutex_unlock(globals.func_mutex);
-
-       return status;
+       return console_fnkey_pressed(2);
 }
-
-
-SWITCH_DECLARE(switch_status_t) switch_console_set_complete(const char *string)
+static unsigned char console_f3key(EditLine * el, int ch)
 {
-       char *mydata = NULL, *argv[11] = { 0 };
-       int argc, x;
-       switch_status_t status = SWITCH_STATUS_FALSE;
-
-       if (string && (mydata = strdup(string))) {
-               if ((argc = switch_separate_string(mydata, ' ', argv, (sizeof(argv) / sizeof(argv[0]))))) {
-                       switch_cache_db_handle_t *db = NULL;
-                       switch_stream_handle_t mystream = { 0 };
-                       SWITCH_STANDARD_STREAM(mystream);
-
-                       switch_core_db_handle(&db);
+       return console_fnkey_pressed(3);
+}
+static unsigned char console_f4key(EditLine * el, int ch)
+{
+       return console_fnkey_pressed(4);
+}
+static unsigned char console_f5key(EditLine * el, int ch)
+{
+       return console_fnkey_pressed(5);
+}
+static unsigned char console_f6key(EditLine * el, int ch)
+{
+       return console_fnkey_pressed(6);
+}
+static unsigned char console_f7key(EditLine * el, int ch)
+{
+       return console_fnkey_pressed(7);
+}
+static unsigned char console_f8key(EditLine * el, int ch)
+{
+       return console_fnkey_pressed(8);
+}
+static unsigned char console_f9key(EditLine * el, int ch)
+{
+       return console_fnkey_pressed(9);
+}
+static unsigned char console_f10key(EditLine * el, int ch)
+{
+       return console_fnkey_pressed(10);
+}
+static unsigned char console_f11key(EditLine * el, int ch)
+{
+       return console_fnkey_pressed(11);
+}
+static unsigned char console_f12key(EditLine * el, int ch)
+{
+       return console_fnkey_pressed(12);
+}
 
-                       if (!strcasecmp(argv[0], "stickyadd")) {
-                               mystream.write_function(&mystream, "insert into complete values (1,");
-                               for (x = 0; x < 10; x++) {
-                                       if (argv[x + 1] && !strcasecmp(argv[x + 1], "_any_")) {
-                                               mystream.write_function(&mystream, "%s", "'', ");
-                                       } else {
-                                               if (db->type == SCDB_TYPE_CORE_DB) {
-                                                       mystream.write_function(&mystream, "'%q', ", switch_str_nil(argv[x + 1]));
-                                               } else {
-                                                       mystream.write_function(&mystream, "'%w', ", switch_str_nil(argv[x + 1]));
-                                               }
-                                       }
-                               }
-                               mystream.write_function(&mystream, " '%s')", switch_core_get_variable("hostname"));
-                               switch_cache_db_persistant_execute(db, mystream.data, 5);
-                               status = SWITCH_STATUS_SUCCESS;
-                       } else if (!strcasecmp(argv[0], "add")) {
-                               mystream.write_function(&mystream, "insert into complete values (0,");
-                               for (x = 0; x < 10; x++) {
-                                       if (argv[x + 1] && !strcasecmp(argv[x + 1], "_any_")) {
-                                               mystream.write_function(&mystream, "%s", "'', ");
-                                       } else {
-                                               if (db->type == SCDB_TYPE_CORE_DB) {
-                                                       mystream.write_function(&mystream, "'%q', ", switch_str_nil(argv[x + 1]));
-                                               } else {
-                                                       mystream.write_function(&mystream, "'%w', ", switch_str_nil(argv[x + 1]));
-                                               }
-                                       }
-                               }
-                               mystream.write_function(&mystream, " '%s')", switch_core_get_variable("hostname"));
-                               
-                               switch_cache_db_persistant_execute(db, mystream.data, 5);
-                               status = SWITCH_STATUS_SUCCESS;
-                       } else if (!strcasecmp(argv[0], "del")) {
-                               char *what = argv[1];
-                               if (!strcasecmp(what, "*")) {
-                                       switch_cache_db_persistant_execute(db, "delete from complete", 1);
-                               } else {
-                                       mystream.write_function(&mystream, "delete from complete where ");
-                                       for (x = 0; x < argc - 1; x++) {
-                                               if (db->type == SCDB_TYPE_CORE_DB) {
-                                                       mystream.write_function(&mystream, "a%d = '%q'%q", x + 1, switch_str_nil(argv[x + 1]), x == argc - 2 ? "" : " and ");
-                                               } else {
-                                                       mystream.write_function(&mystream, "a%d = '%w'%w", x + 1, switch_str_nil(argv[x + 1]), x == argc - 2 ? "" : " and ");
-                                               }
-                                       }
-                                       mystream.write_function(&mystream, " and hostname='%s'", switch_core_get_variable("hostname"));
-                                       switch_cache_db_persistant_execute(db, mystream.data, 1);
-                               }
-                               status = SWITCH_STATUS_SUCCESS;
-                       }
 
-                       switch_safe_free(mystream.data);
-                       switch_cache_db_release_db_handle(&db);
-               }
+char *prompt(EditLine * e)
+{
+       if (*prompt_str == '\0') {
+               switch_snprintf(prompt_str, sizeof(prompt_str), "freeswitch@%s> ", hostname);
        }
 
-       switch_safe_free(mydata);
-
-       return status;
-
+       return prompt_str;
 }
 
-
-SWITCH_DECLARE(switch_status_t) switch_console_set_alias(const char *string)
+static void *SWITCH_THREAD_FUNC console_thread(switch_thread_t *thread, void *obj)
 {
-       char *mydata = NULL, *argv[3] = { 0 };
-       int argc;
-       switch_status_t status = SWITCH_STATUS_FALSE;
+       int count;
+       const char *line;
+       switch_memory_pool_t *pool = (switch_memory_pool_t *) obj;
 
-       if (string && (mydata = strdup(string))) {
-               if ((argc = switch_separate_string(mydata, ' ', argv, (sizeof(argv) / sizeof(argv[0])))) >= 2) {
-                       switch_cache_db_handle_t *db = NULL;
-                       char *sql = NULL;
+       while (running) {
+               int32_t arg = 0;
 
-                       switch_core_db_handle(&db);
+               if (getppid() == 1) {
+                       switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING, "We've become an orphan, no more console for us.\n");
+                       break;
+               }
+               
+               switch_core_session_ctl(SCSC_CHECK_RUNNING, &arg);
+               if (!arg) {
+                       break;
+               }
 
-                       if (!strcasecmp(argv[0], "stickyadd") && argc == 3) {
-                               sql = switch_mprintf("delete from aliases where alias='%q' and hostname='%q'", argv[1], switch_core_get_variable("hostname"));
-                               switch_cache_db_persistant_execute(db, sql, 5);
-                               switch_safe_free(sql);
-                               if (db->type == SCDB_TYPE_CORE_DB) {
-                                       sql = switch_mprintf("insert into aliases (sticky, alias, command, hostname) values (1, '%q','%q','%q')", 
-                                                                                argv[1], argv[2], switch_core_get_variable("hostname"));
-                               } else {
-                                       sql = switch_mprintf("insert into aliases (sticky, alias, command, hostname) values (1, '%w','%w','%w')", 
-                                                                                argv[1], argv[2], switch_core_get_variable("hostname"));
-                               }
-                               switch_cache_db_persistant_execute(db, sql, 5);
-                               status = SWITCH_STATUS_SUCCESS;
-                       } else if (!strcasecmp(argv[0], "add") && argc == 3) {
-                               sql = switch_mprintf("delete from aliases where alias='%q' and hostname='%q'", argv[1], switch_core_get_variable("hostname"));
-                               switch_cache_db_persistant_execute(db, sql, 5);
-                               switch_safe_free(sql);
-                               if (db->type == SCDB_TYPE_CORE_DB) {
-                                       sql = switch_mprintf("insert into aliases (sticky, alias, command, hostname) values (0, '%q','%q','%q')", 
-                                                                                argv[1], argv[2], switch_core_get_variable("hostname"));
-                               } else {
-                                       sql = switch_mprintf("insert into aliases (sticky, alias, command, hostname) values (0, '%w','%w','%w')", 
-                                                                                argv[1], argv[2], switch_core_get_variable("hostname"));
-                               }
-                               switch_cache_db_persistant_execute(db, sql, 5);
-                               status = SWITCH_STATUS_SUCCESS;
-                       } else if (!strcasecmp(argv[0], "del") && argc == 2) {
-                               char *what = argv[1];
-                               if (!strcasecmp(what, "*")) {
-                                       sql = switch_mprintf("delete from aliases where hostname='%q'", switch_core_get_variable("hostname"));
-                                       switch_cache_db_persistant_execute(db, sql, 1);
-                               } else {
-                                       sql = switch_mprintf("delete from aliases where alias='%q' and hostname='%q'", argv[1], switch_core_get_variable("hostname"));
-                                       switch_cache_db_persistant_execute(db, sql, 5);
+               line = el_gets(el, &count);
+
+               if (count > 1) {
+                       if (!zstr(line)) {
+                               char *cmd = strdup(line);
+                               char *p;
+                               const LineInfo *lf = el_line(el);
+                               char *foo = (char *) lf->buffer;
+                               if ((p = strrchr(cmd, '\r')) || (p = strrchr(cmd, '\n'))) {
+                                       *p = '\0';
                                }
-                               status = SWITCH_STATUS_SUCCESS;
+                               assert(cmd != NULL);
+                               history(myhistory, &ev, H_ENTER, line);
+                               running = switch_console_process(cmd, 0);
+                               el_deletestr(el, strlen(foo) + 1);
+                               memset(foo, 0, strlen(foo));
+                               free(cmd);
                        }
-                       switch_safe_free(sql);
-                       switch_cache_db_release_db_handle(&db);
                }
+               switch_cond_next();
        }
 
-       switch_safe_free(mydata);
+       switch_core_destroy_memory_pool(&pool);
+       switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Editline thread exiting\n");
+       return NULL;
+}
 
-       return status;
+static unsigned char complete(EditLine * el, int ch)
+{
+       const LineInfo *lf = el_line(el);
 
+       return switch_console_complete(lf->buffer, lf->cursor, switch_core_get_console(), NULL);
 }
 
+
 SWITCH_DECLARE(void) switch_console_loop(void)
 {
        switch_thread_t *thread;
@@ -1146,21 +942,13 @@ SWITCH_DECLARE(void) switch_console_loop(void)
 }
 
 #else
-SWITCH_DECLARE(switch_status_t) switch_console_set_alias(const char *string)
-{
-       return SWITCH_STATUS_FALSE;
-}
-
-SWITCH_DECLARE(switch_status_t) switch_console_set_complete(const char *string)
-{
-       return SWITCH_STATUS_FALSE;
-}
 
 SWITCH_DECLARE(void) switch_console_loop(void)
 {
 
        char cmd[2048] = "";
-       int32_t activity = 1;   
+       int32_t x = 0, activity = 1;    
+       
        gethostname(hostname, sizeof(hostname));
 
        while (running) {
@@ -1253,8 +1041,247 @@ SWITCH_DECLARE(void) switch_console_loop(void)
 #endif
        }
 }
+
+
+
 #endif
 
+
+static struct {
+       switch_hash_t *func_hash;
+       switch_mutex_t *func_mutex;
+} globals;
+
+SWITCH_DECLARE(switch_status_t) switch_console_init(switch_memory_pool_t *pool)
+{
+       switch_mutex_init(&globals.func_mutex, SWITCH_MUTEX_NESTED, pool);
+       switch_core_hash_init(&globals.func_hash, pool);
+       switch_console_add_complete_func("::console::list_uuid", switch_console_list_uuid);
+       return SWITCH_STATUS_SUCCESS;
+}
+
+SWITCH_DECLARE(switch_status_t) switch_console_shutdown(void)
+{
+       return switch_core_hash_destroy(&globals.func_hash);
+}
+
+SWITCH_DECLARE(switch_status_t) switch_console_add_complete_func(const char *name, switch_console_complete_callback_t cb)
+{
+       switch_status_t status;
+       
+       switch_mutex_lock(globals.func_mutex);
+       status = switch_core_hash_insert(globals.func_hash, name, (void *)(intptr_t)cb);
+       switch_mutex_unlock(globals.func_mutex);
+
+       return status;
+}
+
+SWITCH_DECLARE(switch_status_t) switch_console_del_complete_func(const char *name)
+{
+       switch_status_t status;
+       
+       switch_mutex_lock(globals.func_mutex);
+       status = switch_core_hash_insert(globals.func_hash, name, NULL);
+       switch_mutex_unlock(globals.func_mutex);
+
+    return status;
+}
+
+SWITCH_DECLARE(void) switch_console_free_matches(switch_console_callback_match_t **matches)
+{
+       switch_console_callback_match_t *my_match = *matches;
+       switch_console_callback_match_node_t *m, *cur;
+
+       /* Don't play with matches */
+       *matches = NULL;
+
+       m = my_match->head;
+       while(m) {
+               cur = m;
+               m = m->next;
+               free(cur->val);
+               free(cur);
+       }
+}
+
+SWITCH_DECLARE(void) switch_console_push_match(switch_console_callback_match_t **matches, const char *new_val)
+{
+       switch_console_callback_match_node_t *match;
+
+       if (!*matches) {
+               switch_zmalloc(*matches, sizeof(**matches));
+       }
+
+       switch_zmalloc(match, sizeof(*match));
+       match->val = strdup(new_val);
+
+       if ((*matches)->head) {
+               (*matches)->end->next = match;
+       } else {
+               (*matches)->head = match;
+       }
+
+       (*matches)->end = match;
+}
+
+SWITCH_DECLARE(switch_status_t) switch_console_run_complete_func(const char *func, const char *line, const char *last_word,
+                                                                                                                                switch_console_callback_match_t **matches)
+{
+       switch_console_complete_callback_t cb;
+       switch_status_t status = SWITCH_STATUS_FALSE;
+
+       switch_mutex_lock(globals.func_mutex);
+       if ((cb = (switch_console_complete_callback_t)(intptr_t)switch_core_hash_find(globals.func_hash, func))) {
+               status = cb(line, last_word, matches);
+       }
+       switch_mutex_unlock(globals.func_mutex);
+
+       return status;
+}
+
+
+SWITCH_DECLARE(switch_status_t) switch_console_set_complete(const char *string)
+{
+       char *mydata = NULL, *argv[11] = { 0 };
+       int argc, x;
+       switch_status_t status = SWITCH_STATUS_FALSE;
+
+       if (string && (mydata = strdup(string))) {
+               if ((argc = switch_separate_string(mydata, ' ', argv, (sizeof(argv) / sizeof(argv[0]))))) {
+                       switch_cache_db_handle_t *db = NULL;
+                       switch_stream_handle_t mystream = { 0 };
+                       SWITCH_STANDARD_STREAM(mystream);
+
+                       switch_core_db_handle(&db);
+
+                       if (!strcasecmp(argv[0], "stickyadd")) {
+                               mystream.write_function(&mystream, "insert into complete values (1,");
+                               for (x = 0; x < 10; x++) {
+                                       if (argv[x + 1] && !strcasecmp(argv[x + 1], "_any_")) {
+                                               mystream.write_function(&mystream, "%s", "'', ");
+                                       } else {
+                                               if (db->type == SCDB_TYPE_CORE_DB) {
+                                                       mystream.write_function(&mystream, "'%q', ", switch_str_nil(argv[x + 1]));
+                                               } else {
+                                                       mystream.write_function(&mystream, "'%w', ", switch_str_nil(argv[x + 1]));
+                                               }
+                                       }
+                               }
+                               mystream.write_function(&mystream, " '%s')", switch_core_get_variable("hostname"));
+                               switch_cache_db_persistant_execute(db, mystream.data, 5);
+                               status = SWITCH_STATUS_SUCCESS;
+                       } else if (!strcasecmp(argv[0], "add")) {
+                               mystream.write_function(&mystream, "insert into complete values (0,");
+                               for (x = 0; x < 10; x++) {
+                                       if (argv[x + 1] && !strcasecmp(argv[x + 1], "_any_")) {
+                                               mystream.write_function(&mystream, "%s", "'', ");
+                                       } else {
+                                               if (db->type == SCDB_TYPE_CORE_DB) {
+                                                       mystream.write_function(&mystream, "'%q', ", switch_str_nil(argv[x + 1]));
+                                               } else {
+                                                       mystream.write_function(&mystream, "'%w', ", switch_str_nil(argv[x + 1]));
+                                               }
+                                       }
+                               }
+                               mystream.write_function(&mystream, " '%s')", switch_core_get_variable("hostname"));
+                               
+                               switch_cache_db_persistant_execute(db, mystream.data, 5);
+                               status = SWITCH_STATUS_SUCCESS;
+                       } else if (!strcasecmp(argv[0], "del")) {
+                               char *what = argv[1];
+                               if (!strcasecmp(what, "*")) {
+                                       switch_cache_db_persistant_execute(db, "delete from complete", 1);
+                               } else {
+                                       mystream.write_function(&mystream, "delete from complete where ");
+                                       for (x = 0; x < argc - 1; x++) {
+                                               if (db->type == SCDB_TYPE_CORE_DB) {
+                                                       mystream.write_function(&mystream, "a%d = '%q'%q", x + 1, switch_str_nil(argv[x + 1]), x == argc - 2 ? "" : " and ");
+                                               } else {
+                                                       mystream.write_function(&mystream, "a%d = '%w'%w", x + 1, switch_str_nil(argv[x + 1]), x == argc - 2 ? "" : " and ");
+                                               }
+                                       }
+                                       mystream.write_function(&mystream, " and hostname='%s'", switch_core_get_variable("hostname"));
+                                       switch_cache_db_persistant_execute(db, mystream.data, 1);
+                               }
+                               status = SWITCH_STATUS_SUCCESS;
+                       }
+
+                       switch_safe_free(mystream.data);
+                       switch_cache_db_release_db_handle(&db);
+               }
+       }
+
+       switch_safe_free(mydata);
+
+       return status;
+
+}
+
+
+SWITCH_DECLARE(switch_status_t) switch_console_set_alias(const char *string)
+{
+       char *mydata = NULL, *argv[3] = { 0 };
+       int argc;
+       switch_status_t status = SWITCH_STATUS_FALSE;
+
+       if (string && (mydata = strdup(string))) {
+               if ((argc = switch_separate_string(mydata, ' ', argv, (sizeof(argv) / sizeof(argv[0])))) >= 2) {
+                       switch_cache_db_handle_t *db = NULL;
+                       char *sql = NULL;
+
+                       switch_core_db_handle(&db);
+
+                       if (!strcasecmp(argv[0], "stickyadd") && argc == 3) {
+                               sql = switch_mprintf("delete from aliases where alias='%q' and hostname='%q'", argv[1], switch_core_get_variable("hostname"));
+                               switch_cache_db_persistant_execute(db, sql, 5);
+                               switch_safe_free(sql);
+                               if (db->type == SCDB_TYPE_CORE_DB) {
+                                       sql = switch_mprintf("insert into aliases (sticky, alias, command, hostname) values (1, '%q','%q','%q')", 
+                                                                                argv[1], argv[2], switch_core_get_variable("hostname"));
+                               } else {
+                                       sql = switch_mprintf("insert into aliases (sticky, alias, command, hostname) values (1, '%w','%w','%w')", 
+                                                                                argv[1], argv[2], switch_core_get_variable("hostname"));
+                               }
+                               switch_cache_db_persistant_execute(db, sql, 5);
+                               status = SWITCH_STATUS_SUCCESS;
+                       } else if (!strcasecmp(argv[0], "add") && argc == 3) {
+                               sql = switch_mprintf("delete from aliases where alias='%q' and hostname='%q'", argv[1], switch_core_get_variable("hostname"));
+                               switch_cache_db_persistant_execute(db, sql, 5);
+                               switch_safe_free(sql);
+                               if (db->type == SCDB_TYPE_CORE_DB) {
+                                       sql = switch_mprintf("insert into aliases (sticky, alias, command, hostname) values (0, '%q','%q','%q')", 
+                                                                                argv[1], argv[2], switch_core_get_variable("hostname"));
+                               } else {
+                                       sql = switch_mprintf("insert into aliases (sticky, alias, command, hostname) values (0, '%w','%w','%w')", 
+                                                                                argv[1], argv[2], switch_core_get_variable("hostname"));
+                               }
+                               switch_cache_db_persistant_execute(db, sql, 5);
+                               status = SWITCH_STATUS_SUCCESS;
+                       } else if (!strcasecmp(argv[0], "del") && argc == 2) {
+                               char *what = argv[1];
+                               if (!strcasecmp(what, "*")) {
+                                       sql = switch_mprintf("delete from aliases where hostname='%q'", switch_core_get_variable("hostname"));
+                                       switch_cache_db_persistant_execute(db, sql, 1);
+                               } else {
+                                       sql = switch_mprintf("delete from aliases where alias='%q' and hostname='%q'", argv[1], switch_core_get_variable("hostname"));
+                                       switch_cache_db_persistant_execute(db, sql, 5);
+                               }
+                               status = SWITCH_STATUS_SUCCESS;
+                       }
+                       switch_safe_free(sql);
+                       switch_cache_db_release_db_handle(&db);
+               }
+       }
+
+       switch_safe_free(mydata);
+
+       return status;
+
+}
+
+
+
+
 /* For Emacs:
  * Local Variables:
  * mode:c