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