2000-03-20 Michael Snyder <msnyder@cleaver.cygnus.com>
[deliverable/binutils-gdb.git] / gdb / top.c
1 /* Top level stuff for GDB, the GNU debugger.
2 Copyright 1986-2000 Free Software Foundation, Inc.
3
4 This file is part of GDB.
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, 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 "value.h"
32 #include "language.h"
33 #include "terminal.h" /* For job_control. */
34 #include "annotate.h"
35 #include "top.h"
36 #include "version.h"
37
38 /* readline include files */
39 #include <readline/readline.h>
40 #include <readline/history.h>
41
42 /* readline defines this. */
43 #undef savestring
44
45 #include <sys/types.h>
46
47 #include <setjmp.h>
48
49 #include "event-top.h"
50 #include "gdb_string.h"
51 #include "gdb_stat.h"
52 #include <ctype.h>
53 #ifdef UI_OUT
54 #include "ui-out.h"
55 #include "cli-out.h"
56 #endif
57
58 /* Prototypes for local functions */
59
60 static void dont_repeat_command PARAMS ((char *, int));
61
62 static void source_cleanup_lines PARAMS ((PTR));
63
64 static void user_defined_command PARAMS ((char *, int));
65
66 static void init_signals PARAMS ((void));
67
68 #ifdef STOP_SIGNAL
69 static void stop_sig PARAMS ((int));
70 #endif
71
72 static char *line_completion_function PARAMS ((char *, int, char *, int));
73
74 static char *readline_line_completion_function PARAMS ((char *, int));
75
76 /* NOTE 1999-04-29: this function will be static again, after we make the
77 event loop be the default command loop for gdb, and we merge
78 event-top.c into this file, top.c */
79 /* static */ void command_loop_marker PARAMS ((int));
80
81 static void while_command PARAMS ((char *, int));
82
83 static void if_command PARAMS ((char *, int));
84
85 static struct command_line *
86 build_command_line PARAMS ((enum command_control_type, char *));
87
88 static struct command_line *
89 get_command_line PARAMS ((enum command_control_type, char *));
90
91 static void realloc_body_list PARAMS ((struct command_line *, int));
92
93 static enum misc_command_type read_next_line PARAMS ((struct command_line **));
94
95 static enum command_control_type
96 recurse_read_control_structure PARAMS ((struct command_line *));
97
98 static struct cleanup *setup_user_args PARAMS ((char *));
99
100 static char *locate_arg PARAMS ((char *));
101
102 static char *insert_args PARAMS ((char *));
103
104 static void arg_cleanup PARAMS ((void));
105
106 static void init_main PARAMS ((void));
107
108 static void init_cmd_lists PARAMS ((void));
109
110 static void float_handler PARAMS ((int));
111
112 static void init_signals PARAMS ((void));
113
114 static void set_verbose PARAMS ((char *, int, struct cmd_list_element *));
115
116 static void show_history PARAMS ((char *, int));
117
118 static void set_history PARAMS ((char *, int));
119
120 static void set_history_size_command PARAMS ((char *, int,
121 struct cmd_list_element *));
122
123 static void show_commands PARAMS ((char *, int));
124
125 static void echo_command PARAMS ((char *, int));
126
127 static void pwd_command PARAMS ((char *, int));
128
129 static void show_version PARAMS ((char *, int));
130
131 static void document_command PARAMS ((char *, int));
132
133 static void define_command PARAMS ((char *, int));
134
135 static void validate_comname PARAMS ((char *));
136
137 static void help_command PARAMS ((char *, int));
138
139 static void show_command PARAMS ((char *, int));
140
141 static void info_command PARAMS ((char *, int));
142
143 static void complete_command PARAMS ((char *, int));
144
145 static void do_nothing PARAMS ((int));
146
147 #ifdef SIGHUP
148 /* NOTE 1999-04-29: This function will be static again, once we modify
149 gdb to use the event loop as the default command loop and we merge
150 event-top.c into this file, top.c */
151 /* static */ int quit_cover PARAMS ((PTR));
152
153 static void disconnect PARAMS ((int));
154 #endif
155
156 static void source_cleanup PARAMS ((FILE *));
157
158 /* Default command line prompt. This is overriden in some configs. */
159
160 #ifndef DEFAULT_PROMPT
161 #define DEFAULT_PROMPT "(gdb) "
162 #endif
163
164 /* Initialization file name for gdb. This is overridden in some configs. */
165
166 #ifndef GDBINIT_FILENAME
167 #define GDBINIT_FILENAME ".gdbinit"
168 #endif
169 char gdbinit[] = GDBINIT_FILENAME;
170
171 int inhibit_gdbinit = 0;
172
173 /* If nonzero, and GDB has been configured to be able to use windows,
174 attempt to open them upon startup. */
175
176 int use_windows = 1;
177
178 extern char lang_frame_mismatch_warn[]; /* language.c */
179
180 /* Flag for whether we want all the "from_tty" gubbish printed. */
181
182 int caution = 1; /* Default is yes, sigh. */
183
184 /* Define all cmd_list_elements. */
185
186 /* Chain containing all defined commands. */
187
188 struct cmd_list_element *cmdlist;
189
190 /* Chain containing all defined info subcommands. */
191
192 struct cmd_list_element *infolist;
193
194 /* Chain containing all defined enable subcommands. */
195
196 struct cmd_list_element *enablelist;
197
198 /* Chain containing all defined disable subcommands. */
199
200 struct cmd_list_element *disablelist;
201
202 /* Chain containing all defined toggle subcommands. */
203
204 struct cmd_list_element *togglelist;
205
206 /* Chain containing all defined stop subcommands. */
207
208 struct cmd_list_element *stoplist;
209
210 /* Chain containing all defined delete subcommands. */
211
212 struct cmd_list_element *deletelist;
213
214 /* Chain containing all defined "enable breakpoint" subcommands. */
215
216 struct cmd_list_element *enablebreaklist;
217
218 /* Chain containing all defined set subcommands */
219
220 struct cmd_list_element *setlist;
221
222 /* Chain containing all defined unset subcommands */
223
224 struct cmd_list_element *unsetlist;
225
226 /* Chain containing all defined show subcommands. */
227
228 struct cmd_list_element *showlist;
229
230 /* Chain containing all defined \"set history\". */
231
232 struct cmd_list_element *sethistlist;
233
234 /* Chain containing all defined \"show history\". */
235
236 struct cmd_list_element *showhistlist;
237
238 /* Chain containing all defined \"unset history\". */
239
240 struct cmd_list_element *unsethistlist;
241
242 /* Chain containing all defined maintenance subcommands. */
243
244 struct cmd_list_element *maintenancelist;
245
246 /* Chain containing all defined "maintenance info" subcommands. */
247
248 struct cmd_list_element *maintenanceinfolist;
249
250 /* Chain containing all defined "maintenance print" subcommands. */
251
252 struct cmd_list_element *maintenanceprintlist;
253
254 struct cmd_list_element *setprintlist;
255
256 struct cmd_list_element *showprintlist;
257
258 struct cmd_list_element *setchecklist;
259
260 struct cmd_list_element *showchecklist;
261
262 /* stdio stream that command input is being read from. Set to stdin normally.
263 Set by source_command to the file we are sourcing. Set to NULL if we are
264 executing a user-defined command or interacting via a GUI. */
265
266 FILE *instream;
267
268 /* Current working directory. */
269
270 char *current_directory;
271
272 /* The directory name is actually stored here (usually). */
273 char gdb_dirbuf[1024];
274
275 /* Function to call before reading a command, if nonzero.
276 The function receives two args: an input stream,
277 and a prompt string. */
278
279 void (*window_hook) PARAMS ((FILE *, char *));
280
281 int epoch_interface;
282 int xgdb_verbose;
283
284 /* gdb prints this when reading a command interactively */
285 static char *gdb_prompt_string; /* the global prompt string */
286 extern char *get_prompt PARAMS ((void)); /* access function for prompt string */
287
288 /* Buffer used for reading command lines, and the size
289 allocated for it so far. */
290
291 char *line;
292 int linesize = 100;
293
294 /* Nonzero if the current command is modified by "server ". This
295 affects things like recording into the command history, commands
296 repeating on RETURN, etc. This is so a user interface (emacs, GUI,
297 whatever) can issue its own commands and also send along commands
298 from the user, and have the user not notice that the user interface
299 is issuing commands too. */
300 int server_command;
301
302 /* Baud rate specified for talking to serial target systems. Default
303 is left as -1, so targets can choose their own defaults. */
304 /* FIXME: This means that "show remotebaud" and gr_files_info can print -1
305 or (unsigned int)-1. This is a Bad User Interface. */
306
307 int baud_rate = -1;
308
309 /* Timeout limit for response from target. */
310
311 /* The default value has been changed many times over the years. It
312 was originally 5 seconds. But that was thought to be a long time
313 to sit and wait, so it was changed to 2 seconds. That was thought
314 to be plenty unless the connection was going through some terminal
315 server or multiplexer or other form of hairy serial connection.
316
317 In mid-1996, remote_timeout was moved from remote.c to top.c and
318 it began being used in other remote-* targets. It appears that the
319 default was changed to 20 seconds at that time, perhaps because the
320 Hitachi E7000 ICE didn't always respond in a timely manner.
321
322 But if 5 seconds is a long time to sit and wait for retransmissions,
323 20 seconds is far worse. This demonstrates the difficulty of using
324 a single variable for all protocol timeouts.
325
326 As remote.c is used much more than remote-e7000.c, it was changed
327 back to 2 seconds in 1999. */
328
329 int remote_timeout = 2;
330
331 /* Non-zero tells remote* modules to output debugging info. */
332
333 int remote_debug = 0;
334
335 /* Non-zero means the target is running. Note: this is different from
336 saying that there is an active target and we are stopped at a
337 breakpoint, for instance. This is a real indicator whether the
338 target is off and running, which gdb is doing something else. */
339 int target_executing = 0;
340
341 /* Level of control structure. */
342 static int control_level;
343
344 /* Structure for arguments to user defined functions. */
345 #define MAXUSERARGS 10
346 struct user_args
347 {
348 struct user_args *next;
349 struct
350 {
351 char *arg;
352 int len;
353 }
354 a[MAXUSERARGS];
355 int count;
356 }
357 *user_args;
358
359 /* Signal to catch ^Z typed while reading a command: SIGTSTP or SIGCONT. */
360
361 #ifndef STOP_SIGNAL
362 #ifdef SIGTSTP
363 #define STOP_SIGNAL SIGTSTP
364 static void stop_sig PARAMS ((int));
365 #endif
366 #endif
367
368 /* Some System V have job control but not sigsetmask(). */
369 #if !defined (HAVE_SIGSETMASK)
370 #if !defined (USG)
371 #define HAVE_SIGSETMASK 1
372 #else
373 #define HAVE_SIGSETMASK 0
374 #endif
375 #endif
376
377 #if 0 == (HAVE_SIGSETMASK)
378 #define sigsetmask(n)
379 #endif
380
381 /* Hooks for alternate command interfaces. */
382
383 /* Called after most modules have been initialized, but before taking users
384 command file. */
385
386 void (*init_ui_hook) PARAMS ((char *argv0));
387
388 /* This hook is called from within gdb's many mini-event loops which could
389 steal control from a real user interface's event loop. It returns
390 non-zero if the user is requesting a detach, zero otherwise. */
391
392 int (*ui_loop_hook) PARAMS ((int));
393
394 /* Called instead of command_loop at top level. Can be invoked via
395 return_to_top_level. */
396
397 void (*command_loop_hook) PARAMS ((void));
398
399
400 /* Called from print_frame_info to list the line we stopped in. */
401
402 void (*print_frame_info_listing_hook) PARAMS ((struct symtab * s, int line,
403 int stopline, int noerror));
404 /* Replaces most of query. */
405
406 int (*query_hook) PARAMS ((const char *, va_list));
407
408 /* Replaces most of warning. */
409
410 void (*warning_hook) PARAMS ((const char *, va_list));
411
412 /* These three functions support getting lines of text from the user. They
413 are used in sequence. First readline_begin_hook is called with a text
414 string that might be (for example) a message for the user to type in a
415 sequence of commands to be executed at a breakpoint. If this function
416 calls back to a GUI, it might take this opportunity to pop up a text
417 interaction window with this message. Next, readline_hook is called
418 with a prompt that is emitted prior to collecting the user input.
419 It can be called multiple times. Finally, readline_end_hook is called
420 to notify the GUI that we are done with the interaction window and it
421 can close it. */
422
423 void (*readline_begin_hook) PARAMS ((char *,...));
424 char *(*readline_hook) PARAMS ((char *));
425 void (*readline_end_hook) PARAMS ((void));
426
427 /* Called as appropriate to notify the interface of the specified breakpoint
428 conditions. */
429
430 void (*create_breakpoint_hook) PARAMS ((struct breakpoint * bpt));
431 void (*delete_breakpoint_hook) PARAMS ((struct breakpoint * bpt));
432 void (*modify_breakpoint_hook) PARAMS ((struct breakpoint * bpt));
433
434 /* Called as appropriate to notify the interface that we have attached
435 to or detached from an already running process. */
436
437 void (*attach_hook) PARAMS ((void));
438 void (*detach_hook) PARAMS ((void));
439
440 /* Called during long calculations to allow GUI to repair window damage, and to
441 check for stop buttons, etc... */
442
443 void (*interactive_hook) PARAMS ((void));
444
445 /* Called when the registers have changed, as a hint to a GUI
446 to minimize window update. */
447
448 void (*registers_changed_hook) PARAMS ((void));
449
450 /* Tell the GUI someone changed the register REGNO. -1 means
451 that the caller does not know which register changed or
452 that several registers have changed (see value_assign). */
453 void (*register_changed_hook) PARAMS ((int regno));
454
455 /* Tell the GUI someone changed LEN bytes of memory at ADDR */
456 void (*memory_changed_hook) PARAMS ((CORE_ADDR addr, int len));
457
458 /* Called when going to wait for the target. Usually allows the GUI to run
459 while waiting for target events. */
460
461 int (*target_wait_hook) PARAMS ((int pid, struct target_waitstatus * status));
462
463 /* Used by UI as a wrapper around command execution. May do various things
464 like enabling/disabling buttons, etc... */
465
466 void (*call_command_hook) PARAMS ((struct cmd_list_element * c, char *cmd,
467 int from_tty));
468
469 /* Called after a `set' command has finished. Is only run if the
470 `set' command succeeded. */
471
472 void (*set_hook) PARAMS ((struct cmd_list_element *c));
473
474 /* Called when the current thread changes. Argument is thread id. */
475
476 void (*context_hook) PARAMS ((int id));
477
478 /* Takes control from error (). Typically used to prevent longjmps out of the
479 middle of the GUI. Usually used in conjunction with a catch routine. */
480
481 NORETURN void (*error_hook)
482 PARAMS ((void)) ATTR_NORETURN;
483 \f
484
485 /* One should use catch_errors rather than manipulating these
486 directly. */
487 #if defined(HAVE_SIGSETJMP)
488 #define SIGJMP_BUF sigjmp_buf
489 #define SIGSETJMP(buf) sigsetjmp(buf, 1)
490 #define SIGLONGJMP(buf,val) siglongjmp(buf,val)
491 #else
492 #define SIGJMP_BUF jmp_buf
493 #define SIGSETJMP(buf) setjmp(buf)
494 #define SIGLONGJMP(buf,val) longjmp(buf,val)
495 #endif
496
497 /* Where to go for return_to_top_level. */
498 static SIGJMP_BUF *catch_return;
499
500 /* Return for reason REASON to the nearest containing catch_errors(). */
501
502 NORETURN void
503 return_to_top_level (reason)
504 enum return_reason reason;
505 {
506 quit_flag = 0;
507 immediate_quit = 0;
508
509 /* Perhaps it would be cleaner to do this via the cleanup chain (not sure
510 I can think of a reason why that is vital, though). */
511 bpstat_clear_actions (stop_bpstat); /* Clear queued breakpoint commands */
512
513 disable_current_display ();
514 do_cleanups (ALL_CLEANUPS);
515 if (event_loop_p && target_can_async_p () && !target_executing)
516 do_exec_cleanups (ALL_CLEANUPS);
517 if (event_loop_p && sync_execution)
518 do_exec_error_cleanups (ALL_CLEANUPS);
519
520 if (annotation_level > 1)
521 switch (reason)
522 {
523 case RETURN_QUIT:
524 annotate_quit ();
525 break;
526 case RETURN_ERROR:
527 annotate_error ();
528 break;
529 }
530
531 /* Jump to the containing catch_errors() call, communicating REASON
532 to that call via setjmp's return value. Note that REASON can't
533 be zero, by definition in defs.h. */
534
535 (NORETURN void) SIGLONGJMP (*catch_return, (int)reason);
536 }
537
538 /* Call FUNC with arg ARGS, catching any errors. If there is no
539 error, return the value returned by FUNC. If there is an error,
540 print ERRSTRING, print the specific error message, then return
541 zero.
542
543 Must not be called with immediate_quit in effect (bad things might
544 happen, say we got a signal in the middle of a memcpy to quit_return).
545 This is an OK restriction; with very few exceptions immediate_quit can
546 be replaced by judicious use of QUIT.
547
548 MASK specifies what to catch; it is normally set to
549 RETURN_MASK_ALL, if for no other reason than that the code which
550 calls catch_errors might not be set up to deal with a quit which
551 isn't caught. But if the code can deal with it, it generally
552 should be RETURN_MASK_ERROR, unless for some reason it is more
553 useful to abort only the portion of the operation inside the
554 catch_errors. Note that quit should return to the command line
555 fairly quickly, even if some further processing is being done. */
556
557 /* MAYBE: cagney/1999-11-05: catch_errors() in conjunction with
558 error() et.al. could maintain a set of flags that indicate the the
559 current state of each of the longjmp buffers. This would give the
560 longjmp code the chance to detect a longjmp botch (before it gets
561 to longjmperror()). Prior to 1999-11-05 this wasn't possible as
562 code also randomly used a SET_TOP_LEVEL macro that directly
563 initialize the longjmp buffers. */
564
565 /* MAYBE: cagney/1999-11-05: Should the catch_erros and cleanups code
566 be consolidated into a single file instead of being distributed
567 between utils.c and top.c? */
568
569 int
570 catch_errors (func, args, errstring, mask)
571 catch_errors_ftype *func;
572 PTR args;
573 char *errstring;
574 return_mask mask;
575 {
576 SIGJMP_BUF *saved_catch;
577 SIGJMP_BUF catch;
578 int val;
579 struct cleanup *saved_cleanup_chain;
580 char *saved_error_pre_print;
581 char *saved_quit_pre_print;
582
583 /* Return value from SIGSETJMP(): enum return_reason if error or
584 quit caught, 0 otherwise. */
585 int caught;
586
587 /* Override error/quit messages during FUNC. */
588
589 saved_error_pre_print = error_pre_print;
590 saved_quit_pre_print = quit_pre_print;
591
592 if (mask & RETURN_MASK_ERROR)
593 error_pre_print = errstring;
594 if (mask & RETURN_MASK_QUIT)
595 quit_pre_print = errstring;
596
597 /* Prevent error/quit during FUNC from calling cleanups established
598 prior to here. */
599
600 saved_cleanup_chain = save_cleanups ();
601
602 /* Call FUNC, catching error/quit events. */
603
604 saved_catch = catch_return;
605 catch_return = &catch;
606 caught = SIGSETJMP (catch);
607 if (!caught)
608 val = (*func) (args);
609 catch_return = saved_catch;
610
611 /* FIXME: cagney/1999-11-05: A correct FUNC implementaton will
612 clean things up (restoring the cleanup chain) to the state they
613 were just prior to the call. Unfortunatly, many FUNC's are not
614 that well behaved. This could be fixed by adding either a
615 do_cleanups call (to cover the problem) or an assertion check to
616 detect bad FUNCs code. */
617
618 /* Restore the cleanup chain and error/quit messages to their
619 original states. */
620
621 restore_cleanups (saved_cleanup_chain);
622
623 if (mask & RETURN_MASK_QUIT)
624 quit_pre_print = saved_quit_pre_print;
625 if (mask & RETURN_MASK_ERROR)
626 error_pre_print = saved_error_pre_print;
627
628 /* Return normally if no error/quit event occurred. */
629
630 if (!caught)
631 return val;
632
633 /* If the caller didn't request that the event be caught, relay the
634 event to the next containing catch_errors(). */
635
636 if (!(mask & RETURN_MASK (caught)))
637 return_to_top_level (caught);
638
639 /* Tell the caller that an event was caught.
640
641 FIXME: nsd/2000-02-22: When MASK is RETURN_MASK_ALL, the caller
642 can't tell what type of event occurred.
643
644 A possible fix is to add a new interface, catch_event(), that
645 returns enum return_reason after catching an error or a quit.
646
647 When returning normally, i.e. without catching an error or a
648 quit, catch_event() could return RETURN_NORMAL, which would be
649 added to enum return_reason. FUNC would return information
650 exclusively via ARGS.
651
652 Alternatively, normal catch_event() could return FUNC's return
653 value. The caller would need to be aware of potential overlap
654 with enum return_reason, which could be publicly restricted to
655 negative values to simplify return value processing in FUNC and
656 in the caller. */
657
658 return 0;
659 }
660
661 struct captured_command_args
662 {
663 catch_command_errors_ftype *command;
664 char *arg;
665 int from_tty;
666 };
667
668 static int
669 do_captured_command (void *data)
670 {
671 struct captured_command_args *context = data;
672 context->command (context->arg, context->from_tty);
673 /* FIXME: cagney/1999-11-07: Technically this do_cleanups() call
674 isn't needed. Instead an assertion check could be made that
675 simply confirmed that the called function correctly cleaned up
676 after its self. Unfortunatly, old code (prior to 1999-11-04) in
677 main.c was calling SET_TOP_LEVEL(), calling the command function,
678 and then *always* calling do_cleanups(). For the moment we
679 remain ``bug compatible'' with that old code.. */
680 do_cleanups (ALL_CLEANUPS);
681 return 1;
682 }
683
684 int
685 catch_command_errors (catch_command_errors_ftype *command,
686 char *arg, int from_tty, return_mask mask)
687 {
688 struct captured_command_args args;
689 args.command = command;
690 args.arg = arg;
691 args.from_tty = from_tty;
692 return catch_errors (do_captured_command, &args, "", mask);
693 }
694
695
696 /* Handler for SIGHUP. */
697
698 #ifdef SIGHUP
699 static void
700 disconnect (signo)
701 int signo;
702 {
703 catch_errors (quit_cover, NULL,
704 "Could not kill the program being debugged", RETURN_MASK_ALL);
705 signal (SIGHUP, SIG_DFL);
706 kill (getpid (), SIGHUP);
707 }
708
709 /* Just a little helper function for disconnect(). */
710
711 /* NOTE 1999-04-29: This function will be static again, once we modify
712 gdb to use the event loop as the default command loop and we merge
713 event-top.c into this file, top.c */
714 /* static */ int
715 quit_cover (s)
716 PTR s;
717 {
718 caution = 0; /* Throw caution to the wind -- we're exiting.
719 This prevents asking the user dumb questions. */
720 quit_command ((char *) 0, 0);
721 return 0;
722 }
723 #endif /* defined SIGHUP */
724 \f
725 /* Line number we are currently in in a file which is being sourced. */
726 /* NOTE 1999-04-29: This variable will be static again, once we modify
727 gdb to use the event loop as the default command loop and we merge
728 event-top.c into this file, top.c */
729 /* static */ int source_line_number;
730
731 /* Name of the file we are sourcing. */
732 /* NOTE 1999-04-29: This variable will be static again, once we modify
733 gdb to use the event loop as the default command loop and we merge
734 event-top.c into this file, top.c */
735 /* static */ char *source_file_name;
736
737 /* Buffer containing the error_pre_print used by the source stuff.
738 Malloc'd. */
739 /* NOTE 1999-04-29: This variable will be static again, once we modify
740 gdb to use the event loop as the default command loop and we merge
741 event-top.c into this file, top.c */
742 /* static */ char *source_error;
743 static int source_error_allocated;
744
745 /* Something to glom on to the start of error_pre_print if source_file_name
746 is set. */
747 /* NOTE 1999-04-29: This variable will be static again, once we modify
748 gdb to use the event loop as the default command loop and we merge
749 event-top.c into this file, top.c */
750 /* static */ char *source_pre_error;
751
752 /* Clean up on error during a "source" command (or execution of a
753 user-defined command). */
754
755 static void
756 source_cleanup (stream)
757 FILE *stream;
758 {
759 /* Restore the previous input stream. */
760 instream = stream;
761 }
762
763 /* Read commands from STREAM. */
764 void
765 read_command_file (stream)
766 FILE *stream;
767 {
768 struct cleanup *cleanups;
769
770 cleanups = make_cleanup ((make_cleanup_func) source_cleanup, instream);
771 instream = stream;
772 command_loop ();
773 do_cleanups (cleanups);
774 }
775 \f
776 extern void init_proc PARAMS ((void));
777
778 void (*pre_init_ui_hook) PARAMS ((void));
779
780 void
781 gdb_init (argv0)
782 char *argv0;
783 {
784 if (pre_init_ui_hook)
785 pre_init_ui_hook ();
786
787 /* Run the init function of each source file */
788
789 getcwd (gdb_dirbuf, sizeof (gdb_dirbuf));
790 current_directory = gdb_dirbuf;
791
792 #ifdef __MSDOS__
793 /* Make sure we return to the original directory upon exit, come
794 what may, since the OS doesn't do that for us. */
795 make_final_cleanup ((make_cleanup_func) chdir, strsave (current_directory));
796 #endif
797
798 init_cmd_lists (); /* This needs to be done first */
799 initialize_targets (); /* Setup target_terminal macros for utils.c */
800 initialize_utils (); /* Make errors and warnings possible */
801 initialize_all_files ();
802 initialize_current_architecture ();
803 init_main (); /* But that omits this file! Do it now */
804
805 /* The signal handling mechanism is different depending whether or
806 not the async version is run. NOTE: in the future we plan to make
807 the event loop be the default engine of gdb, and this difference
808 will disappear. */
809 if (event_loop_p)
810 async_init_signals ();
811 else
812 init_signals ();
813
814 /* We need a default language for parsing expressions, so simple things like
815 "set width 0" won't fail if no language is explicitly set in a config file
816 or implicitly set by reading an executable during startup. */
817 set_language (language_c);
818 expected_language = current_language; /* don't warn about the change. */
819
820 #ifdef UI_OUT
821 /* Install the default UI */
822 uiout = cli_out_new (gdb_stdout);
823 #endif
824
825 #ifdef UI_OUT
826 /* All the interpreters should have had a look at things by now.
827 Initialize the selected interpreter. */
828 if (interpreter_p && !init_ui_hook)
829 {
830 fprintf_unfiltered (gdb_stderr, "Interpreter `%s' unrecognized.\n",
831 interpreter_p);
832 exit (1);
833 }
834 #endif
835
836 if (init_ui_hook)
837 init_ui_hook (argv0);
838 }
839
840 /* Allocate, initialize a new command line structure for one of the
841 control commands (if/while). */
842
843 static struct command_line *
844 build_command_line (type, args)
845 enum command_control_type type;
846 char *args;
847 {
848 struct command_line *cmd;
849
850 if (args == NULL)
851 error ("if/while commands require arguments.\n");
852
853 cmd = (struct command_line *) xmalloc (sizeof (struct command_line));
854 cmd->next = NULL;
855 cmd->control_type = type;
856
857 cmd->body_count = 1;
858 cmd->body_list
859 = (struct command_line **) xmalloc (sizeof (struct command_line *)
860 * cmd->body_count);
861 memset (cmd->body_list, 0, sizeof (struct command_line *) * cmd->body_count);
862 cmd->line = savestring (args, strlen (args));
863 return cmd;
864 }
865
866 /* Build and return a new command structure for the control commands
867 such as "if" and "while". */
868
869 static struct command_line *
870 get_command_line (type, arg)
871 enum command_control_type type;
872 char *arg;
873 {
874 struct command_line *cmd;
875 struct cleanup *old_chain = NULL;
876
877 /* Allocate and build a new command line structure. */
878 cmd = build_command_line (type, arg);
879
880 old_chain = make_cleanup ((make_cleanup_func) free_command_lines, &cmd);
881
882 /* Read in the body of this command. */
883 if (recurse_read_control_structure (cmd) == invalid_control)
884 {
885 warning ("error reading in control structure\n");
886 do_cleanups (old_chain);
887 return NULL;
888 }
889
890 discard_cleanups (old_chain);
891 return cmd;
892 }
893
894 /* Recursively print a command (including full control structures). */
895 #ifdef UI_OUT
896 void
897 print_command_lines (uiout, cmd, depth)
898 struct ui_out *uiout;
899 struct command_line *cmd;
900 unsigned int depth;
901 {
902 struct command_line *list;
903
904 list = cmd;
905 while (list)
906 {
907
908 if (depth)
909 ui_out_spaces (uiout, 2 * depth);
910
911 /* A simple command, print it and continue. */
912 if (list->control_type == simple_control)
913 {
914 ui_out_field_string (uiout, NULL, list->line);
915 ui_out_text (uiout, "\n");
916 list = list->next;
917 continue;
918 }
919
920 /* loop_continue to jump to the start of a while loop, print it
921 and continue. */
922 if (list->control_type == continue_control)
923 {
924 ui_out_field_string (uiout, NULL, "loop_continue");
925 ui_out_text (uiout, "\n");
926 list = list->next;
927 continue;
928 }
929
930 /* loop_break to break out of a while loop, print it and continue. */
931 if (list->control_type == break_control)
932 {
933 ui_out_field_string (uiout, NULL, "loop_break");
934 ui_out_text (uiout, "\n");
935 list = list->next;
936 continue;
937 }
938
939 /* A while command. Recursively print its subcommands and continue. */
940 if (list->control_type == while_control)
941 {
942 ui_out_text (uiout, "while ");
943 ui_out_field_fmt (uiout, NULL, "while %s", list->line);
944 ui_out_text (uiout, "\n");
945 print_command_lines (uiout, *list->body_list, depth + 1);
946 ui_out_field_string (uiout, NULL, "end");
947 if (depth)
948 ui_out_spaces (uiout, 2 * depth);
949 ui_out_text (uiout, "end\n");
950 list = list->next;
951 continue;
952 }
953
954 /* An if command. Recursively print both arms before continueing. */
955 if (list->control_type == if_control)
956 {
957 ui_out_text (uiout, "if ");
958 ui_out_field_fmt (uiout, NULL, "if %s", list->line);
959 ui_out_text (uiout, "\n");
960 /* The true arm. */
961 print_command_lines (uiout, list->body_list[0], depth + 1);
962
963 /* Show the false arm if it exists. */
964 if (list->body_count == 2)
965 {
966 if (depth)
967 ui_out_spaces (uiout, 2 * depth);
968 ui_out_field_string (uiout, NULL, "else");
969 ui_out_text (uiout, "else\n");
970 print_command_lines (uiout, list->body_list[1], depth + 1);
971 }
972
973 ui_out_field_string (uiout, NULL, "end");
974 if (depth)
975 ui_out_spaces (uiout, 2 * depth);
976 ui_out_text (uiout, "end\n");
977 list = list->next;
978 continue;
979 }
980
981 /* ignore illegal command type and try next */
982 list = list->next;
983 } /* while (list) */
984 }
985 #else
986 void
987 print_command_line (cmd, depth, stream)
988 struct command_line *cmd;
989 unsigned int depth;
990 struct ui_file *stream;
991 {
992 unsigned int i;
993
994 if (depth)
995 {
996 for (i = 0; i < depth; i++)
997 fputs_filtered (" ", stream);
998 }
999
1000 /* A simple command, print it and return. */
1001 if (cmd->control_type == simple_control)
1002 {
1003 fputs_filtered (cmd->line, stream);
1004 fputs_filtered ("\n", stream);
1005 return;
1006 }
1007
1008 /* loop_continue to jump to the start of a while loop, print it
1009 and return. */
1010 if (cmd->control_type == continue_control)
1011 {
1012 fputs_filtered ("loop_continue\n", stream);
1013 return;
1014 }
1015
1016 /* loop_break to break out of a while loop, print it and return. */
1017 if (cmd->control_type == break_control)
1018 {
1019 fputs_filtered ("loop_break\n", stream);
1020 return;
1021 }
1022
1023 /* A while command. Recursively print its subcommands before returning. */
1024 if (cmd->control_type == while_control)
1025 {
1026 struct command_line *list;
1027 fputs_filtered ("while ", stream);
1028 fputs_filtered (cmd->line, stream);
1029 fputs_filtered ("\n", stream);
1030 list = *cmd->body_list;
1031 while (list)
1032 {
1033 print_command_line (list, depth + 1, stream);
1034 list = list->next;
1035 }
1036 }
1037
1038 /* An if command. Recursively print both arms before returning. */
1039 if (cmd->control_type == if_control)
1040 {
1041 fputs_filtered ("if ", stream);
1042 fputs_filtered (cmd->line, stream);
1043 fputs_filtered ("\n", stream);
1044 /* The true arm. */
1045 print_command_line (cmd->body_list[0], depth + 1, stream);
1046
1047 /* Show the false arm if it exists. */
1048 if (cmd->body_count == 2)
1049 {
1050 if (depth)
1051 {
1052 for (i = 0; i < depth; i++)
1053 fputs_filtered (" ", stream);
1054 }
1055 fputs_filtered ("else\n", stream);
1056 print_command_line (cmd->body_list[1], depth + 1, stream);
1057 }
1058 if (depth)
1059 {
1060 for (i = 0; i < depth; i++)
1061 fputs_filtered (" ", stream);
1062 }
1063 fputs_filtered ("end\n", stream);
1064 }
1065 }
1066 #endif
1067
1068 /* Execute the command in CMD. */
1069
1070 enum command_control_type
1071 execute_control_command (cmd)
1072 struct command_line *cmd;
1073 {
1074 struct expression *expr;
1075 struct command_line *current;
1076 struct cleanup *old_chain = 0;
1077 value_ptr val;
1078 value_ptr val_mark;
1079 int loop;
1080 enum command_control_type ret;
1081 char *new_line;
1082
1083 switch (cmd->control_type)
1084 {
1085 case simple_control:
1086 /* A simple command, execute it and return. */
1087 new_line = insert_args (cmd->line);
1088 if (!new_line)
1089 return invalid_control;
1090 old_chain = make_cleanup ((make_cleanup_func) free_current_contents,
1091 &new_line);
1092 execute_command (new_line, 0);
1093 ret = cmd->control_type;
1094 break;
1095
1096 case continue_control:
1097 case break_control:
1098 /* Return for "continue", and "break" so we can either
1099 continue the loop at the top, or break out. */
1100 ret = cmd->control_type;
1101 break;
1102
1103 case while_control:
1104 {
1105 /* Parse the loop control expression for the while statement. */
1106 new_line = insert_args (cmd->line);
1107 if (!new_line)
1108 return invalid_control;
1109 old_chain = make_cleanup ((make_cleanup_func) free_current_contents,
1110 &new_line);
1111 expr = parse_expression (new_line);
1112 make_cleanup ((make_cleanup_func) free_current_contents, &expr);
1113
1114 ret = simple_control;
1115 loop = 1;
1116
1117 /* Keep iterating so long as the expression is true. */
1118 while (loop == 1)
1119 {
1120 int cond_result;
1121
1122 QUIT;
1123
1124 /* Evaluate the expression. */
1125 val_mark = value_mark ();
1126 val = evaluate_expression (expr);
1127 cond_result = value_true (val);
1128 value_free_to_mark (val_mark);
1129
1130 /* If the value is false, then break out of the loop. */
1131 if (!cond_result)
1132 break;
1133
1134 /* Execute the body of the while statement. */
1135 current = *cmd->body_list;
1136 while (current)
1137 {
1138 ret = execute_control_command (current);
1139
1140 /* If we got an error, or a "break" command, then stop
1141 looping. */
1142 if (ret == invalid_control || ret == break_control)
1143 {
1144 loop = 0;
1145 break;
1146 }
1147
1148 /* If we got a "continue" command, then restart the loop
1149 at this point. */
1150 if (ret == continue_control)
1151 break;
1152
1153 /* Get the next statement. */
1154 current = current->next;
1155 }
1156 }
1157
1158 /* Reset RET so that we don't recurse the break all the way down. */
1159 if (ret == break_control)
1160 ret = simple_control;
1161
1162 break;
1163 }
1164
1165 case if_control:
1166 {
1167 new_line = insert_args (cmd->line);
1168 if (!new_line)
1169 return invalid_control;
1170 old_chain = make_cleanup ((make_cleanup_func) free_current_contents,
1171 &new_line);
1172 /* Parse the conditional for the if statement. */
1173 expr = parse_expression (new_line);
1174 make_cleanup ((make_cleanup_func) free_current_contents, &expr);
1175
1176 current = NULL;
1177 ret = simple_control;
1178
1179 /* Evaluate the conditional. */
1180 val_mark = value_mark ();
1181 val = evaluate_expression (expr);
1182
1183 /* Choose which arm to take commands from based on the value of the
1184 conditional expression. */
1185 if (value_true (val))
1186 current = *cmd->body_list;
1187 else if (cmd->body_count == 2)
1188 current = *(cmd->body_list + 1);
1189 value_free_to_mark (val_mark);
1190
1191 /* Execute commands in the given arm. */
1192 while (current)
1193 {
1194 ret = execute_control_command (current);
1195
1196 /* If we got an error, get out. */
1197 if (ret != simple_control)
1198 break;
1199
1200 /* Get the next statement in the body. */
1201 current = current->next;
1202 }
1203
1204 break;
1205 }
1206
1207 default:
1208 warning ("Invalid control type in command structure.");
1209 return invalid_control;
1210 }
1211
1212 if (old_chain)
1213 do_cleanups (old_chain);
1214
1215 return ret;
1216 }
1217
1218 /* "while" command support. Executes a body of statements while the
1219 loop condition is nonzero. */
1220
1221 static void
1222 while_command (arg, from_tty)
1223 char *arg;
1224 int from_tty;
1225 {
1226 struct command_line *command = NULL;
1227
1228 control_level = 1;
1229 command = get_command_line (while_control, arg);
1230
1231 if (command == NULL)
1232 return;
1233
1234 execute_control_command (command);
1235 free_command_lines (&command);
1236 }
1237
1238 /* "if" command support. Execute either the true or false arm depending
1239 on the value of the if conditional. */
1240
1241 static void
1242 if_command (arg, from_tty)
1243 char *arg;
1244 int from_tty;
1245 {
1246 struct command_line *command = NULL;
1247
1248 control_level = 1;
1249 command = get_command_line (if_control, arg);
1250
1251 if (command == NULL)
1252 return;
1253
1254 execute_control_command (command);
1255 free_command_lines (&command);
1256 }
1257
1258 /* Cleanup */
1259 static void
1260 arg_cleanup ()
1261 {
1262 struct user_args *oargs = user_args;
1263 if (!user_args)
1264 internal_error ("Internal error, arg_cleanup called with no user args.\n");
1265
1266 user_args = user_args->next;
1267 free (oargs);
1268 }
1269
1270 /* Bind the incomming arguments for a user defined command to
1271 $arg0, $arg1 ... $argMAXUSERARGS. */
1272
1273 static struct cleanup *
1274 setup_user_args (p)
1275 char *p;
1276 {
1277 struct user_args *args;
1278 struct cleanup *old_chain;
1279 unsigned int arg_count = 0;
1280
1281 args = (struct user_args *) xmalloc (sizeof (struct user_args));
1282 memset (args, 0, sizeof (struct user_args));
1283
1284 args->next = user_args;
1285 user_args = args;
1286
1287 old_chain = make_cleanup ((make_cleanup_func) arg_cleanup, 0);
1288
1289 if (p == NULL)
1290 return old_chain;
1291
1292 while (*p)
1293 {
1294 char *start_arg;
1295 int squote = 0;
1296 int dquote = 0;
1297 int bsquote = 0;
1298
1299 if (arg_count >= MAXUSERARGS)
1300 {
1301 error ("user defined function may only have %d arguments.\n",
1302 MAXUSERARGS);
1303 return old_chain;
1304 }
1305
1306 /* Strip whitespace. */
1307 while (*p == ' ' || *p == '\t')
1308 p++;
1309
1310 /* P now points to an argument. */
1311 start_arg = p;
1312 user_args->a[arg_count].arg = p;
1313
1314 /* Get to the end of this argument. */
1315 while (*p)
1316 {
1317 if (((*p == ' ' || *p == '\t')) && !squote && !dquote && !bsquote)
1318 break;
1319 else
1320 {
1321 if (bsquote)
1322 bsquote = 0;
1323 else if (*p == '\\')
1324 bsquote = 1;
1325 else if (squote)
1326 {
1327 if (*p == '\'')
1328 squote = 0;
1329 }
1330 else if (dquote)
1331 {
1332 if (*p == '"')
1333 dquote = 0;
1334 }
1335 else
1336 {
1337 if (*p == '\'')
1338 squote = 1;
1339 else if (*p == '"')
1340 dquote = 1;
1341 }
1342 p++;
1343 }
1344 }
1345
1346 user_args->a[arg_count].len = p - start_arg;
1347 arg_count++;
1348 user_args->count++;
1349 }
1350 return old_chain;
1351 }
1352
1353 /* Given character string P, return a point to the first argument ($arg),
1354 or NULL if P contains no arguments. */
1355
1356 static char *
1357 locate_arg (p)
1358 char *p;
1359 {
1360 while ((p = strchr (p, '$')))
1361 {
1362 if (strncmp (p, "$arg", 4) == 0 && isdigit (p[4]))
1363 return p;
1364 p++;
1365 }
1366 return NULL;
1367 }
1368
1369 /* Insert the user defined arguments stored in user_arg into the $arg
1370 arguments found in line, with the updated copy being placed into nline. */
1371
1372 static char *
1373 insert_args (line)
1374 char *line;
1375 {
1376 char *p, *save_line, *new_line;
1377 unsigned len, i;
1378
1379 /* First we need to know how much memory to allocate for the new line. */
1380 save_line = line;
1381 len = 0;
1382 while ((p = locate_arg (line)))
1383 {
1384 len += p - line;
1385 i = p[4] - '0';
1386
1387 if (i >= user_args->count)
1388 {
1389 error ("Missing argument %d in user function.\n", i);
1390 return NULL;
1391 }
1392 len += user_args->a[i].len;
1393 line = p + 5;
1394 }
1395
1396 /* Don't forget the tail. */
1397 len += strlen (line);
1398
1399 /* Allocate space for the new line and fill it in. */
1400 new_line = (char *) xmalloc (len + 1);
1401 if (new_line == NULL)
1402 return NULL;
1403
1404 /* Restore pointer to beginning of old line. */
1405 line = save_line;
1406
1407 /* Save pointer to beginning of new line. */
1408 save_line = new_line;
1409
1410 while ((p = locate_arg (line)))
1411 {
1412 int i, len;
1413
1414 memcpy (new_line, line, p - line);
1415 new_line += p - line;
1416 i = p[4] - '0';
1417
1418 len = user_args->a[i].len;
1419 if (len)
1420 {
1421 memcpy (new_line, user_args->a[i].arg, len);
1422 new_line += len;
1423 }
1424 line = p + 5;
1425 }
1426 /* Don't forget the tail. */
1427 strcpy (new_line, line);
1428
1429 /* Return a pointer to the beginning of the new line. */
1430 return save_line;
1431 }
1432
1433 void
1434 execute_user_command (c, args)
1435 struct cmd_list_element *c;
1436 char *args;
1437 {
1438 register struct command_line *cmdlines;
1439 struct cleanup *old_chain;
1440 enum command_control_type ret;
1441
1442 old_chain = setup_user_args (args);
1443
1444 cmdlines = c->user_commands;
1445 if (cmdlines == 0)
1446 /* Null command */
1447 return;
1448
1449 /* Set the instream to 0, indicating execution of a
1450 user-defined function. */
1451 old_chain = make_cleanup ((make_cleanup_func) source_cleanup, instream);
1452 instream = (FILE *) 0;
1453 while (cmdlines)
1454 {
1455 ret = execute_control_command (cmdlines);
1456 if (ret != simple_control && ret != break_control)
1457 {
1458 warning ("Error in control structure.\n");
1459 break;
1460 }
1461 cmdlines = cmdlines->next;
1462 }
1463 do_cleanups (old_chain);
1464 }
1465
1466 /* Execute the line P as a command.
1467 Pass FROM_TTY as second argument to the defining function. */
1468
1469 void
1470 execute_command (p, from_tty)
1471 char *p;
1472 int from_tty;
1473 {
1474 register struct cmd_list_element *c;
1475 register enum language flang;
1476 static int warned = 0;
1477 char *line;
1478 /* FIXME: These should really be in an appropriate header file */
1479 extern void serial_log_command PARAMS ((const char *));
1480
1481 free_all_values ();
1482
1483 /* Force cleanup of any alloca areas if using C alloca instead of
1484 a builtin alloca. */
1485 alloca (0);
1486
1487 /* This can happen when command_line_input hits end of file. */
1488 if (p == NULL)
1489 return;
1490
1491 serial_log_command (p);
1492
1493 while (*p == ' ' || *p == '\t')
1494 p++;
1495 if (*p)
1496 {
1497 char *arg;
1498 line = p;
1499
1500 c = lookup_cmd (&p, cmdlist, "", 0, 1);
1501
1502 /* If the target is running, we allow only a limited set of
1503 commands. */
1504 if (event_loop_p && target_can_async_p () && target_executing)
1505 if (!strcmp (c->name, "help")
1506 && !strcmp (c->name, "pwd")
1507 && !strcmp (c->name, "show")
1508 && !strcmp (c->name, "stop"))
1509 error ("Cannot execute this command while the target is running.");
1510
1511 /* Pass null arg rather than an empty one. */
1512 arg = *p ? p : 0;
1513
1514 /* Clear off trailing whitespace, except for set and complete command. */
1515 if (arg && c->type != set_cmd && c->function.cfunc != complete_command)
1516 {
1517 p = arg + strlen (arg) - 1;
1518 while (p >= arg && (*p == ' ' || *p == '\t'))
1519 p--;
1520 *(p + 1) = '\0';
1521 }
1522
1523 /* If this command has been hooked, run the hook first. */
1524 if (c->hook)
1525 execute_user_command (c->hook, (char *) 0);
1526
1527 if (c->flags & DEPRECATED_WARN_USER)
1528 deprecated_cmd_warning (&line);
1529
1530 if (c->class == class_user)
1531 execute_user_command (c, arg);
1532 else if (c->type == set_cmd || c->type == show_cmd)
1533 do_setshow_command (arg, from_tty & caution, c);
1534 else if (c->function.cfunc == NO_FUNCTION)
1535 error ("That is not a command, just a help topic.");
1536 else if (call_command_hook)
1537 call_command_hook (c, arg, from_tty & caution);
1538 else
1539 (*c->function.cfunc) (arg, from_tty & caution);
1540 }
1541
1542 /* Tell the user if the language has changed (except first time). */
1543 if (current_language != expected_language)
1544 {
1545 if (language_mode == language_mode_auto)
1546 {
1547 language_info (1); /* Print what changed. */
1548 }
1549 warned = 0;
1550 }
1551
1552 /* Warn the user if the working language does not match the
1553 language of the current frame. Only warn the user if we are
1554 actually running the program, i.e. there is a stack. */
1555 /* FIXME: This should be cacheing the frame and only running when
1556 the frame changes. */
1557
1558 if (target_has_stack)
1559 {
1560 flang = get_frame_language ();
1561 if (!warned
1562 && flang != language_unknown
1563 && flang != current_language->la_language)
1564 {
1565 printf_filtered ("%s\n", lang_frame_mismatch_warn);
1566 warned = 1;
1567 }
1568 }
1569 }
1570
1571 /* ARGSUSED */
1572 /* NOTE 1999-04-29: This function will be static again, once we modify
1573 gdb to use the event loop as the default command loop and we merge
1574 event-top.c into this file, top.c */
1575 /* static */ void
1576 command_loop_marker (foo)
1577 int foo;
1578 {
1579 }
1580
1581 /* Read commands from `instream' and execute them
1582 until end of file or error reading instream. */
1583
1584 void
1585 command_loop ()
1586 {
1587 struct cleanup *old_chain;
1588 char *command;
1589 int stdin_is_tty = ISATTY (stdin);
1590 long time_at_cmd_start;
1591 #ifdef HAVE_SBRK
1592 long space_at_cmd_start = 0;
1593 #endif
1594 extern int display_time;
1595 extern int display_space;
1596
1597 while (instream && !feof (instream))
1598 {
1599 #if defined(TUI)
1600 extern int insert_mode;
1601 #endif
1602 if (window_hook && instream == stdin)
1603 (*window_hook) (instream, get_prompt ());
1604
1605 quit_flag = 0;
1606 if (instream == stdin && stdin_is_tty)
1607 reinitialize_more_filter ();
1608 old_chain = make_cleanup ((make_cleanup_func) command_loop_marker, 0);
1609
1610 #if defined(TUI)
1611 /* A bit of paranoia: I want to make sure the "insert_mode" global
1612 * is clear except when it is being used for command-line editing
1613 * (see tuiIO.c, utils.c); otherwise normal output will
1614 * get messed up in the TUI. So clear it before/after
1615 * the command-line-input call. - RT
1616 */
1617 insert_mode = 0;
1618 #endif
1619 /* Get a command-line. This calls the readline package. */
1620 command = command_line_input (instream == stdin ?
1621 get_prompt () : (char *) NULL,
1622 instream == stdin, "prompt");
1623 #if defined(TUI)
1624 insert_mode = 0;
1625 #endif
1626 if (command == 0)
1627 return;
1628
1629 time_at_cmd_start = get_run_time ();
1630
1631 if (display_space)
1632 {
1633 #ifdef HAVE_SBRK
1634 extern char **environ;
1635 char *lim = (char *) sbrk (0);
1636
1637 space_at_cmd_start = (long) (lim - (char *) &environ);
1638 #endif
1639 }
1640
1641 execute_command (command, instream == stdin);
1642 /* Do any commands attached to breakpoint we stopped at. */
1643 bpstat_do_actions (&stop_bpstat);
1644 do_cleanups (old_chain);
1645
1646 if (display_time)
1647 {
1648 long cmd_time = get_run_time () - time_at_cmd_start;
1649
1650 printf_unfiltered ("Command execution time: %ld.%06ld\n",
1651 cmd_time / 1000000, cmd_time % 1000000);
1652 }
1653
1654 if (display_space)
1655 {
1656 #ifdef HAVE_SBRK
1657 extern char **environ;
1658 char *lim = (char *) sbrk (0);
1659 long space_now = lim - (char *) &environ;
1660 long space_diff = space_now - space_at_cmd_start;
1661
1662 printf_unfiltered ("Space used: %ld (%c%ld for this command)\n",
1663 space_now,
1664 (space_diff >= 0 ? '+' : '-'),
1665 space_diff);
1666 #endif
1667 }
1668 }
1669 }
1670
1671 /* Read commands from `instream' and execute them until end of file or
1672 error reading instream. This command loop doesnt care about any
1673 such things as displaying time and space usage. If the user asks
1674 for those, they won't work. */
1675 void
1676 simplified_command_loop (read_input_func, execute_command_func)
1677 char *(*read_input_func) (char *);
1678 void (*execute_command_func) (char *, int);
1679 {
1680 struct cleanup *old_chain;
1681 char *command;
1682 int stdin_is_tty = ISATTY (stdin);
1683
1684 while (instream && !feof (instream))
1685 {
1686 quit_flag = 0;
1687 if (instream == stdin && stdin_is_tty)
1688 reinitialize_more_filter ();
1689 old_chain = make_cleanup ((make_cleanup_func) command_loop_marker, 0);
1690
1691 /* Get a command-line. */
1692 command = (*read_input_func) (instream == stdin ?
1693 get_prompt () : (char *) NULL);
1694
1695 if (command == 0)
1696 return;
1697
1698 (*execute_command_func) (command, instream == stdin);
1699
1700 /* Do any commands attached to breakpoint we stopped at. */
1701 bpstat_do_actions (&stop_bpstat);
1702
1703 do_cleanups (old_chain);
1704 }
1705 }
1706 \f
1707 /* Commands call this if they do not want to be repeated by null lines. */
1708
1709 void
1710 dont_repeat ()
1711 {
1712 if (server_command)
1713 return;
1714
1715 /* If we aren't reading from standard input, we are saving the last
1716 thing read from stdin in line and don't want to delete it. Null lines
1717 won't repeat here in any case. */
1718 if (instream == stdin)
1719 *line = 0;
1720 }
1721 \f
1722 /* Read a line from the stream "instream" without command line editing.
1723
1724 It prints PROMPT_ARG once at the start.
1725 Action is compatible with "readline", e.g. space for the result is
1726 malloc'd and should be freed by the caller.
1727
1728 A NULL return means end of file. */
1729 char *
1730 gdb_readline (prompt_arg)
1731 char *prompt_arg;
1732 {
1733 int c;
1734 char *result;
1735 int input_index = 0;
1736 int result_size = 80;
1737
1738 if (prompt_arg)
1739 {
1740 /* Don't use a _filtered function here. It causes the assumed
1741 character position to be off, since the newline we read from
1742 the user is not accounted for. */
1743 fputs_unfiltered (prompt_arg, gdb_stdout);
1744 #ifdef MPW
1745 /* Move to a new line so the entered line doesn't have a prompt
1746 on the front of it. */
1747 fputs_unfiltered ("\n", gdb_stdout);
1748 #endif /* MPW */
1749 gdb_flush (gdb_stdout);
1750 }
1751
1752 result = (char *) xmalloc (result_size);
1753
1754 while (1)
1755 {
1756 /* Read from stdin if we are executing a user defined command.
1757 This is the right thing for prompt_for_continue, at least. */
1758 c = fgetc (instream ? instream : stdin);
1759
1760 if (c == EOF)
1761 {
1762 if (input_index > 0)
1763 /* The last line does not end with a newline. Return it, and
1764 if we are called again fgetc will still return EOF and
1765 we'll return NULL then. */
1766 break;
1767 free (result);
1768 return NULL;
1769 }
1770
1771 if (c == '\n')
1772 #ifndef CRLF_SOURCE_FILES
1773 break;
1774 #else
1775 {
1776 if (input_index > 0 && result[input_index - 1] == '\r')
1777 input_index--;
1778 break;
1779 }
1780 #endif
1781
1782 result[input_index++] = c;
1783 while (input_index >= result_size)
1784 {
1785 result_size *= 2;
1786 result = (char *) xrealloc (result, result_size);
1787 }
1788 }
1789
1790 result[input_index++] = '\0';
1791 return result;
1792 }
1793
1794 /* Variables which control command line editing and history
1795 substitution. These variables are given default values at the end
1796 of this file. */
1797 static int command_editing_p;
1798 /* NOTE 1999-04-29: This variable will be static again, once we modify
1799 gdb to use the event loop as the default command loop and we merge
1800 event-top.c into this file, top.c */
1801 /* static */ int history_expansion_p;
1802 static int write_history_p;
1803 static int history_size;
1804 static char *history_filename;
1805
1806 /* readline uses the word breaks for two things:
1807 (1) In figuring out where to point the TEXT parameter to the
1808 rl_completion_entry_function. Since we don't use TEXT for much,
1809 it doesn't matter a lot what the word breaks are for this purpose, but
1810 it does affect how much stuff M-? lists.
1811 (2) If one of the matches contains a word break character, readline
1812 will quote it. That's why we switch between
1813 gdb_completer_word_break_characters and
1814 gdb_completer_command_word_break_characters. I'm not sure when
1815 we need this behavior (perhaps for funky characters in C++ symbols?). */
1816
1817 /* Variables which are necessary for fancy command line editing. */
1818 char *gdb_completer_word_break_characters =
1819 " \t\n!@#$%^&*()+=|~`}{[]\"';:?/>.<,-";
1820
1821 /* When completing on command names, we remove '-' from the list of
1822 word break characters, since we use it in command names. If the
1823 readline library sees one in any of the current completion strings,
1824 it thinks that the string needs to be quoted and automatically supplies
1825 a leading quote. */
1826 char *gdb_completer_command_word_break_characters =
1827 " \t\n!@#$%^&*()+=|~`}{[]\"';:?/>.<,";
1828
1829 /* Characters that can be used to quote completion strings. Note that we
1830 can't include '"' because the gdb C parser treats such quoted sequences
1831 as strings. */
1832 char *gdb_completer_quote_characters =
1833 "'";
1834
1835 /* Functions that are used as part of the fancy command line editing. */
1836
1837 /* This can be used for functions which don't want to complete on symbols
1838 but don't want to complete on anything else either. */
1839 /* ARGSUSED */
1840 char **
1841 noop_completer (text, prefix)
1842 char *text;
1843 char *prefix;
1844 {
1845 return NULL;
1846 }
1847
1848 /* Complete on filenames. */
1849 char **
1850 filename_completer (text, word)
1851 char *text;
1852 char *word;
1853 {
1854 /* From readline. */
1855 extern char *filename_completion_function PARAMS ((char *, int));
1856 int subsequent_name;
1857 char **return_val;
1858 int return_val_used;
1859 int return_val_alloced;
1860
1861 return_val_used = 0;
1862 /* Small for testing. */
1863 return_val_alloced = 1;
1864 return_val = (char **) xmalloc (return_val_alloced * sizeof (char *));
1865
1866 subsequent_name = 0;
1867 while (1)
1868 {
1869 char *p;
1870 p = filename_completion_function (text, subsequent_name);
1871 if (return_val_used >= return_val_alloced)
1872 {
1873 return_val_alloced *= 2;
1874 return_val =
1875 (char **) xrealloc (return_val,
1876 return_val_alloced * sizeof (char *));
1877 }
1878 if (p == NULL)
1879 {
1880 return_val[return_val_used++] = p;
1881 break;
1882 }
1883 /* Like emacs, don't complete on old versions. Especially useful
1884 in the "source" command. */
1885 if (p[strlen (p) - 1] == '~')
1886 continue;
1887
1888 {
1889 char *q;
1890 if (word == text)
1891 /* Return exactly p. */
1892 return_val[return_val_used++] = p;
1893 else if (word > text)
1894 {
1895 /* Return some portion of p. */
1896 q = xmalloc (strlen (p) + 5);
1897 strcpy (q, p + (word - text));
1898 return_val[return_val_used++] = q;
1899 free (p);
1900 }
1901 else
1902 {
1903 /* Return some of TEXT plus p. */
1904 q = xmalloc (strlen (p) + (text - word) + 5);
1905 strncpy (q, word, text - word);
1906 q[text - word] = '\0';
1907 strcat (q, p);
1908 return_val[return_val_used++] = q;
1909 free (p);
1910 }
1911 }
1912 subsequent_name = 1;
1913 }
1914 #if 0
1915 /* There is no way to do this just long enough to affect quote inserting
1916 without also affecting the next completion. This should be fixed in
1917 readline. FIXME. */
1918 /* Insure that readline does the right thing
1919 with respect to inserting quotes. */
1920 rl_completer_word_break_characters = "";
1921 #endif
1922 return return_val;
1923 }
1924
1925 /* Here are some useful test cases for completion. FIXME: These should
1926 be put in the test suite. They should be tested with both M-? and TAB.
1927
1928 "show output-" "radix"
1929 "show output" "-radix"
1930 "p" ambiguous (commands starting with p--path, print, printf, etc.)
1931 "p " ambiguous (all symbols)
1932 "info t foo" no completions
1933 "info t " no completions
1934 "info t" ambiguous ("info target", "info terminal", etc.)
1935 "info ajksdlfk" no completions
1936 "info ajksdlfk " no completions
1937 "info" " "
1938 "info " ambiguous (all info commands)
1939 "p \"a" no completions (string constant)
1940 "p 'a" ambiguous (all symbols starting with a)
1941 "p b-a" ambiguous (all symbols starting with a)
1942 "p b-" ambiguous (all symbols)
1943 "file Make" "file" (word break hard to screw up here)
1944 "file ../gdb.stabs/we" "ird" (needs to not break word at slash)
1945 */
1946
1947 /* Generate completions one by one for the completer. Each time we are
1948 called return another potential completion to the caller.
1949 line_completion just completes on commands or passes the buck to the
1950 command's completer function, the stuff specific to symbol completion
1951 is in make_symbol_completion_list.
1952
1953 TEXT is the caller's idea of the "word" we are looking at.
1954
1955 MATCHES is the number of matches that have currently been collected from
1956 calling this completion function. When zero, then we need to initialize,
1957 otherwise the initialization has already taken place and we can just
1958 return the next potential completion string.
1959
1960 LINE_BUFFER is available to be looked at; it contains the entire text
1961 of the line. POINT is the offset in that line of the cursor. You
1962 should pretend that the line ends at POINT.
1963
1964 Returns NULL if there are no more completions, else a pointer to a string
1965 which is a possible completion, it is the caller's responsibility to
1966 free the string. */
1967
1968 static char *
1969 line_completion_function (text, matches, line_buffer, point)
1970 char *text;
1971 int matches;
1972 char *line_buffer;
1973 int point;
1974 {
1975 static char **list = (char **) NULL; /* Cache of completions */
1976 static int index; /* Next cached completion */
1977 char *output = NULL;
1978 char *tmp_command, *p;
1979 /* Pointer within tmp_command which corresponds to text. */
1980 char *word;
1981 struct cmd_list_element *c, *result_list;
1982
1983 if (matches == 0)
1984 {
1985 /* The caller is beginning to accumulate a new set of completions, so
1986 we need to find all of them now, and cache them for returning one at
1987 a time on future calls. */
1988
1989 if (list)
1990 {
1991 /* Free the storage used by LIST, but not by the strings inside.
1992 This is because rl_complete_internal () frees the strings. */
1993 free ((PTR) list);
1994 }
1995 list = 0;
1996 index = 0;
1997
1998 /* Choose the default set of word break characters to break completions.
1999 If we later find out that we are doing completions on command strings
2000 (as opposed to strings supplied by the individual command completer
2001 functions, which can be any string) then we will switch to the
2002 special word break set for command strings, which leaves out the
2003 '-' character used in some commands. */
2004
2005 rl_completer_word_break_characters =
2006 gdb_completer_word_break_characters;
2007
2008 /* Decide whether to complete on a list of gdb commands or on symbols. */
2009 tmp_command = (char *) alloca (point + 1);
2010 p = tmp_command;
2011
2012 strncpy (tmp_command, line_buffer, point);
2013 tmp_command[point] = '\0';
2014 /* Since text always contains some number of characters leading up
2015 to point, we can find the equivalent position in tmp_command
2016 by subtracting that many characters from the end of tmp_command. */
2017 word = tmp_command + point - strlen (text);
2018
2019 if (point == 0)
2020 {
2021 /* An empty line we want to consider ambiguous; that is, it
2022 could be any command. */
2023 c = (struct cmd_list_element *) -1;
2024 result_list = 0;
2025 }
2026 else
2027 {
2028 c = lookup_cmd_1 (&p, cmdlist, &result_list, 1);
2029 }
2030
2031 /* Move p up to the next interesting thing. */
2032 while (*p == ' ' || *p == '\t')
2033 {
2034 p++;
2035 }
2036
2037 if (!c)
2038 {
2039 /* It is an unrecognized command. So there are no
2040 possible completions. */
2041 list = NULL;
2042 }
2043 else if (c == (struct cmd_list_element *) -1)
2044 {
2045 char *q;
2046
2047 /* lookup_cmd_1 advances p up to the first ambiguous thing, but
2048 doesn't advance over that thing itself. Do so now. */
2049 q = p;
2050 while (*q && (isalnum (*q) || *q == '-' || *q == '_'))
2051 ++q;
2052 if (q != tmp_command + point)
2053 {
2054 /* There is something beyond the ambiguous
2055 command, so there are no possible completions. For
2056 example, "info t " or "info t foo" does not complete
2057 to anything, because "info t" can be "info target" or
2058 "info terminal". */
2059 list = NULL;
2060 }
2061 else
2062 {
2063 /* We're trying to complete on the command which was ambiguous.
2064 This we can deal with. */
2065 if (result_list)
2066 {
2067 list = complete_on_cmdlist (*result_list->prefixlist, p,
2068 word);
2069 }
2070 else
2071 {
2072 list = complete_on_cmdlist (cmdlist, p, word);
2073 }
2074 /* Insure that readline does the right thing with respect to
2075 inserting quotes. */
2076 rl_completer_word_break_characters =
2077 gdb_completer_command_word_break_characters;
2078 }
2079 }
2080 else
2081 {
2082 /* We've recognized a full command. */
2083
2084 if (p == tmp_command + point)
2085 {
2086 /* There is no non-whitespace in the line beyond the command. */
2087
2088 if (p[-1] == ' ' || p[-1] == '\t')
2089 {
2090 /* The command is followed by whitespace; we need to complete
2091 on whatever comes after command. */
2092 if (c->prefixlist)
2093 {
2094 /* It is a prefix command; what comes after it is
2095 a subcommand (e.g. "info "). */
2096 list = complete_on_cmdlist (*c->prefixlist, p, word);
2097
2098 /* Insure that readline does the right thing
2099 with respect to inserting quotes. */
2100 rl_completer_word_break_characters =
2101 gdb_completer_command_word_break_characters;
2102 }
2103 else if (c->enums)
2104 {
2105 list = complete_on_enum (c->enums, p, word);
2106 rl_completer_word_break_characters =
2107 gdb_completer_command_word_break_characters;
2108 }
2109 else
2110 {
2111 /* It is a normal command; what comes after it is
2112 completed by the command's completer function. */
2113 list = (*c->completer) (p, word);
2114 }
2115 }
2116 else
2117 {
2118 /* The command is not followed by whitespace; we need to
2119 complete on the command itself. e.g. "p" which is a
2120 command itself but also can complete to "print", "ptype"
2121 etc. */
2122 char *q;
2123
2124 /* Find the command we are completing on. */
2125 q = p;
2126 while (q > tmp_command)
2127 {
2128 if (isalnum (q[-1]) || q[-1] == '-' || q[-1] == '_')
2129 --q;
2130 else
2131 break;
2132 }
2133
2134 list = complete_on_cmdlist (result_list, q, word);
2135
2136 /* Insure that readline does the right thing
2137 with respect to inserting quotes. */
2138 rl_completer_word_break_characters =
2139 gdb_completer_command_word_break_characters;
2140 }
2141 }
2142 else
2143 {
2144 /* There is non-whitespace beyond the command. */
2145
2146 if (c->prefixlist && !c->allow_unknown)
2147 {
2148 /* It is an unrecognized subcommand of a prefix command,
2149 e.g. "info adsfkdj". */
2150 list = NULL;
2151 }
2152 else if (c->enums)
2153 {
2154 list = complete_on_enum (c->enums, p, word);
2155 }
2156 else
2157 {
2158 /* It is a normal command. */
2159 list = (*c->completer) (p, word);
2160 }
2161 }
2162 }
2163 }
2164
2165 /* If we found a list of potential completions during initialization then
2166 dole them out one at a time. The vector of completions is NULL
2167 terminated, so after returning the last one, return NULL (and continue
2168 to do so) each time we are called after that, until a new list is
2169 available. */
2170
2171 if (list)
2172 {
2173 output = list[index];
2174 if (output)
2175 {
2176 index++;
2177 }
2178 }
2179
2180 #if 0
2181 /* Can't do this because readline hasn't yet checked the word breaks
2182 for figuring out whether to insert a quote. */
2183 if (output == NULL)
2184 /* Make sure the word break characters are set back to normal for the
2185 next time that readline tries to complete something. */
2186 rl_completer_word_break_characters =
2187 gdb_completer_word_break_characters;
2188 #endif
2189
2190 return (output);
2191 }
2192
2193 /* Line completion interface function for readline. */
2194
2195 static char *
2196 readline_line_completion_function (text, matches)
2197 char *text;
2198 int matches;
2199 {
2200 return line_completion_function (text, matches, rl_line_buffer, rl_point);
2201 }
2202
2203 /* Skip over a possibly quoted word (as defined by the quote characters
2204 and word break characters the completer uses). Returns pointer to the
2205 location after the "word". */
2206
2207 char *
2208 skip_quoted (str)
2209 char *str;
2210 {
2211 char quote_char = '\0';
2212 char *scan;
2213
2214 for (scan = str; *scan != '\0'; scan++)
2215 {
2216 if (quote_char != '\0')
2217 {
2218 /* Ignore everything until the matching close quote char */
2219 if (*scan == quote_char)
2220 {
2221 /* Found matching close quote. */
2222 scan++;
2223 break;
2224 }
2225 }
2226 else if (strchr (gdb_completer_quote_characters, *scan))
2227 {
2228 /* Found start of a quoted string. */
2229 quote_char = *scan;
2230 }
2231 else if (strchr (gdb_completer_word_break_characters, *scan))
2232 {
2233 break;
2234 }
2235 }
2236 return (scan);
2237 }
2238 \f
2239
2240 #ifdef STOP_SIGNAL
2241 static void
2242 stop_sig (signo)
2243 int signo;
2244 {
2245 #if STOP_SIGNAL == SIGTSTP
2246 signal (SIGTSTP, SIG_DFL);
2247 sigsetmask (0);
2248 kill (getpid (), SIGTSTP);
2249 signal (SIGTSTP, stop_sig);
2250 #else
2251 signal (STOP_SIGNAL, stop_sig);
2252 #endif
2253 printf_unfiltered ("%s", get_prompt ());
2254 gdb_flush (gdb_stdout);
2255
2256 /* Forget about any previous command -- null line now will do nothing. */
2257 dont_repeat ();
2258 }
2259 #endif /* STOP_SIGNAL */
2260
2261 /* Initialize signal handlers. */
2262 static void
2263 do_nothing (signo)
2264 int signo;
2265 {
2266 /* Under System V the default disposition of a signal is reinstated after
2267 the signal is caught and delivered to an application process. On such
2268 systems one must restore the replacement signal handler if one wishes
2269 to continue handling the signal in one's program. On BSD systems this
2270 is not needed but it is harmless, and it simplifies the code to just do
2271 it unconditionally. */
2272 signal (signo, do_nothing);
2273 }
2274
2275 static void
2276 init_signals ()
2277 {
2278 signal (SIGINT, request_quit);
2279
2280 /* If SIGTRAP was set to SIG_IGN, then the SIG_IGN will get passed
2281 to the inferior and breakpoints will be ignored. */
2282 #ifdef SIGTRAP
2283 signal (SIGTRAP, SIG_DFL);
2284 #endif
2285
2286 /* If we initialize SIGQUIT to SIG_IGN, then the SIG_IGN will get
2287 passed to the inferior, which we don't want. It would be
2288 possible to do a "signal (SIGQUIT, SIG_DFL)" after we fork, but
2289 on BSD4.3 systems using vfork, that can affect the
2290 GDB process as well as the inferior (the signal handling tables
2291 might be in memory, shared between the two). Since we establish
2292 a handler for SIGQUIT, when we call exec it will set the signal
2293 to SIG_DFL for us. */
2294 signal (SIGQUIT, do_nothing);
2295 #ifdef SIGHUP
2296 if (signal (SIGHUP, do_nothing) != SIG_IGN)
2297 signal (SIGHUP, disconnect);
2298 #endif
2299 signal (SIGFPE, float_handler);
2300
2301 #if defined(SIGWINCH) && defined(SIGWINCH_HANDLER)
2302 signal (SIGWINCH, SIGWINCH_HANDLER);
2303 #endif
2304 }
2305 \f
2306 /* Read one line from the command input stream `instream'
2307 into the local static buffer `linebuffer' (whose current length
2308 is `linelength').
2309 The buffer is made bigger as necessary.
2310 Returns the address of the start of the line.
2311
2312 NULL is returned for end of file.
2313
2314 *If* the instream == stdin & stdin is a terminal, the line read
2315 is copied into the file line saver (global var char *line,
2316 length linesize) so that it can be duplicated.
2317
2318 This routine either uses fancy command line editing or
2319 simple input as the user has requested. */
2320
2321 char *
2322 command_line_input (prompt_arg, repeat, annotation_suffix)
2323 char *prompt_arg;
2324 int repeat;
2325 char *annotation_suffix;
2326 {
2327 static char *linebuffer = 0;
2328 static unsigned linelength = 0;
2329 register char *p;
2330 char *p1;
2331 char *rl;
2332 char *local_prompt = prompt_arg;
2333 char *nline;
2334 char got_eof = 0;
2335
2336 /* The annotation suffix must be non-NULL. */
2337 if (annotation_suffix == NULL)
2338 annotation_suffix = "";
2339
2340 if (annotation_level > 1 && instream == stdin)
2341 {
2342 local_prompt = alloca ((prompt_arg == NULL ? 0 : strlen (prompt_arg))
2343 + strlen (annotation_suffix) + 40);
2344 if (prompt_arg == NULL)
2345 local_prompt[0] = '\0';
2346 else
2347 strcpy (local_prompt, prompt_arg);
2348 strcat (local_prompt, "\n\032\032");
2349 strcat (local_prompt, annotation_suffix);
2350 strcat (local_prompt, "\n");
2351 }
2352
2353 if (linebuffer == 0)
2354 {
2355 linelength = 80;
2356 linebuffer = (char *) xmalloc (linelength);
2357 }
2358
2359 p = linebuffer;
2360
2361 /* Control-C quits instantly if typed while in this loop
2362 since it should not wait until the user types a newline. */
2363 immediate_quit++;
2364 #ifdef STOP_SIGNAL
2365 if (job_control)
2366 {
2367 if (event_loop_p)
2368 signal (STOP_SIGNAL, handle_stop_sig);
2369 else
2370 signal (STOP_SIGNAL, stop_sig);
2371 }
2372 #endif
2373
2374 while (1)
2375 {
2376 /* Make sure that all output has been output. Some machines may let
2377 you get away with leaving out some of the gdb_flush, but not all. */
2378 wrap_here ("");
2379 gdb_flush (gdb_stdout);
2380 gdb_flush (gdb_stderr);
2381
2382 if (source_file_name != NULL)
2383 {
2384 ++source_line_number;
2385 sprintf (source_error,
2386 "%s%s:%d: Error in sourced command file:\n",
2387 source_pre_error,
2388 source_file_name,
2389 source_line_number);
2390 error_pre_print = source_error;
2391 }
2392
2393 if (annotation_level > 1 && instream == stdin)
2394 {
2395 printf_unfiltered ("\n\032\032pre-");
2396 printf_unfiltered (annotation_suffix);
2397 printf_unfiltered ("\n");
2398 }
2399
2400 /* Don't use fancy stuff if not talking to stdin. */
2401 if (readline_hook && instream == NULL)
2402 {
2403 rl = (*readline_hook) (local_prompt);
2404 }
2405 else if (command_editing_p && instream == stdin && ISATTY (instream))
2406 {
2407 rl = readline (local_prompt);
2408 }
2409 else
2410 {
2411 rl = gdb_readline (local_prompt);
2412 }
2413
2414 if (annotation_level > 1 && instream == stdin)
2415 {
2416 printf_unfiltered ("\n\032\032post-");
2417 printf_unfiltered (annotation_suffix);
2418 printf_unfiltered ("\n");
2419 }
2420
2421 if (!rl || rl == (char *) EOF)
2422 {
2423 got_eof = 1;
2424 break;
2425 }
2426 if (strlen (rl) + 1 + (p - linebuffer) > linelength)
2427 {
2428 linelength = strlen (rl) + 1 + (p - linebuffer);
2429 nline = (char *) xrealloc (linebuffer, linelength);
2430 p += nline - linebuffer;
2431 linebuffer = nline;
2432 }
2433 p1 = rl;
2434 /* Copy line. Don't copy null at end. (Leaves line alone
2435 if this was just a newline) */
2436 while (*p1)
2437 *p++ = *p1++;
2438
2439 free (rl); /* Allocated in readline. */
2440
2441 if (p == linebuffer || *(p - 1) != '\\')
2442 break;
2443
2444 p--; /* Put on top of '\'. */
2445 local_prompt = (char *) 0;
2446 }
2447
2448 #ifdef STOP_SIGNAL
2449 if (job_control)
2450 signal (STOP_SIGNAL, SIG_DFL);
2451 #endif
2452 immediate_quit--;
2453
2454 if (got_eof)
2455 return NULL;
2456
2457 #define SERVER_COMMAND_LENGTH 7
2458 server_command =
2459 (p - linebuffer > SERVER_COMMAND_LENGTH)
2460 && STREQN (linebuffer, "server ", SERVER_COMMAND_LENGTH);
2461 if (server_command)
2462 {
2463 /* Note that we don't set `line'. Between this and the check in
2464 dont_repeat, this insures that repeating will still do the
2465 right thing. */
2466 *p = '\0';
2467 return linebuffer + SERVER_COMMAND_LENGTH;
2468 }
2469
2470 /* Do history expansion if that is wished. */
2471 if (history_expansion_p && instream == stdin
2472 && ISATTY (instream))
2473 {
2474 char *history_value;
2475 int expanded;
2476
2477 *p = '\0'; /* Insert null now. */
2478 expanded = history_expand (linebuffer, &history_value);
2479 if (expanded)
2480 {
2481 /* Print the changes. */
2482 printf_unfiltered ("%s\n", history_value);
2483
2484 /* If there was an error, call this function again. */
2485 if (expanded < 0)
2486 {
2487 free (history_value);
2488 return command_line_input (prompt_arg, repeat, annotation_suffix);
2489 }
2490 if (strlen (history_value) > linelength)
2491 {
2492 linelength = strlen (history_value) + 1;
2493 linebuffer = (char *) xrealloc (linebuffer, linelength);
2494 }
2495 strcpy (linebuffer, history_value);
2496 p = linebuffer + strlen (linebuffer);
2497 free (history_value);
2498 }
2499 }
2500
2501 /* If we just got an empty line, and that is supposed
2502 to repeat the previous command, return the value in the
2503 global buffer. */
2504 if (repeat && p == linebuffer)
2505 return line;
2506 for (p1 = linebuffer; *p1 == ' ' || *p1 == '\t'; p1++);
2507 if (repeat && !*p1)
2508 return line;
2509
2510 *p = 0;
2511
2512 /* Add line to history if appropriate. */
2513 if (instream == stdin
2514 && ISATTY (stdin) && *linebuffer)
2515 add_history (linebuffer);
2516
2517 /* Note: lines consisting solely of comments are added to the command
2518 history. This is useful when you type a command, and then
2519 realize you don't want to execute it quite yet. You can comment
2520 out the command and then later fetch it from the value history
2521 and remove the '#'. The kill ring is probably better, but some
2522 people are in the habit of commenting things out. */
2523 if (*p1 == '#')
2524 *p1 = '\0'; /* Found a comment. */
2525
2526 /* Save into global buffer if appropriate. */
2527 if (repeat)
2528 {
2529 if (linelength > linesize)
2530 {
2531 line = xrealloc (line, linelength);
2532 linesize = linelength;
2533 }
2534 strcpy (line, linebuffer);
2535 return line;
2536 }
2537
2538 return linebuffer;
2539 }
2540 \f
2541
2542 /* Expand the body_list of COMMAND so that it can hold NEW_LENGTH
2543 code bodies. This is typically used when we encounter an "else"
2544 clause for an "if" command. */
2545
2546 static void
2547 realloc_body_list (command, new_length)
2548 struct command_line *command;
2549 int new_length;
2550 {
2551 int n;
2552 struct command_line **body_list;
2553
2554 n = command->body_count;
2555
2556 /* Nothing to do? */
2557 if (new_length <= n)
2558 return;
2559
2560 body_list = (struct command_line **)
2561 xmalloc (sizeof (struct command_line *) * new_length);
2562
2563 memcpy (body_list, command->body_list, sizeof (struct command_line *) * n);
2564
2565 free (command->body_list);
2566 command->body_list = body_list;
2567 command->body_count = new_length;
2568 }
2569
2570 /* Read one line from the input stream. If the command is an "else" or
2571 "end", return such an indication to the caller. */
2572
2573 static enum misc_command_type
2574 read_next_line (command)
2575 struct command_line **command;
2576 {
2577 char *p, *p1, *prompt_ptr, control_prompt[256];
2578 int i = 0;
2579
2580 if (control_level >= 254)
2581 error ("Control nesting too deep!\n");
2582
2583 /* Set a prompt based on the nesting of the control commands. */
2584 if (instream == stdin || (instream == 0 && readline_hook != NULL))
2585 {
2586 for (i = 0; i < control_level; i++)
2587 control_prompt[i] = ' ';
2588 control_prompt[i] = '>';
2589 control_prompt[i + 1] = '\0';
2590 prompt_ptr = (char *) &control_prompt[0];
2591 }
2592 else
2593 prompt_ptr = NULL;
2594
2595 p = command_line_input (prompt_ptr, instream == stdin, "commands");
2596
2597 /* Not sure what to do here. */
2598 if (p == NULL)
2599 return end_command;
2600
2601 /* Strip leading and trailing whitespace. */
2602 while (*p == ' ' || *p == '\t')
2603 p++;
2604
2605 p1 = p + strlen (p);
2606 while (p1 != p && (p1[-1] == ' ' || p1[-1] == '\t'))
2607 p1--;
2608
2609 /* Blanks and comments don't really do anything, but we need to
2610 distinguish them from else, end and other commands which can be
2611 executed. */
2612 if (p1 == p || p[0] == '#')
2613 return nop_command;
2614
2615 /* Is this the end of a simple, while, or if control structure? */
2616 if (p1 - p == 3 && !strncmp (p, "end", 3))
2617 return end_command;
2618
2619 /* Is the else clause of an if control structure? */
2620 if (p1 - p == 4 && !strncmp (p, "else", 4))
2621 return else_command;
2622
2623 /* Check for while, if, break, continue, etc and build a new command
2624 line structure for them. */
2625 if (p1 - p > 5 && !strncmp (p, "while", 5))
2626 *command = build_command_line (while_control, p + 6);
2627 else if (p1 - p > 2 && !strncmp (p, "if", 2))
2628 *command = build_command_line (if_control, p + 3);
2629 else if (p1 - p == 10 && !strncmp (p, "loop_break", 10))
2630 {
2631 *command = (struct command_line *)
2632 xmalloc (sizeof (struct command_line));
2633 (*command)->next = NULL;
2634 (*command)->line = NULL;
2635 (*command)->control_type = break_control;
2636 (*command)->body_count = 0;
2637 (*command)->body_list = NULL;
2638 }
2639 else if (p1 - p == 13 && !strncmp (p, "loop_continue", 13))
2640 {
2641 *command = (struct command_line *)
2642 xmalloc (sizeof (struct command_line));
2643 (*command)->next = NULL;
2644 (*command)->line = NULL;
2645 (*command)->control_type = continue_control;
2646 (*command)->body_count = 0;
2647 (*command)->body_list = NULL;
2648 }
2649 else
2650 {
2651 /* A normal command. */
2652 *command = (struct command_line *)
2653 xmalloc (sizeof (struct command_line));
2654 (*command)->next = NULL;
2655 (*command)->line = savestring (p, p1 - p);
2656 (*command)->control_type = simple_control;
2657 (*command)->body_count = 0;
2658 (*command)->body_list = NULL;
2659 }
2660
2661 /* Nothing special. */
2662 return ok_command;
2663 }
2664
2665 /* Recursively read in the control structures and create a command_line
2666 structure from them.
2667
2668 The parent_control parameter is the control structure in which the
2669 following commands are nested. */
2670
2671 static enum command_control_type
2672 recurse_read_control_structure (current_cmd)
2673 struct command_line *current_cmd;
2674 {
2675 int current_body, i;
2676 enum misc_command_type val;
2677 enum command_control_type ret;
2678 struct command_line **body_ptr, *child_tail, *next;
2679
2680 child_tail = NULL;
2681 current_body = 1;
2682
2683 /* Sanity checks. */
2684 if (current_cmd->control_type == simple_control)
2685 {
2686 error ("Recursed on a simple control type\n");
2687 return invalid_control;
2688 }
2689
2690 if (current_body > current_cmd->body_count)
2691 {
2692 error ("Allocated body is smaller than this command type needs\n");
2693 return invalid_control;
2694 }
2695
2696 /* Read lines from the input stream and build control structures. */
2697 while (1)
2698 {
2699 dont_repeat ();
2700
2701 next = NULL;
2702 val = read_next_line (&next);
2703
2704 /* Just skip blanks and comments. */
2705 if (val == nop_command)
2706 continue;
2707
2708 if (val == end_command)
2709 {
2710 if (current_cmd->control_type == while_control
2711 || current_cmd->control_type == if_control)
2712 {
2713 /* Success reading an entire control structure. */
2714 ret = simple_control;
2715 break;
2716 }
2717 else
2718 {
2719 ret = invalid_control;
2720 break;
2721 }
2722 }
2723
2724 /* Not the end of a control structure. */
2725 if (val == else_command)
2726 {
2727 if (current_cmd->control_type == if_control
2728 && current_body == 1)
2729 {
2730 realloc_body_list (current_cmd, 2);
2731 current_body = 2;
2732 child_tail = NULL;
2733 continue;
2734 }
2735 else
2736 {
2737 ret = invalid_control;
2738 break;
2739 }
2740 }
2741
2742 if (child_tail)
2743 {
2744 child_tail->next = next;
2745 }
2746 else
2747 {
2748 body_ptr = current_cmd->body_list;
2749 for (i = 1; i < current_body; i++)
2750 body_ptr++;
2751
2752 *body_ptr = next;
2753
2754 }
2755
2756 child_tail = next;
2757
2758 /* If the latest line is another control structure, then recurse
2759 on it. */
2760 if (next->control_type == while_control
2761 || next->control_type == if_control)
2762 {
2763 control_level++;
2764 ret = recurse_read_control_structure (next);
2765 control_level--;
2766
2767 if (ret != simple_control)
2768 break;
2769 }
2770 }
2771
2772 dont_repeat ();
2773
2774 return ret;
2775 }
2776
2777 /* Read lines from the input stream and accumulate them in a chain of
2778 struct command_line's, which is then returned. For input from a
2779 terminal, the special command "end" is used to mark the end of the
2780 input, and is not included in the returned chain of commands. */
2781
2782 #define END_MESSAGE "End with a line saying just \"end\"."
2783
2784 struct command_line *
2785 read_command_lines (prompt_arg, from_tty)
2786 char *prompt_arg;
2787 int from_tty;
2788 {
2789 struct command_line *head, *tail, *next;
2790 struct cleanup *old_chain;
2791 enum command_control_type ret;
2792 enum misc_command_type val;
2793
2794 control_level = 0;
2795 if (readline_begin_hook)
2796 {
2797 /* Note - intentional to merge messages with no newline */
2798 (*readline_begin_hook) ("%s %s\n", prompt_arg, END_MESSAGE);
2799 }
2800 else if (from_tty && input_from_terminal_p ())
2801 {
2802 printf_unfiltered ("%s\n%s\n", prompt_arg, END_MESSAGE);
2803 gdb_flush (gdb_stdout);
2804 }
2805
2806 head = tail = NULL;
2807 old_chain = NULL;
2808
2809 while (1)
2810 {
2811 val = read_next_line (&next);
2812
2813 /* Ignore blank lines or comments. */
2814 if (val == nop_command)
2815 continue;
2816
2817 if (val == end_command)
2818 {
2819 ret = simple_control;
2820 break;
2821 }
2822
2823 if (val != ok_command)
2824 {
2825 ret = invalid_control;
2826 break;
2827 }
2828
2829 if (next->control_type == while_control
2830 || next->control_type == if_control)
2831 {
2832 control_level++;
2833 ret = recurse_read_control_structure (next);
2834 control_level--;
2835
2836 if (ret == invalid_control)
2837 break;
2838 }
2839
2840 if (tail)
2841 {
2842 tail->next = next;
2843 }
2844 else
2845 {
2846 head = next;
2847 old_chain = make_cleanup ((make_cleanup_func) free_command_lines,
2848 &head);
2849 }
2850 tail = next;
2851 }
2852
2853 dont_repeat ();
2854
2855 if (head)
2856 {
2857 if (ret != invalid_control)
2858 {
2859 discard_cleanups (old_chain);
2860 }
2861 else
2862 do_cleanups (old_chain);
2863 }
2864
2865 if (readline_end_hook)
2866 {
2867 (*readline_end_hook) ();
2868 }
2869 return (head);
2870 }
2871
2872 /* Free a chain of struct command_line's. */
2873
2874 void
2875 free_command_lines (lptr)
2876 struct command_line **lptr;
2877 {
2878 register struct command_line *l = *lptr;
2879 register struct command_line *next;
2880 struct command_line **blist;
2881 int i;
2882
2883 while (l)
2884 {
2885 if (l->body_count > 0)
2886 {
2887 blist = l->body_list;
2888 for (i = 0; i < l->body_count; i++, blist++)
2889 free_command_lines (blist);
2890 }
2891 next = l->next;
2892 free (l->line);
2893 free ((PTR) l);
2894 l = next;
2895 }
2896 }
2897 \f
2898 /* Add an element to the list of info subcommands. */
2899
2900 struct cmd_list_element *
2901 add_info (name, fun, doc)
2902 char *name;
2903 void (*fun) PARAMS ((char *, int));
2904 char *doc;
2905 {
2906 return add_cmd (name, no_class, fun, doc, &infolist);
2907 }
2908
2909 /* Add an alias to the list of info subcommands. */
2910
2911 struct cmd_list_element *
2912 add_info_alias (name, oldname, abbrev_flag)
2913 char *name;
2914 char *oldname;
2915 int abbrev_flag;
2916 {
2917 return add_alias_cmd (name, oldname, 0, abbrev_flag, &infolist);
2918 }
2919
2920 /* The "info" command is defined as a prefix, with allow_unknown = 0.
2921 Therefore, its own definition is called only for "info" with no args. */
2922
2923 /* ARGSUSED */
2924 static void
2925 info_command (arg, from_tty)
2926 char *arg;
2927 int from_tty;
2928 {
2929 printf_unfiltered ("\"info\" must be followed by the name of an info command.\n");
2930 help_list (infolist, "info ", -1, gdb_stdout);
2931 }
2932
2933 /* The "complete" command is used by Emacs to implement completion. */
2934
2935 /* ARGSUSED */
2936 static void
2937 complete_command (arg, from_tty)
2938 char *arg;
2939 int from_tty;
2940 {
2941 int i;
2942 int argpoint;
2943 char *completion;
2944
2945 dont_repeat ();
2946
2947 if (arg == NULL)
2948 arg = "";
2949 argpoint = strlen (arg);
2950
2951 for (completion = line_completion_function (arg, i = 0, arg, argpoint);
2952 completion;
2953 completion = line_completion_function (arg, ++i, arg, argpoint))
2954 {
2955 printf_unfiltered ("%s\n", completion);
2956 free (completion);
2957 }
2958 }
2959
2960 /* The "show" command with no arguments shows all the settings. */
2961
2962 /* ARGSUSED */
2963 static void
2964 show_command (arg, from_tty)
2965 char *arg;
2966 int from_tty;
2967 {
2968 cmd_show_list (showlist, from_tty, "");
2969 }
2970 \f
2971 /* Add an element to the list of commands. */
2972
2973 struct cmd_list_element *
2974 add_com (name, class, fun, doc)
2975 char *name;
2976 enum command_class class;
2977 void (*fun) PARAMS ((char *, int));
2978 char *doc;
2979 {
2980 return add_cmd (name, class, fun, doc, &cmdlist);
2981 }
2982
2983 /* Add an alias or abbreviation command to the list of commands. */
2984
2985 struct cmd_list_element *
2986 add_com_alias (name, oldname, class, abbrev_flag)
2987 char *name;
2988 char *oldname;
2989 enum command_class class;
2990 int abbrev_flag;
2991 {
2992 return add_alias_cmd (name, oldname, class, abbrev_flag, &cmdlist);
2993 }
2994
2995 void
2996 error_no_arg (why)
2997 char *why;
2998 {
2999 error ("Argument required (%s).", why);
3000 }
3001
3002 /* ARGSUSED */
3003 static void
3004 help_command (command, from_tty)
3005 char *command;
3006 int from_tty; /* Ignored */
3007 {
3008 help_cmd (command, gdb_stdout);
3009 }
3010 \f
3011 static void
3012 validate_comname (comname)
3013 char *comname;
3014 {
3015 register char *p;
3016
3017 if (comname == 0)
3018 error_no_arg ("name of command to define");
3019
3020 p = comname;
3021 while (*p)
3022 {
3023 if (!isalnum (*p) && *p != '-' && *p != '_')
3024 error ("Junk in argument list: \"%s\"", p);
3025 p++;
3026 }
3027 }
3028
3029 /* This is just a placeholder in the command data structures. */
3030 static void
3031 user_defined_command (ignore, from_tty)
3032 char *ignore;
3033 int from_tty;
3034 {
3035 }
3036
3037 static void
3038 define_command (comname, from_tty)
3039 char *comname;
3040 int from_tty;
3041 {
3042 register struct command_line *cmds;
3043 register struct cmd_list_element *c, *newc, *hookc = 0;
3044 char *tem = comname;
3045 char tmpbuf[128];
3046 #define HOOK_STRING "hook-"
3047 #define HOOK_LEN 5
3048
3049 validate_comname (comname);
3050
3051 /* Look it up, and verify that we got an exact match. */
3052 c = lookup_cmd (&tem, cmdlist, "", -1, 1);
3053 if (c && !STREQ (comname, c->name))
3054 c = 0;
3055
3056 if (c)
3057 {
3058 if (c->class == class_user || c->class == class_alias)
3059 tem = "Redefine command \"%s\"? ";
3060 else
3061 tem = "Really redefine built-in command \"%s\"? ";
3062 if (!query (tem, c->name))
3063 error ("Command \"%s\" not redefined.", c->name);
3064 }
3065
3066 /* If this new command is a hook, then mark the command which it
3067 is hooking. Note that we allow hooking `help' commands, so that
3068 we can hook the `stop' pseudo-command. */
3069
3070 if (!strncmp (comname, HOOK_STRING, HOOK_LEN))
3071 {
3072 /* Look up cmd it hooks, and verify that we got an exact match. */
3073 tem = comname + HOOK_LEN;
3074 hookc = lookup_cmd (&tem, cmdlist, "", -1, 0);
3075 if (hookc && !STREQ (comname + HOOK_LEN, hookc->name))
3076 hookc = 0;
3077 if (!hookc)
3078 {
3079 warning ("Your new `%s' command does not hook any existing command.",
3080 comname);
3081 if (!query ("Proceed? "))
3082 error ("Not confirmed.");
3083 }
3084 }
3085
3086 comname = savestring (comname, strlen (comname));
3087
3088 /* If the rest of the commands will be case insensitive, this one
3089 should behave in the same manner. */
3090 for (tem = comname; *tem; tem++)
3091 if (isupper (*tem))
3092 *tem = tolower (*tem);
3093
3094 sprintf (tmpbuf, "Type commands for definition of \"%s\".", comname);
3095 cmds = read_command_lines (tmpbuf, from_tty);
3096
3097 if (c && c->class == class_user)
3098 free_command_lines (&c->user_commands);
3099
3100 newc = add_cmd (comname, class_user, user_defined_command,
3101 (c && c->class == class_user)
3102 ? c->doc : savestring ("User-defined.", 13), &cmdlist);
3103 newc->user_commands = cmds;
3104
3105 /* If this new command is a hook, then mark both commands as being
3106 tied. */
3107 if (hookc)
3108 {
3109 hookc->hook = newc; /* Target gets hooked. */
3110 newc->hookee = hookc; /* We are marked as hooking target cmd. */
3111 }
3112 }
3113
3114 static void
3115 document_command (comname, from_tty)
3116 char *comname;
3117 int from_tty;
3118 {
3119 struct command_line *doclines;
3120 register struct cmd_list_element *c;
3121 char *tem = comname;
3122 char tmpbuf[128];
3123
3124 validate_comname (comname);
3125
3126 c = lookup_cmd (&tem, cmdlist, "", 0, 1);
3127
3128 if (c->class != class_user)
3129 error ("Command \"%s\" is built-in.", comname);
3130
3131 sprintf (tmpbuf, "Type documentation for \"%s\".", comname);
3132 doclines = read_command_lines (tmpbuf, from_tty);
3133
3134 if (c->doc)
3135 free (c->doc);
3136
3137 {
3138 register struct command_line *cl1;
3139 register int len = 0;
3140
3141 for (cl1 = doclines; cl1; cl1 = cl1->next)
3142 len += strlen (cl1->line) + 1;
3143
3144 c->doc = (char *) xmalloc (len + 1);
3145 *c->doc = 0;
3146
3147 for (cl1 = doclines; cl1; cl1 = cl1->next)
3148 {
3149 strcat (c->doc, cl1->line);
3150 if (cl1->next)
3151 strcat (c->doc, "\n");
3152 }
3153 }
3154
3155 free_command_lines (&doclines);
3156 }
3157 \f
3158 /* Print the GDB banner. */
3159 void
3160 print_gdb_version (stream)
3161 struct ui_file *stream;
3162 {
3163 /* From GNU coding standards, first line is meant to be easy for a
3164 program to parse, and is just canonical program name and version
3165 number, which starts after last space. */
3166
3167 #ifdef UI_OUT
3168 /* Print it console style until a format is defined */
3169 fprintf_filtered (stream, "GNU gdb %s (UI_OUT)\n", version);
3170 #else
3171 fprintf_filtered (stream, "GNU gdb %s\n", version);
3172 #endif
3173
3174 /* Second line is a copyright notice. */
3175
3176 fprintf_filtered (stream, "Copyright 1998 Free Software Foundation, Inc.\n");
3177
3178 /* Following the copyright is a brief statement that the program is
3179 free software, that users are free to copy and change it on
3180 certain conditions, that it is covered by the GNU GPL, and that
3181 there is no warranty. */
3182
3183 fprintf_filtered (stream, "\
3184 GDB is free software, covered by the GNU General Public License, and you are\n\
3185 welcome to change it and/or distribute copies of it under certain conditions.\n\
3186 Type \"show copying\" to see the conditions.\n\
3187 There is absolutely no warranty for GDB. Type \"show warranty\" for details.\n");
3188
3189 /* After the required info we print the configuration information. */
3190
3191 fprintf_filtered (stream, "This GDB was configured as \"");
3192 if (!STREQ (host_name, target_name))
3193 {
3194 fprintf_filtered (stream, "--host=%s --target=%s", host_name, target_name);
3195 }
3196 else
3197 {
3198 fprintf_filtered (stream, "%s", host_name);
3199 }
3200 fprintf_filtered (stream, "\".");
3201 }
3202
3203 /* ARGSUSED */
3204 static void
3205 show_version (args, from_tty)
3206 char *args;
3207 int from_tty;
3208 {
3209 immediate_quit++;
3210 print_gdb_version (gdb_stdout);
3211 printf_filtered ("\n");
3212 immediate_quit--;
3213 }
3214 \f
3215 /* get_prompt: access method for the GDB prompt string. */
3216
3217 #define MAX_PROMPT_SIZE 256
3218
3219 /*
3220 * int get_prompt_1 (char * buf);
3221 *
3222 * Work-horse for get_prompt (called via catch_errors).
3223 * Argument is buffer to hold the formatted prompt.
3224 *
3225 * Returns: 1 for success (use formatted prompt)
3226 * 0 for failure (use gdb_prompt_string).
3227 */
3228
3229 static int gdb_prompt_escape;
3230
3231 static int
3232 get_prompt_1 (formatted_prompt)
3233 char *formatted_prompt;
3234 {
3235 char *local_prompt;
3236
3237 if (event_loop_p)
3238 local_prompt = PROMPT (0);
3239 else
3240 local_prompt = gdb_prompt_string;
3241
3242
3243 if (gdb_prompt_escape == 0)
3244 {
3245 return 0; /* do no formatting */
3246 }
3247 else
3248 /* formatted prompt */
3249 {
3250 char fmt[40], *promptp, *outp, *tmp;
3251 value_ptr arg_val;
3252 DOUBLEST doubleval;
3253 LONGEST longval;
3254 CORE_ADDR addrval;
3255
3256 int i, len;
3257 struct type *arg_type, *elt_type;
3258
3259 promptp = local_prompt;
3260 outp = formatted_prompt;
3261
3262 while (*promptp != '\0')
3263 {
3264 int available = MAX_PROMPT_SIZE - (outp - formatted_prompt) - 1;
3265
3266 if (*promptp != gdb_prompt_escape)
3267 {
3268 if (available >= 1) /* overflow protect */
3269 *outp++ = *promptp++;
3270 }
3271 else
3272 {
3273 /* GDB prompt string contains escape char. Parse for arg.
3274 Two consecutive escape chars followed by arg followed by
3275 a comma means to insert the arg using a default format.
3276 Otherwise a printf format string may be included between
3277 the two escape chars. eg:
3278 %%foo, insert foo using default format
3279 %2.2f%foo, insert foo using "%2.2f" format
3280 A mismatch between the format string and the data type
3281 of "foo" is an error (which we don't know how to protect
3282 against). */
3283
3284 fmt[0] = '\0'; /* assume null format string */
3285 if (promptp[1] == gdb_prompt_escape) /* double esc char */
3286 {
3287 promptp += 2; /* skip past two escape chars. */
3288 }
3289 else
3290 {
3291 /* extract format string from between two esc chars */
3292 i = 0;
3293 do
3294 {
3295 fmt[i++] = *promptp++; /* copy format string */
3296 }
3297 while (i < sizeof (fmt) - 1 &&
3298 *promptp != gdb_prompt_escape &&
3299 *promptp != '\0');
3300
3301 if (*promptp != gdb_prompt_escape)
3302 error ("Syntax error at prompt position %d",
3303 promptp - local_prompt);
3304 else
3305 {
3306 promptp++; /* skip second escape char */
3307 fmt[i++] = '\0'; /* terminate the format string */
3308 }
3309 }
3310
3311 arg_val = parse_to_comma_and_eval (&promptp);
3312 if (*promptp == ',')
3313 promptp++; /* skip past the comma */
3314 arg_type = check_typedef (VALUE_TYPE (arg_val));
3315 switch (TYPE_CODE (arg_type))
3316 {
3317 case TYPE_CODE_ARRAY:
3318 elt_type = check_typedef (TYPE_TARGET_TYPE (arg_type));
3319 if (TYPE_LENGTH (arg_type) > 0 &&
3320 TYPE_LENGTH (elt_type) == 1 &&
3321 TYPE_CODE (elt_type) == TYPE_CODE_INT)
3322 {
3323 int len = TYPE_LENGTH (arg_type);
3324
3325 if (VALUE_LAZY (arg_val))
3326 value_fetch_lazy (arg_val);
3327 tmp = VALUE_CONTENTS (arg_val);
3328
3329 if (len > available)
3330 len = available; /* overflow protect */
3331
3332 /* FIXME: how to protect GDB from crashing
3333 from bad user-supplied format string? */
3334 if (fmt[0] != 0)
3335 sprintf (outp, fmt, tmp);
3336 else
3337 strncpy (outp, tmp, len);
3338 outp[len] = '\0';
3339 }
3340 break;
3341 case TYPE_CODE_PTR:
3342 elt_type = check_typedef (TYPE_TARGET_TYPE (arg_type));
3343 addrval = value_as_pointer (arg_val);
3344
3345 if (TYPE_LENGTH (elt_type) == 1 &&
3346 TYPE_CODE (elt_type) == TYPE_CODE_INT &&
3347 addrval != 0)
3348 {
3349 /* display it as a string */
3350 char *default_fmt = "%s";
3351 char *tmp;
3352 int err = 0;
3353
3354 /* Limiting the number of bytes that the following call
3355 will read protects us from sprintf overflow later. */
3356 i = target_read_string (addrval, /* src */
3357 &tmp, /* dest */
3358 available, /* len */
3359 &err);
3360 if (err) /* read failed */
3361 error ("%s on target_read", safe_strerror (err));
3362
3363 tmp[i] = '\0'; /* force-terminate string */
3364 /* FIXME: how to protect GDB from crashing
3365 from bad user-supplied format string? */
3366 sprintf (outp, fmt[0] == 0 ? default_fmt : fmt,
3367 tmp);
3368 free (tmp);
3369 }
3370 else
3371 {
3372 /* display it as a pointer */
3373 char *default_fmt = "0x%x";
3374
3375 /* FIXME: how to protect GDB from crashing
3376 from bad user-supplied format string? */
3377 if (available >= 16 /*? */ ) /* overflow protect */
3378 sprintf (outp, fmt[0] == 0 ? default_fmt : fmt,
3379 (long) addrval);
3380 }
3381 break;
3382 case TYPE_CODE_FLT:
3383 {
3384 char *default_fmt = "%g";
3385
3386 doubleval = value_as_double (arg_val);
3387 /* FIXME: how to protect GDB from crashing
3388 from bad user-supplied format string? */
3389 if (available >= 16 /*? */ ) /* overflow protect */
3390 sprintf (outp, fmt[0] == 0 ? default_fmt : fmt,
3391 (double) doubleval);
3392 break;
3393 }
3394 case TYPE_CODE_INT:
3395 {
3396 char *default_fmt = "%d";
3397
3398 longval = value_as_long (arg_val);
3399 /* FIXME: how to protect GDB from crashing
3400 from bad user-supplied format string? */
3401 if (available >= 16 /*? */ ) /* overflow protect */
3402 sprintf (outp, fmt[0] == 0 ? default_fmt : fmt,
3403 (long) longval);
3404 break;
3405 }
3406 case TYPE_CODE_BOOL:
3407 {
3408 /* no default format for bool */
3409 longval = value_as_long (arg_val);
3410 if (available >= 8 /*? */ ) /* overflow protect */
3411 {
3412 if (longval)
3413 strcpy (outp, "<true>");
3414 else
3415 strcpy (outp, "<false>");
3416 }
3417 break;
3418 }
3419 case TYPE_CODE_ENUM:
3420 {
3421 /* no default format for enum */
3422 longval = value_as_long (arg_val);
3423 len = TYPE_NFIELDS (arg_type);
3424 /* find enum name if possible */
3425 for (i = 0; i < len; i++)
3426 if (TYPE_FIELD_BITPOS (arg_type, i) == longval)
3427 break; /* match -- end loop */
3428
3429 if (i < len) /* enum name found */
3430 {
3431 char *name = TYPE_FIELD_NAME (arg_type, i);
3432
3433 strncpy (outp, name, available);
3434 /* in casel available < strlen (name), */
3435 outp[available] = '\0';
3436 }
3437 else
3438 {
3439 if (available >= 16 /*? */ ) /* overflow protect */
3440 sprintf (outp, "%ld", (long) longval);
3441 }
3442 break;
3443 }
3444 case TYPE_CODE_VOID:
3445 *outp = '\0';
3446 break; /* void type -- no output */
3447 default:
3448 error ("bad data type at prompt position %d",
3449 promptp - local_prompt);
3450 break;
3451 }
3452 outp += strlen (outp);
3453 }
3454 }
3455 *outp++ = '\0'; /* terminate prompt string */
3456 return 1;
3457 }
3458 }
3459
3460 char *
3461 get_prompt ()
3462 {
3463 static char buf[MAX_PROMPT_SIZE];
3464
3465 if (catch_errors (get_prompt_1, buf, "bad formatted prompt: ",
3466 RETURN_MASK_ALL))
3467 {
3468 return &buf[0]; /* successful formatted prompt */
3469 }
3470 else
3471 {
3472 /* Prompt could not be formatted. */
3473 if (event_loop_p)
3474 return PROMPT (0);
3475 else
3476 return gdb_prompt_string;
3477 }
3478 }
3479
3480 void
3481 set_prompt (s)
3482 char *s;
3483 {
3484 /* ??rehrauer: I don't know why this fails, since it looks as though
3485 assignments to prompt are wrapped in calls to savestring...
3486 if (prompt != NULL)
3487 free (prompt);
3488 */
3489 if (event_loop_p)
3490 PROMPT (0) = savestring (s, strlen (s));
3491 else
3492 gdb_prompt_string = savestring (s, strlen (s));
3493 }
3494 \f
3495
3496 /* If necessary, make the user confirm that we should quit. Return
3497 non-zero if we should quit, zero if we shouldn't. */
3498
3499 int
3500 quit_confirm ()
3501 {
3502 if (inferior_pid != 0 && target_has_execution)
3503 {
3504 char *s;
3505
3506 /* This is something of a hack. But there's no reliable way to
3507 see if a GUI is running. The `use_windows' variable doesn't
3508 cut it. */
3509 if (init_ui_hook)
3510 s = "A debugging session is active.\nDo you still want to close the debugger?";
3511 else if (attach_flag)
3512 s = "The program is running. Quit anyway (and detach it)? ";
3513 else
3514 s = "The program is running. Exit anyway? ";
3515
3516 if (!query (s))
3517 return 0;
3518 }
3519
3520 return 1;
3521 }
3522
3523 /* Quit without asking for confirmation. */
3524
3525 void
3526 quit_force (args, from_tty)
3527 char *args;
3528 int from_tty;
3529 {
3530 int exit_code = 0;
3531
3532 /* An optional expression may be used to cause gdb to terminate with the
3533 value of that expression. */
3534 if (args)
3535 {
3536 value_ptr val = parse_and_eval (args);
3537
3538 exit_code = (int) value_as_long (val);
3539 }
3540
3541 if (inferior_pid != 0 && target_has_execution)
3542 {
3543 if (attach_flag)
3544 target_detach (args, from_tty);
3545 else
3546 target_kill ();
3547 }
3548
3549 /* UDI wants this, to kill the TIP. */
3550 target_close (1);
3551
3552 /* Save the history information if it is appropriate to do so. */
3553 if (write_history_p && history_filename)
3554 write_history (history_filename);
3555
3556 do_final_cleanups (ALL_CLEANUPS); /* Do any final cleanups before exiting */
3557
3558 #if defined(TUI)
3559 /* tuiDo((TuiOpaqueFuncPtr)tuiCleanUp); */
3560 /* The above does not need to be inside a tuiDo(), since
3561 * it is not manipulating the curses screen, but rather,
3562 * it is tearing it down.
3563 */
3564 if (tui_version)
3565 tuiCleanUp ();
3566 #endif
3567
3568 exit (exit_code);
3569 }
3570
3571 /* Handle the quit command. */
3572
3573 void
3574 quit_command (args, from_tty)
3575 char *args;
3576 int from_tty;
3577 {
3578 if (!quit_confirm ())
3579 error ("Not confirmed.");
3580 quit_force (args, from_tty);
3581 }
3582
3583 /* Returns whether GDB is running on a terminal and whether the user
3584 desires that questions be asked of them on that terminal. */
3585
3586 int
3587 input_from_terminal_p ()
3588 {
3589 return gdb_has_a_terminal () && (instream == stdin) & caution;
3590 }
3591 \f
3592 /* ARGSUSED */
3593 static void
3594 pwd_command (args, from_tty)
3595 char *args;
3596 int from_tty;
3597 {
3598 if (args)
3599 error ("The \"pwd\" command does not take an argument: %s", args);
3600 getcwd (gdb_dirbuf, sizeof (gdb_dirbuf));
3601
3602 if (!STREQ (gdb_dirbuf, current_directory))
3603 printf_unfiltered ("Working directory %s\n (canonically %s).\n",
3604 current_directory, gdb_dirbuf);
3605 else
3606 printf_unfiltered ("Working directory %s.\n", current_directory);
3607 }
3608
3609 void
3610 cd_command (dir, from_tty)
3611 char *dir;
3612 int from_tty;
3613 {
3614 int len;
3615 /* Found something other than leading repetitions of "/..". */
3616 int found_real_path;
3617 char *p;
3618
3619 /* If the new directory is absolute, repeat is a no-op; if relative,
3620 repeat might be useful but is more likely to be a mistake. */
3621 dont_repeat ();
3622
3623 if (dir == 0)
3624 error_no_arg ("new working directory");
3625
3626 dir = tilde_expand (dir);
3627 make_cleanup (free, dir);
3628
3629 if (chdir (dir) < 0)
3630 perror_with_name (dir);
3631
3632 #if defined(_WIN32) || defined(__MSDOS__)
3633 /* There's too much mess with DOSish names like "d:", "d:.",
3634 "d:./foo" etc. Instead of having lots of special #ifdef'ed code,
3635 simply get the canonicalized name of the current directory. */
3636 dir = getcwd (gdb_dirbuf, sizeof (gdb_dirbuf));
3637 #endif
3638
3639 len = strlen (dir);
3640 if (SLASH_P (dir[len-1]))
3641 {
3642 /* Remove the trailing slash unless this is a root directory
3643 (including a drive letter on non-Unix systems). */
3644 if (!(len == 1) /* "/" */
3645 #if defined(_WIN32) || defined(__MSDOS__)
3646 && !(!SLASH_P (*dir) && ROOTED_P (dir) && len <= 3) /* "d:/" */
3647 #endif
3648 )
3649 len--;
3650 }
3651
3652 dir = savestring (dir, len);
3653 if (ROOTED_P (dir))
3654 current_directory = dir;
3655 else
3656 {
3657 if (SLASH_P (current_directory[strlen (current_directory) - 1]))
3658 current_directory = concat (current_directory, dir, NULL);
3659 else
3660 current_directory = concat (current_directory, SLASH_STRING, dir, NULL);
3661 free (dir);
3662 }
3663
3664 /* Now simplify any occurrences of `.' and `..' in the pathname. */
3665
3666 found_real_path = 0;
3667 for (p = current_directory; *p;)
3668 {
3669 if (SLASH_P (p[0]) && p[1] == '.' && (p[2] == 0 || SLASH_P (p[2])))
3670 strcpy (p, p + 2);
3671 else if (SLASH_P (p[0]) && p[1] == '.' && p[2] == '.'
3672 && (p[3] == 0 || SLASH_P (p[3])))
3673 {
3674 if (found_real_path)
3675 {
3676 /* Search backwards for the directory just before the "/.."
3677 and obliterate it and the "/..". */
3678 char *q = p;
3679 while (q != current_directory && !SLASH_P (q[-1]))
3680 --q;
3681
3682 if (q == current_directory)
3683 /* current_directory is
3684 a relative pathname ("can't happen"--leave it alone). */
3685 ++p;
3686 else
3687 {
3688 strcpy (q - 1, p + 3);
3689 p = q - 1;
3690 }
3691 }
3692 else
3693 /* We are dealing with leading repetitions of "/..", for example
3694 "/../..", which is the Mach super-root. */
3695 p += 3;
3696 }
3697 else
3698 {
3699 found_real_path = 1;
3700 ++p;
3701 }
3702 }
3703
3704 forget_cached_source_info ();
3705
3706 if (from_tty)
3707 pwd_command ((char *) 0, 1);
3708 }
3709 \f
3710 struct source_cleanup_lines_args
3711 {
3712 int old_line;
3713 char *old_file;
3714 char *old_pre_error;
3715 char *old_error_pre_print;
3716 };
3717
3718 static void
3719 source_cleanup_lines (args)
3720 PTR args;
3721 {
3722 struct source_cleanup_lines_args *p =
3723 (struct source_cleanup_lines_args *) args;
3724 source_line_number = p->old_line;
3725 source_file_name = p->old_file;
3726 source_pre_error = p->old_pre_error;
3727 error_pre_print = p->old_error_pre_print;
3728 }
3729
3730 /* ARGSUSED */
3731 void
3732 source_command (args, from_tty)
3733 char *args;
3734 int from_tty;
3735 {
3736 FILE *stream;
3737 struct cleanup *old_cleanups;
3738 char *file = args;
3739 struct source_cleanup_lines_args old_lines;
3740 int needed_length;
3741
3742 if (file == NULL)
3743 {
3744 error ("source command requires pathname of file to source.");
3745 }
3746
3747 file = tilde_expand (file);
3748 old_cleanups = make_cleanup (free, file);
3749
3750 stream = fopen (file, FOPEN_RT);
3751 if (!stream)
3752 {
3753 if (from_tty)
3754 perror_with_name (file);
3755 else
3756 return;
3757 }
3758
3759 make_cleanup ((make_cleanup_func) fclose, stream);
3760
3761 old_lines.old_line = source_line_number;
3762 old_lines.old_file = source_file_name;
3763 old_lines.old_pre_error = source_pre_error;
3764 old_lines.old_error_pre_print = error_pre_print;
3765 make_cleanup (source_cleanup_lines, &old_lines);
3766 source_line_number = 0;
3767 source_file_name = file;
3768 source_pre_error = error_pre_print == NULL ? "" : error_pre_print;
3769 source_pre_error = savestring (source_pre_error, strlen (source_pre_error));
3770 make_cleanup (free, source_pre_error);
3771 /* This will get set every time we read a line. So it won't stay "" for
3772 long. */
3773 error_pre_print = "";
3774
3775 needed_length = strlen (source_file_name) + strlen (source_pre_error) + 80;
3776 if (source_error_allocated < needed_length)
3777 {
3778 source_error_allocated *= 2;
3779 if (source_error_allocated < needed_length)
3780 source_error_allocated = needed_length;
3781 if (source_error == NULL)
3782 source_error = xmalloc (source_error_allocated);
3783 else
3784 source_error = xrealloc (source_error, source_error_allocated);
3785 }
3786
3787 read_command_file (stream);
3788
3789 do_cleanups (old_cleanups);
3790 }
3791
3792 /* ARGSUSED */
3793 static void
3794 echo_command (text, from_tty)
3795 char *text;
3796 int from_tty;
3797 {
3798 char *p = text;
3799 register int c;
3800
3801 if (text)
3802 while ((c = *p++) != '\0')
3803 {
3804 if (c == '\\')
3805 {
3806 /* \ at end of argument is used after spaces
3807 so they won't be lost. */
3808 if (*p == 0)
3809 return;
3810
3811 c = parse_escape (&p);
3812 if (c >= 0)
3813 printf_filtered ("%c", c);
3814 }
3815 else
3816 printf_filtered ("%c", c);
3817 }
3818
3819 /* Force this output to appear now. */
3820 wrap_here ("");
3821 gdb_flush (gdb_stdout);
3822 }
3823
3824 /* ARGSUSED */
3825 static void
3826 dont_repeat_command (ignored, from_tty)
3827 char *ignored;
3828 int from_tty;
3829 {
3830 *line = 0; /* Can't call dont_repeat here because we're not
3831 necessarily reading from stdin. */
3832 }
3833 \f
3834 /* Functions to manipulate command line editing control variables. */
3835
3836 /* Number of commands to print in each call to show_commands. */
3837 #define Hist_print 10
3838 static void
3839 show_commands (args, from_tty)
3840 char *args;
3841 int from_tty;
3842 {
3843 /* Index for history commands. Relative to history_base. */
3844 int offset;
3845
3846 /* Number of the history entry which we are planning to display next.
3847 Relative to history_base. */
3848 static int num = 0;
3849
3850 /* The first command in the history which doesn't exist (i.e. one more
3851 than the number of the last command). Relative to history_base. */
3852 int hist_len;
3853
3854 extern HIST_ENTRY *history_get PARAMS ((int));
3855
3856 /* Print out some of the commands from the command history. */
3857 /* First determine the length of the history list. */
3858 hist_len = history_size;
3859 for (offset = 0; offset < history_size; offset++)
3860 {
3861 if (!history_get (history_base + offset))
3862 {
3863 hist_len = offset;
3864 break;
3865 }
3866 }
3867
3868 if (args)
3869 {
3870 if (args[0] == '+' && args[1] == '\0')
3871 /* "info editing +" should print from the stored position. */
3872 ;
3873 else
3874 /* "info editing <exp>" should print around command number <exp>. */
3875 num = (parse_and_eval_address (args) - history_base) - Hist_print / 2;
3876 }
3877 /* "show commands" means print the last Hist_print commands. */
3878 else
3879 {
3880 num = hist_len - Hist_print;
3881 }
3882
3883 if (num < 0)
3884 num = 0;
3885
3886 /* If there are at least Hist_print commands, we want to display the last
3887 Hist_print rather than, say, the last 6. */
3888 if (hist_len - num < Hist_print)
3889 {
3890 num = hist_len - Hist_print;
3891 if (num < 0)
3892 num = 0;
3893 }
3894
3895 for (offset = num; offset < num + Hist_print && offset < hist_len; offset++)
3896 {
3897 printf_filtered ("%5d %s\n", history_base + offset,
3898 (history_get (history_base + offset))->line);
3899 }
3900
3901 /* The next command we want to display is the next one that we haven't
3902 displayed yet. */
3903 num += Hist_print;
3904
3905 /* If the user repeats this command with return, it should do what
3906 "show commands +" does. This is unnecessary if arg is null,
3907 because "show commands +" is not useful after "show commands". */
3908 if (from_tty && args)
3909 {
3910 args[0] = '+';
3911 args[1] = '\0';
3912 }
3913 }
3914
3915 /* Called by do_setshow_command. */
3916 /* ARGSUSED */
3917 static void
3918 set_history_size_command (args, from_tty, c)
3919 char *args;
3920 int from_tty;
3921 struct cmd_list_element *c;
3922 {
3923 if (history_size == INT_MAX)
3924 unstifle_history ();
3925 else if (history_size >= 0)
3926 stifle_history (history_size);
3927 else
3928 {
3929 history_size = INT_MAX;
3930 error ("History size must be non-negative");
3931 }
3932 }
3933
3934 /* ARGSUSED */
3935 static void
3936 set_history (args, from_tty)
3937 char *args;
3938 int from_tty;
3939 {
3940 printf_unfiltered ("\"set history\" must be followed by the name of a history subcommand.\n");
3941 help_list (sethistlist, "set history ", -1, gdb_stdout);
3942 }
3943
3944 /* ARGSUSED */
3945 static void
3946 show_history (args, from_tty)
3947 char *args;
3948 int from_tty;
3949 {
3950 cmd_show_list (showhistlist, from_tty, "");
3951 }
3952
3953 int info_verbose = 0; /* Default verbose msgs off */
3954
3955 /* Called by do_setshow_command. An elaborate joke. */
3956 /* ARGSUSED */
3957 static void
3958 set_verbose (args, from_tty, c)
3959 char *args;
3960 int from_tty;
3961 struct cmd_list_element *c;
3962 {
3963 char *cmdname = "verbose";
3964 struct cmd_list_element *showcmd;
3965
3966 showcmd = lookup_cmd_1 (&cmdname, showlist, NULL, 1);
3967
3968 if (info_verbose)
3969 {
3970 c->doc = "Set verbose printing of informational messages.";
3971 showcmd->doc = "Show verbose printing of informational messages.";
3972 }
3973 else
3974 {
3975 c->doc = "Set verbosity.";
3976 showcmd->doc = "Show verbosity.";
3977 }
3978 }
3979
3980 static void
3981 float_handler (signo)
3982 int signo;
3983 {
3984 /* This message is based on ANSI C, section 4.7. Note that integer
3985 divide by zero causes this, so "float" is a misnomer. */
3986 signal (SIGFPE, float_handler);
3987 error ("Erroneous arithmetic operation.");
3988 }
3989 \f
3990
3991 static void
3992 init_cmd_lists ()
3993 {
3994 cmdlist = NULL;
3995 infolist = NULL;
3996 enablelist = NULL;
3997 disablelist = NULL;
3998 togglelist = NULL;
3999 stoplist = NULL;
4000 deletelist = NULL;
4001 enablebreaklist = NULL;
4002 setlist = NULL;
4003 unsetlist = NULL;
4004 showlist = NULL;
4005 sethistlist = NULL;
4006 showhistlist = NULL;
4007 unsethistlist = NULL;
4008 maintenancelist = NULL;
4009 maintenanceinfolist = NULL;
4010 maintenanceprintlist = NULL;
4011 setprintlist = NULL;
4012 showprintlist = NULL;
4013 setchecklist = NULL;
4014 showchecklist = NULL;
4015 }
4016
4017 /* Init the history buffer. Note that we are called after the init file(s)
4018 * have been read so that the user can change the history file via his
4019 * .gdbinit file (for instance). The GDBHISTFILE environment variable
4020 * overrides all of this.
4021 */
4022
4023 void
4024 init_history ()
4025 {
4026 char *tmpenv;
4027
4028 tmpenv = getenv ("HISTSIZE");
4029 if (tmpenv)
4030 history_size = atoi (tmpenv);
4031 else if (!history_size)
4032 history_size = 256;
4033
4034 stifle_history (history_size);
4035
4036 tmpenv = getenv ("GDBHISTFILE");
4037 if (tmpenv)
4038 history_filename = savestring (tmpenv, strlen (tmpenv));
4039 else if (!history_filename)
4040 {
4041 /* We include the current directory so that if the user changes
4042 directories the file written will be the same as the one
4043 that was read. */
4044 #ifdef __MSDOS__
4045 /* No leading dots in file names are allowed on MSDOS. */
4046 history_filename = concat (current_directory, "/_gdb_history", NULL);
4047 #else
4048 history_filename = concat (current_directory, "/.gdb_history", NULL);
4049 #endif
4050 }
4051 read_history (history_filename);
4052 }
4053
4054 static void
4055 init_main ()
4056 {
4057 struct cmd_list_element *c;
4058
4059 /* If we are running the asynchronous version,
4060 we initialize the prompts differently. */
4061 if (!event_loop_p)
4062 {
4063 gdb_prompt_string = savestring (DEFAULT_PROMPT, strlen (DEFAULT_PROMPT));
4064 }
4065 else
4066 {
4067 /* initialize the prompt stack to a simple "(gdb) " prompt or to
4068 whatever the DEFAULT_PROMPT is. */
4069 the_prompts.top = 0;
4070 PREFIX (0) = "";
4071 PROMPT (0) = savestring (DEFAULT_PROMPT, strlen (DEFAULT_PROMPT));
4072 SUFFIX (0) = "";
4073 /* Set things up for annotation_level > 1, if the user ever decides
4074 to use it. */
4075 async_annotation_suffix = "prompt";
4076 /* Set the variable associated with the setshow prompt command. */
4077 new_async_prompt = savestring (PROMPT (0), strlen (PROMPT (0)));
4078 }
4079 gdb_prompt_escape = 0; /* default to none. */
4080
4081 /* Set the important stuff up for command editing. */
4082 command_editing_p = 1;
4083 history_expansion_p = 0;
4084 write_history_p = 0;
4085
4086 /* Setup important stuff for command line editing. */
4087 rl_completion_entry_function = (int (*)()) readline_line_completion_function;
4088 rl_completer_word_break_characters = gdb_completer_word_break_characters;
4089 rl_completer_quote_characters = gdb_completer_quote_characters;
4090 rl_readline_name = "gdb";
4091
4092 /* Define the classes of commands.
4093 They will appear in the help list in the reverse of this order. */
4094
4095 add_cmd ("internals", class_maintenance, NO_FUNCTION,
4096 "Maintenance commands.\n\
4097 Some gdb commands are provided just for use by gdb maintainers.\n\
4098 These commands are subject to frequent change, and may not be as\n\
4099 well documented as user commands.",
4100 &cmdlist);
4101 add_cmd ("obscure", class_obscure, NO_FUNCTION, "Obscure features.", &cmdlist);
4102 add_cmd ("aliases", class_alias, NO_FUNCTION, "Aliases of other commands.", &cmdlist);
4103 add_cmd ("user-defined", class_user, NO_FUNCTION, "User-defined commands.\n\
4104 The commands in this class are those defined by the user.\n\
4105 Use the \"define\" command to define a command.", &cmdlist);
4106 add_cmd ("support", class_support, NO_FUNCTION, "Support facilities.", &cmdlist);
4107 if (!dbx_commands)
4108 add_cmd ("status", class_info, NO_FUNCTION, "Status inquiries.", &cmdlist);
4109 add_cmd ("files", class_files, NO_FUNCTION, "Specifying and examining files.", &cmdlist);
4110 add_cmd ("breakpoints", class_breakpoint, NO_FUNCTION, "Making program stop at certain points.", &cmdlist);
4111 add_cmd ("data", class_vars, NO_FUNCTION, "Examining data.", &cmdlist);
4112 add_cmd ("stack", class_stack, NO_FUNCTION, "Examining the stack.\n\
4113 The stack is made up of stack frames. Gdb assigns numbers to stack frames\n\
4114 counting from zero for the innermost (currently executing) frame.\n\n\
4115 At any time gdb identifies one frame as the \"selected\" frame.\n\
4116 Variable lookups are done with respect to the selected frame.\n\
4117 When the program being debugged stops, gdb selects the innermost frame.\n\
4118 The commands below can be used to select other frames by number or address.",
4119 &cmdlist);
4120 add_cmd ("running", class_run, NO_FUNCTION, "Running the program.", &cmdlist);
4121
4122 add_com ("pwd", class_files, pwd_command,
4123 "Print working directory. This is used for your program as well.");
4124 c = add_cmd ("cd", class_files, cd_command,
4125 "Set working directory to DIR for debugger and program being debugged.\n\
4126 The change does not take effect for the program being debugged\n\
4127 until the next time it is started.", &cmdlist);
4128 c->completer = filename_completer;
4129
4130 /* The set prompt command is different depending whether or not the
4131 async version is run. NOTE: this difference is going to
4132 disappear as we make the event loop be the default engine of
4133 gdb. */
4134 if (!event_loop_p)
4135 {
4136 add_show_from_set
4137 (add_set_cmd ("prompt", class_support, var_string,
4138 (char *) &gdb_prompt_string, "Set gdb's prompt",
4139 &setlist),
4140 &showlist);
4141 }
4142 else
4143 {
4144 c = add_set_cmd ("prompt", class_support, var_string,
4145 (char *) &new_async_prompt, "Set gdb's prompt",
4146 &setlist);
4147 add_show_from_set (c, &showlist);
4148 c->function.sfunc = set_async_prompt;
4149 }
4150
4151 add_show_from_set
4152 (add_set_cmd ("prompt-escape-char", class_support, var_zinteger,
4153 (char *) &gdb_prompt_escape,
4154 "Set escape character for formatting of gdb's prompt",
4155 &setlist),
4156 &showlist);
4157
4158 add_com ("echo", class_support, echo_command,
4159 "Print a constant string. Give string as argument.\n\
4160 C escape sequences may be used in the argument.\n\
4161 No newline is added at the end of the argument;\n\
4162 use \"\\n\" if you want a newline to be printed.\n\
4163 Since leading and trailing whitespace are ignored in command arguments,\n\
4164 if you want to print some you must use \"\\\" before leading whitespace\n\
4165 to be printed or after trailing whitespace.");
4166 add_com ("document", class_support, document_command,
4167 "Document a user-defined command.\n\
4168 Give command name as argument. Give documentation on following lines.\n\
4169 End with a line of just \"end\".");
4170 add_com ("define", class_support, define_command,
4171 "Define a new command name. Command name is argument.\n\
4172 Definition appears on following lines, one command per line.\n\
4173 End with a line of just \"end\".\n\
4174 Use the \"document\" command to give documentation for the new command.\n\
4175 Commands defined in this way may have up to ten arguments.");
4176
4177 #ifdef __STDC__
4178 c = add_cmd ("source", class_support, source_command,
4179 "Read commands from a file named FILE.\n\
4180 Note that the file \"" GDBINIT_FILENAME "\" is read automatically in this way\n\
4181 when gdb is started.", &cmdlist);
4182 #else
4183 /* Punt file name, we can't help it easily. */
4184 c = add_cmd ("source", class_support, source_command,
4185 "Read commands from a file named FILE.\n\
4186 Note that the file \".gdbinit\" is read automatically in this way\n\
4187 when gdb is started.", &cmdlist);
4188 #endif
4189 c->completer = filename_completer;
4190
4191 add_com ("quit", class_support, quit_command, "Exit gdb.");
4192 add_com ("help", class_support, help_command, "Print list of commands.");
4193 add_com_alias ("q", "quit", class_support, 1);
4194 add_com_alias ("h", "help", class_support, 1);
4195
4196 add_com ("dont-repeat", class_support, dont_repeat_command, "Don't repeat this command.\n\
4197 Primarily used inside of user-defined commands that should not be repeated when\n\
4198 hitting return.");
4199
4200 c = add_set_cmd ("verbose", class_support, var_boolean, (char *) &info_verbose,
4201 "Set ",
4202 &setlist),
4203 add_show_from_set (c, &showlist);
4204 c->function.sfunc = set_verbose;
4205 set_verbose (NULL, 0, c);
4206
4207 /* The set editing command is different depending whether or not the
4208 async version is run. NOTE: this difference is going to disappear
4209 as we make the event loop be the default engine of gdb. */
4210 if (!event_loop_p)
4211 {
4212 add_show_from_set
4213 (add_set_cmd ("editing", class_support, var_boolean, (char *) &command_editing_p,
4214 "Set editing of command lines as they are typed.\n\
4215 Use \"on\" to enable the editing, and \"off\" to disable it.\n\
4216 Without an argument, command line editing is enabled. To edit, use\n\
4217 EMACS-like or VI-like commands like control-P or ESC.", &setlist),
4218 &showlist);
4219 }
4220 else
4221 {
4222 c = add_set_cmd ("editing", class_support, var_boolean, (char *) &async_command_editing_p,
4223 "Set editing of command lines as they are typed.\n\
4224 Use \"on\" to enable the editing, and \"off\" to disable it.\n\
4225 Without an argument, command line editing is enabled. To edit, use\n\
4226 EMACS-like or VI-like commands like control-P or ESC.", &setlist);
4227
4228 add_show_from_set (c, &showlist);
4229 c->function.sfunc = set_async_editing_command;
4230 }
4231
4232 add_prefix_cmd ("history", class_support, set_history,
4233 "Generic command for setting command history parameters.",
4234 &sethistlist, "set history ", 0, &setlist);
4235 add_prefix_cmd ("history", class_support, show_history,
4236 "Generic command for showing command history parameters.",
4237 &showhistlist, "show history ", 0, &showlist);
4238
4239 add_show_from_set
4240 (add_set_cmd ("expansion", no_class, var_boolean, (char *) &history_expansion_p,
4241 "Set history expansion on command input.\n\
4242 Without an argument, history expansion is enabled.", &sethistlist),
4243 &showhistlist);
4244
4245 add_show_from_set
4246 (add_set_cmd ("save", no_class, var_boolean, (char *) &write_history_p,
4247 "Set saving of the history record on exit.\n\
4248 Use \"on\" to enable the saving, and \"off\" to disable it.\n\
4249 Without an argument, saving is enabled.", &sethistlist),
4250 &showhistlist);
4251
4252 c = add_set_cmd ("size", no_class, var_integer, (char *) &history_size,
4253 "Set the size of the command history, \n\
4254 ie. the number of previous commands to keep a record of.", &sethistlist);
4255 add_show_from_set (c, &showhistlist);
4256 c->function.sfunc = set_history_size_command;
4257
4258 add_show_from_set
4259 (add_set_cmd ("filename", no_class, var_filename, (char *) &history_filename,
4260 "Set the filename in which to record the command history\n\
4261 (the list of previous commands of which a record is kept).", &sethistlist),
4262 &showhistlist);
4263
4264 add_show_from_set
4265 (add_set_cmd ("confirm", class_support, var_boolean,
4266 (char *) &caution,
4267 "Set whether to confirm potentially dangerous operations.",
4268 &setlist),
4269 &showlist);
4270
4271 add_prefix_cmd ("info", class_info, info_command,
4272 "Generic command for showing things about the program being debugged.",
4273 &infolist, "info ", 0, &cmdlist);
4274 add_com_alias ("i", "info", class_info, 1);
4275
4276 add_com ("complete", class_obscure, complete_command,
4277 "List the completions for the rest of the line as a command.");
4278
4279 add_prefix_cmd ("show", class_info, show_command,
4280 "Generic command for showing things about the debugger.",
4281 &showlist, "show ", 0, &cmdlist);
4282 /* Another way to get at the same thing. */
4283 add_info ("set", show_command, "Show all GDB settings.");
4284
4285 add_cmd ("commands", no_class, show_commands,
4286 "Show the history of commands you typed.\n\
4287 You can supply a command number to start with, or a `+' to start after\n\
4288 the previous command number shown.",
4289 &showlist);
4290
4291 add_cmd ("version", no_class, show_version,
4292 "Show what version of GDB this is.", &showlist);
4293
4294 add_com ("while", class_support, while_command,
4295 "Execute nested commands WHILE the conditional expression is non zero.\n\
4296 The conditional expression must follow the word `while' and must in turn be\n\
4297 followed by a new line. The nested commands must be entered one per line,\n\
4298 and should be terminated by the word `end'.");
4299
4300 add_com ("if", class_support, if_command,
4301 "Execute nested commands once IF the conditional expression is non zero.\n\
4302 The conditional expression must follow the word `if' and must in turn be\n\
4303 followed by a new line. The nested commands must be entered one per line,\n\
4304 and should be terminated by the word 'else' or `end'. If an else clause\n\
4305 is used, the same rules apply to its nested commands as to the first ones.");
4306
4307 /* If target is open when baud changes, it doesn't take effect until the
4308 next open (I think, not sure). */
4309 add_show_from_set (add_set_cmd ("remotebaud", no_class,
4310 var_zinteger, (char *) &baud_rate,
4311 "Set baud rate for remote serial I/O.\n\
4312 This value is used to set the speed of the serial port when debugging\n\
4313 using remote targets.", &setlist),
4314 &showlist);
4315
4316 add_show_from_set (
4317 add_set_cmd ("remotedebug", no_class, var_zinteger, (char *) &remote_debug,
4318 "Set debugging of remote protocol.\n\
4319 When enabled, each packet sent or received with the remote target\n\
4320 is displayed.", &setlist),
4321 &showlist);
4322
4323 add_show_from_set (
4324 add_set_cmd ("remotetimeout", no_class, var_integer, (char *) &remote_timeout,
4325 "Set timeout limit to wait for target to respond.\n\
4326 This value is used to set the time limit for gdb to wait for a response\n\
4327 from the target.", &setlist),
4328 &showlist);
4329
4330 /* The set annotate command is different depending whether or not
4331 the async version is run. NOTE: this difference is going to
4332 disappear as we make the event loop be the default engine of
4333 gdb. */
4334 if (!event_loop_p)
4335 {
4336 c = add_set_cmd ("annotate", class_obscure, var_zinteger,
4337 (char *) &annotation_level, "Set annotation_level.\n\
4338 0 == normal; 1 == fullname (for use when running under emacs)\n\
4339 2 == output annotated suitably for use by programs that control GDB.",
4340 &setlist);
4341 c = add_show_from_set (c, &showlist);
4342 }
4343 else
4344 {
4345 c = add_set_cmd ("annotate", class_obscure, var_zinteger,
4346 (char *) &annotation_level, "Set annotation_level.\n\
4347 0 == normal; 1 == fullname (for use when running under emacs)\n\
4348 2 == output annotated suitably for use by programs that control GDB.",
4349 &setlist);
4350 add_show_from_set (c, &showlist);
4351 c->function.sfunc = set_async_annotation_level;
4352 }
4353 if (event_loop_p)
4354 {
4355 add_show_from_set
4356 (add_set_cmd ("exec-done-display", class_support, var_boolean, (char *) &exec_done_display_p,
4357 "Set notification of completion for asynchronous execution commands.\n\
4358 Use \"on\" to enable the notification, and \"off\" to disable it.", &setlist),
4359 &showlist);
4360 }
4361 }
This page took 0.122312 seconds and 4 git commands to generate.