1
|
1 // input.cc -*- C++ -*- |
|
2 /* |
|
3 |
269
|
4 Copyright (C) 1992, 1993, 1994 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 |
|
20 Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. |
|
21 |
|
22 */ |
|
23 |
|
24 // Use the GNU readline library for command line editing and hisory. |
|
25 |
240
|
26 #ifdef HAVE_CONFIG_H |
|
27 #include "config.h" |
1
|
28 #endif |
|
29 |
529
|
30 #include <sys/types.h> |
|
31 #ifdef HAVE_UNISTD_H |
|
32 #include <unistd.h> |
|
33 #endif |
|
34 #include <time.h> |
1
|
35 #include <stdio.h> |
|
36 #include <stdlib.h> |
195
|
37 #include <iostream.h> |
188
|
38 #include <string.h> |
1
|
39 #include <assert.h> |
|
40 |
|
41 // This must come before anything that includes iostream.h... |
|
42 extern "C" |
|
43 { |
|
44 #include "readline/readline.h" |
269
|
45 #include "readline/history.h" |
1
|
46 |
271
|
47 extern void free_undo_list (); |
|
48 |
252
|
49 extern char *xmalloc (); |
|
50 |
1
|
51 /* |
|
52 * Yes, this sucks, but it avoids a conflict with another readline |
|
53 * function declared in iostream.h. |
|
54 */ |
|
55 #if 0 |
|
56 #define LINE_SIZE 8192 |
529
|
57 static int no_line_editing = 0; |
1
|
58 #endif |
|
59 |
|
60 char * |
|
61 gnu_readline (char *s) |
|
62 { |
|
63 #if 0 |
|
64 static int state = 0; |
529
|
65 static char *line_from_stdin = 0; |
1
|
66 if (no_line_editing) |
|
67 { |
|
68 if (! state) |
|
69 { |
|
70 line_from_stdin = (char *) malloc (LINE_SIZE); |
|
71 state = 1; |
|
72 } |
|
73 fputs ("octave> ", stdout); |
|
74 fgets (line_from_stdin, LINE_SIZE, stdin); |
|
75 return line_from_stdin; |
|
76 } |
|
77 else |
|
78 #endif |
|
79 return readline (s); |
|
80 } |
|
81 } |
|
82 |
529
|
83 #include "help.h" |
1
|
84 #include "error.h" |
|
85 #include "utils.h" |
|
86 #include "input.h" |
|
87 #include "pager.h" |
529
|
88 #include "parse.h" |
|
89 #include "dirfns.h" |
|
90 #include "octave.h" |
|
91 #include "variables.h" |
|
92 #include "tree-const.h" |
1
|
93 #include "octave-hist.h" |
|
94 #include "sighandlers.h" |
|
95 #include "user-prefs.h" |
529
|
96 #include "oct-obj.h" |
|
97 #include "defun.h" |
|
98 |
|
99 #ifndef MAXPATHLEN |
|
100 #define MAXPATHLEN 1024 |
|
101 #endif |
|
102 |
|
103 // The size that strings change by. |
|
104 #ifndef DEFAULT_ARRAY_SIZE |
|
105 #define DEFAULT_ARRAY_SIZE 512 |
|
106 #endif |
|
107 |
|
108 // The growth rate for the prompt string. |
|
109 #ifndef PROMPT_GROWTH |
|
110 #define PROMPT_GROWTH 50 |
|
111 #endif |
1
|
112 |
|
113 // Global pointer for eval(). |
529
|
114 const char *current_eval_string = 0; |
1
|
115 |
|
116 // Nonzero means get input from current_eval_string. |
|
117 int get_input_from_eval_string = 0; |
|
118 |
338
|
119 // Nonzero means we're parsing a function file. |
|
120 int reading_fcn_file = 0; |
1
|
121 |
338
|
122 // Simple name of function file we are reading. |
529
|
123 char *curr_fcn_file_name = 0; |
1
|
124 |
|
125 // Nonzero means we're parsing a script file. |
|
126 int reading_script_file = 0; |
|
127 |
|
128 // If we are reading from an M-file, this is it. |
529
|
129 FILE *ff_instream = 0; |
1
|
130 |
|
131 // Nonzero means we are using readline. |
|
132 int using_readline = 1; |
|
133 |
|
134 // Nonzero means commands are echoed as they are executed (-x). |
|
135 int echo_input = 0; |
|
136 |
|
137 // Nonzero means this is an interactive shell. |
|
138 int interactive = 0; |
|
139 |
|
140 // Nonzero means the user forced this shell to be interactive (-i). |
|
141 int forced_interactive = 0; |
|
142 |
|
143 // Should we issue a prompt? |
|
144 int promptflag = 1; |
|
145 |
|
146 // The current line of input, from wherever. |
529
|
147 char *current_input_line = 0; |
1
|
148 |
|
149 // A line of input from readline. |
529
|
150 static char *octave_gets_line = 0; |
|
151 |
|
152 extern tree_constant eval_string (const char *string, int print, |
|
153 int ans_assign, int& parse_status); |
|
154 |
|
155 /* |
|
156 * Append SOURCE to TARGET at INDEX. SIZE is the current amount of |
|
157 * space allocated to TARGET. SOURCE can be NULL, in which case |
|
158 * nothing happens. Gets rid of SOURCE by free ()ing it. Returns |
|
159 * TARGET in case the location has changed. |
|
160 */ |
|
161 static char * |
|
162 sub_append_string (char *source, char *target, int *index, int *size) |
|
163 { |
|
164 if (source) |
|
165 { |
|
166 while ((int)strlen (source) >= (int)(*size - *index)) |
|
167 { |
|
168 char *tmp = new char [*size += DEFAULT_ARRAY_SIZE]; |
|
169 strcpy (tmp, target); |
|
170 delete [] target; |
|
171 target = tmp; |
|
172 } |
|
173 |
|
174 strcat (target, source); |
|
175 *index += strlen (source); |
|
176 |
|
177 delete [] source; |
|
178 } |
|
179 return target; |
|
180 } |
|
181 |
|
182 /* |
|
183 * Return the octal number parsed from STRING, or -1 to indicate that |
|
184 * the string contained a bad number. |
|
185 */ |
|
186 int |
|
187 read_octal (const char *string) |
|
188 { |
|
189 int result = 0; |
|
190 int digits = 0; |
|
191 |
|
192 while (*string && *string >= '0' && *string < '8') |
|
193 { |
|
194 digits++; |
|
195 result = (result * 8) + *string++ - '0'; |
|
196 } |
|
197 |
|
198 if (! digits || result > 0777 || *string) |
|
199 result = -1; |
|
200 |
|
201 return result; |
|
202 } |
|
203 |
|
204 /* |
|
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 */ |
|
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; |
|
232 while (c = *string++) |
|
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 |
|
308 // Use the value of PWD because it is much more effecient. |
|
309 |
|
310 temp = user_pref.pwd; |
|
311 |
|
312 if (! temp) |
|
313 getcwd (t_string, MAXPATHLEN); |
|
314 else |
|
315 strcpy (t_string, temp); |
|
316 #else |
|
317 getcwd (t_string, MAXPATHLEN); |
|
318 #endif /* EFFICIENT */ |
|
319 |
|
320 if (c == 'W') |
|
321 { |
|
322 char *dir = strrchr (t_string, '/'); |
|
323 if (dir && dir != t_string) |
|
324 strcpy (t_string, dir + 1); |
|
325 temp = strsave (t_string); |
|
326 } |
|
327 else |
|
328 temp = strsave (polite_directory_format (t_string)); |
|
329 goto add_string; |
|
330 } |
|
331 |
|
332 case 'u': |
|
333 { |
|
334 temp = strsave (user_name); |
|
335 |
|
336 goto add_string; |
|
337 } |
|
338 |
|
339 case 'h': |
|
340 { |
|
341 char *t_string; |
|
342 |
|
343 temp = strsave (host_name); |
|
344 if (t_string = strchr (temp, '.')) |
|
345 *t_string = '\0'; |
|
346 |
|
347 goto add_string; |
|
348 } |
|
349 |
|
350 case '#': |
|
351 { |
|
352 char number_buffer[128]; |
|
353 sprintf (number_buffer, "%d", current_command_number); |
|
354 temp = strsave (number_buffer); |
|
355 goto add_string; |
|
356 } |
|
357 |
|
358 case '!': |
|
359 { |
|
360 char number_buffer[128]; |
|
361 int num = current_history_number (); |
|
362 if (num > 0) |
|
363 sprintf (number_buffer, "%d", num); |
|
364 else |
|
365 strcpy (number_buffer, "!"); |
|
366 temp = strsave (number_buffer); |
|
367 goto add_string; |
|
368 } |
|
369 |
|
370 case '$': |
|
371 temp = strsave (geteuid () == 0 ? "#" : "$"); |
|
372 goto add_string; |
|
373 |
|
374 case '\\': |
|
375 temp = strsave ("\\"); |
|
376 goto add_string; |
|
377 |
|
378 default: |
|
379 temp = strsave ("\\ "); |
|
380 temp[1] = c; |
|
381 |
|
382 add_string: |
|
383 if (c) |
|
384 string++; |
|
385 result = |
|
386 (char *)sub_append_string (temp, result, |
|
387 &result_index, &result_size); |
|
388 temp = 0; /* Free ()'ed in sub_append_string (). */ |
|
389 result[result_index] = '\0'; |
|
390 break; |
|
391 } |
|
392 } |
|
393 else |
|
394 { |
|
395 while (3 + result_index > result_size) |
|
396 { |
|
397 char *tmp = new char [result_size += PROMPT_GROWTH]; |
|
398 strcpy (tmp, result); |
|
399 delete [] result; |
|
400 result = tmp; |
|
401 } |
|
402 result[result_index++] = c; |
|
403 result[result_index] = '\0'; |
|
404 } |
|
405 } |
|
406 |
|
407 #if 0 |
|
408 /* I don't really think that this is a good idea. Do you? */ |
|
409 if (! find_variable ("NO_PROMPT_VARS")) |
|
410 { |
|
411 WORD_LIST *expand_string (), *list; |
|
412 char *string_list (); |
|
413 |
|
414 list = expand_string (result, 1); |
|
415 free (result); |
|
416 result = string_list (list); |
|
417 dispose_words (list); |
|
418 } |
|
419 #endif |
|
420 |
|
421 return result; |
|
422 } |
1
|
423 /* |
|
424 * Use GNU readline to get an input line and store it in the history |
|
425 * list. |
|
426 */ |
287
|
427 static char * |
1
|
428 octave_gets (void) |
|
429 { |
529
|
430 if (octave_gets_line) |
126
|
431 { |
|
432 free (octave_gets_line); |
529
|
433 octave_gets_line = 0; |
126
|
434 } |
1
|
435 |
|
436 if (interactive || forced_interactive) |
|
437 { |
|
438 char *ps = (promptflag > 0) ? user_pref.ps1 : user_pref.ps2; |
|
439 char *prompt = decode_prompt_string (ps); |
|
440 |
|
441 if (interactive) |
|
442 { |
|
443 pipe_handler_error_count = 0; |
|
444 flush_output_to_pager (); |
|
445 } |
|
446 |
|
447 octave_gets_line = gnu_readline (prompt); |
|
448 delete [] prompt; |
|
449 } |
|
450 else |
|
451 octave_gets_line = gnu_readline (""); |
|
452 |
|
453 current_input_line = octave_gets_line; |
|
454 |
|
455 if (octave_gets_line && *octave_gets_line) |
|
456 { |
|
457 maybe_save_history (octave_gets_line); |
|
458 |
|
459 if (echo_input) |
|
460 { |
287
|
461 if (! forced_interactive) |
1
|
462 cout << "+ "; |
287
|
463 |
|
464 cout << octave_gets_line << "\n"; |
1
|
465 } |
|
466 } |
|
467 return octave_gets_line; |
|
468 } |
|
469 |
|
470 /* |
|
471 * Read a line from the input stream. |
|
472 */ |
269
|
473 int |
1
|
474 octave_read (char *buf, int max_size) |
|
475 { |
|
476 int status = 0; |
|
477 |
529
|
478 static char *stashed_line = 0; |
1
|
479 |
|
480 if (get_input_from_eval_string) |
|
481 { |
|
482 int len = strlen (current_eval_string); |
|
483 if (len < max_size - 1) |
|
484 { |
|
485 strcpy (buf, current_eval_string); |
|
486 buf[len++] = '\n'; |
|
487 buf[len] = '\0'; // Paranoia. |
|
488 status = len; |
|
489 } |
|
490 else |
|
491 status = -1; |
|
492 |
|
493 if (stashed_line) |
|
494 delete [] stashed_line; |
|
495 |
|
496 stashed_line = strsave (buf); |
|
497 current_input_line = stashed_line; |
|
498 } |
|
499 else if (using_readline) |
|
500 { |
|
501 char *cp = octave_gets (); |
529
|
502 if (cp) |
1
|
503 { |
|
504 int len = strlen (cp); |
|
505 if (len >= max_size) |
|
506 status = -1; |
|
507 else |
|
508 { |
|
509 strcpy (buf, cp); |
|
510 buf[len++] = '\n'; |
|
511 buf[len] = '\0'; // Paranoia. |
|
512 status = len; |
|
513 } |
|
514 } |
|
515 current_input_line = cp; |
|
516 } |
|
517 else |
|
518 { |
|
519 FILE *curr_stream = rl_instream; |
338
|
520 if (reading_fcn_file || reading_script_file) |
339
|
521 curr_stream = ff_instream; |
1
|
522 |
529
|
523 assert (curr_stream); |
1
|
524 |
|
525 // Why is this required? |
|
526 buf[0] = '\0'; |
|
527 |
529
|
528 if (fgets (buf, max_size, curr_stream)) |
1
|
529 { |
|
530 int len = strlen (buf); |
|
531 if (len > max_size - 2) |
|
532 status = -1; |
|
533 else |
|
534 { |
|
535 if (buf[len-1] != '\n') |
|
536 { |
|
537 buf[len++] = '\n'; |
|
538 buf[len] = '\0'; |
|
539 } |
|
540 status = len; |
|
541 } |
|
542 } |
|
543 else |
|
544 status = 0; // Tell yylex that we found EOF. |
|
545 |
|
546 if (stashed_line) |
|
547 delete [] stashed_line; |
|
548 |
|
549 stashed_line = strsave (buf); |
|
550 current_input_line = stashed_line; |
287
|
551 |
|
552 if (echo_input && current_input_line && *current_input_line) |
|
553 { |
|
554 if (! forced_interactive) |
|
555 cout << "+ "; |
|
556 |
|
557 cout << current_input_line << "\n"; |
|
558 } |
1
|
559 } |
|
560 input_line_number++; |
|
561 return status; |
|
562 } |
|
563 |
|
564 /* |
|
565 * Fix things up so that input can come from file `name', printing a |
|
566 * warning if the file doesn't exist. |
|
567 */ |
|
568 FILE * |
529
|
569 get_input_from_file (char *name, int warn) |
1
|
570 { |
529
|
571 FILE *instream = 0; |
1
|
572 |
|
573 if (name && *name) |
|
574 instream = fopen (name, "r"); |
|
575 |
529
|
576 if (! instream && warn) |
217
|
577 warning ("%s: no such file or directory", name); |
1
|
578 |
338
|
579 if (reading_fcn_file || reading_script_file) |
339
|
580 ff_instream = instream; |
1
|
581 else |
|
582 rl_instream = instream; |
|
583 |
|
584 return instream; |
|
585 } |
|
586 |
|
587 /* |
|
588 * Fix things up so that input can come from the standard input. This |
|
589 * may need to become much more complicated, which is why it's in a |
|
590 * separate function. |
|
591 */ |
|
592 FILE * |
|
593 get_input_from_stdin (void) |
|
594 { |
|
595 rl_instream = stdin; |
|
596 return rl_instream; |
|
597 } |
|
598 |
269
|
599 static char * |
1
|
600 command_generator (char *text, int state) |
|
601 { |
|
602 static int len = 0; |
|
603 static int list_index = 0; |
|
604 |
529
|
605 static char **name_list = 0; |
1
|
606 |
|
607 if (state == 0) |
|
608 { |
|
609 list_index = 0; |
|
610 len = strlen (text); |
|
611 |
529
|
612 if (name_list) |
244
|
613 { |
|
614 char **ptr = name_list; |
|
615 while (ptr && *ptr) |
|
616 delete [] *ptr++; |
|
617 delete [] name_list; |
|
618 } |
1
|
619 |
|
620 name_list = make_name_list (); |
|
621 } |
|
622 |
|
623 char *name; |
529
|
624 while ((name = name_list[list_index]) != 0) |
1
|
625 { |
|
626 list_index++; |
|
627 if (strncmp (name, text, len) == 0) |
244
|
628 { |
|
629 char *buf = xmalloc (1 + strlen (name)); |
|
630 strcpy (buf, name); |
|
631 return buf; |
|
632 } |
1
|
633 } |
|
634 |
529
|
635 return 0; |
1
|
636 } |
|
637 |
269
|
638 static char ** |
1
|
639 command_completer (char *text, int start, int end) |
|
640 { |
529
|
641 char **matches = 0; |
1
|
642 matches = completion_matches (text, command_generator); |
|
643 return matches; |
|
644 } |
|
645 |
269
|
646 /* |
|
647 * The next two functions implement the equivalent of the K*rn shell |
|
648 * C-o operate-and-get-next-history-line editing command. Stolen from |
|
649 * the GNU Bourne Again SHell. |
|
650 */ |
|
651 |
|
652 // ?? |
|
653 static int saved_history_line_to_use = 0; |
|
654 |
|
655 // ?? |
529
|
656 static Function *old_rl_startup_hook = 0; |
269
|
657 |
|
658 static void |
|
659 set_saved_history (void) |
|
660 { |
|
661 HIST_ENTRY *h; |
|
662 |
|
663 if (saved_history_line_to_use) |
|
664 { |
|
665 if (history_set_pos (saved_history_line_to_use)) |
|
666 { |
|
667 h = current_history (); |
|
668 if (h) |
|
669 { |
|
670 rl_insert_text (h->line); |
|
671 |
|
672 // Get rid of any undo list created by the previous insert, so the |
|
673 // line won't totally be erased when the edits are undone (they will |
|
674 // be normally, because this is a history line -- cf. readline.c: |
|
675 // line 380 or so). |
|
676 if (rl_undo_list) |
|
677 { |
|
678 free_undo_list (); |
529
|
679 rl_undo_list = 0; |
269
|
680 } |
|
681 } |
|
682 } |
|
683 } |
|
684 saved_history_line_to_use = 0; |
|
685 rl_startup_hook = old_rl_startup_hook; |
|
686 } |
|
687 |
|
688 static void |
|
689 operate_and_get_next (int count, int c) |
|
690 { |
|
691 int where; |
|
692 extern int history_stifled, history_length, max_input_history; |
|
693 |
|
694 /* Accept the current line. */ |
|
695 rl_newline (); |
|
696 |
|
697 /* Find the current line, and find the next line to use. */ |
|
698 where = where_history (); |
|
699 |
|
700 if (history_stifled && (history_length >= max_input_history)) |
|
701 saved_history_line_to_use = where; |
|
702 else |
|
703 saved_history_line_to_use = where + 1; |
|
704 |
|
705 old_rl_startup_hook = rl_startup_hook; |
|
706 rl_startup_hook = (Function *) set_saved_history; |
|
707 } |
|
708 |
1
|
709 void |
|
710 initialize_readline (void) |
|
711 { |
|
712 // Allow conditional parsing of the ~/.inputrc file |
|
713 rl_readline_name = "Octave"; |
|
714 |
|
715 // Tell the completer that we want to try first. |
271
|
716 rl_attempted_completion_function = (CPPFunction *) command_completer; |
269
|
717 |
|
718 // Bind operate-and-get-next. |
|
719 rl_add_defun ("operate-and-get-next", |
|
720 (Function *) operate_and_get_next, CTRL ('O')); |
1
|
721 } |
|
722 |
529
|
723 static int |
|
724 match_sans_spaces (const char *standard, const char *test) |
|
725 { |
|
726 const char *tp = test; |
|
727 while (*tp == ' ' || *tp == '\t') |
|
728 tp++; |
|
729 |
|
730 const char *ep = test + strlen (test) - 1; |
|
731 while (*ep == ' ' || *ep == '\t') |
|
732 ep--; |
|
733 |
|
734 int len = ep - tp + 1; |
|
735 |
|
736 return (strncmp (standard, tp, len) == 0); |
|
737 } |
|
738 |
|
739 static Octave_object |
|
740 get_user_input (const Octave_object& args, int nargout, int debug = 0) |
|
741 { |
|
742 tree_constant retval; |
|
743 |
|
744 int nargin = args.length (); |
|
745 |
|
746 int read_as_string = 0; |
|
747 |
|
748 if (nargin == 3) |
|
749 read_as_string++; |
|
750 |
|
751 char *prompt = "debug> "; |
|
752 if (nargin > 1) |
|
753 { |
|
754 if (args(1).is_string_type ()) |
|
755 prompt = args(1).string_value (); |
|
756 else |
|
757 { |
|
758 error ("input: unrecognized argument"); |
|
759 return retval; |
|
760 } |
|
761 } |
|
762 |
|
763 again: |
|
764 |
|
765 flush_output_to_pager (); |
|
766 |
|
767 char *input_buf = gnu_readline (prompt); |
|
768 |
|
769 if (input_buf) |
|
770 { |
|
771 maybe_save_history (input_buf); |
|
772 |
|
773 int len = strlen (input_buf); |
|
774 |
|
775 if (len < 1) |
|
776 { |
|
777 if (debug) |
|
778 goto again; |
|
779 else |
|
780 return retval; |
|
781 } |
|
782 |
|
783 if (match_sans_spaces ("exit", input_buf) |
|
784 || match_sans_spaces ("quit", input_buf) |
|
785 || match_sans_spaces ("return", input_buf)) |
|
786 return tree_constant (); |
|
787 else if (read_as_string) |
|
788 retval = input_buf; |
|
789 else |
|
790 { |
|
791 int parse_status = 0; |
|
792 retval = eval_string (input_buf, 0, 0, parse_status); |
|
793 if (debug && retval.is_defined ()) |
|
794 retval.eval (1); |
|
795 } |
|
796 } |
|
797 else |
|
798 error ("input: reading user-input failed!"); |
|
799 |
|
800 if (debug) |
|
801 goto again; |
|
802 |
|
803 return retval; |
|
804 } |
|
805 |
|
806 DEFUN ("input", Finput, Sinput, 3, 1, |
|
807 "input (PROMPT [, S])\n\ |
|
808 \n\ |
|
809 Prompt user for input. If the second argument is present, return |
|
810 value as a string.") |
|
811 { |
|
812 Octave_object retval; |
|
813 |
|
814 int nargin = args.length (); |
|
815 |
|
816 if (nargin == 2 || nargin == 3) |
|
817 retval = get_user_input (args, nargout); |
|
818 else |
|
819 print_usage ("input"); |
|
820 |
|
821 return retval; |
|
822 } |
|
823 |
|
824 DEFUN ("keyboard", Fkeyboard, Skeyboard, 2, 1, |
|
825 "keyboard (PROMPT)\n\ |
|
826 \n\ |
|
827 maybe help in debugging function files") |
|
828 { |
|
829 Octave_object retval; |
|
830 |
|
831 int nargin = args.length (); |
|
832 |
|
833 if (nargin == 1 || nargin == 2) |
|
834 retval = get_user_input (args, nargout, 1); |
|
835 else |
|
836 print_usage ("keyboard"); |
|
837 |
|
838 return retval; |
|
839 } |
|
840 |
1
|
841 /* |
|
842 ;;; Local Variables: *** |
|
843 ;;; mode: C++ *** |
|
844 ;;; page-delimiter: "^/\\*" *** |
|
845 ;;; End: *** |
|
846 */ |