1
|
1 // octave-hist.cc -*- C++ -*- |
|
2 /* |
|
3 |
|
4 Copyright (C) 1992, 1993 John W. Eaton |
|
5 |
|
6 This file is part of Octave. |
|
7 |
|
8 Octave is free software; you can redistribute it and/or modify it |
|
9 under the terms of the GNU General Public License as published by the |
|
10 Free Software Foundation; either version 2, or (at your option) any |
|
11 later version. |
|
12 |
|
13 Octave is distributed in the hope that it will be useful, but WITHOUT |
|
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
16 for more details. |
|
17 |
|
18 You should have received a copy of the GNU General Public License |
|
19 along with Octave; see the file COPYING. If not, write to the Free |
|
20 Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. |
|
21 |
|
22 The functions listed below were adapted from similar functions from |
|
23 GNU Bash, the Bourne Again SHell, copyright (C) 1987, 1989, 1991 Free |
|
24 Software Foundation, Inc. |
|
25 |
|
26 do_history edit_history_readline |
|
27 do_edit_history edit_history_add_hist |
|
28 |
|
29 */ |
|
30 |
|
31 #ifdef __GNUG__ |
|
32 #pragma implementation |
|
33 #endif |
|
34 |
|
35 #include <sys/types.h> |
|
36 #ifdef HAVE_UNISTD_H |
|
37 #include <unistd.h> |
|
38 #endif |
|
39 #include <fcntl.h> |
|
40 #include <stdlib.h> |
|
41 #include <stdio.h> |
|
42 #include <string.h> |
|
43 #include <signal.h> |
|
44 #include <fstream.h> |
|
45 #include <strstream.h> |
|
46 |
|
47 #include "statdefs.h" |
|
48 #include "utils.h" |
|
49 #include "error.h" |
|
50 #include "input.h" |
|
51 #include "octave.h" |
|
52 #include "unwind-prot.h" |
|
53 #include "octave-hist.h" |
|
54 #include "sighandlers.h" |
|
55 |
|
56 extern "C" |
|
57 { |
|
58 #include <readline/history.h> |
|
59 } |
|
60 |
|
61 // Nonzero means we are saving history lines. |
|
62 int saving_history = 1; |
|
63 |
|
64 // The number of lines to save in the history file. |
|
65 static int octave_hist_size = 1024; |
|
66 |
|
67 // The name of the history file. |
|
68 static char *octave_hist_file; |
|
69 |
|
70 // The number of hisory lines we read from the history file. |
|
71 static int history_lines_in_file = 0; |
|
72 |
|
73 // The number of history lines we've saved so far. |
|
74 static int history_lines_this_session = 0; |
|
75 |
|
76 /* |
|
77 * Get some default values, possibly reading them from the |
|
78 * environment. |
|
79 */ |
|
80 static int |
|
81 default_history_size (void) |
|
82 { |
|
83 int size = 1024; |
|
84 char *env_size = getenv ("OCTAVE_HISTSIZE"); |
|
85 if (env_size != (char *) NULL) |
|
86 { |
|
87 int val; |
|
88 if (sscanf (env_size, "%d", &val) == 1) |
|
89 size = val > 0 ? val : 0; |
|
90 } |
|
91 return size; |
|
92 } |
|
93 |
|
94 static char * |
|
95 default_history_file (void) |
|
96 { |
|
97 char *file = (char *) NULL;; |
|
98 |
|
99 char *env_file = getenv ("OCTAVE_HISTFILE"); |
|
100 if (env_file != (char *) NULL) |
|
101 { |
|
102 fstream f (env_file, (ios::in | ios::out)); |
|
103 if (f != 0) |
|
104 { |
|
105 file = strsave (env_file); |
|
106 f.close (); |
|
107 } |
|
108 } |
|
109 |
|
110 if (file == (char *) NULL) |
|
111 { |
|
112 if (home_directory != NULL) |
|
113 file = strconcat (home_directory, "/.octave_hist"); |
|
114 } |
|
115 |
|
116 return file; |
|
117 } |
|
118 |
|
119 /* |
|
120 * Prime the history list. |
|
121 */ |
|
122 void |
|
123 initialize_history (void) |
|
124 { |
|
125 octave_hist_file = default_history_file (); |
|
126 octave_hist_size = default_history_size (); |
|
127 |
|
128 read_history (octave_hist_file); |
|
129 using_history (); |
|
130 history_lines_in_file = where_history (); |
|
131 } |
|
132 |
|
133 void |
|
134 clean_up_history (void) |
|
135 { |
|
136 stifle_history (octave_hist_size); |
|
137 write_history (octave_hist_file); |
|
138 } |
|
139 |
|
140 void |
|
141 maybe_save_history (char *s) |
|
142 { |
|
143 if (saving_history) |
|
144 { |
|
145 add_history (s); |
|
146 history_lines_this_session++; |
|
147 } |
|
148 } |
|
149 |
|
150 /* |
|
151 * Display, save, or load history. Stolen and modified from bash. |
|
152 * |
|
153 * Arg of -w FILENAME means write file, arg of -r FILENAME |
|
154 * means read file, arg of -q means don't number lines. Arg of N |
|
155 * means only display that many items. |
|
156 */ |
|
157 void |
|
158 do_history (int argc, char **argv) |
|
159 { |
|
160 HIST_ENTRY **hlist; |
|
161 |
|
162 int numbered_output = 1; |
|
163 |
|
164 while (--argc > 0) |
|
165 { |
|
166 argv++; |
|
167 |
|
168 if (*argv[0] == '-' && strlen (*argv) == 2 |
|
169 && ((*argv)[1] == 'r' || (*argv)[1] == 'w' |
|
170 || (*argv)[1] == 'a' || (*argv)[1] == 'n')) |
|
171 { |
|
172 char *file; |
|
173 int result = 0; |
|
174 |
|
175 if (argc > 1) |
|
176 file = *(argv+1); |
|
177 else |
|
178 file = octave_hist_file; |
|
179 |
|
180 switch ((*argv)[1]) |
|
181 { |
|
182 case 'a': // Append `new' lines to file. |
|
183 { |
|
184 if (history_lines_this_session) |
|
185 { |
|
186 if (history_lines_this_session < where_history ()) |
|
187 { |
|
188 // If the filename was supplied, then create it if it doesn't already |
|
189 // exist. |
|
190 if (file) |
|
191 { |
|
192 struct stat buf; |
|
193 |
|
194 if (stat (file, &buf) == -1) |
|
195 { |
|
196 int tem; |
|
197 |
|
198 tem = open (file, O_CREAT, 0666); |
|
199 close (tem); |
|
200 } |
|
201 } |
|
202 |
|
203 result = |
|
204 append_history (history_lines_this_session, file); |
|
205 history_lines_in_file += history_lines_this_session; |
|
206 history_lines_this_session = 0; |
|
207 } |
|
208 } |
|
209 } |
|
210 break; |
|
211 case 'w': // Write entire history. |
|
212 result = write_history (file); |
|
213 break; |
|
214 case 'r': // Read entire file. |
|
215 result = read_history (file); |
|
216 break; |
|
217 case 'n': // Read `new' history from file. |
|
218 // Read all of the lines in the file that we haven't already read. |
|
219 using_history (); |
|
220 result = read_history_range (file, history_lines_in_file, -1); |
|
221 using_history (); |
|
222 history_lines_in_file = where_history (); |
|
223 break; |
|
224 } |
|
225 return; |
|
226 } |
|
227 else if (strcmp (*argv, "-q") == 0) |
|
228 numbered_output = 0; |
|
229 else if (strcmp (*argv, "--") == 0) |
|
230 { |
|
231 argc--; |
|
232 argv++; |
|
233 break; |
|
234 } |
|
235 else |
|
236 break; |
|
237 } |
|
238 |
|
239 int limited = 0; |
|
240 int limit = 0; |
|
241 |
|
242 if (argc > 0) |
|
243 { |
|
244 limited = 1; |
|
245 if (sscanf (*argv, "%d", &limit) != 1) |
|
246 { |
|
247 if (*argv[0] == '-') |
|
248 message ("history", "unrecognized option `%s'", *argv); |
|
249 else |
|
250 message ("history", "bad non-numeric arg `%s'", *argv); |
|
251 return; |
|
252 } |
|
253 } |
|
254 |
|
255 hlist = history_list (); |
|
256 |
|
257 if (hlist) |
|
258 { |
|
259 for (int i = 0; hlist[i] != (HIST_ENTRY *) NULL; i++) |
|
260 ; // Do nothing. |
|
261 |
|
262 if (limit < 0) |
|
263 limit = -limit; |
|
264 |
|
265 if (!limited) |
|
266 i = 0; |
|
267 else |
|
268 if ((i -= limit) < 0) |
|
269 i = 0; |
|
270 |
|
271 while (hlist[i]) |
|
272 { |
|
273 // QUIT; // in bash: (interrupt_state) throw_to_top_level (); |
|
274 |
|
275 if (numbered_output) |
|
276 cerr.form ("%5d%c", i + history_base, hlist[i]->data ? '*' : ' '); |
|
277 cerr << hlist[i]->line << "\n"; |
|
278 i++; |
|
279 } |
|
280 } |
|
281 } |
|
282 |
|
283 /* |
|
284 * Read the edited history lines from STREAM and return them |
|
285 * one at a time. This can read unlimited length lines. The |
|
286 * caller should free the storage. |
|
287 */ |
|
288 static char * |
|
289 edit_history_readline (fstream& stream) |
|
290 { |
|
291 char c; |
|
292 int line_len = 128; |
|
293 int lindex = 0; |
|
294 char *line = new char [line_len]; |
|
295 line[0] = '\0'; |
|
296 |
|
297 while (stream.get (c)) |
|
298 { |
|
299 if (lindex + 2 >= line_len) |
|
300 { |
|
301 char *tmp_line = new char [line_len += 128]; |
|
302 strcpy (tmp_line, line); |
|
303 delete [] line; |
|
304 line = tmp_line; |
|
305 } |
|
306 |
|
307 if (c == '\n') |
|
308 { |
|
309 line[lindex++] = '\n'; |
|
310 line[lindex++] = '\0'; |
|
311 return line; |
|
312 } |
|
313 else |
|
314 line[lindex++] = c; |
|
315 } |
|
316 |
|
317 if (! lindex) |
|
318 { |
|
319 delete [] line; |
|
320 return (char *) NULL; |
|
321 } |
|
322 |
|
323 if (lindex + 2 >= line_len) |
|
324 { |
|
325 char *tmp_line = new char [lindex+3]; |
|
326 strcpy (tmp_line, line); |
|
327 delete [] line; |
|
328 line = tmp_line; |
|
329 } |
|
330 |
|
331 // Finish with newline if none in file. |
|
332 |
|
333 line[lindex++] = '\n'; |
|
334 line[lindex++] = '\0'; |
|
335 return line; |
|
336 } |
|
337 |
64
|
338 extern "C" |
|
339 { |
|
340 HIST_ENTRY *history_get (); |
|
341 } |
|
342 |
|
343 /* |
|
344 * Use `command' to replace the last entry in the history list, which, |
|
345 * by this time, is `run_history blah...'. The intent is that the |
|
346 * new command become the history entry, and that `fc' should never |
|
347 * appear in the history list. This way you can do `run_history' to |
|
348 * your heart's content. |
|
349 */ |
|
350 static void |
|
351 edit_history_repl_hist (char *command) |
|
352 { |
|
353 if (command == (char *) NULL || *command == '\0') |
|
354 return; |
|
355 |
|
356 HIST_ENTRY **hlist = history_list (); |
|
357 |
|
358 if (hlist == (HIST_ENTRY **) NULL) |
|
359 return; |
|
360 |
|
361 for (int i = 0; hlist[i]; i++) |
|
362 ; // Count 'em. |
|
363 i--; |
|
364 |
|
365 /* History_get () takes a parameter that should be |
|
366 offset by history_base. */ |
|
367 |
|
368 // Don't free this. |
|
369 HIST_ENTRY *histent = history_get (history_base + i); |
|
370 if (histent == (HIST_ENTRY *) NULL) |
|
371 return; |
|
372 |
|
373 char *data = (char *) NULL; |
|
374 if (histent->data != (char *) NULL) |
|
375 { |
|
376 int len = strlen (histent->data); |
|
377 data = (char *) malloc (len); |
|
378 strcpy (data, histent->data); |
|
379 } |
|
380 |
|
381 int n = strlen (command); |
|
382 |
|
383 if (command[n - 1] == '\n') |
|
384 command[n - 1] = '\0'; |
|
385 |
|
386 if (command != (char *) NULL && *command != '\0') |
|
387 { |
|
388 HIST_ENTRY *discard = replace_history_entry (i, command, data); |
|
389 if (discard != (HIST_ENTRY *) NULL) |
|
390 { |
|
391 if (discard->line != (char *) NULL) |
|
392 free (discard->line); |
|
393 |
|
394 free ((char *) discard); |
|
395 } |
|
396 } |
|
397 } |
|
398 |
1
|
399 static void |
|
400 edit_history_add_hist (char *line) |
|
401 { |
|
402 if (line != (char *) NULL) |
|
403 { |
|
404 int len = strlen (line); |
|
405 if (len > 0 && line[len-1] == '\n') |
|
406 line[len-1] = '\0'; |
|
407 |
|
408 if (line[0] != '\0') |
|
409 add_history (line); |
|
410 } |
|
411 } |
|
412 |
|
413 #define histline(i) (hlist[(i)]->line) |
|
414 |
|
415 #define EDIT_COMMAND "${EDITOR:-vi}" |
|
416 |
64
|
417 static char * |
|
418 mk_tmp_hist_file (int argc, char **argv, int insert_curr, char *warn_for) |
1
|
419 { |
|
420 HIST_ENTRY **hlist; |
|
421 |
|
422 hlist = history_list (); |
|
423 |
|
424 int hist_count = 0; |
|
425 |
|
426 while (hlist[hist_count++] != (HIST_ENTRY *) NULL) |
|
427 ; // Find the number of items in the history list. |
|
428 |
|
429 // The current command line is already part of the history list by the |
|
430 // time we get to this point. Delete it from the list. |
|
431 |
|
432 hist_count -= 2; |
64
|
433 if (! insert_curr) |
|
434 remove_history (hist_count); |
1
|
435 hist_count--; |
|
436 |
|
437 // If no numbers have been specified, the default is to edit the last |
|
438 // command in the history list. |
|
439 |
|
440 int hist_end = hist_count; |
|
441 int hist_beg = hist_count; |
|
442 int reverse = 0; |
|
443 |
|
444 // Process options |
|
445 |
|
446 int usage_error = 0; |
|
447 if (argc == 3) |
|
448 { |
|
449 argv++; |
|
450 if (sscanf (*argv++, "%d", &hist_beg) != 1 |
|
451 || sscanf (*argv, "%d", &hist_end) != 1) |
|
452 usage_error = 1; |
|
453 else |
|
454 { |
|
455 hist_beg--; |
|
456 hist_end--; |
|
457 } |
|
458 } |
|
459 else if (argc == 2) |
|
460 { |
|
461 argv++; |
|
462 if (sscanf (*argv++, "%d", &hist_beg) != 1) |
|
463 usage_error = 1; |
|
464 else |
|
465 { |
|
466 hist_beg--; |
|
467 hist_end = hist_beg; |
|
468 } |
|
469 } |
|
470 |
|
471 if (hist_beg < 0 || hist_end < 0 || hist_beg > hist_count |
|
472 || hist_end > hist_count) |
|
473 { |
64
|
474 error ("%s: history specification out of range", warn_for); |
|
475 return (char *) NULL; |
1
|
476 } |
|
477 |
|
478 if (usage_error) |
|
479 { |
64
|
480 usage ("%s [first] [last]", warn_for); |
|
481 return (char *) NULL; |
1
|
482 } |
|
483 |
|
484 if (hist_end < hist_beg) |
|
485 { |
|
486 int t = hist_end; |
|
487 hist_end = hist_beg; |
|
488 hist_beg = t; |
|
489 reverse = 1; |
|
490 } |
|
491 |
|
492 char *name = tmpnam ((char *) NULL); |
|
493 |
|
494 fstream file (name, ios::out); |
64
|
495 |
1
|
496 if (! file) |
|
497 { |
64
|
498 error ("%s: couldn't open temporary file `%s'", warn_for, name); |
|
499 return (char *) NULL; |
1
|
500 } |
|
501 |
|
502 if (reverse) |
|
503 { |
|
504 for (int i = hist_end; i >= hist_beg; i--) |
|
505 file << histline (i) << "\n"; |
|
506 } |
|
507 else |
|
508 { |
|
509 for (int i = hist_beg; i <= hist_end; i++) |
|
510 file << histline (i) << "\n"; |
|
511 } |
|
512 |
|
513 file.close (); |
|
514 |
64
|
515 return name; |
|
516 } |
|
517 |
|
518 void |
|
519 do_edit_history (int argc, char **argv) |
|
520 { |
|
521 char *name = mk_tmp_hist_file (argc, argv, 0, "edit_history"); |
|
522 |
|
523 if (name == (char *) NULL) |
|
524 return; |
|
525 |
1
|
526 // Call up our favorite editor on the file of commands. |
|
527 |
|
528 ostrstream buf; |
|
529 buf << EDIT_COMMAND << " " << name << ends; |
|
530 char *cmd = buf.str (); |
|
531 |
|
532 // Ignore interrupts while we are off editing commands. Should we |
64
|
533 // maybe avoid using system()? |
1
|
534 |
|
535 volatile sig_handler *saved_sigint_handler = signal (SIGINT, SIG_IGN); |
|
536 system (cmd); |
|
537 signal (SIGINT, saved_sigint_handler); |
|
538 |
|
539 // Write the commands to the history file since parse_and_execute |
|
540 // disables command line history while it executes. |
|
541 |
64
|
542 fstream file (name, ios::in); |
1
|
543 |
|
544 char *line; |
64
|
545 int first = 1; |
1
|
546 while ((line = edit_history_readline (file)) != NULL) |
|
547 { |
|
548 |
|
549 // Skip blank lines |
|
550 |
|
551 if (line[0] == '\n') |
|
552 { |
|
553 delete [] line; |
|
554 continue; |
|
555 } |
|
556 |
64
|
557 if (first) |
|
558 { |
|
559 first = 0; |
|
560 edit_history_repl_hist (line); |
|
561 } |
|
562 else |
|
563 edit_history_add_hist (line); |
1
|
564 } |
|
565 |
|
566 file.close (); |
|
567 |
|
568 // Turn on command echo, so the output from this will make better sense. |
|
569 |
|
570 begin_unwind_frame ("do_edit_history"); |
|
571 unwind_protect_int (echo_input); |
|
572 echo_input = 1; |
|
573 |
|
574 parse_and_execute (name, 1); |
|
575 |
|
576 run_unwind_frame ("do_edit_history"); |
|
577 |
|
578 // Delete the temporary file. Should probably be done with an |
|
579 // unwind_protect. |
|
580 |
|
581 unlink (name); |
|
582 } |
|
583 |
64
|
584 void |
|
585 do_run_history (int argc, char **argv) |
|
586 { |
|
587 char *name = mk_tmp_hist_file (argc, argv, 1, "run_history"); |
|
588 |
|
589 if (name == (char *) NULL) |
|
590 return; |
|
591 |
|
592 // Turn on command echo, so the output from this will make better sense. |
|
593 |
|
594 begin_unwind_frame ("do_run_history"); |
|
595 unwind_protect_int (echo_input); |
|
596 echo_input = 1; |
|
597 |
|
598 parse_and_execute (name, 1); |
|
599 |
|
600 run_unwind_frame ("do_run_history"); |
|
601 |
|
602 // Delete the temporary file. Should probably be done with an |
|
603 // unwind_protect. |
|
604 |
|
605 unlink (name); |
|
606 } |
|
607 |
1
|
608 int |
|
609 current_history_number (void) |
|
610 { |
|
611 using_history (); |
|
612 |
|
613 if (octave_hist_size > 0) |
|
614 return history_base + where_history (); |
|
615 else |
|
616 return -1; |
|
617 |
|
618 } |
|
619 |
|
620 /* |
|
621 ;;; Local Variables: *** |
|
622 ;;; mode: C++ *** |
|
623 ;;; page-delimiter: "^/\\*" *** |
|
624 ;;; End: *** |
|
625 */ |