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