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