1 /* Handle lists of commands, their decoding and documentation, for GDB.
2 Copyright 1986, 1989, 1990, 1991, 1998, 2000 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"
37 /* FIXME: this should be auto-configured! */
42 /* Prototypes for local functions */
44 static void undef_cmd_error
PARAMS ((char *, char *));
46 static void show_user
PARAMS ((char *, int));
48 static void show_user_1 (struct cmd_list_element
*, struct ui_file
*);
50 static void make_command
PARAMS ((char *, int));
52 static void shell_escape
PARAMS ((char *, int));
54 static int parse_binary_operation
PARAMS ((char *));
56 static void print_doc_line (struct ui_file
*, char *);
58 static struct cmd_list_element
*find_cmd
PARAMS ((char *command
,
60 struct cmd_list_element
* clist
,
61 int ignore_help_classes
,
64 void _initialize_command
PARAMS ((void));
66 /* Add element named NAME.
67 CLASS is the top level category into which commands are broken down
69 FUN should be the function to execute the command;
70 it will get a character string as argument, with leading
71 and trailing blanks already eliminated.
73 DOC is a documentation string for the command.
74 Its first line should be a complete sentence.
75 It should start with ? for a command that is an abbreviation
76 or with * for a command that most users don't need to know about.
78 Add this command to command list *LIST.
80 Returns a pointer to the added command (not necessarily the head
83 struct cmd_list_element
*
84 add_cmd (name
, class, fun
, doc
, list
)
86 enum command_class
class;
87 void (*fun
) PARAMS ((char *, int));
89 struct cmd_list_element
**list
;
91 register struct cmd_list_element
*c
92 = (struct cmd_list_element
*) xmalloc (sizeof (struct cmd_list_element
));
93 struct cmd_list_element
*p
;
95 delete_cmd (name
, list
);
97 if (*list
== NULL
|| STRCMP ((*list
)->name
, name
) >= 0)
105 while (p
->next
&& STRCMP (p
->next
->name
, name
) <= 0)
115 c
->function
.cfunc
= fun
;
118 c
->prefixlist
= NULL
;
119 c
->prefixname
= NULL
;
120 c
->allow_unknown
= 0;
122 c
->completer
= make_symbol_completion_list
;
123 c
->type
= not_set_cmd
;
125 c
->var_type
= var_boolean
;
127 c
->user_commands
= NULL
;
129 c
->cmd_pointer
= NULL
;
134 /* Same as above, except that the abbrev_flag is set. */
136 #if 0 /* Currently unused */
138 struct cmd_list_element
*
139 add_abbrev_cmd (name
, class, fun
, doc
, list
)
141 enum command_class
class;
142 void (*fun
) PARAMS ((char *, int));
144 struct cmd_list_element
**list
;
146 register struct cmd_list_element
*c
147 = add_cmd (name
, class, fun
, doc
, list
);
155 struct cmd_list_element
*
156 add_alias_cmd (name
, oldname
, class, abbrev_flag
, list
)
159 enum command_class
class;
161 struct cmd_list_element
**list
;
163 /* Must do this since lookup_cmd tries to side-effect its first arg */
165 register struct cmd_list_element
*old
;
166 register struct cmd_list_element
*c
;
167 copied_name
= (char *) alloca (strlen (oldname
) + 1);
168 strcpy (copied_name
, oldname
);
169 old
= lookup_cmd (&copied_name
, *list
, "", 1, 1);
173 delete_cmd (name
, list
);
177 c
= add_cmd (name
, class, old
->function
.cfunc
, old
->doc
, list
);
178 c
->prefixlist
= old
->prefixlist
;
179 c
->prefixname
= old
->prefixname
;
180 c
->allow_unknown
= old
->allow_unknown
;
181 c
->abbrev_flag
= abbrev_flag
;
182 c
->cmd_pointer
= old
;
186 /* Like add_cmd but adds an element for a command prefix:
187 a name that should be followed by a subcommand to be looked up
188 in another command list. PREFIXLIST should be the address
189 of the variable containing that list. */
191 struct cmd_list_element
*
192 add_prefix_cmd (name
, class, fun
, doc
, prefixlist
, prefixname
,
195 enum command_class
class;
196 void (*fun
) PARAMS ((char *, int));
198 struct cmd_list_element
**prefixlist
;
201 struct cmd_list_element
**list
;
203 register struct cmd_list_element
*c
= add_cmd (name
, class, fun
, doc
, list
);
204 c
->prefixlist
= prefixlist
;
205 c
->prefixname
= prefixname
;
206 c
->allow_unknown
= allow_unknown
;
210 /* Like add_prefix_cmd but sets the abbrev_flag on the new command. */
212 struct cmd_list_element
*
213 add_abbrev_prefix_cmd (name
, class, fun
, doc
, prefixlist
, prefixname
,
216 enum command_class
class;
217 void (*fun
) PARAMS ((char *, int));
219 struct cmd_list_element
**prefixlist
;
222 struct cmd_list_element
**list
;
224 register struct cmd_list_element
*c
= add_cmd (name
, class, fun
, doc
, list
);
225 c
->prefixlist
= prefixlist
;
226 c
->prefixname
= prefixname
;
227 c
->allow_unknown
= allow_unknown
;
232 /* This is an empty "cfunc". */
234 not_just_help_class_command (args
, from_tty
)
240 /* This is an empty "sfunc". */
241 static void empty_sfunc
PARAMS ((char *, int, struct cmd_list_element
*));
244 empty_sfunc (args
, from_tty
, c
)
247 struct cmd_list_element
*c
;
251 /* Add element named NAME to command list LIST (the list for set
252 or some sublist thereof).
253 CLASS is as in add_cmd.
254 VAR_TYPE is the kind of thing we are setting.
255 VAR is address of the variable being controlled by this command.
256 DOC is the documentation string. */
258 struct cmd_list_element
*
259 add_set_cmd (name
, class, var_type
, var
, doc
, list
)
261 enum command_class
class;
265 struct cmd_list_element
**list
;
267 struct cmd_list_element
*c
268 = add_cmd (name
, class, NO_FUNCTION
, doc
, list
);
271 c
->var_type
= var_type
;
273 /* This needs to be something besides NO_FUNCTION so that this isn't
274 treated as a help class. */
275 c
->function
.sfunc
= empty_sfunc
;
279 /* Add element named NAME to command list LIST (the list for set
280 or some sublist thereof).
281 CLASS is as in add_cmd.
282 ENUMLIST is a list of strings which may follow NAME.
283 VAR is address of the variable which will contain the matching string
285 DOC is the documentation string. */
287 struct cmd_list_element
*
288 add_set_enum_cmd (name
, class, enumlist
, var
, doc
, list
)
290 enum command_class
class;
294 struct cmd_list_element
**list
;
296 struct cmd_list_element
*c
297 = add_set_cmd (name
, class, var_enum
, var
, doc
, list
);
303 /* Where SETCMD has already been added, add the corresponding show
304 command to LIST and return a pointer to the added command (not
305 necessarily the head of LIST). */
306 struct cmd_list_element
*
307 add_show_from_set (setcmd
, list
)
308 struct cmd_list_element
*setcmd
;
309 struct cmd_list_element
**list
;
311 struct cmd_list_element
*showcmd
=
312 (struct cmd_list_element
*) xmalloc (sizeof (struct cmd_list_element
));
313 struct cmd_list_element
*p
;
315 memcpy (showcmd
, setcmd
, sizeof (struct cmd_list_element
));
316 delete_cmd (showcmd
->name
, list
);
317 showcmd
->type
= show_cmd
;
319 /* Replace "set " at start of docstring with "show ". */
320 if (setcmd
->doc
[0] == 'S' && setcmd
->doc
[1] == 'e'
321 && setcmd
->doc
[2] == 't' && setcmd
->doc
[3] == ' ')
322 showcmd
->doc
= concat ("Show ", setcmd
->doc
+ 4, NULL
);
324 fprintf_unfiltered (gdb_stderr
, "GDB internal error: Bad docstring for set command\n");
326 if (*list
== NULL
|| STRCMP ((*list
)->name
, showcmd
->name
) >= 0)
328 showcmd
->next
= *list
;
334 while (p
->next
&& STRCMP (p
->next
->name
, showcmd
->name
) <= 0)
338 showcmd
->next
= p
->next
;
345 /* Remove the command named NAME from the command list. */
348 delete_cmd (name
, list
)
350 struct cmd_list_element
**list
;
352 register struct cmd_list_element
*c
;
353 struct cmd_list_element
*p
;
355 while (*list
&& STREQ ((*list
)->name
, name
))
358 (*list
)->hookee
->hook
= 0; /* Hook slips out of its mouth */
365 for (c
= *list
; c
->next
;)
367 if (STREQ (c
->next
->name
, name
))
370 c
->next
->hookee
->hook
= 0; /* hooked cmd gets away. */
372 free ((PTR
) c
->next
);
380 /* This command really has to deal with two things:
381 * 1) I want documentation on *this string* (usually called by
382 * "help commandname").
383 * 2) I want documentation on *this list* (usually called by
384 * giving a command that requires subcommands. Also called by saying
387 * I am going to split this into two seperate comamnds, help_cmd and
392 help_cmd (command
, stream
)
394 struct ui_file
*stream
;
396 struct cmd_list_element
*c
;
397 extern struct cmd_list_element
*cmdlist
;
401 help_list (cmdlist
, "", all_classes
, stream
);
405 c
= lookup_cmd (&command
, cmdlist
, "", 0, 0);
410 /* There are three cases here.
411 If c->prefixlist is nonzero, we have a prefix command.
412 Print its documentation, then list its subcommands.
414 If c->function is nonzero, we really have a command.
415 Print its documentation and return.
417 If c->function is zero, we have a class name.
418 Print its documentation (as if it were a command)
419 and then set class to the number of this class
420 so that the commands in the class will be listed. */
422 fputs_filtered (c
->doc
, stream
);
423 fputs_filtered ("\n", stream
);
425 if (c
->prefixlist
== 0 && c
->function
.cfunc
!= NULL
)
427 fprintf_filtered (stream
, "\n");
429 /* If this is a prefix command, print it's subcommands */
431 help_list (*c
->prefixlist
, c
->prefixname
, all_commands
, stream
);
433 /* If this is a class name, print all of the commands in the class */
434 if (c
->function
.cfunc
== NULL
)
435 help_list (cmdlist
, "", c
->class, stream
);
438 fprintf_filtered (stream
, "\nThis command has a hook defined: %s\n",
443 * Get a specific kind of help on a command list.
446 * CMDTYPE is the prefix to use in the title string.
447 * CLASS is the class with which to list the nodes of this list (see
448 * documentation for help_cmd_list below), As usual, ALL_COMMANDS for
449 * everything, ALL_CLASSES for just classes, and non-negative for only things
450 * in a specific class.
451 * and STREAM is the output stream on which to print things.
452 * If you call this routine with a class >= 0, it recurses.
455 help_list (list
, cmdtype
, class, stream
)
456 struct cmd_list_element
*list
;
458 enum command_class
class;
459 struct ui_file
*stream
;
462 char *cmdtype1
, *cmdtype2
;
464 /* If CMDTYPE is "foo ", CMDTYPE1 gets " foo" and CMDTYPE2 gets "foo sub" */
465 len
= strlen (cmdtype
);
466 cmdtype1
= (char *) alloca (len
+ 1);
468 cmdtype2
= (char *) alloca (len
+ 4);
473 strncpy (cmdtype1
+ 1, cmdtype
, len
- 1);
475 strncpy (cmdtype2
, cmdtype
, len
- 1);
476 strcpy (cmdtype2
+ len
- 1, " sub");
479 if (class == all_classes
)
480 fprintf_filtered (stream
, "List of classes of %scommands:\n\n", cmdtype2
);
482 fprintf_filtered (stream
, "List of %scommands:\n\n", cmdtype2
);
484 help_cmd_list (list
, class, cmdtype
, (int) class >= 0, stream
);
486 if (class == all_classes
)
487 fprintf_filtered (stream
, "\n\
488 Type \"help%s\" followed by a class name for a list of commands in that class.",
491 fprintf_filtered (stream
, "\n\
492 Type \"help%s\" followed by %scommand name for full documentation.\n\
493 Command name abbreviations are allowed if unambiguous.\n",
497 /* Print only the first line of STR on STREAM. */
499 print_doc_line (stream
, str
)
500 struct ui_file
*stream
;
503 static char *line_buffer
= 0;
504 static int line_size
;
510 line_buffer
= (char *) xmalloc (line_size
);
514 while (*p
&& *p
!= '\n' && *p
!= '.' && *p
!= ',')
516 if (p
- str
> line_size
- 1)
518 line_size
= p
- str
+ 1;
519 free ((PTR
) line_buffer
);
520 line_buffer
= (char *) xmalloc (line_size
);
522 strncpy (line_buffer
, str
, p
- str
);
523 line_buffer
[p
- str
] = '\0';
524 if (islower (line_buffer
[0]))
525 line_buffer
[0] = toupper (line_buffer
[0]);
527 ui_out_text (uiout
, line_buffer
);
529 fputs_filtered (line_buffer
, stream
);
534 * Implement a help command on command list LIST.
535 * RECURSE should be non-zero if this should be done recursively on
536 * all sublists of LIST.
537 * PREFIX is the prefix to print before each command name.
538 * STREAM is the stream upon which the output should be written.
540 * A non-negative class number to list only commands in that
542 * ALL_COMMANDS to list all commands in list.
543 * ALL_CLASSES to list all classes in list.
545 * Note that RECURSE will be active on *all* sublists, not just the
546 * ones selected by the criteria above (ie. the selection mechanism
547 * is at the low level, not the high-level).
550 help_cmd_list (list
, class, prefix
, recurse
, stream
)
551 struct cmd_list_element
*list
;
552 enum command_class
class;
555 struct ui_file
*stream
;
557 register struct cmd_list_element
*c
;
559 for (c
= list
; c
; c
= c
->next
)
561 if (c
->abbrev_flag
== 0 &&
562 (class == all_commands
563 || (class == all_classes
&& c
->function
.cfunc
== NULL
)
564 || (class == c
->class && c
->function
.cfunc
!= NULL
)))
566 fprintf_filtered (stream
, "%s%s -- ", prefix
, c
->name
);
567 print_doc_line (stream
, c
->doc
);
568 fputs_filtered ("\n", stream
);
571 && c
->prefixlist
!= 0
572 && c
->abbrev_flag
== 0)
573 help_cmd_list (*c
->prefixlist
, class, c
->prefixname
, 1, stream
);
578 /* Search the input clist for 'command'. Return the command if
579 found (or NULL if not), and return the number of commands
582 static struct cmd_list_element
*
583 find_cmd (command
, len
, clist
, ignore_help_classes
, nfound
)
586 struct cmd_list_element
*clist
;
587 int ignore_help_classes
;
590 struct cmd_list_element
*found
, *c
;
592 found
= (struct cmd_list_element
*) NULL
;
594 for (c
= clist
; c
; c
= c
->next
)
595 if (!strncmp (command
, c
->name
, len
)
596 && (!ignore_help_classes
|| c
->function
.cfunc
))
600 if (c
->name
[len
] == '\0')
609 /* This routine takes a line of TEXT and a CLIST in which to start the
610 lookup. When it returns it will have incremented the text pointer past
611 the section of text it matched, set *RESULT_LIST to point to the list in
612 which the last word was matched, and will return a pointer to the cmd
613 list element which the text matches. It will return NULL if no match at
614 all was possible. It will return -1 (cast appropriately, ick) if ambigous
615 matches are possible; in this case *RESULT_LIST will be set to point to
616 the list in which there are ambiguous choices (and *TEXT will be set to
617 the ambiguous text string).
619 If the located command was an abbreviation, this routine returns the base
620 command of the abbreviation.
622 It does no error reporting whatsoever; control will always return
623 to the superior routine.
625 In the case of an ambiguous return (-1), *RESULT_LIST will be set to point
626 at the prefix_command (ie. the best match) *or* (special case) will be NULL
627 if no prefix command was ever found. For example, in the case of "info a",
628 "info" matches without ambiguity, but "a" could be "args" or "address", so
629 *RESULT_LIST is set to the cmd_list_element for "info". So in this case
630 RESULT_LIST should not be interpeted as a pointer to the beginning of a
631 list; it simply points to a specific command. In the case of an ambiguous
632 return *TEXT is advanced past the last non-ambiguous prefix (e.g.
633 "info t" can be "info types" or "info target"; upon return *TEXT has been
634 advanced past "info ").
636 If RESULT_LIST is NULL, don't set *RESULT_LIST (but don't otherwise
637 affect the operation).
639 This routine does *not* modify the text pointed to by TEXT.
641 If IGNORE_HELP_CLASSES is nonzero, ignore any command list elements which
642 are actually help classes rather than commands (i.e. the function field of
643 the struct cmd_list_element is NULL). */
645 struct cmd_list_element
*
646 lookup_cmd_1 (text
, clist
, result_list
, ignore_help_classes
)
648 struct cmd_list_element
*clist
, **result_list
;
649 int ignore_help_classes
;
652 int len
, tmp
, nfound
;
653 struct cmd_list_element
*found
, *c
;
655 while (**text
== ' ' || **text
== '\t')
658 /* Treating underscores as part of command words is important
659 so that "set args_foo()" doesn't get interpreted as
660 "set args _foo()". */
662 *p
&& (isalnum (*p
) || *p
== '-' || *p
== '_' ||
664 (*p
== '+' || *p
== '<' || *p
== '>' || *p
== '$')) ||
665 (xdb_commands
&& (*p
== '!' || *p
== '/' || *p
== '?')));
669 /* If nothing but whitespace, return 0. */
675 /* *text and p now bracket the first command word to lookup (and
676 it's length is len). We copy this into a local temporary */
679 command
= (char *) alloca (len
+ 1);
680 for (tmp
= 0; tmp
< len
; tmp
++)
682 char x
= (*text
)[tmp
];
690 found
= find_cmd (command
, len
, clist
, ignore_help_classes
, &nfound
);
693 ** We didn't find the command in the entered case, so lower case it
696 if (!found
|| nfound
== 0)
698 for (tmp
= 0; tmp
< len
; tmp
++)
700 char x
= command
[tmp
];
701 command
[tmp
] = isupper (x
) ? tolower (x
) : x
;
703 found
= find_cmd (command
, len
, clist
, ignore_help_classes
, &nfound
);
706 /* If nothing matches, we have a simple failure. */
712 if (result_list
!= NULL
)
713 /* Will be modified in calling routine
714 if we know what the prefix command is. */
716 return (struct cmd_list_element
*) -1; /* Ambiguous. */
719 /* We've matched something on this list. Move text pointer forward. */
723 /* If this was an abbreviation, use the base command instead. */
725 if (found
->cmd_pointer
)
726 found
= found
->cmd_pointer
;
728 /* If we found a prefix command, keep looking. */
730 if (found
->prefixlist
)
732 c
= lookup_cmd_1 (text
, *found
->prefixlist
, result_list
,
733 ignore_help_classes
);
736 /* Didn't find anything; this is as far as we got. */
737 if (result_list
!= NULL
)
738 *result_list
= clist
;
741 else if (c
== (struct cmd_list_element
*) -1)
743 /* We've gotten this far properly, but the next step
744 is ambiguous. We need to set the result list to the best
745 we've found (if an inferior hasn't already set it). */
746 if (result_list
!= NULL
)
748 /* This used to say *result_list = *found->prefixlist
749 If that was correct, need to modify the documentation
750 at the top of this function to clarify what is supposed
752 *result_list
= found
;
763 if (result_list
!= NULL
)
764 *result_list
= clist
;
769 /* All this hair to move the space to the front of cmdtype */
772 undef_cmd_error (cmdtype
, q
)
775 error ("Undefined %scommand: \"%s\". Try \"help%s%.*s\".",
779 strlen (cmdtype
) - 1,
783 /* Look up the contents of *LINE as a command in the command list LIST.
784 LIST is a chain of struct cmd_list_element's.
785 If it is found, return the struct cmd_list_element for that command
786 and update *LINE to point after the command name, at the first argument.
787 If not found, call error if ALLOW_UNKNOWN is zero
788 otherwise (or if error returns) return zero.
789 Call error if specified command is ambiguous,
790 unless ALLOW_UNKNOWN is negative.
791 CMDTYPE precedes the word "command" in the error message.
793 If INGNORE_HELP_CLASSES is nonzero, ignore any command list
794 elements which are actually help classes rather than commands (i.e.
795 the function field of the struct cmd_list_element is 0). */
797 struct cmd_list_element
*
798 lookup_cmd (line
, list
, cmdtype
, allow_unknown
, ignore_help_classes
)
800 struct cmd_list_element
*list
;
803 int ignore_help_classes
;
805 struct cmd_list_element
*last_list
= 0;
806 struct cmd_list_element
*c
=
807 lookup_cmd_1 (line
, list
, &last_list
, ignore_help_classes
);
809 /* This is wrong for complete_command. */
810 char *ptr
= (*line
) + strlen (*line
) - 1;
812 /* Clear off trailing whitespace. */
813 while (ptr
>= *line
&& (*ptr
== ' ' || *ptr
== '\t'))
823 error ("Lack of needed %scommand", cmdtype
);
828 while (isalnum (*p
) || *p
== '-')
831 q
= (char *) alloca (p
- *line
+ 1);
832 strncpy (q
, *line
, p
- *line
);
834 undef_cmd_error (cmdtype
, q
);
840 else if (c
== (struct cmd_list_element
*) -1)
842 /* Ambigous. Local values should be off prefixlist or called
844 int local_allow_unknown
= (last_list
? last_list
->allow_unknown
:
846 char *local_cmdtype
= last_list
? last_list
->prefixname
: cmdtype
;
847 struct cmd_list_element
*local_list
=
848 (last_list
? *(last_list
->prefixlist
) : list
);
850 if (local_allow_unknown
< 0)
853 return last_list
; /* Found something. */
855 return 0; /* Found nothing. */
859 /* Report as error. */
864 ((*line
)[amb_len
] && (*line
)[amb_len
] != ' '
865 && (*line
)[amb_len
] != '\t');
870 for (c
= local_list
; c
; c
= c
->next
)
871 if (!strncmp (*line
, c
->name
, amb_len
))
873 if (strlen (ambbuf
) + strlen (c
->name
) + 6 < (int) sizeof ambbuf
)
876 strcat (ambbuf
, ", ");
877 strcat (ambbuf
, c
->name
);
881 strcat (ambbuf
, "..");
885 error ("Ambiguous %scommand \"%s\": %s.", local_cmdtype
,
892 /* We've got something. It may still not be what the caller
893 wants (if this command *needs* a subcommand). */
894 while (**line
== ' ' || **line
== '\t')
897 if (c
->prefixlist
&& **line
&& !c
->allow_unknown
)
898 undef_cmd_error (c
->prefixname
, *line
);
900 /* Seems to be what he wants. Return it. */
907 /* Look up the contents of *LINE as a command in the command list LIST.
908 LIST is a chain of struct cmd_list_element's.
909 If it is found, return the struct cmd_list_element for that command
910 and update *LINE to point after the command name, at the first argument.
911 If not found, call error if ALLOW_UNKNOWN is zero
912 otherwise (or if error returns) return zero.
913 Call error if specified command is ambiguous,
914 unless ALLOW_UNKNOWN is negative.
915 CMDTYPE precedes the word "command" in the error message. */
917 struct cmd_list_element
*
918 lookup_cmd (line
, list
, cmdtype
, allow_unknown
)
920 struct cmd_list_element
*list
;
925 register struct cmd_list_element
*c
, *found
;
931 /* Skip leading whitespace. */
933 while (**line
== ' ' || **line
== '\t')
936 /* Clear out trailing whitespace. */
938 p
= *line
+ strlen (*line
);
939 while (p
!= *line
&& (p
[-1] == ' ' || p
[-1] == '\t'))
943 /* Find end of command name. */
946 while (*p
== '-' || isalnum (*p
))
949 /* Look up the command name.
950 If exact match, keep that.
951 Otherwise, take command abbreviated, if unique. Note that (in my
952 opinion) a null string does *not* indicate ambiguity; simply the
953 end of the argument. */
958 error ("Lack of needed %scommand", cmdtype
);
962 /* Copy over to a local buffer, converting to lowercase on the way.
963 This is in case the command being parsed is a subcommand which
964 doesn't match anything, and that's ok. We want the original
965 untouched for the routine of the original command. */
967 processed_cmd
= (char *) alloca (p
- *line
+ 1);
968 for (cmd_len
= 0; cmd_len
< p
- *line
; cmd_len
++)
970 char x
= (*line
)[cmd_len
];
972 processed_cmd
[cmd_len
] = tolower (x
);
974 processed_cmd
[cmd_len
] = x
;
976 processed_cmd
[cmd_len
] = '\0';
978 /* Check all possibilities in the current command list. */
981 for (c
= list
; c
; c
= c
->next
)
983 if (!strncmp (processed_cmd
, c
->name
, cmd_len
))
987 if (c
->name
[cmd_len
] == 0)
995 /* Report error for undefined command name. */
999 if (nfound
> 1 && allow_unknown
>= 0)
1002 for (c
= list
; c
; c
= c
->next
)
1003 if (!strncmp (processed_cmd
, c
->name
, cmd_len
))
1005 if (strlen (ambbuf
) + strlen (c
->name
) + 6 < sizeof ambbuf
)
1007 if (strlen (ambbuf
))
1008 strcat (ambbuf
, ", ");
1009 strcat (ambbuf
, c
->name
);
1013 strcat (ambbuf
, "..");
1017 error ("Ambiguous %scommand \"%s\": %s.", cmdtype
,
1018 processed_cmd
, ambbuf
);
1020 else if (!allow_unknown
)
1021 error ("Undefined %scommand: \"%s\".", cmdtype
, processed_cmd
);
1025 /* Skip whitespace before the argument. */
1027 while (*p
== ' ' || *p
== '\t')
1031 if (found
->prefixlist
&& *p
)
1033 c
= lookup_cmd (line
, *found
->prefixlist
, found
->prefixname
,
1034 found
->allow_unknown
);
1043 /* Helper function for SYMBOL_COMPLETION_FUNCTION. */
1045 /* Return a vector of char pointers which point to the different
1046 possible completions in LIST of TEXT.
1048 WORD points in the same buffer as TEXT, and completions should be
1049 returned relative to this position. For example, suppose TEXT is "foo"
1050 and we want to complete to "foobar". If WORD is "oo", return
1051 "oobar"; if WORD is "baz/foo", return "baz/foobar". */
1054 complete_on_cmdlist (list
, text
, word
)
1055 struct cmd_list_element
*list
;
1059 struct cmd_list_element
*ptr
;
1061 int sizeof_matchlist
;
1063 int textlen
= strlen (text
);
1065 sizeof_matchlist
= 10;
1066 matchlist
= (char **) xmalloc (sizeof_matchlist
* sizeof (char *));
1069 for (ptr
= list
; ptr
; ptr
= ptr
->next
)
1070 if (!strncmp (ptr
->name
, text
, textlen
)
1071 && !ptr
->abbrev_flag
1072 && (ptr
->function
.cfunc
1073 || ptr
->prefixlist
))
1075 if (matches
== sizeof_matchlist
)
1077 sizeof_matchlist
*= 2;
1078 matchlist
= (char **) xrealloc ((char *) matchlist
,
1080 * sizeof (char *)));
1083 matchlist
[matches
] = (char *)
1084 xmalloc (strlen (word
) + strlen (ptr
->name
) + 1);
1086 strcpy (matchlist
[matches
], ptr
->name
);
1087 else if (word
> text
)
1089 /* Return some portion of ptr->name. */
1090 strcpy (matchlist
[matches
], ptr
->name
+ (word
- text
));
1094 /* Return some of text plus ptr->name. */
1095 strncpy (matchlist
[matches
], word
, text
- word
);
1096 matchlist
[matches
][text
- word
] = '\0';
1097 strcat (matchlist
[matches
], ptr
->name
);
1104 free ((PTR
) matchlist
);
1109 matchlist
= (char **) xrealloc ((char *) matchlist
, ((matches
+ 1)
1110 * sizeof (char *)));
1111 matchlist
[matches
] = (char *) 0;
1117 /* Helper function for SYMBOL_COMPLETION_FUNCTION. */
1119 /* Return a vector of char pointers which point to the different
1120 possible completions in CMD of TEXT.
1122 WORD points in the same buffer as TEXT, and completions should be
1123 returned relative to this position. For example, suppose TEXT is "foo"
1124 and we want to complete to "foobar". If WORD is "oo", return
1125 "oobar"; if WORD is "baz/foo", return "baz/foobar". */
1128 complete_on_enum (enumlist
, text
, word
)
1134 int sizeof_matchlist
;
1136 int textlen
= strlen (text
);
1140 sizeof_matchlist
= 10;
1141 matchlist
= (char **) xmalloc (sizeof_matchlist
* sizeof (char *));
1144 for (i
= 0; (name
= enumlist
[i
]) != NULL
; i
++)
1145 if (strncmp (name
, text
, textlen
) == 0)
1147 if (matches
== sizeof_matchlist
)
1149 sizeof_matchlist
*= 2;
1150 matchlist
= (char **) xrealloc ((char *) matchlist
,
1152 * sizeof (char *)));
1155 matchlist
[matches
] = (char *)
1156 xmalloc (strlen (word
) + strlen (name
) + 1);
1158 strcpy (matchlist
[matches
], name
);
1159 else if (word
> text
)
1161 /* Return some portion of name. */
1162 strcpy (matchlist
[matches
], name
+ (word
- text
));
1166 /* Return some of text plus name. */
1167 strncpy (matchlist
[matches
], word
, text
- word
);
1168 matchlist
[matches
][text
- word
] = '\0';
1169 strcat (matchlist
[matches
], name
);
1176 free ((PTR
) matchlist
);
1181 matchlist
= (char **) xrealloc ((char *) matchlist
, ((matches
+ 1)
1182 * sizeof (char *)));
1183 matchlist
[matches
] = (char *) 0;
1190 parse_binary_operation (arg
)
1198 length
= strlen (arg
);
1200 while (arg
[length
- 1] == ' ' || arg
[length
- 1] == '\t')
1203 if (!strncmp (arg
, "on", length
)
1204 || !strncmp (arg
, "1", length
)
1205 || !strncmp (arg
, "yes", length
))
1207 else if (!strncmp (arg
, "off", length
)
1208 || !strncmp (arg
, "0", length
)
1209 || !strncmp (arg
, "no", length
))
1213 error ("\"on\" or \"off\" expected.");
1218 /* Do a "set" or "show" command. ARG is NULL if no argument, or the text
1219 of the argument, and FROM_TTY is nonzero if this command is being entered
1220 directly by the user (i.e. these are just like any other
1221 command). C is the command list element for the command. */
1223 do_setshow_command (arg
, from_tty
, c
)
1226 struct cmd_list_element
*c
;
1228 if (c
->type
== set_cmd
)
1230 switch (c
->var_type
)
1241 new = (char *) xmalloc (strlen (arg
) + 2);
1244 while ((ch
= *p
++) != '\000')
1248 /* \ at end of argument is used after spaces
1249 so they won't be lost. */
1250 /* This is obsolete now that we no longer strip
1251 trailing whitespace and actually, the backslash
1252 didn't get here in my test, readline or
1253 something did something funky with a backslash
1254 right before a newline. */
1257 ch
= parse_escape (&p
);
1259 break; /* C loses */
1267 if (*(p
- 1) != '\\')
1271 new = (char *) xrealloc (new, q
- new);
1272 if (*(char **) c
->var
!= NULL
)
1273 free (*(char **) c
->var
);
1274 *(char **) c
->var
= new;
1277 case var_string_noescape
:
1280 if (*(char **) c
->var
!= NULL
)
1281 free (*(char **) c
->var
);
1282 *(char **) c
->var
= savestring (arg
, strlen (arg
));
1286 error_no_arg ("filename to set it to.");
1287 if (*(char **) c
->var
!= NULL
)
1288 free (*(char **) c
->var
);
1289 *(char **) c
->var
= tilde_expand (arg
);
1292 *(int *) c
->var
= parse_binary_operation (arg
);
1296 error_no_arg ("integer to set it to.");
1297 *(unsigned int *) c
->var
= parse_and_eval_address (arg
);
1298 if (*(unsigned int *) c
->var
== 0)
1299 *(unsigned int *) c
->var
= UINT_MAX
;
1305 error_no_arg ("integer to set it to.");
1306 val
= parse_and_eval_address (arg
);
1308 *(int *) c
->var
= INT_MAX
;
1309 else if (val
>= INT_MAX
)
1310 error ("integer %u out of range", val
);
1312 *(int *) c
->var
= val
;
1317 error_no_arg ("integer to set it to.");
1318 *(int *) c
->var
= parse_and_eval_address (arg
);
1328 /* if no argument was supplied, print an informative error message */
1332 strcpy (msg
, "Requires an argument. Valid arguments are ");
1333 for (i
= 0; c
->enums
[i
]; i
++)
1337 strcat (msg
, c
->enums
[i
]);
1343 p
= strchr (arg
, ' ');
1351 for (i
= 0; c
->enums
[i
]; i
++)
1352 if (strncmp (arg
, c
->enums
[i
], len
) == 0)
1354 match
= c
->enums
[i
];
1359 error ("Undefined item: \"%s\".", arg
);
1362 error ("Ambiguous item \"%s\".", arg
);
1364 *(char **) c
->var
= match
;
1368 error ("gdb internal error: bad var_type in do_setshow_command");
1371 else if (c
->type
== show_cmd
)
1374 struct cleanup
*old_chain
;
1375 struct ui_stream
*stb
;
1378 stb
= ui_out_stream_new (uiout
);
1379 old_chain
= make_cleanup ((make_cleanup_func
) ui_out_stream_delete
, stb
);
1382 /* Print doc minus "show" at start. */
1383 print_doc_line (gdb_stdout
, c
->doc
+ 5);
1386 ui_out_text (uiout
, " is ");
1387 ui_out_wrap_hint (uiout
, " ");
1389 switch (c
->var_type
)
1395 if (*(unsigned char **) c
->var
)
1396 fputstr_filtered (*(unsigned char **) c
->var
, '"', stb
->stream
);
1400 case var_string_noescape
:
1403 if (*(char **) c
->var
)
1404 fputs_filtered (*(char **) c
->var
, stb
->stream
);
1408 fputs_filtered (*(int *) c
->var
? "on" : "off", stb
->stream
);
1411 if (*(unsigned int *) c
->var
== UINT_MAX
)
1413 fputs_filtered ("unlimited", stb
->stream
);
1416 /* else fall through */
1418 fprintf_filtered (stb
->stream
, "%u", *(unsigned int *) c
->var
);
1421 if (*(int *) c
->var
== INT_MAX
)
1423 fputs_filtered ("unlimited", stb
->stream
);
1426 fprintf_filtered (stb
->stream
, "%d", *(int *) c
->var
);
1430 error ("gdb internal error: bad var_type in do_setshow_command");
1433 ui_out_text (uiout
, "\"");
1434 ui_out_field_stream (uiout
, "value", stb
);
1436 ui_out_text (uiout
, "\"");
1437 ui_out_text (uiout
, ".\n");
1438 do_cleanups (old_chain
);
1440 fputs_filtered (" is ", gdb_stdout
);
1442 switch (c
->var_type
)
1446 fputs_filtered ("\"", gdb_stdout
);
1447 if (*(unsigned char **) c
->var
)
1448 fputstr_filtered (*(unsigned char **) c
->var
, '"', gdb_stdout
);
1449 fputs_filtered ("\"", gdb_stdout
);
1452 case var_string_noescape
:
1455 fputs_filtered ("\"", gdb_stdout
);
1456 if (*(char **) c
->var
)
1457 fputs_filtered (*(char **) c
->var
, gdb_stdout
);
1458 fputs_filtered ("\"", gdb_stdout
);
1461 fputs_filtered (*(int *) c
->var
? "on" : "off", gdb_stdout
);
1464 if (*(unsigned int *) c
->var
== UINT_MAX
)
1466 fputs_filtered ("unlimited", gdb_stdout
);
1469 /* else fall through */
1471 fprintf_filtered (gdb_stdout
, "%u", *(unsigned int *) c
->var
);
1474 if (*(int *) c
->var
== INT_MAX
)
1476 fputs_filtered ("unlimited", gdb_stdout
);
1479 fprintf_filtered (gdb_stdout
, "%d", *(int *) c
->var
);
1483 error ("gdb internal error: bad var_type in do_setshow_command");
1485 fputs_filtered (".\n", gdb_stdout
);
1489 error ("gdb internal error: bad cmd_type in do_setshow_command");
1490 (*c
->function
.sfunc
) (NULL
, from_tty
, c
);
1491 if (c
->type
== set_cmd
&& set_hook
)
1495 /* Show all the settings in a list of show commands. */
1498 cmd_show_list (list
, from_tty
, prefix
)
1499 struct cmd_list_element
*list
;
1504 ui_out_list_begin (uiout
, "showlist");
1506 for (; list
!= NULL
; list
= list
->next
)
1508 /* If we find a prefix, run its list, prefixing our output by its
1509 prefix (with "show " skipped). */
1511 if (list
->prefixlist
&& !list
->abbrev_flag
)
1513 ui_out_list_begin (uiout
, "optionlist");
1514 ui_out_field_string (uiout
, "prefix", list
->prefixname
+ 5);
1515 cmd_show_list (*list
->prefixlist
, from_tty
, list
->prefixname
+ 5);
1516 ui_out_list_end (uiout
);
1518 if (list
->type
== show_cmd
)
1520 ui_out_list_begin (uiout
, "option");
1521 ui_out_text (uiout
, prefix
);
1522 ui_out_field_string (uiout
, "name", list
->name
);
1523 ui_out_text (uiout
, ": ");
1524 do_setshow_command ((char *) NULL
, from_tty
, list
);
1525 ui_out_list_end (uiout
);
1528 if (list
->prefixlist
&& !list
->abbrev_flag
)
1529 cmd_show_list (*list
->prefixlist
, from_tty
, list
->prefixname
+ 5);
1530 if (list
->type
== show_cmd
)
1532 fputs_filtered (prefix
, gdb_stdout
);
1533 fputs_filtered (list
->name
, gdb_stdout
);
1534 fputs_filtered (": ", gdb_stdout
);
1535 do_setshow_command ((char *) NULL
, from_tty
, list
);
1540 ui_out_list_end (uiout
);
1546 shell_escape (arg
, from_tty
)
1551 /* If ARG is NULL, they want an inferior shell, but `system' just
1552 reports if the shell is available when passed a NULL arg. */
1553 int rc
= system (arg
? arg
: "");
1556 arg
= "inferior shell";
1560 fprintf_unfiltered (gdb_stderr
, "Cannot execute %s: %s\n", arg
,
1561 safe_strerror (errno
));
1562 gdb_flush (gdb_stderr
);
1566 fprintf_unfiltered (gdb_stderr
, "%s exited with status %d\n", arg
, rc
);
1567 gdb_flush (gdb_stderr
);
1570 /* Make sure to return to the directory GDB thinks it is, in case the
1571 shell command we just ran changed it. */
1572 chdir (current_directory
);
1574 #else /* Can fork. */
1575 int rc
, status
, pid
;
1576 char *p
, *user_shell
;
1578 if ((user_shell
= (char *) getenv ("SHELL")) == NULL
)
1579 user_shell
= "/bin/sh";
1581 /* Get the name of the shell for arg0 */
1582 if ((p
= strrchr (user_shell
, '/')) == NULL
)
1585 p
++; /* Get past '/' */
1587 if ((pid
= fork ()) == 0)
1590 execl (user_shell
, p
, 0);
1592 execl (user_shell
, p
, "-c", arg
, 0);
1594 fprintf_unfiltered (gdb_stderr
, "Cannot execute %s: %s\n", user_shell
,
1595 safe_strerror (errno
));
1596 gdb_flush (gdb_stderr
);
1601 while ((rc
= wait (&status
)) != pid
&& rc
!= -1)
1604 error ("Fork failed");
1605 #endif /* Can fork. */
1609 make_command (arg
, from_tty
)
1619 p
= xmalloc (sizeof ("make ") + strlen (arg
));
1620 strcpy (p
, "make ");
1621 strcpy (p
+ sizeof ("make ") - 1, arg
);
1624 shell_escape (p
, from_tty
);
1628 show_user_1 (c
, stream
)
1629 struct cmd_list_element
*c
;
1630 struct ui_file
*stream
;
1632 register struct command_line
*cmdlines
;
1634 cmdlines
= c
->user_commands
;
1637 fputs_filtered ("User command ", stream
);
1638 fputs_filtered (c
->name
, stream
);
1639 fputs_filtered (":\n", stream
);
1642 print_command_lines (uiout
, cmdlines
, 1);
1643 fputs_filtered ("\n", stream
);
1647 print_command_line (cmdlines
, 4, stream
);
1648 cmdlines
= cmdlines
->next
;
1650 fputs_filtered ("\n", stream
);
1656 show_user (args
, from_tty
)
1660 struct cmd_list_element
*c
;
1661 extern struct cmd_list_element
*cmdlist
;
1665 c
= lookup_cmd (&args
, cmdlist
, "", 0, 1);
1666 if (c
->class != class_user
)
1667 error ("Not a user command.");
1668 show_user_1 (c
, gdb_stdout
);
1672 for (c
= cmdlist
; c
; c
= c
->next
)
1674 if (c
->class == class_user
)
1675 show_user_1 (c
, gdb_stdout
);
1681 _initialize_command ()
1683 add_com ("shell", class_support
, shell_escape
,
1684 "Execute the rest of the line as a shell command. \n\
1685 With no arguments, run an inferior shell.");
1688 add_com_alias ("!", "shell", class_support
, 0);
1690 add_com ("make", class_support
, make_command
,
1691 "Run the ``make'' program using the rest of the line as arguments.");
1692 add_cmd ("user", no_class
, show_user
,
1693 "Show definitions of user defined commands.\n\
1694 Argument is the name of the user defined command.\n\
1695 With no argument, show definitions of all user defined commands.", &showlist
);