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 */ |
|
22 |
2939
|
23 // Get command input interactively or from files. |
1
|
24 |
240
|
25 #ifdef HAVE_CONFIG_H |
1192
|
26 #include <config.h> |
1
|
27 #endif |
|
28 |
1343
|
29 #include <cstdio> |
|
30 #include <cstdlib> |
|
31 #include <cstring> |
|
32 #include <cassert> |
|
33 |
3503
|
34 #include <iostream> |
1728
|
35 #include <string> |
|
36 |
1350
|
37 #ifdef HAVE_UNISTD_H |
2442
|
38 #ifdef HAVE_SYS_TYPES_H |
529
|
39 #include <sys/types.h> |
2442
|
40 #endif |
529
|
41 #include <unistd.h> |
|
42 #endif |
1343
|
43 |
2927
|
44 #include "cmd-edit.h" |
3189
|
45 #include "file-ops.h" |
1755
|
46 #include "str-vec.h" |
|
47 |
1352
|
48 #include "defun.h" |
|
49 #include "dirfns.h" |
1
|
50 #include "error.h" |
2181
|
51 #include "gripes.h" |
3016
|
52 #include "help.h" |
1
|
53 #include "input.h" |
1352
|
54 #include "oct-map.h" |
1742
|
55 #include "oct-hist.h" |
1670
|
56 #include "toplev.h" |
1755
|
57 #include "oct-obj.h" |
1
|
58 #include "pager.h" |
529
|
59 #include "parse.h" |
1352
|
60 #include "pathlen.h" |
3772
|
61 #include "pt.h" |
1755
|
62 #include "pt-const.h" |
3805
|
63 #include "pt-stmt.h" |
1352
|
64 #include "sighandlers.h" |
|
65 #include "symtab.h" |
1114
|
66 #include "sysdep.h" |
3098
|
67 #include "unwind-prot.h" |
1352
|
68 #include "utils.h" |
|
69 #include "variables.h" |
529
|
70 |
2181
|
71 // Primary prompt string. |
3523
|
72 static std::string Vps1; |
2181
|
73 |
|
74 // Secondary prompt string. |
3523
|
75 static std::string Vps2; |
2181
|
76 |
|
77 // String printed before echoed input (enabled by --echo-input). |
3523
|
78 std::string Vps4; |
2181
|
79 |
3019
|
80 // Echo commands as they are executed? |
|
81 // |
|
82 // 1 ==> echo commands read from script files |
|
83 // 2 ==> echo commands from functions |
|
84 // 4 ==> echo commands read from command line |
|
85 // |
|
86 // more than one state can be active at once. |
|
87 int Vecho_executing_commands; |
|
88 |
3165
|
89 // The time we last printed a prompt. |
3255
|
90 octave_time Vlast_prompt_time; |
3165
|
91 |
2181
|
92 // Character to append after successful command-line completion attempts. |
|
93 static char Vcompletion_append_char; |
|
94 |
1
|
95 // Global pointer for eval(). |
3523
|
96 std::string current_eval_string; |
1
|
97 |
3019
|
98 // TRUE means get input from current_eval_string. |
|
99 bool get_input_from_eval_string = false; |
1
|
100 |
3019
|
101 // TRUE means we're parsing a function file. |
|
102 bool reading_fcn_file = false; |
1
|
103 |
338
|
104 // Simple name of function file we are reading. |
3523
|
105 std::string curr_fcn_file_name; |
1606
|
106 |
|
107 // Full name of file we are reading. |
3523
|
108 std::string curr_fcn_file_full_name; |
1
|
109 |
3019
|
110 // TRUE means we're parsing a script file. |
|
111 bool reading_script_file = false; |
1
|
112 |
|
113 // If we are reading from an M-file, this is it. |
529
|
114 FILE *ff_instream = 0; |
1
|
115 |
3019
|
116 // TRUE means this is an interactive shell. |
|
117 bool interactive = false; |
1
|
118 |
3019
|
119 // TRUE means the user forced this shell to be interactive (-i). |
|
120 bool forced_interactive = false; |
1
|
121 |
|
122 // Should we issue a prompt? |
|
123 int promptflag = 1; |
|
124 |
|
125 // The current line of input, from wherever. |
3523
|
126 std::string current_input_line; |
1
|
127 |
3804
|
128 // TRUE after a call to completion_matches. |
2299
|
129 bool octave_completion_matches_called = false; |
|
130 |
1044
|
131 static void |
3523
|
132 do_input_echo (const std::string& input_string) |
1044
|
133 { |
1588
|
134 int do_echo = reading_script_file ? |
2205
|
135 (Vecho_executing_commands & ECHO_SCRIPTS) |
2618
|
136 : (Vecho_executing_commands & ECHO_CMD_LINE) && ! forced_interactive; |
1588
|
137 |
|
138 if (do_echo) |
1044
|
139 { |
1403
|
140 if (forced_interactive) |
|
141 { |
1755
|
142 if (promptflag > 0) |
2927
|
143 octave_stdout << command_editor::decode_prompt_string (Vps1); |
1755
|
144 else |
2927
|
145 octave_stdout << command_editor::decode_prompt_string (Vps2); |
1403
|
146 } |
|
147 else |
2927
|
148 octave_stdout << command_editor::decode_prompt_string (Vps4); |
1044
|
149 |
1755
|
150 if (! input_string.empty ()) |
1044
|
151 { |
2095
|
152 octave_stdout << input_string; |
1755
|
153 |
|
154 if (input_string[input_string.length () - 1] != '\n') |
2095
|
155 octave_stdout << "\n"; |
1044
|
156 } |
|
157 } |
|
158 } |
|
159 |
3536
|
160 std::string |
3523
|
161 gnu_readline (const std::string& s, bool force_readline) |
1822
|
162 { |
3523
|
163 std::string retval; |
1822
|
164 |
2927
|
165 if (line_editing || force_readline) |
1822
|
166 { |
3219
|
167 bool eof; |
1898
|
168 |
3219
|
169 retval = command_editor::readline (s, eof); |
|
170 |
|
171 if (! eof && retval.empty ()) |
2927
|
172 retval = "\n"; |
1822
|
173 } |
|
174 else |
|
175 { |
2927
|
176 if (! s.empty () && (interactive || forced_interactive)) |
2618
|
177 { |
2927
|
178 FILE *stream = command_editor::get_output_stream (); |
|
179 |
|
180 fprintf (stream, s.c_str ()); |
|
181 fflush (stream); |
2618
|
182 } |
1822
|
183 |
2927
|
184 FILE *curr_stream = command_editor::get_input_stream (); |
|
185 |
1822
|
186 if (reading_fcn_file || reading_script_file) |
|
187 curr_stream = ff_instream; |
|
188 |
2927
|
189 retval = octave_fgets (curr_stream); |
1822
|
190 } |
|
191 |
|
192 return retval; |
|
193 } |
581
|
194 |
3536
|
195 static std::string |
1
|
196 octave_gets (void) |
|
197 { |
3523
|
198 std::string retval; |
1
|
199 |
3255
|
200 Vlast_prompt_time.stamp (); |
3165
|
201 |
1822
|
202 if ((interactive || forced_interactive) |
|
203 && (! (reading_fcn_file || reading_script_file))) |
1
|
204 { |
3523
|
205 std::string ps = (promptflag > 0) ? Vps1 : Vps2; |
1755
|
206 |
3523
|
207 std::string prompt = command_editor::decode_prompt_string (ps); |
1
|
208 |
2618
|
209 pipe_handler_error_count = 0; |
|
210 |
|
211 flush_octave_stdout (); |
1
|
212 |
2095
|
213 octave_diary << prompt; |
581
|
214 |
2927
|
215 retval = gnu_readline (prompt); |
1
|
216 } |
|
217 else |
1822
|
218 retval = gnu_readline (""); |
1
|
219 |
2927
|
220 current_input_line = retval; |
1
|
221 |
1822
|
222 if (! current_input_line.empty ()) |
1
|
223 { |
1799
|
224 if (! input_from_startup_file) |
2927
|
225 command_history::add (current_input_line); |
1
|
226 |
3176
|
227 if (! (reading_fcn_file || reading_script_file)) |
|
228 { |
|
229 octave_diary << current_input_line; |
|
230 |
|
231 if (current_input_line[current_input_line.length () - 1] != '\n') |
|
232 octave_diary << "\n"; |
|
233 } |
581
|
234 |
1822
|
235 do_input_echo (current_input_line); |
1
|
236 } |
3176
|
237 else if (! (reading_fcn_file || reading_script_file)) |
|
238 octave_diary << "\n"; |
581
|
239 |
1822
|
240 return retval; |
1
|
241 } |
|
242 |
581
|
243 // Read a line from the input stream. |
|
244 |
3536
|
245 static std::string |
1822
|
246 get_user_input (void) |
1
|
247 { |
3523
|
248 std::string retval; |
1
|
249 |
|
250 if (get_input_from_eval_string) |
|
251 { |
2927
|
252 retval = current_eval_string; |
1822
|
253 |
2927
|
254 size_t len = retval.length (); |
1822
|
255 |
2927
|
256 if (retval[len-1] != '\n') |
|
257 retval.append ("\n"); |
1822
|
258 } |
|
259 else |
|
260 retval = octave_gets (); |
|
261 |
2927
|
262 current_input_line = retval; |
1822
|
263 |
2114
|
264 if (! get_input_from_eval_string) |
|
265 input_line_number++; |
1822
|
266 |
|
267 return retval; |
|
268 } |
|
269 |
|
270 int |
|
271 octave_read (char *buf, unsigned max_size) |
|
272 { |
2927
|
273 // XXX FIXME XXX -- is this a safe way to buffer the input? |
|
274 |
3523
|
275 static std::string input_buf; |
2927
|
276 static const char *pos = 0; |
|
277 static size_t chars_left = 0; |
1822
|
278 |
|
279 int status = 0; |
|
280 |
2927
|
281 if (input_buf.empty ()) |
1822
|
282 { |
2927
|
283 pos = 0; |
|
284 |
|
285 input_buf = get_user_input (); |
1822
|
286 |
2927
|
287 chars_left = input_buf.length (); |
|
288 |
|
289 pos = input_buf.c_str (); |
1822
|
290 } |
|
291 |
|
292 if (chars_left > 0) |
|
293 { |
|
294 buf[0] = '\0'; |
|
295 |
2927
|
296 size_t len = max_size > 2 ? max_size - 2 : 0; |
1822
|
297 |
2927
|
298 assert (len > 0); |
|
299 |
|
300 strncpy (buf, pos, len); |
1822
|
301 |
|
302 if (chars_left > len) |
1
|
303 { |
1822
|
304 chars_left -= len; |
|
305 |
2927
|
306 pos += len; |
1822
|
307 |
|
308 buf[len] = '\0'; |
|
309 |
1
|
310 status = len; |
|
311 } |
|
312 else |
|
313 { |
2927
|
314 input_buf = ""; |
1755
|
315 |
1822
|
316 len = chars_left; |
|
317 |
|
318 if (buf[len-1] != '\n') |
|
319 buf[len++] = '\n'; |
1
|
320 |
1822
|
321 buf[len] = '\0'; |
1755
|
322 |
1822
|
323 status = len; |
1
|
324 } |
1044
|
325 } |
1822
|
326 else if (chars_left == 0) |
2862
|
327 { |
2927
|
328 input_buf = ""; |
2862
|
329 |
|
330 status = 0; |
|
331 } |
1822
|
332 else |
|
333 status = -1; |
1044
|
334 |
1
|
335 return status; |
|
336 } |
|
337 |
581
|
338 // Fix things up so that input can come from file `name', printing a |
|
339 // warning if the file doesn't exist. |
|
340 |
1
|
341 FILE * |
3523
|
342 get_input_from_file (const std::string& name, int warn) |
1
|
343 { |
529
|
344 FILE *instream = 0; |
1
|
345 |
1750
|
346 if (name.length () > 0) |
|
347 instream = fopen (name.c_str (), "r"); |
1
|
348 |
529
|
349 if (! instream && warn) |
1750
|
350 warning ("%s: no such file or directory", name.c_str ()); |
1
|
351 |
338
|
352 if (reading_fcn_file || reading_script_file) |
339
|
353 ff_instream = instream; |
1
|
354 else |
2927
|
355 command_editor::set_input_stream (instream); |
1
|
356 |
|
357 return instream; |
|
358 } |
|
359 |
581
|
360 // Fix things up so that input can come from the standard input. This |
|
361 // may need to become much more complicated, which is why it's in a |
|
362 // separate function. |
|
363 |
1
|
364 FILE * |
|
365 get_input_from_stdin (void) |
|
366 { |
2927
|
367 command_editor::set_input_stream (stdin); |
|
368 return command_editor::get_input_stream (); |
1
|
369 } |
|
370 |
2921
|
371 // XXX FIXME XXX -- make this generate file names when appropriate. |
|
372 |
2247
|
373 static string_vector |
3523
|
374 generate_possible_completions (const std::string& text, std::string& prefix, |
|
375 std::string& hint) |
1280
|
376 { |
2247
|
377 string_vector names; |
1280
|
378 |
2921
|
379 prefix = ""; |
1280
|
380 |
2921
|
381 if (! text.empty () && text != "." && text.rfind ('.') != NPOS) |
1430
|
382 names = generate_struct_completions (text, prefix, hint); |
|
383 else |
2247
|
384 names = make_name_list (); |
1280
|
385 |
2944
|
386 // Sort and remove duplicates. |
2348
|
387 |
2944
|
388 names.qsort (true); |
2348
|
389 |
1280
|
390 return names; |
|
391 } |
|
392 |
3536
|
393 static std::string |
3523
|
394 generate_completion (const std::string& text, int state) |
1280
|
395 { |
3523
|
396 std::string retval; |
2944
|
397 |
3523
|
398 static std::string prefix; |
|
399 static std::string hint; |
1280
|
400 |
2921
|
401 static size_t prefix_len = 0; |
|
402 static size_t hint_len = 0; |
1280
|
403 |
1
|
404 static int list_index = 0; |
2921
|
405 static int name_list_len = 0; |
2247
|
406 static string_vector name_list; |
1
|
407 |
1280
|
408 static int matches = 0; |
1
|
409 |
|
410 if (state == 0) |
|
411 { |
|
412 list_index = 0; |
|
413 |
2921
|
414 prefix = ""; |
1280
|
415 |
2921
|
416 hint = text; |
1280
|
417 |
|
418 name_list = generate_possible_completions (text, prefix, hint); |
|
419 |
2921
|
420 name_list_len = name_list.length (); |
|
421 |
|
422 prefix_len = prefix.length (); |
1280
|
423 |
2921
|
424 hint_len = hint.length (); |
1280
|
425 |
|
426 matches = 0; |
2247
|
427 |
2921
|
428 for (int i = 0; i < name_list_len; i++) |
3565
|
429 if (hint == name_list[i].substr (0, hint_len)) |
2247
|
430 matches++; |
1
|
431 } |
|
432 |
2921
|
433 if (name_list_len > 0 && matches > 0) |
1
|
434 { |
2921
|
435 while (list_index < name_list_len) |
244
|
436 { |
3523
|
437 std::string name = name_list[list_index]; |
2247
|
438 |
1280
|
439 list_index++; |
2247
|
440 |
3565
|
441 if (hint == name.substr (0, hint_len)) |
1280
|
442 { |
2944
|
443 if (! prefix.empty ()) |
|
444 retval = prefix + "." + name; |
|
445 else |
|
446 retval = name; |
1280
|
447 |
2944
|
448 if (matches == 1 && looks_like_struct (retval)) |
2927
|
449 command_editor::set_completion_append_character ('.'); |
1280
|
450 else |
2927
|
451 command_editor::set_completion_append_character |
|
452 (Vcompletion_append_char); |
1280
|
453 |
2944
|
454 break; |
1280
|
455 } |
244
|
456 } |
1
|
457 } |
|
458 |
2944
|
459 return retval; |
1
|
460 } |
|
461 |
2927
|
462 void |
|
463 initialize_command_input (void) |
|
464 { |
|
465 // If we are using readline, this allows conditional parsing of the |
|
466 // .inputrc file. |
269
|
467 |
2927
|
468 command_editor::set_name ("Octave"); |
1358
|
469 |
3004
|
470 command_editor::set_basic_quote_characters ("\""); |
2944
|
471 |
|
472 command_editor::set_completion_function (generate_completion); |
269
|
473 } |
|
474 |
2927
|
475 static bool |
3523
|
476 match_sans_spaces (const std::string& standard, const std::string& test) |
269
|
477 { |
3180
|
478 size_t beg = test.find_first_not_of (" \t"); |
|
479 |
|
480 if (beg != NPOS) |
|
481 { |
|
482 size_t end = test.find_last_not_of (" \t"); |
269
|
483 |
3180
|
484 size_t len = end == NPOS ? NPOS : end - beg + 1; |
1
|
485 |
3484
|
486 return (test.substr (beg, len) == standard); |
3180
|
487 } |
|
488 |
|
489 return false; |
529
|
490 } |
|
491 |
581
|
492 // If the user simply hits return, this will produce an empty matrix. |
|
493 |
2086
|
494 static octave_value_list |
3100
|
495 get_user_input (const octave_value_list& args, bool debug, int nargout) |
529
|
496 { |
3100
|
497 octave_value_list retval; |
529
|
498 |
|
499 int nargin = args.length (); |
|
500 |
|
501 int read_as_string = 0; |
|
502 |
712
|
503 if (nargin == 2) |
529
|
504 read_as_string++; |
|
505 |
3523
|
506 std::string prompt ("debug> "); |
1761
|
507 |
712
|
508 if (nargin > 0) |
3707
|
509 { |
|
510 prompt = args(0).string_value (); |
636
|
511 |
3707
|
512 if (error_state) |
|
513 { |
|
514 error ("input: unrecognized argument"); |
|
515 return retval; |
|
516 } |
529
|
517 } |
|
518 |
|
519 again: |
|
520 |
2095
|
521 flush_octave_stdout (); |
529
|
522 |
3523
|
523 std::string input_buf = gnu_readline (prompt.c_str (), true); |
529
|
524 |
2927
|
525 if (! input_buf.empty ()) |
529
|
526 { |
1799
|
527 if (! input_from_startup_file) |
2927
|
528 command_history::add (input_buf); |
529
|
529 |
2927
|
530 size_t len = input_buf.length (); |
529
|
531 |
|
532 if (len < 1) |
|
533 { |
|
534 if (debug) |
|
535 goto again; |
|
536 else |
963
|
537 { |
|
538 if (read_as_string) |
|
539 return ""; |
|
540 else |
|
541 return Matrix (); |
|
542 } |
529
|
543 } |
|
544 |
3772
|
545 if (debug) |
|
546 { |
|
547 if (match_sans_spaces ("exit", input_buf) |
3180
|
548 || match_sans_spaces ("quit", input_buf) |
3772
|
549 || match_sans_spaces ("return", input_buf) |
|
550 || match_sans_spaces ("dbg_cont", input_buf)) |
|
551 { |
|
552 return retval; |
|
553 } |
|
554 else if (match_sans_spaces ("dbg_step", input_buf)) |
|
555 { |
|
556 tree::break_next = true; |
3805
|
557 |
|
558 tree::last_line = 0; |
|
559 |
|
560 tree::break_function = curr_function; |
|
561 |
|
562 return retval; |
|
563 } |
|
564 else if (match_sans_spaces ("dbg_next", input_buf)) |
|
565 { |
|
566 tree::break_next = true; |
|
567 |
|
568 tree::last_line = curr_statement->line (); |
|
569 |
|
570 tree::break_function = curr_function; |
|
571 |
3772
|
572 return retval; |
|
573 } |
963
|
574 } |
3772
|
575 |
|
576 if (read_as_string) |
963
|
577 { |
3081
|
578 // XXX FIXME XXX -- fix gnu_readline and octave_gets instead! |
|
579 if (input_buf.length () == 1 && input_buf[0] == '\n') |
3100
|
580 retval(0) = ""; |
3081
|
581 else |
3100
|
582 retval(0) = input_buf; |
963
|
583 } |
529
|
584 else |
|
585 { |
|
586 int parse_status = 0; |
2900
|
587 |
3100
|
588 bool silent = ! debug; |
2900
|
589 |
3100
|
590 retval = eval_string (input_buf, silent, parse_status, nargout); |
|
591 |
3215
|
592 if (! debug && retval.length () == 0) |
3100
|
593 retval(0) = Matrix (); |
529
|
594 } |
|
595 } |
|
596 else |
|
597 error ("input: reading user-input failed!"); |
|
598 |
|
599 if (debug) |
3098
|
600 { |
|
601 // Clear error_state so that if errors were encountered while |
|
602 // evaluating user input, extra error messages will not be |
|
603 // printed after we return. |
|
604 |
|
605 error_state = 0; |
|
606 |
3100
|
607 retval = octave_value_list (); |
3098
|
608 |
|
609 goto again; |
|
610 } |
529
|
611 |
|
612 return retval; |
|
613 } |
|
614 |
3100
|
615 DEFUN (input, args, nargout, |
3372
|
616 "-*- texinfo -*-\n\ |
|
617 @deftypefn {Built-in Function} {} input (@var{prompt})\n\ |
|
618 @deftypefnx {Built-in Function} {} input (@var{prompt}, \"s\")\n\ |
|
619 Print a prompt and wait for user input. For example,\n\ |
|
620 \n\ |
|
621 @example\n\ |
|
622 input (\"Pick a number, any number! \")\n\ |
|
623 @end example\n\ |
|
624 \n\ |
|
625 @noindent\n\ |
|
626 prints the prompt\n\ |
|
627 \n\ |
|
628 @example\n\ |
|
629 Pick a number, any number!\n\ |
|
630 @end example\n\ |
529
|
631 \n\ |
3372
|
632 @noindent\n\ |
|
633 and waits for the user to enter a value. The string entered by the user\n\ |
|
634 is evaluated as an expression, so it may be a literal constant, a\n\ |
|
635 variable name, or any other valid expression.\n\ |
|
636 \n\ |
|
637 Currently, @code{input} only returns one value, regardless of the number\n\ |
|
638 of values produced by the evaluation of the expression.\n\ |
|
639 \n\ |
|
640 If you are only interested in getting a literal string value, you can\n\ |
|
641 call @code{input} with the character string @code{\"s\"} as the second\n\ |
|
642 argument. This tells Octave to return the string entered by the user\n\ |
|
643 directly, without evaluating it first.\n\ |
|
644 \n\ |
|
645 Because there may be output waiting to be displayed by the pager, it is\n\ |
|
646 a good idea to always call @code{fflush (stdout)} before calling\n\ |
|
647 @code{input}. This will ensure that all pending output is written to\n\ |
|
648 the screen before your prompt. @xref{Input and Output}.\n\ |
|
649 @end deftypefn") |
529
|
650 { |
2086
|
651 octave_value_list retval; |
529
|
652 |
|
653 int nargin = args.length (); |
|
654 |
712
|
655 if (nargin == 1 || nargin == 2) |
3100
|
656 retval = get_user_input (args, false, nargout); |
529
|
657 else |
|
658 print_usage ("input"); |
|
659 |
|
660 return retval; |
|
661 } |
|
662 |
3098
|
663 static void |
|
664 restore_command_history (void *) |
|
665 { |
|
666 command_history::ignore_entries (! Vsaving_history); |
|
667 } |
|
668 |
3707
|
669 octave_value |
|
670 do_keyboard (const octave_value_list& args) |
|
671 { |
|
672 octave_value retval; |
|
673 |
|
674 int nargin = args.length (); |
|
675 |
|
676 assert (nargin == 0 || nargin == 1); |
|
677 |
|
678 unwind_protect::begin_frame ("do_keyboard"); |
|
679 |
|
680 // XXX FIXME XXX -- we shouldn't need both the |
|
681 // command_history object and the |
|
682 // Vsaving_history variable... |
|
683 command_history::ignore_entries (false); |
|
684 |
|
685 unwind_protect::add (restore_command_history, 0); |
|
686 |
|
687 unwind_protect_bool (Vsaving_history); |
|
688 |
|
689 Vsaving_history = true; |
|
690 |
|
691 retval = get_user_input (args, true, 0); |
|
692 |
|
693 unwind_protect::run_frame ("do_keyboard"); |
|
694 |
|
695 return retval; |
|
696 } |
|
697 |
1957
|
698 DEFUN (keyboard, args, , |
3372
|
699 "-*- texinfo -*-\n\ |
|
700 @deftypefn {Built-in Function} {} keyboard (@var{prompt})\n\ |
|
701 This function is normally used for simple debugging. When the\n\ |
|
702 @code{keyboard} function is executed, Octave prints a prompt and waits\n\ |
|
703 for user input. The input strings are then evaluated and the results\n\ |
|
704 are printed. This makes it possible to examine the values of variables\n\ |
|
705 within a function, and to assign new values to variables. No value is\n\ |
|
706 returned from the @code{keyboard} function, and it continues to prompt\n\ |
|
707 for input until the user types @samp{quit}, or @samp{exit}.\n\ |
529
|
708 \n\ |
3372
|
709 If @code{keyboard} is invoked without any arguments, a default prompt of\n\ |
|
710 @samp{debug> } is used.\n\ |
|
711 @end deftypefn") |
529
|
712 { |
2086
|
713 octave_value_list retval; |
529
|
714 |
|
715 int nargin = args.length (); |
|
716 |
712
|
717 if (nargin == 0 || nargin == 1) |
3707
|
718 do_keyboard (args); |
529
|
719 else |
|
720 print_usage ("keyboard"); |
|
721 |
|
722 return retval; |
|
723 } |
|
724 |
3741
|
725 DEFUN_TEXT (echo, args, , |
3332
|
726 "-*- texinfo -*-\n\ |
|
727 @deffn {Command} echo options\n\ |
|
728 Control whether commands are displayed as they are executed. Valid\n\ |
|
729 options are:\n\ |
1588
|
730 \n\ |
3332
|
731 @table @code\n\ |
|
732 @item on\n\ |
|
733 Enable echoing of commands as they are executed in script files.\n\ |
|
734 \n\ |
|
735 @item off\n\ |
|
736 Disable echoing of commands as they are executed in script files.\n\ |
1588
|
737 \n\ |
3332
|
738 @item on all\n\ |
|
739 Enable echoing of commands as they are executed in script files and\n\ |
|
740 functions.\n\ |
1588
|
741 \n\ |
3332
|
742 @item off all\n\ |
|
743 Disable echoing of commands as they are executed in script files and\n\ |
|
744 functions.\n\ |
|
745 @end table\n\ |
|
746 \n\ |
|
747 @noindent\n\ |
|
748 If invoked without any arguments, @code{echo} toggles the current echo\n\ |
|
749 state.\n\ |
3333
|
750 @end deffn") |
1588
|
751 { |
2086
|
752 octave_value_list retval; |
1588
|
753 |
1755
|
754 int argc = args.length () + 1; |
|
755 |
1968
|
756 string_vector argv = args.make_argv ("echo"); |
1755
|
757 |
|
758 if (error_state) |
|
759 return retval; |
1588
|
760 |
|
761 switch (argc) |
|
762 { |
|
763 case 1: |
|
764 { |
2205
|
765 if ((Vecho_executing_commands & ECHO_SCRIPTS) |
|
766 || (Vecho_executing_commands & ECHO_FUNCTIONS)) |
2431
|
767 bind_builtin_variable ("echo_executing_commands", |
2800
|
768 static_cast<double> (ECHO_OFF)); |
1588
|
769 else |
2431
|
770 bind_builtin_variable ("echo_executing_commands", |
2800
|
771 static_cast<double> (ECHO_SCRIPTS)); |
1588
|
772 } |
|
773 break; |
|
774 |
|
775 case 2: |
|
776 { |
3523
|
777 std::string arg = argv[1]; |
1755
|
778 |
|
779 if (arg == "on") |
2431
|
780 bind_builtin_variable ("echo_executing_commands", |
2800
|
781 static_cast<double> (ECHO_SCRIPTS)); |
1755
|
782 else if (arg == "off") |
2431
|
783 bind_builtin_variable ("echo_executing_commands", |
2800
|
784 static_cast<double> (ECHO_OFF)); |
1588
|
785 else |
|
786 print_usage ("echo"); |
|
787 } |
|
788 break; |
|
789 |
|
790 case 3: |
|
791 { |
3523
|
792 std::string arg = argv[1]; |
1755
|
793 |
|
794 if (arg == "on" && argv[2] == "all") |
2800
|
795 { |
|
796 int tmp = (ECHO_SCRIPTS | ECHO_FUNCTIONS); |
|
797 bind_builtin_variable ("echo_executing_commands", |
|
798 static_cast<double> (tmp)); |
|
799 } |
1755
|
800 else if (arg == "off" && argv[2] == "all") |
2431
|
801 bind_builtin_variable ("echo_executing_commands", |
2800
|
802 static_cast<double> (ECHO_OFF)); |
1588
|
803 else |
|
804 print_usage ("echo"); |
|
805 } |
|
806 break; |
|
807 |
|
808 default: |
|
809 print_usage ("echo"); |
|
810 break; |
|
811 } |
|
812 |
|
813 return retval; |
|
814 } |
|
815 |
2234
|
816 DEFUN (completion_matches, args, nargout, |
3332
|
817 "-*- texinfo -*-\n\ |
|
818 @deftypefn {Built-in Function} {} completion_matches (@var{hint})\n\ |
|
819 Generate possible completions given @var{hint}.\n\ |
2299
|
820 \n\ |
|
821 This function is provided for the benefit of programs like Emacs which\n\ |
3332
|
822 might be controlling Octave and handling user input. The current\n\ |
|
823 command number is not incremented when this function is called. This is\n\ |
|
824 a feature, not a bug.\n\ |
3333
|
825 @end deftypefn") |
2234
|
826 { |
2281
|
827 octave_value retval; |
2234
|
828 |
|
829 int nargin = args.length (); |
|
830 |
|
831 if (nargin == 1) |
|
832 { |
3523
|
833 std::string hint = args(0).string_value (); |
2234
|
834 |
|
835 if (! error_state) |
|
836 { |
|
837 int n = 32; |
|
838 |
|
839 string_vector list (n); |
|
840 |
|
841 int k = 0; |
|
842 |
|
843 for (;;) |
|
844 { |
3523
|
845 std::string cmd = generate_completion (hint, k); |
2234
|
846 |
2944
|
847 if (! cmd.empty ()) |
2234
|
848 { |
2944
|
849 if (k == n) |
2234
|
850 { |
2944
|
851 n *= 2; |
|
852 list.resize (n); |
|
853 } |
2235
|
854 |
2944
|
855 list[k++] = cmd; |
2234
|
856 } |
|
857 else |
|
858 { |
|
859 list.resize (k); |
|
860 break; |
|
861 } |
|
862 } |
|
863 |
|
864 if (nargout > 0) |
2281
|
865 { |
|
866 if (! list.empty ()) |
|
867 retval = list; |
2282
|
868 else |
|
869 retval = ""; |
2281
|
870 } |
2234
|
871 else |
|
872 { |
2235
|
873 // We don't use string_vector::list_in_columns here |
|
874 // because it will be easier for Emacs if the names |
|
875 // appear in a single column. |
|
876 |
2234
|
877 int len = list.length (); |
|
878 |
|
879 for (int i = 0; i < len; i++) |
|
880 octave_stdout << list[i] << "\n"; |
|
881 } |
2299
|
882 |
|
883 octave_completion_matches_called = true; |
2234
|
884 } |
|
885 } |
|
886 else |
|
887 print_usage ("completion_matches"); |
|
888 |
|
889 return retval; |
|
890 } |
|
891 |
3189
|
892 DEFUN (read_readline_init_file, args, , |
3448
|
893 "-*- texinfo -*-\n\ |
|
894 @deftypefn {Built-in Function} {} read_readline_init_file (@var{file})\n\ |
|
895 Read the readline library initialiazation file @var{file}. If\n\ |
|
896 @var{file} is omitted, read the default initialization file (normally\n\ |
|
897 @file{~/.inputrc}.\n\ |
|
898 @end deftypefn") |
3189
|
899 { |
|
900 octave_value_list retval; |
|
901 |
|
902 int nargin = args.length (); |
|
903 |
|
904 if (nargin == 0) |
|
905 command_editor::read_init_file (); |
|
906 else if (nargin == 1) |
|
907 { |
3523
|
908 std::string file = file_ops::tilde_expand (args(0).string_value ()); |
3189
|
909 |
|
910 if (! error_state) |
|
911 command_editor::read_init_file (file); |
|
912 } |
|
913 else |
|
914 print_usage ("read_readline_init_file"); |
|
915 |
|
916 return retval; |
|
917 } |
|
918 |
3523
|
919 static std::string hook_fcn; |
3498
|
920 static octave_value user_data; |
|
921 |
3519
|
922 static void |
|
923 input_event_hook (void) |
3498
|
924 { |
|
925 if (user_data.is_defined ()) |
|
926 feval (hook_fcn, user_data, 0); |
|
927 else |
|
928 feval (hook_fcn, octave_value_list (), 0); |
|
929 } |
|
930 |
|
931 DEFUN (input_event_hook, args, , |
|
932 "-*- texinfo -*-\n\ |
|
933 @deftypefn {Built-in Function} {[@var{ofcn}, @var{odata}] =} input_event_hook (@var{fcn}, @var{data})\n\ |
|
934 Given the name of a function as a string and any Octave value object,\n\ |
|
935 install @var{fcn} as a function to call periodically, when Octave is\n\ |
|
936 waiting for input. The function should have the form\n\ |
|
937 @example\n\ |
|
938 @var{fcn} (@var{data})\n\ |
|
939 @end example\n\ |
|
940 \n\ |
|
941 If @var{data} is omitted, Octave calls the function without any\n\ |
|
942 arguments. If both @var{fcn} and @var{data} are omitted, Octave\n\ |
|
943 clears the hook. In all cases, the name of the previous hook function\n\ |
|
944 and the user data are returned.\n\ |
|
945 @end deftypefn") |
|
946 { |
|
947 octave_value_list retval; |
|
948 |
|
949 int nargin = args.length (); |
|
950 |
|
951 if (nargin > 2) |
|
952 print_usage ("input_event_hook"); |
|
953 else |
|
954 { |
|
955 octave_value tmp_user_data; |
|
956 |
3523
|
957 std::string tmp_hook_fcn; |
3498
|
958 |
|
959 if (nargin > 1) |
|
960 tmp_user_data = args(1); |
|
961 |
|
962 if (nargin > 0) |
|
963 { |
|
964 tmp_hook_fcn = args(0).string_value (); |
|
965 |
|
966 if (error_state) |
|
967 { |
|
968 error ("input_event_hook: expecting string as first arg"); |
|
969 return retval; |
|
970 } |
|
971 |
|
972 command_editor::set_event_hook (input_event_hook); |
|
973 } |
|
974 |
|
975 if (nargin == 0) |
|
976 command_editor::set_event_hook (0); |
|
977 |
|
978 retval(1) = user_data; |
|
979 retval(0) = hook_fcn; |
|
980 |
|
981 hook_fcn = tmp_hook_fcn; |
|
982 user_data = tmp_user_data; |
|
983 } |
|
984 |
|
985 return retval; |
|
986 } |
|
987 |
2181
|
988 static int |
|
989 ps1 (void) |
|
990 { |
|
991 int status = 0; |
|
992 |
|
993 Vps1 = builtin_string_variable ("PS1"); |
|
994 |
|
995 return status; |
|
996 } |
|
997 |
|
998 static int |
|
999 ps2 (void) |
|
1000 { |
|
1001 int status = 0; |
|
1002 |
|
1003 Vps2 = builtin_string_variable ("PS2"); |
|
1004 |
|
1005 return status; |
|
1006 } |
|
1007 |
|
1008 static int |
|
1009 ps4 (void) |
|
1010 { |
|
1011 int status = 0; |
|
1012 |
|
1013 Vps4 = builtin_string_variable ("PS4"); |
|
1014 |
|
1015 return status; |
|
1016 } |
|
1017 |
|
1018 static int |
|
1019 completion_append_char (void) |
|
1020 { |
|
1021 int status = 0; |
|
1022 |
3523
|
1023 std::string s = builtin_string_variable ("completion_append_char"); |
2181
|
1024 |
|
1025 switch (s.length ()) |
|
1026 { |
|
1027 case 1: |
|
1028 Vcompletion_append_char = s[0]; |
|
1029 break; |
|
1030 |
|
1031 case 0: |
|
1032 Vcompletion_append_char = '\0'; |
|
1033 break; |
|
1034 |
|
1035 default: |
|
1036 warning ("completion_append_char must be a single character"); |
|
1037 status = -1; |
|
1038 break; |
|
1039 } |
|
1040 |
|
1041 return status; |
|
1042 } |
|
1043 |
3019
|
1044 static int |
|
1045 echo_executing_commands (void) |
|
1046 { |
|
1047 Vecho_executing_commands = check_preference ("echo_executing_commands"); |
|
1048 |
|
1049 return 0; |
|
1050 } |
|
1051 |
2181
|
1052 void |
|
1053 symbols_of_input (void) |
|
1054 { |
3258
|
1055 DEFVAR (PS1, "\\s:\\#> ", ps1, |
3332
|
1056 "-*- texinfo -*-\n\ |
|
1057 @defvr {Built-in Variable} PS1\n\ |
|
1058 The primary prompt string. When executing interactively, Octave\n\ |
|
1059 displays the primary prompt @code{PS1} when it is ready to read a\n\ |
|
1060 command.\n\ |
|
1061 \n\ |
|
1062 The default value of @code{PS1} is @code{\"\\s:\\#> \"}. To change it, use a\n\ |
|
1063 command like\n\ |
|
1064 \n\ |
|
1065 @example\n\ |
|
1066 octave:13> PS1 = \"\\\\u@@\\\\H> \"\n\ |
|
1067 @end example\n\ |
|
1068 \n\ |
|
1069 @noindent\n\ |
|
1070 which will result in the prompt @samp{boris@@kremvax> } for the user\n\ |
|
1071 @samp{boris} logged in on the host @samp{kremvax.kgb.su}. Note that two\n\ |
|
1072 backslashes are required to enter a backslash into a string.\n\ |
|
1073 @xref{Strings}.\n\ |
3333
|
1074 @end defvr"); |
2181
|
1075 |
3258
|
1076 DEFVAR (PS2, "> ", ps2, |
3332
|
1077 "-*- texinfo -*-\n\ |
|
1078 @defvr {Built-in Variable} PS2\n\ |
|
1079 The secondary prompt string, which is printed when Octave is\n\ |
|
1080 expecting additional input to complete a command. For example, when\n\ |
|
1081 defining a function over several lines, Octave will print the value of\n\ |
|
1082 @code{PS1} at the beginning of each line after the first. The default\n\ |
|
1083 value of @code{PS2} is @code{\"> \"}.\n\ |
3333
|
1084 @end defvr"); |
2181
|
1085 |
3258
|
1086 DEFVAR (PS4, "+ ", ps4, |
3332
|
1087 "-*- texinfo -*-\n\ |
|
1088 @defvr {Built-in Variable} PS4\n\ |
|
1089 If Octave is invoked with the @code{--echo-input} option, the value of\n\ |
|
1090 @code{PS4} is printed before each line of input that is echoed. The\n\ |
3333
|
1091 default value of @code{PS4} is @code{\"+ \"}. @xref{Invoking Octave}, for\n\ |
3332
|
1092 a description of @code{--echo-input}.\n\ |
3333
|
1093 @end defvr"); |
2181
|
1094 |
3258
|
1095 DEFVAR (completion_append_char, " ", completion_append_char, |
3332
|
1096 "-*- texinfo -*-\n\ |
|
1097 @defvr {Built-in Variable} completion_append_char\n\ |
|
1098 The value of @code{completion_append_char} is used as the character to\n\ |
|
1099 append to successful command-line completion attempts. The default\n\ |
|
1100 value is @code{\" \"} (a single space).\n\ |
3333
|
1101 @end defvr"); |
3019
|
1102 |
3258
|
1103 DEFVAR (echo_executing_commands, static_cast<double> (ECHO_OFF), |
3019
|
1104 echo_executing_commands, |
3332
|
1105 "-*- texinfo -*-\n\ |
|
1106 @defvr {Built-in Variable} echo_executing_commands\n\ |
|
1107 This variable may also be used to control the echo state. It may be\n\ |
|
1108 the sum of the following values:\n\ |
|
1109 \n\ |
|
1110 @table @asis\n\ |
|
1111 @item 1\n\ |
|
1112 Echo commands read from script files.\n\ |
|
1113 \n\ |
|
1114 @item 2\n\ |
|
1115 Echo commands from functions.\n\ |
|
1116 \n\ |
|
1117 @item 4\n\ |
|
1118 Echo commands read from command line.\n\ |
|
1119 @end table\n\ |
|
1120 \n\ |
|
1121 More than one state can be active at once. For example, a value of 3 is\n\ |
|
1122 equivalent to the command @kbd{echo on all}.\n\ |
|
1123 \n\ |
|
1124 The value of @code{echo_executing_commands} is set by the @kbd{echo}\n\ |
|
1125 command and the command line option @code{--echo-input}.\n\ |
3333
|
1126 @end defvr"); |
2181
|
1127 } |
|
1128 |
1
|
1129 /* |
|
1130 ;;; Local Variables: *** |
|
1131 ;;; mode: C++ *** |
|
1132 ;;; End: *** |
|
1133 */ |