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