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 <ctime> |
|
30 #include <cstdio> |
|
31 #include <cstdlib> |
|
32 #include <cstring> |
|
33 #include <cassert> |
|
34 #include <csignal> |
|
35 |
1728
|
36 #include <string> |
|
37 |
1349
|
38 #include <iostream.h> |
|
39 |
1350
|
40 #ifdef HAVE_UNISTD_H |
2442
|
41 #ifdef HAVE_SYS_TYPES_H |
529
|
42 #include <sys/types.h> |
2442
|
43 #endif |
529
|
44 #include <unistd.h> |
|
45 #endif |
1343
|
46 |
2927
|
47 #include "cmd-edit.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. |
|
90 time_t Vlast_prompt_time; |
|
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 { |
2927
|
167 retval = command_editor::readline (s); |
1898
|
168 |
2927
|
169 if (retval.empty ()) |
|
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 |
2927
|
193 static string |
1
|
194 octave_gets (void) |
|
195 { |
2927
|
196 string retval; |
1
|
197 |
3165
|
198 Vlast_prompt_time = time (0); |
|
199 |
1822
|
200 if ((interactive || forced_interactive) |
|
201 && (! (reading_fcn_file || reading_script_file))) |
1
|
202 { |
2927
|
203 string ps = (promptflag > 0) ? Vps1 : Vps2; |
1755
|
204 |
2927
|
205 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 |
2095
|
225 octave_diary << current_input_line; |
581
|
226 |
1822
|
227 do_input_echo (current_input_line); |
1
|
228 } |
581
|
229 |
2095
|
230 octave_diary << "\n"; |
581
|
231 |
1822
|
232 return retval; |
1
|
233 } |
|
234 |
581
|
235 // Read a line from the input stream. |
|
236 |
2927
|
237 static string |
1822
|
238 get_user_input (void) |
1
|
239 { |
2927
|
240 string retval; |
1
|
241 |
|
242 if (get_input_from_eval_string) |
|
243 { |
2927
|
244 retval = current_eval_string; |
1822
|
245 |
2927
|
246 size_t len = retval.length (); |
1822
|
247 |
2927
|
248 if (retval[len-1] != '\n') |
|
249 retval.append ("\n"); |
1822
|
250 } |
|
251 else |
|
252 retval = octave_gets (); |
|
253 |
2927
|
254 current_input_line = retval; |
1822
|
255 |
2114
|
256 if (! get_input_from_eval_string) |
|
257 input_line_number++; |
1822
|
258 |
|
259 return retval; |
|
260 } |
|
261 |
|
262 int |
|
263 octave_read (char *buf, unsigned max_size) |
|
264 { |
2927
|
265 // XXX FIXME XXX -- is this a safe way to buffer the input? |
|
266 |
|
267 static string input_buf; |
|
268 static const char *pos = 0; |
|
269 static size_t chars_left = 0; |
1822
|
270 |
|
271 int status = 0; |
|
272 |
2927
|
273 if (input_buf.empty ()) |
1822
|
274 { |
2927
|
275 pos = 0; |
|
276 |
|
277 input_buf = get_user_input (); |
1822
|
278 |
2927
|
279 chars_left = input_buf.length (); |
|
280 |
|
281 pos = input_buf.c_str (); |
1822
|
282 } |
|
283 |
|
284 if (chars_left > 0) |
|
285 { |
|
286 buf[0] = '\0'; |
|
287 |
2927
|
288 size_t len = max_size > 2 ? max_size - 2 : 0; |
1822
|
289 |
2927
|
290 assert (len > 0); |
|
291 |
|
292 strncpy (buf, pos, len); |
1822
|
293 |
|
294 if (chars_left > len) |
1
|
295 { |
1822
|
296 chars_left -= len; |
|
297 |
2927
|
298 pos += len; |
1822
|
299 |
|
300 buf[len] = '\0'; |
|
301 |
1
|
302 status = len; |
|
303 } |
|
304 else |
|
305 { |
2927
|
306 input_buf = ""; |
1755
|
307 |
1822
|
308 len = chars_left; |
|
309 |
|
310 if (buf[len-1] != '\n') |
|
311 buf[len++] = '\n'; |
1
|
312 |
1822
|
313 buf[len] = '\0'; |
1755
|
314 |
1822
|
315 status = len; |
1
|
316 } |
1044
|
317 } |
1822
|
318 else if (chars_left == 0) |
2862
|
319 { |
2927
|
320 input_buf = ""; |
2862
|
321 |
|
322 status = 0; |
|
323 } |
1822
|
324 else |
|
325 status = -1; |
1044
|
326 |
1
|
327 return status; |
|
328 } |
|
329 |
581
|
330 // Fix things up so that input can come from file `name', printing a |
|
331 // warning if the file doesn't exist. |
|
332 |
1
|
333 FILE * |
1750
|
334 get_input_from_file (const string& name, int warn) |
1
|
335 { |
529
|
336 FILE *instream = 0; |
1
|
337 |
1750
|
338 if (name.length () > 0) |
|
339 instream = fopen (name.c_str (), "r"); |
1
|
340 |
529
|
341 if (! instream && warn) |
1750
|
342 warning ("%s: no such file or directory", name.c_str ()); |
1
|
343 |
338
|
344 if (reading_fcn_file || reading_script_file) |
339
|
345 ff_instream = instream; |
1
|
346 else |
2927
|
347 command_editor::set_input_stream (instream); |
1
|
348 |
|
349 return instream; |
|
350 } |
|
351 |
581
|
352 // Fix things up so that input can come from the standard input. This |
|
353 // may need to become much more complicated, which is why it's in a |
|
354 // separate function. |
|
355 |
1
|
356 FILE * |
|
357 get_input_from_stdin (void) |
|
358 { |
2927
|
359 command_editor::set_input_stream (stdin); |
|
360 return command_editor::get_input_stream (); |
1
|
361 } |
|
362 |
2921
|
363 // XXX FIXME XXX -- make this generate file names when appropriate. |
|
364 |
2247
|
365 static string_vector |
2921
|
366 generate_possible_completions (const string& text, string& prefix, |
|
367 string& hint) |
1280
|
368 { |
2247
|
369 string_vector names; |
1280
|
370 |
2921
|
371 prefix = ""; |
1280
|
372 |
2921
|
373 if (! text.empty () && text != "." && text.rfind ('.') != NPOS) |
1430
|
374 names = generate_struct_completions (text, prefix, hint); |
|
375 else |
2247
|
376 names = make_name_list (); |
1280
|
377 |
2944
|
378 // Sort and remove duplicates. |
2348
|
379 |
2944
|
380 names.qsort (true); |
2348
|
381 |
1280
|
382 return names; |
|
383 } |
|
384 |
2944
|
385 static string |
|
386 generate_completion (const string& text, int state) |
1280
|
387 { |
2944
|
388 string retval; |
|
389 |
2921
|
390 static string prefix; |
|
391 static string hint; |
1280
|
392 |
2921
|
393 static size_t prefix_len = 0; |
|
394 static size_t hint_len = 0; |
1280
|
395 |
1
|
396 static int list_index = 0; |
2921
|
397 static int name_list_len = 0; |
2247
|
398 static string_vector name_list; |
1
|
399 |
1280
|
400 static int matches = 0; |
1
|
401 |
|
402 if (state == 0) |
|
403 { |
|
404 list_index = 0; |
|
405 |
2921
|
406 prefix = ""; |
1280
|
407 |
2921
|
408 hint = text; |
1280
|
409 |
|
410 name_list = generate_possible_completions (text, prefix, hint); |
|
411 |
2921
|
412 name_list_len = name_list.length (); |
|
413 |
|
414 prefix_len = prefix.length (); |
1280
|
415 |
2921
|
416 hint_len = hint.length (); |
1280
|
417 |
|
418 matches = 0; |
2247
|
419 |
2921
|
420 for (int i = 0; i < name_list_len; i++) |
|
421 if (! name_list[i].compare (hint, 0, hint_len)) |
2247
|
422 matches++; |
1
|
423 } |
|
424 |
2921
|
425 if (name_list_len > 0 && matches > 0) |
1
|
426 { |
2921
|
427 while (list_index < name_list_len) |
244
|
428 { |
2921
|
429 string name = name_list[list_index]; |
2247
|
430 |
1280
|
431 list_index++; |
2247
|
432 |
2921
|
433 if (! name.compare (hint, 0, hint_len)) |
1280
|
434 { |
2944
|
435 if (! prefix.empty ()) |
|
436 retval = prefix + "." + name; |
|
437 else |
|
438 retval = name; |
1280
|
439 |
2944
|
440 if (matches == 1 && looks_like_struct (retval)) |
2927
|
441 command_editor::set_completion_append_character ('.'); |
1280
|
442 else |
2927
|
443 command_editor::set_completion_append_character |
|
444 (Vcompletion_append_char); |
1280
|
445 |
2944
|
446 break; |
1280
|
447 } |
244
|
448 } |
1
|
449 } |
|
450 |
2944
|
451 return retval; |
1
|
452 } |
|
453 |
2927
|
454 void |
|
455 initialize_command_input (void) |
|
456 { |
|
457 // If we are using readline, this allows conditional parsing of the |
|
458 // .inputrc file. |
269
|
459 |
2927
|
460 command_editor::set_name ("Octave"); |
1358
|
461 |
3004
|
462 command_editor::set_basic_quote_characters ("\""); |
2944
|
463 |
|
464 command_editor::set_completion_function (generate_completion); |
269
|
465 } |
|
466 |
2927
|
467 static bool |
|
468 match_sans_spaces (const string& standard, const string& test) |
269
|
469 { |
2927
|
470 string tmp = test; |
269
|
471 |
2927
|
472 size_t beg = test.find_first_not_of (" \t"); |
|
473 size_t end = test.find_last_not_of (" \t"); |
|
474 size_t len = beg - end + 1; |
1
|
475 |
2927
|
476 return test.compare (standard, beg, len) == 0; |
529
|
477 } |
|
478 |
581
|
479 // If the user simply hits return, this will produce an empty matrix. |
|
480 |
2086
|
481 static octave_value_list |
3100
|
482 get_user_input (const octave_value_list& args, bool debug, int nargout) |
529
|
483 { |
3100
|
484 octave_value_list retval; |
529
|
485 |
|
486 int nargin = args.length (); |
|
487 |
|
488 int read_as_string = 0; |
|
489 |
712
|
490 if (nargin == 2) |
529
|
491 read_as_string++; |
|
492 |
1761
|
493 string prompt ("debug> "); |
|
494 |
712
|
495 if (nargin > 0) |
529
|
496 { |
1761
|
497 prompt = args(0).string_value (); |
636
|
498 |
|
499 if (error_state) |
|
500 { |
|
501 error ("input: unrecognized argument"); |
|
502 return retval; |
|
503 } |
529
|
504 } |
|
505 |
|
506 again: |
|
507 |
2095
|
508 flush_octave_stdout (); |
529
|
509 |
2927
|
510 string input_buf = gnu_readline (prompt.c_str (), true); |
529
|
511 |
2927
|
512 if (! input_buf.empty ()) |
529
|
513 { |
1799
|
514 if (! input_from_startup_file) |
2927
|
515 command_history::add (input_buf); |
529
|
516 |
2927
|
517 size_t len = input_buf.length (); |
529
|
518 |
|
519 if (len < 1) |
|
520 { |
|
521 if (debug) |
|
522 goto again; |
|
523 else |
963
|
524 { |
|
525 if (read_as_string) |
|
526 return ""; |
|
527 else |
|
528 return Matrix (); |
|
529 } |
529
|
530 } |
|
531 |
|
532 if (match_sans_spaces ("exit", input_buf) |
|
533 || match_sans_spaces ("quit", input_buf) |
|
534 || match_sans_spaces ("return", input_buf)) |
963
|
535 { |
|
536 return retval; |
|
537 } |
529
|
538 else if (read_as_string) |
963
|
539 { |
3081
|
540 // XXX FIXME XXX -- fix gnu_readline and octave_gets instead! |
|
541 if (input_buf.length () == 1 && input_buf[0] == '\n') |
3100
|
542 retval(0) = ""; |
3081
|
543 else |
3100
|
544 retval(0) = input_buf; |
963
|
545 } |
529
|
546 else |
|
547 { |
|
548 int parse_status = 0; |
2900
|
549 |
3100
|
550 bool silent = ! debug; |
2900
|
551 |
3100
|
552 retval = eval_string (input_buf, silent, parse_status, nargout); |
|
553 |
|
554 if (! debug && retval.length == 0) |
|
555 retval(0) = Matrix (); |
529
|
556 } |
|
557 } |
|
558 else |
|
559 error ("input: reading user-input failed!"); |
|
560 |
|
561 if (debug) |
3098
|
562 { |
|
563 // Clear error_state so that if errors were encountered while |
|
564 // evaluating user input, extra error messages will not be |
|
565 // printed after we return. |
|
566 |
|
567 error_state = 0; |
|
568 |
3100
|
569 retval = octave_value_list (); |
3098
|
570 |
|
571 goto again; |
|
572 } |
529
|
573 |
|
574 return retval; |
|
575 } |
|
576 |
3100
|
577 DEFUN (input, args, nargout, |
529
|
578 "input (PROMPT [, S])\n\ |
|
579 \n\ |
2802
|
580 Prompt user for input. If the second argument is present, return\n\ |
529
|
581 value as a string.") |
|
582 { |
2086
|
583 octave_value_list retval; |
529
|
584 |
|
585 int nargin = args.length (); |
|
586 |
712
|
587 if (nargin == 1 || nargin == 2) |
3100
|
588 retval = get_user_input (args, false, nargout); |
529
|
589 else |
|
590 print_usage ("input"); |
|
591 |
|
592 return retval; |
|
593 } |
|
594 |
3098
|
595 static void |
|
596 restore_command_history (void *) |
|
597 { |
|
598 command_history::ignore_entries (! Vsaving_history); |
|
599 } |
|
600 |
1957
|
601 DEFUN (keyboard, args, , |
529
|
602 "keyboard (PROMPT)\n\ |
|
603 \n\ |
|
604 maybe help in debugging function files") |
|
605 { |
2086
|
606 octave_value_list retval; |
529
|
607 |
|
608 int nargin = args.length (); |
|
609 |
712
|
610 if (nargin == 0 || nargin == 1) |
3098
|
611 { |
|
612 unwind_protect::begin_frame ("keyboard"); |
|
613 |
|
614 // XXX FIXME XXX -- we shouldn't need both the |
|
615 // command_history object and the |
|
616 // Vsaving_history variable... |
|
617 command_history::ignore_entries (false); |
|
618 |
|
619 unwind_protect::add (restore_command_history, 0); |
|
620 |
|
621 unwind_protect_bool (Vsaving_history); |
|
622 |
|
623 Vsaving_history = true; |
|
624 |
3100
|
625 retval = get_user_input (args, true, 0); |
3098
|
626 |
|
627 unwind_protect::run_frame ("keyboard"); |
|
628 } |
529
|
629 else |
|
630 print_usage ("keyboard"); |
|
631 |
|
632 return retval; |
|
633 } |
|
634 |
1957
|
635 DEFUN_TEXT(echo, args, , |
1588
|
636 "echo [options]\n\ |
|
637 \n\ |
|
638 echo [on|off] -- enable or disable echoing of commands as\n\ |
|
639 they are executed in script files\n\ |
|
640 \n\ |
|
641 echo [on all|off all] -- enable or disable echoing of commands as they\n\ |
|
642 are executed in script files and functions\n\ |
|
643 \n\ |
|
644 Without any arguments, toggle the current echo state.") |
|
645 { |
2086
|
646 octave_value_list retval; |
1588
|
647 |
1755
|
648 int argc = args.length () + 1; |
|
649 |
1968
|
650 string_vector argv = args.make_argv ("echo"); |
1755
|
651 |
|
652 if (error_state) |
|
653 return retval; |
1588
|
654 |
|
655 switch (argc) |
|
656 { |
|
657 case 1: |
|
658 { |
2205
|
659 if ((Vecho_executing_commands & ECHO_SCRIPTS) |
|
660 || (Vecho_executing_commands & ECHO_FUNCTIONS)) |
2431
|
661 bind_builtin_variable ("echo_executing_commands", |
2800
|
662 static_cast<double> (ECHO_OFF)); |
1588
|
663 else |
2431
|
664 bind_builtin_variable ("echo_executing_commands", |
2800
|
665 static_cast<double> (ECHO_SCRIPTS)); |
1588
|
666 } |
|
667 break; |
|
668 |
|
669 case 2: |
|
670 { |
1755
|
671 string arg = argv[1]; |
|
672 |
|
673 if (arg == "on") |
2431
|
674 bind_builtin_variable ("echo_executing_commands", |
2800
|
675 static_cast<double> (ECHO_SCRIPTS)); |
1755
|
676 else if (arg == "off") |
2431
|
677 bind_builtin_variable ("echo_executing_commands", |
2800
|
678 static_cast<double> (ECHO_OFF)); |
1588
|
679 else |
|
680 print_usage ("echo"); |
|
681 } |
|
682 break; |
|
683 |
|
684 case 3: |
|
685 { |
1755
|
686 string arg = argv[1]; |
|
687 |
|
688 if (arg == "on" && argv[2] == "all") |
2800
|
689 { |
|
690 int tmp = (ECHO_SCRIPTS | ECHO_FUNCTIONS); |
|
691 bind_builtin_variable ("echo_executing_commands", |
|
692 static_cast<double> (tmp)); |
|
693 } |
1755
|
694 else if (arg == "off" && argv[2] == "all") |
2431
|
695 bind_builtin_variable ("echo_executing_commands", |
2800
|
696 static_cast<double> (ECHO_OFF)); |
1588
|
697 else |
|
698 print_usage ("echo"); |
|
699 } |
|
700 break; |
|
701 |
|
702 default: |
|
703 print_usage ("echo"); |
|
704 break; |
|
705 } |
|
706 |
|
707 return retval; |
|
708 } |
|
709 |
2234
|
710 DEFUN (completion_matches, args, nargout, |
2299
|
711 "completion_matches (HINT): generate possible completions given HINT\n\ |
|
712 \n\ |
|
713 This function is provided for the benefit of programs like Emacs which\n\ |
|
714 might be controlling Octave and handling user input. The current command\n\ |
|
715 number is not incremented when this function is called. This is a feature,\n\ |
|
716 not a bug.") |
2234
|
717 { |
2281
|
718 octave_value retval; |
2234
|
719 |
|
720 int nargin = args.length (); |
|
721 |
|
722 if (nargin == 1) |
|
723 { |
2944
|
724 string hint = args(0).string_value (); |
2234
|
725 |
|
726 if (! error_state) |
|
727 { |
|
728 int n = 32; |
|
729 |
|
730 string_vector list (n); |
|
731 |
|
732 int k = 0; |
|
733 |
|
734 for (;;) |
|
735 { |
2944
|
736 string cmd = generate_completion (hint, k); |
2234
|
737 |
2944
|
738 if (! cmd.empty ()) |
2234
|
739 { |
2944
|
740 if (k == n) |
2234
|
741 { |
2944
|
742 n *= 2; |
|
743 list.resize (n); |
|
744 } |
2235
|
745 |
2944
|
746 list[k++] = cmd; |
2234
|
747 } |
|
748 else |
|
749 { |
|
750 list.resize (k); |
|
751 break; |
|
752 } |
|
753 } |
|
754 |
|
755 if (nargout > 0) |
2281
|
756 { |
|
757 if (! list.empty ()) |
|
758 retval = list; |
2282
|
759 else |
|
760 retval = ""; |
2281
|
761 } |
2234
|
762 else |
|
763 { |
2235
|
764 // We don't use string_vector::list_in_columns here |
|
765 // because it will be easier for Emacs if the names |
|
766 // appear in a single column. |
|
767 |
2234
|
768 int len = list.length (); |
|
769 |
|
770 for (int i = 0; i < len; i++) |
|
771 octave_stdout << list[i] << "\n"; |
|
772 } |
2299
|
773 |
|
774 octave_completion_matches_called = true; |
2234
|
775 } |
|
776 } |
|
777 else |
|
778 print_usage ("completion_matches"); |
|
779 |
|
780 return retval; |
|
781 } |
|
782 |
2181
|
783 static int |
|
784 ps1 (void) |
|
785 { |
|
786 int status = 0; |
|
787 |
|
788 Vps1 = builtin_string_variable ("PS1"); |
|
789 |
|
790 return status; |
|
791 } |
|
792 |
|
793 static int |
|
794 ps2 (void) |
|
795 { |
|
796 int status = 0; |
|
797 |
|
798 Vps2 = builtin_string_variable ("PS2"); |
|
799 |
|
800 return status; |
|
801 } |
|
802 |
|
803 static int |
|
804 ps4 (void) |
|
805 { |
|
806 int status = 0; |
|
807 |
|
808 Vps4 = builtin_string_variable ("PS4"); |
|
809 |
|
810 return status; |
|
811 } |
|
812 |
|
813 static int |
|
814 completion_append_char (void) |
|
815 { |
|
816 int status = 0; |
|
817 |
|
818 string s = builtin_string_variable ("completion_append_char"); |
|
819 |
|
820 switch (s.length ()) |
|
821 { |
|
822 case 1: |
|
823 Vcompletion_append_char = s[0]; |
|
824 break; |
|
825 |
|
826 case 0: |
|
827 Vcompletion_append_char = '\0'; |
|
828 break; |
|
829 |
|
830 default: |
|
831 warning ("completion_append_char must be a single character"); |
|
832 status = -1; |
|
833 break; |
|
834 } |
|
835 |
|
836 return status; |
|
837 } |
|
838 |
3019
|
839 static int |
|
840 echo_executing_commands (void) |
|
841 { |
|
842 Vecho_executing_commands = check_preference ("echo_executing_commands"); |
|
843 |
|
844 return 0; |
|
845 } |
|
846 |
2181
|
847 void |
|
848 symbols_of_input (void) |
|
849 { |
|
850 DEFVAR (PS1, "\\s:\\#> ", 0, ps1, |
|
851 "primary prompt string"); |
|
852 |
|
853 DEFVAR (PS2, "> ", 0, ps2, |
|
854 "secondary prompt string"); |
|
855 |
|
856 DEFVAR (PS4, "+ ", 0, ps4, |
|
857 "string printed before echoed input (enabled by --echo-input)"); |
|
858 |
|
859 DEFVAR (completion_append_char, " ", 0, completion_append_char, |
|
860 "the string to append after successful command-line completion attempts"); |
3019
|
861 |
|
862 DEFVAR (echo_executing_commands, static_cast<double> (ECHO_OFF), 0, |
|
863 echo_executing_commands, |
|
864 "echo commands as they are executed"); |
2181
|
865 } |
|
866 |
1
|
867 /* |
|
868 ;;; Local Variables: *** |
|
869 ;;; mode: C++ *** |
|
870 ;;; End: *** |
|
871 */ |