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