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" |
1352
|
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" |
1352
|
67 #include "utils.h" |
|
68 #include "variables.h" |
529
|
69 |
2181
|
70 // Primary prompt string. |
|
71 static string Vps1; |
|
72 |
|
73 // Secondary prompt string. |
|
74 static string Vps2; |
|
75 |
|
76 // String printed before echoed input (enabled by --echo-input). |
|
77 string Vps4; |
|
78 |
|
79 // Character to append after successful command-line completion attempts. |
|
80 static char Vcompletion_append_char; |
|
81 |
1
|
82 // Global pointer for eval(). |
1755
|
83 string current_eval_string; |
1
|
84 |
|
85 // Nonzero means get input from current_eval_string. |
|
86 int get_input_from_eval_string = 0; |
|
87 |
338
|
88 // Nonzero means we're parsing a function file. |
|
89 int reading_fcn_file = 0; |
1
|
90 |
338
|
91 // Simple name of function file we are reading. |
1755
|
92 string curr_fcn_file_name; |
1606
|
93 |
|
94 // Full name of file we are reading. |
1755
|
95 string curr_fcn_file_full_name; |
1
|
96 |
|
97 // Nonzero means we're parsing a script file. |
|
98 int reading_script_file = 0; |
|
99 |
|
100 // If we are reading from an M-file, this is it. |
529
|
101 FILE *ff_instream = 0; |
1
|
102 |
|
103 // Nonzero means this is an interactive shell. |
|
104 int interactive = 0; |
|
105 |
|
106 // Nonzero means the user forced this shell to be interactive (-i). |
|
107 int forced_interactive = 0; |
|
108 |
|
109 // Should we issue a prompt? |
|
110 int promptflag = 1; |
|
111 |
|
112 // The current line of input, from wherever. |
1755
|
113 string current_input_line; |
1
|
114 |
2299
|
115 // TRUE after a call to completion_matches(). |
|
116 bool octave_completion_matches_called = false; |
|
117 |
1044
|
118 static void |
1755
|
119 do_input_echo (const string& input_string) |
1044
|
120 { |
1588
|
121 int do_echo = reading_script_file ? |
2205
|
122 (Vecho_executing_commands & ECHO_SCRIPTS) |
2618
|
123 : (Vecho_executing_commands & ECHO_CMD_LINE) && ! forced_interactive; |
1588
|
124 |
|
125 if (do_echo) |
1044
|
126 { |
1403
|
127 if (forced_interactive) |
|
128 { |
1755
|
129 if (promptflag > 0) |
2927
|
130 octave_stdout << command_editor::decode_prompt_string (Vps1); |
1755
|
131 else |
2927
|
132 octave_stdout << command_editor::decode_prompt_string (Vps2); |
1403
|
133 } |
|
134 else |
2927
|
135 octave_stdout << command_editor::decode_prompt_string (Vps4); |
1044
|
136 |
1755
|
137 if (! input_string.empty ()) |
1044
|
138 { |
2095
|
139 octave_stdout << input_string; |
1755
|
140 |
|
141 if (input_string[input_string.length () - 1] != '\n') |
2095
|
142 octave_stdout << "\n"; |
1044
|
143 } |
|
144 } |
|
145 } |
|
146 |
2927
|
147 string |
|
148 gnu_readline (const string& s, bool force_readline) |
1822
|
149 { |
2927
|
150 string retval; |
1822
|
151 |
2927
|
152 if (line_editing || force_readline) |
1822
|
153 { |
2927
|
154 retval = command_editor::readline (s); |
1898
|
155 |
2927
|
156 if (retval.empty ()) |
|
157 retval = "\n"; |
1822
|
158 } |
|
159 else |
|
160 { |
2927
|
161 if (! s.empty () && (interactive || forced_interactive)) |
2618
|
162 { |
2927
|
163 FILE *stream = command_editor::get_output_stream (); |
|
164 |
|
165 fprintf (stream, s.c_str ()); |
|
166 fflush (stream); |
2618
|
167 } |
1822
|
168 |
2927
|
169 FILE *curr_stream = command_editor::get_input_stream (); |
|
170 |
1822
|
171 if (reading_fcn_file || reading_script_file) |
|
172 curr_stream = ff_instream; |
|
173 |
2927
|
174 retval = octave_fgets (curr_stream); |
1822
|
175 } |
|
176 |
|
177 return retval; |
|
178 } |
581
|
179 |
2927
|
180 static string |
1
|
181 octave_gets (void) |
|
182 { |
2927
|
183 string retval; |
1
|
184 |
1822
|
185 if ((interactive || forced_interactive) |
|
186 && (! (reading_fcn_file || reading_script_file))) |
1
|
187 { |
2927
|
188 string ps = (promptflag > 0) ? Vps1 : Vps2; |
1755
|
189 |
2927
|
190 string prompt = command_editor::decode_prompt_string (ps); |
1
|
191 |
2618
|
192 pipe_handler_error_count = 0; |
|
193 |
|
194 flush_octave_stdout (); |
1
|
195 |
2095
|
196 octave_diary << prompt; |
581
|
197 |
2927
|
198 retval = gnu_readline (prompt); |
1
|
199 } |
|
200 else |
1822
|
201 retval = gnu_readline (""); |
1
|
202 |
2927
|
203 current_input_line = retval; |
1
|
204 |
1822
|
205 if (! current_input_line.empty ()) |
1
|
206 { |
1799
|
207 if (! input_from_startup_file) |
2927
|
208 command_history::add (current_input_line); |
1
|
209 |
2095
|
210 octave_diary << current_input_line; |
581
|
211 |
1822
|
212 do_input_echo (current_input_line); |
1
|
213 } |
581
|
214 |
2095
|
215 octave_diary << "\n"; |
581
|
216 |
1822
|
217 return retval; |
1
|
218 } |
|
219 |
581
|
220 // Read a line from the input stream. |
|
221 |
2927
|
222 static string |
1822
|
223 get_user_input (void) |
1
|
224 { |
2927
|
225 string retval; |
1
|
226 |
|
227 if (get_input_from_eval_string) |
|
228 { |
2927
|
229 retval = current_eval_string; |
1822
|
230 |
2927
|
231 size_t len = retval.length (); |
1822
|
232 |
2927
|
233 if (retval[len-1] != '\n') |
|
234 retval.append ("\n"); |
1822
|
235 } |
|
236 else |
|
237 retval = octave_gets (); |
|
238 |
2927
|
239 current_input_line = retval; |
1822
|
240 |
2114
|
241 if (! get_input_from_eval_string) |
|
242 input_line_number++; |
1822
|
243 |
|
244 return retval; |
|
245 } |
|
246 |
|
247 int |
|
248 octave_read (char *buf, unsigned max_size) |
|
249 { |
2927
|
250 // XXX FIXME XXX -- is this a safe way to buffer the input? |
|
251 |
|
252 static string input_buf; |
|
253 static const char *pos = 0; |
|
254 static size_t chars_left = 0; |
1822
|
255 |
|
256 int status = 0; |
|
257 |
2927
|
258 if (input_buf.empty ()) |
1822
|
259 { |
2927
|
260 pos = 0; |
|
261 |
|
262 input_buf = get_user_input (); |
1822
|
263 |
2927
|
264 chars_left = input_buf.length (); |
|
265 |
|
266 pos = input_buf.c_str (); |
1822
|
267 } |
|
268 |
|
269 if (chars_left > 0) |
|
270 { |
|
271 buf[0] = '\0'; |
|
272 |
2927
|
273 size_t len = max_size > 2 ? max_size - 2 : 0; |
1822
|
274 |
2927
|
275 assert (len > 0); |
|
276 |
|
277 strncpy (buf, pos, len); |
1822
|
278 |
|
279 if (chars_left > len) |
1
|
280 { |
1822
|
281 chars_left -= len; |
|
282 |
2927
|
283 pos += len; |
1822
|
284 |
|
285 buf[len] = '\0'; |
|
286 |
1
|
287 status = len; |
|
288 } |
|
289 else |
|
290 { |
2927
|
291 input_buf = ""; |
1755
|
292 |
1822
|
293 len = chars_left; |
|
294 |
|
295 if (buf[len-1] != '\n') |
|
296 buf[len++] = '\n'; |
1
|
297 |
1822
|
298 buf[len] = '\0'; |
1755
|
299 |
1822
|
300 status = len; |
1
|
301 } |
1044
|
302 } |
1822
|
303 else if (chars_left == 0) |
2862
|
304 { |
2927
|
305 input_buf = ""; |
2862
|
306 |
|
307 status = 0; |
|
308 } |
1822
|
309 else |
|
310 status = -1; |
1044
|
311 |
1
|
312 return status; |
|
313 } |
|
314 |
581
|
315 // Fix things up so that input can come from file `name', printing a |
|
316 // warning if the file doesn't exist. |
|
317 |
1
|
318 FILE * |
1750
|
319 get_input_from_file (const string& name, int warn) |
1
|
320 { |
529
|
321 FILE *instream = 0; |
1
|
322 |
1750
|
323 if (name.length () > 0) |
|
324 instream = fopen (name.c_str (), "r"); |
1
|
325 |
529
|
326 if (! instream && warn) |
1750
|
327 warning ("%s: no such file or directory", name.c_str ()); |
1
|
328 |
338
|
329 if (reading_fcn_file || reading_script_file) |
339
|
330 ff_instream = instream; |
1
|
331 else |
2927
|
332 command_editor::set_input_stream (instream); |
1
|
333 |
|
334 return instream; |
|
335 } |
|
336 |
581
|
337 // Fix things up so that input can come from the standard input. This |
|
338 // may need to become much more complicated, which is why it's in a |
|
339 // separate function. |
|
340 |
1
|
341 FILE * |
|
342 get_input_from_stdin (void) |
|
343 { |
2927
|
344 command_editor::set_input_stream (stdin); |
|
345 return command_editor::get_input_stream (); |
1
|
346 } |
|
347 |
2921
|
348 // XXX FIXME XXX -- make this generate file names when appropriate. |
|
349 |
2247
|
350 static string_vector |
2921
|
351 generate_possible_completions (const string& text, string& prefix, |
|
352 string& hint) |
1280
|
353 { |
2247
|
354 string_vector names; |
1280
|
355 |
2921
|
356 prefix = ""; |
1280
|
357 |
2921
|
358 if (! text.empty () && text != "." && text.rfind ('.') != NPOS) |
1430
|
359 names = generate_struct_completions (text, prefix, hint); |
|
360 else |
2247
|
361 names = make_name_list (); |
1280
|
362 |
2348
|
363 names.qsort (); |
|
364 |
|
365 // Remove duplicates. |
|
366 |
|
367 // XXX FIXME XXX -- maybe this should be defined for all Array objects. |
|
368 |
|
369 int k = 0; |
|
370 |
|
371 int len = names.length (); |
|
372 |
|
373 for (int i = 1; i < len; i++) |
|
374 { |
|
375 if (names[i] != names[k]) |
|
376 { |
|
377 k++; |
|
378 |
|
379 if (k != i) |
|
380 names[k] = names[i]; |
|
381 } |
|
382 } |
|
383 |
|
384 names.resize (k+1); |
|
385 |
1280
|
386 return names; |
|
387 } |
|
388 |
|
389 static char * |
|
390 command_generator (const char *text, int state) |
|
391 { |
2921
|
392 static string prefix; |
|
393 static string hint; |
1280
|
394 |
2921
|
395 static size_t prefix_len = 0; |
|
396 static size_t hint_len = 0; |
1280
|
397 |
1
|
398 static int list_index = 0; |
2921
|
399 static int name_list_len = 0; |
2247
|
400 static string_vector name_list; |
1
|
401 |
1280
|
402 static int matches = 0; |
1
|
403 |
|
404 if (state == 0) |
|
405 { |
|
406 list_index = 0; |
|
407 |
2921
|
408 prefix = ""; |
1280
|
409 |
2921
|
410 hint = text; |
1280
|
411 |
|
412 name_list = generate_possible_completions (text, prefix, hint); |
|
413 |
2921
|
414 name_list_len = name_list.length (); |
|
415 |
|
416 prefix_len = prefix.length (); |
1280
|
417 |
2921
|
418 hint_len = hint.length (); |
1280
|
419 |
|
420 matches = 0; |
2247
|
421 |
2921
|
422 for (int i = 0; i < name_list_len; i++) |
|
423 if (! name_list[i].compare (hint, 0, hint_len)) |
2247
|
424 matches++; |
1
|
425 } |
|
426 |
2921
|
427 if (name_list_len > 0 && matches > 0) |
1
|
428 { |
2921
|
429 while (list_index < name_list_len) |
244
|
430 { |
2921
|
431 string name = name_list[list_index]; |
2247
|
432 |
1280
|
433 list_index++; |
2247
|
434 |
2921
|
435 if (! name.compare (hint, 0, hint_len)) |
1280
|
436 { |
2921
|
437 int len = 2 + prefix_len + name.length (); |
|
438 |
2800
|
439 char *buf = static_cast<char *> (malloc (len)); |
1280
|
440 |
2921
|
441 if (! prefix.empty ()) |
1280
|
442 { |
2921
|
443 strcpy (buf, prefix.c_str ()); |
1280
|
444 strcat (buf, "."); |
2921
|
445 strcat (buf, name.c_str ()); |
1280
|
446 } |
|
447 else |
2921
|
448 strcpy (buf, name.c_str ()); |
1280
|
449 |
|
450 if (matches == 1 && looks_like_struct (buf)) |
2927
|
451 command_editor::set_completion_append_character ('.'); |
1280
|
452 else |
2927
|
453 command_editor::set_completion_append_character |
|
454 (Vcompletion_append_char); |
1280
|
455 |
|
456 return buf; |
|
457 } |
244
|
458 } |
1
|
459 } |
|
460 |
529
|
461 return 0; |
1
|
462 } |
|
463 |
2927
|
464 #if 0 |
269
|
465 static char ** |
1488
|
466 command_completer (char *text, int /* start */, int /* end */) |
1
|
467 { |
529
|
468 char **matches = 0; |
1
|
469 matches = completion_matches (text, command_generator); |
|
470 return matches; |
|
471 } |
2927
|
472 #endif |
269
|
473 |
2927
|
474 void |
|
475 initialize_command_input (void) |
|
476 { |
|
477 // If we are using readline, this allows conditional parsing of the |
|
478 // .inputrc file. |
269
|
479 |
2927
|
480 command_editor::set_name ("Octave"); |
1358
|
481 |
2927
|
482 command_editor::set_paren_string_delimiters ("\""); |
269
|
483 } |
|
484 |
2927
|
485 static bool |
|
486 match_sans_spaces (const string& standard, const string& test) |
269
|
487 { |
2927
|
488 string tmp = test; |
269
|
489 |
2927
|
490 size_t beg = test.find_first_not_of (" \t"); |
|
491 size_t end = test.find_last_not_of (" \t"); |
|
492 size_t len = beg - end + 1; |
1
|
493 |
2927
|
494 return test.compare (standard, beg, len) == 0; |
529
|
495 } |
|
496 |
581
|
497 // If the user simply hits return, this will produce an empty matrix. |
|
498 |
2086
|
499 static octave_value_list |
|
500 get_user_input (const octave_value_list& args, int debug = 0) |
529
|
501 { |
2086
|
502 octave_value retval; |
529
|
503 |
|
504 int nargin = args.length (); |
|
505 |
|
506 int read_as_string = 0; |
|
507 |
712
|
508 if (nargin == 2) |
529
|
509 read_as_string++; |
|
510 |
1761
|
511 string prompt ("debug> "); |
|
512 |
712
|
513 if (nargin > 0) |
529
|
514 { |
1761
|
515 prompt = args(0).string_value (); |
636
|
516 |
|
517 if (error_state) |
|
518 { |
|
519 error ("input: unrecognized argument"); |
|
520 return retval; |
|
521 } |
529
|
522 } |
|
523 |
|
524 again: |
|
525 |
2095
|
526 flush_octave_stdout (); |
529
|
527 |
2927
|
528 string input_buf = gnu_readline (prompt.c_str (), true); |
529
|
529 |
2927
|
530 if (! input_buf.empty ()) |
529
|
531 { |
1799
|
532 if (! input_from_startup_file) |
2927
|
533 command_history::add (input_buf); |
529
|
534 |
2927
|
535 size_t len = input_buf.length (); |
529
|
536 |
|
537 if (len < 1) |
|
538 { |
|
539 if (debug) |
|
540 goto again; |
|
541 else |
963
|
542 { |
|
543 if (read_as_string) |
|
544 return ""; |
|
545 else |
|
546 return Matrix (); |
|
547 } |
529
|
548 } |
|
549 |
|
550 if (match_sans_spaces ("exit", input_buf) |
|
551 || match_sans_spaces ("quit", input_buf) |
|
552 || match_sans_spaces ("return", input_buf)) |
963
|
553 { |
|
554 return retval; |
|
555 } |
529
|
556 else if (read_as_string) |
963
|
557 { |
|
558 retval = input_buf; |
|
559 } |
529
|
560 else |
|
561 { |
|
562 int parse_status = 0; |
2900
|
563 |
2896
|
564 retval = eval_string (input_buf, true, parse_status); |
2900
|
565 |
581
|
566 if (retval.is_defined ()) |
|
567 { |
|
568 if (debug) |
2900
|
569 retval.print (octave_stdout); |
581
|
570 } |
|
571 else |
963
|
572 retval = Matrix (); |
529
|
573 } |
|
574 } |
|
575 else |
|
576 error ("input: reading user-input failed!"); |
|
577 |
|
578 if (debug) |
|
579 goto again; |
|
580 |
|
581 return retval; |
|
582 } |
|
583 |
1957
|
584 DEFUN (input, args, , |
529
|
585 "input (PROMPT [, S])\n\ |
|
586 \n\ |
2802
|
587 Prompt user for input. If the second argument is present, return\n\ |
529
|
588 value as a string.") |
|
589 { |
2086
|
590 octave_value_list retval; |
529
|
591 |
|
592 int nargin = args.length (); |
|
593 |
712
|
594 if (nargin == 1 || nargin == 2) |
1488
|
595 retval = get_user_input (args); |
529
|
596 else |
|
597 print_usage ("input"); |
|
598 |
|
599 return retval; |
|
600 } |
|
601 |
1957
|
602 DEFUN (keyboard, args, , |
529
|
603 "keyboard (PROMPT)\n\ |
|
604 \n\ |
|
605 maybe help in debugging function files") |
|
606 { |
2086
|
607 octave_value_list retval; |
529
|
608 |
|
609 int nargin = args.length (); |
|
610 |
712
|
611 if (nargin == 0 || nargin == 1) |
1488
|
612 retval = get_user_input (args, 1); |
529
|
613 else |
|
614 print_usage ("keyboard"); |
|
615 |
|
616 return retval; |
|
617 } |
|
618 |
1957
|
619 DEFUN_TEXT(echo, args, , |
1588
|
620 "echo [options]\n\ |
|
621 \n\ |
|
622 echo [on|off] -- enable or disable echoing of commands as\n\ |
|
623 they are executed in script files\n\ |
|
624 \n\ |
|
625 echo [on all|off all] -- enable or disable echoing of commands as they\n\ |
|
626 are executed in script files and functions\n\ |
|
627 \n\ |
|
628 Without any arguments, toggle the current echo state.") |
|
629 { |
2086
|
630 octave_value_list retval; |
1588
|
631 |
1755
|
632 int argc = args.length () + 1; |
|
633 |
1968
|
634 string_vector argv = args.make_argv ("echo"); |
1755
|
635 |
|
636 if (error_state) |
|
637 return retval; |
1588
|
638 |
|
639 switch (argc) |
|
640 { |
|
641 case 1: |
|
642 { |
2205
|
643 if ((Vecho_executing_commands & ECHO_SCRIPTS) |
|
644 || (Vecho_executing_commands & ECHO_FUNCTIONS)) |
2431
|
645 bind_builtin_variable ("echo_executing_commands", |
2800
|
646 static_cast<double> (ECHO_OFF)); |
1588
|
647 else |
2431
|
648 bind_builtin_variable ("echo_executing_commands", |
2800
|
649 static_cast<double> (ECHO_SCRIPTS)); |
1588
|
650 } |
|
651 break; |
|
652 |
|
653 case 2: |
|
654 { |
1755
|
655 string arg = argv[1]; |
|
656 |
|
657 if (arg == "on") |
2431
|
658 bind_builtin_variable ("echo_executing_commands", |
2800
|
659 static_cast<double> (ECHO_SCRIPTS)); |
1755
|
660 else if (arg == "off") |
2431
|
661 bind_builtin_variable ("echo_executing_commands", |
2800
|
662 static_cast<double> (ECHO_OFF)); |
1588
|
663 else |
|
664 print_usage ("echo"); |
|
665 } |
|
666 break; |
|
667 |
|
668 case 3: |
|
669 { |
1755
|
670 string arg = argv[1]; |
|
671 |
|
672 if (arg == "on" && argv[2] == "all") |
2800
|
673 { |
|
674 int tmp = (ECHO_SCRIPTS | ECHO_FUNCTIONS); |
|
675 bind_builtin_variable ("echo_executing_commands", |
|
676 static_cast<double> (tmp)); |
|
677 } |
1755
|
678 else if (arg == "off" && argv[2] == "all") |
2431
|
679 bind_builtin_variable ("echo_executing_commands", |
2800
|
680 static_cast<double> (ECHO_OFF)); |
1588
|
681 else |
|
682 print_usage ("echo"); |
|
683 } |
|
684 break; |
|
685 |
|
686 default: |
|
687 print_usage ("echo"); |
|
688 break; |
|
689 } |
|
690 |
|
691 return retval; |
|
692 } |
|
693 |
2234
|
694 DEFUN (completion_matches, args, nargout, |
2299
|
695 "completion_matches (HINT): generate possible completions given HINT\n\ |
|
696 \n\ |
|
697 This function is provided for the benefit of programs like Emacs which\n\ |
|
698 might be controlling Octave and handling user input. The current command\n\ |
|
699 number is not incremented when this function is called. This is a feature,\n\ |
|
700 not a bug.") |
2234
|
701 { |
2281
|
702 octave_value retval; |
2234
|
703 |
|
704 int nargin = args.length (); |
|
705 |
|
706 if (nargin == 1) |
|
707 { |
|
708 string hint_string = args(0).string_value (); |
|
709 |
|
710 if (! error_state) |
|
711 { |
|
712 int n = 32; |
|
713 |
|
714 string_vector list (n); |
|
715 |
|
716 const char *hint = hint_string.c_str (); |
|
717 |
|
718 int k = 0; |
|
719 |
|
720 for (;;) |
|
721 { |
|
722 const char *cmd = command_generator (hint, k); |
|
723 |
|
724 if (cmd) |
|
725 { |
2235
|
726 if (*cmd) |
2234
|
727 { |
2235
|
728 if (k == n) |
|
729 { |
|
730 n *= 2; |
|
731 list.resize (n); |
|
732 } |
|
733 |
|
734 list[k++] = cmd; |
2234
|
735 } |
|
736 } |
|
737 else |
|
738 { |
|
739 list.resize (k); |
|
740 break; |
|
741 } |
|
742 } |
|
743 |
|
744 if (nargout > 0) |
2281
|
745 { |
|
746 if (! list.empty ()) |
|
747 retval = list; |
2282
|
748 else |
|
749 retval = ""; |
2281
|
750 } |
2234
|
751 else |
|
752 { |
2235
|
753 // We don't use string_vector::list_in_columns here |
|
754 // because it will be easier for Emacs if the names |
|
755 // appear in a single column. |
|
756 |
2234
|
757 int len = list.length (); |
|
758 |
|
759 for (int i = 0; i < len; i++) |
|
760 octave_stdout << list[i] << "\n"; |
|
761 } |
2299
|
762 |
|
763 octave_completion_matches_called = true; |
2234
|
764 } |
|
765 } |
|
766 else |
|
767 print_usage ("completion_matches"); |
|
768 |
|
769 return retval; |
|
770 } |
|
771 |
2181
|
772 static int |
|
773 ps1 (void) |
|
774 { |
|
775 int status = 0; |
|
776 |
|
777 Vps1 = builtin_string_variable ("PS1"); |
|
778 |
|
779 return status; |
|
780 } |
|
781 |
|
782 static int |
|
783 ps2 (void) |
|
784 { |
|
785 int status = 0; |
|
786 |
|
787 Vps2 = builtin_string_variable ("PS2"); |
|
788 |
|
789 return status; |
|
790 } |
|
791 |
|
792 static int |
|
793 ps4 (void) |
|
794 { |
|
795 int status = 0; |
|
796 |
|
797 Vps4 = builtin_string_variable ("PS4"); |
|
798 |
|
799 return status; |
|
800 } |
|
801 |
|
802 static int |
|
803 completion_append_char (void) |
|
804 { |
|
805 int status = 0; |
|
806 |
|
807 string s = builtin_string_variable ("completion_append_char"); |
|
808 |
|
809 switch (s.length ()) |
|
810 { |
|
811 case 1: |
|
812 Vcompletion_append_char = s[0]; |
|
813 break; |
|
814 |
|
815 case 0: |
|
816 Vcompletion_append_char = '\0'; |
|
817 break; |
|
818 |
|
819 default: |
|
820 warning ("completion_append_char must be a single character"); |
|
821 status = -1; |
|
822 break; |
|
823 } |
|
824 |
|
825 return status; |
|
826 } |
|
827 |
|
828 void |
|
829 symbols_of_input (void) |
|
830 { |
|
831 DEFVAR (PS1, "\\s:\\#> ", 0, ps1, |
|
832 "primary prompt string"); |
|
833 |
|
834 DEFVAR (PS2, "> ", 0, ps2, |
|
835 "secondary prompt string"); |
|
836 |
|
837 DEFVAR (PS4, "+ ", 0, ps4, |
|
838 "string printed before echoed input (enabled by --echo-input)"); |
|
839 |
|
840 DEFVAR (completion_append_char, " ", 0, completion_append_char, |
|
841 "the string to append after successful command-line completion attempts"); |
|
842 } |
|
843 |
1
|
844 /* |
|
845 ;;; Local Variables: *** |
|
846 ;;; mode: C++ *** |
|
847 ;;; End: *** |
|
848 */ |