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