2929 * This file is part of LwSHELL - Lightweight shell library.
3030 *
3131 * Author: Tilen MAJERLE <[email protected] > 32- * Version: v1.0 .0
32+ * Version: v1.1 .0
3333 */
3434#include <string.h>
3535#include "lwshell/lwshell.h"
3636
37+ /* Check enabled features */
38+ #if LWSHELL_CFG_USE_ENABLE_LIST_CMD && !LWSHELL_CFG_USE_OUTPUT
39+ #error "To use list command feature, LWSHELL_CFG_USE_OUTPUT must be enabled"
40+ #endif
41+
3742/* Default characters */
3843#define LWSHELL_ASCII_NULL 0x00/*!< Null character */
3944#define LWSHELL_ASCII_BACKSPACE 0x08/*!< Backspace */
@@ -87,7 +92,6 @@ static lwshell_t shell;
8792static void
8893prv_parse_input (lwshell_t * lw ) {
8994 size_t s_len ;
90- char ch , prev_ch ;
9195 char * str ;
9296
9397 lw = LWSHELL_GET_LW (lw );
@@ -100,14 +104,11 @@ prv_parse_input(lwshell_t* lw) {
100104
101105 /* Must be more than `1` character since we have to include end of line */
102106 if (lw -> buff_ptr > 0 ) {
103- uint8_t in_quote = 0 ;
104-
105107 /* Set default values */
106108 lw -> argc = 0 ;
107109 lw -> argv [0 ] = lw -> buff ;
108110
109111 /* Process complete input */
110- prev_ch = '\0' ;
111112 str = lw -> buff ;
112113
113114 /* Process complete string */
@@ -140,7 +141,7 @@ prv_parse_input(lwshell_t* lw) {
140141 }
141142 } else {
142143 lw -> argv [lw -> argc ++ ] = str ; /* Set start of argument directly on character */
143- while (( * str != ' ' && * str != '\0' ) ) {
144+ while (* str != ' ' && * str != '\0' ) {
144145 if (* str == '"' ) { /* Quote should not be here... */
145146 * str = '\0' ; /* ...add NULL termination to end token */
146147 }
@@ -159,10 +160,12 @@ prv_parse_input(lwshell_t* lw) {
159160 /* Check for command */
160161 if (lw -> argc > 0 && cmds_cnt > 0 ) {
161162 lwshell_cmd_t * c = NULL ;
163+ size_t arg_len = strlen (lw -> argv [0 ]);
164+
162165 /* Process all commands */
163166 for (size_t i = 0 ; i < cmds_cnt ; ++ i ) {
164- if (strlen ( lw -> argv [ 0 ]) == strlen (cmds [i ].name )
165- && strncmp (cmds [i ].name , lw -> argv [0 ], strlen ( lw -> argv [ 0 ]) ) == 0 ) {
167+ if (arg_len == strlen (cmds [i ].name )
168+ && strncmp (cmds [i ].name , lw -> argv [0 ], arg_len ) == 0 ) {
166169 c = & cmds [i ];
167170 break ;
168171 }
@@ -178,6 +181,18 @@ prv_parse_input(lwshell_t* lw) {
178181 } else {
179182 c -> fn (lw -> argc , lw -> argv );
180183 }
184+ #if LWSHELL_CFG_USE_ENABLE_LIST_CMD
185+ } else if (strncmp (lw -> argv [0 ], "listcmd" , 7 ) == 0 ) {
186+ LW_OUTPUT (lw , "List of registered commands\r\n" );
187+ for (size_t i = 0 ; i < cmds_cnt ; ++ i ) {
188+ LW_OUTPUT (lw , cmds [i ].name );
189+ LW_OUTPUT (lw , "\t\t\t" );
190+ LW_OUTPUT (lw , cmds [i ].desc );
191+ LW_OUTPUT (lw , "\r\n" );
192+ }
193+ #endif /* LWSHELL_CFG_USE_ENABLE_LIST_CMD */
194+ } else {
195+ LW_OUTPUT (lw , "Unknown command\r\n" );
181196 }
182197 }
183198 }
0 commit comments