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