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