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...) |
1466
|
57 |
1
|
58 #include "readline/readline.h" |
269
|
59 #include "readline/history.h" |
1
|
60 |
581
|
61 // Yes, this sucks, but it avoids a conflict with another readline |
|
62 // function declared in iostream.h. |
1350
|
63 // (Apparently, there isn't one there now...) |
581
|
64 |
1
|
65 #if 0 |
|
66 #define LINE_SIZE 8192 |
529
|
67 static int no_line_editing = 0; |
1
|
68 #endif |
|
69 |
|
70 char * |
1363
|
71 gnu_readline (const char *s) |
1
|
72 { |
|
73 #if 0 |
|
74 static int state = 0; |
529
|
75 static char *line_from_stdin = 0; |
1
|
76 if (no_line_editing) |
|
77 { |
|
78 if (! state) |
|
79 { |
|
80 line_from_stdin = (char *) malloc (LINE_SIZE); |
|
81 state = 1; |
|
82 } |
|
83 fputs ("octave> ", stdout); |
|
84 fgets (line_from_stdin, LINE_SIZE, stdin); |
|
85 return line_from_stdin; |
|
86 } |
|
87 else |
|
88 #endif |
|
89 return readline (s); |
|
90 } |
|
91 |
1352
|
92 #include "defun.h" |
|
93 #include "dirfns.h" |
1
|
94 #include "error.h" |
1352
|
95 #include "help.h" |
1
|
96 #include "input.h" |
1352
|
97 #include "oct-map.h" |
|
98 #include "oct-obj.h" |
|
99 #include "octave-hist.h" |
|
100 #include "octave.h" |
1
|
101 #include "pager.h" |
529
|
102 #include "parse.h" |
1352
|
103 #include "pathlen.h" |
|
104 #include "sighandlers.h" |
|
105 #include "symtab.h" |
1114
|
106 #include "sysdep.h" |
529
|
107 #include "tree-const.h" |
1
|
108 #include "user-prefs.h" |
1352
|
109 #include "utils.h" |
|
110 #include "variables.h" |
529
|
111 |
|
112 // The size that strings change by. |
|
113 #ifndef DEFAULT_ARRAY_SIZE |
|
114 #define DEFAULT_ARRAY_SIZE 512 |
|
115 #endif |
|
116 |
|
117 // The growth rate for the prompt string. |
|
118 #ifndef PROMPT_GROWTH |
|
119 #define PROMPT_GROWTH 50 |
|
120 #endif |
1
|
121 |
|
122 // Global pointer for eval(). |
529
|
123 const char *current_eval_string = 0; |
1
|
124 |
|
125 // Nonzero means get input from current_eval_string. |
|
126 int get_input_from_eval_string = 0; |
|
127 |
338
|
128 // Nonzero means we're parsing a function file. |
|
129 int reading_fcn_file = 0; |
1
|
130 |
338
|
131 // Simple name of function file we are reading. |
529
|
132 char *curr_fcn_file_name = 0; |
1
|
133 |
|
134 // Nonzero means we're parsing a script file. |
|
135 int reading_script_file = 0; |
|
136 |
|
137 // If we are reading from an M-file, this is it. |
529
|
138 FILE *ff_instream = 0; |
1
|
139 |
|
140 // Nonzero means we are using readline. |
|
141 int using_readline = 1; |
|
142 |
616
|
143 // Nonzero means commands are echoed as they are executed. |
|
144 // (--echo-commands; -x). |
1
|
145 int echo_input = 0; |
|
146 |
|
147 // Nonzero means this is an interactive shell. |
|
148 int interactive = 0; |
|
149 |
|
150 // Nonzero means the user forced this shell to be interactive (-i). |
|
151 int forced_interactive = 0; |
|
152 |
|
153 // Should we issue a prompt? |
|
154 int promptflag = 1; |
|
155 |
|
156 // The current line of input, from wherever. |
529
|
157 char *current_input_line = 0; |
1
|
158 |
|
159 // A line of input from readline. |
529
|
160 static char *octave_gets_line = 0; |
|
161 |
581
|
162 // Append SOURCE to TARGET at INDEX. SIZE is the current amount of |
|
163 // space allocated to TARGET. SOURCE can be NULL, in which case |
|
164 // nothing happens. Gets rid of SOURCE by free ()ing it. Returns |
|
165 // TARGET in case the location has changed. |
|
166 |
529
|
167 static char * |
|
168 sub_append_string (char *source, char *target, int *index, int *size) |
|
169 { |
|
170 if (source) |
|
171 { |
|
172 while ((int)strlen (source) >= (int)(*size - *index)) |
|
173 { |
|
174 char *tmp = new char [*size += DEFAULT_ARRAY_SIZE]; |
|
175 strcpy (tmp, target); |
|
176 delete [] target; |
|
177 target = tmp; |
|
178 } |
|
179 |
|
180 strcat (target, source); |
|
181 *index += strlen (source); |
|
182 |
|
183 delete [] source; |
|
184 } |
|
185 return target; |
|
186 } |
|
187 |
581
|
188 // Return the octal number parsed from STRING, or -1 to indicate that |
|
189 // the string contained a bad number. |
|
190 |
529
|
191 int |
|
192 read_octal (const char *string) |
|
193 { |
|
194 int result = 0; |
|
195 int digits = 0; |
|
196 |
|
197 while (*string && *string >= '0' && *string < '8') |
|
198 { |
|
199 digits++; |
|
200 result = (result * 8) + *string++ - '0'; |
|
201 } |
|
202 |
|
203 if (! digits || result > 0777 || *string) |
|
204 result = -1; |
|
205 |
|
206 return result; |
|
207 } |
|
208 |
581
|
209 // Return a string which will be printed as a prompt. The string may |
|
210 // contain special characters which are decoded as follows: |
|
211 // |
|
212 // \t the time |
|
213 // \d the date |
|
214 // \n CRLF |
|
215 // \s the name of the shell (program) |
|
216 // \w the current working directory |
|
217 // \W the last element of PWD |
|
218 // \u your username |
|
219 // \h the hostname |
|
220 // \# the command number of this command |
|
221 // \! the history number of this command |
|
222 // \$ a $ or a # if you are root |
|
223 // \<octal> character code in octal |
|
224 // \\ a backslash |
|
225 |
529
|
226 static char * |
|
227 decode_prompt_string (const char *string) |
|
228 { |
|
229 int result_size = PROMPT_GROWTH; |
|
230 int result_index = 0; |
|
231 char *result = new char [PROMPT_GROWTH]; |
|
232 int c; |
|
233 char *temp = 0; |
|
234 |
|
235 result[0] = 0; |
1215
|
236 while ((c = *string++)) |
529
|
237 { |
|
238 if (c == '\\') |
|
239 { |
|
240 c = *string; |
|
241 |
|
242 switch (c) |
|
243 { |
|
244 case '0': |
|
245 case '1': |
|
246 case '2': |
|
247 case '3': |
|
248 case '4': |
|
249 case '5': |
|
250 case '6': |
|
251 case '7': |
|
252 { |
|
253 char octal_string[4]; |
|
254 int n; |
|
255 |
|
256 strncpy (octal_string, string, 3); |
|
257 octal_string[3] = '\0'; |
|
258 |
|
259 n = read_octal (octal_string); |
|
260 |
|
261 temp = strsave ("\\"); |
|
262 if (n != -1) |
|
263 { |
|
264 string += 3; |
|
265 temp[0] = n; |
|
266 } |
|
267 |
|
268 c = 0; |
|
269 goto add_string; |
|
270 } |
|
271 |
|
272 case 't': |
|
273 case 'd': |
|
274 /* Make the current time/date into a string. */ |
|
275 { |
|
276 time_t the_time = time (0); |
|
277 char *ttemp = ctime (&the_time); |
|
278 temp = strsave (ttemp); |
|
279 |
|
280 if (c == 't') |
|
281 { |
|
282 strcpy (temp, temp + 11); |
|
283 temp[8] = '\0'; |
|
284 } |
|
285 else |
|
286 temp[10] = '\0'; |
|
287 |
|
288 goto add_string; |
|
289 } |
1
|
290 |
529
|
291 case 'n': |
|
292 if (! no_line_editing) |
|
293 temp = strsave ("\r\n"); |
|
294 else |
|
295 temp = strsave ("\n"); |
|
296 goto add_string; |
|
297 |
|
298 case 's': |
|
299 { |
|
300 temp = base_pathname (prog_name); |
|
301 temp = strsave (temp); |
|
302 goto add_string; |
|
303 } |
|
304 |
|
305 case 'w': |
|
306 case 'W': |
|
307 { |
|
308 char t_string[MAXPATHLEN]; |
|
309 #define EFFICIENT |
|
310 #ifdef EFFICIENT |
|
311 |
1358
|
312 // Use the value of PWD because it is much more |
|
313 // effecient. |
529
|
314 |
|
315 temp = user_pref.pwd; |
|
316 |
|
317 if (! temp) |
1112
|
318 octave_getcwd (t_string, MAXPATHLEN); |
529
|
319 else |
|
320 strcpy (t_string, temp); |
|
321 #else |
1112
|
322 octave_getcwd (t_string, MAXPATHLEN); |
529
|
323 #endif /* EFFICIENT */ |
|
324 |
|
325 if (c == 'W') |
|
326 { |
|
327 char *dir = strrchr (t_string, '/'); |
|
328 if (dir && dir != t_string) |
|
329 strcpy (t_string, dir + 1); |
|
330 temp = strsave (t_string); |
|
331 } |
|
332 else |
|
333 temp = strsave (polite_directory_format (t_string)); |
|
334 goto add_string; |
|
335 } |
|
336 |
|
337 case 'u': |
|
338 { |
|
339 temp = strsave (user_name); |
|
340 |
|
341 goto add_string; |
|
342 } |
|
343 |
|
344 case 'h': |
|
345 { |
|
346 char *t_string; |
|
347 |
|
348 temp = strsave (host_name); |
1215
|
349 t_string = strchr (temp, '.'); |
|
350 if (t_string); |
529
|
351 *t_string = '\0'; |
|
352 |
|
353 goto add_string; |
|
354 } |
|
355 |
|
356 case '#': |
|
357 { |
|
358 char number_buffer[128]; |
|
359 sprintf (number_buffer, "%d", current_command_number); |
|
360 temp = strsave (number_buffer); |
|
361 goto add_string; |
|
362 } |
|
363 |
|
364 case '!': |
|
365 { |
|
366 char number_buffer[128]; |
|
367 int num = current_history_number (); |
|
368 if (num > 0) |
|
369 sprintf (number_buffer, "%d", num); |
|
370 else |
|
371 strcpy (number_buffer, "!"); |
|
372 temp = strsave (number_buffer); |
|
373 goto add_string; |
|
374 } |
|
375 |
|
376 case '$': |
|
377 temp = strsave (geteuid () == 0 ? "#" : "$"); |
|
378 goto add_string; |
|
379 |
|
380 case '\\': |
|
381 temp = strsave ("\\"); |
|
382 goto add_string; |
|
383 |
|
384 default: |
|
385 temp = strsave ("\\ "); |
|
386 temp[1] = c; |
|
387 |
|
388 add_string: |
|
389 if (c) |
|
390 string++; |
|
391 result = |
|
392 (char *)sub_append_string (temp, result, |
|
393 &result_index, &result_size); |
581
|
394 temp = 0; // Free ()'ed in sub_append_string (). |
529
|
395 result[result_index] = '\0'; |
|
396 break; |
|
397 } |
|
398 } |
|
399 else |
|
400 { |
|
401 while (3 + result_index > result_size) |
|
402 { |
|
403 char *tmp = new char [result_size += PROMPT_GROWTH]; |
|
404 strcpy (tmp, result); |
|
405 delete [] result; |
|
406 result = tmp; |
|
407 } |
|
408 result[result_index++] = c; |
|
409 result[result_index] = '\0'; |
|
410 } |
|
411 } |
|
412 |
|
413 #if 0 |
1358
|
414 // I don't really think that this is a good idea. Do you? |
|
415 |
529
|
416 if (! find_variable ("NO_PROMPT_VARS")) |
|
417 { |
|
418 WORD_LIST *expand_string (), *list; |
|
419 char *string_list (); |
|
420 |
|
421 list = expand_string (result, 1); |
|
422 free (result); |
|
423 result = string_list (list); |
|
424 dispose_words (list); |
|
425 } |
|
426 #endif |
|
427 |
|
428 return result; |
|
429 } |
581
|
430 |
1044
|
431 static void |
|
432 do_input_echo (const char *input_string) |
|
433 { |
|
434 if (echo_input) |
|
435 { |
|
436 ostrstream buf; |
|
437 |
1403
|
438 if (forced_interactive) |
|
439 { |
|
440 char *ps = (promptflag > 0) ? user_pref.ps1 : user_pref.ps2; |
|
441 char *prefix = decode_prompt_string (ps); |
|
442 buf << prefix; |
|
443 delete [] prefix; |
|
444 } |
|
445 else |
1044
|
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 |
1358
|
566 // Why is this required? |
1
|
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 |
1430
|
698 // XXX FIXME XXX -- make this generate file names when appropriate. |
|
699 |
1280
|
700 static char ** |
|
701 generate_possible_completions (const char *text, char *& prefix, |
|
702 char *& hint) |
1
|
703 { |
1280
|
704 char **names = 0; |
|
705 |
|
706 prefix = 0; |
|
707 |
1430
|
708 if (text && *text && *text != '.' && strrchr (text, '.')) |
|
709 names = generate_struct_completions (text, prefix, hint); |
|
710 else |
|
711 names = make_name_list (); |
1280
|
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); |
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 { |
1358
|
927 // Allow conditional parsing of the ~/.inputrc file |
|
928 |
1
|
929 rl_readline_name = "Octave"; |
|
930 |
1358
|
931 // Tell the completer that we want to try first. |
|
932 |
271
|
933 rl_attempted_completion_function = (CPPFunction *) command_completer; |
269
|
934 |
1358
|
935 // Bind operate-and-get-next. |
|
936 |
269
|
937 rl_add_defun ("operate-and-get-next", |
|
938 (Function *) operate_and_get_next, CTRL ('O')); |
1441
|
939 |
|
940 // Don't treat single quotes as string delimiters when doing paren |
|
941 // matching. |
|
942 |
|
943 rl_paren_string_delimiters = "\""; |
1
|
944 } |
|
945 |
529
|
946 static int |
|
947 match_sans_spaces (const char *standard, const char *test) |
|
948 { |
1034
|
949 char *tmp = strsave (test); |
|
950 |
|
951 char *tp = tmp; |
529
|
952 while (*tp == ' ' || *tp == '\t') |
|
953 tp++; |
|
954 |
1125
|
955 char *ep = tmp + strlen (tmp) - 1; |
529
|
956 while (*ep == ' ' || *ep == '\t') |
|
957 ep--; |
|
958 |
1034
|
959 *(ep+1) = '\0'; |
|
960 |
|
961 int retval = strcmp (standard, tp) == 0; |
529
|
962 |
1034
|
963 delete [] tmp; |
|
964 |
|
965 return retval; |
|
966 |
529
|
967 } |
|
968 |
581
|
969 // If the user simply hits return, this will produce an empty matrix. |
|
970 |
529
|
971 static Octave_object |
1488
|
972 get_user_input (const Octave_object& args, int debug = 0) |
529
|
973 { |
|
974 tree_constant retval; |
|
975 |
|
976 int nargin = args.length (); |
|
977 |
|
978 int read_as_string = 0; |
|
979 |
712
|
980 if (nargin == 2) |
529
|
981 read_as_string++; |
|
982 |
1363
|
983 const char *prompt = "debug> "; |
712
|
984 if (nargin > 0) |
529
|
985 { |
712
|
986 prompt = args(0).string_value (); |
636
|
987 |
|
988 if (error_state) |
|
989 { |
|
990 error ("input: unrecognized argument"); |
|
991 return retval; |
|
992 } |
529
|
993 } |
|
994 |
|
995 again: |
|
996 |
|
997 flush_output_to_pager (); |
|
998 |
|
999 char *input_buf = gnu_readline (prompt); |
|
1000 |
|
1001 if (input_buf) |
|
1002 { |
|
1003 maybe_save_history (input_buf); |
|
1004 |
|
1005 int len = strlen (input_buf); |
|
1006 |
|
1007 if (len < 1) |
|
1008 { |
|
1009 if (debug) |
|
1010 goto again; |
|
1011 else |
963
|
1012 { |
|
1013 if (read_as_string) |
|
1014 return ""; |
|
1015 else |
|
1016 return Matrix (); |
|
1017 } |
529
|
1018 } |
|
1019 |
|
1020 if (match_sans_spaces ("exit", input_buf) |
|
1021 || match_sans_spaces ("quit", input_buf) |
|
1022 || match_sans_spaces ("return", input_buf)) |
963
|
1023 { |
|
1024 return retval; |
|
1025 } |
529
|
1026 else if (read_as_string) |
963
|
1027 { |
|
1028 retval = input_buf; |
|
1029 } |
529
|
1030 else |
|
1031 { |
|
1032 int parse_status = 0; |
1488
|
1033 retval = eval_string (input_buf, 0, parse_status); |
581
|
1034 if (retval.is_defined ()) |
|
1035 { |
|
1036 if (debug) |
|
1037 retval.eval (1); |
|
1038 } |
|
1039 else |
963
|
1040 retval = Matrix (); |
529
|
1041 } |
|
1042 } |
|
1043 else |
|
1044 error ("input: reading user-input failed!"); |
|
1045 |
|
1046 if (debug) |
|
1047 goto again; |
|
1048 |
|
1049 return retval; |
|
1050 } |
|
1051 |
1488
|
1052 DEFUN ("input", Finput, Sinput, 10, |
529
|
1053 "input (PROMPT [, S])\n\ |
|
1054 \n\ |
|
1055 Prompt user for input. If the second argument is present, return |
|
1056 value as a string.") |
|
1057 { |
|
1058 Octave_object retval; |
|
1059 |
|
1060 int nargin = args.length (); |
|
1061 |
712
|
1062 if (nargin == 1 || nargin == 2) |
1488
|
1063 retval = get_user_input (args); |
529
|
1064 else |
|
1065 print_usage ("input"); |
|
1066 |
|
1067 return retval; |
|
1068 } |
|
1069 |
1488
|
1070 DEFUN ("keyboard", Fkeyboard, Skeyboard, 10, |
529
|
1071 "keyboard (PROMPT)\n\ |
|
1072 \n\ |
|
1073 maybe help in debugging function files") |
|
1074 { |
|
1075 Octave_object retval; |
|
1076 |
|
1077 int nargin = args.length (); |
|
1078 |
712
|
1079 if (nargin == 0 || nargin == 1) |
1488
|
1080 retval = get_user_input (args, 1); |
529
|
1081 else |
|
1082 print_usage ("keyboard"); |
|
1083 |
|
1084 return retval; |
|
1085 } |
|
1086 |
1
|
1087 /* |
|
1088 ;;; Local Variables: *** |
|
1089 ;;; mode: C++ *** |
|
1090 ;;; page-delimiter: "^/\\*" *** |
|
1091 ;;; End: *** |
|
1092 */ |