From 0564d81e09275057542f930076087dd32ea026b2 Mon Sep 17 00:00:00 2001 From: Anthony Minessale Date: Wed, 23 Apr 2008 16:46:24 +0000 Subject: [PATCH] update git-svn-id: http://svn.freeswitch.org/svn/freeswitch/trunk@8176 d0543943-73ff-0310-b7d9-9358b9ac24b2 --- src/include/switch_core.h | 1 + .../applications/mod_commands/mod_commands.c | 37 +------ src/switch_console.c | 100 ++++++++++++++++-- src/switch_core_sqldb.c | 13 ++- 4 files changed, 106 insertions(+), 45 deletions(-) diff --git a/src/include/switch_core.h b/src/include/switch_core.h index 0a0633e0bc..e10eeaafa0 100644 --- a/src/include/switch_core.h +++ b/src/include/switch_core.h @@ -1600,6 +1600,7 @@ SWITCH_DECLARE(switch_bool_t) switch_check_network_list_ip(const char *ip_str, c SWITCH_DECLARE(void) switch_time_set_monotonic(switch_bool_t enable); SWITCH_DECLARE(uint32_t) switch_core_max_dtmf_duration(uint32_t duration); SWITCH_DECLARE(uint32_t) switch_core_default_dtmf_duration(uint32_t duration); +SWITCH_DECLARE(switch_status_t) switch_console_set_complete(const char *string); ///\} diff --git a/src/mod/applications/mod_commands/mod_commands.c b/src/mod/applications/mod_commands/mod_commands.c index 2516e58b4e..c9da978ae3 100644 --- a/src/mod/applications/mod_commands/mod_commands.c +++ b/src/mod/applications/mod_commands/mod_commands.c @@ -1888,45 +1888,16 @@ static int show_callback(void *pArg, int argc, char **argv, char **columnNames) #define COMPLETE_SYNTAX "add |del [|all]" SWITCH_STANDARD_API(complete_function) { - char *mydata = NULL, *argv[2] = {0}; - int fail = 1, argc; - char sql[1024] = ""; - if (cmd && (mydata = strdup(cmd))) { - if ((argc = switch_separate_string(mydata, ' ', argv, (sizeof(argv) / sizeof(argv[0]))))) { - switch_core_db_t *db = switch_core_db_handle(); - - if (!strcasecmp(argv[0], "add")) { - switch_snprintf(sql, sizeof(sql), "delete from complete where name = '%s'", argv[1]); - switch_core_db_persistant_execute(db, sql, 1); - switch_snprintf(sql, sizeof(sql), "insert into complete values ('%s')", argv[1]); - switch_core_db_persistant_execute(db, sql, 1); - stream->write_function(stream, "+OK\n"); - fail = 0; - } else if (!strcasecmp(argv[0], "del")) { - char *what = argv[1]; - if (!strcasecmp(what, "*")) { - switch_snprintf(sql, sizeof(sql), "delete from complete"); - } else { - switch_snprintf(sql, sizeof(sql), "delete from complete where name = '%s'", what); - } - switch_core_db_persistant_execute(db, sql, 1); - stream->write_function(stream, "+OK\n"); - fail = 0; - } + switch_status_t status; - switch_core_db_close(db); - } - } - - switch_safe_free(mydata); - - if (fail) { + if ((status = switch_console_set_complete(cmd)) == SWITCH_STATUS_SUCCESS) { + stream->write_function(stream, "+OK\n"); + } else { stream->write_function(stream, "-USAGE: %s\n", COMPLETE_SYNTAX); } return SWITCH_STATUS_SUCCESS; - } #define SHOW_SYNTAX "codec|application|api|dialplan|file|timer|calls|channels" diff --git a/src/switch_console.c b/src/switch_console.c index 358f5301ee..c6ad6fb18f 100644 --- a/src/switch_console.c +++ b/src/switch_console.c @@ -371,6 +371,7 @@ struct helper { EditLine *el; int len; int hits; + int words; char last[512]; FILE *out; }; @@ -378,10 +379,17 @@ struct helper { static int comp_callback(void *pArg, int argc, char **argv, char **columnNames) { struct helper *h = (struct helper *) pArg; - - fprintf(h->out, "%20s\t", argv[0]); + char *target = NULL; - switch_copy_string(h->last, argv[0], sizeof(h->last)); + target = argv[0]; + + if (!target) { + return -1; + } + + fprintf(h->out, "%20s\t", target); + + switch_copy_string(h->last, target, sizeof(h->last)); if ((++h->hits % 4) == 0) { fprintf(h->out, "\n"); @@ -400,7 +408,6 @@ static unsigned char complete(EditLine *el, int ch) char *p, *lp = NULL; char *errmsg = NULL; struct helper h = { el }; - int words = 0; unsigned char ret = CC_REDISPLAY; h.out = switch_core_get_console(); @@ -416,7 +423,7 @@ static unsigned char complete(EditLine *el, int ch) for(p = buf; p && *p; p++) { if (*p == ' ') { lp = p; - words++; + h.words++; } } @@ -428,7 +435,7 @@ static unsigned char complete(EditLine *el, int ch) fprintf(h.out, "\n\n"); - if (words == 0) { + if (h.words == 0) { sql = switch_mprintf("select distinct name from interfaces where type='api' and name like '%s%%' order by name", buf); } else { sql = switch_mprintf("select distinct uuid from channels where uuid like '%s%%' order by uuid", buf); @@ -444,14 +451,42 @@ static unsigned char complete(EditLine *el, int ch) } if (h.hits != 1) { - switch_safe_free(sql); - sql = switch_mprintf("select distinct name from complete where name like '%s%%' order by name", buf); - switch_core_db_exec(db, sql, comp_callback, &h, &errmsg); + char *dupdup = strdup(dup); + switch_assert(dupdup); + int x, argc = 0; + char *argv[10] = {0}; + switch_stream_handle_t stream = { 0 }; + SWITCH_STANDARD_STREAM(stream); + + argc = switch_separate_string(dupdup, ' ', argv, (sizeof(argv) / sizeof(argv[0]))); + + if (h.words == 0) { + stream.write_function(&stream, + "select distinct a1 from complete where " + "a1 not in (select name from interfaces) and "); + } else { + stream.write_function(&stream, + "select distinct a%d from complete where ", h.words + 1); + + } + + for(x = 0; x < argc; x++) { + stream.write_function(&stream, "(a%d = '' or a%d like '%s%%')%s", x+1, x+1, switch_str_nil(argv[x]), x == argc -1 ? "" : " and "); + } + + switch_core_db_exec(db, stream.data, comp_callback, &h, &errmsg); + if (errmsg) { - switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "error [%s][%s]\n", sql, errmsg); + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "error [%s][%s]\n", (char *) stream.data, errmsg); free(errmsg); ret = CC_ERROR; + } + + switch_safe_free(dupdup); + switch_safe_free(stream.data); + + if (ret == CC_ERROR) { goto end; } } @@ -475,6 +510,51 @@ static unsigned char complete(EditLine *el, int ch) return (ret); } + +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_core_db_t *db = switch_core_db_handle(); + switch_stream_handle_t mystream = { 0 }; + SWITCH_STANDARD_STREAM(mystream); + + if (!strcasecmp(argv[0], "add")) { + mystream.write_function(&mystream, "insert into complete values ("); + for(x = 0; x < 10; x++) { + mystream.write_function(&mystream, "'%s'%s", switch_str_nil(argv[x+1]), x == 9 ? ")" : ", "); + } + switch_core_db_persistant_execute(db, mystream.data, 1); + status = SWITCH_STATUS_SUCCESS; + } else if (!strcasecmp(argv[0], "del")) { + char *what = argv[1]; + if (!strcasecmp(what, "*")) { + switch_core_db_persistant_execute(db, "delete from complete", 1); + } else { + mystream.write_function(&mystream, "delete from complete where "); + for(x = 0; x < argc - 1; x++) { + mystream.write_function(&mystream, "a%d = '%s'%s", x+1, switch_str_nil(argv[x+1]), x == argc - 2 ? "" : " and "); + } + switch_core_db_persistant_execute(db, mystream.data, 1); + } + status = SWITCH_STATUS_SUCCESS; + } + switch_safe_free(mystream.data); + switch_core_db_close(db); + } + } + + switch_safe_free(mydata); + + return status; + +} + + SWITCH_DECLARE(void) switch_console_loop(void) { switch_thread_t *thread; diff --git a/src/switch_core_sqldb.c b/src/switch_core_sqldb.c index 4fd854d24c..06e737591b 100644 --- a/src/switch_core_sqldb.c +++ b/src/switch_core_sqldb.c @@ -368,7 +368,16 @@ void switch_core_sqldb_start(switch_memory_pool_t *pool) } else { char create_complete_sql[] = "CREATE TABLE complete (\n" - " name VARCHAR(255)\n" + " a1 VARCHAR(255),\n" + " a2 VARCHAR(255),\n" + " a3 VARCHAR(255),\n" + " a4 VARCHAR(255),\n" + " a5 VARCHAR(255),\n" + " a6 VARCHAR(255),\n" + " a7 VARCHAR(255),\n" + " a8 VARCHAR(255),\n" + " a9 VARCHAR(255),\n" + " a10 VARCHAR(255)\n" ");\n"; char create_channels_sql[] = @@ -430,7 +439,7 @@ void switch_core_sqldb_start(switch_memory_pool_t *pool) switch_core_db_exec(sql_manager.db, "PRAGMA cache_size=8000", NULL, NULL, NULL); switch_core_db_exec(sql_manager.db, "PRAGMA temp_store=MEMORY;", NULL, NULL, NULL); - switch_core_db_exec(sql_manager.db, create_complete_sql, NULL, NULL, NULL); + switch_core_db_test_reactive(sql_manager.db, "select a1 from complete", "DROP TABLE complete", create_complete_sql); switch_core_db_exec(sql_manager.db, create_channels_sql, NULL, NULL, NULL); switch_core_db_exec(sql_manager.db, create_calls_sql, NULL, NULL, NULL); switch_core_db_exec(sql_manager.db, create_interfaces_sql, NULL, NULL, NULL);