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