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