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