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