[PATCH 1/2] cli: allow users to disable history if unused at all

Hanyuan Zhao hanyuan-z at qq.com
Sat Feb 17 14:54:43 CET 2024


This commit allows user to determine whether to have history recording
in command-line. Previously the history data as uninitialized static
array would not directly take much space in binary file since it only
marks size in the binary. However now it asks to allocate space. By
connecting the original CMD_HISTORY flag in Kconfig, users could unset
the whole history function, and the memory usage could be eased, if the
history function is not used at all.

Signed-off-by: Hanyuan Zhao <hanyuan-z at qq.com>
---
 cmd/Kconfig           |  1 +
 common/cli_readline.c | 44 ++++++++++++++++++++++++-------------------
 2 files changed, 26 insertions(+), 19 deletions(-)

diff --git a/cmd/Kconfig b/cmd/Kconfig
index a86b570517..af4dbc95fc 100644
--- a/cmd/Kconfig
+++ b/cmd/Kconfig
@@ -184,6 +184,7 @@ config CMD_FWU_METADATA
 
 config CMD_HISTORY
 	bool "history"
+	default y
 	depends on CMDLINE_EDITING
 	help
 	  Show the command-line history, i.e. a list of commands that are in
diff --git a/common/cli_readline.c b/common/cli_readline.c
index 2507be2295..eec6d8b0a2 100644
--- a/common/cli_readline.c
+++ b/common/cli_readline.c
@@ -67,12 +67,13 @@ static char *delete_char (char *buffer, char *p, int *colp, int *np, int plen)
 #define CTL_BACKSPACE		('\b')
 #define DEL			((char)255)
 #define DEL7			((char)127)
-#define CREAD_HIST_CHAR		('!')
 
 #define getcmd_putch(ch)	putc(ch)
 #define getcmd_getch()		getchar()
 #define getcmd_cbeep()		getcmd_putch('\a')
 
+#ifdef CONFIG_CMD_HISTORY
+#define CREAD_HIST_CHAR		('!')
 #ifdef CONFIG_SPL_BUILD
 #define HIST_MAX		3
 #define HIST_SIZE		32
@@ -90,14 +91,6 @@ static char *hist_list[HIST_MAX];
 
 #define add_idx_minus_one() ((hist_add_idx == 0) ? hist_max : hist_add_idx-1)
 
-static void getcmd_putchars(int count, int ch)
-{
-	int i;
-
-	for (i = 0; i < count; i++)
-		getcmd_putch(ch);
-}
-
 static int hist_init(void)
 {
 	unsigned char *hist;
@@ -110,7 +103,7 @@ static int hist_init(void)
 
 	hist = calloc(HIST_MAX, HIST_SIZE + 1);
 	if (!hist)
-		return -ENOMEM;
+		panic("%s: calloc: out of memory!\n", __func__);
 
 	for (i = 0; i < HIST_MAX; i++)
 		hist_list[i] = hist + (i * (HIST_SIZE + 1));
@@ -192,6 +185,15 @@ void cread_print_hist_list(void)
 		i++;
 	}
 }
+#endif
+
+static void getcmd_putchars(int count, int ch)
+{
+	int i;
+
+	for (i = 0; i < count; i++)
+		getcmd_putch(ch);
+}
 
 #define BEGINNING_OF_LINE() {			\
 	while (cls->num) {			\
@@ -365,6 +367,7 @@ int cread_line_process_ch(struct cli_line_state *cls, char ichar)
 			cls->eol_num--;
 		}
 		break;
+#ifdef CONFIG_CMD_HISTORY
 	case CTL_CH('p'):
 	case CTL_CH('n'):
 		if (cls->history) {
@@ -394,6 +397,7 @@ int cread_line_process_ch(struct cli_line_state *cls, char ichar)
 			break;
 		}
 		break;
+#endif
 	case '\t':
 		if (IS_ENABLED(CONFIG_AUTO_COMPLETE) && cls->cmd_complete) {
 			int num2, col;
@@ -490,19 +494,23 @@ static int cread_line(const char *const prompt, char *buf, unsigned int *len,
 	}
 	*len = cls->eol_num;
 
+#ifdef CONFIG_CMD_HISTORY
 	if (buf[0] && buf[0] != CREAD_HIST_CHAR)
 		cread_add_to_hist(buf);
 	hist_cur = hist_add_idx;
+#endif
 
 	return 0;
 }
 
 #else /* !CONFIG_CMDLINE_EDITING */
 
+#ifdef CONFIG_CMD_HISTORY
 static inline int hist_init(void)
 {
 	return 0;
 }
+#endif
 
 static int cread_line(const char *const prompt, char *buf, unsigned int *len,
 		      int timeout)
@@ -640,20 +648,18 @@ int cli_readline_into_buffer(const char *const prompt, char *buffer,
 	char *p = buffer;
 	uint len = CONFIG_SYS_CBSIZE;
 	int rc;
-	static int initted;
+#ifdef CONFIG_CMD_HISTORY
+	static int hist_initted;
+#endif
 
-	/*
-	 * History uses a global array which is not
-	 * writable until after relocation to RAM.
-	 * Revert to non-history version if still
-	 * running from flash.
-	 */
 	if (IS_ENABLED(CONFIG_CMDLINE_EDITING) && (gd->flags & GD_FLG_RELOC)) {
-		if (!initted) {
+#ifdef CONFIG_CMD_HISTORY
+		if (!hist_initted) {
 			rc = hist_init();
 			if (rc == 0)
-				initted = 1;
+				hist_initted = 1;
 		}
+#endif
 
 		if (prompt)
 			puts(prompt);
-- 
2.34.1



More information about the U-Boot mailing list