Mercurial > hg > octave-lyh
annotate src/input.cc @ 7787:6b521b1e3631
Add dbquit and make dbstep compatible. Use parser in debug mode to handle multi-line input
author | David Bateman <dbateman@free.fr> |
---|---|
date | Tue, 13 May 2008 21:12:12 +0200 |
parents | 8e14a01ffe9f |
children | 5640a70cbab1 |
rev | line source |
---|---|
1 | 1 /* |
2 | |
7017 | 3 Copyright (C) 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, |
4 2002, 2003, 2004, 2005, 2006, 2007 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 | |
7016 | 10 Free Software Foundation; either version 3 of the License, or (at your |
11 option) any later version. | |
1 | 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 | |
7016 | 19 along with Octave; see the file COPYING. If not, see |
20 <http://www.gnu.org/licenses/>. | |
1 | 21 |
22 */ | |
23 | |
2939 | 24 // Get command input interactively or from files. |
1 | 25 |
240 | 26 #ifdef HAVE_CONFIG_H |
1192 | 27 #include <config.h> |
1 | 28 #endif |
29 | |
1343 | 30 #include <cstdio> |
31 #include <cstdlib> | |
32 #include <cstring> | |
33 #include <cassert> | |
34 | |
3503 | 35 #include <iostream> |
5765 | 36 #include <sstream> |
1728 | 37 #include <string> |
38 | |
1350 | 39 #ifdef HAVE_UNISTD_H |
2442 | 40 #ifdef HAVE_SYS_TYPES_H |
529 | 41 #include <sys/types.h> |
2442 | 42 #endif |
529 | 43 #include <unistd.h> |
44 #endif | |
1343 | 45 |
2927 | 46 #include "cmd-edit.h" |
3189 | 47 #include "file-ops.h" |
4153 | 48 #include "quit.h" |
1755 | 49 #include "str-vec.h" |
50 | |
1352 | 51 #include "defun.h" |
52 #include "dirfns.h" | |
1 | 53 #include "error.h" |
2181 | 54 #include "gripes.h" |
3016 | 55 #include "help.h" |
1 | 56 #include "input.h" |
5832 | 57 #include "load-path.h" |
1352 | 58 #include "oct-map.h" |
1742 | 59 #include "oct-hist.h" |
1670 | 60 #include "toplev.h" |
1755 | 61 #include "oct-obj.h" |
1 | 62 #include "pager.h" |
529 | 63 #include "parse.h" |
1352 | 64 #include "pathlen.h" |
3772 | 65 #include "pt.h" |
1755 | 66 #include "pt-const.h" |
3805 | 67 #include "pt-stmt.h" |
1352 | 68 #include "sighandlers.h" |
1114 | 69 #include "sysdep.h" |
7752
40c428ea3408
initial implementation of dbup and dbdown
John W. Eaton <jwe@octave.org>
parents:
7734
diff
changeset
|
70 #include "toplev.h" |
3098 | 71 #include "unwind-prot.h" |
1352 | 72 #include "utils.h" |
73 #include "variables.h" | |
529 | 74 |
2181 | 75 // Primary prompt string. |
5794 | 76 static std::string VPS1 = "\\s:\\#> "; |
2181 | 77 |
78 // Secondary prompt string. | |
5794 | 79 static std::string VPS2 = "> "; |
2181 | 80 |
81 // String printed before echoed input (enabled by --echo-input). | |
5794 | 82 std::string VPS4 = "+ "; |
2181 | 83 |
3019 | 84 // Echo commands as they are executed? |
85 // | |
86 // 1 ==> echo commands read from script files | |
87 // 2 ==> echo commands from functions | |
88 // 4 ==> echo commands read from command line | |
89 // | |
90 // more than one state can be active at once. | |
5794 | 91 int Vecho_executing_commands = ECHO_OFF; |
3019 | 92 |
3165 | 93 // The time we last printed a prompt. |
5832 | 94 octave_time Vlast_prompt_time = 0.0; |
3165 | 95 |
2181 | 96 // Character to append after successful command-line completion attempts. |
5794 | 97 static char Vcompletion_append_char = ' '; |
2181 | 98 |
1 | 99 // Global pointer for eval(). |
3523 | 100 std::string current_eval_string; |
1 | 101 |
3019 | 102 // TRUE means get input from current_eval_string. |
103 bool get_input_from_eval_string = false; | |
1 | 104 |
3877 | 105 // TRUE means we haven't been asked for the input from |
106 // current_eval_string yet. | |
107 bool input_from_eval_string_pending = false; | |
108 | |
5189 | 109 // TRUE means that input is coming from a file that was named on |
110 // the command line. | |
111 bool input_from_command_line_file = false; | |
112 | |
3019 | 113 // TRUE means we're parsing a function file. |
114 bool reading_fcn_file = false; | |
1 | 115 |
338 | 116 // Simple name of function file we are reading. |
3523 | 117 std::string curr_fcn_file_name; |
1606 | 118 |
119 // Full name of file we are reading. | |
3523 | 120 std::string curr_fcn_file_full_name; |
1 | 121 |
3019 | 122 // TRUE means we're parsing a script file. |
123 bool reading_script_file = false; | |
1 | 124 |
125 // If we are reading from an M-file, this is it. | |
529 | 126 FILE *ff_instream = 0; |
1 | 127 |
3019 | 128 // TRUE means this is an interactive shell. |
129 bool interactive = false; | |
1 | 130 |
3019 | 131 // TRUE means the user forced this shell to be interactive (-i). |
132 bool forced_interactive = false; | |
1 | 133 |
134 // Should we issue a prompt? | |
135 int promptflag = 1; | |
136 | |
137 // The current line of input, from wherever. | |
3523 | 138 std::string current_input_line; |
1 | 139 |
3804 | 140 // TRUE after a call to completion_matches. |
2299 | 141 bool octave_completion_matches_called = false; |
142 | |
6257 | 143 // TRUE if the plotting system has requested a call to drawnow at |
144 // the next user prompt. | |
7409 | 145 bool Vdrawnow_requested = false; |
6257 | 146 |
7752
40c428ea3408
initial implementation of dbup and dbdown
John W. Eaton <jwe@octave.org>
parents:
7734
diff
changeset
|
147 // TRUE if we are in debugging mode. |
40c428ea3408
initial implementation of dbup and dbdown
John W. Eaton <jwe@octave.org>
parents:
7734
diff
changeset
|
148 bool Vdebugging = false; |
40c428ea3408
initial implementation of dbup and dbdown
John W. Eaton <jwe@octave.org>
parents:
7734
diff
changeset
|
149 |
7787
6b521b1e3631
Add dbquit and make dbstep compatible. Use parser in debug mode to handle multi-line input
David Bateman <dbateman@free.fr>
parents:
7758
diff
changeset
|
150 // The current line that we are debugging |
6b521b1e3631
Add dbquit and make dbstep compatible. Use parser in debug mode to handle multi-line input
David Bateman <dbateman@free.fr>
parents:
7758
diff
changeset
|
151 int Vdebugging_current_line = -1; |
6b521b1e3631
Add dbquit and make dbstep compatible. Use parser in debug mode to handle multi-line input
David Bateman <dbateman@free.fr>
parents:
7758
diff
changeset
|
152 |
7294 | 153 // TRUE if we are running in the Emacs GUD mode. |
154 static bool Vgud_mode = false; | |
155 | |
1044 | 156 static void |
3523 | 157 do_input_echo (const std::string& input_string) |
1044 | 158 { |
1588 | 159 int do_echo = reading_script_file ? |
2205 | 160 (Vecho_executing_commands & ECHO_SCRIPTS) |
2618 | 161 : (Vecho_executing_commands & ECHO_CMD_LINE) && ! forced_interactive; |
1588 | 162 |
163 if (do_echo) | |
1044 | 164 { |
1403 | 165 if (forced_interactive) |
166 { | |
1755 | 167 if (promptflag > 0) |
5794 | 168 octave_stdout << command_editor::decode_prompt_string (VPS1); |
1755 | 169 else |
5794 | 170 octave_stdout << command_editor::decode_prompt_string (VPS2); |
1403 | 171 } |
172 else | |
5794 | 173 octave_stdout << command_editor::decode_prompt_string (VPS4); |
1044 | 174 |
1755 | 175 if (! input_string.empty ()) |
1044 | 176 { |
2095 | 177 octave_stdout << input_string; |
1755 | 178 |
179 if (input_string[input_string.length () - 1] != '\n') | |
2095 | 180 octave_stdout << "\n"; |
1044 | 181 } |
182 } | |
183 } | |
184 | |
3536 | 185 std::string |
3523 | 186 gnu_readline (const std::string& s, bool force_readline) |
1822 | 187 { |
5142 | 188 OCTAVE_QUIT; |
189 | |
3523 | 190 std::string retval; |
1822 | 191 |
2927 | 192 if (line_editing || force_readline) |
1822 | 193 { |
3219 | 194 bool eof; |
1898 | 195 |
3219 | 196 retval = command_editor::readline (s, eof); |
197 | |
198 if (! eof && retval.empty ()) | |
2927 | 199 retval = "\n"; |
1822 | 200 } |
201 else | |
202 { | |
2927 | 203 if (! s.empty () && (interactive || forced_interactive)) |
2618 | 204 { |
2927 | 205 FILE *stream = command_editor::get_output_stream (); |
206 | |
207 fprintf (stream, s.c_str ()); | |
208 fflush (stream); | |
2618 | 209 } |
1822 | 210 |
2927 | 211 FILE *curr_stream = command_editor::get_input_stream (); |
212 | |
1822 | 213 if (reading_fcn_file || reading_script_file) |
214 curr_stream = ff_instream; | |
215 | |
2927 | 216 retval = octave_fgets (curr_stream); |
1822 | 217 } |
218 | |
219 return retval; | |
220 } | |
581 | 221 |
6257 | 222 static inline std::string |
7459
d3fe4d466bc2
don't inhibit drawnow in debug mode
John W. Eaton <jwe@octave.org>
parents:
7409
diff
changeset
|
223 interactive_input (const std::string& s, bool force_readline = false) |
6257 | 224 { |
225 Vlast_prompt_time.stamp (); | |
226 | |
7459
d3fe4d466bc2
don't inhibit drawnow in debug mode
John W. Eaton <jwe@octave.org>
parents:
7409
diff
changeset
|
227 if (Vdrawnow_requested && (interactive || forced_interactive)) |
6257 | 228 { |
229 feval ("drawnow"); | |
230 | |
6367 | 231 flush_octave_stdout (); |
232 | |
6305 | 233 // We set Vdrawnow_requested to false even if there is an error |
234 // in drawnow so that the error doesn't reappear at every prompt. | |
235 | |
6257 | 236 Vdrawnow_requested = false; |
6305 | 237 |
238 if (error_state) | |
239 return "\n"; | |
6257 | 240 } |
241 | |
242 return gnu_readline (s, force_readline); | |
243 } | |
244 | |
3536 | 245 static std::string |
1 | 246 octave_gets (void) |
247 { | |
5142 | 248 OCTAVE_QUIT; |
249 | |
3523 | 250 std::string retval; |
1 | 251 |
1822 | 252 if ((interactive || forced_interactive) |
5832 | 253 && (! (reading_fcn_file |
254 || reading_script_file | |
255 || input_from_startup_file | |
256 || input_from_command_line_file))) | |
1 | 257 { |
5794 | 258 std::string ps = (promptflag > 0) ? VPS1 : VPS2; |
1755 | 259 |
3523 | 260 std::string prompt = command_editor::decode_prompt_string (ps); |
1 | 261 |
2618 | 262 pipe_handler_error_count = 0; |
263 | |
264 flush_octave_stdout (); | |
1 | 265 |
2095 | 266 octave_diary << prompt; |
581 | 267 |
6257 | 268 retval = interactive_input (prompt); |
5832 | 269 |
270 // There is no need to update the load_path cache if there is no | |
271 // user input. | |
7336 | 272 if (! retval.empty () && retval.find_first_not_of (" \t\n\r") != NPOS) |
5832 | 273 load_path::update (); |
1 | 274 } |
275 else | |
1822 | 276 retval = gnu_readline (""); |
1 | 277 |
2927 | 278 current_input_line = retval; |
1 | 279 |
1822 | 280 if (! current_input_line.empty ()) |
1 | 281 { |
5189 | 282 if (! (input_from_startup_file || input_from_command_line_file)) |
2927 | 283 command_history::add (current_input_line); |
1 | 284 |
3176 | 285 if (! (reading_fcn_file || reading_script_file)) |
286 { | |
287 octave_diary << current_input_line; | |
288 | |
289 if (current_input_line[current_input_line.length () - 1] != '\n') | |
290 octave_diary << "\n"; | |
291 } | |
581 | 292 |
1822 | 293 do_input_echo (current_input_line); |
1 | 294 } |
3176 | 295 else if (! (reading_fcn_file || reading_script_file)) |
296 octave_diary << "\n"; | |
581 | 297 |
1822 | 298 return retval; |
1 | 299 } |
300 | |
581 | 301 // Read a line from the input stream. |
302 | |
3536 | 303 static std::string |
1822 | 304 get_user_input (void) |
1 | 305 { |
5142 | 306 OCTAVE_QUIT; |
307 | |
3523 | 308 std::string retval; |
1 | 309 |
310 if (get_input_from_eval_string) | |
311 { | |
3877 | 312 if (input_from_eval_string_pending) |
313 { | |
314 input_from_eval_string_pending = false; | |
315 | |
316 retval = current_eval_string; | |
1822 | 317 |
3877 | 318 size_t len = retval.length (); |
1822 | 319 |
6206 | 320 if (len > 0 && retval[len-1] != '\n') |
3877 | 321 retval.append ("\n"); |
322 } | |
1822 | 323 } |
324 else | |
325 retval = octave_gets (); | |
326 | |
2927 | 327 current_input_line = retval; |
1822 | 328 |
2114 | 329 if (! get_input_from_eval_string) |
330 input_line_number++; | |
1822 | 331 |
332 return retval; | |
333 } | |
334 | |
335 int | |
336 octave_read (char *buf, unsigned max_size) | |
337 { | |
5775 | 338 // FIXME -- is this a safe way to buffer the input? |
2927 | 339 |
4046 | 340 static const char * const eol = "\n"; |
3523 | 341 static std::string input_buf; |
2927 | 342 static const char *pos = 0; |
343 static size_t chars_left = 0; | |
1822 | 344 |
345 int status = 0; | |
4046 | 346 if (chars_left == 0) |
1822 | 347 { |
2927 | 348 pos = 0; |
349 | |
350 input_buf = get_user_input (); | |
1822 | 351 |
2927 | 352 chars_left = input_buf.length (); |
353 | |
354 pos = input_buf.c_str (); | |
1822 | 355 } |
356 | |
357 if (chars_left > 0) | |
358 { | |
4046 | 359 size_t len = max_size > chars_left ? chars_left : max_size; |
2927 | 360 assert (len > 0); |
361 | |
4046 | 362 memcpy (buf, pos, len); |
1822 | 363 |
4046 | 364 chars_left -= len; |
365 pos += len; | |
1822 | 366 |
4046 | 367 // Make sure input ends with a new line character. |
368 if (chars_left == 0 && buf[len-1] != '\n') | |
369 { | |
370 if (len < max_size) | |
371 { | |
372 // There is enough room to plug the newline character in | |
373 // the buffer. | |
374 buf[len++] = '\n'; | |
375 } | |
376 else | |
377 { | |
378 // There isn't enough room to plug the newline character | |
379 // in the buffer so make sure it is returned on the next | |
380 // octave_read call. | |
381 pos = eol; | |
382 chars_left = 1; | |
383 } | |
1 | 384 } |
1822 | 385 |
4046 | 386 status = len; |
1 | 387 |
1044 | 388 } |
1822 | 389 else if (chars_left == 0) |
2862 | 390 { |
391 status = 0; | |
392 } | |
1822 | 393 else |
394 status = -1; | |
1044 | 395 |
1 | 396 return status; |
397 } | |
398 | |
581 | 399 // Fix things up so that input can come from file `name', printing a |
400 // warning if the file doesn't exist. | |
401 | |
1 | 402 FILE * |
3523 | 403 get_input_from_file (const std::string& name, int warn) |
1 | 404 { |
529 | 405 FILE *instream = 0; |
1 | 406 |
1750 | 407 if (name.length () > 0) |
7728
13820b9f5fd9
more consistent handling of CR/CRLF/LF line endings in lexer and parser
John W. Eaton <jwe@octave.org>
parents:
7719
diff
changeset
|
408 instream = fopen (name.c_str (), "rb"); |
1 | 409 |
529 | 410 if (! instream && warn) |
1750 | 411 warning ("%s: no such file or directory", name.c_str ()); |
1 | 412 |
338 | 413 if (reading_fcn_file || reading_script_file) |
339 | 414 ff_instream = instream; |
1 | 415 else |
2927 | 416 command_editor::set_input_stream (instream); |
1 | 417 |
418 return instream; | |
419 } | |
420 | |
581 | 421 // Fix things up so that input can come from the standard input. This |
422 // may need to become much more complicated, which is why it's in a | |
423 // separate function. | |
424 | |
1 | 425 FILE * |
426 get_input_from_stdin (void) | |
427 { | |
2927 | 428 command_editor::set_input_stream (stdin); |
429 return command_editor::get_input_stream (); | |
1 | 430 } |
431 | |
5775 | 432 // FIXME -- make this generate file names when appropriate. |
2921 | 433 |
2247 | 434 static string_vector |
3523 | 435 generate_possible_completions (const std::string& text, std::string& prefix, |
436 std::string& hint) | |
1280 | 437 { |
2247 | 438 string_vector names; |
1280 | 439 |
2921 | 440 prefix = ""; |
1280 | 441 |
4604 | 442 if (looks_like_struct (text)) |
1430 | 443 names = generate_struct_completions (text, prefix, hint); |
444 else | |
2247 | 445 names = make_name_list (); |
1280 | 446 |
2944 | 447 // Sort and remove duplicates. |
2348 | 448 |
2944 | 449 names.qsort (true); |
2348 | 450 |
1280 | 451 return names; |
452 } | |
453 | |
3536 | 454 static std::string |
3523 | 455 generate_completion (const std::string& text, int state) |
1280 | 456 { |
3523 | 457 std::string retval; |
2944 | 458 |
3523 | 459 static std::string prefix; |
460 static std::string hint; | |
1280 | 461 |
2921 | 462 static size_t hint_len = 0; |
1280 | 463 |
1 | 464 static int list_index = 0; |
2921 | 465 static int name_list_len = 0; |
4604 | 466 static int name_list_total_len = 0; |
2247 | 467 static string_vector name_list; |
4604 | 468 static string_vector file_name_list; |
1 | 469 |
1280 | 470 static int matches = 0; |
1 | 471 |
472 if (state == 0) | |
473 { | |
474 list_index = 0; | |
475 | |
2921 | 476 prefix = ""; |
1280 | 477 |
2921 | 478 hint = text; |
1280 | 479 |
480 name_list = generate_possible_completions (text, prefix, hint); | |
481 | |
2921 | 482 name_list_len = name_list.length (); |
483 | |
4604 | 484 file_name_list = command_editor::generate_filename_completions (text); |
485 | |
486 name_list.append (file_name_list); | |
487 | |
488 name_list_total_len = name_list.length (); | |
489 | |
2921 | 490 hint_len = hint.length (); |
1280 | 491 |
492 matches = 0; | |
2247 | 493 |
2921 | 494 for (int i = 0; i < name_list_len; i++) |
3565 | 495 if (hint == name_list[i].substr (0, hint_len)) |
2247 | 496 matches++; |
1 | 497 } |
498 | |
4604 | 499 if (name_list_total_len > 0 && matches > 0) |
1 | 500 { |
4604 | 501 while (list_index < name_list_total_len) |
244 | 502 { |
3523 | 503 std::string name = name_list[list_index]; |
2247 | 504 |
1280 | 505 list_index++; |
2247 | 506 |
3565 | 507 if (hint == name.substr (0, hint_len)) |
1280 | 508 { |
4604 | 509 if (list_index <= name_list_len && ! prefix.empty ()) |
2944 | 510 retval = prefix + "." + name; |
511 else | |
512 retval = name; | |
1280 | 513 |
5775 | 514 // FIXME -- looks_like_struct is broken for now, |
4179 | 515 // so it always returns false. |
516 | |
6979 | 517 if (matches == 1 && looks_like_struct (retval)) |
4179 | 518 { |
519 // Don't append anything, since we don't know | |
520 // whether it should be '(' or '.'. | |
521 | |
522 command_editor::set_completion_append_character ('\0'); | |
523 } | |
524 else | |
525 command_editor::set_completion_append_character | |
526 (Vcompletion_append_char); | |
1280 | 527 |
2944 | 528 break; |
1280 | 529 } |
244 | 530 } |
1 | 531 } |
532 | |
2944 | 533 return retval; |
1 | 534 } |
535 | |
6979 | 536 static std::string |
537 quoting_filename (const std::string &text, int, char quote) | |
538 { | |
539 if (quote) | |
540 return text; | |
541 else | |
542 return (std::string ("'") + text); | |
543 } | |
544 | |
2927 | 545 void |
546 initialize_command_input (void) | |
547 { | |
548 // If we are using readline, this allows conditional parsing of the | |
549 // .inputrc file. | |
269 | 550 |
2927 | 551 command_editor::set_name ("Octave"); |
1358 | 552 |
5775 | 553 // FIXME -- this needs to include a comma too, but that |
3933 | 554 // causes trouble for the new struct element completion code. |
555 | |
4272 | 556 static const char *s = "\t\n !\"\'*+-/:;<=>(){}[\\]^`~"; |
3933 | 557 |
558 command_editor::set_basic_word_break_characters (s); | |
559 | |
560 command_editor::set_completer_word_break_characters (s); | |
561 | |
3004 | 562 command_editor::set_basic_quote_characters ("\""); |
2944 | 563 |
6979 | 564 command_editor::set_filename_quote_characters (" \t\n\\\"'@<>=;|&()#$`?*[!:{"); |
565 command_editor::set_completer_quote_characters ("'\""); | |
566 | |
2944 | 567 command_editor::set_completion_function (generate_completion); |
6979 | 568 |
569 command_editor::set_quoting_function (quoting_filename); | |
269 | 570 } |
571 | |
7787
6b521b1e3631
Add dbquit and make dbstep compatible. Use parser in debug mode to handle multi-line input
David Bateman <dbateman@free.fr>
parents:
7758
diff
changeset
|
572 static void |
6b521b1e3631
Add dbquit and make dbstep compatible. Use parser in debug mode to handle multi-line input
David Bateman <dbateman@free.fr>
parents:
7758
diff
changeset
|
573 get_debug_input (const std::string& prompt) |
269 | 574 { |
7787
6b521b1e3631
Add dbquit and make dbstep compatible. Use parser in debug mode to handle multi-line input
David Bateman <dbateman@free.fr>
parents:
7758
diff
changeset
|
575 octave_user_code *caller = octave_call_stack::caller_user_code (); |
6b521b1e3631
Add dbquit and make dbstep compatible. Use parser in debug mode to handle multi-line input
David Bateman <dbateman@free.fr>
parents:
7758
diff
changeset
|
576 std::string nm; |
6b521b1e3631
Add dbquit and make dbstep compatible. Use parser in debug mode to handle multi-line input
David Bateman <dbateman@free.fr>
parents:
7758
diff
changeset
|
577 |
6b521b1e3631
Add dbquit and make dbstep compatible. Use parser in debug mode to handle multi-line input
David Bateman <dbateman@free.fr>
parents:
7758
diff
changeset
|
578 if (caller) |
6b521b1e3631
Add dbquit and make dbstep compatible. Use parser in debug mode to handle multi-line input
David Bateman <dbateman@free.fr>
parents:
7758
diff
changeset
|
579 { |
6b521b1e3631
Add dbquit and make dbstep compatible. Use parser in debug mode to handle multi-line input
David Bateman <dbateman@free.fr>
parents:
7758
diff
changeset
|
580 nm = caller->fcn_file_name (); |
6b521b1e3631
Add dbquit and make dbstep compatible. Use parser in debug mode to handle multi-line input
David Bateman <dbateman@free.fr>
parents:
7758
diff
changeset
|
581 |
6b521b1e3631
Add dbquit and make dbstep compatible. Use parser in debug mode to handle multi-line input
David Bateman <dbateman@free.fr>
parents:
7758
diff
changeset
|
582 if (nm.empty ()) |
6b521b1e3631
Add dbquit and make dbstep compatible. Use parser in debug mode to handle multi-line input
David Bateman <dbateman@free.fr>
parents:
7758
diff
changeset
|
583 nm = caller->name (); |
6b521b1e3631
Add dbquit and make dbstep compatible. Use parser in debug mode to handle multi-line input
David Bateman <dbateman@free.fr>
parents:
7758
diff
changeset
|
584 |
6b521b1e3631
Add dbquit and make dbstep compatible. Use parser in debug mode to handle multi-line input
David Bateman <dbateman@free.fr>
parents:
7758
diff
changeset
|
585 Vdebugging_current_line = octave_call_stack::current_line (); |
6b521b1e3631
Add dbquit and make dbstep compatible. Use parser in debug mode to handle multi-line input
David Bateman <dbateman@free.fr>
parents:
7758
diff
changeset
|
586 } |
6b521b1e3631
Add dbquit and make dbstep compatible. Use parser in debug mode to handle multi-line input
David Bateman <dbateman@free.fr>
parents:
7758
diff
changeset
|
587 else |
6b521b1e3631
Add dbquit and make dbstep compatible. Use parser in debug mode to handle multi-line input
David Bateman <dbateman@free.fr>
parents:
7758
diff
changeset
|
588 Vdebugging_current_line = -1; |
3180 | 589 |
7787
6b521b1e3631
Add dbquit and make dbstep compatible. Use parser in debug mode to handle multi-line input
David Bateman <dbateman@free.fr>
parents:
7758
diff
changeset
|
590 std::ostringstream buf; |
6b521b1e3631
Add dbquit and make dbstep compatible. Use parser in debug mode to handle multi-line input
David Bateman <dbateman@free.fr>
parents:
7758
diff
changeset
|
591 |
6b521b1e3631
Add dbquit and make dbstep compatible. Use parser in debug mode to handle multi-line input
David Bateman <dbateman@free.fr>
parents:
7758
diff
changeset
|
592 if (! nm.empty ()) |
3180 | 593 { |
7787
6b521b1e3631
Add dbquit and make dbstep compatible. Use parser in debug mode to handle multi-line input
David Bateman <dbateman@free.fr>
parents:
7758
diff
changeset
|
594 if (Vgud_mode) |
6b521b1e3631
Add dbquit and make dbstep compatible. Use parser in debug mode to handle multi-line input
David Bateman <dbateman@free.fr>
parents:
7758
diff
changeset
|
595 { |
6b521b1e3631
Add dbquit and make dbstep compatible. Use parser in debug mode to handle multi-line input
David Bateman <dbateman@free.fr>
parents:
7758
diff
changeset
|
596 static char ctrl_z = 'Z' & 0x1f; |
269 | 597 |
7787
6b521b1e3631
Add dbquit and make dbstep compatible. Use parser in debug mode to handle multi-line input
David Bateman <dbateman@free.fr>
parents:
7758
diff
changeset
|
598 buf << ctrl_z << ctrl_z << nm << ":" << Vdebugging_current_line; |
6b521b1e3631
Add dbquit and make dbstep compatible. Use parser in debug mode to handle multi-line input
David Bateman <dbateman@free.fr>
parents:
7758
diff
changeset
|
599 } |
6b521b1e3631
Add dbquit and make dbstep compatible. Use parser in debug mode to handle multi-line input
David Bateman <dbateman@free.fr>
parents:
7758
diff
changeset
|
600 else |
6b521b1e3631
Add dbquit and make dbstep compatible. Use parser in debug mode to handle multi-line input
David Bateman <dbateman@free.fr>
parents:
7758
diff
changeset
|
601 { |
6b521b1e3631
Add dbquit and make dbstep compatible. Use parser in debug mode to handle multi-line input
David Bateman <dbateman@free.fr>
parents:
7758
diff
changeset
|
602 buf << "stopped in " << nm; |
1 | 603 |
7787
6b521b1e3631
Add dbquit and make dbstep compatible. Use parser in debug mode to handle multi-line input
David Bateman <dbateman@free.fr>
parents:
7758
diff
changeset
|
604 if (Vdebugging_current_line > 0) |
6b521b1e3631
Add dbquit and make dbstep compatible. Use parser in debug mode to handle multi-line input
David Bateman <dbateman@free.fr>
parents:
7758
diff
changeset
|
605 buf << " at line " << Vdebugging_current_line; |
6b521b1e3631
Add dbquit and make dbstep compatible. Use parser in debug mode to handle multi-line input
David Bateman <dbateman@free.fr>
parents:
7758
diff
changeset
|
606 } |
3180 | 607 } |
608 | |
7787
6b521b1e3631
Add dbquit and make dbstep compatible. Use parser in debug mode to handle multi-line input
David Bateman <dbateman@free.fr>
parents:
7758
diff
changeset
|
609 std::string msg = buf.str (); |
6b521b1e3631
Add dbquit and make dbstep compatible. Use parser in debug mode to handle multi-line input
David Bateman <dbateman@free.fr>
parents:
7758
diff
changeset
|
610 |
6b521b1e3631
Add dbquit and make dbstep compatible. Use parser in debug mode to handle multi-line input
David Bateman <dbateman@free.fr>
parents:
7758
diff
changeset
|
611 if (! msg.empty ()) |
6b521b1e3631
Add dbquit and make dbstep compatible. Use parser in debug mode to handle multi-line input
David Bateman <dbateman@free.fr>
parents:
7758
diff
changeset
|
612 message (Vgud_mode ? 0 : "keyboard", msg.c_str ()); |
6b521b1e3631
Add dbquit and make dbstep compatible. Use parser in debug mode to handle multi-line input
David Bateman <dbateman@free.fr>
parents:
7758
diff
changeset
|
613 |
6b521b1e3631
Add dbquit and make dbstep compatible. Use parser in debug mode to handle multi-line input
David Bateman <dbateman@free.fr>
parents:
7758
diff
changeset
|
614 unwind_protect::begin_frame ("get_debug_input"); |
6b521b1e3631
Add dbquit and make dbstep compatible. Use parser in debug mode to handle multi-line input
David Bateman <dbateman@free.fr>
parents:
7758
diff
changeset
|
615 |
6b521b1e3631
Add dbquit and make dbstep compatible. Use parser in debug mode to handle multi-line input
David Bateman <dbateman@free.fr>
parents:
7758
diff
changeset
|
616 unwind_protect_str (VPS1); |
6b521b1e3631
Add dbquit and make dbstep compatible. Use parser in debug mode to handle multi-line input
David Bateman <dbateman@free.fr>
parents:
7758
diff
changeset
|
617 VPS1 = prompt; |
6b521b1e3631
Add dbquit and make dbstep compatible. Use parser in debug mode to handle multi-line input
David Bateman <dbateman@free.fr>
parents:
7758
diff
changeset
|
618 |
6b521b1e3631
Add dbquit and make dbstep compatible. Use parser in debug mode to handle multi-line input
David Bateman <dbateman@free.fr>
parents:
7758
diff
changeset
|
619 while (Vdebugging) |
6b521b1e3631
Add dbquit and make dbstep compatible. Use parser in debug mode to handle multi-line input
David Bateman <dbateman@free.fr>
parents:
7758
diff
changeset
|
620 { |
6b521b1e3631
Add dbquit and make dbstep compatible. Use parser in debug mode to handle multi-line input
David Bateman <dbateman@free.fr>
parents:
7758
diff
changeset
|
621 reset_error_handler (); |
6b521b1e3631
Add dbquit and make dbstep compatible. Use parser in debug mode to handle multi-line input
David Bateman <dbateman@free.fr>
parents:
7758
diff
changeset
|
622 |
6b521b1e3631
Add dbquit and make dbstep compatible. Use parser in debug mode to handle multi-line input
David Bateman <dbateman@free.fr>
parents:
7758
diff
changeset
|
623 reset_parser (); |
6b521b1e3631
Add dbquit and make dbstep compatible. Use parser in debug mode to handle multi-line input
David Bateman <dbateman@free.fr>
parents:
7758
diff
changeset
|
624 |
6b521b1e3631
Add dbquit and make dbstep compatible. Use parser in debug mode to handle multi-line input
David Bateman <dbateman@free.fr>
parents:
7758
diff
changeset
|
625 // This is the same as yyparse in parse.y. |
6b521b1e3631
Add dbquit and make dbstep compatible. Use parser in debug mode to handle multi-line input
David Bateman <dbateman@free.fr>
parents:
7758
diff
changeset
|
626 int retval = octave_parse (); |
6b521b1e3631
Add dbquit and make dbstep compatible. Use parser in debug mode to handle multi-line input
David Bateman <dbateman@free.fr>
parents:
7758
diff
changeset
|
627 |
6b521b1e3631
Add dbquit and make dbstep compatible. Use parser in debug mode to handle multi-line input
David Bateman <dbateman@free.fr>
parents:
7758
diff
changeset
|
628 if (retval == 0 && global_command) |
6b521b1e3631
Add dbquit and make dbstep compatible. Use parser in debug mode to handle multi-line input
David Bateman <dbateman@free.fr>
parents:
7758
diff
changeset
|
629 { |
6b521b1e3631
Add dbquit and make dbstep compatible. Use parser in debug mode to handle multi-line input
David Bateman <dbateman@free.fr>
parents:
7758
diff
changeset
|
630 global_command->eval (); |
6b521b1e3631
Add dbquit and make dbstep compatible. Use parser in debug mode to handle multi-line input
David Bateman <dbateman@free.fr>
parents:
7758
diff
changeset
|
631 |
6b521b1e3631
Add dbquit and make dbstep compatible. Use parser in debug mode to handle multi-line input
David Bateman <dbateman@free.fr>
parents:
7758
diff
changeset
|
632 delete global_command; |
6b521b1e3631
Add dbquit and make dbstep compatible. Use parser in debug mode to handle multi-line input
David Bateman <dbateman@free.fr>
parents:
7758
diff
changeset
|
633 |
6b521b1e3631
Add dbquit and make dbstep compatible. Use parser in debug mode to handle multi-line input
David Bateman <dbateman@free.fr>
parents:
7758
diff
changeset
|
634 global_command = 0; |
6b521b1e3631
Add dbquit and make dbstep compatible. Use parser in debug mode to handle multi-line input
David Bateman <dbateman@free.fr>
parents:
7758
diff
changeset
|
635 |
6b521b1e3631
Add dbquit and make dbstep compatible. Use parser in debug mode to handle multi-line input
David Bateman <dbateman@free.fr>
parents:
7758
diff
changeset
|
636 OCTAVE_QUIT; |
6b521b1e3631
Add dbquit and make dbstep compatible. Use parser in debug mode to handle multi-line input
David Bateman <dbateman@free.fr>
parents:
7758
diff
changeset
|
637 |
6b521b1e3631
Add dbquit and make dbstep compatible. Use parser in debug mode to handle multi-line input
David Bateman <dbateman@free.fr>
parents:
7758
diff
changeset
|
638 if (octave_completion_matches_called) |
6b521b1e3631
Add dbquit and make dbstep compatible. Use parser in debug mode to handle multi-line input
David Bateman <dbateman@free.fr>
parents:
7758
diff
changeset
|
639 octave_completion_matches_called = false; |
6b521b1e3631
Add dbquit and make dbstep compatible. Use parser in debug mode to handle multi-line input
David Bateman <dbateman@free.fr>
parents:
7758
diff
changeset
|
640 } |
6b521b1e3631
Add dbquit and make dbstep compatible. Use parser in debug mode to handle multi-line input
David Bateman <dbateman@free.fr>
parents:
7758
diff
changeset
|
641 } |
6b521b1e3631
Add dbquit and make dbstep compatible. Use parser in debug mode to handle multi-line input
David Bateman <dbateman@free.fr>
parents:
7758
diff
changeset
|
642 |
6b521b1e3631
Add dbquit and make dbstep compatible. Use parser in debug mode to handle multi-line input
David Bateman <dbateman@free.fr>
parents:
7758
diff
changeset
|
643 unwind_protect::run_frame ("get_debug_input"); |
529 | 644 } |
645 | |
581 | 646 // If the user simply hits return, this will produce an empty matrix. |
647 | |
2086 | 648 static octave_value_list |
7752
40c428ea3408
initial implementation of dbup and dbdown
John W. Eaton <jwe@octave.org>
parents:
7734
diff
changeset
|
649 get_user_input (const octave_value_list& args, int nargout) |
529 | 650 { |
3100 | 651 octave_value_list retval; |
529 | 652 |
653 int nargin = args.length (); | |
654 | |
655 int read_as_string = 0; | |
656 | |
712 | 657 if (nargin == 2) |
529 | 658 read_as_string++; |
659 | |
7787
6b521b1e3631
Add dbquit and make dbstep compatible. Use parser in debug mode to handle multi-line input
David Bateman <dbateman@free.fr>
parents:
7758
diff
changeset
|
660 std::string prompt = args(0).string_value (); |
4975 | 661 |
7787
6b521b1e3631
Add dbquit and make dbstep compatible. Use parser in debug mode to handle multi-line input
David Bateman <dbateman@free.fr>
parents:
7758
diff
changeset
|
662 if (error_state) |
6b521b1e3631
Add dbquit and make dbstep compatible. Use parser in debug mode to handle multi-line input
David Bateman <dbateman@free.fr>
parents:
7758
diff
changeset
|
663 { |
6b521b1e3631
Add dbquit and make dbstep compatible. Use parser in debug mode to handle multi-line input
David Bateman <dbateman@free.fr>
parents:
7758
diff
changeset
|
664 error ("input: unrecognized argument"); |
6b521b1e3631
Add dbquit and make dbstep compatible. Use parser in debug mode to handle multi-line input
David Bateman <dbateman@free.fr>
parents:
7758
diff
changeset
|
665 return retval; |
4975 | 666 } |
667 | |
2095 | 668 flush_octave_stdout (); |
529 | 669 |
4565 | 670 octave_diary << prompt; |
671 | |
7459
d3fe4d466bc2
don't inhibit drawnow in debug mode
John W. Eaton <jwe@octave.org>
parents:
7409
diff
changeset
|
672 std::string input_buf = interactive_input (prompt.c_str (), true); |
529 | 673 |
7459
d3fe4d466bc2
don't inhibit drawnow in debug mode
John W. Eaton <jwe@octave.org>
parents:
7409
diff
changeset
|
674 if (! (error_state || input_buf.empty ())) |
529 | 675 { |
1799 | 676 if (! input_from_startup_file) |
2927 | 677 command_history::add (input_buf); |
529 | 678 |
2927 | 679 size_t len = input_buf.length (); |
529 | 680 |
4565 | 681 octave_diary << input_buf; |
682 | |
683 if (input_buf[len - 1] != '\n') | |
684 octave_diary << "\n"; | |
685 | |
529 | 686 if (len < 1) |
7787
6b521b1e3631
Add dbquit and make dbstep compatible. Use parser in debug mode to handle multi-line input
David Bateman <dbateman@free.fr>
parents:
7758
diff
changeset
|
687 return read_as_string ? octave_value ("") : octave_value (Matrix ()); |
3772 | 688 |
689 if (read_as_string) | |
963 | 690 { |
5775 | 691 // FIXME -- fix gnu_readline and octave_gets instead! |
3081 | 692 if (input_buf.length () == 1 && input_buf[0] == '\n') |
3100 | 693 retval(0) = ""; |
3081 | 694 else |
3100 | 695 retval(0) = input_buf; |
963 | 696 } |
529 | 697 else |
698 { | |
699 int parse_status = 0; | |
2900 | 700 |
7787
6b521b1e3631
Add dbquit and make dbstep compatible. Use parser in debug mode to handle multi-line input
David Bateman <dbateman@free.fr>
parents:
7758
diff
changeset
|
701 retval = eval_string (input_buf, true, parse_status, nargout); |
3100 | 702 |
7752
40c428ea3408
initial implementation of dbup and dbdown
John W. Eaton <jwe@octave.org>
parents:
7734
diff
changeset
|
703 if (! Vdebugging && retval.length () == 0) |
3100 | 704 retval(0) = Matrix (); |
529 | 705 } |
706 } | |
707 else | |
708 error ("input: reading user-input failed!"); | |
709 | |
710 return retval; | |
711 } | |
712 | |
3100 | 713 DEFUN (input, args, nargout, |
3372 | 714 "-*- texinfo -*-\n\ |
715 @deftypefn {Built-in Function} {} input (@var{prompt})\n\ | |
716 @deftypefnx {Built-in Function} {} input (@var{prompt}, \"s\")\n\ | |
717 Print a prompt and wait for user input. For example,\n\ | |
718 \n\ | |
719 @example\n\ | |
720 input (\"Pick a number, any number! \")\n\ | |
721 @end example\n\ | |
722 \n\ | |
723 @noindent\n\ | |
724 prints the prompt\n\ | |
725 \n\ | |
726 @example\n\ | |
727 Pick a number, any number!\n\ | |
728 @end example\n\ | |
529 | 729 \n\ |
3372 | 730 @noindent\n\ |
731 and waits for the user to enter a value. The string entered by the user\n\ | |
732 is evaluated as an expression, so it may be a literal constant, a\n\ | |
733 variable name, or any other valid expression.\n\ | |
734 \n\ | |
735 Currently, @code{input} only returns one value, regardless of the number\n\ | |
736 of values produced by the evaluation of the expression.\n\ | |
737 \n\ | |
738 If you are only interested in getting a literal string value, you can\n\ | |
739 call @code{input} with the character string @code{\"s\"} as the second\n\ | |
740 argument. This tells Octave to return the string entered by the user\n\ | |
741 directly, without evaluating it first.\n\ | |
742 \n\ | |
743 Because there may be output waiting to be displayed by the pager, it is\n\ | |
744 a good idea to always call @code{fflush (stdout)} before calling\n\ | |
745 @code{input}. This will ensure that all pending output is written to\n\ | |
746 the screen before your prompt. @xref{Input and Output}.\n\ | |
747 @end deftypefn") | |
529 | 748 { |
2086 | 749 octave_value_list retval; |
529 | 750 |
751 int nargin = args.length (); | |
752 | |
712 | 753 if (nargin == 1 || nargin == 2) |
7752
40c428ea3408
initial implementation of dbup and dbdown
John W. Eaton <jwe@octave.org>
parents:
7734
diff
changeset
|
754 retval = get_user_input (args, nargout); |
529 | 755 else |
5823 | 756 print_usage (); |
529 | 757 |
758 return retval; | |
759 } | |
760 | |
5640 | 761 bool |
762 octave_yes_or_no (const std::string& prompt) | |
763 { | |
764 std::string prompt_string = prompt + "(yes or no) "; | |
765 | |
766 while (1) | |
767 { | |
7459
d3fe4d466bc2
don't inhibit drawnow in debug mode
John W. Eaton <jwe@octave.org>
parents:
7409
diff
changeset
|
768 std::string input_buf = interactive_input (prompt_string, true); |
5640 | 769 |
770 if (input_buf == "yes") | |
771 return true; | |
772 else if (input_buf == "no") | |
773 return false; | |
774 else | |
775 message (0, "Please answer yes or no."); | |
776 } | |
777 } | |
778 | |
779 DEFUN (yes_or_no, args, , | |
780 "-*- texinfo -*-\n\ | |
781 @deftypefn {Built-in Function} {} yes_or_no (@var{prompt})\n\ | |
782 Ask the user a yes-or-no question. Return 1 if the answer is yes.\n\ | |
783 Takes one argument, which is the string to display to ask the\n\ | |
784 question. It should end in a space; @samp{yes-or-no-p} adds\n\ | |
785 @samp{(yes or no) } to it. The user must confirm the answer with\n\ | |
786 RET and can edit it until it has been confirmed.\n\ | |
787 @end deftypefn") | |
788 { | |
789 octave_value retval; | |
790 | |
791 int nargin = args.length (); | |
792 | |
793 if (nargin == 0 || nargin == 1) | |
794 { | |
795 std::string prompt; | |
796 | |
797 if (nargin == 1) | |
798 { | |
799 prompt = args(0).string_value (); | |
800 | |
801 if (error_state) | |
802 { | |
803 error ("yes_or_no: expecting argument to be character string"); | |
804 return retval; | |
805 } | |
806 } | |
807 | |
808 retval = octave_yes_or_no (prompt); | |
809 } | |
810 else | |
5823 | 811 print_usage (); |
5640 | 812 |
813 return retval; | |
814 } | |
815 | |
3098 | 816 static void |
817 restore_command_history (void *) | |
818 { | |
819 command_history::ignore_entries (! Vsaving_history); | |
820 } | |
821 | |
7752
40c428ea3408
initial implementation of dbup and dbdown
John W. Eaton <jwe@octave.org>
parents:
7734
diff
changeset
|
822 static size_t saved_frame = 0; |
40c428ea3408
initial implementation of dbup and dbdown
John W. Eaton <jwe@octave.org>
parents:
7734
diff
changeset
|
823 |
40c428ea3408
initial implementation of dbup and dbdown
John W. Eaton <jwe@octave.org>
parents:
7734
diff
changeset
|
824 static void |
40c428ea3408
initial implementation of dbup and dbdown
John W. Eaton <jwe@octave.org>
parents:
7734
diff
changeset
|
825 restore_frame (void *) |
40c428ea3408
initial implementation of dbup and dbdown
John W. Eaton <jwe@octave.org>
parents:
7734
diff
changeset
|
826 { |
40c428ea3408
initial implementation of dbup and dbdown
John W. Eaton <jwe@octave.org>
parents:
7734
diff
changeset
|
827 octave_call_stack::goto_frame (saved_frame); |
40c428ea3408
initial implementation of dbup and dbdown
John W. Eaton <jwe@octave.org>
parents:
7734
diff
changeset
|
828 } |
40c428ea3408
initial implementation of dbup and dbdown
John W. Eaton <jwe@octave.org>
parents:
7734
diff
changeset
|
829 |
3707 | 830 octave_value |
831 do_keyboard (const octave_value_list& args) | |
832 { | |
833 octave_value retval; | |
834 | |
835 int nargin = args.length (); | |
836 | |
837 assert (nargin == 0 || nargin == 1); | |
838 | |
839 unwind_protect::begin_frame ("do_keyboard"); | |
840 | |
5775 | 841 // FIXME -- we shouldn't need both the |
3707 | 842 // command_history object and the |
843 // Vsaving_history variable... | |
844 command_history::ignore_entries (false); | |
845 | |
846 unwind_protect::add (restore_command_history, 0); | |
847 | |
848 unwind_protect_bool (Vsaving_history); | |
7752
40c428ea3408
initial implementation of dbup and dbdown
John W. Eaton <jwe@octave.org>
parents:
7734
diff
changeset
|
849 unwind_protect_bool (Vdebugging); |
40c428ea3408
initial implementation of dbup and dbdown
John W. Eaton <jwe@octave.org>
parents:
7734
diff
changeset
|
850 |
40c428ea3408
initial implementation of dbup and dbdown
John W. Eaton <jwe@octave.org>
parents:
7734
diff
changeset
|
851 saved_frame = octave_call_stack::current_frame (); |
40c428ea3408
initial implementation of dbup and dbdown
John W. Eaton <jwe@octave.org>
parents:
7734
diff
changeset
|
852 unwind_protect::add (restore_frame); |
40c428ea3408
initial implementation of dbup and dbdown
John W. Eaton <jwe@octave.org>
parents:
7734
diff
changeset
|
853 unwind_protect_size_t (saved_frame); |
3707 | 854 |
855 Vsaving_history = true; | |
7752
40c428ea3408
initial implementation of dbup and dbdown
John W. Eaton <jwe@octave.org>
parents:
7734
diff
changeset
|
856 Vdebugging = true; |
3707 | 857 |
7787
6b521b1e3631
Add dbquit and make dbstep compatible. Use parser in debug mode to handle multi-line input
David Bateman <dbateman@free.fr>
parents:
7758
diff
changeset
|
858 std::string prompt = "debug> "; |
6b521b1e3631
Add dbquit and make dbstep compatible. Use parser in debug mode to handle multi-line input
David Bateman <dbateman@free.fr>
parents:
7758
diff
changeset
|
859 if (nargin > 0) |
6b521b1e3631
Add dbquit and make dbstep compatible. Use parser in debug mode to handle multi-line input
David Bateman <dbateman@free.fr>
parents:
7758
diff
changeset
|
860 prompt = args(0).string_value (); |
4233 | 861 |
7787
6b521b1e3631
Add dbquit and make dbstep compatible. Use parser in debug mode to handle multi-line input
David Bateman <dbateman@free.fr>
parents:
7758
diff
changeset
|
862 if (! error_state) |
6b521b1e3631
Add dbquit and make dbstep compatible. Use parser in debug mode to handle multi-line input
David Bateman <dbateman@free.fr>
parents:
7758
diff
changeset
|
863 get_debug_input (prompt); |
3707 | 864 |
865 unwind_protect::run_frame ("do_keyboard"); | |
866 | |
867 return retval; | |
868 } | |
869 | |
1957 | 870 DEFUN (keyboard, args, , |
3372 | 871 "-*- texinfo -*-\n\ |
872 @deftypefn {Built-in Function} {} keyboard (@var{prompt})\n\ | |
873 This function is normally used for simple debugging. When the\n\ | |
874 @code{keyboard} function is executed, Octave prints a prompt and waits\n\ | |
875 for user input. The input strings are then evaluated and the results\n\ | |
876 are printed. This makes it possible to examine the values of variables\n\ | |
877 within a function, and to assign new values to variables. No value is\n\ | |
878 returned from the @code{keyboard} function, and it continues to prompt\n\ | |
879 for input until the user types @samp{quit}, or @samp{exit}.\n\ | |
529 | 880 \n\ |
3372 | 881 If @code{keyboard} is invoked without any arguments, a default prompt of\n\ |
882 @samp{debug> } is used.\n\ | |
883 @end deftypefn") | |
529 | 884 { |
2086 | 885 octave_value_list retval; |
529 | 886 |
887 int nargin = args.length (); | |
888 | |
712 | 889 if (nargin == 0 || nargin == 1) |
7752
40c428ea3408
initial implementation of dbup and dbdown
John W. Eaton <jwe@octave.org>
parents:
7734
diff
changeset
|
890 { |
40c428ea3408
initial implementation of dbup and dbdown
John W. Eaton <jwe@octave.org>
parents:
7734
diff
changeset
|
891 saved_frame = octave_call_stack::current_frame (); |
40c428ea3408
initial implementation of dbup and dbdown
John W. Eaton <jwe@octave.org>
parents:
7734
diff
changeset
|
892 unwind_protect::add (restore_frame); |
40c428ea3408
initial implementation of dbup and dbdown
John W. Eaton <jwe@octave.org>
parents:
7734
diff
changeset
|
893 unwind_protect_size_t (saved_frame); |
40c428ea3408
initial implementation of dbup and dbdown
John W. Eaton <jwe@octave.org>
parents:
7734
diff
changeset
|
894 |
40c428ea3408
initial implementation of dbup and dbdown
John W. Eaton <jwe@octave.org>
parents:
7734
diff
changeset
|
895 // Skip the frame assigned to the keyboard function. |
40c428ea3408
initial implementation of dbup and dbdown
John W. Eaton <jwe@octave.org>
parents:
7734
diff
changeset
|
896 octave_call_stack::goto_frame (1); |
40c428ea3408
initial implementation of dbup and dbdown
John W. Eaton <jwe@octave.org>
parents:
7734
diff
changeset
|
897 |
40c428ea3408
initial implementation of dbup and dbdown
John W. Eaton <jwe@octave.org>
parents:
7734
diff
changeset
|
898 do_keyboard (args); |
40c428ea3408
initial implementation of dbup and dbdown
John W. Eaton <jwe@octave.org>
parents:
7734
diff
changeset
|
899 |
40c428ea3408
initial implementation of dbup and dbdown
John W. Eaton <jwe@octave.org>
parents:
7734
diff
changeset
|
900 unwind_protect::run (); |
40c428ea3408
initial implementation of dbup and dbdown
John W. Eaton <jwe@octave.org>
parents:
7734
diff
changeset
|
901 } |
529 | 902 else |
5823 | 903 print_usage (); |
529 | 904 |
905 return retval; | |
906 } | |
907 | |
4208 | 908 DEFCMD (echo, args, , |
3332 | 909 "-*- texinfo -*-\n\ |
910 @deffn {Command} echo options\n\ | |
911 Control whether commands are displayed as they are executed. Valid\n\ | |
912 options are:\n\ | |
1588 | 913 \n\ |
3332 | 914 @table @code\n\ |
915 @item on\n\ | |
916 Enable echoing of commands as they are executed in script files.\n\ | |
917 \n\ | |
918 @item off\n\ | |
919 Disable echoing of commands as they are executed in script files.\n\ | |
1588 | 920 \n\ |
3332 | 921 @item on all\n\ |
922 Enable echoing of commands as they are executed in script files and\n\ | |
923 functions.\n\ | |
1588 | 924 \n\ |
3332 | 925 @item off all\n\ |
926 Disable echoing of commands as they are executed in script files and\n\ | |
927 functions.\n\ | |
928 @end table\n\ | |
929 \n\ | |
930 @noindent\n\ | |
931 If invoked without any arguments, @code{echo} toggles the current echo\n\ | |
932 state.\n\ | |
3333 | 933 @end deffn") |
1588 | 934 { |
2086 | 935 octave_value_list retval; |
1588 | 936 |
1755 | 937 int argc = args.length () + 1; |
938 | |
1968 | 939 string_vector argv = args.make_argv ("echo"); |
1755 | 940 |
941 if (error_state) | |
942 return retval; | |
1588 | 943 |
944 switch (argc) | |
945 { | |
946 case 1: | |
947 { | |
2205 | 948 if ((Vecho_executing_commands & ECHO_SCRIPTS) |
949 || (Vecho_executing_commands & ECHO_FUNCTIONS)) | |
5794 | 950 Vecho_executing_commands = ECHO_OFF; |
1588 | 951 else |
5794 | 952 Vecho_executing_commands = ECHO_SCRIPTS; |
1588 | 953 } |
954 break; | |
955 | |
956 case 2: | |
957 { | |
3523 | 958 std::string arg = argv[1]; |
1755 | 959 |
960 if (arg == "on") | |
5794 | 961 Vecho_executing_commands = ECHO_SCRIPTS; |
1755 | 962 else if (arg == "off") |
5794 | 963 Vecho_executing_commands = ECHO_OFF; |
1588 | 964 else |
5823 | 965 print_usage (); |
1588 | 966 } |
967 break; | |
968 | |
969 case 3: | |
970 { | |
3523 | 971 std::string arg = argv[1]; |
1755 | 972 |
973 if (arg == "on" && argv[2] == "all") | |
2800 | 974 { |
975 int tmp = (ECHO_SCRIPTS | ECHO_FUNCTIONS); | |
5794 | 976 Vecho_executing_commands = tmp; |
2800 | 977 } |
1755 | 978 else if (arg == "off" && argv[2] == "all") |
5794 | 979 Vecho_executing_commands = ECHO_OFF; |
1588 | 980 else |
5823 | 981 print_usage (); |
1588 | 982 } |
983 break; | |
984 | |
985 default: | |
5823 | 986 print_usage (); |
1588 | 987 break; |
988 } | |
989 | |
990 return retval; | |
991 } | |
992 | |
2234 | 993 DEFUN (completion_matches, args, nargout, |
3332 | 994 "-*- texinfo -*-\n\ |
995 @deftypefn {Built-in Function} {} completion_matches (@var{hint})\n\ | |
996 Generate possible completions given @var{hint}.\n\ | |
2299 | 997 \n\ |
998 This function is provided for the benefit of programs like Emacs which\n\ | |
3332 | 999 might be controlling Octave and handling user input. The current\n\ |
1000 command number is not incremented when this function is called. This is\n\ | |
1001 a feature, not a bug.\n\ | |
3333 | 1002 @end deftypefn") |
2234 | 1003 { |
2281 | 1004 octave_value retval; |
2234 | 1005 |
1006 int nargin = args.length (); | |
1007 | |
1008 if (nargin == 1) | |
1009 { | |
3523 | 1010 std::string hint = args(0).string_value (); |
2234 | 1011 |
1012 if (! error_state) | |
1013 { | |
1014 int n = 32; | |
1015 | |
1016 string_vector list (n); | |
1017 | |
1018 int k = 0; | |
1019 | |
1020 for (;;) | |
1021 { | |
3523 | 1022 std::string cmd = generate_completion (hint, k); |
2234 | 1023 |
2944 | 1024 if (! cmd.empty ()) |
2234 | 1025 { |
2944 | 1026 if (k == n) |
2234 | 1027 { |
2944 | 1028 n *= 2; |
1029 list.resize (n); | |
1030 } | |
2235 | 1031 |
2944 | 1032 list[k++] = cmd; |
2234 | 1033 } |
1034 else | |
1035 { | |
1036 list.resize (k); | |
1037 break; | |
1038 } | |
1039 } | |
1040 | |
1041 if (nargout > 0) | |
2281 | 1042 { |
1043 if (! list.empty ()) | |
1044 retval = list; | |
2282 | 1045 else |
1046 retval = ""; | |
2281 | 1047 } |
2234 | 1048 else |
1049 { | |
2235 | 1050 // We don't use string_vector::list_in_columns here |
1051 // because it will be easier for Emacs if the names | |
1052 // appear in a single column. | |
1053 | |
2234 | 1054 int len = list.length (); |
1055 | |
1056 for (int i = 0; i < len; i++) | |
1057 octave_stdout << list[i] << "\n"; | |
1058 } | |
2299 | 1059 |
1060 octave_completion_matches_called = true; | |
2234 | 1061 } |
1062 } | |
1063 else | |
5823 | 1064 print_usage (); |
2234 | 1065 |
1066 return retval; | |
1067 } | |
1068 | |
3189 | 1069 DEFUN (read_readline_init_file, args, , |
3448 | 1070 "-*- texinfo -*-\n\ |
1071 @deftypefn {Built-in Function} {} read_readline_init_file (@var{file})\n\ | |
7007 | 1072 Read the readline library initialization file @var{file}. If\n\ |
3448 | 1073 @var{file} is omitted, read the default initialization file (normally\n\ |
6615 | 1074 @file{~/.inputrc}).\n\ |
1075 \n\ | |
1076 @xref{Readline Init File, , , readline, GNU Readline Library},\n\ | |
1077 for details.\n\ | |
3448 | 1078 @end deftypefn") |
3189 | 1079 { |
1080 octave_value_list retval; | |
1081 | |
1082 int nargin = args.length (); | |
1083 | |
1084 if (nargin == 0) | |
1085 command_editor::read_init_file (); | |
1086 else if (nargin == 1) | |
1087 { | |
5872 | 1088 std::string file = args(0).string_value (); |
3189 | 1089 |
1090 if (! error_state) | |
1091 command_editor::read_init_file (file); | |
1092 } | |
1093 else | |
5823 | 1094 print_usage (); |
3189 | 1095 |
1096 return retval; | |
1097 } | |
1098 | |
7758
8e14a01ffe9f
input.cc (Fre_read_readline_init_file): new function
John W. Eaton <jwe@octave.org>
parents:
7752
diff
changeset
|
1099 DEFUN (re_read_readline_init_file, args, , |
8e14a01ffe9f
input.cc (Fre_read_readline_init_file): new function
John W. Eaton <jwe@octave.org>
parents:
7752
diff
changeset
|
1100 "-*- texinfo -*-\n\ |
8e14a01ffe9f
input.cc (Fre_read_readline_init_file): new function
John W. Eaton <jwe@octave.org>
parents:
7752
diff
changeset
|
1101 @deftypefn {Built-in Function} {} re_read_readline_init_file ()\n\ |
8e14a01ffe9f
input.cc (Fre_read_readline_init_file): new function
John W. Eaton <jwe@octave.org>
parents:
7752
diff
changeset
|
1102 Re-read the last readline library initialization file that was read.\n\ |
8e14a01ffe9f
input.cc (Fre_read_readline_init_file): new function
John W. Eaton <jwe@octave.org>
parents:
7752
diff
changeset
|
1103 @xref{Readline Init File, , , readline, GNU Readline Library},\n\ |
8e14a01ffe9f
input.cc (Fre_read_readline_init_file): new function
John W. Eaton <jwe@octave.org>
parents:
7752
diff
changeset
|
1104 for details.\n\ |
8e14a01ffe9f
input.cc (Fre_read_readline_init_file): new function
John W. Eaton <jwe@octave.org>
parents:
7752
diff
changeset
|
1105 @end deftypefn") |
8e14a01ffe9f
input.cc (Fre_read_readline_init_file): new function
John W. Eaton <jwe@octave.org>
parents:
7752
diff
changeset
|
1106 { |
8e14a01ffe9f
input.cc (Fre_read_readline_init_file): new function
John W. Eaton <jwe@octave.org>
parents:
7752
diff
changeset
|
1107 octave_value_list retval; |
8e14a01ffe9f
input.cc (Fre_read_readline_init_file): new function
John W. Eaton <jwe@octave.org>
parents:
7752
diff
changeset
|
1108 |
8e14a01ffe9f
input.cc (Fre_read_readline_init_file): new function
John W. Eaton <jwe@octave.org>
parents:
7752
diff
changeset
|
1109 if (args.length () == 0) |
8e14a01ffe9f
input.cc (Fre_read_readline_init_file): new function
John W. Eaton <jwe@octave.org>
parents:
7752
diff
changeset
|
1110 command_editor::re_read_init_file (); |
8e14a01ffe9f
input.cc (Fre_read_readline_init_file): new function
John W. Eaton <jwe@octave.org>
parents:
7752
diff
changeset
|
1111 else |
8e14a01ffe9f
input.cc (Fre_read_readline_init_file): new function
John W. Eaton <jwe@octave.org>
parents:
7752
diff
changeset
|
1112 print_usage (); |
8e14a01ffe9f
input.cc (Fre_read_readline_init_file): new function
John W. Eaton <jwe@octave.org>
parents:
7752
diff
changeset
|
1113 |
8e14a01ffe9f
input.cc (Fre_read_readline_init_file): new function
John W. Eaton <jwe@octave.org>
parents:
7752
diff
changeset
|
1114 return retval; |
8e14a01ffe9f
input.cc (Fre_read_readline_init_file): new function
John W. Eaton <jwe@octave.org>
parents:
7752
diff
changeset
|
1115 } |
8e14a01ffe9f
input.cc (Fre_read_readline_init_file): new function
John W. Eaton <jwe@octave.org>
parents:
7752
diff
changeset
|
1116 |
3523 | 1117 static std::string hook_fcn; |
3498 | 1118 static octave_value user_data; |
1119 | |
4802 | 1120 static int |
3519 | 1121 input_event_hook (void) |
3498 | 1122 { |
4526 | 1123 if (is_valid_function (hook_fcn)) |
1124 { | |
1125 if (user_data.is_defined ()) | |
1126 feval (hook_fcn, user_data, 0); | |
1127 else | |
1128 feval (hook_fcn, octave_value_list (), 0); | |
1129 } | |
3498 | 1130 else |
4526 | 1131 { |
1132 hook_fcn = std::string (); | |
1133 user_data = octave_value (); | |
1134 | |
6913 | 1135 command_editor::remove_event_hook (input_event_hook); |
4802 | 1136 } |
1137 | |
1138 return 0; | |
3498 | 1139 } |
1140 | |
1141 DEFUN (input_event_hook, args, , | |
1142 "-*- texinfo -*-\n\ | |
1143 @deftypefn {Built-in Function} {[@var{ofcn}, @var{odata}] =} input_event_hook (@var{fcn}, @var{data})\n\ | |
1144 Given the name of a function as a string and any Octave value object,\n\ | |
1145 install @var{fcn} as a function to call periodically, when Octave is\n\ | |
1146 waiting for input. The function should have the form\n\ | |
1147 @example\n\ | |
1148 @var{fcn} (@var{data})\n\ | |
1149 @end example\n\ | |
1150 \n\ | |
1151 If @var{data} is omitted, Octave calls the function without any\n\ | |
1152 arguments. If both @var{fcn} and @var{data} are omitted, Octave\n\ | |
1153 clears the hook. In all cases, the name of the previous hook function\n\ | |
1154 and the user data are returned.\n\ | |
1155 @end deftypefn") | |
1156 { | |
1157 octave_value_list retval; | |
1158 | |
1159 int nargin = args.length (); | |
1160 | |
1161 if (nargin > 2) | |
5823 | 1162 print_usage (); |
3498 | 1163 else |
1164 { | |
1165 octave_value tmp_user_data; | |
1166 | |
3523 | 1167 std::string tmp_hook_fcn; |
3498 | 1168 |
1169 if (nargin > 1) | |
1170 tmp_user_data = args(1); | |
1171 | |
1172 if (nargin > 0) | |
1173 { | |
1174 tmp_hook_fcn = args(0).string_value (); | |
1175 | |
1176 if (error_state) | |
1177 { | |
1178 error ("input_event_hook: expecting string as first arg"); | |
1179 return retval; | |
1180 } | |
1181 | |
6913 | 1182 command_editor::add_event_hook (input_event_hook); |
3498 | 1183 } |
1184 | |
1185 if (nargin == 0) | |
6913 | 1186 command_editor::remove_event_hook (input_event_hook); |
3498 | 1187 |
1188 retval(1) = user_data; | |
1189 retval(0) = hook_fcn; | |
1190 | |
1191 hook_fcn = tmp_hook_fcn; | |
1192 user_data = tmp_user_data; | |
1193 } | |
1194 | |
1195 return retval; | |
1196 } | |
1197 | |
5794 | 1198 DEFUN (PS1, args, nargout, |
1199 "-*- texinfo -*-\n\ | |
1200 @deftypefn {Built-in Function} {@var{val} =} PS1 ()\n\ | |
1201 @deftypefnx {Built-in Function} {@var{old_val} =} PS1 (@var{new_val})\n\ | |
1202 Query or set the primary prompt string. When executing interactively,\n\ | |
1203 Octave displays the primary prompt when it is ready to read a command.\n\ | |
3332 | 1204 \n\ |
5794 | 1205 The default value of the primary prompt string is @code{\"\\s:\\#> \"}.\n\ |
1206 To change it, use a command like\n\ | |
3332 | 1207 \n\ |
1208 @example\n\ | |
7097 | 1209 octave:13> PS1 (\"\\\\u@@\\\\H> \")\n\ |
3332 | 1210 @end example\n\ |
1211 \n\ | |
1212 @noindent\n\ | |
1213 which will result in the prompt @samp{boris@@kremvax> } for the user\n\ | |
1214 @samp{boris} logged in on the host @samp{kremvax.kgb.su}. Note that two\n\ | |
5794 | 1215 backslashes are required to enter a backslash into a double-quoted\n\ |
1216 character string.\n\ | |
3332 | 1217 @xref{Strings}.\n\ |
6373 | 1218 @seealso{PS2, PS4}\n\ |
5794 | 1219 @end deftypefn") |
1220 { | |
1221 return SET_INTERNAL_VARIABLE (PS1); | |
1222 } | |
2181 | 1223 |
5794 | 1224 DEFUN (PS2, args, nargout, |
1225 "-*- texinfo -*-\n\ | |
1226 @deftypefn {Built-in Function} {@var{val} =} PS2 ()\n\ | |
1227 @deftypefnx {Built-in Function} {@var{old_val} =} PS2 (@var{new_val})\n\ | |
1228 Query or set the secondary prompt string. The secondary prompt is\n\ | |
1229 printed when Octave is expecting additional input to complete a\n\ | |
1230 command. For example, if you are typing a for loop that spans several\n\ | |
1231 lines, Octave will print the secondary prompt at the beginning of\n\ | |
1232 each line after the first. The default value of the secondary prompt\n\ | |
1233 string is @code{\"> \"}.\n\ | |
1234 @seealso{PS1, PS4}\n\ | |
1235 @end deftypefn") | |
1236 { | |
1237 return SET_INTERNAL_VARIABLE (PS2); | |
1238 } | |
2181 | 1239 |
5794 | 1240 DEFUN (PS4, args, nargout, |
1241 "-*- texinfo -*-\n\ | |
1242 @deftypefn {Built-in Function} {@var{val} =} PS4 ()\n\ | |
1243 @deftypefnx {Built-in Function} {@var{old_val} =} PS4 (@var{new_val})\n\ | |
1244 Query or set the character string used to prefix output produced\n\ | |
1245 when echoing commands when @code{echo_executing_commands} is enabled.\n\ | |
6620 | 1246 The default value is @code{\"+ \"}.\n\ |
1247 @xref{Invoking Octave from the Command Line}, for a description of\n\ | |
1248 @code{--echo-commands}.\n\ | |
5794 | 1249 @seealso{echo_executing_commands, PS1, PS2}\n\ |
1250 @end deftypefn") | |
1251 { | |
1252 return SET_INTERNAL_VARIABLE (PS4); | |
1253 } | |
2181 | 1254 |
5794 | 1255 DEFUN (completion_append_char, args, nargout, |
1256 "-*- texinfo -*-\n\ | |
1257 @deftypefn {Built-in Function} {@var{val} =} completion_append_char ()\n\ | |
1258 @deftypefnx {Built-in Function} {@var{old_val} =} completion_append_char (@var{new_val})\n\ | |
1259 Query or set the internal character variable that is appended to\n\ | |
1260 successful command-line completion attempts. The default\n\ | |
3332 | 1261 value is @code{\" \"} (a single space).\n\ |
5794 | 1262 @end deftypefn") |
1263 { | |
1264 return SET_INTERNAL_VARIABLE (completion_append_char); | |
1265 } | |
3019 | 1266 |
5794 | 1267 DEFUN (echo_executing_commands, args, nargout, |
1268 "-*- texinfo -*-\n\ | |
1269 @deftypefn {Built-in Function} {@var{val} =} echo_executing_commands ()\n\ | |
1270 @deftypefnx {Built-in Function} {@var{old_val} =} echo_executing_commands (@var{new_val})\n\ | |
1271 Query or set the internal variable that controls the echo state.\n\ | |
1272 It may be the sum of the following values:\n\ | |
3332 | 1273 \n\ |
1274 @table @asis\n\ | |
1275 @item 1\n\ | |
1276 Echo commands read from script files.\n\ | |
1277 \n\ | |
1278 @item 2\n\ | |
1279 Echo commands from functions.\n\ | |
1280 \n\ | |
1281 @item 4\n\ | |
1282 Echo commands read from command line.\n\ | |
1283 @end table\n\ | |
1284 \n\ | |
1285 More than one state can be active at once. For example, a value of 3 is\n\ | |
1286 equivalent to the command @kbd{echo on all}.\n\ | |
1287 \n\ | |
1288 The value of @code{echo_executing_commands} is set by the @kbd{echo}\n\ | |
1289 command and the command line option @code{--echo-input}.\n\ | |
5794 | 1290 @end deftypefn") |
1291 { | |
1292 return SET_INTERNAL_VARIABLE (echo_executing_commands); | |
2181 | 1293 } |
1294 | |
6257 | 1295 DEFUN (__request_drawnow__, args, , |
1296 "-*- texinfo -*-\n\ | |
1297 @deftypefn {Built-in Function} {} __request_drawnow__ ()\n\ | |
1298 @deftypefnx {Built-in Function} {} __request_drawnow__ (@var{flag})\n\ | |
6945 | 1299 Undocumented internal function.\n\ |
6257 | 1300 @end deftypefn") |
1301 { | |
1302 octave_value retval; | |
1303 | |
1304 int nargin = args.length (); | |
1305 | |
1306 if (nargin == 0) | |
1307 Vdrawnow_requested = true; | |
1308 else if (nargin == 1) | |
1309 Vdrawnow_requested = args(0).bool_value (); | |
1310 else | |
1311 print_usage (); | |
1312 | |
1313 return retval; | |
1314 } | |
1315 | |
7294 | 1316 DEFUN (__gud_mode__, args, , |
1317 "-*- texinfo -*-\n\ | |
1318 @deftypefn {Built-in Function} {} __gud_mode__ ()\n\ | |
1319 Undocumented internal function.\n\ | |
1320 @end deftypefn") | |
1321 { | |
1322 octave_value retval; | |
1323 | |
1324 int nargin = args.length (); | |
1325 | |
1326 if (nargin == 0) | |
1327 retval = Vgud_mode; | |
1328 else if (nargin == 1) | |
1329 Vgud_mode = args(0).bool_value (); | |
1330 else | |
1331 print_usage (); | |
1332 | |
1333 return retval; | |
1334 } | |
1335 | |
1 | 1336 /* |
1337 ;;; Local Variables: *** | |
1338 ;;; mode: C++ *** | |
1339 ;;; End: *** | |
1340 */ |