From bd97fb615f9f511e33cad9aad3ad8c95aaa147be Mon Sep 17 00:00:00 2001 From: Anthony Minessale Date: Wed, 20 Jan 2010 17:45:53 +0000 Subject: [PATCH] allow alias expansion from fs_cli git-svn-id: http://svn.freeswitch.org/svn/freeswitch/trunk@16416 d0543943-73ff-0310-b7d9-9358b9ac24b2 --- libs/esl/fs_cli.c | 7 +- src/include/switch_console.h | 2 + .../applications/mod_commands/mod_commands.c | 33 +++++--- .../mod_event_socket/mod_event_socket.c | 22 ++++- src/switch_console.c | 81 ++++++++++--------- 5 files changed, 91 insertions(+), 54 deletions(-) diff --git a/libs/esl/fs_cli.c b/libs/esl/fs_cli.c index 1012f07fa3..d200519142 100644 --- a/libs/esl/fs_cli.c +++ b/libs/esl/fs_cli.c @@ -703,8 +703,11 @@ static int process_command(esl_handle_t *handle, const char *cmd) char cmd_str[1024] = ""; const char *err = NULL; - snprintf(cmd_str, sizeof(cmd_str), "api %s\n\n", cmd); - esl_send_recv(handle, cmd_str); + snprintf(cmd_str, sizeof(cmd_str), "api %s\nconsole_execute: true\n\n", cmd); + if (esl_send_recv(handle, cmd_str)) { + printf("Socket Interrupted, bye!\n"); + return 1; + } if (handle->last_sr_event) { if (handle->last_sr_event->body) { printf("%s\n", handle->last_sr_event->body); diff --git a/src/include/switch_console.h b/src/include/switch_console.h index 022441d258..06641558e6 100644 --- a/src/include/switch_console.h +++ b/src/include/switch_console.h @@ -89,6 +89,8 @@ SWITCH_DECLARE(unsigned char) switch_console_complete(const char *line, const ch FILE *console_out, switch_stream_handle_t *stream, switch_xml_t xml); SWITCH_DECLARE(void) switch_console_sort_matches(switch_console_callback_match_t *matches); SWITCH_DECLARE(void) switch_console_save_history(void); +SWITCH_DECLARE(char *) switch_console_expand_alias(char *cmd, char *arg); +SWITCH_DECLARE(switch_status_t) switch_console_execute(char *xcmd, int rec, switch_stream_handle_t *istream); SWITCH_END_EXTERN_C #endif diff --git a/src/mod/applications/mod_commands/mod_commands.c b/src/mod/applications/mod_commands/mod_commands.c index fe67d29822..042aa68bab 100644 --- a/src/mod/applications/mod_commands/mod_commands.c +++ b/src/mod/applications/mod_commands/mod_commands.c @@ -53,6 +53,22 @@ SWITCH_STANDARD_API(hostname_api_function) return SWITCH_STATUS_SUCCESS; } + +SWITCH_STANDARD_API(shutdown_function) +{ + switch_session_ctl_t command = SCSC_SHUTDOWN; + int arg = 0; + switch_core_session_ctl(command, &arg); + + return SWITCH_STATUS_SUCCESS; +} + +SWITCH_STANDARD_API(version_function) +{ + stream->write_function(stream, "FreeSWITCH Version %s\n", SWITCH_VERSION_FULL); + return SWITCH_STATUS_SUCCESS; +} + SWITCH_STANDARD_API(db_cache_function) { int argc; char *mydata = NULL, *argv[2]; @@ -3421,15 +3437,6 @@ SWITCH_STANDARD_API(show_function) return status; } -SWITCH_STANDARD_API(version_function) -{ - char version_string[1024]; - switch_snprintf(version_string, sizeof(version_string) - 1, "FreeSWITCH Version %s\n", SWITCH_VERSION_FULL); - - stream->write_function(stream, version_string); - return SWITCH_STATUS_SUCCESS; -} - SWITCH_STANDARD_API(help_function) { char showcmd[1024]; @@ -4079,6 +4086,9 @@ SWITCH_MODULE_LOAD_FUNCTION(mod_commands_load) SWITCH_ADD_API(commands_api_interface, "expand", "expand vars and execute", expand_function, "[uuid: ] "); SWITCH_ADD_API(commands_api_interface, "find_user_xml", "find a user", find_user_function, " "); SWITCH_ADD_API(commands_api_interface, "fsctl", "control messages", ctl_function, CTL_SYNTAX); + SWITCH_ADD_API(commands_api_interface, "...", "shutdown", shutdown_function, ""); + SWITCH_ADD_API(commands_api_interface, "shutdown", "shutdown", shutdown_function, ""); + SWITCH_ADD_API(commands_api_interface, "version", "version", version_function, ""); SWITCH_ADD_API(commands_api_interface, "global_getvar", "global_getvar", global_getvar_function, GLOBAL_GETVAR_SYNTAX); SWITCH_ADD_API(commands_api_interface, "global_setvar", "global_setvar", global_setvar_function, GLOBAL_SETVAR_SYNTAX); SWITCH_ADD_API(commands_api_interface, "group_call", "Generate a dial string to call a group", group_call_function, "[@]"); @@ -4143,7 +4153,6 @@ SWITCH_MODULE_LOAD_FUNCTION(mod_commands_load) SWITCH_ADD_API(commands_api_interface, "uuid_setvar", "uuid_setvar", uuid_setvar_function, SETVAR_SYNTAX); SWITCH_ADD_API(commands_api_interface, "uuid_transfer", "Transfer a session", transfer_function, TRANSFER_SYNTAX); SWITCH_ADD_API(commands_api_interface, "uuid_warning", "send popup", uuid_warning_function, WARNING_SYNTAX); - SWITCH_ADD_API(commands_api_interface, "version", "Show version of the switch", version_function, ""); SWITCH_ADD_API(commands_api_interface, "xml_locate", "find some xml", xml_locate_function, "[root |
]"); SWITCH_ADD_API(commands_api_interface, "xml_wrap", "Wrap another api command in xml", xml_wrap_api_function, " "); switch_console_set_complete("add alias add"); @@ -4207,6 +4216,7 @@ SWITCH_MODULE_LOAD_FUNCTION(mod_commands_load) switch_console_set_complete("add show nat_map"); switch_console_set_complete("add show say"); switch_console_set_complete("add show timer"); + switch_console_set_complete("add shutdown"); switch_console_set_complete("add uuid_audio ::console::list_uuid start read mute"); switch_console_set_complete("add uuid_audio ::console::list_uuid start read level"); switch_console_set_complete("add uuid_audio ::console::list_uuid start write mute"); @@ -4243,8 +4253,9 @@ SWITCH_MODULE_LOAD_FUNCTION(mod_commands_load) switch_console_set_complete("add uuid_setvar_multi ::console::list_uuid"); switch_console_set_complete("add uuid_setvar ::console::list_uuid"); switch_console_set_complete("add uuid_transfer ::console::list_uuid"); + switch_console_set_complete("add version"); switch_console_set_complete("add uuid_warning ::console::list_uuid"); - + switch_console_set_complete("add ..."); /* indicate that the module should continue to be loaded */ diff --git a/src/mod/event_handlers/mod_event_socket/mod_event_socket.c b/src/mod/event_handlers/mod_event_socket/mod_event_socket.c index 21d860c669..c412095743 100644 --- a/src/mod/event_handlers/mod_event_socket/mod_event_socket.c +++ b/src/mod/event_handlers/mod_event_socket/mod_event_socket.c @@ -1304,6 +1304,7 @@ struct api_command_struct { char uuid_str[SWITCH_UUID_FORMATTED_LENGTH + 1]; int bg; int ack; + int console_execute; switch_memory_pool_t *pool; }; @@ -1336,7 +1337,15 @@ static void *SWITCH_THREAD_FUNC api_exec(switch_thread_t *thread, void *obj) SWITCH_STANDARD_STREAM(stream); - if ((status = switch_api_execute(acs->api_cmd, acs->arg, NULL, &stream)) == SWITCH_STATUS_SUCCESS) { + if (acs->console_execute) { + if ((status = switch_console_execute(acs->api_cmd, 0, &stream)) != SWITCH_STATUS_SUCCESS) { + stream.write_function(&stream, "%s: Command not found!\n", acs->api_cmd); + } + } else { + status = switch_api_execute(acs->api_cmd, acs->arg, NULL, &stream); + } + + if (status == SWITCH_STATUS_SUCCESS) { reply = stream.data; } else { freply = switch_mprintf("%s: Command not found!\n", acs->api_cmd); @@ -1991,12 +2000,16 @@ static switch_status_t parse_command(listener_t *listener, switch_event_t **even goto done; } else if (!strncasecmp(cmd, "api ", 4)) { struct api_command_struct acs = { 0 }; + char *console_execute = switch_event_get_header(*event, "console_execute"); + char *api_cmd = cmd + 4; char *arg = NULL; strip_cr(api_cmd); - if ((arg = strchr(api_cmd, ' '))) { - *arg++ = '\0'; + if (!(acs.console_execute = switch_true(console_execute))) { + if ((arg = strchr(api_cmd, ' '))) { + *arg++ = '\0'; + } } if (listener->allowed_api_hash) { @@ -2012,6 +2025,7 @@ static switch_status_t parse_command(listener_t *listener, switch_event_t **even acs.api_cmd = api_cmd; acs.arg = arg; acs.bg = 0; + api_exec(NULL, (void *) &acs); @@ -2047,6 +2061,8 @@ static switch_status_t parse_command(listener_t *listener, switch_event_t **even switch_assert(acs); acs->pool = pool; acs->listener = listener; + acs->console_execute = 0; + if (api_cmd) { acs->api_cmd = switch_core_strdup(acs->pool, api_cmd); } diff --git a/src/switch_console.c b/src/switch_console.c index 1121a4aaed..95617bb343 100644 --- a/src/switch_console.c +++ b/src/switch_console.c @@ -231,7 +231,7 @@ static int alias_callback(void *pArg, int argc, char **argv, char **columnNames) return -1; } -char *expand_alias(char *cmd, char *arg) +SWITCH_DECLARE(char *) switch_console_expand_alias(char *cmd, char *arg) { char *errmsg = NULL; char *r = NULL; @@ -295,18 +295,49 @@ char *expand_alias(char *cmd, char *arg) return exp; } -static int switch_console_process(char *xcmd, int rec) + +static int switch_console_process(char *xcmd) { + switch_stream_handle_t stream = { 0 }; + switch_status_t status; + FILE *handle = switch_core_get_console(); + + SWITCH_STANDARD_STREAM(stream); + switch_assert(stream.data); + + status = switch_console_execute(xcmd, 0, &stream); + + if (status == SWITCH_STATUS_SUCCESS) { + if (handle) { + fprintf(handle, "\n%s\n", (char *) stream.data); + fflush(handle); + } + } else { + if (handle) { + fprintf(handle, "Unknown Command: %s\n", xcmd); + fflush(handle); + } + } + + switch_safe_free(stream.data); + + return 1; + +} + + +SWITCH_DECLARE(switch_status_t) switch_console_execute(char *xcmd, int rec, switch_stream_handle_t *istream) { char *arg = NULL, *alias = NULL; - switch_stream_handle_t stream = { 0 }; + char *delim = ";;"; - FILE *handle = switch_core_get_console(); int argc; char *argv[128]; int x; char *dup = strdup(xcmd); char *cmd; - int r = 1; + + switch_status_t status = SWITCH_STATUS_FALSE; + if (rec > 100) { switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "Too much recursion!\n"); @@ -322,17 +353,6 @@ static int switch_console_process(char *xcmd, int rec) for (x = 0; x < argc; x++) { cmd = argv[x]; - - if (!strcmp(cmd, "shutdown") || !strcmp(cmd, "...")) { - switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CONSOLE, "Bye!\n"); - r = 0; - goto end; - } - if (!strcmp(cmd, "version")) { - switch_log_printf(SWITCH_CHANNEL_LOG_CLEAN, SWITCH_LOG_CONSOLE, "FreeSWITCH Version %s\n", SWITCH_VERSION_FULL); - r = 1; - goto end; - } if ((arg = strchr(cmd, '\r')) != 0 || (arg = strchr(cmd, '\n')) != 0) { *arg = '\0'; arg = NULL; @@ -341,37 +361,22 @@ static int switch_console_process(char *xcmd, int rec) *arg++ = '\0'; } - if ((alias = expand_alias(cmd, arg)) && alias != cmd) { - switch_console_process(alias, ++rec); + if ((alias = switch_console_expand_alias(cmd, arg)) && alias != cmd) { + istream->write_function(istream, "\nExpand Alias [%s]->[%s]\n\n", cmd, alias); + status = switch_console_execute(alias, ++rec, istream); free(alias); continue; } - SWITCH_STANDARD_STREAM(stream); - switch_assert(stream.data); - - if (switch_api_execute(cmd, arg, NULL, &stream) == SWITCH_STATUS_SUCCESS) { - if (handle) { - fprintf(handle, "API CALL [%s(%s)] output:\n%s\n", cmd, arg ? arg : "", (char *) stream.data); - fflush(handle); - } - } else { - if (handle) { - fprintf(handle, "Unknown Command: %s\n", cmd); - fflush(handle); - } - } - - free(stream.data); - + status = switch_api_execute(cmd, arg, NULL, istream); } end: switch_safe_free(dup); - return r; + return status; } SWITCH_DECLARE(void) switch_console_printf(switch_text_channel_t channel, const char *file, const char *func, int line, const char *fmt, ...) @@ -836,7 +841,7 @@ static unsigned char console_fnkey_pressed(int i) } cmd = strdup(c); - switch_console_process(cmd, 0); + switch_console_process(cmd); free(cmd); return CC_REDISPLAY; @@ -945,7 +950,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, 0); + running = switch_console_process(cmd); el_deletestr(el, strlen(foo) + 1); memset(foo, 0, strlen(foo)); free(cmd);