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 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 *find_cmd (char *command,
42 struct cmd_list_element *clist,
43 int ignore_help_classes,
46 static void help_all (struct ui_file *stream);
49 print_help_for_command (struct cmd_list_element *c, char *prefix, int recurse,
50 struct ui_file *stream);
53 /* Set the callback function for the specified command. For each both
54 the commands callback and func() are set. The latter set to a
55 bounce function (unless cfunc / sfunc is NULL that is). */
58 do_cfunc (struct cmd_list_element *c, char *args, int from_tty)
60 c->function.cfunc (args, from_tty); /* Ok. */
64 set_cmd_cfunc (struct cmd_list_element *cmd, cmd_cfunc_ftype *cfunc)
70 cmd->function.cfunc = cfunc; /* Ok. */
74 do_sfunc (struct cmd_list_element *c, char *args, int from_tty)
76 c->function.sfunc (args, from_tty, c); /* Ok. */
80 set_cmd_sfunc (struct cmd_list_element *cmd, cmd_sfunc_ftype *sfunc)
86 cmd->function.sfunc = sfunc; /* Ok. */
90 cmd_cfunc_eq (struct cmd_list_element *cmd,
91 void (*cfunc) (char *args, int from_tty))
93 return cmd->func == do_cfunc && cmd->function.cfunc == cfunc;
97 set_cmd_context (struct cmd_list_element *cmd, void *context)
99 cmd->context = context;
103 get_cmd_context (struct cmd_list_element *cmd)
109 cmd_type (struct cmd_list_element *cmd)
115 set_cmd_completer (struct cmd_list_element *cmd,
116 char **(*completer) (char *text, char *word))
118 cmd->completer = completer; /* Ok. */
122 /* Add element named NAME.
123 CLASS is the top level category into which commands are broken down
125 FUN should be the function to execute the command;
126 it will get a character string as argument, with leading
127 and trailing blanks already eliminated.
129 DOC is a documentation string for the command.
130 Its first line should be a complete sentence.
131 It should start with ? for a command that is an abbreviation
132 or with * for a command that most users don't need to know about.
134 Add this command to command list *LIST.
136 Returns a pointer to the added command (not necessarily the head
139 struct cmd_list_element *
140 add_cmd (char *name, enum command_class class, void (*fun) (char *, int),
141 char *doc, struct cmd_list_element **list)
143 struct cmd_list_element *c
144 = (struct cmd_list_element *) xmalloc (sizeof (struct cmd_list_element));
145 struct cmd_list_element *p;
147 delete_cmd (name, list);
149 if (*list == NULL || strcmp ((*list)->name, name) >= 0)
157 while (p->next && strcmp (p->next->name, name) <= 0)
167 set_cmd_cfunc (c, fun);
168 set_cmd_context (c, NULL);
171 c->replacement = NULL;
172 c->pre_show_hook = NULL;
176 c->prefixlist = NULL;
177 c->prefixname = NULL;
178 c->allow_unknown = 0;
180 set_cmd_completer (c, make_symbol_completion_list);
181 c->type = not_set_cmd;
183 c->var_type = var_boolean;
185 c->user_commands = NULL;
186 c->hookee_pre = NULL;
187 c->hookee_post = NULL;
188 c->cmd_pointer = NULL;
193 /* Deprecates a command CMD.
194 REPLACEMENT is the name of the command which should be used in place
195 of this command, or NULL if no such command exists.
197 This function does not check to see if command REPLACEMENT exists
198 since gdb may not have gotten around to adding REPLACEMENT when this
201 Returns a pointer to the deprecated command. */
203 struct cmd_list_element *
204 deprecate_cmd (struct cmd_list_element *cmd, char *replacement)
206 cmd->flags |= (CMD_DEPRECATED | DEPRECATED_WARN_USER);
208 if (replacement != NULL)
209 cmd->replacement = replacement;
211 cmd->replacement = NULL;
216 struct cmd_list_element *
217 add_alias_cmd (char *name, char *oldname, enum command_class class,
218 int abbrev_flag, struct cmd_list_element **list)
220 /* Must do this since lookup_cmd tries to side-effect its first arg */
222 struct cmd_list_element *old;
223 struct cmd_list_element *c;
224 copied_name = (char *) alloca (strlen (oldname) + 1);
225 strcpy (copied_name, oldname);
226 old = lookup_cmd (&copied_name, *list, "", 1, 1);
230 delete_cmd (name, list);
234 c = add_cmd (name, class, NULL, old->doc, list);
235 /* NOTE: Both FUNC and all the FUNCTIONs need to be copied. */
237 c->function = old->function;
238 c->prefixlist = old->prefixlist;
239 c->prefixname = old->prefixname;
240 c->allow_unknown = old->allow_unknown;
241 c->abbrev_flag = abbrev_flag;
242 c->cmd_pointer = old;
246 /* Like add_cmd but adds an element for a command prefix:
247 a name that should be followed by a subcommand to be looked up
248 in another command list. PREFIXLIST should be the address
249 of the variable containing that list. */
251 struct cmd_list_element *
252 add_prefix_cmd (char *name, enum command_class class, void (*fun) (char *, int),
253 char *doc, struct cmd_list_element **prefixlist,
254 char *prefixname, int allow_unknown,
255 struct cmd_list_element **list)
257 struct cmd_list_element *c = add_cmd (name, class, fun, doc, list);
258 c->prefixlist = prefixlist;
259 c->prefixname = prefixname;
260 c->allow_unknown = allow_unknown;
264 /* Like add_prefix_cmd but sets the abbrev_flag on the new command. */
266 struct cmd_list_element *
267 add_abbrev_prefix_cmd (char *name, enum command_class class,
268 void (*fun) (char *, int), char *doc,
269 struct cmd_list_element **prefixlist, char *prefixname,
270 int allow_unknown, struct cmd_list_element **list)
272 struct cmd_list_element *c = add_cmd (name, class, fun, doc, list);
273 c->prefixlist = prefixlist;
274 c->prefixname = prefixname;
275 c->allow_unknown = allow_unknown;
280 /* This is an empty "cfunc". */
282 not_just_help_class_command (char *args, int from_tty)
286 /* This is an empty "sfunc". */
287 static void empty_sfunc (char *, int, struct cmd_list_element *);
290 empty_sfunc (char *args, int from_tty, struct cmd_list_element *c)
294 /* Add element named NAME to command list LIST (the list for set/show
295 or some sublist thereof).
296 TYPE is set_cmd or show_cmd.
297 CLASS is as in add_cmd.
298 VAR_TYPE is the kind of thing we are setting.
299 VAR is address of the variable being controlled by this command.
300 DOC is the documentation string. */
302 static struct cmd_list_element *
303 add_set_or_show_cmd (char *name,
305 enum command_class class,
309 struct cmd_list_element **list)
311 struct cmd_list_element *c = add_cmd (name, class, NULL, doc, list);
312 gdb_assert (type == set_cmd || type == show_cmd);
314 c->var_type = var_type;
316 /* This needs to be something besides NULL so that this isn't
317 treated as a help class. */
318 set_cmd_sfunc (c, empty_sfunc);
322 /* Add element named NAME to both the command SET_LIST and SHOW_LIST.
323 CLASS is as in add_cmd. VAR_TYPE is the kind of thing we are
324 setting. VAR is address of the variable being controlled by this
325 command. SET_FUNC and SHOW_FUNC are the callback functions (if
326 non-NULL). SET_DOC, SHOW_DOC and HELP_DOC are the documentation
327 strings. PRINT the format string to print the value. SET_RESULT
328 and SHOW_RESULT, if not NULL, are set to the resulting command
332 add_setshow_cmd_full (char *name,
333 enum command_class class,
334 var_types var_type, void *var,
335 const char *set_doc, const char *show_doc,
336 const char *help_doc,
337 cmd_sfunc_ftype *set_func,
338 show_value_ftype *show_func,
339 struct cmd_list_element **set_list,
340 struct cmd_list_element **show_list,
341 struct cmd_list_element **set_result,
342 struct cmd_list_element **show_result)
344 struct cmd_list_element *set;
345 struct cmd_list_element *show;
349 if (help_doc != NULL)
351 full_set_doc = xstrprintf ("%s\n%s", set_doc, help_doc);
352 full_show_doc = xstrprintf ("%s\n%s", show_doc, help_doc);
356 full_set_doc = xstrdup (set_doc);
357 full_show_doc = xstrdup (show_doc);
359 set = add_set_or_show_cmd (name, set_cmd, class, var_type, var,
360 full_set_doc, set_list);
361 if (set_func != NULL)
362 set_cmd_sfunc (set, set_func);
363 show = add_set_or_show_cmd (name, show_cmd, class, var_type, var,
364 full_show_doc, show_list);
365 show->show_value_func = show_func;
367 if (set_result != NULL)
369 if (show_result != NULL)
373 struct cmd_list_element *
374 deprecated_add_set_cmd (char *name,
375 enum command_class class,
379 struct cmd_list_element **list)
381 return add_set_or_show_cmd (name, set_cmd, class, var_type, var, doc, list);
384 /* Add element named NAME to command list LIST (the list for set or
385 some sublist thereof). CLASS is as in add_cmd. ENUMLIST is a list
386 of strings which may follow NAME. VAR is address of the variable
387 which will contain the matching string (from ENUMLIST). */
390 add_setshow_enum_cmd (char *name,
391 enum command_class class,
392 const char *enumlist[],
395 const char *show_doc,
396 const char *help_doc,
397 cmd_sfunc_ftype *set_func,
398 show_value_ftype *show_func,
399 struct cmd_list_element **set_list,
400 struct cmd_list_element **show_list)
402 struct cmd_list_element *c;
403 add_setshow_cmd_full (name, class, var_enum, var,
404 set_doc, show_doc, help_doc,
411 /* Add an auto-boolean command named NAME to both the set and show
412 command list lists. CLASS is as in add_cmd. VAR is address of the
413 variable which will contain the value. DOC is the documentation
414 string. FUNC is the corresponding callback. */
416 add_setshow_auto_boolean_cmd (char *name,
417 enum command_class class,
418 enum auto_boolean *var,
419 const char *set_doc, 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 static const char *auto_boolean_enums[] = { "on", "off", "auto", NULL };
427 struct cmd_list_element *c;
428 add_setshow_cmd_full (name, class, var_auto_boolean, var,
429 set_doc, show_doc, help_doc,
433 c->enums = auto_boolean_enums;
436 /* Add element named NAME to both the set and show command LISTs (the
437 list for set/show or some sublist thereof). CLASS is as in
438 add_cmd. VAR is address of the variable which will contain the
439 value. SET_DOC and SHOW_DOC are the documentation strings. */
441 add_setshow_boolean_cmd (char *name, enum command_class class, int *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 *boolean_enums[] = { "on", "off", NULL };
450 struct cmd_list_element *c;
451 add_setshow_cmd_full (name, class, var_boolean, var,
452 set_doc, show_doc, help_doc,
456 c->enums = boolean_enums;
459 /* Add element named NAME to both the set and show command LISTs (the
460 list for set/show or some sublist thereof). */
462 add_setshow_filename_cmd (char *name, enum command_class class,
464 const char *set_doc, const char *show_doc,
465 const char *help_doc,
466 cmd_sfunc_ftype *set_func,
467 show_value_ftype *show_func,
468 struct cmd_list_element **set_list,
469 struct cmd_list_element **show_list)
471 struct cmd_list_element *set_result;
472 add_setshow_cmd_full (name, class, var_filename, var,
473 set_doc, show_doc, help_doc,
477 set_cmd_completer (set_result, filename_completer);
480 /* Add element named NAME to both the set and show command LISTs (the
481 list for set/show or some sublist thereof). */
483 add_setshow_string_cmd (char *name, enum command_class class,
485 const char *set_doc, const char *show_doc,
486 const char *help_doc,
487 cmd_sfunc_ftype *set_func,
488 show_value_ftype *show_func,
489 struct cmd_list_element **set_list,
490 struct cmd_list_element **show_list)
492 add_setshow_cmd_full (name, class, var_string, var,
493 set_doc, show_doc, help_doc,
499 /* Add element named NAME to both the set and show command LISTs (the
500 list for set/show or some sublist thereof). */
502 add_setshow_string_noescape_cmd (char *name, enum command_class class,
504 const char *set_doc, const char *show_doc,
505 const char *help_doc,
506 cmd_sfunc_ftype *set_func,
507 show_value_ftype *show_func,
508 struct cmd_list_element **set_list,
509 struct cmd_list_element **show_list)
511 add_setshow_cmd_full (name, class, var_string_noescape, var,
512 set_doc, show_doc, help_doc,
518 /* Add element named NAME to both the set and show command LISTs (the
519 list for set/show or some sublist thereof). */
521 add_setshow_optional_filename_cmd (char *name, enum command_class class,
523 const char *set_doc, const char *show_doc,
524 const char *help_doc,
525 cmd_sfunc_ftype *set_func,
526 show_value_ftype *show_func,
527 struct cmd_list_element **set_list,
528 struct cmd_list_element **show_list)
530 add_setshow_cmd_full (name, class, var_optional_filename, var,
531 set_doc, show_doc, help_doc,
537 /* Add element named NAME to both the set and show command LISTs (the
538 list for set/show or some sublist thereof). CLASS is as in
539 add_cmd. VAR is address of the variable which will contain the
540 value. SET_DOC and SHOW_DOC are the documentation strings. */
542 add_setshow_integer_cmd (char *name, enum command_class class,
544 const char *set_doc, const char *show_doc,
545 const char *help_doc,
546 cmd_sfunc_ftype *set_func,
547 show_value_ftype *show_func,
548 struct cmd_list_element **set_list,
549 struct cmd_list_element **show_list)
551 add_setshow_cmd_full (name, class, var_integer, var,
552 set_doc, show_doc, help_doc,
558 /* Add element named NAME to both the set and show command LISTs (the
559 list for set/show or some sublist thereof). CLASS is as in
560 add_cmd. VAR is address of the variable which will contain the
561 value. SET_DOC and SHOW_DOC are the documentation strings. */
563 add_setshow_uinteger_cmd (char *name, enum command_class class,
565 const char *set_doc, const char *show_doc,
566 const char *help_doc,
567 cmd_sfunc_ftype *set_func,
568 show_value_ftype *show_func,
569 struct cmd_list_element **set_list,
570 struct cmd_list_element **show_list)
572 add_setshow_cmd_full (name, class, var_uinteger, var,
573 set_doc, show_doc, help_doc,
579 /* Add element named NAME to both the set and show command LISTs (the
580 list for set/show or some sublist thereof). CLASS is as in
581 add_cmd. VAR is address of the variable which will contain the
582 value. SET_DOC and SHOW_DOC are the documentation strings. */
584 add_setshow_zinteger_cmd (char *name, enum command_class class,
586 const char *set_doc, const char *show_doc,
587 const char *help_doc,
588 cmd_sfunc_ftype *set_func,
589 show_value_ftype *show_func,
590 struct cmd_list_element **set_list,
591 struct cmd_list_element **show_list)
593 add_setshow_cmd_full (name, class, var_zinteger, var,
594 set_doc, show_doc, help_doc,
600 /* Remove the command named NAME from the command list. */
603 delete_cmd (char *name, struct cmd_list_element **list)
605 struct cmd_list_element *c;
606 struct cmd_list_element *p;
608 while (*list && strcmp ((*list)->name, name) == 0)
610 if ((*list)->hookee_pre)
611 (*list)->hookee_pre->hook_pre = 0; /* Hook slips out of its mouth */
612 if ((*list)->hookee_post)
613 (*list)->hookee_post->hook_post = 0; /* Hook slips out of its bottom */
620 for (c = *list; c->next;)
622 if (strcmp (c->next->name, name) == 0)
624 if (c->next->hookee_pre)
625 c->next->hookee_pre->hook_pre = 0; /* hooked cmd gets away. */
626 if (c->next->hookee_post)
627 c->next->hookee_post->hook_post = 0; /* remove post hook */
628 /* :( no fishing metaphore */
638 /* Shorthands to the commands above. */
640 /* Add an element to the list of info subcommands. */
642 struct cmd_list_element *
643 add_info (char *name, void (*fun) (char *, int), char *doc)
645 return add_cmd (name, no_class, fun, doc, &infolist);
648 /* Add an alias to the list of info subcommands. */
650 struct cmd_list_element *
651 add_info_alias (char *name, char *oldname, int abbrev_flag)
653 return add_alias_cmd (name, oldname, 0, abbrev_flag, &infolist);
656 /* Add an element to the list of commands. */
658 struct cmd_list_element *
659 add_com (char *name, enum command_class class, void (*fun) (char *, int),
662 return add_cmd (name, class, fun, doc, &cmdlist);
665 /* Add an alias or abbreviation command to the list of commands. */
667 struct cmd_list_element *
668 add_com_alias (char *name, char *oldname, enum command_class class,
671 return add_alias_cmd (name, oldname, class, abbrev_flag, &cmdlist);
674 /* Recursively walk the commandlist structures, and print out the
675 documentation of commands that match our regex in either their
676 name, or their documentation.
679 apropos_cmd (struct ui_file *stream, struct cmd_list_element *commandlist,
680 struct re_pattern_buffer *regex, char *prefix)
682 struct cmd_list_element *c;
683 int returnvalue=1; /*Needed to avoid double printing*/
684 /* Walk through the commands */
685 for (c=commandlist;c;c=c->next)
689 /* Try to match against the name*/
690 returnvalue=re_search(regex,c->name,strlen(c->name),0,strlen(c->name),NULL);
691 if (returnvalue >= 0)
693 print_help_for_command (c, prefix,
694 0 /* don't recurse */, stream);
697 if (c->doc != NULL && returnvalue != 0)
699 /* Try to match against documentation */
700 if (re_search(regex,c->doc,strlen(c->doc),0,strlen(c->doc),NULL) >=0)
702 print_help_for_command (c, prefix,
703 0 /* don't recurse */, stream);
706 /* Check if this command has subcommands */
707 if (c->prefixlist != NULL)
709 /* Recursively call ourselves on the subcommand list,
710 passing the right prefix in.
712 apropos_cmd (stream,*c->prefixlist,regex,c->prefixname);
717 /* This command really has to deal with two things:
718 * 1) I want documentation on *this string* (usually called by
719 * "help commandname").
720 * 2) I want documentation on *this list* (usually called by
721 * giving a command that requires subcommands. Also called by saying
724 * I am going to split this into two seperate comamnds, help_cmd and
729 help_cmd (char *command, struct ui_file *stream)
731 struct cmd_list_element *c;
732 extern struct cmd_list_element *cmdlist;
736 help_list (cmdlist, "", all_classes, stream);
740 if (strcmp (command, "all") == 0)
746 c = lookup_cmd (&command, cmdlist, "", 0, 0);
751 /* There are three cases here.
752 If c->prefixlist is nonzero, we have a prefix command.
753 Print its documentation, then list its subcommands.
755 If c->func is non NULL, we really have a command. Print its
756 documentation and return.
758 If c->func is NULL, we have a class name. Print its
759 documentation (as if it were a command) and then set class to the
760 number of this class so that the commands in the class will be
763 fputs_filtered (c->doc, stream);
764 fputs_filtered ("\n", stream);
766 if (c->prefixlist == 0 && c->func != NULL)
768 fprintf_filtered (stream, "\n");
770 /* If this is a prefix command, print it's subcommands */
772 help_list (*c->prefixlist, c->prefixname, all_commands, stream);
774 /* If this is a class name, print all of the commands in the class */
776 help_list (cmdlist, "", c->class, stream);
778 if (c->hook_pre || c->hook_post)
779 fprintf_filtered (stream,
780 "\nThis command has a hook (or hooks) defined:\n");
783 fprintf_filtered (stream,
784 "\tThis command is run after : %s (pre hook)\n",
787 fprintf_filtered (stream,
788 "\tThis command is run before : %s (post hook)\n",
793 * Get a specific kind of help on a command list.
796 * CMDTYPE is the prefix to use in the title string.
797 * CLASS is the class with which to list the nodes of this list (see
798 * documentation for help_cmd_list below), As usual, ALL_COMMANDS for
799 * everything, ALL_CLASSES for just classes, and non-negative for only things
800 * in a specific class.
801 * and STREAM is the output stream on which to print things.
802 * If you call this routine with a class >= 0, it recurses.
805 help_list (struct cmd_list_element *list, char *cmdtype,
806 enum command_class class, struct ui_file *stream)
809 char *cmdtype1, *cmdtype2;
811 /* If CMDTYPE is "foo ", CMDTYPE1 gets " foo" and CMDTYPE2 gets "foo sub" */
812 len = strlen (cmdtype);
813 cmdtype1 = (char *) alloca (len + 1);
815 cmdtype2 = (char *) alloca (len + 4);
820 strncpy (cmdtype1 + 1, cmdtype, len - 1);
822 strncpy (cmdtype2, cmdtype, len - 1);
823 strcpy (cmdtype2 + len - 1, " sub");
826 if (class == all_classes)
827 fprintf_filtered (stream, "List of classes of %scommands:\n\n", cmdtype2);
829 fprintf_filtered (stream, "List of %scommands:\n\n", cmdtype2);
831 help_cmd_list (list, class, cmdtype, (int) class >= 0, stream);
833 if (class == all_classes)
835 fprintf_filtered (stream, "\n\
836 Type \"help%s\" followed by a class name for a list of commands in ",
839 fprintf_filtered (stream, "that class.");
841 fprintf_filtered (stream, "\n\
842 Type \"help all\" for the list of all commands.");
845 fprintf_filtered (stream, "\nType \"help%s\" followed by %scommand name ",
848 fputs_filtered ("for ", stream);
850 fputs_filtered ("full ", stream);
852 fputs_filtered ("documentation.\n", stream);
853 fputs_filtered ("Type \"apropos word\" to search "
854 "for commands related to \"word\".\n", stream);
855 fputs_filtered ("Command name abbreviations are allowed if unambiguous.\n",
860 help_all (struct ui_file *stream)
862 struct cmd_list_element *c;
863 extern struct cmd_list_element *cmdlist;
864 int seen_unclassified = 0;
866 for (c = cmdlist; c; c = c->next)
870 /* If this is a class name, print all of the commands in the class */
874 fprintf_filtered (stream, "\nCommand class: %s\n\n", c->name);
875 help_cmd_list (cmdlist, c->class, "", 1, stream);
879 /* While it's expected that all commands are in some class,
880 as a safety measure, we'll print commands outside of any
883 for (c = cmdlist; c; c = c->next)
888 if (c->class == no_class)
890 if (!seen_unclassified)
892 fprintf_filtered (stream, "\nUnclassified commands\n\n");
893 seen_unclassified = 1;
895 print_help_for_command (c, "", 1, stream);
901 /* Print only the first line of STR on STREAM. */
903 print_doc_line (struct ui_file *stream, char *str)
905 static char *line_buffer = 0;
906 static int line_size;
912 line_buffer = (char *) xmalloc (line_size);
916 while (*p && *p != '\n' && *p != '.' && *p != ',')
918 if (p - str > line_size - 1)
920 line_size = p - str + 1;
922 line_buffer = (char *) xmalloc (line_size);
924 strncpy (line_buffer, str, p - str);
925 line_buffer[p - str] = '\0';
926 if (islower (line_buffer[0]))
927 line_buffer[0] = toupper (line_buffer[0]);
928 ui_out_text (uiout, line_buffer);
931 /* Print one-line help for command C.
932 If RECURSE is non-zero, also print one-line descriptions
933 of all prefixed subcommands. */
935 print_help_for_command (struct cmd_list_element *c, char *prefix, int recurse,
936 struct ui_file *stream)
938 fprintf_filtered (stream, "%s%s -- ", prefix, c->name);
939 print_doc_line (stream, c->doc);
940 fputs_filtered ("\n", stream);
943 && c->prefixlist != 0
944 && c->abbrev_flag == 0)
945 /* Subcommands of a prefix command typically have 'all_commands'
946 as class. If we pass CLASS to recursive invocation,
947 most often we won't see anything. */
948 help_cmd_list (*c->prefixlist, all_commands, c->prefixname, 1, stream);
952 * Implement a help command on command list LIST.
953 * RECURSE should be non-zero if this should be done recursively on
954 * all sublists of LIST.
955 * PREFIX is the prefix to print before each command name.
956 * STREAM is the stream upon which the output should be written.
958 * A non-negative class number to list only commands in that
960 * ALL_COMMANDS to list all commands in list.
961 * ALL_CLASSES to list all classes in list.
963 * Note that RECURSE will be active on *all* sublists, not just the
964 * ones selected by the criteria above (ie. the selection mechanism
965 * is at the low level, not the high-level).
968 help_cmd_list (struct cmd_list_element *list, enum command_class class,
969 char *prefix, int recurse, struct ui_file *stream)
971 struct cmd_list_element *c;
973 for (c = list; c; c = c->next)
975 if (c->abbrev_flag == 0 &&
976 (class == all_commands
977 || (class == all_classes && c->func == NULL)
978 || (class == c->class && c->func != NULL)))
980 print_help_for_command (c, prefix, recurse, stream);
986 /* Search the input clist for 'command'. Return the command if
987 found (or NULL if not), and return the number of commands
990 static struct cmd_list_element *
991 find_cmd (char *command, int len, struct cmd_list_element *clist,
992 int ignore_help_classes, int *nfound)
994 struct cmd_list_element *found, *c;
996 found = (struct cmd_list_element *) NULL;
998 for (c = clist; c; c = c->next)
999 if (!strncmp (command, c->name, len)
1000 && (!ignore_help_classes || c->func))
1004 if (c->name[len] == '\0')
1014 find_command_name_length (const char *text)
1016 const char *p = text;
1018 /* Treating underscores as part of command words is important
1019 so that "set args_foo()" doesn't get interpreted as
1020 "set args _foo()". */
1021 /* Some characters are only used for TUI specific commands. However, they
1022 are always allowed for the sake of consistency.
1023 The XDB compatibility characters are only allowed when using the right
1024 mode because they clash with other GDB commands - specifically '/' is
1025 used as a suffix for print, examine and display.
1026 Note that this is larger than the character set allowed when creating
1027 user-defined commands. */
1028 while (isalnum (*p) || *p == '-' || *p == '_' ||
1029 /* Characters used by TUI specific commands. */
1030 *p == '+' || *p == '<' || *p == '>' || *p == '$' ||
1031 /* Characters used for XDB compatibility. */
1032 (xdb_commands && (*p == '!' || *p == '/' || *p == '?')))
1038 /* This routine takes a line of TEXT and a CLIST in which to start the
1039 lookup. When it returns it will have incremented the text pointer past
1040 the section of text it matched, set *RESULT_LIST to point to the list in
1041 which the last word was matched, and will return a pointer to the cmd
1042 list element which the text matches. It will return NULL if no match at
1043 all was possible. It will return -1 (cast appropriately, ick) if ambigous
1044 matches are possible; in this case *RESULT_LIST will be set to point to
1045 the list in which there are ambiguous choices (and *TEXT will be set to
1046 the ambiguous text string).
1048 If the located command was an abbreviation, this routine returns the base
1049 command of the abbreviation.
1051 It does no error reporting whatsoever; control will always return
1052 to the superior routine.
1054 In the case of an ambiguous return (-1), *RESULT_LIST will be set to point
1055 at the prefix_command (ie. the best match) *or* (special case) will be NULL
1056 if no prefix command was ever found. For example, in the case of "info a",
1057 "info" matches without ambiguity, but "a" could be "args" or "address", so
1058 *RESULT_LIST is set to the cmd_list_element for "info". So in this case
1059 RESULT_LIST should not be interpeted as a pointer to the beginning of a
1060 list; it simply points to a specific command. In the case of an ambiguous
1061 return *TEXT is advanced past the last non-ambiguous prefix (e.g.
1062 "info t" can be "info types" or "info target"; upon return *TEXT has been
1063 advanced past "info ").
1065 If RESULT_LIST is NULL, don't set *RESULT_LIST (but don't otherwise
1066 affect the operation).
1068 This routine does *not* modify the text pointed to by TEXT.
1070 If IGNORE_HELP_CLASSES is nonzero, ignore any command list elements which
1071 are actually help classes rather than commands (i.e. the function field of
1072 the struct cmd_list_element is NULL). */
1074 struct cmd_list_element *
1075 lookup_cmd_1 (char **text, struct cmd_list_element *clist,
1076 struct cmd_list_element **result_list, int ignore_help_classes)
1079 int len, tmp, nfound;
1080 struct cmd_list_element *found, *c;
1083 while (**text == ' ' || **text == '\t')
1086 /* Identify the name of the command. */
1087 len = find_command_name_length (*text);
1089 /* If nothing but whitespace, return 0. */
1093 /* *text and p now bracket the first command word to lookup (and
1094 it's length is len). We copy this into a local temporary */
1097 command = (char *) alloca (len + 1);
1098 for (tmp = 0; tmp < len; tmp++)
1100 char x = (*text)[tmp];
1103 command[len] = '\0';
1108 found = find_cmd (command, len, clist, ignore_help_classes, &nfound);
1111 ** We didn't find the command in the entered case, so lower case it
1112 ** and search again.
1114 if (!found || nfound == 0)
1116 for (tmp = 0; tmp < len; tmp++)
1118 char x = command[tmp];
1119 command[tmp] = isupper (x) ? tolower (x) : x;
1121 found = find_cmd (command, len, clist, ignore_help_classes, &nfound);
1124 /* If nothing matches, we have a simple failure. */
1130 if (result_list != NULL)
1131 /* Will be modified in calling routine
1132 if we know what the prefix command is. */
1134 return (struct cmd_list_element *) -1; /* Ambiguous. */
1137 /* We've matched something on this list. Move text pointer forward. */
1141 if (found->cmd_pointer)
1143 /* We drop the alias (abbreviation) in favor of the command it is
1144 pointing to. If the alias is deprecated, though, we need to
1145 warn the user about it before we drop it. Note that while we
1146 are warning about the alias, we may also warn about the command
1147 itself and we will adjust the appropriate DEPRECATED_WARN_USER
1150 if (found->flags & DEPRECATED_WARN_USER)
1151 deprecated_cmd_warning (&line);
1152 found = found->cmd_pointer;
1154 /* If we found a prefix command, keep looking. */
1156 if (found->prefixlist)
1158 c = lookup_cmd_1 (text, *found->prefixlist, result_list,
1159 ignore_help_classes);
1162 /* Didn't find anything; this is as far as we got. */
1163 if (result_list != NULL)
1164 *result_list = clist;
1167 else if (c == (struct cmd_list_element *) -1)
1169 /* We've gotten this far properly, but the next step
1170 is ambiguous. We need to set the result list to the best
1171 we've found (if an inferior hasn't already set it). */
1172 if (result_list != NULL)
1174 /* This used to say *result_list = *found->prefixlist
1175 If that was correct, need to modify the documentation
1176 at the top of this function to clarify what is supposed
1178 *result_list = found;
1189 if (result_list != NULL)
1190 *result_list = clist;
1195 /* All this hair to move the space to the front of cmdtype */
1198 undef_cmd_error (char *cmdtype, char *q)
1200 error (_("Undefined %scommand: \"%s\". Try \"help%s%.*s\"."),
1203 *cmdtype ? " " : "",
1204 (int) strlen (cmdtype) - 1,
1208 /* Look up the contents of *LINE as a command in the command list LIST.
1209 LIST is a chain of struct cmd_list_element's.
1210 If it is found, return the struct cmd_list_element for that command
1211 and update *LINE to point after the command name, at the first argument.
1212 If not found, call error if ALLOW_UNKNOWN is zero
1213 otherwise (or if error returns) return zero.
1214 Call error if specified command is ambiguous,
1215 unless ALLOW_UNKNOWN is negative.
1216 CMDTYPE precedes the word "command" in the error message.
1218 If INGNORE_HELP_CLASSES is nonzero, ignore any command list
1219 elements which are actually help classes rather than commands (i.e.
1220 the function field of the struct cmd_list_element is 0). */
1222 struct cmd_list_element *
1223 lookup_cmd (char **line, struct cmd_list_element *list, char *cmdtype,
1224 int allow_unknown, int ignore_help_classes)
1226 struct cmd_list_element *last_list = 0;
1227 struct cmd_list_element *c;
1229 /* Note: Do not remove trailing whitespace here because this
1230 would be wrong for complete_command. Jim Kingdon */
1233 error (_("Lack of needed %scommand"), cmdtype);
1235 c = lookup_cmd_1 (line, list, &last_list, ignore_help_classes);
1242 int len = find_command_name_length (*line);
1244 q = (char *) alloca (len + 1);
1245 strncpy (q, *line, len);
1247 undef_cmd_error (cmdtype, q);
1252 else if (c == (struct cmd_list_element *) -1)
1254 /* Ambigous. Local values should be off prefixlist or called
1256 int local_allow_unknown = (last_list ? last_list->allow_unknown :
1258 char *local_cmdtype = last_list ? last_list->prefixname : cmdtype;
1259 struct cmd_list_element *local_list =
1260 (last_list ? *(last_list->prefixlist) : list);
1262 if (local_allow_unknown < 0)
1265 return last_list; /* Found something. */
1267 return 0; /* Found nothing. */
1271 /* Report as error. */
1276 ((*line)[amb_len] && (*line)[amb_len] != ' '
1277 && (*line)[amb_len] != '\t');
1282 for (c = local_list; c; c = c->next)
1283 if (!strncmp (*line, c->name, amb_len))
1285 if (strlen (ambbuf) + strlen (c->name) + 6 < (int) sizeof ambbuf)
1287 if (strlen (ambbuf))
1288 strcat (ambbuf, ", ");
1289 strcat (ambbuf, c->name);
1293 strcat (ambbuf, "..");
1297 error (_("Ambiguous %scommand \"%s\": %s."), local_cmdtype,
1299 return 0; /* lint */
1304 /* We've got something. It may still not be what the caller
1305 wants (if this command *needs* a subcommand). */
1306 while (**line == ' ' || **line == '\t')
1309 if (c->prefixlist && **line && !c->allow_unknown)
1310 undef_cmd_error (c->prefixname, *line);
1312 /* Seems to be what he wants. Return it. */
1318 /* We are here presumably because an alias or command in *TEXT is
1319 deprecated and a warning message should be generated. This function
1320 decodes *TEXT and potentially generates a warning message as outlined
1323 Example for 'set endian big' which has a fictitious alias 'seb'.
1325 If alias wasn't used in *TEXT, and the command is deprecated:
1326 "warning: 'set endian big' is deprecated."
1328 If alias was used, and only the alias is deprecated:
1329 "warning: 'seb' an alias for the command 'set endian big' is deprecated."
1331 If alias was used and command is deprecated (regardless of whether the
1332 alias itself is deprecated:
1334 "warning: 'set endian big' (seb) is deprecated."
1336 After the message has been sent, clear the appropriate flags in the
1337 command and/or the alias so the user is no longer bothered.
1341 deprecated_cmd_warning (char **text)
1343 struct cmd_list_element *alias = NULL;
1344 struct cmd_list_element *prefix_cmd = NULL;
1345 struct cmd_list_element *cmd = NULL;
1346 struct cmd_list_element *c;
1349 if (!lookup_cmd_composition (*text, &alias, &prefix_cmd, &cmd))
1350 /* return if text doesn't evaluate to a command */
1353 if (!((alias ? (alias->flags & DEPRECATED_WARN_USER) : 0)
1354 || (cmd->flags & DEPRECATED_WARN_USER) ) )
1355 /* return if nothing is deprecated */
1358 printf_filtered ("Warning:");
1360 if (alias && !(cmd->flags & CMD_DEPRECATED))
1361 printf_filtered (" '%s', an alias for the", alias->name);
1363 printf_filtered (" command '");
1366 printf_filtered ("%s", prefix_cmd->prefixname);
1368 printf_filtered ("%s", cmd->name);
1370 if (alias && (cmd->flags & CMD_DEPRECATED))
1371 printf_filtered ("' (%s) is deprecated.\n", alias->name);
1373 printf_filtered ("' is deprecated.\n");
1376 /* if it is only the alias that is deprecated, we want to indicate the
1377 new alias, otherwise we'll indicate the new command */
1379 if (alias && !(cmd->flags & CMD_DEPRECATED))
1381 if (alias->replacement)
1382 printf_filtered ("Use '%s'.\n\n", alias->replacement);
1384 printf_filtered ("No alternative known.\n\n");
1388 if (cmd->replacement)
1389 printf_filtered ("Use '%s'.\n\n", cmd->replacement);
1391 printf_filtered ("No alternative known.\n\n");
1394 /* We've warned you, now we'll keep quiet */
1396 alias->flags &= ~DEPRECATED_WARN_USER;
1398 cmd->flags &= ~DEPRECATED_WARN_USER;
1403 /* Look up the contents of LINE as a command in the command list 'cmdlist'.
1404 Return 1 on success, 0 on failure.
1406 If LINE refers to an alias, *alias will point to that alias.
1408 If LINE is a postfix command (i.e. one that is preceeded by a prefix
1409 command) set *prefix_cmd.
1411 Set *cmd to point to the command LINE indicates.
1413 If any of *alias, *prefix_cmd, or *cmd cannot be determined or do not
1414 exist, they are NULL when we return.
1418 lookup_cmd_composition (char *text,
1419 struct cmd_list_element **alias,
1420 struct cmd_list_element **prefix_cmd,
1421 struct cmd_list_element **cmd)
1424 int len, tmp, nfound;
1425 struct cmd_list_element *cur_list;
1426 struct cmd_list_element *prev_cmd;
1435 /* Go through as many command lists as we need to
1436 to find the command TEXT refers to. */
1440 while (*text == ' ' || *text == '\t')
1443 /* Identify the name of the command. */
1444 len = find_command_name_length (text);
1446 /* If nothing but whitespace, return. */
1450 /* text is the start of the first command word to lookup (and
1451 it's length is len). We copy this into a local temporary */
1453 command = (char *) alloca (len + 1);
1454 for (tmp = 0; tmp < len; tmp++)
1459 command[len] = '\0';
1464 *cmd = find_cmd (command, len, cur_list, 1, &nfound);
1466 /* We didn't find the command in the entered case, so lower case it
1469 if (!*cmd || nfound == 0)
1471 for (tmp = 0; tmp < len; tmp++)
1473 char x = command[tmp];
1474 command[tmp] = isupper (x) ? tolower (x) : x;
1476 *cmd = find_cmd (command, len, cur_list, 1, &nfound);
1479 if (*cmd == (struct cmd_list_element *) -1)
1481 return 0; /* ambiguous */
1485 return 0; /* nothing found */
1488 if ((*cmd)->cmd_pointer)
1490 /* cmd was actually an alias, we note that an alias was used
1491 (by assigning *alais) and we set *cmd.
1494 *cmd = (*cmd)->cmd_pointer;
1496 *prefix_cmd = prev_cmd;
1498 if ((*cmd)->prefixlist)
1499 cur_list = *(*cmd)->prefixlist;
1507 /* Helper function for SYMBOL_COMPLETION_FUNCTION. */
1509 /* Return a vector of char pointers which point to the different
1510 possible completions in LIST of TEXT.
1512 WORD points in the same buffer as TEXT, and completions should be
1513 returned relative to this position. For example, suppose TEXT is "foo"
1514 and we want to complete to "foobar". If WORD is "oo", return
1515 "oobar"; if WORD is "baz/foo", return "baz/foobar". */
1518 complete_on_cmdlist (struct cmd_list_element *list, char *text, char *word)
1520 struct cmd_list_element *ptr;
1522 int sizeof_matchlist;
1524 int textlen = strlen (text);
1526 sizeof_matchlist = 10;
1527 matchlist = (char **) xmalloc (sizeof_matchlist * sizeof (char *));
1530 for (ptr = list; ptr; ptr = ptr->next)
1531 if (!strncmp (ptr->name, text, textlen)
1532 && !ptr->abbrev_flag
1534 || ptr->prefixlist))
1536 if (matches == sizeof_matchlist)
1538 sizeof_matchlist *= 2;
1539 matchlist = (char **) xrealloc ((char *) matchlist,
1541 * sizeof (char *)));
1544 matchlist[matches] = (char *)
1545 xmalloc (strlen (word) + strlen (ptr->name) + 1);
1547 strcpy (matchlist[matches], ptr->name);
1548 else if (word > text)
1550 /* Return some portion of ptr->name. */
1551 strcpy (matchlist[matches], ptr->name + (word - text));
1555 /* Return some of text plus ptr->name. */
1556 strncpy (matchlist[matches], word, text - word);
1557 matchlist[matches][text - word] = '\0';
1558 strcat (matchlist[matches], ptr->name);
1570 matchlist = (char **) xrealloc ((char *) matchlist, ((matches + 1)
1571 * sizeof (char *)));
1572 matchlist[matches] = (char *) 0;
1578 /* Helper function for SYMBOL_COMPLETION_FUNCTION. */
1580 /* Return a vector of char pointers which point to the different
1581 possible completions in CMD of TEXT.
1583 WORD points in the same buffer as TEXT, and completions should be
1584 returned relative to this position. For example, suppose TEXT is "foo"
1585 and we want to complete to "foobar". If WORD is "oo", return
1586 "oobar"; if WORD is "baz/foo", return "baz/foobar". */
1589 complete_on_enum (const char *enumlist[],
1594 int sizeof_matchlist;
1596 int textlen = strlen (text);
1600 sizeof_matchlist = 10;
1601 matchlist = (char **) xmalloc (sizeof_matchlist * sizeof (char *));
1604 for (i = 0; (name = enumlist[i]) != NULL; i++)
1605 if (strncmp (name, text, textlen) == 0)
1607 if (matches == sizeof_matchlist)
1609 sizeof_matchlist *= 2;
1610 matchlist = (char **) xrealloc ((char *) matchlist,
1612 * sizeof (char *)));
1615 matchlist[matches] = (char *)
1616 xmalloc (strlen (word) + strlen (name) + 1);
1618 strcpy (matchlist[matches], name);
1619 else if (word > text)
1621 /* Return some portion of name. */
1622 strcpy (matchlist[matches], name + (word - text));
1626 /* Return some of text plus name. */
1627 strncpy (matchlist[matches], word, text - word);
1628 matchlist[matches][text - word] = '\0';
1629 strcat (matchlist[matches], name);
1641 matchlist = (char **) xrealloc ((char *) matchlist, ((matches + 1)
1642 * sizeof (char *)));
1643 matchlist[matches] = (char *) 0;
1650 /* check function pointer */
1652 cmd_func_p (struct cmd_list_element *cmd)
1654 return (cmd->func != NULL);
1658 /* call the command function */
1660 cmd_func (struct cmd_list_element *cmd, char *args, int from_tty)
1662 if (cmd_func_p (cmd))
1663 (*cmd->func) (cmd, args, from_tty);
1665 error (_("Invalid command"));