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