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 #include "gdb_assert.h"
33 /* Prototypes for local functions */
35 static void undef_cmd_error (char *, char *);
37 static struct cmd_list_element *find_cmd (char *command,
39 struct cmd_list_element *clist,
40 int ignore_help_classes,
43 static void help_all (struct ui_file *stream);
45 /* Set the callback function for the specified command. For each both
46 the commands callback and func() are set. The latter set to a
47 bounce function (unless cfunc / sfunc is NULL that is). */
50 do_cfunc (struct cmd_list_element *c, char *args, int from_tty)
52 c->function.cfunc (args, from_tty); /* Ok. */
56 set_cmd_cfunc (struct cmd_list_element *cmd, cmd_cfunc_ftype *cfunc)
62 cmd->function.cfunc = cfunc; /* Ok. */
66 do_sfunc (struct cmd_list_element *c, char *args, int from_tty)
68 c->function.sfunc (args, from_tty, c); /* Ok. */
72 set_cmd_sfunc (struct cmd_list_element *cmd, cmd_sfunc_ftype *sfunc)
78 cmd->function.sfunc = sfunc; /* Ok. */
82 cmd_cfunc_eq (struct cmd_list_element *cmd,
83 void (*cfunc) (char *args, int from_tty))
85 return cmd->func == do_cfunc && cmd->function.cfunc == cfunc;
89 set_cmd_context (struct cmd_list_element *cmd, void *context)
91 cmd->context = context;
95 get_cmd_context (struct cmd_list_element *cmd)
101 cmd_type (struct cmd_list_element *cmd)
107 set_cmd_completer (struct cmd_list_element *cmd,
108 char **(*completer) (char *text, char *word))
110 cmd->completer = completer; /* Ok. */
114 /* Add element named NAME.
115 CLASS is the top level category into which commands are broken down
117 FUN should be the function to execute the command;
118 it will get a character string as argument, with leading
119 and trailing blanks already eliminated.
121 DOC is a documentation string for the command.
122 Its first line should be a complete sentence.
123 It should start with ? for a command that is an abbreviation
124 or with * for a command that most users don't need to know about.
126 Add this command to command list *LIST.
128 Returns a pointer to the added command (not necessarily the head
131 struct cmd_list_element *
132 add_cmd (char *name, enum command_class class, void (*fun) (char *, int),
133 char *doc, struct cmd_list_element **list)
135 register struct cmd_list_element *c
136 = (struct cmd_list_element *) xmalloc (sizeof (struct cmd_list_element));
137 struct cmd_list_element *p;
139 delete_cmd (name, list);
141 if (*list == NULL || strcmp ((*list)->name, name) >= 0)
149 while (p->next && strcmp (p->next->name, name) <= 0)
159 set_cmd_cfunc (c, fun);
160 set_cmd_context (c, NULL);
163 c->replacement = NULL;
164 c->pre_show_hook = NULL;
168 c->prefixlist = NULL;
169 c->prefixname = NULL;
170 c->allow_unknown = 0;
172 set_cmd_completer (c, make_symbol_completion_list);
173 c->type = not_set_cmd;
175 c->var_type = var_boolean;
177 c->user_commands = NULL;
178 c->hookee_pre = NULL;
179 c->hookee_post = NULL;
180 c->cmd_pointer = NULL;
185 /* Same as above, except that the abbrev_flag is set. */
186 /* Note: Doesn't seem to be used anywhere currently. */
188 struct cmd_list_element *
189 add_abbrev_cmd (char *name, enum command_class class, void (*fun) (char *, int),
190 char *doc, struct cmd_list_element **list)
192 register struct cmd_list_element *c
193 = add_cmd (name, class, fun, doc, list);
199 /* Deprecates a command CMD.
200 REPLACEMENT is the name of the command which should be used in place
201 of this command, or NULL if no such command exists.
203 This function does not check to see if command REPLACEMENT exists
204 since gdb may not have gotten around to adding REPLACEMENT when this
207 Returns a pointer to the deprecated command. */
209 struct cmd_list_element *
210 deprecate_cmd (struct cmd_list_element *cmd, char *replacement)
212 cmd->flags |= (CMD_DEPRECATED | DEPRECATED_WARN_USER);
214 if (replacement != NULL)
215 cmd->replacement = replacement;
217 cmd->replacement = NULL;
222 struct cmd_list_element *
223 add_alias_cmd (char *name, char *oldname, enum command_class class,
224 int abbrev_flag, struct cmd_list_element **list)
226 /* Must do this since lookup_cmd tries to side-effect its first arg */
228 register struct cmd_list_element *old;
229 register struct cmd_list_element *c;
230 copied_name = (char *) alloca (strlen (oldname) + 1);
231 strcpy (copied_name, oldname);
232 old = lookup_cmd (&copied_name, *list, "", 1, 1);
236 delete_cmd (name, list);
240 c = add_cmd (name, class, NULL, old->doc, list);
241 /* NOTE: Both FUNC and all the FUNCTIONs need to be copied. */
243 c->function = old->function;
244 c->prefixlist = old->prefixlist;
245 c->prefixname = old->prefixname;
246 c->allow_unknown = old->allow_unknown;
247 c->abbrev_flag = abbrev_flag;
248 c->cmd_pointer = old;
252 /* Like add_cmd but adds an element for a command prefix:
253 a name that should be followed by a subcommand to be looked up
254 in another command list. PREFIXLIST should be the address
255 of the variable containing that list. */
257 struct cmd_list_element *
258 add_prefix_cmd (char *name, enum command_class class, void (*fun) (char *, int),
259 char *doc, struct cmd_list_element **prefixlist,
260 char *prefixname, int allow_unknown,
261 struct cmd_list_element **list)
263 register struct cmd_list_element *c = add_cmd (name, class, fun, doc, list);
264 c->prefixlist = prefixlist;
265 c->prefixname = prefixname;
266 c->allow_unknown = allow_unknown;
270 /* Like add_prefix_cmd but sets the abbrev_flag on the new command. */
272 struct cmd_list_element *
273 add_abbrev_prefix_cmd (char *name, enum command_class class,
274 void (*fun) (char *, int), char *doc,
275 struct cmd_list_element **prefixlist, char *prefixname,
276 int allow_unknown, struct cmd_list_element **list)
278 register struct cmd_list_element *c = add_cmd (name, class, fun, doc, list);
279 c->prefixlist = prefixlist;
280 c->prefixname = prefixname;
281 c->allow_unknown = allow_unknown;
286 /* This is an empty "cfunc". */
288 not_just_help_class_command (char *args, int from_tty)
292 /* This is an empty "sfunc". */
293 static void empty_sfunc (char *, int, struct cmd_list_element *);
296 empty_sfunc (char *args, int from_tty, struct cmd_list_element *c)
300 /* Add element named NAME to command list LIST (the list for set/show
301 or some sublist thereof).
302 TYPE is set_cmd or show_cmd.
303 CLASS is as in add_cmd.
304 VAR_TYPE is the kind of thing we are setting.
305 VAR is address of the variable being controlled by this command.
306 DOC is the documentation string. */
308 static struct cmd_list_element *
309 add_set_or_show_cmd (char *name,
311 enum command_class class,
315 struct cmd_list_element **list)
317 struct cmd_list_element *c = add_cmd (name, class, NULL, doc, list);
318 gdb_assert (type == set_cmd || type == show_cmd);
320 c->var_type = var_type;
322 /* This needs to be something besides NULL so that this isn't
323 treated as a help class. */
324 set_cmd_sfunc (c, empty_sfunc);
328 /* Add element named NAME to both the command SET_LIST and SHOW_LIST.
329 CLASS is as in add_cmd. VAR_TYPE is the kind of thing we are
330 setting. VAR is address of the variable being controlled by this
331 command. SET_FUNC and SHOW_FUNC are the callback functions (if
332 non-NULL). SET_DOC and SHOW_DOC are the documentation strings.
333 SET_RESULT and SHOW_RESULT, if not NULL, are set to the resulting
334 command structures. */
337 add_setshow_cmd_full (char *name,
338 enum command_class class,
339 var_types var_type, void *var,
340 char *set_doc, char *show_doc,
341 cmd_sfunc_ftype *set_func, cmd_sfunc_ftype *show_func,
342 struct cmd_list_element **set_list,
343 struct cmd_list_element **show_list,
344 struct cmd_list_element **set_result,
345 struct cmd_list_element **show_result)
347 struct cmd_list_element *set;
348 struct cmd_list_element *show;
349 set = add_set_or_show_cmd (name, set_cmd, class, var_type, var,
351 if (set_func != NULL)
352 set_cmd_sfunc (set, set_func);
353 show = add_set_or_show_cmd (name, show_cmd, class, var_type, var,
354 show_doc, show_list);
355 if (show_func != NULL)
356 set_cmd_sfunc (show, show_func);
358 if (set_result != NULL)
360 if (show_result != NULL)
364 /* Add element named NAME to both the command SET_LIST and SHOW_LIST.
365 CLASS is as in add_cmd. VAR_TYPE is the kind of thing we are
366 setting. VAR is address of the variable being controlled by this
367 command. SET_FUNC and SHOW_FUNC are the callback functions (if
368 non-NULL). SET_DOC and SHOW_DOC are the documentation strings. */
371 add_setshow_cmd (char *name,
372 enum command_class class,
373 var_types var_type, void *var,
374 char *set_doc, char *show_doc,
375 cmd_sfunc_ftype *set_func, cmd_sfunc_ftype *show_func,
376 struct cmd_list_element **set_list,
377 struct cmd_list_element **show_list)
379 add_setshow_cmd_full (name, class, var_type, var, set_doc, show_doc,
380 set_func, show_func, set_list, show_list,
384 struct cmd_list_element *
385 add_set_cmd (char *name,
386 enum command_class class,
390 struct cmd_list_element **list)
392 return add_set_or_show_cmd (name, set_cmd, class, var_type, var, doc, list);
395 /* Add element named NAME to command list LIST (the list for set
396 or some sublist thereof).
397 CLASS is as in add_cmd.
398 ENUMLIST is a list of strings which may follow NAME.
399 VAR is address of the variable which will contain the matching string
401 DOC is the documentation string. */
403 struct cmd_list_element *
404 add_set_enum_cmd (char *name,
405 enum command_class class,
406 const char *enumlist[],
409 struct cmd_list_element **list)
411 struct cmd_list_element *c
412 = add_set_cmd (name, class, var_enum, var, doc, list);
418 /* Add an auto-boolean command named NAME to both the set and show
419 command list lists. CLASS is as in add_cmd. VAR is address of the
420 variable which will contain the value. DOC is the documentation
421 string. FUNC is the corresponding callback. */
423 add_setshow_auto_boolean_cmd (char *name,
424 enum command_class class,
425 enum auto_boolean *var,
426 char *set_doc, char *show_doc,
427 cmd_sfunc_ftype *set_func,
428 cmd_sfunc_ftype *show_func,
429 struct cmd_list_element **set_list,
430 struct cmd_list_element **show_list)
432 static const char *auto_boolean_enums[] = { "on", "off", "auto", NULL };
433 struct cmd_list_element *c;
434 add_setshow_cmd_full (name, class, var_auto_boolean, var,
435 set_doc, show_doc, set_func, show_func,
438 c->enums = auto_boolean_enums;
441 /* Add element named NAME to both the set and show command LISTs (the
442 list for set/show or some sublist thereof). CLASS is as in
443 add_cmd. VAR is address of the variable which will contain the
444 value. SET_DOC and SHOW_DOR are the documentation strings. */
446 add_setshow_boolean_cmd (char *name,
447 enum command_class class,
448 int *var, char *set_doc, char *show_doc,
449 cmd_sfunc_ftype *set_func,
450 cmd_sfunc_ftype *show_func,
451 struct cmd_list_element **set_list,
452 struct cmd_list_element **show_list)
454 static const char *boolean_enums[] = { "on", "off", NULL };
455 struct cmd_list_element *c;
456 add_setshow_cmd_full (name, class, var_boolean, var,
461 c->enums = boolean_enums;
464 /* Where SETCMD has already been added, add the corresponding show
465 command to LIST and return a pointer to the added command (not
466 necessarily the head of LIST). */
467 /* NOTE: cagney/2002-03-17: The original version of add_show_from_set
468 used memcpy() to clone `set' into `show'. This meant that in
469 addition to all the needed fields (var, name, et.al.) some
470 unnecessary fields were copied (namely the callback function). The
471 function explictly copies relevant fields. For a `set' and `show'
472 command to share the same callback, the caller must set both
474 struct cmd_list_element *
475 add_show_from_set (struct cmd_list_element *setcmd,
476 struct cmd_list_element **list)
479 const static char setstring[] = "Set ";
481 /* Create a doc string by replacing "Set " at the start of the
482 `set'' command's doco with "Show ". */
483 gdb_assert (strncmp (setcmd->doc, setstring, sizeof (setstring) - 1) == 0);
484 doc = concat ("Show ", setcmd->doc + sizeof (setstring) - 1, NULL);
486 /* Insert the basic command. */
487 return add_set_or_show_cmd (setcmd->name, show_cmd, setcmd->class,
488 setcmd->var_type, setcmd->var, doc, list);
491 /* Remove the command named NAME from the command list. */
494 delete_cmd (char *name, struct cmd_list_element **list)
496 register struct cmd_list_element *c;
497 struct cmd_list_element *p;
499 while (*list && STREQ ((*list)->name, name))
501 if ((*list)->hookee_pre)
502 (*list)->hookee_pre->hook_pre = 0; /* Hook slips out of its mouth */
503 if ((*list)->hookee_post)
504 (*list)->hookee_post->hook_post = 0; /* Hook slips out of its bottom */
511 for (c = *list; c->next;)
513 if (STREQ (c->next->name, name))
515 if (c->next->hookee_pre)
516 c->next->hookee_pre->hook_pre = 0; /* hooked cmd gets away. */
517 if (c->next->hookee_post)
518 c->next->hookee_post->hook_post = 0; /* remove post hook */
519 /* :( no fishing metaphore */
529 /* Shorthands to the commands above. */
531 /* Add an element to the list of info subcommands. */
533 struct cmd_list_element *
534 add_info (char *name, void (*fun) (char *, int), char *doc)
536 return add_cmd (name, no_class, fun, doc, &infolist);
539 /* Add an alias to the list of info subcommands. */
541 struct cmd_list_element *
542 add_info_alias (char *name, char *oldname, int abbrev_flag)
544 return add_alias_cmd (name, oldname, 0, abbrev_flag, &infolist);
547 /* Add an element to the list of commands. */
549 struct cmd_list_element *
550 add_com (char *name, enum command_class class, void (*fun) (char *, int),
553 return add_cmd (name, class, fun, doc, &cmdlist);
556 /* Add an alias or abbreviation command to the list of commands. */
558 struct cmd_list_element *
559 add_com_alias (char *name, char *oldname, enum command_class class,
562 return add_alias_cmd (name, oldname, class, abbrev_flag, &cmdlist);
565 /* Recursively walk the commandlist structures, and print out the
566 documentation of commands that match our regex in either their
567 name, or their documentation.
570 apropos_cmd (struct ui_file *stream, struct cmd_list_element *commandlist,
571 struct re_pattern_buffer *regex, char *prefix)
573 register struct cmd_list_element *c;
574 int returnvalue=1; /*Needed to avoid double printing*/
575 /* Walk through the commands */
576 for (c=commandlist;c;c=c->next)
580 /* Try to match against the name*/
581 returnvalue=re_search(regex,c->name,strlen(c->name),0,strlen(c->name),NULL);
582 if (returnvalue >= 0)
584 /* Stolen from help_cmd_list. We don't directly use
585 * help_cmd_list because it doesn't let us print out
588 fprintf_filtered (stream, "%s%s -- ", prefix, c->name);
589 print_doc_line (stream, c->doc);
590 fputs_filtered ("\n", stream);
591 returnvalue=0; /*Set this so we don't print it again.*/
594 if (c->doc != NULL && returnvalue != 0)
596 /* Try to match against documentation */
597 if (re_search(regex,c->doc,strlen(c->doc),0,strlen(c->doc),NULL) >=0)
599 /* Stolen from help_cmd_list. We don't directly use
600 * help_cmd_list because it doesn't let us print out
603 fprintf_filtered (stream, "%s%s -- ", prefix, c->name);
604 print_doc_line (stream, c->doc);
605 fputs_filtered ("\n", stream);
608 /* Check if this command has subcommands */
609 if (c->prefixlist != NULL)
611 /* Recursively call ourselves on the subcommand list,
612 passing the right prefix in.
614 apropos_cmd (stream,*c->prefixlist,regex,c->prefixname);
619 /* This command really has to deal with two things:
620 * 1) I want documentation on *this string* (usually called by
621 * "help commandname").
622 * 2) I want documentation on *this list* (usually called by
623 * giving a command that requires subcommands. Also called by saying
626 * I am going to split this into two seperate comamnds, help_cmd and
631 help_cmd (char *command, struct ui_file *stream)
633 struct cmd_list_element *c;
634 extern struct cmd_list_element *cmdlist;
638 help_list (cmdlist, "", all_classes, stream);
642 if (strcmp (command, "all") == 0)
648 c = lookup_cmd (&command, cmdlist, "", 0, 0);
653 /* There are three cases here.
654 If c->prefixlist is nonzero, we have a prefix command.
655 Print its documentation, then list its subcommands.
657 If c->func is non NULL, we really have a command. Print its
658 documentation and return.
660 If c->func is NULL, we have a class name. Print its
661 documentation (as if it were a command) and then set class to the
662 number of this class so that the commands in the class will be
665 fputs_filtered (c->doc, stream);
666 fputs_filtered ("\n", stream);
668 if (c->prefixlist == 0 && c->func != NULL)
670 fprintf_filtered (stream, "\n");
672 /* If this is a prefix command, print it's subcommands */
674 help_list (*c->prefixlist, c->prefixname, all_commands, stream);
676 /* If this is a class name, print all of the commands in the class */
678 help_list (cmdlist, "", c->class, stream);
680 if (c->hook_pre || c->hook_post)
681 fprintf_filtered (stream,
682 "\nThis command has a hook (or hooks) defined:\n");
685 fprintf_filtered (stream,
686 "\tThis command is run after : %s (pre hook)\n",
689 fprintf_filtered (stream,
690 "\tThis command is run before : %s (post hook)\n",
695 * Get a specific kind of help on a command list.
698 * CMDTYPE is the prefix to use in the title string.
699 * CLASS is the class with which to list the nodes of this list (see
700 * documentation for help_cmd_list below), As usual, ALL_COMMANDS for
701 * everything, ALL_CLASSES for just classes, and non-negative for only things
702 * in a specific class.
703 * and STREAM is the output stream on which to print things.
704 * If you call this routine with a class >= 0, it recurses.
707 help_list (struct cmd_list_element *list, char *cmdtype,
708 enum command_class class, struct ui_file *stream)
711 char *cmdtype1, *cmdtype2;
713 /* If CMDTYPE is "foo ", CMDTYPE1 gets " foo" and CMDTYPE2 gets "foo sub" */
714 len = strlen (cmdtype);
715 cmdtype1 = (char *) alloca (len + 1);
717 cmdtype2 = (char *) alloca (len + 4);
722 strncpy (cmdtype1 + 1, cmdtype, len - 1);
724 strncpy (cmdtype2, cmdtype, len - 1);
725 strcpy (cmdtype2 + len - 1, " sub");
728 if (class == all_classes)
729 fprintf_filtered (stream, "List of classes of %scommands:\n\n", cmdtype2);
731 fprintf_filtered (stream, "List of %scommands:\n\n", cmdtype2);
733 help_cmd_list (list, class, cmdtype, (int) class >= 0, stream);
735 if (class == all_classes)
737 fprintf_filtered (stream, "\n\
738 Type \"help%s\" followed by a class name for a list of commands in ",
741 fprintf_filtered (stream, "that class.");
744 fprintf_filtered (stream, "\nType \"help%s\" followed by %scommand name ",
747 fputs_filtered ("for ", stream);
749 fputs_filtered ("full ", stream);
751 fputs_filtered ("documentation.\n", stream);
752 fputs_filtered ("Command name abbreviations are allowed if unambiguous.\n",
757 help_all (struct ui_file *stream)
759 struct cmd_list_element *c;
760 extern struct cmd_list_element *cmdlist;
762 for (c = cmdlist; c; c = c->next)
766 /* If this is a prefix command, print it's subcommands */
768 help_cmd_list (*c->prefixlist, all_commands, c->prefixname, 0, stream);
770 /* If this is a class name, print all of the commands in the class */
771 else if (c->func == NULL)
772 help_cmd_list (cmdlist, c->class, "", 0, stream);
776 /* Print only the first line of STR on STREAM. */
778 print_doc_line (struct ui_file *stream, char *str)
780 static char *line_buffer = 0;
781 static int line_size;
787 line_buffer = (char *) xmalloc (line_size);
791 while (*p && *p != '\n' && *p != '.' && *p != ',')
793 if (p - str > line_size - 1)
795 line_size = p - str + 1;
797 line_buffer = (char *) xmalloc (line_size);
799 strncpy (line_buffer, str, p - str);
800 line_buffer[p - str] = '\0';
801 if (islower (line_buffer[0]))
802 line_buffer[0] = toupper (line_buffer[0]);
803 ui_out_text (uiout, line_buffer);
807 * Implement a help command on command list LIST.
808 * RECURSE should be non-zero if this should be done recursively on
809 * all sublists of LIST.
810 * PREFIX is the prefix to print before each command name.
811 * STREAM is the stream upon which the output should be written.
813 * A non-negative class number to list only commands in that
815 * ALL_COMMANDS to list all commands in list.
816 * ALL_CLASSES to list all classes in list.
818 * Note that RECURSE will be active on *all* sublists, not just the
819 * ones selected by the criteria above (ie. the selection mechanism
820 * is at the low level, not the high-level).
823 help_cmd_list (struct cmd_list_element *list, enum command_class class,
824 char *prefix, int recurse, struct ui_file *stream)
826 register struct cmd_list_element *c;
828 for (c = list; c; c = c->next)
830 if (c->abbrev_flag == 0 &&
831 (class == all_commands
832 || (class == all_classes && c->func == NULL)
833 || (class == c->class && c->func != NULL)))
835 fprintf_filtered (stream, "%s%s -- ", prefix, c->name);
836 print_doc_line (stream, c->doc);
837 fputs_filtered ("\n", stream);
840 && c->prefixlist != 0
841 && c->abbrev_flag == 0)
842 help_cmd_list (*c->prefixlist, class, c->prefixname, 1, stream);
847 /* Search the input clist for 'command'. Return the command if
848 found (or NULL if not), and return the number of commands
851 static struct cmd_list_element *
852 find_cmd (char *command, int len, struct cmd_list_element *clist,
853 int ignore_help_classes, int *nfound)
855 struct cmd_list_element *found, *c;
857 found = (struct cmd_list_element *) NULL;
859 for (c = clist; c; c = c->next)
860 if (!strncmp (command, c->name, len)
861 && (!ignore_help_classes || c->func))
865 if (c->name[len] == '\0')
874 /* This routine takes a line of TEXT and a CLIST in which to start the
875 lookup. When it returns it will have incremented the text pointer past
876 the section of text it matched, set *RESULT_LIST to point to the list in
877 which the last word was matched, and will return a pointer to the cmd
878 list element which the text matches. It will return NULL if no match at
879 all was possible. It will return -1 (cast appropriately, ick) if ambigous
880 matches are possible; in this case *RESULT_LIST will be set to point to
881 the list in which there are ambiguous choices (and *TEXT will be set to
882 the ambiguous text string).
884 If the located command was an abbreviation, this routine returns the base
885 command of the abbreviation.
887 It does no error reporting whatsoever; control will always return
888 to the superior routine.
890 In the case of an ambiguous return (-1), *RESULT_LIST will be set to point
891 at the prefix_command (ie. the best match) *or* (special case) will be NULL
892 if no prefix command was ever found. For example, in the case of "info a",
893 "info" matches without ambiguity, but "a" could be "args" or "address", so
894 *RESULT_LIST is set to the cmd_list_element for "info". So in this case
895 RESULT_LIST should not be interpeted as a pointer to the beginning of a
896 list; it simply points to a specific command. In the case of an ambiguous
897 return *TEXT is advanced past the last non-ambiguous prefix (e.g.
898 "info t" can be "info types" or "info target"; upon return *TEXT has been
899 advanced past "info ").
901 If RESULT_LIST is NULL, don't set *RESULT_LIST (but don't otherwise
902 affect the operation).
904 This routine does *not* modify the text pointed to by TEXT.
906 If IGNORE_HELP_CLASSES is nonzero, ignore any command list elements which
907 are actually help classes rather than commands (i.e. the function field of
908 the struct cmd_list_element is NULL). */
910 struct cmd_list_element *
911 lookup_cmd_1 (char **text, struct cmd_list_element *clist,
912 struct cmd_list_element **result_list, int ignore_help_classes)
915 int len, tmp, nfound;
916 struct cmd_list_element *found, *c;
919 while (**text == ' ' || **text == '\t')
922 /* Treating underscores as part of command words is important
923 so that "set args_foo()" doesn't get interpreted as
924 "set args _foo()". */
926 *p && (isalnum (*p) || *p == '-' || *p == '_' ||
928 (*p == '+' || *p == '<' || *p == '>' || *p == '$')) ||
929 (xdb_commands && (*p == '!' || *p == '/' || *p == '?')));
933 /* If nothing but whitespace, return 0. */
939 /* *text and p now bracket the first command word to lookup (and
940 it's length is len). We copy this into a local temporary */
943 command = (char *) alloca (len + 1);
944 for (tmp = 0; tmp < len; tmp++)
946 char x = (*text)[tmp];
954 found = find_cmd (command, len, clist, ignore_help_classes, &nfound);
957 ** We didn't find the command in the entered case, so lower case it
960 if (!found || nfound == 0)
962 for (tmp = 0; tmp < len; tmp++)
964 char x = command[tmp];
965 command[tmp] = isupper (x) ? tolower (x) : x;
967 found = find_cmd (command, len, clist, ignore_help_classes, &nfound);
970 /* If nothing matches, we have a simple failure. */
976 if (result_list != NULL)
977 /* Will be modified in calling routine
978 if we know what the prefix command is. */
980 return (struct cmd_list_element *) -1; /* Ambiguous. */
983 /* We've matched something on this list. Move text pointer forward. */
987 if (found->cmd_pointer)
989 /* We drop the alias (abbreviation) in favor of the command it is
990 pointing to. If the alias is deprecated, though, we need to
991 warn the user about it before we drop it. Note that while we
992 are warning about the alias, we may also warn about the command
993 itself and we will adjust the appropriate DEPRECATED_WARN_USER
996 if (found->flags & DEPRECATED_WARN_USER)
997 deprecated_cmd_warning (&line);
998 found = found->cmd_pointer;
1000 /* If we found a prefix command, keep looking. */
1002 if (found->prefixlist)
1004 c = lookup_cmd_1 (text, *found->prefixlist, result_list,
1005 ignore_help_classes);
1008 /* Didn't find anything; this is as far as we got. */
1009 if (result_list != NULL)
1010 *result_list = clist;
1013 else if (c == (struct cmd_list_element *) -1)
1015 /* We've gotten this far properly, but the next step
1016 is ambiguous. We need to set the result list to the best
1017 we've found (if an inferior hasn't already set it). */
1018 if (result_list != NULL)
1020 /* This used to say *result_list = *found->prefixlist
1021 If that was correct, need to modify the documentation
1022 at the top of this function to clarify what is supposed
1024 *result_list = found;
1035 if (result_list != NULL)
1036 *result_list = clist;
1041 /* All this hair to move the space to the front of cmdtype */
1044 undef_cmd_error (char *cmdtype, char *q)
1046 error ("Undefined %scommand: \"%s\". Try \"help%s%.*s\".",
1049 *cmdtype ? " " : "",
1050 (int) strlen (cmdtype) - 1,
1054 /* Look up the contents of *LINE as a command in the command list LIST.
1055 LIST is a chain of struct cmd_list_element's.
1056 If it is found, return the struct cmd_list_element for that command
1057 and update *LINE to point after the command name, at the first argument.
1058 If not found, call error if ALLOW_UNKNOWN is zero
1059 otherwise (or if error returns) return zero.
1060 Call error if specified command is ambiguous,
1061 unless ALLOW_UNKNOWN is negative.
1062 CMDTYPE precedes the word "command" in the error message.
1064 If INGNORE_HELP_CLASSES is nonzero, ignore any command list
1065 elements which are actually help classes rather than commands (i.e.
1066 the function field of the struct cmd_list_element is 0). */
1068 struct cmd_list_element *
1069 lookup_cmd (char **line, struct cmd_list_element *list, char *cmdtype,
1070 int allow_unknown, int ignore_help_classes)
1072 struct cmd_list_element *last_list = 0;
1073 struct cmd_list_element *c =
1074 lookup_cmd_1 (line, list, &last_list, ignore_help_classes);
1076 /* Note: Do not remove trailing whitespace here because this
1077 would be wrong for complete_command. Jim Kingdon */
1084 error ("Lack of needed %scommand", cmdtype);
1087 char *p = *line, *q;
1089 while (isalnum (*p) || *p == '-')
1092 q = (char *) alloca (p - *line + 1);
1093 strncpy (q, *line, p - *line);
1094 q[p - *line] = '\0';
1095 undef_cmd_error (cmdtype, q);
1101 else if (c == (struct cmd_list_element *) -1)
1103 /* Ambigous. Local values should be off prefixlist or called
1105 int local_allow_unknown = (last_list ? last_list->allow_unknown :
1107 char *local_cmdtype = last_list ? last_list->prefixname : cmdtype;
1108 struct cmd_list_element *local_list =
1109 (last_list ? *(last_list->prefixlist) : list);
1111 if (local_allow_unknown < 0)
1114 return last_list; /* Found something. */
1116 return 0; /* Found nothing. */
1120 /* Report as error. */
1125 ((*line)[amb_len] && (*line)[amb_len] != ' '
1126 && (*line)[amb_len] != '\t');
1131 for (c = local_list; c; c = c->next)
1132 if (!strncmp (*line, c->name, amb_len))
1134 if (strlen (ambbuf) + strlen (c->name) + 6 < (int) sizeof ambbuf)
1136 if (strlen (ambbuf))
1137 strcat (ambbuf, ", ");
1138 strcat (ambbuf, c->name);
1142 strcat (ambbuf, "..");
1146 error ("Ambiguous %scommand \"%s\": %s.", local_cmdtype,
1148 return 0; /* lint */
1153 /* We've got something. It may still not be what the caller
1154 wants (if this command *needs* a subcommand). */
1155 while (**line == ' ' || **line == '\t')
1158 if (c->prefixlist && **line && !c->allow_unknown)
1159 undef_cmd_error (c->prefixname, *line);
1161 /* Seems to be what he wants. Return it. */
1167 /* We are here presumably because an alias or command in *TEXT is
1168 deprecated and a warning message should be generated. This function
1169 decodes *TEXT and potentially generates a warning message as outlined
1172 Example for 'set endian big' which has a fictitious alias 'seb'.
1174 If alias wasn't used in *TEXT, and the command is deprecated:
1175 "warning: 'set endian big' is deprecated."
1177 If alias was used, and only the alias is deprecated:
1178 "warning: 'seb' an alias for the command 'set endian big' is deprecated."
1180 If alias was used and command is deprecated (regardless of whether the
1181 alias itself is deprecated:
1183 "warning: 'set endian big' (seb) is deprecated."
1185 After the message has been sent, clear the appropriate flags in the
1186 command and/or the alias so the user is no longer bothered.
1190 deprecated_cmd_warning (char **text)
1192 struct cmd_list_element *alias = NULL;
1193 struct cmd_list_element *prefix_cmd = NULL;
1194 struct cmd_list_element *cmd = NULL;
1195 struct cmd_list_element *c;
1198 if (!lookup_cmd_composition (*text, &alias, &prefix_cmd, &cmd))
1199 /* return if text doesn't evaluate to a command */
1202 if (!((alias ? (alias->flags & DEPRECATED_WARN_USER) : 0)
1203 || (cmd->flags & DEPRECATED_WARN_USER) ) )
1204 /* return if nothing is deprecated */
1207 printf_filtered ("Warning:");
1209 if (alias && !(cmd->flags & CMD_DEPRECATED))
1210 printf_filtered (" '%s', an alias for the", alias->name);
1212 printf_filtered (" command '");
1215 printf_filtered ("%s", prefix_cmd->prefixname);
1217 printf_filtered ("%s", cmd->name);
1219 if (alias && (cmd->flags & CMD_DEPRECATED))
1220 printf_filtered ("' (%s) is deprecated.\n", alias->name);
1222 printf_filtered ("' is deprecated.\n");
1225 /* if it is only the alias that is deprecated, we want to indicate the
1226 new alias, otherwise we'll indicate the new command */
1228 if (alias && !(cmd->flags & CMD_DEPRECATED))
1230 if (alias->replacement)
1231 printf_filtered ("Use '%s'.\n\n", alias->replacement);
1233 printf_filtered ("No alternative known.\n\n");
1237 if (cmd->replacement)
1238 printf_filtered ("Use '%s'.\n\n", cmd->replacement);
1240 printf_filtered ("No alternative known.\n\n");
1243 /* We've warned you, now we'll keep quiet */
1245 alias->flags &= ~DEPRECATED_WARN_USER;
1247 cmd->flags &= ~DEPRECATED_WARN_USER;
1252 /* Look up the contents of LINE as a command in the command list 'cmdlist'.
1253 Return 1 on success, 0 on failure.
1255 If LINE refers to an alias, *alias will point to that alias.
1257 If LINE is a postfix command (i.e. one that is preceeded by a prefix
1258 command) set *prefix_cmd.
1260 Set *cmd to point to the command LINE indicates.
1262 If any of *alias, *prefix_cmd, or *cmd cannot be determined or do not
1263 exist, they are NULL when we return.
1267 lookup_cmd_composition (char *text,
1268 struct cmd_list_element **alias,
1269 struct cmd_list_element **prefix_cmd,
1270 struct cmd_list_element **cmd)
1273 int len, tmp, nfound;
1274 struct cmd_list_element *cur_list;
1275 struct cmd_list_element *prev_cmd;
1284 /* Go through as many command lists as we need to
1285 to find the command TEXT refers to. */
1289 while (*text == ' ' || *text == '\t')
1292 /* Treating underscores as part of command words is important
1293 so that "set args_foo()" doesn't get interpreted as
1294 "set args _foo()". */
1296 *p && (isalnum (*p) || *p == '-' || *p == '_' ||
1298 (*p == '+' || *p == '<' || *p == '>' || *p == '$')) ||
1299 (xdb_commands && (*p == '!' || *p == '/' || *p == '?')));
1303 /* If nothing but whitespace, return. */
1309 /* text and p now bracket the first command word to lookup (and
1310 it's length is len). We copy this into a local temporary */
1312 command = (char *) alloca (len + 1);
1313 for (tmp = 0; tmp < len; tmp++)
1318 command[len] = '\0';
1323 *cmd = find_cmd (command, len, cur_list, 1, &nfound);
1325 /* We didn't find the command in the entered case, so lower case it
1328 if (!*cmd || nfound == 0)
1330 for (tmp = 0; tmp < len; tmp++)
1332 char x = command[tmp];
1333 command[tmp] = isupper (x) ? tolower (x) : x;
1335 *cmd = find_cmd (command, len, cur_list, 1, &nfound);
1338 if (*cmd == (struct cmd_list_element *) -1)
1340 return 0; /* ambiguous */
1344 return 0; /* nothing found */
1347 if ((*cmd)->cmd_pointer)
1349 /* cmd was actually an alias, we note that an alias was used
1350 (by assigning *alais) and we set *cmd.
1353 *cmd = (*cmd)->cmd_pointer;
1355 *prefix_cmd = prev_cmd;
1357 if ((*cmd)->prefixlist)
1358 cur_list = *(*cmd)->prefixlist;
1366 /* Helper function for SYMBOL_COMPLETION_FUNCTION. */
1368 /* Return a vector of char pointers which point to the different
1369 possible completions in LIST of TEXT.
1371 WORD points in the same buffer as TEXT, and completions should be
1372 returned relative to this position. For example, suppose TEXT is "foo"
1373 and we want to complete to "foobar". If WORD is "oo", return
1374 "oobar"; if WORD is "baz/foo", return "baz/foobar". */
1377 complete_on_cmdlist (struct cmd_list_element *list, char *text, char *word)
1379 struct cmd_list_element *ptr;
1381 int sizeof_matchlist;
1383 int textlen = strlen (text);
1385 sizeof_matchlist = 10;
1386 matchlist = (char **) xmalloc (sizeof_matchlist * sizeof (char *));
1389 for (ptr = list; ptr; ptr = ptr->next)
1390 if (!strncmp (ptr->name, text, textlen)
1391 && !ptr->abbrev_flag
1393 || ptr->prefixlist))
1395 if (matches == sizeof_matchlist)
1397 sizeof_matchlist *= 2;
1398 matchlist = (char **) xrealloc ((char *) matchlist,
1400 * sizeof (char *)));
1403 matchlist[matches] = (char *)
1404 xmalloc (strlen (word) + strlen (ptr->name) + 1);
1406 strcpy (matchlist[matches], ptr->name);
1407 else if (word > text)
1409 /* Return some portion of ptr->name. */
1410 strcpy (matchlist[matches], ptr->name + (word - text));
1414 /* Return some of text plus ptr->name. */
1415 strncpy (matchlist[matches], word, text - word);
1416 matchlist[matches][text - word] = '\0';
1417 strcat (matchlist[matches], ptr->name);
1429 matchlist = (char **) xrealloc ((char *) matchlist, ((matches + 1)
1430 * sizeof (char *)));
1431 matchlist[matches] = (char *) 0;
1437 /* Helper function for SYMBOL_COMPLETION_FUNCTION. */
1439 /* Return a vector of char pointers which point to the different
1440 possible completions in CMD of TEXT.
1442 WORD points in the same buffer as TEXT, and completions should be
1443 returned relative to this position. For example, suppose TEXT is "foo"
1444 and we want to complete to "foobar". If WORD is "oo", return
1445 "oobar"; if WORD is "baz/foo", return "baz/foobar". */
1448 complete_on_enum (const char *enumlist[],
1453 int sizeof_matchlist;
1455 int textlen = strlen (text);
1459 sizeof_matchlist = 10;
1460 matchlist = (char **) xmalloc (sizeof_matchlist * sizeof (char *));
1463 for (i = 0; (name = enumlist[i]) != NULL; i++)
1464 if (strncmp (name, text, textlen) == 0)
1466 if (matches == sizeof_matchlist)
1468 sizeof_matchlist *= 2;
1469 matchlist = (char **) xrealloc ((char *) matchlist,
1471 * sizeof (char *)));
1474 matchlist[matches] = (char *)
1475 xmalloc (strlen (word) + strlen (name) + 1);
1477 strcpy (matchlist[matches], name);
1478 else if (word > text)
1480 /* Return some portion of name. */
1481 strcpy (matchlist[matches], name + (word - text));
1485 /* Return some of text plus name. */
1486 strncpy (matchlist[matches], word, text - word);
1487 matchlist[matches][text - word] = '\0';
1488 strcat (matchlist[matches], name);
1500 matchlist = (char **) xrealloc ((char *) matchlist, ((matches + 1)
1501 * sizeof (char *)));
1502 matchlist[matches] = (char *) 0;