1
|
1 /* |
|
2 |
2847
|
3 Copyright (C) 1996, 1997 John W. Eaton |
1
|
4 |
|
5 This file is part of Octave. |
|
6 |
|
7 Octave is free software; you can redistribute it and/or modify it |
|
8 under the terms of the GNU General Public License as published by the |
|
9 Free Software Foundation; either version 2, or (at your option) any |
|
10 later version. |
|
11 |
|
12 Octave is distributed in the hope that it will be useful, but WITHOUT |
|
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
15 for more details. |
|
16 |
|
17 You should have received a copy of the GNU General Public License |
|
18 along with Octave; see the file COPYING. If not, write to the Free |
1315
|
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
1
|
20 |
|
21 The functions listed below were adapted from similar functions from |
|
22 GNU Bash, the Bourne Again SHell, copyright (C) 1987, 1989, 1991 Free |
|
23 Software Foundation, Inc. |
|
24 |
|
25 do_history edit_history_readline |
|
26 do_edit_history edit_history_add_hist |
|
27 |
|
28 */ |
|
29 |
240
|
30 #ifdef HAVE_CONFIG_H |
1192
|
31 #include <config.h> |
1
|
32 #endif |
|
33 |
1346
|
34 #include <csignal> |
1343
|
35 #include <cstdlib> |
|
36 #include <cstring> |
|
37 |
1750
|
38 #include <string> |
|
39 |
1350
|
40 #include <fstream.h> |
|
41 |
|
42 #ifdef HAVE_UNISTD_H |
2442
|
43 #ifdef HAVE_SYS_TYPES_H |
1
|
44 #include <sys/types.h> |
2442
|
45 #endif |
1
|
46 #include <unistd.h> |
|
47 #endif |
1350
|
48 |
1799
|
49 #include "cmd-hist.h" |
1802
|
50 #include "file-ops.h" |
2926
|
51 #include "oct-env.h" |
1755
|
52 #include "str-vec.h" |
|
53 |
2492
|
54 #include <defaults.h> |
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" |
1690
|
62 #include "sysdep.h" |
1750
|
63 #include "toplev.h" |
1352
|
64 #include "unwind-prot.h" |
|
65 #include "utils.h" |
2205
|
66 #include "variables.h" |
1
|
67 |
168
|
68 // Nonzero means input is coming from temporary history file. |
|
69 int input_from_tmp_history_file = 0; |
|
70 |
581
|
71 // Get some default values, possibly reading them from the |
|
72 // environment. |
|
73 |
1646
|
74 int |
1
|
75 default_history_size (void) |
|
76 { |
|
77 int size = 1024; |
2926
|
78 |
|
79 string env_size = octave_env::getenv ("OCTAVE_HISTSIZE"); |
|
80 |
|
81 if (! env_size.empty ()) |
1
|
82 { |
|
83 int val; |
2926
|
84 |
|
85 if (sscanf (env_size.c_str (), "%d", &val) == 1) |
1
|
86 size = val > 0 ? val : 0; |
|
87 } |
2926
|
88 |
1
|
89 return size; |
|
90 } |
|
91 |
1755
|
92 string |
1
|
93 default_history_file (void) |
|
94 { |
1755
|
95 string file; |
1
|
96 |
2926
|
97 string env_file = octave_env::getenv ("OCTAVE_HISTFILE"); |
1755
|
98 |
2926
|
99 if (! env_file.empty ()) |
1
|
100 { |
2926
|
101 fstream f (env_file.c_str (), (ios::in | ios::out)); |
1755
|
102 |
529
|
103 if (f) |
1
|
104 { |
1755
|
105 file = env_file; |
1
|
106 f.close (); |
|
107 } |
|
108 } |
|
109 |
1755
|
110 if (file.empty ()) |
|
111 { |
2926
|
112 string home_dir = octave_env::get_home_directory (); |
|
113 |
|
114 if (! home_dir.empty ()) |
1969
|
115 { |
2926
|
116 file = home_dir; |
1969
|
117 file.append ("/.octave_hist"); |
|
118 } |
1755
|
119 else |
|
120 file = ".octave_hist"; |
|
121 } |
1
|
122 |
|
123 return file; |
|
124 } |
|
125 |
581
|
126 // Display, save, or load history. Stolen and modified from bash. |
|
127 // |
|
128 // Arg of -w FILENAME means write file, arg of -r FILENAME |
|
129 // means read file, arg of -q means don't number lines. Arg of N |
|
130 // means only display that many items. |
|
131 |
1755
|
132 static void |
|
133 do_history (int argc, const string_vector& argv) |
1
|
134 { |
|
135 int numbered_output = 1; |
|
136 |
1755
|
137 int i; |
|
138 for (i = 1; i < argc; i++) |
1
|
139 { |
2435
|
140 string option = argv[i]; |
|
141 |
|
142 if (option == "-r" || option == "-w" || option == "-a" |
|
143 || option == "-n") |
1
|
144 { |
1755
|
145 if (i < argc - 1) |
1799
|
146 { |
2926
|
147 string file = file_ops::tilde_expand (argv[i+1]); |
|
148 command_history::set_file (file); |
1799
|
149 } |
1
|
150 |
2435
|
151 if (option == "-a") |
|
152 // Append `new' lines to file. |
2926
|
153 command_history::append (); |
2435
|
154 |
|
155 else if (option == "-w") |
|
156 // Write entire history. |
2926
|
157 command_history::write (); |
777
|
158 |
2435
|
159 else if (option == "-r") |
|
160 // Read entire file. |
2926
|
161 command_history::read (); |
777
|
162 |
2435
|
163 else if (option == "-n") |
|
164 // Read `new' history from file. |
2926
|
165 command_history::read_range (); |
777
|
166 |
2435
|
167 else |
|
168 panic_impossible (); |
|
169 |
1
|
170 return; |
|
171 } |
1755
|
172 else if (argv[i] == "-q") |
1
|
173 numbered_output = 0; |
1755
|
174 else if (argv[i] == "--") |
1
|
175 { |
1755
|
176 i++; |
1
|
177 break; |
|
178 } |
|
179 else |
|
180 break; |
|
181 } |
|
182 |
1799
|
183 int limit = -1; |
1
|
184 |
1755
|
185 if (i < argc) |
1
|
186 { |
1755
|
187 if (sscanf (argv[i].c_str (), "%d", &limit) != 1) |
1
|
188 { |
1755
|
189 if (argv[i][0] == '-') |
|
190 error ("history: unrecognized option `%s'", argv[i].c_str ()); |
1
|
191 else |
1755
|
192 error ("history: bad non-numeric arg `%s'", argv[i].c_str ()); |
1799
|
193 |
1
|
194 return; |
|
195 } |
|
196 |
|
197 if (limit < 0) |
|
198 limit = -limit; |
1799
|
199 } |
1
|
200 |
2926
|
201 string_vector hlist = command_history::list (limit, numbered_output); |
1
|
202 |
1799
|
203 int len = hlist.length (); |
|
204 |
2095
|
205 for (i = 0; i < len; i++) |
|
206 octave_stdout << hlist[i] << "\n"; |
1
|
207 } |
|
208 |
581
|
209 // Read the edited history lines from STREAM and return them |
|
210 // one at a time. This can read unlimited length lines. The |
2434
|
211 // caller should free the storage. |
581
|
212 |
1
|
213 static char * |
|
214 edit_history_readline (fstream& stream) |
|
215 { |
|
216 char c; |
|
217 int line_len = 128; |
|
218 int lindex = 0; |
|
219 char *line = new char [line_len]; |
|
220 line[0] = '\0'; |
|
221 |
|
222 while (stream.get (c)) |
|
223 { |
|
224 if (lindex + 2 >= line_len) |
|
225 { |
|
226 char *tmp_line = new char [line_len += 128]; |
|
227 strcpy (tmp_line, line); |
|
228 delete [] line; |
|
229 line = tmp_line; |
|
230 } |
|
231 |
|
232 if (c == '\n') |
|
233 { |
|
234 line[lindex++] = '\n'; |
|
235 line[lindex++] = '\0'; |
|
236 return line; |
|
237 } |
|
238 else |
|
239 line[lindex++] = c; |
|
240 } |
|
241 |
|
242 if (! lindex) |
|
243 { |
|
244 delete [] line; |
529
|
245 return 0; |
1
|
246 } |
|
247 |
|
248 if (lindex + 2 >= line_len) |
|
249 { |
|
250 char *tmp_line = new char [lindex+3]; |
|
251 strcpy (tmp_line, line); |
|
252 delete [] line; |
|
253 line = tmp_line; |
|
254 } |
|
255 |
1358
|
256 // Finish with newline if none in file. |
1
|
257 |
|
258 line[lindex++] = '\n'; |
|
259 line[lindex++] = '\0'; |
|
260 return line; |
|
261 } |
|
262 |
581
|
263 // Use `command' to replace the last entry in the history list, which, |
|
264 // by this time, is `run_history blah...'. The intent is that the |
1799
|
265 // new command becomes the history entry, and that `fc' should never |
581
|
266 // appear in the history list. This way you can do `run_history' to |
|
267 // your heart's content. |
|
268 |
64
|
269 static void |
1799
|
270 edit_history_repl_hist (const string& command) |
64
|
271 { |
1799
|
272 if (! command.empty ()) |
|
273 { |
2926
|
274 string_vector hlist = command_history::list (); |
64
|
275 |
1799
|
276 int len = hlist.length (); |
64
|
277 |
1799
|
278 if (len > 0) |
|
279 { |
|
280 int i = len - 1; |
64
|
281 |
2926
|
282 string histent = command_history::get_entry (i); |
64
|
283 |
1799
|
284 if (! histent.empty ()) |
|
285 { |
|
286 string cmd = command; |
64
|
287 |
1799
|
288 int cmd_len = cmd.length (); |
64
|
289 |
1799
|
290 if (cmd[cmd_len - 1] == '\n') |
|
291 cmd.resize (cmd_len - 1); |
64
|
292 |
1799
|
293 if (! cmd.empty ()) |
2926
|
294 command_history::replace_entry (i, cmd); |
1799
|
295 } |
64
|
296 } |
|
297 } |
|
298 } |
|
299 |
1
|
300 static void |
1799
|
301 edit_history_add_hist (const string& line) |
1
|
302 { |
1799
|
303 if (! line.empty ()) |
1
|
304 { |
1799
|
305 string tmp = line; |
1
|
306 |
1799
|
307 int len = tmp.length (); |
|
308 |
|
309 if (len > 0 && tmp[len-1] == '\n') |
|
310 tmp.resize (len - 1); |
|
311 |
|
312 if (! tmp.empty ()) |
2926
|
313 command_history::add (tmp); |
1
|
314 } |
|
315 } |
|
316 |
1755
|
317 static string |
|
318 mk_tmp_hist_file (int argc, const string_vector& argv, |
2804
|
319 int insert_curr, const char *warn_for) |
1
|
320 { |
1799
|
321 string retval; |
1
|
322 |
2926
|
323 string_vector hlist = command_history::list (); |
1
|
324 |
1799
|
325 int hist_count = hlist.length (); |
1
|
326 |
1358
|
327 // The current command line is already part of the history list by |
|
328 // the time we get to this point. Delete it from the list. |
1
|
329 |
|
330 hist_count -= 2; |
1799
|
331 |
64
|
332 if (! insert_curr) |
2926
|
333 command_history::remove (hist_count); |
1799
|
334 |
1
|
335 hist_count--; |
|
336 |
1358
|
337 // If no numbers have been specified, the default is to edit the |
|
338 // last command in the history list. |
1
|
339 |
|
340 int hist_end = hist_count; |
|
341 int hist_beg = hist_count; |
|
342 int reverse = 0; |
|
343 |
1358
|
344 // Process options. |
1
|
345 |
|
346 int usage_error = 0; |
|
347 if (argc == 3) |
|
348 { |
1755
|
349 if (sscanf (argv[1].c_str (), "%d", &hist_beg) != 1 |
|
350 || sscanf (argv[2].c_str (), "%d", &hist_end) != 1) |
1
|
351 usage_error = 1; |
|
352 else |
|
353 { |
|
354 hist_beg--; |
|
355 hist_end--; |
|
356 } |
|
357 } |
|
358 else if (argc == 2) |
|
359 { |
1755
|
360 if (sscanf (argv[1].c_str (), "%d", &hist_beg) != 1) |
1
|
361 usage_error = 1; |
|
362 else |
|
363 { |
|
364 hist_beg--; |
|
365 hist_end = hist_beg; |
|
366 } |
|
367 } |
|
368 |
|
369 if (hist_beg < 0 || hist_end < 0 || hist_beg > hist_count |
|
370 || hist_end > hist_count) |
|
371 { |
64
|
372 error ("%s: history specification out of range", warn_for); |
1799
|
373 return retval; |
1
|
374 } |
|
375 |
|
376 if (usage_error) |
|
377 { |
64
|
378 usage ("%s [first] [last]", warn_for); |
1799
|
379 return retval; |
1
|
380 } |
|
381 |
|
382 if (hist_end < hist_beg) |
|
383 { |
|
384 int t = hist_end; |
|
385 hist_end = hist_beg; |
|
386 hist_beg = t; |
|
387 reverse = 1; |
|
388 } |
|
389 |
2926
|
390 string name = file_ops::tempnam (); |
1
|
391 |
1755
|
392 fstream file (name.c_str (), ios::out); |
64
|
393 |
1
|
394 if (! file) |
|
395 { |
1755
|
396 error ("%s: couldn't open temporary file `%s'", warn_for, |
|
397 name.c_str ()); |
1799
|
398 return retval; |
1
|
399 } |
|
400 |
|
401 if (reverse) |
|
402 { |
|
403 for (int i = hist_end; i >= hist_beg; i--) |
1799
|
404 file << hlist[i] << "\n"; |
1
|
405 } |
|
406 else |
|
407 { |
|
408 for (int i = hist_beg; i <= hist_end; i++) |
1799
|
409 file << hlist[i] << "\n"; |
1
|
410 } |
|
411 |
|
412 file.close (); |
|
413 |
1755
|
414 return name; |
64
|
415 } |
|
416 |
1755
|
417 static void |
|
418 do_edit_history (int argc, const string_vector& argv) |
64
|
419 { |
1755
|
420 string name = mk_tmp_hist_file (argc, argv, 0, "edit_history"); |
64
|
421 |
1755
|
422 if (name.empty ()) |
64
|
423 return; |
|
424 |
1358
|
425 // Call up our favorite editor on the file of commands. |
1
|
426 |
2205
|
427 string cmd = Veditor; |
1799
|
428 cmd.append (" "); |
|
429 cmd.append (name); |
1
|
430 |
1358
|
431 // Ignore interrupts while we are off editing commands. Should we |
|
432 // maybe avoid using system()? |
1
|
433 |
2705
|
434 volatile octave_interrupt_handler old_interrupt_handler |
2554
|
435 = octave_ignore_interrupts (); |
1443
|
436 |
1799
|
437 system (cmd.c_str ()); |
1443
|
438 |
2554
|
439 octave_set_interrupt_handler (old_interrupt_handler); |
1
|
440 |
1358
|
441 // Write the commands to the history file since parse_and_execute |
|
442 // disables command line history while it executes. |
1
|
443 |
1755
|
444 fstream file (name.c_str (), ios::in); |
1
|
445 |
|
446 char *line; |
64
|
447 int first = 1; |
529
|
448 while ((line = edit_history_readline (file)) != 0) |
1
|
449 { |
1358
|
450 // Skip blank lines. |
1
|
451 |
|
452 if (line[0] == '\n') |
|
453 { |
|
454 delete [] line; |
|
455 continue; |
|
456 } |
|
457 |
64
|
458 if (first) |
|
459 { |
|
460 first = 0; |
|
461 edit_history_repl_hist (line); |
|
462 } |
|
463 else |
|
464 edit_history_add_hist (line); |
1
|
465 } |
|
466 |
|
467 file.close (); |
|
468 |
1358
|
469 // Turn on command echo, so the output from this will make better |
|
470 // sense. |
1
|
471 |
|
472 begin_unwind_frame ("do_edit_history"); |
2205
|
473 unwind_protect_int (Vecho_executing_commands); |
168
|
474 unwind_protect_int (input_from_tmp_history_file); |
2205
|
475 Vecho_executing_commands = ECHO_CMD_LINE; |
168
|
476 input_from_tmp_history_file = 1; |
1
|
477 |
2897
|
478 parse_and_execute (name); |
1
|
479 |
|
480 run_unwind_frame ("do_edit_history"); |
|
481 |
1358
|
482 // Delete the temporary file. Should probably be done with an |
|
483 // unwind_protect. |
1
|
484 |
1755
|
485 unlink (name.c_str ()); |
1
|
486 } |
|
487 |
1755
|
488 static void |
|
489 do_run_history (int argc, const string_vector& argv) |
64
|
490 { |
1755
|
491 string name = mk_tmp_hist_file (argc, argv, 1, "run_history"); |
64
|
492 |
1755
|
493 if (name.empty ()) |
64
|
494 return; |
|
495 |
1799
|
496 // Turn on command echo so the output from this will make better |
1358
|
497 // sense. |
64
|
498 |
|
499 begin_unwind_frame ("do_run_history"); |
2205
|
500 unwind_protect_int (Vecho_executing_commands); |
168
|
501 unwind_protect_int (input_from_tmp_history_file); |
2205
|
502 Vecho_executing_commands = ECHO_CMD_LINE; |
168
|
503 input_from_tmp_history_file = 1; |
64
|
504 |
2897
|
505 parse_and_execute (name); |
64
|
506 |
|
507 run_unwind_frame ("do_run_history"); |
|
508 |
1799
|
509 // Delete the temporary file. |
|
510 |
|
511 // XXX FIXME XXX -- should probably be done using an unwind_protect. |
64
|
512 |
1755
|
513 unlink (name.c_str ()); |
64
|
514 } |
|
515 |
1957
|
516 DEFUN_TEXT (edit_history, args, , |
529
|
517 "edit_history [first] [last]\n\ |
|
518 \n\ |
|
519 edit commands from the history list") |
|
520 { |
2086
|
521 octave_value_list retval; |
529
|
522 |
1755
|
523 int argc = args.length () + 1; |
|
524 |
1968
|
525 string_vector argv = args.make_argv ("edit_history"); |
1755
|
526 |
|
527 if (error_state) |
|
528 return retval; |
529
|
529 |
|
530 do_edit_history (argc, argv); |
|
531 |
|
532 return retval; |
|
533 } |
|
534 |
1957
|
535 DEFUN_TEXT (history, args, , |
529
|
536 "history [N] [-w file] [-r file] [-q]\n\ |
|
537 \n\ |
|
538 display, save, or load command history") |
|
539 { |
2086
|
540 octave_value_list retval; |
529
|
541 |
1755
|
542 int argc = args.length () + 1; |
|
543 |
1968
|
544 string_vector argv = args.make_argv ("history"); |
1755
|
545 |
|
546 if (error_state) |
|
547 return retval; |
529
|
548 |
|
549 do_history (argc, argv); |
|
550 |
|
551 return retval; |
|
552 } |
|
553 |
1957
|
554 DEFUN_TEXT (run_history, args, , |
529
|
555 "run_history [first] [last]\n\ |
|
556 \n\ |
|
557 run commands from the history list") |
|
558 { |
2086
|
559 octave_value_list retval; |
529
|
560 |
1755
|
561 int argc = args.length () + 1; |
|
562 |
1968
|
563 string_vector argv = args.make_argv ("run_history"); |
1755
|
564 |
|
565 if (error_state) |
|
566 return retval; |
529
|
567 |
|
568 do_run_history (argc, argv); |
|
569 |
|
570 return retval; |
|
571 } |
|
572 |
1
|
573 /* |
|
574 ;;; Local Variables: *** |
|
575 ;;; mode: C++ *** |
|
576 ;;; End: *** |
|
577 */ |