From 8b6dc52cb9103cbf1401b49086d9cd909ade0ccd Mon Sep 17 00:00:00 2001 From: Anthony Minessale Date: Wed, 23 Apr 2008 17:38:13 +0000 Subject: [PATCH] dunno, some stuff... git-svn-id: http://svn.freeswitch.org/svn/freeswitch/trunk@8179 d0543943-73ff-0310-b7d9-9358b9ac24b2 --- src/include/switch_core.h | 1 + .../applications/mod_commands/mod_commands.c | 25 ++++- src/switch_console.c | 96 +++++++++++++++++-- src/switch_core_sqldb.c | 7 ++ 4 files changed, 121 insertions(+), 8 deletions(-) diff --git a/src/include/switch_core.h b/src/include/switch_core.h index e10eeaafa0..59008338f2 100644 --- a/src/include/switch_core.h +++ b/src/include/switch_core.h @@ -1601,6 +1601,7 @@ 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); +SWITCH_DECLARE(switch_status_t) switch_console_set_alias(const char *string); ///\} diff --git a/src/mod/applications/mod_commands/mod_commands.c b/src/mod/applications/mod_commands/mod_commands.c index c9da978ae3..c1811ca612 100644 --- a/src/mod/applications/mod_commands/mod_commands.c +++ b/src/mod/applications/mod_commands/mod_commands.c @@ -1885,7 +1885,7 @@ static int show_callback(void *pArg, int argc, char **argv, char **columnNames) return 0; } -#define COMPLETE_SYNTAX "add |del [|all]" +#define COMPLETE_SYNTAX "add |del [|*]" SWITCH_STANDARD_API(complete_function) { @@ -1900,7 +1900,23 @@ SWITCH_STANDARD_API(complete_function) return SWITCH_STATUS_SUCCESS; } -#define SHOW_SYNTAX "codec|application|api|dialplan|file|timer|calls|channels" + +#define ALIAS_SYNTAX "add | del [|*]" +SWITCH_STANDARD_API(alias_function) +{ + + switch_status_t status; + + if ((status = switch_console_set_alias(cmd)) == SWITCH_STATUS_SUCCESS) { + stream->write_function(stream, "+OK\n"); + } else { + stream->write_function(stream, "-USAGE: %s\n", ALIAS_SYNTAX); + } + + return SWITCH_STATUS_SUCCESS; +} + +#define SHOW_SYNTAX "codec|application|api|dialplan|file|timer|calls|channels|aliases|complete" SWITCH_STANDARD_API(show_function) { char sql[1024]; @@ -1951,6 +1967,10 @@ SWITCH_STANDARD_API(show_function) sprintf(sql, "select * from calls order by created_epoch"); } else if (!strcasecmp(command, "channels")) { sprintf(sql, "select * from channels order by created_epoch"); + } else if (!strcasecmp(command, "aliases")) { + sprintf(sql, "select * from aliases order by alias"); + } else if (!strcasecmp(command, "complete")) { + sprintf(sql, "select * from complete order by a1,a2,a3,a4,a5,a6,a7,a8,a9,a10"); } else if (!strncasecmp(command, "help", 4)) { char *cmdname = NULL; @@ -2280,6 +2300,7 @@ SWITCH_MODULE_LOAD_FUNCTION(mod_commands_load) SWITCH_ADD_API(commands_api_interface, "break", "Break", break_function, BREAK_SYNTAX); SWITCH_ADD_API(commands_api_interface, "show", "Show", show_function, SHOW_SYNTAX); SWITCH_ADD_API(commands_api_interface, "complete", "Complete", complete_function, COMPLETE_SYNTAX); + SWITCH_ADD_API(commands_api_interface, "alias", "Alias", alias_function, ALIAS_SYNTAX); SWITCH_ADD_API(commands_api_interface, "status", "status", status_function, ""); SWITCH_ADD_API(commands_api_interface, "uuid_bridge", "uuid_bridge", uuid_bridge_function, ""); SWITCH_ADD_API(commands_api_interface, "uuid_setvar", "uuid_setvar", uuid_setvar_function, SETVAR_SYNTAX); diff --git a/src/switch_console.c b/src/switch_console.c index c6ad6fb18f..dcdadf4dd3 100644 --- a/src/switch_console.c +++ b/src/switch_console.c @@ -154,9 +154,47 @@ SWITCH_DECLARE_NONSTD(switch_status_t) switch_console_stream_write(switch_stream return ret ? SWITCH_STATUS_FALSE : SWITCH_STATUS_SUCCESS; } -static int switch_console_process(char *cmd) +static int alias_callback(void *pArg, int argc, char **argv, char **columnNames) { - char *arg = NULL; + char **r = (char **) pArg; + *r = strdup(argv[0]); + return -1; +} + +char *expand_alias(char *cmd, char *arg) +{ + char *errmsg = NULL; + char *r = NULL; + char *sql; + char *exp = NULL; + switch_core_db_t *db = switch_core_db_handle(); + + sql = switch_mprintf("select command from aliases where alias='%s'", cmd); + switch_core_db_exec(db, sql, alias_callback, &r, &errmsg); + + if (errmsg) { + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "error [%s][%s]\n", sql, errmsg); + free(errmsg); + } + + if (r) { + if (arg) { + exp = switch_mprintf("%s %s", r, arg); + free(r); + } else { + exp = r; + } + } else { + exp = cmd; + } + + switch_core_db_close(db); + return exp; +} + +static int switch_console_process(char *cmd, int rec) +{ + char *arg = NULL, *alias = NULL; switch_stream_handle_t stream = { 0 }; if (!strcmp(cmd, "shutdown") || !strcmp(cmd, "...")) { @@ -175,6 +213,12 @@ static int switch_console_process(char *cmd) *arg++ = '\0'; } + if (!rec && (alias = expand_alias(cmd, arg)) && alias != cmd) { + int r = switch_console_process(alias, ++rec); + free(alias); + return r; + } + SWITCH_STANDARD_STREAM(stream); if (stream.data) { if (switch_api_execute(cmd, arg, NULL, &stream) == SWITCH_STATUS_SUCCESS) { @@ -268,7 +312,7 @@ static unsigned char console_fnkey_pressed(int i) { } cmd = strdup(c); - switch_console_process(cmd); + switch_console_process(cmd, 0); free(cmd); return CC_REDISPLAY; @@ -353,7 +397,7 @@ static void *SWITCH_THREAD_FUNC console_thread(switch_thread_t *thread, void *ob } assert(cmd != NULL); history(myhistory, &ev, H_ENTER, line); - running = switch_console_process(cmd); + running = switch_console_process(cmd, 0); el_deletestr(el, strlen(foo)+1); memset(foo, 0, strlen(foo)); free(cmd); @@ -528,7 +572,7 @@ SWITCH_DECLARE(switch_status_t) switch_console_set_complete(const char *string) 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); + switch_core_db_persistant_execute(db, mystream.data, 5); status = SWITCH_STATUS_SUCCESS; } else if (!strcasecmp(argv[0], "del")) { char *what = argv[1]; @@ -555,6 +599,46 @@ SWITCH_DECLARE(switch_status_t) switch_console_set_complete(const char *string) } +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_core_db_t *db = switch_core_db_handle(); + char *sql = NULL; + + if (!strcasecmp(argv[0], "add") && argc == 3) { + sql = switch_mprintf("delete from aliases where alias='%q'", argv[1]); + switch_core_db_persistant_execute(db, sql, 5); + switch_safe_free(sql); + sql = switch_mprintf("insert into aliases (alias, command) values ('%q','%q')", argv[1], argv[2]); + switch_core_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, "*")) { + switch_core_db_persistant_execute(db, "delete from aliases", 1); + } else { + sql = switch_mprintf("delete from aliases where alias='%q'", argv[1]); + switch_core_db_persistant_execute(db, sql, 5); + } + status = SWITCH_STATUS_SUCCESS; + } + switch_safe_free(sql); + switch_core_db_close(db); + } + } + + switch_safe_free(mydata); + + return status; + +} + + SWITCH_DECLARE(void) switch_console_loop(void) { switch_thread_t *thread; @@ -712,7 +796,7 @@ SWITCH_DECLARE(void) switch_console_loop(void) } if (cmd[0]) { - running = switch_console_process(cmd); + running = switch_console_process(cmd, 0); } } } diff --git a/src/switch_core_sqldb.c b/src/switch_core_sqldb.c index 06e737591b..8931fef30a 100644 --- a/src/switch_core_sqldb.c +++ b/src/switch_core_sqldb.c @@ -380,6 +380,12 @@ void switch_core_sqldb_start(switch_memory_pool_t *pool) " a10 VARCHAR(255)\n" ");\n"; + char create_alias_sql[] = + "CREATE TABLE aliases (\n" + " alias VARCHAR(255),\n" + " command VARCHAR(255)\n" + ");\n"; + char create_channels_sql[] = "CREATE TABLE channels (\n" " uuid VARCHAR(255),\n" @@ -440,6 +446,7 @@ void switch_core_sqldb_start(switch_memory_pool_t *pool) switch_core_db_exec(sql_manager.db, "PRAGMA temp_store=MEMORY;", NULL, NULL, NULL); switch_core_db_test_reactive(sql_manager.db, "select a1 from complete", "DROP TABLE complete", create_complete_sql); + switch_core_db_test_reactive(sql_manager.db, "select alias from aliases", "DROP TABLE aliases", create_alias_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);