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