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