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