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