From e9af9e02fe83182c9250384f99a05a46dc00b4af Mon Sep 17 00:00:00 2001 From: Michael Jerris Date: Sun, 8 Oct 2006 08:09:57 +0000 Subject: [PATCH] make help an api command, still need to add syntax and descriptions to the db so we can output those as well. git-svn-id: http://svn.freeswitch.org/svn/freeswitch/trunk@3002 d0543943-73ff-0310-b7d9-9358b9ac24b2 --- .../applications/mod_commands/mod_commands.c | 48 +++++++++++++++++-- src/switch_console.c | 8 ---- 2 files changed, 45 insertions(+), 11 deletions(-) diff --git a/src/mod/applications/mod_commands/mod_commands.c b/src/mod/applications/mod_commands/mod_commands.c index d6c552705e..5636d14f11 100644 --- a/src/mod/applications/mod_commands/mod_commands.c +++ b/src/mod/applications/mod_commands/mod_commands.c @@ -384,6 +384,7 @@ struct holder { switch_stream_handle_t *stream; char *http; uint32_t count; + int print_title; }; static int show_callback(void *pArg, int argc, char **argv, char **columnNames){ @@ -391,7 +392,7 @@ static int show_callback(void *pArg, int argc, char **argv, char **columnNames){ int x; - if (holder->count == 0) { + if (holder->print_title && holder->count == 0) { if (holder->http) { holder->stream->write_function(holder->stream, "\n"); } @@ -432,6 +433,7 @@ static switch_status_t show_function(char *cmd, switch_core_session_t *session, char *errmsg; switch_core_db_t *db = switch_core_db_handle(); struct holder holder = {0}; + int help = 0; if (session) { return SWITCH_STATUS_FALSE; @@ -441,6 +443,8 @@ static switch_status_t show_function(char *cmd, switch_core_session_t *session, holder.http = switch_event_get_header(stream->event, "http-host"); } + holder.print_title = 1; + if (!cmd) { sprintf (sql, "select * from interfaces"); } @@ -456,6 +460,17 @@ static switch_status_t show_function(char *cmd, switch_core_session_t *session, else if ( !strcmp(cmd,"channels")) { sprintf (sql, "select * from channels"); } + else if (!strncasecmp(cmd, "help", 4)) { + char *cmdname = NULL; + help = 1; + holder.print_title = 0; + if ((cmdname = strchr(cmd, ' ')) != 0) { + *cmdname++ = '\0'; + sprintf (sql, "select name from interfaces where type = 'api' and name = '%s'", cmdname); + } else { + sprintf (sql, "select name from interfaces where type = 'api'"); + } + } else { stream->write_function(stream, "USAGE: %s\n", show_api_interface.syntax); return SWITCH_STATUS_SUCCESS; @@ -474,11 +489,13 @@ static switch_status_t show_function(char *cmd, switch_core_session_t *session, holder.stream->write_function(holder.stream, ""); } - if (errmsg) { stream->write_function(stream, "SQL ERR [%s]\n",errmsg); switch_core_db_free(errmsg); errmsg = NULL; + } else if (help) { + if (holder.count == 0) + stream->write_function(stream, "No such command.\n"); } else { stream->write_function(stream, "\n%u total.\n", holder.count); } @@ -487,14 +504,39 @@ static switch_status_t show_function(char *cmd, switch_core_session_t *session, return SWITCH_STATUS_SUCCESS; } +static switch_status_t help_function(char *cmd, switch_core_session_t *session, switch_stream_handle_t *stream) +{ + char showcmd[1024]; + int all = 0; + if (switch_strlen_zero(cmd)) { + sprintf (showcmd, "help"); + all = 1; + } else { + sprintf (showcmd, "help %s", cmd); + } + if (all) + stream->write_function(stream, "\nValid Commands:\n\n"); + show_function(showcmd, session, stream); + if (all) + stream->write_function(stream, "version\n" "help - umm yeah..\n" "shutdown - stop the program\n\n"); + return SWITCH_STATUS_SUCCESS; +} + +static switch_api_interface_t help_api_interface = { + /*.interface_name */ "help", + /*.desc */ "Show help for all the api commands", + /*.function */ help_function, + /*.syntax */ "help", + /*.next */ NULL +}; static switch_api_interface_t ctl_api_interface = { /*.interface_name */ "fsctl", /*.desc */ "control messages", /*.function */ ctl_function, /*.syntax */ "fsctl [hupall|pause|resume|shutdown]", - /*.next */ + /*.next */ &help_api_interface }; static switch_api_interface_t uuid_bridge_api_interface = { diff --git a/src/switch_console.c b/src/switch_console.c index 2b2d821f58..cde782f85d 100644 --- a/src/switch_console.c +++ b/src/switch_console.c @@ -112,14 +112,6 @@ static int switch_console_process(char *cmd) switch_log_printf(SWITCH_CHANNEL_LOG_CLEAN, SWITCH_LOG_CONSOLE, "FreeSwitch Version %s\n", SWITCH_VERSION_FULL); return 1; } - if (!strcmp(cmd, "help")) { - switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CONSOLE, - "\n" - "Valid Commands:\n\n" - "version\n" "help - umm yeah..\n" "shutdown - stop the program\n\n"); - return 1; - } - if ((arg = strchr(cmd, '\r')) != 0 || (arg = strchr(cmd, '\n')) != 0 ) { *arg = '\0'; arg = NULL;