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 |
2247
|
664 static string_vector |
1280
|
665 generate_struct_completions (const char *text, char *& prefix, |
|
666 char *& hint) |
|
667 { |
2247
|
668 string_vector names; |
1280
|
669 |
2876
|
670 // XXX FIXME XXX -- this needs some work, eh? |
|
671 |
|
672 #if 0 |
1288
|
673 assert (text); |
|
674 |
1280
|
675 char *id = strsave (text); |
|
676 char *ptr = strchr (id, '.'); |
|
677 *ptr = '\0'; |
|
678 |
|
679 char *elts = ptr + 1; |
|
680 ptr = strrchr (elts, '.'); |
|
681 if (ptr) |
|
682 *ptr = '\0'; |
|
683 else |
|
684 elts = 0; |
|
685 |
|
686 prefix = strsave (text); |
|
687 ptr = strrchr (prefix, '.'); |
|
688 *ptr = '\0'; |
|
689 |
1288
|
690 delete [] hint; |
1280
|
691 hint = strsave (ptr + 1); |
|
692 |
2856
|
693 symbol_record *sr = curr_sym_tab->lookup (id); |
1280
|
694 if (! sr) |
2856
|
695 sr = global_sym_tab->lookup (id); |
1280
|
696 |
|
697 if (sr && sr->is_defined ()) |
|
698 { |
|
699 tree_fvc *tmp_fvc = sr->def (); |
|
700 |
2371
|
701 tree_constant *def = 0; |
1280
|
702 if (tmp_fvc->is_constant ()) |
2800
|
703 def = static_cast<tree_constant *> (tmp_fvc); |
1280
|
704 |
|
705 if (def && def->is_map ()) |
|
706 { |
|
707 if (elts && *elts) |
|
708 { |
2856
|
709 octave_value ult = def->lookup_map_element (elts, false, true); |
1280
|
710 |
|
711 if (ult.is_map ()) |
|
712 { |
|
713 Octave_map m = ult.map_value (); |
2247
|
714 names = m.make_name_list (); |
1280
|
715 } |
|
716 } |
|
717 else |
|
718 { |
|
719 Octave_map m = def->map_value (); |
2247
|
720 names = m.make_name_list (); |
1280
|
721 } |
|
722 } |
|
723 } |
|
724 |
|
725 delete [] id; |
2876
|
726 #endif |
1280
|
727 |
|
728 return names; |
|
729 } |
|
730 |
1430
|
731 // XXX FIXME XXX -- make this generate file names when appropriate. |
|
732 |
2247
|
733 static string_vector |
1280
|
734 generate_possible_completions (const char *text, char *& prefix, |
|
735 char *& hint) |
1
|
736 { |
2247
|
737 string_vector names; |
1280
|
738 |
|
739 prefix = 0; |
|
740 |
1430
|
741 if (text && *text && *text != '.' && strrchr (text, '.')) |
|
742 names = generate_struct_completions (text, prefix, hint); |
|
743 else |
2247
|
744 names = make_name_list (); |
1280
|
745 |
2348
|
746 names.qsort (); |
|
747 |
|
748 // Remove duplicates. |
|
749 |
|
750 // XXX FIXME XXX -- maybe this should be defined for all Array objects. |
|
751 |
|
752 int k = 0; |
|
753 |
|
754 int len = names.length (); |
|
755 |
|
756 for (int i = 1; i < len; i++) |
|
757 { |
|
758 if (names[i] != names[k]) |
|
759 { |
|
760 k++; |
|
761 |
|
762 if (k != i) |
|
763 names[k] = names[i]; |
|
764 } |
|
765 } |
|
766 |
|
767 names.resize (k+1); |
|
768 |
1280
|
769 return names; |
|
770 } |
|
771 |
|
772 static int |
|
773 looks_like_struct (const char *nm) |
|
774 { |
|
775 int retval = 0; |
|
776 |
2876
|
777 // XXX FIXME XXX -- this needs some work, eh? |
|
778 |
|
779 #if 0 |
1288
|
780 assert (nm); |
|
781 |
1280
|
782 char *id = strsave (nm); |
|
783 char *elts = 0; |
|
784 char *ptr = strchr (id, '.'); |
|
785 if (ptr) |
|
786 { |
|
787 *ptr = '\0'; |
|
788 elts = ptr + 1; |
|
789 } |
|
790 |
2856
|
791 symbol_record *sr = curr_sym_tab->lookup (id); |
1280
|
792 if (! sr) |
2856
|
793 sr = global_sym_tab->lookup (id); |
1280
|
794 |
|
795 if (sr && sr->is_defined ()) |
|
796 { |
|
797 tree_fvc *tmp_fvc = sr->def (); |
|
798 |
2371
|
799 tree_constant *def = 0; |
1280
|
800 if (tmp_fvc->is_constant ()) |
2800
|
801 def = static_cast<tree_constant *> (tmp_fvc); |
1280
|
802 |
|
803 if (def && def->is_map ()) |
|
804 { |
|
805 if (elts && *elts) |
|
806 { |
2856
|
807 octave_value ult = def->lookup_map_element (elts, false, true); |
1280
|
808 |
|
809 if (ult.is_map ()) |
|
810 retval = 1; |
|
811 } |
|
812 else |
|
813 retval = 1; |
|
814 } |
|
815 } |
|
816 |
|
817 delete [] id; |
2876
|
818 #endif |
1280
|
819 |
|
820 return retval; |
|
821 } |
|
822 |
2235
|
823 // XXX FIXME XXX -- this has to return a pointer to char, but it |
|
824 // should be converted to use a generating function that returns a |
|
825 // string_vector. |
|
826 |
1280
|
827 static char * |
|
828 command_generator (const char *text, int state) |
|
829 { |
|
830 static char *prefix = 0; |
|
831 static char *hint = 0; |
|
832 |
|
833 static int prefix_len = 0; |
|
834 static int hint_len = 0; |
|
835 |
1
|
836 static int list_index = 0; |
2247
|
837 static int list_length = 0; |
|
838 static string_vector name_list; |
1
|
839 |
1280
|
840 static int matches = 0; |
1
|
841 |
|
842 if (state == 0) |
|
843 { |
|
844 list_index = 0; |
|
845 |
1280
|
846 delete [] prefix; |
|
847 prefix = 0; |
|
848 |
|
849 delete [] hint; |
1288
|
850 hint = strsave (text); |
1280
|
851 |
|
852 name_list = generate_possible_completions (text, prefix, hint); |
|
853 |
|
854 prefix_len = 0; |
|
855 if (prefix) |
1288
|
856 prefix_len = strlen (prefix); |
1280
|
857 |
1288
|
858 assert (hint); |
|
859 hint_len = strlen (hint); |
1280
|
860 |
|
861 matches = 0; |
2247
|
862 |
|
863 list_length = name_list.length (); |
|
864 |
|
865 for (int i = 0; i < list_length; i++) |
|
866 if (name_list[i].compare (hint, 0, hint_len)) |
|
867 matches++; |
1
|
868 } |
|
869 |
2247
|
870 if (list_length > 0 && matches > 0) |
1
|
871 { |
1755
|
872 const char *name; |
|
873 |
2247
|
874 while (list_index < list_length) |
244
|
875 { |
2247
|
876 name = name_list[list_index].c_str (); |
|
877 |
1280
|
878 list_index++; |
2247
|
879 |
1280
|
880 if (strncmp (name, hint, hint_len) == 0) |
|
881 { |
1288
|
882 int len = 2 + prefix_len + strlen (name); |
2800
|
883 char *buf = static_cast<char *> (malloc (len)); |
1280
|
884 |
|
885 if (prefix) |
|
886 { |
|
887 strcpy (buf, prefix); |
|
888 strcat (buf, "."); |
|
889 strcat (buf, name); |
|
890 } |
|
891 else |
|
892 strcpy (buf, name); |
|
893 |
|
894 if (matches == 1 && looks_like_struct (buf)) |
|
895 rl_completion_append_character = '.'; |
|
896 else |
2247
|
897 rl_completion_append_character = Vcompletion_append_char; |
1280
|
898 |
|
899 return buf; |
|
900 } |
244
|
901 } |
1
|
902 } |
|
903 |
529
|
904 return 0; |
1
|
905 } |
|
906 |
269
|
907 static char ** |
1488
|
908 command_completer (char *text, int /* start */, int /* end */) |
1
|
909 { |
529
|
910 char **matches = 0; |
1
|
911 matches = completion_matches (text, command_generator); |
|
912 return matches; |
|
913 } |
|
914 |
581
|
915 // The next two functions implement the equivalent of the K*rn shell |
|
916 // C-o operate-and-get-next-history-line editing command. Stolen from |
|
917 // the GNU Bourne Again SHell. |
269
|
918 |
|
919 // ?? |
|
920 static int saved_history_line_to_use = 0; |
|
921 |
|
922 // ?? |
529
|
923 static Function *old_rl_startup_hook = 0; |
269
|
924 |
|
925 static void |
|
926 set_saved_history (void) |
|
927 { |
|
928 HIST_ENTRY *h; |
|
929 |
|
930 if (saved_history_line_to_use) |
|
931 { |
|
932 if (history_set_pos (saved_history_line_to_use)) |
|
933 { |
|
934 h = current_history (); |
|
935 if (h) |
|
936 { |
|
937 rl_insert_text (h->line); |
|
938 |
1358
|
939 // Get rid of any undo list created by the previous |
|
940 // insert, so the line won't totally be erased when the |
|
941 // edits are undone (they will be normally, because this |
|
942 // is a history line -- cf. readline.c: line 380 or |
|
943 // so). |
|
944 |
269
|
945 if (rl_undo_list) |
|
946 { |
|
947 free_undo_list (); |
529
|
948 rl_undo_list = 0; |
269
|
949 } |
|
950 } |
|
951 } |
|
952 } |
|
953 saved_history_line_to_use = 0; |
|
954 rl_startup_hook = old_rl_startup_hook; |
|
955 } |
|
956 |
|
957 static void |
1488
|
958 operate_and_get_next (int /* count */, int /* c */) |
269
|
959 { |
|
960 int where; |
|
961 |
1358
|
962 // Accept the current line. |
|
963 |
269
|
964 rl_newline (); |
|
965 |
1358
|
966 // Find the current line, and find the next line to use. |
|
967 |
269
|
968 where = where_history (); |
|
969 |
1430
|
970 if ((history_is_stifled () && (history_length >= max_input_history)) |
|
971 || (where >= history_length - 1)) |
269
|
972 saved_history_line_to_use = where; |
|
973 else |
|
974 saved_history_line_to_use = where + 1; |
|
975 |
|
976 old_rl_startup_hook = rl_startup_hook; |
2800
|
977 rl_startup_hook = static_cast<Function *> (set_saved_history); |
269
|
978 } |
|
979 |
1
|
980 void |
|
981 initialize_readline (void) |
|
982 { |
1705
|
983 // Set things up internally in case some function that uses readline |
|
984 // (currently Fclc(), maybe others) is called before readline(). |
|
985 |
|
986 rl_initialize (); |
|
987 |
1358
|
988 // Allow conditional parsing of the ~/.inputrc file |
|
989 |
1
|
990 rl_readline_name = "Octave"; |
|
991 |
1358
|
992 // Tell the completer that we want to try first. |
|
993 |
2800
|
994 rl_attempted_completion_function |
|
995 = static_cast<CPPFunction *> (command_completer); |
269
|
996 |
1358
|
997 // Bind operate-and-get-next. |
|
998 |
2800
|
999 rl_add_defun ("operate-and-get-next", operate_and_get_next, CTRL ('O')); |
1441
|
1000 |
1569
|
1001 |
|
1002 // And the history search functions. |
|
1003 |
2800
|
1004 rl_add_defun ("history-search-backward", rl_history_search_backward, |
|
1005 META ('p')); |
1569
|
1006 |
2800
|
1007 rl_add_defun ("history-search-forward", rl_history_search_forward, |
|
1008 META ('n')); |
1569
|
1009 |
1441
|
1010 // Don't treat single quotes as string delimiters when doing paren |
|
1011 // matching. |
|
1012 |
|
1013 rl_paren_string_delimiters = "\""; |
1
|
1014 } |
|
1015 |
529
|
1016 static int |
|
1017 match_sans_spaces (const char *standard, const char *test) |
|
1018 { |
1034
|
1019 char *tmp = strsave (test); |
|
1020 |
|
1021 char *tp = tmp; |
529
|
1022 while (*tp == ' ' || *tp == '\t') |
|
1023 tp++; |
|
1024 |
1125
|
1025 char *ep = tmp + strlen (tmp) - 1; |
529
|
1026 while (*ep == ' ' || *ep == '\t') |
|
1027 ep--; |
|
1028 |
1034
|
1029 *(ep+1) = '\0'; |
|
1030 |
|
1031 int retval = strcmp (standard, tp) == 0; |
529
|
1032 |
1034
|
1033 delete [] tmp; |
|
1034 |
|
1035 return retval; |
|
1036 |
529
|
1037 } |
|
1038 |
581
|
1039 // If the user simply hits return, this will produce an empty matrix. |
|
1040 |
2086
|
1041 static octave_value_list |
|
1042 get_user_input (const octave_value_list& args, int debug = 0) |
529
|
1043 { |
2086
|
1044 octave_value retval; |
529
|
1045 |
|
1046 int nargin = args.length (); |
|
1047 |
|
1048 int read_as_string = 0; |
|
1049 |
712
|
1050 if (nargin == 2) |
529
|
1051 read_as_string++; |
|
1052 |
1761
|
1053 string prompt ("debug> "); |
|
1054 |
712
|
1055 if (nargin > 0) |
529
|
1056 { |
1761
|
1057 prompt = args(0).string_value (); |
636
|
1058 |
|
1059 if (error_state) |
|
1060 { |
|
1061 error ("input: unrecognized argument"); |
|
1062 return retval; |
|
1063 } |
529
|
1064 } |
|
1065 |
|
1066 again: |
|
1067 |
2095
|
1068 flush_octave_stdout (); |
529
|
1069 |
2275
|
1070 char *input_buf = gnu_readline (prompt.c_str (), true); |
529
|
1071 |
|
1072 if (input_buf) |
|
1073 { |
1799
|
1074 if (! input_from_startup_file) |
|
1075 octave_command_history.add (input_buf); |
529
|
1076 |
|
1077 int len = strlen (input_buf); |
|
1078 |
|
1079 if (len < 1) |
|
1080 { |
|
1081 if (debug) |
|
1082 goto again; |
|
1083 else |
963
|
1084 { |
|
1085 if (read_as_string) |
|
1086 return ""; |
|
1087 else |
|
1088 return Matrix (); |
|
1089 } |
529
|
1090 } |
|
1091 |
|
1092 if (match_sans_spaces ("exit", input_buf) |
|
1093 || match_sans_spaces ("quit", input_buf) |
|
1094 || match_sans_spaces ("return", input_buf)) |
963
|
1095 { |
|
1096 return retval; |
|
1097 } |
529
|
1098 else if (read_as_string) |
963
|
1099 { |
|
1100 retval = input_buf; |
|
1101 } |
529
|
1102 else |
|
1103 { |
|
1104 int parse_status = 0; |
2896
|
1105 retval = eval_string (input_buf, true, parse_status); |
581
|
1106 if (retval.is_defined ()) |
|
1107 { |
|
1108 if (debug) |
2371
|
1109 retval.print (); |
581
|
1110 } |
|
1111 else |
963
|
1112 retval = Matrix (); |
529
|
1113 } |
|
1114 } |
|
1115 else |
|
1116 error ("input: reading user-input failed!"); |
|
1117 |
|
1118 if (debug) |
|
1119 goto again; |
|
1120 |
|
1121 return retval; |
|
1122 } |
|
1123 |
1957
|
1124 DEFUN (input, args, , |
529
|
1125 "input (PROMPT [, S])\n\ |
|
1126 \n\ |
2802
|
1127 Prompt user for input. If the second argument is present, return\n\ |
529
|
1128 value as a string.") |
|
1129 { |
2086
|
1130 octave_value_list retval; |
529
|
1131 |
|
1132 int nargin = args.length (); |
|
1133 |
712
|
1134 if (nargin == 1 || nargin == 2) |
1488
|
1135 retval = get_user_input (args); |
529
|
1136 else |
|
1137 print_usage ("input"); |
|
1138 |
|
1139 return retval; |
|
1140 } |
|
1141 |
1957
|
1142 DEFUN (keyboard, args, , |
529
|
1143 "keyboard (PROMPT)\n\ |
|
1144 \n\ |
|
1145 maybe help in debugging function files") |
|
1146 { |
2086
|
1147 octave_value_list retval; |
529
|
1148 |
|
1149 int nargin = args.length (); |
|
1150 |
712
|
1151 if (nargin == 0 || nargin == 1) |
1488
|
1152 retval = get_user_input (args, 1); |
529
|
1153 else |
|
1154 print_usage ("keyboard"); |
|
1155 |
|
1156 return retval; |
|
1157 } |
|
1158 |
1957
|
1159 DEFUN_TEXT(echo, args, , |
1588
|
1160 "echo [options]\n\ |
|
1161 \n\ |
|
1162 echo [on|off] -- enable or disable echoing of commands as\n\ |
|
1163 they are executed in script files\n\ |
|
1164 \n\ |
|
1165 echo [on all|off all] -- enable or disable echoing of commands as they\n\ |
|
1166 are executed in script files and functions\n\ |
|
1167 \n\ |
|
1168 Without any arguments, toggle the current echo state.") |
|
1169 { |
2086
|
1170 octave_value_list retval; |
1588
|
1171 |
1755
|
1172 int argc = args.length () + 1; |
|
1173 |
1968
|
1174 string_vector argv = args.make_argv ("echo"); |
1755
|
1175 |
|
1176 if (error_state) |
|
1177 return retval; |
1588
|
1178 |
|
1179 switch (argc) |
|
1180 { |
|
1181 case 1: |
|
1182 { |
2205
|
1183 if ((Vecho_executing_commands & ECHO_SCRIPTS) |
|
1184 || (Vecho_executing_commands & ECHO_FUNCTIONS)) |
2431
|
1185 bind_builtin_variable ("echo_executing_commands", |
2800
|
1186 static_cast<double> (ECHO_OFF)); |
1588
|
1187 else |
2431
|
1188 bind_builtin_variable ("echo_executing_commands", |
2800
|
1189 static_cast<double> (ECHO_SCRIPTS)); |
1588
|
1190 } |
|
1191 break; |
|
1192 |
|
1193 case 2: |
|
1194 { |
1755
|
1195 string arg = argv[1]; |
|
1196 |
|
1197 if (arg == "on") |
2431
|
1198 bind_builtin_variable ("echo_executing_commands", |
2800
|
1199 static_cast<double> (ECHO_SCRIPTS)); |
1755
|
1200 else if (arg == "off") |
2431
|
1201 bind_builtin_variable ("echo_executing_commands", |
2800
|
1202 static_cast<double> (ECHO_OFF)); |
1588
|
1203 else |
|
1204 print_usage ("echo"); |
|
1205 } |
|
1206 break; |
|
1207 |
|
1208 case 3: |
|
1209 { |
1755
|
1210 string arg = argv[1]; |
|
1211 |
|
1212 if (arg == "on" && argv[2] == "all") |
2800
|
1213 { |
|
1214 int tmp = (ECHO_SCRIPTS | ECHO_FUNCTIONS); |
|
1215 bind_builtin_variable ("echo_executing_commands", |
|
1216 static_cast<double> (tmp)); |
|
1217 } |
1755
|
1218 else if (arg == "off" && argv[2] == "all") |
2431
|
1219 bind_builtin_variable ("echo_executing_commands", |
2800
|
1220 static_cast<double> (ECHO_OFF)); |
1588
|
1221 else |
|
1222 print_usage ("echo"); |
|
1223 } |
|
1224 break; |
|
1225 |
|
1226 default: |
|
1227 print_usage ("echo"); |
|
1228 break; |
|
1229 } |
|
1230 |
|
1231 return retval; |
|
1232 } |
|
1233 |
2234
|
1234 DEFUN (completion_matches, args, nargout, |
2299
|
1235 "completion_matches (HINT): generate possible completions given HINT\n\ |
|
1236 \n\ |
|
1237 This function is provided for the benefit of programs like Emacs which\n\ |
|
1238 might be controlling Octave and handling user input. The current command\n\ |
|
1239 number is not incremented when this function is called. This is a feature,\n\ |
|
1240 not a bug.") |
2234
|
1241 { |
2281
|
1242 octave_value retval; |
2234
|
1243 |
|
1244 int nargin = args.length (); |
|
1245 |
|
1246 if (nargin == 1) |
|
1247 { |
|
1248 string hint_string = args(0).string_value (); |
|
1249 |
|
1250 if (! error_state) |
|
1251 { |
|
1252 int n = 32; |
|
1253 |
|
1254 string_vector list (n); |
|
1255 |
|
1256 const char *hint = hint_string.c_str (); |
|
1257 |
|
1258 int k = 0; |
|
1259 |
|
1260 for (;;) |
|
1261 { |
|
1262 const char *cmd = command_generator (hint, k); |
|
1263 |
|
1264 if (cmd) |
|
1265 { |
2235
|
1266 if (*cmd) |
2234
|
1267 { |
2235
|
1268 if (k == n) |
|
1269 { |
|
1270 n *= 2; |
|
1271 list.resize (n); |
|
1272 } |
|
1273 |
|
1274 list[k++] = cmd; |
2234
|
1275 } |
|
1276 } |
|
1277 else |
|
1278 { |
|
1279 list.resize (k); |
|
1280 break; |
|
1281 } |
|
1282 } |
|
1283 |
|
1284 if (nargout > 0) |
2281
|
1285 { |
|
1286 if (! list.empty ()) |
|
1287 retval = list; |
2282
|
1288 else |
|
1289 retval = ""; |
2281
|
1290 } |
2234
|
1291 else |
|
1292 { |
2235
|
1293 // We don't use string_vector::list_in_columns here |
|
1294 // because it will be easier for Emacs if the names |
|
1295 // appear in a single column. |
|
1296 |
2234
|
1297 int len = list.length (); |
|
1298 |
|
1299 for (int i = 0; i < len; i++) |
|
1300 octave_stdout << list[i] << "\n"; |
|
1301 } |
2299
|
1302 |
|
1303 octave_completion_matches_called = true; |
2234
|
1304 } |
|
1305 } |
|
1306 else |
|
1307 print_usage ("completion_matches"); |
|
1308 |
|
1309 return retval; |
|
1310 } |
|
1311 |
2181
|
1312 static int |
|
1313 ps1 (void) |
|
1314 { |
|
1315 int status = 0; |
|
1316 |
|
1317 Vps1 = builtin_string_variable ("PS1"); |
|
1318 |
|
1319 return status; |
|
1320 } |
|
1321 |
|
1322 static int |
|
1323 ps2 (void) |
|
1324 { |
|
1325 int status = 0; |
|
1326 |
|
1327 Vps2 = builtin_string_variable ("PS2"); |
|
1328 |
|
1329 return status; |
|
1330 } |
|
1331 |
|
1332 static int |
|
1333 ps4 (void) |
|
1334 { |
|
1335 int status = 0; |
|
1336 |
|
1337 Vps4 = builtin_string_variable ("PS4"); |
|
1338 |
|
1339 return status; |
|
1340 } |
|
1341 |
|
1342 static int |
|
1343 completion_append_char (void) |
|
1344 { |
|
1345 int status = 0; |
|
1346 |
|
1347 string s = builtin_string_variable ("completion_append_char"); |
|
1348 |
|
1349 switch (s.length ()) |
|
1350 { |
|
1351 case 1: |
|
1352 Vcompletion_append_char = s[0]; |
|
1353 break; |
|
1354 |
|
1355 case 0: |
|
1356 Vcompletion_append_char = '\0'; |
|
1357 break; |
|
1358 |
|
1359 default: |
|
1360 warning ("completion_append_char must be a single character"); |
|
1361 status = -1; |
|
1362 break; |
|
1363 } |
|
1364 |
|
1365 return status; |
|
1366 } |
|
1367 |
|
1368 void |
|
1369 symbols_of_input (void) |
|
1370 { |
|
1371 DEFVAR (PS1, "\\s:\\#> ", 0, ps1, |
|
1372 "primary prompt string"); |
|
1373 |
|
1374 DEFVAR (PS2, "> ", 0, ps2, |
|
1375 "secondary prompt string"); |
|
1376 |
|
1377 DEFVAR (PS4, "+ ", 0, ps4, |
|
1378 "string printed before echoed input (enabled by --echo-input)"); |
|
1379 |
|
1380 DEFVAR (completion_append_char, " ", 0, completion_append_char, |
|
1381 "the string to append after successful command-line completion attempts"); |
|
1382 } |
|
1383 |
1
|
1384 /* |
|
1385 ;;; Local Variables: *** |
|
1386 ;;; mode: C++ *** |
|
1387 ;;; End: *** |
|
1388 */ |