1 From 772cc01d3739d50ae43858fed8ec733c0c7bf00c Mon Sep 17 00:00:00 2001
2 From: Dennis Groenen <tj.groenen@gmail.com>
3 Date: Sun, 21 Aug 2011 13:28:34 +0200
4 Subject: [PATCH] add support for delayed history saving
7 include/libbb.h | 3 +++
8 libbb/Config.src | 8 ++++++++
9 libbb/lineedit.c | 24 +++++++++++++++++++++++-
11 shell/hush.c | 5 +++++
12 5 files changed, 44 insertions(+), 1 deletions(-)
14 diff --git a/include/libbb.h b/include/libbb.h
15 index 63d0419..233b674 100644
18 @@ -1446,6 +1446,9 @@ line_input_t *new_line_input_t(int flags) FAST_FUNC;
19 * >0 length of input string, including terminating '\n'
21 int read_line_input(line_input_t *st, const char *prompt, char *command, int maxsize, int timeout) FAST_FUNC;
22 +# if ENABLE_FEATURE_EDITING_DELAYEDHISTORY
23 +void save_history(line_input_t *st);
27 int read_line_input(const char* prompt, char* command, int maxsize) FAST_FUNC;
28 diff --git a/libbb/Config.src b/libbb/Config.src
29 index aa44236..deee15f 100644
30 --- a/libbb/Config.src
31 +++ b/libbb/Config.src
32 @@ -94,6 +94,14 @@ config FEATURE_EDITING_SAVEHISTORY
34 Enable history saving in shells.
36 +config FEATURE_EDITING_DELAYEDHISTORY
37 + bool "Delayed history saving"
39 + depends on FEATURE_EDITING_SAVEHISTORY
41 + Disable saving history to disk after every command. Write out
42 + history only upon shell closure instead.
44 config FEATURE_REVERSE_SEARCH
45 bool "Reverse history search"
47 diff --git a/libbb/lineedit.c b/libbb/lineedit.c
48 index 1026519..48ac509 100644
49 --- a/libbb/lineedit.c
50 +++ b/libbb/lineedit.c
51 @@ -1392,10 +1392,29 @@ static void load_history(line_input_t *st_parm)
55 -/* state->flags is already checked to be nonzero */
56 +# if ENABLE_FEATURE_EDITING_DELAYEDHISTORY
57 +void save_history(line_input_t *st)
59 static void save_history(char *str)
63 +# if ENABLE_FEATURE_EDITING_DELAYEDHISTORY
64 +# if MAX_HISTORY > 0 && ENABLE_FEATURE_EDITING_SAVEHISTORY
66 + state = st ? st : (line_input_t*) &const_int_0;
67 + f_hist_file = fopen(state->hist_file,"a");
70 + for (i = state->cnt_history_in_file; i < state->cnt_history; i++)
71 + fprintf(f_hist_file, "%s\n", state->history[i]);
73 + fclose(f_hist_file);
78 + /* state->flags is already checked to be nonzero */
81 fd = open(state->hist_file, O_WRONLY | O_CREAT | O_APPEND, 0600);
82 @@ -1409,6 +1428,7 @@ static void save_history(char *str)
86 +# endif /* FEATURE_EDITING_DELAYEDHISTORY */
88 /* did we write so much that history file needs trimming? */
89 state->cnt_history_in_file++;
90 @@ -1475,10 +1495,12 @@ static void remember_in_history(char *str)
91 /* i <= state->max_history */
92 state->cur_history = i;
93 state->cnt_history = i;
94 +#if !ENABLE_FEATURE_EDITING_DELAYEDHISTORY /* save_history() will be called by exit_shell() */
95 # if MAX_HISTORY > 0 && ENABLE_FEATURE_EDITING_SAVEHISTORY
96 if ((state->flags & SAVE_HISTORY) && state->hist_file)
100 IF_FEATURE_EDITING_FANCY_PROMPT(num_ok_lines++;)
103 diff --git a/shell/ash.c b/shell/ash.c
104 index d48cd01..08d4e92 100644
107 @@ -12888,6 +12888,11 @@ exitshell(void)
111 +#if ENABLE_FEATURE_EDITING_DELAYEDHISTORY
112 + if ((line_input_state->flags & SAVE_HISTORY) && line_input_state->hist_file)
113 + save_history(line_input_state);
117 TRACE(("pid %d, exitshell(%d)\n", getpid(), status));
118 if (setjmp(loc.loc)) {
119 diff --git a/shell/hush.c b/shell/hush.c
120 index e4138ad..a7428d6 100644
123 @@ -1541,6 +1541,11 @@ static sighandler_t pick_sighandler(unsigned sig)
124 static void hush_exit(int exitcode) NORETURN;
125 static void hush_exit(int exitcode)
127 +#if ENABLE_FEATURE_EDITING_DELAYEDHISTORY
128 + if ((G.line_input_state->flags & SAVE_HISTORY) && G.line_input_state->hist_file)
129 + save_history(G.line_input_state);
133 if (G.exiting <= 0 && G.traps && G.traps[0] && G.traps[0][0]) {