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