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