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