From 2c1a2863cee9c272ad82725c3cf666a578b50fdf Mon Sep 17 00:00:00 2001 From: "Martine S. Lenders" Date: Thu, 19 Sep 2019 10:26:31 +0200 Subject: [PATCH] shell: make shell_run run shell forever This change is in preparation to [PR 10788]. PR 10788 will make the shell exitable which may lead to unexpected behavior in comparison to previous usage of the shell. To prevent this, this PR introduces two "new" functions to the shell's API: `shell_run_once()` and `shell_run_forever()`. `shell_run_once()` basically has the same behavior as `shell_run()` in current master: Start a shell and continue reading lines until EOF is reached. `shell_run_forever()` wraps around `shell_run_once()` and restarts the shell if it exits. `shell_run()` is re-introduced as a back-porting alias for `shell_run_forever()`. As a consequence all current calls to `shell_run()` won't exit even with [PR 10788] merged (which would add EOT as additional exit condition for `shell_run_once()`). [PR 10788]: https://github.com/RIOT-OS/RIOT/pull/10788 --- sys/include/shell.h | 32 ++++++++++++++++++++++++++++++-- sys/shell/shell.c | 3 ++- 2 files changed, 32 insertions(+), 3 deletions(-) diff --git a/sys/include/shell.h b/sys/include/shell.h index 2c2766936b..234fe80b35 100644 --- a/sys/include/shell.h +++ b/sys/include/shell.h @@ -64,13 +64,41 @@ typedef struct shell_command_t { } shell_command_t; /** - * @brief Start a shell. + * @brief Start a shell and exit once EOF is reached. * * @param[in] commands ptr to array of command structs * @param[in] line_buf Buffer that will be used for reading a line * @param[in] len nr of bytes that fit in line_buf */ -void shell_run(const shell_command_t *commands, char *line_buf, int len); +void shell_run_once(const shell_command_t *commands, char *line_buf, int len); + +/** + * @brief Start a shell and restart it if it exits + * + * @param[in] commands ptr to array of command structs + * @param[in] line_buf Buffer that will be used for reading a line + * @param[in] len nr of bytes that fit in line_buf + */ +static inline void shell_run_forever(const shell_command_t *commands, + char *line_buf, int len) +{ + while (1) { + shell_run_once(commands, line_buf, len); + } +} + +/** + * @brief Back-porting alias for @ref shell_run_forever + * + * @param[in] commands ptr to array of command structs + * @param[in] line_buf Buffer that will be used for reading a line + * @param[in] len nr of bytes that fit in line_buf + */ +static inline void shell_run(const shell_command_t *commands, + char *line_buf, int len) +{ + shell_run_forever(commands, line_buf, len); +} #ifdef __cplusplus } diff --git a/sys/shell/shell.c b/sys/shell/shell.c index f791525394..6948e3e4a9 100644 --- a/sys/shell/shell.c +++ b/sys/shell/shell.c @@ -290,7 +290,8 @@ static inline void print_prompt(void) flush_if_needed(); } -void shell_run(const shell_command_t *shell_commands, char *line_buf, int len) +void shell_run_once(const shell_command_t *shell_commands, + char *line_buf, int len) { print_prompt();