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