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