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