Skip to content

Commit 898a59e

Browse files
hsearlecarlescufi
authored andcommitted
Shell: Edit shell to store/retrieve return values
This change to the shell API enables the user to retrieve the return code of the most recently run shell command. This is useful both for users wanting specific information beyond the print statements in a command, but especially for automated users that prefer a simple return code to parsing human readable text. This was tested using all default shell commands, as well as eeprom, flash, and i2c, where specific errors could be forced. In all cases, the correct return value was returned by the new retval command. Signed-off-by: Hunter Searle <hsearle@xes-inc.com>
1 parent 7abcfc4 commit 898a59e

File tree

5 files changed

+45
-1
lines changed

5 files changed

+45
-1
lines changed

include/zephyr/shell/shell.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -823,6 +823,7 @@ struct shell_ctx {
823823

824824
struct k_mutex wr_mtx;
825825
k_tid_t tid;
826+
int ret_val;
826827
};
827828

828829
extern const struct log_backend_api log_backend_shell_api;
@@ -1239,6 +1240,15 @@ int shell_obscure_set(const struct shell *sh, bool obscure);
12391240
*/
12401241
int shell_mode_delete_set(const struct shell *sh, bool val);
12411242

1243+
/**
1244+
* @brief Retrieve return value of most recently executed shell command.
1245+
*
1246+
* @param[in] sh Pointer to the shell instance
1247+
*
1248+
* @retval return value of previous command
1249+
*/
1250+
int shell_get_return_value(const struct shell *sh);
1251+
12421252
/**
12431253
* @}
12441254
*/

subsys/shell/Kconfig

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,14 @@ config SHELL_AUTOSTART
251251
help
252252
If enabled, shell will be automatically started.
253253

254+
config SHELL_CMDS_RETURN_VALUE
255+
bool "Retval command"
256+
depends on SHELL_CMDS
257+
default y
258+
help
259+
This option enables the retval command. It is used to retrieve
260+
the return value from the most recently executed command.
261+
254262
source "subsys/shell/modules/Kconfig"
255263

256264
endif # SHELL

subsys/shell/shell.c

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1001,7 +1001,7 @@ static void state_collect(const struct shell *sh)
10011001
z_cursor_next_line_move(sh);
10021002
} else {
10031003
/* Command execution */
1004-
(void)execute(sh);
1004+
sh->ctx->ret_val = execute(sh);
10051005
}
10061006
/* Function responsible for printing prompt
10071007
* on received NL.
@@ -1670,6 +1670,15 @@ int shell_use_vt100_set(const struct shell *sh, bool val)
16701670
return (int)z_flag_use_vt100_set(sh, val);
16711671
}
16721672

1673+
int shell_get_return_value(const struct shell *sh)
1674+
{
1675+
if (sh == NULL) {
1676+
return -EINVAL;
1677+
}
1678+
1679+
return z_shell_get_return_value(sh);
1680+
}
1681+
16731682
int shell_echo_set(const struct shell *sh, bool val)
16741683
{
16751684
if (sh == NULL) {

subsys/shell/shell_cmds.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include "shell_vt100.h"
1111

1212
#define SHELL_MSG_CMD_NOT_SUPPORTED "Command not supported.\n"
13+
#define SHELL_HELP_RETVAL "Print return value of most recent command"
1314
#define SHELL_HELP_CLEAR "Clear screen."
1415
#define SHELL_HELP_BACKENDS "List active shell backends.\n"
1516
#define SHELL_HELP_BACKSPACE_MODE "Toggle backspace key mode.\n" \
@@ -402,6 +403,15 @@ static int cmd_resize(const struct shell *sh, size_t argc, char **argv)
402403
return 0;
403404
}
404405

406+
static int cmd_get_retval(const struct shell *sh, size_t argc, char **argv)
407+
{
408+
ARG_UNUSED(argc);
409+
ARG_UNUSED(argv);
410+
411+
shell_print(sh, "%d", shell_get_return_value(sh));
412+
return 0;
413+
}
414+
405415
static bool no_args(const struct shell_static_entry *entry)
406416
{
407417
return (entry->args.mandatory == 1) && (entry->args.optional == 0);
@@ -498,3 +508,5 @@ SHELL_COND_CMD_ARG_REGISTER(CONFIG_SHELL_CMDS_RESIZE, resize, &m_sub_resize,
498508
SHELL_COND_CMD_ARG_REGISTER(CONFIG_SHELL_CMDS_SELECT, select, NULL,
499509
SHELL_HELP_SELECT, cmd_select, 2,
500510
SHELL_OPT_ARG_CHECK_SKIP);
511+
SHELL_COND_CMD_ARG_REGISTER(CONFIG_SHELL_CMDS_RETURN_VALUE, retval, NULL,
512+
SHELL_HELP_RETVAL, cmd_get_retval, 1, 0);

subsys/shell/shell_ops.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,11 @@ static inline uint8_t z_flag_last_nl_get(const struct shell *sh)
185185
return sh->ctx->ctx.flags.last_nl;
186186
}
187187

188+
static inline int z_shell_get_return_value(const struct shell *sh)
189+
{
190+
return sh->ctx->ret_val;
191+
}
192+
188193
static inline void z_flag_last_nl_set(const struct shell *sh, uint8_t val)
189194
{
190195
sh->ctx->ctx.flags.last_nl = val;

0 commit comments

Comments
 (0)