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