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