Skip to content

Commit

Permalink
shell: make shell_run run shell forever
Browse files Browse the repository at this point in the history
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]: RIOT-OS#10788
  • Loading branch information
miri64 committed Sep 19, 2019
1 parent 5631b69 commit 2c1a286
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 3 deletions.
32 changes: 30 additions & 2 deletions sys/include/shell.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
3 changes: 2 additions & 1 deletion sys/shell/shell.c
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down

0 comments on commit 2c1a286

Please sign in to comment.