From 270c926c3b2fc89157fdf430ad8581a2199ef061 Mon Sep 17 00:00:00 2001 From: Tilen Majerle Date: Sun, 21 Nov 2021 18:37:56 +0100 Subject: [PATCH] Add option for "listcmd" to list all registered commands --- dev/VisualStudio/lwshell_opts.h | 3 ++- lwshell/src/include/lwshell/lwshell_opt.h | 11 ++++++++++- lwshell/src/lwshell/lwshell.c | 17 +++++++++++++++++ 3 files changed, 29 insertions(+), 2 deletions(-) diff --git a/dev/VisualStudio/lwshell_opts.h b/dev/VisualStudio/lwshell_opts.h index 5ea5472..da21680 100644 --- a/dev/VisualStudio/lwshell_opts.h +++ b/dev/VisualStudio/lwshell_opts.h @@ -38,6 +38,7 @@ #include "windows.h" -#define LWSHELL_CFG_USE_OUTPUT 1 +#define LWSHELL_CFG_USE_OUTPUT 1 +#define LWSHELL_CFG_USE_ENABLE_LIST_CMD 1 #endif /* LWSHELL_HDR_OPTS_H */ diff --git a/lwshell/src/include/lwshell/lwshell_opt.h b/lwshell/src/include/lwshell/lwshell_opt.h index 070d484..6de5ede 100644 --- a/lwshell/src/include/lwshell/lwshell_opt.h +++ b/lwshell/src/include/lwshell/lwshell_opt.h @@ -1,4 +1,4 @@ -/** +/** * \file lwshell_opt.h * \brief LwSHELL options */ @@ -97,6 +97,15 @@ extern "C" { #define LWSHELL_CFG_USE_OUTPUT 1 #endif +/** + * \brief Enables `1` or disables `0` generic ˙listcmd` command to list of registered commands + * + * \ref LWSHELL_CFG_USE_OUTPUT must be enabled to use this feature + */ +#ifndef LWSHELL_CFG_USE_ENABLE_LIST_CMD +#define LWSHELL_CFG_USE_ENABLE_LIST_CMD 0 +#endif + /** * \} */ diff --git a/lwshell/src/lwshell/lwshell.c b/lwshell/src/lwshell/lwshell.c index cbf6154..6dd782f 100644 --- a/lwshell/src/lwshell/lwshell.c +++ b/lwshell/src/lwshell/lwshell.c @@ -34,6 +34,11 @@ #include #include "lwshell/lwshell.h" +/* Check enabled features */ +#if LWSHELL_CFG_USE_ENABLE_LIST_CMD && !LWSHELL_CFG_USE_OUTPUT +#error "To use list command feature, LWSHELL_CFG_USE_OUTPUT must be enabled" +#endif + /* Default characters */ #define LWSHELL_ASCII_NULL 0x00/*!< Null character */ #define LWSHELL_ASCII_BACKSPACE 0x08/*!< Backspace */ @@ -176,6 +181,18 @@ prv_parse_input(lwshell_t* lw) { } else { c->fn(lw->argc, lw->argv); } +#if LWSHELL_CFG_USE_ENABLE_LIST_CMD + } else if (strncmp(lw->argv[0], "listcmd", 7) == 0) { + LW_OUTPUT(lw, "List of registered commands\r\n"); + for (size_t i = 0; i < cmds_cnt; ++i) { + LW_OUTPUT(lw, cmds[i].name); + LW_OUTPUT(lw, "\t\t\t"); + LW_OUTPUT(lw, cmds[i].desc); + LW_OUTPUT(lw, "\r\n"); + } +#endif /* LWSHELL_CFG_USE_ENABLE_LIST_CMD */ + } else { + LW_OUTPUT(lw, "Unknown command\r\n"); } } }