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