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