update busybox-power against BusyBox 1.19.2 release
[busybox-power] / debian / patches / 0001-add-support-for-delayed-history-saving.patch
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
5
6 ---
7  include/libbb.h  |    3 +++
8  libbb/Config.src |    8 ++++++++
9  libbb/lineedit.c |   24 +++++++++++++++++++++++-
10  shell/ash.c      |    5 +++++
11  shell/hush.c     |    5 +++++
12  5 files changed, 44 insertions(+), 1 deletions(-)
13
14 diff --git a/include/libbb.h b/include/libbb.h
15 index 63d0419..233b674 100644
16 --- a/include/libbb.h
17 +++ b/include/libbb.h
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'
20   */
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);
24 +# endif
25  #else
26  #define MAX_HISTORY 0
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
33         help
34           Enable history saving in shells.
35  
36 +config FEATURE_EDITING_DELAYEDHISTORY
37 +       bool "Delayed history saving"
38 +       default n
39 +       depends on FEATURE_EDITING_SAVEHISTORY
40 +       help
41 +         Disable saving history to disk after every command. Write out
42 +         history only upon shell closure instead.
43 +
44  config FEATURE_REVERSE_SEARCH
45         bool "Reverse history search"
46         default y
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)
52         }
53  }
54  
55 -/* state->flags is already checked to be nonzero */
56 +# if ENABLE_FEATURE_EDITING_DELAYEDHISTORY
57 +void save_history(line_input_t *st)
58 +# else
59  static void save_history(char *str)
60 +# endif
61  {
62         int fd;
63 +# if ENABLE_FEATURE_EDITING_DELAYEDHISTORY
64 +#  if MAX_HISTORY > 0 && ENABLE_FEATURE_EDITING_SAVEHISTORY
65 +       FILE *f_hist_file;
66 +       state = st ? st : (line_input_t*) &const_int_0;
67 +       f_hist_file = fopen(state->hist_file,"a");
68 +       if (f_hist_file) {
69 +               int i;
70 +               for (i = state->cnt_history_in_file; i < state->cnt_history; i++)
71 +                       fprintf(f_hist_file, "%s\n", state->history[i]);
72 +       }
73 +       fclose(f_hist_file);
74 +#  else
75 +       return;
76 +#  endif
77 +# else
78 +       /* state->flags is already checked to be nonzero */
79         int len, len2;
80  
81         fd = open(state->hist_file, O_WRONLY | O_CREAT | O_APPEND, 0600);
82 @@ -1409,6 +1428,7 @@ static void save_history(char *str)
83         close(fd);
84         if (len2 != len + 1)
85                 return; /* "wtf?" */
86 +# endif /* FEATURE_EDITING_DELAYEDHISTORY */
87  
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)
97                 save_history(str);
98  # endif
99 +#endif
100         IF_FEATURE_EDITING_FANCY_PROMPT(num_ok_lines++;)
101  }
102  
103 diff --git a/shell/ash.c b/shell/ash.c
104 index d48cd01..08d4e92 100644
105 --- a/shell/ash.c
106 +++ b/shell/ash.c
107 @@ -12888,6 +12888,11 @@ exitshell(void)
108         char *p;
109         int status;
110  
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);
114 +#endif
115 +
116         status = exitstatus;
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
121 --- a/shell/hush.c
122 +++ b/shell/hush.c
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)
126  {
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);
130 +#endif
131 +
132         fflush_all();
133         if (G.exiting <= 0 && G.traps && G.traps[0] && G.traps[0][0]) {
134                 char *argv[3];
135 -- 
136 1.7.6
137