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