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