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