]>
Commit | Line | Data |
---|---|---|
7d9884b9 | 1 | /* Top level `main' program for GDB, the GNU debugger. |
51b57ded FF |
2 | Copyright 1986, 1987, 1988, 1989, 1990, 1991, 1992 |
3 | Free Software Foundation, Inc. | |
bd5635a1 RP |
4 | |
5 | This file is part of GDB. | |
6 | ||
e522fb52 | 7 | This program is free software; you can redistribute it and/or modify |
bd5635a1 | 8 | it under the terms of the GNU General Public License as published by |
e522fb52 JG |
9 | the Free Software Foundation; either version 2 of the License, or |
10 | (at your option) any later version. | |
bd5635a1 | 11 | |
e522fb52 | 12 | This program is distributed in the hope that it will be useful, |
bd5635a1 RP |
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 | |
e522fb52 JG |
18 | along with this program; if not, write to the Free Software |
19 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ | |
bd5635a1 | 20 | |
bd5635a1 RP |
21 | #include "defs.h" |
22 | #include "gdbcmd.h" | |
51b57ded | 23 | #include "call-cmds.h" |
bd5635a1 RP |
24 | #include "symtab.h" |
25 | #include "inferior.h" | |
26 | #include "signals.h" | |
27 | #include "target.h" | |
28 | #include "breakpoint.h" | |
30875e1c SG |
29 | #include "gdbtypes.h" |
30 | #include "expression.h" | |
bd099407 | 31 | #include "language.h" |
c307bb11 | 32 | #include "serial.h" /* For job_control. */ |
bd5635a1 | 33 | |
e522fb52 JG |
34 | #include "getopt.h" |
35 | ||
36 | /* readline include files */ | |
37 | #include "readline.h" | |
38 | #include "history.h" | |
bd5635a1 RP |
39 | |
40 | /* readline defines this. */ | |
41 | #undef savestring | |
42 | ||
43 | #ifdef USG | |
44 | #include <sys/types.h> | |
45 | #include <unistd.h> | |
46 | #endif | |
47 | ||
48 | #include <string.h> | |
ee0613d1 | 49 | #ifndef NO_SYS_FILE |
bd5635a1 | 50 | #include <sys/file.h> |
ee0613d1 | 51 | #endif |
bd5635a1 RP |
52 | #include <setjmp.h> |
53 | #include <sys/param.h> | |
54 | #include <sys/stat.h> | |
e522fb52 | 55 | #include <ctype.h> |
bd5635a1 RP |
56 | |
57 | #ifdef SET_STACK_LIMIT_HUGE | |
58 | #include <sys/time.h> | |
59 | #include <sys/resource.h> | |
bd5635a1 RP |
60 | |
61 | int original_stack_limit; | |
62 | #endif | |
63 | ||
30875e1c SG |
64 | /* Prototypes for local functions */ |
65 | ||
66 | static char * | |
67 | symbol_completion_function PARAMS ((char *, int)); | |
68 | ||
69 | static void | |
70 | command_loop PARAMS ((void)); | |
71 | ||
72 | static void | |
73 | command_loop_marker PARAMS ((int)); | |
74 | ||
75 | static void | |
fb29d681 | 76 | print_gdb_version PARAMS ((FILE *)); |
30875e1c SG |
77 | |
78 | static void | |
79 | quit_command PARAMS ((char *, int)); | |
80 | ||
81 | static void | |
fc61e9ee | 82 | init_main PARAMS ((void)); |
30875e1c SG |
83 | |
84 | static void | |
fc61e9ee | 85 | init_history PARAMS ((void)); |
30875e1c SG |
86 | |
87 | static void | |
fc61e9ee | 88 | init_cmd_lists PARAMS ((void)); |
30875e1c SG |
89 | |
90 | static void | |
91 | float_handler PARAMS ((int)); | |
92 | ||
93 | static void | |
94 | source_command PARAMS ((char *, int)); | |
95 | ||
c307bb11 | 96 | static void cd_command PARAMS ((char *, int)); |
30875e1c SG |
97 | |
98 | static void | |
99 | print_gnu_advertisement PARAMS ((void)); | |
100 | ||
101 | static void | |
102 | init_signals PARAMS ((void)); | |
103 | ||
104 | static void | |
105 | read_command_file PARAMS ((FILE *)); | |
106 | ||
107 | static void | |
108 | set_verbose PARAMS ((char *, int, struct cmd_list_element *)); | |
109 | ||
110 | static void | |
111 | show_history PARAMS ((char *, int)); | |
112 | ||
113 | static void | |
114 | set_history PARAMS ((char *, int)); | |
115 | ||
116 | static void | |
117 | set_history_size_command PARAMS ((char *, int, struct cmd_list_element *)); | |
118 | ||
119 | static void | |
120 | show_commands PARAMS ((char *, int)); | |
121 | ||
30875e1c SG |
122 | static void |
123 | echo_command PARAMS ((char *, int)); | |
124 | ||
125 | static void | |
126 | pwd_command PARAMS ((char *, int)); | |
127 | ||
128 | static void | |
129 | show_version PARAMS ((char *, int)); | |
130 | ||
131 | static void | |
132 | document_command PARAMS ((char *, int)); | |
133 | ||
134 | static void | |
135 | define_command PARAMS ((char *, int)); | |
136 | ||
137 | static void | |
138 | validate_comname PARAMS ((char *)); | |
139 | ||
140 | static void | |
141 | help_command PARAMS ((char *, int)); | |
142 | ||
143 | static void | |
144 | show_command PARAMS ((char *, int)); | |
145 | ||
146 | static void | |
147 | info_command PARAMS ((char *, int)); | |
148 | ||
149 | static void | |
150 | do_nothing PARAMS ((int)); | |
151 | ||
597dc86b SG |
152 | static int |
153 | quit_cover PARAMS ((char *)); | |
154 | ||
30875e1c SG |
155 | static void |
156 | disconnect PARAMS ((int)); | |
157 | ||
158 | static void | |
159 | source_cleanup PARAMS ((FILE *)); | |
bd5635a1 RP |
160 | |
161 | /* If this definition isn't overridden by the header files, assume | |
162 | that isatty and fileno exist on this system. */ | |
163 | #ifndef ISATTY | |
164 | #define ISATTY(FP) (isatty (fileno (FP))) | |
165 | #endif | |
166 | ||
167 | /* Initialization file name for gdb. This is overridden in some configs. */ | |
168 | ||
169 | #ifndef GDBINIT_FILENAME | |
170 | #define GDBINIT_FILENAME ".gdbinit" | |
171 | #endif | |
6fe90fc8 JK |
172 | static char gdbinit[] = GDBINIT_FILENAME; |
173 | static int inhibit_gdbinit = 0; | |
bd5635a1 | 174 | |
f266e564 JK |
175 | #define ALL_CLEANUPS ((struct cleanup *)0) |
176 | ||
bd5635a1 RP |
177 | /* Version number of GDB, as a string. */ |
178 | ||
179 | extern char *version; | |
180 | ||
57cb418a RP |
181 | /* Canonical host name as a string. */ |
182 | ||
183 | extern char *host_canonical; | |
184 | ||
185 | /* Canonical target name as a string. */ | |
186 | ||
187 | extern char *target_canonical; | |
188 | ||
81066208 JG |
189 | /* Message to be printed before the error message, when an error occurs. */ |
190 | ||
191 | extern char *error_pre_print; | |
192 | ||
318bf84f FF |
193 | /* Message to be printed before the warning message, when a warning occurs. */ |
194 | ||
195 | extern char *warning_pre_print; | |
196 | ||
bd099407 JG |
197 | extern char lang_frame_mismatch_warn[]; /* language.c */ |
198 | ||
bd5635a1 RP |
199 | /* Flag for whether we want all the "from_tty" gubbish printed. */ |
200 | ||
201 | int caution = 1; /* Default is yes, sigh. */ | |
202 | ||
203 | /* | |
204 | * Define all cmd_list_element's | |
205 | */ | |
206 | ||
207 | /* Chain containing all defined commands. */ | |
208 | ||
209 | struct cmd_list_element *cmdlist; | |
210 | ||
211 | /* Chain containing all defined info subcommands. */ | |
212 | ||
213 | struct cmd_list_element *infolist; | |
214 | ||
215 | /* Chain containing all defined enable subcommands. */ | |
216 | ||
217 | struct cmd_list_element *enablelist; | |
218 | ||
219 | /* Chain containing all defined disable subcommands. */ | |
220 | ||
221 | struct cmd_list_element *disablelist; | |
222 | ||
223 | /* Chain containing all defined delete subcommands. */ | |
224 | ||
225 | struct cmd_list_element *deletelist; | |
226 | ||
227 | /* Chain containing all defined "enable breakpoint" subcommands. */ | |
228 | ||
229 | struct cmd_list_element *enablebreaklist; | |
230 | ||
231 | /* Chain containing all defined set subcommands */ | |
232 | ||
233 | struct cmd_list_element *setlist; | |
234 | ||
0239d9b3 FF |
235 | /* Chain containing all defined unset subcommands */ |
236 | ||
237 | struct cmd_list_element *unsetlist; | |
238 | ||
bd5635a1 | 239 | /* Chain containing all defined show subcommands. */ |
0239d9b3 | 240 | |
bd5635a1 RP |
241 | struct cmd_list_element *showlist; |
242 | ||
243 | /* Chain containing all defined \"set history\". */ | |
244 | ||
245 | struct cmd_list_element *sethistlist; | |
246 | ||
247 | /* Chain containing all defined \"show history\". */ | |
0239d9b3 | 248 | |
bd5635a1 RP |
249 | struct cmd_list_element *showhistlist; |
250 | ||
251 | /* Chain containing all defined \"unset history\". */ | |
252 | ||
253 | struct cmd_list_element *unsethistlist; | |
254 | ||
0239d9b3 FF |
255 | /* Chain containing all defined maintenance subcommands. */ |
256 | ||
257 | #if MAINTENANCE_CMDS | |
258 | struct cmd_list_element *maintenancelist; | |
259 | #endif | |
260 | ||
261 | /* Chain containing all defined "maintenance info" subcommands. */ | |
262 | ||
263 | #if MAINTENANCE_CMDS | |
264 | struct cmd_list_element *maintenanceinfolist; | |
265 | #endif | |
266 | ||
a8e033f2 SG |
267 | /* Chain containing all defined "maintenance print" subcommands. */ |
268 | ||
269 | #if MAINTENANCE_CMDS | |
270 | struct cmd_list_element *maintenanceprintlist; | |
271 | #endif | |
272 | ||
0239d9b3 FF |
273 | struct cmd_list_element *setprintlist; |
274 | ||
275 | struct cmd_list_element *showprintlist; | |
276 | ||
277 | struct cmd_list_element *setchecklist; | |
278 | ||
279 | struct cmd_list_element *showchecklist; | |
280 | ||
bd5635a1 RP |
281 | /* stdio stream that command input is being read from. */ |
282 | ||
283 | FILE *instream; | |
284 | ||
285 | /* Current working directory. */ | |
286 | ||
287 | char *current_directory; | |
288 | ||
289 | /* The directory name is actually stored here (usually). */ | |
f7402c04 | 290 | static char dirbuf[1024]; |
bd5635a1 RP |
291 | |
292 | /* Function to call before reading a command, if nonzero. | |
293 | The function receives two args: an input stream, | |
294 | and a prompt string. */ | |
bd099407 | 295 | |
30875e1c | 296 | void (*window_hook) PARAMS ((FILE *, char *)); |
bd5635a1 | 297 | |
318bf84f FF |
298 | extern int mapped_symbol_files; |
299 | extern int readnow_symbol_files; | |
300 | ||
bd5635a1 RP |
301 | int epoch_interface; |
302 | int xgdb_verbose; | |
303 | ||
bd5635a1 RP |
304 | /* gdb prints this when reading a command interactively */ |
305 | static char *prompt; | |
306 | ||
307 | /* Buffer used for reading command lines, and the size | |
308 | allocated for it so far. */ | |
309 | ||
310 | char *line; | |
311 | int linesize = 100; | |
312 | ||
313 | /* Baud rate specified for talking to serial target systems. Default | |
314 | is left as a zero pointer, so targets can choose their own defaults. */ | |
315 | ||
316 | char *baud_rate; | |
317 | ||
318 | /* Signal to catch ^Z typed while reading a command: SIGTSTP or SIGCONT. */ | |
319 | ||
320 | #ifndef STOP_SIGNAL | |
321 | #ifdef SIGTSTP | |
322 | #define STOP_SIGNAL SIGTSTP | |
30875e1c | 323 | static void stop_sig PARAMS ((int)); |
bd5635a1 RP |
324 | #endif |
325 | #endif | |
d566d62a JK |
326 | |
327 | /* Some System V have job control but not sigsetmask(). */ | |
328 | #if !defined (HAVE_SIGSETMASK) | |
329 | #define HAVE_SIGSETMASK !defined (USG) | |
330 | #endif | |
331 | ||
8b3c897a | 332 | #if 0 == (HAVE_SIGSETMASK) |
d566d62a JK |
333 | #define sigsetmask(n) |
334 | #endif | |
bd5635a1 | 335 | \f |
9748446f JK |
336 | /* Where to go for return_to_top_level (RETURN_ERROR). */ |
337 | static jmp_buf error_return; | |
338 | /* Where to go for return_to_top_level (RETURN_QUIT). */ | |
339 | static jmp_buf quit_return; | |
bd5635a1 | 340 | |
9748446f JK |
341 | /* Temporary variable for SET_TOP_LEVEL. */ |
342 | static int top_level_val; | |
343 | ||
344 | /* Do a setjmp on error_return and quit_return. catch_errors is | |
345 | generally a cleaner way to do this, but main() would look pretty | |
346 | ugly if it had to use catch_errors each time. */ | |
347 | ||
348 | #define SET_TOP_LEVEL() \ | |
349 | (((top_level_val = setjmp (error_return)) \ | |
350 | ? (PTR) 0 : (PTR) memcpy (quit_return, error_return, sizeof (jmp_buf))) \ | |
351 | , top_level_val) | |
352 | ||
353 | /* Return for reason REASON. This generally gets back to the command | |
354 | loop, but can be caught via catch_errors. */ | |
bd5635a1 | 355 | |
30875e1c | 356 | NORETURN void |
9748446f JK |
357 | return_to_top_level (reason) |
358 | enum return_reason reason; | |
bd5635a1 RP |
359 | { |
360 | quit_flag = 0; | |
361 | immediate_quit = 0; | |
c307bb11 JK |
362 | |
363 | /* Perhaps it would be cleaner to do this via the cleanup chain (not sure | |
364 | I can think of a reason why that is vital, though). */ | |
bd5635a1 | 365 | bpstat_clear_actions(stop_bpstat); /* Clear queued breakpoint commands */ |
c307bb11 | 366 | |
bd5635a1 | 367 | disable_current_display (); |
f266e564 | 368 | do_cleanups (ALL_CLEANUPS); |
9748446f JK |
369 | (NORETURN void) longjmp |
370 | (reason == RETURN_ERROR ? error_return : quit_return, 1); | |
bd5635a1 RP |
371 | } |
372 | ||
9748446f JK |
373 | /* Call FUNC with arg ARGS, catching any errors. If there is no |
374 | error, return the value returned by FUNC. If there is an error, | |
375 | print ERRSTRING, print the specific error message, then return | |
376 | zero. | |
377 | ||
378 | MASK specifies what to catch; it is normally set to | |
379 | RETURN_MASK_ALL, if for no other reason than that the code which | |
380 | calls catch_errors might not be set up to deal with a quit which | |
381 | isn't caught. But if the code can deal with it, it generally | |
382 | should be RETURN_MASK_ERROR, unless for some reason it is more | |
383 | useful to abort only the portion of the operation inside the | |
384 | catch_errors. Note that quit should return to the command line | |
385 | fairly quickly, even if some further processing is being done. */ | |
bd5635a1 RP |
386 | |
387 | int | |
9748446f | 388 | catch_errors (func, args, errstring, mask) |
30875e1c | 389 | int (*func) PARAMS ((char *)); |
9748446f | 390 | PTR args; |
bd5635a1 | 391 | char *errstring; |
9748446f | 392 | return_mask mask; |
bd5635a1 | 393 | { |
9748446f JK |
394 | jmp_buf saved_error; |
395 | jmp_buf saved_quit; | |
396 | jmp_buf tmp_jmp; | |
bd5635a1 RP |
397 | int val; |
398 | struct cleanup *saved_cleanup_chain; | |
81066208 | 399 | char *saved_error_pre_print; |
bd5635a1 RP |
400 | |
401 | saved_cleanup_chain = save_cleanups (); | |
81066208 | 402 | saved_error_pre_print = error_pre_print; |
bd5635a1 | 403 | |
9748446f JK |
404 | if (mask & RETURN_MASK_ERROR) |
405 | memcpy ((char *)saved_error, (char *)error_return, sizeof (jmp_buf)); | |
406 | if (mask & RETURN_MASK_QUIT) | |
407 | memcpy (saved_quit, quit_return, sizeof (jmp_buf)); | |
81066208 | 408 | error_pre_print = errstring; |
bd5635a1 | 409 | |
9748446f JK |
410 | if (setjmp (tmp_jmp) == 0) |
411 | { | |
412 | if (mask & RETURN_MASK_ERROR) | |
413 | memcpy (error_return, tmp_jmp, sizeof (jmp_buf)); | |
414 | if (mask & RETURN_MASK_QUIT) | |
415 | memcpy (quit_return, tmp_jmp, sizeof (jmp_buf)); | |
416 | val = (*func) (args); | |
417 | } | |
bd5635a1 | 418 | else |
81066208 | 419 | val = 0; |
bd5635a1 RP |
420 | |
421 | restore_cleanups (saved_cleanup_chain); | |
422 | ||
81066208 | 423 | error_pre_print = saved_error_pre_print; |
9748446f JK |
424 | if (mask & RETURN_MASK_ERROR) |
425 | memcpy (error_return, saved_error, sizeof (jmp_buf)); | |
426 | if (mask & RETURN_MASK_QUIT) | |
427 | memcpy (quit_return, saved_quit, sizeof (jmp_buf)); | |
bd5635a1 RP |
428 | return val; |
429 | } | |
430 | ||
431 | /* Handler for SIGHUP. */ | |
432 | ||
433 | static void | |
30875e1c SG |
434 | disconnect (signo) |
435 | int signo; | |
bd5635a1 | 436 | { |
6fe90fc8 | 437 | catch_errors (quit_cover, NULL, |
9748446f | 438 | "Could not kill the program being debugged", RETURN_MASK_ALL); |
597dc86b | 439 | signal (SIGHUP, SIG_DFL); |
bd5635a1 RP |
440 | kill (getpid (), SIGHUP); |
441 | } | |
597dc86b SG |
442 | |
443 | /* Just a little helper function for disconnect(). */ | |
444 | ||
445 | static int | |
446 | quit_cover (s) | |
447 | char *s; | |
448 | { | |
449 | caution = 0; /* Throw caution to the wind -- we're exiting. | |
450 | This prevents asking the user dumb questions. */ | |
451 | quit_command((char *)0, 0); | |
452 | return 0; | |
453 | } | |
bd5635a1 RP |
454 | \f |
455 | /* Clean up on error during a "source" command (or execution of a | |
456 | user-defined command). */ | |
457 | ||
458 | static void | |
459 | source_cleanup (stream) | |
460 | FILE *stream; | |
461 | { | |
462 | /* Restore the previous input stream. */ | |
463 | instream = stream; | |
464 | } | |
465 | ||
466 | /* Read commands from STREAM. */ | |
467 | static void | |
468 | read_command_file (stream) | |
469 | FILE *stream; | |
470 | { | |
471 | struct cleanup *cleanups; | |
472 | ||
473 | cleanups = make_cleanup (source_cleanup, instream); | |
474 | instream = stream; | |
475 | command_loop (); | |
476 | do_cleanups (cleanups); | |
477 | } | |
478 | \f | |
479 | int | |
480 | main (argc, argv) | |
481 | int argc; | |
482 | char **argv; | |
483 | { | |
484 | int count; | |
bd5635a1 RP |
485 | static int quiet = 0; |
486 | static int batch = 0; | |
487 | ||
488 | /* Pointers to various arguments from command line. */ | |
489 | char *symarg = NULL; | |
490 | char *execarg = NULL; | |
491 | char *corearg = NULL; | |
492 | char *cdarg = NULL; | |
493 | char *ttyarg = NULL; | |
494 | ||
c307bb11 JK |
495 | /* These are static so that we can take their address in an initializer. */ |
496 | static int print_help; | |
497 | static int print_version; | |
498 | ||
499 | /* Pointers to all arguments of --command option. */ | |
bd5635a1 RP |
500 | char **cmdarg; |
501 | /* Allocated size of cmdarg. */ | |
502 | int cmdsize; | |
503 | /* Number of elements of cmdarg used. */ | |
504 | int ncmd; | |
505 | ||
c307bb11 | 506 | /* Indices of all arguments of --directory option. */ |
bd5635a1 RP |
507 | char **dirarg; |
508 | /* Allocated size. */ | |
509 | int dirsize; | |
510 | /* Number of elements used. */ | |
511 | int ndir; | |
512 | ||
3a16d640 JG |
513 | struct stat homebuf, cwdbuf; |
514 | char *homedir, *homeinit; | |
515 | ||
bd5635a1 RP |
516 | register int i; |
517 | ||
518 | /* This needs to happen before the first use of malloc. */ | |
318bf84f | 519 | init_malloc ((PTR) NULL); |
bd5635a1 RP |
520 | |
521 | #if defined (ALIGN_STACK_ON_STARTUP) | |
522 | i = (int) &count & 0x3; | |
523 | if (i != 0) | |
524 | alloca (4 - i); | |
525 | #endif | |
526 | ||
bd099407 | 527 | /* If error() is called from initialization code, just exit */ |
9748446f | 528 | if (SET_TOP_LEVEL ()) { |
bd099407 JG |
529 | exit(1); |
530 | } | |
531 | ||
bd5635a1 RP |
532 | cmdsize = 1; |
533 | cmdarg = (char **) xmalloc (cmdsize * sizeof (*cmdarg)); | |
534 | ncmd = 0; | |
535 | dirsize = 1; | |
536 | dirarg = (char **) xmalloc (dirsize * sizeof (*dirarg)); | |
537 | ndir = 0; | |
538 | ||
539 | quit_flag = 0; | |
540 | line = (char *) xmalloc (linesize); | |
541 | line[0] = '\0'; /* Terminate saved (now empty) cmd line */ | |
542 | instream = stdin; | |
543 | ||
f7402c04 | 544 | getcwd (dirbuf, sizeof (dirbuf)); |
bd5635a1 RP |
545 | current_directory = dirbuf; |
546 | ||
547 | #ifdef SET_STACK_LIMIT_HUGE | |
548 | { | |
549 | struct rlimit rlim; | |
550 | ||
551 | /* Set the stack limit huge so that alloca (particularly stringtab | |
552 | * in dbxread.c) does not fail. */ | |
553 | getrlimit (RLIMIT_STACK, &rlim); | |
554 | original_stack_limit = rlim.rlim_cur; | |
555 | rlim.rlim_cur = rlim.rlim_max; | |
556 | setrlimit (RLIMIT_STACK, &rlim); | |
557 | } | |
558 | #endif /* SET_STACK_LIMIT_HUGE */ | |
559 | ||
560 | /* Parse arguments and options. */ | |
561 | { | |
562 | int c; | |
bd5635a1 RP |
563 | /* When var field is 0, use flag field to record the equivalent |
564 | short option (or arbitrary numbers starting at 10 for those | |
565 | with no equivalent). */ | |
566 | static struct option long_options[] = | |
567 | { | |
318bf84f FF |
568 | {"readnow", no_argument, &readnow_symbol_files, 1}, |
569 | {"r", no_argument, &readnow_symbol_files, 1}, | |
570 | {"mapped", no_argument, &mapped_symbol_files, 1}, | |
571 | {"m", no_argument, &mapped_symbol_files, 1}, | |
ee0613d1 JG |
572 | {"quiet", no_argument, &quiet, 1}, |
573 | {"q", no_argument, &quiet, 1}, | |
fb29d681 | 574 | {"silent", no_argument, &quiet, 1}, |
ee0613d1 JG |
575 | {"nx", no_argument, &inhibit_gdbinit, 1}, |
576 | {"n", no_argument, &inhibit_gdbinit, 1}, | |
577 | {"batch", no_argument, &batch, 1}, | |
578 | {"epoch", no_argument, &epoch_interface, 1}, | |
579 | {"fullname", no_argument, &frame_file_full_name, 1}, | |
580 | {"f", no_argument, &frame_file_full_name, 1}, | |
581 | {"help", no_argument, &print_help, 1}, | |
582 | {"se", required_argument, 0, 10}, | |
583 | {"symbols", required_argument, 0, 's'}, | |
584 | {"s", required_argument, 0, 's'}, | |
585 | {"exec", required_argument, 0, 'e'}, | |
586 | {"e", required_argument, 0, 'e'}, | |
587 | {"core", required_argument, 0, 'c'}, | |
588 | {"c", required_argument, 0, 'c'}, | |
589 | {"command", required_argument, 0, 'x'}, | |
c307bb11 | 590 | {"version", no_argument, &print_version, 1}, |
ee0613d1 JG |
591 | {"x", required_argument, 0, 'x'}, |
592 | {"directory", required_argument, 0, 'd'}, | |
593 | {"cd", required_argument, 0, 11}, | |
594 | {"tty", required_argument, 0, 't'}, | |
595 | {"baud", required_argument, 0, 'b'}, | |
596 | {"b", required_argument, 0, 'b'}, | |
bd5635a1 RP |
597 | /* Allow machine descriptions to add more options... */ |
598 | #ifdef ADDITIONAL_OPTIONS | |
599 | ADDITIONAL_OPTIONS | |
600 | #endif | |
ee0613d1 | 601 | {0, no_argument, 0, 0}, |
bd5635a1 RP |
602 | }; |
603 | ||
604 | while (1) | |
605 | { | |
ee0613d1 JG |
606 | int option_index; |
607 | ||
bd5635a1 RP |
608 | c = getopt_long_only (argc, argv, "", |
609 | long_options, &option_index); | |
610 | if (c == EOF) | |
611 | break; | |
612 | ||
613 | /* Long option that takes an argument. */ | |
614 | if (c == 0 && long_options[option_index].flag == 0) | |
615 | c = long_options[option_index].val; | |
616 | ||
617 | switch (c) | |
618 | { | |
619 | case 0: | |
620 | /* Long option that just sets a flag. */ | |
621 | break; | |
622 | case 10: | |
623 | symarg = optarg; | |
624 | execarg = optarg; | |
625 | break; | |
626 | case 11: | |
627 | cdarg = optarg; | |
628 | break; | |
629 | case 's': | |
630 | symarg = optarg; | |
631 | break; | |
632 | case 'e': | |
633 | execarg = optarg; | |
634 | break; | |
635 | case 'c': | |
636 | corearg = optarg; | |
637 | break; | |
638 | case 'x': | |
639 | cmdarg[ncmd++] = optarg; | |
640 | if (ncmd >= cmdsize) | |
641 | { | |
642 | cmdsize *= 2; | |
643 | cmdarg = (char **) xrealloc ((char *)cmdarg, | |
644 | cmdsize * sizeof (*cmdarg)); | |
645 | } | |
646 | break; | |
647 | case 'd': | |
648 | dirarg[ndir++] = optarg; | |
649 | if (ndir >= dirsize) | |
650 | { | |
651 | dirsize *= 2; | |
652 | dirarg = (char **) xrealloc ((char *)dirarg, | |
653 | dirsize * sizeof (*dirarg)); | |
654 | } | |
655 | break; | |
656 | case 't': | |
657 | ttyarg = optarg; | |
658 | break; | |
659 | case 'q': | |
660 | quiet = 1; | |
661 | break; | |
662 | case 'b': | |
663 | baud_rate = optarg; | |
664 | break; | |
665 | #ifdef ADDITIONAL_OPTION_CASES | |
666 | ADDITIONAL_OPTION_CASES | |
667 | #endif | |
668 | case '?': | |
597dc86b | 669 | fprintf (stderr, |
fb29d681 | 670 | "Use `%s --help' for a complete list of options.\n", |
bd5635a1 RP |
671 | argv[0]); |
672 | exit (1); | |
673 | } | |
bd5635a1 | 674 | } |
fb29d681 | 675 | |
bd5635a1 RP |
676 | /* OK, that's all the options. The other arguments are filenames. */ |
677 | count = 0; | |
678 | for (; optind < argc; optind++) | |
679 | switch (++count) | |
680 | { | |
681 | case 1: | |
682 | symarg = argv[optind]; | |
683 | execarg = argv[optind]; | |
684 | break; | |
685 | case 2: | |
686 | corearg = argv[optind]; | |
687 | break; | |
688 | case 3: | |
597dc86b | 689 | fprintf (stderr, |
bd5635a1 RP |
690 | "Excess command line arguments ignored. (%s%s)\n", |
691 | argv[optind], (optind == argc - 1) ? "" : " ..."); | |
692 | break; | |
693 | } | |
694 | if (batch) | |
695 | quiet = 1; | |
696 | } | |
697 | ||
698 | /* Run the init function of each source file */ | |
699 | ||
fc61e9ee | 700 | init_cmd_lists (); /* This needs to be done first */ |
bd5635a1 | 701 | initialize_all_files (); |
fc61e9ee | 702 | init_main (); /* But that omits this file! Do it now */ |
bd5635a1 RP |
703 | init_signals (); |
704 | ||
c307bb11 JK |
705 | /* Do these (and anything which might call wrap_here or *_filtered) |
706 | after initialize_all_files. */ | |
707 | if (print_version) | |
708 | { | |
709 | print_gdb_version (stdout); | |
710 | wrap_here (""); | |
711 | printf_filtered ("\n"); | |
712 | exit (0); | |
713 | } | |
714 | ||
715 | if (print_help) | |
716 | { | |
717 | /* --version is intentionally not documented here, because we | |
718 | are printing the version here, and the help is long enough | |
719 | already. */ | |
720 | ||
721 | print_gdb_version (stdout); | |
722 | /* Make sure the output gets printed. */ | |
723 | wrap_here (""); | |
724 | printf_filtered ("\n"); | |
725 | ||
726 | /* But don't use *_filtered here. We don't want to prompt for continue | |
727 | no matter how small the screen or how much we're going to print. */ | |
728 | fputs ("\ | |
729 | This is the GNU debugger. Usage:\n\ | |
730 | gdb [options] [executable-file [core-file or process-id]]\n\ | |
731 | Options:\n\ | |
732 | --help Print this message.\n\ | |
733 | --quiet Do not print version number on startup.\n\ | |
734 | --fullname Output information used by emacs-GDB interface.\n\ | |
735 | --epoch Output information used by epoch emacs-GDB interface.\n\ | |
736 | --batch Exit after processing options.\n\ | |
737 | --nx Do not read .gdbinit file.\n\ | |
738 | --tty=TTY Use TTY for input/output by the program being debugged.\n\ | |
739 | --cd=DIR Change current directory to DIR.\n\ | |
740 | --directory=DIR Search for source files in DIR.\n\ | |
741 | --command=FILE Execute GDB commands from FILE.\n\ | |
742 | --symbols=SYMFILE Read symbols from SYMFILE.\n\ | |
743 | --exec=EXECFILE Use EXECFILE as the executable.\n\ | |
744 | --se=FILE Use FILE as symbol file and executable file.\n\ | |
745 | --core=COREFILE Analyze the core dump COREFILE.\n\ | |
746 | -b BAUDRATE Set serial port baud rate used for remote debugging.\n\ | |
747 | --mapped Use mapped symbol files if supported on this system.\n\ | |
748 | --readnow Fully read symbol files on first access.\n\ | |
749 | ", stdout); | |
750 | #ifdef ADDITIONAL_OPTION_HELP | |
751 | fputs (ADDITIONAL_OPTION_HELP, stdout); | |
752 | #endif | |
753 | fputs ("\n\ | |
754 | For more information, type \"help\" from within GDB, or consult the\n\ | |
755 | GDB manual (available as on-line info or a printed manual).\n", stdout); | |
756 | exit (0); | |
757 | } | |
758 | ||
bd5635a1 RP |
759 | if (!quiet) |
760 | { | |
81066208 JG |
761 | /* Print all the junk at the top, with trailing "..." if we are about |
762 | to read a symbol file (possibly slowly). */ | |
763 | print_gnu_advertisement (); | |
fb29d681 | 764 | print_gdb_version (stdout); |
81066208 JG |
765 | if (symarg) |
766 | printf_filtered (".."); | |
bd099407 | 767 | wrap_here(""); |
81066208 | 768 | fflush (stdout); /* Force to screen during slow operations */ |
bd5635a1 RP |
769 | } |
770 | ||
81066208 | 771 | error_pre_print = "\n\n"; |
51b57ded FF |
772 | /* We may get more than one warning, don't double space all of them... */ |
773 | warning_pre_print = "\nwarning: "; | |
81066208 | 774 | |
538280d8 FF |
775 | /* We need a default language for parsing expressions, so simple things like |
776 | "set width 0" won't fail if no language is explicitly set in a config file | |
777 | or implicitly set by reading an executable during startup. */ | |
778 | set_language (language_c); | |
b5af69c3 | 779 | expected_language = current_language; /* don't warn about the change. */ |
538280d8 | 780 | |
3a16d640 JG |
781 | /* Read and execute $HOME/.gdbinit file, if it exists. This is done |
782 | *before* all the command line arguments are processed; it sets | |
783 | global parameters, which are independent of what file you are | |
784 | debugging or what directory you are in. */ | |
785 | homedir = getenv ("HOME"); | |
786 | if (homedir) | |
787 | { | |
788 | homeinit = (char *) alloca (strlen (getenv ("HOME")) + | |
789 | strlen (gdbinit) + 10); | |
790 | strcpy (homeinit, getenv ("HOME")); | |
791 | strcat (homeinit, "/"); | |
792 | strcat (homeinit, gdbinit); | |
793 | if (!inhibit_gdbinit && access (homeinit, R_OK) == 0) | |
794 | { | |
9748446f | 795 | if (!SET_TOP_LEVEL ()) |
3a16d640 JG |
796 | source_command (homeinit, 0); |
797 | } | |
798 | do_cleanups (ALL_CLEANUPS); | |
799 | ||
800 | /* Do stats; no need to do them elsewhere since we'll only | |
801 | need them if homedir is set. Make sure that they are | |
802 | zero in case one of them fails (this guarantees that they | |
803 | won't match if either exists). */ | |
804 | ||
805 | memset (&homebuf, 0, sizeof (struct stat)); | |
806 | memset (&cwdbuf, 0, sizeof (struct stat)); | |
807 | ||
808 | stat (homeinit, &homebuf); | |
809 | stat (gdbinit, &cwdbuf); /* We'll only need this if | |
810 | homedir was set. */ | |
811 | } | |
812 | ||
bd5635a1 RP |
813 | /* Now perform all the actions indicated by the arguments. */ |
814 | if (cdarg != NULL) | |
815 | { | |
9748446f | 816 | if (!SET_TOP_LEVEL ()) |
bd5635a1 RP |
817 | { |
818 | cd_command (cdarg, 0); | |
819 | init_source_path (); | |
820 | } | |
821 | } | |
f266e564 JK |
822 | do_cleanups (ALL_CLEANUPS); |
823 | ||
bd5635a1 | 824 | for (i = 0; i < ndir; i++) |
9748446f | 825 | if (!SET_TOP_LEVEL ()) |
bd5635a1 | 826 | directory_command (dirarg[i], 0); |
51b57ded | 827 | free ((PTR)dirarg); |
f266e564 JK |
828 | do_cleanups (ALL_CLEANUPS); |
829 | ||
bd5635a1 RP |
830 | if (execarg != NULL |
831 | && symarg != NULL | |
2e4964ad | 832 | && STREQ (execarg, symarg)) |
bd5635a1 RP |
833 | { |
834 | /* The exec file and the symbol-file are the same. If we can't open | |
835 | it, better only print one error message. */ | |
9748446f | 836 | if (!SET_TOP_LEVEL ()) |
bd5635a1 RP |
837 | { |
838 | exec_file_command (execarg, !batch); | |
81066208 | 839 | symbol_file_command (symarg, 0); |
bd5635a1 RP |
840 | } |
841 | } | |
842 | else | |
843 | { | |
844 | if (execarg != NULL) | |
9748446f | 845 | if (!SET_TOP_LEVEL ()) |
bd5635a1 RP |
846 | exec_file_command (execarg, !batch); |
847 | if (symarg != NULL) | |
9748446f | 848 | if (!SET_TOP_LEVEL ()) |
81066208 | 849 | symbol_file_command (symarg, 0); |
bd5635a1 | 850 | } |
f266e564 JK |
851 | do_cleanups (ALL_CLEANUPS); |
852 | ||
81066208 JG |
853 | /* After the symbol file has been read, print a newline to get us |
854 | beyond the copyright line... But errors should still set off | |
855 | the error message with a (single) blank line. */ | |
afe4ca15 JG |
856 | if (!quiet) |
857 | printf_filtered ("\n"); | |
81066208 | 858 | error_pre_print = "\n"; |
318bf84f | 859 | warning_pre_print = "\nwarning: "; |
81066208 | 860 | |
bd5635a1 | 861 | if (corearg != NULL) |
9748446f | 862 | if (!SET_TOP_LEVEL ()) |
bd5635a1 | 863 | core_file_command (corearg, !batch); |
9748446f | 864 | else if (isdigit (corearg[0]) && !SET_TOP_LEVEL ()) |
bd5635a1 | 865 | attach_command (corearg, !batch); |
f266e564 | 866 | do_cleanups (ALL_CLEANUPS); |
bd5635a1 RP |
867 | |
868 | if (ttyarg != NULL) | |
9748446f | 869 | if (!SET_TOP_LEVEL ()) |
bd5635a1 | 870 | tty_command (ttyarg, !batch); |
f266e564 | 871 | do_cleanups (ALL_CLEANUPS); |
bd5635a1 RP |
872 | |
873 | #ifdef ADDITIONAL_OPTION_HANDLER | |
874 | ADDITIONAL_OPTION_HANDLER; | |
875 | #endif | |
876 | ||
81066208 JG |
877 | /* Error messages should no longer be distinguished with extra output. */ |
878 | error_pre_print = 0; | |
318bf84f | 879 | warning_pre_print = "warning: "; |
81066208 | 880 | |
3a16d640 JG |
881 | /* Read the .gdbinit file in the current directory, *if* it isn't |
882 | the same as the $HOME/.gdbinit file (it should exist, also). */ | |
883 | ||
884 | if (!homedir | |
885 | || memcmp ((char *) &homebuf, (char *) &cwdbuf, sizeof (struct stat))) | |
886 | if (!inhibit_gdbinit && access (gdbinit, R_OK) == 0) | |
bd5635a1 | 887 | { |
9748446f | 888 | if (!SET_TOP_LEVEL ()) |
d75710b0 | 889 | source_command (gdbinit, 0); |
bd5635a1 | 890 | } |
d75710b0 FF |
891 | do_cleanups (ALL_CLEANUPS); |
892 | ||
893 | for (i = 0; i < ncmd; i++) | |
894 | { | |
9748446f | 895 | if (!SET_TOP_LEVEL ()) |
d75710b0 FF |
896 | { |
897 | if (cmdarg[i][0] == '-' && cmdarg[i][1] == '\0') | |
898 | read_command_file (stdin); | |
899 | else | |
900 | source_command (cmdarg[i], !batch); | |
901 | do_cleanups (ALL_CLEANUPS); | |
902 | } | |
903 | } | |
51b57ded | 904 | free ((PTR)cmdarg); |
bd5635a1 | 905 | |
8b3c897a | 906 | /* Read in the old history after all the command files have been read. */ |
fc61e9ee | 907 | init_history(); |
8b3c897a | 908 | |
bd5635a1 RP |
909 | if (batch) |
910 | { | |
911 | /* We have hit the end of the batch file. */ | |
912 | exit (0); | |
913 | } | |
914 | ||
915 | /* Do any host- or target-specific hacks. This is used for i960 targets | |
916 | to force the user to set a nindy target and spec its parameters. */ | |
917 | ||
918 | #ifdef BEFORE_MAIN_LOOP_HOOK | |
919 | BEFORE_MAIN_LOOP_HOOK; | |
920 | #endif | |
921 | ||
922 | /* The command loop. */ | |
923 | ||
924 | while (1) | |
925 | { | |
9748446f | 926 | if (!SET_TOP_LEVEL ()) |
bd5635a1 | 927 | { |
f266e564 | 928 | do_cleanups (ALL_CLEANUPS); /* Do complete cleanup */ |
bd5635a1 RP |
929 | command_loop (); |
930 | quit_command ((char *)0, instream == stdin); | |
931 | } | |
932 | } | |
933 | /* No exit -- exit is through quit_command. */ | |
934 | } | |
935 | ||
b5af69c3 JG |
936 | void |
937 | execute_user_command (c, args) | |
938 | struct cmd_list_element *c; | |
939 | char *args; | |
940 | { | |
941 | register struct command_line *cmdlines; | |
942 | struct cleanup *old_chain; | |
943 | ||
944 | if (args) | |
945 | error ("User-defined commands cannot take arguments."); | |
946 | ||
947 | cmdlines = c->user_commands; | |
948 | if (cmdlines == 0) | |
949 | /* Null command */ | |
950 | return; | |
951 | ||
952 | /* Set the instream to 0, indicating execution of a | |
953 | user-defined function. */ | |
954 | old_chain = make_cleanup (source_cleanup, instream); | |
955 | instream = (FILE *) 0; | |
956 | while (cmdlines) | |
957 | { | |
958 | execute_command (cmdlines->line, 0); | |
959 | cmdlines = cmdlines->next; | |
960 | } | |
961 | do_cleanups (old_chain); | |
962 | } | |
963 | ||
bd5635a1 RP |
964 | /* Execute the line P as a command. |
965 | Pass FROM_TTY as second argument to the defining function. */ | |
966 | ||
967 | void | |
968 | execute_command (p, from_tty) | |
969 | char *p; | |
970 | int from_tty; | |
971 | { | |
972 | register struct cmd_list_element *c; | |
bd099407 | 973 | register enum language flang; |
bd099407 | 974 | static int warned = 0; |
bd5635a1 RP |
975 | |
976 | free_all_values (); | |
977 | ||
978 | /* This can happen when command_line_input hits end of file. */ | |
979 | if (p == NULL) | |
980 | return; | |
981 | ||
982 | while (*p == ' ' || *p == '\t') p++; | |
983 | if (*p) | |
984 | { | |
985 | char *arg; | |
986 | ||
987 | c = lookup_cmd (&p, cmdlist, "", 0, 1); | |
988 | /* Pass null arg rather than an empty one. */ | |
989 | arg = *p ? p : 0; | |
b5af69c3 JG |
990 | |
991 | /* If this command has been hooked, run the hook first. */ | |
992 | if (c->hook) | |
993 | execute_user_command (c->hook, (char *)0); | |
994 | ||
bd5635a1 | 995 | if (c->class == class_user) |
b5af69c3 | 996 | execute_user_command (c, arg); |
bd5635a1 RP |
997 | else if (c->type == set_cmd || c->type == show_cmd) |
998 | do_setshow_command (arg, from_tty & caution, c); | |
30875e1c | 999 | else if (c->function.cfunc == NO_FUNCTION) |
bd5635a1 RP |
1000 | error ("That is not a command, just a help topic."); |
1001 | else | |
30875e1c | 1002 | (*c->function.cfunc) (arg, from_tty & caution); |
bd099407 JG |
1003 | } |
1004 | ||
afe4ca15 | 1005 | /* Tell the user if the language has changed (except first time). */ |
b5af69c3 | 1006 | if (current_language != expected_language) |
bd099407 JG |
1007 | { |
1008 | if (language_mode == language_mode_auto) { | |
b5af69c3 | 1009 | language_info (1); /* Print what changed. */ |
bd5635a1 | 1010 | } |
bd099407 JG |
1011 | warned = 0; |
1012 | } | |
1013 | ||
1014 | /* Warn the user if the working language does not match the | |
1015 | language of the current frame. Only warn the user if we are | |
1016 | actually running the program, i.e. there is a stack. */ | |
afe4ca15 JG |
1017 | /* FIXME: This should be cacheing the frame and only running when |
1018 | the frame changes. */ | |
bd099407 JG |
1019 | if (target_has_stack) |
1020 | { | |
afe4ca15 JG |
1021 | flang = get_frame_language (); |
1022 | if (!warned | |
1023 | && flang != language_unknown | |
1024 | && flang != current_language->la_language) | |
bd099407 JG |
1025 | { |
1026 | printf_filtered ("%s\n", lang_frame_mismatch_warn); | |
1027 | warned = 1; | |
1028 | } | |
1029 | } | |
bd5635a1 RP |
1030 | } |
1031 | ||
1032 | /* ARGSUSED */ | |
30875e1c | 1033 | static void |
f266e564 | 1034 | command_loop_marker (foo) |
bd5635a1 RP |
1035 | int foo; |
1036 | { | |
1037 | } | |
1038 | ||
1039 | /* Read commands from `instream' and execute them | |
1040 | until end of file or error reading instream. */ | |
30875e1c | 1041 | static void |
bd5635a1 RP |
1042 | command_loop () |
1043 | { | |
1044 | struct cleanup *old_chain; | |
1045 | char *command; | |
1046 | int stdin_is_tty = ISATTY (stdin); | |
1047 | ||
1048 | while (!feof (instream)) | |
1049 | { | |
1050 | if (window_hook && instream == stdin) | |
1051 | (*window_hook) (instream, prompt); | |
1052 | ||
1053 | quit_flag = 0; | |
1054 | if (instream == stdin && stdin_is_tty) | |
1055 | reinitialize_more_filter (); | |
f266e564 | 1056 | old_chain = make_cleanup (command_loop_marker, 0); |
30875e1c | 1057 | command = command_line_input (instream == stdin ? prompt : (char *) NULL, |
bd5635a1 RP |
1058 | instream == stdin); |
1059 | if (command == 0) | |
1060 | return; | |
1061 | execute_command (command, instream == stdin); | |
1062 | /* Do any commands attached to breakpoint we stopped at. */ | |
1063 | bpstat_do_actions (&stop_bpstat); | |
1064 | do_cleanups (old_chain); | |
1065 | } | |
1066 | } | |
1067 | \f | |
1068 | /* Commands call this if they do not want to be repeated by null lines. */ | |
1069 | ||
1070 | void | |
1071 | dont_repeat () | |
1072 | { | |
1073 | /* If we aren't reading from standard input, we are saving the last | |
1074 | thing read from stdin in line and don't want to delete it. Null lines | |
1075 | won't repeat here in any case. */ | |
1076 | if (instream == stdin) | |
1077 | *line = 0; | |
1078 | } | |
1079 | \f | |
1080 | /* Read a line from the stream "instream" without command line editing. | |
1081 | ||
1082 | It prints PRROMPT once at the start. | |
bdbd5f50 JG |
1083 | Action is compatible with "readline", e.g. space for the result is |
1084 | malloc'd and should be freed by the caller. | |
bd5635a1 | 1085 | |
bdbd5f50 | 1086 | A NULL return means end of file. */ |
bd5635a1 | 1087 | char * |
bdbd5f50 | 1088 | gdb_readline (prrompt) |
bd5635a1 | 1089 | char *prrompt; |
bd5635a1 RP |
1090 | { |
1091 | int c; | |
1092 | char *result; | |
1093 | int input_index = 0; | |
1094 | int result_size = 80; | |
1095 | ||
1096 | if (prrompt) | |
1097 | { | |
c35475f9 JG |
1098 | /* Don't use a _filtered function here. It causes the assumed |
1099 | character position to be off, since the newline we read from | |
1100 | the user is not accounted for. */ | |
1101 | fputs (prrompt, stdout); | |
bd5635a1 RP |
1102 | fflush (stdout); |
1103 | } | |
1104 | ||
bdbd5f50 | 1105 | result = (char *) xmalloc (result_size); |
bd5635a1 RP |
1106 | |
1107 | while (1) | |
1108 | { | |
1109 | /* Read from stdin if we are executing a user defined command. | |
1110 | This is the right thing for prompt_for_continue, at least. */ | |
1111 | c = fgetc (instream ? instream : stdin); | |
bdbd5f50 JG |
1112 | |
1113 | if (c == EOF) | |
bd5635a1 | 1114 | { |
bdbd5f50 JG |
1115 | free (result); |
1116 | return NULL; | |
bd5635a1 | 1117 | } |
bd5635a1 | 1118 | |
bdbd5f50 JG |
1119 | if (c == '\n') |
1120 | break; | |
bd5635a1 | 1121 | |
bdbd5f50 JG |
1122 | result[input_index++] = c; |
1123 | while (input_index >= result_size) | |
1124 | { | |
1125 | result_size *= 2; | |
1126 | result = (char *) xrealloc (result, result_size); | |
1127 | } | |
bd5635a1 | 1128 | } |
bdbd5f50 JG |
1129 | |
1130 | result[input_index++] = '\0'; | |
1131 | return result; | |
bd5635a1 RP |
1132 | } |
1133 | ||
bd5635a1 RP |
1134 | /* Variables which control command line editing and history |
1135 | substitution. These variables are given default values at the end | |
1136 | of this file. */ | |
1137 | static int command_editing_p; | |
1138 | static int history_expansion_p; | |
1139 | static int write_history_p; | |
1140 | static int history_size; | |
1141 | static char *history_filename; | |
1142 | ||
c307bb11 JK |
1143 | /* readline uses the word breaks for two things: |
1144 | (1) In figuring out where to point the TEXT parameter to the | |
1145 | rl_completion_entry_function. Since we don't use TEXT for much, | |
1146 | it doesn't matter a lot what the word breaks are for this purpose, but | |
1147 | it does affect how much stuff M-? lists. | |
1148 | (2) If one of the matches contains a word break character, readline | |
1149 | will quote it. That's why we switch between | |
1150 | gdb_completer_word_break_characters and | |
1151 | gdb_completer_command_word_break_characters. I'm not sure when | |
1152 | we need this behavior (perhaps for funky characters in C++ symbols?). */ | |
1153 | ||
bd5635a1 RP |
1154 | /* Variables which are necessary for fancy command line editing. */ |
1155 | char *gdb_completer_word_break_characters = | |
a8e033f2 SG |
1156 | " \t\n!@#$%^&*()+=|~`}{[]\"';:?/>.<,-"; |
1157 | ||
1158 | /* When completing on command names, we remove '-' from the list of | |
1159 | word break characters, since we use it in command names. If the | |
1160 | readline library sees one in any of the current completion strings, | |
1161 | it thinks that the string needs to be quoted and automatically supplies | |
1162 | a leading quote. */ | |
1163 | char *gdb_completer_command_word_break_characters = | |
1164 | " \t\n!@#$%^&*()+=|~`}{[]\"';:?/>.<,"; | |
1165 | ||
51b57ded FF |
1166 | /* Characters that can be used to quote completion strings. Note that we |
1167 | can't include '"' because the gdb C parser treats such quoted sequences | |
1168 | as strings. */ | |
1169 | char *gdb_completer_quote_characters = | |
1170 | "'"; | |
bd5635a1 RP |
1171 | |
1172 | /* Functions that are used as part of the fancy command line editing. */ | |
1173 | ||
1174 | /* This can be used for functions which don't want to complete on symbols | |
1175 | but don't want to complete on anything else either. */ | |
1176 | /* ARGSUSED */ | |
1177 | char ** | |
c307bb11 | 1178 | noop_completer (text, prefix) |
bd5635a1 | 1179 | char *text; |
c307bb11 | 1180 | char *prefix; |
bd5635a1 RP |
1181 | { |
1182 | return NULL; | |
1183 | } | |
1184 | ||
bd50d1b0 | 1185 | /* Complete on filenames. */ |
bd50d1b0 | 1186 | char ** |
c307bb11 | 1187 | filename_completer (text, word) |
bd50d1b0 | 1188 | char *text; |
c307bb11 | 1189 | char *word; |
bd50d1b0 JK |
1190 | { |
1191 | /* From readline. */ | |
1192 | extern char *filename_completion_function (); | |
1193 | int subsequent_name; | |
1194 | char **return_val; | |
1195 | int return_val_used; | |
1196 | int return_val_alloced; | |
1197 | ||
1198 | return_val_used = 0; | |
1199 | /* Small for testing. */ | |
1200 | return_val_alloced = 1; | |
1201 | return_val = (char **) xmalloc (return_val_alloced * sizeof (char *)); | |
1202 | ||
1203 | subsequent_name = 0; | |
1204 | while (1) | |
1205 | { | |
1206 | char *p; | |
1207 | p = filename_completion_function (text, subsequent_name); | |
1208 | if (return_val_used >= return_val_alloced) | |
1209 | { | |
1210 | return_val_alloced *= 2; | |
1211 | return_val = | |
1212 | (char **) xrealloc (return_val, | |
1213 | return_val_alloced * sizeof (char *)); | |
1214 | } | |
bd50d1b0 | 1215 | if (p == NULL) |
c307bb11 JK |
1216 | { |
1217 | return_val[return_val_used++] = p; | |
1218 | break; | |
1219 | } | |
1220 | /* Like emacs, don't complete on old versions. Especially useful | |
1221 | in the "source" command. */ | |
1222 | if (p[strlen (p) - 1] == '~') | |
1223 | continue; | |
1224 | ||
1225 | { | |
1226 | char *q; | |
1227 | if (word == text) | |
1228 | /* Return exactly p. */ | |
1229 | return_val[return_val_used++] = p; | |
1230 | else if (word > text) | |
1231 | { | |
1232 | /* Return some portion of p. */ | |
1233 | q = xmalloc (strlen (p) + 5); | |
1234 | strcpy (q, p + (word - text)); | |
1235 | return_val[return_val_used++] = q; | |
1236 | free (p); | |
1237 | } | |
1238 | else | |
1239 | { | |
1240 | /* Return some of TEXT plus p. */ | |
1241 | q = xmalloc (strlen (p) + (text - word) + 5); | |
1242 | strncpy (q, word, text - word); | |
1243 | q[text - word] = '\0'; | |
1244 | strcat (q, p); | |
1245 | return_val[return_val_used++] = q; | |
1246 | free (p); | |
1247 | } | |
1248 | } | |
bd50d1b0 JK |
1249 | subsequent_name = 1; |
1250 | } | |
c307bb11 JK |
1251 | #if 0 |
1252 | /* There is no way to do this just long enough to affect quote inserting | |
1253 | without also affecting the next completion. This should be fixed in | |
1254 | readline. FIXME. */ | |
1255 | /* Insure that readline does the right thing | |
1256 | with respect to inserting quotes. */ | |
1257 | rl_completer_word_break_characters = ""; | |
1258 | #endif | |
bd50d1b0 JK |
1259 | return return_val; |
1260 | } | |
1261 | ||
c307bb11 JK |
1262 | /* Here are some useful test cases for completion. FIXME: These should |
1263 | be put in the test suite. They should be tested with both M-? and TAB. | |
1264 | ||
1265 | "show output-" "radix" | |
1266 | "show output" "-radix" | |
1267 | "p" ambiguous (commands starting with p--path, print, printf, etc.) | |
1268 | "p " ambiguous (all symbols) | |
1269 | "info t foo" no completions | |
1270 | "info t " no completions | |
1271 | "info t" ambiguous ("info target", "info terminal", etc.) | |
1272 | "info ajksdlfk" no completions | |
1273 | "info ajksdlfk " no completions | |
1274 | "info" " " | |
1275 | "info " ambiguous (all info commands) | |
1276 | "p \"a" no completions (string constant) | |
1277 | "p 'a" ambiguous (all symbols starting with a) | |
1278 | "p b-a" ambiguous (all symbols starting with a) | |
1279 | "p b-" ambiguous (all symbols) | |
1280 | "file Make" "file" (word break hard to screw up here) | |
1281 | "file ../gdb.stabs/wi" "erd" (needs to not break word at slash) | |
1282 | */ | |
1283 | ||
1284 | /* Generate completions one by one for the completer. Each time we are | |
1285 | called return another potential completion to the caller. The function | |
1286 | is misnamed; it just completes on commands or passes the buck to the | |
1287 | command's completer function; the stuff specific to symbol completion | |
1288 | is in make_symbol_completion_list. | |
1289 | ||
1290 | TEXT is readline's idea of the "word" we are looking at; we don't really | |
1291 | like readline's ideas about word breaking so we ignore it. | |
51b57ded FF |
1292 | |
1293 | MATCHES is the number of matches that have currently been collected from | |
1294 | calling this completion function. When zero, then we need to initialize, | |
1295 | otherwise the initialization has already taken place and we can just | |
1296 | return the next potential completion string. | |
30875e1c | 1297 | |
51b57ded FF |
1298 | Returns NULL if there are no more completions, else a pointer to a string |
1299 | which is a possible completion. | |
1300 | ||
1301 | RL_LINE_BUFFER is available to be looked at; it contains the entire text | |
1302 | of the line. RL_POINT is the offset in that line of the cursor. You | |
1303 | should pretend that the line ends at RL_POINT. */ | |
c307bb11 | 1304 | |
30875e1c | 1305 | static char * |
51b57ded | 1306 | symbol_completion_function (text, matches) |
bd5635a1 | 1307 | char *text; |
51b57ded | 1308 | int matches; |
bd5635a1 | 1309 | { |
51b57ded FF |
1310 | static char **list = (char **)NULL; /* Cache of completions */ |
1311 | static int index; /* Next cached completion */ | |
1312 | char *output = NULL; | |
bd5635a1 | 1313 | char *tmp_command, *p; |
c307bb11 JK |
1314 | /* Pointer within tmp_command which corresponds to text. */ |
1315 | char *word; | |
bd5635a1 | 1316 | struct cmd_list_element *c, *result_list; |
51b57ded FF |
1317 | extern char *rl_line_buffer; |
1318 | extern int rl_point; | |
bd5635a1 | 1319 | |
51b57ded | 1320 | if (matches == 0) |
bd5635a1 | 1321 | { |
51b57ded FF |
1322 | /* The caller is beginning to accumulate a new set of completions, so |
1323 | we need to find all of them now, and cache them for returning one at | |
1324 | a time on future calls. */ | |
1325 | ||
bd5635a1 | 1326 | if (list) |
51b57ded FF |
1327 | { |
1328 | /* Free the storage used by LIST, but not by the strings inside. | |
1329 | This is because rl_complete_internal () frees the strings. */ | |
1330 | free ((PTR)list); | |
1331 | } | |
bd5635a1 RP |
1332 | list = 0; |
1333 | index = 0; | |
1334 | ||
a8e033f2 SG |
1335 | /* Choose the default set of word break characters to break completions. |
1336 | If we later find out that we are doing completions on command strings | |
1337 | (as opposed to strings supplied by the individual command completer | |
1338 | functions, which can be any string) then we will switch to the | |
1339 | special word break set for command strings, which leaves out the | |
c307bb11 | 1340 | '-' character used in some commands. */ |
bd50d1b0 | 1341 | |
a8e033f2 SG |
1342 | rl_completer_word_break_characters = |
1343 | gdb_completer_word_break_characters; | |
1344 | ||
51b57ded | 1345 | /* Decide whether to complete on a list of gdb commands or on symbols. */ |
bd5635a1 RP |
1346 | tmp_command = (char *) alloca (rl_point + 1); |
1347 | p = tmp_command; | |
c307bb11 | 1348 | |
bd5635a1 RP |
1349 | strncpy (tmp_command, rl_line_buffer, rl_point); |
1350 | tmp_command[rl_point] = '\0'; | |
c307bb11 JK |
1351 | /* Since text always contains some number of characters leading up |
1352 | to rl_point, we can find the equivalent position in tmp_command | |
1353 | by subtracting that many characters from the end of tmp_command. */ | |
1354 | word = tmp_command + rl_point - strlen (text); | |
bd5635a1 RP |
1355 | |
1356 | if (rl_point == 0) | |
1357 | { | |
51b57ded FF |
1358 | /* An empty line we want to consider ambiguous; that is, it |
1359 | could be any command. */ | |
bd5635a1 RP |
1360 | c = (struct cmd_list_element *) -1; |
1361 | result_list = 0; | |
1362 | } | |
1363 | else | |
51b57ded FF |
1364 | { |
1365 | c = lookup_cmd_1 (&p, cmdlist, &result_list, 1); | |
1366 | } | |
bd5635a1 RP |
1367 | |
1368 | /* Move p up to the next interesting thing. */ | |
1369 | while (*p == ' ' || *p == '\t') | |
51b57ded FF |
1370 | { |
1371 | p++; | |
1372 | } | |
bd5635a1 RP |
1373 | |
1374 | if (!c) | |
51b57ded | 1375 | { |
c307bb11 JK |
1376 | /* It is an unrecognized command. So there are no |
1377 | possible completions. */ | |
51b57ded FF |
1378 | list = NULL; |
1379 | } | |
bd5635a1 RP |
1380 | else if (c == (struct cmd_list_element *) -1) |
1381 | { | |
c307bb11 JK |
1382 | char *q; |
1383 | ||
1384 | /* lookup_cmd_1 advances p up to the first ambiguous thing, but | |
1385 | doesn't advance over that thing itself. Do so now. */ | |
1386 | q = p; | |
1387 | while (*q && (isalnum (*q) || *q == '-' || *q == '_')) | |
1388 | ++q; | |
1389 | if (q != tmp_command + rl_point) | |
51b57ded | 1390 | { |
c307bb11 JK |
1391 | /* There is something beyond the ambiguous |
1392 | command, so there are no possible completions. For | |
1393 | example, "info t " or "info t foo" does not complete | |
1394 | to anything, because "info t" can be "info target" or | |
1395 | "info terminal". */ | |
1396 | list = NULL; | |
51b57ded | 1397 | } |
bd5635a1 | 1398 | else |
51b57ded | 1399 | { |
c307bb11 JK |
1400 | /* We're trying to complete on the command which was ambiguous. |
1401 | This we can deal with. */ | |
1402 | if (result_list) | |
1403 | { | |
1404 | list = complete_on_cmdlist (*result_list->prefixlist, p, | |
1405 | word); | |
1406 | } | |
1407 | else | |
1408 | { | |
1409 | list = complete_on_cmdlist (cmdlist, p, word); | |
1410 | } | |
1411 | /* Insure that readline does the right thing with respect to | |
1412 | inserting quotes. */ | |
1413 | rl_completer_word_break_characters = | |
1414 | gdb_completer_command_word_break_characters; | |
51b57ded | 1415 | } |
bd5635a1 RP |
1416 | } |
1417 | else | |
1418 | { | |
c307bb11 | 1419 | /* We've recognized a full command. */ |
bd5635a1 | 1420 | |
c307bb11 | 1421 | if (p == tmp_command + rl_point) |
51b57ded | 1422 | { |
c307bb11 JK |
1423 | /* There is no non-whitespace in the line beyond the command. */ |
1424 | ||
1425 | if (p[-1] == ' ' || p[-1] == '\t') | |
51b57ded | 1426 | { |
c307bb11 JK |
1427 | /* The command is followed by whitespace; we need to complete |
1428 | on whatever comes after command. */ | |
1429 | if (c->prefixlist) | |
1430 | { | |
1431 | /* It is a prefix command; what comes after it is | |
1432 | a subcommand (e.g. "info "). */ | |
1433 | list = complete_on_cmdlist (*c->prefixlist, p, word); | |
1434 | ||
1435 | /* Insure that readline does the right thing | |
1436 | with respect to inserting quotes. */ | |
1437 | rl_completer_word_break_characters = | |
1438 | gdb_completer_command_word_break_characters; | |
1439 | } | |
1440 | else | |
1441 | { | |
1442 | /* It is a normal command; what comes after it is | |
1443 | completed by the command's completer function. */ | |
1444 | list = (*c->completer) (p, word); | |
1445 | } | |
51b57ded | 1446 | } |
bd5635a1 | 1447 | else |
51b57ded | 1448 | { |
c307bb11 JK |
1449 | /* The command is not followed by whitespace; we need to |
1450 | complete on the command itself. e.g. "p" which is a | |
1451 | command itself but also can complete to "print", "ptype" | |
1452 | etc. */ | |
1453 | char *q; | |
1454 | ||
1455 | /* Find the command we are completing on. */ | |
1456 | q = p; | |
1457 | while (q > tmp_command) | |
1458 | { | |
1459 | if (isalnum (q[-1]) || q[-1] == '-' || q[-1] == '_') | |
1460 | --q; | |
1461 | else | |
1462 | break; | |
1463 | } | |
1464 | ||
1465 | list = complete_on_cmdlist (result_list, q, word); | |
1466 | ||
1467 | /* Insure that readline does the right thing | |
1468 | with respect to inserting quotes. */ | |
1469 | rl_completer_word_break_characters = | |
1470 | gdb_completer_command_word_break_characters; | |
51b57ded | 1471 | } |
bd5635a1 RP |
1472 | } |
1473 | else | |
1474 | { | |
c307bb11 JK |
1475 | /* There is non-whitespace beyond the command. */ |
1476 | ||
bd5635a1 RP |
1477 | if (c->prefixlist && !c->allow_unknown) |
1478 | { | |
c307bb11 JK |
1479 | /* It is an unrecognized subcommand of a prefix command, |
1480 | e.g. "info adsfkdj". */ | |
bd5635a1 | 1481 | list = NULL; |
bd5635a1 RP |
1482 | } |
1483 | else | |
51b57ded | 1484 | { |
c307bb11 JK |
1485 | /* It is a normal command. */ |
1486 | list = (*c->completer) (p, word); | |
51b57ded | 1487 | } |
bd5635a1 RP |
1488 | } |
1489 | } | |
1490 | } | |
1491 | ||
51b57ded FF |
1492 | /* If we found a list of potential completions during initialization then |
1493 | dole them out one at a time. The vector of completions is NULL | |
1494 | terminated, so after returning the last one, return NULL (and continue | |
1495 | to do so) each time we are called after that, until a new list is | |
1496 | available. */ | |
bd5635a1 | 1497 | |
51b57ded FF |
1498 | if (list) |
1499 | { | |
1500 | output = list[index]; | |
1501 | if (output) | |
1502 | { | |
1503 | index++; | |
1504 | } | |
1505 | } | |
bd5635a1 | 1506 | |
c307bb11 JK |
1507 | #if 0 |
1508 | /* Can't do this because readline hasn't yet checked the word breaks | |
1509 | for figuring out whether to insert a quote. */ | |
1510 | if (output == NULL) | |
1511 | /* Make sure the word break characters are set back to normal for the | |
1512 | next time that readline tries to complete something. */ | |
1513 | rl_completer_word_break_characters = | |
1514 | gdb_completer_word_break_characters; | |
1515 | #endif | |
1516 | ||
bd5635a1 RP |
1517 | return (output); |
1518 | } | |
51b57ded FF |
1519 | |
1520 | /* Skip over a possibly quoted word (as defined by the quote characters | |
1521 | and word break characters the completer uses). Returns pointer to the | |
1522 | location after the "word". */ | |
1523 | ||
1524 | char * | |
1525 | skip_quoted (str) | |
1526 | char *str; | |
1527 | { | |
1528 | char quote_char = '\0'; | |
1529 | char *scan; | |
1530 | ||
1531 | for (scan = str; *scan != '\0'; scan++) | |
1532 | { | |
1533 | if (quote_char != '\0') | |
1534 | { | |
1535 | /* Ignore everything until the matching close quote char */ | |
1536 | if (*scan == quote_char) | |
1537 | { | |
1538 | /* Found matching close quote. */ | |
1539 | scan++; | |
1540 | break; | |
1541 | } | |
1542 | } | |
1543 | else if (strchr (gdb_completer_quote_characters, *scan)) | |
1544 | { | |
1545 | /* Found start of a quoted string. */ | |
1546 | quote_char = *scan; | |
1547 | } | |
1548 | else if (strchr (gdb_completer_word_break_characters, *scan)) | |
1549 | { | |
1550 | break; | |
1551 | } | |
1552 | } | |
1553 | return (scan); | |
1554 | } | |
1555 | ||
bd5635a1 RP |
1556 | \f |
1557 | #ifdef STOP_SIGNAL | |
1558 | static void | |
30875e1c SG |
1559 | stop_sig (signo) |
1560 | int signo; | |
bd5635a1 RP |
1561 | { |
1562 | #if STOP_SIGNAL == SIGTSTP | |
1563 | signal (SIGTSTP, SIG_DFL); | |
1564 | sigsetmask (0); | |
1565 | kill (getpid (), SIGTSTP); | |
1566 | signal (SIGTSTP, stop_sig); | |
1567 | #else | |
1568 | signal (STOP_SIGNAL, stop_sig); | |
1569 | #endif | |
597dc86b | 1570 | printf ("%s", prompt); |
bd5635a1 RP |
1571 | fflush (stdout); |
1572 | ||
1573 | /* Forget about any previous command -- null line now will do nothing. */ | |
1574 | dont_repeat (); | |
1575 | } | |
1576 | #endif /* STOP_SIGNAL */ | |
1577 | ||
bd5635a1 | 1578 | /* Initialize signal handlers. */ |
f266e564 | 1579 | static void |
30875e1c SG |
1580 | do_nothing (signo) |
1581 | int signo; | |
f266e564 JK |
1582 | { |
1583 | } | |
1584 | ||
bd5635a1 RP |
1585 | static void |
1586 | init_signals () | |
1587 | { | |
bd5635a1 RP |
1588 | signal (SIGINT, request_quit); |
1589 | ||
1590 | /* If we initialize SIGQUIT to SIG_IGN, then the SIG_IGN will get | |
1591 | passed to the inferior, which we don't want. It would be | |
1592 | possible to do a "signal (SIGQUIT, SIG_DFL)" after we fork, but | |
bdbd5f50 | 1593 | on BSD4.3 systems using vfork, that can affect the |
bd5635a1 | 1594 | GDB process as well as the inferior (the signal handling tables |
bdbd5f50 | 1595 | might be in memory, shared between the two). Since we establish |
bd5635a1 RP |
1596 | a handler for SIGQUIT, when we call exec it will set the signal |
1597 | to SIG_DFL for us. */ | |
1598 | signal (SIGQUIT, do_nothing); | |
1599 | if (signal (SIGHUP, do_nothing) != SIG_IGN) | |
1600 | signal (SIGHUP, disconnect); | |
1601 | signal (SIGFPE, float_handler); | |
1eeba686 PB |
1602 | |
1603 | #if defined(SIGWINCH) && defined(SIGWINCH_HANDLER) | |
1604 | signal (SIGWINCH, SIGWINCH_HANDLER); | |
1605 | #endif | |
bd5635a1 RP |
1606 | } |
1607 | \f | |
1608 | /* Read one line from the command input stream `instream' | |
1609 | into the local static buffer `linebuffer' (whose current length | |
1610 | is `linelength'). | |
1611 | The buffer is made bigger as necessary. | |
1612 | Returns the address of the start of the line. | |
1613 | ||
1614 | NULL is returned for end of file. | |
1615 | ||
1616 | *If* the instream == stdin & stdin is a terminal, the line read | |
1617 | is copied into the file line saver (global var char *line, | |
1618 | length linesize) so that it can be duplicated. | |
1619 | ||
1620 | This routine either uses fancy command line editing or | |
1621 | simple input as the user has requested. */ | |
1622 | ||
1623 | char * | |
1624 | command_line_input (prrompt, repeat) | |
1625 | char *prrompt; | |
1626 | int repeat; | |
1627 | { | |
1628 | static char *linebuffer = 0; | |
30875e1c | 1629 | static unsigned linelength = 0; |
bd5635a1 RP |
1630 | register char *p; |
1631 | char *p1; | |
1632 | char *rl; | |
1633 | char *local_prompt = prrompt; | |
1634 | register int c; | |
1635 | char *nline; | |
1636 | char got_eof = 0; | |
1637 | ||
1638 | if (linebuffer == 0) | |
1639 | { | |
1640 | linelength = 80; | |
1641 | linebuffer = (char *) xmalloc (linelength); | |
1642 | } | |
1643 | ||
1644 | p = linebuffer; | |
1645 | ||
1646 | /* Control-C quits instantly if typed while in this loop | |
1647 | since it should not wait until the user types a newline. */ | |
1648 | immediate_quit++; | |
1649 | #ifdef STOP_SIGNAL | |
c307bb11 JK |
1650 | if (job_control) |
1651 | signal (STOP_SIGNAL, stop_sig); | |
bd5635a1 RP |
1652 | #endif |
1653 | ||
1654 | while (1) | |
1655 | { | |
e522fb52 JG |
1656 | /* Reports are that some Sys V's don't flush stdout/err on reads |
1657 | from stdin, when stdin/out are sockets rather than ttys. So we | |
1658 | have to do it ourselves, to make emacs-gdb and xxgdb work. | |
1659 | On other machines, doing this once per input should be a cheap nop. */ | |
1660 | fflush (stdout); | |
1661 | fflush (stderr); | |
1662 | ||
bd5635a1 RP |
1663 | /* Don't use fancy stuff if not talking to stdin. */ |
1664 | if (command_editing_p && instream == stdin | |
1665 | && ISATTY (instream)) | |
1666 | rl = readline (local_prompt); | |
1667 | else | |
bdbd5f50 | 1668 | rl = gdb_readline (local_prompt); |
bd5635a1 RP |
1669 | |
1670 | if (!rl || rl == (char *) EOF) | |
1671 | { | |
1672 | got_eof = 1; | |
1673 | break; | |
1674 | } | |
1675 | if (strlen(rl) + 1 + (p - linebuffer) > linelength) | |
1676 | { | |
1677 | linelength = strlen(rl) + 1 + (p - linebuffer); | |
1678 | nline = (char *) xrealloc (linebuffer, linelength); | |
1679 | p += nline - linebuffer; | |
1680 | linebuffer = nline; | |
1681 | } | |
1682 | p1 = rl; | |
1683 | /* Copy line. Don't copy null at end. (Leaves line alone | |
1684 | if this was just a newline) */ | |
1685 | while (*p1) | |
1686 | *p++ = *p1++; | |
1687 | ||
1688 | free (rl); /* Allocated in readline. */ | |
1689 | ||
1690 | if (p == linebuffer || *(p - 1) != '\\') | |
1691 | break; | |
1692 | ||
1693 | p--; /* Put on top of '\'. */ | |
1694 | local_prompt = (char *) 0; | |
1695 | } | |
1696 | ||
1697 | #ifdef STOP_SIGNAL | |
c307bb11 JK |
1698 | if (job_control) |
1699 | signal (STOP_SIGNAL, SIG_DFL); | |
bd5635a1 RP |
1700 | #endif |
1701 | immediate_quit--; | |
1702 | ||
1703 | if (got_eof) | |
1704 | return NULL; | |
1705 | ||
1706 | /* Do history expansion if that is wished. */ | |
1707 | if (history_expansion_p && instream == stdin | |
1708 | && ISATTY (instream)) | |
1709 | { | |
1710 | char *history_value; | |
1711 | int expanded; | |
1712 | ||
1713 | *p = '\0'; /* Insert null now. */ | |
1714 | expanded = history_expand (linebuffer, &history_value); | |
1715 | if (expanded) | |
1716 | { | |
1717 | /* Print the changes. */ | |
597dc86b | 1718 | printf ("%s\n", history_value); |
bd5635a1 RP |
1719 | |
1720 | /* If there was an error, call this function again. */ | |
1721 | if (expanded < 0) | |
1722 | { | |
1723 | free (history_value); | |
1724 | return command_line_input (prrompt, repeat); | |
1725 | } | |
1726 | if (strlen (history_value) > linelength) | |
1727 | { | |
1728 | linelength = strlen (history_value) + 1; | |
1729 | linebuffer = (char *) xrealloc (linebuffer, linelength); | |
1730 | } | |
1731 | strcpy (linebuffer, history_value); | |
1732 | p = linebuffer + strlen(linebuffer); | |
1733 | free (history_value); | |
1734 | } | |
1735 | } | |
1736 | ||
1737 | /* If we just got an empty line, and that is supposed | |
1738 | to repeat the previous command, return the value in the | |
1739 | global buffer. */ | |
1740 | if (repeat) | |
1741 | { | |
1742 | if (p == linebuffer) | |
1743 | return line; | |
1744 | p1 = linebuffer; | |
1745 | while (*p1 == ' ' || *p1 == '\t') | |
1746 | p1++; | |
1747 | if (!*p1) | |
1748 | return line; | |
1749 | } | |
1750 | ||
1751 | *p = 0; | |
1752 | ||
1753 | /* Add line to history if appropriate. */ | |
1754 | if (instream == stdin | |
1755 | && ISATTY (stdin) && *linebuffer) | |
1756 | add_history (linebuffer); | |
1757 | ||
1758 | /* Note: lines consisting soley of comments are added to the command | |
1759 | history. This is useful when you type a command, and then | |
1760 | realize you don't want to execute it quite yet. You can comment | |
1761 | out the command and then later fetch it from the value history | |
1762 | and remove the '#'. The kill ring is probably better, but some | |
1763 | people are in the habit of commenting things out. */ | |
1764 | p1 = linebuffer; | |
1765 | while ((c = *p1++) != '\0') | |
1766 | { | |
1767 | if (c == '"') | |
1768 | while ((c = *p1++) != '"') | |
1769 | { | |
1770 | /* Make sure an escaped '"' doesn't make us think the string | |
1771 | is ended. */ | |
1772 | if (c == '\\') | |
1773 | parse_escape (&p1); | |
1774 | if (c == '\0') | |
1775 | break; | |
1776 | } | |
1777 | else if (c == '\'') | |
1778 | while ((c = *p1++) != '\'') | |
1779 | { | |
1780 | /* Make sure an escaped '\'' doesn't make us think the string | |
1781 | is ended. */ | |
1782 | if (c == '\\') | |
1783 | parse_escape (&p1); | |
1784 | if (c == '\0') | |
1785 | break; | |
1786 | } | |
1787 | else if (c == '#') | |
1788 | { | |
1789 | /* Found a comment. */ | |
1790 | p1[-1] = '\0'; | |
1791 | break; | |
1792 | } | |
1793 | } | |
1794 | ||
1795 | /* Save into global buffer if appropriate. */ | |
1796 | if (repeat) | |
1797 | { | |
1798 | if (linelength > linesize) | |
1799 | { | |
1800 | line = xrealloc (line, linelength); | |
1801 | linesize = linelength; | |
1802 | } | |
1803 | strcpy (line, linebuffer); | |
1804 | return line; | |
1805 | } | |
1806 | ||
1807 | return linebuffer; | |
1808 | } | |
1809 | \f | |
1810 | /* Read lines from the input stream | |
1811 | and accumulate them in a chain of struct command_line's | |
1812 | which is then returned. */ | |
1813 | ||
1814 | struct command_line * | |
1815 | read_command_lines () | |
1816 | { | |
1817 | struct command_line *first = 0; | |
1818 | register struct command_line *next, *tail = 0; | |
1819 | register char *p, *p1; | |
1820 | struct cleanup *old_chain = 0; | |
1821 | ||
1822 | while (1) | |
1823 | { | |
1824 | dont_repeat (); | |
30875e1c | 1825 | p = command_line_input ((char *) NULL, instream == stdin); |
bd5635a1 RP |
1826 | if (p == NULL) |
1827 | /* Treat end of file like "end". */ | |
1828 | break; | |
1829 | ||
1830 | /* Remove leading and trailing blanks. */ | |
1831 | while (*p == ' ' || *p == '\t') p++; | |
1832 | p1 = p + strlen (p); | |
1833 | while (p1 != p && (p1[-1] == ' ' || p1[-1] == '\t')) p1--; | |
1834 | ||
1835 | /* Is this "end"? */ | |
1836 | if (p1 - p == 3 && !strncmp (p, "end", 3)) | |
1837 | break; | |
1838 | ||
1839 | /* No => add this line to the chain of command lines. */ | |
1840 | next = (struct command_line *) xmalloc (sizeof (struct command_line)); | |
1841 | next->line = savestring (p, p1 - p); | |
1842 | next->next = 0; | |
1843 | if (tail) | |
1844 | { | |
1845 | tail->next = next; | |
1846 | } | |
1847 | else | |
1848 | { | |
1849 | /* We just read the first line. | |
1850 | From now on, arrange to throw away the lines we have | |
1851 | if we quit or get an error while inside this function. */ | |
1852 | first = next; | |
1853 | old_chain = make_cleanup (free_command_lines, &first); | |
1854 | } | |
1855 | tail = next; | |
1856 | } | |
1857 | ||
1858 | dont_repeat (); | |
1859 | ||
1860 | /* Now we are about to return the chain to our caller, | |
1861 | so freeing it becomes his responsibility. */ | |
1862 | if (first) | |
1863 | discard_cleanups (old_chain); | |
1864 | return first; | |
1865 | } | |
1866 | ||
1867 | /* Free a chain of struct command_line's. */ | |
1868 | ||
1869 | void | |
1870 | free_command_lines (lptr) | |
1871 | struct command_line **lptr; | |
1872 | { | |
1873 | register struct command_line *l = *lptr; | |
1874 | register struct command_line *next; | |
1875 | ||
1876 | while (l) | |
1877 | { | |
1878 | next = l->next; | |
1879 | free (l->line); | |
51b57ded | 1880 | free ((PTR)l); |
bd5635a1 RP |
1881 | l = next; |
1882 | } | |
1883 | } | |
1884 | \f | |
1885 | /* Add an element to the list of info subcommands. */ | |
1886 | ||
1887 | void | |
1888 | add_info (name, fun, doc) | |
1889 | char *name; | |
30875e1c | 1890 | void (*fun) PARAMS ((char *, int)); |
bd5635a1 RP |
1891 | char *doc; |
1892 | { | |
1893 | add_cmd (name, no_class, fun, doc, &infolist); | |
1894 | } | |
1895 | ||
1896 | /* Add an alias to the list of info subcommands. */ | |
1897 | ||
1898 | void | |
1899 | add_info_alias (name, oldname, abbrev_flag) | |
1900 | char *name; | |
1901 | char *oldname; | |
1902 | int abbrev_flag; | |
1903 | { | |
1904 | add_alias_cmd (name, oldname, 0, abbrev_flag, &infolist); | |
1905 | } | |
1906 | ||
1907 | /* The "info" command is defined as a prefix, with allow_unknown = 0. | |
1908 | Therefore, its own definition is called only for "info" with no args. */ | |
1909 | ||
1910 | /* ARGSUSED */ | |
1911 | static void | |
1912 | info_command (arg, from_tty) | |
1913 | char *arg; | |
1914 | int from_tty; | |
1915 | { | |
597dc86b | 1916 | printf ("\"info\" must be followed by the name of an info command.\n"); |
bd5635a1 RP |
1917 | help_list (infolist, "info ", -1, stdout); |
1918 | } | |
1919 | ||
1920 | /* The "show" command with no arguments shows all the settings. */ | |
1921 | ||
1922 | /* ARGSUSED */ | |
1923 | static void | |
1924 | show_command (arg, from_tty) | |
1925 | char *arg; | |
1926 | int from_tty; | |
1927 | { | |
1928 | cmd_show_list (showlist, from_tty, ""); | |
1929 | } | |
1930 | \f | |
1931 | /* Add an element to the list of commands. */ | |
1932 | ||
1933 | void | |
1934 | add_com (name, class, fun, doc) | |
1935 | char *name; | |
1936 | enum command_class class; | |
30875e1c | 1937 | void (*fun) PARAMS ((char *, int)); |
bd5635a1 RP |
1938 | char *doc; |
1939 | { | |
1940 | add_cmd (name, class, fun, doc, &cmdlist); | |
1941 | } | |
1942 | ||
1943 | /* Add an alias or abbreviation command to the list of commands. */ | |
1944 | ||
1945 | void | |
1946 | add_com_alias (name, oldname, class, abbrev_flag) | |
1947 | char *name; | |
1948 | char *oldname; | |
1949 | enum command_class class; | |
1950 | int abbrev_flag; | |
1951 | { | |
1952 | add_alias_cmd (name, oldname, class, abbrev_flag, &cmdlist); | |
1953 | } | |
1954 | ||
1955 | void | |
1956 | error_no_arg (why) | |
1957 | char *why; | |
1958 | { | |
1959 | error ("Argument required (%s).", why); | |
1960 | } | |
1961 | ||
e1ce8aa5 | 1962 | /* ARGSUSED */ |
bd5635a1 RP |
1963 | static void |
1964 | help_command (command, from_tty) | |
1965 | char *command; | |
1966 | int from_tty; /* Ignored */ | |
1967 | { | |
1968 | help_cmd (command, stdout); | |
1969 | } | |
1970 | \f | |
1971 | static void | |
1972 | validate_comname (comname) | |
1973 | char *comname; | |
1974 | { | |
1975 | register char *p; | |
1976 | ||
1977 | if (comname == 0) | |
1978 | error_no_arg ("name of command to define"); | |
1979 | ||
1980 | p = comname; | |
1981 | while (*p) | |
1982 | { | |
c2e4669f | 1983 | if (!isalnum(*p) && *p != '-') |
bd5635a1 RP |
1984 | error ("Junk in argument list: \"%s\"", p); |
1985 | p++; | |
1986 | } | |
1987 | } | |
1988 | ||
51b57ded FF |
1989 | /* This is just a placeholder in the command data structures. */ |
1990 | static void | |
1991 | user_defined_command (ignore, from_tty) | |
1992 | char *ignore; | |
1993 | int from_tty; | |
1994 | { | |
1995 | } | |
1996 | ||
bd5635a1 RP |
1997 | static void |
1998 | define_command (comname, from_tty) | |
1999 | char *comname; | |
2000 | int from_tty; | |
2001 | { | |
2002 | register struct command_line *cmds; | |
fb29d681 | 2003 | register struct cmd_list_element *c, *newc, *hookc = 0; |
bd5635a1 | 2004 | char *tem = comname; |
b5af69c3 JG |
2005 | #define HOOK_STRING "hook-" |
2006 | #define HOOK_LEN 5 | |
bd5635a1 RP |
2007 | |
2008 | validate_comname (comname); | |
2009 | ||
afe4ca15 | 2010 | /* Look it up, and verify that we got an exact match. */ |
bd5635a1 | 2011 | c = lookup_cmd (&tem, cmdlist, "", -1, 1); |
2e4964ad | 2012 | if (c && !STREQ (comname, c->name)) |
afe4ca15 JG |
2013 | c = 0; |
2014 | ||
bd5635a1 RP |
2015 | if (c) |
2016 | { | |
2017 | if (c->class == class_user || c->class == class_alias) | |
2018 | tem = "Redefine command \"%s\"? "; | |
2019 | else | |
2020 | tem = "Really redefine built-in command \"%s\"? "; | |
c2e4669f JG |
2021 | if (!query (tem, c->name)) |
2022 | error ("Command \"%s\" not redefined.", c->name); | |
bd5635a1 RP |
2023 | } |
2024 | ||
b5af69c3 JG |
2025 | /* If this new command is a hook, then mark the command which it |
2026 | is hooking. Note that we allow hooking `help' commands, so that | |
2027 | we can hook the `stop' pseudo-command. */ | |
2028 | ||
2029 | if (!strncmp (comname, HOOK_STRING, HOOK_LEN)) | |
2030 | { | |
2031 | /* Look up cmd it hooks, and verify that we got an exact match. */ | |
2032 | tem = comname+HOOK_LEN; | |
2033 | hookc = lookup_cmd (&tem, cmdlist, "", -1, 0); | |
2e4964ad | 2034 | if (hookc && !STREQ (comname+HOOK_LEN, hookc->name)) |
b5af69c3 JG |
2035 | hookc = 0; |
2036 | if (!hookc) | |
2037 | { | |
2038 | warning ("Your new `%s' command does not hook any existing command.", | |
2039 | comname); | |
2040 | if (!query ("Proceed? ", (char *)0)) | |
2041 | error ("Not confirmed."); | |
2042 | } | |
2043 | } | |
2044 | ||
c2e4669f JG |
2045 | comname = savestring (comname, strlen (comname)); |
2046 | ||
b5af69c3 | 2047 | /* If the rest of the commands will be case insensitive, this one |
c2e4669f JG |
2048 | should behave in the same manner. */ |
2049 | for (tem = comname; *tem; tem++) | |
2050 | if (isupper(*tem)) *tem = tolower(*tem); | |
2051 | ||
bd5635a1 RP |
2052 | if (from_tty) |
2053 | { | |
597dc86b | 2054 | printf ("Type commands for definition of \"%s\".\n\ |
bd5635a1 RP |
2055 | End with a line saying just \"end\".\n", comname); |
2056 | fflush (stdout); | |
2057 | } | |
bd5635a1 RP |
2058 | |
2059 | cmds = read_command_lines (); | |
2060 | ||
2061 | if (c && c->class == class_user) | |
2062 | free_command_lines (&c->user_commands); | |
2063 | ||
51b57ded | 2064 | newc = add_cmd (comname, class_user, user_defined_command, |
bd5635a1 RP |
2065 | (c && c->class == class_user) |
2066 | ? c->doc : savestring ("User-defined.", 13), &cmdlist); | |
2067 | newc->user_commands = cmds; | |
b5af69c3 JG |
2068 | |
2069 | /* If this new command is a hook, then mark both commands as being | |
2070 | tied. */ | |
2071 | if (hookc) | |
2072 | { | |
2073 | hookc->hook = newc; /* Target gets hooked. */ | |
2074 | newc->hookee = hookc; /* We are marked as hooking target cmd. */ | |
2075 | } | |
bd5635a1 RP |
2076 | } |
2077 | ||
2078 | static void | |
2079 | document_command (comname, from_tty) | |
2080 | char *comname; | |
2081 | int from_tty; | |
2082 | { | |
2083 | struct command_line *doclines; | |
2084 | register struct cmd_list_element *c; | |
2085 | char *tem = comname; | |
2086 | ||
2087 | validate_comname (comname); | |
2088 | ||
2089 | c = lookup_cmd (&tem, cmdlist, "", 0, 1); | |
2090 | ||
2091 | if (c->class != class_user) | |
2092 | error ("Command \"%s\" is built-in.", comname); | |
2093 | ||
2094 | if (from_tty) | |
597dc86b | 2095 | printf ("Type documentation for \"%s\".\n\ |
bd5635a1 RP |
2096 | End with a line saying just \"end\".\n", comname); |
2097 | ||
2098 | doclines = read_command_lines (); | |
2099 | ||
2100 | if (c->doc) free (c->doc); | |
2101 | ||
2102 | { | |
2103 | register struct command_line *cl1; | |
2104 | register int len = 0; | |
2105 | ||
2106 | for (cl1 = doclines; cl1; cl1 = cl1->next) | |
2107 | len += strlen (cl1->line) + 1; | |
2108 | ||
2109 | c->doc = (char *) xmalloc (len + 1); | |
2110 | *c->doc = 0; | |
2111 | ||
2112 | for (cl1 = doclines; cl1; cl1 = cl1->next) | |
2113 | { | |
2114 | strcat (c->doc, cl1->line); | |
2115 | if (cl1->next) | |
2116 | strcat (c->doc, "\n"); | |
2117 | } | |
2118 | } | |
2119 | ||
2120 | free_command_lines (&doclines); | |
2121 | } | |
2122 | \f | |
2123 | static void | |
81066208 | 2124 | print_gnu_advertisement() |
bd5635a1 | 2125 | { |
597dc86b | 2126 | printf ("\ |
bd5635a1 | 2127 | GDB is free software and you are welcome to distribute copies of it\n\ |
afe4ca15 JG |
2128 | under certain conditions; type \"show copying\" to see the conditions.\n\ |
2129 | There is absolutely no warranty for GDB; type \"show warranty\" for details.\n\ | |
81066208 JG |
2130 | "); |
2131 | } | |
2132 | ||
2133 | static void | |
fb29d681 JG |
2134 | print_gdb_version (stream) |
2135 | FILE *stream; | |
81066208 | 2136 | { |
fb29d681 | 2137 | fprintf_filtered (stream, "\ |
57cb418a RP |
2138 | GDB %s (%s", version, host_canonical); |
2139 | ||
2140 | if (strcmp(host_canonical, target_canonical)) | |
2141 | fprintf_filtered (stream, " --target %s", target_canonical); | |
2142 | ||
2143 | fprintf_filtered (stream, "), "); | |
2144 | wrap_here(""); | |
2145 | fprintf_filtered (stream, "Copyright 1993 Free Software Foundation, Inc."); | |
bd5635a1 RP |
2146 | } |
2147 | ||
e1ce8aa5 | 2148 | /* ARGSUSED */ |
bd5635a1 | 2149 | static void |
f266e564 | 2150 | show_version (args, from_tty) |
bd5635a1 RP |
2151 | char *args; |
2152 | int from_tty; | |
2153 | { | |
2154 | immediate_quit++; | |
81066208 | 2155 | print_gnu_advertisement (); |
fb29d681 | 2156 | print_gdb_version (stdout); |
81066208 | 2157 | printf_filtered ("\n"); |
bd5635a1 RP |
2158 | immediate_quit--; |
2159 | } | |
2160 | \f | |
2161 | /* xgdb calls this to reprint the usual GDB prompt. */ | |
2162 | ||
2163 | void | |
2164 | print_prompt () | |
2165 | { | |
597dc86b | 2166 | printf ("%s", prompt); |
bd5635a1 RP |
2167 | fflush (stdout); |
2168 | } | |
2169 | \f | |
2170 | static void | |
2171 | quit_command (args, from_tty) | |
2172 | char *args; | |
2173 | int from_tty; | |
2174 | { | |
f266e564 | 2175 | if (inferior_pid != 0 && target_has_execution) |
bd5635a1 | 2176 | { |
6fe90fc8 | 2177 | if (attach_flag) |
bd5635a1 | 2178 | { |
6fe90fc8 | 2179 | if (query ("The program is running. Quit anyway (and detach it)? ")) |
597dc86b SG |
2180 | target_detach (args, from_tty); |
2181 | else | |
6fe90fc8 | 2182 | error ("Not confirmed."); |
bd5635a1 RP |
2183 | } |
2184 | else | |
6fe90fc8 JK |
2185 | { |
2186 | if (query ("The program is running. Quit anyway (and kill it)? ")) | |
2187 | target_kill (); | |
2188 | else | |
2189 | error ("Not confirmed."); | |
2190 | } | |
bd5635a1 RP |
2191 | } |
2192 | /* Save the history information if it is appropriate to do so. */ | |
2193 | if (write_history_p && history_filename) | |
2194 | write_history (history_filename); | |
2195 | exit (0); | |
2196 | } | |
2197 | ||
ee0613d1 JG |
2198 | /* Returns whether GDB is running on a terminal and whether the user |
2199 | desires that questions be asked of them on that terminal. */ | |
2200 | ||
bd5635a1 RP |
2201 | int |
2202 | input_from_terminal_p () | |
2203 | { | |
c307bb11 | 2204 | return gdb_has_a_terminal () && (instream == stdin) & caution; |
bd5635a1 RP |
2205 | } |
2206 | \f | |
e1ce8aa5 | 2207 | /* ARGSUSED */ |
bd5635a1 RP |
2208 | static void |
2209 | pwd_command (args, from_tty) | |
2210 | char *args; | |
2211 | int from_tty; | |
2212 | { | |
2213 | if (args) error ("The \"pwd\" command does not take an argument: %s", args); | |
f7402c04 | 2214 | getcwd (dirbuf, sizeof (dirbuf)); |
bd5635a1 | 2215 | |
2e4964ad | 2216 | if (!STREQ (dirbuf, current_directory)) |
597dc86b | 2217 | printf ("Working directory %s\n (canonically %s).\n", |
bd5635a1 RP |
2218 | current_directory, dirbuf); |
2219 | else | |
597dc86b | 2220 | printf ("Working directory %s.\n", current_directory); |
bd5635a1 RP |
2221 | } |
2222 | ||
2223 | static void | |
2224 | cd_command (dir, from_tty) | |
2225 | char *dir; | |
2226 | int from_tty; | |
2227 | { | |
2228 | int len; | |
c307bb11 JK |
2229 | /* Found something other than leading repetitions of "/..". */ |
2230 | int found_real_path; | |
2231 | char *p; | |
bd5635a1 | 2232 | |
9107291d JK |
2233 | /* If the new directory is absolute, repeat is a no-op; if relative, |
2234 | repeat might be useful but is more likely to be a mistake. */ | |
2235 | dont_repeat (); | |
2236 | ||
bd5635a1 RP |
2237 | if (dir == 0) |
2238 | error_no_arg ("new working directory"); | |
2239 | ||
2240 | dir = tilde_expand (dir); | |
2241 | make_cleanup (free, dir); | |
2242 | ||
ee0613d1 JG |
2243 | if (chdir (dir) < 0) |
2244 | perror_with_name (dir); | |
2245 | ||
bd5635a1 RP |
2246 | len = strlen (dir); |
2247 | dir = savestring (dir, len - (len > 1 && dir[len-1] == '/')); | |
2248 | if (dir[0] == '/') | |
2249 | current_directory = dir; | |
2250 | else | |
2251 | { | |
c307bb11 JK |
2252 | if (current_directory[0] == '/' && current_directory[1] == '\0') |
2253 | current_directory = concat (current_directory, dir, NULL); | |
2254 | else | |
2255 | current_directory = concat (current_directory, "/", dir, NULL); | |
bd5635a1 RP |
2256 | free (dir); |
2257 | } | |
2258 | ||
2259 | /* Now simplify any occurrences of `.' and `..' in the pathname. */ | |
2260 | ||
c307bb11 JK |
2261 | found_real_path = 0; |
2262 | for (p = current_directory; *p;) | |
bd5635a1 | 2263 | { |
c307bb11 JK |
2264 | if (p[0] == '/' && p[1] == '.' && (p[2] == 0 || p[2] == '/')) |
2265 | strcpy (p, p + 2); | |
2266 | else if (p[0] == '/' && p[1] == '.' && p[2] == '.' | |
2267 | && (p[3] == 0 || p[3] == '/')) | |
bd5635a1 | 2268 | { |
c307bb11 | 2269 | if (found_real_path) |
bd5635a1 | 2270 | { |
c307bb11 JK |
2271 | /* Search backwards for the directory just before the "/.." |
2272 | and obliterate it and the "/..". */ | |
bd5635a1 | 2273 | char *q = p; |
c307bb11 JK |
2274 | while (q != current_directory && q[-1] != '/') |
2275 | --q; | |
2276 | ||
2277 | if (q == current_directory) | |
2278 | /* current_directory is | |
2279 | a relative pathname ("can't happen"--leave it alone). */ | |
2280 | ++p; | |
2281 | else | |
bd5635a1 | 2282 | { |
c307bb11 JK |
2283 | strcpy (q - 1, p + 3); |
2284 | p = q - 1; | |
bd5635a1 RP |
2285 | } |
2286 | } | |
c307bb11 JK |
2287 | else |
2288 | /* We are dealing with leading repetitions of "/..", for example | |
2289 | "/../..", which is the Mach super-root. */ | |
2290 | p += 3; | |
2291 | } | |
2292 | else | |
2293 | { | |
2294 | found_real_path = 1; | |
2295 | ++p; | |
bd5635a1 RP |
2296 | } |
2297 | } | |
2298 | ||
bd5635a1 RP |
2299 | forget_cached_source_info (); |
2300 | ||
2301 | if (from_tty) | |
2302 | pwd_command ((char *) 0, 1); | |
2303 | } | |
2304 | \f | |
e1ce8aa5 | 2305 | /* ARGSUSED */ |
bd5635a1 RP |
2306 | static void |
2307 | source_command (args, from_tty) | |
2308 | char *args; | |
2309 | int from_tty; | |
2310 | { | |
2311 | FILE *stream; | |
2312 | struct cleanup *cleanups; | |
2313 | char *file = args; | |
2314 | ||
6fe90fc8 JK |
2315 | if (file == NULL) |
2316 | { | |
2317 | error ("source command requires pathname of file to source."); | |
2318 | } | |
bd5635a1 RP |
2319 | |
2320 | file = tilde_expand (file); | |
2321 | make_cleanup (free, file); | |
2322 | ||
6fe90fc8 | 2323 | stream = fopen (file, FOPEN_RT); |
bd5635a1 RP |
2324 | if (stream == 0) |
2325 | perror_with_name (file); | |
2326 | ||
2327 | cleanups = make_cleanup (fclose, stream); | |
2328 | ||
2329 | read_command_file (stream); | |
2330 | ||
2331 | do_cleanups (cleanups); | |
2332 | } | |
2333 | ||
2334 | /* ARGSUSED */ | |
2335 | static void | |
2336 | echo_command (text, from_tty) | |
2337 | char *text; | |
2338 | int from_tty; | |
2339 | { | |
2340 | char *p = text; | |
2341 | register int c; | |
2342 | ||
2343 | if (text) | |
a8a69e63 | 2344 | while ((c = *p++) != '\0') |
bd5635a1 RP |
2345 | { |
2346 | if (c == '\\') | |
2347 | { | |
2348 | /* \ at end of argument is used after spaces | |
2349 | so they won't be lost. */ | |
2350 | if (*p == 0) | |
2351 | return; | |
2352 | ||
2353 | c = parse_escape (&p); | |
2354 | if (c >= 0) | |
afe4ca15 | 2355 | printf_filtered ("%c", c); |
bd5635a1 RP |
2356 | } |
2357 | else | |
afe4ca15 | 2358 | printf_filtered ("%c", c); |
bd5635a1 | 2359 | } |
afe4ca15 JG |
2360 | |
2361 | /* Force this output to appear now. */ | |
2362 | wrap_here (""); | |
f266e564 | 2363 | fflush (stdout); |
bd5635a1 RP |
2364 | } |
2365 | ||
bd5635a1 RP |
2366 | \f |
2367 | /* Functions to manipulate command line editing control variables. */ | |
2368 | ||
f266e564 | 2369 | /* Number of commands to print in each call to show_commands. */ |
bd5635a1 RP |
2370 | #define Hist_print 10 |
2371 | static void | |
f266e564 | 2372 | show_commands (args, from_tty) |
bd5635a1 RP |
2373 | char *args; |
2374 | int from_tty; | |
2375 | { | |
2376 | /* Index for history commands. Relative to history_base. */ | |
2377 | int offset; | |
2378 | ||
2379 | /* Number of the history entry which we are planning to display next. | |
2380 | Relative to history_base. */ | |
2381 | static int num = 0; | |
2382 | ||
2383 | /* The first command in the history which doesn't exist (i.e. one more | |
2384 | than the number of the last command). Relative to history_base. */ | |
2385 | int hist_len; | |
2386 | ||
30875e1c | 2387 | extern struct _hist_entry *history_get PARAMS ((int)); |
bd5635a1 RP |
2388 | extern int history_base; |
2389 | ||
bd5635a1 RP |
2390 | /* Print out some of the commands from the command history. */ |
2391 | /* First determine the length of the history list. */ | |
2392 | hist_len = history_size; | |
2393 | for (offset = 0; offset < history_size; offset++) | |
2394 | { | |
2395 | if (!history_get (history_base + offset)) | |
2396 | { | |
2397 | hist_len = offset; | |
2398 | break; | |
2399 | } | |
2400 | } | |
2401 | ||
2402 | if (args) | |
2403 | { | |
2404 | if (args[0] == '+' && args[1] == '\0') | |
2405 | /* "info editing +" should print from the stored position. */ | |
2406 | ; | |
2407 | else | |
2408 | /* "info editing <exp>" should print around command number <exp>. */ | |
2409 | num = (parse_and_eval_address (args) - history_base) - Hist_print / 2; | |
2410 | } | |
ee0613d1 | 2411 | /* "show commands" means print the last Hist_print commands. */ |
bd5635a1 RP |
2412 | else |
2413 | { | |
2414 | num = hist_len - Hist_print; | |
2415 | } | |
2416 | ||
2417 | if (num < 0) | |
2418 | num = 0; | |
2419 | ||
2420 | /* If there are at least Hist_print commands, we want to display the last | |
2421 | Hist_print rather than, say, the last 6. */ | |
2422 | if (hist_len - num < Hist_print) | |
2423 | { | |
2424 | num = hist_len - Hist_print; | |
2425 | if (num < 0) | |
2426 | num = 0; | |
2427 | } | |
2428 | ||
bd5635a1 RP |
2429 | for (offset = num; offset < num + Hist_print && offset < hist_len; offset++) |
2430 | { | |
2431 | printf_filtered ("%5d %s\n", history_base + offset, | |
2432 | (history_get (history_base + offset))->line); | |
2433 | } | |
2434 | ||
2435 | /* The next command we want to display is the next one that we haven't | |
2436 | displayed yet. */ | |
2437 | num += Hist_print; | |
2438 | ||
2439 | /* If the user repeats this command with return, it should do what | |
ee0613d1 JG |
2440 | "show commands +" does. This is unnecessary if arg is null, |
2441 | because "show commands +" is not useful after "show commands". */ | |
bd5635a1 RP |
2442 | if (from_tty && args) |
2443 | { | |
2444 | args[0] = '+'; | |
2445 | args[1] = '\0'; | |
2446 | } | |
2447 | } | |
2448 | ||
2449 | /* Called by do_setshow_command. */ | |
e1ce8aa5 | 2450 | /* ARGSUSED */ |
bd5635a1 RP |
2451 | static void |
2452 | set_history_size_command (args, from_tty, c) | |
2453 | char *args; | |
2454 | int from_tty; | |
2455 | struct cmd_list_element *c; | |
2456 | { | |
6fe90fc8 | 2457 | if (history_size == INT_MAX) |
bd5635a1 | 2458 | unstifle_history (); |
c2e4669f | 2459 | else if (history_size >= 0) |
bd5635a1 | 2460 | stifle_history (history_size); |
c2e4669f JG |
2461 | else |
2462 | { | |
6fe90fc8 | 2463 | history_size = INT_MAX; |
c2e4669f JG |
2464 | error ("History size must be non-negative"); |
2465 | } | |
bd5635a1 RP |
2466 | } |
2467 | ||
e1ce8aa5 | 2468 | /* ARGSUSED */ |
bd5635a1 RP |
2469 | static void |
2470 | set_history (args, from_tty) | |
2471 | char *args; | |
2472 | int from_tty; | |
2473 | { | |
597dc86b | 2474 | printf ("\"set history\" must be followed by the name of a history subcommand.\n"); |
bd5635a1 RP |
2475 | help_list (sethistlist, "set history ", -1, stdout); |
2476 | } | |
2477 | ||
e1ce8aa5 | 2478 | /* ARGSUSED */ |
bd5635a1 RP |
2479 | static void |
2480 | show_history (args, from_tty) | |
2481 | char *args; | |
2482 | int from_tty; | |
2483 | { | |
f266e564 | 2484 | cmd_show_list (showhistlist, from_tty, ""); |
bd5635a1 RP |
2485 | } |
2486 | ||
2487 | int info_verbose = 0; /* Default verbose msgs off */ | |
2488 | ||
2489 | /* Called by do_setshow_command. An elaborate joke. */ | |
e1ce8aa5 | 2490 | /* ARGSUSED */ |
bd5635a1 RP |
2491 | static void |
2492 | set_verbose (args, from_tty, c) | |
2493 | char *args; | |
2494 | int from_tty; | |
2495 | struct cmd_list_element *c; | |
2496 | { | |
2497 | char *cmdname = "verbose"; | |
2498 | struct cmd_list_element *showcmd; | |
2499 | ||
2500 | showcmd = lookup_cmd_1 (&cmdname, showlist, NULL, 1); | |
2501 | ||
2502 | if (info_verbose) | |
2503 | { | |
2504 | c->doc = "Set verbose printing of informational messages."; | |
2505 | showcmd->doc = "Show verbose printing of informational messages."; | |
2506 | } | |
2507 | else | |
2508 | { | |
2509 | c->doc = "Set verbosity."; | |
2510 | showcmd->doc = "Show verbosity."; | |
2511 | } | |
2512 | } | |
2513 | ||
2514 | static void | |
30875e1c SG |
2515 | float_handler (signo) |
2516 | int signo; | |
bd5635a1 RP |
2517 | { |
2518 | /* This message is based on ANSI C, section 4.7. Note that integer | |
2519 | divide by zero causes this, so "float" is a misnomer. */ | |
76a0ffb4 | 2520 | signal (SIGFPE, float_handler); |
bd5635a1 RP |
2521 | error ("Erroneous arithmetic operation."); |
2522 | } | |
2523 | ||
2524 | /* Return whether we are running a batch file or from terminal. */ | |
2525 | int | |
2526 | batch_mode () | |
2527 | { | |
2528 | return !(instream == stdin && ISATTY (stdin)); | |
2529 | } | |
2530 | ||
2531 | \f | |
2532 | static void | |
fc61e9ee | 2533 | init_cmd_lists () |
bd5635a1 | 2534 | { |
0239d9b3 FF |
2535 | cmdlist = NULL; |
2536 | infolist = NULL; | |
2537 | enablelist = NULL; | |
2538 | disablelist = NULL; | |
2539 | deletelist = NULL; | |
2540 | enablebreaklist = NULL; | |
2541 | setlist = NULL; | |
2542 | unsetlist = NULL; | |
bd5635a1 | 2543 | showlist = NULL; |
0239d9b3 | 2544 | sethistlist = NULL; |
bd5635a1 | 2545 | showhistlist = NULL; |
0239d9b3 FF |
2546 | unsethistlist = NULL; |
2547 | #if MAINTENANCE_CMDS | |
2548 | maintenancelist = NULL; | |
2549 | maintenanceinfolist = NULL; | |
a8e033f2 | 2550 | maintenanceprintlist = NULL; |
0239d9b3 FF |
2551 | #endif |
2552 | setprintlist = NULL; | |
2553 | showprintlist = NULL; | |
2554 | setchecklist = NULL; | |
2555 | showchecklist = NULL; | |
bd5635a1 RP |
2556 | } |
2557 | ||
8b3c897a SG |
2558 | /* Init the history buffer. Note that we are called after the init file(s) |
2559 | * have been read so that the user can change the history file via his | |
2560 | * .gdbinit file (for instance). The GDBHISTFILE environment variable | |
2561 | * overrides all of this. | |
2562 | */ | |
2563 | ||
bd5635a1 | 2564 | static void |
fc61e9ee | 2565 | init_history() |
bd5635a1 | 2566 | { |
bd5635a1 | 2567 | char *tmpenv; |
bd5635a1 | 2568 | |
318bf84f FF |
2569 | tmpenv = getenv ("HISTSIZE"); |
2570 | if (tmpenv) | |
bd5635a1 | 2571 | history_size = atoi (tmpenv); |
8b3c897a | 2572 | else if (!history_size) |
bd5635a1 RP |
2573 | history_size = 256; |
2574 | ||
2575 | stifle_history (history_size); | |
2576 | ||
318bf84f FF |
2577 | tmpenv = getenv ("GDBHISTFILE"); |
2578 | if (tmpenv) | |
bd5635a1 | 2579 | history_filename = savestring (tmpenv, strlen(tmpenv)); |
8b3c897a | 2580 | else if (!history_filename) { |
bd5635a1 RP |
2581 | /* We include the current directory so that if the user changes |
2582 | directories the file written will be the same as the one | |
2583 | that was read. */ | |
7d9884b9 | 2584 | history_filename = concat (current_directory, "/.gdb_history", NULL); |
8b3c897a | 2585 | } |
bd5635a1 | 2586 | read_history (history_filename); |
8b3c897a | 2587 | } |
bd5635a1 | 2588 | |
8b3c897a | 2589 | static void |
fc61e9ee | 2590 | init_main () |
8b3c897a SG |
2591 | { |
2592 | struct cmd_list_element *c; | |
2593 | ||
2594 | #ifdef DEFAULT_PROMPT | |
2595 | prompt = savestring (DEFAULT_PROMPT, strlen(DEFAULT_PROMPT)); | |
2596 | #else | |
2597 | prompt = savestring ("(gdb) ", 6); | |
2598 | #endif | |
2599 | ||
2600 | /* Set the important stuff up for command editing. */ | |
2601 | command_editing_p = 1; | |
2602 | history_expansion_p = 0; | |
2603 | write_history_p = 0; | |
2604 | ||
bd5635a1 RP |
2605 | /* Setup important stuff for command line editing. */ |
2606 | rl_completion_entry_function = (int (*)()) symbol_completion_function; | |
2607 | rl_completer_word_break_characters = gdb_completer_word_break_characters; | |
51b57ded | 2608 | rl_completer_quote_characters = gdb_completer_quote_characters; |
bd5635a1 RP |
2609 | rl_readline_name = "gdb"; |
2610 | ||
2611 | /* Define the classes of commands. | |
2612 | They will appear in the help list in the reverse of this order. */ | |
2613 | ||
0239d9b3 FF |
2614 | add_cmd ("internals", class_maintenance, NO_FUNCTION, |
2615 | "Maintenance commands.\n\ | |
2616 | Some gdb commands are provided just for use by gdb maintainers.\n\ | |
2617 | These commands are subject to frequent change, and may not be as\n\ | |
2618 | well documented as user commands.", | |
2619 | &cmdlist); | |
bd5635a1 RP |
2620 | add_cmd ("obscure", class_obscure, NO_FUNCTION, "Obscure features.", &cmdlist); |
2621 | add_cmd ("aliases", class_alias, NO_FUNCTION, "Aliases of other commands.", &cmdlist); | |
2622 | add_cmd ("user-defined", class_user, NO_FUNCTION, "User-defined commands.\n\ | |
2623 | The commands in this class are those defined by the user.\n\ | |
2624 | Use the \"define\" command to define a command.", &cmdlist); | |
2625 | add_cmd ("support", class_support, NO_FUNCTION, "Support facilities.", &cmdlist); | |
2626 | add_cmd ("status", class_info, NO_FUNCTION, "Status inquiries.", &cmdlist); | |
2627 | add_cmd ("files", class_files, NO_FUNCTION, "Specifying and examining files.", &cmdlist); | |
2628 | add_cmd ("breakpoints", class_breakpoint, NO_FUNCTION, "Making program stop at certain points.", &cmdlist); | |
2629 | add_cmd ("data", class_vars, NO_FUNCTION, "Examining data.", &cmdlist); | |
2630 | add_cmd ("stack", class_stack, NO_FUNCTION, "Examining the stack.\n\ | |
2631 | The stack is made up of stack frames. Gdb assigns numbers to stack frames\n\ | |
2632 | counting from zero for the innermost (currently executing) frame.\n\n\ | |
2633 | At any time gdb identifies one frame as the \"selected\" frame.\n\ | |
2634 | Variable lookups are done with respect to the selected frame.\n\ | |
2635 | When the program being debugged stops, gdb selects the innermost frame.\n\ | |
2636 | The commands below can be used to select other frames by number or address.", | |
2637 | &cmdlist); | |
2638 | add_cmd ("running", class_run, NO_FUNCTION, "Running the program.", &cmdlist); | |
2639 | ||
2640 | add_com ("pwd", class_files, pwd_command, | |
2641 | "Print working directory. This is used for your program as well."); | |
df0f0dcc | 2642 | c = add_cmd ("cd", class_files, cd_command, |
bd5635a1 RP |
2643 | "Set working directory to DIR for debugger and program being debugged.\n\ |
2644 | The change does not take effect for the program being debugged\n\ | |
df0f0dcc JK |
2645 | until the next time it is started.", &cmdlist); |
2646 | c->completer = filename_completer; | |
bd5635a1 RP |
2647 | |
2648 | add_show_from_set | |
2649 | (add_set_cmd ("prompt", class_support, var_string, (char *)&prompt, | |
2650 | "Set gdb's prompt", | |
2651 | &setlist), | |
2652 | &showlist); | |
2653 | ||
2654 | add_com ("echo", class_support, echo_command, | |
2655 | "Print a constant string. Give string as argument.\n\ | |
2656 | C escape sequences may be used in the argument.\n\ | |
2657 | No newline is added at the end of the argument;\n\ | |
2658 | use \"\\n\" if you want a newline to be printed.\n\ | |
2659 | Since leading and trailing whitespace are ignored in command arguments,\n\ | |
2660 | if you want to print some you must use \"\\\" before leading whitespace\n\ | |
2661 | to be printed or after trailing whitespace."); | |
2662 | add_com ("document", class_support, document_command, | |
2663 | "Document a user-defined command.\n\ | |
2664 | Give command name as argument. Give documentation on following lines.\n\ | |
2665 | End with a line of just \"end\"."); | |
2666 | add_com ("define", class_support, define_command, | |
2667 | "Define a new command name. Command name is argument.\n\ | |
2668 | Definition appears on following lines, one command per line.\n\ | |
2669 | End with a line of just \"end\".\n\ | |
2670 | Use the \"document\" command to give documentation for the new command.\n\ | |
2671 | Commands defined in this way do not take arguments."); | |
2672 | ||
2673 | #ifdef __STDC__ | |
df0f0dcc | 2674 | c = add_cmd ("source", class_support, source_command, |
bd5635a1 RP |
2675 | "Read commands from a file named FILE.\n\ |
2676 | Note that the file \"" GDBINIT_FILENAME "\" is read automatically in this way\n\ | |
df0f0dcc | 2677 | when gdb is started.", &cmdlist); |
bd5635a1 RP |
2678 | #else |
2679 | /* Punt file name, we can't help it easily. */ | |
df0f0dcc | 2680 | c = add_cmd ("source", class_support, source_command, |
bd5635a1 RP |
2681 | "Read commands from a file named FILE.\n\ |
2682 | Note that the file \".gdbinit\" is read automatically in this way\n\ | |
df0f0dcc | 2683 | when gdb is started.", &cmdlist); |
bd5635a1 | 2684 | #endif |
df0f0dcc | 2685 | c->completer = filename_completer; |
bd5635a1 RP |
2686 | |
2687 | add_com ("quit", class_support, quit_command, "Exit gdb."); | |
2688 | add_com ("help", class_support, help_command, "Print list of commands."); | |
2689 | add_com_alias ("q", "quit", class_support, 1); | |
2690 | add_com_alias ("h", "help", class_support, 1); | |
2691 | ||
2692 | ||
2693 | c = add_set_cmd ("verbose", class_support, var_boolean, (char *)&info_verbose, | |
2694 | "Set ", | |
2695 | &setlist), | |
2696 | add_show_from_set (c, &showlist); | |
30875e1c | 2697 | c->function.sfunc = set_verbose; |
bd5635a1 RP |
2698 | set_verbose (NULL, 0, c); |
2699 | ||
bd5635a1 RP |
2700 | add_show_from_set |
2701 | (add_set_cmd ("editing", class_support, var_boolean, (char *)&command_editing_p, | |
ee0613d1 | 2702 | "Set editing of command lines as they are typed.\n\ |
bd5635a1 | 2703 | Use \"on\" to enable to enable the editing, and \"off\" to disable it.\n\ |
ee0613d1 JG |
2704 | Without an argument, command line editing is enabled. To edit, use\n\ |
2705 | EMACS-like or VI-like commands like control-P or ESC.", &setlist), | |
bd5635a1 RP |
2706 | &showlist); |
2707 | ||
2708 | add_prefix_cmd ("history", class_support, set_history, | |
2709 | "Generic command for setting command history parameters.", | |
2710 | &sethistlist, "set history ", 0, &setlist); | |
2711 | add_prefix_cmd ("history", class_support, show_history, | |
2712 | "Generic command for showing command history parameters.", | |
2713 | &showhistlist, "show history ", 0, &showlist); | |
2714 | ||
2715 | add_show_from_set | |
2716 | (add_set_cmd ("expansion", no_class, var_boolean, (char *)&history_expansion_p, | |
2717 | "Set history expansion on command input.\n\ | |
2718 | Without an argument, history expansion is enabled.", &sethistlist), | |
2719 | &showhistlist); | |
2720 | ||
2721 | add_show_from_set | |
f266e564 | 2722 | (add_set_cmd ("save", no_class, var_boolean, (char *)&write_history_p, |
bd5635a1 RP |
2723 | "Set saving of the history record on exit.\n\ |
2724 | Use \"on\" to enable to enable the saving, and \"off\" to disable it.\n\ | |
2725 | Without an argument, saving is enabled.", &sethistlist), | |
2726 | &showhistlist); | |
2727 | ||
6fe90fc8 | 2728 | c = add_set_cmd ("size", no_class, var_integer, (char *)&history_size, |
bd5635a1 RP |
2729 | "Set the size of the command history, \n\ |
2730 | ie. the number of previous commands to keep a record of.", &sethistlist); | |
2731 | add_show_from_set (c, &showhistlist); | |
30875e1c | 2732 | c->function.sfunc = set_history_size_command; |
bd5635a1 RP |
2733 | |
2734 | add_show_from_set | |
2735 | (add_set_cmd ("filename", no_class, var_filename, (char *)&history_filename, | |
2736 | "Set the filename in which to record the command history\n\ | |
2737 | (the list of previous commands of which a record is kept).", &sethistlist), | |
2738 | &showhistlist); | |
2739 | ||
2740 | add_show_from_set | |
f266e564 | 2741 | (add_set_cmd ("confirm", class_support, var_boolean, |
bd5635a1 | 2742 | (char *)&caution, |
f266e564 JK |
2743 | "Set whether to confirm potentially dangerous operations.", |
2744 | &setlist), | |
bd5635a1 RP |
2745 | &showlist); |
2746 | ||
2747 | add_prefix_cmd ("info", class_info, info_command, | |
ee0613d1 | 2748 | "Generic command for showing things about the program being debugged.", |
bd5635a1 RP |
2749 | &infolist, "info ", 0, &cmdlist); |
2750 | add_com_alias ("i", "info", class_info, 1); | |
2751 | ||
2752 | add_prefix_cmd ("show", class_info, show_command, | |
ee0613d1 | 2753 | "Generic command for showing things about the debugger.", |
bd5635a1 RP |
2754 | &showlist, "show ", 0, &cmdlist); |
2755 | /* Another way to get at the same thing. */ | |
2756 | add_info ("set", show_command, "Show all GDB settings."); | |
2757 | ||
ee0613d1 JG |
2758 | add_cmd ("commands", no_class, show_commands, |
2759 | "Show the the history of commands you typed.\n\ | |
2760 | You can supply a command number to start with, or a `+' to start after\n\ | |
2761 | the previous command number shown.", | |
f266e564 | 2762 | &showlist); |
bd5635a1 | 2763 | |
f266e564 | 2764 | add_cmd ("version", no_class, show_version, |
ee0613d1 | 2765 | "Show what version of GDB this is.", &showlist); |
bd5635a1 | 2766 | } |