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 |
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 |
2927
|
243 static string |
1822
|
244 get_user_input (void) |
1
|
245 { |
2927
|
246 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 |
|
273 static string input_buf; |
|
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 * |
1750
|
340 get_input_from_file (const 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 |
2921
|
372 generate_possible_completions (const string& text, string& prefix, |
|
373 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 |
2944
|
391 static string |
|
392 generate_completion (const string& text, int state) |
1280
|
393 { |
2944
|
394 string retval; |
|
395 |
2921
|
396 static string prefix; |
|
397 static 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++) |
|
427 if (! name_list[i].compare (hint, 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 { |
2921
|
435 string name = name_list[list_index]; |
2247
|
436 |
1280
|
437 list_index++; |
2247
|
438 |
2921
|
439 if (! name.compare (hint, 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 |
|
474 match_sans_spaces (const string& standard, const string& test) |
269
|
475 { |
2927
|
476 string tmp = test; |
269
|
477 |
2927
|
478 size_t beg = test.find_first_not_of (" \t"); |
|
479 size_t end = test.find_last_not_of (" \t"); |
|
480 size_t len = beg - end + 1; |
1
|
481 |
2927
|
482 return test.compare (standard, beg, len) == 0; |
529
|
483 } |
|
484 |
581
|
485 // If the user simply hits return, this will produce an empty matrix. |
|
486 |
2086
|
487 static octave_value_list |
3100
|
488 get_user_input (const octave_value_list& args, bool debug, int nargout) |
529
|
489 { |
3100
|
490 octave_value_list retval; |
529
|
491 |
|
492 int nargin = args.length (); |
|
493 |
|
494 int read_as_string = 0; |
|
495 |
712
|
496 if (nargin == 2) |
529
|
497 read_as_string++; |
|
498 |
1761
|
499 string prompt ("debug> "); |
|
500 |
712
|
501 if (nargin > 0) |
529
|
502 { |
1761
|
503 prompt = args(0).string_value (); |
636
|
504 |
|
505 if (error_state) |
|
506 { |
|
507 error ("input: unrecognized argument"); |
|
508 return retval; |
|
509 } |
529
|
510 } |
|
511 |
|
512 again: |
|
513 |
2095
|
514 flush_octave_stdout (); |
529
|
515 |
2927
|
516 string input_buf = gnu_readline (prompt.c_str (), true); |
529
|
517 |
2927
|
518 if (! input_buf.empty ()) |
529
|
519 { |
1799
|
520 if (! input_from_startup_file) |
2927
|
521 command_history::add (input_buf); |
529
|
522 |
2927
|
523 size_t len = input_buf.length (); |
529
|
524 |
|
525 if (len < 1) |
|
526 { |
|
527 if (debug) |
|
528 goto again; |
|
529 else |
963
|
530 { |
|
531 if (read_as_string) |
|
532 return ""; |
|
533 else |
|
534 return Matrix (); |
|
535 } |
529
|
536 } |
|
537 |
|
538 if (match_sans_spaces ("exit", input_buf) |
|
539 || match_sans_spaces ("quit", input_buf) |
|
540 || match_sans_spaces ("return", input_buf)) |
963
|
541 { |
|
542 return retval; |
|
543 } |
529
|
544 else if (read_as_string) |
963
|
545 { |
3081
|
546 // XXX FIXME XXX -- fix gnu_readline and octave_gets instead! |
|
547 if (input_buf.length () == 1 && input_buf[0] == '\n') |
3100
|
548 retval(0) = ""; |
3081
|
549 else |
3100
|
550 retval(0) = input_buf; |
963
|
551 } |
529
|
552 else |
|
553 { |
|
554 int parse_status = 0; |
2900
|
555 |
3100
|
556 bool silent = ! debug; |
2900
|
557 |
3100
|
558 retval = eval_string (input_buf, silent, parse_status, nargout); |
|
559 |
|
560 if (! debug && retval.length == 0) |
|
561 retval(0) = Matrix (); |
529
|
562 } |
|
563 } |
|
564 else |
|
565 error ("input: reading user-input failed!"); |
|
566 |
|
567 if (debug) |
3098
|
568 { |
|
569 // Clear error_state so that if errors were encountered while |
|
570 // evaluating user input, extra error messages will not be |
|
571 // printed after we return. |
|
572 |
|
573 error_state = 0; |
|
574 |
3100
|
575 retval = octave_value_list (); |
3098
|
576 |
|
577 goto again; |
|
578 } |
529
|
579 |
|
580 return retval; |
|
581 } |
|
582 |
3100
|
583 DEFUN (input, args, nargout, |
529
|
584 "input (PROMPT [, S])\n\ |
|
585 \n\ |
2802
|
586 Prompt user for input. If the second argument is present, return\n\ |
529
|
587 value as a string.") |
|
588 { |
2086
|
589 octave_value_list retval; |
529
|
590 |
|
591 int nargin = args.length (); |
|
592 |
712
|
593 if (nargin == 1 || nargin == 2) |
3100
|
594 retval = get_user_input (args, false, nargout); |
529
|
595 else |
|
596 print_usage ("input"); |
|
597 |
|
598 return retval; |
|
599 } |
|
600 |
3098
|
601 static void |
|
602 restore_command_history (void *) |
|
603 { |
|
604 command_history::ignore_entries (! Vsaving_history); |
|
605 } |
|
606 |
1957
|
607 DEFUN (keyboard, args, , |
529
|
608 "keyboard (PROMPT)\n\ |
|
609 \n\ |
|
610 maybe help in debugging function files") |
|
611 { |
2086
|
612 octave_value_list retval; |
529
|
613 |
|
614 int nargin = args.length (); |
|
615 |
712
|
616 if (nargin == 0 || nargin == 1) |
3098
|
617 { |
|
618 unwind_protect::begin_frame ("keyboard"); |
|
619 |
|
620 // XXX FIXME XXX -- we shouldn't need both the |
|
621 // command_history object and the |
|
622 // Vsaving_history variable... |
|
623 command_history::ignore_entries (false); |
|
624 |
|
625 unwind_protect::add (restore_command_history, 0); |
|
626 |
|
627 unwind_protect_bool (Vsaving_history); |
|
628 |
|
629 Vsaving_history = true; |
|
630 |
3100
|
631 retval = get_user_input (args, true, 0); |
3098
|
632 |
|
633 unwind_protect::run_frame ("keyboard"); |
|
634 } |
529
|
635 else |
|
636 print_usage ("keyboard"); |
|
637 |
|
638 return retval; |
|
639 } |
|
640 |
1957
|
641 DEFUN_TEXT(echo, args, , |
1588
|
642 "echo [options]\n\ |
|
643 \n\ |
|
644 echo [on|off] -- enable or disable echoing of commands as\n\ |
|
645 they are executed in script files\n\ |
|
646 \n\ |
|
647 echo [on all|off all] -- enable or disable echoing of commands as they\n\ |
|
648 are executed in script files and functions\n\ |
|
649 \n\ |
|
650 Without any arguments, toggle the current echo state.") |
|
651 { |
2086
|
652 octave_value_list retval; |
1588
|
653 |
1755
|
654 int argc = args.length () + 1; |
|
655 |
1968
|
656 string_vector argv = args.make_argv ("echo"); |
1755
|
657 |
|
658 if (error_state) |
|
659 return retval; |
1588
|
660 |
|
661 switch (argc) |
|
662 { |
|
663 case 1: |
|
664 { |
2205
|
665 if ((Vecho_executing_commands & ECHO_SCRIPTS) |
|
666 || (Vecho_executing_commands & ECHO_FUNCTIONS)) |
2431
|
667 bind_builtin_variable ("echo_executing_commands", |
2800
|
668 static_cast<double> (ECHO_OFF)); |
1588
|
669 else |
2431
|
670 bind_builtin_variable ("echo_executing_commands", |
2800
|
671 static_cast<double> (ECHO_SCRIPTS)); |
1588
|
672 } |
|
673 break; |
|
674 |
|
675 case 2: |
|
676 { |
1755
|
677 string arg = argv[1]; |
|
678 |
|
679 if (arg == "on") |
2431
|
680 bind_builtin_variable ("echo_executing_commands", |
2800
|
681 static_cast<double> (ECHO_SCRIPTS)); |
1755
|
682 else if (arg == "off") |
2431
|
683 bind_builtin_variable ("echo_executing_commands", |
2800
|
684 static_cast<double> (ECHO_OFF)); |
1588
|
685 else |
|
686 print_usage ("echo"); |
|
687 } |
|
688 break; |
|
689 |
|
690 case 3: |
|
691 { |
1755
|
692 string arg = argv[1]; |
|
693 |
|
694 if (arg == "on" && argv[2] == "all") |
2800
|
695 { |
|
696 int tmp = (ECHO_SCRIPTS | ECHO_FUNCTIONS); |
|
697 bind_builtin_variable ("echo_executing_commands", |
|
698 static_cast<double> (tmp)); |
|
699 } |
1755
|
700 else if (arg == "off" && argv[2] == "all") |
2431
|
701 bind_builtin_variable ("echo_executing_commands", |
2800
|
702 static_cast<double> (ECHO_OFF)); |
1588
|
703 else |
|
704 print_usage ("echo"); |
|
705 } |
|
706 break; |
|
707 |
|
708 default: |
|
709 print_usage ("echo"); |
|
710 break; |
|
711 } |
|
712 |
|
713 return retval; |
|
714 } |
|
715 |
2234
|
716 DEFUN (completion_matches, args, nargout, |
2299
|
717 "completion_matches (HINT): generate possible completions given HINT\n\ |
|
718 \n\ |
|
719 This function is provided for the benefit of programs like Emacs which\n\ |
|
720 might be controlling Octave and handling user input. The current command\n\ |
|
721 number is not incremented when this function is called. This is a feature,\n\ |
|
722 not a bug.") |
2234
|
723 { |
2281
|
724 octave_value retval; |
2234
|
725 |
|
726 int nargin = args.length (); |
|
727 |
|
728 if (nargin == 1) |
|
729 { |
2944
|
730 string hint = args(0).string_value (); |
2234
|
731 |
|
732 if (! error_state) |
|
733 { |
|
734 int n = 32; |
|
735 |
|
736 string_vector list (n); |
|
737 |
|
738 int k = 0; |
|
739 |
|
740 for (;;) |
|
741 { |
2944
|
742 string cmd = generate_completion (hint, k); |
2234
|
743 |
2944
|
744 if (! cmd.empty ()) |
2234
|
745 { |
2944
|
746 if (k == n) |
2234
|
747 { |
2944
|
748 n *= 2; |
|
749 list.resize (n); |
|
750 } |
2235
|
751 |
2944
|
752 list[k++] = cmd; |
2234
|
753 } |
|
754 else |
|
755 { |
|
756 list.resize (k); |
|
757 break; |
|
758 } |
|
759 } |
|
760 |
|
761 if (nargout > 0) |
2281
|
762 { |
|
763 if (! list.empty ()) |
|
764 retval = list; |
2282
|
765 else |
|
766 retval = ""; |
2281
|
767 } |
2234
|
768 else |
|
769 { |
2235
|
770 // We don't use string_vector::list_in_columns here |
|
771 // because it will be easier for Emacs if the names |
|
772 // appear in a single column. |
|
773 |
2234
|
774 int len = list.length (); |
|
775 |
|
776 for (int i = 0; i < len; i++) |
|
777 octave_stdout << list[i] << "\n"; |
|
778 } |
2299
|
779 |
|
780 octave_completion_matches_called = true; |
2234
|
781 } |
|
782 } |
|
783 else |
|
784 print_usage ("completion_matches"); |
|
785 |
|
786 return retval; |
|
787 } |
|
788 |
2181
|
789 static int |
|
790 ps1 (void) |
|
791 { |
|
792 int status = 0; |
|
793 |
|
794 Vps1 = builtin_string_variable ("PS1"); |
|
795 |
|
796 return status; |
|
797 } |
|
798 |
|
799 static int |
|
800 ps2 (void) |
|
801 { |
|
802 int status = 0; |
|
803 |
|
804 Vps2 = builtin_string_variable ("PS2"); |
|
805 |
|
806 return status; |
|
807 } |
|
808 |
|
809 static int |
|
810 ps4 (void) |
|
811 { |
|
812 int status = 0; |
|
813 |
|
814 Vps4 = builtin_string_variable ("PS4"); |
|
815 |
|
816 return status; |
|
817 } |
|
818 |
|
819 static int |
|
820 completion_append_char (void) |
|
821 { |
|
822 int status = 0; |
|
823 |
|
824 string s = builtin_string_variable ("completion_append_char"); |
|
825 |
|
826 switch (s.length ()) |
|
827 { |
|
828 case 1: |
|
829 Vcompletion_append_char = s[0]; |
|
830 break; |
|
831 |
|
832 case 0: |
|
833 Vcompletion_append_char = '\0'; |
|
834 break; |
|
835 |
|
836 default: |
|
837 warning ("completion_append_char must be a single character"); |
|
838 status = -1; |
|
839 break; |
|
840 } |
|
841 |
|
842 return status; |
|
843 } |
|
844 |
3019
|
845 static int |
|
846 echo_executing_commands (void) |
|
847 { |
|
848 Vecho_executing_commands = check_preference ("echo_executing_commands"); |
|
849 |
|
850 return 0; |
|
851 } |
|
852 |
2181
|
853 void |
|
854 symbols_of_input (void) |
|
855 { |
|
856 DEFVAR (PS1, "\\s:\\#> ", 0, ps1, |
|
857 "primary prompt string"); |
|
858 |
|
859 DEFVAR (PS2, "> ", 0, ps2, |
|
860 "secondary prompt string"); |
|
861 |
|
862 DEFVAR (PS4, "+ ", 0, ps4, |
|
863 "string printed before echoed input (enabled by --echo-input)"); |
|
864 |
|
865 DEFVAR (completion_append_char, " ", 0, completion_append_char, |
|
866 "the string to append after successful command-line completion attempts"); |
3019
|
867 |
|
868 DEFVAR (echo_executing_commands, static_cast<double> (ECHO_OFF), 0, |
|
869 echo_executing_commands, |
|
870 "echo commands as they are executed"); |
2181
|
871 } |
|
872 |
1
|
873 /* |
|
874 ;;; Local Variables: *** |
|
875 ;;; mode: C++ *** |
|
876 ;;; End: *** |
|
877 */ |