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