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