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