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