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 #include <strstream.h> |
|
42 |
|
43 #ifdef HAVE_UNISTD_H |
1
|
44 #include <sys/types.h> |
|
45 #include <unistd.h> |
|
46 #endif |
1350
|
47 |
1799
|
48 #include "cmd-hist.h" |
1802
|
49 #include "file-ops.h" |
1755
|
50 #include "str-vec.h" |
|
51 |
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" |
195
|
62 #include "user-prefs.h" |
1352
|
63 #include "utils.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 { |
|
108 if (! home_directory.empty ()) |
1969
|
109 { |
|
110 file = home_directory; |
|
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 |
|
199 if (len > 0) |
|
200 { |
581
|
201 ostrstream output_buf; |
|
202 |
1799
|
203 for (i = 0; i < len; i++) |
|
204 output_buf << hlist[i] << "\n"; |
581
|
205 |
|
206 output_buf << ends; |
1799
|
207 |
581
|
208 maybe_page_output (output_buf); |
1
|
209 } |
|
210 } |
|
211 |
581
|
212 // Read the edited history lines from STREAM and return them |
|
213 // one at a time. This can read unlimited length lines. The |
|
214 // caller should free the storage. |
|
215 |
1
|
216 static char * |
|
217 edit_history_readline (fstream& stream) |
|
218 { |
|
219 char c; |
|
220 int line_len = 128; |
|
221 int lindex = 0; |
|
222 char *line = new char [line_len]; |
|
223 line[0] = '\0'; |
|
224 |
|
225 while (stream.get (c)) |
|
226 { |
|
227 if (lindex + 2 >= line_len) |
|
228 { |
|
229 char *tmp_line = new char [line_len += 128]; |
|
230 strcpy (tmp_line, line); |
|
231 delete [] line; |
|
232 line = tmp_line; |
|
233 } |
|
234 |
|
235 if (c == '\n') |
|
236 { |
|
237 line[lindex++] = '\n'; |
|
238 line[lindex++] = '\0'; |
|
239 return line; |
|
240 } |
|
241 else |
|
242 line[lindex++] = c; |
|
243 } |
|
244 |
|
245 if (! lindex) |
|
246 { |
|
247 delete [] line; |
529
|
248 return 0; |
1
|
249 } |
|
250 |
|
251 if (lindex + 2 >= line_len) |
|
252 { |
|
253 char *tmp_line = new char [lindex+3]; |
|
254 strcpy (tmp_line, line); |
|
255 delete [] line; |
|
256 line = tmp_line; |
|
257 } |
|
258 |
1358
|
259 // Finish with newline if none in file. |
1
|
260 |
|
261 line[lindex++] = '\n'; |
|
262 line[lindex++] = '\0'; |
|
263 return line; |
|
264 } |
|
265 |
581
|
266 // Use `command' to replace the last entry in the history list, which, |
|
267 // by this time, is `run_history blah...'. The intent is that the |
1799
|
268 // new command becomes the history entry, and that `fc' should never |
581
|
269 // appear in the history list. This way you can do `run_history' to |
|
270 // your heart's content. |
|
271 |
64
|
272 static void |
1799
|
273 edit_history_repl_hist (const string& command) |
64
|
274 { |
1799
|
275 if (! command.empty ()) |
|
276 { |
|
277 string_vector hlist = octave_command_history.list (); |
64
|
278 |
1799
|
279 int len = hlist.length (); |
64
|
280 |
1799
|
281 if (len > 0) |
|
282 { |
|
283 int i = len - 1; |
64
|
284 |
1799
|
285 string histent = octave_command_history.get_entry (i); |
64
|
286 |
1799
|
287 if (! histent.empty ()) |
|
288 { |
|
289 string cmd = command; |
64
|
290 |
1799
|
291 int cmd_len = cmd.length (); |
64
|
292 |
1799
|
293 if (cmd[cmd_len - 1] == '\n') |
|
294 cmd.resize (cmd_len - 1); |
64
|
295 |
1799
|
296 if (! cmd.empty ()) |
|
297 octave_command_history.replace_entry (i, cmd); |
|
298 } |
64
|
299 } |
|
300 } |
|
301 } |
|
302 |
1
|
303 static void |
1799
|
304 edit_history_add_hist (const string& line) |
1
|
305 { |
1799
|
306 if (! line.empty ()) |
1
|
307 { |
1799
|
308 string tmp = line; |
1
|
309 |
1799
|
310 int len = tmp.length (); |
|
311 |
|
312 if (len > 0 && tmp[len-1] == '\n') |
|
313 tmp.resize (len - 1); |
|
314 |
|
315 if (! tmp.empty ()) |
|
316 octave_command_history.add (tmp); |
1
|
317 } |
|
318 } |
|
319 |
1755
|
320 static string |
|
321 mk_tmp_hist_file (int argc, const string_vector& argv, |
|
322 int insert_curr, char *warn_for) |
1
|
323 { |
1799
|
324 string retval; |
1
|
325 |
1799
|
326 string_vector hlist = octave_command_history.list (); |
1
|
327 |
1799
|
328 int hist_count = hlist.length (); |
1
|
329 |
1358
|
330 // The current command line is already part of the history list by |
|
331 // the time we get to this point. Delete it from the list. |
1
|
332 |
|
333 hist_count -= 2; |
1799
|
334 |
64
|
335 if (! insert_curr) |
1799
|
336 octave_command_history.remove (hist_count); |
|
337 |
1
|
338 hist_count--; |
|
339 |
1358
|
340 // If no numbers have been specified, the default is to edit the |
|
341 // last command in the history list. |
1
|
342 |
|
343 int hist_end = hist_count; |
|
344 int hist_beg = hist_count; |
|
345 int reverse = 0; |
|
346 |
1358
|
347 // Process options. |
1
|
348 |
|
349 int usage_error = 0; |
|
350 if (argc == 3) |
|
351 { |
1755
|
352 if (sscanf (argv[1].c_str (), "%d", &hist_beg) != 1 |
|
353 || sscanf (argv[2].c_str (), "%d", &hist_end) != 1) |
1
|
354 usage_error = 1; |
|
355 else |
|
356 { |
|
357 hist_beg--; |
|
358 hist_end--; |
|
359 } |
|
360 } |
|
361 else if (argc == 2) |
|
362 { |
1755
|
363 if (sscanf (argv[1].c_str (), "%d", &hist_beg) != 1) |
1
|
364 usage_error = 1; |
|
365 else |
|
366 { |
|
367 hist_beg--; |
|
368 hist_end = hist_beg; |
|
369 } |
|
370 } |
|
371 |
|
372 if (hist_beg < 0 || hist_end < 0 || hist_beg > hist_count |
|
373 || hist_end > hist_count) |
|
374 { |
64
|
375 error ("%s: history specification out of range", warn_for); |
1799
|
376 return retval; |
1
|
377 } |
|
378 |
|
379 if (usage_error) |
|
380 { |
64
|
381 usage ("%s [first] [last]", warn_for); |
1799
|
382 return retval; |
1
|
383 } |
|
384 |
|
385 if (hist_end < hist_beg) |
|
386 { |
|
387 int t = hist_end; |
|
388 hist_end = hist_beg; |
|
389 hist_beg = t; |
|
390 reverse = 1; |
|
391 } |
|
392 |
1802
|
393 string name = oct_tempnam (); |
1
|
394 |
1755
|
395 fstream file (name.c_str (), ios::out); |
64
|
396 |
1
|
397 if (! file) |
|
398 { |
1755
|
399 error ("%s: couldn't open temporary file `%s'", warn_for, |
|
400 name.c_str ()); |
1799
|
401 return retval; |
1
|
402 } |
|
403 |
|
404 if (reverse) |
|
405 { |
|
406 for (int i = hist_end; i >= hist_beg; i--) |
1799
|
407 file << hlist[i] << "\n"; |
1
|
408 } |
|
409 else |
|
410 { |
|
411 for (int i = hist_beg; i <= hist_end; i++) |
1799
|
412 file << hlist[i] << "\n"; |
1
|
413 } |
|
414 |
|
415 file.close (); |
|
416 |
1755
|
417 return name; |
64
|
418 } |
|
419 |
1755
|
420 static void |
|
421 do_edit_history (int argc, const string_vector& argv) |
64
|
422 { |
1755
|
423 string name = mk_tmp_hist_file (argc, argv, 0, "edit_history"); |
64
|
424 |
1755
|
425 if (name.empty ()) |
64
|
426 return; |
|
427 |
1358
|
428 // Call up our favorite editor on the file of commands. |
1
|
429 |
1799
|
430 string cmd = user_pref.editor; |
|
431 cmd.append (" "); |
|
432 cmd.append (name); |
1
|
433 |
1358
|
434 // Ignore interrupts while we are off editing commands. Should we |
|
435 // maybe avoid using system()? |
1
|
436 |
1443
|
437 volatile sig_handler *saved_sigint_handler |
|
438 = octave_set_signal_handler (SIGINT, SIG_IGN); |
|
439 |
1799
|
440 system (cmd.c_str ()); |
1443
|
441 |
|
442 octave_set_signal_handler (SIGINT, saved_sigint_handler); |
1
|
443 |
1358
|
444 // Write the commands to the history file since parse_and_execute |
|
445 // disables command line history while it executes. |
1
|
446 |
1755
|
447 fstream file (name.c_str (), ios::in); |
1
|
448 |
|
449 char *line; |
64
|
450 int first = 1; |
529
|
451 while ((line = edit_history_readline (file)) != 0) |
1
|
452 { |
1358
|
453 // Skip blank lines. |
1
|
454 |
|
455 if (line[0] == '\n') |
|
456 { |
|
457 delete [] line; |
|
458 continue; |
|
459 } |
|
460 |
64
|
461 if (first) |
|
462 { |
|
463 first = 0; |
|
464 edit_history_repl_hist (line); |
|
465 } |
|
466 else |
|
467 edit_history_add_hist (line); |
1
|
468 } |
|
469 |
|
470 file.close (); |
|
471 |
1358
|
472 // Turn on command echo, so the output from this will make better |
|
473 // sense. |
1
|
474 |
|
475 begin_unwind_frame ("do_edit_history"); |
1588
|
476 unwind_protect_int (user_pref.echo_executing_commands); |
168
|
477 unwind_protect_int (input_from_tmp_history_file); |
1588
|
478 user_pref.echo_executing_commands = ECHO_CMD_LINE; |
168
|
479 input_from_tmp_history_file = 1; |
1
|
480 |
|
481 parse_and_execute (name, 1); |
|
482 |
|
483 run_unwind_frame ("do_edit_history"); |
|
484 |
1358
|
485 // Delete the temporary file. Should probably be done with an |
|
486 // unwind_protect. |
1
|
487 |
1755
|
488 unlink (name.c_str ()); |
1
|
489 } |
|
490 |
1755
|
491 static void |
|
492 do_run_history (int argc, const string_vector& argv) |
64
|
493 { |
1755
|
494 string name = mk_tmp_hist_file (argc, argv, 1, "run_history"); |
64
|
495 |
1755
|
496 if (name.empty ()) |
64
|
497 return; |
|
498 |
1799
|
499 // Turn on command echo so the output from this will make better |
1358
|
500 // sense. |
64
|
501 |
|
502 begin_unwind_frame ("do_run_history"); |
1588
|
503 unwind_protect_int (user_pref.echo_executing_commands); |
168
|
504 unwind_protect_int (input_from_tmp_history_file); |
1588
|
505 user_pref.echo_executing_commands = ECHO_CMD_LINE; |
168
|
506 input_from_tmp_history_file = 1; |
64
|
507 |
|
508 parse_and_execute (name, 1); |
|
509 |
|
510 run_unwind_frame ("do_run_history"); |
|
511 |
1799
|
512 // Delete the temporary file. |
|
513 |
|
514 // XXX FIXME XXX -- should probably be done using an unwind_protect. |
64
|
515 |
1755
|
516 unlink (name.c_str ()); |
64
|
517 } |
|
518 |
1957
|
519 DEFUN_TEXT (edit_history, args, , |
529
|
520 "edit_history [first] [last]\n\ |
|
521 \n\ |
|
522 edit commands from the history list") |
|
523 { |
2086
|
524 octave_value_list retval; |
529
|
525 |
1755
|
526 int argc = args.length () + 1; |
|
527 |
1968
|
528 string_vector argv = args.make_argv ("edit_history"); |
1755
|
529 |
|
530 if (error_state) |
|
531 return retval; |
529
|
532 |
|
533 do_edit_history (argc, argv); |
|
534 |
|
535 return retval; |
|
536 } |
|
537 |
1957
|
538 DEFUN_TEXT (history, args, , |
529
|
539 "history [N] [-w file] [-r file] [-q]\n\ |
|
540 \n\ |
|
541 display, save, or load command history") |
|
542 { |
2086
|
543 octave_value_list retval; |
529
|
544 |
1755
|
545 int argc = args.length () + 1; |
|
546 |
1968
|
547 string_vector argv = args.make_argv ("history"); |
1755
|
548 |
|
549 if (error_state) |
|
550 return retval; |
529
|
551 |
|
552 do_history (argc, argv); |
|
553 |
|
554 return retval; |
|
555 } |
|
556 |
1957
|
557 DEFUN_TEXT (run_history, args, , |
529
|
558 "run_history [first] [last]\n\ |
|
559 \n\ |
|
560 run commands from the history list") |
|
561 { |
2086
|
562 octave_value_list retval; |
529
|
563 |
1755
|
564 int argc = args.length () + 1; |
|
565 |
1968
|
566 string_vector argv = args.make_argv ("run_history"); |
1755
|
567 |
|
568 if (error_state) |
|
569 return retval; |
529
|
570 |
|
571 do_run_history (argc, argv); |
|
572 |
|
573 return retval; |
|
574 } |
|
575 |
1
|
576 /* |
|
577 ;;; Local Variables: *** |
|
578 ;;; mode: C++ *** |
|
579 ;;; End: *** |
|
580 */ |