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