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