1 /* Handle lists of commands, their decoding and documentation, for GDB.
2 Copyright 1986, 1989, 1990, 1991, 1998 Free Software Foundation, Inc.
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place - Suite 330,
17 Boston, MA 02111-1307, USA. */
24 #include "gdb_string.h"
32 #ifdef HAVE_SYS_WAIT_H
39 /* FIXME: this should be auto-configured! */
44 /* Prototypes for local functions */
46 static void undef_cmd_error
PARAMS ((char *, char *));
48 static void show_user
PARAMS ((char *, int));
50 static void show_user_1
PARAMS ((struct cmd_list_element
*, GDB_FILE
*));
52 static void make_command
PARAMS ((char *, int));
54 static void shell_escape
PARAMS ((char *, int));
56 static int parse_binary_operation
PARAMS ((char *));
58 static void print_doc_line
PARAMS ((GDB_FILE
*, char *));
60 static struct cmd_list_element
*find_cmd
PARAMS ((char *command
,
62 struct cmd_list_element
* clist
,
63 int ignore_help_classes
,
66 void _initialize_command
PARAMS ((void));
68 /* Add element named NAME.
69 CLASS is the top level category into which commands are broken down
71 FUN should be the function to execute the command;
72 it will get a character string as argument, with leading
73 and trailing blanks already eliminated.
75 DOC is a documentation string for the command.
76 Its first line should be a complete sentence.
77 It should start with ? for a command that is an abbreviation
78 or with * for a command that most users don't need to know about.
80 Add this command to command list *LIST.
82 Returns a pointer to the added command (not necessarily the head
85 struct cmd_list_element
*
86 add_cmd (name
, class, fun
, doc
, list
)
88 enum command_class
class;
89 void (*fun
) PARAMS ((char *, int));
91 struct cmd_list_element
**list
;
93 register struct cmd_list_element
*c
94 = (struct cmd_list_element
*) xmalloc (sizeof (struct cmd_list_element
));
95 struct cmd_list_element
*p
;
97 delete_cmd (name
, list
);
99 if (*list
== NULL
|| STRCMP ((*list
)->name
, name
) >= 0)
107 while (p
->next
&& STRCMP (p
->next
->name
, name
) <= 0)
117 c
->function
.cfunc
= fun
;
120 c
->prefixlist
= NULL
;
121 c
->prefixname
= NULL
;
122 c
->allow_unknown
= 0;
124 c
->completer
= make_symbol_completion_list
;
125 c
->type
= not_set_cmd
;
127 c
->var_type
= var_boolean
;
129 c
->user_commands
= NULL
;
131 c
->cmd_pointer
= NULL
;
136 /* Same as above, except that the abbrev_flag is set. */
138 #if 0 /* Currently unused */
140 struct cmd_list_element
*
141 add_abbrev_cmd (name
, class, fun
, doc
, list
)
143 enum command_class
class;
144 void (*fun
) PARAMS ((char *, int));
146 struct cmd_list_element
**list
;
148 register struct cmd_list_element
*c
149 = add_cmd (name
, class, fun
, doc
, list
);
157 struct cmd_list_element
*
158 add_alias_cmd (name
, oldname
, class, abbrev_flag
, list
)
161 enum command_class
class;
163 struct cmd_list_element
**list
;
165 /* Must do this since lookup_cmd tries to side-effect its first arg */
167 register struct cmd_list_element
*old
;
168 register struct cmd_list_element
*c
;
169 copied_name
= (char *) alloca (strlen (oldname
) + 1);
170 strcpy (copied_name
, oldname
);
171 old
= lookup_cmd (&copied_name
, *list
, "", 1, 1);
175 delete_cmd (name
, list
);
179 c
= add_cmd (name
, class, old
->function
.cfunc
, old
->doc
, list
);
180 c
->prefixlist
= old
->prefixlist
;
181 c
->prefixname
= old
->prefixname
;
182 c
->allow_unknown
= old
->allow_unknown
;
183 c
->abbrev_flag
= abbrev_flag
;
184 c
->cmd_pointer
= old
;
188 /* Like add_cmd but adds an element for a command prefix:
189 a name that should be followed by a subcommand to be looked up
190 in another command list. PREFIXLIST should be the address
191 of the variable containing that list. */
193 struct cmd_list_element
*
194 add_prefix_cmd (name
, class, fun
, doc
, prefixlist
, prefixname
,
197 enum command_class
class;
198 void (*fun
) PARAMS ((char *, int));
200 struct cmd_list_element
**prefixlist
;
203 struct cmd_list_element
**list
;
205 register struct cmd_list_element
*c
= add_cmd (name
, class, fun
, doc
, list
);
206 c
->prefixlist
= prefixlist
;
207 c
->prefixname
= prefixname
;
208 c
->allow_unknown
= allow_unknown
;
212 /* Like add_prefix_cmd but sets the abbrev_flag on the new command. */
214 struct cmd_list_element
*
215 add_abbrev_prefix_cmd (name
, class, fun
, doc
, prefixlist
, prefixname
,
218 enum command_class
class;
219 void (*fun
) PARAMS ((char *, int));
221 struct cmd_list_element
**prefixlist
;
224 struct cmd_list_element
**list
;
226 register struct cmd_list_element
*c
= add_cmd (name
, class, fun
, doc
, list
);
227 c
->prefixlist
= prefixlist
;
228 c
->prefixname
= prefixname
;
229 c
->allow_unknown
= allow_unknown
;
234 /* This is an empty "cfunc". */
236 not_just_help_class_command (args
, from_tty
)
242 /* This is an empty "sfunc". */
243 static void empty_sfunc
PARAMS ((char *, int, struct cmd_list_element
*));
246 empty_sfunc (args
, from_tty
, c
)
249 struct cmd_list_element
*c
;
253 /* Add element named NAME to command list LIST (the list for set
254 or some sublist thereof).
255 CLASS is as in add_cmd.
256 VAR_TYPE is the kind of thing we are setting.
257 VAR is address of the variable being controlled by this command.
258 DOC is the documentation string. */
260 struct cmd_list_element
*
261 add_set_cmd (name
, class, var_type
, var
, doc
, list
)
263 enum command_class
class;
267 struct cmd_list_element
**list
;
269 struct cmd_list_element
*c
270 = add_cmd (name
, class, NO_FUNCTION
, doc
, list
);
273 c
->var_type
= var_type
;
275 /* This needs to be something besides NO_FUNCTION so that this isn't
276 treated as a help class. */
277 c
->function
.sfunc
= empty_sfunc
;
281 /* Add element named NAME to command list LIST (the list for set
282 or some sublist thereof).
283 CLASS is as in add_cmd.
284 ENUMLIST is a list of strings which may follow NAME.
285 VAR is address of the variable which will contain the matching string
287 DOC is the documentation string. */
289 struct cmd_list_element
*
290 add_set_enum_cmd (name
, class, enumlist
, var
, doc
, list
)
292 enum command_class
class;
296 struct cmd_list_element
**list
;
298 struct cmd_list_element
*c
299 = add_set_cmd (name
, class, var_enum
, var
, doc
, list
);
305 /* Where SETCMD has already been added, add the corresponding show
306 command to LIST and return a pointer to the added command (not
307 necessarily the head of LIST). */
308 struct cmd_list_element
*
309 add_show_from_set (setcmd
, list
)
310 struct cmd_list_element
*setcmd
;
311 struct cmd_list_element
**list
;
313 struct cmd_list_element
*showcmd
=
314 (struct cmd_list_element
*) xmalloc (sizeof (struct cmd_list_element
));
315 struct cmd_list_element
*p
;
317 memcpy (showcmd
, setcmd
, sizeof (struct cmd_list_element
));
318 delete_cmd (showcmd
->name
, list
);
319 showcmd
->type
= show_cmd
;
321 /* Replace "set " at start of docstring with "show ". */
322 if (setcmd
->doc
[0] == 'S' && setcmd
->doc
[1] == 'e'
323 && setcmd
->doc
[2] == 't' && setcmd
->doc
[3] == ' ')
324 showcmd
->doc
= concat ("Show ", setcmd
->doc
+ 4, NULL
);
326 fprintf_unfiltered (gdb_stderr
, "GDB internal error: Bad docstring for set command\n");
328 if (*list
== NULL
|| STRCMP ((*list
)->name
, showcmd
->name
) >= 0)
330 showcmd
->next
= *list
;
336 while (p
->next
&& STRCMP (p
->next
->name
, showcmd
->name
) <= 0)
340 showcmd
->next
= p
->next
;
347 /* Remove the command named NAME from the command list. */
350 delete_cmd (name
, list
)
352 struct cmd_list_element
**list
;
354 register struct cmd_list_element
*c
;
355 struct cmd_list_element
*p
;
357 while (*list
&& STREQ ((*list
)->name
, name
))
360 (*list
)->hookee
->hook
= 0; /* Hook slips out of its mouth */
367 for (c
= *list
; c
->next
;)
369 if (STREQ (c
->next
->name
, name
))
372 c
->next
->hookee
->hook
= 0; /* hooked cmd gets away. */
374 free ((PTR
) c
->next
);
382 /* This command really has to deal with two things:
383 * 1) I want documentation on *this string* (usually called by
384 * "help commandname").
385 * 2) I want documentation on *this list* (usually called by
386 * giving a command that requires subcommands. Also called by saying
389 * I am going to split this into two seperate comamnds, help_cmd and
394 help_cmd (command
, stream
)
398 struct cmd_list_element
*c
;
399 extern struct cmd_list_element
*cmdlist
;
403 help_list (cmdlist
, "", all_classes
, stream
);
407 c
= lookup_cmd (&command
, cmdlist
, "", 0, 0);
412 /* There are three cases here.
413 If c->prefixlist is nonzero, we have a prefix command.
414 Print its documentation, then list its subcommands.
416 If c->function is nonzero, we really have a command.
417 Print its documentation and return.
419 If c->function is zero, we have a class name.
420 Print its documentation (as if it were a command)
421 and then set class to the number of this class
422 so that the commands in the class will be listed. */
424 fputs_filtered (c
->doc
, stream
);
425 fputs_filtered ("\n", stream
);
427 if (c
->prefixlist
== 0 && c
->function
.cfunc
!= NULL
)
429 fprintf_filtered (stream
, "\n");
431 /* If this is a prefix command, print it's subcommands */
433 help_list (*c
->prefixlist
, c
->prefixname
, all_commands
, stream
);
435 /* If this is a class name, print all of the commands in the class */
436 if (c
->function
.cfunc
== NULL
)
437 help_list (cmdlist
, "", c
->class, stream
);
440 fprintf_filtered (stream
, "\nThis command has a hook defined: %s\n",
445 * Get a specific kind of help on a command list.
448 * CMDTYPE is the prefix to use in the title string.
449 * CLASS is the class with which to list the nodes of this list (see
450 * documentation for help_cmd_list below), As usual, ALL_COMMANDS for
451 * everything, ALL_CLASSES for just classes, and non-negative for only things
452 * in a specific class.
453 * and STREAM is the output stream on which to print things.
454 * If you call this routine with a class >= 0, it recurses.
457 help_list (list
, cmdtype
, class, stream
)
458 struct cmd_list_element
*list
;
460 enum command_class
class;
464 char *cmdtype1
, *cmdtype2
;
466 /* If CMDTYPE is "foo ", CMDTYPE1 gets " foo" and CMDTYPE2 gets "foo sub" */
467 len
= strlen (cmdtype
);
468 cmdtype1
= (char *) alloca (len
+ 1);
470 cmdtype2
= (char *) alloca (len
+ 4);
475 strncpy (cmdtype1
+ 1, cmdtype
, len
- 1);
477 strncpy (cmdtype2
, cmdtype
, len
- 1);
478 strcpy (cmdtype2
+ len
- 1, " sub");
481 if (class == all_classes
)
482 fprintf_filtered (stream
, "List of classes of %scommands:\n\n", cmdtype2
);
484 fprintf_filtered (stream
, "List of %scommands:\n\n", cmdtype2
);
486 help_cmd_list (list
, class, cmdtype
, (int) class >= 0, stream
);
488 if (class == all_classes
)
489 fprintf_filtered (stream
, "\n\
490 Type \"help%s\" followed by a class name for a list of commands in that class.",
493 fprintf_filtered (stream
, "\n\
494 Type \"help%s\" followed by %scommand name for full documentation.\n\
495 Command name abbreviations are allowed if unambiguous.\n",
499 /* Print only the first line of STR on STREAM. */
501 print_doc_line (stream
, str
)
505 static char *line_buffer
= 0;
506 static int line_size
;
512 line_buffer
= (char *) xmalloc (line_size
);
516 while (*p
&& *p
!= '\n' && *p
!= '.' && *p
!= ',')
518 if (p
- str
> line_size
- 1)
520 line_size
= p
- str
+ 1;
521 free ((PTR
) line_buffer
);
522 line_buffer
= (char *) xmalloc (line_size
);
524 strncpy (line_buffer
, str
, p
- str
);
525 line_buffer
[p
- str
] = '\0';
526 if (islower (line_buffer
[0]))
527 line_buffer
[0] = toupper (line_buffer
[0]);
528 fputs_filtered (line_buffer
, stream
);
532 * Implement a help command on command list LIST.
533 * RECURSE should be non-zero if this should be done recursively on
534 * all sublists of LIST.
535 * PREFIX is the prefix to print before each command name.
536 * STREAM is the stream upon which the output should be written.
538 * A non-negative class number to list only commands in that
540 * ALL_COMMANDS to list all commands in list.
541 * ALL_CLASSES to list all classes in list.
543 * Note that RECURSE will be active on *all* sublists, not just the
544 * ones selected by the criteria above (ie. the selection mechanism
545 * is at the low level, not the high-level).
548 help_cmd_list (list
, class, prefix
, recurse
, stream
)
549 struct cmd_list_element
*list
;
550 enum command_class
class;
555 register struct cmd_list_element
*c
;
557 for (c
= list
; c
; c
= c
->next
)
559 if (c
->abbrev_flag
== 0 &&
560 (class == all_commands
561 || (class == all_classes
&& c
->function
.cfunc
== NULL
)
562 || (class == c
->class && c
->function
.cfunc
!= NULL
)))
564 fprintf_filtered (stream
, "%s%s -- ", prefix
, c
->name
);
565 print_doc_line (stream
, c
->doc
);
566 fputs_filtered ("\n", stream
);
569 && c
->prefixlist
!= 0
570 && c
->abbrev_flag
== 0)
571 help_cmd_list (*c
->prefixlist
, class, c
->prefixname
, 1, stream
);
576 /* Search the input clist for 'command'. Return the command if
577 found (or NULL if not), and return the number of commands
580 static struct cmd_list_element
*
581 find_cmd (command
, len
, clist
, ignore_help_classes
, nfound
)
584 struct cmd_list_element
*clist
;
585 int ignore_help_classes
;
588 struct cmd_list_element
*found
, *c
;
590 found
= (struct cmd_list_element
*) NULL
;
592 for (c
= clist
; c
; c
= c
->next
)
593 if (!strncmp (command
, c
->name
, len
)
594 && (!ignore_help_classes
|| c
->function
.cfunc
))
598 if (c
->name
[len
] == '\0')
607 /* This routine takes a line of TEXT and a CLIST in which to start the
608 lookup. When it returns it will have incremented the text pointer past
609 the section of text it matched, set *RESULT_LIST to point to the list in
610 which the last word was matched, and will return a pointer to the cmd
611 list element which the text matches. It will return NULL if no match at
612 all was possible. It will return -1 (cast appropriately, ick) if ambigous
613 matches are possible; in this case *RESULT_LIST will be set to point to
614 the list in which there are ambiguous choices (and *TEXT will be set to
615 the ambiguous text string).
617 If the located command was an abbreviation, this routine returns the base
618 command of the abbreviation.
620 It does no error reporting whatsoever; control will always return
621 to the superior routine.
623 In the case of an ambiguous return (-1), *RESULT_LIST will be set to point
624 at the prefix_command (ie. the best match) *or* (special case) will be NULL
625 if no prefix command was ever found. For example, in the case of "info a",
626 "info" matches without ambiguity, but "a" could be "args" or "address", so
627 *RESULT_LIST is set to the cmd_list_element for "info". So in this case
628 RESULT_LIST should not be interpeted as a pointer to the beginning of a
629 list; it simply points to a specific command. In the case of an ambiguous
630 return *TEXT is advanced past the last non-ambiguous prefix (e.g.
631 "info t" can be "info types" or "info target"; upon return *TEXT has been
632 advanced past "info ").
634 If RESULT_LIST is NULL, don't set *RESULT_LIST (but don't otherwise
635 affect the operation).
637 This routine does *not* modify the text pointed to by TEXT.
639 If IGNORE_HELP_CLASSES is nonzero, ignore any command list elements which
640 are actually help classes rather than commands (i.e. the function field of
641 the struct cmd_list_element is NULL). */
643 struct cmd_list_element
*
644 lookup_cmd_1 (text
, clist
, result_list
, ignore_help_classes
)
646 struct cmd_list_element
*clist
, **result_list
;
647 int ignore_help_classes
;
650 int len
, tmp
, nfound
;
651 struct cmd_list_element
*found
, *c
;
653 while (**text
== ' ' || **text
== '\t')
656 /* Treating underscores as part of command words is important
657 so that "set args_foo()" doesn't get interpreted as
658 "set args _foo()". */
660 *p
&& (isalnum (*p
) || *p
== '-' || *p
== '_' ||
662 (*p
== '+' || *p
== '<' || *p
== '>' || *p
== '$')) ||
663 (xdb_commands
&& (*p
== '!' || *p
== '/' || *p
== '?')));
667 /* If nothing but whitespace, return 0. */
673 /* *text and p now bracket the first command word to lookup (and
674 it's length is len). We copy this into a local temporary */
677 command
= (char *) alloca (len
+ 1);
678 for (tmp
= 0; tmp
< len
; tmp
++)
680 char x
= (*text
)[tmp
];
688 found
= find_cmd (command
, len
, clist
, ignore_help_classes
, &nfound
);
691 ** We didn't find the command in the entered case, so lower case it
694 if (!found
|| nfound
== 0)
696 for (tmp
= 0; tmp
< len
; tmp
++)
698 char x
= command
[tmp
];
699 command
[tmp
] = isupper (x
) ? tolower (x
) : x
;
701 found
= find_cmd (command
, len
, clist
, ignore_help_classes
, &nfound
);
704 /* If nothing matches, we have a simple failure. */
710 if (result_list
!= NULL
)
711 /* Will be modified in calling routine
712 if we know what the prefix command is. */
714 return (struct cmd_list_element
*) -1; /* Ambiguous. */
717 /* We've matched something on this list. Move text pointer forward. */
721 /* If this was an abbreviation, use the base command instead. */
723 if (found
->cmd_pointer
)
724 found
= found
->cmd_pointer
;
726 /* If we found a prefix command, keep looking. */
728 if (found
->prefixlist
)
730 c
= lookup_cmd_1 (text
, *found
->prefixlist
, result_list
,
731 ignore_help_classes
);
734 /* Didn't find anything; this is as far as we got. */
735 if (result_list
!= NULL
)
736 *result_list
= clist
;
739 else if (c
== (struct cmd_list_element
*) -1)
741 /* We've gotten this far properly, but the next step
742 is ambiguous. We need to set the result list to the best
743 we've found (if an inferior hasn't already set it). */
744 if (result_list
!= NULL
)
746 /* This used to say *result_list = *found->prefixlist
747 If that was correct, need to modify the documentation
748 at the top of this function to clarify what is supposed
750 *result_list
= found
;
761 if (result_list
!= NULL
)
762 *result_list
= clist
;
767 /* All this hair to move the space to the front of cmdtype */
770 undef_cmd_error (cmdtype
, q
)
773 error ("Undefined %scommand: \"%s\". Try \"help%s%.*s\".",
777 strlen (cmdtype
) - 1,
781 /* Look up the contents of *LINE as a command in the command list LIST.
782 LIST is a chain of struct cmd_list_element's.
783 If it is found, return the struct cmd_list_element for that command
784 and update *LINE to point after the command name, at the first argument.
785 If not found, call error if ALLOW_UNKNOWN is zero
786 otherwise (or if error returns) return zero.
787 Call error if specified command is ambiguous,
788 unless ALLOW_UNKNOWN is negative.
789 CMDTYPE precedes the word "command" in the error message.
791 If INGNORE_HELP_CLASSES is nonzero, ignore any command list
792 elements which are actually help classes rather than commands (i.e.
793 the function field of the struct cmd_list_element is 0). */
795 struct cmd_list_element
*
796 lookup_cmd (line
, list
, cmdtype
, allow_unknown
, ignore_help_classes
)
798 struct cmd_list_element
*list
;
801 int ignore_help_classes
;
803 struct cmd_list_element
*last_list
= 0;
804 struct cmd_list_element
*c
=
805 lookup_cmd_1 (line
, list
, &last_list
, ignore_help_classes
);
807 /* This is wrong for complete_command. */
808 char *ptr
= (*line
) + strlen (*line
) - 1;
810 /* Clear off trailing whitespace. */
811 while (ptr
>= *line
&& (*ptr
== ' ' || *ptr
== '\t'))
821 error ("Lack of needed %scommand", cmdtype
);
826 while (isalnum (*p
) || *p
== '-')
829 q
= (char *) alloca (p
- *line
+ 1);
830 strncpy (q
, *line
, p
- *line
);
832 undef_cmd_error (cmdtype
, q
);
838 else if (c
== (struct cmd_list_element
*) -1)
840 /* Ambigous. Local values should be off prefixlist or called
842 int local_allow_unknown
= (last_list
? last_list
->allow_unknown
:
844 char *local_cmdtype
= last_list
? last_list
->prefixname
: cmdtype
;
845 struct cmd_list_element
*local_list
=
846 (last_list
? *(last_list
->prefixlist
) : list
);
848 if (local_allow_unknown
< 0)
851 return last_list
; /* Found something. */
853 return 0; /* Found nothing. */
857 /* Report as error. */
862 ((*line
)[amb_len
] && (*line
)[amb_len
] != ' '
863 && (*line
)[amb_len
] != '\t');
868 for (c
= local_list
; c
; c
= c
->next
)
869 if (!strncmp (*line
, c
->name
, amb_len
))
871 if (strlen (ambbuf
) + strlen (c
->name
) + 6 < (int) sizeof ambbuf
)
874 strcat (ambbuf
, ", ");
875 strcat (ambbuf
, c
->name
);
879 strcat (ambbuf
, "..");
883 error ("Ambiguous %scommand \"%s\": %s.", local_cmdtype
,
890 /* We've got something. It may still not be what the caller
891 wants (if this command *needs* a subcommand). */
892 while (**line
== ' ' || **line
== '\t')
895 if (c
->prefixlist
&& **line
&& !c
->allow_unknown
)
896 undef_cmd_error (c
->prefixname
, *line
);
898 /* Seems to be what he wants. Return it. */
905 /* Look up the contents of *LINE as a command in the command list LIST.
906 LIST is a chain of struct cmd_list_element's.
907 If it is found, return the struct cmd_list_element for that command
908 and update *LINE to point after the command name, at the first argument.
909 If not found, call error if ALLOW_UNKNOWN is zero
910 otherwise (or if error returns) return zero.
911 Call error if specified command is ambiguous,
912 unless ALLOW_UNKNOWN is negative.
913 CMDTYPE precedes the word "command" in the error message. */
915 struct cmd_list_element
*
916 lookup_cmd (line
, list
, cmdtype
, allow_unknown
)
918 struct cmd_list_element
*list
;
923 register struct cmd_list_element
*c
, *found
;
929 /* Skip leading whitespace. */
931 while (**line
== ' ' || **line
== '\t')
934 /* Clear out trailing whitespace. */
936 p
= *line
+ strlen (*line
);
937 while (p
!= *line
&& (p
[-1] == ' ' || p
[-1] == '\t'))
941 /* Find end of command name. */
944 while (*p
== '-' || isalnum (*p
))
947 /* Look up the command name.
948 If exact match, keep that.
949 Otherwise, take command abbreviated, if unique. Note that (in my
950 opinion) a null string does *not* indicate ambiguity; simply the
951 end of the argument. */
956 error ("Lack of needed %scommand", cmdtype
);
960 /* Copy over to a local buffer, converting to lowercase on the way.
961 This is in case the command being parsed is a subcommand which
962 doesn't match anything, and that's ok. We want the original
963 untouched for the routine of the original command. */
965 processed_cmd
= (char *) alloca (p
- *line
+ 1);
966 for (cmd_len
= 0; cmd_len
< p
- *line
; cmd_len
++)
968 char x
= (*line
)[cmd_len
];
970 processed_cmd
[cmd_len
] = tolower (x
);
972 processed_cmd
[cmd_len
] = x
;
974 processed_cmd
[cmd_len
] = '\0';
976 /* Check all possibilities in the current command list. */
979 for (c
= list
; c
; c
= c
->next
)
981 if (!strncmp (processed_cmd
, c
->name
, cmd_len
))
985 if (c
->name
[cmd_len
] == 0)
993 /* Report error for undefined command name. */
997 if (nfound
> 1 && allow_unknown
>= 0)
1000 for (c
= list
; c
; c
= c
->next
)
1001 if (!strncmp (processed_cmd
, c
->name
, cmd_len
))
1003 if (strlen (ambbuf
) + strlen (c
->name
) + 6 < sizeof ambbuf
)
1005 if (strlen (ambbuf
))
1006 strcat (ambbuf
, ", ");
1007 strcat (ambbuf
, c
->name
);
1011 strcat (ambbuf
, "..");
1015 error ("Ambiguous %scommand \"%s\": %s.", cmdtype
,
1016 processed_cmd
, ambbuf
);
1018 else if (!allow_unknown
)
1019 error ("Undefined %scommand: \"%s\".", cmdtype
, processed_cmd
);
1023 /* Skip whitespace before the argument. */
1025 while (*p
== ' ' || *p
== '\t')
1029 if (found
->prefixlist
&& *p
)
1031 c
= lookup_cmd (line
, *found
->prefixlist
, found
->prefixname
,
1032 found
->allow_unknown
);
1041 /* Helper function for SYMBOL_COMPLETION_FUNCTION. */
1043 /* Return a vector of char pointers which point to the different
1044 possible completions in LIST of TEXT.
1046 WORD points in the same buffer as TEXT, and completions should be
1047 returned relative to this position. For example, suppose TEXT is "foo"
1048 and we want to complete to "foobar". If WORD is "oo", return
1049 "oobar"; if WORD is "baz/foo", return "baz/foobar". */
1052 complete_on_cmdlist (list
, text
, word
)
1053 struct cmd_list_element
*list
;
1057 struct cmd_list_element
*ptr
;
1059 int sizeof_matchlist
;
1061 int textlen
= strlen (text
);
1063 sizeof_matchlist
= 10;
1064 matchlist
= (char **) xmalloc (sizeof_matchlist
* sizeof (char *));
1067 for (ptr
= list
; ptr
; ptr
= ptr
->next
)
1068 if (!strncmp (ptr
->name
, text
, textlen
)
1069 && !ptr
->abbrev_flag
1070 && (ptr
->function
.cfunc
1071 || ptr
->prefixlist
))
1073 if (matches
== sizeof_matchlist
)
1075 sizeof_matchlist
*= 2;
1076 matchlist
= (char **) xrealloc ((char *) matchlist
,
1078 * sizeof (char *)));
1081 matchlist
[matches
] = (char *)
1082 xmalloc (strlen (word
) + strlen (ptr
->name
) + 1);
1084 strcpy (matchlist
[matches
], ptr
->name
);
1085 else if (word
> text
)
1087 /* Return some portion of ptr->name. */
1088 strcpy (matchlist
[matches
], ptr
->name
+ (word
- text
));
1092 /* Return some of text plus ptr->name. */
1093 strncpy (matchlist
[matches
], word
, text
- word
);
1094 matchlist
[matches
][text
- word
] = '\0';
1095 strcat (matchlist
[matches
], ptr
->name
);
1102 free ((PTR
) matchlist
);
1107 matchlist
= (char **) xrealloc ((char *) matchlist
, ((matches
+ 1)
1108 * sizeof (char *)));
1109 matchlist
[matches
] = (char *) 0;
1115 /* Helper function for SYMBOL_COMPLETION_FUNCTION. */
1117 /* Return a vector of char pointers which point to the different
1118 possible completions in CMD of TEXT.
1120 WORD points in the same buffer as TEXT, and completions should be
1121 returned relative to this position. For example, suppose TEXT is "foo"
1122 and we want to complete to "foobar". If WORD is "oo", return
1123 "oobar"; if WORD is "baz/foo", return "baz/foobar". */
1126 complete_on_enum (enumlist
, text
, word
)
1132 int sizeof_matchlist
;
1134 int textlen
= strlen (text
);
1138 sizeof_matchlist
= 10;
1139 matchlist
= (char **) xmalloc (sizeof_matchlist
* sizeof (char *));
1142 for (i
= 0; (name
= enumlist
[i
]) != NULL
; i
++)
1143 if (strncmp (name
, text
, textlen
) == 0)
1145 if (matches
== sizeof_matchlist
)
1147 sizeof_matchlist
*= 2;
1148 matchlist
= (char **) xrealloc ((char *) matchlist
,
1150 * sizeof (char *)));
1153 matchlist
[matches
] = (char *)
1154 xmalloc (strlen (word
) + strlen (name
) + 1);
1156 strcpy (matchlist
[matches
], name
);
1157 else if (word
> text
)
1159 /* Return some portion of name. */
1160 strcpy (matchlist
[matches
], name
+ (word
- text
));
1164 /* Return some of text plus name. */
1165 strncpy (matchlist
[matches
], word
, text
- word
);
1166 matchlist
[matches
][text
- word
] = '\0';
1167 strcat (matchlist
[matches
], name
);
1174 free ((PTR
) matchlist
);
1179 matchlist
= (char **) xrealloc ((char *) matchlist
, ((matches
+ 1)
1180 * sizeof (char *)));
1181 matchlist
[matches
] = (char *) 0;
1188 parse_binary_operation (arg
)
1196 length
= strlen (arg
);
1198 while (arg
[length
- 1] == ' ' || arg
[length
- 1] == '\t')
1201 if (!strncmp (arg
, "on", length
)
1202 || !strncmp (arg
, "1", length
)
1203 || !strncmp (arg
, "yes", length
))
1205 else if (!strncmp (arg
, "off", length
)
1206 || !strncmp (arg
, "0", length
)
1207 || !strncmp (arg
, "no", length
))
1211 error ("\"on\" or \"off\" expected.");
1216 /* Do a "set" or "show" command. ARG is NULL if no argument, or the text
1217 of the argument, and FROM_TTY is nonzero if this command is being entered
1218 directly by the user (i.e. these are just like any other
1219 command). C is the command list element for the command. */
1221 do_setshow_command (arg
, from_tty
, c
)
1224 struct cmd_list_element
*c
;
1226 if (c
->type
== set_cmd
)
1228 switch (c
->var_type
)
1239 new = (char *) xmalloc (strlen (arg
) + 2);
1242 while ((ch
= *p
++) != '\000')
1246 /* \ at end of argument is used after spaces
1247 so they won't be lost. */
1248 /* This is obsolete now that we no longer strip
1249 trailing whitespace and actually, the backslash
1250 didn't get here in my test, readline or
1251 something did something funky with a backslash
1252 right before a newline. */
1255 ch
= parse_escape (&p
);
1257 break; /* C loses */
1265 if (*(p
- 1) != '\\')
1269 new = (char *) xrealloc (new, q
- new);
1270 if (*(char **) c
->var
!= NULL
)
1271 free (*(char **) c
->var
);
1272 *(char **) c
->var
= new;
1275 case var_string_noescape
:
1278 if (*(char **) c
->var
!= NULL
)
1279 free (*(char **) c
->var
);
1280 *(char **) c
->var
= savestring (arg
, strlen (arg
));
1284 error_no_arg ("filename to set it to.");
1285 if (*(char **) c
->var
!= NULL
)
1286 free (*(char **) c
->var
);
1287 *(char **) c
->var
= tilde_expand (arg
);
1290 *(int *) c
->var
= parse_binary_operation (arg
);
1294 error_no_arg ("integer to set it to.");
1295 *(unsigned int *) c
->var
= parse_and_eval_address (arg
);
1296 if (*(unsigned int *) c
->var
== 0)
1297 *(unsigned int *) c
->var
= UINT_MAX
;
1303 error_no_arg ("integer to set it to.");
1304 val
= parse_and_eval_address (arg
);
1306 *(int *) c
->var
= INT_MAX
;
1307 else if (val
>= INT_MAX
)
1308 error ("integer %u out of range", val
);
1310 *(int *) c
->var
= val
;
1315 error_no_arg ("integer to set it to.");
1316 *(int *) c
->var
= parse_and_eval_address (arg
);
1326 /* if no argument was supplied, print an informative error message */
1330 strcpy (msg
, "Requires an argument. Valid arguments are ");
1331 for (i
= 0; c
->enums
[i
]; i
++)
1335 strcat (msg
, c
->enums
[i
]);
1341 p
= strchr (arg
, ' ');
1349 for (i
= 0; c
->enums
[i
]; i
++)
1350 if (strncmp (arg
, c
->enums
[i
], len
) == 0)
1352 match
= c
->enums
[i
];
1357 error ("Undefined item: \"%s\".", arg
);
1360 error ("Ambiguous item \"%s\".", arg
);
1362 *(char **) c
->var
= match
;
1366 error ("gdb internal error: bad var_type in do_setshow_command");
1369 else if (c
->type
== show_cmd
)
1371 /* Print doc minus "show" at start. */
1372 print_doc_line (gdb_stdout
, c
->doc
+ 5);
1374 fputs_filtered (" is ", gdb_stdout
);
1376 switch (c
->var_type
)
1382 fputs_filtered ("\"", gdb_stdout
);
1383 if (*(unsigned char **) c
->var
)
1384 fputstr_filtered (*(unsigned char **) c
->var
, '"', gdb_stdout
);
1385 fputs_filtered ("\"", gdb_stdout
);
1388 case var_string_noescape
:
1391 fputs_filtered ("\"", gdb_stdout
);
1392 if (*(char **) c
->var
)
1393 fputs_filtered (*(char **) c
->var
, gdb_stdout
);
1394 fputs_filtered ("\"", gdb_stdout
);
1397 fputs_filtered (*(int *) c
->var
? "on" : "off", gdb_stdout
);
1400 if (*(unsigned int *) c
->var
== UINT_MAX
)
1402 fputs_filtered ("unlimited", gdb_stdout
);
1405 /* else fall through */
1407 fprintf_filtered (gdb_stdout
, "%u", *(unsigned int *) c
->var
);
1410 if (*(int *) c
->var
== INT_MAX
)
1412 fputs_filtered ("unlimited", gdb_stdout
);
1415 fprintf_filtered (gdb_stdout
, "%d", *(int *) c
->var
);
1419 error ("gdb internal error: bad var_type in do_setshow_command");
1421 fputs_filtered (".\n", gdb_stdout
);
1424 error ("gdb internal error: bad cmd_type in do_setshow_command");
1425 (*c
->function
.sfunc
) (NULL
, from_tty
, c
);
1426 if (c
->type
== set_cmd
&& set_hook
)
1430 /* Show all the settings in a list of show commands. */
1433 cmd_show_list (list
, from_tty
, prefix
)
1434 struct cmd_list_element
*list
;
1438 for (; list
!= NULL
; list
= list
->next
)
1440 /* If we find a prefix, run its list, prefixing our output by its
1441 prefix (with "show " skipped). */
1442 if (list
->prefixlist
&& !list
->abbrev_flag
)
1443 cmd_show_list (*list
->prefixlist
, from_tty
, list
->prefixname
+ 5);
1444 if (list
->type
== show_cmd
)
1446 fputs_filtered (prefix
, gdb_stdout
);
1447 fputs_filtered (list
->name
, gdb_stdout
);
1448 fputs_filtered (": ", gdb_stdout
);
1449 do_setshow_command ((char *) NULL
, from_tty
, list
);
1456 shell_escape (arg
, from_tty
)
1461 /* If ARG is NULL, they want an inferior shell, but `system' just
1462 reports if the shell is available when passed a NULL arg. */
1463 int rc
= system (arg
? arg
: "");
1466 arg
= "inferior shell";
1470 fprintf_unfiltered (gdb_stderr
, "Cannot execute %s: %s\n", arg
,
1471 safe_strerror (errno
));
1472 gdb_flush (gdb_stderr
);
1476 fprintf_unfiltered (gdb_stderr
, "%s exited with status %d\n", arg
, rc
);
1477 gdb_flush (gdb_stderr
);
1480 /* Make sure to return to the directory GDB thinks it is, in case the
1481 shell command we just ran changed it. */
1482 chdir (current_directory
);
1484 #else /* Can fork. */
1485 int rc
, status
, pid
;
1486 char *p
, *user_shell
;
1488 if ((user_shell
= (char *) getenv ("SHELL")) == NULL
)
1489 user_shell
= "/bin/sh";
1491 /* Get the name of the shell for arg0 */
1492 if ((p
= strrchr (user_shell
, '/')) == NULL
)
1495 p
++; /* Get past '/' */
1497 if ((pid
= fork ()) == 0)
1500 execl (user_shell
, p
, 0);
1502 execl (user_shell
, p
, "-c", arg
, 0);
1504 fprintf_unfiltered (gdb_stderr
, "Cannot execute %s: %s\n", user_shell
,
1505 safe_strerror (errno
));
1506 gdb_flush (gdb_stderr
);
1511 while ((rc
= wait (&status
)) != pid
&& rc
!= -1)
1514 error ("Fork failed");
1515 #endif /* Can fork. */
1519 make_command (arg
, from_tty
)
1529 p
= xmalloc (sizeof ("make ") + strlen (arg
));
1530 strcpy (p
, "make ");
1531 strcpy (p
+ sizeof ("make ") - 1, arg
);
1534 shell_escape (p
, from_tty
);
1538 show_user_1 (c
, stream
)
1539 struct cmd_list_element
*c
;
1542 register struct command_line
*cmdlines
;
1544 cmdlines
= c
->user_commands
;
1547 fputs_filtered ("User command ", stream
);
1548 fputs_filtered (c
->name
, stream
);
1549 fputs_filtered (":\n", stream
);
1553 print_command_line (cmdlines
, 4, stream
);
1554 cmdlines
= cmdlines
->next
;
1556 fputs_filtered ("\n", stream
);
1561 show_user (args
, from_tty
)
1565 struct cmd_list_element
*c
;
1566 extern struct cmd_list_element
*cmdlist
;
1570 c
= lookup_cmd (&args
, cmdlist
, "", 0, 1);
1571 if (c
->class != class_user
)
1572 error ("Not a user command.");
1573 show_user_1 (c
, gdb_stdout
);
1577 for (c
= cmdlist
; c
; c
= c
->next
)
1579 if (c
->class == class_user
)
1580 show_user_1 (c
, gdb_stdout
);
1586 _initialize_command ()
1588 add_com ("shell", class_support
, shell_escape
,
1589 "Execute the rest of the line as a shell command. \n\
1590 With no arguments, run an inferior shell.");
1593 add_com_alias ("!", "shell", class_support
, 0);
1595 add_com ("make", class_support
, make_command
,
1596 "Run the ``make'' program using the rest of the line as arguments.");
1597 add_cmd ("user", no_class
, show_user
,
1598 "Show definitions of user defined commands.\n\
1599 Argument is the name of the user defined command.\n\
1600 With no argument, show definitions of all user defined commands.", &showlist
);