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