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, 2009, 2010 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,
42 struct cmd_list_element **prehook,
43 struct cmd_list_element **prehookee,
44 struct cmd_list_element **posthook,
45 struct cmd_list_element **posthookee);
47 static struct cmd_list_element *find_cmd (char *command,
49 struct cmd_list_element *clist,
50 int ignore_help_classes,
53 static void help_all (struct ui_file *stream);
56 print_help_for_command (struct cmd_list_element *c, char *prefix, int recurse,
57 struct ui_file *stream);
60 /* Set the callback function for the specified command. For each both
61 the commands callback and func() are set. The latter set to a
62 bounce function (unless cfunc / sfunc is NULL that is). */
65 do_cfunc (struct cmd_list_element *c, char *args, int from_tty)
67 c->function.cfunc (args, from_tty); /* Ok. */
71 set_cmd_cfunc (struct cmd_list_element *cmd, cmd_cfunc_ftype *cfunc)
77 cmd->function.cfunc = cfunc; /* Ok. */
81 do_sfunc (struct cmd_list_element *c, char *args, int from_tty)
83 c->function.sfunc (args, from_tty, c); /* Ok. */
87 set_cmd_sfunc (struct cmd_list_element *cmd, cmd_sfunc_ftype *sfunc)
93 cmd->function.sfunc = sfunc; /* Ok. */
97 cmd_cfunc_eq (struct cmd_list_element *cmd,
98 void (*cfunc) (char *args, int from_tty))
100 return cmd->func == do_cfunc && cmd->function.cfunc == cfunc;
104 set_cmd_context (struct cmd_list_element *cmd, void *context)
106 cmd->context = context;
110 get_cmd_context (struct cmd_list_element *cmd)
116 cmd_type (struct cmd_list_element *cmd)
122 set_cmd_completer (struct cmd_list_element *cmd,
123 char **(*completer) (struct cmd_list_element *self,
124 char *text, char *word))
126 cmd->completer = completer; /* Ok. */
130 /* Add element named NAME.
131 CLASS is the top level category into which commands are broken down
133 FUN should be the function to execute the command;
134 it will get a character string as argument, with leading
135 and trailing blanks already eliminated.
137 DOC is a documentation string for the command.
138 Its first line should be a complete sentence.
139 It should start with ? for a command that is an abbreviation
140 or with * for a command that most users don't need to know about.
142 Add this command to command list *LIST.
144 Returns a pointer to the added command (not necessarily the head
147 struct cmd_list_element *
148 add_cmd (char *name, enum command_class class, void (*fun) (char *, int),
149 char *doc, struct cmd_list_element **list)
151 struct cmd_list_element *c
152 = (struct cmd_list_element *) xmalloc (sizeof (struct cmd_list_element));
153 struct cmd_list_element *p, *iter;
155 /* Turn each alias of the old command into an alias of the new
157 c->aliases = delete_cmd (name, list, &c->hook_pre, &c->hookee_pre,
158 &c->hook_post, &c->hookee_post);
159 for (iter = c->aliases; iter; iter = iter->alias_chain)
160 iter->cmd_pointer = c;
162 c->hook_pre->hookee_pre = c;
164 c->hookee_pre->hook_pre = c;
166 c->hook_post->hookee_post = c;
168 c->hookee_post->hook_post = c;
170 if (*list == NULL || strcmp ((*list)->name, name) >= 0)
178 while (p->next && strcmp (p->next->name, name) <= 0)
188 set_cmd_cfunc (c, fun);
189 set_cmd_context (c, NULL);
192 c->replacement = NULL;
193 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_fn);
201 c->type = not_set_cmd;
203 c->var_type = var_boolean;
205 c->user_commands = NULL;
206 c->cmd_pointer = NULL;
207 c->alias_chain = NULL;
212 /* Deprecates a command CMD.
213 REPLACEMENT is the name of the command which should be used in
214 place of this command, or NULL if no such command exists.
216 This function does not check to see if command REPLACEMENT exists
217 since gdb may not have gotten around to adding REPLACEMENT when
218 this function is called.
220 Returns a pointer to the deprecated command. */
222 struct cmd_list_element *
223 deprecate_cmd (struct cmd_list_element *cmd, char *replacement)
225 cmd->flags |= (CMD_DEPRECATED | DEPRECATED_WARN_USER);
227 if (replacement != NULL)
228 cmd->replacement = replacement;
230 cmd->replacement = NULL;
235 struct cmd_list_element *
236 add_alias_cmd (char *name, char *oldname, enum command_class class,
237 int abbrev_flag, struct cmd_list_element **list)
239 /* Must do this since lookup_cmd tries to side-effect its first
242 struct cmd_list_element *old;
243 struct cmd_list_element *c;
245 copied_name = (char *) alloca (strlen (oldname) + 1);
246 strcpy (copied_name, oldname);
247 old = lookup_cmd (&copied_name, *list, "", 1, 1);
251 struct cmd_list_element *prehook, *prehookee, *posthook, *posthookee;
252 struct cmd_list_element *aliases = delete_cmd (name, list,
253 &prehook, &prehookee,
254 &posthook, &posthookee);
256 /* If this happens, it means a programmer error somewhere. */
257 gdb_assert (!aliases && !prehook && !prehookee
258 && !posthook && ! posthookee);
262 c = add_cmd (name, class, NULL, old->doc, list);
263 /* NOTE: Both FUNC and all the FUNCTIONs need to be copied. */
265 c->function = old->function;
266 c->prefixlist = old->prefixlist;
267 c->prefixname = old->prefixname;
268 c->allow_unknown = old->allow_unknown;
269 c->abbrev_flag = abbrev_flag;
270 c->cmd_pointer = old;
271 c->alias_chain = old->aliases;
276 /* Like add_cmd but adds an element for a command prefix: a name that
277 should be followed by a subcommand to be looked up in another
278 command list. PREFIXLIST should be the address of the variable
279 containing that list. */
281 struct cmd_list_element *
282 add_prefix_cmd (char *name, enum command_class class, void (*fun) (char *, int),
283 char *doc, struct cmd_list_element **prefixlist,
284 char *prefixname, int allow_unknown,
285 struct cmd_list_element **list)
287 struct cmd_list_element *c = add_cmd (name, class, fun, doc, list);
289 c->prefixlist = prefixlist;
290 c->prefixname = prefixname;
291 c->allow_unknown = allow_unknown;
295 /* Like add_prefix_cmd but sets the abbrev_flag on the new command. */
297 struct cmd_list_element *
298 add_abbrev_prefix_cmd (char *name, enum command_class class,
299 void (*fun) (char *, int), char *doc,
300 struct cmd_list_element **prefixlist, char *prefixname,
301 int allow_unknown, struct cmd_list_element **list)
303 struct cmd_list_element *c = add_cmd (name, class, fun, doc, list);
305 c->prefixlist = prefixlist;
306 c->prefixname = prefixname;
307 c->allow_unknown = allow_unknown;
312 /* This is an empty "cfunc". */
314 not_just_help_class_command (char *args, int from_tty)
318 /* This is an empty "sfunc". */
319 static void empty_sfunc (char *, int, struct cmd_list_element *);
322 empty_sfunc (char *args, int from_tty, struct cmd_list_element *c)
326 /* Add element named NAME to command list LIST (the list for set/show
327 or some sublist thereof).
328 TYPE is set_cmd or show_cmd.
329 CLASS is as in add_cmd.
330 VAR_TYPE is the kind of thing we are setting.
331 VAR is address of the variable being controlled by this command.
332 DOC is the documentation string. */
334 static struct cmd_list_element *
335 add_set_or_show_cmd (char *name,
337 enum command_class class,
341 struct cmd_list_element **list)
343 struct cmd_list_element *c = add_cmd (name, class, NULL, doc, list);
345 gdb_assert (type == set_cmd || type == show_cmd);
347 c->var_type = var_type;
349 /* This needs to be something besides NULL so that this isn't
350 treated as a help class. */
351 set_cmd_sfunc (c, empty_sfunc);
355 /* Add element named NAME to both the command SET_LIST and SHOW_LIST.
356 CLASS is as in add_cmd. VAR_TYPE is the kind of thing we are
357 setting. VAR is address of the variable being controlled by this
358 command. SET_FUNC and SHOW_FUNC are the callback functions (if
359 non-NULL). SET_DOC, SHOW_DOC and HELP_DOC are the documentation
360 strings. PRINT the format string to print the value. SET_RESULT
361 and SHOW_RESULT, if not NULL, are set to the resulting command
365 add_setshow_cmd_full (char *name,
366 enum command_class class,
367 var_types var_type, void *var,
368 const char *set_doc, const char *show_doc,
369 const char *help_doc,
370 cmd_sfunc_ftype *set_func,
371 show_value_ftype *show_func,
372 struct cmd_list_element **set_list,
373 struct cmd_list_element **show_list,
374 struct cmd_list_element **set_result,
375 struct cmd_list_element **show_result)
377 struct cmd_list_element *set;
378 struct cmd_list_element *show;
382 if (help_doc != NULL)
384 full_set_doc = xstrprintf ("%s\n%s", set_doc, help_doc);
385 full_show_doc = xstrprintf ("%s\n%s", show_doc, help_doc);
389 full_set_doc = xstrdup (set_doc);
390 full_show_doc = xstrdup (show_doc);
392 set = add_set_or_show_cmd (name, set_cmd, class, var_type, var,
393 full_set_doc, set_list);
394 if (set_func != NULL)
395 set_cmd_sfunc (set, set_func);
396 show = add_set_or_show_cmd (name, show_cmd, class, var_type, var,
397 full_show_doc, show_list);
398 show->show_value_func = show_func;
400 if (set_result != NULL)
402 if (show_result != NULL)
406 /* Add element named NAME to command list LIST (the list for set or
407 some sublist thereof). CLASS is as in add_cmd. ENUMLIST is a list
408 of strings which may follow NAME. VAR is address of the variable
409 which will contain the matching string (from ENUMLIST). */
412 add_setshow_enum_cmd (char *name,
413 enum command_class class,
414 const char *enumlist[],
417 const char *show_doc,
418 const char *help_doc,
419 cmd_sfunc_ftype *set_func,
420 show_value_ftype *show_func,
421 struct cmd_list_element **set_list,
422 struct cmd_list_element **show_list)
424 struct cmd_list_element *c;
426 add_setshow_cmd_full (name, class, var_enum, var,
427 set_doc, show_doc, help_doc,
434 /* Add an auto-boolean command named NAME to both the set and show
435 command list lists. CLASS is as in add_cmd. VAR is address of the
436 variable which will contain the value. DOC is the documentation
437 string. FUNC is the corresponding callback. */
439 add_setshow_auto_boolean_cmd (char *name,
440 enum command_class class,
441 enum auto_boolean *var,
442 const char *set_doc, const char *show_doc,
443 const char *help_doc,
444 cmd_sfunc_ftype *set_func,
445 show_value_ftype *show_func,
446 struct cmd_list_element **set_list,
447 struct cmd_list_element **show_list)
449 static const char *auto_boolean_enums[] = { "on", "off", "auto", NULL };
450 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;
476 add_setshow_cmd_full (name, class, var_boolean, var,
477 set_doc, show_doc, help_doc,
481 c->enums = boolean_enums;
484 /* Add element named NAME to both the set and show command LISTs (the
485 list for set/show or some sublist thereof). */
487 add_setshow_filename_cmd (char *name, enum command_class class,
489 const char *set_doc, const char *show_doc,
490 const char *help_doc,
491 cmd_sfunc_ftype *set_func,
492 show_value_ftype *show_func,
493 struct cmd_list_element **set_list,
494 struct cmd_list_element **show_list)
496 struct cmd_list_element *set_result;
498 add_setshow_cmd_full (name, class, var_filename, var,
499 set_doc, show_doc, help_doc,
503 set_cmd_completer (set_result, filename_completer);
506 /* Add element named NAME to both the set and show command LISTs (the
507 list for set/show or some sublist thereof). */
509 add_setshow_string_cmd (char *name, enum command_class class,
511 const char *set_doc, const char *show_doc,
512 const char *help_doc,
513 cmd_sfunc_ftype *set_func,
514 show_value_ftype *show_func,
515 struct cmd_list_element **set_list,
516 struct cmd_list_element **show_list)
518 add_setshow_cmd_full (name, class, var_string, var,
519 set_doc, show_doc, help_doc,
525 /* Add element named NAME to both the set and show command LISTs (the
526 list for set/show or some sublist thereof). */
528 add_setshow_string_noescape_cmd (char *name, enum command_class class,
530 const char *set_doc, const char *show_doc,
531 const char *help_doc,
532 cmd_sfunc_ftype *set_func,
533 show_value_ftype *show_func,
534 struct cmd_list_element **set_list,
535 struct cmd_list_element **show_list)
537 add_setshow_cmd_full (name, class, var_string_noescape, var,
538 set_doc, show_doc, help_doc,
544 /* Add element named NAME to both the set and show command LISTs (the
545 list for set/show or some sublist thereof). */
547 add_setshow_optional_filename_cmd (char *name, enum command_class class,
549 const char *set_doc, const char *show_doc,
550 const char *help_doc,
551 cmd_sfunc_ftype *set_func,
552 show_value_ftype *show_func,
553 struct cmd_list_element **set_list,
554 struct cmd_list_element **show_list)
556 struct cmd_list_element *set_result;
558 add_setshow_cmd_full (name, class, var_optional_filename, var,
559 set_doc, show_doc, help_doc,
564 set_cmd_completer (set_result, filename_completer);
568 /* Add element named NAME to both the set and show command LISTs (the
569 list for set/show or some sublist thereof). CLASS is as in
570 add_cmd. VAR is address of the variable which will contain the
571 value. SET_DOC and SHOW_DOC are the documentation strings. */
573 add_setshow_integer_cmd (char *name, enum command_class class,
575 const char *set_doc, const char *show_doc,
576 const char *help_doc,
577 cmd_sfunc_ftype *set_func,
578 show_value_ftype *show_func,
579 struct cmd_list_element **set_list,
580 struct cmd_list_element **show_list)
582 add_setshow_cmd_full (name, class, var_integer, var,
583 set_doc, show_doc, help_doc,
589 /* Add element named NAME to both the set and show command LISTs (the
590 list for set/show or some sublist thereof). CLASS is as in
591 add_cmd. VAR is address of the variable which will contain the
592 value. SET_DOC and SHOW_DOC are the documentation strings. */
594 add_setshow_uinteger_cmd (char *name, enum command_class class,
596 const char *set_doc, const char *show_doc,
597 const char *help_doc,
598 cmd_sfunc_ftype *set_func,
599 show_value_ftype *show_func,
600 struct cmd_list_element **set_list,
601 struct cmd_list_element **show_list)
603 add_setshow_cmd_full (name, class, var_uinteger, var,
604 set_doc, show_doc, help_doc,
610 /* Add element named NAME to both the set and show command LISTs (the
611 list for set/show or some sublist thereof). CLASS is as in
612 add_cmd. VAR is address of the variable which will contain the
613 value. SET_DOC and SHOW_DOC are the documentation strings. */
615 add_setshow_zinteger_cmd (char *name, enum command_class class,
617 const char *set_doc, const char *show_doc,
618 const char *help_doc,
619 cmd_sfunc_ftype *set_func,
620 show_value_ftype *show_func,
621 struct cmd_list_element **set_list,
622 struct cmd_list_element **show_list)
624 add_setshow_cmd_full (name, class, var_zinteger, var,
625 set_doc, show_doc, help_doc,
631 /* Add element named NAME to both the set and show command LISTs (the
632 list for set/show or some sublist thereof). CLASS is as in
633 add_cmd. VAR is address of the variable which will contain the
634 value. SET_DOC and SHOW_DOC are the documentation strings. */
636 add_setshow_zuinteger_cmd (char *name, enum command_class class,
638 const char *set_doc, const char *show_doc,
639 const char *help_doc,
640 cmd_sfunc_ftype *set_func,
641 show_value_ftype *show_func,
642 struct cmd_list_element **set_list,
643 struct cmd_list_element **show_list)
645 add_setshow_cmd_full (name, class, var_zuinteger, var,
646 set_doc, show_doc, help_doc,
652 /* Remove the command named NAME from the command list. Return the
653 list commands which were aliased to the deleted command. If the
654 command had no aliases, return NULL. The various *HOOKs are set to
655 the pre- and post-hook commands for the deleted command. If the
656 command does not have a hook, the corresponding out parameter is
659 static struct cmd_list_element *
660 delete_cmd (char *name, struct cmd_list_element **list,
661 struct cmd_list_element **prehook,
662 struct cmd_list_element **prehookee,
663 struct cmd_list_element **posthook,
664 struct cmd_list_element **posthookee)
666 struct cmd_list_element *iter;
667 struct cmd_list_element **previous_chain_ptr;
668 struct cmd_list_element *aliases = NULL;
674 previous_chain_ptr = list;
676 for (iter = *previous_chain_ptr; iter; iter = *previous_chain_ptr)
678 if (strcmp (iter->name, name) == 0)
681 iter->destroyer (iter, iter->context);
682 if (iter->hookee_pre)
683 iter->hookee_pre->hook_pre = 0;
684 *prehook = iter->hook_pre;
685 *prehookee = iter->hookee_pre;
686 if (iter->hookee_post)
687 iter->hookee_post->hook_post = 0;
688 *posthook = iter->hook_post;
689 *posthookee = iter->hookee_post;
691 /* Update the link. */
692 *previous_chain_ptr = iter->next;
694 aliases = iter->aliases;
696 /* If this command was an alias, remove it from the list of
698 if (iter->cmd_pointer)
700 struct cmd_list_element **prevp = &iter->cmd_pointer->aliases;
701 struct cmd_list_element *a = *prevp;
705 prevp = &a->alias_chain;
708 *prevp = iter->alias_chain;
713 /* We won't see another command with the same name. */
717 previous_chain_ptr = &iter->next;
723 /* Shorthands to the commands above. */
725 /* Add an element to the list of info subcommands. */
727 struct cmd_list_element *
728 add_info (char *name, void (*fun) (char *, int), char *doc)
730 return add_cmd (name, no_class, fun, doc, &infolist);
733 /* Add an alias to the list of info subcommands. */
735 struct cmd_list_element *
736 add_info_alias (char *name, char *oldname, int abbrev_flag)
738 return add_alias_cmd (name, oldname, 0, abbrev_flag, &infolist);
741 /* Add an element to the list of commands. */
743 struct cmd_list_element *
744 add_com (char *name, enum command_class class, void (*fun) (char *, int),
747 return add_cmd (name, class, fun, doc, &cmdlist);
750 /* Add an alias or abbreviation command to the list of commands. */
752 struct cmd_list_element *
753 add_com_alias (char *name, char *oldname, enum command_class class,
756 return add_alias_cmd (name, oldname, class, abbrev_flag, &cmdlist);
759 /* Recursively walk the commandlist structures, and print out the
760 documentation of commands that match our regex in either their
761 name, or their documentation.
764 apropos_cmd (struct ui_file *stream,
765 struct cmd_list_element *commandlist,
766 struct re_pattern_buffer *regex, char *prefix)
768 struct cmd_list_element *c;
771 /* Walk through the commands. */
772 for (c=commandlist;c;c=c->next)
774 returnvalue = -1; /* Needed to avoid double printing. */
777 /* Try to match against the name. */
778 returnvalue = re_search (regex, c->name, strlen(c->name),
779 0, strlen (c->name), NULL);
780 if (returnvalue >= 0)
782 print_help_for_command (c, prefix,
783 0 /* don't recurse */, stream);
786 if (c->doc != NULL && returnvalue < 0)
788 /* Try to match against documentation. */
789 if (re_search(regex,c->doc,strlen(c->doc),0,strlen(c->doc),NULL) >=0)
791 print_help_for_command (c, prefix,
792 0 /* don't recurse */, stream);
795 /* Check if this command has subcommands and is not an
796 abbreviation. We skip listing subcommands of abbreviations
797 in order to avoid duplicates in the output. */
798 if (c->prefixlist != NULL && !c->abbrev_flag)
800 /* Recursively call ourselves on the subcommand list,
801 passing the right prefix in. */
802 apropos_cmd (stream,*c->prefixlist,regex,c->prefixname);
807 /* This command really has to deal with two things:
808 1) I want documentation on *this string* (usually called by
811 2) I want documentation on *this list* (usually called by giving a
812 command that requires subcommands. Also called by saying just
815 I am going to split this into two seperate comamnds, help_cmd and
819 help_cmd (char *command, struct ui_file *stream)
821 struct cmd_list_element *c;
822 extern struct cmd_list_element *cmdlist;
826 help_list (cmdlist, "", all_classes, stream);
830 if (strcmp (command, "all") == 0)
836 c = lookup_cmd (&command, cmdlist, "", 0, 0);
841 /* There are three cases here.
842 If c->prefixlist is nonzero, we have a prefix command.
843 Print its documentation, then list its subcommands.
845 If c->func is non NULL, we really have a command. Print its
846 documentation and return.
848 If c->func is NULL, we have a class name. Print its
849 documentation (as if it were a command) and then set class to the
850 number of this class so that the commands in the class will be
853 fputs_filtered (c->doc, stream);
854 fputs_filtered ("\n", stream);
856 if (c->prefixlist == 0 && c->func != NULL)
858 fprintf_filtered (stream, "\n");
860 /* If this is a prefix command, print it's subcommands. */
862 help_list (*c->prefixlist, c->prefixname, all_commands, stream);
864 /* If this is a class name, print all of the commands in the class. */
866 help_list (cmdlist, "", c->class, stream);
868 if (c->hook_pre || c->hook_post)
869 fprintf_filtered (stream,
870 "\nThis command has a hook (or hooks) defined:\n");
873 fprintf_filtered (stream,
874 "\tThis command is run after : %s (pre hook)\n",
877 fprintf_filtered (stream,
878 "\tThis command is run before : %s (post hook)\n",
883 * Get a specific kind of help on a command list.
886 * CMDTYPE is the prefix to use in the title string.
887 * CLASS is the class with which to list the nodes of this list (see
888 * documentation for help_cmd_list below), As usual, ALL_COMMANDS for
889 * everything, ALL_CLASSES for just classes, and non-negative for only things
890 * in a specific class.
891 * and STREAM is the output stream on which to print things.
892 * If you call this routine with a class >= 0, it recurses.
895 help_list (struct cmd_list_element *list, char *cmdtype,
896 enum command_class class, struct ui_file *stream)
899 char *cmdtype1, *cmdtype2;
901 /* If CMDTYPE is "foo ", CMDTYPE1 gets " foo" and CMDTYPE2 gets "foo sub".
903 len = strlen (cmdtype);
904 cmdtype1 = (char *) alloca (len + 1);
906 cmdtype2 = (char *) alloca (len + 4);
911 strncpy (cmdtype1 + 1, cmdtype, len - 1);
913 strncpy (cmdtype2, cmdtype, len - 1);
914 strcpy (cmdtype2 + len - 1, " sub");
917 if (class == all_classes)
918 fprintf_filtered (stream, "List of classes of %scommands:\n\n", cmdtype2);
920 fprintf_filtered (stream, "List of %scommands:\n\n", cmdtype2);
922 help_cmd_list (list, class, cmdtype, (int) class >= 0, stream);
924 if (class == all_classes)
926 fprintf_filtered (stream, "\n\
927 Type \"help%s\" followed by a class name for a list of commands in ",
930 fprintf_filtered (stream, "that class.");
932 fprintf_filtered (stream, "\n\
933 Type \"help all\" for the list of all commands.");
936 fprintf_filtered (stream, "\nType \"help%s\" followed by %scommand name ",
939 fputs_filtered ("for ", stream);
941 fputs_filtered ("full ", stream);
943 fputs_filtered ("documentation.\n", stream);
944 fputs_filtered ("Type \"apropos word\" to search "
945 "for commands related to \"word\".\n", stream);
946 fputs_filtered ("Command name abbreviations are allowed if unambiguous.\n",
951 help_all (struct ui_file *stream)
953 struct cmd_list_element *c;
954 extern struct cmd_list_element *cmdlist;
955 int seen_unclassified = 0;
957 for (c = cmdlist; c; c = c->next)
961 /* If this is a class name, print all of the commands in the
966 fprintf_filtered (stream, "\nCommand class: %s\n\n", c->name);
967 help_cmd_list (cmdlist, c->class, "", 1, stream);
971 /* While it's expected that all commands are in some class,
972 as a safety measure, we'll print commands outside of any
975 for (c = cmdlist; c; c = c->next)
980 if (c->class == no_class)
982 if (!seen_unclassified)
984 fprintf_filtered (stream, "\nUnclassified commands\n\n");
985 seen_unclassified = 1;
987 print_help_for_command (c, "", 1, stream);
993 /* Print only the first line of STR on STREAM. */
995 print_doc_line (struct ui_file *stream, char *str)
997 static char *line_buffer = 0;
998 static int line_size;
1004 line_buffer = (char *) xmalloc (line_size);
1008 while (*p && *p != '\n' && *p != '.' && *p != ',')
1010 if (p - str > line_size - 1)
1012 line_size = p - str + 1;
1013 xfree (line_buffer);
1014 line_buffer = (char *) xmalloc (line_size);
1016 strncpy (line_buffer, str, p - str);
1017 line_buffer[p - str] = '\0';
1018 if (islower (line_buffer[0]))
1019 line_buffer[0] = toupper (line_buffer[0]);
1020 ui_out_text (uiout, line_buffer);
1023 /* Print one-line help for command C.
1024 If RECURSE is non-zero, also print one-line descriptions
1025 of all prefixed subcommands. */
1027 print_help_for_command (struct cmd_list_element *c, char *prefix, int recurse,
1028 struct ui_file *stream)
1030 fprintf_filtered (stream, "%s%s -- ", prefix, c->name);
1031 print_doc_line (stream, c->doc);
1032 fputs_filtered ("\n", stream);
1035 && c->prefixlist != 0
1036 && c->abbrev_flag == 0)
1037 /* Subcommands of a prefix command typically have 'all_commands'
1038 as class. If we pass CLASS to recursive invocation,
1039 most often we won't see anything. */
1040 help_cmd_list (*c->prefixlist, all_commands, c->prefixname, 1, stream);
1044 * Implement a help command on command list LIST.
1045 * RECURSE should be non-zero if this should be done recursively on
1046 * all sublists of LIST.
1047 * PREFIX is the prefix to print before each command name.
1048 * STREAM is the stream upon which the output should be written.
1050 * A non-negative class number to list only commands in that
1052 * ALL_COMMANDS to list all commands in list.
1053 * ALL_CLASSES to list all classes in list.
1055 * Note that RECURSE will be active on *all* sublists, not just the
1056 * ones selected by the criteria above (ie. the selection mechanism
1057 * is at the low level, not the high-level).
1060 help_cmd_list (struct cmd_list_element *list, enum command_class class,
1061 char *prefix, int recurse, struct ui_file *stream)
1063 struct cmd_list_element *c;
1065 for (c = list; c; c = c->next)
1067 if (c->abbrev_flag == 0
1068 && (class == all_commands
1069 || (class == all_classes && c->func == NULL)
1070 || (class == c->class && c->func != NULL)))
1072 print_help_for_command (c, prefix, recurse, stream);
1074 else if (c->abbrev_flag == 0 && recurse
1075 && class == class_user && c->prefixlist != NULL)
1076 /* User-defined commands may be subcommands. */
1077 help_cmd_list (*c->prefixlist, class, c->prefixname,
1083 /* Search the input clist for 'command'. Return the command if
1084 found (or NULL if not), and return the number of commands
1087 static struct cmd_list_element *
1088 find_cmd (char *command, int len, struct cmd_list_element *clist,
1089 int ignore_help_classes, int *nfound)
1091 struct cmd_list_element *found, *c;
1093 found = (struct cmd_list_element *) NULL;
1095 for (c = clist; c; c = c->next)
1096 if (!strncmp (command, c->name, len)
1097 && (!ignore_help_classes || c->func))
1101 if (c->name[len] == '\0')
1111 find_command_name_length (const char *text)
1113 const char *p = text;
1115 /* Treating underscores as part of command words is important
1116 so that "set args_foo()" doesn't get interpreted as
1117 "set args _foo()". */
1118 /* Some characters are only used for TUI specific commands.
1119 However, they are always allowed for the sake of consistency.
1121 The XDB compatibility characters are only allowed when using the
1122 right mode because they clash with other GDB commands -
1123 specifically '/' is used as a suffix for print, examine and
1126 Note that this is larger than the character set allowed when
1127 creating user-defined commands. */
1129 while (isalnum (*p) || *p == '-' || *p == '_'
1130 /* Characters used by TUI specific commands. */
1131 || *p == '+' || *p == '<' || *p == '>' || *p == '$'
1132 /* Characters used for XDB compatibility. */
1133 || (xdb_commands && (*p == '!' || *p == '/' || *p == '?')))
1139 /* This routine takes a line of TEXT and a CLIST in which to start the
1140 lookup. When it returns it will have incremented the text pointer past
1141 the section of text it matched, set *RESULT_LIST to point to the list in
1142 which the last word was matched, and will return a pointer to the cmd
1143 list element which the text matches. It will return NULL if no match at
1144 all was possible. It will return -1 (cast appropriately, ick) if ambigous
1145 matches are possible; in this case *RESULT_LIST will be set to point to
1146 the list in which there are ambiguous choices (and *TEXT will be set to
1147 the ambiguous text string).
1149 If the located command was an abbreviation, this routine returns the base
1150 command of the abbreviation.
1152 It does no error reporting whatsoever; control will always return
1153 to the superior routine.
1155 In the case of an ambiguous return (-1), *RESULT_LIST will be set to point
1156 at the prefix_command (ie. the best match) *or* (special case) will be NULL
1157 if no prefix command was ever found. For example, in the case of "info a",
1158 "info" matches without ambiguity, but "a" could be "args" or "address", so
1159 *RESULT_LIST is set to the cmd_list_element for "info". So in this case
1160 RESULT_LIST should not be interpeted as a pointer to the beginning of a
1161 list; it simply points to a specific command. In the case of an ambiguous
1162 return *TEXT is advanced past the last non-ambiguous prefix (e.g.
1163 "info t" can be "info types" or "info target"; upon return *TEXT has been
1164 advanced past "info ").
1166 If RESULT_LIST is NULL, don't set *RESULT_LIST (but don't otherwise
1167 affect the operation).
1169 This routine does *not* modify the text pointed to by TEXT.
1171 If IGNORE_HELP_CLASSES is nonzero, ignore any command list elements which
1172 are actually help classes rather than commands (i.e. the function field of
1173 the struct cmd_list_element is NULL). */
1175 struct cmd_list_element *
1176 lookup_cmd_1 (char **text, struct cmd_list_element *clist,
1177 struct cmd_list_element **result_list, int ignore_help_classes)
1180 int len, tmp, nfound;
1181 struct cmd_list_element *found, *c;
1184 while (**text == ' ' || **text == '\t')
1187 /* Identify the name of the command. */
1188 len = find_command_name_length (*text);
1190 /* If nothing but whitespace, return 0. */
1194 /* *text and p now bracket the first command word to lookup (and
1195 it's length is len). We copy this into a local temporary. */
1198 command = (char *) alloca (len + 1);
1199 memcpy (command, *text, len);
1200 command[len] = '\0';
1205 found = find_cmd (command, len, clist, ignore_help_classes, &nfound);
1207 /* We didn't find the command in the entered case, so lower case it
1208 and search again. */
1209 if (!found || nfound == 0)
1211 for (tmp = 0; tmp < len; tmp++)
1213 char x = command[tmp];
1215 command[tmp] = isupper (x) ? tolower (x) : x;
1217 found = find_cmd (command, len, clist, ignore_help_classes, &nfound);
1220 /* If nothing matches, we have a simple failure. */
1226 if (result_list != NULL)
1227 /* Will be modified in calling routine
1228 if we know what the prefix command is. */
1230 return (struct cmd_list_element *) -1; /* Ambiguous. */
1233 /* We've matched something on this list. Move text pointer forward. */
1237 if (found->cmd_pointer)
1239 /* We drop the alias (abbreviation) in favor of the command it
1240 is pointing to. If the alias is deprecated, though, we need to
1241 warn the user about it before we drop it. Note that while we
1242 are warning about the alias, we may also warn about the command
1243 itself and we will adjust the appropriate DEPRECATED_WARN_USER
1246 if (found->flags & DEPRECATED_WARN_USER)
1247 deprecated_cmd_warning (&line);
1248 found = found->cmd_pointer;
1250 /* If we found a prefix command, keep looking. */
1252 if (found->prefixlist)
1254 c = lookup_cmd_1 (text, *found->prefixlist, result_list,
1255 ignore_help_classes);
1258 /* Didn't find anything; this is as far as we got. */
1259 if (result_list != NULL)
1260 *result_list = clist;
1263 else if (c == (struct cmd_list_element *) -1)
1265 /* We've gotten this far properly, but the next step is
1266 ambiguous. We need to set the result list to the best
1267 we've found (if an inferior hasn't already set it). */
1268 if (result_list != NULL)
1270 /* This used to say *result_list = *found->prefixlist.
1271 If that was correct, need to modify the documentation
1272 at the top of this function to clarify what is
1273 supposed to be going on. */
1274 *result_list = found;
1285 if (result_list != NULL)
1286 *result_list = clist;
1291 /* All this hair to move the space to the front of cmdtype */
1294 undef_cmd_error (char *cmdtype, char *q)
1296 error (_("Undefined %scommand: \"%s\". Try \"help%s%.*s\"."),
1299 *cmdtype ? " " : "",
1300 (int) strlen (cmdtype) - 1,
1304 /* Look up the contents of *LINE as a command in the command list LIST.
1305 LIST is a chain of struct cmd_list_element's.
1306 If it is found, return the struct cmd_list_element for that command
1307 and update *LINE to point after the command name, at the first argument.
1308 If not found, call error if ALLOW_UNKNOWN is zero
1309 otherwise (or if error returns) return zero.
1310 Call error if specified command is ambiguous,
1311 unless ALLOW_UNKNOWN is negative.
1312 CMDTYPE precedes the word "command" in the error message.
1314 If INGNORE_HELP_CLASSES is nonzero, ignore any command list
1315 elements which are actually help classes rather than commands (i.e.
1316 the function field of the struct cmd_list_element is 0). */
1318 struct cmd_list_element *
1319 lookup_cmd (char **line, struct cmd_list_element *list, char *cmdtype,
1320 int allow_unknown, int ignore_help_classes)
1322 struct cmd_list_element *last_list = 0;
1323 struct cmd_list_element *c;
1325 /* Note: Do not remove trailing whitespace here because this
1326 would be wrong for complete_command. Jim Kingdon */
1329 error (_("Lack of needed %scommand"), cmdtype);
1331 c = lookup_cmd_1 (line, list, &last_list, ignore_help_classes);
1338 int len = find_command_name_length (*line);
1340 q = (char *) alloca (len + 1);
1341 strncpy (q, *line, len);
1343 undef_cmd_error (cmdtype, q);
1348 else if (c == (struct cmd_list_element *) -1)
1350 /* Ambigous. Local values should be off prefixlist or called
1352 int local_allow_unknown = (last_list ? last_list->allow_unknown :
1354 char *local_cmdtype = last_list ? last_list->prefixname : cmdtype;
1355 struct cmd_list_element *local_list =
1356 (last_list ? *(last_list->prefixlist) : list);
1358 if (local_allow_unknown < 0)
1361 return last_list; /* Found something. */
1363 return 0; /* Found nothing. */
1367 /* Report as error. */
1372 ((*line)[amb_len] && (*line)[amb_len] != ' '
1373 && (*line)[amb_len] != '\t');
1378 for (c = local_list; c; c = c->next)
1379 if (!strncmp (*line, c->name, amb_len))
1381 if (strlen (ambbuf) + strlen (c->name) + 6 < (int) sizeof ambbuf)
1383 if (strlen (ambbuf))
1384 strcat (ambbuf, ", ");
1385 strcat (ambbuf, c->name);
1389 strcat (ambbuf, "..");
1393 error (_("Ambiguous %scommand \"%s\": %s."), local_cmdtype,
1395 return 0; /* lint */
1400 /* We've got something. It may still not be what the caller
1401 wants (if this command *needs* a subcommand). */
1402 while (**line == ' ' || **line == '\t')
1405 if (c->prefixlist && **line && !c->allow_unknown)
1406 undef_cmd_error (c->prefixname, *line);
1408 /* Seems to be what he wants. Return it. */
1414 /* We are here presumably because an alias or command in *TEXT is
1415 deprecated and a warning message should be generated. This
1416 function decodes *TEXT and potentially generates a warning message
1419 Example for 'set endian big' which has a fictitious alias 'seb'.
1421 If alias wasn't used in *TEXT, and the command is deprecated:
1422 "warning: 'set endian big' is deprecated."
1424 If alias was used, and only the alias is deprecated:
1425 "warning: 'seb' an alias for the command 'set endian big' is deprecated."
1427 If alias was used and command is deprecated (regardless of whether
1428 the alias itself is deprecated:
1430 "warning: 'set endian big' (seb) is deprecated."
1432 After the message has been sent, clear the appropriate flags in the
1433 command and/or the alias so the user is no longer bothered.
1437 deprecated_cmd_warning (char **text)
1439 struct cmd_list_element *alias = NULL;
1440 struct cmd_list_element *prefix_cmd = NULL;
1441 struct cmd_list_element *cmd = NULL;
1443 if (!lookup_cmd_composition (*text, &alias, &prefix_cmd, &cmd))
1444 /* Return if text doesn't evaluate to a command. */
1447 if (!((alias ? (alias->flags & DEPRECATED_WARN_USER) : 0)
1448 || (cmd->flags & DEPRECATED_WARN_USER) ) )
1449 /* Return if nothing is deprecated. */
1452 printf_filtered ("Warning:");
1454 if (alias && !(cmd->flags & CMD_DEPRECATED))
1455 printf_filtered (" '%s', an alias for the", alias->name);
1457 printf_filtered (" command '");
1460 printf_filtered ("%s", prefix_cmd->prefixname);
1462 printf_filtered ("%s", cmd->name);
1464 if (alias && (cmd->flags & CMD_DEPRECATED))
1465 printf_filtered ("' (%s) is deprecated.\n", alias->name);
1467 printf_filtered ("' is deprecated.\n");
1470 /* If it is only the alias that is deprecated, we want to indicate
1471 the new alias, otherwise we'll indicate the new command. */
1473 if (alias && !(cmd->flags & CMD_DEPRECATED))
1475 if (alias->replacement)
1476 printf_filtered ("Use '%s'.\n\n", alias->replacement);
1478 printf_filtered ("No alternative known.\n\n");
1482 if (cmd->replacement)
1483 printf_filtered ("Use '%s'.\n\n", cmd->replacement);
1485 printf_filtered ("No alternative known.\n\n");
1488 /* We've warned you, now we'll keep quiet. */
1490 alias->flags &= ~DEPRECATED_WARN_USER;
1492 cmd->flags &= ~DEPRECATED_WARN_USER;
1496 /* Look up the contents of LINE as a command in the command list 'cmdlist'.
1497 Return 1 on success, 0 on failure.
1499 If LINE refers to an alias, *alias will point to that alias.
1501 If LINE is a postfix command (i.e. one that is preceeded by a prefix
1502 command) set *prefix_cmd.
1504 Set *cmd to point to the command LINE indicates.
1506 If any of *alias, *prefix_cmd, or *cmd cannot be determined or do not
1507 exist, they are NULL when we return.
1511 lookup_cmd_composition (char *text,
1512 struct cmd_list_element **alias,
1513 struct cmd_list_element **prefix_cmd,
1514 struct cmd_list_element **cmd)
1517 int len, tmp, nfound;
1518 struct cmd_list_element *cur_list;
1519 struct cmd_list_element *prev_cmd;
1529 /* Go through as many command lists as we need to
1530 to find the command TEXT refers to. */
1534 while (*text == ' ' || *text == '\t')
1537 /* Identify the name of the command. */
1538 len = find_command_name_length (text);
1540 /* If nothing but whitespace, return. */
1544 /* Text is the start of the first command word to lookup (and
1545 it's length is len). We copy this into a local temporary. */
1547 command = (char *) alloca (len + 1);
1548 memcpy (command, text, len);
1549 command[len] = '\0';
1554 *cmd = find_cmd (command, len, cur_list, 1, &nfound);
1556 /* We didn't find the command in the entered case, so lower case
1557 it and search again.
1559 if (!*cmd || nfound == 0)
1561 for (tmp = 0; tmp < len; tmp++)
1563 char x = command[tmp];
1565 command[tmp] = isupper (x) ? tolower (x) : x;
1567 *cmd = find_cmd (command, len, cur_list, 1, &nfound);
1570 if (*cmd == (struct cmd_list_element *) -1)
1572 return 0; /* ambiguous */
1576 return 0; /* nothing found */
1579 if ((*cmd)->cmd_pointer)
1581 /* cmd was actually an alias, we note that an alias was
1582 used (by assigning *alais) and we set *cmd. */
1584 *cmd = (*cmd)->cmd_pointer;
1586 *prefix_cmd = prev_cmd;
1588 if ((*cmd)->prefixlist)
1589 cur_list = *(*cmd)->prefixlist;
1597 /* Helper function for SYMBOL_COMPLETION_FUNCTION. */
1599 /* Return a vector of char pointers which point to the different
1600 possible completions in LIST of TEXT.
1602 WORD points in the same buffer as TEXT, and completions should be
1603 returned relative to this position. For example, suppose TEXT is
1604 "foo" and we want to complete to "foobar". If WORD is "oo", return
1605 "oobar"; if WORD is "baz/foo", return "baz/foobar". */
1608 complete_on_cmdlist (struct cmd_list_element *list, char *text, char *word)
1610 struct cmd_list_element *ptr;
1612 int sizeof_matchlist;
1614 int textlen = strlen (text);
1616 int saw_deprecated_match = 0;
1618 sizeof_matchlist = 10;
1619 matchlist = (char **) xmalloc (sizeof_matchlist * sizeof (char *));
1622 /* We do one or two passes. In the first pass, we skip deprecated
1623 commands. If we see no matching commands in the first pass, and
1624 if we did happen to see a matching deprecated command, we do
1625 another loop to collect those. */
1626 for (pass = 0; matches == 0 && pass < 2; ++pass)
1628 for (ptr = list; ptr; ptr = ptr->next)
1629 if (!strncmp (ptr->name, text, textlen)
1630 && !ptr->abbrev_flag
1632 || ptr->prefixlist))
1636 if ((ptr->flags & CMD_DEPRECATED) != 0)
1638 saw_deprecated_match = 1;
1643 if (matches == sizeof_matchlist)
1645 sizeof_matchlist *= 2;
1646 matchlist = (char **) xrealloc ((char *) matchlist,
1648 * sizeof (char *)));
1651 matchlist[matches] = (char *)
1652 xmalloc (strlen (word) + strlen (ptr->name) + 1);
1654 strcpy (matchlist[matches], ptr->name);
1655 else if (word > text)
1657 /* Return some portion of ptr->name. */
1658 strcpy (matchlist[matches], ptr->name + (word - text));
1662 /* Return some of text plus ptr->name. */
1663 strncpy (matchlist[matches], word, text - word);
1664 matchlist[matches][text - word] = '\0';
1665 strcat (matchlist[matches], ptr->name);
1669 /* If we saw no matching deprecated commands in the first pass,
1671 if (!saw_deprecated_match)
1682 matchlist = (char **) xrealloc ((char *) matchlist, ((matches + 1)
1683 * sizeof (char *)));
1684 matchlist[matches] = (char *) 0;
1690 /* Helper function for SYMBOL_COMPLETION_FUNCTION. */
1692 /* Return a vector of char pointers which point to the different
1693 possible completions in CMD of TEXT.
1695 WORD points in the same buffer as TEXT, and completions should be
1696 returned relative to this position. For example, suppose TEXT is "foo"
1697 and we want to complete to "foobar". If WORD is "oo", return
1698 "oobar"; if WORD is "baz/foo", return "baz/foobar". */
1701 complete_on_enum (const char *enumlist[],
1706 int sizeof_matchlist;
1708 int textlen = strlen (text);
1712 sizeof_matchlist = 10;
1713 matchlist = (char **) xmalloc (sizeof_matchlist * sizeof (char *));
1716 for (i = 0; (name = enumlist[i]) != NULL; i++)
1717 if (strncmp (name, text, textlen) == 0)
1719 if (matches == sizeof_matchlist)
1721 sizeof_matchlist *= 2;
1722 matchlist = (char **) xrealloc ((char *) matchlist,
1724 * sizeof (char *)));
1727 matchlist[matches] = (char *)
1728 xmalloc (strlen (word) + strlen (name) + 1);
1730 strcpy (matchlist[matches], name);
1731 else if (word > text)
1733 /* Return some portion of name. */
1734 strcpy (matchlist[matches], name + (word - text));
1738 /* Return some of text plus name. */
1739 strncpy (matchlist[matches], word, text - word);
1740 matchlist[matches][text - word] = '\0';
1741 strcat (matchlist[matches], name);
1753 matchlist = (char **) xrealloc ((char *) matchlist, ((matches + 1)
1754 * sizeof (char *)));
1755 matchlist[matches] = (char *) 0;
1762 /* Check function pointer. */
1764 cmd_func_p (struct cmd_list_element *cmd)
1766 return (cmd->func != NULL);
1770 /* Call the command function. */
1772 cmd_func (struct cmd_list_element *cmd, char *args, int from_tty)
1774 if (cmd_func_p (cmd))
1775 (*cmd->func) (cmd, args, from_tty);
1777 error (_("Invalid command"));