From 048d745dfeb245d206d50d1b3381282a8d806682 Mon Sep 17 00:00:00 2001 From: Richard Fuchs Date: Fri, 17 Jan 2025 15:11:40 -0400 Subject: [PATCH] MT#55283 replace internal help with generated one Change-Id: I2bab78c0a1ce85c1868398d48bcd2a3bab847ee2 --- daemon/cli.c | 40 ++++++++++++-- utils/rtpengine-ctl | 129 +++----------------------------------------- 2 files changed, 43 insertions(+), 126 deletions(-) diff --git a/daemon/cli.c b/daemon/cli.c index c4e87da9e..9f88118c3 100644 --- a/daemon/cli.c +++ b/daemon/cli.c @@ -59,6 +59,7 @@ static void cli_incoming_active(str *instr, struct cli_writer *cw, const cli_han static void cli_incoming_standby(str *instr, struct cli_writer *cw, const cli_handler_t *); static void cli_incoming_debug(str *instr, struct cli_writer *cw, const cli_handler_t *); static void cli_incoming_call(str *instr, struct cli_writer *cw, const cli_handler_t *); +static void cli_incoming_usage(str *instr, struct cli_writer *cw, const cli_handler_t *); static void cli_incoming_set_maxopenfiles(str *instr, struct cli_writer *cw, const cli_handler_t *); static void cli_incoming_set_maxsessions(str *instr, struct cli_writer *cw, const cli_handler_t *); @@ -261,6 +262,7 @@ HANDLER_END #endif HANDLER_START(cli_top_handlers) + HANDLER_CMD("usage", cli_incoming_usage, NULL, "print the full list of all available commands") HANDLER_GENERIC("list", cli_list_handlers) HANDLER_CMD("terminate", cli_incoming_terminate, " | all | own | foreign", "terminate one particular call, or all (owned/foreign) calls") HANDLER_GENERIC("set", cli_set_handlers) @@ -284,19 +286,19 @@ static void cli_list_tag_info(struct cli_writer *cw, struct call_monologue *ml); -static void cli_handler_print_help(const cli_command_t *cmd, struct cli_writer *cw) { +static void __cli_handler_print_help(const cli_command_t *cmd, const str *prefix, struct cli_writer *cw) { if (!cmd->help) { if (cmd->params) - cw->cw_printf(cw, "\t%s %s\n", cmd->cmd, cmd->params); + cw->cw_printf(cw, STR_FORMAT "%s %s\n", STR_FMT(prefix), cmd->cmd, cmd->params); else - cw->cw_printf(cw, "\t%s\n", cmd->cmd); + cw->cw_printf(cw, STR_FORMAT "%s\n", STR_FMT(prefix), cmd->cmd); } else { size_t len = 0; if (cmd->params) - len += cw->cw_printf(cw, "\t%s %s", cmd->cmd, cmd->params); + len += cw->cw_printf(cw, STR_FORMAT "%s %s", STR_FMT(prefix), cmd->cmd, cmd->params); else - len += cw->cw_printf(cw, "\t%s", cmd->cmd); + len += cw->cw_printf(cw, STR_FORMAT "%s", STR_FMT(prefix), cmd->cmd); if (len < 50) cw->cw_printf(cw, "%.*s: %s\n", 50 - (int) len, " ", cmd->help); else @@ -304,6 +306,10 @@ static void cli_handler_print_help(const cli_command_t *cmd, struct cli_writer * } } +static void cli_handler_print_help(const cli_command_t *cmd, struct cli_writer *cw) { + __cli_handler_print_help(cmd, STR_PTR("\t"), cw); +} + static void cli_handler_do(const cli_handler_t *handler, str *instr, struct cli_writer *cw) { @@ -2114,3 +2120,27 @@ const char *cli_ng(ng_command_ctx_t *ctx) { return NULL; } + +static void cli_recurse_help(struct cli_writer *cw, const cli_handler_t *handler, str *prefix) { + for (unsigned int i = 0; i < handler->num_commands; i++) { + __auto_type c = &handler->commands[i]; + __cli_handler_print_help(c, prefix, cw); + + if (c->next) { + prefix->len += 4; + cli_recurse_help(cw, c->next, prefix); + prefix->len -= 4; + } + + if (prefix->len == 4) + cw->cw_printf(cw, "\n"); + } +} + +static void cli_incoming_usage(str *instr, struct cli_writer *cw, const cli_handler_t *handler) { + cw->cw_printf(cw, " Supported commands are:\n\n"); + + str prefix = STR(" "); + prefix.len = 4; + cli_recurse_help(cw, &cli_top_handlers, &prefix); +} diff --git a/utils/rtpengine-ctl b/utils/rtpengine-ctl index 13a68bf4d..62dd1a268 100755 --- a/utils/rtpengine-ctl +++ b/utils/rtpengine-ctl @@ -11,11 +11,12 @@ my $ip; my $port; my $conffile = '/etc/rtpengine/rtpengine.conf'; my $listen; +my $help; Getopt::Long::Configure('require_order'); my $optret = GetOptions( - 'help|h' => sub { showusage(); exit 0; }, + 'help|h' => \$help, 'ip=s' => \$ip, 'port=i' => \$port, 'config-file=s' => \$conffile, @@ -45,9 +46,12 @@ $argumentstring = trim($argumentstring); $ip //= '127.0.0.1'; $port //= 9900; -if (!$argumentstring || !$optret || $port <= 0 || $port > 65535) { - showusage(); - exit 1; +if ($help || !$argumentstring || !$optret || $port <= 0 || $port > 65535) { + $argumentstring = 'usage'; + + print "\n"; + print " rtpengine-ctl [ -ip [:] -port ] \n"; + print "\n"; } # create a connecting socket @@ -77,121 +81,4 @@ do { $socket->close(); -sub showusage { - print "\n"; - print " rtpengine-ctl [ -ip [:] -port ] \n"; - print "\n"; - print " Supported commands are:\n"; - print "\n"; - print " help : generated complete list of commands\n"; - print "\n"; - print " list