[git commit] bc: do not allocate line editing state until needed

Denys Vlasenko vda.linux at googlemail.com
Mon Dec 21 20:36:58 UTC 2020


commit: https://git.busybox.net/busybox/commit/?id=00eb23b47aa79461b913b320eba3c95b90e6eec4
branch: https://git.busybox.net/busybox/commit/?id=refs/heads/master

function                                             old     new   delta
xc_read_line                                         324     353     +29
free_line_input_t                                     34      39      +5
xc_vm_init                                           656     640     -16
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 2/1 up/down: 34/-16)             Total: 18 bytes

Signed-off-by: Denys Vlasenko <vda.linux at googlemail.com>
---
 include/libbb.h  |  4 ++++
 libbb/lineedit.c | 18 +++++++++++-------
 miscutils/bc.c   | 37 ++++++++++++++++++-------------------
 shell/ash.c      |  3 +--
 shell/hush.c     |  3 +--
 5 files changed, 35 insertions(+), 30 deletions(-)

diff --git a/include/libbb.h b/include/libbb.h
index 1c3d905b6..cae54658b 100644
--- a/include/libbb.h
+++ b/include/libbb.h
@@ -1906,7 +1906,11 @@ enum {
 	FOR_SHELL        = DO_HISTORY | TAB_COMPLETION | USERNAME_COMPLETION,
 };
 line_input_t *new_line_input_t(int flags) FAST_FUNC;
+#if ENABLE_FEATURE_EDITING_SAVEHISTORY
 void free_line_input_t(line_input_t *n) FAST_FUNC;
+#else
+# define free_line_input_t(n) free(n)
+#endif
 /*
  * maxsize must be >= 2.
  * Returns:
diff --git a/libbb/lineedit.c b/libbb/lineedit.c
index c3b5738e2..b3e7abac5 100644
--- a/libbb/lineedit.c
+++ b/libbb/lineedit.c
@@ -1417,15 +1417,19 @@ void FAST_FUNC show_history(const line_input_t *st)
 		printf("%4d %s\n", i, st->history[i]);
 }
 
+# if ENABLE_FEATURE_EDITING_SAVEHISTORY
 void FAST_FUNC free_line_input_t(line_input_t *n)
 {
-# if ENABLE_FEATURE_EDITING_SAVEHISTORY
-	int i = n->cnt_history;
-	while (i > 0)
-		free(n->history[--i]);
-#endif
-	free(n);
+	if (n) {
+		int i = n->cnt_history;
+		while (i > 0)
+			free(n->history[--i]);
+		free(n);
+	}
 }
+# else
+/* #defined to free() in libbb.h */
+# endif
 
 # if ENABLE_FEATURE_EDITING_SAVEHISTORY
 /* We try to ensure that concurrent additions to the history
@@ -1506,7 +1510,7 @@ void save_history(line_input_t *st)
 {
 	FILE *fp;
 
-	if (!st->hist_file)
+	if (!st || !st->hist_file)
 		return;
 	if (st->cnt_history <= st->cnt_history_in_file)
 		return;
diff --git a/miscutils/bc.c b/miscutils/bc.c
index f339b895c..1227e2d13 100644
--- a/miscutils/bc.c
+++ b/miscutils/bc.c
@@ -2545,6 +2545,8 @@ static void xc_read_line(BcVec *vec, FILE *fp)
 # if ENABLE_FEATURE_EDITING
 	if (G_ttyin && fp == stdin) {
 		int n, i;
+		if (!G.line_input_state)
+			G.line_input_state = new_line_input_t(DO_HISTORY);
 #  define line_buf bb_common_bufsiz1
 		n = read_line_input(G.line_input_state, "", line_buf, COMMON_BUFSIZE);
 		if (n <= 0) { // read errors or EOF, or ^D, or ^C
@@ -6872,22 +6874,6 @@ static BC_STATUS zxc_program_exec(void)
 }
 #define zxc_program_exec(...) (zxc_program_exec(__VA_ARGS__) COMMA_SUCCESS)
 
-static unsigned xc_vm_envLen(const char *var)
-{
-	char *lenv;
-	unsigned len;
-
-	lenv = getenv(var);
-	len = BC_NUM_PRINT_WIDTH;
-	if (!lenv) return len;
-
-	len = bb_strtou(lenv, NULL, 10) - 1;
-	if (errno || len < 2 || len >= INT_MAX)
-		len = BC_NUM_PRINT_WIDTH;
-
-	return len;
-}
-
 static BC_STATUS zxc_vm_process(const char *text)
 {
 	BcStatus s;
@@ -7377,12 +7363,25 @@ static void xc_program_init(void)
 	bc_char_vec_init(&G.input_buffer);
 }
 
+static unsigned xc_vm_envLen(const char *var)
+{
+	char *lenv;
+	unsigned len;
+
+	lenv = getenv(var);
+	len = BC_NUM_PRINT_WIDTH;
+	if (!lenv) return len;
+
+	len = bb_strtou(lenv, NULL, 10) - 1;
+	if (errno || len < 2 || len >= INT_MAX)
+		len = BC_NUM_PRINT_WIDTH;
+
+	return len;
+}
+
 static int xc_vm_init(const char *env_len)
 {
 	G.prog.len = xc_vm_envLen(env_len);
-#if ENABLE_FEATURE_EDITING
-	G.line_input_state = new_line_input_t(DO_HISTORY);
-#endif
 	bc_vec_init(&G.files, sizeof(char *), NULL);
 
 	xc_program_init();
diff --git a/shell/ash.c b/shell/ash.c
index 87d329f87..f4d296289 100644
--- a/shell/ash.c
+++ b/shell/ash.c
@@ -14177,8 +14177,7 @@ exitshell(void)
 	char *p;
 
 #if ENABLE_FEATURE_EDITING_SAVE_ON_EXIT
-	if (line_input_state)
-		save_history(line_input_state);
+	save_history(line_input_state); /* may be NULL */
 #endif
 	savestatus = exitstatus;
 	TRACE(("pid %d, exitshell(%d)\n", getpid(), savestatus));
diff --git a/shell/hush.c b/shell/hush.c
index a8f7237d5..f0f0da746 100644
--- a/shell/hush.c
+++ b/shell/hush.c
@@ -2056,8 +2056,7 @@ static sighandler_t pick_sighandler(unsigned sig)
 static void hush_exit(int exitcode)
 {
 #if ENABLE_FEATURE_EDITING_SAVE_ON_EXIT
-	if (G.line_input_state)
-		save_history(G.line_input_state);
+	save_history(G.line_input_state); /* may be NULL */
 #endif
 
 	fflush_all();


More information about the busybox-cvs mailing list