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