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; |
|
447 |
|
448 do |
|
449 { |
|
450 if (fgets (bufptr, grow_size, curr_stream)) |
|
451 { |
|
452 int len = strlen (bufptr); |
|
453 |
|
454 if (len == grow_size - 1) |
|
455 { |
|
456 int tmp = bufptr - buf + grow_size - 1; |
|
457 grow_size *= 2; |
|
458 max_size += grow_size; |
2800
|
459 buf = static_cast<char *> (realloc (buf, max_size)); |
1822
|
460 bufptr = buf + tmp; |
|
461 |
|
462 if (*(bufptr-1) == '\n') |
|
463 { |
|
464 *bufptr = '\0'; |
|
465 retval = buf; |
|
466 } |
|
467 } |
|
468 else if (bufptr[len-1] != '\n') |
|
469 { |
|
470 bufptr[len++] = '\n'; |
|
471 bufptr[len] = '\0'; |
|
472 retval = buf; |
|
473 } |
|
474 else |
|
475 retval = buf; |
|
476 } |
|
477 else |
|
478 break; |
|
479 } |
|
480 while (! retval); |
|
481 } |
|
482 |
|
483 return retval; |
|
484 } |
581
|
485 |
287
|
486 static char * |
1
|
487 octave_gets (void) |
|
488 { |
1822
|
489 char *retval = 0; |
1
|
490 |
1822
|
491 if ((interactive || forced_interactive) |
|
492 && (! (reading_fcn_file || reading_script_file))) |
1
|
493 { |
2181
|
494 const char *ps = (promptflag > 0) ? Vps1.c_str () : |
|
495 Vps2.c_str (); |
1755
|
496 |
1761
|
497 string prompt = decode_prompt_string (ps); |
1
|
498 |
2618
|
499 pipe_handler_error_count = 0; |
|
500 |
|
501 flush_octave_stdout (); |
1
|
502 |
2095
|
503 octave_diary << prompt; |
581
|
504 |
1822
|
505 retval = gnu_readline (prompt.c_str ()); |
1
|
506 } |
|
507 else |
1822
|
508 retval = gnu_readline (""); |
1
|
509 |
1822
|
510 if (retval) |
|
511 current_input_line = retval; |
|
512 else |
|
513 current_input_line = ""; |
1
|
514 |
1822
|
515 if (! current_input_line.empty ()) |
1
|
516 { |
1799
|
517 if (! input_from_startup_file) |
1822
|
518 octave_command_history.add (current_input_line); |
1
|
519 |
2095
|
520 octave_diary << current_input_line; |
581
|
521 |
1822
|
522 do_input_echo (current_input_line); |
1
|
523 } |
581
|
524 |
2095
|
525 octave_diary << "\n"; |
581
|
526 |
1822
|
527 return retval; |
1
|
528 } |
|
529 |
581
|
530 // Read a line from the input stream. |
|
531 |
1822
|
532 static char * |
|
533 get_user_input (void) |
1
|
534 { |
1822
|
535 char *retval = 0; |
1
|
536 |
|
537 if (get_input_from_eval_string) |
|
538 { |
1755
|
539 size_t len = current_eval_string.length (); |
|
540 |
2800
|
541 retval = static_cast<char *> (malloc (len + 2)); |
1822
|
542 |
|
543 strcpy (retval, current_eval_string.c_str ()); |
|
544 |
|
545 retval[len++] = '\n'; |
|
546 retval[len] = '\0'; // Paranoia. |
|
547 } |
|
548 else |
|
549 retval = octave_gets (); |
|
550 |
|
551 if (retval) |
|
552 current_input_line = retval; |
|
553 |
2114
|
554 if (! get_input_from_eval_string) |
|
555 input_line_number++; |
1822
|
556 |
|
557 return retval; |
|
558 } |
|
559 |
|
560 int |
|
561 octave_read (char *buf, unsigned max_size) |
|
562 { |
|
563 static char *input_buf = 0; |
|
564 static char *cur_pos = 0; |
|
565 static int chars_left = 0; |
|
566 |
|
567 int status = 0; |
|
568 |
|
569 if (! input_buf) |
|
570 { |
|
571 cur_pos = input_buf = get_user_input (); |
|
572 |
|
573 chars_left = input_buf ? strlen (input_buf) : 0; |
|
574 } |
|
575 |
|
576 if (chars_left > 0) |
|
577 { |
|
578 buf[0] = '\0'; |
|
579 |
|
580 int len = max_size - 2; |
|
581 |
|
582 strncpy (buf, cur_pos, len); |
|
583 |
|
584 if (chars_left > len) |
1
|
585 { |
1822
|
586 chars_left -= len; |
|
587 |
|
588 cur_pos += len; |
|
589 |
|
590 buf[len] = '\0'; |
|
591 |
1
|
592 status = len; |
|
593 } |
|
594 else |
|
595 { |
1822
|
596 free (input_buf); |
|
597 input_buf = 0; |
1755
|
598 |
1822
|
599 len = chars_left; |
|
600 |
|
601 if (buf[len-1] != '\n') |
|
602 buf[len++] = '\n'; |
1
|
603 |
1822
|
604 buf[len] = '\0'; |
1755
|
605 |
1822
|
606 status = len; |
1
|
607 } |
1044
|
608 } |
1822
|
609 else if (chars_left == 0) |
|
610 status = 0; |
|
611 else |
|
612 status = -1; |
1044
|
613 |
1
|
614 return status; |
|
615 } |
|
616 |
581
|
617 // Fix things up so that input can come from file `name', printing a |
|
618 // warning if the file doesn't exist. |
|
619 |
1
|
620 FILE * |
1750
|
621 get_input_from_file (const string& name, int warn) |
1
|
622 { |
529
|
623 FILE *instream = 0; |
1
|
624 |
1750
|
625 if (name.length () > 0) |
|
626 instream = fopen (name.c_str (), "r"); |
1
|
627 |
529
|
628 if (! instream && warn) |
1750
|
629 warning ("%s: no such file or directory", name.c_str ()); |
1
|
630 |
338
|
631 if (reading_fcn_file || reading_script_file) |
339
|
632 ff_instream = instream; |
1
|
633 else |
|
634 rl_instream = instream; |
|
635 |
|
636 return instream; |
|
637 } |
|
638 |
581
|
639 // Fix things up so that input can come from the standard input. This |
|
640 // may need to become much more complicated, which is why it's in a |
|
641 // separate function. |
|
642 |
1
|
643 FILE * |
|
644 get_input_from_stdin (void) |
|
645 { |
|
646 rl_instream = stdin; |
|
647 return rl_instream; |
|
648 } |
|
649 |
2247
|
650 static string_vector |
1280
|
651 generate_struct_completions (const char *text, char *& prefix, |
|
652 char *& hint) |
|
653 { |
2247
|
654 string_vector names; |
1280
|
655 |
1288
|
656 assert (text); |
|
657 |
1280
|
658 char *id = strsave (text); |
|
659 char *ptr = strchr (id, '.'); |
|
660 *ptr = '\0'; |
|
661 |
|
662 char *elts = ptr + 1; |
|
663 ptr = strrchr (elts, '.'); |
|
664 if (ptr) |
|
665 *ptr = '\0'; |
|
666 else |
|
667 elts = 0; |
|
668 |
|
669 prefix = strsave (text); |
|
670 ptr = strrchr (prefix, '.'); |
|
671 *ptr = '\0'; |
|
672 |
1288
|
673 delete [] hint; |
1280
|
674 hint = strsave (ptr + 1); |
|
675 |
2856
|
676 symbol_record *sr = curr_sym_tab->lookup (id); |
1280
|
677 if (! sr) |
2856
|
678 sr = global_sym_tab->lookup (id); |
1280
|
679 |
|
680 if (sr && sr->is_defined ()) |
|
681 { |
|
682 tree_fvc *tmp_fvc = sr->def (); |
|
683 |
2371
|
684 tree_constant *def = 0; |
1280
|
685 if (tmp_fvc->is_constant ()) |
2800
|
686 def = static_cast<tree_constant *> (tmp_fvc); |
1280
|
687 |
|
688 if (def && def->is_map ()) |
|
689 { |
|
690 if (elts && *elts) |
|
691 { |
2856
|
692 octave_value ult = def->lookup_map_element (elts, false, true); |
1280
|
693 |
|
694 if (ult.is_map ()) |
|
695 { |
|
696 Octave_map m = ult.map_value (); |
2247
|
697 names = m.make_name_list (); |
1280
|
698 } |
|
699 } |
|
700 else |
|
701 { |
|
702 Octave_map m = def->map_value (); |
2247
|
703 names = m.make_name_list (); |
1280
|
704 } |
|
705 } |
|
706 } |
|
707 |
|
708 delete [] id; |
|
709 |
|
710 return names; |
|
711 } |
|
712 |
1430
|
713 // XXX FIXME XXX -- make this generate file names when appropriate. |
|
714 |
2247
|
715 static string_vector |
1280
|
716 generate_possible_completions (const char *text, char *& prefix, |
|
717 char *& hint) |
1
|
718 { |
2247
|
719 string_vector names; |
1280
|
720 |
|
721 prefix = 0; |
|
722 |
1430
|
723 if (text && *text && *text != '.' && strrchr (text, '.')) |
|
724 names = generate_struct_completions (text, prefix, hint); |
|
725 else |
2247
|
726 names = make_name_list (); |
1280
|
727 |
2348
|
728 names.qsort (); |
|
729 |
|
730 // Remove duplicates. |
|
731 |
|
732 // XXX FIXME XXX -- maybe this should be defined for all Array objects. |
|
733 |
|
734 int k = 0; |
|
735 |
|
736 int len = names.length (); |
|
737 |
|
738 for (int i = 1; i < len; i++) |
|
739 { |
|
740 if (names[i] != names[k]) |
|
741 { |
|
742 k++; |
|
743 |
|
744 if (k != i) |
|
745 names[k] = names[i]; |
|
746 } |
|
747 } |
|
748 |
|
749 names.resize (k+1); |
|
750 |
1280
|
751 return names; |
|
752 } |
|
753 |
|
754 static int |
|
755 looks_like_struct (const char *nm) |
|
756 { |
|
757 int retval = 0; |
|
758 |
1288
|
759 assert (nm); |
|
760 |
1280
|
761 char *id = strsave (nm); |
|
762 char *elts = 0; |
|
763 char *ptr = strchr (id, '.'); |
|
764 if (ptr) |
|
765 { |
|
766 *ptr = '\0'; |
|
767 elts = ptr + 1; |
|
768 } |
|
769 |
2856
|
770 symbol_record *sr = curr_sym_tab->lookup (id); |
1280
|
771 if (! sr) |
2856
|
772 sr = global_sym_tab->lookup (id); |
1280
|
773 |
|
774 if (sr && sr->is_defined ()) |
|
775 { |
|
776 tree_fvc *tmp_fvc = sr->def (); |
|
777 |
2371
|
778 tree_constant *def = 0; |
1280
|
779 if (tmp_fvc->is_constant ()) |
2800
|
780 def = static_cast<tree_constant *> (tmp_fvc); |
1280
|
781 |
|
782 if (def && def->is_map ()) |
|
783 { |
|
784 if (elts && *elts) |
|
785 { |
2856
|
786 octave_value ult = def->lookup_map_element (elts, false, true); |
1280
|
787 |
|
788 if (ult.is_map ()) |
|
789 retval = 1; |
|
790 } |
|
791 else |
|
792 retval = 1; |
|
793 } |
|
794 } |
|
795 |
|
796 delete [] id; |
|
797 |
|
798 return retval; |
|
799 } |
|
800 |
2235
|
801 // XXX FIXME XXX -- this has to return a pointer to char, but it |
|
802 // should be converted to use a generating function that returns a |
|
803 // string_vector. |
|
804 |
1280
|
805 static char * |
|
806 command_generator (const char *text, int state) |
|
807 { |
|
808 static char *prefix = 0; |
|
809 static char *hint = 0; |
|
810 |
|
811 static int prefix_len = 0; |
|
812 static int hint_len = 0; |
|
813 |
1
|
814 static int list_index = 0; |
2247
|
815 static int list_length = 0; |
|
816 static string_vector name_list; |
1
|
817 |
1280
|
818 static int matches = 0; |
1
|
819 |
|
820 if (state == 0) |
|
821 { |
|
822 list_index = 0; |
|
823 |
1280
|
824 delete [] prefix; |
|
825 prefix = 0; |
|
826 |
|
827 delete [] hint; |
1288
|
828 hint = strsave (text); |
1280
|
829 |
|
830 name_list = generate_possible_completions (text, prefix, hint); |
|
831 |
|
832 prefix_len = 0; |
|
833 if (prefix) |
1288
|
834 prefix_len = strlen (prefix); |
1280
|
835 |
1288
|
836 assert (hint); |
|
837 hint_len = strlen (hint); |
1280
|
838 |
|
839 matches = 0; |
2247
|
840 |
|
841 list_length = name_list.length (); |
|
842 |
|
843 for (int i = 0; i < list_length; i++) |
|
844 if (name_list[i].compare (hint, 0, hint_len)) |
|
845 matches++; |
1
|
846 } |
|
847 |
2247
|
848 if (list_length > 0 && matches > 0) |
1
|
849 { |
1755
|
850 const char *name; |
|
851 |
2247
|
852 while (list_index < list_length) |
244
|
853 { |
2247
|
854 name = name_list[list_index].c_str (); |
|
855 |
1280
|
856 list_index++; |
2247
|
857 |
1280
|
858 if (strncmp (name, hint, hint_len) == 0) |
|
859 { |
1288
|
860 int len = 2 + prefix_len + strlen (name); |
2800
|
861 char *buf = static_cast<char *> (malloc (len)); |
1280
|
862 |
|
863 if (prefix) |
|
864 { |
|
865 strcpy (buf, prefix); |
|
866 strcat (buf, "."); |
|
867 strcat (buf, name); |
|
868 } |
|
869 else |
|
870 strcpy (buf, name); |
|
871 |
|
872 if (matches == 1 && looks_like_struct (buf)) |
|
873 rl_completion_append_character = '.'; |
|
874 else |
2247
|
875 rl_completion_append_character = Vcompletion_append_char; |
1280
|
876 |
|
877 return buf; |
|
878 } |
244
|
879 } |
1
|
880 } |
|
881 |
529
|
882 return 0; |
1
|
883 } |
|
884 |
269
|
885 static char ** |
1488
|
886 command_completer (char *text, int /* start */, int /* end */) |
1
|
887 { |
529
|
888 char **matches = 0; |
1
|
889 matches = completion_matches (text, command_generator); |
|
890 return matches; |
|
891 } |
|
892 |
581
|
893 // The next two functions implement the equivalent of the K*rn shell |
|
894 // C-o operate-and-get-next-history-line editing command. Stolen from |
|
895 // the GNU Bourne Again SHell. |
269
|
896 |
|
897 // ?? |
|
898 static int saved_history_line_to_use = 0; |
|
899 |
|
900 // ?? |
529
|
901 static Function *old_rl_startup_hook = 0; |
269
|
902 |
|
903 static void |
|
904 set_saved_history (void) |
|
905 { |
|
906 HIST_ENTRY *h; |
|
907 |
|
908 if (saved_history_line_to_use) |
|
909 { |
|
910 if (history_set_pos (saved_history_line_to_use)) |
|
911 { |
|
912 h = current_history (); |
|
913 if (h) |
|
914 { |
|
915 rl_insert_text (h->line); |
|
916 |
1358
|
917 // Get rid of any undo list created by the previous |
|
918 // insert, so the line won't totally be erased when the |
|
919 // edits are undone (they will be normally, because this |
|
920 // is a history line -- cf. readline.c: line 380 or |
|
921 // so). |
|
922 |
269
|
923 if (rl_undo_list) |
|
924 { |
|
925 free_undo_list (); |
529
|
926 rl_undo_list = 0; |
269
|
927 } |
|
928 } |
|
929 } |
|
930 } |
|
931 saved_history_line_to_use = 0; |
|
932 rl_startup_hook = old_rl_startup_hook; |
|
933 } |
|
934 |
|
935 static void |
1488
|
936 operate_and_get_next (int /* count */, int /* c */) |
269
|
937 { |
|
938 int where; |
|
939 |
1358
|
940 // Accept the current line. |
|
941 |
269
|
942 rl_newline (); |
|
943 |
1358
|
944 // Find the current line, and find the next line to use. |
|
945 |
269
|
946 where = where_history (); |
|
947 |
1430
|
948 if ((history_is_stifled () && (history_length >= max_input_history)) |
|
949 || (where >= history_length - 1)) |
269
|
950 saved_history_line_to_use = where; |
|
951 else |
|
952 saved_history_line_to_use = where + 1; |
|
953 |
|
954 old_rl_startup_hook = rl_startup_hook; |
2800
|
955 rl_startup_hook = static_cast<Function *> (set_saved_history); |
269
|
956 } |
|
957 |
1
|
958 void |
|
959 initialize_readline (void) |
|
960 { |
1705
|
961 // Set things up internally in case some function that uses readline |
|
962 // (currently Fclc(), maybe others) is called before readline(). |
|
963 |
|
964 rl_initialize (); |
|
965 |
1358
|
966 // Allow conditional parsing of the ~/.inputrc file |
|
967 |
1
|
968 rl_readline_name = "Octave"; |
|
969 |
1358
|
970 // Tell the completer that we want to try first. |
|
971 |
2800
|
972 rl_attempted_completion_function |
|
973 = static_cast<CPPFunction *> (command_completer); |
269
|
974 |
1358
|
975 // Bind operate-and-get-next. |
|
976 |
2800
|
977 rl_add_defun ("operate-and-get-next", operate_and_get_next, CTRL ('O')); |
1441
|
978 |
1569
|
979 |
|
980 // And the history search functions. |
|
981 |
2800
|
982 rl_add_defun ("history-search-backward", rl_history_search_backward, |
|
983 META ('p')); |
1569
|
984 |
2800
|
985 rl_add_defun ("history-search-forward", rl_history_search_forward, |
|
986 META ('n')); |
1569
|
987 |
1441
|
988 // Don't treat single quotes as string delimiters when doing paren |
|
989 // matching. |
|
990 |
|
991 rl_paren_string_delimiters = "\""; |
1
|
992 } |
|
993 |
529
|
994 static int |
|
995 match_sans_spaces (const char *standard, const char *test) |
|
996 { |
1034
|
997 char *tmp = strsave (test); |
|
998 |
|
999 char *tp = tmp; |
529
|
1000 while (*tp == ' ' || *tp == '\t') |
|
1001 tp++; |
|
1002 |
1125
|
1003 char *ep = tmp + strlen (tmp) - 1; |
529
|
1004 while (*ep == ' ' || *ep == '\t') |
|
1005 ep--; |
|
1006 |
1034
|
1007 *(ep+1) = '\0'; |
|
1008 |
|
1009 int retval = strcmp (standard, tp) == 0; |
529
|
1010 |
1034
|
1011 delete [] tmp; |
|
1012 |
|
1013 return retval; |
|
1014 |
529
|
1015 } |
|
1016 |
581
|
1017 // If the user simply hits return, this will produce an empty matrix. |
|
1018 |
2086
|
1019 static octave_value_list |
|
1020 get_user_input (const octave_value_list& args, int debug = 0) |
529
|
1021 { |
2086
|
1022 octave_value retval; |
529
|
1023 |
|
1024 int nargin = args.length (); |
|
1025 |
|
1026 int read_as_string = 0; |
|
1027 |
712
|
1028 if (nargin == 2) |
529
|
1029 read_as_string++; |
|
1030 |
1761
|
1031 string prompt ("debug> "); |
|
1032 |
712
|
1033 if (nargin > 0) |
529
|
1034 { |
1761
|
1035 prompt = args(0).string_value (); |
636
|
1036 |
|
1037 if (error_state) |
|
1038 { |
|
1039 error ("input: unrecognized argument"); |
|
1040 return retval; |
|
1041 } |
529
|
1042 } |
|
1043 |
|
1044 again: |
|
1045 |
2095
|
1046 flush_octave_stdout (); |
529
|
1047 |
2275
|
1048 char *input_buf = gnu_readline (prompt.c_str (), true); |
529
|
1049 |
|
1050 if (input_buf) |
|
1051 { |
1799
|
1052 if (! input_from_startup_file) |
|
1053 octave_command_history.add (input_buf); |
529
|
1054 |
|
1055 int len = strlen (input_buf); |
|
1056 |
|
1057 if (len < 1) |
|
1058 { |
|
1059 if (debug) |
|
1060 goto again; |
|
1061 else |
963
|
1062 { |
|
1063 if (read_as_string) |
|
1064 return ""; |
|
1065 else |
|
1066 return Matrix (); |
|
1067 } |
529
|
1068 } |
|
1069 |
|
1070 if (match_sans_spaces ("exit", input_buf) |
|
1071 || match_sans_spaces ("quit", input_buf) |
|
1072 || match_sans_spaces ("return", input_buf)) |
963
|
1073 { |
|
1074 return retval; |
|
1075 } |
529
|
1076 else if (read_as_string) |
963
|
1077 { |
|
1078 retval = input_buf; |
|
1079 } |
529
|
1080 else |
|
1081 { |
|
1082 int parse_status = 0; |
1488
|
1083 retval = eval_string (input_buf, 0, parse_status); |
581
|
1084 if (retval.is_defined ()) |
|
1085 { |
|
1086 if (debug) |
2371
|
1087 retval.print (); |
581
|
1088 } |
|
1089 else |
963
|
1090 retval = Matrix (); |
529
|
1091 } |
|
1092 } |
|
1093 else |
|
1094 error ("input: reading user-input failed!"); |
|
1095 |
|
1096 if (debug) |
|
1097 goto again; |
|
1098 |
|
1099 return retval; |
|
1100 } |
|
1101 |
1957
|
1102 DEFUN (input, args, , |
529
|
1103 "input (PROMPT [, S])\n\ |
|
1104 \n\ |
2802
|
1105 Prompt user for input. If the second argument is present, return\n\ |
529
|
1106 value as a string.") |
|
1107 { |
2086
|
1108 octave_value_list retval; |
529
|
1109 |
|
1110 int nargin = args.length (); |
|
1111 |
712
|
1112 if (nargin == 1 || nargin == 2) |
1488
|
1113 retval = get_user_input (args); |
529
|
1114 else |
|
1115 print_usage ("input"); |
|
1116 |
|
1117 return retval; |
|
1118 } |
|
1119 |
1957
|
1120 DEFUN (keyboard, args, , |
529
|
1121 "keyboard (PROMPT)\n\ |
|
1122 \n\ |
|
1123 maybe help in debugging function files") |
|
1124 { |
2086
|
1125 octave_value_list retval; |
529
|
1126 |
|
1127 int nargin = args.length (); |
|
1128 |
712
|
1129 if (nargin == 0 || nargin == 1) |
1488
|
1130 retval = get_user_input (args, 1); |
529
|
1131 else |
|
1132 print_usage ("keyboard"); |
|
1133 |
|
1134 return retval; |
|
1135 } |
|
1136 |
1957
|
1137 DEFUN_TEXT(echo, args, , |
1588
|
1138 "echo [options]\n\ |
|
1139 \n\ |
|
1140 echo [on|off] -- enable or disable echoing of commands as\n\ |
|
1141 they are executed in script files\n\ |
|
1142 \n\ |
|
1143 echo [on all|off all] -- enable or disable echoing of commands as they\n\ |
|
1144 are executed in script files and functions\n\ |
|
1145 \n\ |
|
1146 Without any arguments, toggle the current echo state.") |
|
1147 { |
2086
|
1148 octave_value_list retval; |
1588
|
1149 |
1755
|
1150 int argc = args.length () + 1; |
|
1151 |
1968
|
1152 string_vector argv = args.make_argv ("echo"); |
1755
|
1153 |
|
1154 if (error_state) |
|
1155 return retval; |
1588
|
1156 |
|
1157 switch (argc) |
|
1158 { |
|
1159 case 1: |
|
1160 { |
2205
|
1161 if ((Vecho_executing_commands & ECHO_SCRIPTS) |
|
1162 || (Vecho_executing_commands & ECHO_FUNCTIONS)) |
2431
|
1163 bind_builtin_variable ("echo_executing_commands", |
2800
|
1164 static_cast<double> (ECHO_OFF)); |
1588
|
1165 else |
2431
|
1166 bind_builtin_variable ("echo_executing_commands", |
2800
|
1167 static_cast<double> (ECHO_SCRIPTS)); |
1588
|
1168 } |
|
1169 break; |
|
1170 |
|
1171 case 2: |
|
1172 { |
1755
|
1173 string arg = argv[1]; |
|
1174 |
|
1175 if (arg == "on") |
2431
|
1176 bind_builtin_variable ("echo_executing_commands", |
2800
|
1177 static_cast<double> (ECHO_SCRIPTS)); |
1755
|
1178 else if (arg == "off") |
2431
|
1179 bind_builtin_variable ("echo_executing_commands", |
2800
|
1180 static_cast<double> (ECHO_OFF)); |
1588
|
1181 else |
|
1182 print_usage ("echo"); |
|
1183 } |
|
1184 break; |
|
1185 |
|
1186 case 3: |
|
1187 { |
1755
|
1188 string arg = argv[1]; |
|
1189 |
|
1190 if (arg == "on" && argv[2] == "all") |
2800
|
1191 { |
|
1192 int tmp = (ECHO_SCRIPTS | ECHO_FUNCTIONS); |
|
1193 bind_builtin_variable ("echo_executing_commands", |
|
1194 static_cast<double> (tmp)); |
|
1195 } |
1755
|
1196 else if (arg == "off" && argv[2] == "all") |
2431
|
1197 bind_builtin_variable ("echo_executing_commands", |
2800
|
1198 static_cast<double> (ECHO_OFF)); |
1588
|
1199 else |
|
1200 print_usage ("echo"); |
|
1201 } |
|
1202 break; |
|
1203 |
|
1204 default: |
|
1205 print_usage ("echo"); |
|
1206 break; |
|
1207 } |
|
1208 |
|
1209 return retval; |
|
1210 } |
|
1211 |
2234
|
1212 DEFUN (completion_matches, args, nargout, |
2299
|
1213 "completion_matches (HINT): generate possible completions given HINT\n\ |
|
1214 \n\ |
|
1215 This function is provided for the benefit of programs like Emacs which\n\ |
|
1216 might be controlling Octave and handling user input. The current command\n\ |
|
1217 number is not incremented when this function is called. This is a feature,\n\ |
|
1218 not a bug.") |
2234
|
1219 { |
2281
|
1220 octave_value retval; |
2234
|
1221 |
|
1222 int nargin = args.length (); |
|
1223 |
|
1224 if (nargin == 1) |
|
1225 { |
|
1226 string hint_string = args(0).string_value (); |
|
1227 |
|
1228 if (! error_state) |
|
1229 { |
|
1230 int n = 32; |
|
1231 |
|
1232 string_vector list (n); |
|
1233 |
|
1234 const char *hint = hint_string.c_str (); |
|
1235 |
|
1236 int k = 0; |
|
1237 |
|
1238 for (;;) |
|
1239 { |
|
1240 const char *cmd = command_generator (hint, k); |
|
1241 |
|
1242 if (cmd) |
|
1243 { |
2235
|
1244 if (*cmd) |
2234
|
1245 { |
2235
|
1246 if (k == n) |
|
1247 { |
|
1248 n *= 2; |
|
1249 list.resize (n); |
|
1250 } |
|
1251 |
|
1252 list[k++] = cmd; |
2234
|
1253 } |
|
1254 } |
|
1255 else |
|
1256 { |
|
1257 list.resize (k); |
|
1258 break; |
|
1259 } |
|
1260 } |
|
1261 |
|
1262 if (nargout > 0) |
2281
|
1263 { |
|
1264 if (! list.empty ()) |
|
1265 retval = list; |
2282
|
1266 else |
|
1267 retval = ""; |
2281
|
1268 } |
2234
|
1269 else |
|
1270 { |
2235
|
1271 // We don't use string_vector::list_in_columns here |
|
1272 // because it will be easier for Emacs if the names |
|
1273 // appear in a single column. |
|
1274 |
2234
|
1275 int len = list.length (); |
|
1276 |
|
1277 for (int i = 0; i < len; i++) |
|
1278 octave_stdout << list[i] << "\n"; |
|
1279 } |
2299
|
1280 |
|
1281 octave_completion_matches_called = true; |
2234
|
1282 } |
|
1283 } |
|
1284 else |
|
1285 print_usage ("completion_matches"); |
|
1286 |
|
1287 return retval; |
|
1288 } |
|
1289 |
2181
|
1290 static int |
|
1291 ps1 (void) |
|
1292 { |
|
1293 int status = 0; |
|
1294 |
|
1295 Vps1 = builtin_string_variable ("PS1"); |
|
1296 |
|
1297 return status; |
|
1298 } |
|
1299 |
|
1300 static int |
|
1301 ps2 (void) |
|
1302 { |
|
1303 int status = 0; |
|
1304 |
|
1305 Vps2 = builtin_string_variable ("PS2"); |
|
1306 |
|
1307 return status; |
|
1308 } |
|
1309 |
|
1310 static int |
|
1311 ps4 (void) |
|
1312 { |
|
1313 int status = 0; |
|
1314 |
|
1315 Vps4 = builtin_string_variable ("PS4"); |
|
1316 |
|
1317 return status; |
|
1318 } |
|
1319 |
|
1320 static int |
|
1321 completion_append_char (void) |
|
1322 { |
|
1323 int status = 0; |
|
1324 |
|
1325 string s = builtin_string_variable ("completion_append_char"); |
|
1326 |
|
1327 switch (s.length ()) |
|
1328 { |
|
1329 case 1: |
|
1330 Vcompletion_append_char = s[0]; |
|
1331 break; |
|
1332 |
|
1333 case 0: |
|
1334 Vcompletion_append_char = '\0'; |
|
1335 break; |
|
1336 |
|
1337 default: |
|
1338 warning ("completion_append_char must be a single character"); |
|
1339 status = -1; |
|
1340 break; |
|
1341 } |
|
1342 |
|
1343 return status; |
|
1344 } |
|
1345 |
|
1346 void |
|
1347 symbols_of_input (void) |
|
1348 { |
|
1349 DEFVAR (PS1, "\\s:\\#> ", 0, ps1, |
|
1350 "primary prompt string"); |
|
1351 |
|
1352 DEFVAR (PS2, "> ", 0, ps2, |
|
1353 "secondary prompt string"); |
|
1354 |
|
1355 DEFVAR (PS4, "+ ", 0, ps4, |
|
1356 "string printed before echoed input (enabled by --echo-input)"); |
|
1357 |
|
1358 DEFVAR (completion_append_char, " ", 0, completion_append_char, |
|
1359 "the string to append after successful command-line completion attempts"); |
|
1360 } |
|
1361 |
1
|
1362 /* |
|
1363 ;;; Local Variables: *** |
|
1364 ;;; mode: C++ *** |
|
1365 ;;; End: *** |
|
1366 */ |