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