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