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