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