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