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 |
1349
|
47 #include <iostream.h> |
|
48 #include <strstream.h> |
|
49 |
529
|
50 #include <sys/types.h> |
|
51 #ifdef HAVE_UNISTD_H |
|
52 #include <unistd.h> |
|
53 #endif |
1343
|
54 |
1
|
55 // This must come before anything that includes iostream.h... |
|
56 extern "C" |
|
57 { |
|
58 #include "readline/readline.h" |
269
|
59 #include "readline/history.h" |
1
|
60 |
271
|
61 extern void free_undo_list (); |
|
62 |
252
|
63 extern char *xmalloc (); |
|
64 |
581
|
65 // Yes, this sucks, but it avoids a conflict with another readline |
|
66 // function declared in iostream.h. |
|
67 |
1
|
68 #if 0 |
|
69 #define LINE_SIZE 8192 |
529
|
70 static int no_line_editing = 0; |
1
|
71 #endif |
|
72 |
|
73 char * |
|
74 gnu_readline (char *s) |
|
75 { |
|
76 #if 0 |
|
77 static int state = 0; |
529
|
78 static char *line_from_stdin = 0; |
1
|
79 if (no_line_editing) |
|
80 { |
|
81 if (! state) |
|
82 { |
|
83 line_from_stdin = (char *) malloc (LINE_SIZE); |
|
84 state = 1; |
|
85 } |
|
86 fputs ("octave> ", stdout); |
|
87 fgets (line_from_stdin, LINE_SIZE, stdin); |
|
88 return line_from_stdin; |
|
89 } |
|
90 else |
|
91 #endif |
|
92 return readline (s); |
|
93 } |
|
94 } |
|
95 |
1349
|
96 #include "pathlen.h" |
529
|
97 #include "help.h" |
1
|
98 #include "error.h" |
|
99 #include "utils.h" |
|
100 #include "input.h" |
|
101 #include "pager.h" |
529
|
102 #include "parse.h" |
|
103 #include "dirfns.h" |
|
104 #include "octave.h" |
1114
|
105 #include "sysdep.h" |
529
|
106 #include "variables.h" |
|
107 #include "tree-const.h" |
1
|
108 #include "octave-hist.h" |
|
109 #include "sighandlers.h" |
|
110 #include "user-prefs.h" |
529
|
111 #include "oct-obj.h" |
1280
|
112 #include "oct-map.h" |
|
113 #include "symtab.h" |
529
|
114 #include "defun.h" |
|
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(). |
529
|
127 const char *current_eval_string = 0; |
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. |
529
|
136 char *curr_fcn_file_name = 0; |
1
|
137 |
|
138 // Nonzero means we're parsing a script file. |
|
139 int reading_script_file = 0; |
|
140 |
|
141 // If we are reading from an M-file, this is it. |
529
|
142 FILE *ff_instream = 0; |
1
|
143 |
|
144 // Nonzero means we are using readline. |
|
145 int using_readline = 1; |
|
146 |
616
|
147 // Nonzero means commands are echoed as they are executed. |
|
148 // (--echo-commands; -x). |
1
|
149 int echo_input = 0; |
|
150 |
|
151 // Nonzero means this is an interactive shell. |
|
152 int interactive = 0; |
|
153 |
|
154 // Nonzero means the user forced this shell to be interactive (-i). |
|
155 int forced_interactive = 0; |
|
156 |
|
157 // Should we issue a prompt? |
|
158 int promptflag = 1; |
|
159 |
|
160 // The current line of input, from wherever. |
529
|
161 char *current_input_line = 0; |
1
|
162 |
|
163 // A line of input from readline. |
529
|
164 static char *octave_gets_line = 0; |
|
165 |
|
166 extern tree_constant eval_string (const char *string, int print, |
|
167 int ans_assign, int& parse_status); |
|
168 |
581
|
169 // Append SOURCE to TARGET at INDEX. SIZE is the current amount of |
|
170 // space allocated to TARGET. SOURCE can be NULL, in which case |
|
171 // nothing happens. Gets rid of SOURCE by free ()ing it. Returns |
|
172 // TARGET in case the location has changed. |
|
173 |
529
|
174 static char * |
|
175 sub_append_string (char *source, char *target, int *index, int *size) |
|
176 { |
|
177 if (source) |
|
178 { |
|
179 while ((int)strlen (source) >= (int)(*size - *index)) |
|
180 { |
|
181 char *tmp = new char [*size += DEFAULT_ARRAY_SIZE]; |
|
182 strcpy (tmp, target); |
|
183 delete [] target; |
|
184 target = tmp; |
|
185 } |
|
186 |
|
187 strcat (target, source); |
|
188 *index += strlen (source); |
|
189 |
|
190 delete [] source; |
|
191 } |
|
192 return target; |
|
193 } |
|
194 |
581
|
195 // Return the octal number parsed from STRING, or -1 to indicate that |
|
196 // the string contained a bad number. |
|
197 |
529
|
198 int |
|
199 read_octal (const char *string) |
|
200 { |
|
201 int result = 0; |
|
202 int digits = 0; |
|
203 |
|
204 while (*string && *string >= '0' && *string < '8') |
|
205 { |
|
206 digits++; |
|
207 result = (result * 8) + *string++ - '0'; |
|
208 } |
|
209 |
|
210 if (! digits || result > 0777 || *string) |
|
211 result = -1; |
|
212 |
|
213 return result; |
|
214 } |
|
215 |
581
|
216 // Return a string which will be printed as a prompt. The string may |
|
217 // contain special characters which are decoded as follows: |
|
218 // |
|
219 // \t the time |
|
220 // \d the date |
|
221 // \n CRLF |
|
222 // \s the name of the shell (program) |
|
223 // \w the current working directory |
|
224 // \W the last element of PWD |
|
225 // \u your username |
|
226 // \h the hostname |
|
227 // \# the command number of this command |
|
228 // \! the history number of this command |
|
229 // \$ a $ or a # if you are root |
|
230 // \<octal> character code in octal |
|
231 // \\ a backslash |
|
232 |
529
|
233 static char * |
|
234 decode_prompt_string (const char *string) |
|
235 { |
|
236 int result_size = PROMPT_GROWTH; |
|
237 int result_index = 0; |
|
238 char *result = new char [PROMPT_GROWTH]; |
|
239 int c; |
|
240 char *temp = 0; |
|
241 |
|
242 result[0] = 0; |
1215
|
243 while ((c = *string++)) |
529
|
244 { |
|
245 if (c == '\\') |
|
246 { |
|
247 c = *string; |
|
248 |
|
249 switch (c) |
|
250 { |
|
251 case '0': |
|
252 case '1': |
|
253 case '2': |
|
254 case '3': |
|
255 case '4': |
|
256 case '5': |
|
257 case '6': |
|
258 case '7': |
|
259 { |
|
260 char octal_string[4]; |
|
261 int n; |
|
262 |
|
263 strncpy (octal_string, string, 3); |
|
264 octal_string[3] = '\0'; |
|
265 |
|
266 n = read_octal (octal_string); |
|
267 |
|
268 temp = strsave ("\\"); |
|
269 if (n != -1) |
|
270 { |
|
271 string += 3; |
|
272 temp[0] = n; |
|
273 } |
|
274 |
|
275 c = 0; |
|
276 goto add_string; |
|
277 } |
|
278 |
|
279 case 't': |
|
280 case 'd': |
|
281 /* Make the current time/date into a string. */ |
|
282 { |
|
283 time_t the_time = time (0); |
|
284 char *ttemp = ctime (&the_time); |
|
285 temp = strsave (ttemp); |
|
286 |
|
287 if (c == 't') |
|
288 { |
|
289 strcpy (temp, temp + 11); |
|
290 temp[8] = '\0'; |
|
291 } |
|
292 else |
|
293 temp[10] = '\0'; |
|
294 |
|
295 goto add_string; |
|
296 } |
1
|
297 |
529
|
298 case 'n': |
|
299 if (! no_line_editing) |
|
300 temp = strsave ("\r\n"); |
|
301 else |
|
302 temp = strsave ("\n"); |
|
303 goto add_string; |
|
304 |
|
305 case 's': |
|
306 { |
|
307 temp = base_pathname (prog_name); |
|
308 temp = strsave (temp); |
|
309 goto add_string; |
|
310 } |
|
311 |
|
312 case 'w': |
|
313 case 'W': |
|
314 { |
|
315 char t_string[MAXPATHLEN]; |
|
316 #define EFFICIENT |
|
317 #ifdef EFFICIENT |
|
318 |
|
319 // Use the value of PWD because it is much more effecient. |
|
320 |
|
321 temp = user_pref.pwd; |
|
322 |
|
323 if (! temp) |
1112
|
324 octave_getcwd (t_string, MAXPATHLEN); |
529
|
325 else |
|
326 strcpy (t_string, temp); |
|
327 #else |
1112
|
328 octave_getcwd (t_string, MAXPATHLEN); |
529
|
329 #endif /* EFFICIENT */ |
|
330 |
|
331 if (c == 'W') |
|
332 { |
|
333 char *dir = strrchr (t_string, '/'); |
|
334 if (dir && dir != t_string) |
|
335 strcpy (t_string, dir + 1); |
|
336 temp = strsave (t_string); |
|
337 } |
|
338 else |
|
339 temp = strsave (polite_directory_format (t_string)); |
|
340 goto add_string; |
|
341 } |
|
342 |
|
343 case 'u': |
|
344 { |
|
345 temp = strsave (user_name); |
|
346 |
|
347 goto add_string; |
|
348 } |
|
349 |
|
350 case 'h': |
|
351 { |
|
352 char *t_string; |
|
353 |
|
354 temp = strsave (host_name); |
1215
|
355 t_string = strchr (temp, '.'); |
|
356 if (t_string); |
529
|
357 *t_string = '\0'; |
|
358 |
|
359 goto add_string; |
|
360 } |
|
361 |
|
362 case '#': |
|
363 { |
|
364 char number_buffer[128]; |
|
365 sprintf (number_buffer, "%d", current_command_number); |
|
366 temp = strsave (number_buffer); |
|
367 goto add_string; |
|
368 } |
|
369 |
|
370 case '!': |
|
371 { |
|
372 char number_buffer[128]; |
|
373 int num = current_history_number (); |
|
374 if (num > 0) |
|
375 sprintf (number_buffer, "%d", num); |
|
376 else |
|
377 strcpy (number_buffer, "!"); |
|
378 temp = strsave (number_buffer); |
|
379 goto add_string; |
|
380 } |
|
381 |
|
382 case '$': |
|
383 temp = strsave (geteuid () == 0 ? "#" : "$"); |
|
384 goto add_string; |
|
385 |
|
386 case '\\': |
|
387 temp = strsave ("\\"); |
|
388 goto add_string; |
|
389 |
|
390 default: |
|
391 temp = strsave ("\\ "); |
|
392 temp[1] = c; |
|
393 |
|
394 add_string: |
|
395 if (c) |
|
396 string++; |
|
397 result = |
|
398 (char *)sub_append_string (temp, result, |
|
399 &result_index, &result_size); |
581
|
400 temp = 0; // Free ()'ed in sub_append_string (). |
529
|
401 result[result_index] = '\0'; |
|
402 break; |
|
403 } |
|
404 } |
|
405 else |
|
406 { |
|
407 while (3 + result_index > result_size) |
|
408 { |
|
409 char *tmp = new char [result_size += PROMPT_GROWTH]; |
|
410 strcpy (tmp, result); |
|
411 delete [] result; |
|
412 result = tmp; |
|
413 } |
|
414 result[result_index++] = c; |
|
415 result[result_index] = '\0'; |
|
416 } |
|
417 } |
|
418 |
|
419 #if 0 |
581
|
420 // I don't really think that this is a good idea. Do you? |
529
|
421 if (! find_variable ("NO_PROMPT_VARS")) |
|
422 { |
|
423 WORD_LIST *expand_string (), *list; |
|
424 char *string_list (); |
|
425 |
|
426 list = expand_string (result, 1); |
|
427 free (result); |
|
428 result = string_list (list); |
|
429 dispose_words (list); |
|
430 } |
|
431 #endif |
|
432 |
|
433 return result; |
|
434 } |
581
|
435 |
1044
|
436 static void |
|
437 do_input_echo (const char *input_string) |
|
438 { |
|
439 if (echo_input) |
|
440 { |
|
441 ostrstream buf; |
|
442 |
|
443 if (! forced_interactive) |
|
444 { |
|
445 char *prefix = decode_prompt_string (user_pref.ps4); |
|
446 buf << prefix; |
|
447 delete [] prefix; |
|
448 } |
|
449 |
|
450 if (input_string) |
|
451 { |
|
452 buf << input_string; |
|
453 int len = strlen (input_string); |
|
454 if (input_string[len-1] != '\n') |
|
455 buf << "\n"; |
|
456 } |
|
457 |
|
458 maybe_page_output (buf); |
|
459 } |
|
460 } |
|
461 |
581
|
462 // Use GNU readline to get an input line and store it in the history |
|
463 // list. |
|
464 |
287
|
465 static char * |
1
|
466 octave_gets (void) |
|
467 { |
529
|
468 if (octave_gets_line) |
126
|
469 { |
|
470 free (octave_gets_line); |
529
|
471 octave_gets_line = 0; |
126
|
472 } |
1
|
473 |
|
474 if (interactive || forced_interactive) |
|
475 { |
|
476 char *ps = (promptflag > 0) ? user_pref.ps1 : user_pref.ps2; |
|
477 char *prompt = decode_prompt_string (ps); |
|
478 |
|
479 if (interactive) |
|
480 { |
|
481 pipe_handler_error_count = 0; |
|
482 flush_output_to_pager (); |
|
483 } |
|
484 |
581
|
485 maybe_write_to_diary_file (prompt); |
|
486 |
1
|
487 octave_gets_line = gnu_readline (prompt); |
581
|
488 |
1
|
489 delete [] prompt; |
|
490 } |
|
491 else |
|
492 octave_gets_line = gnu_readline (""); |
|
493 |
|
494 current_input_line = octave_gets_line; |
|
495 |
|
496 if (octave_gets_line && *octave_gets_line) |
|
497 { |
|
498 maybe_save_history (octave_gets_line); |
|
499 |
581
|
500 maybe_write_to_diary_file (octave_gets_line); |
|
501 |
1044
|
502 do_input_echo (octave_gets_line); |
1
|
503 } |
581
|
504 |
|
505 maybe_write_to_diary_file ("\n"); |
|
506 |
1
|
507 return octave_gets_line; |
|
508 } |
|
509 |
581
|
510 // Read a line from the input stream. |
|
511 |
269
|
512 int |
1
|
513 octave_read (char *buf, int max_size) |
|
514 { |
|
515 int status = 0; |
|
516 |
529
|
517 static char *stashed_line = 0; |
1
|
518 |
|
519 if (get_input_from_eval_string) |
|
520 { |
|
521 int len = strlen (current_eval_string); |
|
522 if (len < max_size - 1) |
|
523 { |
|
524 strcpy (buf, current_eval_string); |
|
525 buf[len++] = '\n'; |
|
526 buf[len] = '\0'; // Paranoia. |
|
527 status = len; |
|
528 } |
|
529 else |
|
530 status = -1; |
|
531 |
|
532 if (stashed_line) |
|
533 delete [] stashed_line; |
|
534 |
|
535 stashed_line = strsave (buf); |
|
536 current_input_line = stashed_line; |
|
537 } |
|
538 else if (using_readline) |
|
539 { |
|
540 char *cp = octave_gets (); |
529
|
541 if (cp) |
1
|
542 { |
|
543 int len = strlen (cp); |
|
544 if (len >= max_size) |
|
545 status = -1; |
|
546 else |
|
547 { |
|
548 strcpy (buf, cp); |
|
549 buf[len++] = '\n'; |
|
550 buf[len] = '\0'; // Paranoia. |
|
551 status = len; |
|
552 } |
|
553 } |
|
554 current_input_line = cp; |
|
555 } |
|
556 else |
|
557 { |
|
558 FILE *curr_stream = rl_instream; |
338
|
559 if (reading_fcn_file || reading_script_file) |
339
|
560 curr_stream = ff_instream; |
1
|
561 |
529
|
562 assert (curr_stream); |
1
|
563 |
|
564 // Why is this required? |
|
565 buf[0] = '\0'; |
|
566 |
529
|
567 if (fgets (buf, max_size, curr_stream)) |
1
|
568 { |
|
569 int len = strlen (buf); |
|
570 if (len > max_size - 2) |
|
571 status = -1; |
|
572 else |
|
573 { |
|
574 if (buf[len-1] != '\n') |
|
575 { |
|
576 buf[len++] = '\n'; |
|
577 buf[len] = '\0'; |
|
578 } |
|
579 status = len; |
|
580 } |
|
581 } |
|
582 else |
|
583 status = 0; // Tell yylex that we found EOF. |
|
584 |
|
585 if (stashed_line) |
|
586 delete [] stashed_line; |
|
587 |
|
588 stashed_line = strsave (buf); |
1044
|
589 |
1
|
590 current_input_line = stashed_line; |
287
|
591 |
1044
|
592 do_input_echo (current_input_line); |
|
593 } |
287
|
594 |
1
|
595 input_line_number++; |
1044
|
596 |
1
|
597 return status; |
|
598 } |
|
599 |
581
|
600 // Fix things up so that input can come from file `name', printing a |
|
601 // warning if the file doesn't exist. |
|
602 |
1
|
603 FILE * |
529
|
604 get_input_from_file (char *name, int warn) |
1
|
605 { |
529
|
606 FILE *instream = 0; |
1
|
607 |
|
608 if (name && *name) |
|
609 instream = fopen (name, "r"); |
|
610 |
529
|
611 if (! instream && warn) |
217
|
612 warning ("%s: no such file or directory", name); |
1
|
613 |
338
|
614 if (reading_fcn_file || reading_script_file) |
339
|
615 ff_instream = instream; |
1
|
616 else |
|
617 rl_instream = instream; |
|
618 |
|
619 return instream; |
|
620 } |
|
621 |
581
|
622 // Fix things up so that input can come from the standard input. This |
|
623 // may need to become much more complicated, which is why it's in a |
|
624 // separate function. |
|
625 |
1
|
626 FILE * |
|
627 get_input_from_stdin (void) |
|
628 { |
|
629 rl_instream = stdin; |
|
630 return rl_instream; |
|
631 } |
|
632 |
1280
|
633 static char ** |
|
634 generate_struct_completions (const char *text, char *& prefix, |
|
635 char *& hint) |
|
636 { |
|
637 char **names = 0; |
|
638 |
1288
|
639 assert (text); |
|
640 |
1280
|
641 char *id = strsave (text); |
|
642 char *ptr = strchr (id, '.'); |
|
643 *ptr = '\0'; |
|
644 |
|
645 char *elts = ptr + 1; |
|
646 ptr = strrchr (elts, '.'); |
|
647 if (ptr) |
|
648 *ptr = '\0'; |
|
649 else |
|
650 elts = 0; |
|
651 |
|
652 prefix = strsave (text); |
|
653 ptr = strrchr (prefix, '.'); |
|
654 *ptr = '\0'; |
|
655 |
1288
|
656 delete [] hint; |
1280
|
657 hint = strsave (ptr + 1); |
|
658 |
|
659 symbol_record *sr = curr_sym_tab->lookup (id, 0, 0); |
|
660 if (! sr) |
|
661 sr = global_sym_tab->lookup (id, 0, 0); |
|
662 |
|
663 if (sr && sr->is_defined ()) |
|
664 { |
|
665 tree_fvc *tmp_fvc = sr->def (); |
|
666 |
|
667 tree_constant *def = 0; |
|
668 if (tmp_fvc->is_constant ()) |
|
669 def = (tree_constant *) tmp_fvc; |
|
670 |
|
671 if (def && def->is_map ()) |
|
672 { |
|
673 if (elts && *elts) |
|
674 { |
|
675 tree_constant ult = def->lookup_map_element (elts, 0, 1); |
|
676 |
|
677 if (ult.is_map ()) |
|
678 { |
|
679 Octave_map m = ult.map_value (); |
|
680 names = m.make_name_list (); |
|
681 } |
|
682 } |
|
683 else |
|
684 { |
|
685 Octave_map m = def->map_value (); |
|
686 names = m.make_name_list (); |
|
687 } |
|
688 } |
|
689 } |
|
690 |
|
691 delete [] id; |
|
692 |
|
693 return names; |
|
694 } |
|
695 |
|
696 static char ** |
|
697 generate_possible_completions (const char *text, char *& prefix, |
|
698 char *& hint) |
1
|
699 { |
1280
|
700 char **names = 0; |
|
701 |
|
702 prefix = 0; |
|
703 |
|
704 if (text && *text && *text != '.') |
|
705 { |
|
706 if (strrchr (text, '.')) |
1288
|
707 names = generate_struct_completions (text, prefix, hint); |
1280
|
708 else |
1288
|
709 names = make_name_list (); |
1280
|
710 } |
|
711 |
|
712 return names; |
|
713 } |
|
714 |
|
715 static int |
|
716 looks_like_struct (const char *nm) |
|
717 { |
|
718 int retval = 0; |
|
719 |
1288
|
720 assert (nm); |
|
721 |
1280
|
722 char *id = strsave (nm); |
|
723 char *elts = 0; |
|
724 char *ptr = strchr (id, '.'); |
|
725 if (ptr) |
|
726 { |
|
727 *ptr = '\0'; |
|
728 elts = ptr + 1; |
|
729 } |
|
730 |
|
731 symbol_record *sr = curr_sym_tab->lookup (id, 0, 0); |
|
732 if (! sr) |
|
733 sr = global_sym_tab->lookup (id, 0, 0); |
|
734 |
|
735 if (sr && sr->is_defined ()) |
|
736 { |
|
737 tree_fvc *tmp_fvc = sr->def (); |
|
738 |
|
739 tree_constant *def = 0; |
|
740 if (tmp_fvc->is_constant ()) |
|
741 def = (tree_constant *) tmp_fvc; |
|
742 |
|
743 if (def && def->is_map ()) |
|
744 { |
|
745 if (elts && *elts) |
|
746 { |
|
747 tree_constant ult = def->lookup_map_element (elts, 0, 1); |
|
748 |
|
749 if (ult.is_map ()) |
|
750 retval = 1; |
|
751 } |
|
752 else |
|
753 retval = 1; |
|
754 } |
|
755 } |
|
756 |
|
757 delete [] id; |
|
758 |
|
759 return retval; |
|
760 } |
|
761 |
|
762 static char * |
|
763 command_generator (const char *text, int state) |
|
764 { |
|
765 static char *prefix = 0; |
|
766 static char *hint = 0; |
|
767 |
|
768 static int prefix_len = 0; |
|
769 static int hint_len = 0; |
|
770 |
1
|
771 static int list_index = 0; |
1280
|
772 static char **name_list = 0; |
1
|
773 |
1280
|
774 static int matches = 0; |
1
|
775 |
|
776 if (state == 0) |
|
777 { |
|
778 list_index = 0; |
|
779 |
529
|
780 if (name_list) |
244
|
781 { |
|
782 char **ptr = name_list; |
|
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 { |
1280
|
818 char *name; |
|
819 while ((name = name_list[list_index]) != 0) |
244
|
820 { |
1280
|
821 list_index++; |
|
822 if (strncmp (name, hint, hint_len) == 0) |
|
823 { |
1288
|
824 int len = 2 + prefix_len + strlen (name); |
|
825 char *buf = (char *) xmalloc (len); |
1280
|
826 |
|
827 if (prefix) |
|
828 { |
|
829 strcpy (buf, prefix); |
|
830 strcat (buf, "."); |
|
831 strcat (buf, name); |
|
832 } |
|
833 else |
|
834 strcpy (buf, name); |
|
835 |
|
836 if (matches == 1 && looks_like_struct (buf)) |
|
837 rl_completion_append_character = '.'; |
|
838 else |
|
839 rl_completion_append_character = ' '; |
|
840 |
|
841 return buf; |
|
842 } |
244
|
843 } |
1
|
844 } |
|
845 |
529
|
846 return 0; |
1
|
847 } |
|
848 |
269
|
849 static char ** |
1
|
850 command_completer (char *text, int start, int end) |
|
851 { |
529
|
852 char **matches = 0; |
1
|
853 matches = completion_matches (text, command_generator); |
|
854 return matches; |
|
855 } |
|
856 |
581
|
857 // The next two functions implement the equivalent of the K*rn shell |
|
858 // C-o operate-and-get-next-history-line editing command. Stolen from |
|
859 // the GNU Bourne Again SHell. |
269
|
860 |
|
861 // ?? |
|
862 static int saved_history_line_to_use = 0; |
|
863 |
|
864 // ?? |
529
|
865 static Function *old_rl_startup_hook = 0; |
269
|
866 |
|
867 static void |
|
868 set_saved_history (void) |
|
869 { |
|
870 HIST_ENTRY *h; |
|
871 |
|
872 if (saved_history_line_to_use) |
|
873 { |
|
874 if (history_set_pos (saved_history_line_to_use)) |
|
875 { |
|
876 h = current_history (); |
|
877 if (h) |
|
878 { |
|
879 rl_insert_text (h->line); |
|
880 |
|
881 // Get rid of any undo list created by the previous insert, so the |
|
882 // line won't totally be erased when the edits are undone (they will |
|
883 // be normally, because this is a history line -- cf. readline.c: |
|
884 // line 380 or so). |
|
885 if (rl_undo_list) |
|
886 { |
|
887 free_undo_list (); |
529
|
888 rl_undo_list = 0; |
269
|
889 } |
|
890 } |
|
891 } |
|
892 } |
|
893 saved_history_line_to_use = 0; |
|
894 rl_startup_hook = old_rl_startup_hook; |
|
895 } |
|
896 |
|
897 static void |
|
898 operate_and_get_next (int count, int c) |
|
899 { |
|
900 int where; |
|
901 extern int history_stifled, history_length, max_input_history; |
|
902 |
581
|
903 // Accept the current line. |
269
|
904 rl_newline (); |
|
905 |
581
|
906 // Find the current line, and find the next line to use. |
269
|
907 where = where_history (); |
|
908 |
|
909 if (history_stifled && (history_length >= max_input_history)) |
|
910 saved_history_line_to_use = where; |
|
911 else |
|
912 saved_history_line_to_use = where + 1; |
|
913 |
|
914 old_rl_startup_hook = rl_startup_hook; |
|
915 rl_startup_hook = (Function *) set_saved_history; |
|
916 } |
|
917 |
1
|
918 void |
|
919 initialize_readline (void) |
|
920 { |
|
921 // Allow conditional parsing of the ~/.inputrc file |
|
922 rl_readline_name = "Octave"; |
|
923 |
|
924 // Tell the completer that we want to try first. |
271
|
925 rl_attempted_completion_function = (CPPFunction *) command_completer; |
269
|
926 |
|
927 // Bind operate-and-get-next. |
|
928 rl_add_defun ("operate-and-get-next", |
|
929 (Function *) operate_and_get_next, CTRL ('O')); |
1
|
930 } |
|
931 |
529
|
932 static int |
|
933 match_sans_spaces (const char *standard, const char *test) |
|
934 { |
1034
|
935 char *tmp = strsave (test); |
|
936 |
|
937 char *tp = tmp; |
529
|
938 while (*tp == ' ' || *tp == '\t') |
|
939 tp++; |
|
940 |
1125
|
941 char *ep = tmp + strlen (tmp) - 1; |
529
|
942 while (*ep == ' ' || *ep == '\t') |
|
943 ep--; |
|
944 |
1034
|
945 *(ep+1) = '\0'; |
|
946 |
|
947 int retval = strcmp (standard, tp) == 0; |
529
|
948 |
1034
|
949 delete [] tmp; |
|
950 |
|
951 return retval; |
|
952 |
529
|
953 } |
|
954 |
581
|
955 // If the user simply hits return, this will produce an empty matrix. |
|
956 |
529
|
957 static Octave_object |
|
958 get_user_input (const Octave_object& args, int nargout, int debug = 0) |
|
959 { |
|
960 tree_constant retval; |
|
961 |
|
962 int nargin = args.length (); |
|
963 |
|
964 int read_as_string = 0; |
|
965 |
712
|
966 if (nargin == 2) |
529
|
967 read_as_string++; |
|
968 |
|
969 char *prompt = "debug> "; |
712
|
970 if (nargin > 0) |
529
|
971 { |
712
|
972 prompt = args(0).string_value (); |
636
|
973 |
|
974 if (error_state) |
|
975 { |
|
976 error ("input: unrecognized argument"); |
|
977 return retval; |
|
978 } |
529
|
979 } |
|
980 |
|
981 again: |
|
982 |
|
983 flush_output_to_pager (); |
|
984 |
|
985 char *input_buf = gnu_readline (prompt); |
|
986 |
|
987 if (input_buf) |
|
988 { |
|
989 maybe_save_history (input_buf); |
|
990 |
|
991 int len = strlen (input_buf); |
|
992 |
|
993 if (len < 1) |
|
994 { |
|
995 if (debug) |
|
996 goto again; |
|
997 else |
963
|
998 { |
|
999 if (read_as_string) |
|
1000 return ""; |
|
1001 else |
|
1002 return Matrix (); |
|
1003 } |
529
|
1004 } |
|
1005 |
|
1006 if (match_sans_spaces ("exit", input_buf) |
|
1007 || match_sans_spaces ("quit", input_buf) |
|
1008 || match_sans_spaces ("return", input_buf)) |
963
|
1009 { |
|
1010 return retval; |
|
1011 } |
529
|
1012 else if (read_as_string) |
963
|
1013 { |
|
1014 retval = input_buf; |
|
1015 } |
529
|
1016 else |
|
1017 { |
|
1018 int parse_status = 0; |
|
1019 retval = eval_string (input_buf, 0, 0, parse_status); |
581
|
1020 if (retval.is_defined ()) |
|
1021 { |
|
1022 if (debug) |
|
1023 retval.eval (1); |
|
1024 } |
|
1025 else |
963
|
1026 retval = Matrix (); |
529
|
1027 } |
|
1028 } |
|
1029 else |
|
1030 error ("input: reading user-input failed!"); |
|
1031 |
|
1032 if (debug) |
|
1033 goto again; |
|
1034 |
|
1035 return retval; |
|
1036 } |
|
1037 |
712
|
1038 DEFUN ("input", Finput, Sinput, 2, 1, |
529
|
1039 "input (PROMPT [, S])\n\ |
|
1040 \n\ |
|
1041 Prompt user for input. If the second argument is present, return |
|
1042 value as a string.") |
|
1043 { |
|
1044 Octave_object retval; |
|
1045 |
|
1046 int nargin = args.length (); |
|
1047 |
712
|
1048 if (nargin == 1 || nargin == 2) |
529
|
1049 retval = get_user_input (args, nargout); |
|
1050 else |
|
1051 print_usage ("input"); |
|
1052 |
|
1053 return retval; |
|
1054 } |
|
1055 |
712
|
1056 DEFUN ("keyboard", Fkeyboard, Skeyboard, 1, 1, |
529
|
1057 "keyboard (PROMPT)\n\ |
|
1058 \n\ |
|
1059 maybe help in debugging function files") |
|
1060 { |
|
1061 Octave_object retval; |
|
1062 |
|
1063 int nargin = args.length (); |
|
1064 |
712
|
1065 if (nargin == 0 || nargin == 1) |
529
|
1066 retval = get_user_input (args, nargout, 1); |
|
1067 else |
|
1068 print_usage ("keyboard"); |
|
1069 |
|
1070 return retval; |
|
1071 } |
|
1072 |
1
|
1073 /* |
|
1074 ;;; Local Variables: *** |
|
1075 ;;; mode: C++ *** |
|
1076 ;;; page-delimiter: "^/\\*" *** |
|
1077 ;;; End: *** |
|
1078 */ |