1 /* Handle lists of commands, their decoding and documentation, for GDB.
3 Copyright 1986, 1989, 1990, 1991, 1998, 2000, 2001, 2002 Free
4 Software Foundation, Inc.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
24 #include "gdb_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;
92 c->pre_show_hook = NULL;
100 c->completer = make_symbol_completion_list;
101 c->type = not_set_cmd;
103 c->var_type = var_boolean;
105 c->user_commands = NULL;
106 c->hookee_pre = NULL;
107 c->hookee_post = NULL;
108 c->cmd_pointer = NULL;
113 /* Same as above, except that the abbrev_flag is set. */
114 /* Note: Doesn't seem to be used anywhere currently. */
116 struct cmd_list_element *
117 add_abbrev_cmd (char *name, enum command_class class, void (*fun) (char *, int),
118 char *doc, struct cmd_list_element **list)
120 register struct cmd_list_element *c
121 = add_cmd (name, class, fun, doc, list);
127 /* Deprecates a command CMD.
128 REPLACEMENT is the name of the command which should be used in place
129 of this command, or NULL if no such command exists.
131 This function does not check to see if command REPLACEMENT exists
132 since gdb may not have gotten around to adding REPLACEMENT when this
135 Returns a pointer to the deprecated command. */
137 struct cmd_list_element *
138 deprecate_cmd (struct cmd_list_element *cmd, char *replacement)
140 cmd->flags |= (CMD_DEPRECATED | DEPRECATED_WARN_USER);
142 if (replacement != NULL)
143 cmd->replacement = replacement;
145 cmd->replacement = NULL;
150 struct cmd_list_element *
151 add_alias_cmd (char *name, char *oldname, enum command_class class,
152 int abbrev_flag, struct cmd_list_element **list)
154 /* Must do this since lookup_cmd tries to side-effect its first arg */
156 register struct cmd_list_element *old;
157 register struct cmd_list_element *c;
158 copied_name = (char *) alloca (strlen (oldname) + 1);
159 strcpy (copied_name, oldname);
160 old = lookup_cmd (&copied_name, *list, "", 1, 1);
164 delete_cmd (name, list);
168 c = add_cmd (name, class, old->function.cfunc, old->doc, list);
169 c->prefixlist = old->prefixlist;
170 c->prefixname = old->prefixname;
171 c->allow_unknown = old->allow_unknown;
172 c->abbrev_flag = abbrev_flag;
173 c->cmd_pointer = old;
177 /* Like add_cmd but adds an element for a command prefix:
178 a name that should be followed by a subcommand to be looked up
179 in another command list. PREFIXLIST should be the address
180 of the variable containing that list. */
182 struct cmd_list_element *
183 add_prefix_cmd (char *name, enum command_class class, void (*fun) (char *, int),
184 char *doc, struct cmd_list_element **prefixlist,
185 char *prefixname, int allow_unknown,
186 struct cmd_list_element **list)
188 register struct cmd_list_element *c = add_cmd (name, class, fun, doc, list);
189 c->prefixlist = prefixlist;
190 c->prefixname = prefixname;
191 c->allow_unknown = allow_unknown;
195 /* Like add_prefix_cmd but sets the abbrev_flag on the new command. */
197 struct cmd_list_element *
198 add_abbrev_prefix_cmd (char *name, enum command_class class,
199 void (*fun) (char *, int), char *doc,
200 struct cmd_list_element **prefixlist, char *prefixname,
201 int allow_unknown, 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;
211 /* This is an empty "cfunc". */
213 not_just_help_class_command (char *args, int from_tty)
217 /* This is an empty "sfunc". */
218 static void empty_sfunc (char *, int, struct cmd_list_element *);
221 empty_sfunc (char *args, int from_tty, struct cmd_list_element *c)
225 /* Add element named NAME to command list LIST (the list for set
226 or some sublist thereof).
227 CLASS is as in add_cmd.
228 VAR_TYPE is the kind of thing we are setting.
229 VAR is address of the variable being controlled by this command.
230 DOC is the documentation string. */
232 struct cmd_list_element *
233 add_set_cmd (char *name,
234 enum command_class class,
238 struct cmd_list_element **list)
240 struct cmd_list_element *c = add_cmd (name, class, NULL, doc, list);
243 c->var_type = var_type;
245 /* This needs to be something besides NULL so that this isn't
246 treated as a help class. */
247 c->function.sfunc = empty_sfunc;
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 ENUMLIST is a list of strings which may follow NAME.
255 VAR is address of the variable which will contain the matching string
257 DOC is the documentation string. */
259 struct cmd_list_element *
260 add_set_enum_cmd (char *name,
261 enum command_class class,
262 const char *enumlist[],
265 struct cmd_list_element **list)
267 struct cmd_list_element *c
268 = add_set_cmd (name, class, var_enum, var, doc, list);
274 /* Add element named NAME to command list LIST (the list for set
275 or some sublist thereof).
276 CLASS is as in add_cmd.
277 VAR is address of the variable which will contain the value.
278 DOC is the documentation string. */
279 struct cmd_list_element *
280 add_set_auto_boolean_cmd (char *name,
281 enum command_class class,
282 enum cmd_auto_boolean *var,
284 struct cmd_list_element **list)
286 static const char *auto_boolean_enums[] = { "on", "off", "auto", NULL };
287 struct cmd_list_element *c;
288 c = add_set_cmd (name, class, var_auto_boolean, var, doc, list);
289 c->enums = auto_boolean_enums;
293 /* Add element named NAME to command list LIST (the list for set
294 or some sublist thereof).
295 CLASS is as in add_cmd.
296 VAR is address of the variable which will contain the value.
297 DOC is the documentation string. */
298 struct cmd_list_element *
299 add_set_boolean_cmd (char *name,
300 enum command_class class,
303 struct cmd_list_element **list)
305 static const char *boolean_enums[] = { "on", "off", NULL };
306 struct cmd_list_element *c;
307 c = add_set_cmd (name, class, var_boolean, var, doc, list);
308 c->enums = boolean_enums;
312 /* Where SETCMD has already been added, add the corresponding show
313 command to LIST and return a pointer to the added command (not
314 necessarily the head of LIST). */
315 struct cmd_list_element *
316 add_show_from_set (struct cmd_list_element *setcmd,
317 struct cmd_list_element **list)
319 struct cmd_list_element *showcmd =
320 (struct cmd_list_element *) xmalloc (sizeof (struct cmd_list_element));
321 struct cmd_list_element *p;
323 memcpy (showcmd, setcmd, sizeof (struct cmd_list_element));
324 delete_cmd (showcmd->name, list);
325 showcmd->type = show_cmd;
327 /* Replace "set " at start of docstring with "show ". */
328 if (setcmd->doc[0] == 'S' && setcmd->doc[1] == 'e'
329 && setcmd->doc[2] == 't' && setcmd->doc[3] == ' ')
330 showcmd->doc = concat ("Show ", setcmd->doc + 4, NULL);
332 fprintf_unfiltered (gdb_stderr, "GDB internal error: Bad docstring for set command\n");
334 if (*list == NULL || strcmp ((*list)->name, showcmd->name) >= 0)
336 showcmd->next = *list;
342 while (p->next && strcmp (p->next->name, showcmd->name) <= 0)
346 showcmd->next = p->next;
353 /* Remove the command named NAME from the command list. */
356 delete_cmd (char *name, struct cmd_list_element **list)
358 register struct cmd_list_element *c;
359 struct cmd_list_element *p;
361 while (*list && STREQ ((*list)->name, name))
363 if ((*list)->hookee_pre)
364 (*list)->hookee_pre->hook_pre = 0; /* Hook slips out of its mouth */
365 if ((*list)->hookee_post)
366 (*list)->hookee_post->hook_post = 0; /* Hook slips out of its bottom */
373 for (c = *list; c->next;)
375 if (STREQ (c->next->name, name))
377 if (c->next->hookee_pre)
378 c->next->hookee_pre->hook_pre = 0; /* hooked cmd gets away. */
379 if (c->next->hookee_post)
380 c->next->hookee_post->hook_post = 0; /* remove post hook */
381 /* :( no fishing metaphore */
391 /* Shorthands to the commands above. */
393 /* Add an element to the list of info subcommands. */
395 struct cmd_list_element *
396 add_info (char *name, void (*fun) (char *, int), char *doc)
398 return add_cmd (name, no_class, fun, doc, &infolist);
401 /* Add an alias to the list of info subcommands. */
403 struct cmd_list_element *
404 add_info_alias (char *name, char *oldname, int abbrev_flag)
406 return add_alias_cmd (name, oldname, 0, abbrev_flag, &infolist);
409 /* Add an element to the list of commands. */
411 struct cmd_list_element *
412 add_com (char *name, enum command_class class, void (*fun) (char *, int),
415 return add_cmd (name, class, fun, doc, &cmdlist);
418 /* Add an alias or abbreviation command to the list of commands. */
420 struct cmd_list_element *
421 add_com_alias (char *name, char *oldname, enum command_class class,
424 return add_alias_cmd (name, oldname, class, abbrev_flag, &cmdlist);
427 /* Recursively walk the commandlist structures, and print out the
428 documentation of commands that match our regex in either their
429 name, or their documentation.
432 apropos_cmd (struct ui_file *stream, struct cmd_list_element *commandlist,
433 struct re_pattern_buffer *regex, char *prefix)
435 register struct cmd_list_element *c;
436 int returnvalue=1; /*Needed to avoid double printing*/
437 /* Walk through the commands */
438 for (c=commandlist;c;c=c->next)
442 /* Try to match against the name*/
443 returnvalue=re_search(regex,c->name,strlen(c->name),0,strlen(c->name),NULL);
444 if (returnvalue >= 0)
446 /* Stolen from help_cmd_list. We don't directly use
447 * help_cmd_list because it doesn't let us print out
450 fprintf_filtered (stream, "%s%s -- ", prefix, c->name);
451 print_doc_line (stream, c->doc);
452 fputs_filtered ("\n", stream);
453 returnvalue=0; /*Set this so we don't print it again.*/
456 if (c->doc != NULL && returnvalue != 0)
458 /* Try to match against documentation */
459 if (re_search(regex,c->doc,strlen(c->doc),0,strlen(c->doc),NULL) >=0)
461 /* Stolen from help_cmd_list. We don't directly use
462 * help_cmd_list because it doesn't let us print out
465 fprintf_filtered (stream, "%s%s -- ", prefix, c->name);
466 print_doc_line (stream, c->doc);
467 fputs_filtered ("\n", stream);
470 /* Check if this command has subcommands */
471 if (c->prefixlist != NULL)
473 /* Recursively call ourselves on the subcommand list,
474 passing the right prefix in.
476 apropos_cmd (stream,*c->prefixlist,regex,c->prefixname);
481 /* This command really has to deal with two things:
482 * 1) I want documentation on *this string* (usually called by
483 * "help commandname").
484 * 2) I want documentation on *this list* (usually called by
485 * giving a command that requires subcommands. Also called by saying
488 * I am going to split this into two seperate comamnds, help_cmd and
493 help_cmd (char *command, struct ui_file *stream)
495 struct cmd_list_element *c;
496 extern struct cmd_list_element *cmdlist;
500 help_list (cmdlist, "", all_classes, stream);
504 if (strcmp (command, "all") == 0)
510 c = lookup_cmd (&command, cmdlist, "", 0, 0);
515 /* There are three cases here.
516 If c->prefixlist is nonzero, we have a prefix command.
517 Print its documentation, then list its subcommands.
519 If c->function is nonzero, we really have a command.
520 Print its documentation and return.
522 If c->function is zero, we have a class name.
523 Print its documentation (as if it were a command)
524 and then set class to the number of this class
525 so that the commands in the class will be listed. */
527 fputs_filtered (c->doc, stream);
528 fputs_filtered ("\n", stream);
530 if (c->prefixlist == 0 && c->function.cfunc != NULL)
532 fprintf_filtered (stream, "\n");
534 /* If this is a prefix command, print it's subcommands */
536 help_list (*c->prefixlist, c->prefixname, all_commands, stream);
538 /* If this is a class name, print all of the commands in the class */
539 if (c->function.cfunc == NULL)
540 help_list (cmdlist, "", c->class, stream);
542 if (c->hook_pre || c->hook_post)
543 fprintf_filtered (stream,
544 "\nThis command has a hook (or hooks) defined:\n");
547 fprintf_filtered (stream,
548 "\tThis command is run after : %s (pre hook)\n",
551 fprintf_filtered (stream,
552 "\tThis command is run before : %s (post hook)\n",
557 * Get a specific kind of help on a command list.
560 * CMDTYPE is the prefix to use in the title string.
561 * CLASS is the class with which to list the nodes of this list (see
562 * documentation for help_cmd_list below), As usual, ALL_COMMANDS for
563 * everything, ALL_CLASSES for just classes, and non-negative for only things
564 * in a specific class.
565 * and STREAM is the output stream on which to print things.
566 * If you call this routine with a class >= 0, it recurses.
569 help_list (struct cmd_list_element *list, char *cmdtype,
570 enum command_class class, struct ui_file *stream)
573 char *cmdtype1, *cmdtype2;
575 /* If CMDTYPE is "foo ", CMDTYPE1 gets " foo" and CMDTYPE2 gets "foo sub" */
576 len = strlen (cmdtype);
577 cmdtype1 = (char *) alloca (len + 1);
579 cmdtype2 = (char *) alloca (len + 4);
584 strncpy (cmdtype1 + 1, cmdtype, len - 1);
586 strncpy (cmdtype2, cmdtype, len - 1);
587 strcpy (cmdtype2 + len - 1, " sub");
590 if (class == all_classes)
591 fprintf_filtered (stream, "List of classes of %scommands:\n\n", cmdtype2);
593 fprintf_filtered (stream, "List of %scommands:\n\n", cmdtype2);
595 help_cmd_list (list, class, cmdtype, (int) class >= 0, stream);
597 if (class == all_classes)
598 fprintf_filtered (stream, "\n\
599 Type \"help%s\" followed by a class name for a list of commands in that class.",
602 fprintf_filtered (stream, "\n\
603 Type \"help%s\" followed by %scommand name for full documentation.\n\
604 Command name abbreviations are allowed if unambiguous.\n",
609 help_all (struct ui_file *stream)
611 struct cmd_list_element *c;
612 extern struct cmd_list_element *cmdlist;
614 for (c = cmdlist; c; c = c->next)
618 /* If this is a prefix command, print it's subcommands */
620 help_cmd_list (*c->prefixlist, all_commands, c->prefixname, 0, stream);
622 /* If this is a class name, print all of the commands in the class */
623 else if (c->function.cfunc == NULL)
624 help_cmd_list (cmdlist, c->class, "", 0, stream);
628 /* Print only the first line of STR on STREAM. */
630 print_doc_line (struct ui_file *stream, char *str)
632 static char *line_buffer = 0;
633 static int line_size;
639 line_buffer = (char *) xmalloc (line_size);
643 while (*p && *p != '\n' && *p != '.' && *p != ',')
645 if (p - str > line_size - 1)
647 line_size = p - str + 1;
649 line_buffer = (char *) xmalloc (line_size);
651 strncpy (line_buffer, str, p - str);
652 line_buffer[p - str] = '\0';
653 if (islower (line_buffer[0]))
654 line_buffer[0] = toupper (line_buffer[0]);
655 ui_out_text (uiout, line_buffer);
659 * Implement a help command on command list LIST.
660 * RECURSE should be non-zero if this should be done recursively on
661 * all sublists of LIST.
662 * PREFIX is the prefix to print before each command name.
663 * STREAM is the stream upon which the output should be written.
665 * A non-negative class number to list only commands in that
667 * ALL_COMMANDS to list all commands in list.
668 * ALL_CLASSES to list all classes in list.
670 * Note that RECURSE will be active on *all* sublists, not just the
671 * ones selected by the criteria above (ie. the selection mechanism
672 * is at the low level, not the high-level).
675 help_cmd_list (struct cmd_list_element *list, enum command_class class,
676 char *prefix, int recurse, struct ui_file *stream)
678 register struct cmd_list_element *c;
680 for (c = list; c; c = c->next)
682 if (c->abbrev_flag == 0 &&
683 (class == all_commands
684 || (class == all_classes && c->function.cfunc == NULL)
685 || (class == c->class && c->function.cfunc != NULL)))
687 fprintf_filtered (stream, "%s%s -- ", prefix, c->name);
688 print_doc_line (stream, c->doc);
689 fputs_filtered ("\n", stream);
692 && c->prefixlist != 0
693 && c->abbrev_flag == 0)
694 help_cmd_list (*c->prefixlist, class, c->prefixname, 1, stream);
699 /* Search the input clist for 'command'. Return the command if
700 found (or NULL if not), and return the number of commands
703 static struct cmd_list_element *
704 find_cmd (char *command, int len, struct cmd_list_element *clist,
705 int ignore_help_classes, int *nfound)
707 struct cmd_list_element *found, *c;
709 found = (struct cmd_list_element *) NULL;
711 for (c = clist; c; c = c->next)
712 if (!strncmp (command, c->name, len)
713 && (!ignore_help_classes || c->function.cfunc))
717 if (c->name[len] == '\0')
726 /* This routine takes a line of TEXT and a CLIST in which to start the
727 lookup. When it returns it will have incremented the text pointer past
728 the section of text it matched, set *RESULT_LIST to point to the list in
729 which the last word was matched, and will return a pointer to the cmd
730 list element which the text matches. It will return NULL if no match at
731 all was possible. It will return -1 (cast appropriately, ick) if ambigous
732 matches are possible; in this case *RESULT_LIST will be set to point to
733 the list in which there are ambiguous choices (and *TEXT will be set to
734 the ambiguous text string).
736 If the located command was an abbreviation, this routine returns the base
737 command of the abbreviation.
739 It does no error reporting whatsoever; control will always return
740 to the superior routine.
742 In the case of an ambiguous return (-1), *RESULT_LIST will be set to point
743 at the prefix_command (ie. the best match) *or* (special case) will be NULL
744 if no prefix command was ever found. For example, in the case of "info a",
745 "info" matches without ambiguity, but "a" could be "args" or "address", so
746 *RESULT_LIST is set to the cmd_list_element for "info". So in this case
747 RESULT_LIST should not be interpeted as a pointer to the beginning of a
748 list; it simply points to a specific command. In the case of an ambiguous
749 return *TEXT is advanced past the last non-ambiguous prefix (e.g.
750 "info t" can be "info types" or "info target"; upon return *TEXT has been
751 advanced past "info ").
753 If RESULT_LIST is NULL, don't set *RESULT_LIST (but don't otherwise
754 affect the operation).
756 This routine does *not* modify the text pointed to by TEXT.
758 If IGNORE_HELP_CLASSES is nonzero, ignore any command list elements which
759 are actually help classes rather than commands (i.e. the function field of
760 the struct cmd_list_element is NULL). */
762 struct cmd_list_element *
763 lookup_cmd_1 (char **text, struct cmd_list_element *clist,
764 struct cmd_list_element **result_list, int ignore_help_classes)
767 int len, tmp, nfound;
768 struct cmd_list_element *found, *c;
771 while (**text == ' ' || **text == '\t')
774 /* Treating underscores as part of command words is important
775 so that "set args_foo()" doesn't get interpreted as
776 "set args _foo()". */
778 *p && (isalnum (*p) || *p == '-' || *p == '_' ||
780 (*p == '+' || *p == '<' || *p == '>' || *p == '$')) ||
781 (xdb_commands && (*p == '!' || *p == '/' || *p == '?')));
785 /* If nothing but whitespace, return 0. */
791 /* *text and p now bracket the first command word to lookup (and
792 it's length is len). We copy this into a local temporary */
795 command = (char *) alloca (len + 1);
796 for (tmp = 0; tmp < len; tmp++)
798 char x = (*text)[tmp];
806 found = find_cmd (command, len, clist, ignore_help_classes, &nfound);
809 ** We didn't find the command in the entered case, so lower case it
812 if (!found || nfound == 0)
814 for (tmp = 0; tmp < len; tmp++)
816 char x = command[tmp];
817 command[tmp] = isupper (x) ? tolower (x) : x;
819 found = find_cmd (command, len, clist, ignore_help_classes, &nfound);
822 /* If nothing matches, we have a simple failure. */
828 if (result_list != NULL)
829 /* Will be modified in calling routine
830 if we know what the prefix command is. */
832 return (struct cmd_list_element *) -1; /* Ambiguous. */
835 /* We've matched something on this list. Move text pointer forward. */
839 if (found->cmd_pointer)
841 /* We drop the alias (abbreviation) in favor of the command it is
842 pointing to. If the alias is deprecated, though, we need to
843 warn the user about it before we drop it. Note that while we
844 are warning about the alias, we may also warn about the command
845 itself and we will adjust the appropriate DEPRECATED_WARN_USER
848 if (found->flags & DEPRECATED_WARN_USER)
849 deprecated_cmd_warning (&line);
850 found = found->cmd_pointer;
852 /* If we found a prefix command, keep looking. */
854 if (found->prefixlist)
856 c = lookup_cmd_1 (text, *found->prefixlist, result_list,
857 ignore_help_classes);
860 /* Didn't find anything; this is as far as we got. */
861 if (result_list != NULL)
862 *result_list = clist;
865 else if (c == (struct cmd_list_element *) -1)
867 /* We've gotten this far properly, but the next step
868 is ambiguous. We need to set the result list to the best
869 we've found (if an inferior hasn't already set it). */
870 if (result_list != NULL)
872 /* This used to say *result_list = *found->prefixlist
873 If that was correct, need to modify the documentation
874 at the top of this function to clarify what is supposed
876 *result_list = found;
887 if (result_list != NULL)
888 *result_list = clist;
893 /* All this hair to move the space to the front of cmdtype */
896 undef_cmd_error (char *cmdtype, char *q)
898 error ("Undefined %scommand: \"%s\". Try \"help%s%.*s\".",
902 strlen (cmdtype) - 1,
906 /* Look up the contents of *LINE as a command in the command list LIST.
907 LIST is a chain of struct cmd_list_element's.
908 If it is found, return the struct cmd_list_element for that command
909 and update *LINE to point after the command name, at the first argument.
910 If not found, call error if ALLOW_UNKNOWN is zero
911 otherwise (or if error returns) return zero.
912 Call error if specified command is ambiguous,
913 unless ALLOW_UNKNOWN is negative.
914 CMDTYPE precedes the word "command" in the error message.
916 If INGNORE_HELP_CLASSES is nonzero, ignore any command list
917 elements which are actually help classes rather than commands (i.e.
918 the function field of the struct cmd_list_element is 0). */
920 struct cmd_list_element *
921 lookup_cmd (char **line, struct cmd_list_element *list, char *cmdtype,
922 int allow_unknown, int ignore_help_classes)
924 struct cmd_list_element *last_list = 0;
925 struct cmd_list_element *c =
926 lookup_cmd_1 (line, list, &last_list, ignore_help_classes);
928 /* Note: Do not remove trailing whitespace here because this
929 would be wrong for complete_command. Jim Kingdon */
936 error ("Lack of needed %scommand", cmdtype);
941 while (isalnum (*p) || *p == '-')
944 q = (char *) alloca (p - *line + 1);
945 strncpy (q, *line, p - *line);
947 undef_cmd_error (cmdtype, q);
953 else if (c == (struct cmd_list_element *) -1)
955 /* Ambigous. Local values should be off prefixlist or called
957 int local_allow_unknown = (last_list ? last_list->allow_unknown :
959 char *local_cmdtype = last_list ? last_list->prefixname : cmdtype;
960 struct cmd_list_element *local_list =
961 (last_list ? *(last_list->prefixlist) : list);
963 if (local_allow_unknown < 0)
966 return last_list; /* Found something. */
968 return 0; /* Found nothing. */
972 /* Report as error. */
977 ((*line)[amb_len] && (*line)[amb_len] != ' '
978 && (*line)[amb_len] != '\t');
983 for (c = local_list; c; c = c->next)
984 if (!strncmp (*line, c->name, amb_len))
986 if (strlen (ambbuf) + strlen (c->name) + 6 < (int) sizeof ambbuf)
989 strcat (ambbuf, ", ");
990 strcat (ambbuf, c->name);
994 strcat (ambbuf, "..");
998 error ("Ambiguous %scommand \"%s\": %s.", local_cmdtype,
1000 return 0; /* lint */
1005 /* We've got something. It may still not be what the caller
1006 wants (if this command *needs* a subcommand). */
1007 while (**line == ' ' || **line == '\t')
1010 if (c->prefixlist && **line && !c->allow_unknown)
1011 undef_cmd_error (c->prefixname, *line);
1013 /* Seems to be what he wants. Return it. */
1019 /* We are here presumably because an alias or command in *TEXT is
1020 deprecated and a warning message should be generated. This function
1021 decodes *TEXT and potentially generates a warning message as outlined
1024 Example for 'set endian big' which has a fictitious alias 'seb'.
1026 If alias wasn't used in *TEXT, and the command is deprecated:
1027 "warning: 'set endian big' is deprecated."
1029 If alias was used, and only the alias is deprecated:
1030 "warning: 'seb' an alias for the command 'set endian big' is deprecated."
1032 If alias was used and command is deprecated (regardless of whether the
1033 alias itself is deprecated:
1035 "warning: 'set endian big' (seb) is deprecated."
1037 After the message has been sent, clear the appropriate flags in the
1038 command and/or the alias so the user is no longer bothered.
1042 deprecated_cmd_warning (char **text)
1044 struct cmd_list_element *alias = NULL;
1045 struct cmd_list_element *prefix_cmd = NULL;
1046 struct cmd_list_element *cmd = NULL;
1047 struct cmd_list_element *c;
1050 if (!lookup_cmd_composition (*text, &alias, &prefix_cmd, &cmd))
1051 /* return if text doesn't evaluate to a command */
1054 if (!((alias ? (alias->flags & DEPRECATED_WARN_USER) : 0)
1055 || (cmd->flags & DEPRECATED_WARN_USER) ) )
1056 /* return if nothing is deprecated */
1059 printf_filtered ("Warning:");
1061 if (alias && !(cmd->flags & CMD_DEPRECATED))
1062 printf_filtered (" '%s', an alias for the", alias->name);
1064 printf_filtered (" command '");
1067 printf_filtered ("%s", prefix_cmd->prefixname);
1069 printf_filtered ("%s", cmd->name);
1071 if (alias && (cmd->flags & CMD_DEPRECATED))
1072 printf_filtered ("' (%s) is deprecated.\n", alias->name);
1074 printf_filtered ("' is deprecated.\n");
1077 /* if it is only the alias that is deprecated, we want to indicate the
1078 new alias, otherwise we'll indicate the new command */
1080 if (alias && !(cmd->flags & CMD_DEPRECATED))
1082 if (alias->replacement)
1083 printf_filtered ("Use '%s'.\n\n", alias->replacement);
1085 printf_filtered ("No alternative known.\n\n");
1089 if (cmd->replacement)
1090 printf_filtered ("Use '%s'.\n\n", cmd->replacement);
1092 printf_filtered ("No alternative known.\n\n");
1095 /* We've warned you, now we'll keep quiet */
1097 alias->flags &= ~DEPRECATED_WARN_USER;
1099 cmd->flags &= ~DEPRECATED_WARN_USER;
1104 /* Look up the contents of LINE as a command in the command list 'cmdlist'.
1105 Return 1 on success, 0 on failure.
1107 If LINE refers to an alias, *alias will point to that alias.
1109 If LINE is a postfix command (i.e. one that is preceeded by a prefix
1110 command) set *prefix_cmd.
1112 Set *cmd to point to the command LINE indicates.
1114 If any of *alias, *prefix_cmd, or *cmd cannot be determined or do not
1115 exist, they are NULL when we return.
1119 lookup_cmd_composition (char *text,
1120 struct cmd_list_element **alias,
1121 struct cmd_list_element **prefix_cmd,
1122 struct cmd_list_element **cmd)
1125 int len, tmp, nfound;
1126 struct cmd_list_element *cur_list;
1127 struct cmd_list_element *prev_cmd;
1136 /* Go through as many command lists as we need to
1137 to find the command TEXT refers to. */
1141 while (*text == ' ' || *text == '\t')
1144 /* Treating underscores as part of command words is important
1145 so that "set args_foo()" doesn't get interpreted as
1146 "set args _foo()". */
1148 *p && (isalnum (*p) || *p == '-' || *p == '_' ||
1150 (*p == '+' || *p == '<' || *p == '>' || *p == '$')) ||
1151 (xdb_commands && (*p == '!' || *p == '/' || *p == '?')));
1155 /* If nothing but whitespace, return. */
1161 /* text and p now bracket the first command word to lookup (and
1162 it's length is len). We copy this into a local temporary */
1164 command = (char *) alloca (len + 1);
1165 for (tmp = 0; tmp < len; tmp++)
1170 command[len] = '\0';
1175 *cmd = find_cmd (command, len, cur_list, 1, &nfound);
1177 /* We didn't find the command in the entered case, so lower case it
1180 if (!*cmd || nfound == 0)
1182 for (tmp = 0; tmp < len; tmp++)
1184 char x = command[tmp];
1185 command[tmp] = isupper (x) ? tolower (x) : x;
1187 *cmd = find_cmd (command, len, cur_list, 1, &nfound);
1190 if (*cmd == (struct cmd_list_element *) -1)
1192 return 0; /* ambiguous */
1196 return 0; /* nothing found */
1199 if ((*cmd)->cmd_pointer)
1201 /* cmd was actually an alias, we note that an alias was used
1202 (by assigning *alais) and we set *cmd.
1205 *cmd = (*cmd)->cmd_pointer;
1207 *prefix_cmd = prev_cmd;
1209 if ((*cmd)->prefixlist)
1210 cur_list = *(*cmd)->prefixlist;
1218 /* Helper function for SYMBOL_COMPLETION_FUNCTION. */
1220 /* Return a vector of char pointers which point to the different
1221 possible completions in LIST of TEXT.
1223 WORD points in the same buffer as TEXT, and completions should be
1224 returned relative to this position. For example, suppose TEXT is "foo"
1225 and we want to complete to "foobar". If WORD is "oo", return
1226 "oobar"; if WORD is "baz/foo", return "baz/foobar". */
1229 complete_on_cmdlist (struct cmd_list_element *list, char *text, char *word)
1231 struct cmd_list_element *ptr;
1233 int sizeof_matchlist;
1235 int textlen = strlen (text);
1237 sizeof_matchlist = 10;
1238 matchlist = (char **) xmalloc (sizeof_matchlist * sizeof (char *));
1241 for (ptr = list; ptr; ptr = ptr->next)
1242 if (!strncmp (ptr->name, text, textlen)
1243 && !ptr->abbrev_flag
1244 && (ptr->function.cfunc
1245 || ptr->prefixlist))
1247 if (matches == sizeof_matchlist)
1249 sizeof_matchlist *= 2;
1250 matchlist = (char **) xrealloc ((char *) matchlist,
1252 * sizeof (char *)));
1255 matchlist[matches] = (char *)
1256 xmalloc (strlen (word) + strlen (ptr->name) + 1);
1258 strcpy (matchlist[matches], ptr->name);
1259 else if (word > text)
1261 /* Return some portion of ptr->name. */
1262 strcpy (matchlist[matches], ptr->name + (word - text));
1266 /* Return some of text plus ptr->name. */
1267 strncpy (matchlist[matches], word, text - word);
1268 matchlist[matches][text - word] = '\0';
1269 strcat (matchlist[matches], ptr->name);
1281 matchlist = (char **) xrealloc ((char *) matchlist, ((matches + 1)
1282 * sizeof (char *)));
1283 matchlist[matches] = (char *) 0;
1289 /* Helper function for SYMBOL_COMPLETION_FUNCTION. */
1291 /* Return a vector of char pointers which point to the different
1292 possible completions in CMD of TEXT.
1294 WORD points in the same buffer as TEXT, and completions should be
1295 returned relative to this position. For example, suppose TEXT is "foo"
1296 and we want to complete to "foobar". If WORD is "oo", return
1297 "oobar"; if WORD is "baz/foo", return "baz/foobar". */
1300 complete_on_enum (const char *enumlist[],
1305 int sizeof_matchlist;
1307 int textlen = strlen (text);
1311 sizeof_matchlist = 10;
1312 matchlist = (char **) xmalloc (sizeof_matchlist * sizeof (char *));
1315 for (i = 0; (name = enumlist[i]) != NULL; i++)
1316 if (strncmp (name, text, textlen) == 0)
1318 if (matches == sizeof_matchlist)
1320 sizeof_matchlist *= 2;
1321 matchlist = (char **) xrealloc ((char *) matchlist,
1323 * sizeof (char *)));
1326 matchlist[matches] = (char *)
1327 xmalloc (strlen (word) + strlen (name) + 1);
1329 strcpy (matchlist[matches], name);
1330 else if (word > text)
1332 /* Return some portion of name. */
1333 strcpy (matchlist[matches], name + (word - text));
1337 /* Return some of text plus name. */
1338 strncpy (matchlist[matches], word, text - word);
1339 matchlist[matches][text - word] = '\0';
1340 strcat (matchlist[matches], name);
1352 matchlist = (char **) xrealloc ((char *) matchlist, ((matches + 1)
1353 * sizeof (char *)));
1354 matchlist[matches] = (char *) 0;