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