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