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. */
22 #include "gnu-regex.h"
28 #include "cli/cli-cmds.h"
29 #include "cli/cli-decode.h"
31 /* Prototypes for local functions */
33 static void undef_cmd_error (char *, char *);
35 static struct cmd_list_element
*find_cmd (char *command
,
37 struct cmd_list_element
*clist
,
38 int ignore_help_classes
,
41 static void help_all (struct ui_file
*stream
);
43 /* Add element named NAME.
44 CLASS is the top level category into which commands are broken down
46 FUN should be the function to execute the command;
47 it will get a character string as argument, with leading
48 and trailing blanks already eliminated.
50 DOC is a documentation string for the command.
51 Its first line should be a complete sentence.
52 It should start with ? for a command that is an abbreviation
53 or with * for a command that most users don't need to know about.
55 Add this command to command list *LIST.
57 Returns a pointer to the added command (not necessarily the head
60 struct cmd_list_element
*
61 add_cmd (char *name
, enum command_class
class, void (*fun
) (char *, int),
62 char *doc
, struct cmd_list_element
**list
)
64 register struct cmd_list_element
*c
65 = (struct cmd_list_element
*) xmalloc (sizeof (struct cmd_list_element
));
66 struct cmd_list_element
*p
;
68 delete_cmd (name
, list
);
70 if (*list
== NULL
|| STRCMP ((*list
)->name
, name
) >= 0)
78 while (p
->next
&& STRCMP (p
->next
->name
, name
) <= 0)
88 c
->function
.cfunc
= fun
;
91 c
->replacement
= NULL
;
99 c
->completer
= make_symbol_completion_list
;
100 c
->type
= not_set_cmd
;
102 c
->var_type
= var_boolean
;
104 c
->user_commands
= NULL
;
105 c
->hookee_pre
= NULL
;
106 c
->hookee_post
= NULL
;
107 c
->cmd_pointer
= NULL
;
113 /* Deprecates a command CMD.
114 REPLACEMENT is the name of the command which should be used in place
115 of this command, or NULL if no such command exists.
117 This function does not check to see if command REPLACEMENT exists
118 since gdb may not have gotten around to adding REPLACEMENT when this
121 Returns a pointer to the deprecated command. */
123 struct cmd_list_element
*
124 deprecate_cmd (struct cmd_list_element
*cmd
, char *replacement
)
126 cmd
->flags
|= (CMD_DEPRECATED
| DEPRECATED_WARN_USER
);
128 if (replacement
!= NULL
)
129 cmd
->replacement
= replacement
;
131 cmd
->replacement
= NULL
;
137 /* Same as above, except that the abbrev_flag is set. */
139 #if 0 /* Currently unused */
141 struct cmd_list_element
*
142 add_abbrev_cmd (char *name
, enum command_class
class, void (*fun
) (char *, int),
143 char *doc
, struct cmd_list_element
**list
)
145 register struct cmd_list_element
*c
146 = add_cmd (name
, class, fun
, doc
, list
);
154 struct cmd_list_element
*
155 add_alias_cmd (char *name
, char *oldname
, enum command_class
class,
156 int abbrev_flag
, struct cmd_list_element
**list
)
158 /* Must do this since lookup_cmd tries to side-effect its first arg */
160 register struct cmd_list_element
*old
;
161 register struct cmd_list_element
*c
;
162 copied_name
= (char *) alloca (strlen (oldname
) + 1);
163 strcpy (copied_name
, oldname
);
164 old
= lookup_cmd (&copied_name
, *list
, "", 1, 1);
168 delete_cmd (name
, list
);
172 c
= add_cmd (name
, class, old
->function
.cfunc
, old
->doc
, list
);
173 c
->prefixlist
= old
->prefixlist
;
174 c
->prefixname
= old
->prefixname
;
175 c
->allow_unknown
= old
->allow_unknown
;
176 c
->abbrev_flag
= abbrev_flag
;
177 c
->cmd_pointer
= old
;
181 /* Like add_cmd but adds an element for a command prefix:
182 a name that should be followed by a subcommand to be looked up
183 in another command list. PREFIXLIST should be the address
184 of the variable containing that list. */
186 struct cmd_list_element
*
187 add_prefix_cmd (char *name
, enum command_class
class, void (*fun
) (char *, int),
188 char *doc
, struct cmd_list_element
**prefixlist
,
189 char *prefixname
, int allow_unknown
,
190 struct cmd_list_element
**list
)
192 register struct cmd_list_element
*c
= add_cmd (name
, class, fun
, doc
, list
);
193 c
->prefixlist
= prefixlist
;
194 c
->prefixname
= prefixname
;
195 c
->allow_unknown
= allow_unknown
;
199 /* Like add_prefix_cmd but sets the abbrev_flag on the new command. */
201 struct cmd_list_element
*
202 add_abbrev_prefix_cmd (char *name
, enum command_class
class,
203 void (*fun
) (char *, int), char *doc
,
204 struct cmd_list_element
**prefixlist
, char *prefixname
,
205 int allow_unknown
, struct cmd_list_element
**list
)
207 register struct cmd_list_element
*c
= add_cmd (name
, class, fun
, doc
, list
);
208 c
->prefixlist
= prefixlist
;
209 c
->prefixname
= prefixname
;
210 c
->allow_unknown
= allow_unknown
;
215 /* This is an empty "cfunc". */
217 not_just_help_class_command (char *args
, int from_tty
)
221 /* This is an empty "sfunc". */
222 static void empty_sfunc (char *, int, struct cmd_list_element
*);
225 empty_sfunc (char *args
, int from_tty
, struct cmd_list_element
*c
)
229 /* Add element named NAME to command list LIST (the list for set
230 or some sublist thereof).
231 CLASS is as in add_cmd.
232 VAR_TYPE is the kind of thing we are setting.
233 VAR is address of the variable being controlled by this command.
234 DOC is the documentation string. */
236 struct cmd_list_element
*
237 add_set_cmd (char *name
,
238 enum command_class
class,
242 struct cmd_list_element
**list
)
244 struct cmd_list_element
*c
245 = add_cmd (name
, class, NO_FUNCTION
, doc
, list
);
248 c
->var_type
= var_type
;
250 /* This needs to be something besides NO_FUNCTION so that this isn't
251 treated as a help class. */
252 c
->function
.sfunc
= empty_sfunc
;
256 /* Add element named NAME to command list LIST (the list for set
257 or some sublist thereof).
258 CLASS is as in add_cmd.
259 ENUMLIST is a list of strings which may follow NAME.
260 VAR is address of the variable which will contain the matching string
262 DOC is the documentation string. */
264 struct cmd_list_element
*
265 add_set_enum_cmd (char *name
,
266 enum command_class
class,
267 const char *enumlist
[],
270 struct cmd_list_element
**list
)
272 struct cmd_list_element
*c
273 = add_set_cmd (name
, class, var_enum
, var
, doc
, list
);
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 VAR is address of the variable which will contain the value.
283 DOC is the documentation string. */
284 struct cmd_list_element
*
285 add_set_auto_boolean_cmd (char *name
,
286 enum command_class
class,
287 enum cmd_auto_boolean
*var
,
289 struct cmd_list_element
**list
)
291 static const char *auto_boolean_enums
[] = { "on", "off", "auto", NULL
};
292 struct cmd_list_element
*c
;
293 c
= add_set_cmd (name
, class, var_auto_boolean
, var
, doc
, list
);
294 c
->enums
= auto_boolean_enums
;
298 /* Where SETCMD has already been added, add the corresponding show
299 command to LIST and return a pointer to the added command (not
300 necessarily the head of LIST). */
301 struct cmd_list_element
*
302 add_show_from_set (struct cmd_list_element
*setcmd
,
303 struct cmd_list_element
**list
)
305 struct cmd_list_element
*showcmd
=
306 (struct cmd_list_element
*) xmalloc (sizeof (struct cmd_list_element
));
307 struct cmd_list_element
*p
;
309 memcpy (showcmd
, setcmd
, sizeof (struct cmd_list_element
));
310 delete_cmd (showcmd
->name
, list
);
311 showcmd
->type
= show_cmd
;
313 /* Replace "set " at start of docstring with "show ". */
314 if (setcmd
->doc
[0] == 'S' && setcmd
->doc
[1] == 'e'
315 && setcmd
->doc
[2] == 't' && setcmd
->doc
[3] == ' ')
316 showcmd
->doc
= concat ("Show ", setcmd
->doc
+ 4, NULL
);
318 fprintf_unfiltered (gdb_stderr
, "GDB internal error: Bad docstring for set command\n");
320 if (*list
== NULL
|| STRCMP ((*list
)->name
, showcmd
->name
) >= 0)
322 showcmd
->next
= *list
;
328 while (p
->next
&& STRCMP (p
->next
->name
, showcmd
->name
) <= 0)
332 showcmd
->next
= p
->next
;
339 /* Remove the command named NAME from the command list. */
342 delete_cmd (char *name
, struct cmd_list_element
**list
)
344 register struct cmd_list_element
*c
;
345 struct cmd_list_element
*p
;
347 while (*list
&& STREQ ((*list
)->name
, name
))
349 if ((*list
)->hookee_pre
)
350 (*list
)->hookee_pre
->hook_pre
= 0; /* Hook slips out of its mouth */
351 if ((*list
)->hookee_post
)
352 (*list
)->hookee_post
->hook_post
= 0; /* Hook slips out of its bottom */
359 for (c
= *list
; c
->next
;)
361 if (STREQ (c
->next
->name
, name
))
363 if (c
->next
->hookee_pre
)
364 c
->next
->hookee_pre
->hook_pre
= 0; /* hooked cmd gets away. */
365 if (c
->next
->hookee_post
)
366 c
->next
->hookee_post
->hook_post
= 0; /* remove post hook */
367 /* :( no fishing metaphore */
369 free ((PTR
) c
->next
);
377 /* Shorthands to the commands above. */
379 /* Add an element to the list of info subcommands. */
381 struct cmd_list_element
*
382 add_info (char *name
, void (*fun
) (char *, int), char *doc
)
384 return add_cmd (name
, no_class
, fun
, doc
, &infolist
);
387 /* Add an alias to the list of info subcommands. */
389 struct cmd_list_element
*
390 add_info_alias (char *name
, char *oldname
, int abbrev_flag
)
392 return add_alias_cmd (name
, oldname
, 0, abbrev_flag
, &infolist
);
395 /* Add an element to the list of commands. */
397 struct cmd_list_element
*
398 add_com (char *name
, enum command_class
class, void (*fun
) (char *, int),
401 return add_cmd (name
, class, fun
, doc
, &cmdlist
);
404 /* Add an alias or abbreviation command to the list of commands. */
406 struct cmd_list_element
*
407 add_com_alias (char *name
, char *oldname
, enum command_class
class,
410 return add_alias_cmd (name
, oldname
, class, abbrev_flag
, &cmdlist
);
413 /* Recursively walk the commandlist structures, and print out the
414 documentation of commands that match our regex in either their
415 name, or their documentation.
418 apropos_cmd (struct ui_file
*stream
, struct cmd_list_element
*commandlist
,
419 struct re_pattern_buffer
*regex
, char *prefix
)
421 register struct cmd_list_element
*c
;
422 int returnvalue
=1; /*Needed to avoid double printing*/
423 /* Walk through the commands */
424 for (c
=commandlist
;c
;c
=c
->next
)
428 /* Try to match against the name*/
429 returnvalue
=re_search(regex
,c
->name
,strlen(c
->name
),0,strlen(c
->name
),NULL
);
430 if (returnvalue
>= 0)
432 /* Stolen from help_cmd_list. We don't directly use
433 * help_cmd_list because it doesn't let us print out
436 fprintf_filtered (stream
, "%s%s -- ", prefix
, c
->name
);
437 print_doc_line (stream
, c
->doc
);
438 fputs_filtered ("\n", stream
);
439 returnvalue
=0; /*Set this so we don't print it again.*/
442 if (c
->doc
!= NULL
&& returnvalue
!= 0)
444 /* Try to match against documentation */
445 if (re_search(regex
,c
->doc
,strlen(c
->doc
),0,strlen(c
->doc
),NULL
) >=0)
447 /* Stolen from help_cmd_list. We don't directly use
448 * help_cmd_list because it doesn't let us print out
451 fprintf_filtered (stream
, "%s%s -- ", prefix
, c
->name
);
452 print_doc_line (stream
, c
->doc
);
453 fputs_filtered ("\n", stream
);
456 /* Check if this command has subcommands */
457 if (c
->prefixlist
!= NULL
)
459 /* Recursively call ourselves on the subcommand list,
460 passing the right prefix in.
462 apropos_cmd (stream
,*c
->prefixlist
,regex
,c
->prefixname
);
467 /* This command really has to deal with two things:
468 * 1) I want documentation on *this string* (usually called by
469 * "help commandname").
470 * 2) I want documentation on *this list* (usually called by
471 * giving a command that requires subcommands. Also called by saying
474 * I am going to split this into two seperate comamnds, help_cmd and
479 help_cmd (char *command
, struct ui_file
*stream
)
481 struct cmd_list_element
*c
;
482 extern struct cmd_list_element
*cmdlist
;
486 help_list (cmdlist
, "", all_classes
, stream
);
490 if (strcmp (command
, "all") == 0)
496 c
= lookup_cmd (&command
, cmdlist
, "", 0, 0);
501 /* There are three cases here.
502 If c->prefixlist is nonzero, we have a prefix command.
503 Print its documentation, then list its subcommands.
505 If c->function is nonzero, we really have a command.
506 Print its documentation and return.
508 If c->function is zero, we have a class name.
509 Print its documentation (as if it were a command)
510 and then set class to the number of this class
511 so that the commands in the class will be listed. */
513 fputs_filtered (c
->doc
, stream
);
514 fputs_filtered ("\n", stream
);
516 if (c
->prefixlist
== 0 && c
->function
.cfunc
!= NULL
)
518 fprintf_filtered (stream
, "\n");
520 /* If this is a prefix command, print it's subcommands */
522 help_list (*c
->prefixlist
, c
->prefixname
, all_commands
, stream
);
524 /* If this is a class name, print all of the commands in the class */
525 if (c
->function
.cfunc
== NULL
)
526 help_list (cmdlist
, "", c
->class, stream
);
528 if (c
->hook_pre
|| c
->hook_post
)
529 fprintf_filtered (stream
,
530 "\nThis command has a hook (or hooks) defined:\n");
533 fprintf_filtered (stream
,
534 "\tThis command is run after : %s (pre hook)\n",
537 fprintf_filtered (stream
,
538 "\tThis command is run before : %s (post hook)\n",
543 * Get a specific kind of help on a command list.
546 * CMDTYPE is the prefix to use in the title string.
547 * CLASS is the class with which to list the nodes of this list (see
548 * documentation for help_cmd_list below), As usual, ALL_COMMANDS for
549 * everything, ALL_CLASSES for just classes, and non-negative for only things
550 * in a specific class.
551 * and STREAM is the output stream on which to print things.
552 * If you call this routine with a class >= 0, it recurses.
555 help_list (struct cmd_list_element
*list
, char *cmdtype
,
556 enum command_class
class, struct ui_file
*stream
)
559 char *cmdtype1
, *cmdtype2
;
561 /* If CMDTYPE is "foo ", CMDTYPE1 gets " foo" and CMDTYPE2 gets "foo sub" */
562 len
= strlen (cmdtype
);
563 cmdtype1
= (char *) alloca (len
+ 1);
565 cmdtype2
= (char *) alloca (len
+ 4);
570 strncpy (cmdtype1
+ 1, cmdtype
, len
- 1);
572 strncpy (cmdtype2
, cmdtype
, len
- 1);
573 strcpy (cmdtype2
+ len
- 1, " sub");
576 if (class == all_classes
)
577 fprintf_filtered (stream
, "List of classes of %scommands:\n\n", cmdtype2
);
579 fprintf_filtered (stream
, "List of %scommands:\n\n", cmdtype2
);
581 help_cmd_list (list
, class, cmdtype
, (int) class >= 0, stream
);
583 if (class == all_classes
)
584 fprintf_filtered (stream
, "\n\
585 Type \"help%s\" followed by a class name for a list of commands in that class.",
588 fprintf_filtered (stream
, "\n\
589 Type \"help%s\" followed by %scommand name for full documentation.\n\
590 Command name abbreviations are allowed if unambiguous.\n",
595 help_all (struct ui_file
*stream
)
597 struct cmd_list_element
*c
;
598 extern struct cmd_list_element
*cmdlist
;
600 for (c
= cmdlist
; c
; c
= c
->next
)
604 /* If this is a prefix command, print it's subcommands */
606 help_cmd_list (*c
->prefixlist
, all_commands
, c
->prefixname
, 0, stream
);
608 /* If this is a class name, print all of the commands in the class */
609 else if (c
->function
.cfunc
== NULL
)
610 help_cmd_list (cmdlist
, c
->class, "", 0, stream
);
614 /* Print only the first line of STR on STREAM. */
616 print_doc_line (struct ui_file
*stream
, char *str
)
618 static char *line_buffer
= 0;
619 static int line_size
;
625 line_buffer
= (char *) xmalloc (line_size
);
629 while (*p
&& *p
!= '\n' && *p
!= '.' && *p
!= ',')
631 if (p
- str
> line_size
- 1)
633 line_size
= p
- str
+ 1;
634 free ((PTR
) line_buffer
);
635 line_buffer
= (char *) xmalloc (line_size
);
637 strncpy (line_buffer
, str
, p
- str
);
638 line_buffer
[p
- str
] = '\0';
639 if (islower (line_buffer
[0]))
640 line_buffer
[0] = toupper (line_buffer
[0]);
642 ui_out_text (uiout
, line_buffer
);
644 fputs_filtered (line_buffer
, stream
);
649 * Implement a help command on command list LIST.
650 * RECURSE should be non-zero if this should be done recursively on
651 * all sublists of LIST.
652 * PREFIX is the prefix to print before each command name.
653 * STREAM is the stream upon which the output should be written.
655 * A non-negative class number to list only commands in that
657 * ALL_COMMANDS to list all commands in list.
658 * ALL_CLASSES to list all classes in list.
660 * Note that RECURSE will be active on *all* sublists, not just the
661 * ones selected by the criteria above (ie. the selection mechanism
662 * is at the low level, not the high-level).
665 help_cmd_list (struct cmd_list_element
*list
, enum command_class
class,
666 char *prefix
, int recurse
, struct ui_file
*stream
)
668 register struct cmd_list_element
*c
;
670 for (c
= list
; c
; c
= c
->next
)
672 if (c
->abbrev_flag
== 0 &&
673 (class == all_commands
674 || (class == all_classes
&& c
->function
.cfunc
== NULL
)
675 || (class == c
->class && c
->function
.cfunc
!= NULL
)))
677 fprintf_filtered (stream
, "%s%s -- ", prefix
, c
->name
);
678 print_doc_line (stream
, c
->doc
);
679 fputs_filtered ("\n", stream
);
682 && c
->prefixlist
!= 0
683 && c
->abbrev_flag
== 0)
684 help_cmd_list (*c
->prefixlist
, class, c
->prefixname
, 1, stream
);
689 /* Search the input clist for 'command'. Return the command if
690 found (or NULL if not), and return the number of commands
693 static struct cmd_list_element
*
694 find_cmd (char *command
, int len
, struct cmd_list_element
*clist
,
695 int ignore_help_classes
, int *nfound
)
697 struct cmd_list_element
*found
, *c
;
699 found
= (struct cmd_list_element
*) NULL
;
701 for (c
= clist
; c
; c
= c
->next
)
702 if (!strncmp (command
, c
->name
, len
)
703 && (!ignore_help_classes
|| c
->function
.cfunc
))
707 if (c
->name
[len
] == '\0')
716 /* This routine takes a line of TEXT and a CLIST in which to start the
717 lookup. When it returns it will have incremented the text pointer past
718 the section of text it matched, set *RESULT_LIST to point to the list in
719 which the last word was matched, and will return a pointer to the cmd
720 list element which the text matches. It will return NULL if no match at
721 all was possible. It will return -1 (cast appropriately, ick) if ambigous
722 matches are possible; in this case *RESULT_LIST will be set to point to
723 the list in which there are ambiguous choices (and *TEXT will be set to
724 the ambiguous text string).
726 If the located command was an abbreviation, this routine returns the base
727 command of the abbreviation.
729 It does no error reporting whatsoever; control will always return
730 to the superior routine.
732 In the case of an ambiguous return (-1), *RESULT_LIST will be set to point
733 at the prefix_command (ie. the best match) *or* (special case) will be NULL
734 if no prefix command was ever found. For example, in the case of "info a",
735 "info" matches without ambiguity, but "a" could be "args" or "address", so
736 *RESULT_LIST is set to the cmd_list_element for "info". So in this case
737 RESULT_LIST should not be interpeted as a pointer to the beginning of a
738 list; it simply points to a specific command. In the case of an ambiguous
739 return *TEXT is advanced past the last non-ambiguous prefix (e.g.
740 "info t" can be "info types" or "info target"; upon return *TEXT has been
741 advanced past "info ").
743 If RESULT_LIST is NULL, don't set *RESULT_LIST (but don't otherwise
744 affect the operation).
746 This routine does *not* modify the text pointed to by TEXT.
748 If IGNORE_HELP_CLASSES is nonzero, ignore any command list elements which
749 are actually help classes rather than commands (i.e. the function field of
750 the struct cmd_list_element is NULL). */
752 struct cmd_list_element
*
753 lookup_cmd_1 (char **text
, struct cmd_list_element
*clist
,
754 struct cmd_list_element
**result_list
, int ignore_help_classes
)
757 int len
, tmp
, nfound
;
758 struct cmd_list_element
*found
, *c
;
761 while (**text
== ' ' || **text
== '\t')
764 /* Treating underscores as part of command words is important
765 so that "set args_foo()" doesn't get interpreted as
766 "set args _foo()". */
768 *p
&& (isalnum (*p
) || *p
== '-' || *p
== '_' ||
770 (*p
== '+' || *p
== '<' || *p
== '>' || *p
== '$')) ||
771 (xdb_commands
&& (*p
== '!' || *p
== '/' || *p
== '?')));
775 /* If nothing but whitespace, return 0. */
781 /* *text and p now bracket the first command word to lookup (and
782 it's length is len). We copy this into a local temporary */
785 command
= (char *) alloca (len
+ 1);
786 for (tmp
= 0; tmp
< len
; tmp
++)
788 char x
= (*text
)[tmp
];
796 found
= find_cmd (command
, len
, clist
, ignore_help_classes
, &nfound
);
799 ** We didn't find the command in the entered case, so lower case it
802 if (!found
|| nfound
== 0)
804 for (tmp
= 0; tmp
< len
; tmp
++)
806 char x
= command
[tmp
];
807 command
[tmp
] = isupper (x
) ? tolower (x
) : x
;
809 found
= find_cmd (command
, len
, clist
, ignore_help_classes
, &nfound
);
812 /* If nothing matches, we have a simple failure. */
818 if (result_list
!= NULL
)
819 /* Will be modified in calling routine
820 if we know what the prefix command is. */
822 return (struct cmd_list_element
*) -1; /* Ambiguous. */
825 /* We've matched something on this list. Move text pointer forward. */
829 if (found
->cmd_pointer
)
831 /* We drop the alias (abbreviation) in favor of the command it is
832 pointing to. If the alias is deprecated, though, we need to
833 warn the user about it before we drop it. Note that while we
834 are warning about the alias, we may also warn about the command
835 itself and we will adjust the appropriate DEPRECATED_WARN_USER
838 if (found
->flags
& DEPRECATED_WARN_USER
)
839 deprecated_cmd_warning (&line
);
840 found
= found
->cmd_pointer
;
842 /* If we found a prefix command, keep looking. */
844 if (found
->prefixlist
)
846 c
= lookup_cmd_1 (text
, *found
->prefixlist
, result_list
,
847 ignore_help_classes
);
850 /* Didn't find anything; this is as far as we got. */
851 if (result_list
!= NULL
)
852 *result_list
= clist
;
855 else if (c
== (struct cmd_list_element
*) -1)
857 /* We've gotten this far properly, but the next step
858 is ambiguous. We need to set the result list to the best
859 we've found (if an inferior hasn't already set it). */
860 if (result_list
!= NULL
)
862 /* This used to say *result_list = *found->prefixlist
863 If that was correct, need to modify the documentation
864 at the top of this function to clarify what is supposed
866 *result_list
= found
;
877 if (result_list
!= NULL
)
878 *result_list
= clist
;
883 /* All this hair to move the space to the front of cmdtype */
886 undef_cmd_error (char *cmdtype
, char *q
)
888 error ("Undefined %scommand: \"%s\". Try \"help%s%.*s\".",
892 strlen (cmdtype
) - 1,
896 /* Look up the contents of *LINE as a command in the command list LIST.
897 LIST is a chain of struct cmd_list_element's.
898 If it is found, return the struct cmd_list_element for that command
899 and update *LINE to point after the command name, at the first argument.
900 If not found, call error if ALLOW_UNKNOWN is zero
901 otherwise (or if error returns) return zero.
902 Call error if specified command is ambiguous,
903 unless ALLOW_UNKNOWN is negative.
904 CMDTYPE precedes the word "command" in the error message.
906 If INGNORE_HELP_CLASSES is nonzero, ignore any command list
907 elements which are actually help classes rather than commands (i.e.
908 the function field of the struct cmd_list_element is 0). */
910 struct cmd_list_element
*
911 lookup_cmd (char **line
, struct cmd_list_element
*list
, char *cmdtype
,
912 int allow_unknown
, int ignore_help_classes
)
914 struct cmd_list_element
*last_list
= 0;
915 struct cmd_list_element
*c
=
916 lookup_cmd_1 (line
, list
, &last_list
, ignore_help_classes
);
918 /* This is wrong for complete_command. */
919 char *ptr
= (*line
) + strlen (*line
) - 1;
921 /* Clear off trailing whitespace. */
922 while (ptr
>= *line
&& (*ptr
== ' ' || *ptr
== '\t'))
932 error ("Lack of needed %scommand", cmdtype
);
937 while (isalnum (*p
) || *p
== '-')
940 q
= (char *) alloca (p
- *line
+ 1);
941 strncpy (q
, *line
, p
- *line
);
943 undef_cmd_error (cmdtype
, q
);
949 else if (c
== (struct cmd_list_element
*) -1)
951 /* Ambigous. Local values should be off prefixlist or called
953 int local_allow_unknown
= (last_list
? last_list
->allow_unknown
:
955 char *local_cmdtype
= last_list
? last_list
->prefixname
: cmdtype
;
956 struct cmd_list_element
*local_list
=
957 (last_list
? *(last_list
->prefixlist
) : list
);
959 if (local_allow_unknown
< 0)
962 return last_list
; /* Found something. */
964 return 0; /* Found nothing. */
968 /* Report as error. */
973 ((*line
)[amb_len
] && (*line
)[amb_len
] != ' '
974 && (*line
)[amb_len
] != '\t');
979 for (c
= local_list
; c
; c
= c
->next
)
980 if (!strncmp (*line
, c
->name
, amb_len
))
982 if (strlen (ambbuf
) + strlen (c
->name
) + 6 < (int) sizeof ambbuf
)
985 strcat (ambbuf
, ", ");
986 strcat (ambbuf
, c
->name
);
990 strcat (ambbuf
, "..");
994 error ("Ambiguous %scommand \"%s\": %s.", local_cmdtype
,
1001 /* We've got something. It may still not be what the caller
1002 wants (if this command *needs* a subcommand). */
1003 while (**line
== ' ' || **line
== '\t')
1006 if (c
->prefixlist
&& **line
&& !c
->allow_unknown
)
1007 undef_cmd_error (c
->prefixname
, *line
);
1009 /* Seems to be what he wants. Return it. */
1015 /* We are here presumably because an alias or command in *TEXT is
1016 deprecated and a warning message should be generated. This function
1017 decodes *TEXT and potentially generates a warning message as outlined
1020 Example for 'set endian big' which has a fictitious alias 'seb'.
1022 If alias wasn't used in *TEXT, and the command is deprecated:
1023 "warning: 'set endian big' is deprecated."
1025 If alias was used, and only the alias is deprecated:
1026 "warning: 'seb' an alias for the command 'set endian big' is deprecated."
1028 If alias was used and command is deprecated (regardless of whether the
1029 alias itself is deprecated:
1031 "warning: 'set endian big' (seb) is deprecated."
1033 After the message has been sent, clear the appropriate flags in the
1034 command and/or the alias so the user is no longer bothered.
1038 deprecated_cmd_warning (char **text
)
1040 struct cmd_list_element
*alias
= NULL
;
1041 struct cmd_list_element
*prefix_cmd
= NULL
;
1042 struct cmd_list_element
*cmd
= NULL
;
1043 struct cmd_list_element
*c
;
1046 if (!lookup_cmd_composition (*text
, &alias
, &prefix_cmd
, &cmd
))
1047 /* return if text doesn't evaluate to a command */
1050 if (!((alias
? (alias
->flags
& DEPRECATED_WARN_USER
) : 0)
1051 || (cmd
->flags
& DEPRECATED_WARN_USER
) ) )
1052 /* return if nothing is deprecated */
1055 printf_filtered ("Warning:");
1057 if (alias
&& !(cmd
->flags
& CMD_DEPRECATED
))
1058 printf_filtered (" '%s', an alias for the", alias
->name
);
1060 printf_filtered (" command '");
1063 printf_filtered ("%s", prefix_cmd
->prefixname
);
1065 printf_filtered ("%s", cmd
->name
);
1067 if (alias
&& (cmd
->flags
& CMD_DEPRECATED
))
1068 printf_filtered ("' (%s) is deprecated.\n", alias
->name
);
1070 printf_filtered ("' is deprecated.\n");
1073 /* if it is only the alias that is deprecated, we want to indicate the
1074 new alias, otherwise we'll indicate the new command */
1076 if (alias
&& !(cmd
->flags
& CMD_DEPRECATED
))
1078 if (alias
->replacement
)
1079 printf_filtered ("Use '%s'.\n\n", alias
->replacement
);
1081 printf_filtered ("No alternative known.\n\n");
1085 if (cmd
->replacement
)
1086 printf_filtered ("Use '%s'.\n\n", cmd
->replacement
);
1088 printf_filtered ("No alternative known.\n\n");
1091 /* We've warned you, now we'll keep quiet */
1093 alias
->flags
&= ~DEPRECATED_WARN_USER
;
1095 cmd
->flags
&= ~DEPRECATED_WARN_USER
;
1100 /* Look up the contents of LINE as a command in the command list 'cmdlist'.
1101 Return 1 on success, 0 on failure.
1103 If LINE refers to an alias, *alias will point to that alias.
1105 If LINE is a postfix command (i.e. one that is preceeded by a prefix
1106 command) set *prefix_cmd.
1108 Set *cmd to point to the command LINE indicates.
1110 If any of *alias, *prefix_cmd, or *cmd cannot be determined or do not
1111 exist, they are NULL when we return.
1115 lookup_cmd_composition (char *text
,
1116 struct cmd_list_element
**alias
,
1117 struct cmd_list_element
**prefix_cmd
,
1118 struct cmd_list_element
**cmd
)
1121 int len
, tmp
, nfound
;
1122 struct cmd_list_element
*cur_list
;
1123 struct cmd_list_element
*prev_cmd
;
1132 /* Go through as many command lists as we need to
1133 to find the command TEXT refers to. */
1137 while (*text
== ' ' || *text
== '\t')
1140 /* Treating underscores as part of command words is important
1141 so that "set args_foo()" doesn't get interpreted as
1142 "set args _foo()". */
1144 *p
&& (isalnum (*p
) || *p
== '-' || *p
== '_' ||
1146 (*p
== '+' || *p
== '<' || *p
== '>' || *p
== '$')) ||
1147 (xdb_commands
&& (*p
== '!' || *p
== '/' || *p
== '?')));
1151 /* If nothing but whitespace, return. */
1157 /* text and p now bracket the first command word to lookup (and
1158 it's length is len). We copy this into a local temporary */
1160 command
= (char *) alloca (len
+ 1);
1161 for (tmp
= 0; tmp
< len
; tmp
++)
1166 command
[len
] = '\0';
1171 *cmd
= find_cmd (command
, len
, cur_list
, 1, &nfound
);
1173 /* We didn't find the command in the entered case, so lower case it
1176 if (!*cmd
|| nfound
== 0)
1178 for (tmp
= 0; tmp
< len
; tmp
++)
1180 char x
= command
[tmp
];
1181 command
[tmp
] = isupper (x
) ? tolower (x
) : x
;
1183 *cmd
= find_cmd (command
, len
, cur_list
, 1, &nfound
);
1186 if (*cmd
== (struct cmd_list_element
*) -1)
1188 return 0; /* ambiguous */
1192 return 0; /* nothing found */
1195 if ((*cmd
)->cmd_pointer
)
1197 /* cmd was actually an alias, we note that an alias was used
1198 (by assigning *alais) and we set *cmd.
1201 *cmd
= (*cmd
)->cmd_pointer
;
1203 *prefix_cmd
= prev_cmd
;
1205 if ((*cmd
)->prefixlist
)
1206 cur_list
= *(*cmd
)->prefixlist
;
1218 /* Look up the contents of *LINE as a command in the command list LIST.
1219 LIST is a chain of struct cmd_list_element's.
1220 If it is found, return the struct cmd_list_element for that command
1221 and update *LINE to point after the command name, at the first argument.
1222 If not found, call error if ALLOW_UNKNOWN is zero
1223 otherwise (or if error returns) return zero.
1224 Call error if specified command is ambiguous,
1225 unless ALLOW_UNKNOWN is negative.
1226 CMDTYPE precedes the word "command" in the error message. */
1228 struct cmd_list_element
*
1229 lookup_cmd (char **line
, struct cmd_list_element
*list
, char *cmdtype
,
1233 register struct cmd_list_element
*c
, *found
;
1236 char *processed_cmd
;
1239 /* Skip leading whitespace. */
1241 while (**line
== ' ' || **line
== '\t')
1244 /* Clear out trailing whitespace. */
1246 p
= *line
+ strlen (*line
);
1247 while (p
!= *line
&& (p
[-1] == ' ' || p
[-1] == '\t'))
1251 /* Find end of command name. */
1254 while (*p
== '-' || isalnum (*p
))
1257 /* Look up the command name.
1258 If exact match, keep that.
1259 Otherwise, take command abbreviated, if unique. Note that (in my
1260 opinion) a null string does *not* indicate ambiguity; simply the
1261 end of the argument. */
1266 error ("Lack of needed %scommand", cmdtype
);
1270 /* Copy over to a local buffer, converting to lowercase on the way.
1271 This is in case the command being parsed is a subcommand which
1272 doesn't match anything, and that's ok. We want the original
1273 untouched for the routine of the original command. */
1275 processed_cmd
= (char *) alloca (p
- *line
+ 1);
1276 for (cmd_len
= 0; cmd_len
< p
- *line
; cmd_len
++)
1278 char x
= (*line
)[cmd_len
];
1280 processed_cmd
[cmd_len
] = tolower (x
);
1282 processed_cmd
[cmd_len
] = x
;
1284 processed_cmd
[cmd_len
] = '\0';
1286 /* Check all possibilities in the current command list. */
1289 for (c
= list
; c
; c
= c
->next
)
1291 if (!strncmp (processed_cmd
, c
->name
, cmd_len
))
1295 if (c
->name
[cmd_len
] == 0)
1303 /* Report error for undefined command name. */
1307 if (nfound
> 1 && allow_unknown
>= 0)
1310 for (c
= list
; c
; c
= c
->next
)
1311 if (!strncmp (processed_cmd
, c
->name
, cmd_len
))
1313 if (strlen (ambbuf
) + strlen (c
->name
) + 6 < sizeof ambbuf
)
1315 if (strlen (ambbuf
))
1316 strcat (ambbuf
, ", ");
1317 strcat (ambbuf
, c
->name
);
1321 strcat (ambbuf
, "..");
1325 error ("Ambiguous %scommand \"%s\": %s.", cmdtype
,
1326 processed_cmd
, ambbuf
);
1328 else if (!allow_unknown
)
1329 error ("Undefined %scommand: \"%s\".", cmdtype
, processed_cmd
);
1333 /* Skip whitespace before the argument. */
1335 while (*p
== ' ' || *p
== '\t')
1339 if (found
->prefixlist
&& *p
)
1341 c
= lookup_cmd (line
, *found
->prefixlist
, found
->prefixname
,
1342 found
->allow_unknown
);
1351 /* Helper function for SYMBOL_COMPLETION_FUNCTION. */
1353 /* Return a vector of char pointers which point to the different
1354 possible completions in LIST of TEXT.
1356 WORD points in the same buffer as TEXT, and completions should be
1357 returned relative to this position. For example, suppose TEXT is "foo"
1358 and we want to complete to "foobar". If WORD is "oo", return
1359 "oobar"; if WORD is "baz/foo", return "baz/foobar". */
1362 complete_on_cmdlist (struct cmd_list_element
*list
, char *text
, char *word
)
1364 struct cmd_list_element
*ptr
;
1366 int sizeof_matchlist
;
1368 int textlen
= strlen (text
);
1370 sizeof_matchlist
= 10;
1371 matchlist
= (char **) xmalloc (sizeof_matchlist
* sizeof (char *));
1374 for (ptr
= list
; ptr
; ptr
= ptr
->next
)
1375 if (!strncmp (ptr
->name
, text
, textlen
)
1376 && !ptr
->abbrev_flag
1377 && (ptr
->function
.cfunc
1378 || ptr
->prefixlist
))
1380 if (matches
== sizeof_matchlist
)
1382 sizeof_matchlist
*= 2;
1383 matchlist
= (char **) xrealloc ((char *) matchlist
,
1385 * sizeof (char *)));
1388 matchlist
[matches
] = (char *)
1389 xmalloc (strlen (word
) + strlen (ptr
->name
) + 1);
1391 strcpy (matchlist
[matches
], ptr
->name
);
1392 else if (word
> text
)
1394 /* Return some portion of ptr->name. */
1395 strcpy (matchlist
[matches
], ptr
->name
+ (word
- text
));
1399 /* Return some of text plus ptr->name. */
1400 strncpy (matchlist
[matches
], word
, text
- word
);
1401 matchlist
[matches
][text
- word
] = '\0';
1402 strcat (matchlist
[matches
], ptr
->name
);
1409 free ((PTR
) matchlist
);
1414 matchlist
= (char **) xrealloc ((char *) matchlist
, ((matches
+ 1)
1415 * sizeof (char *)));
1416 matchlist
[matches
] = (char *) 0;
1422 /* Helper function for SYMBOL_COMPLETION_FUNCTION. */
1424 /* Return a vector of char pointers which point to the different
1425 possible completions in CMD of TEXT.
1427 WORD points in the same buffer as TEXT, and completions should be
1428 returned relative to this position. For example, suppose TEXT is "foo"
1429 and we want to complete to "foobar". If WORD is "oo", return
1430 "oobar"; if WORD is "baz/foo", return "baz/foobar". */
1433 complete_on_enum (const char *enumlist
[],
1438 int sizeof_matchlist
;
1440 int textlen
= strlen (text
);
1444 sizeof_matchlist
= 10;
1445 matchlist
= (char **) xmalloc (sizeof_matchlist
* sizeof (char *));
1448 for (i
= 0; (name
= enumlist
[i
]) != NULL
; i
++)
1449 if (strncmp (name
, text
, textlen
) == 0)
1451 if (matches
== sizeof_matchlist
)
1453 sizeof_matchlist
*= 2;
1454 matchlist
= (char **) xrealloc ((char *) matchlist
,
1456 * sizeof (char *)));
1459 matchlist
[matches
] = (char *)
1460 xmalloc (strlen (word
) + strlen (name
) + 1);
1462 strcpy (matchlist
[matches
], name
);
1463 else if (word
> text
)
1465 /* Return some portion of name. */
1466 strcpy (matchlist
[matches
], name
+ (word
- text
));
1470 /* Return some of text plus name. */
1471 strncpy (matchlist
[matches
], word
, text
- word
);
1472 matchlist
[matches
][text
- word
] = '\0';
1473 strcat (matchlist
[matches
], name
);
1480 free ((PTR
) matchlist
);
1485 matchlist
= (char **) xrealloc ((char *) matchlist
, ((matches
+ 1)
1486 * sizeof (char *)));
1487 matchlist
[matches
] = (char *) 0;