Skip to content

Commit 9b8682c

Browse files
nordic-krchJakub Rzeszutko
authored and
Jakub Rzeszutko
committed
shell: Add wildcard support
Extended shell to support wildcard characters: * and ? and expand commands accordingly. Increased default stack size. Signed-off-by: Jakub Rzeszutko <[email protected]> Signed-off-by: Krzysztof Chruscinski <[email protected]>
1 parent eb4e216 commit 9b8682c

File tree

5 files changed

+365
-4
lines changed

5 files changed

+365
-4
lines changed

subsys/shell/CMakeLists.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,8 @@ zephyr_sources_ifdef(
3333
CONFIG_LOG
3434
shell_log_backend.c
3535
)
36+
37+
zephyr_sources_ifdef(
38+
CONFIG_SHELL_WILDCARD
39+
shell_wildcard.c
40+
)

subsys/shell/Kconfig

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,10 @@
77
# SPDX-License-Identifier: Apache-2.0
88
#
99

10+
menu "Shell Options"
1011

11-
config CONSOLE_SHELL
12-
bool "Enable console input handler [ Experimental ]"
12+
menuconfig CONSOLE_SHELL
13+
bool "Enable legacy shell [ Experimental ]"
1314
select CONSOLE_HANDLER
1415
select CONSOLE_SUBSYS
1516
help
@@ -34,7 +35,7 @@ source "subsys/shell/modules/Kconfig"
3435

3536
endif
3637

37-
config SHELL
38+
menuconfig SHELL
3839
bool "Enable shell"
3940
select LOG_RUNTIME_FILTERING
4041
select POLL
@@ -47,7 +48,7 @@ source "subsys/logging/Kconfig.template.log_config"
4748

4849
config SHELL_STACK_SIZE
4950
int "Shell thread stack size"
50-
default 1024 if MULTITHREADING
51+
default 2048 if MULTITHREADING
5152
default 0 if !MULTITHREADING
5253
help
5354
Stack size for thread created for each instance.
@@ -89,6 +90,12 @@ config SHELL_ARGC_MAX
8990
If command is composed of more than defined, argument SHELL_ARGC_MAX
9091
and following are passed as one argument in the string.
9192

93+
config SHELL_WILDCARD
94+
bool "Enable wildcard support in shell"
95+
select FNMATCH
96+
help
97+
Enables using * in shell.
98+
9299
config SHELL_ECHO_STATUS
93100
bool "Enable echo on shell"
94101
default y
@@ -116,6 +123,7 @@ config SHELL_HELP
116123

117124
config SHELL_HELP_ON_WRONG_ARGUMENT_COUNT
118125
bool "Enable printing help on wrong argument count"
126+
depends on SHELL_HELP
119127
default y
120128

121129
config SHELL_HISTORY
@@ -163,3 +171,5 @@ config SHELL_CMDS_RESIZE
163171
Resize command can be turned off to safe code memory (~0,5k).
164172

165173
endif #SHELL
174+
endmenu
175+

