1
|
1 // octave-hist.cc -*- C++ -*- |
|
2 /* |
|
3 |
296
|
4 Copyright (C) 1992, 1993, 1994 John W. Eaton |
1
|
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 |
240
|
31 #ifdef HAVE_CONFIG_H |
|
32 #include "config.h" |
1
|
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 <string.h> |
|
42 #include <signal.h> |
|
43 #include <fstream.h> |
|
44 #include <strstream.h> |
|
45 |
|
46 #include "statdefs.h" |
|
47 #include "utils.h" |
|
48 #include "error.h" |
|
49 #include "input.h" |
581
|
50 #include "pager.h" |
1
|
51 #include "octave.h" |
529
|
52 #include "oct-obj.h" |
195
|
53 #include "user-prefs.h" |
1
|
54 #include "unwind-prot.h" |
|
55 #include "octave-hist.h" |
|
56 #include "sighandlers.h" |
529
|
57 #include "defun.h" |
1
|
58 |
|
59 extern "C" |
|
60 { |
|
61 #include <readline/history.h> |
|
62 } |
|
63 |
168
|
64 // Nonzero means input is coming from temporary history file. |
|
65 int input_from_tmp_history_file = 0; |
|
66 |
1
|
67 // Nonzero means we are saving history lines. |
|
68 int saving_history = 1; |
|
69 |
|
70 // The number of lines to save in the history file. |
|
71 static int octave_hist_size = 1024; |
|
72 |
|
73 // The name of the history file. |
|
74 static char *octave_hist_file; |
|
75 |
|
76 // The number of hisory lines we read from the history file. |
|
77 static int history_lines_in_file = 0; |
|
78 |
|
79 // The number of history lines we've saved so far. |
|
80 static int history_lines_this_session = 0; |
|
81 |
581
|
82 // Get some default values, possibly reading them from the |
|
83 // environment. |
|
84 |
1
|
85 static int |
|
86 default_history_size (void) |
|
87 { |
|
88 int size = 1024; |
|
89 char *env_size = getenv ("OCTAVE_HISTSIZE"); |
529
|
90 if (env_size) |
1
|
91 { |
|
92 int val; |
|
93 if (sscanf (env_size, "%d", &val) == 1) |
|
94 size = val > 0 ? val : 0; |
|
95 } |
|
96 return size; |
|
97 } |
|
98 |
|
99 static char * |
|
100 default_history_file (void) |
|
101 { |
529
|
102 char *file = 0; |
1
|
103 |
|
104 char *env_file = getenv ("OCTAVE_HISTFILE"); |
529
|
105 if (env_file) |
1
|
106 { |
|
107 fstream f (env_file, (ios::in | ios::out)); |
529
|
108 if (f) |
1
|
109 { |
|
110 file = strsave (env_file); |
|
111 f.close (); |
|
112 } |
|
113 } |
|
114 |
529
|
115 if (! file && home_directory) |
|
116 file = strconcat (home_directory, "/.octave_hist"); |
1
|
117 |
|
118 return file; |
|
119 } |
|
120 |
581
|
121 // Prime the history list. |
|
122 |
1
|
123 void |
|
124 initialize_history (void) |
|
125 { |
|
126 octave_hist_file = default_history_file (); |
|
127 octave_hist_size = default_history_size (); |
|
128 |
|
129 read_history (octave_hist_file); |
|
130 using_history (); |
|
131 history_lines_in_file = where_history (); |
|
132 } |
|
133 |
|
134 void |
|
135 clean_up_history (void) |
|
136 { |
|
137 stifle_history (octave_hist_size); |
|
138 write_history (octave_hist_file); |
|
139 } |
|
140 |
|
141 void |
|
142 maybe_save_history (char *s) |
|
143 { |
|
144 if (saving_history) |
|
145 { |
|
146 add_history (s); |
|
147 history_lines_this_session++; |
|
148 } |
|
149 } |
|
150 |
581
|
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 |
1
|
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] == '-') |
217
|
248 error ("history: unrecognized option `%s'", *argv); |
1
|
249 else |
217
|
250 error ("history: bad non-numeric arg `%s'", *argv); |
1
|
251 return; |
|
252 } |
|
253 } |
|
254 |
|
255 hlist = history_list (); |
|
256 |
|
257 if (hlist) |
|
258 { |
529
|
259 for (int i = 0; hlist[i]; i++) |
1
|
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 |
581
|
271 ostrstream output_buf; |
|
272 |
1
|
273 while (hlist[i]) |
|
274 { |
|
275 // QUIT; // in bash: (interrupt_state) throw_to_top_level (); |
|
276 |
|
277 if (numbered_output) |
581
|
278 output_buf.form ("%5d%c", i + history_base, |
|
279 hlist[i]->data ? '*' : ' '); |
|
280 output_buf << hlist[i]->line << "\n"; |
1
|
281 i++; |
|
282 } |
581
|
283 |
|
284 output_buf << ends; |
|
285 maybe_page_output (output_buf); |
1
|
286 } |
|
287 } |
|
288 |
581
|
289 // Read the edited history lines from STREAM and return them |
|
290 // one at a time. This can read unlimited length lines. The |
|
291 // caller should free the storage. |
|
292 |
1
|
293 static char * |
|
294 edit_history_readline (fstream& stream) |
|
295 { |
|
296 char c; |
|
297 int line_len = 128; |
|
298 int lindex = 0; |
|
299 char *line = new char [line_len]; |
|
300 line[0] = '\0'; |
|
301 |
|
302 while (stream.get (c)) |
|
303 { |
|
304 if (lindex + 2 >= line_len) |
|
305 { |
|
306 char *tmp_line = new char [line_len += 128]; |
|
307 strcpy (tmp_line, line); |
|
308 delete [] line; |
|
309 line = tmp_line; |
|
310 } |
|
311 |
|
312 if (c == '\n') |
|
313 { |
|
314 line[lindex++] = '\n'; |
|
315 line[lindex++] = '\0'; |
|
316 return line; |
|
317 } |
|
318 else |
|
319 line[lindex++] = c; |
|
320 } |
|
321 |
|
322 if (! lindex) |
|
323 { |
|
324 delete [] line; |
529
|
325 return 0; |
1
|
326 } |
|
327 |
|
328 if (lindex + 2 >= line_len) |
|
329 { |
|
330 char *tmp_line = new char [lindex+3]; |
|
331 strcpy (tmp_line, line); |
|
332 delete [] line; |
|
333 line = tmp_line; |
|
334 } |
|
335 |
|
336 // Finish with newline if none in file. |
|
337 |
|
338 line[lindex++] = '\n'; |
|
339 line[lindex++] = '\0'; |
|
340 return line; |
|
341 } |
|
342 |
64
|
343 extern "C" |
|
344 { |
|
345 HIST_ENTRY *history_get (); |
|
346 } |
|
347 |
581
|
348 // Use `command' to replace the last entry in the history list, which, |
|
349 // by this time, is `run_history blah...'. The intent is that the |
|
350 // new command become the history entry, and that `fc' should never |
|
351 // appear in the history list. This way you can do `run_history' to |
|
352 // your heart's content. |
|
353 |
64
|
354 static void |
|
355 edit_history_repl_hist (char *command) |
|
356 { |
529
|
357 if (! command || ! *command) |
64
|
358 return; |
|
359 |
|
360 HIST_ENTRY **hlist = history_list (); |
|
361 |
529
|
362 if (! hlist) |
64
|
363 return; |
|
364 |
|
365 for (int i = 0; hlist[i]; i++) |
|
366 ; // Count 'em. |
|
367 i--; |
|
368 |
581
|
369 // History_get () takes a parameter that should be offset by history_base. |
64
|
370 |
|
371 // Don't free this. |
|
372 HIST_ENTRY *histent = history_get (history_base + i); |
529
|
373 if (! histent) |
64
|
374 return; |
|
375 |
529
|
376 char *data = 0; |
|
377 if (histent->data) |
64
|
378 { |
|
379 int len = strlen (histent->data); |
|
380 data = (char *) malloc (len); |
|
381 strcpy (data, histent->data); |
|
382 } |
|
383 |
|
384 int n = strlen (command); |
|
385 |
|
386 if (command[n - 1] == '\n') |
|
387 command[n - 1] = '\0'; |
|
388 |
529
|
389 if (command && *command) |
64
|
390 { |
|
391 HIST_ENTRY *discard = replace_history_entry (i, command, data); |
529
|
392 if (discard) |
64
|
393 { |
529
|
394 if (discard->line) |
64
|
395 free (discard->line); |
|
396 |
|
397 free ((char *) discard); |
|
398 } |
|
399 } |
|
400 } |
|
401 |
1
|
402 static void |
|
403 edit_history_add_hist (char *line) |
|
404 { |
529
|
405 if (line) |
1
|
406 { |
|
407 int len = strlen (line); |
|
408 if (len > 0 && line[len-1] == '\n') |
|
409 line[len-1] = '\0'; |
|
410 |
|
411 if (line[0] != '\0') |
|
412 add_history (line); |
|
413 } |
|
414 } |
|
415 |
|
416 #define histline(i) (hlist[(i)]->line) |
|
417 |
64
|
418 static char * |
|
419 mk_tmp_hist_file (int argc, char **argv, int insert_curr, char *warn_for) |
1
|
420 { |
|
421 HIST_ENTRY **hlist; |
|
422 |
|
423 hlist = history_list (); |
|
424 |
|
425 int hist_count = 0; |
|
426 |
529
|
427 while (hlist[hist_count++]) |
1
|
428 ; // Find the number of items in the history list. |
|
429 |
|
430 // The current command line is already part of the history list by the |
|
431 // time we get to this point. Delete it from the list. |
|
432 |
|
433 hist_count -= 2; |
64
|
434 if (! insert_curr) |
|
435 remove_history (hist_count); |
1
|
436 hist_count--; |
|
437 |
|
438 // If no numbers have been specified, the default is to edit the last |
|
439 // command in the history list. |
|
440 |
|
441 int hist_end = hist_count; |
|
442 int hist_beg = hist_count; |
|
443 int reverse = 0; |
|
444 |
|
445 // Process options |
|
446 |
|
447 int usage_error = 0; |
|
448 if (argc == 3) |
|
449 { |
|
450 argv++; |
|
451 if (sscanf (*argv++, "%d", &hist_beg) != 1 |
|
452 || sscanf (*argv, "%d", &hist_end) != 1) |
|
453 usage_error = 1; |
|
454 else |
|
455 { |
|
456 hist_beg--; |
|
457 hist_end--; |
|
458 } |
|
459 } |
|
460 else if (argc == 2) |
|
461 { |
|
462 argv++; |
|
463 if (sscanf (*argv++, "%d", &hist_beg) != 1) |
|
464 usage_error = 1; |
|
465 else |
|
466 { |
|
467 hist_beg--; |
|
468 hist_end = hist_beg; |
|
469 } |
|
470 } |
|
471 |
|
472 if (hist_beg < 0 || hist_end < 0 || hist_beg > hist_count |
|
473 || hist_end > hist_count) |
|
474 { |
64
|
475 error ("%s: history specification out of range", warn_for); |
529
|
476 return 0; |
1
|
477 } |
|
478 |
|
479 if (usage_error) |
|
480 { |
64
|
481 usage ("%s [first] [last]", warn_for); |
529
|
482 return 0; |
1
|
483 } |
|
484 |
|
485 if (hist_end < hist_beg) |
|
486 { |
|
487 int t = hist_end; |
|
488 hist_end = hist_beg; |
|
489 hist_beg = t; |
|
490 reverse = 1; |
|
491 } |
|
492 |
637
|
493 char *name = octave_tmp_file_name (0); |
1
|
494 |
|
495 fstream file (name, ios::out); |
64
|
496 |
1
|
497 if (! file) |
|
498 { |
64
|
499 error ("%s: couldn't open temporary file `%s'", warn_for, name); |
529
|
500 return 0; |
1
|
501 } |
|
502 |
|
503 if (reverse) |
|
504 { |
|
505 for (int i = hist_end; i >= hist_beg; i--) |
|
506 file << histline (i) << "\n"; |
|
507 } |
|
508 else |
|
509 { |
|
510 for (int i = hist_beg; i <= hist_end; i++) |
|
511 file << histline (i) << "\n"; |
|
512 } |
|
513 |
|
514 file.close (); |
|
515 |
168
|
516 return strsave (name); |
64
|
517 } |
|
518 |
|
519 void |
|
520 do_edit_history (int argc, char **argv) |
|
521 { |
|
522 char *name = mk_tmp_hist_file (argc, argv, 0, "edit_history"); |
|
523 |
529
|
524 if (! name) |
64
|
525 return; |
|
526 |
1
|
527 // Call up our favorite editor on the file of commands. |
|
528 |
|
529 ostrstream buf; |
195
|
530 buf << user_pref.editor << " " << name << ends; |
1
|
531 char *cmd = buf.str (); |
|
532 |
|
533 // Ignore interrupts while we are off editing commands. Should we |
64
|
534 // maybe avoid using system()? |
1
|
535 |
|
536 volatile sig_handler *saved_sigint_handler = signal (SIGINT, SIG_IGN); |
|
537 system (cmd); |
|
538 signal (SIGINT, saved_sigint_handler); |
|
539 |
|
540 // Write the commands to the history file since parse_and_execute |
|
541 // disables command line history while it executes. |
|
542 |
64
|
543 fstream file (name, ios::in); |
1
|
544 |
|
545 char *line; |
64
|
546 int first = 1; |
529
|
547 while ((line = edit_history_readline (file)) != 0) |
1
|
548 { |
|
549 |
|
550 // Skip blank lines |
|
551 |
|
552 if (line[0] == '\n') |
|
553 { |
|
554 delete [] line; |
|
555 continue; |
|
556 } |
|
557 |
64
|
558 if (first) |
|
559 { |
|
560 first = 0; |
|
561 edit_history_repl_hist (line); |
|
562 } |
|
563 else |
|
564 edit_history_add_hist (line); |
1
|
565 } |
|
566 |
|
567 file.close (); |
|
568 |
|
569 // Turn on command echo, so the output from this will make better sense. |
|
570 |
|
571 begin_unwind_frame ("do_edit_history"); |
|
572 unwind_protect_int (echo_input); |
168
|
573 unwind_protect_int (input_from_tmp_history_file); |
1
|
574 echo_input = 1; |
168
|
575 input_from_tmp_history_file = 1; |
1
|
576 |
|
577 parse_and_execute (name, 1); |
|
578 |
|
579 run_unwind_frame ("do_edit_history"); |
|
580 |
|
581 // Delete the temporary file. Should probably be done with an |
|
582 // unwind_protect. |
|
583 |
|
584 unlink (name); |
168
|
585 |
|
586 delete [] name; |
1
|
587 } |
|
588 |
64
|
589 void |
|
590 do_run_history (int argc, char **argv) |
|
591 { |
|
592 char *name = mk_tmp_hist_file (argc, argv, 1, "run_history"); |
|
593 |
529
|
594 if (! name) |
64
|
595 return; |
|
596 |
|
597 // Turn on command echo, so the output from this will make better sense. |
|
598 |
|
599 begin_unwind_frame ("do_run_history"); |
|
600 unwind_protect_int (echo_input); |
168
|
601 unwind_protect_int (input_from_tmp_history_file); |
64
|
602 echo_input = 1; |
168
|
603 input_from_tmp_history_file = 1; |
64
|
604 |
|
605 parse_and_execute (name, 1); |
|
606 |
|
607 run_unwind_frame ("do_run_history"); |
|
608 |
|
609 // Delete the temporary file. Should probably be done with an |
|
610 // unwind_protect. |
|
611 |
|
612 unlink (name); |
168
|
613 |
|
614 delete [] name; |
64
|
615 } |
|
616 |
1
|
617 int |
|
618 current_history_number (void) |
|
619 { |
|
620 using_history (); |
|
621 |
|
622 if (octave_hist_size > 0) |
|
623 return history_base + where_history (); |
|
624 else |
|
625 return -1; |
|
626 |
|
627 } |
|
628 |
529
|
629 DEFUN_TEXT ("edit_history", Fedit_history, Sedit_history, -1, 1, |
|
630 "edit_history [first] [last]\n\ |
|
631 \n\ |
|
632 edit commands from the history list") |
|
633 { |
|
634 Octave_object retval; |
|
635 |
|
636 DEFINE_ARGV("edit_history"); |
|
637 |
|
638 do_edit_history (argc, argv); |
|
639 |
|
640 DELETE_ARGV; |
|
641 |
|
642 return retval; |
|
643 } |
|
644 |
|
645 DEFUN_TEXT ("history", Fhistory, Shistory, -1, 1, |
|
646 "history [N] [-w file] [-r file] [-q]\n\ |
|
647 \n\ |
|
648 display, save, or load command history") |
|
649 { |
|
650 Octave_object retval; |
|
651 |
|
652 DEFINE_ARGV("history"); |
|
653 |
|
654 do_history (argc, argv); |
|
655 |
|
656 DELETE_ARGV; |
|
657 |
|
658 return retval; |
|
659 } |
|
660 |
|
661 DEFUN_TEXT ("run_history", Frun_history, Srun_history, -1, 1, |
|
662 "run_history [first] [last]\n\ |
|
663 \n\ |
|
664 run commands from the history list") |
|
665 { |
|
666 Octave_object retval; |
|
667 |
|
668 DEFINE_ARGV("run_history"); |
|
669 |
|
670 do_run_history (argc, argv); |
|
671 |
|
672 DELETE_ARGV; |
|
673 |
|
674 return retval; |
|
675 } |
|
676 |
1
|
677 /* |
|
678 ;;; Local Variables: *** |
|
679 ;;; mode: C++ *** |
|
680 ;;; page-delimiter: "^/\\*" *** |
|
681 ;;; End: *** |
|
682 */ |