1 /* Handle lists of commands, their decoding and documentation, for GDB.
3 Copyright (c) 1986, 1989, 1990, 1991, 1998, 2000, 2001, 2002, 2004, 2007,
4 2008 Free 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 3 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, see <http://www.gnu.org/licenses/>. */
22 #include "gdb_regex.h"
23 #include "gdb_string.h"
24 #include "completer.h"
27 #include "cli/cli-cmds.h"
28 #include "cli/cli-decode.h"
31 #include "tui/tui.h" /* For tui_active et.al. */
34 #include "gdb_assert.h"
36 /* Prototypes for local functions */
38 static void undef_cmd_error (char *, char *);
40 static struct cmd_list_element *delete_cmd (char *name,
41 struct cmd_list_element **list);
43 static struct cmd_list_element *find_cmd (char *command,
45 struct cmd_list_element *clist,
46 int ignore_help_classes,
49 static void help_all (struct ui_file *stream);
52 print_help_for_command (struct cmd_list_element *c, char *prefix, int recurse,
53 struct ui_file *stream);
56 /* Set the callback function for the specified command. For each both
57 the commands callback and func() are set. The latter set to a
58 bounce function (unless cfunc / sfunc is NULL that is). */
61 do_cfunc (struct cmd_list_element *c, char *args, int from_tty)
63 c->function.cfunc (args, from_tty); /* Ok. */
67 set_cmd_cfunc (struct cmd_list_element *cmd, cmd_cfunc_ftype *cfunc)
73 cmd->function.cfunc = cfunc; /* Ok. */
77 do_sfunc (struct cmd_list_element *c, char *args, int from_tty)
79 c->function.sfunc (args, from_tty, c); /* Ok. */
83 set_cmd_sfunc (struct cmd_list_element *cmd, cmd_sfunc_ftype *sfunc)
89 cmd->function.sfunc = sfunc; /* Ok. */
93 cmd_cfunc_eq (struct cmd_list_element *cmd,
94 void (*cfunc) (char *args, int from_tty))
96 return cmd->func == do_cfunc && cmd->function.cfunc == cfunc;
100 set_cmd_context (struct cmd_list_element *cmd, void *context)
102 cmd->context = context;
106 get_cmd_context (struct cmd_list_element *cmd)
112 set_cmd_no_selected_thread_ok (struct cmd_list_element *cmd)
114 cmd->flags |= CMD_NO_SELECTED_THREAD_OK;
118 get_cmd_no_selected_thread_ok (struct cmd_list_element *cmd)
120 return cmd->flags & CMD_NO_SELECTED_THREAD_OK;
124 cmd_type (struct cmd_list_element *cmd)
130 set_cmd_completer (struct cmd_list_element *cmd,
131 char **(*completer) (char *text, char *word))
133 cmd->completer = completer; /* Ok. */
137 /* Add element named NAME.
138 CLASS is the top level category into which commands are broken down
140 FUN should be the function to execute the command;
141 it will get a character string as argument, with leading
142 and trailing blanks already eliminated.
144 DOC is a documentation string for the command.
145 Its first line should be a complete sentence.
146 It should start with ? for a command that is an abbreviation
147 or with * for a command that most users don't need to know about.
149 Add this command to command list *LIST.
151 Returns a pointer to the added command (not necessarily the head
154 struct cmd_list_element *
155 add_cmd (char *name, enum command_class class, void (*fun) (char *, int),
156 char *doc, struct cmd_list_element **list)
158 struct cmd_list_element *c
159 = (struct cmd_list_element *) xmalloc (sizeof (struct cmd_list_element));
160 struct cmd_list_element *p, *iter;
162 /* Turn each alias of the old command into an alias of the new
164 c->aliases = delete_cmd (name, list);
165 for (iter = c->aliases; iter; iter = iter->alias_chain)
166 iter->cmd_pointer = c;
168 if (*list == NULL || strcmp ((*list)->name, name) >= 0)
176 while (p->next && strcmp (p->next->name, name) <= 0)
186 set_cmd_cfunc (c, fun);
187 set_cmd_context (c, NULL);
190 c->replacement = NULL;
191 c->pre_show_hook = NULL;
195 c->prefixlist = NULL;
196 c->prefixname = NULL;
197 c->allow_unknown = 0;
199 set_cmd_completer (c, make_symbol_completion_list);
200 c->type = not_set_cmd;
202 c->var_type = var_boolean;
204 c->user_commands = NULL;
205 c->hookee_pre = NULL;
206 c->hookee_post = NULL;
207 c->cmd_pointer = NULL;
208 c->alias_chain = NULL;
213 /* Deprecates a command CMD.
214 REPLACEMENT is the name of the command which should be used in place
215 of this command, or NULL if no such command exists.
217 This function does not check to see if command REPLACEMENT exists
218 since gdb may not have gotten around to adding REPLACEMENT when this
221 Returns a pointer to the deprecated command. */
223 struct cmd_list_element *
224 deprecate_cmd (struct cmd_list_element *cmd, char *replacement)
226 cmd->flags |= (CMD_DEPRECATED | DEPRECATED_WARN_USER);
228 if (replacement != NULL)
229 cmd->replacement = replacement;
231 cmd->replacement = NULL;
236 struct cmd_list_element *
237 add_alias_cmd (char *name, char *oldname, enum command_class class,
238 int abbrev_flag, struct cmd_list_element **list)
240 /* Must do this since lookup_cmd tries to side-effect its first arg */
242 struct cmd_list_element *old;
243 struct cmd_list_element *c;
244 copied_name = (char *) alloca (strlen (oldname) + 1);
245 strcpy (copied_name, oldname);
246 old = lookup_cmd (&copied_name, *list, "", 1, 1);
250 struct cmd_list_element *aliases = delete_cmd (name, list);
251 /* If this happens, it means a programmer error somewhere. */
252 gdb_assert (!aliases);
256 c = add_cmd (name, class, NULL, old->doc, list);
257 /* NOTE: Both FUNC and all the FUNCTIONs need to be copied. */
259 c->function = old->function;
260 c->prefixlist = old->prefixlist;
261 c->prefixname = old->prefixname;
262 c->allow_unknown = old->allow_unknown;
263 c->abbrev_flag = abbrev_flag;
264 c->cmd_pointer = old;
265 c->alias_chain = old->aliases;
270 /* Like add_cmd but adds an element for a command prefix:
271 a name that should be followed by a subcommand to be looked up
272 in another command list. PREFIXLIST should be the address
273 of the variable containing that list. */
275 struct cmd_list_element *
276 add_prefix_cmd (char *name, enum command_class class, void (*fun) (char *, int),
277 char *doc, struct cmd_list_element **prefixlist,
278 char *prefixname, int allow_unknown,
279 struct cmd_list_element **list)
281 struct cmd_list_element *c = add_cmd (name, class, fun, doc, list);
282 c->prefixlist = prefixlist;
283 c->prefixname = prefixname;
284 c->allow_unknown = allow_unknown;
288 /* Like add_prefix_cmd but sets the abbrev_flag on the new command. */
290 struct cmd_list_element *
291 add_abbrev_prefix_cmd (char *name, enum command_class class,
292 void (*fun) (char *, int), char *doc,
293 struct cmd_list_element **prefixlist, char *prefixname,
294 int allow_unknown, struct cmd_list_element **list)
296 struct cmd_list_element *c = add_cmd (name, class, fun, doc, list);
297 c->prefixlist = prefixlist;
298 c->prefixname = prefixname;
299 c->allow_unknown = allow_unknown;
304 /* This is an empty "cfunc". */
306 not_just_help_class_command (char *args, int from_tty)
310 /* This is an empty "sfunc". */
311 static void empty_sfunc (char *, int, struct cmd_list_element *);
314 empty_sfunc (char *args, int from_tty, struct cmd_list_element *c)
318 /* Add element named NAME to command list LIST (the list for set/show
319 or some sublist thereof).
320 TYPE is set_cmd or show_cmd.
321 CLASS is as in add_cmd.
322 VAR_TYPE is the kind of thing we are setting.
323 VAR is address of the variable being controlled by this command.
324 DOC is the documentation string. */
326 static struct cmd_list_element *
327 add_set_or_show_cmd (char *name,
329 enum command_class class,
333 struct cmd_list_element **list)
335 struct cmd_list_element *c = add_cmd (name, class, NULL, doc, list);
336 gdb_assert (type == set_cmd || type == show_cmd);
338 c->var_type = var_type;
340 /* This needs to be something besides NULL so that this isn't
341 treated as a help class. */
342 set_cmd_sfunc (c, empty_sfunc);
346 /* Add element named NAME to both the command SET_LIST and SHOW_LIST.
347 CLASS is as in add_cmd. VAR_TYPE is the kind of thing we are
348 setting. VAR is address of the variable being controlled by this
349 command. SET_FUNC and SHOW_FUNC are the callback functions (if
350 non-NULL). SET_DOC, SHOW_DOC and HELP_DOC are the documentation
351 strings. PRINT the format string to print the value. SET_RESULT
352 and SHOW_RESULT, if not NULL, are set to the resulting command
356 add_setshow_cmd_full (char *name,
357 enum command_class class,
358 var_types var_type, void *var,
359 const char *set_doc, const char *show_doc,
360 const char *help_doc,
361 cmd_sfunc_ftype *set_func,
362 show_value_ftype *show_func,
363 struct cmd_list_element **set_list,
364 struct cmd_list_element **show_list,
365 struct cmd_list_element **set_result,
366 struct cmd_list_element **show_result)
368 struct cmd_list_element *set;
369 struct cmd_list_element *show;
373 if (help_doc != NULL)
375 full_set_doc = xstrprintf ("%s\n%s", set_doc, help_doc);
376 full_show_doc = xstrprintf ("%s\n%s", show_doc, help_doc);
380 full_set_doc = xstrdup (set_doc);
381 full_show_doc = xstrdup (show_doc);
383 set = add_set_or_show_cmd (name, set_cmd, class, var_type, var,
384 full_set_doc, set_list);
385 if (set_func != NULL)
386 set_cmd_sfunc (set, set_func);
387 show = add_set_or_show_cmd (name, show_cmd, class, var_type, var,
388 full_show_doc, show_list);
389 show->show_value_func = show_func;
391 if (set_result != NULL)
393 if (show_result != NULL)
397 struct cmd_list_element *
398 deprecated_add_set_cmd (char *name,
399 enum command_class class,
403 struct cmd_list_element **list)
405 return add_set_or_show_cmd (name, set_cmd, class, var_type, var, doc, list);
408 /* Add element named NAME to command list LIST (the list for set or
409 some sublist thereof). CLASS is as in add_cmd. ENUMLIST is a list
410 of strings which may follow NAME. VAR is address of the variable
411 which will contain the matching string (from ENUMLIST). */
414 add_setshow_enum_cmd (char *name,
415 enum command_class class,
416 const char *enumlist[],
419 const char *show_doc,
420 const char *help_doc,
421 cmd_sfunc_ftype *set_func,
422 show_value_ftype *show_func,
423 struct cmd_list_element **set_list,
424 struct cmd_list_element **show_list)
426 struct cmd_list_element *c;
427 add_setshow_cmd_full (name, class, var_enum, var,
428 set_doc, show_doc, help_doc,
435 /* Add an auto-boolean command named NAME to both the set and show
436 command list lists. CLASS is as in add_cmd. VAR is address of the
437 variable which will contain the value. DOC is the documentation
438 string. FUNC is the corresponding callback. */
440 add_setshow_auto_boolean_cmd (char *name,
441 enum command_class class,
442 enum auto_boolean *var,
443 const char *set_doc, const char *show_doc,
444 const char *help_doc,
445 cmd_sfunc_ftype *set_func,
446 show_value_ftype *show_func,
447 struct cmd_list_element **set_list,
448 struct cmd_list_element **show_list)
450 static const char *auto_boolean_enums[] = { "on", "off", "auto", NULL };
451 struct cmd_list_element *c;
452 add_setshow_cmd_full (name, class, var_auto_boolean, var,
453 set_doc, show_doc, help_doc,
457 c->enums = auto_boolean_enums;
460 /* Add element named NAME to both the set and show command LISTs (the
461 list for set/show or some sublist thereof). CLASS is as in
462 add_cmd. VAR is address of the variable which will contain the
463 value. SET_DOC and SHOW_DOC are the documentation strings. */
465 add_setshow_boolean_cmd (char *name, enum command_class class, int *var,
466 const char *set_doc, const char *show_doc,
467 const char *help_doc,
468 cmd_sfunc_ftype *set_func,
469 show_value_ftype *show_func,
470 struct cmd_list_element **set_list,
471 struct cmd_list_element **show_list)
473 static const char *boolean_enums[] = { "on", "off", NULL };
474 struct cmd_list_element *c;
475 add_setshow_cmd_full (name, class, var_boolean, var,
476 set_doc, show_doc, help_doc,
480 c->enums = boolean_enums;
483 /* Add element named NAME to both the set and show command LISTs (the
484 list for set/show or some sublist thereof). */
486 add_setshow_filename_cmd (char *name, enum command_class class,
488 const char *set_doc, const char *show_doc,
489 const char *help_doc,
490 cmd_sfunc_ftype *set_func,
491 show_value_ftype *show_func,
492 struct cmd_list_element **set_list,
493 struct cmd_list_element **show_list)
495 struct cmd_list_element *set_result;
496 add_setshow_cmd_full (name, class, var_filename, var,
497 set_doc, show_doc, help_doc,
501 set_cmd_completer (set_result, filename_completer);
504 /* Add element named NAME to both the set and show command LISTs (the
505 list for set/show or some sublist thereof). */
507 add_setshow_string_cmd (char *name, enum command_class class,
509 const char *set_doc, const char *show_doc,
510 const char *help_doc,
511 cmd_sfunc_ftype *set_func,
512 show_value_ftype *show_func,
513 struct cmd_list_element **set_list,
514 struct cmd_list_element **show_list)
516 add_setshow_cmd_full (name, class, var_string, var,
517 set_doc, show_doc, help_doc,
523 /* Add element named NAME to both the set and show command LISTs (the
524 list for set/show or some sublist thereof). */
526 add_setshow_string_noescape_cmd (char *name, enum command_class class,
528 const char *set_doc, const char *show_doc,
529 const char *help_doc,
530 cmd_sfunc_ftype *set_func,
531 show_value_ftype *show_func,
532 struct cmd_list_element **set_list,
533 struct cmd_list_element **show_list)
535 add_setshow_cmd_full (name, class, var_string_noescape, var,
536 set_doc, show_doc, help_doc,
542 /* Add element named NAME to both the set and show command LISTs (the
543 list for set/show or some sublist thereof). */
545 add_setshow_optional_filename_cmd (char *name, enum command_class class,
547 const char *set_doc, const char *show_doc,
548 const char *help_doc,
549 cmd_sfunc_ftype *set_func,
550 show_value_ftype *show_func,
551 struct cmd_list_element **set_list,
552 struct cmd_list_element **show_list)
554 struct cmd_list_element *set_result;
556 add_setshow_cmd_full (name, class, var_optional_filename, var,
557 set_doc, show_doc, help_doc,
562 set_cmd_completer (set_result, filename_completer);
566 /* Add element named NAME to both the set and show command LISTs (the
567 list for set/show or some sublist thereof). CLASS is as in
568 add_cmd. VAR is address of the variable which will contain the
569 value. SET_DOC and SHOW_DOC are the documentation strings. */
571 add_setshow_integer_cmd (char *name, enum command_class class,
573 const char *set_doc, const char *show_doc,
574 const char *help_doc,
575 cmd_sfunc_ftype *set_func,
576 show_value_ftype *show_func,
577 struct cmd_list_element **set_list,
578 struct cmd_list_element **show_list)
580 add_setshow_cmd_full (name, class, var_integer, var,
581 set_doc, show_doc, help_doc,
587 /* Add element named NAME to both the set and show command LISTs (the
588 list for set/show or some sublist thereof). CLASS is as in
589 add_cmd. VAR is address of the variable which will contain the
590 value. SET_DOC and SHOW_DOC are the documentation strings. */
592 add_setshow_uinteger_cmd (char *name, enum command_class class,
594 const char *set_doc, const char *show_doc,
595 const char *help_doc,
596 cmd_sfunc_ftype *set_func,
597 show_value_ftype *show_func,
598 struct cmd_list_element **set_list,
599 struct cmd_list_element **show_list)
601 add_setshow_cmd_full (name, class, var_uinteger, var,
602 set_doc, show_doc, help_doc,
608 /* Add element named NAME to both the set and show command LISTs (the
609 list for set/show or some sublist thereof). CLASS is as in
610 add_cmd. VAR is address of the variable which will contain the
611 value. SET_DOC and SHOW_DOC are the documentation strings. */
613 add_setshow_zinteger_cmd (char *name, enum command_class class,
615 const char *set_doc, const char *show_doc,
616 const char *help_doc,
617 cmd_sfunc_ftype *set_func,
618 show_value_ftype *show_func,
619 struct cmd_list_element **set_list,
620 struct cmd_list_element **show_list)
622 add_setshow_cmd_full (name, class, var_zinteger, var,
623 set_doc, show_doc, help_doc,
629 /* Remove the command named NAME from the command list. Return the
630 list commands which were aliased to the deleted command. If the
631 command had no aliases, return NULL. */
633 static struct cmd_list_element *
634 delete_cmd (char *name, struct cmd_list_element **list)
636 struct cmd_list_element *iter;
637 struct cmd_list_element **previous_chain_ptr;
638 struct cmd_list_element *aliases = NULL;
640 previous_chain_ptr = list;
642 for (iter = *previous_chain_ptr; iter; iter = *previous_chain_ptr)
644 if (strcmp (iter->name, name) == 0)
646 if (iter->hookee_pre)
647 iter->hookee_pre->hook_pre = 0;
648 if (iter->hookee_post)
649 iter->hookee_post->hook_post = 0;
651 /* Update the link. */
652 *previous_chain_ptr = iter->next;
654 aliases = iter->aliases;
656 /* If this command was an alias, remove it from the list of
658 if (iter->cmd_pointer)
660 struct cmd_list_element **prevp = &iter->cmd_pointer->aliases;
661 struct cmd_list_element *a = *prevp;
665 prevp = &a->alias_chain;
668 *prevp = iter->alias_chain;
673 /* We won't see another command with the same name. */
677 previous_chain_ptr = &iter->next;
683 /* Shorthands to the commands above. */
685 /* Add an element to the list of info subcommands. */
687 struct cmd_list_element *
688 add_info (char *name, void (*fun) (char *, int), char *doc)
690 return add_cmd (name, no_class, fun, doc, &infolist);
693 /* Add an alias to the list of info subcommands. */
695 struct cmd_list_element *
696 add_info_alias (char *name, char *oldname, int abbrev_flag)
698 return add_alias_cmd (name, oldname, 0, abbrev_flag, &infolist);
701 /* Add an element to the list of commands. */
703 struct cmd_list_element *
704 add_com (char *name, enum command_class class, void (*fun) (char *, int),
707 return add_cmd (name, class, fun, doc, &cmdlist);
710 /* Add an alias or abbreviation command to the list of commands. */
712 struct cmd_list_element *
713 add_com_alias (char *name, char *oldname, enum command_class class,
716 return add_alias_cmd (name, oldname, class, abbrev_flag, &cmdlist);
719 /* Recursively walk the commandlist structures, and print out the
720 documentation of commands that match our regex in either their
721 name, or their documentation.
724 apropos_cmd (struct ui_file *stream, struct cmd_list_element *commandlist,
725 struct re_pattern_buffer *regex, char *prefix)
727 struct cmd_list_element *c;
728 int returnvalue=1; /*Needed to avoid double printing*/
729 /* Walk through the commands */
730 for (c=commandlist;c;c=c->next)
734 /* Try to match against the name*/
735 returnvalue=re_search(regex,c->name,strlen(c->name),0,strlen(c->name),NULL);
736 if (returnvalue >= 0)
738 print_help_for_command (c, prefix,
739 0 /* don't recurse */, stream);
742 if (c->doc != NULL && returnvalue != 0)
744 /* Try to match against documentation */
745 if (re_search(regex,c->doc,strlen(c->doc),0,strlen(c->doc),NULL) >=0)
747 print_help_for_command (c, prefix,
748 0 /* don't recurse */, stream);
751 /* Check if this command has subcommands */
752 if (c->prefixlist != NULL)
754 /* Recursively call ourselves on the subcommand list,
755 passing the right prefix in.
757 apropos_cmd (stream,*c->prefixlist,regex,c->prefixname);
762 /* This command really has to deal with two things:
763 * 1) I want documentation on *this string* (usually called by
764 * "help commandname").
765 * 2) I want documentation on *this list* (usually called by
766 * giving a command that requires subcommands. Also called by saying
769 * I am going to split this into two seperate comamnds, help_cmd and
774 help_cmd (char *command, struct ui_file *stream)
776 struct cmd_list_element *c;
777 extern struct cmd_list_element *cmdlist;
781 help_list (cmdlist, "", all_classes, stream);
785 if (strcmp (command, "all") == 0)
791 c = lookup_cmd (&command, cmdlist, "", 0, 0);
796 /* There are three cases here.
797 If c->prefixlist is nonzero, we have a prefix command.
798 Print its documentation, then list its subcommands.
800 If c->func is non NULL, we really have a command. Print its
801 documentation and return.
803 If c->func is NULL, we have a class name. Print its
804 documentation (as if it were a command) and then set class to the
805 number of this class so that the commands in the class will be
808 fputs_filtered (c->doc, stream);
809 fputs_filtered ("\n", stream);
811 if (c->prefixlist == 0 && c->func != NULL)
813 fprintf_filtered (stream, "\n");
815 /* If this is a prefix command, print it's subcommands */
817 help_list (*c->prefixlist, c->prefixname, all_commands, stream);
819 /* If this is a class name, print all of the commands in the class */
821 help_list (cmdlist, "", c->class, stream);
823 if (c->hook_pre || c->hook_post)
824 fprintf_filtered (stream,
825 "\nThis command has a hook (or hooks) defined:\n");
828 fprintf_filtered (stream,
829 "\tThis command is run after : %s (pre hook)\n",
832 fprintf_filtered (stream,
833 "\tThis command is run before : %s (post hook)\n",
838 * Get a specific kind of help on a command list.
841 * CMDTYPE is the prefix to use in the title string.
842 * CLASS is the class with which to list the nodes of this list (see
843 * documentation for help_cmd_list below), As usual, ALL_COMMANDS for
844 * everything, ALL_CLASSES for just classes, and non-negative for only things
845 * in a specific class.
846 * and STREAM is the output stream on which to print things.
847 * If you call this routine with a class >= 0, it recurses.
850 help_list (struct cmd_list_element *list, char *cmdtype,
851 enum command_class class, struct ui_file *stream)
854 char *cmdtype1, *cmdtype2;
856 /* If CMDTYPE is "foo ", CMDTYPE1 gets " foo" and CMDTYPE2 gets "foo sub" */
857 len = strlen (cmdtype);
858 cmdtype1 = (char *) alloca (len + 1);
860 cmdtype2 = (char *) alloca (len + 4);
865 strncpy (cmdtype1 + 1, cmdtype, len - 1);
867 strncpy (cmdtype2, cmdtype, len - 1);
868 strcpy (cmdtype2 + len - 1, " sub");
871 if (class == all_classes)
872 fprintf_filtered (stream, "List of classes of %scommands:\n\n", cmdtype2);
874 fprintf_filtered (stream, "List of %scommands:\n\n", cmdtype2);
876 help_cmd_list (list, class, cmdtype, (int) class >= 0, stream);
878 if (class == all_classes)
880 fprintf_filtered (stream, "\n\
881 Type \"help%s\" followed by a class name for a list of commands in ",
884 fprintf_filtered (stream, "that class.");
886 fprintf_filtered (stream, "\n\
887 Type \"help all\" for the list of all commands.");
890 fprintf_filtered (stream, "\nType \"help%s\" followed by %scommand name ",
893 fputs_filtered ("for ", stream);
895 fputs_filtered ("full ", stream);
897 fputs_filtered ("documentation.\n", stream);
898 fputs_filtered ("Type \"apropos word\" to search "
899 "for commands related to \"word\".\n", stream);
900 fputs_filtered ("Command name abbreviations are allowed if unambiguous.\n",
905 help_all (struct ui_file *stream)
907 struct cmd_list_element *c;
908 extern struct cmd_list_element *cmdlist;
909 int seen_unclassified = 0;
911 for (c = cmdlist; c; c = c->next)
915 /* If this is a class name, print all of the commands in the class */
919 fprintf_filtered (stream, "\nCommand class: %s\n\n", c->name);
920 help_cmd_list (cmdlist, c->class, "", 1, stream);
924 /* While it's expected that all commands are in some class,
925 as a safety measure, we'll print commands outside of any
928 for (c = cmdlist; c; c = c->next)
933 if (c->class == no_class)
935 if (!seen_unclassified)
937 fprintf_filtered (stream, "\nUnclassified commands\n\n");
938 seen_unclassified = 1;
940 print_help_for_command (c, "", 1, stream);
946 /* Print only the first line of STR on STREAM. */
948 print_doc_line (struct ui_file *stream, char *str)
950 static char *line_buffer = 0;
951 static int line_size;
957 line_buffer = (char *) xmalloc (line_size);
961 while (*p && *p != '\n' && *p != '.' && *p != ',')
963 if (p - str > line_size - 1)
965 line_size = p - str + 1;
967 line_buffer = (char *) xmalloc (line_size);
969 strncpy (line_buffer, str, p - str);
970 line_buffer[p - str] = '\0';
971 if (islower (line_buffer[0]))
972 line_buffer[0] = toupper (line_buffer[0]);
973 ui_out_text (uiout, line_buffer);
976 /* Print one-line help for command C.
977 If RECURSE is non-zero, also print one-line descriptions
978 of all prefixed subcommands. */
980 print_help_for_command (struct cmd_list_element *c, char *prefix, int recurse,
981 struct ui_file *stream)
983 fprintf_filtered (stream, "%s%s -- ", prefix, c->name);
984 print_doc_line (stream, c->doc);
985 fputs_filtered ("\n", stream);
988 && c->prefixlist != 0
989 && c->abbrev_flag == 0)
990 /* Subcommands of a prefix command typically have 'all_commands'
991 as class. If we pass CLASS to recursive invocation,
992 most often we won't see anything. */
993 help_cmd_list (*c->prefixlist, all_commands, c->prefixname, 1, stream);
997 * Implement a help command on command list LIST.
998 * RECURSE should be non-zero if this should be done recursively on
999 * all sublists of LIST.
1000 * PREFIX is the prefix to print before each command name.
1001 * STREAM is the stream upon which the output should be written.
1003 * A non-negative class number to list only commands in that
1005 * ALL_COMMANDS to list all commands in list.
1006 * ALL_CLASSES to list all classes in list.
1008 * Note that RECURSE will be active on *all* sublists, not just the
1009 * ones selected by the criteria above (ie. the selection mechanism
1010 * is at the low level, not the high-level).
1013 help_cmd_list (struct cmd_list_element *list, enum command_class class,
1014 char *prefix, int recurse, struct ui_file *stream)
1016 struct cmd_list_element *c;
1018 for (c = list; c; c = c->next)
1020 if (c->abbrev_flag == 0 &&
1021 (class == all_commands
1022 || (class == all_classes && c->func == NULL)
1023 || (class == c->class && c->func != NULL)))
1025 print_help_for_command (c, prefix, recurse, stream);
1031 /* Search the input clist for 'command'. Return the command if
1032 found (or NULL if not), and return the number of commands
1035 static struct cmd_list_element *
1036 find_cmd (char *command, int len, struct cmd_list_element *clist,
1037 int ignore_help_classes, int *nfound)
1039 struct cmd_list_element *found, *c;
1041 found = (struct cmd_list_element *) NULL;
1043 for (c = clist; c; c = c->next)
1044 if (!strncmp (command, c->name, len)
1045 && (!ignore_help_classes || c->func))
1049 if (c->name[len] == '\0')
1059 find_command_name_length (const char *text)
1061 const char *p = text;
1063 /* Treating underscores as part of command words is important
1064 so that "set args_foo()" doesn't get interpreted as
1065 "set args _foo()". */
1066 /* Some characters are only used for TUI specific commands. However, they
1067 are always allowed for the sake of consistency.
1068 The XDB compatibility characters are only allowed when using the right
1069 mode because they clash with other GDB commands - specifically '/' is
1070 used as a suffix for print, examine and display.
1071 Note that this is larger than the character set allowed when creating
1072 user-defined commands. */
1073 while (isalnum (*p) || *p == '-' || *p == '_' ||
1074 /* Characters used by TUI specific commands. */
1075 *p == '+' || *p == '<' || *p == '>' || *p == '$' ||
1076 /* Characters used for XDB compatibility. */
1077 (xdb_commands && (*p == '!' || *p == '/' || *p == '?')))
1083 /* This routine takes a line of TEXT and a CLIST in which to start the
1084 lookup. When it returns it will have incremented the text pointer past
1085 the section of text it matched, set *RESULT_LIST to point to the list in
1086 which the last word was matched, and will return a pointer to the cmd
1087 list element which the text matches. It will return NULL if no match at
1088 all was possible. It will return -1 (cast appropriately, ick) if ambigous
1089 matches are possible; in this case *RESULT_LIST will be set to point to
1090 the list in which there are ambiguous choices (and *TEXT will be set to
1091 the ambiguous text string).
1093 If the located command was an abbreviation, this routine returns the base
1094 command of the abbreviation.
1096 It does no error reporting whatsoever; control will always return
1097 to the superior routine.
1099 In the case of an ambiguous return (-1), *RESULT_LIST will be set to point
1100 at the prefix_command (ie. the best match) *or* (special case) will be NULL
1101 if no prefix command was ever found. For example, in the case of "info a",
1102 "info" matches without ambiguity, but "a" could be "args" or "address", so
1103 *RESULT_LIST is set to the cmd_list_element for "info". So in this case
1104 RESULT_LIST should not be interpeted as a pointer to the beginning of a
1105 list; it simply points to a specific command. In the case of an ambiguous
1106 return *TEXT is advanced past the last non-ambiguous prefix (e.g.
1107 "info t" can be "info types" or "info target"; upon return *TEXT has been
1108 advanced past "info ").
1110 If RESULT_LIST is NULL, don't set *RESULT_LIST (but don't otherwise
1111 affect the operation).
1113 This routine does *not* modify the text pointed to by TEXT.
1115 If IGNORE_HELP_CLASSES is nonzero, ignore any command list elements which
1116 are actually help classes rather than commands (i.e. the function field of
1117 the struct cmd_list_element is NULL). */
1119 struct cmd_list_element *
1120 lookup_cmd_1 (char **text, struct cmd_list_element *clist,
1121 struct cmd_list_element **result_list, int ignore_help_classes)
1124 int len, tmp, nfound;
1125 struct cmd_list_element *found, *c;
1128 while (**text == ' ' || **text == '\t')
1131 /* Identify the name of the command. */
1132 len = find_command_name_length (*text);
1134 /* If nothing but whitespace, return 0. */
1138 /* *text and p now bracket the first command word to lookup (and
1139 it's length is len). We copy this into a local temporary */
1142 command = (char *) alloca (len + 1);
1143 memcpy (command, *text, len);
1144 command[len] = '\0';
1149 found = find_cmd (command, len, clist, ignore_help_classes, &nfound);
1152 ** We didn't find the command in the entered case, so lower case it
1153 ** and search again.
1155 if (!found || nfound == 0)
1157 for (tmp = 0; tmp < len; tmp++)
1159 char x = command[tmp];
1160 command[tmp] = isupper (x) ? tolower (x) : x;
1162 found = find_cmd (command, len, clist, ignore_help_classes, &nfound);
1165 /* If nothing matches, we have a simple failure. */
1171 if (result_list != NULL)
1172 /* Will be modified in calling routine
1173 if we know what the prefix command is. */
1175 return (struct cmd_list_element *) -1; /* Ambiguous. */
1178 /* We've matched something on this list. Move text pointer forward. */
1182 if (found->cmd_pointer)
1184 /* We drop the alias (abbreviation) in favor of the command it is
1185 pointing to. If the alias is deprecated, though, we need to
1186 warn the user about it before we drop it. Note that while we
1187 are warning about the alias, we may also warn about the command
1188 itself and we will adjust the appropriate DEPRECATED_WARN_USER
1191 if (found->flags & DEPRECATED_WARN_USER)
1192 deprecated_cmd_warning (&line);
1193 found = found->cmd_pointer;
1195 /* If we found a prefix command, keep looking. */
1197 if (found->prefixlist)
1199 c = lookup_cmd_1 (text, *found->prefixlist, result_list,
1200 ignore_help_classes);
1203 /* Didn't find anything; this is as far as we got. */
1204 if (result_list != NULL)
1205 *result_list = clist;
1208 else if (c == (struct cmd_list_element *) -1)
1210 /* We've gotten this far properly, but the next step
1211 is ambiguous. We need to set the result list to the best
1212 we've found (if an inferior hasn't already set it). */
1213 if (result_list != NULL)
1215 /* This used to say *result_list = *found->prefixlist
1216 If that was correct, need to modify the documentation
1217 at the top of this function to clarify what is supposed
1219 *result_list = found;
1230 if (result_list != NULL)
1231 *result_list = clist;
1236 /* All this hair to move the space to the front of cmdtype */
1239 undef_cmd_error (char *cmdtype, char *q)
1241 error (_("Undefined %scommand: \"%s\". Try \"help%s%.*s\"."),
1244 *cmdtype ? " " : "",
1245 (int) strlen (cmdtype) - 1,
1249 /* Look up the contents of *LINE as a command in the command list LIST.
1250 LIST is a chain of struct cmd_list_element's.
1251 If it is found, return the struct cmd_list_element for that command
1252 and update *LINE to point after the command name, at the first argument.
1253 If not found, call error if ALLOW_UNKNOWN is zero
1254 otherwise (or if error returns) return zero.
1255 Call error if specified command is ambiguous,
1256 unless ALLOW_UNKNOWN is negative.
1257 CMDTYPE precedes the word "command" in the error message.
1259 If INGNORE_HELP_CLASSES is nonzero, ignore any command list
1260 elements which are actually help classes rather than commands (i.e.
1261 the function field of the struct cmd_list_element is 0). */
1263 struct cmd_list_element *
1264 lookup_cmd (char **line, struct cmd_list_element *list, char *cmdtype,
1265 int allow_unknown, int ignore_help_classes)
1267 struct cmd_list_element *last_list = 0;
1268 struct cmd_list_element *c;
1270 /* Note: Do not remove trailing whitespace here because this
1271 would be wrong for complete_command. Jim Kingdon */
1274 error (_("Lack of needed %scommand"), cmdtype);
1276 c = lookup_cmd_1 (line, list, &last_list, ignore_help_classes);
1283 int len = find_command_name_length (*line);
1285 q = (char *) alloca (len + 1);
1286 strncpy (q, *line, len);
1288 undef_cmd_error (cmdtype, q);
1293 else if (c == (struct cmd_list_element *) -1)
1295 /* Ambigous. Local values should be off prefixlist or called
1297 int local_allow_unknown = (last_list ? last_list->allow_unknown :
1299 char *local_cmdtype = last_list ? last_list->prefixname : cmdtype;
1300 struct cmd_list_element *local_list =
1301 (last_list ? *(last_list->prefixlist) : list);
1303 if (local_allow_unknown < 0)
1306 return last_list; /* Found something. */
1308 return 0; /* Found nothing. */
1312 /* Report as error. */
1317 ((*line)[amb_len] && (*line)[amb_len] != ' '
1318 && (*line)[amb_len] != '\t');
1323 for (c = local_list; c; c = c->next)
1324 if (!strncmp (*line, c->name, amb_len))
1326 if (strlen (ambbuf) + strlen (c->name) + 6 < (int) sizeof ambbuf)
1328 if (strlen (ambbuf))
1329 strcat (ambbuf, ", ");
1330 strcat (ambbuf, c->name);
1334 strcat (ambbuf, "..");
1338 error (_("Ambiguous %scommand \"%s\": %s."), local_cmdtype,
1340 return 0; /* lint */
1345 /* We've got something. It may still not be what the caller
1346 wants (if this command *needs* a subcommand). */
1347 while (**line == ' ' || **line == '\t')
1350 if (c->prefixlist && **line && !c->allow_unknown)
1351 undef_cmd_error (c->prefixname, *line);
1353 /* Seems to be what he wants. Return it. */
1359 /* We are here presumably because an alias or command in *TEXT is
1360 deprecated and a warning message should be generated. This function
1361 decodes *TEXT and potentially generates a warning message as outlined
1364 Example for 'set endian big' which has a fictitious alias 'seb'.
1366 If alias wasn't used in *TEXT, and the command is deprecated:
1367 "warning: 'set endian big' is deprecated."
1369 If alias was used, and only the alias is deprecated:
1370 "warning: 'seb' an alias for the command 'set endian big' is deprecated."
1372 If alias was used and command is deprecated (regardless of whether the
1373 alias itself is deprecated:
1375 "warning: 'set endian big' (seb) is deprecated."
1377 After the message has been sent, clear the appropriate flags in the
1378 command and/or the alias so the user is no longer bothered.
1382 deprecated_cmd_warning (char **text)
1384 struct cmd_list_element *alias = NULL;
1385 struct cmd_list_element *prefix_cmd = NULL;
1386 struct cmd_list_element *cmd = NULL;
1387 struct cmd_list_element *c;
1390 if (!lookup_cmd_composition (*text, &alias, &prefix_cmd, &cmd))
1391 /* return if text doesn't evaluate to a command */
1394 if (!((alias ? (alias->flags & DEPRECATED_WARN_USER) : 0)
1395 || (cmd->flags & DEPRECATED_WARN_USER) ) )
1396 /* return if nothing is deprecated */
1399 printf_filtered ("Warning:");
1401 if (alias && !(cmd->flags & CMD_DEPRECATED))
1402 printf_filtered (" '%s', an alias for the", alias->name);
1404 printf_filtered (" command '");
1407 printf_filtered ("%s", prefix_cmd->prefixname);
1409 printf_filtered ("%s", cmd->name);
1411 if (alias && (cmd->flags & CMD_DEPRECATED))
1412 printf_filtered ("' (%s) is deprecated.\n", alias->name);
1414 printf_filtered ("' is deprecated.\n");
1417 /* if it is only the alias that is deprecated, we want to indicate the
1418 new alias, otherwise we'll indicate the new command */
1420 if (alias && !(cmd->flags & CMD_DEPRECATED))
1422 if (alias->replacement)
1423 printf_filtered ("Use '%s'.\n\n", alias->replacement);
1425 printf_filtered ("No alternative known.\n\n");
1429 if (cmd->replacement)
1430 printf_filtered ("Use '%s'.\n\n", cmd->replacement);
1432 printf_filtered ("No alternative known.\n\n");
1435 /* We've warned you, now we'll keep quiet */
1437 alias->flags &= ~DEPRECATED_WARN_USER;
1439 cmd->flags &= ~DEPRECATED_WARN_USER;
1444 /* Look up the contents of LINE as a command in the command list 'cmdlist'.
1445 Return 1 on success, 0 on failure.
1447 If LINE refers to an alias, *alias will point to that alias.
1449 If LINE is a postfix command (i.e. one that is preceeded by a prefix
1450 command) set *prefix_cmd.
1452 Set *cmd to point to the command LINE indicates.
1454 If any of *alias, *prefix_cmd, or *cmd cannot be determined or do not
1455 exist, they are NULL when we return.
1459 lookup_cmd_composition (char *text,
1460 struct cmd_list_element **alias,
1461 struct cmd_list_element **prefix_cmd,
1462 struct cmd_list_element **cmd)
1465 int len, tmp, nfound;
1466 struct cmd_list_element *cur_list;
1467 struct cmd_list_element *prev_cmd;
1476 /* Go through as many command lists as we need to
1477 to find the command TEXT refers to. */
1481 while (*text == ' ' || *text == '\t')
1484 /* Identify the name of the command. */
1485 len = find_command_name_length (text);
1487 /* If nothing but whitespace, return. */
1491 /* text is the start of the first command word to lookup (and
1492 it's length is len). We copy this into a local temporary */
1494 command = (char *) alloca (len + 1);
1495 memcpy (command, text, len);
1496 command[len] = '\0';
1501 *cmd = find_cmd (command, len, cur_list, 1, &nfound);
1503 /* We didn't find the command in the entered case, so lower case it
1506 if (!*cmd || nfound == 0)
1508 for (tmp = 0; tmp < len; tmp++)
1510 char x = command[tmp];
1511 command[tmp] = isupper (x) ? tolower (x) : x;
1513 *cmd = find_cmd (command, len, cur_list, 1, &nfound);
1516 if (*cmd == (struct cmd_list_element *) -1)
1518 return 0; /* ambiguous */
1522 return 0; /* nothing found */
1525 if ((*cmd)->cmd_pointer)
1527 /* cmd was actually an alias, we note that an alias was used
1528 (by assigning *alais) and we set *cmd.
1531 *cmd = (*cmd)->cmd_pointer;
1533 *prefix_cmd = prev_cmd;
1535 if ((*cmd)->prefixlist)
1536 cur_list = *(*cmd)->prefixlist;
1544 /* Helper function for SYMBOL_COMPLETION_FUNCTION. */
1546 /* Return a vector of char pointers which point to the different
1547 possible completions in LIST of TEXT.
1549 WORD points in the same buffer as TEXT, and completions should be
1550 returned relative to this position. For example, suppose TEXT is "foo"
1551 and we want to complete to "foobar". If WORD is "oo", return
1552 "oobar"; if WORD is "baz/foo", return "baz/foobar". */
1555 complete_on_cmdlist (struct cmd_list_element *list, char *text, char *word)
1557 struct cmd_list_element *ptr;
1559 int sizeof_matchlist;
1561 int textlen = strlen (text);
1563 sizeof_matchlist = 10;
1564 matchlist = (char **) xmalloc (sizeof_matchlist * sizeof (char *));
1567 for (ptr = list; ptr; ptr = ptr->next)
1568 if (!strncmp (ptr->name, text, textlen)
1569 && !ptr->abbrev_flag
1571 || ptr->prefixlist))
1573 if (matches == sizeof_matchlist)
1575 sizeof_matchlist *= 2;
1576 matchlist = (char **) xrealloc ((char *) matchlist,
1578 * sizeof (char *)));
1581 matchlist[matches] = (char *)
1582 xmalloc (strlen (word) + strlen (ptr->name) + 1);
1584 strcpy (matchlist[matches], ptr->name);
1585 else if (word > text)
1587 /* Return some portion of ptr->name. */
1588 strcpy (matchlist[matches], ptr->name + (word - text));
1592 /* Return some of text plus ptr->name. */
1593 strncpy (matchlist[matches], word, text - word);
1594 matchlist[matches][text - word] = '\0';
1595 strcat (matchlist[matches], ptr->name);
1607 matchlist = (char **) xrealloc ((char *) matchlist, ((matches + 1)
1608 * sizeof (char *)));
1609 matchlist[matches] = (char *) 0;
1615 /* Helper function for SYMBOL_COMPLETION_FUNCTION. */
1617 /* Return a vector of char pointers which point to the different
1618 possible completions in CMD of TEXT.
1620 WORD points in the same buffer as TEXT, and completions should be
1621 returned relative to this position. For example, suppose TEXT is "foo"
1622 and we want to complete to "foobar". If WORD is "oo", return
1623 "oobar"; if WORD is "baz/foo", return "baz/foobar". */
1626 complete_on_enum (const char *enumlist[],
1631 int sizeof_matchlist;
1633 int textlen = strlen (text);
1637 sizeof_matchlist = 10;
1638 matchlist = (char **) xmalloc (sizeof_matchlist * sizeof (char *));
1641 for (i = 0; (name = enumlist[i]) != NULL; i++)
1642 if (strncmp (name, text, textlen) == 0)
1644 if (matches == sizeof_matchlist)
1646 sizeof_matchlist *= 2;
1647 matchlist = (char **) xrealloc ((char *) matchlist,
1649 * sizeof (char *)));
1652 matchlist[matches] = (char *)
1653 xmalloc (strlen (word) + strlen (name) + 1);
1655 strcpy (matchlist[matches], name);
1656 else if (word > text)
1658 /* Return some portion of name. */
1659 strcpy (matchlist[matches], name + (word - text));
1663 /* Return some of text plus name. */
1664 strncpy (matchlist[matches], word, text - word);
1665 matchlist[matches][text - word] = '\0';
1666 strcat (matchlist[matches], name);
1678 matchlist = (char **) xrealloc ((char *) matchlist, ((matches + 1)
1679 * sizeof (char *)));
1680 matchlist[matches] = (char *) 0;
1687 /* check function pointer */
1689 cmd_func_p (struct cmd_list_element *cmd)
1691 return (cmd->func != NULL);
1695 /* call the command function */
1697 cmd_func (struct cmd_list_element *cmd, char *args, int from_tty)
1699 if (cmd_func_p (cmd))
1700 (*cmd->func) (cmd, args, from_tty);
1702 error (_("Invalid command"));