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