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