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