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