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