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