| 1 | /* Top level stuff for GDB, the GNU debugger. |
| 2 | Copyright 1986, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 1999 |
| 3 | Free Software Foundation, Inc. |
| 4 | |
| 5 | This file is part of GDB. |
| 6 | |
| 7 | This program is free software; you can redistribute it and/or modify |
| 8 | it under the terms of the GNU General Public License as published by |
| 9 | the Free Software Foundation; either version 2 of the License, or |
| 10 | (at your option) any later version. |
| 11 | |
| 12 | This program is distributed in the hope that it will be useful, |
| 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | GNU General Public License for more details. |
| 16 | |
| 17 | You should have received a copy of the GNU General Public License |
| 18 | along with this program; if not, write to the Free Software |
| 19 | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ |
| 20 | |
| 21 | #include "defs.h" |
| 22 | #include "gdbcmd.h" |
| 23 | #include "call-cmds.h" |
| 24 | #include "symtab.h" |
| 25 | #include "inferior.h" |
| 26 | #include "signals.h" |
| 27 | #include "target.h" |
| 28 | #include "breakpoint.h" |
| 29 | #include "gdbtypes.h" |
| 30 | #include "expression.h" |
| 31 | #include "value.h" |
| 32 | #include "language.h" |
| 33 | #include "terminal.h" /* For job_control. */ |
| 34 | #include "annotate.h" |
| 35 | #include "top.h" |
| 36 | |
| 37 | /* readline include files */ |
| 38 | #include <readline/readline.h> |
| 39 | #include <readline/history.h> |
| 40 | |
| 41 | /* readline defines this. */ |
| 42 | #undef savestring |
| 43 | |
| 44 | #include <sys/types.h> |
| 45 | #ifdef HAVE_UNISTD_H |
| 46 | #include <unistd.h> |
| 47 | #endif |
| 48 | |
| 49 | #include "event-loop.h" |
| 50 | #include "gdb_string.h" |
| 51 | #include "gdb_stat.h" |
| 52 | #include <ctype.h> |
| 53 | |
| 54 | /* Prototypes for local functions */ |
| 55 | |
| 56 | static void dont_repeat_command PARAMS ((char *, int)); |
| 57 | |
| 58 | static void source_cleanup_lines PARAMS ((PTR)); |
| 59 | |
| 60 | static void user_defined_command PARAMS ((char *, int)); |
| 61 | |
| 62 | static void init_signals PARAMS ((void)); |
| 63 | |
| 64 | #ifdef STOP_SIGNAL |
| 65 | static void stop_sig PARAMS ((int)); |
| 66 | #endif |
| 67 | |
| 68 | static char * line_completion_function PARAMS ((char *, int, char *, int)); |
| 69 | |
| 70 | static char * readline_line_completion_function PARAMS ((char *, int)); |
| 71 | |
| 72 | /* NOTE 1999-04-29: this function will be static again, after we make the |
| 73 | event loop be the default command loop for gdb, and we merge |
| 74 | event-top.c into this file, top.c */ |
| 75 | /* static */ void command_loop_marker PARAMS ((int)); |
| 76 | |
| 77 | static void while_command PARAMS ((char *, int)); |
| 78 | |
| 79 | static void if_command PARAMS ((char *, int)); |
| 80 | |
| 81 | static struct command_line * |
| 82 | build_command_line PARAMS ((enum command_control_type, char *)); |
| 83 | |
| 84 | static struct command_line * |
| 85 | get_command_line PARAMS ((enum command_control_type, char *)); |
| 86 | |
| 87 | static void realloc_body_list PARAMS ((struct command_line *, int)); |
| 88 | |
| 89 | static enum misc_command_type read_next_line PARAMS ((struct command_line **)); |
| 90 | |
| 91 | static enum command_control_type |
| 92 | recurse_read_control_structure PARAMS ((struct command_line *)); |
| 93 | |
| 94 | static struct cleanup * setup_user_args PARAMS ((char *)); |
| 95 | |
| 96 | static char * locate_arg PARAMS ((char *)); |
| 97 | |
| 98 | static char * insert_args PARAMS ((char *)); |
| 99 | |
| 100 | static void arg_cleanup PARAMS ((void)); |
| 101 | |
| 102 | static void init_main PARAMS ((void)); |
| 103 | |
| 104 | static void init_cmd_lists PARAMS ((void)); |
| 105 | |
| 106 | static void float_handler PARAMS ((int)); |
| 107 | |
| 108 | static void init_signals PARAMS ((void)); |
| 109 | |
| 110 | static void set_verbose PARAMS ((char *, int, struct cmd_list_element *)); |
| 111 | |
| 112 | static void show_history PARAMS ((char *, int)); |
| 113 | |
| 114 | static void set_history PARAMS ((char *, int)); |
| 115 | |
| 116 | static void set_history_size_command PARAMS ((char *, int, |
| 117 | struct cmd_list_element *)); |
| 118 | |
| 119 | static void show_commands PARAMS ((char *, int)); |
| 120 | |
| 121 | static void echo_command PARAMS ((char *, int)); |
| 122 | |
| 123 | static void pwd_command PARAMS ((char *, int)); |
| 124 | |
| 125 | static void show_version PARAMS ((char *, int)); |
| 126 | |
| 127 | static void document_command PARAMS ((char *, int)); |
| 128 | |
| 129 | static void define_command PARAMS ((char *, int)); |
| 130 | |
| 131 | static void validate_comname PARAMS ((char *)); |
| 132 | |
| 133 | static void help_command PARAMS ((char *, int)); |
| 134 | |
| 135 | static void show_command PARAMS ((char *, int)); |
| 136 | |
| 137 | static void info_command PARAMS ((char *, int)); |
| 138 | |
| 139 | static void complete_command PARAMS ((char *, int)); |
| 140 | |
| 141 | static void do_nothing PARAMS ((int)); |
| 142 | |
| 143 | #ifdef SIGHUP |
| 144 | /* NOTE 1999-04-29: This function will be static again, once we modify |
| 145 | gdb to use the event loop as the default command loop and we merge |
| 146 | event-top.c into this file, top.c */ |
| 147 | /* static */ int quit_cover PARAMS ((PTR)); |
| 148 | |
| 149 | static void disconnect PARAMS ((int)); |
| 150 | #endif |
| 151 | |
| 152 | static void source_cleanup PARAMS ((FILE *)); |
| 153 | |
| 154 | /* If this definition isn't overridden by the header files, assume |
| 155 | that isatty and fileno exist on this system. */ |
| 156 | #ifndef ISATTY |
| 157 | #define ISATTY(FP) (isatty (fileno (FP))) |
| 158 | #endif |
| 159 | |
| 160 | /* Initialization file name for gdb. This is overridden in some configs. */ |
| 161 | |
| 162 | #ifndef GDBINIT_FILENAME |
| 163 | #define GDBINIT_FILENAME ".gdbinit" |
| 164 | #endif |
| 165 | char gdbinit[] = GDBINIT_FILENAME; |
| 166 | |
| 167 | int inhibit_gdbinit = 0; |
| 168 | |
| 169 | /* If nonzero, and GDB has been configured to be able to use windows, |
| 170 | attempt to open them upon startup. */ |
| 171 | |
| 172 | int use_windows = 1; |
| 173 | |
| 174 | /* Version number of GDB, as a string. */ |
| 175 | |
| 176 | extern char *version; |
| 177 | |
| 178 | /* Canonical host name as a string. */ |
| 179 | |
| 180 | extern char *host_name; |
| 181 | |
| 182 | /* Canonical target name as a string. */ |
| 183 | |
| 184 | extern char *target_name; |
| 185 | |
| 186 | extern char lang_frame_mismatch_warn[]; /* language.c */ |
| 187 | |
| 188 | /* Flag for whether we want all the "from_tty" gubbish printed. */ |
| 189 | |
| 190 | int caution = 1; /* Default is yes, sigh. */ |
| 191 | |
| 192 | /* Define all cmd_list_elements. */ |
| 193 | |
| 194 | /* Chain containing all defined commands. */ |
| 195 | |
| 196 | struct cmd_list_element *cmdlist; |
| 197 | |
| 198 | /* Chain containing all defined info subcommands. */ |
| 199 | |
| 200 | struct cmd_list_element *infolist; |
| 201 | |
| 202 | /* Chain containing all defined enable subcommands. */ |
| 203 | |
| 204 | struct cmd_list_element *enablelist; |
| 205 | |
| 206 | /* Chain containing all defined disable subcommands. */ |
| 207 | |
| 208 | struct cmd_list_element *disablelist; |
| 209 | |
| 210 | /* Chain containing all defined toggle subcommands. */ |
| 211 | |
| 212 | struct cmd_list_element *togglelist; |
| 213 | |
| 214 | /* Chain containing all defined stop subcommands. */ |
| 215 | |
| 216 | struct cmd_list_element *stoplist; |
| 217 | |
| 218 | /* Chain containing all defined delete subcommands. */ |
| 219 | |
| 220 | struct cmd_list_element *deletelist; |
| 221 | |
| 222 | /* Chain containing all defined "enable breakpoint" subcommands. */ |
| 223 | |
| 224 | struct cmd_list_element *enablebreaklist; |
| 225 | |
| 226 | /* Chain containing all defined set subcommands */ |
| 227 | |
| 228 | struct cmd_list_element *setlist; |
| 229 | |
| 230 | /* Chain containing all defined unset subcommands */ |
| 231 | |
| 232 | struct cmd_list_element *unsetlist; |
| 233 | |
| 234 | /* Chain containing all defined show subcommands. */ |
| 235 | |
| 236 | struct cmd_list_element *showlist; |
| 237 | |
| 238 | /* Chain containing all defined \"set history\". */ |
| 239 | |
| 240 | struct cmd_list_element *sethistlist; |
| 241 | |
| 242 | /* Chain containing all defined \"show history\". */ |
| 243 | |
| 244 | struct cmd_list_element *showhistlist; |
| 245 | |
| 246 | /* Chain containing all defined \"unset history\". */ |
| 247 | |
| 248 | struct cmd_list_element *unsethistlist; |
| 249 | |
| 250 | /* Chain containing all defined maintenance subcommands. */ |
| 251 | |
| 252 | struct cmd_list_element *maintenancelist; |
| 253 | |
| 254 | /* Chain containing all defined "maintenance info" subcommands. */ |
| 255 | |
| 256 | struct cmd_list_element *maintenanceinfolist; |
| 257 | |
| 258 | /* Chain containing all defined "maintenance print" subcommands. */ |
| 259 | |
| 260 | struct cmd_list_element *maintenanceprintlist; |
| 261 | |
| 262 | struct cmd_list_element *setprintlist; |
| 263 | |
| 264 | struct cmd_list_element *showprintlist; |
| 265 | |
| 266 | struct cmd_list_element *setchecklist; |
| 267 | |
| 268 | struct cmd_list_element *showchecklist; |
| 269 | |
| 270 | /* stdio stream that command input is being read from. Set to stdin normally. |
| 271 | Set by source_command to the file we are sourcing. Set to NULL if we are |
| 272 | executing a user-defined command or interacting via a GUI. */ |
| 273 | |
| 274 | FILE *instream; |
| 275 | |
| 276 | /* Current working directory. */ |
| 277 | |
| 278 | char *current_directory; |
| 279 | |
| 280 | /* The directory name is actually stored here (usually). */ |
| 281 | char gdb_dirbuf[1024]; |
| 282 | |
| 283 | /* Function to call before reading a command, if nonzero. |
| 284 | The function receives two args: an input stream, |
| 285 | and a prompt string. */ |
| 286 | |
| 287 | void (*window_hook) PARAMS ((FILE *, char *)); |
| 288 | |
| 289 | int epoch_interface; |
| 290 | int xgdb_verbose; |
| 291 | |
| 292 | /* gdb prints this when reading a command interactively */ |
| 293 | static char *gdb_prompt_string; /* the global prompt string */ |
| 294 | extern char *get_prompt PARAMS((void)); /* access function for prompt string */ |
| 295 | |
| 296 | /* Buffer used for reading command lines, and the size |
| 297 | allocated for it so far. */ |
| 298 | |
| 299 | char *line; |
| 300 | int linesize = 100; |
| 301 | |
| 302 | /* Nonzero if the current command is modified by "server ". This |
| 303 | affects things like recording into the command history, comamnds |
| 304 | repeating on RETURN, etc. This is so a user interface (emacs, GUI, |
| 305 | whatever) can issue its own commands and also send along commands |
| 306 | from the user, and have the user not notice that the user interface |
| 307 | is issuing commands too. */ |
| 308 | int server_command; |
| 309 | |
| 310 | /* Baud rate specified for talking to serial target systems. Default |
| 311 | is left as -1, so targets can choose their own defaults. */ |
| 312 | /* FIXME: This means that "show remotebaud" and gr_files_info can print -1 |
| 313 | or (unsigned int)-1. This is a Bad User Interface. */ |
| 314 | |
| 315 | int baud_rate = -1; |
| 316 | |
| 317 | /* Timeout limit for response from target. */ |
| 318 | |
| 319 | int remote_timeout = 20; /* Set default to 20 */ |
| 320 | |
| 321 | /* Non-zero tells remote* modules to output debugging info. */ |
| 322 | |
| 323 | int remote_debug = 0; |
| 324 | |
| 325 | /* Non-zero means the target is running. Note: this is different from |
| 326 | saying that there is an active target and we are stopped at a |
| 327 | breakpoint, for instance. This is a real indicator whether the |
| 328 | target is off and running, which gdb is doing something else. */ |
| 329 | int target_executing = 0; |
| 330 | |
| 331 | /* Level of control structure. */ |
| 332 | static int control_level; |
| 333 | |
| 334 | /* Structure for arguments to user defined functions. */ |
| 335 | #define MAXUSERARGS 10 |
| 336 | struct user_args |
| 337 | { |
| 338 | struct user_args *next; |
| 339 | struct |
| 340 | { |
| 341 | char *arg; |
| 342 | int len; |
| 343 | } a[MAXUSERARGS]; |
| 344 | int count; |
| 345 | } *user_args; |
| 346 | |
| 347 | /* Signal to catch ^Z typed while reading a command: SIGTSTP or SIGCONT. */ |
| 348 | |
| 349 | #ifndef STOP_SIGNAL |
| 350 | #ifdef SIGTSTP |
| 351 | #define STOP_SIGNAL SIGTSTP |
| 352 | static void stop_sig PARAMS ((int)); |
| 353 | #endif |
| 354 | #endif |
| 355 | |
| 356 | /* Some System V have job control but not sigsetmask(). */ |
| 357 | #if !defined (HAVE_SIGSETMASK) |
| 358 | #if !defined (USG) |
| 359 | #define HAVE_SIGSETMASK 1 |
| 360 | #else |
| 361 | #define HAVE_SIGSETMASK 0 |
| 362 | #endif |
| 363 | #endif |
| 364 | |
| 365 | #if 0 == (HAVE_SIGSETMASK) |
| 366 | #define sigsetmask(n) |
| 367 | #endif |
| 368 | |
| 369 | /* Hooks for alternate command interfaces. */ |
| 370 | |
| 371 | /* Called after most modules have been initialized, but before taking users |
| 372 | command file. */ |
| 373 | |
| 374 | void (*init_ui_hook) PARAMS ((char *argv0)); |
| 375 | |
| 376 | /* This hook is called from within gdb's many mini-event loops which could |
| 377 | steal control from a real user interface's event loop. It returns |
| 378 | non-zero if the user is requesting a detach, zero otherwise. */ |
| 379 | |
| 380 | int (*ui_loop_hook) PARAMS ((int)); |
| 381 | |
| 382 | /* Called instead of command_loop at top level. Can be invoked via |
| 383 | return_to_top_level. */ |
| 384 | |
| 385 | void (*command_loop_hook) PARAMS ((void)); |
| 386 | |
| 387 | |
| 388 | /* Called instead of fputs for all output. */ |
| 389 | |
| 390 | void (*fputs_unfiltered_hook) PARAMS ((const char *linebuffer, GDB_FILE *stream)); |
| 391 | |
| 392 | /* Called from print_frame_info to list the line we stopped in. */ |
| 393 | |
| 394 | void (*print_frame_info_listing_hook) PARAMS ((struct symtab *s, int line, |
| 395 | int stopline, int noerror)); |
| 396 | /* Replaces most of query. */ |
| 397 | |
| 398 | int (*query_hook) PARAMS ((const char *, va_list)); |
| 399 | |
| 400 | /* Replaces most of warning. */ |
| 401 | |
| 402 | void (*warning_hook) PARAMS ((const char *, va_list)); |
| 403 | |
| 404 | /* Called from gdb_flush to flush output. */ |
| 405 | |
| 406 | void (*flush_hook) PARAMS ((GDB_FILE *stream)); |
| 407 | |
| 408 | /* These three functions support getting lines of text from the user. They |
| 409 | are used in sequence. First readline_begin_hook is called with a text |
| 410 | string that might be (for example) a message for the user to type in a |
| 411 | sequence of commands to be executed at a breakpoint. If this function |
| 412 | calls back to a GUI, it might take this opportunity to pop up a text |
| 413 | interaction window with this message. Next, readline_hook is called |
| 414 | with a prompt that is emitted prior to collecting the user input. |
| 415 | It can be called multiple times. Finally, readline_end_hook is called |
| 416 | to notify the GUI that we are done with the interaction window and it |
| 417 | can close it. */ |
| 418 | |
| 419 | void (*readline_begin_hook) PARAMS ((char *, ...)); |
| 420 | char * (*readline_hook) PARAMS ((char *)); |
| 421 | void (*readline_end_hook) PARAMS ((void)); |
| 422 | |
| 423 | /* Called as appropriate to notify the interface of the specified breakpoint |
| 424 | conditions. */ |
| 425 | |
| 426 | void (*create_breakpoint_hook) PARAMS ((struct breakpoint *bpt)); |
| 427 | void (*delete_breakpoint_hook) PARAMS ((struct breakpoint *bpt)); |
| 428 | void (*modify_breakpoint_hook) PARAMS ((struct breakpoint *bpt)); |
| 429 | |
| 430 | /* Called during long calculations to allow GUI to repair window damage, and to |
| 431 | check for stop buttons, etc... */ |
| 432 | |
| 433 | void (*interactive_hook) PARAMS ((void)); |
| 434 | |
| 435 | /* Called when the registers have changed, as a hint to a GUI |
| 436 | to minimize window update. */ |
| 437 | |
| 438 | void (*registers_changed_hook) PARAMS ((void)); |
| 439 | |
| 440 | /* Tell the GUI someone changed the register REGNO. -1 means |
| 441 | that the caller does not know which register changed or |
| 442 | that several registers have changed (see value_assign).*/ |
| 443 | void (*register_changed_hook) PARAMS ((int regno)); |
| 444 | |
| 445 | /* Tell the GUI someone changed LEN bytes of memory at ADDR */ |
| 446 | void (*memory_changed_hook) PARAMS ((CORE_ADDR addr, int len)); |
| 447 | |
| 448 | /* Called when going to wait for the target. Usually allows the GUI to run |
| 449 | while waiting for target events. */ |
| 450 | |
| 451 | int (*target_wait_hook) PARAMS ((int pid, struct target_waitstatus *status)); |
| 452 | |
| 453 | /* Used by UI as a wrapper around command execution. May do various things |
| 454 | like enabling/disabling buttons, etc... */ |
| 455 | |
| 456 | void (*call_command_hook) PARAMS ((struct cmd_list_element *c, char *cmd, |
| 457 | int from_tty)); |
| 458 | |
| 459 | /* Called when the current thread changes. Argument is thread id. */ |
| 460 | |
| 461 | void (*context_hook) PARAMS ((int id)); |
| 462 | |
| 463 | /* Takes control from error (). Typically used to prevent longjmps out of the |
| 464 | middle of the GUI. Usually used in conjunction with a catch routine. */ |
| 465 | |
| 466 | NORETURN void (*error_hook) PARAMS ((void)) ATTR_NORETURN; |
| 467 | |
| 468 | \f |
| 469 | /* Where to go for return_to_top_level (RETURN_ERROR). */ |
| 470 | SIGJMP_BUF error_return; |
| 471 | /* Where to go for return_to_top_level (RETURN_QUIT). */ |
| 472 | SIGJMP_BUF quit_return; |
| 473 | |
| 474 | /* Return for reason REASON. This generally gets back to the command |
| 475 | loop, but can be caught via catch_errors. */ |
| 476 | |
| 477 | NORETURN void |
| 478 | return_to_top_level (reason) |
| 479 | enum return_reason reason; |
| 480 | { |
| 481 | quit_flag = 0; |
| 482 | immediate_quit = 0; |
| 483 | |
| 484 | /* Perhaps it would be cleaner to do this via the cleanup chain (not sure |
| 485 | I can think of a reason why that is vital, though). */ |
| 486 | bpstat_clear_actions(stop_bpstat); /* Clear queued breakpoint commands */ |
| 487 | |
| 488 | disable_current_display (); |
| 489 | do_cleanups (ALL_CLEANUPS); |
| 490 | if (async_p && target_has_async) |
| 491 | do_exec_cleanups (ALL_CLEANUPS); |
| 492 | |
| 493 | if (annotation_level > 1) |
| 494 | switch (reason) |
| 495 | { |
| 496 | case RETURN_QUIT: |
| 497 | annotate_quit (); |
| 498 | break; |
| 499 | case RETURN_ERROR: |
| 500 | annotate_error (); |
| 501 | break; |
| 502 | } |
| 503 | |
| 504 | (NORETURN void) SIGLONGJMP |
| 505 | (reason == RETURN_ERROR ? error_return : quit_return, 1); |
| 506 | } |
| 507 | |
| 508 | /* Call FUNC with arg ARGS, catching any errors. If there is no |
| 509 | error, return the value returned by FUNC. If there is an error, |
| 510 | print ERRSTRING, print the specific error message, then return |
| 511 | zero. |
| 512 | |
| 513 | Must not be called with immediate_quit in effect (bad things might |
| 514 | happen, say we got a signal in the middle of a memcpy to quit_return). |
| 515 | This is an OK restriction; with very few exceptions immediate_quit can |
| 516 | be replaced by judicious use of QUIT. |
| 517 | |
| 518 | MASK specifies what to catch; it is normally set to |
| 519 | RETURN_MASK_ALL, if for no other reason than that the code which |
| 520 | calls catch_errors might not be set up to deal with a quit which |
| 521 | isn't caught. But if the code can deal with it, it generally |
| 522 | should be RETURN_MASK_ERROR, unless for some reason it is more |
| 523 | useful to abort only the portion of the operation inside the |
| 524 | catch_errors. Note that quit should return to the command line |
| 525 | fairly quickly, even if some further processing is being done. */ |
| 526 | |
| 527 | int |
| 528 | catch_errors (func, args, errstring, mask) |
| 529 | catch_errors_ftype *func; |
| 530 | PTR args; |
| 531 | char *errstring; |
| 532 | return_mask mask; |
| 533 | { |
| 534 | SIGJMP_BUF saved_error; |
| 535 | SIGJMP_BUF saved_quit; |
| 536 | SIGJMP_BUF tmp_jmp; |
| 537 | int val; |
| 538 | struct cleanup *saved_cleanup_chain; |
| 539 | char *saved_error_pre_print; |
| 540 | char *saved_quit_pre_print; |
| 541 | |
| 542 | saved_cleanup_chain = save_cleanups (); |
| 543 | saved_error_pre_print = error_pre_print; |
| 544 | saved_quit_pre_print = quit_pre_print; |
| 545 | |
| 546 | if (mask & RETURN_MASK_ERROR) |
| 547 | { |
| 548 | memcpy ((char *)saved_error, (char *)error_return, sizeof (SIGJMP_BUF)); |
| 549 | error_pre_print = errstring; |
| 550 | } |
| 551 | if (mask & RETURN_MASK_QUIT) |
| 552 | { |
| 553 | memcpy (saved_quit, quit_return, sizeof (SIGJMP_BUF)); |
| 554 | quit_pre_print = errstring; |
| 555 | } |
| 556 | |
| 557 | if (SIGSETJMP (tmp_jmp) == 0) |
| 558 | { |
| 559 | if (mask & RETURN_MASK_ERROR) |
| 560 | memcpy (error_return, tmp_jmp, sizeof (SIGJMP_BUF)); |
| 561 | if (mask & RETURN_MASK_QUIT) |
| 562 | memcpy (quit_return, tmp_jmp, sizeof (SIGJMP_BUF)); |
| 563 | val = (*func) (args); |
| 564 | } |
| 565 | else |
| 566 | val = 0; |
| 567 | |
| 568 | restore_cleanups (saved_cleanup_chain); |
| 569 | |
| 570 | if (mask & RETURN_MASK_ERROR) |
| 571 | { |
| 572 | memcpy (error_return, saved_error, sizeof (SIGJMP_BUF)); |
| 573 | error_pre_print = saved_error_pre_print; |
| 574 | } |
| 575 | if (mask & RETURN_MASK_QUIT) |
| 576 | { |
| 577 | memcpy (quit_return, saved_quit, sizeof (SIGJMP_BUF)); |
| 578 | quit_pre_print = saved_quit_pre_print; |
| 579 | } |
| 580 | return val; |
| 581 | } |
| 582 | |
| 583 | /* Handler for SIGHUP. */ |
| 584 | |
| 585 | #ifdef SIGHUP |
| 586 | static void |
| 587 | disconnect (signo) |
| 588 | int signo; |
| 589 | { |
| 590 | catch_errors (quit_cover, NULL, |
| 591 | "Could not kill the program being debugged", RETURN_MASK_ALL); |
| 592 | signal (SIGHUP, SIG_DFL); |
| 593 | kill (getpid (), SIGHUP); |
| 594 | } |
| 595 | |
| 596 | /* Just a little helper function for disconnect(). */ |
| 597 | |
| 598 | /* NOTE 1999-04-29: This function will be static again, once we modify |
| 599 | gdb to use the event loop as the default command loop and we merge |
| 600 | event-top.c into this file, top.c */ |
| 601 | /* static */ int |
| 602 | quit_cover (s) |
| 603 | PTR s; |
| 604 | { |
| 605 | caution = 0; /* Throw caution to the wind -- we're exiting. |
| 606 | This prevents asking the user dumb questions. */ |
| 607 | quit_command((char *)0, 0); |
| 608 | return 0; |
| 609 | } |
| 610 | #endif /* defined SIGHUP */ |
| 611 | \f |
| 612 | /* Line number we are currently in in a file which is being sourced. */ |
| 613 | /* NOTE 1999-04-29: This variable will be static again, once we modify |
| 614 | gdb to use the event loop as the default command loop and we merge |
| 615 | event-top.c into this file, top.c */ |
| 616 | /* static */ int source_line_number; |
| 617 | |
| 618 | /* Name of the file we are sourcing. */ |
| 619 | /* NOTE 1999-04-29: This variable will be static again, once we modify |
| 620 | gdb to use the event loop as the default command loop and we merge |
| 621 | event-top.c into this file, top.c */ |
| 622 | /* static */ char *source_file_name; |
| 623 | |
| 624 | /* Buffer containing the error_pre_print used by the source stuff. |
| 625 | Malloc'd. */ |
| 626 | /* NOTE 1999-04-29: This variable will be static again, once we modify |
| 627 | gdb to use the event loop as the default command loop and we merge |
| 628 | event-top.c into this file, top.c */ |
| 629 | /* static */ char *source_error; |
| 630 | static int source_error_allocated; |
| 631 | |
| 632 | /* Something to glom on to the start of error_pre_print if source_file_name |
| 633 | is set. */ |
| 634 | /* NOTE 1999-04-29: This variable will be static again, once we modify |
| 635 | gdb to use the event loop as the default command loop and we merge |
| 636 | event-top.c into this file, top.c */ |
| 637 | /* static */ char *source_pre_error; |
| 638 | |
| 639 | /* Clean up on error during a "source" command (or execution of a |
| 640 | user-defined command). */ |
| 641 | |
| 642 | static void |
| 643 | source_cleanup (stream) |
| 644 | FILE *stream; |
| 645 | { |
| 646 | /* Restore the previous input stream. */ |
| 647 | instream = stream; |
| 648 | } |
| 649 | |
| 650 | /* Read commands from STREAM. */ |
| 651 | void |
| 652 | read_command_file (stream) |
| 653 | FILE *stream; |
| 654 | { |
| 655 | struct cleanup *cleanups; |
| 656 | |
| 657 | cleanups = make_cleanup ((make_cleanup_func) source_cleanup, instream); |
| 658 | instream = stream; |
| 659 | command_loop (); |
| 660 | do_cleanups (cleanups); |
| 661 | } |
| 662 | \f |
| 663 | extern void init_proc PARAMS ((void)); |
| 664 | |
| 665 | void (*pre_init_ui_hook) PARAMS ((void)); |
| 666 | |
| 667 | void |
| 668 | gdb_init (argv0) |
| 669 | char *argv0; |
| 670 | { |
| 671 | if (pre_init_ui_hook) |
| 672 | pre_init_ui_hook (); |
| 673 | |
| 674 | /* Run the init function of each source file */ |
| 675 | |
| 676 | getcwd (gdb_dirbuf, sizeof (gdb_dirbuf)); |
| 677 | current_directory = gdb_dirbuf; |
| 678 | |
| 679 | init_cmd_lists (); /* This needs to be done first */ |
| 680 | initialize_targets (); /* Setup target_terminal macros for utils.c */ |
| 681 | initialize_utils (); /* Make errors and warnings possible */ |
| 682 | initialize_all_files (); |
| 683 | init_main (); /* But that omits this file! Do it now */ |
| 684 | |
| 685 | /* The signal handling mechanism is different depending whether or |
| 686 | not the async version is run. NOTE: in the future we plan to make |
| 687 | the event loop be the default engine of gdb, and this difference |
| 688 | will disappear. */ |
| 689 | if (async_p) |
| 690 | async_init_signals (); |
| 691 | else |
| 692 | init_signals (); |
| 693 | |
| 694 | /* We need a default language for parsing expressions, so simple things like |
| 695 | "set width 0" won't fail if no language is explicitly set in a config file |
| 696 | or implicitly set by reading an executable during startup. */ |
| 697 | set_language (language_c); |
| 698 | expected_language = current_language; /* don't warn about the change. */ |
| 699 | |
| 700 | /* All the interpreters should have had a look at things by now. |
| 701 | Initialize the selected interpreter. */ |
| 702 | if (init_ui_hook) |
| 703 | init_ui_hook (argv0); |
| 704 | } |
| 705 | |
| 706 | /* Allocate, initialize a new command line structure for one of the |
| 707 | control commands (if/while). */ |
| 708 | |
| 709 | static struct command_line * |
| 710 | build_command_line (type, args) |
| 711 | enum command_control_type type; |
| 712 | char *args; |
| 713 | { |
| 714 | struct command_line *cmd; |
| 715 | |
| 716 | if (args == NULL) |
| 717 | error ("if/while commands require arguments.\n"); |
| 718 | |
| 719 | cmd = (struct command_line *)xmalloc (sizeof (struct command_line)); |
| 720 | cmd->next = NULL; |
| 721 | cmd->control_type = type; |
| 722 | |
| 723 | cmd->body_count = 1; |
| 724 | cmd->body_list |
| 725 | = (struct command_line **)xmalloc (sizeof (struct command_line *) |
| 726 | * cmd->body_count); |
| 727 | memset (cmd->body_list, 0, sizeof (struct command_line *) * cmd->body_count); |
| 728 | cmd->line = savestring (args, strlen (args)); |
| 729 | return cmd; |
| 730 | } |
| 731 | |
| 732 | /* Build and return a new command structure for the control commands |
| 733 | such as "if" and "while". */ |
| 734 | |
| 735 | static struct command_line * |
| 736 | get_command_line (type, arg) |
| 737 | enum command_control_type type; |
| 738 | char *arg; |
| 739 | { |
| 740 | struct command_line *cmd; |
| 741 | struct cleanup *old_chain = NULL; |
| 742 | |
| 743 | /* Allocate and build a new command line structure. */ |
| 744 | cmd = build_command_line (type, arg); |
| 745 | |
| 746 | old_chain = make_cleanup ((make_cleanup_func) free_command_lines, &cmd); |
| 747 | |
| 748 | /* Read in the body of this command. */ |
| 749 | if (recurse_read_control_structure (cmd) == invalid_control) |
| 750 | { |
| 751 | warning ("error reading in control structure\n"); |
| 752 | do_cleanups (old_chain); |
| 753 | return NULL; |
| 754 | } |
| 755 | |
| 756 | discard_cleanups (old_chain); |
| 757 | return cmd; |
| 758 | } |
| 759 | |
| 760 | /* Recursively print a command (including full control structures). */ |
| 761 | void |
| 762 | print_command_line (cmd, depth, stream) |
| 763 | struct command_line *cmd; |
| 764 | unsigned int depth; |
| 765 | GDB_FILE *stream; |
| 766 | { |
| 767 | unsigned int i; |
| 768 | |
| 769 | if (depth) |
| 770 | { |
| 771 | for (i = 0; i < depth; i++) |
| 772 | fputs_filtered (" ", stream); |
| 773 | } |
| 774 | |
| 775 | /* A simple command, print it and return. */ |
| 776 | if (cmd->control_type == simple_control) |
| 777 | { |
| 778 | fputs_filtered (cmd->line, stream); |
| 779 | fputs_filtered ("\n", stream); |
| 780 | return; |
| 781 | } |
| 782 | |
| 783 | /* loop_continue to jump to the start of a while loop, print it |
| 784 | and return. */ |
| 785 | if (cmd->control_type == continue_control) |
| 786 | { |
| 787 | fputs_filtered ("loop_continue\n", stream); |
| 788 | return; |
| 789 | } |
| 790 | |
| 791 | /* loop_break to break out of a while loop, print it and return. */ |
| 792 | if (cmd->control_type == break_control) |
| 793 | { |
| 794 | fputs_filtered ("loop_break\n", stream); |
| 795 | return; |
| 796 | } |
| 797 | |
| 798 | /* A while command. Recursively print its subcommands before returning. */ |
| 799 | if (cmd->control_type == while_control) |
| 800 | { |
| 801 | struct command_line *list; |
| 802 | fputs_filtered ("while ", stream); |
| 803 | fputs_filtered (cmd->line, stream); |
| 804 | fputs_filtered ("\n", stream); |
| 805 | list = *cmd->body_list; |
| 806 | while (list) |
| 807 | { |
| 808 | print_command_line (list, depth + 1, stream); |
| 809 | list = list->next; |
| 810 | } |
| 811 | } |
| 812 | |
| 813 | /* An if command. Recursively print both arms before returning. */ |
| 814 | if (cmd->control_type == if_control) |
| 815 | { |
| 816 | fputs_filtered ("if ", stream); |
| 817 | fputs_filtered (cmd->line, stream); |
| 818 | fputs_filtered ("\n", stream); |
| 819 | /* The true arm. */ |
| 820 | print_command_line (cmd->body_list[0], depth + 1, stream); |
| 821 | |
| 822 | /* Show the false arm if it exists. */ |
| 823 | if (cmd->body_count == 2) |
| 824 | { |
| 825 | if (depth) |
| 826 | { |
| 827 | for (i = 0; i < depth; i++) |
| 828 | fputs_filtered (" ", stream); |
| 829 | } |
| 830 | fputs_filtered ("else\n", stream); |
| 831 | print_command_line (cmd->body_list[1], depth + 1, stream); |
| 832 | } |
| 833 | if (depth) |
| 834 | { |
| 835 | for (i = 0; i < depth; i++) |
| 836 | fputs_filtered (" ", stream); |
| 837 | } |
| 838 | fputs_filtered ("end\n", stream); |
| 839 | } |
| 840 | } |
| 841 | |
| 842 | /* Execute the command in CMD. */ |
| 843 | |
| 844 | enum command_control_type |
| 845 | execute_control_command (cmd) |
| 846 | struct command_line *cmd; |
| 847 | { |
| 848 | struct expression *expr; |
| 849 | struct command_line *current; |
| 850 | struct cleanup *old_chain = 0; |
| 851 | value_ptr val; |
| 852 | value_ptr val_mark; |
| 853 | int loop; |
| 854 | enum command_control_type ret; |
| 855 | char *new_line; |
| 856 | |
| 857 | switch (cmd->control_type) |
| 858 | { |
| 859 | case simple_control: |
| 860 | /* A simple command, execute it and return. */ |
| 861 | new_line = insert_args (cmd->line); |
| 862 | if (!new_line) |
| 863 | return invalid_control; |
| 864 | old_chain = make_cleanup ((make_cleanup_func) free_current_contents, |
| 865 | &new_line); |
| 866 | execute_command (new_line, 0); |
| 867 | ret = cmd->control_type; |
| 868 | break; |
| 869 | |
| 870 | case continue_control: |
| 871 | case break_control: |
| 872 | /* Return for "continue", and "break" so we can either |
| 873 | continue the loop at the top, or break out. */ |
| 874 | ret = cmd->control_type; |
| 875 | break; |
| 876 | |
| 877 | case while_control: |
| 878 | { |
| 879 | /* Parse the loop control expression for the while statement. */ |
| 880 | new_line = insert_args (cmd->line); |
| 881 | if (!new_line) |
| 882 | return invalid_control; |
| 883 | old_chain = make_cleanup ((make_cleanup_func) free_current_contents, |
| 884 | &new_line); |
| 885 | expr = parse_expression (new_line); |
| 886 | make_cleanup ((make_cleanup_func) free_current_contents, &expr); |
| 887 | |
| 888 | ret = simple_control; |
| 889 | loop = 1; |
| 890 | |
| 891 | /* Keep iterating so long as the expression is true. */ |
| 892 | while (loop == 1) |
| 893 | { |
| 894 | int cond_result; |
| 895 | |
| 896 | QUIT; |
| 897 | |
| 898 | /* Evaluate the expression. */ |
| 899 | val_mark = value_mark (); |
| 900 | val = evaluate_expression (expr); |
| 901 | cond_result = value_true (val); |
| 902 | value_free_to_mark (val_mark); |
| 903 | |
| 904 | /* If the value is false, then break out of the loop. */ |
| 905 | if (!cond_result) |
| 906 | break; |
| 907 | |
| 908 | /* Execute the body of the while statement. */ |
| 909 | current = *cmd->body_list; |
| 910 | while (current) |
| 911 | { |
| 912 | ret = execute_control_command (current); |
| 913 | |
| 914 | /* If we got an error, or a "break" command, then stop |
| 915 | looping. */ |
| 916 | if (ret == invalid_control || ret == break_control) |
| 917 | { |
| 918 | loop = 0; |
| 919 | break; |
| 920 | } |
| 921 | |
| 922 | /* If we got a "continue" command, then restart the loop |
| 923 | at this point. */ |
| 924 | if (ret == continue_control) |
| 925 | break; |
| 926 | |
| 927 | /* Get the next statement. */ |
| 928 | current = current->next; |
| 929 | } |
| 930 | } |
| 931 | |
| 932 | /* Reset RET so that we don't recurse the break all the way down. */ |
| 933 | if (ret == break_control) |
| 934 | ret = simple_control; |
| 935 | |
| 936 | break; |
| 937 | } |
| 938 | |
| 939 | case if_control: |
| 940 | { |
| 941 | new_line = insert_args (cmd->line); |
| 942 | if (!new_line) |
| 943 | return invalid_control; |
| 944 | old_chain = make_cleanup ((make_cleanup_func) free_current_contents, |
| 945 | &new_line); |
| 946 | /* Parse the conditional for the if statement. */ |
| 947 | expr = parse_expression (new_line); |
| 948 | make_cleanup ((make_cleanup_func) free_current_contents, &expr); |
| 949 | |
| 950 | current = NULL; |
| 951 | ret = simple_control; |
| 952 | |
| 953 | /* Evaluate the conditional. */ |
| 954 | val_mark = value_mark (); |
| 955 | val = evaluate_expression (expr); |
| 956 | |
| 957 | /* Choose which arm to take commands from based on the value of the |
| 958 | conditional expression. */ |
| 959 | if (value_true (val)) |
| 960 | current = *cmd->body_list; |
| 961 | else if (cmd->body_count == 2) |
| 962 | current = *(cmd->body_list + 1); |
| 963 | value_free_to_mark (val_mark); |
| 964 | |
| 965 | /* Execute commands in the given arm. */ |
| 966 | while (current) |
| 967 | { |
| 968 | ret = execute_control_command (current); |
| 969 | |
| 970 | /* If we got an error, get out. */ |
| 971 | if (ret != simple_control) |
| 972 | break; |
| 973 | |
| 974 | /* Get the next statement in the body. */ |
| 975 | current = current->next; |
| 976 | } |
| 977 | |
| 978 | break; |
| 979 | } |
| 980 | |
| 981 | default: |
| 982 | warning ("Invalid control type in command structure."); |
| 983 | return invalid_control; |
| 984 | } |
| 985 | |
| 986 | if (old_chain) |
| 987 | do_cleanups (old_chain); |
| 988 | |
| 989 | return ret; |
| 990 | } |
| 991 | |
| 992 | /* "while" command support. Executes a body of statements while the |
| 993 | loop condition is nonzero. */ |
| 994 | |
| 995 | static void |
| 996 | while_command (arg, from_tty) |
| 997 | char *arg; |
| 998 | int from_tty; |
| 999 | { |
| 1000 | struct command_line *command = NULL; |
| 1001 | |
| 1002 | control_level = 1; |
| 1003 | command = get_command_line (while_control, arg); |
| 1004 | |
| 1005 | if (command == NULL) |
| 1006 | return; |
| 1007 | |
| 1008 | execute_control_command (command); |
| 1009 | free_command_lines (&command); |
| 1010 | } |
| 1011 | |
| 1012 | /* "if" command support. Execute either the true or false arm depending |
| 1013 | on the value of the if conditional. */ |
| 1014 | |
| 1015 | static void |
| 1016 | if_command (arg, from_tty) |
| 1017 | char *arg; |
| 1018 | int from_tty; |
| 1019 | { |
| 1020 | struct command_line *command = NULL; |
| 1021 | |
| 1022 | control_level = 1; |
| 1023 | command = get_command_line (if_control, arg); |
| 1024 | |
| 1025 | if (command == NULL) |
| 1026 | return; |
| 1027 | |
| 1028 | execute_control_command (command); |
| 1029 | free_command_lines (&command); |
| 1030 | } |
| 1031 | |
| 1032 | /* Cleanup */ |
| 1033 | static void |
| 1034 | arg_cleanup () |
| 1035 | { |
| 1036 | struct user_args *oargs = user_args; |
| 1037 | if (!user_args) |
| 1038 | fatal ("Internal error, arg_cleanup called with no user args.\n"); |
| 1039 | |
| 1040 | user_args = user_args->next; |
| 1041 | free (oargs); |
| 1042 | } |
| 1043 | |
| 1044 | /* Bind the incomming arguments for a user defined command to |
| 1045 | $arg0, $arg1 ... $argMAXUSERARGS. */ |
| 1046 | |
| 1047 | static struct cleanup * |
| 1048 | setup_user_args (p) |
| 1049 | char *p; |
| 1050 | { |
| 1051 | struct user_args *args; |
| 1052 | struct cleanup *old_chain; |
| 1053 | unsigned int arg_count = 0; |
| 1054 | |
| 1055 | args = (struct user_args *)xmalloc (sizeof (struct user_args)); |
| 1056 | memset (args, 0, sizeof (struct user_args)); |
| 1057 | |
| 1058 | args->next = user_args; |
| 1059 | user_args = args; |
| 1060 | |
| 1061 | old_chain = make_cleanup ((make_cleanup_func) arg_cleanup, 0); |
| 1062 | |
| 1063 | if (p == NULL) |
| 1064 | return old_chain; |
| 1065 | |
| 1066 | while (*p) |
| 1067 | { |
| 1068 | char *start_arg; |
| 1069 | int squote = 0; |
| 1070 | int dquote = 0; |
| 1071 | int bsquote = 0; |
| 1072 | |
| 1073 | if (arg_count >= MAXUSERARGS) |
| 1074 | { |
| 1075 | error ("user defined function may only have %d arguments.\n", |
| 1076 | MAXUSERARGS); |
| 1077 | return old_chain; |
| 1078 | } |
| 1079 | |
| 1080 | /* Strip whitespace. */ |
| 1081 | while (*p == ' ' || *p == '\t') |
| 1082 | p++; |
| 1083 | |
| 1084 | /* P now points to an argument. */ |
| 1085 | start_arg = p; |
| 1086 | user_args->a[arg_count].arg = p; |
| 1087 | |
| 1088 | /* Get to the end of this argument. */ |
| 1089 | while (*p) |
| 1090 | { |
| 1091 | if (((*p == ' ' || *p == '\t')) && !squote && !dquote && !bsquote) |
| 1092 | break; |
| 1093 | else |
| 1094 | { |
| 1095 | if (bsquote) |
| 1096 | bsquote = 0; |
| 1097 | else if (*p == '\\') |
| 1098 | bsquote = 1; |
| 1099 | else if (squote) |
| 1100 | { |
| 1101 | if (*p == '\'') |
| 1102 | squote = 0; |
| 1103 | } |
| 1104 | else if (dquote) |
| 1105 | { |
| 1106 | if (*p == '"') |
| 1107 | dquote = 0; |
| 1108 | } |
| 1109 | else |
| 1110 | { |
| 1111 | if (*p == '\'') |
| 1112 | squote = 1; |
| 1113 | else if (*p == '"') |
| 1114 | dquote = 1; |
| 1115 | } |
| 1116 | p++; |
| 1117 | } |
| 1118 | } |
| 1119 | |
| 1120 | user_args->a[arg_count].len = p - start_arg; |
| 1121 | arg_count++; |
| 1122 | user_args->count++; |
| 1123 | } |
| 1124 | return old_chain; |
| 1125 | } |
| 1126 | |
| 1127 | /* Given character string P, return a point to the first argument ($arg), |
| 1128 | or NULL if P contains no arguments. */ |
| 1129 | |
| 1130 | static char * |
| 1131 | locate_arg (p) |
| 1132 | char *p; |
| 1133 | { |
| 1134 | while ((p = strchr (p, '$'))) |
| 1135 | { |
| 1136 | if (strncmp (p, "$arg", 4) == 0 && isdigit (p[4])) |
| 1137 | return p; |
| 1138 | p++; |
| 1139 | } |
| 1140 | return NULL; |
| 1141 | } |
| 1142 | |
| 1143 | /* Insert the user defined arguments stored in user_arg into the $arg |
| 1144 | arguments found in line, with the updated copy being placed into nline. */ |
| 1145 | |
| 1146 | static char * |
| 1147 | insert_args (line) |
| 1148 | char *line; |
| 1149 | { |
| 1150 | char *p, *save_line, *new_line; |
| 1151 | unsigned len, i; |
| 1152 | |
| 1153 | /* First we need to know how much memory to allocate for the new line. */ |
| 1154 | save_line = line; |
| 1155 | len = 0; |
| 1156 | while ((p = locate_arg (line))) |
| 1157 | { |
| 1158 | len += p - line; |
| 1159 | i = p[4] - '0'; |
| 1160 | |
| 1161 | if (i >= user_args->count) |
| 1162 | { |
| 1163 | error ("Missing argument %d in user function.\n", i); |
| 1164 | return NULL; |
| 1165 | } |
| 1166 | len += user_args->a[i].len; |
| 1167 | line = p + 5; |
| 1168 | } |
| 1169 | |
| 1170 | /* Don't forget the tail. */ |
| 1171 | len += strlen (line); |
| 1172 | |
| 1173 | /* Allocate space for the new line and fill it in. */ |
| 1174 | new_line = (char *)xmalloc (len + 1); |
| 1175 | if (new_line == NULL) |
| 1176 | return NULL; |
| 1177 | |
| 1178 | /* Restore pointer to beginning of old line. */ |
| 1179 | line = save_line; |
| 1180 | |
| 1181 | /* Save pointer to beginning of new line. */ |
| 1182 | save_line = new_line; |
| 1183 | |
| 1184 | while ((p = locate_arg (line))) |
| 1185 | { |
| 1186 | int i, len; |
| 1187 | |
| 1188 | memcpy (new_line, line, p - line); |
| 1189 | new_line += p - line; |
| 1190 | i = p[4] - '0'; |
| 1191 | |
| 1192 | len = user_args->a[i].len; |
| 1193 | if (len) |
| 1194 | { |
| 1195 | memcpy (new_line, user_args->a[i].arg, len); |
| 1196 | new_line += len; |
| 1197 | } |
| 1198 | line = p + 5; |
| 1199 | } |
| 1200 | /* Don't forget the tail. */ |
| 1201 | strcpy (new_line, line); |
| 1202 | |
| 1203 | /* Return a pointer to the beginning of the new line. */ |
| 1204 | return save_line; |
| 1205 | } |
| 1206 | |
| 1207 | void |
| 1208 | execute_user_command (c, args) |
| 1209 | struct cmd_list_element *c; |
| 1210 | char *args; |
| 1211 | { |
| 1212 | register struct command_line *cmdlines; |
| 1213 | struct cleanup *old_chain; |
| 1214 | enum command_control_type ret; |
| 1215 | |
| 1216 | old_chain = setup_user_args (args); |
| 1217 | |
| 1218 | cmdlines = c->user_commands; |
| 1219 | if (cmdlines == 0) |
| 1220 | /* Null command */ |
| 1221 | return; |
| 1222 | |
| 1223 | /* Set the instream to 0, indicating execution of a |
| 1224 | user-defined function. */ |
| 1225 | old_chain = make_cleanup ((make_cleanup_func) source_cleanup, instream); |
| 1226 | instream = (FILE *) 0; |
| 1227 | while (cmdlines) |
| 1228 | { |
| 1229 | ret = execute_control_command (cmdlines); |
| 1230 | if (ret != simple_control && ret != break_control) |
| 1231 | { |
| 1232 | warning ("Error in control structure.\n"); |
| 1233 | break; |
| 1234 | } |
| 1235 | cmdlines = cmdlines->next; |
| 1236 | } |
| 1237 | do_cleanups (old_chain); |
| 1238 | } |
| 1239 | |
| 1240 | /* Execute the line P as a command. |
| 1241 | Pass FROM_TTY as second argument to the defining function. */ |
| 1242 | |
| 1243 | void |
| 1244 | execute_command (p, from_tty) |
| 1245 | char *p; |
| 1246 | int from_tty; |
| 1247 | { |
| 1248 | register struct cmd_list_element *c; |
| 1249 | register enum language flang; |
| 1250 | static int warned = 0; |
| 1251 | /* FIXME: These should really be in an appropriate header file */ |
| 1252 | extern void serial_log_command PARAMS ((const char *)); |
| 1253 | |
| 1254 | free_all_values (); |
| 1255 | |
| 1256 | /* Force cleanup of any alloca areas if using C alloca instead of |
| 1257 | a builtin alloca. */ |
| 1258 | alloca (0); |
| 1259 | |
| 1260 | /* This can happen when command_line_input hits end of file. */ |
| 1261 | if (p == NULL) |
| 1262 | return; |
| 1263 | |
| 1264 | serial_log_command (p); |
| 1265 | |
| 1266 | while (*p == ' ' || *p == '\t') p++; |
| 1267 | if (*p) |
| 1268 | { |
| 1269 | char *arg; |
| 1270 | |
| 1271 | c = lookup_cmd (&p, cmdlist, "", 0, 1); |
| 1272 | |
| 1273 | /* If the target is running, we allow only a limited set of |
| 1274 | commands. */ |
| 1275 | if (async_p && target_has_async && target_executing) |
| 1276 | if (!strcmp (c->name, "help") |
| 1277 | && !strcmp (c->name, "pwd") |
| 1278 | && !strcmp (c->name, "show") |
| 1279 | && !strcmp (c->name, "stop")) |
| 1280 | error ("Cannot execute this command while the target is running."); |
| 1281 | |
| 1282 | /* Pass null arg rather than an empty one. */ |
| 1283 | arg = *p ? p : 0; |
| 1284 | |
| 1285 | /* Clear off trailing whitespace, except for set and complete command. */ |
| 1286 | if (arg && c->type != set_cmd && c->function.cfunc != complete_command) |
| 1287 | { |
| 1288 | p = arg + strlen (arg) - 1; |
| 1289 | while (p >= arg && (*p == ' ' || *p == '\t')) |
| 1290 | p--; |
| 1291 | *(p + 1) = '\0'; |
| 1292 | } |
| 1293 | |
| 1294 | /* If this command has been hooked, run the hook first. */ |
| 1295 | if (c->hook) |
| 1296 | execute_user_command (c->hook, (char *)0); |
| 1297 | |
| 1298 | if (c->class == class_user) |
| 1299 | execute_user_command (c, arg); |
| 1300 | else if (c->type == set_cmd || c->type == show_cmd) |
| 1301 | do_setshow_command (arg, from_tty & caution, c); |
| 1302 | else if (c->function.cfunc == NO_FUNCTION) |
| 1303 | error ("That is not a command, just a help topic."); |
| 1304 | else if (call_command_hook) |
| 1305 | call_command_hook (c, arg, from_tty & caution); |
| 1306 | else |
| 1307 | (*c->function.cfunc) (arg, from_tty & caution); |
| 1308 | } |
| 1309 | |
| 1310 | /* Tell the user if the language has changed (except first time). */ |
| 1311 | if (current_language != expected_language) |
| 1312 | { |
| 1313 | if (language_mode == language_mode_auto) { |
| 1314 | language_info (1); /* Print what changed. */ |
| 1315 | } |
| 1316 | warned = 0; |
| 1317 | } |
| 1318 | |
| 1319 | /* Warn the user if the working language does not match the |
| 1320 | language of the current frame. Only warn the user if we are |
| 1321 | actually running the program, i.e. there is a stack. */ |
| 1322 | /* FIXME: This should be cacheing the frame and only running when |
| 1323 | the frame changes. */ |
| 1324 | |
| 1325 | if (target_has_stack) |
| 1326 | { |
| 1327 | flang = get_frame_language (); |
| 1328 | if (!warned |
| 1329 | && flang != language_unknown |
| 1330 | && flang != current_language->la_language) |
| 1331 | { |
| 1332 | printf_filtered ("%s\n", lang_frame_mismatch_warn); |
| 1333 | warned = 1; |
| 1334 | } |
| 1335 | } |
| 1336 | } |
| 1337 | |
| 1338 | /* ARGSUSED */ |
| 1339 | /* NOTE 1999-04-29: This function will be static again, once we modify |
| 1340 | gdb to use the event loop as the default command loop and we merge |
| 1341 | event-top.c into this file, top.c */ |
| 1342 | /* static */ void |
| 1343 | command_loop_marker (foo) |
| 1344 | int foo; |
| 1345 | { |
| 1346 | } |
| 1347 | |
| 1348 | /* Read commands from `instream' and execute them |
| 1349 | until end of file or error reading instream. */ |
| 1350 | |
| 1351 | void |
| 1352 | command_loop () |
| 1353 | { |
| 1354 | struct cleanup *old_chain; |
| 1355 | char *command; |
| 1356 | int stdin_is_tty = ISATTY (stdin); |
| 1357 | long time_at_cmd_start; |
| 1358 | #ifdef HAVE_SBRK |
| 1359 | long space_at_cmd_start = 0; |
| 1360 | #endif |
| 1361 | extern int display_time; |
| 1362 | extern int display_space; |
| 1363 | |
| 1364 | while (instream && !feof (instream)) |
| 1365 | { |
| 1366 | #if defined(TUI) |
| 1367 | extern int insert_mode; |
| 1368 | #endif |
| 1369 | if (window_hook && instream == stdin) |
| 1370 | (*window_hook) (instream, get_prompt ()); |
| 1371 | |
| 1372 | quit_flag = 0; |
| 1373 | if (instream == stdin && stdin_is_tty) |
| 1374 | reinitialize_more_filter (); |
| 1375 | old_chain = make_cleanup ((make_cleanup_func) command_loop_marker, 0); |
| 1376 | |
| 1377 | #if defined(TUI) |
| 1378 | /* A bit of paranoia: I want to make sure the "insert_mode" global |
| 1379 | * is clear except when it is being used for command-line editing |
| 1380 | * (see tuiIO.c, utils.c); otherwise normal output will |
| 1381 | * get messed up in the TUI. So clear it before/after |
| 1382 | * the command-line-input call. - RT |
| 1383 | */ |
| 1384 | insert_mode = 0; |
| 1385 | #endif |
| 1386 | /* Get a command-line. This calls the readline package. */ |
| 1387 | command = command_line_input (instream == stdin ? |
| 1388 | get_prompt () : (char *) NULL, |
| 1389 | instream == stdin, "prompt"); |
| 1390 | #if defined(TUI) |
| 1391 | insert_mode = 0; |
| 1392 | #endif |
| 1393 | if (command == 0) |
| 1394 | return; |
| 1395 | |
| 1396 | time_at_cmd_start = get_run_time (); |
| 1397 | |
| 1398 | if (display_space) |
| 1399 | { |
| 1400 | #ifdef HAVE_SBRK |
| 1401 | extern char **environ; |
| 1402 | char *lim = (char *) sbrk (0); |
| 1403 | |
| 1404 | space_at_cmd_start = (long) (lim - (char *) &environ); |
| 1405 | #endif |
| 1406 | } |
| 1407 | |
| 1408 | execute_command (command, instream == stdin); |
| 1409 | /* Do any commands attached to breakpoint we stopped at. */ |
| 1410 | bpstat_do_actions (&stop_bpstat); |
| 1411 | do_cleanups (old_chain); |
| 1412 | |
| 1413 | if (display_time) |
| 1414 | { |
| 1415 | long cmd_time = get_run_time () - time_at_cmd_start; |
| 1416 | |
| 1417 | printf_unfiltered ("Command execution time: %ld.%06ld\n", |
| 1418 | cmd_time / 1000000, cmd_time % 1000000); |
| 1419 | } |
| 1420 | |
| 1421 | if (display_space) |
| 1422 | { |
| 1423 | #ifdef HAVE_SBRK |
| 1424 | extern char **environ; |
| 1425 | char *lim = (char *) sbrk (0); |
| 1426 | long space_now = lim - (char *) &environ; |
| 1427 | long space_diff = space_now - space_at_cmd_start; |
| 1428 | |
| 1429 | printf_unfiltered ("Space used: %ld (%c%ld for this command)\n", |
| 1430 | space_now, |
| 1431 | (space_diff >= 0 ? '+' : '-'), |
| 1432 | space_diff); |
| 1433 | #endif |
| 1434 | } |
| 1435 | } |
| 1436 | } |
| 1437 | |
| 1438 | \f |
| 1439 | /* Commands call this if they do not want to be repeated by null lines. */ |
| 1440 | |
| 1441 | void |
| 1442 | dont_repeat () |
| 1443 | { |
| 1444 | if (server_command) |
| 1445 | return; |
| 1446 | |
| 1447 | /* If we aren't reading from standard input, we are saving the last |
| 1448 | thing read from stdin in line and don't want to delete it. Null lines |
| 1449 | won't repeat here in any case. */ |
| 1450 | if (instream == stdin) |
| 1451 | *line = 0; |
| 1452 | } |
| 1453 | \f |
| 1454 | /* Read a line from the stream "instream" without command line editing. |
| 1455 | |
| 1456 | It prints PROMPT_ARG once at the start. |
| 1457 | Action is compatible with "readline", e.g. space for the result is |
| 1458 | malloc'd and should be freed by the caller. |
| 1459 | |
| 1460 | A NULL return means end of file. */ |
| 1461 | char * |
| 1462 | gdb_readline (prompt_arg) |
| 1463 | char *prompt_arg; |
| 1464 | { |
| 1465 | int c; |
| 1466 | char *result; |
| 1467 | int input_index = 0; |
| 1468 | int result_size = 80; |
| 1469 | |
| 1470 | if (prompt_arg) |
| 1471 | { |
| 1472 | /* Don't use a _filtered function here. It causes the assumed |
| 1473 | character position to be off, since the newline we read from |
| 1474 | the user is not accounted for. */ |
| 1475 | fputs_unfiltered (prompt_arg, gdb_stdout); |
| 1476 | #ifdef MPW |
| 1477 | /* Move to a new line so the entered line doesn't have a prompt |
| 1478 | on the front of it. */ |
| 1479 | fputs_unfiltered ("\n", gdb_stdout); |
| 1480 | #endif /* MPW */ |
| 1481 | gdb_flush (gdb_stdout); |
| 1482 | } |
| 1483 | |
| 1484 | result = (char *) xmalloc (result_size); |
| 1485 | |
| 1486 | while (1) |
| 1487 | { |
| 1488 | /* Read from stdin if we are executing a user defined command. |
| 1489 | This is the right thing for prompt_for_continue, at least. */ |
| 1490 | c = fgetc (instream ? instream : stdin); |
| 1491 | |
| 1492 | if (c == EOF) |
| 1493 | { |
| 1494 | if (input_index > 0) |
| 1495 | /* The last line does not end with a newline. Return it, and |
| 1496 | if we are called again fgetc will still return EOF and |
| 1497 | we'll return NULL then. */ |
| 1498 | break; |
| 1499 | free (result); |
| 1500 | return NULL; |
| 1501 | } |
| 1502 | |
| 1503 | if (c == '\n') |
| 1504 | #ifndef CRLF_SOURCE_FILES |
| 1505 | break; |
| 1506 | #else |
| 1507 | { |
| 1508 | if (input_index > 0 && result[input_index - 1] == '\r') |
| 1509 | input_index--; |
| 1510 | break; |
| 1511 | } |
| 1512 | #endif |
| 1513 | |
| 1514 | result[input_index++] = c; |
| 1515 | while (input_index >= result_size) |
| 1516 | { |
| 1517 | result_size *= 2; |
| 1518 | result = (char *) xrealloc (result, result_size); |
| 1519 | } |
| 1520 | } |
| 1521 | |
| 1522 | result[input_index++] = '\0'; |
| 1523 | return result; |
| 1524 | } |
| 1525 | |
| 1526 | /* Variables which control command line editing and history |
| 1527 | substitution. These variables are given default values at the end |
| 1528 | of this file. */ |
| 1529 | static int command_editing_p; |
| 1530 | /* NOTE 1999-04-29: This variable will be static again, once we modify |
| 1531 | gdb to use the event loop as the default command loop and we merge |
| 1532 | event-top.c into this file, top.c */ |
| 1533 | /* static */ int history_expansion_p; |
| 1534 | static int write_history_p; |
| 1535 | static int history_size; |
| 1536 | static char *history_filename; |
| 1537 | |
| 1538 | /* readline uses the word breaks for two things: |
| 1539 | (1) In figuring out where to point the TEXT parameter to the |
| 1540 | rl_completion_entry_function. Since we don't use TEXT for much, |
| 1541 | it doesn't matter a lot what the word breaks are for this purpose, but |
| 1542 | it does affect how much stuff M-? lists. |
| 1543 | (2) If one of the matches contains a word break character, readline |
| 1544 | will quote it. That's why we switch between |
| 1545 | gdb_completer_word_break_characters and |
| 1546 | gdb_completer_command_word_break_characters. I'm not sure when |
| 1547 | we need this behavior (perhaps for funky characters in C++ symbols?). */ |
| 1548 | |
| 1549 | /* Variables which are necessary for fancy command line editing. */ |
| 1550 | char *gdb_completer_word_break_characters = |
| 1551 | " \t\n!@#$%^&*()+=|~`}{[]\"';:?/>.<,-"; |
| 1552 | |
| 1553 | /* When completing on command names, we remove '-' from the list of |
| 1554 | word break characters, since we use it in command names. If the |
| 1555 | readline library sees one in any of the current completion strings, |
| 1556 | it thinks that the string needs to be quoted and automatically supplies |
| 1557 | a leading quote. */ |
| 1558 | char *gdb_completer_command_word_break_characters = |
| 1559 | " \t\n!@#$%^&*()+=|~`}{[]\"';:?/>.<,"; |
| 1560 | |
| 1561 | /* Characters that can be used to quote completion strings. Note that we |
| 1562 | can't include '"' because the gdb C parser treats such quoted sequences |
| 1563 | as strings. */ |
| 1564 | char *gdb_completer_quote_characters = |
| 1565 | "'"; |
| 1566 | |
| 1567 | /* Functions that are used as part of the fancy command line editing. */ |
| 1568 | |
| 1569 | /* This can be used for functions which don't want to complete on symbols |
| 1570 | but don't want to complete on anything else either. */ |
| 1571 | /* ARGSUSED */ |
| 1572 | char ** |
| 1573 | noop_completer (text, prefix) |
| 1574 | char *text; |
| 1575 | char *prefix; |
| 1576 | { |
| 1577 | return NULL; |
| 1578 | } |
| 1579 | |
| 1580 | /* Complete on filenames. */ |
| 1581 | char ** |
| 1582 | filename_completer (text, word) |
| 1583 | char *text; |
| 1584 | char *word; |
| 1585 | { |
| 1586 | /* From readline. */ |
| 1587 | extern char *filename_completion_function PARAMS ((char *, int)); |
| 1588 | int subsequent_name; |
| 1589 | char **return_val; |
| 1590 | int return_val_used; |
| 1591 | int return_val_alloced; |
| 1592 | |
| 1593 | return_val_used = 0; |
| 1594 | /* Small for testing. */ |
| 1595 | return_val_alloced = 1; |
| 1596 | return_val = (char **) xmalloc (return_val_alloced * sizeof (char *)); |
| 1597 | |
| 1598 | subsequent_name = 0; |
| 1599 | while (1) |
| 1600 | { |
| 1601 | char *p; |
| 1602 | p = filename_completion_function (text, subsequent_name); |
| 1603 | if (return_val_used >= return_val_alloced) |
| 1604 | { |
| 1605 | return_val_alloced *= 2; |
| 1606 | return_val = |
| 1607 | (char **) xrealloc (return_val, |
| 1608 | return_val_alloced * sizeof (char *)); |
| 1609 | } |
| 1610 | if (p == NULL) |
| 1611 | { |
| 1612 | return_val[return_val_used++] = p; |
| 1613 | break; |
| 1614 | } |
| 1615 | /* Like emacs, don't complete on old versions. Especially useful |
| 1616 | in the "source" command. */ |
| 1617 | if (p[strlen (p) - 1] == '~') |
| 1618 | continue; |
| 1619 | |
| 1620 | { |
| 1621 | char *q; |
| 1622 | if (word == text) |
| 1623 | /* Return exactly p. */ |
| 1624 | return_val[return_val_used++] = p; |
| 1625 | else if (word > text) |
| 1626 | { |
| 1627 | /* Return some portion of p. */ |
| 1628 | q = xmalloc (strlen (p) + 5); |
| 1629 | strcpy (q, p + (word - text)); |
| 1630 | return_val[return_val_used++] = q; |
| 1631 | free (p); |
| 1632 | } |
| 1633 | else |
| 1634 | { |
| 1635 | /* Return some of TEXT plus p. */ |
| 1636 | q = xmalloc (strlen (p) + (text - word) + 5); |
| 1637 | strncpy (q, word, text - word); |
| 1638 | q[text - word] = '\0'; |
| 1639 | strcat (q, p); |
| 1640 | return_val[return_val_used++] = q; |
| 1641 | free (p); |
| 1642 | } |
| 1643 | } |
| 1644 | subsequent_name = 1; |
| 1645 | } |
| 1646 | #if 0 |
| 1647 | /* There is no way to do this just long enough to affect quote inserting |
| 1648 | without also affecting the next completion. This should be fixed in |
| 1649 | readline. FIXME. */ |
| 1650 | /* Insure that readline does the right thing |
| 1651 | with respect to inserting quotes. */ |
| 1652 | rl_completer_word_break_characters = ""; |
| 1653 | #endif |
| 1654 | return return_val; |
| 1655 | } |
| 1656 | |
| 1657 | /* Here are some useful test cases for completion. FIXME: These should |
| 1658 | be put in the test suite. They should be tested with both M-? and TAB. |
| 1659 | |
| 1660 | "show output-" "radix" |
| 1661 | "show output" "-radix" |
| 1662 | "p" ambiguous (commands starting with p--path, print, printf, etc.) |
| 1663 | "p " ambiguous (all symbols) |
| 1664 | "info t foo" no completions |
| 1665 | "info t " no completions |
| 1666 | "info t" ambiguous ("info target", "info terminal", etc.) |
| 1667 | "info ajksdlfk" no completions |
| 1668 | "info ajksdlfk " no completions |
| 1669 | "info" " " |
| 1670 | "info " ambiguous (all info commands) |
| 1671 | "p \"a" no completions (string constant) |
| 1672 | "p 'a" ambiguous (all symbols starting with a) |
| 1673 | "p b-a" ambiguous (all symbols starting with a) |
| 1674 | "p b-" ambiguous (all symbols) |
| 1675 | "file Make" "file" (word break hard to screw up here) |
| 1676 | "file ../gdb.stabs/we" "ird" (needs to not break word at slash) |
| 1677 | */ |
| 1678 | |
| 1679 | /* Generate completions one by one for the completer. Each time we are |
| 1680 | called return another potential completion to the caller. |
| 1681 | line_completion just completes on commands or passes the buck to the |
| 1682 | command's completer function, the stuff specific to symbol completion |
| 1683 | is in make_symbol_completion_list. |
| 1684 | |
| 1685 | TEXT is the caller's idea of the "word" we are looking at. |
| 1686 | |
| 1687 | MATCHES is the number of matches that have currently been collected from |
| 1688 | calling this completion function. When zero, then we need to initialize, |
| 1689 | otherwise the initialization has already taken place and we can just |
| 1690 | return the next potential completion string. |
| 1691 | |
| 1692 | LINE_BUFFER is available to be looked at; it contains the entire text |
| 1693 | of the line. POINT is the offset in that line of the cursor. You |
| 1694 | should pretend that the line ends at POINT. |
| 1695 | |
| 1696 | Returns NULL if there are no more completions, else a pointer to a string |
| 1697 | which is a possible completion, it is the caller's responsibility to |
| 1698 | free the string. */ |
| 1699 | |
| 1700 | static char * |
| 1701 | line_completion_function (text, matches, line_buffer, point) |
| 1702 | char *text; |
| 1703 | int matches; |
| 1704 | char *line_buffer; |
| 1705 | int point; |
| 1706 | { |
| 1707 | static char **list = (char **)NULL; /* Cache of completions */ |
| 1708 | static int index; /* Next cached completion */ |
| 1709 | char *output = NULL; |
| 1710 | char *tmp_command, *p; |
| 1711 | /* Pointer within tmp_command which corresponds to text. */ |
| 1712 | char *word; |
| 1713 | struct cmd_list_element *c, *result_list; |
| 1714 | |
| 1715 | if (matches == 0) |
| 1716 | { |
| 1717 | /* The caller is beginning to accumulate a new set of completions, so |
| 1718 | we need to find all of them now, and cache them for returning one at |
| 1719 | a time on future calls. */ |
| 1720 | |
| 1721 | if (list) |
| 1722 | { |
| 1723 | /* Free the storage used by LIST, but not by the strings inside. |
| 1724 | This is because rl_complete_internal () frees the strings. */ |
| 1725 | free ((PTR)list); |
| 1726 | } |
| 1727 | list = 0; |
| 1728 | index = 0; |
| 1729 | |
| 1730 | /* Choose the default set of word break characters to break completions. |
| 1731 | If we later find out that we are doing completions on command strings |
| 1732 | (as opposed to strings supplied by the individual command completer |
| 1733 | functions, which can be any string) then we will switch to the |
| 1734 | special word break set for command strings, which leaves out the |
| 1735 | '-' character used in some commands. */ |
| 1736 | |
| 1737 | rl_completer_word_break_characters = |
| 1738 | gdb_completer_word_break_characters; |
| 1739 | |
| 1740 | /* Decide whether to complete on a list of gdb commands or on symbols. */ |
| 1741 | tmp_command = (char *) alloca (point + 1); |
| 1742 | p = tmp_command; |
| 1743 | |
| 1744 | strncpy (tmp_command, line_buffer, point); |
| 1745 | tmp_command[point] = '\0'; |
| 1746 | /* Since text always contains some number of characters leading up |
| 1747 | to point, we can find the equivalent position in tmp_command |
| 1748 | by subtracting that many characters from the end of tmp_command. */ |
| 1749 | word = tmp_command + point - strlen (text); |
| 1750 | |
| 1751 | if (point == 0) |
| 1752 | { |
| 1753 | /* An empty line we want to consider ambiguous; that is, it |
| 1754 | could be any command. */ |
| 1755 | c = (struct cmd_list_element *) -1; |
| 1756 | result_list = 0; |
| 1757 | } |
| 1758 | else |
| 1759 | { |
| 1760 | c = lookup_cmd_1 (&p, cmdlist, &result_list, 1); |
| 1761 | } |
| 1762 | |
| 1763 | /* Move p up to the next interesting thing. */ |
| 1764 | while (*p == ' ' || *p == '\t') |
| 1765 | { |
| 1766 | p++; |
| 1767 | } |
| 1768 | |
| 1769 | if (!c) |
| 1770 | { |
| 1771 | /* It is an unrecognized command. So there are no |
| 1772 | possible completions. */ |
| 1773 | list = NULL; |
| 1774 | } |
| 1775 | else if (c == (struct cmd_list_element *) -1) |
| 1776 | { |
| 1777 | char *q; |
| 1778 | |
| 1779 | /* lookup_cmd_1 advances p up to the first ambiguous thing, but |
| 1780 | doesn't advance over that thing itself. Do so now. */ |
| 1781 | q = p; |
| 1782 | while (*q && (isalnum (*q) || *q == '-' || *q == '_')) |
| 1783 | ++q; |
| 1784 | if (q != tmp_command + point) |
| 1785 | { |
| 1786 | /* There is something beyond the ambiguous |
| 1787 | command, so there are no possible completions. For |
| 1788 | example, "info t " or "info t foo" does not complete |
| 1789 | to anything, because "info t" can be "info target" or |
| 1790 | "info terminal". */ |
| 1791 | list = NULL; |
| 1792 | } |
| 1793 | else |
| 1794 | { |
| 1795 | /* We're trying to complete on the command which was ambiguous. |
| 1796 | This we can deal with. */ |
| 1797 | if (result_list) |
| 1798 | { |
| 1799 | list = complete_on_cmdlist (*result_list->prefixlist, p, |
| 1800 | word); |
| 1801 | } |
| 1802 | else |
| 1803 | { |
| 1804 | list = complete_on_cmdlist (cmdlist, p, word); |
| 1805 | } |
| 1806 | /* Insure that readline does the right thing with respect to |
| 1807 | inserting quotes. */ |
| 1808 | rl_completer_word_break_characters = |
| 1809 | gdb_completer_command_word_break_characters; |
| 1810 | } |
| 1811 | } |
| 1812 | else |
| 1813 | { |
| 1814 | /* We've recognized a full command. */ |
| 1815 | |
| 1816 | if (p == tmp_command + point) |
| 1817 | { |
| 1818 | /* There is no non-whitespace in the line beyond the command. */ |
| 1819 | |
| 1820 | if (p[-1] == ' ' || p[-1] == '\t') |
| 1821 | { |
| 1822 | /* The command is followed by whitespace; we need to complete |
| 1823 | on whatever comes after command. */ |
| 1824 | if (c->prefixlist) |
| 1825 | { |
| 1826 | /* It is a prefix command; what comes after it is |
| 1827 | a subcommand (e.g. "info "). */ |
| 1828 | list = complete_on_cmdlist (*c->prefixlist, p, word); |
| 1829 | |
| 1830 | /* Insure that readline does the right thing |
| 1831 | with respect to inserting quotes. */ |
| 1832 | rl_completer_word_break_characters = |
| 1833 | gdb_completer_command_word_break_characters; |
| 1834 | } |
| 1835 | else if (c->enums) |
| 1836 | { |
| 1837 | list = complete_on_enum (c->enums, p, word); |
| 1838 | rl_completer_word_break_characters = |
| 1839 | gdb_completer_command_word_break_characters; |
| 1840 | } |
| 1841 | else |
| 1842 | { |
| 1843 | /* It is a normal command; what comes after it is |
| 1844 | completed by the command's completer function. */ |
| 1845 | list = (*c->completer) (p, word); |
| 1846 | } |
| 1847 | } |
| 1848 | else |
| 1849 | { |
| 1850 | /* The command is not followed by whitespace; we need to |
| 1851 | complete on the command itself. e.g. "p" which is a |
| 1852 | command itself but also can complete to "print", "ptype" |
| 1853 | etc. */ |
| 1854 | char *q; |
| 1855 | |
| 1856 | /* Find the command we are completing on. */ |
| 1857 | q = p; |
| 1858 | while (q > tmp_command) |
| 1859 | { |
| 1860 | if (isalnum (q[-1]) || q[-1] == '-' || q[-1] == '_') |
| 1861 | --q; |
| 1862 | else |
| 1863 | break; |
| 1864 | } |
| 1865 | |
| 1866 | list = complete_on_cmdlist (result_list, q, word); |
| 1867 | |
| 1868 | /* Insure that readline does the right thing |
| 1869 | with respect to inserting quotes. */ |
| 1870 | rl_completer_word_break_characters = |
| 1871 | gdb_completer_command_word_break_characters; |
| 1872 | } |
| 1873 | } |
| 1874 | else |
| 1875 | { |
| 1876 | /* There is non-whitespace beyond the command. */ |
| 1877 | |
| 1878 | if (c->prefixlist && !c->allow_unknown) |
| 1879 | { |
| 1880 | /* It is an unrecognized subcommand of a prefix command, |
| 1881 | e.g. "info adsfkdj". */ |
| 1882 | list = NULL; |
| 1883 | } |
| 1884 | else if (c->enums) |
| 1885 | { |
| 1886 | list = complete_on_enum (c->enums, p, word); |
| 1887 | } |
| 1888 | else |
| 1889 | { |
| 1890 | /* It is a normal command. */ |
| 1891 | list = (*c->completer) (p, word); |
| 1892 | } |
| 1893 | } |
| 1894 | } |
| 1895 | } |
| 1896 | |
| 1897 | /* If we found a list of potential completions during initialization then |
| 1898 | dole them out one at a time. The vector of completions is NULL |
| 1899 | terminated, so after returning the last one, return NULL (and continue |
| 1900 | to do so) each time we are called after that, until a new list is |
| 1901 | available. */ |
| 1902 | |
| 1903 | if (list) |
| 1904 | { |
| 1905 | output = list[index]; |
| 1906 | if (output) |
| 1907 | { |
| 1908 | index++; |
| 1909 | } |
| 1910 | } |
| 1911 | |
| 1912 | #if 0 |
| 1913 | /* Can't do this because readline hasn't yet checked the word breaks |
| 1914 | for figuring out whether to insert a quote. */ |
| 1915 | if (output == NULL) |
| 1916 | /* Make sure the word break characters are set back to normal for the |
| 1917 | next time that readline tries to complete something. */ |
| 1918 | rl_completer_word_break_characters = |
| 1919 | gdb_completer_word_break_characters; |
| 1920 | #endif |
| 1921 | |
| 1922 | return (output); |
| 1923 | } |
| 1924 | |
| 1925 | /* Line completion interface function for readline. */ |
| 1926 | |
| 1927 | static char * |
| 1928 | readline_line_completion_function (text, matches) |
| 1929 | char *text; |
| 1930 | int matches; |
| 1931 | { |
| 1932 | return line_completion_function (text, matches, rl_line_buffer, rl_point); |
| 1933 | } |
| 1934 | |
| 1935 | /* Skip over a possibly quoted word (as defined by the quote characters |
| 1936 | and word break characters the completer uses). Returns pointer to the |
| 1937 | location after the "word". */ |
| 1938 | |
| 1939 | char * |
| 1940 | skip_quoted (str) |
| 1941 | char *str; |
| 1942 | { |
| 1943 | char quote_char = '\0'; |
| 1944 | char *scan; |
| 1945 | |
| 1946 | for (scan = str; *scan != '\0'; scan++) |
| 1947 | { |
| 1948 | if (quote_char != '\0') |
| 1949 | { |
| 1950 | /* Ignore everything until the matching close quote char */ |
| 1951 | if (*scan == quote_char) |
| 1952 | { |
| 1953 | /* Found matching close quote. */ |
| 1954 | scan++; |
| 1955 | break; |
| 1956 | } |
| 1957 | } |
| 1958 | else if (strchr (gdb_completer_quote_characters, *scan)) |
| 1959 | { |
| 1960 | /* Found start of a quoted string. */ |
| 1961 | quote_char = *scan; |
| 1962 | } |
| 1963 | else if (strchr (gdb_completer_word_break_characters, *scan)) |
| 1964 | { |
| 1965 | break; |
| 1966 | } |
| 1967 | } |
| 1968 | return (scan); |
| 1969 | } |
| 1970 | |
| 1971 | \f |
| 1972 | #ifdef STOP_SIGNAL |
| 1973 | static void |
| 1974 | stop_sig (signo) |
| 1975 | int signo; |
| 1976 | { |
| 1977 | #if STOP_SIGNAL == SIGTSTP |
| 1978 | signal (SIGTSTP, SIG_DFL); |
| 1979 | sigsetmask (0); |
| 1980 | kill (getpid (), SIGTSTP); |
| 1981 | signal (SIGTSTP, stop_sig); |
| 1982 | #else |
| 1983 | signal (STOP_SIGNAL, stop_sig); |
| 1984 | #endif |
| 1985 | printf_unfiltered ("%s", get_prompt ()); |
| 1986 | gdb_flush (gdb_stdout); |
| 1987 | |
| 1988 | /* Forget about any previous command -- null line now will do nothing. */ |
| 1989 | dont_repeat (); |
| 1990 | } |
| 1991 | #endif /* STOP_SIGNAL */ |
| 1992 | |
| 1993 | /* Initialize signal handlers. */ |
| 1994 | static void |
| 1995 | do_nothing (signo) |
| 1996 | int signo; |
| 1997 | { |
| 1998 | /* Under System V the default disposition of a signal is reinstated after |
| 1999 | the signal is caught and delivered to an application process. On such |
| 2000 | systems one must restore the replacement signal handler if one wishes |
| 2001 | to continue handling the signal in one's program. On BSD systems this |
| 2002 | is not needed but it is harmless, and it simplifies the code to just do |
| 2003 | it unconditionally. */ |
| 2004 | signal (signo, do_nothing); |
| 2005 | } |
| 2006 | |
| 2007 | static void |
| 2008 | init_signals () |
| 2009 | { |
| 2010 | signal (SIGINT, request_quit); |
| 2011 | |
| 2012 | /* If SIGTRAP was set to SIG_IGN, then the SIG_IGN will get passed |
| 2013 | to the inferior and breakpoints will be ignored. */ |
| 2014 | #ifdef SIGTRAP |
| 2015 | signal (SIGTRAP, SIG_DFL); |
| 2016 | #endif |
| 2017 | |
| 2018 | /* If we initialize SIGQUIT to SIG_IGN, then the SIG_IGN will get |
| 2019 | passed to the inferior, which we don't want. It would be |
| 2020 | possible to do a "signal (SIGQUIT, SIG_DFL)" after we fork, but |
| 2021 | on BSD4.3 systems using vfork, that can affect the |
| 2022 | GDB process as well as the inferior (the signal handling tables |
| 2023 | might be in memory, shared between the two). Since we establish |
| 2024 | a handler for SIGQUIT, when we call exec it will set the signal |
| 2025 | to SIG_DFL for us. */ |
| 2026 | signal (SIGQUIT, do_nothing); |
| 2027 | #ifdef SIGHUP |
| 2028 | if (signal (SIGHUP, do_nothing) != SIG_IGN) |
| 2029 | signal (SIGHUP, disconnect); |
| 2030 | #endif |
| 2031 | signal (SIGFPE, float_handler); |
| 2032 | |
| 2033 | #if defined(SIGWINCH) && defined(SIGWINCH_HANDLER) |
| 2034 | signal (SIGWINCH, SIGWINCH_HANDLER); |
| 2035 | #endif |
| 2036 | } |
| 2037 | \f |
| 2038 | /* Read one line from the command input stream `instream' |
| 2039 | into the local static buffer `linebuffer' (whose current length |
| 2040 | is `linelength'). |
| 2041 | The buffer is made bigger as necessary. |
| 2042 | Returns the address of the start of the line. |
| 2043 | |
| 2044 | NULL is returned for end of file. |
| 2045 | |
| 2046 | *If* the instream == stdin & stdin is a terminal, the line read |
| 2047 | is copied into the file line saver (global var char *line, |
| 2048 | length linesize) so that it can be duplicated. |
| 2049 | |
| 2050 | This routine either uses fancy command line editing or |
| 2051 | simple input as the user has requested. */ |
| 2052 | |
| 2053 | char * |
| 2054 | command_line_input (prompt_arg, repeat, annotation_suffix) |
| 2055 | char *prompt_arg; |
| 2056 | int repeat; |
| 2057 | char *annotation_suffix; |
| 2058 | { |
| 2059 | static char *linebuffer = 0; |
| 2060 | static unsigned linelength = 0; |
| 2061 | register char *p; |
| 2062 | char *p1; |
| 2063 | char *rl; |
| 2064 | char *local_prompt = prompt_arg; |
| 2065 | char *nline; |
| 2066 | char got_eof = 0; |
| 2067 | |
| 2068 | /* The annotation suffix must be non-NULL. */ |
| 2069 | if (annotation_suffix == NULL) |
| 2070 | annotation_suffix = ""; |
| 2071 | |
| 2072 | if (annotation_level > 1 && instream == stdin) |
| 2073 | { |
| 2074 | local_prompt = alloca ((prompt_arg == NULL ? 0 : strlen (prompt_arg)) |
| 2075 | + strlen (annotation_suffix) + 40); |
| 2076 | if (prompt_arg == NULL) |
| 2077 | local_prompt[0] = '\0'; |
| 2078 | else |
| 2079 | strcpy (local_prompt, prompt_arg); |
| 2080 | strcat (local_prompt, "\n\032\032"); |
| 2081 | strcat (local_prompt, annotation_suffix); |
| 2082 | strcat (local_prompt, "\n"); |
| 2083 | } |
| 2084 | |
| 2085 | if (linebuffer == 0) |
| 2086 | { |
| 2087 | linelength = 80; |
| 2088 | linebuffer = (char *) xmalloc (linelength); |
| 2089 | } |
| 2090 | |
| 2091 | p = linebuffer; |
| 2092 | |
| 2093 | /* Control-C quits instantly if typed while in this loop |
| 2094 | since it should not wait until the user types a newline. */ |
| 2095 | immediate_quit++; |
| 2096 | #ifdef STOP_SIGNAL |
| 2097 | if (job_control) |
| 2098 | { |
| 2099 | if (async_p) |
| 2100 | signal (STOP_SIGNAL, handle_stop_sig); |
| 2101 | else |
| 2102 | signal (STOP_SIGNAL, stop_sig); |
| 2103 | } |
| 2104 | #endif |
| 2105 | |
| 2106 | while (1) |
| 2107 | { |
| 2108 | /* Make sure that all output has been output. Some machines may let |
| 2109 | you get away with leaving out some of the gdb_flush, but not all. */ |
| 2110 | wrap_here (""); |
| 2111 | gdb_flush (gdb_stdout); |
| 2112 | gdb_flush (gdb_stderr); |
| 2113 | |
| 2114 | if (source_file_name != NULL) |
| 2115 | { |
| 2116 | ++source_line_number; |
| 2117 | sprintf (source_error, |
| 2118 | "%s%s:%d: Error in sourced command file:\n", |
| 2119 | source_pre_error, |
| 2120 | source_file_name, |
| 2121 | source_line_number); |
| 2122 | error_pre_print = source_error; |
| 2123 | } |
| 2124 | |
| 2125 | if (annotation_level > 1 && instream == stdin) |
| 2126 | { |
| 2127 | printf_unfiltered ("\n\032\032pre-"); |
| 2128 | printf_unfiltered (annotation_suffix); |
| 2129 | printf_unfiltered ("\n"); |
| 2130 | } |
| 2131 | |
| 2132 | /* Don't use fancy stuff if not talking to stdin. */ |
| 2133 | if (readline_hook && instream == NULL) |
| 2134 | { |
| 2135 | rl = (*readline_hook) (local_prompt); |
| 2136 | } |
| 2137 | else if (command_editing_p && instream == stdin && ISATTY (instream)) |
| 2138 | { |
| 2139 | rl = readline (local_prompt); |
| 2140 | } |
| 2141 | else |
| 2142 | { |
| 2143 | rl = gdb_readline (local_prompt); |
| 2144 | } |
| 2145 | |
| 2146 | if (annotation_level > 1 && instream == stdin) |
| 2147 | { |
| 2148 | printf_unfiltered ("\n\032\032post-"); |
| 2149 | printf_unfiltered (annotation_suffix); |
| 2150 | printf_unfiltered ("\n"); |
| 2151 | } |
| 2152 | |
| 2153 | if (!rl || rl == (char *) EOF) |
| 2154 | { |
| 2155 | got_eof = 1; |
| 2156 | break; |
| 2157 | } |
| 2158 | if (strlen(rl) + 1 + (p - linebuffer) > linelength) |
| 2159 | { |
| 2160 | linelength = strlen(rl) + 1 + (p - linebuffer); |
| 2161 | nline = (char *) xrealloc (linebuffer, linelength); |
| 2162 | p += nline - linebuffer; |
| 2163 | linebuffer = nline; |
| 2164 | } |
| 2165 | p1 = rl; |
| 2166 | /* Copy line. Don't copy null at end. (Leaves line alone |
| 2167 | if this was just a newline) */ |
| 2168 | while (*p1) |
| 2169 | *p++ = *p1++; |
| 2170 | |
| 2171 | free (rl); /* Allocated in readline. */ |
| 2172 | |
| 2173 | if (p == linebuffer || *(p - 1) != '\\') |
| 2174 | break; |
| 2175 | |
| 2176 | p--; /* Put on top of '\'. */ |
| 2177 | local_prompt = (char *) 0; |
| 2178 | } |
| 2179 | |
| 2180 | #ifdef STOP_SIGNAL |
| 2181 | if (job_control) |
| 2182 | signal (STOP_SIGNAL, SIG_DFL); |
| 2183 | #endif |
| 2184 | immediate_quit--; |
| 2185 | |
| 2186 | if (got_eof) |
| 2187 | return NULL; |
| 2188 | |
| 2189 | #define SERVER_COMMAND_LENGTH 7 |
| 2190 | server_command = |
| 2191 | (p - linebuffer > SERVER_COMMAND_LENGTH) |
| 2192 | && STREQN (linebuffer, "server ", SERVER_COMMAND_LENGTH); |
| 2193 | if (server_command) |
| 2194 | { |
| 2195 | /* Note that we don't set `line'. Between this and the check in |
| 2196 | dont_repeat, this insures that repeating will still do the |
| 2197 | right thing. */ |
| 2198 | *p = '\0'; |
| 2199 | return linebuffer + SERVER_COMMAND_LENGTH; |
| 2200 | } |
| 2201 | |
| 2202 | /* Do history expansion if that is wished. */ |
| 2203 | if (history_expansion_p && instream == stdin |
| 2204 | && ISATTY (instream)) |
| 2205 | { |
| 2206 | char *history_value; |
| 2207 | int expanded; |
| 2208 | |
| 2209 | *p = '\0'; /* Insert null now. */ |
| 2210 | expanded = history_expand (linebuffer, &history_value); |
| 2211 | if (expanded) |
| 2212 | { |
| 2213 | /* Print the changes. */ |
| 2214 | printf_unfiltered ("%s\n", history_value); |
| 2215 | |
| 2216 | /* If there was an error, call this function again. */ |
| 2217 | if (expanded < 0) |
| 2218 | { |
| 2219 | free (history_value); |
| 2220 | return command_line_input (prompt_arg, repeat, annotation_suffix); |
| 2221 | } |
| 2222 | if (strlen (history_value) > linelength) |
| 2223 | { |
| 2224 | linelength = strlen (history_value) + 1; |
| 2225 | linebuffer = (char *) xrealloc (linebuffer, linelength); |
| 2226 | } |
| 2227 | strcpy (linebuffer, history_value); |
| 2228 | p = linebuffer + strlen(linebuffer); |
| 2229 | free (history_value); |
| 2230 | } |
| 2231 | } |
| 2232 | |
| 2233 | /* If we just got an empty line, and that is supposed |
| 2234 | to repeat the previous command, return the value in the |
| 2235 | global buffer. */ |
| 2236 | if (repeat && p == linebuffer) |
| 2237 | return line; |
| 2238 | for (p1 = linebuffer; *p1 == ' ' || *p1 == '\t'; p1++) ; |
| 2239 | if (repeat && !*p1) |
| 2240 | return line; |
| 2241 | |
| 2242 | *p = 0; |
| 2243 | |
| 2244 | /* Add line to history if appropriate. */ |
| 2245 | if (instream == stdin |
| 2246 | && ISATTY (stdin) && *linebuffer) |
| 2247 | add_history (linebuffer); |
| 2248 | |
| 2249 | /* Note: lines consisting solely of comments are added to the command |
| 2250 | history. This is useful when you type a command, and then |
| 2251 | realize you don't want to execute it quite yet. You can comment |
| 2252 | out the command and then later fetch it from the value history |
| 2253 | and remove the '#'. The kill ring is probably better, but some |
| 2254 | people are in the habit of commenting things out. */ |
| 2255 | if (*p1 == '#') |
| 2256 | *p1 = '\0'; /* Found a comment. */ |
| 2257 | |
| 2258 | /* Save into global buffer if appropriate. */ |
| 2259 | if (repeat) |
| 2260 | { |
| 2261 | if (linelength > linesize) |
| 2262 | { |
| 2263 | line = xrealloc (line, linelength); |
| 2264 | linesize = linelength; |
| 2265 | } |
| 2266 | strcpy (line, linebuffer); |
| 2267 | return line; |
| 2268 | } |
| 2269 | |
| 2270 | return linebuffer; |
| 2271 | } |
| 2272 | \f |
| 2273 | |
| 2274 | /* Expand the body_list of COMMAND so that it can hold NEW_LENGTH |
| 2275 | code bodies. This is typically used when we encounter an "else" |
| 2276 | clause for an "if" command. */ |
| 2277 | |
| 2278 | static void |
| 2279 | realloc_body_list (command, new_length) |
| 2280 | struct command_line *command; |
| 2281 | int new_length; |
| 2282 | { |
| 2283 | int n; |
| 2284 | struct command_line **body_list; |
| 2285 | |
| 2286 | n = command->body_count; |
| 2287 | |
| 2288 | /* Nothing to do? */ |
| 2289 | if (new_length <= n) |
| 2290 | return; |
| 2291 | |
| 2292 | body_list = (struct command_line **) |
| 2293 | xmalloc (sizeof (struct command_line *) * new_length); |
| 2294 | |
| 2295 | memcpy (body_list, command->body_list, sizeof (struct command_line *) * n); |
| 2296 | |
| 2297 | free (command->body_list); |
| 2298 | command->body_list = body_list; |
| 2299 | command->body_count = new_length; |
| 2300 | } |
| 2301 | |
| 2302 | /* Read one line from the input stream. If the command is an "else" or |
| 2303 | "end", return such an indication to the caller. */ |
| 2304 | |
| 2305 | static enum misc_command_type |
| 2306 | read_next_line (command) |
| 2307 | struct command_line **command; |
| 2308 | { |
| 2309 | char *p, *p1, *prompt_ptr, control_prompt[256]; |
| 2310 | int i = 0; |
| 2311 | |
| 2312 | if (control_level >= 254) |
| 2313 | error ("Control nesting too deep!\n"); |
| 2314 | |
| 2315 | /* Set a prompt based on the nesting of the control commands. */ |
| 2316 | if (instream == stdin || (instream == 0 && readline_hook != NULL)) |
| 2317 | { |
| 2318 | for (i = 0; i < control_level; i++) |
| 2319 | control_prompt[i] = ' '; |
| 2320 | control_prompt[i] = '>'; |
| 2321 | control_prompt[i+1] = '\0'; |
| 2322 | prompt_ptr = (char *)&control_prompt[0]; |
| 2323 | } |
| 2324 | else |
| 2325 | prompt_ptr = NULL; |
| 2326 | |
| 2327 | p = command_line_input (prompt_ptr, instream == stdin, "commands"); |
| 2328 | |
| 2329 | /* Not sure what to do here. */ |
| 2330 | if (p == NULL) |
| 2331 | return end_command; |
| 2332 | |
| 2333 | /* Strip leading and trailing whitespace. */ |
| 2334 | while (*p == ' ' || *p == '\t') |
| 2335 | p++; |
| 2336 | |
| 2337 | p1 = p + strlen (p); |
| 2338 | while (p1 != p && (p1[-1] == ' ' || p1[-1] == '\t')) |
| 2339 | p1--; |
| 2340 | |
| 2341 | /* Blanks and comments don't really do anything, but we need to |
| 2342 | distinguish them from else, end and other commands which can be |
| 2343 | executed. */ |
| 2344 | if (p1 == p || p[0] == '#') |
| 2345 | return nop_command; |
| 2346 | |
| 2347 | /* Is this the end of a simple, while, or if control structure? */ |
| 2348 | if (p1 - p == 3 && !strncmp (p, "end", 3)) |
| 2349 | return end_command; |
| 2350 | |
| 2351 | /* Is the else clause of an if control structure? */ |
| 2352 | if (p1 - p == 4 && !strncmp (p, "else", 4)) |
| 2353 | return else_command; |
| 2354 | |
| 2355 | /* Check for while, if, break, continue, etc and build a new command |
| 2356 | line structure for them. */ |
| 2357 | if (p1 - p > 5 && !strncmp (p, "while", 5)) |
| 2358 | *command = build_command_line (while_control, p + 6); |
| 2359 | else if (p1 - p > 2 && !strncmp (p, "if", 2)) |
| 2360 | *command = build_command_line (if_control, p + 3); |
| 2361 | else if (p1 - p == 10 && !strncmp (p, "loop_break", 10)) |
| 2362 | { |
| 2363 | *command = (struct command_line *) |
| 2364 | xmalloc (sizeof (struct command_line)); |
| 2365 | (*command)->next = NULL; |
| 2366 | (*command)->line = NULL; |
| 2367 | (*command)->control_type = break_control; |
| 2368 | (*command)->body_count = 0; |
| 2369 | (*command)->body_list = NULL; |
| 2370 | } |
| 2371 | else if (p1 - p == 13 && !strncmp (p, "loop_continue", 13)) |
| 2372 | { |
| 2373 | *command = (struct command_line *) |
| 2374 | xmalloc (sizeof (struct command_line)); |
| 2375 | (*command)->next = NULL; |
| 2376 | (*command)->line = NULL; |
| 2377 | (*command)->control_type = continue_control; |
| 2378 | (*command)->body_count = 0; |
| 2379 | (*command)->body_list = NULL; |
| 2380 | } |
| 2381 | else |
| 2382 | { |
| 2383 | /* A normal command. */ |
| 2384 | *command = (struct command_line *) |
| 2385 | xmalloc (sizeof (struct command_line)); |
| 2386 | (*command)->next = NULL; |
| 2387 | (*command)->line = savestring (p, p1 - p); |
| 2388 | (*command)->control_type = simple_control; |
| 2389 | (*command)->body_count = 0; |
| 2390 | (*command)->body_list = NULL; |
| 2391 | } |
| 2392 | |
| 2393 | /* Nothing special. */ |
| 2394 | return ok_command; |
| 2395 | } |
| 2396 | |
| 2397 | /* Recursively read in the control structures and create a command_line |
| 2398 | structure from them. |
| 2399 | |
| 2400 | The parent_control parameter is the control structure in which the |
| 2401 | following commands are nested. */ |
| 2402 | |
| 2403 | static enum command_control_type |
| 2404 | recurse_read_control_structure (current_cmd) |
| 2405 | struct command_line *current_cmd; |
| 2406 | { |
| 2407 | int current_body, i; |
| 2408 | enum misc_command_type val; |
| 2409 | enum command_control_type ret; |
| 2410 | struct command_line **body_ptr, *child_tail, *next; |
| 2411 | |
| 2412 | child_tail = NULL; |
| 2413 | current_body = 1; |
| 2414 | |
| 2415 | /* Sanity checks. */ |
| 2416 | if (current_cmd->control_type == simple_control) |
| 2417 | { |
| 2418 | error ("Recursed on a simple control type\n"); |
| 2419 | return invalid_control; |
| 2420 | } |
| 2421 | |
| 2422 | if (current_body > current_cmd->body_count) |
| 2423 | { |
| 2424 | error ("Allocated body is smaller than this command type needs\n"); |
| 2425 | return invalid_control; |
| 2426 | } |
| 2427 | |
| 2428 | /* Read lines from the input stream and build control structures. */ |
| 2429 | while (1) |
| 2430 | { |
| 2431 | dont_repeat (); |
| 2432 | |
| 2433 | next = NULL; |
| 2434 | val = read_next_line (&next); |
| 2435 | |
| 2436 | /* Just skip blanks and comments. */ |
| 2437 | if (val == nop_command) |
| 2438 | continue; |
| 2439 | |
| 2440 | if (val == end_command) |
| 2441 | { |
| 2442 | if (current_cmd->control_type == while_control |
| 2443 | || current_cmd->control_type == if_control) |
| 2444 | { |
| 2445 | /* Success reading an entire control structure. */ |
| 2446 | ret = simple_control; |
| 2447 | break; |
| 2448 | } |
| 2449 | else |
| 2450 | { |
| 2451 | ret = invalid_control; |
| 2452 | break; |
| 2453 | } |
| 2454 | } |
| 2455 | |
| 2456 | /* Not the end of a control structure. */ |
| 2457 | if (val == else_command) |
| 2458 | { |
| 2459 | if (current_cmd->control_type == if_control |
| 2460 | && current_body == 1) |
| 2461 | { |
| 2462 | realloc_body_list (current_cmd, 2); |
| 2463 | current_body = 2; |
| 2464 | child_tail = NULL; |
| 2465 | continue; |
| 2466 | } |
| 2467 | else |
| 2468 | { |
| 2469 | ret = invalid_control; |
| 2470 | break; |
| 2471 | } |
| 2472 | } |
| 2473 | |
| 2474 | if (child_tail) |
| 2475 | { |
| 2476 | child_tail->next = next; |
| 2477 | } |
| 2478 | else |
| 2479 | { |
| 2480 | body_ptr = current_cmd->body_list; |
| 2481 | for (i = 1; i < current_body; i++) |
| 2482 | body_ptr++; |
| 2483 | |
| 2484 | *body_ptr = next; |
| 2485 | |
| 2486 | } |
| 2487 | |
| 2488 | child_tail = next; |
| 2489 | |
| 2490 | /* If the latest line is another control structure, then recurse |
| 2491 | on it. */ |
| 2492 | if (next->control_type == while_control |
| 2493 | || next->control_type == if_control) |
| 2494 | { |
| 2495 | control_level++; |
| 2496 | ret = recurse_read_control_structure (next); |
| 2497 | control_level--; |
| 2498 | |
| 2499 | if (ret != simple_control) |
| 2500 | break; |
| 2501 | } |
| 2502 | } |
| 2503 | |
| 2504 | dont_repeat (); |
| 2505 | |
| 2506 | return ret; |
| 2507 | } |
| 2508 | |
| 2509 | /* Read lines from the input stream and accumulate them in a chain of |
| 2510 | struct command_line's, which is then returned. For input from a |
| 2511 | terminal, the special command "end" is used to mark the end of the |
| 2512 | input, and is not included in the returned chain of commands. */ |
| 2513 | |
| 2514 | #define END_MESSAGE "End with a line saying just \"end\"." |
| 2515 | |
| 2516 | struct command_line * |
| 2517 | read_command_lines (prompt_arg, from_tty) |
| 2518 | char *prompt_arg; |
| 2519 | int from_tty; |
| 2520 | { |
| 2521 | struct command_line *head, *tail, *next; |
| 2522 | struct cleanup *old_chain; |
| 2523 | enum command_control_type ret; |
| 2524 | enum misc_command_type val; |
| 2525 | |
| 2526 | control_level = 0; |
| 2527 | if (readline_begin_hook) |
| 2528 | { |
| 2529 | /* Note - intentional to merge messages with no newline */ |
| 2530 | (*readline_begin_hook) ("%s %s\n", prompt_arg, END_MESSAGE); |
| 2531 | } |
| 2532 | else if (from_tty && input_from_terminal_p ()) |
| 2533 | { |
| 2534 | printf_unfiltered ("%s\n%s\n", prompt_arg, END_MESSAGE); |
| 2535 | gdb_flush (gdb_stdout); |
| 2536 | } |
| 2537 | |
| 2538 | head = tail = NULL; |
| 2539 | old_chain = NULL; |
| 2540 | |
| 2541 | while (1) |
| 2542 | { |
| 2543 | val = read_next_line (&next); |
| 2544 | |
| 2545 | /* Ignore blank lines or comments. */ |
| 2546 | if (val == nop_command) |
| 2547 | continue; |
| 2548 | |
| 2549 | if (val == end_command) |
| 2550 | { |
| 2551 | ret = simple_control; |
| 2552 | break; |
| 2553 | } |
| 2554 | |
| 2555 | if (val != ok_command) |
| 2556 | { |
| 2557 | ret = invalid_control; |
| 2558 | break; |
| 2559 | } |
| 2560 | |
| 2561 | if (next->control_type == while_control |
| 2562 | || next->control_type == if_control) |
| 2563 | { |
| 2564 | control_level++; |
| 2565 | ret = recurse_read_control_structure (next); |
| 2566 | control_level--; |
| 2567 | |
| 2568 | if (ret == invalid_control) |
| 2569 | break; |
| 2570 | } |
| 2571 | |
| 2572 | if (tail) |
| 2573 | { |
| 2574 | tail->next = next; |
| 2575 | } |
| 2576 | else |
| 2577 | { |
| 2578 | head = next; |
| 2579 | old_chain = make_cleanup ((make_cleanup_func) free_command_lines, |
| 2580 | &head); |
| 2581 | } |
| 2582 | tail = next; |
| 2583 | } |
| 2584 | |
| 2585 | dont_repeat (); |
| 2586 | |
| 2587 | if (head) |
| 2588 | { |
| 2589 | if (ret != invalid_control) |
| 2590 | { |
| 2591 | discard_cleanups (old_chain); |
| 2592 | } |
| 2593 | else |
| 2594 | do_cleanups (old_chain); |
| 2595 | } |
| 2596 | |
| 2597 | if (readline_end_hook) |
| 2598 | { |
| 2599 | (*readline_end_hook) (); |
| 2600 | } |
| 2601 | return (head); |
| 2602 | } |
| 2603 | |
| 2604 | /* Free a chain of struct command_line's. */ |
| 2605 | |
| 2606 | void |
| 2607 | free_command_lines (lptr) |
| 2608 | struct command_line **lptr; |
| 2609 | { |
| 2610 | register struct command_line *l = *lptr; |
| 2611 | register struct command_line *next; |
| 2612 | struct command_line **blist; |
| 2613 | int i; |
| 2614 | |
| 2615 | while (l) |
| 2616 | { |
| 2617 | if (l->body_count > 0) |
| 2618 | { |
| 2619 | blist = l->body_list; |
| 2620 | for (i = 0; i < l->body_count; i++, blist++) |
| 2621 | free_command_lines (blist); |
| 2622 | } |
| 2623 | next = l->next; |
| 2624 | free (l->line); |
| 2625 | free ((PTR)l); |
| 2626 | l = next; |
| 2627 | } |
| 2628 | } |
| 2629 | \f |
| 2630 | /* Add an element to the list of info subcommands. */ |
| 2631 | |
| 2632 | void |
| 2633 | add_info (name, fun, doc) |
| 2634 | char *name; |
| 2635 | void (*fun) PARAMS ((char *, int)); |
| 2636 | char *doc; |
| 2637 | { |
| 2638 | add_cmd (name, no_class, fun, doc, &infolist); |
| 2639 | } |
| 2640 | |
| 2641 | /* Add an alias to the list of info subcommands. */ |
| 2642 | |
| 2643 | void |
| 2644 | add_info_alias (name, oldname, abbrev_flag) |
| 2645 | char *name; |
| 2646 | char *oldname; |
| 2647 | int abbrev_flag; |
| 2648 | { |
| 2649 | add_alias_cmd (name, oldname, 0, abbrev_flag, &infolist); |
| 2650 | } |
| 2651 | |
| 2652 | /* The "info" command is defined as a prefix, with allow_unknown = 0. |
| 2653 | Therefore, its own definition is called only for "info" with no args. */ |
| 2654 | |
| 2655 | /* ARGSUSED */ |
| 2656 | static void |
| 2657 | info_command (arg, from_tty) |
| 2658 | char *arg; |
| 2659 | int from_tty; |
| 2660 | { |
| 2661 | printf_unfiltered ("\"info\" must be followed by the name of an info command.\n"); |
| 2662 | help_list (infolist, "info ", -1, gdb_stdout); |
| 2663 | } |
| 2664 | |
| 2665 | /* The "complete" command is used by Emacs to implement completion. */ |
| 2666 | |
| 2667 | /* ARGSUSED */ |
| 2668 | static void |
| 2669 | complete_command (arg, from_tty) |
| 2670 | char *arg; |
| 2671 | int from_tty; |
| 2672 | { |
| 2673 | int i; |
| 2674 | int argpoint; |
| 2675 | char *completion; |
| 2676 | |
| 2677 | dont_repeat (); |
| 2678 | |
| 2679 | if (arg == NULL) |
| 2680 | arg = ""; |
| 2681 | argpoint = strlen (arg); |
| 2682 | |
| 2683 | for (completion = line_completion_function (arg, i = 0, arg, argpoint); |
| 2684 | completion; |
| 2685 | completion = line_completion_function (arg, ++i, arg, argpoint)) |
| 2686 | { |
| 2687 | printf_unfiltered ("%s\n", completion); |
| 2688 | free (completion); |
| 2689 | } |
| 2690 | } |
| 2691 | |
| 2692 | /* The "show" command with no arguments shows all the settings. */ |
| 2693 | |
| 2694 | /* ARGSUSED */ |
| 2695 | static void |
| 2696 | show_command (arg, from_tty) |
| 2697 | char *arg; |
| 2698 | int from_tty; |
| 2699 | { |
| 2700 | cmd_show_list (showlist, from_tty, ""); |
| 2701 | } |
| 2702 | \f |
| 2703 | /* Add an element to the list of commands. */ |
| 2704 | |
| 2705 | void |
| 2706 | add_com (name, class, fun, doc) |
| 2707 | char *name; |
| 2708 | enum command_class class; |
| 2709 | void (*fun) PARAMS ((char *, int)); |
| 2710 | char *doc; |
| 2711 | { |
| 2712 | add_cmd (name, class, fun, doc, &cmdlist); |
| 2713 | } |
| 2714 | |
| 2715 | /* Add an alias or abbreviation command to the list of commands. */ |
| 2716 | |
| 2717 | void |
| 2718 | add_com_alias (name, oldname, class, abbrev_flag) |
| 2719 | char *name; |
| 2720 | char *oldname; |
| 2721 | enum command_class class; |
| 2722 | int abbrev_flag; |
| 2723 | { |
| 2724 | add_alias_cmd (name, oldname, class, abbrev_flag, &cmdlist); |
| 2725 | } |
| 2726 | |
| 2727 | void |
| 2728 | error_no_arg (why) |
| 2729 | char *why; |
| 2730 | { |
| 2731 | error ("Argument required (%s).", why); |
| 2732 | } |
| 2733 | |
| 2734 | /* ARGSUSED */ |
| 2735 | static void |
| 2736 | help_command (command, from_tty) |
| 2737 | char *command; |
| 2738 | int from_tty; /* Ignored */ |
| 2739 | { |
| 2740 | help_cmd (command, gdb_stdout); |
| 2741 | } |
| 2742 | \f |
| 2743 | static void |
| 2744 | validate_comname (comname) |
| 2745 | char *comname; |
| 2746 | { |
| 2747 | register char *p; |
| 2748 | |
| 2749 | if (comname == 0) |
| 2750 | error_no_arg ("name of command to define"); |
| 2751 | |
| 2752 | p = comname; |
| 2753 | while (*p) |
| 2754 | { |
| 2755 | if (!isalnum(*p) && *p != '-' && *p != '_') |
| 2756 | error ("Junk in argument list: \"%s\"", p); |
| 2757 | p++; |
| 2758 | } |
| 2759 | } |
| 2760 | |
| 2761 | /* This is just a placeholder in the command data structures. */ |
| 2762 | static void |
| 2763 | user_defined_command (ignore, from_tty) |
| 2764 | char *ignore; |
| 2765 | int from_tty; |
| 2766 | { |
| 2767 | } |
| 2768 | |
| 2769 | static void |
| 2770 | define_command (comname, from_tty) |
| 2771 | char *comname; |
| 2772 | int from_tty; |
| 2773 | { |
| 2774 | register struct command_line *cmds; |
| 2775 | register struct cmd_list_element *c, *newc, *hookc = 0; |
| 2776 | char *tem = comname; |
| 2777 | char tmpbuf[128]; |
| 2778 | #define HOOK_STRING "hook-" |
| 2779 | #define HOOK_LEN 5 |
| 2780 | |
| 2781 | validate_comname (comname); |
| 2782 | |
| 2783 | /* Look it up, and verify that we got an exact match. */ |
| 2784 | c = lookup_cmd (&tem, cmdlist, "", -1, 1); |
| 2785 | if (c && !STREQ (comname, c->name)) |
| 2786 | c = 0; |
| 2787 | |
| 2788 | if (c) |
| 2789 | { |
| 2790 | if (c->class == class_user || c->class == class_alias) |
| 2791 | tem = "Redefine command \"%s\"? "; |
| 2792 | else |
| 2793 | tem = "Really redefine built-in command \"%s\"? "; |
| 2794 | if (!query (tem, c->name)) |
| 2795 | error ("Command \"%s\" not redefined.", c->name); |
| 2796 | } |
| 2797 | |
| 2798 | /* If this new command is a hook, then mark the command which it |
| 2799 | is hooking. Note that we allow hooking `help' commands, so that |
| 2800 | we can hook the `stop' pseudo-command. */ |
| 2801 | |
| 2802 | if (!strncmp (comname, HOOK_STRING, HOOK_LEN)) |
| 2803 | { |
| 2804 | /* Look up cmd it hooks, and verify that we got an exact match. */ |
| 2805 | tem = comname+HOOK_LEN; |
| 2806 | hookc = lookup_cmd (&tem, cmdlist, "", -1, 0); |
| 2807 | if (hookc && !STREQ (comname+HOOK_LEN, hookc->name)) |
| 2808 | hookc = 0; |
| 2809 | if (!hookc) |
| 2810 | { |
| 2811 | warning ("Your new `%s' command does not hook any existing command.", |
| 2812 | comname); |
| 2813 | if (!query ("Proceed? ")) |
| 2814 | error ("Not confirmed."); |
| 2815 | } |
| 2816 | } |
| 2817 | |
| 2818 | comname = savestring (comname, strlen (comname)); |
| 2819 | |
| 2820 | /* If the rest of the commands will be case insensitive, this one |
| 2821 | should behave in the same manner. */ |
| 2822 | for (tem = comname; *tem; tem++) |
| 2823 | if (isupper(*tem)) *tem = tolower(*tem); |
| 2824 | |
| 2825 | sprintf (tmpbuf, "Type commands for definition of \"%s\".", comname); |
| 2826 | cmds = read_command_lines (tmpbuf, from_tty); |
| 2827 | |
| 2828 | if (c && c->class == class_user) |
| 2829 | free_command_lines (&c->user_commands); |
| 2830 | |
| 2831 | newc = add_cmd (comname, class_user, user_defined_command, |
| 2832 | (c && c->class == class_user) |
| 2833 | ? c->doc : savestring ("User-defined.", 13), &cmdlist); |
| 2834 | newc->user_commands = cmds; |
| 2835 | |
| 2836 | /* If this new command is a hook, then mark both commands as being |
| 2837 | tied. */ |
| 2838 | if (hookc) |
| 2839 | { |
| 2840 | hookc->hook = newc; /* Target gets hooked. */ |
| 2841 | newc->hookee = hookc; /* We are marked as hooking target cmd. */ |
| 2842 | } |
| 2843 | } |
| 2844 | |
| 2845 | static void |
| 2846 | document_command (comname, from_tty) |
| 2847 | char *comname; |
| 2848 | int from_tty; |
| 2849 | { |
| 2850 | struct command_line *doclines; |
| 2851 | register struct cmd_list_element *c; |
| 2852 | char *tem = comname; |
| 2853 | char tmpbuf[128]; |
| 2854 | |
| 2855 | validate_comname (comname); |
| 2856 | |
| 2857 | c = lookup_cmd (&tem, cmdlist, "", 0, 1); |
| 2858 | |
| 2859 | if (c->class != class_user) |
| 2860 | error ("Command \"%s\" is built-in.", comname); |
| 2861 | |
| 2862 | sprintf (tmpbuf, "Type documentation for \"%s\".", comname); |
| 2863 | doclines = read_command_lines (tmpbuf, from_tty); |
| 2864 | |
| 2865 | if (c->doc) free (c->doc); |
| 2866 | |
| 2867 | { |
| 2868 | register struct command_line *cl1; |
| 2869 | register int len = 0; |
| 2870 | |
| 2871 | for (cl1 = doclines; cl1; cl1 = cl1->next) |
| 2872 | len += strlen (cl1->line) + 1; |
| 2873 | |
| 2874 | c->doc = (char *) xmalloc (len + 1); |
| 2875 | *c->doc = 0; |
| 2876 | |
| 2877 | for (cl1 = doclines; cl1; cl1 = cl1->next) |
| 2878 | { |
| 2879 | strcat (c->doc, cl1->line); |
| 2880 | if (cl1->next) |
| 2881 | strcat (c->doc, "\n"); |
| 2882 | } |
| 2883 | } |
| 2884 | |
| 2885 | free_command_lines (&doclines); |
| 2886 | } |
| 2887 | \f |
| 2888 | /* Print the GDB banner. */ |
| 2889 | void |
| 2890 | print_gdb_version (stream) |
| 2891 | GDB_FILE *stream; |
| 2892 | { |
| 2893 | /* From GNU coding standards, first line is meant to be easy for a |
| 2894 | program to parse, and is just canonical program name and version |
| 2895 | number, which starts after last space. */ |
| 2896 | |
| 2897 | fprintf_filtered (stream, "GNU gdb %s\n", version); |
| 2898 | |
| 2899 | /* Second line is a copyright notice. */ |
| 2900 | |
| 2901 | fprintf_filtered (stream, "Copyright 1998 Free Software Foundation, Inc.\n"); |
| 2902 | |
| 2903 | /* Following the copyright is a brief statement that the program is |
| 2904 | free software, that users are free to copy and change it on |
| 2905 | certain conditions, that it is covered by the GNU GPL, and that |
| 2906 | there is no warranty. */ |
| 2907 | |
| 2908 | fprintf_filtered (stream, "\ |
| 2909 | GDB is free software, covered by the GNU General Public License, and you are\n\ |
| 2910 | welcome to change it and/or distribute copies of it under certain conditions.\n\ |
| 2911 | Type \"show copying\" to see the conditions.\n\ |
| 2912 | There is absolutely no warranty for GDB. Type \"show warranty\" for details.\n"); |
| 2913 | |
| 2914 | /* After the required info we print the configuration information. */ |
| 2915 | |
| 2916 | fprintf_filtered (stream, "This GDB was configured as \""); |
| 2917 | if (!STREQ (host_name, target_name)) |
| 2918 | { |
| 2919 | fprintf_filtered (stream, "--host=%s --target=%s", host_name, target_name); |
| 2920 | } |
| 2921 | else |
| 2922 | { |
| 2923 | fprintf_filtered (stream, "%s", host_name); |
| 2924 | } |
| 2925 | fprintf_filtered (stream, "\"."); |
| 2926 | } |
| 2927 | |
| 2928 | /* ARGSUSED */ |
| 2929 | static void |
| 2930 | show_version (args, from_tty) |
| 2931 | char *args; |
| 2932 | int from_tty; |
| 2933 | { |
| 2934 | immediate_quit++; |
| 2935 | print_gdb_version (gdb_stdout); |
| 2936 | printf_filtered ("\n"); |
| 2937 | immediate_quit--; |
| 2938 | } |
| 2939 | \f |
| 2940 | /* get_prompt: access method for the GDB prompt string. */ |
| 2941 | |
| 2942 | #define MAX_PROMPT_SIZE 256 |
| 2943 | |
| 2944 | /* |
| 2945 | * int get_prompt_1 (char * buf); |
| 2946 | * |
| 2947 | * Work-horse for get_prompt (called via catch_errors). |
| 2948 | * Argument is buffer to hold the formatted prompt. |
| 2949 | * |
| 2950 | * Returns: 1 for success (use formatted prompt) |
| 2951 | * 0 for failure (use gdb_prompt_string). |
| 2952 | */ |
| 2953 | |
| 2954 | static int gdb_prompt_escape; |
| 2955 | |
| 2956 | static int |
| 2957 | get_prompt_1 (formatted_prompt) |
| 2958 | char *formatted_prompt; |
| 2959 | { |
| 2960 | char *local_prompt; |
| 2961 | |
| 2962 | if (async_p) |
| 2963 | local_prompt = PROMPT (0); |
| 2964 | else |
| 2965 | local_prompt = gdb_prompt_string; |
| 2966 | |
| 2967 | |
| 2968 | if (gdb_prompt_escape == 0) |
| 2969 | { |
| 2970 | return 0; /* do no formatting */ |
| 2971 | } |
| 2972 | else /* formatted prompt */ |
| 2973 | { |
| 2974 | char fmt[40], *promptp, *outp, *tmp; |
| 2975 | value_ptr arg_val; |
| 2976 | DOUBLEST doubleval; |
| 2977 | LONGEST longval; |
| 2978 | CORE_ADDR addrval; |
| 2979 | |
| 2980 | int i, len; |
| 2981 | struct type *arg_type, *elt_type; |
| 2982 | |
| 2983 | promptp = local_prompt; |
| 2984 | outp = formatted_prompt; |
| 2985 | |
| 2986 | while (*promptp != '\0') |
| 2987 | { |
| 2988 | int available = MAX_PROMPT_SIZE - (outp - formatted_prompt) - 1; |
| 2989 | |
| 2990 | if (*promptp != gdb_prompt_escape) |
| 2991 | { |
| 2992 | if (available >= 1) /* overflow protect */ |
| 2993 | *outp++ = *promptp++; |
| 2994 | } |
| 2995 | else |
| 2996 | { |
| 2997 | /* GDB prompt string contains escape char. Parse for arg. |
| 2998 | Two consecutive escape chars followed by arg followed by |
| 2999 | a comma means to insert the arg using a default format. |
| 3000 | Otherwise a printf format string may be included between |
| 3001 | the two escape chars. eg: |
| 3002 | %%foo, insert foo using default format |
| 3003 | %2.2f%foo, insert foo using "%2.2f" format |
| 3004 | A mismatch between the format string and the data type |
| 3005 | of "foo" is an error (which we don't know how to protect |
| 3006 | against). */ |
| 3007 | |
| 3008 | fmt[0] = '\0'; /* assume null format string */ |
| 3009 | if (promptp[1] == gdb_prompt_escape) /* double esc char */ |
| 3010 | { |
| 3011 | promptp += 2; /* skip past two escape chars. */ |
| 3012 | } |
| 3013 | else |
| 3014 | { |
| 3015 | /* extract format string from between two esc chars */ |
| 3016 | i = 0; |
| 3017 | do { |
| 3018 | fmt[i++] = *promptp++; /* copy format string */ |
| 3019 | } while (i < sizeof (fmt) - 1 && |
| 3020 | *promptp != gdb_prompt_escape && |
| 3021 | *promptp != '\0'); |
| 3022 | |
| 3023 | if (*promptp != gdb_prompt_escape) |
| 3024 | error ("Syntax error at prompt position %d", |
| 3025 | promptp - local_prompt); |
| 3026 | else |
| 3027 | { |
| 3028 | promptp++; /* skip second escape char */ |
| 3029 | fmt[i++] = '\0'; /* terminate the format string */ |
| 3030 | } |
| 3031 | } |
| 3032 | |
| 3033 | arg_val = parse_to_comma_and_eval (&promptp); |
| 3034 | if (*promptp == ',') |
| 3035 | promptp++; /* skip past the comma */ |
| 3036 | arg_type = check_typedef (VALUE_TYPE (arg_val)); |
| 3037 | switch (TYPE_CODE (arg_type)) |
| 3038 | { |
| 3039 | case TYPE_CODE_ARRAY: |
| 3040 | elt_type = check_typedef (TYPE_TARGET_TYPE (arg_type)); |
| 3041 | if (TYPE_LENGTH (arg_type) > 0 && |
| 3042 | TYPE_LENGTH (elt_type) == 1 && |
| 3043 | TYPE_CODE (elt_type) == TYPE_CODE_INT) |
| 3044 | { |
| 3045 | int len = TYPE_LENGTH (arg_type); |
| 3046 | |
| 3047 | if (VALUE_LAZY (arg_val)) |
| 3048 | value_fetch_lazy (arg_val); |
| 3049 | tmp = VALUE_CONTENTS (arg_val); |
| 3050 | |
| 3051 | if (len > available) |
| 3052 | len = available; /* overflow protect */ |
| 3053 | |
| 3054 | /* FIXME: how to protect GDB from crashing |
| 3055 | from bad user-supplied format string? */ |
| 3056 | if (fmt[0] != 0) |
| 3057 | sprintf (outp, fmt, tmp); |
| 3058 | else |
| 3059 | strncpy (outp, tmp, len); |
| 3060 | outp[len] = '\0'; |
| 3061 | } |
| 3062 | break; |
| 3063 | case TYPE_CODE_PTR: |
| 3064 | elt_type = check_typedef (TYPE_TARGET_TYPE (arg_type)); |
| 3065 | addrval = value_as_pointer (arg_val); |
| 3066 | |
| 3067 | if (TYPE_LENGTH (elt_type) == 1 && |
| 3068 | TYPE_CODE (elt_type) == TYPE_CODE_INT && |
| 3069 | addrval != 0) |
| 3070 | { |
| 3071 | /* display it as a string */ |
| 3072 | char *default_fmt = "%s"; |
| 3073 | char *tmp; |
| 3074 | int err = 0; |
| 3075 | |
| 3076 | /* Limiting the number of bytes that the following call |
| 3077 | will read protects us from sprintf overflow later. */ |
| 3078 | i = target_read_string (addrval, /* src */ |
| 3079 | &tmp, /* dest */ |
| 3080 | available, /* len */ |
| 3081 | &err); |
| 3082 | if (err) /* read failed */ |
| 3083 | error ("%s on target_read", safe_strerror (err)); |
| 3084 | |
| 3085 | tmp[i] = '\0'; /* force-terminate string */ |
| 3086 | /* FIXME: how to protect GDB from crashing |
| 3087 | from bad user-supplied format string? */ |
| 3088 | sprintf (outp, fmt[0] == 0 ? default_fmt : fmt, |
| 3089 | tmp); |
| 3090 | free (tmp); |
| 3091 | } |
| 3092 | else |
| 3093 | { |
| 3094 | /* display it as a pointer */ |
| 3095 | char *default_fmt = "0x%x"; |
| 3096 | |
| 3097 | /* FIXME: how to protect GDB from crashing |
| 3098 | from bad user-supplied format string? */ |
| 3099 | if (available >= 16 /*?*/) /* overflow protect */ |
| 3100 | sprintf (outp, fmt[0] == 0 ? default_fmt : fmt, |
| 3101 | (long) addrval); |
| 3102 | } |
| 3103 | break; |
| 3104 | case TYPE_CODE_FLT: |
| 3105 | { |
| 3106 | char *default_fmt = "%g"; |
| 3107 | |
| 3108 | doubleval = value_as_double (arg_val); |
| 3109 | /* FIXME: how to protect GDB from crashing |
| 3110 | from bad user-supplied format string? */ |
| 3111 | if (available >= 16 /*?*/) /* overflow protect */ |
| 3112 | sprintf (outp, fmt[0] == 0 ? default_fmt : fmt, |
| 3113 | (double) doubleval); |
| 3114 | break; |
| 3115 | } |
| 3116 | case TYPE_CODE_INT: |
| 3117 | { |
| 3118 | char *default_fmt = "%d"; |
| 3119 | |
| 3120 | longval = value_as_long (arg_val); |
| 3121 | /* FIXME: how to protect GDB from crashing |
| 3122 | from bad user-supplied format string? */ |
| 3123 | if (available >= 16 /*?*/) /* overflow protect */ |
| 3124 | sprintf (outp, fmt[0] == 0 ? default_fmt : fmt, |
| 3125 | (long) longval); |
| 3126 | break; |
| 3127 | } |
| 3128 | case TYPE_CODE_BOOL: |
| 3129 | { |
| 3130 | /* no default format for bool */ |
| 3131 | longval = value_as_long (arg_val); |
| 3132 | if (available >= 8 /*?*/) /* overflow protect */ |
| 3133 | { |
| 3134 | if (longval) |
| 3135 | strcpy (outp, "<true>"); |
| 3136 | else |
| 3137 | strcpy (outp, "<false>"); |
| 3138 | } |
| 3139 | break; |
| 3140 | } |
| 3141 | case TYPE_CODE_ENUM: |
| 3142 | { |
| 3143 | /* no default format for enum */ |
| 3144 | longval = value_as_long (arg_val); |
| 3145 | len = TYPE_NFIELDS (arg_type); |
| 3146 | /* find enum name if possible */ |
| 3147 | for (i = 0; i < len; i++) |
| 3148 | if (TYPE_FIELD_BITPOS (arg_type, i) == longval) |
| 3149 | break; /* match -- end loop */ |
| 3150 | |
| 3151 | if (i < len) /* enum name found */ |
| 3152 | { |
| 3153 | char *name = TYPE_FIELD_NAME (arg_type, i); |
| 3154 | |
| 3155 | strncpy (outp, name, available); |
| 3156 | /* in casel available < strlen (name), */ |
| 3157 | outp[available] = '\0'; |
| 3158 | } |
| 3159 | else |
| 3160 | { |
| 3161 | if (available >= 16 /*?*/) /* overflow protect */ |
| 3162 | sprintf (outp, "%d", (long) longval); |
| 3163 | } |
| 3164 | break; |
| 3165 | } |
| 3166 | case TYPE_CODE_VOID: |
| 3167 | *outp = '\0'; |
| 3168 | break; /* void type -- no output */ |
| 3169 | default: |
| 3170 | error ("bad data type at prompt position %d", |
| 3171 | promptp - local_prompt); |
| 3172 | break; |
| 3173 | } |
| 3174 | outp += strlen (outp); |
| 3175 | } |
| 3176 | } |
| 3177 | *outp++ = '\0'; /* terminate prompt string */ |
| 3178 | return 1; |
| 3179 | } |
| 3180 | } |
| 3181 | |
| 3182 | char * |
| 3183 | get_prompt () |
| 3184 | { |
| 3185 | static char buf[MAX_PROMPT_SIZE]; |
| 3186 | |
| 3187 | if (catch_errors (get_prompt_1, buf, "bad formatted prompt: ", |
| 3188 | RETURN_MASK_ALL)) |
| 3189 | { |
| 3190 | return &buf[0]; /* successful formatted prompt */ |
| 3191 | } |
| 3192 | else |
| 3193 | { |
| 3194 | /* Prompt could not be formatted. */ |
| 3195 | if (async_p) |
| 3196 | return PROMPT (0); |
| 3197 | else |
| 3198 | return gdb_prompt_string; |
| 3199 | } |
| 3200 | } |
| 3201 | |
| 3202 | void |
| 3203 | set_prompt (s) |
| 3204 | char *s; |
| 3205 | { |
| 3206 | /* ??rehrauer: I don't know why this fails, since it looks as though |
| 3207 | assignments to prompt are wrapped in calls to savestring... |
| 3208 | if (prompt != NULL) |
| 3209 | free (prompt); |
| 3210 | */ |
| 3211 | if (async_p) |
| 3212 | PROMPT (0) = savestring (s, strlen (s)); |
| 3213 | else |
| 3214 | gdb_prompt_string = savestring (s, strlen (s)); |
| 3215 | } |
| 3216 | |
| 3217 | \f |
| 3218 | /* If necessary, make the user confirm that we should quit. Return |
| 3219 | non-zero if we should quit, zero if we shouldn't. */ |
| 3220 | |
| 3221 | int |
| 3222 | quit_confirm () |
| 3223 | { |
| 3224 | if (inferior_pid != 0 && target_has_execution) |
| 3225 | { |
| 3226 | char *s; |
| 3227 | |
| 3228 | /* This is something of a hack. But there's no reliable way to |
| 3229 | see if a GUI is running. The `use_windows' variable doesn't |
| 3230 | cut it. */ |
| 3231 | if (init_ui_hook) |
| 3232 | s = "A debugging session is active.\nDo you still want to close the debugger?"; |
| 3233 | else if (attach_flag) |
| 3234 | s = "The program is running. Quit anyway (and detach it)? "; |
| 3235 | else |
| 3236 | s = "The program is running. Exit anyway? "; |
| 3237 | |
| 3238 | if (! query (s)) |
| 3239 | return 0; |
| 3240 | } |
| 3241 | |
| 3242 | return 1; |
| 3243 | } |
| 3244 | |
| 3245 | /* Quit without asking for confirmation. */ |
| 3246 | |
| 3247 | void |
| 3248 | quit_force (args, from_tty) |
| 3249 | char *args; |
| 3250 | int from_tty; |
| 3251 | { |
| 3252 | int exit_code = 0; |
| 3253 | |
| 3254 | /* An optional expression may be used to cause gdb to terminate with the |
| 3255 | value of that expression. */ |
| 3256 | if (args) |
| 3257 | { |
| 3258 | value_ptr val = parse_and_eval (args); |
| 3259 | |
| 3260 | exit_code = (int) value_as_long (val); |
| 3261 | } |
| 3262 | |
| 3263 | if (inferior_pid != 0 && target_has_execution) |
| 3264 | { |
| 3265 | if (attach_flag) |
| 3266 | target_detach (args, from_tty); |
| 3267 | else |
| 3268 | target_kill (); |
| 3269 | } |
| 3270 | |
| 3271 | /* UDI wants this, to kill the TIP. */ |
| 3272 | target_close (1); |
| 3273 | |
| 3274 | /* Save the history information if it is appropriate to do so. */ |
| 3275 | if (write_history_p && history_filename) |
| 3276 | write_history (history_filename); |
| 3277 | |
| 3278 | do_final_cleanups(ALL_CLEANUPS); /* Do any final cleanups before exiting */ |
| 3279 | |
| 3280 | #if defined(TUI) |
| 3281 | /* tuiDo((TuiOpaqueFuncPtr)tuiCleanUp); */ |
| 3282 | /* The above does not need to be inside a tuiDo(), since |
| 3283 | * it is not manipulating the curses screen, but rather, |
| 3284 | * it is tearing it down. |
| 3285 | */ |
| 3286 | if (tui_version) |
| 3287 | tuiCleanUp(); |
| 3288 | #endif |
| 3289 | |
| 3290 | exit (exit_code); |
| 3291 | } |
| 3292 | |
| 3293 | /* Handle the quit command. */ |
| 3294 | |
| 3295 | void |
| 3296 | quit_command (args, from_tty) |
| 3297 | char *args; |
| 3298 | int from_tty; |
| 3299 | { |
| 3300 | if (! quit_confirm ()) |
| 3301 | error ("Not confirmed."); |
| 3302 | quit_force (args, from_tty); |
| 3303 | } |
| 3304 | |
| 3305 | /* Returns whether GDB is running on a terminal and whether the user |
| 3306 | desires that questions be asked of them on that terminal. */ |
| 3307 | |
| 3308 | int |
| 3309 | input_from_terminal_p () |
| 3310 | { |
| 3311 | return gdb_has_a_terminal () && (instream == stdin) & caution; |
| 3312 | } |
| 3313 | \f |
| 3314 | /* ARGSUSED */ |
| 3315 | static void |
| 3316 | pwd_command (args, from_tty) |
| 3317 | char *args; |
| 3318 | int from_tty; |
| 3319 | { |
| 3320 | if (args) error ("The \"pwd\" command does not take an argument: %s", args); |
| 3321 | getcwd (gdb_dirbuf, sizeof (gdb_dirbuf)); |
| 3322 | |
| 3323 | if (!STREQ (gdb_dirbuf, current_directory)) |
| 3324 | printf_unfiltered ("Working directory %s\n (canonically %s).\n", |
| 3325 | current_directory, gdb_dirbuf); |
| 3326 | else |
| 3327 | printf_unfiltered ("Working directory %s.\n", current_directory); |
| 3328 | } |
| 3329 | |
| 3330 | void |
| 3331 | cd_command (dir, from_tty) |
| 3332 | char *dir; |
| 3333 | int from_tty; |
| 3334 | { |
| 3335 | int len; |
| 3336 | /* Found something other than leading repetitions of "/..". */ |
| 3337 | int found_real_path; |
| 3338 | char *p; |
| 3339 | |
| 3340 | /* If the new directory is absolute, repeat is a no-op; if relative, |
| 3341 | repeat might be useful but is more likely to be a mistake. */ |
| 3342 | dont_repeat (); |
| 3343 | |
| 3344 | if (dir == 0) |
| 3345 | error_no_arg ("new working directory"); |
| 3346 | |
| 3347 | dir = tilde_expand (dir); |
| 3348 | make_cleanup (free, dir); |
| 3349 | |
| 3350 | if (chdir (dir) < 0) |
| 3351 | perror_with_name (dir); |
| 3352 | |
| 3353 | len = strlen (dir); |
| 3354 | dir = savestring (dir, len - (len > 1 && SLASH_P(dir[len-1]))); |
| 3355 | if (ROOTED_P(dir)) |
| 3356 | current_directory = dir; |
| 3357 | else |
| 3358 | { |
| 3359 | if (SLASH_P (current_directory[0]) && current_directory[1] == '\0') |
| 3360 | current_directory = concat (current_directory, dir, NULL); |
| 3361 | else |
| 3362 | current_directory = concat (current_directory, SLASH_STRING, dir, NULL); |
| 3363 | free (dir); |
| 3364 | } |
| 3365 | |
| 3366 | /* Now simplify any occurrences of `.' and `..' in the pathname. */ |
| 3367 | |
| 3368 | found_real_path = 0; |
| 3369 | for (p = current_directory; *p;) |
| 3370 | { |
| 3371 | if (SLASH_P (p[0]) && p[1] == '.' && (p[2] == 0 || SLASH_P (p[2]))) |
| 3372 | strcpy (p, p + 2); |
| 3373 | else if (SLASH_P (p[0]) && p[1] == '.' && p[2] == '.' |
| 3374 | && (p[3] == 0 || SLASH_P (p[3]))) |
| 3375 | { |
| 3376 | if (found_real_path) |
| 3377 | { |
| 3378 | /* Search backwards for the directory just before the "/.." |
| 3379 | and obliterate it and the "/..". */ |
| 3380 | char *q = p; |
| 3381 | while (q != current_directory && ! SLASH_P (q[-1])) |
| 3382 | --q; |
| 3383 | |
| 3384 | if (q == current_directory) |
| 3385 | /* current_directory is |
| 3386 | a relative pathname ("can't happen"--leave it alone). */ |
| 3387 | ++p; |
| 3388 | else |
| 3389 | { |
| 3390 | strcpy (q - 1, p + 3); |
| 3391 | p = q - 1; |
| 3392 | } |
| 3393 | } |
| 3394 | else |
| 3395 | /* We are dealing with leading repetitions of "/..", for example |
| 3396 | "/../..", which is the Mach super-root. */ |
| 3397 | p += 3; |
| 3398 | } |
| 3399 | else |
| 3400 | { |
| 3401 | found_real_path = 1; |
| 3402 | ++p; |
| 3403 | } |
| 3404 | } |
| 3405 | |
| 3406 | forget_cached_source_info (); |
| 3407 | |
| 3408 | if (from_tty) |
| 3409 | pwd_command ((char *) 0, 1); |
| 3410 | } |
| 3411 | \f |
| 3412 | struct source_cleanup_lines_args { |
| 3413 | int old_line; |
| 3414 | char *old_file; |
| 3415 | char *old_pre_error; |
| 3416 | char *old_error_pre_print; |
| 3417 | }; |
| 3418 | |
| 3419 | static void |
| 3420 | source_cleanup_lines (args) |
| 3421 | PTR args; |
| 3422 | { |
| 3423 | struct source_cleanup_lines_args *p = |
| 3424 | (struct source_cleanup_lines_args *)args; |
| 3425 | source_line_number = p->old_line; |
| 3426 | source_file_name = p->old_file; |
| 3427 | source_pre_error = p->old_pre_error; |
| 3428 | error_pre_print = p->old_error_pre_print; |
| 3429 | } |
| 3430 | |
| 3431 | /* ARGSUSED */ |
| 3432 | void |
| 3433 | source_command (args, from_tty) |
| 3434 | char *args; |
| 3435 | int from_tty; |
| 3436 | { |
| 3437 | FILE *stream; |
| 3438 | struct cleanup *old_cleanups; |
| 3439 | char *file = args; |
| 3440 | struct source_cleanup_lines_args old_lines; |
| 3441 | int needed_length; |
| 3442 | |
| 3443 | if (file == NULL) |
| 3444 | { |
| 3445 | error ("source command requires pathname of file to source."); |
| 3446 | } |
| 3447 | |
| 3448 | file = tilde_expand (file); |
| 3449 | old_cleanups = make_cleanup (free, file); |
| 3450 | |
| 3451 | stream = fopen (file, FOPEN_RT); |
| 3452 | if (!stream) |
| 3453 | { |
| 3454 | if (from_tty) |
| 3455 | perror_with_name (file); |
| 3456 | else |
| 3457 | return; |
| 3458 | } |
| 3459 | |
| 3460 | make_cleanup ((make_cleanup_func) fclose, stream); |
| 3461 | |
| 3462 | old_lines.old_line = source_line_number; |
| 3463 | old_lines.old_file = source_file_name; |
| 3464 | old_lines.old_pre_error = source_pre_error; |
| 3465 | old_lines.old_error_pre_print = error_pre_print; |
| 3466 | make_cleanup (source_cleanup_lines, &old_lines); |
| 3467 | source_line_number = 0; |
| 3468 | source_file_name = file; |
| 3469 | source_pre_error = error_pre_print == NULL ? "" : error_pre_print; |
| 3470 | source_pre_error = savestring (source_pre_error, strlen (source_pre_error)); |
| 3471 | make_cleanup (free, source_pre_error); |
| 3472 | /* This will get set every time we read a line. So it won't stay "" for |
| 3473 | long. */ |
| 3474 | error_pre_print = ""; |
| 3475 | |
| 3476 | needed_length = strlen (source_file_name) + strlen (source_pre_error) + 80; |
| 3477 | if (source_error_allocated < needed_length) |
| 3478 | { |
| 3479 | source_error_allocated *= 2; |
| 3480 | if (source_error_allocated < needed_length) |
| 3481 | source_error_allocated = needed_length; |
| 3482 | if (source_error == NULL) |
| 3483 | source_error = xmalloc (source_error_allocated); |
| 3484 | else |
| 3485 | source_error = xrealloc (source_error, source_error_allocated); |
| 3486 | } |
| 3487 | |
| 3488 | read_command_file (stream); |
| 3489 | |
| 3490 | do_cleanups (old_cleanups); |
| 3491 | } |
| 3492 | |
| 3493 | /* ARGSUSED */ |
| 3494 | static void |
| 3495 | echo_command (text, from_tty) |
| 3496 | char *text; |
| 3497 | int from_tty; |
| 3498 | { |
| 3499 | char *p = text; |
| 3500 | register int c; |
| 3501 | |
| 3502 | if (text) |
| 3503 | while ((c = *p++) != '\0') |
| 3504 | { |
| 3505 | if (c == '\\') |
| 3506 | { |
| 3507 | /* \ at end of argument is used after spaces |
| 3508 | so they won't be lost. */ |
| 3509 | if (*p == 0) |
| 3510 | return; |
| 3511 | |
| 3512 | c = parse_escape (&p); |
| 3513 | if (c >= 0) |
| 3514 | printf_filtered ("%c", c); |
| 3515 | } |
| 3516 | else |
| 3517 | printf_filtered ("%c", c); |
| 3518 | } |
| 3519 | |
| 3520 | /* Force this output to appear now. */ |
| 3521 | wrap_here (""); |
| 3522 | gdb_flush (gdb_stdout); |
| 3523 | } |
| 3524 | |
| 3525 | /* ARGSUSED */ |
| 3526 | static void |
| 3527 | dont_repeat_command (ignored, from_tty) |
| 3528 | char *ignored; |
| 3529 | int from_tty; |
| 3530 | { |
| 3531 | *line = 0; /* Can't call dont_repeat here because we're not |
| 3532 | necessarily reading from stdin. */ |
| 3533 | } |
| 3534 | \f |
| 3535 | /* Functions to manipulate command line editing control variables. */ |
| 3536 | |
| 3537 | /* Number of commands to print in each call to show_commands. */ |
| 3538 | #define Hist_print 10 |
| 3539 | static void |
| 3540 | show_commands (args, from_tty) |
| 3541 | char *args; |
| 3542 | int from_tty; |
| 3543 | { |
| 3544 | /* Index for history commands. Relative to history_base. */ |
| 3545 | int offset; |
| 3546 | |
| 3547 | /* Number of the history entry which we are planning to display next. |
| 3548 | Relative to history_base. */ |
| 3549 | static int num = 0; |
| 3550 | |
| 3551 | /* The first command in the history which doesn't exist (i.e. one more |
| 3552 | than the number of the last command). Relative to history_base. */ |
| 3553 | int hist_len; |
| 3554 | |
| 3555 | extern HIST_ENTRY *history_get PARAMS ((int)); |
| 3556 | |
| 3557 | /* Print out some of the commands from the command history. */ |
| 3558 | /* First determine the length of the history list. */ |
| 3559 | hist_len = history_size; |
| 3560 | for (offset = 0; offset < history_size; offset++) |
| 3561 | { |
| 3562 | if (!history_get (history_base + offset)) |
| 3563 | { |
| 3564 | hist_len = offset; |
| 3565 | break; |
| 3566 | } |
| 3567 | } |
| 3568 | |
| 3569 | if (args) |
| 3570 | { |
| 3571 | if (args[0] == '+' && args[1] == '\0') |
| 3572 | /* "info editing +" should print from the stored position. */ |
| 3573 | ; |
| 3574 | else |
| 3575 | /* "info editing <exp>" should print around command number <exp>. */ |
| 3576 | num = (parse_and_eval_address (args) - history_base) - Hist_print / 2; |
| 3577 | } |
| 3578 | /* "show commands" means print the last Hist_print commands. */ |
| 3579 | else |
| 3580 | { |
| 3581 | num = hist_len - Hist_print; |
| 3582 | } |
| 3583 | |
| 3584 | if (num < 0) |
| 3585 | num = 0; |
| 3586 | |
| 3587 | /* If there are at least Hist_print commands, we want to display the last |
| 3588 | Hist_print rather than, say, the last 6. */ |
| 3589 | if (hist_len - num < Hist_print) |
| 3590 | { |
| 3591 | num = hist_len - Hist_print; |
| 3592 | if (num < 0) |
| 3593 | num = 0; |
| 3594 | } |
| 3595 | |
| 3596 | for (offset = num; offset < num + Hist_print && offset < hist_len; offset++) |
| 3597 | { |
| 3598 | printf_filtered ("%5d %s\n", history_base + offset, |
| 3599 | (history_get (history_base + offset))->line); |
| 3600 | } |
| 3601 | |
| 3602 | /* The next command we want to display is the next one that we haven't |
| 3603 | displayed yet. */ |
| 3604 | num += Hist_print; |
| 3605 | |
| 3606 | /* If the user repeats this command with return, it should do what |
| 3607 | "show commands +" does. This is unnecessary if arg is null, |
| 3608 | because "show commands +" is not useful after "show commands". */ |
| 3609 | if (from_tty && args) |
| 3610 | { |
| 3611 | args[0] = '+'; |
| 3612 | args[1] = '\0'; |
| 3613 | } |
| 3614 | } |
| 3615 | |
| 3616 | /* Called by do_setshow_command. */ |
| 3617 | /* ARGSUSED */ |
| 3618 | static void |
| 3619 | set_history_size_command (args, from_tty, c) |
| 3620 | char *args; |
| 3621 | int from_tty; |
| 3622 | struct cmd_list_element *c; |
| 3623 | { |
| 3624 | if (history_size == INT_MAX) |
| 3625 | unstifle_history (); |
| 3626 | else if (history_size >= 0) |
| 3627 | stifle_history (history_size); |
| 3628 | else |
| 3629 | { |
| 3630 | history_size = INT_MAX; |
| 3631 | error ("History size must be non-negative"); |
| 3632 | } |
| 3633 | } |
| 3634 | |
| 3635 | /* ARGSUSED */ |
| 3636 | static void |
| 3637 | set_history (args, from_tty) |
| 3638 | char *args; |
| 3639 | int from_tty; |
| 3640 | { |
| 3641 | printf_unfiltered ("\"set history\" must be followed by the name of a history subcommand.\n"); |
| 3642 | help_list (sethistlist, "set history ", -1, gdb_stdout); |
| 3643 | } |
| 3644 | |
| 3645 | /* ARGSUSED */ |
| 3646 | static void |
| 3647 | show_history (args, from_tty) |
| 3648 | char *args; |
| 3649 | int from_tty; |
| 3650 | { |
| 3651 | cmd_show_list (showhistlist, from_tty, ""); |
| 3652 | } |
| 3653 | |
| 3654 | int info_verbose = 0; /* Default verbose msgs off */ |
| 3655 | |
| 3656 | /* Called by do_setshow_command. An elaborate joke. */ |
| 3657 | /* ARGSUSED */ |
| 3658 | static void |
| 3659 | set_verbose (args, from_tty, c) |
| 3660 | char *args; |
| 3661 | int from_tty; |
| 3662 | struct cmd_list_element *c; |
| 3663 | { |
| 3664 | char *cmdname = "verbose"; |
| 3665 | struct cmd_list_element *showcmd; |
| 3666 | |
| 3667 | showcmd = lookup_cmd_1 (&cmdname, showlist, NULL, 1); |
| 3668 | |
| 3669 | if (info_verbose) |
| 3670 | { |
| 3671 | c->doc = "Set verbose printing of informational messages."; |
| 3672 | showcmd->doc = "Show verbose printing of informational messages."; |
| 3673 | } |
| 3674 | else |
| 3675 | { |
| 3676 | c->doc = "Set verbosity."; |
| 3677 | showcmd->doc = "Show verbosity."; |
| 3678 | } |
| 3679 | } |
| 3680 | |
| 3681 | static void |
| 3682 | float_handler (signo) |
| 3683 | int signo; |
| 3684 | { |
| 3685 | /* This message is based on ANSI C, section 4.7. Note that integer |
| 3686 | divide by zero causes this, so "float" is a misnomer. */ |
| 3687 | signal (SIGFPE, float_handler); |
| 3688 | error ("Erroneous arithmetic operation."); |
| 3689 | } |
| 3690 | |
| 3691 | \f |
| 3692 | static void |
| 3693 | init_cmd_lists () |
| 3694 | { |
| 3695 | cmdlist = NULL; |
| 3696 | infolist = NULL; |
| 3697 | enablelist = NULL; |
| 3698 | disablelist = NULL; |
| 3699 | togglelist = NULL; |
| 3700 | stoplist = NULL; |
| 3701 | deletelist = NULL; |
| 3702 | enablebreaklist = NULL; |
| 3703 | setlist = NULL; |
| 3704 | unsetlist = NULL; |
| 3705 | showlist = NULL; |
| 3706 | sethistlist = NULL; |
| 3707 | showhistlist = NULL; |
| 3708 | unsethistlist = NULL; |
| 3709 | maintenancelist = NULL; |
| 3710 | maintenanceinfolist = NULL; |
| 3711 | maintenanceprintlist = NULL; |
| 3712 | setprintlist = NULL; |
| 3713 | showprintlist = NULL; |
| 3714 | setchecklist = NULL; |
| 3715 | showchecklist = NULL; |
| 3716 | } |
| 3717 | |
| 3718 | /* Init the history buffer. Note that we are called after the init file(s) |
| 3719 | * have been read so that the user can change the history file via his |
| 3720 | * .gdbinit file (for instance). The GDBHISTFILE environment variable |
| 3721 | * overrides all of this. |
| 3722 | */ |
| 3723 | |
| 3724 | void |
| 3725 | init_history() |
| 3726 | { |
| 3727 | char *tmpenv; |
| 3728 | |
| 3729 | tmpenv = getenv ("HISTSIZE"); |
| 3730 | if (tmpenv) |
| 3731 | history_size = atoi (tmpenv); |
| 3732 | else if (!history_size) |
| 3733 | history_size = 256; |
| 3734 | |
| 3735 | stifle_history (history_size); |
| 3736 | |
| 3737 | tmpenv = getenv ("GDBHISTFILE"); |
| 3738 | if (tmpenv) |
| 3739 | history_filename = savestring (tmpenv, strlen(tmpenv)); |
| 3740 | else if (!history_filename) { |
| 3741 | /* We include the current directory so that if the user changes |
| 3742 | directories the file written will be the same as the one |
| 3743 | that was read. */ |
| 3744 | history_filename = concat (current_directory, "/.gdb_history", NULL); |
| 3745 | } |
| 3746 | read_history (history_filename); |
| 3747 | } |
| 3748 | |
| 3749 | static void |
| 3750 | init_main () |
| 3751 | { |
| 3752 | struct cmd_list_element *c; |
| 3753 | |
| 3754 | /* If we are running the asynchronous version, |
| 3755 | we initialize the prompts differently. */ |
| 3756 | if (!async_p) |
| 3757 | { |
| 3758 | #ifdef DEFAULT_PROMPT |
| 3759 | gdb_prompt_string = savestring (DEFAULT_PROMPT, strlen(DEFAULT_PROMPT)); |
| 3760 | #else |
| 3761 | gdb_prompt_string = savestring ("(gdb) ", 6); |
| 3762 | #endif |
| 3763 | } |
| 3764 | else |
| 3765 | { |
| 3766 | /* initialize the prompt stack to a simple "(gdb) " prompt or to |
| 3767 | whatever the DEFULAT_PROMPT is. */ |
| 3768 | the_prompts.top = 0; |
| 3769 | PREFIX(0) = ""; |
| 3770 | #ifdef DEFAULT_PROMPT |
| 3771 | PROMPT(0) = savestring (DEFAULT_PROMPT, strlen(DEFAULT_PROMPT)); |
| 3772 | #else |
| 3773 | PROMPT(0) = savestring ("(gdb) ", 6); |
| 3774 | #endif |
| 3775 | SUFFIX(0) = ""; |
| 3776 | /* Set things up for annotation_level > 1, if the user ever decides |
| 3777 | to use it. */ |
| 3778 | async_annotation_suffix = "prompt"; |
| 3779 | /* Set the variable associated with the setshow prompt command. */ |
| 3780 | new_async_prompt = savestring (PROMPT (0), strlen (PROMPT (0))); |
| 3781 | } |
| 3782 | gdb_prompt_escape = 0; /* default to none. */ |
| 3783 | |
| 3784 | /* Set the important stuff up for command editing. */ |
| 3785 | command_editing_p = 1; |
| 3786 | history_expansion_p = 0; |
| 3787 | write_history_p = 0; |
| 3788 | |
| 3789 | /* Setup important stuff for command line editing. */ |
| 3790 | rl_completion_entry_function = (int (*)()) readline_line_completion_function; |
| 3791 | rl_completer_word_break_characters = gdb_completer_word_break_characters; |
| 3792 | rl_completer_quote_characters = gdb_completer_quote_characters; |
| 3793 | rl_readline_name = "gdb"; |
| 3794 | |
| 3795 | /* Define the classes of commands. |
| 3796 | They will appear in the help list in the reverse of this order. */ |
| 3797 | |
| 3798 | add_cmd ("internals", class_maintenance, NO_FUNCTION, |
| 3799 | "Maintenance commands.\n\ |
| 3800 | Some gdb commands are provided just for use by gdb maintainers.\n\ |
| 3801 | These commands are subject to frequent change, and may not be as\n\ |
| 3802 | well documented as user commands.", |
| 3803 | &cmdlist); |
| 3804 | add_cmd ("obscure", class_obscure, NO_FUNCTION, "Obscure features.", &cmdlist); |
| 3805 | add_cmd ("aliases", class_alias, NO_FUNCTION, "Aliases of other commands.", &cmdlist); |
| 3806 | add_cmd ("user-defined", class_user, NO_FUNCTION, "User-defined commands.\n\ |
| 3807 | The commands in this class are those defined by the user.\n\ |
| 3808 | Use the \"define\" command to define a command.", &cmdlist); |
| 3809 | add_cmd ("support", class_support, NO_FUNCTION, "Support facilities.", &cmdlist); |
| 3810 | if (!dbx_commands) |
| 3811 | add_cmd ("status", class_info, NO_FUNCTION, "Status inquiries.", &cmdlist); |
| 3812 | add_cmd ("files", class_files, NO_FUNCTION, "Specifying and examining files.", &cmdlist); |
| 3813 | add_cmd ("breakpoints", class_breakpoint, NO_FUNCTION, "Making program stop at certain points.", &cmdlist); |
| 3814 | add_cmd ("data", class_vars, NO_FUNCTION, "Examining data.", &cmdlist); |
| 3815 | add_cmd ("stack", class_stack, NO_FUNCTION, "Examining the stack.\n\ |
| 3816 | The stack is made up of stack frames. Gdb assigns numbers to stack frames\n\ |
| 3817 | counting from zero for the innermost (currently executing) frame.\n\n\ |
| 3818 | At any time gdb identifies one frame as the \"selected\" frame.\n\ |
| 3819 | Variable lookups are done with respect to the selected frame.\n\ |
| 3820 | When the program being debugged stops, gdb selects the innermost frame.\n\ |
| 3821 | The commands below can be used to select other frames by number or address.", |
| 3822 | &cmdlist); |
| 3823 | add_cmd ("running", class_run, NO_FUNCTION, "Running the program.", &cmdlist); |
| 3824 | |
| 3825 | add_com ("pwd", class_files, pwd_command, |
| 3826 | "Print working directory. This is used for your program as well."); |
| 3827 | c = add_cmd ("cd", class_files, cd_command, |
| 3828 | "Set working directory to DIR for debugger and program being debugged.\n\ |
| 3829 | The change does not take effect for the program being debugged\n\ |
| 3830 | until the next time it is started.", &cmdlist); |
| 3831 | c->completer = filename_completer; |
| 3832 | |
| 3833 | /* The set prompt command is different depending whether or not the |
| 3834 | async version is run. NOTE: this difference is going to |
| 3835 | disappear as we make the event loop be the default engine of |
| 3836 | gdb. */ |
| 3837 | if (!async_p) |
| 3838 | { |
| 3839 | add_show_from_set |
| 3840 | (add_set_cmd ("prompt", class_support, var_string, |
| 3841 | (char *) &gdb_prompt_string, "Set gdb's prompt", |
| 3842 | &setlist), |
| 3843 | &showlist); |
| 3844 | } |
| 3845 | else |
| 3846 | { |
| 3847 | c = add_set_cmd ("prompt", class_support, var_string, |
| 3848 | (char *)&new_async_prompt, "Set gdb's prompt", |
| 3849 | &setlist); |
| 3850 | add_show_from_set (c, &showlist); |
| 3851 | c->function.sfunc = set_async_prompt; |
| 3852 | } |
| 3853 | |
| 3854 | add_show_from_set |
| 3855 | (add_set_cmd ("prompt-escape-char", class_support, var_zinteger, |
| 3856 | (char *) &gdb_prompt_escape, |
| 3857 | "Set escape character for formatting of gdb's prompt", |
| 3858 | &setlist), |
| 3859 | &showlist); |
| 3860 | |
| 3861 | add_com ("echo", class_support, echo_command, |
| 3862 | "Print a constant string. Give string as argument.\n\ |
| 3863 | C escape sequences may be used in the argument.\n\ |
| 3864 | No newline is added at the end of the argument;\n\ |
| 3865 | use \"\\n\" if you want a newline to be printed.\n\ |
| 3866 | Since leading and trailing whitespace are ignored in command arguments,\n\ |
| 3867 | if you want to print some you must use \"\\\" before leading whitespace\n\ |
| 3868 | to be printed or after trailing whitespace."); |
| 3869 | add_com ("document", class_support, document_command, |
| 3870 | "Document a user-defined command.\n\ |
| 3871 | Give command name as argument. Give documentation on following lines.\n\ |
| 3872 | End with a line of just \"end\"."); |
| 3873 | add_com ("define", class_support, define_command, |
| 3874 | "Define a new command name. Command name is argument.\n\ |
| 3875 | Definition appears on following lines, one command per line.\n\ |
| 3876 | End with a line of just \"end\".\n\ |
| 3877 | Use the \"document\" command to give documentation for the new command.\n\ |
| 3878 | Commands defined in this way may have up to ten arguments."); |
| 3879 | |
| 3880 | #ifdef __STDC__ |
| 3881 | c = add_cmd ("source", class_support, source_command, |
| 3882 | "Read commands from a file named FILE.\n\ |
| 3883 | Note that the file \"" GDBINIT_FILENAME "\" is read automatically in this way\n\ |
| 3884 | when gdb is started.", &cmdlist); |
| 3885 | #else |
| 3886 | /* Punt file name, we can't help it easily. */ |
| 3887 | c = add_cmd ("source", class_support, source_command, |
| 3888 | "Read commands from a file named FILE.\n\ |
| 3889 | Note that the file \".gdbinit\" is read automatically in this way\n\ |
| 3890 | when gdb is started.", &cmdlist); |
| 3891 | #endif |
| 3892 | c->completer = filename_completer; |
| 3893 | |
| 3894 | add_com ("quit", class_support, quit_command, "Exit gdb."); |
| 3895 | add_com ("help", class_support, help_command, "Print list of commands."); |
| 3896 | add_com_alias ("q", "quit", class_support, 1); |
| 3897 | add_com_alias ("h", "help", class_support, 1); |
| 3898 | |
| 3899 | add_com ("dont-repeat", class_support, dont_repeat_command, "Don't repeat this command.\n\ |
| 3900 | Primarily used inside of user-defined commands that should not be repeated when\n\ |
| 3901 | hitting return."); |
| 3902 | |
| 3903 | c = add_set_cmd ("verbose", class_support, var_boolean, (char *)&info_verbose, |
| 3904 | "Set ", |
| 3905 | &setlist), |
| 3906 | add_show_from_set (c, &showlist); |
| 3907 | c->function.sfunc = set_verbose; |
| 3908 | set_verbose (NULL, 0, c); |
| 3909 | |
| 3910 | /* The set editing command is different depending whether or not the |
| 3911 | async version is run. NOTE: this difference is going to disappear |
| 3912 | as we make the event loop be the default engine of gdb. */ |
| 3913 | if (!async_p) |
| 3914 | { |
| 3915 | add_show_from_set |
| 3916 | (add_set_cmd ("editing", class_support, var_boolean, (char *)&command_editing_p, |
| 3917 | "Set editing of command lines as they are typed.\n\ |
| 3918 | Use \"on\" to enable the editing, and \"off\" to disable it.\n\ |
| 3919 | Without an argument, command line editing is enabled. To edit, use\n\ |
| 3920 | EMACS-like or VI-like commands like control-P or ESC.", &setlist), |
| 3921 | &showlist); |
| 3922 | } |
| 3923 | else |
| 3924 | { |
| 3925 | c = add_set_cmd ("editing", class_support, var_boolean, (char *)&async_command_editing_p, |
| 3926 | "Set editing of command lines as they are typed.\n\ |
| 3927 | Use \"on\" to enable the editing, and \"off\" to disable it.\n\ |
| 3928 | Without an argument, command line editing is enabled. To edit, use\n\ |
| 3929 | EMACS-like or VI-like commands like control-P or ESC.", &setlist); |
| 3930 | |
| 3931 | add_show_from_set (c, &showlist); |
| 3932 | c->function.sfunc = set_async_editing_command; |
| 3933 | } |
| 3934 | |
| 3935 | add_prefix_cmd ("history", class_support, set_history, |
| 3936 | "Generic command for setting command history parameters.", |
| 3937 | &sethistlist, "set history ", 0, &setlist); |
| 3938 | add_prefix_cmd ("history", class_support, show_history, |
| 3939 | "Generic command for showing command history parameters.", |
| 3940 | &showhistlist, "show history ", 0, &showlist); |
| 3941 | |
| 3942 | add_show_from_set |
| 3943 | (add_set_cmd ("expansion", no_class, var_boolean, (char *)&history_expansion_p, |
| 3944 | "Set history expansion on command input.\n\ |
| 3945 | Without an argument, history expansion is enabled.", &sethistlist), |
| 3946 | &showhistlist); |
| 3947 | |
| 3948 | add_show_from_set |
| 3949 | (add_set_cmd ("save", no_class, var_boolean, (char *)&write_history_p, |
| 3950 | "Set saving of the history record on exit.\n\ |
| 3951 | Use \"on\" to enable the saving, and \"off\" to disable it.\n\ |
| 3952 | Without an argument, saving is enabled.", &sethistlist), |
| 3953 | &showhistlist); |
| 3954 | |
| 3955 | c = add_set_cmd ("size", no_class, var_integer, (char *)&history_size, |
| 3956 | "Set the size of the command history, \n\ |
| 3957 | ie. the number of previous commands to keep a record of.", &sethistlist); |
| 3958 | add_show_from_set (c, &showhistlist); |
| 3959 | c->function.sfunc = set_history_size_command; |
| 3960 | |
| 3961 | add_show_from_set |
| 3962 | (add_set_cmd ("filename", no_class, var_filename, (char *)&history_filename, |
| 3963 | "Set the filename in which to record the command history\n\ |
| 3964 | (the list of previous commands of which a record is kept).", &sethistlist), |
| 3965 | &showhistlist); |
| 3966 | |
| 3967 | add_show_from_set |
| 3968 | (add_set_cmd ("confirm", class_support, var_boolean, |
| 3969 | (char *)&caution, |
| 3970 | "Set whether to confirm potentially dangerous operations.", |
| 3971 | &setlist), |
| 3972 | &showlist); |
| 3973 | |
| 3974 | add_prefix_cmd ("info", class_info, info_command, |
| 3975 | "Generic command for showing things about the program being debugged.", |
| 3976 | &infolist, "info ", 0, &cmdlist); |
| 3977 | add_com_alias ("i", "info", class_info, 1); |
| 3978 | |
| 3979 | add_com ("complete", class_obscure, complete_command, |
| 3980 | "List the completions for the rest of the line as a command."); |
| 3981 | |
| 3982 | add_prefix_cmd ("show", class_info, show_command, |
| 3983 | "Generic command for showing things about the debugger.", |
| 3984 | &showlist, "show ", 0, &cmdlist); |
| 3985 | /* Another way to get at the same thing. */ |
| 3986 | add_info ("set", show_command, "Show all GDB settings."); |
| 3987 | |
| 3988 | add_cmd ("commands", no_class, show_commands, |
| 3989 | "Show the history of commands you typed.\n\ |
| 3990 | You can supply a command number to start with, or a `+' to start after\n\ |
| 3991 | the previous command number shown.", |
| 3992 | &showlist); |
| 3993 | |
| 3994 | add_cmd ("version", no_class, show_version, |
| 3995 | "Show what version of GDB this is.", &showlist); |
| 3996 | |
| 3997 | add_com ("while", class_support, while_command, |
| 3998 | "Execute nested commands WHILE the conditional expression is non zero.\n\ |
| 3999 | The conditional expression must follow the word `while' and must in turn be\n\ |
| 4000 | followed by a new line. The nested commands must be entered one per line,\n\ |
| 4001 | and should be terminated by the word `end'."); |
| 4002 | |
| 4003 | add_com ("if", class_support, if_command, |
| 4004 | "Execute nested commands once IF the conditional expression is non zero.\n\ |
| 4005 | The conditional expression must follow the word `if' and must in turn be\n\ |
| 4006 | followed by a new line. The nested commands must be entered one per line,\n\ |
| 4007 | and should be terminated by the word 'else' or `end'. If an else clause\n\ |
| 4008 | is used, the same rules apply to its nested commands as to the first ones."); |
| 4009 | |
| 4010 | /* If target is open when baud changes, it doesn't take effect until the |
| 4011 | next open (I think, not sure). */ |
| 4012 | add_show_from_set (add_set_cmd ("remotebaud", no_class, |
| 4013 | var_zinteger, (char *)&baud_rate, |
| 4014 | "Set baud rate for remote serial I/O.\n\ |
| 4015 | This value is used to set the speed of the serial port when debugging\n\ |
| 4016 | using remote targets.", &setlist), |
| 4017 | &showlist); |
| 4018 | |
| 4019 | add_show_from_set ( |
| 4020 | add_set_cmd ("remotedebug", no_class, var_zinteger, (char *)&remote_debug, |
| 4021 | "Set debugging of remote protocol.\n\ |
| 4022 | When enabled, each packet sent or received with the remote target\n\ |
| 4023 | is displayed.", &setlist), |
| 4024 | &showlist); |
| 4025 | |
| 4026 | add_show_from_set ( |
| 4027 | add_set_cmd ("remotetimeout", no_class, var_integer, (char *)&remote_timeout, |
| 4028 | "Set timeout limit to wait for target to respond.\n\ |
| 4029 | This value is used to set the time limit for gdb to wait for a response\n\ |
| 4030 | from the target.", &setlist), |
| 4031 | &showlist); |
| 4032 | |
| 4033 | /* The set annotate command is different depending whether or not |
| 4034 | the async version is run. NOTE: this difference is going to |
| 4035 | disappear as we make the event loop be the default engine of |
| 4036 | gdb. */ |
| 4037 | if (!async_p) |
| 4038 | { |
| 4039 | c = add_set_cmd ("annotate", class_obscure, var_zinteger, |
| 4040 | (char *)&annotation_level, "Set annotation_level.\n\ |
| 4041 | 0 == normal; 1 == fullname (for use when running under emacs)\n\ |
| 4042 | 2 == output annotated suitably for use by programs that control GDB.", |
| 4043 | &setlist); |
| 4044 | c = add_show_from_set (c, &showlist); |
| 4045 | } |
| 4046 | else |
| 4047 | { |
| 4048 | c = add_set_cmd ("annotate", class_obscure, var_zinteger, |
| 4049 | (char *)&annotation_level, "Set annotation_level.\n\ |
| 4050 | 0 == normal; 1 == fullname (for use when running under emacs)\n\ |
| 4051 | 2 == output annotated suitably for use by programs that control GDB.", |
| 4052 | &setlist); |
| 4053 | add_show_from_set (c, &showlist); |
| 4054 | c->function.sfunc = set_async_annotation_level; |
| 4055 | } |
| 4056 | } |