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