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