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