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