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