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