* gdb.threads/Makefile.in (docdir): Removed.
[deliverable/binutils-gdb.git] / gdb / top.c
CommitLineData
172559ec 1/* Top level stuff for GDB, the GNU debugger.
255181a9 2 Copyright 1986, 1987, 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996
172559ec
JK
3 Free Software Foundation, Inc.
4
5This file is part of GDB.
6
7This program is free software; you can redistribute it and/or modify
8it under the terms of the GNU General Public License as published by
9the Free Software Foundation; either version 2 of the License, or
10(at your option) any later version.
11
12This program is distributed in the hope that it will be useful,
13but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15GNU General Public License for more details.
16
17You should have received a copy of the GNU General Public License
18along with this program; if not, write to the Free Software
6c9638b4 19Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
172559ec
JK
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"
e52bfe0c 31#include "value.h"
172559ec
JK
32#include "language.h"
33#include "terminal.h" /* For job_control. */
34#include "annotate.h"
35#include <setjmp.h>
36#include "top.h"
37
38/* readline include files */
39#include "readline.h"
40#include "history.h"
41
42/* readline defines this. */
43#undef savestring
44
45#include <sys/types.h>
1a494973 46#ifdef HAVE_UNISTD_H
172559ec
JK
47#include <unistd.h>
48#endif
49
2b576293 50#include "gdb_string.h"
172559ec
JK
51#ifndef NO_SYS_FILE
52#include <sys/file.h>
53#endif
54#include <sys/param.h>
2b576293 55#include "gdb_stat.h"
172559ec
JK
56#include <ctype.h>
57
e3be225e
SS
58extern void initialize_targets PARAMS ((void));
59
60extern void initialize_utils PARAMS ((void));
61
172559ec
JK
62/* Prototypes for local functions */
63
9ed8604f
PS
64static char * line_completion_function PARAMS ((char *, int, char *, int));
65
66static char * readline_line_completion_function PARAMS ((char *, int));
172559ec 67
e52bfe0c 68static void command_loop_marker PARAMS ((int));
172559ec 69
e52bfe0c 70static void while_command PARAMS ((char *, int));
172559ec 71
e52bfe0c 72static void if_command PARAMS ((char *, int));
172559ec 73
e52bfe0c
JL
74static struct command_line *
75build_command_line PARAMS ((enum command_control_type, char *));
172559ec 76
e52bfe0c
JL
77static struct command_line *
78get_command_line PARAMS ((enum command_control_type, char *));
172559ec 79
e52bfe0c 80static void realloc_body_list PARAMS ((struct command_line *, int));
172559ec 81
e52bfe0c 82static enum misc_command_type read_next_line PARAMS ((struct command_line **));
172559ec 83
e52bfe0c
JL
84static enum command_control_type
85recurse_read_control_structure PARAMS ((struct command_line *));
172559ec 86
0f8cdd9b
JL
87static struct cleanup * setup_user_args PARAMS ((char *));
88
89static char * locate_arg PARAMS ((char *));
90
91static char * insert_args PARAMS ((char *));
92
93static void arg_cleanup PARAMS ((void));
94
e52bfe0c 95static void init_main PARAMS ((void));
172559ec 96
e52bfe0c 97static void init_cmd_lists PARAMS ((void));
172559ec 98
e52bfe0c 99static void float_handler PARAMS ((int));
172559ec 100
e52bfe0c 101static void init_signals PARAMS ((void));
172559ec 102
e52bfe0c 103static void set_verbose PARAMS ((char *, int, struct cmd_list_element *));
172559ec 104
b8176214
ILT
105#ifdef TARGET_BYTE_ORDER_SELECTABLE
106
107static void set_endian PARAMS ((char *, int));
108
109static void set_endian_big PARAMS ((char *, int));
110
111static void set_endian_little PARAMS ((char *, int));
112
b83ed019
ILT
113static void set_endian_auto PARAMS ((char *, int));
114
b8176214
ILT
115static void show_endian PARAMS ((char *, int));
116
117#endif
118
e52bfe0c 119static void show_history PARAMS ((char *, int));
172559ec 120
e52bfe0c 121static void set_history PARAMS ((char *, int));
172559ec 122
e52bfe0c
JL
123static void set_history_size_command PARAMS ((char *, int,
124 struct cmd_list_element *));
172559ec 125
e52bfe0c 126static void show_commands PARAMS ((char *, int));
172559ec 127
e52bfe0c 128static void echo_command PARAMS ((char *, int));
172559ec 129
e52bfe0c 130static void pwd_command PARAMS ((char *, int));
172559ec 131
e52bfe0c 132static void show_version PARAMS ((char *, int));
172559ec 133
e52bfe0c 134static void document_command PARAMS ((char *, int));
172559ec 135
e52bfe0c 136static void define_command PARAMS ((char *, int));
172559ec 137
e52bfe0c
JL
138static void validate_comname PARAMS ((char *));
139
140static void help_command PARAMS ((char *, int));
141
142static void show_command PARAMS ((char *, int));
143
144static void info_command PARAMS ((char *, int));
145
146static void complete_command PARAMS ((char *, int));
147
148static void do_nothing PARAMS ((int));
149
150static int quit_cover PARAMS ((char *));
151
152static void disconnect PARAMS ((int));
153
154static void source_cleanup PARAMS ((FILE *));
172559ec
JK
155
156/* If this definition isn't overridden by the header files, assume
157 that isatty and fileno exist on this system. */
158#ifndef ISATTY
159#define ISATTY(FP) (isatty (fileno (FP)))
160#endif
161
162/* Initialization file name for gdb. This is overridden in some configs. */
163
164#ifndef GDBINIT_FILENAME
165#define GDBINIT_FILENAME ".gdbinit"
166#endif
167char gdbinit[] = GDBINIT_FILENAME;
f22661ee 168
172559ec
JK
169int inhibit_gdbinit = 0;
170
f22661ee
SS
171/* If nonzero, and GDB has been configured to be able to use windows,
172 attempt to open them upon startup. */
754e5da2 173
f22661ee 174int use_windows = 1;
754e5da2 175
172559ec
JK
176/* Version number of GDB, as a string. */
177
178extern char *version;
179
180/* Canonical host name as a string. */
181
182extern char *host_name;
183
184/* Canonical target name as a string. */
185
186extern char *target_name;
187
188extern char lang_frame_mismatch_warn[]; /* language.c */
189
190/* Flag for whether we want all the "from_tty" gubbish printed. */
191
192int caution = 1; /* Default is yes, sigh. */
193
f22661ee 194/* Define all cmd_list_elements. */
172559ec
JK
195
196/* Chain containing all defined commands. */
197
198struct cmd_list_element *cmdlist;
199
200/* Chain containing all defined info subcommands. */
201
202struct cmd_list_element *infolist;
203
204/* Chain containing all defined enable subcommands. */
205
206struct cmd_list_element *enablelist;
207
208/* Chain containing all defined disable subcommands. */
209
210struct cmd_list_element *disablelist;
211
212/* Chain containing all defined delete subcommands. */
213
214struct cmd_list_element *deletelist;
215
216/* Chain containing all defined "enable breakpoint" subcommands. */
217
218struct cmd_list_element *enablebreaklist;
219
220/* Chain containing all defined set subcommands */
221
222struct cmd_list_element *setlist;
223
224/* Chain containing all defined unset subcommands */
225
226struct cmd_list_element *unsetlist;
227
228/* Chain containing all defined show subcommands. */
229
230struct cmd_list_element *showlist;
231
b8176214
ILT
232#ifdef TARGET_BYTE_ORDER_SELECTABLE
233/* Chain containing the \"set endian\" commands. */
234
235struct cmd_list_element *endianlist;
236#endif
237
172559ec
JK
238/* Chain containing all defined \"set history\". */
239
240struct cmd_list_element *sethistlist;
241
242/* Chain containing all defined \"show history\". */
243
244struct cmd_list_element *showhistlist;
245
246/* Chain containing all defined \"unset history\". */
247
248struct cmd_list_element *unsethistlist;
249
250/* Chain containing all defined maintenance subcommands. */
251
252#if MAINTENANCE_CMDS
253struct cmd_list_element *maintenancelist;
254#endif
255
256/* Chain containing all defined "maintenance info" subcommands. */
257
258#if MAINTENANCE_CMDS
259struct cmd_list_element *maintenanceinfolist;
260#endif
261
262/* Chain containing all defined "maintenance print" subcommands. */
263
264#if MAINTENANCE_CMDS
265struct cmd_list_element *maintenanceprintlist;
266#endif
267
268struct cmd_list_element *setprintlist;
269
270struct cmd_list_element *showprintlist;
271
272struct cmd_list_element *setchecklist;
273
274struct cmd_list_element *showchecklist;
275
276/* stdio stream that command input is being read from. Set to stdin normally.
277 Set by source_command to the file we are sourcing. Set to NULL if we are
41756e56 278 executing a user-defined command or interacting via a GUI. */
172559ec
JK
279
280FILE *instream;
281
282/* Current working directory. */
283
284char *current_directory;
285
286/* The directory name is actually stored here (usually). */
b7ec5b8d 287char gdb_dirbuf[1024];
172559ec
JK
288
289/* Function to call before reading a command, if nonzero.
290 The function receives two args: an input stream,
291 and a prompt string. */
292
293void (*window_hook) PARAMS ((FILE *, char *));
294
295int epoch_interface;
296int xgdb_verbose;
297
298/* gdb prints this when reading a command interactively */
299static char *prompt;
300
301/* Buffer used for reading command lines, and the size
302 allocated for it so far. */
303
304char *line;
305int linesize = 100;
306
307/* Nonzero if the current command is modified by "server ". This
308 affects things like recording into the command history, comamnds
309 repeating on RETURN, etc. This is so a user interface (emacs, GUI,
310 whatever) can issue its own commands and also send along commands
311 from the user, and have the user not notice that the user interface
312 is issuing commands too. */
313int server_command;
314
315/* Baud rate specified for talking to serial target systems. Default
316 is left as -1, so targets can choose their own defaults. */
317/* FIXME: This means that "show remotebaud" and gr_files_info can print -1
318 or (unsigned int)-1. This is a Bad User Interface. */
319
320int baud_rate = -1;
321
322/* Non-zero tells remote* modules to output debugging info. */
323
324int remote_debug = 0;
325
e52bfe0c
JL
326/* Level of control structure. */
327static int control_level;
328
0f8cdd9b
JL
329/* Structure for arguments to user defined functions. */
330#define MAXUSERARGS 10
331struct user_args
332{
333 struct user_args *next;
334 struct
335 {
336 char *arg;
337 int len;
338 } a[MAXUSERARGS];
339 int count;
340} *user_args;
341
172559ec
JK
342/* Signal to catch ^Z typed while reading a command: SIGTSTP or SIGCONT. */
343
344#ifndef STOP_SIGNAL
345#ifdef SIGTSTP
346#define STOP_SIGNAL SIGTSTP
347static void stop_sig PARAMS ((int));
348#endif
349#endif
350
351/* Some System V have job control but not sigsetmask(). */
352#if !defined (HAVE_SIGSETMASK)
353#if !defined (USG)
354#define HAVE_SIGSETMASK 1
355#else
356#define HAVE_SIGSETMASK 0
357#endif
358#endif
359
360#if 0 == (HAVE_SIGSETMASK)
361#define sigsetmask(n)
362#endif
8164ec2e
SG
363
364/* Hooks for alternate command interfaces. */
365
366/* Called after most modules have been initialized, but before taking users
367 command file. */
368
369void (*init_ui_hook) PARAMS ((void));
370
371/* Called instead of command_loop at top level. Can be invoked via
372 return_to_top_level. */
373
374void (*command_loop_hook) PARAMS ((void));
375
764efb58 376
8164ec2e
SG
377/* Called instead of fputs for all output. */
378
86db943c 379void (*fputs_unfiltered_hook) PARAMS ((const char *linebuffer, FILE *stream));
8164ec2e 380
764efb58
SC
381/* Called when the target says something to the host, which may
382 want to appear in a different window. */
383
327332d8 384void (*target_output_hook) PARAMS ((char *));
764efb58 385
8164ec2e
SG
386/* Called from print_frame_info to list the line we stopped in. */
387
388void (*print_frame_info_listing_hook) PARAMS ((struct symtab *s, int line,
389 int stopline, int noerror));
390/* Replaces most of query. */
391
392int (*query_hook) PARAMS (());
393
394/* Called from gdb_flush to flush output. */
395
396void (*flush_hook) PARAMS ((FILE *stream));
397
41756e56
FF
398/* These three functions support getting lines of text from the user. They
399 are used in sequence. First readline_begin_hook is called with a text
400 string that might be (for example) a message for the user to type in a
401 sequence of commands to be executed at a breakpoint. If this function
402 calls back to a GUI, it might take this opportunity to pop up a text
403 interaction window with this message. Next, readline_hook is called
404 with a prompt that is emitted prior to collecting the user input.
405 It can be called multiple times. Finally, readline_end_hook is called
406 to notify the GUI that we are done with the interaction window and it
407 can close it. */
408
409void (*readline_begin_hook) PARAMS ((char *, ...));
410char * (*readline_hook) PARAMS ((char *));
411void (*readline_end_hook) PARAMS ((void));
412
8164ec2e
SG
413/* Called as appropriate to notify the interface of the specified breakpoint
414 conditions. */
415
637b1661 416void (*create_breakpoint_hook) PARAMS ((struct breakpoint *bpt));
8164ec2e 417void (*delete_breakpoint_hook) PARAMS ((struct breakpoint *bpt));
6131622e 418void (*modify_breakpoint_hook) PARAMS ((struct breakpoint *bpt));
637b1661
SG
419
420/* Called during long calculations to allow GUI to repair window damage, and to
421 check for stop buttons, etc... */
422
423void (*interactive_hook) PARAMS ((void));
424
16041d53
SC
425/* Called when the registers have changed, as a hint to a GUI
426 to minimize window update. */
427
428void (*registers_changed_hook) PARAMS ((void));
429
479f0f18
SG
430/* Called when going to wait for the target. Usually allows the GUI to run
431 while waiting for target events. */
432
433int (*target_wait_hook) PARAMS ((int pid, struct target_waitstatus *status));
434
435/* Used by UI as a wrapper around command execution. May do various things
436 like enabling/disabling buttons, etc... */
437
438void (*call_command_hook) PARAMS ((struct cmd_list_element *c, char *cmd,
439 int from_tty));
45993f61
SC
440
441
442/* Takes control from error (). Typically used to prevent longjmps out of the
443 middle of the GUI. Usually used in conjunction with a catch routine. */
444
6b14af2b 445NORETURN void (*error_hook) PARAMS (()) ATTR_NORETURN;
45993f61 446
172559ec
JK
447\f
448/* Where to go for return_to_top_level (RETURN_ERROR). */
449jmp_buf error_return;
450/* Where to go for return_to_top_level (RETURN_QUIT). */
451jmp_buf quit_return;
452
453/* Return for reason REASON. This generally gets back to the command
454 loop, but can be caught via catch_errors. */
455
456NORETURN void
457return_to_top_level (reason)
458 enum return_reason reason;
459{
460 quit_flag = 0;
461 immediate_quit = 0;
462
463 /* Perhaps it would be cleaner to do this via the cleanup chain (not sure
464 I can think of a reason why that is vital, though). */
465 bpstat_clear_actions(stop_bpstat); /* Clear queued breakpoint commands */
466
467 disable_current_display ();
468 do_cleanups (ALL_CLEANUPS);
469
470 if (annotation_level > 1)
471 switch (reason)
472 {
473 case RETURN_QUIT:
474 annotate_quit ();
475 break;
476 case RETURN_ERROR:
477 annotate_error ();
478 break;
479 }
480
481 (NORETURN void) longjmp
482 (reason == RETURN_ERROR ? error_return : quit_return, 1);
483}
484
485/* Call FUNC with arg ARGS, catching any errors. If there is no
486 error, return the value returned by FUNC. If there is an error,
487 print ERRSTRING, print the specific error message, then return
488 zero.
489
490 Must not be called with immediate_quit in effect (bad things might
491 happen, say we got a signal in the middle of a memcpy to quit_return).
492 This is an OK restriction; with very few exceptions immediate_quit can
493 be replaced by judicious use of QUIT.
494
495 MASK specifies what to catch; it is normally set to
496 RETURN_MASK_ALL, if for no other reason than that the code which
497 calls catch_errors might not be set up to deal with a quit which
498 isn't caught. But if the code can deal with it, it generally
499 should be RETURN_MASK_ERROR, unless for some reason it is more
500 useful to abort only the portion of the operation inside the
501 catch_errors. Note that quit should return to the command line
502 fairly quickly, even if some further processing is being done. */
503
504int
505catch_errors (func, args, errstring, mask)
506 int (*func) PARAMS ((char *));
507 PTR args;
508 char *errstring;
509 return_mask mask;
510{
511 jmp_buf saved_error;
512 jmp_buf saved_quit;
513 jmp_buf tmp_jmp;
514 int val;
515 struct cleanup *saved_cleanup_chain;
516 char *saved_error_pre_print;
dee80384 517 char *saved_quit_pre_print;
172559ec
JK
518
519 saved_cleanup_chain = save_cleanups ();
520 saved_error_pre_print = error_pre_print;
dee80384 521 saved_quit_pre_print = quit_pre_print;
172559ec
JK
522
523 if (mask & RETURN_MASK_ERROR)
dee80384
JK
524 {
525 memcpy ((char *)saved_error, (char *)error_return, sizeof (jmp_buf));
526 error_pre_print = errstring;
527 }
172559ec 528 if (mask & RETURN_MASK_QUIT)
dee80384
JK
529 {
530 memcpy (saved_quit, quit_return, sizeof (jmp_buf));
531 quit_pre_print = errstring;
532 }
172559ec
JK
533
534 if (setjmp (tmp_jmp) == 0)
535 {
536 if (mask & RETURN_MASK_ERROR)
537 memcpy (error_return, tmp_jmp, sizeof (jmp_buf));
538 if (mask & RETURN_MASK_QUIT)
539 memcpy (quit_return, tmp_jmp, sizeof (jmp_buf));
540 val = (*func) (args);
541 }
542 else
543 val = 0;
544
545 restore_cleanups (saved_cleanup_chain);
546
172559ec 547 if (mask & RETURN_MASK_ERROR)
dee80384
JK
548 {
549 memcpy (error_return, saved_error, sizeof (jmp_buf));
550 error_pre_print = saved_error_pre_print;
551 }
172559ec 552 if (mask & RETURN_MASK_QUIT)
dee80384
JK
553 {
554 memcpy (quit_return, saved_quit, sizeof (jmp_buf));
555 quit_pre_print = saved_quit_pre_print;
556 }
172559ec
JK
557 return val;
558}
559
560/* Handler for SIGHUP. */
561
562static void
563disconnect (signo)
564int signo;
565{
566 catch_errors (quit_cover, NULL,
567 "Could not kill the program being debugged", RETURN_MASK_ALL);
568 signal (SIGHUP, SIG_DFL);
569 kill (getpid (), SIGHUP);
570}
571
572/* Just a little helper function for disconnect(). */
573
574static int
575quit_cover (s)
576char *s;
577{
578 caution = 0; /* Throw caution to the wind -- we're exiting.
579 This prevents asking the user dumb questions. */
580 quit_command((char *)0, 0);
581 return 0;
582}
583\f
584/* Line number we are currently in in a file which is being sourced. */
585static int source_line_number;
586
587/* Name of the file we are sourcing. */
588static char *source_file_name;
589
590/* Buffer containing the error_pre_print used by the source stuff.
591 Malloc'd. */
592static char *source_error;
593static int source_error_allocated;
594
595/* Something to glom on to the start of error_pre_print if source_file_name
596 is set. */
597static char *source_pre_error;
598
599/* Clean up on error during a "source" command (or execution of a
600 user-defined command). */
601
602static void
603source_cleanup (stream)
604 FILE *stream;
605{
606 /* Restore the previous input stream. */
607 instream = stream;
608}
609
610/* Read commands from STREAM. */
611void
612read_command_file (stream)
613 FILE *stream;
614{
615 struct cleanup *cleanups;
616
617 cleanups = make_cleanup (source_cleanup, instream);
618 instream = stream;
619 command_loop ();
620 do_cleanups (cleanups);
621}
622\f
623extern void init_proc ();
624
43ab4ba5
SS
625void (*pre_init_ui_hook) PARAMS ((void));
626
172559ec
JK
627void
628gdb_init ()
629{
43ab4ba5
SS
630 if (pre_init_ui_hook)
631 pre_init_ui_hook ();
632
172559ec
JK
633 /* Run the init function of each source file */
634
b7ec5b8d
FF
635 getcwd (gdb_dirbuf, sizeof (gdb_dirbuf));
636 current_directory = gdb_dirbuf;
f36b58b1 637
172559ec 638 init_cmd_lists (); /* This needs to be done first */
754e5da2
SG
639 initialize_targets (); /* Setup target_terminal macros for utils.c */
640 initialize_utils (); /* Make errors and warnings possible */
172559ec
JK
641 initialize_all_files ();
642 init_main (); /* But that omits this file! Do it now */
643 init_signals ();
644
645 init_proc ();
646
647 /* We need a default language for parsing expressions, so simple things like
648 "set width 0" won't fail if no language is explicitly set in a config file
649 or implicitly set by reading an executable during startup. */
650 set_language (language_c);
651 expected_language = current_language; /* don't warn about the change. */
754e5da2
SG
652
653 if (init_ui_hook)
654 init_ui_hook ();
172559ec
JK
655}
656
e52bfe0c
JL
657/* Allocate, initialize a new command line structure for one of the
658 control commands (if/while). */
659
660static struct command_line *
661build_command_line (type, args)
662 enum command_control_type type;
663 char *args;
664{
665 struct command_line *cmd;
666
a25ffb08
JL
667 if (args == NULL)
668 error ("if/while commands require arguments.\n");
669
e52bfe0c
JL
670 cmd = (struct command_line *)xmalloc (sizeof (struct command_line));
671 cmd->next = NULL;
672 cmd->control_type = type;
673
674 cmd->body_count = 1;
675 cmd->body_list
676 = (struct command_line **)xmalloc (sizeof (struct command_line *)
677 * cmd->body_count);
678 memset (cmd->body_list, 0, sizeof (struct command_line *) * cmd->body_count);
679 cmd->line = savestring (args, strlen (args));
680 return cmd;
681}
682
683/* Build and return a new command structure for the control commands
684 such as "if" and "while". */
685
686static struct command_line *
687get_command_line (type, arg)
688 enum command_control_type type;
689 char *arg;
690{
691 struct command_line *cmd;
692 struct cleanup *old_chain = NULL;
693
694 /* Allocate and build a new command line structure. */
695 cmd = build_command_line (type, arg);
696
697 old_chain = make_cleanup (free_command_lines, &cmd);
698
699 /* Read in the body of this command. */
700 if (recurse_read_control_structure (cmd) == invalid_control)
701 {
702 warning ("error reading in control structure\n");
703 do_cleanups (old_chain);
704 return NULL;
705 }
706
707 discard_cleanups (old_chain);
708 return cmd;
709}
710
0f8cdd9b
JL
711/* Recursively print a command (including full control structures). */
712void
713print_command_line (cmd, depth)
714 struct command_line *cmd;
715 unsigned int depth;
716{
717 unsigned int i;
718
719 if (depth)
720 {
721 for (i = 0; i < depth; i++)
722 fputs_filtered (" ", gdb_stdout);
723 }
724
725 /* A simple command, print it and return. */
726 if (cmd->control_type == simple_control)
727 {
728 fputs_filtered (cmd->line, gdb_stdout);
729 fputs_filtered ("\n", gdb_stdout);
730 return;
731 }
732
733 /* loop_continue to jump to the start of a while loop, print it
734 and return. */
735 if (cmd->control_type == continue_control)
736 {
737 fputs_filtered ("loop_continue\n", gdb_stdout);
738 return;
739 }
740
741 /* loop_break to break out of a while loop, print it and return. */
742 if (cmd->control_type == break_control)
743 {
744 fputs_filtered ("loop_break\n", gdb_stdout);
745 return;
746 }
747
748 /* A while command. Recursively print its subcommands before returning. */
749 if (cmd->control_type == while_control)
750 {
751 struct command_line *list;
752 fputs_filtered ("while ", gdb_stdout);
753 fputs_filtered (cmd->line, gdb_stdout);
754 fputs_filtered ("\n", gdb_stdout);
755 list = *cmd->body_list;
756 while (list)
757 {
758 print_command_line (list, depth + 1);
759 list = list->next;
760 }
761 }
762
763 /* An if command. Recursively print both arms before returning. */
764 if (cmd->control_type == if_control)
765 {
766 fputs_filtered ("if ", gdb_stdout);
767 fputs_filtered (cmd->line, gdb_stdout);
768 fputs_filtered ("\n", gdb_stdout);
769 /* The true arm. */
770 print_command_line (cmd->body_list[0], depth + 1);
771
772 /* Show the false arm if it exists. */
773 if (cmd->body_count == 2)
774 {
775 if (depth)
776 {
777 for (i = 0; i < depth; i++)
778 fputs_filtered (" ", gdb_stdout);
779 }
780 fputs_filtered ("else\n", gdb_stdout);
781 print_command_line (cmd->body_list[1], depth + 1);
782 }
783 if (depth)
784 {
785 for (i = 0; i < depth; i++)
786 fputs_filtered (" ", gdb_stdout);
787 }
788 fputs_filtered ("end\n", gdb_stdout);
789 }
790}
791
e52bfe0c
JL
792/* Execute the command in CMD. */
793
0f8cdd9b 794enum command_control_type
e52bfe0c
JL
795execute_control_command (cmd)
796 struct command_line *cmd;
797{
798 struct expression *expr;
799 struct command_line *current;
800 struct cleanup *old_chain = 0;
e52bfe0c
JL
801 value_ptr val;
802 int loop;
803 enum command_control_type ret;
0f8cdd9b 804 char *new_line;
e52bfe0c
JL
805
806 switch (cmd->control_type)
807 {
808 case simple_control:
809 /* A simple command, execute it and return. */
0f8cdd9b
JL
810 new_line = insert_args (cmd->line);
811 if (!new_line)
812 return invalid_control;
813 old_chain = make_cleanup (free_current_contents, &new_line);
814 execute_command (new_line, 0);
815 ret = cmd->control_type;
816 break;
e52bfe0c
JL
817
818 case continue_control:
819 case break_control:
820 /* Return for "continue", and "break" so we can either
821 continue the loop at the top, or break out. */
0f8cdd9b
JL
822 ret = cmd->control_type;
823 break;
e52bfe0c
JL
824
825 case while_control:
826 {
827 /* Parse the loop control expression for the while statement. */
0f8cdd9b
JL
828 new_line = insert_args (cmd->line);
829 if (!new_line)
830 return invalid_control;
831 old_chain = make_cleanup (free_current_contents, &new_line);
832 expr = parse_expression (new_line);
833 make_cleanup (free_current_contents, &expr);
834
e52bfe0c 835 ret = simple_control;
b5776522 836 loop = 1;
e52bfe0c
JL
837
838 /* Keep iterating so long as the expression is true. */
b5776522 839 while (loop == 1)
e52bfe0c 840 {
24ecc17a
JL
841 QUIT;
842
e52bfe0c
JL
843 /* Evaluate the expression. */
844 val = evaluate_expression (expr);
845
846 /* If the value is false, then break out of the loop. */
847 if (!value_true (val))
848 break;
849
850 /* Execute the body of the while statement. */
851 current = *cmd->body_list;
852 while (current)
853 {
854 ret = execute_control_command (current);
855
856 /* If we got an error, or a "break" command, then stop
857 looping. */
858 if (ret == invalid_control || ret == break_control)
859 {
b5776522 860 loop = 0;
e52bfe0c
JL
861 break;
862 }
863
864 /* If we got a "continue" command, then restart the loop
865 at this point. */
866 if (ret == continue_control)
867 break;
868
869 /* Get the next statement. */
870 current = current->next;
871 }
872 }
873
874 /* Reset RET so that we don't recurse the break all the way down. */
875 if (ret == break_control)
876 ret = simple_control;
877
878 break;
879 }
880
881 case if_control:
882 {
0f8cdd9b
JL
883 new_line = insert_args (cmd->line);
884 if (!new_line)
885 return invalid_control;
886 old_chain = make_cleanup (free_current_contents, &new_line);
e52bfe0c 887 /* Parse the conditional for the if statement. */
0f8cdd9b
JL
888 expr = parse_expression (new_line);
889 make_cleanup (free_current_contents, &expr);
e52bfe0c
JL
890
891 current = NULL;
892 ret = simple_control;
893
894 /* Evaluate the conditional. */
895 val = evaluate_expression (expr);
896
897 /* Choose which arm to take commands from based on the value of the
898 conditional expression. */
899 if (value_true (val))
900 current = *cmd->body_list;
901 else if (cmd->body_count == 2)
902 current = *(cmd->body_list + 1);
903
904 /* Execute commands in the given arm. */
905 while (current)
906 {
907 ret = execute_control_command (current);
908
909 /* If we got an error, get out. */
910 if (ret != simple_control)
911 break;
912
913 /* Get the next statement in the body. */
914 current = current->next;
915 }
0f8cdd9b 916
e52bfe0c
JL
917 break;
918 }
919
920 default:
921 warning ("Invalid control type in command structure.");
922 return invalid_control;
923 }
924
925 if (old_chain)
926 do_cleanups (old_chain);
927
928 return ret;
929}
930
931/* "while" command support. Executes a body of statements while the
932 loop condition is nonzero. */
933
934static void
935while_command (arg, from_tty)
936 char *arg;
937 int from_tty;
938{
939 struct command_line *command = NULL;
940
941 control_level = 1;
942 command = get_command_line (while_control, arg);
943
944 if (command == NULL)
945 return;
946
947 execute_control_command (command);
948 free_command_lines (&command);
949}
950
951/* "if" command support. Execute either the true or false arm depending
952 on the value of the if conditional. */
953
954static void
955if_command (arg, from_tty)
956 char *arg;
957 int from_tty;
958{
959 struct command_line *command = NULL;
960
961 control_level = 1;
962 command = get_command_line (if_control, arg);
963
964 if (command == NULL)
965 return;
966
967 execute_control_command (command);
968 free_command_lines (&command);
969}
970
0f8cdd9b
JL
971/* Cleanup */
972static void
973arg_cleanup ()
974{
975 struct user_args *oargs = user_args;
976 if (!user_args)
977 fatal ("Internal error, arg_cleanup called with no user args.\n");
978
979 user_args = user_args->next;
980 free (oargs);
981}
982
983/* Bind the incomming arguments for a user defined command to
984 $arg0, $arg1 ... $argMAXUSERARGS. */
985
986static struct cleanup *
987setup_user_args (p)
988 char *p;
989{
990 struct user_args *args;
991 struct cleanup *old_chain;
992 unsigned int arg_count = 0;
993
994 args = (struct user_args *)xmalloc (sizeof (struct user_args));
995 memset (args, 0, sizeof (struct user_args));
996
997 args->next = user_args;
998 user_args = args;
999
1000 old_chain = make_cleanup (arg_cleanup, 0);
1001
1002 if (p == NULL)
1003 return old_chain;
1004
1005 while (*p)
1006 {
1007 char *start_arg;
1008
1009 if (arg_count >= MAXUSERARGS)
1010 {
1011 error ("user defined function may only have %d arguments.\n",
1012 MAXUSERARGS);
1013 return old_chain;
1014 }
1015
1016 /* Strip whitespace. */
1017 while (*p == ' ' || *p == '\t')
1018 p++;
1019
1020 /* P now points to an argument. */
1021 start_arg = p;
1022 user_args->a[arg_count].arg = p;
1023
1024 /* Get to the end of this argument. */
1025 while (*p && *p != ' ' && *p != '\t')
1026 p++;
1027
1028 user_args->a[arg_count].len = p - start_arg;
1029 arg_count++;
1030 user_args->count++;
1031 }
1032 return old_chain;
1033}
1034
1035/* Given character string P, return a point to the first argument ($arg),
1036 or NULL if P contains no arguments. */
1037
1038static char *
1039locate_arg (p)
1040 char *p;
1041{
e3be225e 1042 while ((p = strchr (p, '$')))
0f8cdd9b
JL
1043 {
1044 if (strncmp (p, "$arg", 4) == 0 && isdigit (p[4]))
1045 return p;
1046 p++;
1047 }
1048 return NULL;
1049}
1050
1051/* Insert the user defined arguments stored in user_arg into the $arg
1052 arguments found in line, with the updated copy being placed into nline. */
1053
1054static char *
1055insert_args (line)
1056 char *line;
1057{
1058 char *p, *save_line, *new_line;
1059 unsigned len, i;
1060
1061 /* First we need to know how much memory to allocate for the new line. */
1062 save_line = line;
1063 len = 0;
e3be225e 1064 while ((p = locate_arg (line)))
0f8cdd9b
JL
1065 {
1066 len += p - line;
1067 i = p[4] - '0';
1068
1069 if (i >= user_args->count)
1070 {
1071 error ("Missing argument %d in user function.\n", i);
1072 return NULL;
1073 }
1074 len += user_args->a[i].len;
1075 line = p + 5;
1076 }
1077
1078 /* Don't forget the tail. */
1079 len += strlen (line);
1080
1081 /* Allocate space for the new line and fill it in. */
1082 new_line = (char *)xmalloc (len + 1);
1083 if (new_line == NULL)
1084 return NULL;
1085
1086 /* Restore pointer to beginning of old line. */
1087 line = save_line;
1088
1089 /* Save pointer to beginning of new line. */
1090 save_line = new_line;
1091
e3be225e 1092 while ((p = locate_arg (line)))
0f8cdd9b
JL
1093 {
1094 int i, len;
1095
1096 memcpy (new_line, line, p - line);
1097 new_line += p - line;
1098 i = p[4] - '0';
1099
e3be225e
SS
1100 len = user_args->a[i].len;
1101 if (len)
0f8cdd9b
JL
1102 {
1103 memcpy (new_line, user_args->a[i].arg, len);
1104 new_line += len;
1105 }
1106 line = p + 5;
1107 }
1108 /* Don't forget the tail. */
1109 strcpy (new_line, line);
1110
1111 /* Return a pointer to the beginning of the new line. */
1112 return save_line;
1113}
1114
172559ec
JK
1115void
1116execute_user_command (c, args)
1117 struct cmd_list_element *c;
1118 char *args;
1119{
1120 register struct command_line *cmdlines;
1121 struct cleanup *old_chain;
e52bfe0c
JL
1122 enum command_control_type ret;
1123
0f8cdd9b 1124 old_chain = setup_user_args (args);
172559ec
JK
1125
1126 cmdlines = c->user_commands;
1127 if (cmdlines == 0)
1128 /* Null command */
1129 return;
1130
1131 /* Set the instream to 0, indicating execution of a
1132 user-defined function. */
1133 old_chain = make_cleanup (source_cleanup, instream);
1134 instream = (FILE *) 0;
1135 while (cmdlines)
1136 {
e52bfe0c
JL
1137 ret = execute_control_command (cmdlines);
1138 if (ret != simple_control && ret != break_control)
1139 {
1140 warning ("Error in control structure.\n");
1141 break;
1142 }
172559ec
JK
1143 cmdlines = cmdlines->next;
1144 }
1145 do_cleanups (old_chain);
1146}
1147
1148/* Execute the line P as a command.
1149 Pass FROM_TTY as second argument to the defining function. */
1150
1151void
1152execute_command (p, from_tty)
1153 char *p;
1154 int from_tty;
1155{
1156 register struct cmd_list_element *c;
1157 register enum language flang;
1158 static int warned = 0;
e8f1ad9a 1159 extern FILE *serial_logfp;
172559ec
JK
1160
1161 free_all_values ();
1162
1163 /* This can happen when command_line_input hits end of file. */
1164 if (p == NULL)
1165 return;
e52bfe0c 1166
e8f1ad9a
FF
1167 if (serial_logfp != NULL)
1168 serial_log_command (p);
1169
172559ec
JK
1170 while (*p == ' ' || *p == '\t') p++;
1171 if (*p)
1172 {
1173 char *arg;
e52bfe0c 1174
172559ec
JK
1175 c = lookup_cmd (&p, cmdlist, "", 0, 1);
1176 /* Pass null arg rather than an empty one. */
1177 arg = *p ? p : 0;
1178
080868b4
PS
1179 /* Clear off trailing whitespace, except for set and complete command. */
1180 if (arg && c->type != set_cmd && c->function.cfunc != complete_command)
1181 {
1182 p = arg + strlen (arg) - 1;
1183 while (p >= arg && (*p == ' ' || *p == '\t'))
1184 p--;
1185 *(p + 1) = '\0';
1186 }
1187
172559ec
JK
1188 /* If this command has been hooked, run the hook first. */
1189 if (c->hook)
1190 execute_user_command (c->hook, (char *)0);
1191
1192 if (c->class == class_user)
1193 execute_user_command (c, arg);
1194 else if (c->type == set_cmd || c->type == show_cmd)
1195 do_setshow_command (arg, from_tty & caution, c);
1196 else if (c->function.cfunc == NO_FUNCTION)
1197 error ("That is not a command, just a help topic.");
479f0f18
SG
1198 else if (call_command_hook)
1199 call_command_hook (c, arg, from_tty & caution);
172559ec
JK
1200 else
1201 (*c->function.cfunc) (arg, from_tty & caution);
1202 }
1203
1204 /* Tell the user if the language has changed (except first time). */
1205 if (current_language != expected_language)
1206 {
1207 if (language_mode == language_mode_auto) {
1208 language_info (1); /* Print what changed. */
1209 }
1210 warned = 0;
1211 }
1212
1213 /* Warn the user if the working language does not match the
1214 language of the current frame. Only warn the user if we are
1215 actually running the program, i.e. there is a stack. */
1216 /* FIXME: This should be cacheing the frame and only running when
1217 the frame changes. */
43ab4ba5 1218
172559ec 1219 if (target_has_stack)
172559ec 1220 {
43ab4ba5
SS
1221 flang = get_frame_language ();
1222 if (!warned
1223 && flang != language_unknown
1224 && flang != current_language->la_language)
1225 {
1226 printf_filtered ("%s\n", lang_frame_mismatch_warn);
1227 warned = 1;
1228 }
172559ec 1229 }
172559ec
JK
1230}
1231
1232/* ARGSUSED */
1233static void
1234command_loop_marker (foo)
1235 int foo;
1236{
1237}
1238
1239/* Read commands from `instream' and execute them
1240 until end of file or error reading instream. */
43ab4ba5 1241
172559ec
JK
1242void
1243command_loop ()
1244{
1245 struct cleanup *old_chain;
1246 char *command;
1247 int stdin_is_tty = ISATTY (stdin);
43ab4ba5 1248 long time_at_cmd_start;
38d715a4 1249#ifdef HAVE_SBRK
83ebf439 1250 long space_at_cmd_start;
38d715a4 1251#endif
43ab4ba5
SS
1252 extern int display_time;
1253 extern int display_space;
172559ec 1254
41756e56 1255 while (instream && !feof (instream))
172559ec
JK
1256 {
1257 if (window_hook && instream == stdin)
1258 (*window_hook) (instream, prompt);
1259
1260 quit_flag = 0;
1261 if (instream == stdin && stdin_is_tty)
1262 reinitialize_more_filter ();
1263 old_chain = make_cleanup (command_loop_marker, 0);
1264 command = command_line_input (instream == stdin ? prompt : (char *) NULL,
1265 instream == stdin, "prompt");
1266 if (command == 0)
1267 return;
43ab4ba5
SS
1268
1269 time_at_cmd_start = get_run_time ();
1270
83ebf439
SS
1271 if (display_space)
1272 {
38d715a4 1273#ifdef HAVE_SBRK
83ebf439
SS
1274 extern char **environ;
1275 char *lim = (char *) sbrk (0);
1276
1277 space_at_cmd_start = (long) (lim - (char *) &environ);
38d715a4 1278#endif
83ebf439
SS
1279 }
1280
172559ec
JK
1281 execute_command (command, instream == stdin);
1282 /* Do any commands attached to breakpoint we stopped at. */
1283 bpstat_do_actions (&stop_bpstat);
1284 do_cleanups (old_chain);
43ab4ba5
SS
1285
1286 if (display_time)
1287 {
1288 long cmd_time = get_run_time () - time_at_cmd_start;
1289
1290 printf_unfiltered ("Command execution time: %ld.%06ld\n",
1291 cmd_time / 1000000, cmd_time % 1000000);
1292 }
1293
1294 if (display_space)
1295 {
38d715a4 1296#ifdef HAVE_SBRK
43ab4ba5
SS
1297 extern char **environ;
1298 char *lim = (char *) sbrk (0);
83ebf439
SS
1299 long space_now = lim - (char *) &environ;
1300 long space_diff = space_now - space_at_cmd_start;
43ab4ba5 1301
83ebf439
SS
1302 printf_unfiltered ("Space used: %ld (%c%ld for this command)\n",
1303 space_now,
1304 (space_diff >= 0 ? '+' : '-'),
1305 space_diff);
38d715a4 1306#endif
43ab4ba5 1307 }
172559ec
JK
1308 }
1309}
1310\f
1311/* Commands call this if they do not want to be repeated by null lines. */
1312
1313void
1314dont_repeat ()
1315{
1316 if (server_command)
1317 return;
1318
1319 /* If we aren't reading from standard input, we are saving the last
1320 thing read from stdin in line and don't want to delete it. Null lines
1321 won't repeat here in any case. */
1322 if (instream == stdin)
1323 *line = 0;
1324}
1325\f
1326/* Read a line from the stream "instream" without command line editing.
1327
1328 It prints PRROMPT once at the start.
e52bfe0c 1329 Action is compatible with "readline", e.g. space for the result is
172559ec
JK
1330 malloc'd and should be freed by the caller.
1331
1332 A NULL return means end of file. */
1333char *
1334gdb_readline (prrompt)
1335 char *prrompt;
1336{
1337 int c;
1338 char *result;
1339 int input_index = 0;
1340 int result_size = 80;
1341
1342 if (prrompt)
1343 {
1344 /* Don't use a _filtered function here. It causes the assumed
1345 character position to be off, since the newline we read from
1346 the user is not accounted for. */
1347 fputs_unfiltered (prrompt, gdb_stdout);
172559ec
JK
1348#ifdef MPW
1349 /* Move to a new line so the entered line doesn't have a prompt
1350 on the front of it. */
1351 fputs_unfiltered ("\n", gdb_stdout);
1352#endif /* MPW */
172559ec
JK
1353 gdb_flush (gdb_stdout);
1354 }
e52bfe0c 1355
172559ec
JK
1356 result = (char *) xmalloc (result_size);
1357
1358 while (1)
1359 {
1360 /* Read from stdin if we are executing a user defined command.
1361 This is the right thing for prompt_for_continue, at least. */
1362 c = fgetc (instream ? instream : stdin);
1363
1364 if (c == EOF)
1365 {
1366 if (input_index > 0)
1367 /* The last line does not end with a newline. Return it, and
1368 if we are called again fgetc will still return EOF and
1369 we'll return NULL then. */
1370 break;
1371 free (result);
1372 return NULL;
1373 }
1374
1375 if (c == '\n')
1376 break;
1377
1378 result[input_index++] = c;
1379 while (input_index >= result_size)
1380 {
1381 result_size *= 2;
1382 result = (char *) xrealloc (result, result_size);
1383 }
1384 }
1385
1386 result[input_index++] = '\0';
1387 return result;
1388}
1389
1390/* Variables which control command line editing and history
1391 substitution. These variables are given default values at the end
1392 of this file. */
1393static int command_editing_p;
1394static int history_expansion_p;
1395static int write_history_p;
1396static int history_size;
1397static char *history_filename;
1398
1399/* readline uses the word breaks for two things:
1400 (1) In figuring out where to point the TEXT parameter to the
1401 rl_completion_entry_function. Since we don't use TEXT for much,
1402 it doesn't matter a lot what the word breaks are for this purpose, but
1403 it does affect how much stuff M-? lists.
1404 (2) If one of the matches contains a word break character, readline
1405 will quote it. That's why we switch between
1406 gdb_completer_word_break_characters and
1407 gdb_completer_command_word_break_characters. I'm not sure when
1408 we need this behavior (perhaps for funky characters in C++ symbols?). */
1409
1410/* Variables which are necessary for fancy command line editing. */
1411char *gdb_completer_word_break_characters =
1412 " \t\n!@#$%^&*()+=|~`}{[]\"';:?/>.<,-";
1413
1414/* When completing on command names, we remove '-' from the list of
1415 word break characters, since we use it in command names. If the
1416 readline library sees one in any of the current completion strings,
1417 it thinks that the string needs to be quoted and automatically supplies
1418 a leading quote. */
1419char *gdb_completer_command_word_break_characters =
1420 " \t\n!@#$%^&*()+=|~`}{[]\"';:?/>.<,";
1421
1422/* Characters that can be used to quote completion strings. Note that we
1423 can't include '"' because the gdb C parser treats such quoted sequences
1424 as strings. */
1425char *gdb_completer_quote_characters =
1426 "'";
1427
1428/* Functions that are used as part of the fancy command line editing. */
1429
1430/* This can be used for functions which don't want to complete on symbols
1431 but don't want to complete on anything else either. */
1432/* ARGSUSED */
1433char **
1434noop_completer (text, prefix)
1435 char *text;
1436 char *prefix;
1437{
1438 return NULL;
1439}
1440
1441/* Complete on filenames. */
1442char **
1443filename_completer (text, word)
1444 char *text;
1445 char *word;
1446{
1447 /* From readline. */
1448 extern char *filename_completion_function ();
1449 int subsequent_name;
1450 char **return_val;
1451 int return_val_used;
1452 int return_val_alloced;
1453
1454 return_val_used = 0;
1455 /* Small for testing. */
1456 return_val_alloced = 1;
1457 return_val = (char **) xmalloc (return_val_alloced * sizeof (char *));
1458
1459 subsequent_name = 0;
1460 while (1)
1461 {
1462 char *p;
1463 p = filename_completion_function (text, subsequent_name);
1464 if (return_val_used >= return_val_alloced)
1465 {
1466 return_val_alloced *= 2;
1467 return_val =
1468 (char **) xrealloc (return_val,
1469 return_val_alloced * sizeof (char *));
1470 }
1471 if (p == NULL)
1472 {
1473 return_val[return_val_used++] = p;
1474 break;
1475 }
1476 /* Like emacs, don't complete on old versions. Especially useful
1477 in the "source" command. */
1478 if (p[strlen (p) - 1] == '~')
1479 continue;
1480
1481 {
1482 char *q;
1483 if (word == text)
1484 /* Return exactly p. */
1485 return_val[return_val_used++] = p;
1486 else if (word > text)
1487 {
1488 /* Return some portion of p. */
1489 q = xmalloc (strlen (p) + 5);
1490 strcpy (q, p + (word - text));
1491 return_val[return_val_used++] = q;
1492 free (p);
1493 }
1494 else
1495 {
1496 /* Return some of TEXT plus p. */
1497 q = xmalloc (strlen (p) + (text - word) + 5);
1498 strncpy (q, word, text - word);
1499 q[text - word] = '\0';
1500 strcat (q, p);
1501 return_val[return_val_used++] = q;
1502 free (p);
1503 }
1504 }
1505 subsequent_name = 1;
1506 }
1507#if 0
1508 /* There is no way to do this just long enough to affect quote inserting
1509 without also affecting the next completion. This should be fixed in
1510 readline. FIXME. */
1511 /* Insure that readline does the right thing
1512 with respect to inserting quotes. */
1513 rl_completer_word_break_characters = "";
1514#endif
1515 return return_val;
1516}
1517
1518/* Here are some useful test cases for completion. FIXME: These should
1519 be put in the test suite. They should be tested with both M-? and TAB.
1520
1521 "show output-" "radix"
1522 "show output" "-radix"
1523 "p" ambiguous (commands starting with p--path, print, printf, etc.)
1524 "p " ambiguous (all symbols)
1525 "info t foo" no completions
1526 "info t " no completions
1527 "info t" ambiguous ("info target", "info terminal", etc.)
1528 "info ajksdlfk" no completions
1529 "info ajksdlfk " no completions
1530 "info" " "
1531 "info " ambiguous (all info commands)
1532 "p \"a" no completions (string constant)
1533 "p 'a" ambiguous (all symbols starting with a)
1534 "p b-a" ambiguous (all symbols starting with a)
1535 "p b-" ambiguous (all symbols)
1536 "file Make" "file" (word break hard to screw up here)
1537 "file ../gdb.stabs/we" "ird" (needs to not break word at slash)
1538 */
1539
1540/* Generate completions one by one for the completer. Each time we are
9ed8604f
PS
1541 called return another potential completion to the caller.
1542 line_completion just completes on commands or passes the buck to the
1543 command's completer function, the stuff specific to symbol completion
172559ec
JK
1544 is in make_symbol_completion_list.
1545
9ed8604f 1546 TEXT is the caller's idea of the "word" we are looking at.
172559ec
JK
1547
1548 MATCHES is the number of matches that have currently been collected from
1549 calling this completion function. When zero, then we need to initialize,
1550 otherwise the initialization has already taken place and we can just
1551 return the next potential completion string.
1552
9ed8604f
PS
1553 LINE_BUFFER is available to be looked at; it contains the entire text
1554 of the line. POINT is the offset in that line of the cursor. You
1555 should pretend that the line ends at POINT.
172559ec 1556
9ed8604f
PS
1557 Returns NULL if there are no more completions, else a pointer to a string
1558 which is a possible completion, it is the caller's responsibility to
1559 free the string. */
172559ec
JK
1560
1561static char *
9ed8604f 1562line_completion_function (text, matches, line_buffer, point)
172559ec
JK
1563 char *text;
1564 int matches;
9ed8604f
PS
1565 char *line_buffer;
1566 int point;
172559ec
JK
1567{
1568 static char **list = (char **)NULL; /* Cache of completions */
1569 static int index; /* Next cached completion */
1570 char *output = NULL;
1571 char *tmp_command, *p;
1572 /* Pointer within tmp_command which corresponds to text. */
1573 char *word;
1574 struct cmd_list_element *c, *result_list;
1575
1576 if (matches == 0)
1577 {
1578 /* The caller is beginning to accumulate a new set of completions, so
1579 we need to find all of them now, and cache them for returning one at
1580 a time on future calls. */
1581
1582 if (list)
1583 {
1584 /* Free the storage used by LIST, but not by the strings inside.
1585 This is because rl_complete_internal () frees the strings. */
1586 free ((PTR)list);
1587 }
1588 list = 0;
1589 index = 0;
1590
1591 /* Choose the default set of word break characters to break completions.
1592 If we later find out that we are doing completions on command strings
1593 (as opposed to strings supplied by the individual command completer
1594 functions, which can be any string) then we will switch to the
1595 special word break set for command strings, which leaves out the
1596 '-' character used in some commands. */
1597
1598 rl_completer_word_break_characters =
1599 gdb_completer_word_break_characters;
1600
1601 /* Decide whether to complete on a list of gdb commands or on symbols. */
9ed8604f 1602 tmp_command = (char *) alloca (point + 1);
172559ec
JK
1603 p = tmp_command;
1604
9ed8604f
PS
1605 strncpy (tmp_command, line_buffer, point);
1606 tmp_command[point] = '\0';
172559ec 1607 /* Since text always contains some number of characters leading up
9ed8604f 1608 to point, we can find the equivalent position in tmp_command
172559ec 1609 by subtracting that many characters from the end of tmp_command. */
9ed8604f 1610 word = tmp_command + point - strlen (text);
172559ec 1611
9ed8604f 1612 if (point == 0)
172559ec
JK
1613 {
1614 /* An empty line we want to consider ambiguous; that is, it
1615 could be any command. */
1616 c = (struct cmd_list_element *) -1;
1617 result_list = 0;
1618 }
1619 else
1620 {
1621 c = lookup_cmd_1 (&p, cmdlist, &result_list, 1);
1622 }
1623
1624 /* Move p up to the next interesting thing. */
1625 while (*p == ' ' || *p == '\t')
1626 {
1627 p++;
1628 }
1629
1630 if (!c)
1631 {
1632 /* It is an unrecognized command. So there are no
1633 possible completions. */
1634 list = NULL;
1635 }
1636 else if (c == (struct cmd_list_element *) -1)
1637 {
1638 char *q;
1639
1640 /* lookup_cmd_1 advances p up to the first ambiguous thing, but
1641 doesn't advance over that thing itself. Do so now. */
1642 q = p;
1643 while (*q && (isalnum (*q) || *q == '-' || *q == '_'))
1644 ++q;
9ed8604f 1645 if (q != tmp_command + point)
172559ec
JK
1646 {
1647 /* There is something beyond the ambiguous
1648 command, so there are no possible completions. For
1649 example, "info t " or "info t foo" does not complete
1650 to anything, because "info t" can be "info target" or
1651 "info terminal". */
1652 list = NULL;
1653 }
1654 else
1655 {
1656 /* We're trying to complete on the command which was ambiguous.
1657 This we can deal with. */
1658 if (result_list)
1659 {
1660 list = complete_on_cmdlist (*result_list->prefixlist, p,
1661 word);
1662 }
1663 else
1664 {
1665 list = complete_on_cmdlist (cmdlist, p, word);
1666 }
1667 /* Insure that readline does the right thing with respect to
1668 inserting quotes. */
1669 rl_completer_word_break_characters =
1670 gdb_completer_command_word_break_characters;
1671 }
1672 }
1673 else
1674 {
1675 /* We've recognized a full command. */
1676
9ed8604f 1677 if (p == tmp_command + point)
172559ec
JK
1678 {
1679 /* There is no non-whitespace in the line beyond the command. */
1680
1681 if (p[-1] == ' ' || p[-1] == '\t')
1682 {
1683 /* The command is followed by whitespace; we need to complete
1684 on whatever comes after command. */
1685 if (c->prefixlist)
1686 {
1687 /* It is a prefix command; what comes after it is
1688 a subcommand (e.g. "info "). */
1689 list = complete_on_cmdlist (*c->prefixlist, p, word);
1690
1691 /* Insure that readline does the right thing
1692 with respect to inserting quotes. */
1693 rl_completer_word_break_characters =
1694 gdb_completer_command_word_break_characters;
1695 }
09374c98
SG
1696 else if (c->enums)
1697 {
1698 list = complete_on_enum (c->enums, p, word);
1699 rl_completer_word_break_characters =
1700 gdb_completer_command_word_break_characters;
1701 }
172559ec
JK
1702 else
1703 {
1704 /* It is a normal command; what comes after it is
1705 completed by the command's completer function. */
1706 list = (*c->completer) (p, word);
1707 }
1708 }
1709 else
1710 {
1711 /* The command is not followed by whitespace; we need to
1712 complete on the command itself. e.g. "p" which is a
1713 command itself but also can complete to "print", "ptype"
1714 etc. */
1715 char *q;
1716
1717 /* Find the command we are completing on. */
1718 q = p;
1719 while (q > tmp_command)
1720 {
1721 if (isalnum (q[-1]) || q[-1] == '-' || q[-1] == '_')
1722 --q;
1723 else
1724 break;
1725 }
1726
1727 list = complete_on_cmdlist (result_list, q, word);
1728
1729 /* Insure that readline does the right thing
1730 with respect to inserting quotes. */
1731 rl_completer_word_break_characters =
1732 gdb_completer_command_word_break_characters;
1733 }
1734 }
1735 else
1736 {
1737 /* There is non-whitespace beyond the command. */
1738
1739 if (c->prefixlist && !c->allow_unknown)
1740 {
1741 /* It is an unrecognized subcommand of a prefix command,
1742 e.g. "info adsfkdj". */
1743 list = NULL;
1744 }
09374c98
SG
1745 else if (c->enums)
1746 {
1747 list = complete_on_enum (c->enums, p, word);
1748 }
172559ec
JK
1749 else
1750 {
1751 /* It is a normal command. */
1752 list = (*c->completer) (p, word);
1753 }
1754 }
1755 }
1756 }
1757
1758 /* If we found a list of potential completions during initialization then
1759 dole them out one at a time. The vector of completions is NULL
1760 terminated, so after returning the last one, return NULL (and continue
1761 to do so) each time we are called after that, until a new list is
1762 available. */
1763
1764 if (list)
1765 {
1766 output = list[index];
1767 if (output)
1768 {
1769 index++;
1770 }
1771 }
1772
1773#if 0
1774 /* Can't do this because readline hasn't yet checked the word breaks
1775 for figuring out whether to insert a quote. */
1776 if (output == NULL)
1777 /* Make sure the word break characters are set back to normal for the
1778 next time that readline tries to complete something. */
1779 rl_completer_word_break_characters =
1780 gdb_completer_word_break_characters;
1781#endif
1782
1783 return (output);
1784}
1785
9ed8604f
PS
1786/* Line completion interface function for readline. */
1787
1788static char *
1789readline_line_completion_function (text, matches)
1790 char *text;
1791 int matches;
1792{
1793 return line_completion_function (text, matches, rl_line_buffer, rl_point);
1794}
1795
172559ec
JK
1796/* Skip over a possibly quoted word (as defined by the quote characters
1797 and word break characters the completer uses). Returns pointer to the
1798 location after the "word". */
1799
1800char *
1801skip_quoted (str)
1802 char *str;
1803{
1804 char quote_char = '\0';
1805 char *scan;
1806
1807 for (scan = str; *scan != '\0'; scan++)
1808 {
1809 if (quote_char != '\0')
1810 {
1811 /* Ignore everything until the matching close quote char */
1812 if (*scan == quote_char)
1813 {
1814 /* Found matching close quote. */
1815 scan++;
1816 break;
1817 }
1818 }
1819 else if (strchr (gdb_completer_quote_characters, *scan))
1820 {
1821 /* Found start of a quoted string. */
1822 quote_char = *scan;
1823 }
1824 else if (strchr (gdb_completer_word_break_characters, *scan))
1825 {
1826 break;
1827 }
1828 }
1829 return (scan);
1830}
1831
1832\f
1833#ifdef STOP_SIGNAL
1834static void
1835stop_sig (signo)
1836int signo;
1837{
1838#if STOP_SIGNAL == SIGTSTP
1839 signal (SIGTSTP, SIG_DFL);
1840 sigsetmask (0);
1841 kill (getpid (), SIGTSTP);
1842 signal (SIGTSTP, stop_sig);
1843#else
1844 signal (STOP_SIGNAL, stop_sig);
1845#endif
1846 printf_unfiltered ("%s", prompt);
1847 gdb_flush (gdb_stdout);
1848
1849 /* Forget about any previous command -- null line now will do nothing. */
1850 dont_repeat ();
1851}
1852#endif /* STOP_SIGNAL */
1853
1854/* Initialize signal handlers. */
1855static void
1856do_nothing (signo)
1857int signo;
1858{
1859}
1860
1861static void
1862init_signals ()
1863{
1864 signal (SIGINT, request_quit);
1865
255181a9
PS
1866 /* If SIGTRAP was set to SIG_IGN, then the SIG_IGN will get passed
1867 to the inferior and breakpoints will be ignored. */
1868#ifdef SIGTRAP
1869 signal (SIGTRAP, SIG_DFL);
1870#endif
1871
172559ec
JK
1872 /* If we initialize SIGQUIT to SIG_IGN, then the SIG_IGN will get
1873 passed to the inferior, which we don't want. It would be
1874 possible to do a "signal (SIGQUIT, SIG_DFL)" after we fork, but
1875 on BSD4.3 systems using vfork, that can affect the
1876 GDB process as well as the inferior (the signal handling tables
1877 might be in memory, shared between the two). Since we establish
1878 a handler for SIGQUIT, when we call exec it will set the signal
1879 to SIG_DFL for us. */
1880 signal (SIGQUIT, do_nothing);
1881 if (signal (SIGHUP, do_nothing) != SIG_IGN)
1882 signal (SIGHUP, disconnect);
1883 signal (SIGFPE, float_handler);
1884
1885#if defined(SIGWINCH) && defined(SIGWINCH_HANDLER)
1886 signal (SIGWINCH, SIGWINCH_HANDLER);
1887#endif
1888}
1889\f
1890/* Read one line from the command input stream `instream'
1891 into the local static buffer `linebuffer' (whose current length
1892 is `linelength').
1893 The buffer is made bigger as necessary.
1894 Returns the address of the start of the line.
1895
1896 NULL is returned for end of file.
1897
1898 *If* the instream == stdin & stdin is a terminal, the line read
1899 is copied into the file line saver (global var char *line,
1900 length linesize) so that it can be duplicated.
1901
1902 This routine either uses fancy command line editing or
1903 simple input as the user has requested. */
1904
1905char *
1906command_line_input (prrompt, repeat, annotation_suffix)
1907 char *prrompt;
1908 int repeat;
1909 char *annotation_suffix;
1910{
1911 static char *linebuffer = 0;
1912 static unsigned linelength = 0;
1913 register char *p;
1914 char *p1;
1915 char *rl;
1916 char *local_prompt = prrompt;
172559ec
JK
1917 char *nline;
1918 char got_eof = 0;
1919
dfb14bc8
SS
1920 /* The annotation suffix must be non-NULL. */
1921 if (annotation_suffix == NULL)
1922 annotation_suffix = "";
1923
172559ec
JK
1924 if (annotation_level > 1 && instream == stdin)
1925 {
1926 local_prompt = alloca ((prrompt == NULL ? 0 : strlen (prrompt))
1927 + strlen (annotation_suffix) + 40);
1928 if (prrompt == NULL)
1929 local_prompt[0] = '\0';
1930 else
1931 strcpy (local_prompt, prrompt);
1932 strcat (local_prompt, "\n\032\032");
1933 strcat (local_prompt, annotation_suffix);
1934 strcat (local_prompt, "\n");
1935 }
1936
1937 if (linebuffer == 0)
1938 {
1939 linelength = 80;
1940 linebuffer = (char *) xmalloc (linelength);
1941 }
1942
1943 p = linebuffer;
1944
1945 /* Control-C quits instantly if typed while in this loop
1946 since it should not wait until the user types a newline. */
1947 immediate_quit++;
1948#ifdef STOP_SIGNAL
1949 if (job_control)
1950 signal (STOP_SIGNAL, stop_sig);
1951#endif
1952
1953 while (1)
1954 {
1955 /* Make sure that all output has been output. Some machines may let
1956 you get away with leaving out some of the gdb_flush, but not all. */
1957 wrap_here ("");
1958 gdb_flush (gdb_stdout);
1959 gdb_flush (gdb_stderr);
1960
1961 if (source_file_name != NULL)
1962 {
1963 ++source_line_number;
1964 sprintf (source_error,
1965 "%s%s:%d: Error in sourced command file:\n",
1966 source_pre_error,
1967 source_file_name,
1968 source_line_number);
1969 error_pre_print = source_error;
1970 }
1971
1972 if (annotation_level > 1 && instream == stdin)
1973 {
1974 printf_unfiltered ("\n\032\032pre-");
1975 printf_unfiltered (annotation_suffix);
1976 printf_unfiltered ("\n");
1977 }
1978
1979 /* Don't use fancy stuff if not talking to stdin. */
41756e56
FF
1980 if (readline_hook && instream == NULL)
1981 {
1982 rl = (*readline_hook) (local_prompt);
1983 }
1984 else if (command_editing_p && instream == stdin && ISATTY (instream))
1985 {
1986 rl = readline (local_prompt);
1987 }
172559ec 1988 else
41756e56
FF
1989 {
1990 rl = gdb_readline (local_prompt);
1991 }
172559ec
JK
1992
1993 if (annotation_level > 1 && instream == stdin)
1994 {
1995 printf_unfiltered ("\n\032\032post-");
1996 printf_unfiltered (annotation_suffix);
1997 printf_unfiltered ("\n");
1998 }
1999
2000 if (!rl || rl == (char *) EOF)
2001 {
2002 got_eof = 1;
2003 break;
2004 }
2005 if (strlen(rl) + 1 + (p - linebuffer) > linelength)
2006 {
2007 linelength = strlen(rl) + 1 + (p - linebuffer);
2008 nline = (char *) xrealloc (linebuffer, linelength);
2009 p += nline - linebuffer;
2010 linebuffer = nline;
2011 }
2012 p1 = rl;
2013 /* Copy line. Don't copy null at end. (Leaves line alone
2014 if this was just a newline) */
2015 while (*p1)
2016 *p++ = *p1++;
2017
2018 free (rl); /* Allocated in readline. */
2019
2020 if (p == linebuffer || *(p - 1) != '\\')
2021 break;
2022
2023 p--; /* Put on top of '\'. */
2024 local_prompt = (char *) 0;
2025 }
2026
2027#ifdef STOP_SIGNAL
2028 if (job_control)
2029 signal (STOP_SIGNAL, SIG_DFL);
2030#endif
2031 immediate_quit--;
2032
2033 if (got_eof)
2034 return NULL;
2035
2036#define SERVER_COMMAND_LENGTH 7
2037 server_command =
2038 (p - linebuffer > SERVER_COMMAND_LENGTH)
2039 && STREQN (linebuffer, "server ", SERVER_COMMAND_LENGTH);
2040 if (server_command)
2041 {
2042 /* Note that we don't set `line'. Between this and the check in
2043 dont_repeat, this insures that repeating will still do the
2044 right thing. */
2045 *p = '\0';
2046 return linebuffer + SERVER_COMMAND_LENGTH;
2047 }
2048
2049 /* Do history expansion if that is wished. */
2050 if (history_expansion_p && instream == stdin
2051 && ISATTY (instream))
2052 {
2053 char *history_value;
2054 int expanded;
2055
2056 *p = '\0'; /* Insert null now. */
2057 expanded = history_expand (linebuffer, &history_value);
2058 if (expanded)
2059 {
2060 /* Print the changes. */
2061 printf_unfiltered ("%s\n", history_value);
2062
2063 /* If there was an error, call this function again. */
2064 if (expanded < 0)
2065 {
2066 free (history_value);
2067 return command_line_input (prrompt, repeat, annotation_suffix);
2068 }
2069 if (strlen (history_value) > linelength)
2070 {
2071 linelength = strlen (history_value) + 1;
2072 linebuffer = (char *) xrealloc (linebuffer, linelength);
2073 }
2074 strcpy (linebuffer, history_value);
2075 p = linebuffer + strlen(linebuffer);
2076 free (history_value);
2077 }
2078 }
2079
2080 /* If we just got an empty line, and that is supposed
2081 to repeat the previous command, return the value in the
2082 global buffer. */
f2ed3a80
PB
2083 if (repeat && p == linebuffer)
2084 return line;
2085 for (p1 = linebuffer; *p1 == ' ' || *p1 == '\t'; p1++) ;
2086 if (repeat && !*p1)
2087 return line;
172559ec
JK
2088
2089 *p = 0;
2090
2091 /* Add line to history if appropriate. */
2092 if (instream == stdin
2093 && ISATTY (stdin) && *linebuffer)
2094 add_history (linebuffer);
2095
2096 /* Note: lines consisting solely of comments are added to the command
2097 history. This is useful when you type a command, and then
2098 realize you don't want to execute it quite yet. You can comment
2099 out the command and then later fetch it from the value history
2100 and remove the '#'. The kill ring is probably better, but some
2101 people are in the habit of commenting things out. */
f2ed3a80
PB
2102 if (*p1 == '#')
2103 *p1 = '\0'; /* Found a comment. */
172559ec
JK
2104
2105 /* Save into global buffer if appropriate. */
2106 if (repeat)
2107 {
2108 if (linelength > linesize)
2109 {
2110 line = xrealloc (line, linelength);
2111 linesize = linelength;
2112 }
2113 strcpy (line, linebuffer);
2114 return line;
2115 }
2116
2117 return linebuffer;
2118}
2119\f
e52bfe0c
JL
2120
2121/* Expand the body_list of COMMAND so that it can hold NEW_LENGTH
2122 code bodies. This is typically used when we encounter an "else"
2123 clause for an "if" command. */
2124
2125static void
2126realloc_body_list (command, new_length)
2127 struct command_line *command;
2128 int new_length;
2129{
2130 int n;
2131 struct command_line **body_list;
2132
2133 n = command->body_count;
2134
2135 /* Nothing to do? */
2136 if (new_length <= n)
2137 return;
2138
2139 body_list = (struct command_line **)
2140 xmalloc (sizeof (struct command_line *) * new_length);
2141
2142 memcpy (body_list, command->body_list, sizeof (struct command_line *) * n);
2143
2144 free (command->body_list);
2145 command->body_list = body_list;
2146 command->body_count = new_length;
2147}
2148
2149/* Read one line from the input stream. If the command is an "else" or
2150 "end", return such an indication to the caller. */
2151
2152static enum misc_command_type
2153read_next_line (command)
2154 struct command_line **command;
2155{
2156 char *p, *p1, *prompt_ptr, control_prompt[256];
2157 int i = 0;
2158
2159 if (control_level >= 254)
2160 error ("Control nesting too deep!\n");
2161
2162 /* Set a prompt based on the nesting of the control commands. */
41756e56 2163 if (instream == stdin || (instream == 0 && readline_hook != NULL))
e52bfe0c
JL
2164 {
2165 for (i = 0; i < control_level; i++)
2166 control_prompt[i] = ' ';
2167 control_prompt[i] = '>';
2168 control_prompt[i+1] = '\0';
2169 prompt_ptr = (char *)&control_prompt[0];
2170 }
2171 else
2172 prompt_ptr = NULL;
2173
e04f8901 2174 p = command_line_input (prompt_ptr, instream == stdin, "commands");
e52bfe0c
JL
2175
2176 /* Not sure what to do here. */
2177 if (p == NULL)
2178 return end_command;
2179
2180 /* Strip leading and trailing whitespace. */
2181 while (*p == ' ' || *p == '\t')
2182 p++;
2183
2184 p1 = p + strlen (p);
2185 while (p1 != p && (p1[-1] == ' ' || p1[-1] == '\t'))
2186 p1--;
2187
2188 /* Blanks and comments don't really do anything, but we need to
2189 distinguish them from else, end and other commands which can be
2190 executed. */
2191 if (p1 == p || p[0] == '#')
2192 return nop_command;
2193
2194 /* Is this the end of a simple, while, or if control structure? */
2195 if (p1 - p == 3 && !strncmp (p, "end", 3))
2196 return end_command;
2197
2198 /* Is the else clause of an if control structure? */
2199 if (p1 - p == 4 && !strncmp (p, "else", 4))
2200 return else_command;
2201
2202 /* Check for while, if, break, continue, etc and build a new command
2203 line structure for them. */
2204 if (p1 - p > 5 && !strncmp (p, "while", 5))
2205 *command = build_command_line (while_control, p + 6);
2206 else if (p1 - p > 2 && !strncmp (p, "if", 2))
2207 *command = build_command_line (if_control, p + 3);
ecebd693 2208 else if (p1 - p == 10 && !strncmp (p, "loop_break", 10))
e52bfe0c
JL
2209 {
2210 *command = (struct command_line *)
2211 xmalloc (sizeof (struct command_line));
2212 (*command)->next = NULL;
2213 (*command)->line = NULL;
2214 (*command)->control_type = break_control;
2215 (*command)->body_count = 0;
2216 (*command)->body_list = NULL;
2217 }
ecebd693 2218 else if (p1 - p == 13 && !strncmp (p, "loop_continue", 13))
e52bfe0c
JL
2219 {
2220 *command = (struct command_line *)
2221 xmalloc (sizeof (struct command_line));
2222 (*command)->next = NULL;
2223 (*command)->line = NULL;
2224 (*command)->control_type = continue_control;
2225 (*command)->body_count = 0;
2226 (*command)->body_list = NULL;
2227 }
2228 else
2229 {
2230 /* A normal command. */
2231 *command = (struct command_line *)
2232 xmalloc (sizeof (struct command_line));
2233 (*command)->next = NULL;
2234 (*command)->line = savestring (p, p1 - p);
2235 (*command)->control_type = simple_control;
2236 (*command)->body_count = 0;
2237 (*command)->body_list = NULL;
2238 }
2239
2240 /* Nothing special. */
2241 return ok_command;
2242}
2243
2244/* Recursively read in the control structures and create a command_line
41756e56 2245 structure from them.
e52bfe0c
JL
2246
2247 The parent_control parameter is the control structure in which the
2248 following commands are nested. */
2249
2250static enum command_control_type
2251recurse_read_control_structure (current_cmd)
2252 struct command_line *current_cmd;
2253{
2254 int current_body, i;
2255 enum misc_command_type val;
2256 enum command_control_type ret;
2257 struct command_line **body_ptr, *child_tail, *next;
e52bfe0c 2258
e52bfe0c
JL
2259 child_tail = NULL;
2260 current_body = 1;
2261
2262 /* Sanity checks. */
2263 if (current_cmd->control_type == simple_control)
2264 {
2265 error ("Recursed on a simple control type\n");
2266 return invalid_control;
2267 }
2268
2269 if (current_body > current_cmd->body_count)
2270 {
2271 error ("Allocated body is smaller than this command type needs\n");
2272 return invalid_control;
2273 }
2274
2275 /* Read lines from the input stream and build control structures. */
2276 while (1)
2277 {
2278 dont_repeat ();
2279
2280 next = NULL;
2281 val = read_next_line (&next);
2282
2283 /* Just skip blanks and comments. */
2284 if (val == nop_command)
2285 continue;
2286
2287 if (val == end_command)
2288 {
2289 if (current_cmd->control_type == while_control
2290 || current_cmd->control_type == if_control)
2291 {
2292 /* Success reading an entire control structure. */
2293 ret = simple_control;
2294 break;
2295 }
2296 else
2297 {
2298 ret = invalid_control;
2299 break;
2300 }
2301 }
2302
2303 /* Not the end of a control structure. */
2304 if (val == else_command)
2305 {
2306 if (current_cmd->control_type == if_control
2307 && current_body == 1)
2308 {
2309 realloc_body_list (current_cmd, 2);
2310 current_body = 2;
2311 child_tail = NULL;
2312 continue;
2313 }
2314 else
2315 {
2316 ret = invalid_control;
2317 break;
2318 }
2319 }
2320
2321 if (child_tail)
2322 {
2323 child_tail->next = next;
2324 }
2325 else
2326 {
e52bfe0c
JL
2327 body_ptr = current_cmd->body_list;
2328 for (i = 1; i < current_body; i++)
2329 body_ptr++;
2330
2331 *body_ptr = next;
2332
e52bfe0c
JL
2333 }
2334
2335 child_tail = next;
2336
2337 /* If the latest line is another control structure, then recurse
2338 on it. */
2339 if (next->control_type == while_control
2340 || next->control_type == if_control)
2341 {
2342 control_level++;
2343 ret = recurse_read_control_structure (next);
2344 control_level--;
2345
2346 if (ret != simple_control)
2347 break;
2348 }
2349 }
2350
2351 dont_repeat ();
e52bfe0c
JL
2352
2353 return ret;
2354}
2355
41756e56
FF
2356/* Read lines from the input stream and accumulate them in a chain of
2357 struct command_line's, which is then returned. For input from a
2358 terminal, the special command "end" is used to mark the end of the
2359 input, and is not included in the returned chain of commands. */
e52bfe0c 2360
41756e56 2361#define END_MESSAGE "End with a line saying just \"end\"."
172559ec
JK
2362
2363struct command_line *
41756e56
FF
2364read_command_lines (prompt, from_tty)
2365char *prompt;
2366int from_tty;
172559ec 2367{
e52bfe0c
JL
2368 struct command_line *head, *tail, *next;
2369 struct cleanup *old_chain;
2370 enum command_control_type ret;
2371 enum misc_command_type val;
2372
41756e56
FF
2373 if (readline_begin_hook)
2374 {
2375 /* Note - intentional to merge messages with no newline */
2376 (*readline_begin_hook) ("%s %s\n", prompt, END_MESSAGE);
2377 }
2378 else if (from_tty && input_from_terminal_p ())
2379 {
2380 printf_unfiltered ("%s\n%s\n", prompt, END_MESSAGE);
2381 gdb_flush (gdb_stdout);
2382 }
2383
e52bfe0c
JL
2384 head = tail = NULL;
2385 old_chain = NULL;
172559ec
JK
2386
2387 while (1)
2388 {
e52bfe0c 2389 val = read_next_line (&next);
172559ec 2390
e52bfe0c
JL
2391 /* Ignore blank lines or comments. */
2392 if (val == nop_command)
2393 continue;
2394
2395 if (val == end_command)
2396 {
2397 ret = simple_control;
2398 break;
2399 }
2400
2401 if (val != ok_command)
2402 {
2403 ret = invalid_control;
2404 break;
2405 }
2406
2407 if (next->control_type == while_control
2408 || next->control_type == if_control)
2409 {
2410 control_level++;
2411 ret = recurse_read_control_structure (next);
2412 control_level--;
172559ec 2413
e52bfe0c
JL
2414 if (ret == invalid_control)
2415 break;
2416 }
2417
172559ec
JK
2418 if (tail)
2419 {
2420 tail->next = next;
2421 }
2422 else
2423 {
e52bfe0c
JL
2424 head = next;
2425 old_chain = make_cleanup (free_command_lines, &head);
172559ec
JK
2426 }
2427 tail = next;
2428 }
2429
2430 dont_repeat ();
2431
e52bfe0c
JL
2432 if (head)
2433 {
2434 if (ret != invalid_control)
2435 {
2436 discard_cleanups (old_chain);
e52bfe0c
JL
2437 }
2438 else
2439 do_cleanups (old_chain);
2440 }
2441
41756e56
FF
2442 if (readline_end_hook)
2443 {
2444 (*readline_end_hook) ();
2445 }
2446 return (head);
172559ec
JK
2447}
2448
2449/* Free a chain of struct command_line's. */
2450
2451void
2452free_command_lines (lptr)
2453 struct command_line **lptr;
2454{
2455 register struct command_line *l = *lptr;
2456 register struct command_line *next;
e52bfe0c
JL
2457 struct command_line **blist;
2458 int i;
172559ec
JK
2459
2460 while (l)
2461 {
e52bfe0c
JL
2462 if (l->body_count > 0)
2463 {
2464 blist = l->body_list;
2465 for (i = 0; i < l->body_count; i++, blist++)
2466 free_command_lines (blist);
2467 }
172559ec
JK
2468 next = l->next;
2469 free (l->line);
2470 free ((PTR)l);
2471 l = next;
2472 }
2473}
2474\f
2475/* Add an element to the list of info subcommands. */
2476
2477void
2478add_info (name, fun, doc)
2479 char *name;
2480 void (*fun) PARAMS ((char *, int));
2481 char *doc;
2482{
2483 add_cmd (name, no_class, fun, doc, &infolist);
2484}
2485
2486/* Add an alias to the list of info subcommands. */
2487
2488void
2489add_info_alias (name, oldname, abbrev_flag)
2490 char *name;
2491 char *oldname;
2492 int abbrev_flag;
2493{
2494 add_alias_cmd (name, oldname, 0, abbrev_flag, &infolist);
2495}
2496
2497/* The "info" command is defined as a prefix, with allow_unknown = 0.
2498 Therefore, its own definition is called only for "info" with no args. */
2499
2500/* ARGSUSED */
2501static void
2502info_command (arg, from_tty)
2503 char *arg;
2504 int from_tty;
2505{
2506 printf_unfiltered ("\"info\" must be followed by the name of an info command.\n");
2507 help_list (infolist, "info ", -1, gdb_stdout);
2508}
2509
2510/* The "complete" command is used by Emacs to implement completion. */
2511
2512/* ARGSUSED */
2513static void
2514complete_command (arg, from_tty)
2515 char *arg;
2516 int from_tty;
2517{
2518 int i;
9ed8604f 2519 int argpoint;
172559ec
JK
2520 char *completion;
2521
2522 dont_repeat ();
2523
2524 if (arg == NULL)
9ed8604f
PS
2525 arg = "";
2526 argpoint = strlen (arg);
172559ec 2527
9ed8604f 2528 for (completion = line_completion_function (arg, i = 0, arg, argpoint);
172559ec 2529 completion;
9ed8604f
PS
2530 completion = line_completion_function (arg, ++i, arg, argpoint))
2531 {
2532 printf_unfiltered ("%s\n", completion);
2533 free (completion);
2534 }
172559ec
JK
2535}
2536
2537/* The "show" command with no arguments shows all the settings. */
2538
2539/* ARGSUSED */
2540static void
2541show_command (arg, from_tty)
2542 char *arg;
2543 int from_tty;
2544{
2545 cmd_show_list (showlist, from_tty, "");
2546}
2547\f
2548/* Add an element to the list of commands. */
2549
2550void
2551add_com (name, class, fun, doc)
2552 char *name;
2553 enum command_class class;
2554 void (*fun) PARAMS ((char *, int));
2555 char *doc;
2556{
2557 add_cmd (name, class, fun, doc, &cmdlist);
2558}
2559
2560/* Add an alias or abbreviation command to the list of commands. */
2561
2562void
2563add_com_alias (name, oldname, class, abbrev_flag)
2564 char *name;
2565 char *oldname;
2566 enum command_class class;
2567 int abbrev_flag;
2568{
2569 add_alias_cmd (name, oldname, class, abbrev_flag, &cmdlist);
2570}
2571
2572void
2573error_no_arg (why)
2574 char *why;
2575{
2576 error ("Argument required (%s).", why);
2577}
2578
2579/* ARGSUSED */
2580static void
2581help_command (command, from_tty)
2582 char *command;
2583 int from_tty; /* Ignored */
2584{
2585 help_cmd (command, gdb_stdout);
2586}
2587\f
2588static void
2589validate_comname (comname)
2590 char *comname;
2591{
2592 register char *p;
2593
2594 if (comname == 0)
2595 error_no_arg ("name of command to define");
2596
2597 p = comname;
2598 while (*p)
2599 {
080868b4 2600 if (!isalnum(*p) && *p != '-' && *p != '_')
172559ec
JK
2601 error ("Junk in argument list: \"%s\"", p);
2602 p++;
2603 }
2604}
2605
2606/* This is just a placeholder in the command data structures. */
2607static void
2608user_defined_command (ignore, from_tty)
2609 char *ignore;
2610 int from_tty;
2611{
2612}
2613
2614static void
2615define_command (comname, from_tty)
2616 char *comname;
2617 int from_tty;
2618{
2619 register struct command_line *cmds;
2620 register struct cmd_list_element *c, *newc, *hookc = 0;
2621 char *tem = comname;
41756e56 2622 char tmpbuf[128];
172559ec
JK
2623#define HOOK_STRING "hook-"
2624#define HOOK_LEN 5
2625
2626 validate_comname (comname);
2627
2628 /* Look it up, and verify that we got an exact match. */
2629 c = lookup_cmd (&tem, cmdlist, "", -1, 1);
2630 if (c && !STREQ (comname, c->name))
2631 c = 0;
e52bfe0c 2632
172559ec
JK
2633 if (c)
2634 {
2635 if (c->class == class_user || c->class == class_alias)
2636 tem = "Redefine command \"%s\"? ";
2637 else
2638 tem = "Really redefine built-in command \"%s\"? ";
2639 if (!query (tem, c->name))
2640 error ("Command \"%s\" not redefined.", c->name);
2641 }
2642
2643 /* If this new command is a hook, then mark the command which it
2644 is hooking. Note that we allow hooking `help' commands, so that
2645 we can hook the `stop' pseudo-command. */
2646
2647 if (!strncmp (comname, HOOK_STRING, HOOK_LEN))
2648 {
2649 /* Look up cmd it hooks, and verify that we got an exact match. */
2650 tem = comname+HOOK_LEN;
2651 hookc = lookup_cmd (&tem, cmdlist, "", -1, 0);
2652 if (hookc && !STREQ (comname+HOOK_LEN, hookc->name))
2653 hookc = 0;
2654 if (!hookc)
2655 {
2656 warning ("Your new `%s' command does not hook any existing command.",
2657 comname);
e3033bb0 2658 if (!query ("Proceed? "))
172559ec
JK
2659 error ("Not confirmed.");
2660 }
2661 }
2662
2663 comname = savestring (comname, strlen (comname));
2664
e52bfe0c 2665 /* If the rest of the commands will be case insensitive, this one
172559ec
JK
2666 should behave in the same manner. */
2667 for (tem = comname; *tem; tem++)
2668 if (isupper(*tem)) *tem = tolower(*tem);
2669
e52bfe0c 2670 control_level = 0;
41756e56
FF
2671 sprintf (tmpbuf, "Type commands for definition of \"%s\".", comname);
2672 cmds = read_command_lines (tmpbuf, from_tty);
172559ec
JK
2673
2674 if (c && c->class == class_user)
2675 free_command_lines (&c->user_commands);
2676
2677 newc = add_cmd (comname, class_user, user_defined_command,
2678 (c && c->class == class_user)
2679 ? c->doc : savestring ("User-defined.", 13), &cmdlist);
2680 newc->user_commands = cmds;
2681
2682 /* If this new command is a hook, then mark both commands as being
2683 tied. */
2684 if (hookc)
2685 {
2686 hookc->hook = newc; /* Target gets hooked. */
2687 newc->hookee = hookc; /* We are marked as hooking target cmd. */
2688 }
2689}
2690
2691static void
2692document_command (comname, from_tty)
2693 char *comname;
2694 int from_tty;
2695{
2696 struct command_line *doclines;
2697 register struct cmd_list_element *c;
2698 char *tem = comname;
41756e56 2699 char tmpbuf[128];
172559ec
JK
2700
2701 validate_comname (comname);
2702
2703 c = lookup_cmd (&tem, cmdlist, "", 0, 1);
2704
2705 if (c->class != class_user)
2706 error ("Command \"%s\" is built-in.", comname);
2707
41756e56
FF
2708 sprintf (tmpbuf, "Type documentation for \"%s\".", comname);
2709 doclines = read_command_lines (tmpbuf, from_tty);
172559ec
JK
2710
2711 if (c->doc) free (c->doc);
2712
2713 {
2714 register struct command_line *cl1;
2715 register int len = 0;
2716
2717 for (cl1 = doclines; cl1; cl1 = cl1->next)
2718 len += strlen (cl1->line) + 1;
2719
2720 c->doc = (char *) xmalloc (len + 1);
2721 *c->doc = 0;
2722
2723 for (cl1 = doclines; cl1; cl1 = cl1->next)
2724 {
2725 strcat (c->doc, cl1->line);
2726 if (cl1->next)
2727 strcat (c->doc, "\n");
2728 }
2729 }
2730
2731 free_command_lines (&doclines);
2732}
2733\f
2734void
2735print_gnu_advertisement ()
2736{
2737 printf_unfiltered ("\
2738GDB is free software and you are welcome to distribute copies of it\n\
2739 under certain conditions; type \"show copying\" to see the conditions.\n\
2740There is absolutely no warranty for GDB; type \"show warranty\" for details.\n\
2741");
2742}
2743
2744void
2745print_gdb_version (stream)
2746 GDB_FILE *stream;
2747{
2748 fprintf_filtered (stream, "\
2749GDB %s (%s", version, host_name);
2750
2751 if (!STREQ (host_name, target_name))
2752 fprintf_filtered (stream, " --target %s", target_name);
2753
2754 fprintf_filtered (stream, "), ");
2755 wrap_here("");
f935141e 2756 fprintf_filtered (stream, "Copyright 1996 Free Software Foundation, Inc.");
172559ec
JK
2757}
2758
2759/* ARGSUSED */
2760static void
2761show_version (args, from_tty)
2762 char *args;
2763 int from_tty;
2764{
2765 immediate_quit++;
2766 print_gnu_advertisement ();
2767 print_gdb_version (gdb_stdout);
2768 printf_filtered ("\n");
2769 immediate_quit--;
2770}
2771\f
2772/* xgdb calls this to reprint the usual GDB prompt. Obsolete now that xgdb
2773 is obsolete. */
2774
2775void
2776print_prompt ()
2777{
2778 printf_unfiltered ("%s", prompt);
2779 gdb_flush (gdb_stdout);
2780}
2781\f
2782void
2783quit_command (args, from_tty)
2784 char *args;
2785 int from_tty;
2786{
641c7fdf
SS
2787 int exit_code = 0;
2788
2789 /* An optional expression may be used to cause gdb to terminate with the
2790 value of that expression. */
2791 if (args)
2792 {
2793 value_ptr val = parse_and_eval (args);
2794
2795 exit_code = (int) value_as_long (val);
2796 }
2797
172559ec
JK
2798 if (inferior_pid != 0 && target_has_execution)
2799 {
2800 if (attach_flag)
2801 {
2802 if (query ("The program is running. Quit anyway (and detach it)? "))
2803 target_detach (args, from_tty);
2804 else
2805 error ("Not confirmed.");
2806 }
2807 else
2808 {
2809 if (query ("The program is running. Quit anyway (and kill it)? "))
2810 target_kill ();
2811 else
2812 error ("Not confirmed.");
2813 }
2814 }
2815 /* UDI wants this, to kill the TIP. */
2816 target_close (1);
2817
2818 /* Save the history information if it is appropriate to do so. */
2819 if (write_history_p && history_filename)
2820 write_history (history_filename);
2821
641c7fdf 2822 exit (exit_code);
172559ec
JK
2823}
2824
2825/* Returns whether GDB is running on a terminal and whether the user
2826 desires that questions be asked of them on that terminal. */
2827
2828int
2829input_from_terminal_p ()
2830{
2831 return gdb_has_a_terminal () && (instream == stdin) & caution;
2832}
2833\f
2834/* ARGSUSED */
2835static void
2836pwd_command (args, from_tty)
2837 char *args;
2838 int from_tty;
2839{
2840 if (args) error ("The \"pwd\" command does not take an argument: %s", args);
b7ec5b8d 2841 getcwd (gdb_dirbuf, sizeof (gdb_dirbuf));
172559ec 2842
b7ec5b8d 2843 if (!STREQ (gdb_dirbuf, current_directory))
172559ec 2844 printf_unfiltered ("Working directory %s\n (canonically %s).\n",
b7ec5b8d 2845 current_directory, gdb_dirbuf);
172559ec
JK
2846 else
2847 printf_unfiltered ("Working directory %s.\n", current_directory);
2848}
2849
2850void
2851cd_command (dir, from_tty)
2852 char *dir;
2853 int from_tty;
2854{
2855 int len;
2856 /* Found something other than leading repetitions of "/..". */
2857 int found_real_path;
2858 char *p;
2859
2860 /* If the new directory is absolute, repeat is a no-op; if relative,
2861 repeat might be useful but is more likely to be a mistake. */
2862 dont_repeat ();
2863
2864 if (dir == 0)
2865 error_no_arg ("new working directory");
2866
2867 dir = tilde_expand (dir);
2868 make_cleanup (free, dir);
2869
2870 if (chdir (dir) < 0)
2871 perror_with_name (dir);
2872
2873 len = strlen (dir);
2e1cc801
SS
2874 dir = savestring (dir, len - (len > 1 && SLASH_P(dir[len-1])));
2875 if (ROOTED_P(dir))
172559ec
JK
2876 current_directory = dir;
2877 else
2878 {
2e1cc801 2879 if (SLASH_P (current_directory[0]) && current_directory[1] == '\0')
172559ec
JK
2880 current_directory = concat (current_directory, dir, NULL);
2881 else
2e1cc801 2882 current_directory = concat (current_directory, SLASH_STRING, dir, NULL);
172559ec
JK
2883 free (dir);
2884 }
2885
2886 /* Now simplify any occurrences of `.' and `..' in the pathname. */
2887
2888 found_real_path = 0;
2889 for (p = current_directory; *p;)
2890 {
2e1cc801 2891 if (SLASH_P (p[0]) && p[1] == '.' && (p[2] == 0 || SLASH_P (p[2])))
172559ec 2892 strcpy (p, p + 2);
2e1cc801
SS
2893 else if (SLASH_P (p[0]) && p[1] == '.' && p[2] == '.'
2894 && (p[3] == 0 || SLASH_P (p[3])))
172559ec
JK
2895 {
2896 if (found_real_path)
2897 {
2898 /* Search backwards for the directory just before the "/.."
2899 and obliterate it and the "/..". */
2900 char *q = p;
2e1cc801 2901 while (q != current_directory && ! SLASH_P (q[-1]))
172559ec
JK
2902 --q;
2903
2904 if (q == current_directory)
2905 /* current_directory is
2906 a relative pathname ("can't happen"--leave it alone). */
2907 ++p;
2908 else
2909 {
2910 strcpy (q - 1, p + 3);
2911 p = q - 1;
2912 }
2913 }
2914 else
2915 /* We are dealing with leading repetitions of "/..", for example
2916 "/../..", which is the Mach super-root. */
2917 p += 3;
2918 }
2919 else
2920 {
2921 found_real_path = 1;
2922 ++p;
2923 }
2924 }
2925
2926 forget_cached_source_info ();
2927
2928 if (from_tty)
2929 pwd_command ((char *) 0, 1);
2930}
2931\f
2932struct source_cleanup_lines_args {
2933 int old_line;
2934 char *old_file;
2935 char *old_pre_error;
2936 char *old_error_pre_print;
2937};
2938
2939static void
2940source_cleanup_lines (args)
2941 PTR args;
2942{
2943 struct source_cleanup_lines_args *p =
2944 (struct source_cleanup_lines_args *)args;
2945 source_line_number = p->old_line;
2946 source_file_name = p->old_file;
2947 source_pre_error = p->old_pre_error;
2948 error_pre_print = p->old_error_pre_print;
2949}
2950
2951/* ARGSUSED */
2952void
2953source_command (args, from_tty)
2954 char *args;
2955 int from_tty;
2956{
2957 FILE *stream;
2958 struct cleanup *old_cleanups;
2959 char *file = args;
2960 struct source_cleanup_lines_args old_lines;
2961 int needed_length;
2962
2963 if (file == NULL)
2964 {
2965 error ("source command requires pathname of file to source.");
2966 }
2967
2968 file = tilde_expand (file);
2969 old_cleanups = make_cleanup (free, file);
2970
2971 stream = fopen (file, FOPEN_RT);
2972 if (stream == 0)
2973 perror_with_name (file);
2974
2975 make_cleanup (fclose, stream);
2976
2977 old_lines.old_line = source_line_number;
2978 old_lines.old_file = source_file_name;
2979 old_lines.old_pre_error = source_pre_error;
2980 old_lines.old_error_pre_print = error_pre_print;
2981 make_cleanup (source_cleanup_lines, &old_lines);
2982 source_line_number = 0;
2983 source_file_name = file;
2984 source_pre_error = error_pre_print == NULL ? "" : error_pre_print;
2985 source_pre_error = savestring (source_pre_error, strlen (source_pre_error));
2986 make_cleanup (free, source_pre_error);
2987 /* This will get set every time we read a line. So it won't stay "" for
2988 long. */
2989 error_pre_print = "";
2990
2991 needed_length = strlen (source_file_name) + strlen (source_pre_error) + 80;
2992 if (source_error_allocated < needed_length)
2993 {
2994 source_error_allocated *= 2;
2995 if (source_error_allocated < needed_length)
2996 source_error_allocated = needed_length;
2997 if (source_error == NULL)
2998 source_error = xmalloc (source_error_allocated);
2999 else
3000 source_error = xrealloc (source_error, source_error_allocated);
3001 }
3002
3003 read_command_file (stream);
3004
3005 do_cleanups (old_cleanups);
3006}
3007
3008/* ARGSUSED */
3009static void
3010echo_command (text, from_tty)
3011 char *text;
3012 int from_tty;
3013{
3014 char *p = text;
3015 register int c;
3016
3017 if (text)
3018 while ((c = *p++) != '\0')
3019 {
3020 if (c == '\\')
3021 {
3022 /* \ at end of argument is used after spaces
3023 so they won't be lost. */
3024 if (*p == 0)
3025 return;
3026
3027 c = parse_escape (&p);
3028 if (c >= 0)
3029 printf_filtered ("%c", c);
3030 }
3031 else
3032 printf_filtered ("%c", c);
3033 }
3034
3035 /* Force this output to appear now. */
3036 wrap_here ("");
3037 gdb_flush (gdb_stdout);
3038}
3039
cd10c7e3
SG
3040/* ARGSUSED */
3041static void
3042dont_repeat_command (ignored, from_tty)
3043 char *ignored;
3044 int from_tty;
3045{
3046 *line = 0; /* Can't call dont_repeat here because we're not
3047 necessarily reading from stdin. */
3048}
b8176214
ILT
3049\f
3050#ifdef TARGET_BYTE_ORDER_SELECTABLE
3051
3052/* Functions to manipulate the endianness of the target. */
3053
3054#ifndef TARGET_BYTE_ORDER_DEFAULT
3055#define TARGET_BYTE_ORDER_DEFAULT BIG_ENDIAN
3056#endif
3057
3058int target_byte_order = TARGET_BYTE_ORDER_DEFAULT;
3059
b83ed019
ILT
3060static int target_byte_order_auto = 1;
3061
b8176214
ILT
3062/* Called if the user enters ``set endian'' without an argument. */
3063static void
3064set_endian (args, from_tty)
3065 char *args;
3066 int from_tty;
3067{
b83ed019 3068 printf_unfiltered ("\"set endian\" must be followed by \"auto\", \"big\" or \"little\".\n");
b8176214
ILT
3069 show_endian (args, from_tty);
3070}
3071
3072/* Called by ``set endian big''. */
3073static void
3074set_endian_big (args, from_tty)
3075 char *args;
3076 int from_tty;
3077{
3078 target_byte_order = BIG_ENDIAN;
b83ed019 3079 target_byte_order_auto = 0;
b8176214
ILT
3080}
3081
3082/* Called by ``set endian little''. */
3083static void
3084set_endian_little (args, from_tty)
3085 char *args;
3086 int from_tty;
3087{
3088 target_byte_order = LITTLE_ENDIAN;
b83ed019
ILT
3089 target_byte_order_auto = 0;
3090}
3091
3092/* Called by ``set endian auto''. */
3093static void
3094set_endian_auto (args, from_tty)
3095 char *args;
3096 int from_tty;
3097{
3098 target_byte_order_auto = 1;
b8176214
ILT
3099}
3100
3101/* Called by ``show endian''. */
3102static void
3103show_endian (args, from_tty)
3104 char *args;
3105 int from_tty;
3106{
b83ed019
ILT
3107 const char *msg =
3108 (target_byte_order_auto
3109 ? "The target endianness is set automatically (currently %s endian)\n"
3110 : "The target is assumed to be %s endian\n");
032f80d8 3111 printf_unfiltered ((char *) msg, TARGET_BYTE_ORDER == BIG_ENDIAN ? "big" : "little");
b8176214
ILT
3112}
3113
3114#endif /* defined (TARGET_BYTE_ORDER_SELECTABLE) */
b83ed019
ILT
3115
3116/* Set the endianness from a BFD. */
3117void
3118set_endian_from_file (abfd)
3119 bfd *abfd;
3120{
3121#ifdef TARGET_BYTE_ORDER_SELECTABLE
3122 int want;
3123
3ecd62ca 3124 if (bfd_big_endian (abfd))
b83ed019
ILT
3125 want = BIG_ENDIAN;
3126 else
3127 want = LITTLE_ENDIAN;
3128 if (target_byte_order_auto)
3129 target_byte_order = want;
3130 else if (target_byte_order != want)
3131 warning ("%s endian file does not match %s endian target.",
3132 want == BIG_ENDIAN ? "big" : "little",
3133 TARGET_BYTE_ORDER == BIG_ENDIAN ? "big" : "little");
3134
3135#else /* ! defined (TARGET_BYTE_ORDER_SELECTABLE) */
3136
3ecd62ca 3137 if (bfd_big_endian (abfd)
b83ed019
ILT
3138 ? TARGET_BYTE_ORDER != BIG_ENDIAN
3139 : TARGET_BYTE_ORDER == BIG_ENDIAN)
3140 warning ("%s endian file does not match %s endian target.",
3ecd62ca 3141 bfd_big_endian (abfd) ? "big" : "little",
b83ed019
ILT
3142 TARGET_BYTE_ORDER == BIG_ENDIAN ? "big" : "little");
3143
3144#endif /* ! defined (TARGET_BYTE_ORDER_SELECTABLE) */
3145}
172559ec
JK
3146\f
3147/* Functions to manipulate command line editing control variables. */
3148
3149/* Number of commands to print in each call to show_commands. */
3150#define Hist_print 10
3151static void
3152show_commands (args, from_tty)
3153 char *args;
3154 int from_tty;
3155{
3156 /* Index for history commands. Relative to history_base. */
3157 int offset;
3158
3159 /* Number of the history entry which we are planning to display next.
3160 Relative to history_base. */
3161 static int num = 0;
3162
3163 /* The first command in the history which doesn't exist (i.e. one more
3164 than the number of the last command). Relative to history_base. */
3165 int hist_len;
3166
3167 extern HIST_ENTRY *history_get PARAMS ((int));
3168
3169 /* Print out some of the commands from the command history. */
3170 /* First determine the length of the history list. */
3171 hist_len = history_size;
3172 for (offset = 0; offset < history_size; offset++)
3173 {
3174 if (!history_get (history_base + offset))
3175 {
3176 hist_len = offset;
3177 break;
3178 }
3179 }
3180
3181 if (args)
3182 {
3183 if (args[0] == '+' && args[1] == '\0')
3184 /* "info editing +" should print from the stored position. */
3185 ;
3186 else
3187 /* "info editing <exp>" should print around command number <exp>. */
3188 num = (parse_and_eval_address (args) - history_base) - Hist_print / 2;
3189 }
3190 /* "show commands" means print the last Hist_print commands. */
3191 else
3192 {
3193 num = hist_len - Hist_print;
3194 }
3195
3196 if (num < 0)
3197 num = 0;
3198
3199 /* If there are at least Hist_print commands, we want to display the last
3200 Hist_print rather than, say, the last 6. */
3201 if (hist_len - num < Hist_print)
3202 {
3203 num = hist_len - Hist_print;
3204 if (num < 0)
3205 num = 0;
3206 }
3207
3208 for (offset = num; offset < num + Hist_print && offset < hist_len; offset++)
3209 {
3210 printf_filtered ("%5d %s\n", history_base + offset,
3211 (history_get (history_base + offset))->line);
3212 }
3213
3214 /* The next command we want to display is the next one that we haven't
3215 displayed yet. */
3216 num += Hist_print;
e52bfe0c 3217
172559ec
JK
3218 /* If the user repeats this command with return, it should do what
3219 "show commands +" does. This is unnecessary if arg is null,
3220 because "show commands +" is not useful after "show commands". */
3221 if (from_tty && args)
3222 {
3223 args[0] = '+';
3224 args[1] = '\0';
3225 }
3226}
3227
3228/* Called by do_setshow_command. */
3229/* ARGSUSED */
3230static void
3231set_history_size_command (args, from_tty, c)
3232 char *args;
3233 int from_tty;
3234 struct cmd_list_element *c;
3235{
3236 if (history_size == INT_MAX)
3237 unstifle_history ();
3238 else if (history_size >= 0)
3239 stifle_history (history_size);
3240 else
3241 {
3242 history_size = INT_MAX;
3243 error ("History size must be non-negative");
3244 }
3245}
3246
3247/* ARGSUSED */
3248static void
3249set_history (args, from_tty)
3250 char *args;
3251 int from_tty;
3252{
3253 printf_unfiltered ("\"set history\" must be followed by the name of a history subcommand.\n");
3254 help_list (sethistlist, "set history ", -1, gdb_stdout);
3255}
3256
3257/* ARGSUSED */
3258static void
3259show_history (args, from_tty)
3260 char *args;
3261 int from_tty;
3262{
3263 cmd_show_list (showhistlist, from_tty, "");
3264}
3265
3266int info_verbose = 0; /* Default verbose msgs off */
3267
3268/* Called by do_setshow_command. An elaborate joke. */
3269/* ARGSUSED */
e52bfe0c 3270static void
172559ec
JK
3271set_verbose (args, from_tty, c)
3272 char *args;
3273 int from_tty;
3274 struct cmd_list_element *c;
3275{
3276 char *cmdname = "verbose";
3277 struct cmd_list_element *showcmd;
e52bfe0c 3278
172559ec
JK
3279 showcmd = lookup_cmd_1 (&cmdname, showlist, NULL, 1);
3280
3281 if (info_verbose)
3282 {
3283 c->doc = "Set verbose printing of informational messages.";
3284 showcmd->doc = "Show verbose printing of informational messages.";
3285 }
3286 else
3287 {
3288 c->doc = "Set verbosity.";
3289 showcmd->doc = "Show verbosity.";
3290 }
3291}
3292
3293static void
3294float_handler (signo)
3295int signo;
3296{
3297 /* This message is based on ANSI C, section 4.7. Note that integer
3298 divide by zero causes this, so "float" is a misnomer. */
3299 signal (SIGFPE, float_handler);
3300 error ("Erroneous arithmetic operation.");
3301}
3302
172559ec
JK
3303\f
3304static void
3305init_cmd_lists ()
3306{
3307 cmdlist = NULL;
3308 infolist = NULL;
3309 enablelist = NULL;
3310 disablelist = NULL;
3311 deletelist = NULL;
3312 enablebreaklist = NULL;
3313 setlist = NULL;
3314 unsetlist = NULL;
3315 showlist = NULL;
b8176214
ILT
3316#ifdef TARGET_BYTE_ORDER_SELECTABLE
3317 endianlist = NULL;
3318#endif
172559ec
JK
3319 sethistlist = NULL;
3320 showhistlist = NULL;
3321 unsethistlist = NULL;
3322#if MAINTENANCE_CMDS
3323 maintenancelist = NULL;
3324 maintenanceinfolist = NULL;
3325 maintenanceprintlist = NULL;
3326#endif
3327 setprintlist = NULL;
3328 showprintlist = NULL;
3329 setchecklist = NULL;
3330 showchecklist = NULL;
3331}
3332
3333/* Init the history buffer. Note that we are called after the init file(s)
3334 * have been read so that the user can change the history file via his
3335 * .gdbinit file (for instance). The GDBHISTFILE environment variable
3336 * overrides all of this.
3337 */
3338
3339void
3340init_history()
3341{
3342 char *tmpenv;
3343
3344 tmpenv = getenv ("HISTSIZE");
3345 if (tmpenv)
3346 history_size = atoi (tmpenv);
3347 else if (!history_size)
3348 history_size = 256;
3349
3350 stifle_history (history_size);
3351
3352 tmpenv = getenv ("GDBHISTFILE");
3353 if (tmpenv)
3354 history_filename = savestring (tmpenv, strlen(tmpenv));
3355 else if (!history_filename) {
3356 /* We include the current directory so that if the user changes
3357 directories the file written will be the same as the one
3358 that was read. */
3359 history_filename = concat (current_directory, "/.gdb_history", NULL);
3360 }
3361 read_history (history_filename);
3362}
3363
3364static void
3365init_main ()
3366{
3367 struct cmd_list_element *c;
e52bfe0c 3368
b8176214
ILT
3369#ifdef TARGET_BYTE_ORDER_SELECTABLE
3370
3371 add_prefix_cmd ("endian", class_support, set_endian,
3372 "Set endianness of target.",
3373 &endianlist, "set endian ", 0, &setlist);
3374 add_cmd ("big", class_support, set_endian_big,
3375 "Set target as being big endian.", &endianlist);
3376 add_cmd ("little", class_support, set_endian_little,
3377 "Set target as being little endian.", &endianlist);
b83ed019
ILT
3378 add_cmd ("auto", class_support, set_endian_auto,
3379 "Select target endianness automatically.", &endianlist);
b8176214
ILT
3380 add_cmd ("endian", class_support, show_endian,
3381 "Show endianness of target.", &showlist);
3382
3383#endif /* defined (TARGET_BYTE_ORDER_SELECTABLE) */
3384
172559ec
JK
3385#ifdef DEFAULT_PROMPT
3386 prompt = savestring (DEFAULT_PROMPT, strlen(DEFAULT_PROMPT));
3387#else
3388 prompt = savestring ("(gdb) ", 6);
3389#endif
3390
3391 /* Set the important stuff up for command editing. */
3392 command_editing_p = 1;
3393 history_expansion_p = 0;
3394 write_history_p = 0;
e52bfe0c 3395
172559ec 3396 /* Setup important stuff for command line editing. */
9ed8604f 3397 rl_completion_entry_function = (int (*)()) readline_line_completion_function;
172559ec
JK
3398 rl_completer_word_break_characters = gdb_completer_word_break_characters;
3399 rl_completer_quote_characters = gdb_completer_quote_characters;
3400 rl_readline_name = "gdb";
3401
3402 /* Define the classes of commands.
3403 They will appear in the help list in the reverse of this order. */
3404
3405 add_cmd ("internals", class_maintenance, NO_FUNCTION,
3406 "Maintenance commands.\n\
3407Some gdb commands are provided just for use by gdb maintainers.\n\
3408These commands are subject to frequent change, and may not be as\n\
3409well documented as user commands.",
3410 &cmdlist);
3411 add_cmd ("obscure", class_obscure, NO_FUNCTION, "Obscure features.", &cmdlist);
3412 add_cmd ("aliases", class_alias, NO_FUNCTION, "Aliases of other commands.", &cmdlist);
3413 add_cmd ("user-defined", class_user, NO_FUNCTION, "User-defined commands.\n\
3414The commands in this class are those defined by the user.\n\
3415Use the \"define\" command to define a command.", &cmdlist);
3416 add_cmd ("support", class_support, NO_FUNCTION, "Support facilities.", &cmdlist);
3417 add_cmd ("status", class_info, NO_FUNCTION, "Status inquiries.", &cmdlist);
3418 add_cmd ("files", class_files, NO_FUNCTION, "Specifying and examining files.", &cmdlist);
3419 add_cmd ("breakpoints", class_breakpoint, NO_FUNCTION, "Making program stop at certain points.", &cmdlist);
3420 add_cmd ("data", class_vars, NO_FUNCTION, "Examining data.", &cmdlist);
3421 add_cmd ("stack", class_stack, NO_FUNCTION, "Examining the stack.\n\
3422The stack is made up of stack frames. Gdb assigns numbers to stack frames\n\
3423counting from zero for the innermost (currently executing) frame.\n\n\
3424At any time gdb identifies one frame as the \"selected\" frame.\n\
3425Variable lookups are done with respect to the selected frame.\n\
3426When the program being debugged stops, gdb selects the innermost frame.\n\
3427The commands below can be used to select other frames by number or address.",
3428 &cmdlist);
3429 add_cmd ("running", class_run, NO_FUNCTION, "Running the program.", &cmdlist);
3430
3431 add_com ("pwd", class_files, pwd_command,
3432 "Print working directory. This is used for your program as well.");
3433 c = add_cmd ("cd", class_files, cd_command,
3434 "Set working directory to DIR for debugger and program being debugged.\n\
3435The change does not take effect for the program being debugged\n\
3436until the next time it is started.", &cmdlist);
3437 c->completer = filename_completer;
3438
3439 add_show_from_set
3440 (add_set_cmd ("prompt", class_support, var_string, (char *)&prompt,
3441 "Set gdb's prompt",
3442 &setlist),
3443 &showlist);
e52bfe0c 3444
172559ec
JK
3445 add_com ("echo", class_support, echo_command,
3446 "Print a constant string. Give string as argument.\n\
3447C escape sequences may be used in the argument.\n\
3448No newline is added at the end of the argument;\n\
3449use \"\\n\" if you want a newline to be printed.\n\
3450Since leading and trailing whitespace are ignored in command arguments,\n\
3451if you want to print some you must use \"\\\" before leading whitespace\n\
3452to be printed or after trailing whitespace.");
3453 add_com ("document", class_support, document_command,
3454 "Document a user-defined command.\n\
3455Give command name as argument. Give documentation on following lines.\n\
3456End with a line of just \"end\".");
3457 add_com ("define", class_support, define_command,
3458 "Define a new command name. Command name is argument.\n\
3459Definition appears on following lines, one command per line.\n\
3460End with a line of just \"end\".\n\
3461Use the \"document\" command to give documentation for the new command.\n\
59528079 3462Commands defined in this way may have up to ten arguments.");
172559ec
JK
3463
3464#ifdef __STDC__
3465 c = add_cmd ("source", class_support, source_command,
3466 "Read commands from a file named FILE.\n\
3467Note that the file \"" GDBINIT_FILENAME "\" is read automatically in this way\n\
3468when gdb is started.", &cmdlist);
3469#else
3470 /* Punt file name, we can't help it easily. */
3471 c = add_cmd ("source", class_support, source_command,
3472 "Read commands from a file named FILE.\n\
3473Note that the file \".gdbinit\" is read automatically in this way\n\
3474when gdb is started.", &cmdlist);
3475#endif
3476 c->completer = filename_completer;
3477
3478 add_com ("quit", class_support, quit_command, "Exit gdb.");
3479 add_com ("help", class_support, help_command, "Print list of commands.");
3480 add_com_alias ("q", "quit", class_support, 1);
3481 add_com_alias ("h", "help", class_support, 1);
3482
cd10c7e3
SG
3483 add_com ("dont-repeat", class_support, dont_repeat_command, "Don't repeat this command.\n\
3484Primarily used inside of user-defined commands that should not be repeated when\n\
3485hitting return.");
172559ec
JK
3486
3487 c = add_set_cmd ("verbose", class_support, var_boolean, (char *)&info_verbose,
3488 "Set ",
3489 &setlist),
3490 add_show_from_set (c, &showlist);
3491 c->function.sfunc = set_verbose;
3492 set_verbose (NULL, 0, c);
e52bfe0c 3493
172559ec
JK
3494 add_show_from_set
3495 (add_set_cmd ("editing", class_support, var_boolean, (char *)&command_editing_p,
3496 "Set editing of command lines as they are typed.\n\
3497Use \"on\" to enable to enable the editing, and \"off\" to disable it.\n\
3498Without an argument, command line editing is enabled. To edit, use\n\
3499EMACS-like or VI-like commands like control-P or ESC.", &setlist),
3500 &showlist);
3501
3502 add_prefix_cmd ("history", class_support, set_history,
3503 "Generic command for setting command history parameters.",
3504 &sethistlist, "set history ", 0, &setlist);
3505 add_prefix_cmd ("history", class_support, show_history,
3506 "Generic command for showing command history parameters.",
3507 &showhistlist, "show history ", 0, &showlist);
3508
3509 add_show_from_set
3510 (add_set_cmd ("expansion", no_class, var_boolean, (char *)&history_expansion_p,
3511 "Set history expansion on command input.\n\
3512Without an argument, history expansion is enabled.", &sethistlist),
3513 &showhistlist);
3514
3515 add_show_from_set
3516 (add_set_cmd ("save", no_class, var_boolean, (char *)&write_history_p,
3517 "Set saving of the history record on exit.\n\
3518Use \"on\" to enable to enable the saving, and \"off\" to disable it.\n\
3519Without an argument, saving is enabled.", &sethistlist),
3520 &showhistlist);
3521
3522 c = add_set_cmd ("size", no_class, var_integer, (char *)&history_size,
3523 "Set the size of the command history, \n\
3524ie. the number of previous commands to keep a record of.", &sethistlist);
3525 add_show_from_set (c, &showhistlist);
3526 c->function.sfunc = set_history_size_command;
3527
3528 add_show_from_set
3529 (add_set_cmd ("filename", no_class, var_filename, (char *)&history_filename,
3530 "Set the filename in which to record the command history\n\
3531 (the list of previous commands of which a record is kept).", &sethistlist),
3532 &showhistlist);
3533
3534 add_show_from_set
3535 (add_set_cmd ("confirm", class_support, var_boolean,
3536 (char *)&caution,
3537 "Set whether to confirm potentially dangerous operations.",
3538 &setlist),
3539 &showlist);
3540
3541 add_prefix_cmd ("info", class_info, info_command,
3542 "Generic command for showing things about the program being debugged.",
3543 &infolist, "info ", 0, &cmdlist);
3544 add_com_alias ("i", "info", class_info, 1);
3545
3546 add_com ("complete", class_obscure, complete_command,
3547 "List the completions for the rest of the line as a command.");
3548
3549 add_prefix_cmd ("show", class_info, show_command,
3550 "Generic command for showing things about the debugger.",
3551 &showlist, "show ", 0, &cmdlist);
3552 /* Another way to get at the same thing. */
3553 add_info ("set", show_command, "Show all GDB settings.");
3554
3555 add_cmd ("commands", no_class, show_commands,
943b7032 3556 "Show the history of commands you typed.\n\
172559ec
JK
3557You can supply a command number to start with, or a `+' to start after\n\
3558the previous command number shown.",
3559 &showlist);
3560
3561 add_cmd ("version", no_class, show_version,
3562 "Show what version of GDB this is.", &showlist);
3563
e52bfe0c
JL
3564 add_com ("while", class_support, while_command,
3565"Execute nested commands WHILE the conditional expression is non zero.\n\
943b7032
PS
3566The conditional expression must follow the word `while' and must in turn be\n\
3567followed by a new line. The nested commands must be entered one per line,\n\
e52bfe0c
JL
3568and should be terminated by the word `end'.");
3569
3570 add_com ("if", class_support, if_command,
3571"Execute nested commands once IF the conditional expression is non zero.\n\
943b7032
PS
3572The conditional expression must follow the word `if' and must in turn be\n\
3573followed by a new line. The nested commands must be entered one per line,\n\
3574and should be terminated by the word 'else' or `end'. If an else clause\n\
e52bfe0c
JL
3575is used, the same rules apply to its nested commands as to the first ones.");
3576
172559ec
JK
3577 /* If target is open when baud changes, it doesn't take effect until the
3578 next open (I think, not sure). */
3579 add_show_from_set (add_set_cmd ("remotebaud", no_class,
3580 var_zinteger, (char *)&baud_rate,
3581 "Set baud rate for remote serial I/O.\n\
3582This value is used to set the speed of the serial port when debugging\n\
3583using remote targets.", &setlist),
3584 &showlist);
3585
3586 add_show_from_set (
3587 add_set_cmd ("remotedebug", no_class, var_zinteger, (char *)&remote_debug,
3588 "Set debugging of remote protocol.\n\
3589When enabled, each packet sent or received with the remote target\n\
3590is displayed.", &setlist),
3591 &showlist);
3592}
This page took 0.267499 seconds and 4 git commands to generate.