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