]> git.sur5r.net Git - bacula/bacula/blob - bacula/patches/testing/bconsole_use_history.patch
ebl Add fix to avoid to expect the EndJob message with some
[bacula/bacula] / bacula / patches / testing / bconsole_use_history.patch
1 Index: src/console/console.c
2 ===================================================================
3 --- src/console/console.c       (rĂ©vision 5452)
4 +++ src/console/console.c       (copie de travail)
5 @@ -333,7 +333,137 @@
6  #endif
7  }
8  
9 +#ifdef HAVE_READLINE
10 +#define READLINE_LIBRARY 1
11 +#undef free
12 +#include "readline.h"
13 +#include "history.h"
14  
15 +int
16 +get_cmd(FILE *input, const char *prompt, BSOCK *sock, int sec)
17 +{
18 +   char *line;
19 +
20 +   rl_catch_signals = 0;              /* do it ourselves */
21 +   line = readline((char *)prompt);   /* cast needed for old readlines */
22 +
23 +   if (!line) {
24 +      exit(1);
25 +   }
26 +   strip_trailing_junk(line);
27 +   sock->msglen = pm_strcpy(&sock->msg, line);
28 +   if (sock->msglen) {
29 +      add_history(sock->msg);
30 +   }
31 +   free(line);
32 +   return 1;
33 +}
34 +
35 +#else /* no readline, do it ourselves */
36 +
37 +#if !defined(HAVE_WIN32)
38 +static bool bisatty(int fd)
39 +{
40 +   if (no_conio) {
41 +      return false;
42 +   }
43 +   return isatty(fd);
44 +}
45 +#endif
46 +
47 +/*
48 + *   Returns: 1 if data available
49 + *            0 if timeout
50 + *           -1 if error
51 + */
52 +static int
53 +wait_for_data(int fd, int sec)
54 +{
55 +#if defined(HAVE_WIN32)
56 +   return 1;
57 +#else
58 +   fd_set fdset;
59 +   struct timeval tv;
60 +
61 +   tv.tv_sec = sec;
62 +   tv.tv_usec = 0;
63 +   for ( ;; ) {
64 +      FD_ZERO(&fdset);
65 +      FD_SET((unsigned)fd, &fdset);
66 +      switch(select(fd + 1, &fdset, NULL, NULL, &tv)) {
67 +      case 0:                         /* timeout */
68 +         return 0;
69 +      case -1:
70 +         if (errno == EINTR || errno == EAGAIN) {
71 +            continue;
72 +         }
73 +         return -1;                  /* error return */
74 +      default:
75 +         return 1;
76 +      }
77 +   }
78 +#endif
79 +}
80 +
81 +/*
82 + * Get next input command from terminal.
83 + *
84 + *   Returns: 1 if got input
85 + *            0 if timeout
86 + *           -1 if EOF or error
87 + */
88 +int
89 +get_cmd(FILE *input, const char *prompt, BSOCK *sock, int sec)
90 +{
91 +   int len;
92 +   if (!stop) {
93 +      if (output == stdout || teeout) {
94 +         sendit(prompt);
95 +      }
96 +   }
97 +again:
98 +   switch (wait_for_data(fileno(input), sec)) {
99 +   case 0:
100 +      return 0;                    /* timeout */
101 +   case -1:
102 +      return -1;                   /* error */
103 +   default:
104 +      len = sizeof_pool_memory(sock->msg) - 1;
105 +      if (stop) {
106 +         sleep(1);
107 +         goto again;
108 +      }
109 +#ifdef HAVE_CONIO
110 +      if (bisatty(fileno(input))) {
111 +         input_line(sock->msg, len);
112 +         break;
113 +      }
114 +#endif
115 +#ifdef HAVE_WIN32 /* use special console for input on win32 */
116 +      if (input == stdin) {
117 +         if (win32_cgets(sock->msg, len) == NULL) {
118 +            return -1;
119 +         }
120 +      }
121 +      else
122 +#endif
123 +      if (fgets(sock->msg, len, input) == NULL) {
124 +         return -1;
125 +
126 +      }
127 +      break;
128 +   }
129 +   if (usrbrk()) {
130 +      clrbrk();
131 +   }
132 +   strip_trailing_junk(sock->msg);
133 +   sock->msglen = strlen(sock->msg);
134 +   return 1;
135 +}
136 +
137 +#endif /* end non-readline code */
138 +
139 +
140  /*********************************************************************
141   *
142   *         Main Bacula Console -- User Interface Program
143 @@ -589,6 +719,11 @@
144  
145     /* Run commands in ~/.bconsolerc if any */
146     char *env = getenv("HOME");
147 +
148 +#ifdef HAVE_READLINE
149 +   POOL_MEM history_file;
150 +#endif
151 +
152     if (env) {
153        FILE *fd;
154        pm_strcpy(&UA_sock->msg, env);
155 @@ -598,6 +733,14 @@
156           read_and_process_input(fd, UA_sock);
157           fclose(fd);
158        }
159 +
160 +#ifdef HAVE_READLINE
161 +      pm_strcpy(history_file, env);
162 +      pm_strcat(history_file, "/.bconsole_history");
163 +
164 +      using_history();
165 +      read_history(history_file.c_str());
166 +#endif
167     }
168  
169     read_and_process_input(stdin, UA_sock);
170 @@ -607,6 +750,19 @@
171        UA_sock->close();
172     }
173  
174 +#ifdef HAVE_READLINE
175 +   if (env) {
176 +      /* first, try to truncate the history file, and if it
177 +       * fail, the file is probably not present, and we
178 +       * can use write_history to create it
179 +       */
180 +      if (history_truncate_file(history_file.c_str(), 100) == 0) {
181 +        append_history(history_length, history_file.c_str());
182 +      } else {
183 +        write_history(history_file.c_str());
184 +      }
185 +   }
186 +#endif
187     terminate_console(0);
188     return 0;
189  }
190 @@ -702,138 +858,6 @@
191     return OK;
192  }
193  
194 -
195 -#ifdef HAVE_READLINE
196 -#define READLINE_LIBRARY 1
197 -#undef free
198 -#include "readline.h"
199 -#include "history.h"
200 -
201 -
202 -int
203 -get_cmd(FILE *input, const char *prompt, BSOCK *sock, int sec)
204 -{
205 -   char *line;
206 -
207 -   rl_catch_signals = 0;              /* do it ourselves */
208 -   line = readline((char *)prompt);   /* cast needed for old readlines */
209 -
210 -   if (!line) {
211 -      exit(1);
212 -   }
213 -   strip_trailing_junk(line);
214 -   sock->msglen = pm_strcpy(&sock->msg, line);
215 -   if (sock->msglen) {
216 -      add_history(sock->msg);
217 -   }
218 -   free(line);
219 -   return 1;
220 -}
221 -
222 -#else /* no readline, do it ourselves */
223 -
224 -#if !defined(HAVE_WIN32)
225 -static bool bisatty(int fd)
226 -{
227 -   if (no_conio) {
228 -      return false;
229 -   }
230 -   return isatty(fd);
231 -}
232 -#endif
233 -
234 -/*
235 - *   Returns: 1 if data available
236 - *            0 if timeout
237 - *           -1 if error
238 - */
239 -static int
240 -wait_for_data(int fd, int sec)
241 -{
242 -#if defined(HAVE_WIN32)
243 -   return 1;
244 -#else
245 -   fd_set fdset;
246 -   struct timeval tv;
247 -
248 -   tv.tv_sec = sec;
249 -   tv.tv_usec = 0;
250 -   for ( ;; ) {
251 -      FD_ZERO(&fdset);
252 -      FD_SET((unsigned)fd, &fdset);
253 -      switch(select(fd + 1, &fdset, NULL, NULL, &tv)) {
254 -      case 0:                         /* timeout */
255 -         return 0;
256 -      case -1:
257 -         if (errno == EINTR || errno == EAGAIN) {
258 -            continue;
259 -         }
260 -         return -1;                  /* error return */
261 -      default:
262 -         return 1;
263 -      }
264 -   }
265 -#endif
266 -}
267 -
268 -/*
269 - * Get next input command from terminal.
270 - *
271 - *   Returns: 1 if got input
272 - *            0 if timeout
273 - *           -1 if EOF or error
274 - */
275 -int
276 -get_cmd(FILE *input, const char *prompt, BSOCK *sock, int sec)
277 -{
278 -   int len;
279 -   if (!stop) {
280 -      if (output == stdout || teeout) {
281 -         sendit(prompt);
282 -      }
283 -   }
284 -again:
285 -   switch (wait_for_data(fileno(input), sec)) {
286 -   case 0:
287 -      return 0;                    /* timeout */
288 -   case -1:
289 -      return -1;                   /* error */
290 -   default:
291 -      len = sizeof_pool_memory(sock->msg) - 1;
292 -      if (stop) {
293 -         sleep(1);
294 -         goto again;
295 -      }
296 -#ifdef HAVE_CONIO
297 -      if (bisatty(fileno(input))) {
298 -         input_line(sock->msg, len);
299 -         break;
300 -      }
301 -#endif
302 -#ifdef HAVE_WIN32 /* use special console for input on win32 */
303 -      if (input == stdin) {
304 -         if (win32_cgets(sock->msg, len) == NULL) {
305 -            return -1;
306 -         }
307 -      }
308 -      else
309 -#endif
310 -      if (fgets(sock->msg, len, input) == NULL) {
311 -         return -1;
312 -
313 -      }
314 -      break;
315 -   }
316 -   if (usrbrk()) {
317 -      clrbrk();
318 -   }
319 -   strip_trailing_junk(sock->msg);
320 -   sock->msglen = strlen(sock->msg);
321 -   return 1;
322 -}
323 -
324 -#endif /* end non-readline code */
325 -
326  static int versioncmd(FILE *input, BSOCK *UA_sock)
327  {
328     senditf("Version: " VERSION " (" BDATE ") %s %s %s\n",