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 |
616
|
133 // Nonzero means commands are echoed as they are executed. |
|
134 // (--echo-commands; -x). |
1
|
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 |
581
|
155 // Append SOURCE to TARGET at INDEX. SIZE is the current amount of |
|
156 // space allocated to TARGET. SOURCE can be NULL, in which case |
|
157 // nothing happens. Gets rid of SOURCE by free ()ing it. Returns |
|
158 // TARGET in case the location has changed. |
|
159 |
529
|
160 static char * |
|
161 sub_append_string (char *source, char *target, int *index, int *size) |
|
162 { |
|
163 if (source) |
|
164 { |
|
165 while ((int)strlen (source) >= (int)(*size - *index)) |
|
166 { |
|
167 char *tmp = new char [*size += DEFAULT_ARRAY_SIZE]; |
|
168 strcpy (tmp, target); |
|
169 delete [] target; |
|
170 target = tmp; |
|
171 } |
|
172 |
|
173 strcat (target, source); |
|
174 *index += strlen (source); |
|
175 |
|
176 delete [] source; |
|
177 } |
|
178 return target; |
|
179 } |
|
180 |
581
|
181 // Return the octal number parsed from STRING, or -1 to indicate that |
|
182 // the string contained a bad number. |
|
183 |
529
|
184 int |
|
185 read_octal (const char *string) |
|
186 { |
|
187 int result = 0; |
|
188 int digits = 0; |
|
189 |
|
190 while (*string && *string >= '0' && *string < '8') |
|
191 { |
|
192 digits++; |
|
193 result = (result * 8) + *string++ - '0'; |
|
194 } |
|
195 |
|
196 if (! digits || result > 0777 || *string) |
|
197 result = -1; |
|
198 |
|
199 return result; |
|
200 } |
|
201 |
581
|
202 // Return a string which will be printed as a prompt. The string may |
|
203 // contain special characters which are decoded as follows: |
|
204 // |
|
205 // \t the time |
|
206 // \d the date |
|
207 // \n CRLF |
|
208 // \s the name of the shell (program) |
|
209 // \w the current working directory |
|
210 // \W the last element of PWD |
|
211 // \u your username |
|
212 // \h the hostname |
|
213 // \# the command number of this command |
|
214 // \! the history number of this command |
|
215 // \$ a $ or a # if you are root |
|
216 // \<octal> character code in octal |
|
217 // \\ a backslash |
|
218 |
529
|
219 static char * |
|
220 decode_prompt_string (const char *string) |
|
221 { |
|
222 int result_size = PROMPT_GROWTH; |
|
223 int result_index = 0; |
|
224 char *result = new char [PROMPT_GROWTH]; |
|
225 int c; |
|
226 char *temp = 0; |
|
227 |
|
228 result[0] = 0; |
|
229 while (c = *string++) |
|
230 { |
|
231 if (c == '\\') |
|
232 { |
|
233 c = *string; |
|
234 |
|
235 switch (c) |
|
236 { |
|
237 case '0': |
|
238 case '1': |
|
239 case '2': |
|
240 case '3': |
|
241 case '4': |
|
242 case '5': |
|
243 case '6': |
|
244 case '7': |
|
245 { |
|
246 char octal_string[4]; |
|
247 int n; |
|
248 |
|
249 strncpy (octal_string, string, 3); |
|
250 octal_string[3] = '\0'; |
|
251 |
|
252 n = read_octal (octal_string); |
|
253 |
|
254 temp = strsave ("\\"); |
|
255 if (n != -1) |
|
256 { |
|
257 string += 3; |
|
258 temp[0] = n; |
|
259 } |
|
260 |
|
261 c = 0; |
|
262 goto add_string; |
|
263 } |
|
264 |
|
265 case 't': |
|
266 case 'd': |
|
267 /* Make the current time/date into a string. */ |
|
268 { |
|
269 time_t the_time = time (0); |
|
270 char *ttemp = ctime (&the_time); |
|
271 temp = strsave (ttemp); |
|
272 |
|
273 if (c == 't') |
|
274 { |
|
275 strcpy (temp, temp + 11); |
|
276 temp[8] = '\0'; |
|
277 } |
|
278 else |
|
279 temp[10] = '\0'; |
|
280 |
|
281 goto add_string; |
|
282 } |
1
|
283 |
529
|
284 case 'n': |
|
285 if (! no_line_editing) |
|
286 temp = strsave ("\r\n"); |
|
287 else |
|
288 temp = strsave ("\n"); |
|
289 goto add_string; |
|
290 |
|
291 case 's': |
|
292 { |
|
293 temp = base_pathname (prog_name); |
|
294 temp = strsave (temp); |
|
295 goto add_string; |
|
296 } |
|
297 |
|
298 case 'w': |
|
299 case 'W': |
|
300 { |
|
301 char t_string[MAXPATHLEN]; |
|
302 #define EFFICIENT |
|
303 #ifdef EFFICIENT |
|
304 |
|
305 // Use the value of PWD because it is much more effecient. |
|
306 |
|
307 temp = user_pref.pwd; |
|
308 |
|
309 if (! temp) |
|
310 getcwd (t_string, MAXPATHLEN); |
|
311 else |
|
312 strcpy (t_string, temp); |
|
313 #else |
|
314 getcwd (t_string, MAXPATHLEN); |
|
315 #endif /* EFFICIENT */ |
|
316 |
|
317 if (c == 'W') |
|
318 { |
|
319 char *dir = strrchr (t_string, '/'); |
|
320 if (dir && dir != t_string) |
|
321 strcpy (t_string, dir + 1); |
|
322 temp = strsave (t_string); |
|
323 } |
|
324 else |
|
325 temp = strsave (polite_directory_format (t_string)); |
|
326 goto add_string; |
|
327 } |
|
328 |
|
329 case 'u': |
|
330 { |
|
331 temp = strsave (user_name); |
|
332 |
|
333 goto add_string; |
|
334 } |
|
335 |
|
336 case 'h': |
|
337 { |
|
338 char *t_string; |
|
339 |
|
340 temp = strsave (host_name); |
|
341 if (t_string = strchr (temp, '.')) |
|
342 *t_string = '\0'; |
|
343 |
|
344 goto add_string; |
|
345 } |
|
346 |
|
347 case '#': |
|
348 { |
|
349 char number_buffer[128]; |
|
350 sprintf (number_buffer, "%d", current_command_number); |
|
351 temp = strsave (number_buffer); |
|
352 goto add_string; |
|
353 } |
|
354 |
|
355 case '!': |
|
356 { |
|
357 char number_buffer[128]; |
|
358 int num = current_history_number (); |
|
359 if (num > 0) |
|
360 sprintf (number_buffer, "%d", num); |
|
361 else |
|
362 strcpy (number_buffer, "!"); |
|
363 temp = strsave (number_buffer); |
|
364 goto add_string; |
|
365 } |
|
366 |
|
367 case '$': |
|
368 temp = strsave (geteuid () == 0 ? "#" : "$"); |
|
369 goto add_string; |
|
370 |
|
371 case '\\': |
|
372 temp = strsave ("\\"); |
|
373 goto add_string; |
|
374 |
|
375 default: |
|
376 temp = strsave ("\\ "); |
|
377 temp[1] = c; |
|
378 |
|
379 add_string: |
|
380 if (c) |
|
381 string++; |
|
382 result = |
|
383 (char *)sub_append_string (temp, result, |
|
384 &result_index, &result_size); |
581
|
385 temp = 0; // Free ()'ed in sub_append_string (). |
529
|
386 result[result_index] = '\0'; |
|
387 break; |
|
388 } |
|
389 } |
|
390 else |
|
391 { |
|
392 while (3 + result_index > result_size) |
|
393 { |
|
394 char *tmp = new char [result_size += PROMPT_GROWTH]; |
|
395 strcpy (tmp, result); |
|
396 delete [] result; |
|
397 result = tmp; |
|
398 } |
|
399 result[result_index++] = c; |
|
400 result[result_index] = '\0'; |
|
401 } |
|
402 } |
|
403 |
|
404 #if 0 |
581
|
405 // I don't really think that this is a good idea. Do you? |
529
|
406 if (! find_variable ("NO_PROMPT_VARS")) |
|
407 { |
|
408 WORD_LIST *expand_string (), *list; |
|
409 char *string_list (); |
|
410 |
|
411 list = expand_string (result, 1); |
|
412 free (result); |
|
413 result = string_list (list); |
|
414 dispose_words (list); |
|
415 } |
|
416 #endif |
|
417 |
|
418 return result; |
|
419 } |
581
|
420 |
|
421 // Use GNU readline to get an input line and store it in the history |
|
422 // list. |
|
423 |
287
|
424 static char * |
1
|
425 octave_gets (void) |
|
426 { |
529
|
427 if (octave_gets_line) |
126
|
428 { |
|
429 free (octave_gets_line); |
529
|
430 octave_gets_line = 0; |
126
|
431 } |
1
|
432 |
|
433 if (interactive || forced_interactive) |
|
434 { |
|
435 char *ps = (promptflag > 0) ? user_pref.ps1 : user_pref.ps2; |
|
436 char *prompt = decode_prompt_string (ps); |
|
437 |
|
438 if (interactive) |
|
439 { |
|
440 pipe_handler_error_count = 0; |
|
441 flush_output_to_pager (); |
|
442 } |
|
443 |
581
|
444 maybe_write_to_diary_file (prompt); |
|
445 |
1
|
446 octave_gets_line = gnu_readline (prompt); |
581
|
447 |
1
|
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 |
581
|
459 maybe_write_to_diary_file (octave_gets_line); |
|
460 |
1
|
461 if (echo_input) |
|
462 { |
287
|
463 if (! forced_interactive) |
1
|
464 cout << "+ "; |
287
|
465 |
|
466 cout << octave_gets_line << "\n"; |
1
|
467 } |
|
468 } |
581
|
469 |
|
470 maybe_write_to_diary_file ("\n"); |
|
471 |
1
|
472 return octave_gets_line; |
|
473 } |
|
474 |
581
|
475 // Read a line from the input stream. |
|
476 |
269
|
477 int |
1
|
478 octave_read (char *buf, int max_size) |
|
479 { |
|
480 int status = 0; |
|
481 |
529
|
482 static char *stashed_line = 0; |
1
|
483 |
|
484 if (get_input_from_eval_string) |
|
485 { |
|
486 int len = strlen (current_eval_string); |
|
487 if (len < max_size - 1) |
|
488 { |
|
489 strcpy (buf, current_eval_string); |
|
490 buf[len++] = '\n'; |
|
491 buf[len] = '\0'; // Paranoia. |
|
492 status = len; |
|
493 } |
|
494 else |
|
495 status = -1; |
|
496 |
|
497 if (stashed_line) |
|
498 delete [] stashed_line; |
|
499 |
|
500 stashed_line = strsave (buf); |
|
501 current_input_line = stashed_line; |
|
502 } |
|
503 else if (using_readline) |
|
504 { |
|
505 char *cp = octave_gets (); |
529
|
506 if (cp) |
1
|
507 { |
|
508 int len = strlen (cp); |
|
509 if (len >= max_size) |
|
510 status = -1; |
|
511 else |
|
512 { |
|
513 strcpy (buf, cp); |
|
514 buf[len++] = '\n'; |
|
515 buf[len] = '\0'; // Paranoia. |
|
516 status = len; |
|
517 } |
|
518 } |
|
519 current_input_line = cp; |
|
520 } |
|
521 else |
|
522 { |
|
523 FILE *curr_stream = rl_instream; |
338
|
524 if (reading_fcn_file || reading_script_file) |
339
|
525 curr_stream = ff_instream; |
1
|
526 |
529
|
527 assert (curr_stream); |
1
|
528 |
|
529 // Why is this required? |
|
530 buf[0] = '\0'; |
|
531 |
529
|
532 if (fgets (buf, max_size, curr_stream)) |
1
|
533 { |
|
534 int len = strlen (buf); |
|
535 if (len > max_size - 2) |
|
536 status = -1; |
|
537 else |
|
538 { |
|
539 if (buf[len-1] != '\n') |
|
540 { |
|
541 buf[len++] = '\n'; |
|
542 buf[len] = '\0'; |
|
543 } |
|
544 status = len; |
|
545 } |
|
546 } |
|
547 else |
|
548 status = 0; // Tell yylex that we found EOF. |
|
549 |
|
550 if (stashed_line) |
|
551 delete [] stashed_line; |
|
552 |
|
553 stashed_line = strsave (buf); |
|
554 current_input_line = stashed_line; |
287
|
555 |
|
556 if (echo_input && current_input_line && *current_input_line) |
|
557 { |
|
558 if (! forced_interactive) |
|
559 cout << "+ "; |
|
560 |
|
561 cout << current_input_line << "\n"; |
|
562 } |
1
|
563 } |
|
564 input_line_number++; |
|
565 return status; |
|
566 } |
|
567 |
581
|
568 // Fix things up so that input can come from file `name', printing a |
|
569 // warning if the file doesn't exist. |
|
570 |
1
|
571 FILE * |
529
|
572 get_input_from_file (char *name, int warn) |
1
|
573 { |
529
|
574 FILE *instream = 0; |
1
|
575 |
|
576 if (name && *name) |
|
577 instream = fopen (name, "r"); |
|
578 |
529
|
579 if (! instream && warn) |
217
|
580 warning ("%s: no such file or directory", name); |
1
|
581 |
338
|
582 if (reading_fcn_file || reading_script_file) |
339
|
583 ff_instream = instream; |
1
|
584 else |
|
585 rl_instream = instream; |
|
586 |
|
587 return instream; |
|
588 } |
|
589 |
581
|
590 // Fix things up so that input can come from the standard input. This |
|
591 // may need to become much more complicated, which is why it's in a |
|
592 // separate function. |
|
593 |
1
|
594 FILE * |
|
595 get_input_from_stdin (void) |
|
596 { |
|
597 rl_instream = stdin; |
|
598 return rl_instream; |
|
599 } |
|
600 |
269
|
601 static char * |
1
|
602 command_generator (char *text, int state) |
|
603 { |
|
604 static int len = 0; |
|
605 static int list_index = 0; |
|
606 |
529
|
607 static char **name_list = 0; |
1
|
608 |
|
609 if (state == 0) |
|
610 { |
|
611 list_index = 0; |
|
612 len = strlen (text); |
|
613 |
529
|
614 if (name_list) |
244
|
615 { |
|
616 char **ptr = name_list; |
|
617 while (ptr && *ptr) |
|
618 delete [] *ptr++; |
|
619 delete [] name_list; |
|
620 } |
1
|
621 |
|
622 name_list = make_name_list (); |
|
623 } |
|
624 |
|
625 char *name; |
529
|
626 while ((name = name_list[list_index]) != 0) |
1
|
627 { |
|
628 list_index++; |
|
629 if (strncmp (name, text, len) == 0) |
244
|
630 { |
|
631 char *buf = xmalloc (1 + strlen (name)); |
|
632 strcpy (buf, name); |
|
633 return buf; |
|
634 } |
1
|
635 } |
|
636 |
529
|
637 return 0; |
1
|
638 } |
|
639 |
269
|
640 static char ** |
1
|
641 command_completer (char *text, int start, int end) |
|
642 { |
529
|
643 char **matches = 0; |
1
|
644 matches = completion_matches (text, command_generator); |
|
645 return matches; |
|
646 } |
|
647 |
581
|
648 // The next two functions implement the equivalent of the K*rn shell |
|
649 // C-o operate-and-get-next-history-line editing command. Stolen from |
|
650 // the GNU Bourne Again SHell. |
269
|
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 |
581
|
694 // Accept the current line. |
269
|
695 rl_newline (); |
|
696 |
581
|
697 // Find the current line, and find the next line to use. |
269
|
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 |
581
|
739 // If the user simply hits return, this will produce an empty matrix. |
|
740 |
529
|
741 static Octave_object |
|
742 get_user_input (const Octave_object& args, int nargout, int debug = 0) |
|
743 { |
|
744 tree_constant retval; |
|
745 |
|
746 int nargin = args.length (); |
|
747 |
|
748 int read_as_string = 0; |
|
749 |
|
750 if (nargin == 3) |
|
751 read_as_string++; |
|
752 |
|
753 char *prompt = "debug> "; |
|
754 if (nargin > 1) |
|
755 { |
610
|
756 if (args(1).is_string ()) |
529
|
757 prompt = args(1).string_value (); |
|
758 else |
|
759 { |
|
760 error ("input: unrecognized argument"); |
|
761 return retval; |
|
762 } |
|
763 } |
|
764 |
|
765 again: |
|
766 |
|
767 flush_output_to_pager (); |
|
768 |
|
769 char *input_buf = gnu_readline (prompt); |
|
770 |
|
771 if (input_buf) |
|
772 { |
|
773 maybe_save_history (input_buf); |
|
774 |
|
775 int len = strlen (input_buf); |
|
776 |
|
777 if (len < 1) |
|
778 { |
|
779 if (debug) |
|
780 goto again; |
|
781 else |
|
782 return retval; |
|
783 } |
|
784 |
|
785 if (match_sans_spaces ("exit", input_buf) |
|
786 || match_sans_spaces ("quit", input_buf) |
|
787 || match_sans_spaces ("return", input_buf)) |
|
788 return tree_constant (); |
|
789 else if (read_as_string) |
|
790 retval = input_buf; |
|
791 else |
|
792 { |
|
793 int parse_status = 0; |
|
794 retval = eval_string (input_buf, 0, 0, parse_status); |
581
|
795 if (retval.is_defined ()) |
|
796 { |
|
797 if (debug) |
|
798 retval.eval (1); |
|
799 } |
|
800 else |
|
801 retval = tree_constant (Matrix ()); |
529
|
802 } |
|
803 } |
|
804 else |
|
805 error ("input: reading user-input failed!"); |
|
806 |
|
807 if (debug) |
|
808 goto again; |
|
809 |
|
810 return retval; |
|
811 } |
|
812 |
|
813 DEFUN ("input", Finput, Sinput, 3, 1, |
|
814 "input (PROMPT [, S])\n\ |
|
815 \n\ |
|
816 Prompt user for input. If the second argument is present, return |
|
817 value as a string.") |
|
818 { |
|
819 Octave_object retval; |
|
820 |
|
821 int nargin = args.length (); |
|
822 |
|
823 if (nargin == 2 || nargin == 3) |
|
824 retval = get_user_input (args, nargout); |
|
825 else |
|
826 print_usage ("input"); |
|
827 |
|
828 return retval; |
|
829 } |
|
830 |
|
831 DEFUN ("keyboard", Fkeyboard, Skeyboard, 2, 1, |
|
832 "keyboard (PROMPT)\n\ |
|
833 \n\ |
|
834 maybe help in debugging function files") |
|
835 { |
|
836 Octave_object retval; |
|
837 |
|
838 int nargin = args.length (); |
|
839 |
|
840 if (nargin == 1 || nargin == 2) |
|
841 retval = get_user_input (args, nargout, 1); |
|
842 else |
|
843 print_usage ("keyboard"); |
|
844 |
|
845 return retval; |
|
846 } |
|
847 |
1
|
848 /* |
|
849 ;;; Local Variables: *** |
|
850 ;;; mode: C++ *** |
|
851 ;;; page-delimiter: "^/\\*" *** |
|
852 ;;; End: *** |
|
853 */ |