1
|
1 /* |
|
2 |
1884
|
3 Copyright (C) 1996 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 |
1
|
57 // This must come before anything that includes iostream.h... |
1350
|
58 // (This is apparently no longer true...) |
1466
|
59 |
1
|
60 #include "readline/readline.h" |
269
|
61 #include "readline/history.h" |
1
|
62 |
1755
|
63 #include "str-vec.h" |
|
64 |
1352
|
65 #include "defun.h" |
|
66 #include "dirfns.h" |
1
|
67 #include "error.h" |
2181
|
68 #include "gripes.h" |
1352
|
69 #include "help.h" |
1
|
70 #include "input.h" |
1352
|
71 #include "oct-map.h" |
1742
|
72 #include "oct-hist.h" |
1670
|
73 #include "toplev.h" |
1755
|
74 #include "oct-obj.h" |
1
|
75 #include "pager.h" |
529
|
76 #include "parse.h" |
1352
|
77 #include "pathlen.h" |
1755
|
78 #include "pt-const.h" |
1352
|
79 #include "sighandlers.h" |
|
80 #include "symtab.h" |
1114
|
81 #include "sysdep.h" |
1352
|
82 #include "utils.h" |
|
83 #include "variables.h" |
529
|
84 |
2181
|
85 // Primary prompt string. |
|
86 static string Vps1; |
|
87 |
|
88 // Secondary prompt string. |
|
89 static string Vps2; |
|
90 |
|
91 // String printed before echoed input (enabled by --echo-input). |
|
92 string Vps4; |
|
93 |
|
94 // Character to append after successful command-line completion attempts. |
|
95 static char Vcompletion_append_char; |
|
96 |
1
|
97 // Global pointer for eval(). |
1755
|
98 string current_eval_string; |
1
|
99 |
|
100 // Nonzero means get input from current_eval_string. |
|
101 int get_input_from_eval_string = 0; |
|
102 |
338
|
103 // Nonzero means we're parsing a function file. |
|
104 int reading_fcn_file = 0; |
1
|
105 |
338
|
106 // Simple name of function file we are reading. |
1755
|
107 string curr_fcn_file_name; |
1606
|
108 |
|
109 // Full name of file we are reading. |
1755
|
110 string curr_fcn_file_full_name; |
1
|
111 |
|
112 // Nonzero means we're parsing a script file. |
|
113 int reading_script_file = 0; |
|
114 |
|
115 // If we are reading from an M-file, this is it. |
529
|
116 FILE *ff_instream = 0; |
1
|
117 |
|
118 // Nonzero means this is an interactive shell. |
|
119 int interactive = 0; |
|
120 |
|
121 // Nonzero means the user forced this shell to be interactive (-i). |
|
122 int forced_interactive = 0; |
|
123 |
|
124 // Should we issue a prompt? |
|
125 int promptflag = 1; |
|
126 |
|
127 // The current line of input, from wherever. |
1755
|
128 string current_input_line; |
1
|
129 |
2299
|
130 // TRUE after a call to completion_matches(). |
|
131 bool octave_completion_matches_called = false; |
|
132 |
581
|
133 // Return the octal number parsed from STRING, or -1 to indicate that |
|
134 // the string contained a bad number. |
|
135 |
1822
|
136 static int |
1755
|
137 read_octal (const string& s) |
529
|
138 { |
|
139 int result = 0; |
|
140 int digits = 0; |
|
141 |
1755
|
142 size_t i = 0; |
|
143 size_t slen = s.length (); |
|
144 |
|
145 while (i < slen && s[i] >= '0' && s[i] < '8') |
529
|
146 { |
|
147 digits++; |
1755
|
148 result = (result * 8) + s[i] - '0'; |
|
149 i++; |
529
|
150 } |
|
151 |
1755
|
152 if (! digits || result > 0777 || i < slen) |
529
|
153 result = -1; |
|
154 |
|
155 return result; |
|
156 } |
|
157 |
581
|
158 // Return a string which will be printed as a prompt. The string may |
|
159 // contain special characters which are decoded as follows: |
|
160 // |
|
161 // \t the time |
|
162 // \d the date |
|
163 // \n CRLF |
|
164 // \s the name of the shell (program) |
|
165 // \w the current working directory |
|
166 // \W the last element of PWD |
|
167 // \u your username |
|
168 // \h the hostname |
|
169 // \# the command number of this command |
|
170 // \! the history number of this command |
|
171 // \$ a $ or a # if you are root |
|
172 // \<octal> character code in octal |
|
173 // \\ a backslash |
|
174 |
1755
|
175 static string |
|
176 decode_prompt_string (const string& s) |
529
|
177 { |
1755
|
178 string result; |
|
179 string temp; |
|
180 size_t i = 0; |
|
181 size_t slen = s.length (); |
529
|
182 int c; |
|
183 |
1755
|
184 while (i < slen) |
529
|
185 { |
1755
|
186 c = s[i]; |
|
187 |
|
188 i++; |
|
189 |
529
|
190 if (c == '\\') |
|
191 { |
1755
|
192 c = s[i]; |
529
|
193 |
|
194 switch (c) |
|
195 { |
|
196 case '0': |
|
197 case '1': |
|
198 case '2': |
|
199 case '3': |
|
200 case '4': |
|
201 case '5': |
|
202 case '6': |
|
203 case '7': |
1755
|
204 // Maybe convert an octal number. |
529
|
205 { |
1755
|
206 int n = read_octal (s.substr (i, 3)); |
529
|
207 |
1755
|
208 temp = "\\"; |
529
|
209 |
|
210 if (n != -1) |
|
211 { |
1755
|
212 i += 3; |
529
|
213 temp[0] = n; |
|
214 } |
|
215 |
|
216 c = 0; |
|
217 goto add_string; |
|
218 } |
|
219 |
|
220 case 't': |
|
221 case 'd': |
1755
|
222 // Make the current time/date into a string. |
529
|
223 { |
1755
|
224 time_t now = time (0); |
|
225 |
|
226 temp = ctime (&now); |
529
|
227 |
|
228 if (c == 't') |
|
229 { |
1755
|
230 temp = temp.substr (11); |
|
231 temp.resize (8); |
529
|
232 } |
|
233 else |
1755
|
234 temp.resize (10); |
529
|
235 |
|
236 goto add_string; |
|
237 } |
1
|
238 |
529
|
239 case 'n': |
1755
|
240 { |
1822
|
241 if (using_readline) |
1755
|
242 temp = "\r\n"; |
|
243 else |
|
244 temp = "\n"; |
|
245 |
|
246 goto add_string; |
|
247 } |
529
|
248 |
|
249 case 's': |
|
250 { |
2205
|
251 temp = base_pathname (Vprogram_name); |
1755
|
252 |
529
|
253 goto add_string; |
|
254 } |
|
255 |
|
256 case 'w': |
|
257 case 'W': |
|
258 { |
|
259 #define EFFICIENT |
|
260 #ifdef EFFICIENT |
1358
|
261 // Use the value of PWD because it is much more |
|
262 // effecient. |
529
|
263 |
2205
|
264 temp = Vcurrent_directory; |
529
|
265 |
1755
|
266 if (temp.empty ()) |
|
267 temp = octave_getcwd (); |
529
|
268 #else |
1755
|
269 temp = octave_getcwd (); |
529
|
270 #endif /* EFFICIENT */ |
|
271 |
|
272 if (c == 'W') |
|
273 { |
1755
|
274 size_t pos = temp.rfind ('/'); |
|
275 |
|
276 if (pos != NPOS && pos != 0) |
|
277 temp = temp.substr (pos + 1); |
529
|
278 } |
|
279 else |
1755
|
280 temp = polite_directory_format (temp); |
|
281 |
529
|
282 goto add_string; |
|
283 } |
|
284 |
|
285 case 'u': |
|
286 { |
2205
|
287 temp = Vuser_name; |
529
|
288 |
|
289 goto add_string; |
|
290 } |
|
291 |
2327
|
292 case 'H': |
529
|
293 { |
2205
|
294 temp = Vhost_name; |
529
|
295 |
2287
|
296 goto add_string; |
|
297 } |
|
298 |
2327
|
299 case 'h': |
2287
|
300 { |
|
301 temp = Vhost_name; |
|
302 |
1755
|
303 size_t pos = temp.find ('.'); |
|
304 |
|
305 if (pos != NPOS) |
|
306 temp.resize (pos); |
529
|
307 |
|
308 goto add_string; |
|
309 } |
|
310 |
|
311 case '#': |
|
312 { |
|
313 char number_buffer[128]; |
|
314 sprintf (number_buffer, "%d", current_command_number); |
1755
|
315 temp = number_buffer; |
|
316 |
529
|
317 goto add_string; |
|
318 } |
|
319 |
|
320 case '!': |
|
321 { |
|
322 char number_buffer[128]; |
1799
|
323 int num = octave_command_history.current_number (); |
529
|
324 if (num > 0) |
|
325 sprintf (number_buffer, "%d", num); |
|
326 else |
|
327 strcpy (number_buffer, "!"); |
1755
|
328 temp = number_buffer; |
|
329 |
529
|
330 goto add_string; |
|
331 } |
|
332 |
|
333 case '$': |
1755
|
334 { |
|
335 temp = (geteuid () == 0 ? "#" : "$"); |
|
336 |
|
337 goto add_string; |
|
338 } |
529
|
339 |
1568
|
340 case '[': |
|
341 case ']': |
1755
|
342 { |
|
343 temp.resize (2); |
|
344 |
|
345 temp[0] = '\001'; |
|
346 temp[1] = ((c == '[') |
|
347 ? RL_PROMPT_START_IGNORE |
|
348 : RL_PROMPT_END_IGNORE); |
|
349 |
|
350 goto add_string; |
|
351 } |
1568
|
352 |
529
|
353 case '\\': |
1755
|
354 { |
|
355 temp = "\\"; |
|
356 |
|
357 goto add_string; |
|
358 } |
529
|
359 |
|
360 default: |
1755
|
361 { |
|
362 temp = "\\ "; |
|
363 temp[1] = c; |
|
364 |
|
365 goto add_string; |
|
366 } |
529
|
367 |
|
368 add_string: |
1755
|
369 { |
|
370 if (c) |
|
371 i++; |
|
372 |
|
373 result.append (temp); |
|
374 |
|
375 break; |
|
376 } |
529
|
377 } |
|
378 } |
|
379 else |
1755
|
380 result += c; |
529
|
381 } |
|
382 |
|
383 return result; |
|
384 } |
581
|
385 |
1044
|
386 static void |
1755
|
387 do_input_echo (const string& input_string) |
1044
|
388 { |
1588
|
389 int do_echo = reading_script_file ? |
2205
|
390 (Vecho_executing_commands & ECHO_SCRIPTS) |
|
391 : (Vecho_executing_commands & ECHO_CMD_LINE); |
1588
|
392 |
|
393 if (do_echo) |
1044
|
394 { |
1403
|
395 if (forced_interactive) |
|
396 { |
1755
|
397 if (promptflag > 0) |
2181
|
398 octave_stdout << decode_prompt_string (Vps1); |
1755
|
399 else |
2181
|
400 octave_stdout << decode_prompt_string (Vps2); |
1403
|
401 } |
|
402 else |
2181
|
403 octave_stdout << decode_prompt_string (Vps4); |
1044
|
404 |
1755
|
405 if (! input_string.empty ()) |
1044
|
406 { |
2095
|
407 octave_stdout << input_string; |
1755
|
408 |
|
409 if (input_string[input_string.length () - 1] != '\n') |
2095
|
410 octave_stdout << "\n"; |
1044
|
411 } |
|
412 } |
|
413 } |
|
414 |
1822
|
415 char * |
2275
|
416 gnu_readline (const char *s, bool force_readline) |
1822
|
417 { |
|
418 char *retval = 0; |
|
419 |
2275
|
420 if (using_readline || force_readline) |
1822
|
421 { |
1898
|
422 char *tmp = retval = ::readline (s); |
|
423 |
1900
|
424 if (tmp && strlen (tmp) == 0) |
1898
|
425 { |
1900
|
426 retval = (char *) malloc (2); |
|
427 retval[0] = '\n'; |
|
428 retval[1] = '\0'; |
1898
|
429 } |
1822
|
430 } |
|
431 else |
|
432 { |
|
433 if (s && *s && (interactive || forced_interactive)) |
|
434 fprintf (rl_outstream, s); |
|
435 |
|
436 FILE *curr_stream = rl_instream; |
|
437 if (reading_fcn_file || reading_script_file) |
|
438 curr_stream = ff_instream; |
|
439 |
|
440 int grow_size = 1024; |
|
441 int max_size = grow_size; |
|
442 |
|
443 char *buf = (char *) malloc (max_size); |
|
444 char *bufptr = buf; |
|
445 |
|
446 do |
|
447 { |
|
448 if (fgets (bufptr, grow_size, curr_stream)) |
|
449 { |
|
450 int len = strlen (bufptr); |
|
451 |
|
452 if (len == grow_size - 1) |
|
453 { |
|
454 int tmp = bufptr - buf + grow_size - 1; |
|
455 grow_size *= 2; |
|
456 max_size += grow_size; |
|
457 buf = (char *) realloc (buf, max_size); |
|
458 bufptr = buf + tmp; |
|
459 |
|
460 if (*(bufptr-1) == '\n') |
|
461 { |
|
462 *bufptr = '\0'; |
|
463 retval = buf; |
|
464 } |
|
465 } |
|
466 else if (bufptr[len-1] != '\n') |
|
467 { |
|
468 bufptr[len++] = '\n'; |
|
469 bufptr[len] = '\0'; |
|
470 retval = buf; |
|
471 } |
|
472 else |
|
473 retval = buf; |
|
474 } |
|
475 else |
|
476 break; |
|
477 } |
|
478 while (! retval); |
|
479 } |
|
480 |
|
481 return retval; |
|
482 } |
581
|
483 |
287
|
484 static char * |
1
|
485 octave_gets (void) |
|
486 { |
1822
|
487 char *retval = 0; |
1
|
488 |
1822
|
489 if ((interactive || forced_interactive) |
|
490 && (! (reading_fcn_file || reading_script_file))) |
1
|
491 { |
2181
|
492 const char *ps = (promptflag > 0) ? Vps1.c_str () : |
|
493 Vps2.c_str (); |
1755
|
494 |
1761
|
495 string prompt = decode_prompt_string (ps); |
1
|
496 |
|
497 if (interactive) |
|
498 { |
|
499 pipe_handler_error_count = 0; |
2095
|
500 flush_octave_stdout (); |
1
|
501 } |
|
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 |
1822
|
541 retval = (char *) malloc (len + 2); |
|
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 |
|
676 symbol_record *sr = curr_sym_tab->lookup (id, 0, 0); |
|
677 if (! sr) |
|
678 sr = global_sym_tab->lookup (id, 0, 0); |
|
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 ()) |
2371
|
686 def = (tree_constant *) tmp_fvc; |
1280
|
687 |
|
688 if (def && def->is_map ()) |
|
689 { |
|
690 if (elts && *elts) |
|
691 { |
2086
|
692 octave_value ult = def->lookup_map_element (elts, 0, 1); |
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 |
|
770 symbol_record *sr = curr_sym_tab->lookup (id, 0, 0); |
|
771 if (! sr) |
|
772 sr = global_sym_tab->lookup (id, 0, 0); |
|
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 ()) |
2371
|
780 def = (tree_constant *) tmp_fvc; |
1280
|
781 |
|
782 if (def && def->is_map ()) |
|
783 { |
|
784 if (elts && *elts) |
|
785 { |
2086
|
786 octave_value ult = def->lookup_map_element (elts, 0, 1); |
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); |
1466
|
861 char *buf = (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; |
|
955 rl_startup_hook = (Function *) set_saved_history; |
|
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 |
271
|
972 rl_attempted_completion_function = (CPPFunction *) command_completer; |
269
|
973 |
1358
|
974 // Bind operate-and-get-next. |
|
975 |
269
|
976 rl_add_defun ("operate-and-get-next", |
|
977 (Function *) operate_and_get_next, CTRL ('O')); |
1441
|
978 |
1569
|
979 |
|
980 // And the history search functions. |
|
981 |
|
982 rl_add_defun ("history-search-backward", |
|
983 (Function *) rl_history_search_backward, META ('p')); |
|
984 |
|
985 rl_add_defun ("history-search-forward", |
|
986 (Function *) rl_history_search_forward, META ('n')); |
|
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\ |
|
1105 Prompt user for input. If the second argument is present, return |
|
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", |
|
1164 (double) ECHO_OFF); |
1588
|
1165 else |
2431
|
1166 bind_builtin_variable ("echo_executing_commands", |
|
1167 (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", |
|
1177 (double) ECHO_SCRIPTS); |
1755
|
1178 else if (arg == "off") |
2431
|
1179 bind_builtin_variable ("echo_executing_commands", |
|
1180 (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") |
1588
|
1191 bind_builtin_variable ("echo_executing_commands", |
2431
|
1192 (double) (ECHO_SCRIPTS | ECHO_FUNCTIONS)); |
1755
|
1193 else if (arg == "off" && argv[2] == "all") |
2431
|
1194 bind_builtin_variable ("echo_executing_commands", |
|
1195 (double) ECHO_OFF); |
1588
|
1196 else |
|
1197 print_usage ("echo"); |
|
1198 } |
|
1199 break; |
|
1200 |
|
1201 default: |
|
1202 print_usage ("echo"); |
|
1203 break; |
|
1204 } |
|
1205 |
|
1206 return retval; |
|
1207 } |
|
1208 |
2234
|
1209 DEFUN (completion_matches, args, nargout, |
2299
|
1210 "completion_matches (HINT): generate possible completions given HINT\n\ |
|
1211 \n\ |
|
1212 This function is provided for the benefit of programs like Emacs which\n\ |
|
1213 might be controlling Octave and handling user input. The current command\n\ |
|
1214 number is not incremented when this function is called. This is a feature,\n\ |
|
1215 not a bug.") |
2234
|
1216 { |
2281
|
1217 octave_value retval; |
2234
|
1218 |
|
1219 int nargin = args.length (); |
|
1220 |
|
1221 if (nargin == 1) |
|
1222 { |
|
1223 string hint_string = args(0).string_value (); |
|
1224 |
|
1225 if (! error_state) |
|
1226 { |
|
1227 int n = 32; |
|
1228 |
|
1229 string_vector list (n); |
|
1230 |
|
1231 const char *hint = hint_string.c_str (); |
|
1232 |
|
1233 int k = 0; |
|
1234 |
|
1235 for (;;) |
|
1236 { |
|
1237 const char *cmd = command_generator (hint, k); |
|
1238 |
|
1239 if (cmd) |
|
1240 { |
2235
|
1241 if (*cmd) |
2234
|
1242 { |
2235
|
1243 if (k == n) |
|
1244 { |
|
1245 n *= 2; |
|
1246 list.resize (n); |
|
1247 } |
|
1248 |
|
1249 list[k++] = cmd; |
2234
|
1250 } |
|
1251 } |
|
1252 else |
|
1253 { |
|
1254 list.resize (k); |
|
1255 break; |
|
1256 } |
|
1257 } |
|
1258 |
|
1259 if (nargout > 0) |
2281
|
1260 { |
|
1261 if (! list.empty ()) |
|
1262 retval = list; |
2282
|
1263 else |
|
1264 retval = ""; |
2281
|
1265 } |
2234
|
1266 else |
|
1267 { |
2235
|
1268 // We don't use string_vector::list_in_columns here |
|
1269 // because it will be easier for Emacs if the names |
|
1270 // appear in a single column. |
|
1271 |
2234
|
1272 int len = list.length (); |
|
1273 |
|
1274 for (int i = 0; i < len; i++) |
|
1275 octave_stdout << list[i] << "\n"; |
|
1276 } |
2299
|
1277 |
|
1278 octave_completion_matches_called = true; |
2234
|
1279 } |
|
1280 } |
|
1281 else |
|
1282 print_usage ("completion_matches"); |
|
1283 |
|
1284 return retval; |
|
1285 } |
|
1286 |
2181
|
1287 static int |
|
1288 ps1 (void) |
|
1289 { |
|
1290 int status = 0; |
|
1291 |
|
1292 Vps1 = builtin_string_variable ("PS1"); |
|
1293 |
|
1294 return status; |
|
1295 } |
|
1296 |
|
1297 static int |
|
1298 ps2 (void) |
|
1299 { |
|
1300 int status = 0; |
|
1301 |
|
1302 Vps2 = builtin_string_variable ("PS2"); |
|
1303 |
|
1304 return status; |
|
1305 } |
|
1306 |
|
1307 static int |
|
1308 ps4 (void) |
|
1309 { |
|
1310 int status = 0; |
|
1311 |
|
1312 Vps4 = builtin_string_variable ("PS4"); |
|
1313 |
|
1314 return status; |
|
1315 } |
|
1316 |
|
1317 static int |
|
1318 completion_append_char (void) |
|
1319 { |
|
1320 int status = 0; |
|
1321 |
|
1322 string s = builtin_string_variable ("completion_append_char"); |
|
1323 |
|
1324 switch (s.length ()) |
|
1325 { |
|
1326 case 1: |
|
1327 Vcompletion_append_char = s[0]; |
|
1328 break; |
|
1329 |
|
1330 case 0: |
|
1331 Vcompletion_append_char = '\0'; |
|
1332 break; |
|
1333 |
|
1334 default: |
|
1335 warning ("completion_append_char must be a single character"); |
|
1336 status = -1; |
|
1337 break; |
|
1338 } |
|
1339 |
|
1340 return status; |
|
1341 } |
|
1342 |
|
1343 void |
|
1344 symbols_of_input (void) |
|
1345 { |
|
1346 DEFVAR (PS1, "\\s:\\#> ", 0, ps1, |
|
1347 "primary prompt string"); |
|
1348 |
|
1349 DEFVAR (PS2, "> ", 0, ps2, |
|
1350 "secondary prompt string"); |
|
1351 |
|
1352 DEFVAR (PS4, "+ ", 0, ps4, |
|
1353 "string printed before echoed input (enabled by --echo-input)"); |
|
1354 |
|
1355 DEFVAR (completion_append_char, " ", 0, completion_append_char, |
|
1356 "the string to append after successful command-line completion attempts"); |
|
1357 } |
|
1358 |
1
|
1359 /* |
|
1360 ;;; Local Variables: *** |
|
1361 ;;; mode: C++ *** |
|
1362 ;;; End: *** |
|
1363 */ |