From daea5ceada5ada42de0230e33f03efcafb2684f0 Mon Sep 17 00:00:00 2001 From: Gokul Sivakumar Date: Wed, 3 Nov 2021 22:20:21 +0530 Subject: [PATCH] wlantest: Add new cli "help" command Having a help cli command to view all the supported commands is helpful when running the wlantest_cli manually instead via the python test scripts. $ wlantest_cli help commands: ping = test connection to wlantest terminate = terminate wlantest list_bss = get BSS list list_sta = get STA list flush = drop all collected BSS data clear_sta_counters = clear STA counters clear_bss_counters = clear BSS counters get_sta_counter = get STA counter value get_bss_counter = get BSS counter value inject send version = get wlantest version add_passphrase = add a known passphrase add_wepkey = add a known WEP key info_sta = get STA information info_bss = get BSS information clear_tdls_counters = clear TDLS counters get_tdls_counter = get TDLS counter value get_bss_counter = get BSS counter value relog = re-open log-file (allow rolling logs) get_tx_tid = get STA TX TID counter value get_rx_tid = get STA RX TID counter value help = show this usage help $ wlantest_cli help add_passphrase commands: add_passphrase = add a known passphrase Signed-off-by: Gokul Sivakumar --- wlantest/wlantest_cli.c | 60 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) diff --git a/wlantest/wlantest_cli.c b/wlantest/wlantest_cli.c index ad5a48dea..7aa0c9ce2 100644 --- a/wlantest/wlantest_cli.c +++ b/wlantest/wlantest_cli.c @@ -14,6 +14,9 @@ #include "utils/edit.h" #include "wlantest_ctrl.h" +static void print_help(FILE *stream, const char *cmd); +static char ** wlantest_cli_cmd_list(void); + static int get_cmd_arg_num(const char *str, int pos) { @@ -1566,6 +1569,28 @@ static char ** complete_get_tid(int s, const char *str, int pos) } +static int wlantest_cli_cmd_help(int s, int argc, char *argv[]) +{ + print_help(stdout, argc > 0 ? argv[0] : NULL); + return 0; +} + + +static char ** wlantest_cli_complete_help(int s, const char *str, int pos) +{ + int arg = get_cmd_arg_num(str, pos); + char **res = NULL; + + switch (arg) { + case 1: + res = wlantest_cli_cmd_list(); + break; + } + + return res; +} + + struct wlantest_cli_cmd { const char *cmd; int (*handler)(int s, int argc, char *argv[]); @@ -1623,10 +1648,45 @@ static const struct wlantest_cli_cmd wlantest_cli_commands[] = { { "get_rx_tid", cmd_get_rx_tid, " = get STA RX TID counter value", complete_get_tid }, + { "help", wlantest_cli_cmd_help, + "= show this usage help", wlantest_cli_complete_help }, { NULL, NULL, NULL, NULL } }; +/* + * Prints command usage, lines are padded with the specified string. + */ +static void print_cmd_help(FILE *stream, const struct wlantest_cli_cmd *cmd, + const char *pad) +{ + char c; + size_t n; + + if (!cmd->usage) + return; + fprintf(stream, "%s%s ", pad, cmd->cmd); + for (n = 0; (c = cmd->usage[n]); n++) { + fprintf(stream, "%c", c); + if (c == '\n') + fprintf(stream, "%s", pad); + } + fprintf(stream, "\n"); +} + + +static void print_help(FILE *stream, const char *cmd) +{ + int n; + + fprintf(stream, "commands:\n"); + for (n = 0; wlantest_cli_commands[n].cmd; n++) { + if (!cmd || str_starts(wlantest_cli_commands[n].cmd, cmd)) + print_cmd_help(stream, &wlantest_cli_commands[n], " "); + } +} + + static int ctrl_command(int s, int argc, char *argv[]) { const struct wlantest_cli_cmd *cmd, *match = NULL;