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