subsys/shell/shell.c

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include <shell/shell.h>
1111
#include "shell_utils.h"
1212
#include "shell_ops.h"
13+
#include "shell_wildcard.h"
1314
#include "shell_vt100.h"
1415
#include <assert.h>
1516
#include <atomic.h>
@@ -381,6 +382,15 @@ static const struct shell_static_entry *get_last_command(
381382
*match_arg = SHELL_CMD_ROOT_LVL;
382383

383384
while (*match_arg < argc) {
385+
386+
if (IS_ENABLED(CONFIG_SHELL_WILDCARD)) {
387+
/* ignore wildcard argument */
388+
if (shell_wildcard_character_exist(argv[*match_arg])) {
389+
(*match_arg)++;
390+
continue;
391+
}
392+
}
393+
384394
entry = find_cmd(prev_cmd, *match_arg, argv[*match_arg],
385395
d_entry);
386396
if (entry) {
@@ -922,6 +932,7 @@ static void shell_execute(const struct shell *shell)
922932
const struct shell_cmd_entry *p_cmd = NULL;
923933
size_t cmd_lvl = SHELL_CMD_ROOT_LVL;
924934
size_t cmd_with_handler_lvl = 0;
935+
bool wildcard_found = false;
925936
size_t cmd_idx;
926937
size_t argc;
927938
char quote;
@@ -938,6 +949,10 @@ static void shell_execute(const struct shell *shell)
938949
history_put(shell, shell->ctx->cmd_buff,
939950
shell->ctx->cmd_buff_len);
940951

952+
if (IS_ENABLED(CONFIG_SHELL_WILDCARD)) {
953+
shell_wildcard_prepare(shell);
954+
}
955+
941956
/* create argument list */
942957
quote = shell_make_argv(&argc, &argv[0], shell->ctx->cmd_buff,
943958
CONFIG_SHELL_ARGC_MAX);
@@ -985,6 +1000,28 @@ static void shell_execute(const struct shell *shell)
9851000
break;
9861001
}
9871002

1003+
if (IS_ENABLED(CONFIG_SHELL_WILDCARD)) {
1004+
enum shell_wildcard_status status;
1005+
1006+
status = shell_wildcard_process(shell, p_cmd,
1007+
argv[cmd_lvl]);
1008+
/* Wildcard character found but there is no matching
1009+
* command.
1010+
*/
1011+
if (status == SHELL_WILDCARD_CMD_NO_MATCH_FOUND) {
1012+
break;
1013+
}
1014+
1015+
/* Wildcard character was not found function can process
1016+
* argument.
1017+
*/
1018+
if (status != SHELL_WILDCARD_NOT_FOUND) {
1019+
++cmd_lvl;
1020+
wildcard_found = true;
1021+
continue;
1022+
}
1023+
}
1024+
9881025
cmd_get(p_cmd, cmd_lvl, cmd_idx++, &p_static_entry, &d_entry);
9891026

9901027
if ((cmd_idx == 0) || (p_static_entry == NULL)) {
@@ -994,6 +1031,28 @@ static void shell_execute(const struct shell *shell)
9941031
if (strcmp(argv[cmd_lvl], p_static_entry->syntax) == 0) {
9951032
/* checking if command has a handler */
9961033
if (p_static_entry->handler != NULL) {
1034+
if (IS_ENABLED(CONFIG_SHELL_WILDCARD)) {
1035+
if (wildcard_found) {
1036+
shell_op_cursor_end_move(shell);
1037+
shell_op_cond_next_line(shell);
1038+
1039+
/* An error occurred, fnmatch
1040+
* argument cannot be followed
1041+
* by argument with a handler to
1042+
* avoid multiple function
1043+
* calls.
1044+
*/
1045+
shell_fprintf(shell,
1046+
SHELL_ERROR,
1047+
"Error: requested"
1048+
" multiple function"
1049+
" executions\r\n");
1050+
help_flag_clear(shell);
1051+
1052+
return;
1053+
}
1054+
}
1055+
9971056
shell->ctx->active_cmd = *p_static_entry;
9981057
cmd_with_handler_lvl = cmd_lvl;
9991058
}
@@ -1004,6 +1063,17 @@ static void shell_execute(const struct shell *shell)
10041063
}
10051064
}
10061065

1066+
if (IS_ENABLED(CONFIG_SHELL_WILDCARD)) {
1067+
shell_wildcard_finalize(shell);
1068+
/* cmd_buffer has been overwritten by function finalize function
1069+
* with all expanded commands. Hence shell_make_argv needs to
1070+
* be called again.
1071+
*/
1072+
(void)shell_make_argv(&argc, &argv[0],
1073+
shell->ctx->cmd_buff,
1074+
CONFIG_SHELL_ARGC_MAX);
1075+
}
1076+
10071077
/* Executing the deepest found handler. */
10081078
if (shell->ctx->active_cmd.handler == NULL) {
10091079
if (shell->ctx->active_cmd.help) {

0 commit comments

Comments
 (0)