From 0aabb7e5adc270fbfd31b331a8e3ee3397a24a6e Mon Sep 17 00:00:00 2001 From: Giovanni Di Sirio Date: Sat, 16 Apr 2016 07:27:28 +0000 Subject: [PATCH] More shell enhancements. git-svn-id: svn://svn.code.sf.net/p/chibios/svn/trunk@9285 35acf78f-673a-0410-8e92-d51de3d6d3f4 --- os/various/shell/shell.c | 35 +++++++++++++++++++++++++++-------- os/various/shell/shell.h | 6 ++++-- 2 files changed, 31 insertions(+), 10 deletions(-) diff --git a/os/various/shell/shell.c b/os/various/shell/shell.c index 341f9eb06..19de2f958 100644 --- a/os/various/shell/shell.c +++ b/os/various/shell/shell.c @@ -135,6 +135,9 @@ static bool is_histbuff_space(ShellHistory *shp, int length) { static void save_history(ShellHistory *shp, char *line, int length) { + if (shp == NULL) + return; + if (length > shp->sh_size - 2) return; @@ -172,6 +175,9 @@ static void save_history(ShellHistory *shp, char *line, int length) { static int get_history(ShellHistory *shp, char *line, int dir) { int count=0; + if (shp == NULL) + return -1; + /* Count the number of lines saved in the buffer */ int idx = shp->sh_beg; while (idx != shp->sh_end) { @@ -332,11 +338,25 @@ THD_FUNCTION(shellThread, p) { char *lp, *cmd, *tokp, line[SHELL_MAX_LINE_LENGTH]; char *args[SHELL_MAX_ARGUMENTS + 1]; +#if SHELL_USE_HISTORY == TRUE + *(scfg->sc_histbuf) = 0; + ShellHistory hist = { + scfg->sc_histbuf, + scfg->sc_histsize, + 0, + 0, + 0 + }; + ShellHistory *shp = &hist; +#else + ShellHistory *shp = NULL; +#endif + chprintf(chp, SHELL_NEWLINE_STR); chprintf(chp, "ChibiOS/RT Shell"SHELL_NEWLINE_STR); while (true) { chprintf(chp, SHELL_PROMPT_STR); - if (shellGetLine(scfg, line, sizeof(line))) { + if (shellGetLine(scfg, line, sizeof(line), shp)) { #if (SHELL_CMD_EXIT_ENABLED == TRUE) && !defined(_CHIBIOS_NIL_) chprintf(chp, SHELL_NEWLINE_STR); chprintf(chp, "logout"); @@ -424,22 +444,23 @@ void shellExit(msg_t msg) { * @param[in] scfg pointer to a @p ShellConfig object * @param[in] line pointer to the line buffer * @param[in] size buffer maximum length + * @param[in] shp pointer to a @p ShellHistory object or NULL * @return The operation status. * @retval true the channel was reset or CTRL-D pressed. * @retval false operation successful. * * @api */ -bool shellGetLine(ShellConfig *scfg, char *line, unsigned size) { +bool shellGetLine(ShellConfig *scfg, char *line, unsigned size, ShellHistory *shp) { char *p = line; BaseSequentialStream *chp = scfg->sc_channel; -#if (SHELL_USE_ESC_SEQ == TRUE) +#if SHELL_USE_ESC_SEQ == TRUE bool escape = false; bool bracket = false; #endif -#if (SHELL_USE_HISTORY == TRUE) - ShellHistory *shp = scfg->sc_history; +#if SHELL_USE_HISTORY != TRUE + (void) shp; #endif while (true) { @@ -510,9 +531,7 @@ bool shellGetLine(ShellConfig *scfg, char *line, unsigned size) { if (c == '\r') { chprintf(chp, SHELL_NEWLINE_STR); #if SHELL_USE_HISTORY == TRUE - if (shp != NULL) { - save_history(shp, line, p - line); - } + save_history(shp, line, p - line); #endif *p = 0; return false; diff --git a/os/various/shell/shell.h b/os/various/shell/shell.h index 4620ded8b..b155ec99b 100644 --- a/os/various/shell/shell.h +++ b/os/various/shell/shell.h @@ -148,8 +148,10 @@ typedef struct { const ShellCommand *sc_commands; /**< @brief Shell extra commands table. */ #if (SHELL_USE_HISTORY == TRUE) || defined(__DOXYGEN__) - ShellHistory *sc_history; /**< @brief Shell command history + char *sc_histbuf; /**< @brief Shell command history buffer. */ + const int sc_histsize; /**< @brief Shell history buffer + size. */ #endif #if (SHELL_USE_COMPLETION == TRUE) || defined(__DOXYGEN__) char **sc_completion; /**< @brief Shell command completion @@ -207,7 +209,7 @@ extern "C" { void shellInit(void); THD_FUNCTION(shellThread, p); void shellExit(msg_t msg); - bool shellGetLine(ShellConfig *scfg, char *line, unsigned size); + bool shellGetLine(ShellConfig *scfg, char *line, unsigned size, ShellHistory *shp); #ifdef __cplusplus } #endif