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 |
661
|
23 /* |
|
24 |
1822
|
25 The 2 functions listed below were adapted from similar functions |
661
|
26 from GNU Bash, the Bourne Again SHell, copyright (C) 1987, 1989, 1991 |
|
27 Free Software Foundation, Inc. |
|
28 |
1822
|
29 read_octal decode_prompt_string |
661
|
30 |
|
31 */ |
|
32 |
1
|
33 // Use the GNU readline library for command line editing and hisory. |
|
34 |
240
|
35 #ifdef HAVE_CONFIG_H |
1192
|
36 #include <config.h> |
1
|
37 #endif |
|
38 |
1343
|
39 #include <ctime> |
|
40 #include <cstdio> |
|
41 #include <cstdlib> |
|
42 #include <cstring> |
|
43 #include <cassert> |
|
44 #include <csignal> |
|
45 |
1728
|
46 #include <string> |
|
47 |
1349
|
48 #include <iostream.h> |
|
49 |
1350
|
50 #ifdef HAVE_UNISTD_H |
2442
|
51 #ifdef HAVE_SYS_TYPES_H |
529
|
52 #include <sys/types.h> |
2442
|
53 #endif |
529
|
54 #include <unistd.h> |
|
55 #endif |
1343
|
56 |
2470
|
57 #if defined (USE_READLINE) |
|
58 #include <readline/readline.h> |
|
59 #include <readline/history.h> |
|
60 #endif |
1
|
61 |
1755
|
62 #include "str-vec.h" |
|
63 |
1352
|
64 #include "defun.h" |
|
65 #include "dirfns.h" |
1
|
66 #include "error.h" |
2181
|
67 #include "gripes.h" |
1352
|
68 #include "help.h" |
1
|
69 #include "input.h" |
1352
|
70 #include "oct-map.h" |
1742
|
71 #include "oct-hist.h" |
1670
|
72 #include "toplev.h" |
1755
|
73 #include "oct-obj.h" |
1
|
74 #include "pager.h" |
529
|
75 #include "parse.h" |
1352
|
76 #include "pathlen.h" |
1755
|
77 #include "pt-const.h" |
1352
|
78 #include "sighandlers.h" |
|
79 #include "symtab.h" |
1114
|
80 #include "sysdep.h" |
1352
|
81 #include "utils.h" |
|
82 #include "variables.h" |
529
|
83 |
2181
|
84 // Primary prompt string. |
|
85 static string Vps1; |
|
86 |
|
87 // Secondary prompt string. |
|
88 static string Vps2; |
|
89 |
|
90 // String printed before echoed input (enabled by --echo-input). |
|
91 string Vps4; |
|
92 |
|
93 // Character to append after successful command-line completion attempts. |
|
94 static char Vcompletion_append_char; |
|
95 |
1
|
96 // Global pointer for eval(). |
1755
|
97 string current_eval_string; |
1
|
98 |
|
99 // Nonzero means get input from current_eval_string. |
|
100 int get_input_from_eval_string = 0; |
|
101 |
338
|
102 // Nonzero means we're parsing a function file. |
|
103 int reading_fcn_file = 0; |
1
|
104 |
338
|
105 // Simple name of function file we are reading. |
1755
|
106 string curr_fcn_file_name; |
1606
|
107 |
|
108 // Full name of file we are reading. |
1755
|
109 string curr_fcn_file_full_name; |
1
|
110 |
|
111 // Nonzero means we're parsing a script file. |
|
112 int reading_script_file = 0; |
|
113 |
|
114 // If we are reading from an M-file, this is it. |
529
|
115 FILE *ff_instream = 0; |
1
|
116 |
|
117 // Nonzero means this is an interactive shell. |
|
118 int interactive = 0; |
|
119 |
|
120 // Nonzero means the user forced this shell to be interactive (-i). |
|
121 int forced_interactive = 0; |
|
122 |
|
123 // Should we issue a prompt? |
|
124 int promptflag = 1; |
|
125 |
|
126 // The current line of input, from wherever. |
1755
|
127 string current_input_line; |
1
|
128 |
2299
|
129 // TRUE after a call to completion_matches(). |
|
130 bool octave_completion_matches_called = false; |
|
131 |
581
|
132 // Return the octal number parsed from STRING, or -1 to indicate that |
|
133 // the string contained a bad number. |
|
134 |
1822
|
135 static int |
1755
|
136 read_octal (const string& s) |
529
|
137 { |
|
138 int result = 0; |
|
139 int digits = 0; |
|
140 |
1755
|
141 size_t i = 0; |
|
142 size_t slen = s.length (); |
|
143 |
|
144 while (i < slen && s[i] >= '0' && s[i] < '8') |
529
|
145 { |
|
146 digits++; |
1755
|
147 result = (result * 8) + s[i] - '0'; |
|
148 i++; |
529
|
149 } |
|
150 |
1755
|
151 if (! digits || result > 0777 || i < slen) |
529
|
152 result = -1; |
|
153 |
|
154 return result; |
|
155 } |
|
156 |
581
|
157 // Return a string which will be printed as a prompt. The string may |
|
158 // contain special characters which are decoded as follows: |
|
159 // |
|
160 // \t the time |
|
161 // \d the date |
|
162 // \n CRLF |
|
163 // \s the name of the shell (program) |
|
164 // \w the current working directory |
|
165 // \W the last element of PWD |
|
166 // \u your username |
|
167 // \h the hostname |
|
168 // \# the command number of this command |
|
169 // \! the history number of this command |
|
170 // \$ a $ or a # if you are root |
|
171 // \<octal> character code in octal |
|
172 // \\ a backslash |
|
173 |
1755
|
174 static string |
|
175 decode_prompt_string (const string& s) |
529
|
176 { |
1755
|
177 string result; |
|
178 string temp; |
|
179 size_t i = 0; |
|
180 size_t slen = s.length (); |
529
|
181 int c; |
|
182 |
1755
|
183 while (i < slen) |
529
|
184 { |
1755
|
185 c = s[i]; |
|
186 |
|
187 i++; |
|
188 |
529
|
189 if (c == '\\') |
|
190 { |
1755
|
191 c = s[i]; |
529
|
192 |
|
193 switch (c) |
|
194 { |
|
195 case '0': |
|
196 case '1': |
|
197 case '2': |
|
198 case '3': |
|
199 case '4': |
|
200 case '5': |
|
201 case '6': |
|
202 case '7': |
1755
|
203 // Maybe convert an octal number. |
529
|
204 { |
1755
|
205 int n = read_octal (s.substr (i, 3)); |
529
|
206 |
1755
|
207 temp = "\\"; |
529
|
208 |
|
209 if (n != -1) |
|
210 { |
1755
|
211 i += 3; |
529
|
212 temp[0] = n; |
|
213 } |
|
214 |
|
215 c = 0; |
|
216 goto add_string; |
|
217 } |
|
218 |
|
219 case 't': |
|
220 case 'd': |
1755
|
221 // Make the current time/date into a string. |
529
|
222 { |
1755
|
223 time_t now = time (0); |
|
224 |
|
225 temp = ctime (&now); |
529
|
226 |
|
227 if (c == 't') |
|
228 { |
1755
|
229 temp = temp.substr (11); |
|
230 temp.resize (8); |
529
|
231 } |
|
232 else |
1755
|
233 temp.resize (10); |
529
|
234 |
|
235 goto add_string; |
|
236 } |
1
|
237 |
529
|
238 case 'n': |
1755
|
239 { |
1822
|
240 if (using_readline) |
1755
|
241 temp = "\r\n"; |
|
242 else |
|
243 temp = "\n"; |
|
244 |
|
245 goto add_string; |
|
246 } |
529
|
247 |
|
248 case 's': |
|
249 { |
2205
|
250 temp = base_pathname (Vprogram_name); |
1755
|
251 |
529
|
252 goto add_string; |
|
253 } |
|
254 |
|
255 case 'w': |
|
256 case 'W': |
|
257 { |
|
258 #define EFFICIENT |
|
259 #ifdef EFFICIENT |
1358
|
260 // Use the value of PWD because it is much more |
|
261 // effecient. |
529
|
262 |
2205
|
263 temp = Vcurrent_directory; |
529
|
264 |
1755
|
265 if (temp.empty ()) |
|
266 temp = octave_getcwd (); |
529
|
267 #else |
1755
|
268 temp = octave_getcwd (); |
529
|
269 #endif /* EFFICIENT */ |
|
270 |
|
271 if (c == 'W') |
|
272 { |
1755
|
273 size_t pos = temp.rfind ('/'); |
|
274 |
|
275 if (pos != NPOS && pos != 0) |
|
276 temp = temp.substr (pos + 1); |
529
|
277 } |
|
278 else |
1755
|
279 temp = polite_directory_format (temp); |
|
280 |
529
|
281 goto add_string; |
|
282 } |
|
283 |
|
284 case 'u': |
|
285 { |
2205
|
286 temp = Vuser_name; |
529
|
287 |
|
288 goto add_string; |
|
289 } |
|
290 |
2327
|
291 case 'H': |
529
|
292 { |
2205
|
293 temp = Vhost_name; |
529
|
294 |
2287
|
295 goto add_string; |
|
296 } |
|
297 |
2327
|
298 case 'h': |
2287
|
299 { |
|
300 temp = Vhost_name; |
|
301 |
1755
|
302 size_t pos = temp.find ('.'); |
|
303 |
|
304 if (pos != NPOS) |
|
305 temp.resize (pos); |
529
|
306 |
|
307 goto add_string; |
|
308 } |
|
309 |
|
310 case '#': |
|
311 { |
|
312 char number_buffer[128]; |
|
313 sprintf (number_buffer, "%d", current_command_number); |
1755
|
314 temp = number_buffer; |
|
315 |
529
|
316 goto add_string; |
|
317 } |
|
318 |
|
319 case '!': |
|
320 { |
|
321 char number_buffer[128]; |
1799
|
322 int num = octave_command_history.current_number (); |
529
|
323 if (num > 0) |
|
324 sprintf (number_buffer, "%d", num); |
|
325 else |
|
326 strcpy (number_buffer, "!"); |
1755
|
327 temp = number_buffer; |
|
328 |
529
|
329 goto add_string; |
|
330 } |
|
331 |
|
332 case '$': |
1755
|
333 { |
|
334 temp = (geteuid () == 0 ? "#" : "$"); |
|
335 |
|
336 goto add_string; |
|
337 } |
529
|
338 |
1568
|
339 case '[': |
|
340 case ']': |
1755
|
341 { |
|
342 temp.resize (2); |
|
343 |
|
344 temp[0] = '\001'; |
|
345 temp[1] = ((c == '[') |
|
346 ? RL_PROMPT_START_IGNORE |
|
347 : RL_PROMPT_END_IGNORE); |
|
348 |
|
349 goto add_string; |
|
350 } |
1568
|
351 |
529
|
352 case '\\': |
1755
|
353 { |
|
354 temp = "\\"; |
|
355 |
|
356 goto add_string; |
|
357 } |
529
|
358 |
|
359 default: |
1755
|
360 { |
|
361 temp = "\\ "; |
|
362 temp[1] = c; |
|
363 |
|
364 goto add_string; |
|
365 } |
529
|
366 |
|
367 add_string: |
1755
|
368 { |
|
369 if (c) |
|
370 i++; |
|
371 |
|
372 result.append (temp); |
|
373 |
|
374 break; |
|
375 } |
529
|
376 } |
|
377 } |
|
378 else |
1755
|
379 result += c; |
529
|
380 } |
|
381 |
|
382 return result; |
|
383 } |
581
|
384 |
1044
|
385 static void |
1755
|
386 do_input_echo (const string& input_string) |
1044
|
387 { |
1588
|
388 int do_echo = reading_script_file ? |
2205
|
389 (Vecho_executing_commands & ECHO_SCRIPTS) |
2618
|
390 : (Vecho_executing_commands & ECHO_CMD_LINE) && ! forced_interactive; |
1588
|
391 |
|
392 if (do_echo) |
1044
|
393 { |
1403
|
394 if (forced_interactive) |
|
395 { |
1755
|
396 if (promptflag > 0) |
2181
|
397 octave_stdout << decode_prompt_string (Vps1); |
1755
|
398 else |
2181
|
399 octave_stdout << decode_prompt_string (Vps2); |
1403
|
400 } |
|
401 else |
2181
|
402 octave_stdout << decode_prompt_string (Vps4); |
1044
|
403 |
1755
|
404 if (! input_string.empty ()) |
1044
|
405 { |
2095
|
406 octave_stdout << input_string; |
1755
|
407 |
|
408 if (input_string[input_string.length () - 1] != '\n') |
2095
|
409 octave_stdout << "\n"; |
1044
|
410 } |
|
411 } |
|
412 } |
|
413 |
1822
|
414 char * |
2275
|
415 gnu_readline (const char *s, bool force_readline) |
1822
|
416 { |
|
417 char *retval = 0; |
|
418 |
2275
|
419 if (using_readline || force_readline) |
1822
|
420 { |
1898
|
421 char *tmp = retval = ::readline (s); |
|
422 |
1900
|
423 if (tmp && strlen (tmp) == 0) |
1898
|
424 { |
2800
|
425 retval = static_cast<char *> (malloc (2)); |
1900
|
426 retval[0] = '\n'; |
|
427 retval[1] = '\0'; |
1898
|
428 } |
1822
|
429 } |
|
430 else |
|
431 { |
|
432 if (s && *s && (interactive || forced_interactive)) |
2618
|
433 { |
|
434 fprintf (rl_outstream, s); |
|
435 fflush (rl_outstream); |
|
436 } |
1822
|
437 |
|
438 FILE *curr_stream = rl_instream; |
|
439 if (reading_fcn_file || reading_script_file) |
|
440 curr_stream = ff_instream; |
|
441 |
|
442 int grow_size = 1024; |
|
443 int max_size = grow_size; |
|
444 |
2800
|
445 char *buf = static_cast<char *> (malloc (max_size)); |
1822
|
446 char *bufptr = buf; |
2862
|
447 int len = 0; |
1822
|
448 |
|
449 do |
|
450 { |
|
451 if (fgets (bufptr, grow_size, curr_stream)) |
|
452 { |
2862
|
453 len = strlen (bufptr); |
1822
|
454 |
|
455 if (len == grow_size - 1) |
|
456 { |
|
457 int tmp = bufptr - buf + grow_size - 1; |
|
458 grow_size *= 2; |
|
459 max_size += grow_size; |
2800
|
460 buf = static_cast<char *> (realloc (buf, max_size)); |
1822
|
461 bufptr = buf + tmp; |
|
462 |
|
463 if (*(bufptr-1) == '\n') |
|
464 { |
|
465 *bufptr = '\0'; |
|
466 retval = buf; |
|
467 } |
|
468 } |
|
469 else if (bufptr[len-1] != '\n') |
|
470 { |
|
471 bufptr[len++] = '\n'; |
|
472 bufptr[len] = '\0'; |
|
473 retval = buf; |
|
474 } |
|
475 else |
|
476 retval = buf; |
|
477 } |
|
478 else |
2862
|
479 { |
|
480 if (len == 0) |
|
481 free (buf); |
|
482 |
|
483 break; |
|
484 } |
1822
|
485 } |
|
486 while (! retval); |
|
487 } |
|
488 |
|
489 return retval; |
|
490 } |
581
|
491 |
287
|
492 static char * |
1
|
493 octave_gets (void) |
|
494 { |
1822
|
495 char *retval = 0; |
1
|
496 |
1822
|
497 if ((interactive || forced_interactive) |
|
498 && (! (reading_fcn_file || reading_script_file))) |
1
|
499 { |
2181
|
500 const char *ps = (promptflag > 0) ? Vps1.c_str () : |
|
501 Vps2.c_str (); |
1755
|
502 |
1761
|
503 string prompt = decode_prompt_string (ps); |
1
|
504 |
2618
|
505 pipe_handler_error_count = 0; |
|
506 |
|
507 flush_octave_stdout (); |
1
|
508 |
2095
|
509 octave_diary << prompt; |
581
|
510 |
1822
|
511 retval = gnu_readline (prompt.c_str ()); |
1
|
512 } |
|
513 else |
1822
|
514 retval = gnu_readline (""); |
1
|
515 |
1822
|
516 if (retval) |
|
517 current_input_line = retval; |
|
518 else |
|
519 current_input_line = ""; |
1
|
520 |
1822
|
521 if (! current_input_line.empty ()) |
1
|
522 { |
1799
|
523 if (! input_from_startup_file) |
1822
|
524 octave_command_history.add (current_input_line); |
1
|
525 |
2095
|
526 octave_diary << current_input_line; |
581
|
527 |
1822
|
528 do_input_echo (current_input_line); |
1
|
529 } |
581
|
530 |
2095
|
531 octave_diary << "\n"; |
581
|
532 |
1822
|
533 return retval; |
1
|
534 } |
|
535 |
581
|
536 // Read a line from the input stream. |
|
537 |
1822
|
538 static char * |
|
539 get_user_input (void) |
1
|
540 { |
1822
|
541 char *retval = 0; |
1
|
542 |
|
543 if (get_input_from_eval_string) |
|
544 { |
1755
|
545 size_t len = current_eval_string.length (); |
|
546 |
2800
|
547 retval = static_cast<char *> (malloc (len + 2)); |
1822
|
548 |
|
549 strcpy (retval, current_eval_string.c_str ()); |
|
550 |
|
551 retval[len++] = '\n'; |
|
552 retval[len] = '\0'; // Paranoia. |
|
553 } |
|
554 else |
|
555 retval = octave_gets (); |
|
556 |
|
557 if (retval) |
|
558 current_input_line = retval; |
|
559 |
2114
|
560 if (! get_input_from_eval_string) |
|
561 input_line_number++; |
1822
|
562 |
|
563 return retval; |
|
564 } |
|
565 |
|
566 int |
|
567 octave_read (char *buf, unsigned max_size) |
|
568 { |
|
569 static char *input_buf = 0; |
|
570 static char *cur_pos = 0; |
|
571 static int chars_left = 0; |
|
572 |
|
573 int status = 0; |
|
574 |
|
575 if (! input_buf) |
|
576 { |
|
577 cur_pos = input_buf = get_user_input (); |
|
578 |
|
579 chars_left = input_buf ? strlen (input_buf) : 0; |
|
580 } |
|
581 |
|
582 if (chars_left > 0) |
|
583 { |
|
584 buf[0] = '\0'; |
|
585 |
|
586 int len = max_size - 2; |
|
587 |
|
588 strncpy (buf, cur_pos, len); |
|
589 |
|
590 if (chars_left > len) |
1
|
591 { |
1822
|
592 chars_left -= len; |
|
593 |
|
594 cur_pos += len; |
|
595 |
|
596 buf[len] = '\0'; |
|
597 |
1
|
598 status = len; |
|
599 } |
|
600 else |
|
601 { |
1822
|
602 free (input_buf); |
|
603 input_buf = 0; |
1755
|
604 |
1822
|
605 len = chars_left; |
|
606 |
|
607 if (buf[len-1] != '\n') |
|
608 buf[len++] = '\n'; |
1
|
609 |
1822
|
610 buf[len] = '\0'; |
1755
|
611 |
1822
|
612 status = len; |
1
|
613 } |
1044
|
614 } |
1822
|
615 else if (chars_left == 0) |
2862
|
616 { |
|
617 if (input_buf) |
|
618 { |
|
619 free (input_buf); |
|
620 input_buf = 0; |
|
621 } |
|
622 |
|
623 status = 0; |
|
624 } |
1822
|
625 else |
|
626 status = -1; |
1044
|
627 |
1
|
628 return status; |
|
629 } |
|
630 |
581
|
631 // Fix things up so that input can come from file `name', printing a |
|
632 // warning if the file doesn't exist. |
|
633 |
1
|
634 FILE * |
1750
|
635 get_input_from_file (const string& name, int warn) |
1
|
636 { |
529
|
637 FILE *instream = 0; |
1
|
638 |
1750
|
639 if (name.length () > 0) |
|
640 instream = fopen (name.c_str (), "r"); |
1
|
641 |
529
|
642 if (! instream && warn) |
1750
|
643 warning ("%s: no such file or directory", name.c_str ()); |
1
|
644 |
338
|
645 if (reading_fcn_file || reading_script_file) |
339
|
646 ff_instream = instream; |
1
|
647 else |
|
648 rl_instream = instream; |
|
649 |
|
650 return instream; |
|
651 } |
|
652 |
581
|
653 // Fix things up so that input can come from the standard input. This |
|
654 // may need to become much more complicated, which is why it's in a |
|
655 // separate function. |
|
656 |
1
|
657 FILE * |
|
658 get_input_from_stdin (void) |
|
659 { |
|
660 rl_instream = stdin; |
|
661 return rl_instream; |
|
662 } |
|
663 |
2921
|
664 // XXX FIXME XXX -- make this generate file names when appropriate. |
|
665 |
2247
|
666 static string_vector |
2921
|
667 generate_possible_completions (const string& text, string& prefix, |
|
668 string& hint) |
1280
|
669 { |
2247
|
670 string_vector names; |
1280
|
671 |
2921
|
672 prefix = ""; |
1280
|
673 |
2921
|
674 if (! text.empty () && text != "." && text.rfind ('.') != NPOS) |
1430
|
675 names = generate_struct_completions (text, prefix, hint); |
|
676 else |
2247
|
677 names = make_name_list (); |
1280
|
678 |
2348
|
679 names.qsort (); |
|
680 |
|
681 // Remove duplicates. |
|
682 |
|
683 // XXX FIXME XXX -- maybe this should be defined for all Array objects. |
|
684 |
|
685 int k = 0; |
|
686 |
|
687 int len = names.length (); |
|
688 |
|
689 for (int i = 1; i < len; i++) |
|
690 { |
|
691 if (names[i] != names[k]) |
|
692 { |
|
693 k++; |
|
694 |
|
695 if (k != i) |
|
696 names[k] = names[i]; |
|
697 } |
|
698 } |
|
699 |
|
700 names.resize (k+1); |
|
701 |
1280
|
702 return names; |
|
703 } |
|
704 |
|
705 static char * |
|
706 command_generator (const char *text, int state) |
|
707 { |
2921
|
708 static string prefix; |
|
709 static string hint; |
1280
|
710 |
2921
|
711 static size_t prefix_len = 0; |
|
712 static size_t hint_len = 0; |
1280
|
713 |
1
|
714 static int list_index = 0; |
2921
|
715 static int name_list_len = 0; |
2247
|
716 static string_vector name_list; |
1
|
717 |
1280
|
718 static int matches = 0; |
1
|
719 |
|
720 if (state == 0) |
|
721 { |
|
722 list_index = 0; |
|
723 |
2921
|
724 prefix = ""; |
1280
|
725 |
2921
|
726 hint = text; |
1280
|
727 |
|
728 name_list = generate_possible_completions (text, prefix, hint); |
|
729 |
2921
|
730 name_list_len = name_list.length (); |
|
731 |
|
732 prefix_len = prefix.length (); |
1280
|
733 |
2921
|
734 hint_len = hint.length (); |
1280
|
735 |
|
736 matches = 0; |
2247
|
737 |
2921
|
738 for (int i = 0; i < name_list_len; i++) |
|
739 if (! name_list[i].compare (hint, 0, hint_len)) |
2247
|
740 matches++; |
1
|
741 } |
|
742 |
2921
|
743 if (name_list_len > 0 && matches > 0) |
1
|
744 { |
2921
|
745 while (list_index < name_list_len) |
244
|
746 { |
2921
|
747 string name = name_list[list_index]; |
2247
|
748 |
1280
|
749 list_index++; |
2247
|
750 |
2921
|
751 if (! name.compare (hint, 0, hint_len)) |
1280
|
752 { |
2921
|
753 int len = 2 + prefix_len + name.length (); |
|
754 |
2800
|
755 char *buf = static_cast<char *> (malloc (len)); |
1280
|
756 |
2921
|
757 if (! prefix.empty ()) |
1280
|
758 { |
2921
|
759 strcpy (buf, prefix.c_str ()); |
1280
|
760 strcat (buf, "."); |
2921
|
761 strcat (buf, name.c_str ()); |
1280
|
762 } |
|
763 else |
2921
|
764 strcpy (buf, name.c_str ()); |
1280
|
765 |
|
766 if (matches == 1 && looks_like_struct (buf)) |
|
767 rl_completion_append_character = '.'; |
|
768 else |
2247
|
769 rl_completion_append_character = Vcompletion_append_char; |
1280
|
770 |
|
771 return buf; |
|
772 } |
244
|
773 } |
1
|
774 } |
|
775 |
529
|
776 return 0; |
1
|
777 } |
|
778 |
269
|
779 static char ** |
1488
|
780 command_completer (char *text, int /* start */, int /* end */) |
1
|
781 { |
529
|
782 char **matches = 0; |
1
|
783 matches = completion_matches (text, command_generator); |
|
784 return matches; |
|
785 } |
|
786 |
581
|
787 // The next two functions implement the equivalent of the K*rn shell |
|
788 // C-o operate-and-get-next-history-line editing command. Stolen from |
|
789 // the GNU Bourne Again SHell. |
269
|
790 |
|
791 // ?? |
|
792 static int saved_history_line_to_use = 0; |
|
793 |
|
794 // ?? |
529
|
795 static Function *old_rl_startup_hook = 0; |
269
|
796 |
|
797 static void |
|
798 set_saved_history (void) |
|
799 { |
|
800 HIST_ENTRY *h; |
|
801 |
|
802 if (saved_history_line_to_use) |
|
803 { |
|
804 if (history_set_pos (saved_history_line_to_use)) |
|
805 { |
|
806 h = current_history (); |
|
807 if (h) |
|
808 { |
|
809 rl_insert_text (h->line); |
|
810 |
1358
|
811 // Get rid of any undo list created by the previous |
|
812 // insert, so the line won't totally be erased when the |
|
813 // edits are undone (they will be normally, because this |
|
814 // is a history line -- cf. readline.c: line 380 or |
|
815 // so). |
|
816 |
269
|
817 if (rl_undo_list) |
|
818 { |
|
819 free_undo_list (); |
529
|
820 rl_undo_list = 0; |
269
|
821 } |
|
822 } |
|
823 } |
|
824 } |
|
825 saved_history_line_to_use = 0; |
|
826 rl_startup_hook = old_rl_startup_hook; |
|
827 } |
|
828 |
|
829 static void |
1488
|
830 operate_and_get_next (int /* count */, int /* c */) |
269
|
831 { |
|
832 int where; |
|
833 |
1358
|
834 // Accept the current line. |
|
835 |
269
|
836 rl_newline (); |
|
837 |
1358
|
838 // Find the current line, and find the next line to use. |
|
839 |
269
|
840 where = where_history (); |
|
841 |
1430
|
842 if ((history_is_stifled () && (history_length >= max_input_history)) |
|
843 || (where >= history_length - 1)) |
269
|
844 saved_history_line_to_use = where; |
|
845 else |
|
846 saved_history_line_to_use = where + 1; |
|
847 |
|
848 old_rl_startup_hook = rl_startup_hook; |
2800
|
849 rl_startup_hook = static_cast<Function *> (set_saved_history); |
269
|
850 } |
|
851 |
1
|
852 void |
|
853 initialize_readline (void) |
|
854 { |
1705
|
855 // Set things up internally in case some function that uses readline |
|
856 // (currently Fclc(), maybe others) is called before readline(). |
|
857 |
|
858 rl_initialize (); |
|
859 |
1358
|
860 // Allow conditional parsing of the ~/.inputrc file |
|
861 |
1
|
862 rl_readline_name = "Octave"; |
|
863 |
1358
|
864 // Tell the completer that we want to try first. |
|
865 |
2800
|
866 rl_attempted_completion_function |
|
867 = static_cast<CPPFunction *> (command_completer); |
269
|
868 |
1358
|
869 // Bind operate-and-get-next. |
|
870 |
2800
|
871 rl_add_defun ("operate-and-get-next", operate_and_get_next, CTRL ('O')); |
1441
|
872 |
1569
|
873 |
|
874 // And the history search functions. |
|
875 |
2800
|
876 rl_add_defun ("history-search-backward", rl_history_search_backward, |
|
877 META ('p')); |
1569
|
878 |
2800
|
879 rl_add_defun ("history-search-forward", rl_history_search_forward, |
|
880 META ('n')); |
1569
|
881 |
1441
|
882 // Don't treat single quotes as string delimiters when doing paren |
|
883 // matching. |
|
884 |
|
885 rl_paren_string_delimiters = "\""; |
1
|
886 } |
|
887 |
529
|
888 static int |
|
889 match_sans_spaces (const char *standard, const char *test) |
|
890 { |
1034
|
891 char *tmp = strsave (test); |
|
892 |
|
893 char *tp = tmp; |
529
|
894 while (*tp == ' ' || *tp == '\t') |
|
895 tp++; |
|
896 |
1125
|
897 char *ep = tmp + strlen (tmp) - 1; |
529
|
898 while (*ep == ' ' || *ep == '\t') |
|
899 ep--; |
|
900 |
1034
|
901 *(ep+1) = '\0'; |
|
902 |
|
903 int retval = strcmp (standard, tp) == 0; |
529
|
904 |
1034
|
905 delete [] tmp; |
|
906 |
|
907 return retval; |
|
908 |
529
|
909 } |
|
910 |
581
|
911 // If the user simply hits return, this will produce an empty matrix. |
|
912 |
2086
|
913 static octave_value_list |
|
914 get_user_input (const octave_value_list& args, int debug = 0) |
529
|
915 { |
2086
|
916 octave_value retval; |
529
|
917 |
|
918 int nargin = args.length (); |
|
919 |
|
920 int read_as_string = 0; |
|
921 |
712
|
922 if (nargin == 2) |
529
|
923 read_as_string++; |
|
924 |
1761
|
925 string prompt ("debug> "); |
|
926 |
712
|
927 if (nargin > 0) |
529
|
928 { |
1761
|
929 prompt = args(0).string_value (); |
636
|
930 |
|
931 if (error_state) |
|
932 { |
|
933 error ("input: unrecognized argument"); |
|
934 return retval; |
|
935 } |
529
|
936 } |
|
937 |
|
938 again: |
|
939 |
2095
|
940 flush_octave_stdout (); |
529
|
941 |
2275
|
942 char *input_buf = gnu_readline (prompt.c_str (), true); |
529
|
943 |
|
944 if (input_buf) |
|
945 { |
1799
|
946 if (! input_from_startup_file) |
|
947 octave_command_history.add (input_buf); |
529
|
948 |
|
949 int len = strlen (input_buf); |
|
950 |
|
951 if (len < 1) |
|
952 { |
|
953 if (debug) |
|
954 goto again; |
|
955 else |
963
|
956 { |
|
957 if (read_as_string) |
|
958 return ""; |
|
959 else |
|
960 return Matrix (); |
|
961 } |
529
|
962 } |
|
963 |
|
964 if (match_sans_spaces ("exit", input_buf) |
|
965 || match_sans_spaces ("quit", input_buf) |
|
966 || match_sans_spaces ("return", input_buf)) |
963
|
967 { |
|
968 return retval; |
|
969 } |
529
|
970 else if (read_as_string) |
963
|
971 { |
|
972 retval = input_buf; |
|
973 } |
529
|
974 else |
|
975 { |
|
976 int parse_status = 0; |
2900
|
977 |
2896
|
978 retval = eval_string (input_buf, true, parse_status); |
2900
|
979 |
581
|
980 if (retval.is_defined ()) |
|
981 { |
|
982 if (debug) |
2900
|
983 retval.print (octave_stdout); |
581
|
984 } |
|
985 else |
963
|
986 retval = Matrix (); |
529
|
987 } |
|
988 } |
|
989 else |
|
990 error ("input: reading user-input failed!"); |
|
991 |
|
992 if (debug) |
|
993 goto again; |
|
994 |
|
995 return retval; |
|
996 } |
|
997 |
1957
|
998 DEFUN (input, args, , |
529
|
999 "input (PROMPT [, S])\n\ |
|
1000 \n\ |
2802
|
1001 Prompt user for input. If the second argument is present, return\n\ |
529
|
1002 value as a string.") |
|
1003 { |
2086
|
1004 octave_value_list retval; |
529
|
1005 |
|
1006 int nargin = args.length (); |
|
1007 |
712
|
1008 if (nargin == 1 || nargin == 2) |
1488
|
1009 retval = get_user_input (args); |
529
|
1010 else |
|
1011 print_usage ("input"); |
|
1012 |
|
1013 return retval; |
|
1014 } |
|
1015 |
1957
|
1016 DEFUN (keyboard, args, , |
529
|
1017 "keyboard (PROMPT)\n\ |
|
1018 \n\ |
|
1019 maybe help in debugging function files") |
|
1020 { |
2086
|
1021 octave_value_list retval; |
529
|
1022 |
|
1023 int nargin = args.length (); |
|
1024 |
712
|
1025 if (nargin == 0 || nargin == 1) |
1488
|
1026 retval = get_user_input (args, 1); |
529
|
1027 else |
|
1028 print_usage ("keyboard"); |
|
1029 |
|
1030 return retval; |
|
1031 } |
|
1032 |
1957
|
1033 DEFUN_TEXT(echo, args, , |
1588
|
1034 "echo [options]\n\ |
|
1035 \n\ |
|
1036 echo [on|off] -- enable or disable echoing of commands as\n\ |
|
1037 they are executed in script files\n\ |
|
1038 \n\ |
|
1039 echo [on all|off all] -- enable or disable echoing of commands as they\n\ |
|
1040 are executed in script files and functions\n\ |
|
1041 \n\ |
|
1042 Without any arguments, toggle the current echo state.") |
|
1043 { |
2086
|
1044 octave_value_list retval; |
1588
|
1045 |
1755
|
1046 int argc = args.length () + 1; |
|
1047 |
1968
|
1048 string_vector argv = args.make_argv ("echo"); |
1755
|
1049 |
|
1050 if (error_state) |
|
1051 return retval; |
1588
|
1052 |
|
1053 switch (argc) |
|
1054 { |
|
1055 case 1: |
|
1056 { |
2205
|
1057 if ((Vecho_executing_commands & ECHO_SCRIPTS) |
|
1058 || (Vecho_executing_commands & ECHO_FUNCTIONS)) |
2431
|
1059 bind_builtin_variable ("echo_executing_commands", |
2800
|
1060 static_cast<double> (ECHO_OFF)); |
1588
|
1061 else |
2431
|
1062 bind_builtin_variable ("echo_executing_commands", |
2800
|
1063 static_cast<double> (ECHO_SCRIPTS)); |
1588
|
1064 } |
|
1065 break; |
|
1066 |
|
1067 case 2: |
|
1068 { |
1755
|
1069 string arg = argv[1]; |
|
1070 |
|
1071 if (arg == "on") |
2431
|
1072 bind_builtin_variable ("echo_executing_commands", |
2800
|
1073 static_cast<double> (ECHO_SCRIPTS)); |
1755
|
1074 else if (arg == "off") |
2431
|
1075 bind_builtin_variable ("echo_executing_commands", |
2800
|
1076 static_cast<double> (ECHO_OFF)); |
1588
|
1077 else |
|
1078 print_usage ("echo"); |
|
1079 } |
|
1080 break; |
|
1081 |
|
1082 case 3: |
|
1083 { |
1755
|
1084 string arg = argv[1]; |
|
1085 |
|
1086 if (arg == "on" && argv[2] == "all") |
2800
|
1087 { |
|
1088 int tmp = (ECHO_SCRIPTS | ECHO_FUNCTIONS); |
|
1089 bind_builtin_variable ("echo_executing_commands", |
|
1090 static_cast<double> (tmp)); |
|
1091 } |
1755
|
1092 else if (arg == "off" && argv[2] == "all") |
2431
|
1093 bind_builtin_variable ("echo_executing_commands", |
2800
|
1094 static_cast<double> (ECHO_OFF)); |
1588
|
1095 else |
|
1096 print_usage ("echo"); |
|
1097 } |
|
1098 break; |
|
1099 |
|
1100 default: |
|
1101 print_usage ("echo"); |
|
1102 break; |
|
1103 } |
|
1104 |
|
1105 return retval; |
|
1106 } |
|
1107 |
2234
|
1108 DEFUN (completion_matches, args, nargout, |
2299
|
1109 "completion_matches (HINT): generate possible completions given HINT\n\ |
|
1110 \n\ |
|
1111 This function is provided for the benefit of programs like Emacs which\n\ |
|
1112 might be controlling Octave and handling user input. The current command\n\ |
|
1113 number is not incremented when this function is called. This is a feature,\n\ |
|
1114 not a bug.") |
2234
|
1115 { |
2281
|
1116 octave_value retval; |
2234
|
1117 |
|
1118 int nargin = args.length (); |
|
1119 |
|
1120 if (nargin == 1) |
|
1121 { |
|
1122 string hint_string = args(0).string_value (); |
|
1123 |
|
1124 if (! error_state) |
|
1125 { |
|
1126 int n = 32; |
|
1127 |
|
1128 string_vector list (n); |
|
1129 |
|
1130 const char *hint = hint_string.c_str (); |
|
1131 |
|
1132 int k = 0; |
|
1133 |
|
1134 for (;;) |
|
1135 { |
|
1136 const char *cmd = command_generator (hint, k); |
|
1137 |
|
1138 if (cmd) |
|
1139 { |
2235
|
1140 if (*cmd) |
2234
|
1141 { |
2235
|
1142 if (k == n) |
|
1143 { |
|
1144 n *= 2; |
|
1145 list.resize (n); |
|
1146 } |
|
1147 |
|
1148 list[k++] = cmd; |
2234
|
1149 } |
|
1150 } |
|
1151 else |
|
1152 { |
|
1153 list.resize (k); |
|
1154 break; |
|
1155 } |
|
1156 } |
|
1157 |
|
1158 if (nargout > 0) |
2281
|
1159 { |
|
1160 if (! list.empty ()) |
|
1161 retval = list; |
2282
|
1162 else |
|
1163 retval = ""; |
2281
|
1164 } |
2234
|
1165 else |
|
1166 { |
2235
|
1167 // We don't use string_vector::list_in_columns here |
|
1168 // because it will be easier for Emacs if the names |
|
1169 // appear in a single column. |
|
1170 |
2234
|
1171 int len = list.length (); |
|
1172 |
|
1173 for (int i = 0; i < len; i++) |
|
1174 octave_stdout << list[i] << "\n"; |
|
1175 } |
2299
|
1176 |
|
1177 octave_completion_matches_called = true; |
2234
|
1178 } |
|
1179 } |
|
1180 else |
|
1181 print_usage ("completion_matches"); |
|
1182 |
|
1183 return retval; |
|
1184 } |
|
1185 |
2181
|
1186 static int |
|
1187 ps1 (void) |
|
1188 { |
|
1189 int status = 0; |
|
1190 |
|
1191 Vps1 = builtin_string_variable ("PS1"); |
|
1192 |
|
1193 return status; |
|
1194 } |
|
1195 |
|
1196 static int |
|
1197 ps2 (void) |
|
1198 { |
|
1199 int status = 0; |
|
1200 |
|
1201 Vps2 = builtin_string_variable ("PS2"); |
|
1202 |
|
1203 return status; |
|
1204 } |
|
1205 |
|
1206 static int |
|
1207 ps4 (void) |
|
1208 { |
|
1209 int status = 0; |
|
1210 |
|
1211 Vps4 = builtin_string_variable ("PS4"); |
|
1212 |
|
1213 return status; |
|
1214 } |
|
1215 |
|
1216 static int |
|
1217 completion_append_char (void) |
|
1218 { |
|
1219 int status = 0; |
|
1220 |
|
1221 string s = builtin_string_variable ("completion_append_char"); |
|
1222 |
|
1223 switch (s.length ()) |
|
1224 { |
|
1225 case 1: |
|
1226 Vcompletion_append_char = s[0]; |
|
1227 break; |
|
1228 |
|
1229 case 0: |
|
1230 Vcompletion_append_char = '\0'; |
|
1231 break; |
|
1232 |
|
1233 default: |
|
1234 warning ("completion_append_char must be a single character"); |
|
1235 status = -1; |
|
1236 break; |
|
1237 } |
|
1238 |
|
1239 return status; |
|
1240 } |
|
1241 |
|
1242 void |
|
1243 symbols_of_input (void) |
|
1244 { |
|
1245 DEFVAR (PS1, "\\s:\\#> ", 0, ps1, |
|
1246 "primary prompt string"); |
|
1247 |
|
1248 DEFVAR (PS2, "> ", 0, ps2, |
|
1249 "secondary prompt string"); |
|
1250 |
|
1251 DEFVAR (PS4, "+ ", 0, ps4, |
|
1252 "string printed before echoed input (enabled by --echo-input)"); |
|
1253 |
|
1254 DEFVAR (completion_append_char, " ", 0, completion_append_char, |
|
1255 "the string to append after successful command-line completion attempts"); |
|
1256 } |
|
1257 |
1
|
1258 /* |
|
1259 ;;; Local Variables: *** |
|
1260 ;;; mode: C++ *** |
|
1261 ;;; End: *** |
|
1262 */ |