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