]> cloudbase.mooo.com Git - z180-stamp.git/blobdiff - avr/cli_readline.c
Store only command line input in history buffer, but not data (i.e mm command)
[z180-stamp.git] / avr / cli_readline.c
index 5545015c27436cd17370702882bca0007f99646b..e4c3f5ad5893fbee9e75d62a9c7fe5481b056c3e 100644 (file)
@@ -11,6 +11,7 @@
  * SPDX-License-Identifier:    GPL-2.0
  */
 
+#include "cli_readline.h"
 #include "common.h"
 #include <string.h>
 #include <stdio.h>
@@ -22,7 +23,6 @@
 #include "con-utils.h"
 #include "print-utils.h"
 #include "command.h"
-#include "cli_readline.h"
 
 
 
@@ -357,7 +357,8 @@ static void cread_add_str(char *str, int strsize, bool insert,
        }
 }
 
-static int cread_line(const FLASH char *const prompt, char *buf, uint_fast8_t *len)
+static int cread_line(const FLASH char *const prompt, char *buf,
+                       uint_fast8_t *len, bool enable_history)
 {
        uint_fast8_t num = 0;
        uint_fast8_t eol_num = 0;
@@ -455,32 +456,33 @@ static int cread_line(const FLASH char *const prompt, char *buf, uint_fast8_t *l
                case CTL_CH('p'):                       /* previous-history */
                case KEY_DOWN:
                case CTL_CH('n'):                       /* next-history */
-               {
-                       char *hline;
-
-                       if (ichar == CTL_CH('p') || ichar == KEY_UP)
-                               hline = hist_prev();
-                       else
-                               hline = hist_next();
-
-                       if (!hline) {
+                       if (enable_history) {
+                               char *hline;
+
+                               if (ichar == CTL_CH('p') || ichar == KEY_UP)
+                                       hline = hist_prev();
+                               else
+                                       hline = hist_next();
+
+                               if (hline) {
+                                       /* nuke the current line */
+                                       /* first, go home */
+                                       BEGINNING_OF_LINE();
+
+                                       /* erase to end of line */
+                                       ERASE_TO_EOL();
+
+                                       /* copy new line into place and display */
+                                       strcpy(buf, hline);
+                                       eol_num = strlen(buf);
+                                       REFRESH_TO_EOL();
+                               } else {
+                                       getcmd_cbeep();
+                               }
+                       } else {
                                getcmd_cbeep();
-                               continue;
                        }
-
-                       /* nuke the current line */
-                       /* first, go home */
-                       BEGINNING_OF_LINE();
-
-                       /* erase to end of line */
-                       ERASE_TO_EOL();
-
-                       /* copy new line into place and display */
-                       strcpy(buf, hline);
-                       eol_num = strlen(buf);
-                       REFRESH_TO_EOL();
-                       continue;
-               }
+                       break;
 #ifdef CONFIG_AUTO_COMPLETE
                case '\t': {
                        int num2, col;
@@ -512,16 +514,18 @@ static int cread_line(const FLASH char *const prompt, char *buf, uint_fast8_t *l
        *len = eol_num;
        buf[eol_num] = '\0';    /* lose the newline */
 
-       if (buf[0] /* && buf[0] != CREAD_HIST_CHAR */)
-               cread_add_to_hist(buf);
-       hist_cur = hist_add_idx;
-
+       if (enable_history) {
+               if (buf[0])
+                       cread_add_to_hist(buf);
+               hist_cur = hist_add_idx;
+       }
        return 0;
 }
 
 /****************************************************************************/
 
-static int cli_readline_into_buffer(const FLASH char *const prompt, char *buffer)
+static int cli_readline_into_buffer(const FLASH char *const prompt,
+                       char *buffer, bool enable_history)
 {
        char *p = buffer;
        uint_fast8_t len = CONFIG_SYS_CBSIZE;
@@ -530,11 +534,11 @@ static int cli_readline_into_buffer(const FLASH char *const prompt, char *buffer
        if (prompt)
                my_puts_P(prompt);
 
-       rc = cread_line(prompt, p, &len);
+       rc = cread_line(prompt, p, &len, enable_history);
        return rc < 0 ? rc : (int) len;
 }
 
-int cli_readline(const FLASH char *const prompt)
+int cli_readline(const FLASH char *const prompt, bool enable_history)
 {
        /*
         * If console_buffer isn't 0-length the user will be prompted to modify
@@ -542,5 +546,5 @@ int cli_readline(const FLASH char *const prompt)
         */
        console_buffer[0] = '\0';
 
-       return cli_readline_into_buffer(prompt, console_buffer);
+       return cli_readline_into_buffer(prompt, console_buffer, enable_history);
 }