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