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