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