aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDenys Vlasenko <vda.linux@googlemail.com>2020-12-21 21:36:58 +0100
committerDenys Vlasenko <vda.linux@googlemail.com>2020-12-21 21:36:58 +0100
commit00eb23b47aa79461b913b320eba3c95b90e6eec4 (patch)
treeebeb54dcc6eb977247f5088be9cffc4deaea27e9
parente4202df0918e13130bb511a4ce372cbbe089068d (diff)
downloadbusybox-w32-00eb23b47aa79461b913b320eba3c95b90e6eec4.tar.gz
busybox-w32-00eb23b47aa79461b913b320eba3c95b90e6eec4.tar.bz2
busybox-w32-00eb23b47aa79461b913b320eba3c95b90e6eec4.zip
bc: do not allocate line editing state until needed
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@googlemail.com>
-rw-r--r--include/libbb.h4
-rw-r--r--libbb/lineedit.c18
-rw-r--r--miscutils/bc.c37
-rw-r--r--shell/ash.c3
-rw-r--r--shell/hush.c3
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 {
1906 FOR_SHELL = DO_HISTORY | TAB_COMPLETION | USERNAME_COMPLETION, 1906 FOR_SHELL = DO_HISTORY | TAB_COMPLETION | USERNAME_COMPLETION,
1907}; 1907};
1908line_input_t *new_line_input_t(int flags) FAST_FUNC; 1908line_input_t *new_line_input_t(int flags) FAST_FUNC;
1909#if ENABLE_FEATURE_EDITING_SAVEHISTORY
1909void free_line_input_t(line_input_t *n) FAST_FUNC; 1910void free_line_input_t(line_input_t *n) FAST_FUNC;
1911#else
1912# define free_line_input_t(n) free(n)
1913#endif
1910/* 1914/*
1911 * maxsize must be >= 2. 1915 * maxsize must be >= 2.
1912 * Returns: 1916 * 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)
1417 printf("%4d %s\n", i, st->history[i]); 1417 printf("%4d %s\n", i, st->history[i]);
1418} 1418}
1419 1419
1420# if ENABLE_FEATURE_EDITING_SAVEHISTORY
1420void FAST_FUNC free_line_input_t(line_input_t *n) 1421void FAST_FUNC free_line_input_t(line_input_t *n)
1421{ 1422{
1422# if ENABLE_FEATURE_EDITING_SAVEHISTORY 1423 if (n) {
1423 int i = n->cnt_history; 1424 int i = n->cnt_history;
1424 while (i > 0) 1425 while (i > 0)
1425 free(n->history[--i]); 1426 free(n->history[--i]);
1426#endif 1427 free(n);
1427 free(n); 1428 }
1428} 1429}
1430# else
1431/* #defined to free() in libbb.h */
1432# endif
1429 1433
1430# if ENABLE_FEATURE_EDITING_SAVEHISTORY 1434# if ENABLE_FEATURE_EDITING_SAVEHISTORY
1431/* We try to ensure that concurrent additions to the history 1435/* We try to ensure that concurrent additions to the history
@@ -1506,7 +1510,7 @@ void save_history(line_input_t *st)
1506{ 1510{
1507 FILE *fp; 1511 FILE *fp;
1508 1512
1509 if (!st->hist_file) 1513 if (!st || !st->hist_file)
1510 return; 1514 return;
1511 if (st->cnt_history <= st->cnt_history_in_file) 1515 if (st->cnt_history <= st->cnt_history_in_file)
1512 return; 1516 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)
2545# if ENABLE_FEATURE_EDITING 2545# if ENABLE_FEATURE_EDITING
2546 if (G_ttyin && fp == stdin) { 2546 if (G_ttyin && fp == stdin) {
2547 int n, i; 2547 int n, i;
2548 if (!G.line_input_state)
2549 G.line_input_state = new_line_input_t(DO_HISTORY);
2548# define line_buf bb_common_bufsiz1 2550# define line_buf bb_common_bufsiz1
2549 n = read_line_input(G.line_input_state, "", line_buf, COMMON_BUFSIZE); 2551 n = read_line_input(G.line_input_state, "", line_buf, COMMON_BUFSIZE);
2550 if (n <= 0) { // read errors or EOF, or ^D, or ^C 2552 if (n <= 0) { // read errors or EOF, or ^D, or ^C
@@ -6872,22 +6874,6 @@ static BC_STATUS zxc_program_exec(void)
6872} 6874}
6873#define zxc_program_exec(...) (zxc_program_exec(__VA_ARGS__) COMMA_SUCCESS) 6875#define zxc_program_exec(...) (zxc_program_exec(__VA_ARGS__) COMMA_SUCCESS)
6874 6876
6875static unsigned xc_vm_envLen(const char *var)
6876{
6877 char *lenv;
6878 unsigned len;
6879
6880 lenv = getenv(var);
6881 len = BC_NUM_PRINT_WIDTH;
6882 if (!lenv) return len;
6883
6884 len = bb_strtou(lenv, NULL, 10) - 1;
6885 if (errno || len < 2 || len >= INT_MAX)
6886 len = BC_NUM_PRINT_WIDTH;
6887
6888 return len;
6889}
6890
6891static BC_STATUS zxc_vm_process(const char *text) 6877static BC_STATUS zxc_vm_process(const char *text)
6892{ 6878{
6893 BcStatus s; 6879 BcStatus s;
@@ -7377,12 +7363,25 @@ static void xc_program_init(void)
7377 bc_char_vec_init(&G.input_buffer); 7363 bc_char_vec_init(&G.input_buffer);
7378} 7364}
7379 7365
7366static unsigned xc_vm_envLen(const char *var)
7367{
7368 char *lenv;
7369 unsigned len;
7370
7371 lenv = getenv(var);
7372 len = BC_NUM_PRINT_WIDTH;
7373 if (!lenv) return len;
7374
7375 len = bb_strtou(lenv, NULL, 10) - 1;
7376 if (errno || len < 2 || len >= INT_MAX)
7377 len = BC_NUM_PRINT_WIDTH;
7378
7379 return len;
7380}
7381
7380static int xc_vm_init(const char *env_len) 7382static int xc_vm_init(const char *env_len)
7381{ 7383{
7382 G.prog.len = xc_vm_envLen(env_len); 7384 G.prog.len = xc_vm_envLen(env_len);
7383#if ENABLE_FEATURE_EDITING
7384 G.line_input_state = new_line_input_t(DO_HISTORY);
7385#endif
7386 bc_vec_init(&G.files, sizeof(char *), NULL); 7385 bc_vec_init(&G.files, sizeof(char *), NULL);
7387 7386
7388 xc_program_init(); 7387 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)
14177 char *p; 14177 char *p;
14178 14178
14179#if ENABLE_FEATURE_EDITING_SAVE_ON_EXIT 14179#if ENABLE_FEATURE_EDITING_SAVE_ON_EXIT
14180 if (line_input_state) 14180 save_history(line_input_state); /* may be NULL */
14181 save_history(line_input_state);
14182#endif 14181#endif
14183 savestatus = exitstatus; 14182 savestatus = exitstatus;
14184 TRACE(("pid %d, exitshell(%d)\n", getpid(), savestatus)); 14183 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)
2056static void hush_exit(int exitcode) 2056static void hush_exit(int exitcode)
2057{ 2057{
2058#if ENABLE_FEATURE_EDITING_SAVE_ON_EXIT 2058#if ENABLE_FEATURE_EDITING_SAVE_ON_EXIT
2059 if (G.line_input_state) 2059 save_history(G.line_input_state); /* may be NULL */
2060 save_history(G.line_input_state);
2061#endif 2060#endif
2062 2061
2063 fflush_all(); 2062 fflush_all();