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 |
1465
|
49 #include <readline/history.h> |
|
50 |
1352
|
51 #include "defun.h" |
1
|
52 #include "error.h" |
|
53 #include "input.h" |
1352
|
54 #include "oct-obj.h" |
|
55 #include "octave-hist.h" |
1
|
56 #include "octave.h" |
1352
|
57 #include "pager.h" |
|
58 #include "sighandlers.h" |
|
59 #include "statdefs.h" |
|
60 #include "unwind-prot.h" |
195
|
61 #include "user-prefs.h" |
1352
|
62 #include "utils.h" |
1
|
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 |
1363
|
142 maybe_save_history (const char *s) |
1
|
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 { |
1358
|
188 // If the filename was supplied, then create |
|
189 // it if it doesn't already exist. |
|
190 |
1
|
191 if (file) |
|
192 { |
|
193 struct stat buf; |
|
194 |
|
195 if (stat (file, &buf) == -1) |
|
196 { |
|
197 int tem; |
|
198 |
|
199 tem = open (file, O_CREAT, 0666); |
|
200 close (tem); |
|
201 } |
|
202 } |
|
203 |
|
204 result = |
|
205 append_history (history_lines_this_session, file); |
|
206 history_lines_in_file += history_lines_this_session; |
|
207 history_lines_this_session = 0; |
|
208 } |
|
209 } |
|
210 } |
|
211 break; |
777
|
212 |
1
|
213 case 'w': // Write entire history. |
|
214 result = write_history (file); |
|
215 break; |
777
|
216 |
1
|
217 case 'r': // Read entire file. |
|
218 result = read_history (file); |
|
219 break; |
777
|
220 |
1
|
221 case 'n': // Read `new' history from file. |
1358
|
222 |
|
223 // Read all of the lines in the file that we haven't |
|
224 // already read. |
|
225 |
1
|
226 using_history (); |
|
227 result = read_history_range (file, history_lines_in_file, -1); |
|
228 using_history (); |
|
229 history_lines_in_file = where_history (); |
|
230 break; |
|
231 } |
|
232 return; |
|
233 } |
|
234 else if (strcmp (*argv, "-q") == 0) |
|
235 numbered_output = 0; |
|
236 else if (strcmp (*argv, "--") == 0) |
|
237 { |
|
238 argc--; |
|
239 argv++; |
|
240 break; |
|
241 } |
|
242 else |
|
243 break; |
|
244 } |
|
245 |
|
246 int limited = 0; |
|
247 int limit = 0; |
|
248 |
|
249 if (argc > 0) |
|
250 { |
|
251 limited = 1; |
|
252 if (sscanf (*argv, "%d", &limit) != 1) |
|
253 { |
|
254 if (*argv[0] == '-') |
217
|
255 error ("history: unrecognized option `%s'", *argv); |
1
|
256 else |
217
|
257 error ("history: bad non-numeric arg `%s'", *argv); |
1
|
258 return; |
|
259 } |
|
260 } |
|
261 |
|
262 hlist = history_list (); |
|
263 |
|
264 if (hlist) |
|
265 { |
1321
|
266 int i = 0; |
|
267 |
|
268 for (i = 0; hlist[i]; i++) |
1
|
269 ; // Do nothing. |
|
270 |
|
271 if (limit < 0) |
|
272 limit = -limit; |
|
273 |
|
274 if (!limited) |
|
275 i = 0; |
|
276 else |
|
277 if ((i -= limit) < 0) |
|
278 i = 0; |
|
279 |
581
|
280 ostrstream output_buf; |
|
281 |
1
|
282 while (hlist[i]) |
|
283 { |
|
284 // QUIT; // in bash: (interrupt_state) throw_to_top_level (); |
|
285 |
|
286 if (numbered_output) |
581
|
287 output_buf.form ("%5d%c", i + history_base, |
|
288 hlist[i]->data ? '*' : ' '); |
|
289 output_buf << hlist[i]->line << "\n"; |
1
|
290 i++; |
|
291 } |
581
|
292 |
|
293 output_buf << ends; |
|
294 maybe_page_output (output_buf); |
1
|
295 } |
|
296 } |
|
297 |
581
|
298 // Read the edited history lines from STREAM and return them |
|
299 // one at a time. This can read unlimited length lines. The |
|
300 // caller should free the storage. |
|
301 |
1
|
302 static char * |
|
303 edit_history_readline (fstream& stream) |
|
304 { |
|
305 char c; |
|
306 int line_len = 128; |
|
307 int lindex = 0; |
|
308 char *line = new char [line_len]; |
|
309 line[0] = '\0'; |
|
310 |
|
311 while (stream.get (c)) |
|
312 { |
|
313 if (lindex + 2 >= line_len) |
|
314 { |
|
315 char *tmp_line = new char [line_len += 128]; |
|
316 strcpy (tmp_line, line); |
|
317 delete [] line; |
|
318 line = tmp_line; |
|
319 } |
|
320 |
|
321 if (c == '\n') |
|
322 { |
|
323 line[lindex++] = '\n'; |
|
324 line[lindex++] = '\0'; |
|
325 return line; |
|
326 } |
|
327 else |
|
328 line[lindex++] = c; |
|
329 } |
|
330 |
|
331 if (! lindex) |
|
332 { |
|
333 delete [] line; |
529
|
334 return 0; |
1
|
335 } |
|
336 |
|
337 if (lindex + 2 >= line_len) |
|
338 { |
|
339 char *tmp_line = new char [lindex+3]; |
|
340 strcpy (tmp_line, line); |
|
341 delete [] line; |
|
342 line = tmp_line; |
|
343 } |
|
344 |
1358
|
345 // Finish with newline if none in file. |
1
|
346 |
|
347 line[lindex++] = '\n'; |
|
348 line[lindex++] = '\0'; |
|
349 return line; |
|
350 } |
|
351 |
581
|
352 // Use `command' to replace the last entry in the history list, which, |
|
353 // by this time, is `run_history blah...'. The intent is that the |
|
354 // new command become the history entry, and that `fc' should never |
|
355 // appear in the history list. This way you can do `run_history' to |
|
356 // your heart's content. |
|
357 |
64
|
358 static void |
|
359 edit_history_repl_hist (char *command) |
|
360 { |
529
|
361 if (! command || ! *command) |
64
|
362 return; |
|
363 |
|
364 HIST_ENTRY **hlist = history_list (); |
|
365 |
529
|
366 if (! hlist) |
64
|
367 return; |
|
368 |
1321
|
369 int i = 0; |
|
370 |
|
371 for (i = 0; hlist[i]; i++) |
64
|
372 ; // Count 'em. |
|
373 i--; |
|
374 |
1358
|
375 // History_get () takes a parameter that should be offset by history_base. |
64
|
376 |
1358
|
377 // Don't free this. |
64
|
378 HIST_ENTRY *histent = history_get (history_base + i); |
529
|
379 if (! histent) |
64
|
380 return; |
|
381 |
529
|
382 char *data = 0; |
|
383 if (histent->data) |
64
|
384 { |
|
385 int len = strlen (histent->data); |
|
386 data = (char *) malloc (len); |
|
387 strcpy (data, histent->data); |
|
388 } |
|
389 |
|
390 int n = strlen (command); |
|
391 |
|
392 if (command[n - 1] == '\n') |
|
393 command[n - 1] = '\0'; |
|
394 |
529
|
395 if (command && *command) |
64
|
396 { |
|
397 HIST_ENTRY *discard = replace_history_entry (i, command, data); |
529
|
398 if (discard) |
64
|
399 { |
529
|
400 if (discard->line) |
64
|
401 free (discard->line); |
|
402 |
|
403 free ((char *) discard); |
|
404 } |
|
405 } |
|
406 } |
|
407 |
1
|
408 static void |
|
409 edit_history_add_hist (char *line) |
|
410 { |
529
|
411 if (line) |
1
|
412 { |
|
413 int len = strlen (line); |
|
414 if (len > 0 && line[len-1] == '\n') |
|
415 line[len-1] = '\0'; |
|
416 |
|
417 if (line[0] != '\0') |
|
418 add_history (line); |
|
419 } |
|
420 } |
|
421 |
|
422 #define histline(i) (hlist[(i)]->line) |
|
423 |
64
|
424 static char * |
|
425 mk_tmp_hist_file (int argc, char **argv, int insert_curr, char *warn_for) |
1
|
426 { |
|
427 HIST_ENTRY **hlist; |
|
428 |
|
429 hlist = history_list (); |
|
430 |
|
431 int hist_count = 0; |
|
432 |
529
|
433 while (hlist[hist_count++]) |
1
|
434 ; // Find the number of items in the history list. |
|
435 |
1358
|
436 // The current command line is already part of the history list by |
|
437 // the time we get to this point. Delete it from the list. |
1
|
438 |
|
439 hist_count -= 2; |
64
|
440 if (! insert_curr) |
|
441 remove_history (hist_count); |
1
|
442 hist_count--; |
|
443 |
1358
|
444 // If no numbers have been specified, the default is to edit the |
|
445 // last command in the history list. |
1
|
446 |
|
447 int hist_end = hist_count; |
|
448 int hist_beg = hist_count; |
|
449 int reverse = 0; |
|
450 |
1358
|
451 // Process options. |
1
|
452 |
|
453 int usage_error = 0; |
|
454 if (argc == 3) |
|
455 { |
|
456 argv++; |
|
457 if (sscanf (*argv++, "%d", &hist_beg) != 1 |
|
458 || sscanf (*argv, "%d", &hist_end) != 1) |
|
459 usage_error = 1; |
|
460 else |
|
461 { |
|
462 hist_beg--; |
|
463 hist_end--; |
|
464 } |
|
465 } |
|
466 else if (argc == 2) |
|
467 { |
|
468 argv++; |
|
469 if (sscanf (*argv++, "%d", &hist_beg) != 1) |
|
470 usage_error = 1; |
|
471 else |
|
472 { |
|
473 hist_beg--; |
|
474 hist_end = hist_beg; |
|
475 } |
|
476 } |
|
477 |
|
478 if (hist_beg < 0 || hist_end < 0 || hist_beg > hist_count |
|
479 || hist_end > hist_count) |
|
480 { |
64
|
481 error ("%s: history specification out of range", warn_for); |
529
|
482 return 0; |
1
|
483 } |
|
484 |
|
485 if (usage_error) |
|
486 { |
64
|
487 usage ("%s [first] [last]", warn_for); |
529
|
488 return 0; |
1
|
489 } |
|
490 |
|
491 if (hist_end < hist_beg) |
|
492 { |
|
493 int t = hist_end; |
|
494 hist_end = hist_beg; |
|
495 hist_beg = t; |
|
496 reverse = 1; |
|
497 } |
|
498 |
641
|
499 char *name = octave_tmp_file_name (); |
1
|
500 |
|
501 fstream file (name, ios::out); |
64
|
502 |
1
|
503 if (! file) |
|
504 { |
64
|
505 error ("%s: couldn't open temporary file `%s'", warn_for, name); |
529
|
506 return 0; |
1
|
507 } |
|
508 |
|
509 if (reverse) |
|
510 { |
|
511 for (int i = hist_end; i >= hist_beg; i--) |
|
512 file << histline (i) << "\n"; |
|
513 } |
|
514 else |
|
515 { |
|
516 for (int i = hist_beg; i <= hist_end; i++) |
|
517 file << histline (i) << "\n"; |
|
518 } |
|
519 |
|
520 file.close (); |
|
521 |
168
|
522 return strsave (name); |
64
|
523 } |
|
524 |
|
525 void |
|
526 do_edit_history (int argc, char **argv) |
|
527 { |
|
528 char *name = mk_tmp_hist_file (argc, argv, 0, "edit_history"); |
|
529 |
529
|
530 if (! name) |
64
|
531 return; |
|
532 |
1358
|
533 // Call up our favorite editor on the file of commands. |
1
|
534 |
|
535 ostrstream buf; |
195
|
536 buf << user_pref.editor << " " << name << ends; |
1
|
537 char *cmd = buf.str (); |
|
538 |
1358
|
539 // Ignore interrupts while we are off editing commands. Should we |
|
540 // maybe avoid using system()? |
1
|
541 |
1443
|
542 volatile sig_handler *saved_sigint_handler |
|
543 = octave_set_signal_handler (SIGINT, SIG_IGN); |
|
544 |
1
|
545 system (cmd); |
1443
|
546 |
|
547 octave_set_signal_handler (SIGINT, saved_sigint_handler); |
1
|
548 |
1358
|
549 // Write the commands to the history file since parse_and_execute |
|
550 // disables command line history while it executes. |
1
|
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 { |
1358
|
558 // Skip blank lines. |
1
|
559 |
|
560 if (line[0] == '\n') |
|
561 { |
|
562 delete [] line; |
|
563 continue; |
|
564 } |
|
565 |
64
|
566 if (first) |
|
567 { |
|
568 first = 0; |
|
569 edit_history_repl_hist (line); |
|
570 } |
|
571 else |
|
572 edit_history_add_hist (line); |
1
|
573 } |
|
574 |
|
575 file.close (); |
|
576 |
1358
|
577 // Turn on command echo, so the output from this will make better |
|
578 // sense. |
1
|
579 |
|
580 begin_unwind_frame ("do_edit_history"); |
1588
|
581 unwind_protect_int (user_pref.echo_executing_commands); |
168
|
582 unwind_protect_int (input_from_tmp_history_file); |
1588
|
583 user_pref.echo_executing_commands = ECHO_CMD_LINE; |
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 |
1358
|
590 // Delete the temporary file. Should probably be done with an |
|
591 // unwind_protect. |
1
|
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 |
1358
|
606 // Turn on command echo, so the output from this will make better |
|
607 // sense. |
64
|
608 |
|
609 begin_unwind_frame ("do_run_history"); |
1588
|
610 unwind_protect_int (user_pref.echo_executing_commands); |
168
|
611 unwind_protect_int (input_from_tmp_history_file); |
1588
|
612 user_pref.echo_executing_commands = ECHO_CMD_LINE; |
168
|
613 input_from_tmp_history_file = 1; |
64
|
614 |
|
615 parse_and_execute (name, 1); |
|
616 |
|
617 run_unwind_frame ("do_run_history"); |
|
618 |
1358
|
619 // Delete the temporary file. Should probably be done with an |
|
620 // unwind_protect. |
64
|
621 |
|
622 unlink (name); |
168
|
623 |
|
624 delete [] name; |
64
|
625 } |
|
626 |
1
|
627 int |
|
628 current_history_number (void) |
|
629 { |
|
630 using_history (); |
|
631 |
|
632 if (octave_hist_size > 0) |
|
633 return history_base + where_history (); |
|
634 else |
|
635 return -1; |
|
636 |
|
637 } |
|
638 |
1488
|
639 DEFUN_TEXT ("edit_history", Fedit_history, Sedit_history, 10, |
529
|
640 "edit_history [first] [last]\n\ |
|
641 \n\ |
|
642 edit commands from the history list") |
|
643 { |
|
644 Octave_object retval; |
|
645 |
|
646 DEFINE_ARGV("edit_history"); |
|
647 |
|
648 do_edit_history (argc, argv); |
|
649 |
|
650 DELETE_ARGV; |
|
651 |
|
652 return retval; |
|
653 } |
|
654 |
1488
|
655 DEFUN_TEXT ("history", Fhistory, Shistory, 10, |
529
|
656 "history [N] [-w file] [-r file] [-q]\n\ |
|
657 \n\ |
|
658 display, save, or load command history") |
|
659 { |
|
660 Octave_object retval; |
|
661 |
|
662 DEFINE_ARGV("history"); |
|
663 |
|
664 do_history (argc, argv); |
|
665 |
|
666 DELETE_ARGV; |
|
667 |
|
668 return retval; |
|
669 } |
|
670 |
1488
|
671 DEFUN_TEXT ("run_history", Frun_history, Srun_history, 10, |
529
|
672 "run_history [first] [last]\n\ |
|
673 \n\ |
|
674 run commands from the history list") |
|
675 { |
|
676 Octave_object retval; |
|
677 |
|
678 DEFINE_ARGV("run_history"); |
|
679 |
|
680 do_run_history (argc, argv); |
|
681 |
|
682 DELETE_ARGV; |
|
683 |
|
684 return retval; |
|
685 } |
|
686 |
1
|
687 /* |
|
688 ;;; Local Variables: *** |
|
689 ;;; mode: C++ *** |
|
690 ;;; page-delimiter: "^/\\*" *** |
|
691 ;;; End: *** |
|
692 */ |