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