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