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