1 /* Handle lists of commands, their decoding and documentation, for GDB.
3 Copyright (C) 1986-2022 Free Software Foundation, Inc.
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 3 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>. */
21 #include "gdbsupport/gdb_regex.h"
22 #include "completer.h"
24 #include "cli/cli-cmds.h"
25 #include "cli/cli-decode.h"
26 #include "cli/cli-style.h"
27 #include "gdbsupport/gdb_optional.h"
29 /* Prototypes for local functions. */
31 static void undef_cmd_error (const char *, const char *);
33 static cmd_list_element::aliases_list_type delete_cmd
34 (const char *name, cmd_list_element **list, cmd_list_element **prehook,
35 cmd_list_element **prehookee, cmd_list_element **posthook,
36 cmd_list_element **posthookee);
38 static struct cmd_list_element *find_cmd (const char *command,
40 struct cmd_list_element *clist,
41 int ignore_help_classes,
44 static void help_cmd_list (struct cmd_list_element *list,
45 enum command_class theclass,
47 struct ui_file *stream);
49 static void help_all (struct ui_file *stream);
51 static int lookup_cmd_composition_1 (const char *text,
52 struct cmd_list_element **alias,
53 struct cmd_list_element **prefix_cmd,
54 struct cmd_list_element **cmd,
55 struct cmd_list_element *cur_list);
57 /* Look up a command whose 'subcommands' field is SUBCOMMANDS. Return the
58 command if found, otherwise return NULL. */
60 static struct cmd_list_element *
61 lookup_cmd_with_subcommands (cmd_list_element **subcommands,
62 cmd_list_element *list)
64 struct cmd_list_element *p = NULL;
66 for (p = list; p != NULL; p = p->next)
68 struct cmd_list_element *q;
73 else if (p->subcommands == subcommands)
75 /* If we found an alias, we must return the aliased
77 return p->is_alias () ? p->alias_target : p;
80 q = lookup_cmd_with_subcommands (subcommands, *(p->subcommands));
89 print_help_for_command (const cmd_list_element &c,
90 bool recurse, struct ui_file *stream);
93 do_simple_func (const char *args, int from_tty, cmd_list_element *c)
95 c->function.simple_func (args, from_tty);
99 set_cmd_simple_func (struct cmd_list_element *cmd, cmd_simple_func_ftype *simple_func)
101 if (simple_func == NULL)
104 cmd->func = do_simple_func;
106 cmd->function.simple_func = simple_func;
110 cmd_simple_func_eq (struct cmd_list_element *cmd, cmd_simple_func_ftype *simple_func)
112 return (cmd->func == do_simple_func
113 && cmd->function.simple_func == simple_func);
117 set_cmd_completer (struct cmd_list_element *cmd, completer_ftype *completer)
119 cmd->completer = completer; /* Ok. */
122 /* See definition in commands.h. */
125 set_cmd_completer_handle_brkchars (struct cmd_list_element *cmd,
126 completer_handle_brkchars_ftype *func)
128 cmd->completer_handle_brkchars = func;
132 cmd_list_element::prefixname () const
134 if (!this->is_prefix ())
135 /* Not a prefix command. */
138 std::string prefixname;
139 if (this->prefix != nullptr)
140 prefixname = this->prefix->prefixname ();
142 prefixname += this->name;
148 /* Add element named NAME.
149 Space for NAME and DOC must be allocated by the caller.
150 CLASS is the top level category into which commands are broken down
152 FUN should be the function to execute the command;
153 it will get a character string as argument, with leading
154 and trailing blanks already eliminated.
156 DOC is a documentation string for the command.
157 Its first line should be a complete sentence.
158 It should start with ? for a command that is an abbreviation
159 or with * for a command that most users don't need to know about.
161 Add this command to command list *LIST.
163 Returns a pointer to the added command (not necessarily the head
166 static struct cmd_list_element *
167 do_add_cmd (const char *name, enum command_class theclass,
168 const char *doc, struct cmd_list_element **list)
170 struct cmd_list_element *c = new struct cmd_list_element (name, theclass,
173 /* Turn each alias of the old command into an alias of the new
175 c->aliases = delete_cmd (name, list, &c->hook_pre, &c->hookee_pre,
176 &c->hook_post, &c->hookee_post);
178 for (cmd_list_element &alias : c->aliases)
179 alias.alias_target = c;
182 c->hook_pre->hookee_pre = c;
185 c->hookee_pre->hook_pre = c;
188 c->hook_post->hookee_post = c;
191 c->hookee_post->hook_post = c;
193 if (*list == NULL || strcmp ((*list)->name, name) >= 0)
200 cmd_list_element *p = *list;
201 while (p->next && strcmp (p->next->name, name) <= 0)
209 /* Search the prefix cmd of C, and assigns it to C->prefix.
210 See also add_prefix_cmd and update_prefix_field_of_prefixed_commands. */
211 cmd_list_element *prefixcmd = lookup_cmd_with_subcommands (list, cmdlist);
212 c->prefix = prefixcmd;
218 struct cmd_list_element *
219 add_cmd (const char *name, enum command_class theclass,
220 const char *doc, struct cmd_list_element **list)
222 cmd_list_element *result = do_add_cmd (name, theclass, doc, list);
224 result->function.simple_func = NULL;
228 struct cmd_list_element *
229 add_cmd (const char *name, enum command_class theclass,
230 cmd_simple_func_ftype *fun,
231 const char *doc, struct cmd_list_element **list)
233 cmd_list_element *result = do_add_cmd (name, theclass, doc, list);
234 set_cmd_simple_func (result, fun);
238 /* Add an element with a suppress notification to the LIST of commands. */
240 struct cmd_list_element *
241 add_cmd_suppress_notification (const char *name, enum command_class theclass,
242 cmd_simple_func_ftype *fun, const char *doc,
243 struct cmd_list_element **list,
244 int *suppress_notification)
246 struct cmd_list_element *element;
248 element = add_cmd (name, theclass, fun, doc, list);
249 element->suppress_notification = suppress_notification;
255 /* Deprecates a command CMD.
256 REPLACEMENT is the name of the command which should be used in
257 place of this command, or NULL if no such command exists.
259 This function does not check to see if command REPLACEMENT exists
260 since gdb may not have gotten around to adding REPLACEMENT when
261 this function is called.
263 Returns a pointer to the deprecated command. */
265 struct cmd_list_element *
266 deprecate_cmd (struct cmd_list_element *cmd, const char *replacement)
268 cmd->cmd_deprecated = 1;
269 cmd->deprecated_warn_user = 1;
271 if (replacement != NULL)
272 cmd->replacement = replacement;
274 cmd->replacement = NULL;
279 struct cmd_list_element *
280 add_alias_cmd (const char *name, cmd_list_element *target,
281 enum command_class theclass, int abbrev_flag,
282 struct cmd_list_element **list)
284 gdb_assert (target != nullptr);
286 struct cmd_list_element *c = add_cmd (name, theclass, target->doc, list);
288 /* If TARGET->DOC can be freed, we should make another copy. */
289 if (target->doc_allocated)
291 c->doc = xstrdup (target->doc);
292 c->doc_allocated = 1;
294 /* NOTE: Both FUNC and all the FUNCTIONs need to be copied. */
295 c->func = target->func;
296 c->function = target->function;
297 c->subcommands = target->subcommands;
298 c->allow_unknown = target->allow_unknown;
299 c->abbrev_flag = abbrev_flag;
300 c->alias_target = target;
301 target->aliases.push_front (*c);
306 /* Update the prefix field of all sub-commands of the prefix command C.
307 We must do this when a prefix command is defined as the GDB init sequence
308 does not guarantee that a prefix command is created before its sub-commands.
309 For example, break-catch-sig.c initialization runs before breakpoint.c
310 initialization, but it is breakpoint.c that creates the "catch" command used
311 by the "catch signal" command created by break-catch-sig.c. */
314 update_prefix_field_of_prefixed_commands (struct cmd_list_element *c)
316 for (cmd_list_element *p = *c->subcommands; p != NULL; p = p->next)
320 /* We must recursively update the prefix field to cover
321 e.g. 'info auto-load libthread-db' where the creation
326 In such a case, when 'auto-load' was created by do_add_cmd,
327 the 'libthread-db' prefix field could not be updated, as the
328 'auto-load' command was not yet reachable by
329 lookup_cmd_for_subcommands (list, cmdlist)
330 that searches from the top level 'cmdlist'. */
332 update_prefix_field_of_prefixed_commands (p);
337 /* Like add_cmd but adds an element for a command prefix: a name that
338 should be followed by a subcommand to be looked up in another
339 command list. SUBCOMMANDS should be the address of the variable
340 containing that list. */
342 struct cmd_list_element *
343 add_prefix_cmd (const char *name, enum command_class theclass,
344 cmd_simple_func_ftype *fun,
345 const char *doc, struct cmd_list_element **subcommands,
346 int allow_unknown, struct cmd_list_element **list)
348 struct cmd_list_element *c = add_cmd (name, theclass, fun, doc, list);
350 c->subcommands = subcommands;
351 c->allow_unknown = allow_unknown;
353 /* Now that prefix command C is defined, we need to set the prefix field
354 of all prefixed commands that were defined before C itself was defined. */
355 update_prefix_field_of_prefixed_commands (c);
360 /* A helper function for add_basic_prefix_cmd. This is a command
361 function that just forwards to help_list. */
364 do_prefix_cmd (const char *args, int from_tty, struct cmd_list_element *c)
366 /* Look past all aliases. */
367 while (c->is_alias ())
370 help_list (*c->subcommands, c->prefixname ().c_str (),
371 all_commands, gdb_stdout);
376 struct cmd_list_element *
377 add_basic_prefix_cmd (const char *name, enum command_class theclass,
378 const char *doc, struct cmd_list_element **subcommands,
379 int allow_unknown, struct cmd_list_element **list)
381 struct cmd_list_element *cmd = add_prefix_cmd (name, theclass, nullptr,
383 allow_unknown, list);
384 cmd->func = do_prefix_cmd;
388 /* A helper function for add_show_prefix_cmd. This is a command
389 function that just forwards to cmd_show_list. */
392 do_show_prefix_cmd (const char *args, int from_tty, struct cmd_list_element *c)
394 cmd_show_list (*c->subcommands, from_tty);
399 struct cmd_list_element *
400 add_show_prefix_cmd (const char *name, enum command_class theclass,
401 const char *doc, struct cmd_list_element **subcommands,
402 int allow_unknown, struct cmd_list_element **list)
404 struct cmd_list_element *cmd = add_prefix_cmd (name, theclass, nullptr,
406 allow_unknown, list);
407 cmd->func = do_show_prefix_cmd;
414 add_setshow_prefix_cmd (const char *name, command_class theclass,
415 const char *set_doc, const char *show_doc,
416 cmd_list_element **set_subcommands_list,
417 cmd_list_element **show_subcommands_list,
418 cmd_list_element **set_list,
419 cmd_list_element **show_list)
421 set_show_commands cmds;
423 cmds.set = add_basic_prefix_cmd (name, theclass, set_doc,
424 set_subcommands_list, 0,
426 cmds.show = add_show_prefix_cmd (name, theclass, show_doc,
427 show_subcommands_list, 0,
433 /* Like ADD_PREFIX_CMD but sets the suppress_notification pointer on the
434 new command list element. */
436 struct cmd_list_element *
437 add_prefix_cmd_suppress_notification
438 (const char *name, enum command_class theclass,
439 cmd_simple_func_ftype *fun,
440 const char *doc, struct cmd_list_element **subcommands,
441 int allow_unknown, struct cmd_list_element **list,
442 int *suppress_notification)
444 struct cmd_list_element *element
445 = add_prefix_cmd (name, theclass, fun, doc, subcommands,
446 allow_unknown, list);
447 element->suppress_notification = suppress_notification;
451 /* Like add_prefix_cmd but sets the abbrev_flag on the new command. */
453 struct cmd_list_element *
454 add_abbrev_prefix_cmd (const char *name, enum command_class theclass,
455 cmd_simple_func_ftype *fun, const char *doc,
456 struct cmd_list_element **subcommands,
457 int allow_unknown, struct cmd_list_element **list)
459 struct cmd_list_element *c = add_cmd (name, theclass, fun, doc, list);
461 c->subcommands = subcommands;
462 c->allow_unknown = allow_unknown;
467 /* This is an empty "simple func". */
469 not_just_help_class_command (const char *args, int from_tty)
473 /* This is an empty cmd func. */
476 empty_func (const char *args, int from_tty, cmd_list_element *c)
480 /* Add element named NAME to command list LIST (the list for set/show
481 or some sublist thereof).
482 TYPE is set_cmd or show_cmd.
483 CLASS is as in add_cmd.
484 VAR_TYPE is the kind of thing we are setting.
485 VAR is address of the variable being controlled by this command.
486 SET_SETTING_FUNC is a pointer to an optional function callback used to set
488 GET_SETTING_FUNC is a pointer to an optional function callback used to get
490 DOC is the documentation string. */
492 static struct cmd_list_element *
493 add_set_or_show_cmd (const char *name,
495 enum command_class theclass,
497 const setting::erased_args &arg,
499 struct cmd_list_element **list)
501 struct cmd_list_element *c = add_cmd (name, theclass, doc, list);
503 gdb_assert (type == set_cmd || type == show_cmd);
505 c->var.emplace (var_type, arg);
507 /* This needs to be something besides NULL so that this isn't
508 treated as a help class. */
509 c->func = empty_func;
513 /* Add element named NAME to both the command SET_LIST and SHOW_LIST.
514 CLASS is as in add_cmd. VAR_TYPE is the kind of thing we are
515 setting. VAR is address of the variable being controlled by this
516 command. If nullptr is given as VAR, then both SET_SETTING_FUNC and
517 GET_SETTING_FUNC must be provided. SET_SETTING_FUNC and GET_SETTING_FUNC are
518 callbacks used to access and modify the underlying property, whatever its
519 storage is. SET_FUNC and SHOW_FUNC are the callback functions (if non-NULL).
520 SET_DOC, SHOW_DOC and HELP_DOC are the documentation strings.
522 Return the newly created set and show commands. */
524 static set_show_commands
525 add_setshow_cmd_full_erased (const char *name,
526 enum command_class theclass,
528 const setting::erased_args &args,
529 const char *set_doc, const char *show_doc,
530 const char *help_doc,
531 cmd_func_ftype *set_func,
532 show_value_ftype *show_func,
533 struct cmd_list_element **set_list,
534 struct cmd_list_element **show_list)
536 struct cmd_list_element *set;
537 struct cmd_list_element *show;
538 gdb::unique_xmalloc_ptr<char> full_set_doc;
539 gdb::unique_xmalloc_ptr<char> full_show_doc;
541 if (help_doc != NULL)
543 full_set_doc = xstrprintf ("%s\n%s", set_doc, help_doc);
544 full_show_doc = xstrprintf ("%s\n%s", show_doc, help_doc);
548 full_set_doc = make_unique_xstrdup (set_doc);
549 full_show_doc = make_unique_xstrdup (show_doc);
551 set = add_set_or_show_cmd (name, set_cmd, theclass, var_type, args,
552 full_set_doc.release (), set_list);
553 set->doc_allocated = 1;
555 if (set_func != NULL)
556 set->func = set_func;
558 show = add_set_or_show_cmd (name, show_cmd, theclass, var_type, args,
559 full_show_doc.release (), show_list);
560 show->doc_allocated = 1;
561 show->show_value_func = show_func;
562 /* Disable the default symbol completer. Doesn't make much sense
563 for the "show" command to complete on anything. */
564 set_cmd_completer (show, nullptr);
570 static set_show_commands
571 add_setshow_cmd_full (const char *name,
572 enum command_class theclass,
573 var_types var_type, T *var,
574 const char *set_doc, const char *show_doc,
575 const char *help_doc,
576 typename setting_func_types<T>::set set_setting_func,
577 typename setting_func_types<T>::get get_setting_func,
578 cmd_func_ftype *set_func,
579 show_value_ftype *show_func,
580 struct cmd_list_element **set_list,
581 struct cmd_list_element **show_list)
584 = setting::erase_args (var_type, var,
585 set_setting_func, get_setting_func);
587 return add_setshow_cmd_full_erased (name,
589 var_type, erased_args,
598 /* Add element named NAME to command list LIST (the list for set or
599 some sublist thereof). CLASS is as in add_cmd. ENUMLIST is a list
600 of strings which may follow NAME. VAR is address of the variable
601 which will contain the matching string (from ENUMLIST). */
604 add_setshow_enum_cmd (const char *name,
605 enum command_class theclass,
606 const char *const *enumlist,
609 const char *show_doc,
610 const char *help_doc,
611 cmd_func_ftype *set_func,
612 show_value_ftype *show_func,
613 struct cmd_list_element **set_list,
614 struct cmd_list_element **show_list)
616 set_show_commands commands
617 = add_setshow_cmd_full<const char *> (name, theclass, var_enum, var,
618 set_doc, show_doc, help_doc,
619 nullptr, nullptr, set_func,
620 show_func, set_list, show_list);
621 commands.set->enums = enumlist;
625 /* Same as above but using a getter and a setter function instead of a pointer
626 to a global storage buffer. */
629 add_setshow_enum_cmd (const char *name, command_class theclass,
630 const char *const *enumlist, const char *set_doc,
631 const char *show_doc, const char *help_doc,
632 setting_func_types<const char *>::set set_func,
633 setting_func_types<const char *>::get get_func,
634 show_value_ftype *show_func,
635 cmd_list_element **set_list,
636 cmd_list_element **show_list)
638 auto cmds = add_setshow_cmd_full<const char *> (name, theclass, var_enum,
639 nullptr, set_doc, show_doc,
640 help_doc, set_func, get_func,
641 nullptr, show_func, set_list,
644 cmds.set->enums = enumlist;
649 /* See cli-decode.h. */
650 const char * const auto_boolean_enums[] = { "on", "off", "auto", NULL };
652 /* Add an auto-boolean command named NAME to both the set and show
653 command list lists. CLASS is as in add_cmd. VAR is address of the
654 variable which will contain the value. DOC is the documentation
655 string. FUNC is the corresponding callback. */
658 add_setshow_auto_boolean_cmd (const char *name,
659 enum command_class theclass,
660 enum auto_boolean *var,
661 const char *set_doc, const char *show_doc,
662 const char *help_doc,
663 cmd_func_ftype *set_func,
664 show_value_ftype *show_func,
665 struct cmd_list_element **set_list,
666 struct cmd_list_element **show_list)
668 set_show_commands commands
669 = add_setshow_cmd_full<enum auto_boolean> (name, theclass, var_auto_boolean,
670 var, set_doc, show_doc, help_doc,
671 nullptr, nullptr, set_func,
672 show_func, set_list, show_list);
674 commands.set->enums = auto_boolean_enums;
679 /* Same as above but using a getter and a setter function instead of a pointer
680 to a global storage buffer. */
683 add_setshow_auto_boolean_cmd (const char *name, command_class theclass,
684 const char *set_doc, const char *show_doc,
685 const char *help_doc,
686 setting_func_types<enum auto_boolean>::set set_func,
687 setting_func_types<enum auto_boolean>::get get_func,
688 show_value_ftype *show_func,
689 cmd_list_element **set_list,
690 cmd_list_element **show_list)
692 auto cmds = add_setshow_cmd_full<enum auto_boolean> (name, theclass,
698 set_list, show_list);
700 cmds.set->enums = auto_boolean_enums;
705 /* See cli-decode.h. */
706 const char * const boolean_enums[] = { "on", "off", NULL };
708 /* Add element named NAME to both the set and show command LISTs (the
709 list for set/show or some sublist thereof). CLASS is as in
710 add_cmd. VAR is address of the variable which will contain the
711 value. SET_DOC and SHOW_DOC are the documentation strings.
712 Returns the new command element. */
715 add_setshow_boolean_cmd (const char *name, enum command_class theclass, bool *var,
716 const char *set_doc, const char *show_doc,
717 const char *help_doc,
718 cmd_func_ftype *set_func,
719 show_value_ftype *show_func,
720 struct cmd_list_element **set_list,
721 struct cmd_list_element **show_list)
723 set_show_commands commands
724 = add_setshow_cmd_full<bool> (name, theclass, var_boolean, var,
725 set_doc, show_doc, help_doc,
726 nullptr, nullptr, set_func, show_func,
727 set_list, show_list);
729 commands.set->enums = boolean_enums;
734 /* Same as above but using a getter and a setter function instead of a pointer
735 to a global storage buffer. */
738 add_setshow_boolean_cmd (const char *name, command_class theclass,
739 const char *set_doc, const char *show_doc,
740 const char *help_doc,
741 setting_func_types<bool>::set set_func,
742 setting_func_types<bool>::get get_func,
743 show_value_ftype *show_func,
744 cmd_list_element **set_list,
745 cmd_list_element **show_list)
747 auto cmds = add_setshow_cmd_full<bool> (name, theclass, var_boolean, nullptr,
748 set_doc, show_doc, help_doc,
749 set_func, get_func, nullptr,
750 show_func, set_list, show_list);
752 cmds.set->enums = boolean_enums;
757 /* Add element named NAME to both the set and show command LISTs (the
758 list for set/show or some sublist thereof). */
761 add_setshow_filename_cmd (const char *name, enum command_class theclass,
763 const char *set_doc, const char *show_doc,
764 const char *help_doc,
765 cmd_func_ftype *set_func,
766 show_value_ftype *show_func,
767 struct cmd_list_element **set_list,
768 struct cmd_list_element **show_list)
770 set_show_commands commands
771 = add_setshow_cmd_full<std::string> (name, theclass, var_filename, var,
772 set_doc, show_doc, help_doc,
773 nullptr, nullptr, set_func,
774 show_func, set_list, show_list);
776 set_cmd_completer (commands.set, filename_completer);
781 /* Same as above but using a getter and a setter function instead of a pointer
782 to a global storage buffer. */
785 add_setshow_filename_cmd (const char *name, command_class theclass,
786 const char *set_doc, const char *show_doc,
787 const char *help_doc,
788 setting_func_types<std::string>::set set_func,
789 setting_func_types<std::string>::get get_func,
790 show_value_ftype *show_func,
791 cmd_list_element **set_list,
792 cmd_list_element **show_list)
794 auto cmds = add_setshow_cmd_full<std::string> (name, theclass, var_filename,
795 nullptr, set_doc, show_doc,
796 help_doc, set_func, get_func,
797 nullptr, show_func, set_list,
800 set_cmd_completer (cmds.set, filename_completer);
805 /* Add element named NAME to both the set and show command LISTs (the
806 list for set/show or some sublist thereof). */
809 add_setshow_string_cmd (const char *name, enum command_class theclass,
811 const char *set_doc, const char *show_doc,
812 const char *help_doc,
813 cmd_func_ftype *set_func,
814 show_value_ftype *show_func,
815 struct cmd_list_element **set_list,
816 struct cmd_list_element **show_list)
818 set_show_commands commands
819 = add_setshow_cmd_full<std::string> (name, theclass, var_string, var,
820 set_doc, show_doc, help_doc,
821 nullptr, nullptr, set_func,
822 show_func, set_list, show_list);
824 /* Disable the default symbol completer. */
825 set_cmd_completer (commands.set, nullptr);
830 /* Same as above but using a getter and a setter function instead of a pointer
831 to a global storage buffer. */
834 add_setshow_string_cmd (const char *name, command_class theclass,
835 const char *set_doc, const char *show_doc,
836 const char *help_doc,
837 setting_func_types<std::string>::set set_func,
838 setting_func_types<std::string>::get get_func,
839 show_value_ftype *show_func,
840 cmd_list_element **set_list,
841 cmd_list_element **show_list)
843 auto cmds = add_setshow_cmd_full<std::string> (name, theclass, var_string,
844 nullptr, set_doc, show_doc,
845 help_doc, set_func, get_func,
846 nullptr, show_func, set_list,
849 /* Disable the default symbol completer. */
850 set_cmd_completer (cmds.set, nullptr);
855 /* Add element named NAME to both the set and show command LISTs (the
856 list for set/show or some sublist thereof). */
859 add_setshow_string_noescape_cmd (const char *name, enum command_class theclass,
861 const char *set_doc, const char *show_doc,
862 const char *help_doc,
863 cmd_func_ftype *set_func,
864 show_value_ftype *show_func,
865 struct cmd_list_element **set_list,
866 struct cmd_list_element **show_list)
868 set_show_commands commands
869 = add_setshow_cmd_full<std::string> (name, theclass, var_string_noescape,
870 var, set_doc, show_doc, help_doc,
871 nullptr, nullptr, set_func, show_func,
872 set_list, show_list);
874 /* Disable the default symbol completer. */
875 set_cmd_completer (commands.set, nullptr);
880 /* Same as above but using a getter and a setter function instead of a pointer
881 to a global storage buffer. */
884 add_setshow_string_noescape_cmd (const char *name, command_class theclass,
885 const char *set_doc, const char *show_doc,
886 const char *help_doc,
887 setting_func_types<std::string>::set set_func,
888 setting_func_types<std::string>::get get_func,
889 show_value_ftype *show_func,
890 cmd_list_element **set_list,
891 cmd_list_element **show_list)
893 auto cmds = add_setshow_cmd_full<std::string> (name, theclass,
894 var_string_noescape, nullptr,
895 set_doc, show_doc, help_doc,
897 nullptr, show_func, set_list,
900 /* Disable the default symbol completer. */
901 set_cmd_completer (cmds.set, nullptr);
906 /* Add element named NAME to both the set and show command LISTs (the
907 list for set/show or some sublist thereof). */
910 add_setshow_optional_filename_cmd (const char *name, enum command_class theclass,
912 const char *set_doc, const char *show_doc,
913 const char *help_doc,
914 cmd_func_ftype *set_func,
915 show_value_ftype *show_func,
916 struct cmd_list_element **set_list,
917 struct cmd_list_element **show_list)
919 set_show_commands commands
920 = add_setshow_cmd_full<std::string> (name, theclass, var_optional_filename,
921 var, set_doc, show_doc, help_doc,
922 nullptr, nullptr, set_func, show_func,
923 set_list, show_list);
925 set_cmd_completer (commands.set, filename_completer);
930 /* Same as above but using a getter and a setter function instead of a pointer
931 to a global storage buffer. */
934 add_setshow_optional_filename_cmd (const char *name, command_class theclass,
935 const char *set_doc, const char *show_doc,
936 const char *help_doc,
937 setting_func_types<std::string>::set set_func,
938 setting_func_types<std::string>::get get_func,
939 show_value_ftype *show_func,
940 cmd_list_element **set_list,
941 cmd_list_element **show_list)
944 add_setshow_cmd_full<std::string> (name, theclass, var_optional_filename,
945 nullptr, set_doc, show_doc, help_doc,
946 set_func, get_func, nullptr, show_func,
949 set_cmd_completer (cmds.set, filename_completer);
954 /* Completes on literal "unlimited". Used by integer commands that
955 support a special "unlimited" value. */
958 integer_unlimited_completer (struct cmd_list_element *ignore,
959 completion_tracker &tracker,
960 const char *text, const char *word)
962 static const char * const keywords[] =
968 complete_on_enum (tracker, keywords, text, word);
971 /* Add element named NAME to both the set and show command LISTs (the
972 list for set/show or some sublist thereof). CLASS is as in
973 add_cmd. VAR is address of the variable which will contain the
974 value. SET_DOC and SHOW_DOC are the documentation strings. This
975 function is only used in Python API. Please don't use it elsewhere. */
978 add_setshow_integer_cmd (const char *name, enum command_class theclass,
980 const char *set_doc, const char *show_doc,
981 const char *help_doc,
982 cmd_func_ftype *set_func,
983 show_value_ftype *show_func,
984 struct cmd_list_element **set_list,
985 struct cmd_list_element **show_list)
987 set_show_commands commands
988 = add_setshow_cmd_full<int> (name, theclass, var_integer, var,
989 set_doc, show_doc, help_doc,
990 nullptr, nullptr, set_func,
991 show_func, set_list, show_list);
993 set_cmd_completer (commands.set, integer_unlimited_completer);
998 /* Same as above but using a getter and a setter function instead of a pointer
999 to a global storage buffer. */
1002 add_setshow_integer_cmd (const char *name, command_class theclass,
1003 const char *set_doc, const char *show_doc,
1004 const char *help_doc,
1005 setting_func_types<int>::set set_func,
1006 setting_func_types<int>::get get_func,
1007 show_value_ftype *show_func,
1008 cmd_list_element **set_list,
1009 cmd_list_element **show_list)
1011 auto cmds = add_setshow_cmd_full<int> (name, theclass, var_integer, nullptr,
1012 set_doc, show_doc, help_doc, set_func,
1013 get_func, nullptr, show_func, set_list,
1016 set_cmd_completer (cmds.set, integer_unlimited_completer);
1021 /* Add element named NAME to both the set and show command LISTs (the
1022 list for set/show or some sublist thereof). CLASS is as in
1023 add_cmd. VAR is address of the variable which will contain the
1024 value. SET_DOC and SHOW_DOC are the documentation strings. */
1027 add_setshow_uinteger_cmd (const char *name, enum command_class theclass,
1029 const char *set_doc, const char *show_doc,
1030 const char *help_doc,
1031 cmd_func_ftype *set_func,
1032 show_value_ftype *show_func,
1033 struct cmd_list_element **set_list,
1034 struct cmd_list_element **show_list)
1036 set_show_commands commands
1037 = add_setshow_cmd_full<unsigned int> (name, theclass, var_uinteger, var,
1038 set_doc, show_doc, help_doc,
1039 nullptr, nullptr, set_func,
1040 show_func, set_list, show_list);
1042 set_cmd_completer (commands.set, integer_unlimited_completer);
1047 /* Same as above but using a getter and a setter function instead of a pointer
1048 to a global storage buffer. */
1051 add_setshow_uinteger_cmd (const char *name, command_class theclass,
1052 const char *set_doc, const char *show_doc,
1053 const char *help_doc,
1054 setting_func_types<unsigned int>::set set_func,
1055 setting_func_types<unsigned int>::get get_func,
1056 show_value_ftype *show_func,
1057 cmd_list_element **set_list,
1058 cmd_list_element **show_list)
1060 auto cmds = add_setshow_cmd_full<unsigned int> (name, theclass, var_uinteger,
1061 nullptr, set_doc, show_doc,
1062 help_doc, set_func, get_func,
1063 nullptr, show_func, set_list,
1066 set_cmd_completer (cmds.set, integer_unlimited_completer);
1071 /* Add element named NAME to both the set and show command LISTs (the
1072 list for set/show or some sublist thereof). CLASS is as in
1073 add_cmd. VAR is address of the variable which will contain the
1074 value. SET_DOC and SHOW_DOC are the documentation strings. */
1077 add_setshow_zinteger_cmd (const char *name, enum command_class theclass,
1079 const char *set_doc, const char *show_doc,
1080 const char *help_doc,
1081 cmd_func_ftype *set_func,
1082 show_value_ftype *show_func,
1083 struct cmd_list_element **set_list,
1084 struct cmd_list_element **show_list)
1086 return add_setshow_cmd_full<int> (name, theclass, var_zinteger, var,
1087 set_doc, show_doc, help_doc,
1088 nullptr, nullptr, set_func,
1089 show_func, set_list, show_list);
1092 /* Same as above but using a getter and a setter function instead of a pointer
1093 to a global storage buffer. */
1096 add_setshow_zinteger_cmd (const char *name, command_class theclass,
1097 const char *set_doc, const char *show_doc,
1098 const char *help_doc,
1099 setting_func_types<int>::set set_func,
1100 setting_func_types<int>::get get_func,
1101 show_value_ftype *show_func,
1102 cmd_list_element **set_list,
1103 cmd_list_element **show_list)
1105 return add_setshow_cmd_full<int> (name, theclass, var_zinteger, nullptr,
1106 set_doc, show_doc, help_doc, set_func,
1107 get_func, nullptr, show_func, set_list,
1112 add_setshow_zuinteger_unlimited_cmd (const char *name,
1113 enum command_class theclass,
1115 const char *set_doc,
1116 const char *show_doc,
1117 const char *help_doc,
1118 cmd_func_ftype *set_func,
1119 show_value_ftype *show_func,
1120 struct cmd_list_element **set_list,
1121 struct cmd_list_element **show_list)
1123 set_show_commands commands
1124 = add_setshow_cmd_full<int> (name, theclass, var_zuinteger_unlimited, var,
1125 set_doc, show_doc, help_doc, nullptr,
1126 nullptr, set_func, show_func, set_list,
1129 set_cmd_completer (commands.set, integer_unlimited_completer);
1134 /* Same as above but using a getter and a setter function instead of a pointer
1135 to a global storage buffer. */
1138 add_setshow_zuinteger_unlimited_cmd (const char *name, command_class theclass,
1139 const char *set_doc, const char *show_doc,
1140 const char *help_doc,
1141 setting_func_types<int>::set set_func,
1142 setting_func_types<int>::get get_func,
1143 show_value_ftype *show_func,
1144 cmd_list_element **set_list,
1145 cmd_list_element **show_list)
1148 = add_setshow_cmd_full<int> (name, theclass, var_zuinteger_unlimited,
1149 nullptr, set_doc, show_doc, help_doc, set_func,
1150 get_func, nullptr, show_func, set_list,
1153 set_cmd_completer (cmds.set, integer_unlimited_completer);
1158 /* Add element named NAME to both the set and show command LISTs (the
1159 list for set/show or some sublist thereof). CLASS is as in
1160 add_cmd. VAR is address of the variable which will contain the
1161 value. SET_DOC and SHOW_DOC are the documentation strings. */
1164 add_setshow_zuinteger_cmd (const char *name, enum command_class theclass,
1166 const char *set_doc, const char *show_doc,
1167 const char *help_doc,
1168 cmd_func_ftype *set_func,
1169 show_value_ftype *show_func,
1170 struct cmd_list_element **set_list,
1171 struct cmd_list_element **show_list)
1173 return add_setshow_cmd_full<unsigned int> (name, theclass, var_zuinteger,
1174 var, set_doc, show_doc, help_doc,
1175 nullptr, nullptr, set_func,
1176 show_func, set_list, show_list);
1179 /* Same as above but using a getter and a setter function instead of a pointer
1180 to a global storage buffer. */
1183 add_setshow_zuinteger_cmd (const char *name, command_class theclass,
1184 const char *set_doc, const char *show_doc,
1185 const char *help_doc,
1186 setting_func_types<unsigned int>::set set_func,
1187 setting_func_types<unsigned int>::get get_func,
1188 show_value_ftype *show_func,
1189 cmd_list_element **set_list,
1190 cmd_list_element **show_list)
1192 return add_setshow_cmd_full<unsigned int> (name, theclass, var_zuinteger,
1193 nullptr, set_doc, show_doc,
1194 help_doc, set_func, get_func,
1195 nullptr, show_func, set_list,
1199 /* Remove the command named NAME from the command list. Return the list
1200 commands which were aliased to the deleted command. The various *HOOKs are
1201 set to the pre- and post-hook commands for the deleted command. If the
1202 command does not have a hook, the corresponding out parameter is set to
1205 static cmd_list_element::aliases_list_type
1206 delete_cmd (const char *name, struct cmd_list_element **list,
1207 struct cmd_list_element **prehook,
1208 struct cmd_list_element **prehookee,
1209 struct cmd_list_element **posthook,
1210 struct cmd_list_element **posthookee)
1212 struct cmd_list_element *iter;
1213 struct cmd_list_element **previous_chain_ptr;
1214 cmd_list_element::aliases_list_type aliases;
1220 previous_chain_ptr = list;
1222 for (iter = *previous_chain_ptr; iter; iter = *previous_chain_ptr)
1224 if (strcmp (iter->name, name) == 0)
1226 if (iter->destroyer)
1227 iter->destroyer (iter, iter->context ());
1229 if (iter->hookee_pre)
1230 iter->hookee_pre->hook_pre = 0;
1231 *prehook = iter->hook_pre;
1232 *prehookee = iter->hookee_pre;
1233 if (iter->hookee_post)
1234 iter->hookee_post->hook_post = 0;
1235 *posthook = iter->hook_post;
1236 *posthookee = iter->hookee_post;
1238 /* Update the link. */
1239 *previous_chain_ptr = iter->next;
1241 aliases = std::move (iter->aliases);
1243 /* If this command was an alias, remove it from the list of
1245 if (iter->is_alias ())
1247 auto it = iter->alias_target->aliases.iterator_to (*iter);
1248 iter->alias_target->aliases.erase (it);
1253 /* We won't see another command with the same name. */
1257 previous_chain_ptr = &iter->next;
1263 /* Shorthands to the commands above. */
1265 /* Add an element to the list of info subcommands. */
1267 struct cmd_list_element *
1268 add_info (const char *name, cmd_simple_func_ftype *fun, const char *doc)
1270 return add_cmd (name, class_info, fun, doc, &infolist);
1273 /* Add an alias to the list of info subcommands. */
1276 add_info_alias (const char *name, cmd_list_element *target, int abbrev_flag)
1278 return add_alias_cmd (name, target, class_run, abbrev_flag, &infolist);
1281 /* Add an element to the list of commands. */
1283 struct cmd_list_element *
1284 add_com (const char *name, enum command_class theclass,
1285 cmd_simple_func_ftype *fun,
1288 return add_cmd (name, theclass, fun, doc, &cmdlist);
1291 /* Add an alias or abbreviation command to the list of commands.
1292 For aliases predefined by GDB (such as bt), THECLASS must be
1293 different of class_alias, as class_alias is used to identify
1294 user defined aliases. */
1297 add_com_alias (const char *name, cmd_list_element *target,
1298 command_class theclass, int abbrev_flag)
1300 return add_alias_cmd (name, target, theclass, abbrev_flag, &cmdlist);
1303 /* Add an element with a suppress notification to the list of commands. */
1305 struct cmd_list_element *
1306 add_com_suppress_notification (const char *name, enum command_class theclass,
1307 cmd_simple_func_ftype *fun, const char *doc,
1308 int *suppress_notification)
1310 return add_cmd_suppress_notification (name, theclass, fun, doc,
1311 &cmdlist, suppress_notification);
1314 /* Print the prefix of C followed by name of C in title style. */
1317 fput_command_name_styled (const cmd_list_element &c, struct ui_file *stream)
1319 std::string prefixname
1320 = c.prefix == nullptr ? "" : c.prefix->prefixname ();
1322 fprintf_styled (stream, title_style.style (), "%s%s",
1323 prefixname.c_str (), c.name);
1326 /* Print the definition of alias C using title style for alias
1327 and aliased command. */
1330 fput_alias_definition_styled (const cmd_list_element &c,
1331 struct ui_file *stream)
1333 gdb_assert (c.is_alias ());
1334 fputs_filtered (" alias ", stream);
1335 fput_command_name_styled (c, stream);
1336 fprintf_filtered (stream, " = ");
1337 fput_command_name_styled (*c.alias_target, stream);
1338 fprintf_filtered (stream, " %s\n", c.default_args.c_str ());
1341 /* Print the definition of the aliases of CMD that have default args. */
1344 fput_aliases_definition_styled (const cmd_list_element &cmd,
1345 struct ui_file *stream)
1347 for (const cmd_list_element &alias : cmd.aliases)
1348 if (!alias.cmd_deprecated && !alias.default_args.empty ())
1349 fput_alias_definition_styled (alias, stream);
1353 /* If C has one or more aliases, style print the name of C and
1354 the name of its aliases, separated by commas.
1355 If ALWAYS_FPUT_C_NAME, print the name of C even if it has no aliases.
1356 If one or more names are printed, POSTFIX is printed after the last name.
1360 fput_command_names_styled (const cmd_list_element &c,
1361 bool always_fput_c_name, const char *postfix,
1362 struct ui_file *stream)
1364 /* First, check if we are going to print something. That is, either if
1365 ALWAYS_FPUT_C_NAME is true or if there exists at least one non-deprecated
1368 auto print_alias = [] (const cmd_list_element &alias)
1370 return !alias.cmd_deprecated;
1373 bool print_something = always_fput_c_name;
1374 if (!print_something)
1375 for (const cmd_list_element &alias : c.aliases)
1377 if (!print_alias (alias))
1380 print_something = true;
1384 if (print_something)
1385 fput_command_name_styled (c, stream);
1387 for (const cmd_list_element &alias : c.aliases)
1389 if (!print_alias (alias))
1392 fputs_filtered (", ", stream);
1394 fput_command_name_styled (alias, stream);
1397 if (print_something)
1398 fputs_filtered (postfix, stream);
1401 /* If VERBOSE, print the full help for command C and highlight the
1402 documentation parts matching HIGHLIGHT,
1403 otherwise print only one-line help for command C. */
1406 print_doc_of_command (const cmd_list_element &c, const char *prefix,
1407 bool verbose, compiled_regex &highlight,
1408 struct ui_file *stream)
1410 /* When printing the full documentation, add a line to separate
1411 this documentation from the previous command help, in the likely
1412 case that apropos finds several commands. */
1414 fputs_filtered ("\n", stream);
1416 fput_command_names_styled (c, true,
1417 verbose ? "" : " -- ", stream);
1420 fputs_filtered ("\n", stream);
1421 fput_aliases_definition_styled (c, stream);
1422 fputs_highlighted (c.doc, highlight, stream);
1423 fputs_filtered ("\n", stream);
1427 print_doc_line (stream, c.doc, false);
1428 fputs_filtered ("\n", stream);
1429 fput_aliases_definition_styled (c, stream);
1433 /* Recursively walk the commandlist structures, and print out the
1434 documentation of commands that match our regex in either their
1435 name, or their documentation.
1436 If VERBOSE, prints the complete documentation and highlight the
1437 documentation parts matching REGEX, otherwise prints only
1441 apropos_cmd (struct ui_file *stream,
1442 struct cmd_list_element *commandlist,
1443 bool verbose, compiled_regex ®ex, const char *prefix)
1445 struct cmd_list_element *c;
1448 /* Walk through the commands. */
1449 for (c=commandlist;c;c=c->next)
1453 /* Command aliases/abbreviations are skipped to ensure we print the
1454 doc of a command only once, when encountering the aliased
1459 returnvalue = -1; /* Needed to avoid double printing. */
1460 if (c->name != NULL)
1462 size_t name_len = strlen (c->name);
1464 /* Try to match against the name. */
1465 returnvalue = regex.search (c->name, name_len, 0, name_len, NULL);
1466 if (returnvalue >= 0)
1467 print_doc_of_command (*c, prefix, verbose, regex, stream);
1469 /* Try to match against the name of the aliases. */
1470 for (const cmd_list_element &alias : c->aliases)
1472 name_len = strlen (alias.name);
1473 returnvalue = regex.search (alias.name, name_len, 0, name_len, NULL);
1474 if (returnvalue >= 0)
1476 print_doc_of_command (*c, prefix, verbose, regex, stream);
1481 if (c->doc != NULL && returnvalue < 0)
1483 size_t doc_len = strlen (c->doc);
1485 /* Try to match against documentation. */
1486 if (regex.search (c->doc, doc_len, 0, doc_len, NULL) >= 0)
1487 print_doc_of_command (*c, prefix, verbose, regex, stream);
1489 /* Check if this command has subcommands. */
1490 if (c->is_prefix ())
1492 /* Recursively call ourselves on the subcommand list,
1493 passing the right prefix in. */
1494 apropos_cmd (stream, *c->subcommands, verbose, regex,
1495 c->prefixname ().c_str ());
1500 /* This command really has to deal with two things:
1501 1) I want documentation on *this string* (usually called by
1502 "help commandname").
1504 2) I want documentation on *this list* (usually called by giving a
1505 command that requires subcommands. Also called by saying just
1508 I am going to split this into two separate commands, help_cmd and
1512 help_cmd (const char *command, struct ui_file *stream)
1514 struct cmd_list_element *c, *alias, *prefix_cmd, *c_cmd;
1518 help_list (cmdlist, "", all_classes, stream);
1522 if (strcmp (command, "all") == 0)
1528 const char *orig_command = command;
1529 c = lookup_cmd (&command, cmdlist, "", NULL, 0, 0);
1534 lookup_cmd_composition (orig_command, &alias, &prefix_cmd, &c_cmd);
1536 /* There are three cases here.
1537 If c->subcommands is nonzero, we have a prefix command.
1538 Print its documentation, then list its subcommands.
1540 If c->func is non NULL, we really have a command. Print its
1541 documentation and return.
1543 If c->func is NULL, we have a class name. Print its
1544 documentation (as if it were a command) and then set class to the
1545 number of this class so that the commands in the class will be
1548 /* If the user asked 'help somecommand' and there is no alias,
1549 the false indicates to not output the (single) command name. */
1550 fput_command_names_styled (*c, false, "\n", stream);
1551 fput_aliases_definition_styled (*c, stream);
1552 fputs_filtered (c->doc, stream);
1553 fputs_filtered ("\n", stream);
1555 if (!c->is_prefix () && !c->is_command_class_help ())
1558 fprintf_filtered (stream, "\n");
1560 /* If this is a prefix command, print it's subcommands. */
1561 if (c->is_prefix ())
1562 help_list (*c->subcommands, c->prefixname ().c_str (),
1563 all_commands, stream);
1565 /* If this is a class name, print all of the commands in the class. */
1566 if (c->is_command_class_help ())
1567 help_list (cmdlist, "", c->theclass, stream);
1569 if (c->hook_pre || c->hook_post)
1570 fprintf_filtered (stream,
1571 "\nThis command has a hook (or hooks) defined:\n");
1574 fprintf_filtered (stream,
1575 "\tThis command is run after : %s (pre hook)\n",
1578 fprintf_filtered (stream,
1579 "\tThis command is run before : %s (post hook)\n",
1580 c->hook_post->name);
1584 * Get a specific kind of help on a command list.
1587 * CMDTYPE is the prefix to use in the title string.
1588 * CLASS is the class with which to list the nodes of this list (see
1589 * documentation for help_cmd_list below), As usual, ALL_COMMANDS for
1590 * everything, ALL_CLASSES for just classes, and non-negative for only things
1591 * in a specific class.
1592 * and STREAM is the output stream on which to print things.
1593 * If you call this routine with a class >= 0, it recurses.
1596 help_list (struct cmd_list_element *list, const char *cmdtype,
1597 enum command_class theclass, struct ui_file *stream)
1600 char *cmdtype1, *cmdtype2;
1602 /* If CMDTYPE is "foo ", CMDTYPE1 gets " foo" and CMDTYPE2 gets "foo sub".
1604 len = strlen (cmdtype);
1605 cmdtype1 = (char *) alloca (len + 1);
1607 cmdtype2 = (char *) alloca (len + 4);
1612 memcpy (cmdtype1 + 1, cmdtype, len - 1);
1614 memcpy (cmdtype2, cmdtype, len - 1);
1615 strcpy (cmdtype2 + len - 1, " sub");
1618 if (theclass == all_classes)
1619 fprintf_filtered (stream, "List of classes of %scommands:\n\n", cmdtype2);
1621 fprintf_filtered (stream, "List of %scommands:\n\n", cmdtype2);
1623 help_cmd_list (list, theclass, theclass >= 0, stream);
1625 if (theclass == all_classes)
1627 fprintf_filtered (stream, "\n\
1628 Type \"help%s\" followed by a class name for a list of commands in ",
1631 fprintf_filtered (stream, "that class.");
1633 fprintf_filtered (stream, "\n\
1634 Type \"help all\" for the list of all commands.");
1637 fprintf_filtered (stream, "\nType \"help%s\" followed by %scommand name ",
1638 cmdtype1, cmdtype2);
1640 fputs_filtered ("for ", stream);
1642 fputs_filtered ("full ", stream);
1644 fputs_filtered ("documentation.\n", stream);
1645 fputs_filtered ("Type \"apropos word\" to search "
1646 "for commands related to \"word\".\n", stream);
1647 fputs_filtered ("Type \"apropos -v word\" for full documentation", stream);
1649 fputs_filtered (" of commands related to \"word\".\n", stream);
1650 fputs_filtered ("Command name abbreviations are allowed if unambiguous.\n",
1655 help_all (struct ui_file *stream)
1657 struct cmd_list_element *c;
1658 int seen_unclassified = 0;
1660 for (c = cmdlist; c; c = c->next)
1664 /* If this is a class name, print all of the commands in the
1667 if (c->is_command_class_help ())
1669 fprintf_filtered (stream, "\nCommand class: %s\n\n", c->name);
1670 help_cmd_list (cmdlist, c->theclass, true, stream);
1674 /* While it's expected that all commands are in some class,
1675 as a safety measure, we'll print commands outside of any
1676 class at the end. */
1678 for (c = cmdlist; c; c = c->next)
1683 if (c->theclass == no_class)
1685 if (!seen_unclassified)
1687 fprintf_filtered (stream, "\nUnclassified commands\n\n");
1688 seen_unclassified = 1;
1690 print_help_for_command (*c, true, stream);
1696 /* See cli-decode.h. */
1699 print_doc_line (struct ui_file *stream, const char *str,
1700 bool for_value_prefix)
1702 static char *line_buffer = 0;
1703 static int line_size;
1709 line_buffer = (char *) xmalloc (line_size);
1712 /* Searches for the first end of line or the end of STR. */
1714 while (*p && *p != '\n')
1716 if (p - str > line_size - 1)
1718 line_size = p - str + 1;
1719 xfree (line_buffer);
1720 line_buffer = (char *) xmalloc (line_size);
1722 strncpy (line_buffer, str, p - str);
1723 if (for_value_prefix)
1725 if (islower (line_buffer[0]))
1726 line_buffer[0] = toupper (line_buffer[0]);
1727 gdb_assert (p > str);
1728 if (line_buffer[p - str - 1] == '.')
1729 line_buffer[p - str - 1] = '\0';
1731 line_buffer[p - str] = '\0';
1734 line_buffer[p - str] = '\0';
1735 fputs_filtered (line_buffer, stream);
1738 /* Print one-line help for command C.
1739 If RECURSE is non-zero, also print one-line descriptions
1740 of all prefixed subcommands. */
1742 print_help_for_command (const cmd_list_element &c,
1743 bool recurse, struct ui_file *stream)
1745 fput_command_names_styled (c, true, " -- ", stream);
1746 print_doc_line (stream, c.doc, false);
1747 fputs_filtered ("\n", stream);
1748 if (!c.default_args.empty ())
1749 fput_alias_definition_styled (c, stream);
1750 fput_aliases_definition_styled (c, stream);
1754 && c.abbrev_flag == 0)
1755 /* Subcommands of a prefix command typically have 'all_commands'
1756 as class. If we pass CLASS to recursive invocation,
1757 most often we won't see anything. */
1758 help_cmd_list (*c.subcommands, all_commands, true, stream);
1762 * Implement a help command on command list LIST.
1763 * RECURSE should be non-zero if this should be done recursively on
1764 * all sublists of LIST.
1765 * STREAM is the stream upon which the output should be written.
1766 * THECLASS should be:
1767 * A non-negative class number to list only commands in that
1768 * ALL_COMMANDS to list all commands in list.
1769 * ALL_CLASSES to list all classes in list.
1771 * Note that aliases are only shown when THECLASS is class_alias.
1772 * In the other cases, the aliases will be shown together with their
1775 * Note that RECURSE will be active on *all* sublists, not just the
1776 * ones selected by the criteria above (ie. the selection mechanism
1777 * is at the low level, not the high-level).
1781 help_cmd_list (struct cmd_list_element *list, enum command_class theclass,
1782 bool recurse, struct ui_file *stream)
1784 struct cmd_list_element *c;
1786 for (c = list; c; c = c->next)
1788 if (c->abbrev_flag == 1 || c->cmd_deprecated)
1790 /* Do not show abbreviations or deprecated commands. */
1794 if (c->is_alias () && theclass != class_alias)
1796 /* Do not show an alias, unless specifically showing the
1797 list of aliases: for all other classes, an alias is
1798 shown (if needed) together with its aliased command. */
1802 if (theclass == all_commands
1803 || (theclass == all_classes && c->is_command_class_help ())
1804 || (theclass == c->theclass && !c->is_command_class_help ()))
1807 - showing all commands
1808 - showing all classes and C is a help class
1809 - showing commands of THECLASS and C is not the help class */
1811 /* If we show the class_alias and C is an alias, do not recurse,
1812 as this would show the (possibly very long) not very useful
1813 list of sub-commands of the aliased command. */
1814 print_help_for_command
1816 recurse && (theclass != class_alias || !c->is_alias ()),
1822 && (theclass == class_user || theclass == class_alias)
1825 /* User-defined commands or aliases may be subcommands. */
1826 help_cmd_list (*c->subcommands, theclass, recurse, stream);
1830 /* Do not show C or recurse on C, e.g. because C does not belong to
1831 THECLASS or because C is a help class. */
1836 /* Search the input clist for 'command'. Return the command if
1837 found (or NULL if not), and return the number of commands
1840 static struct cmd_list_element *
1841 find_cmd (const char *command, int len, struct cmd_list_element *clist,
1842 int ignore_help_classes, int *nfound)
1844 struct cmd_list_element *found, *c;
1848 for (c = clist; c; c = c->next)
1849 if (!strncmp (command, c->name, len)
1850 && (!ignore_help_classes || !c->is_command_class_help ()))
1854 if (c->name[len] == '\0')
1863 /* Return the length of command name in TEXT. */
1866 find_command_name_length (const char *text)
1868 const char *p = text;
1870 /* Treating underscores as part of command words is important
1871 so that "set args_foo()" doesn't get interpreted as
1872 "set args _foo()". */
1873 /* Some characters are only used for TUI specific commands.
1874 However, they are always allowed for the sake of consistency.
1876 Note that this is larger than the character set allowed when
1877 creating user-defined commands. */
1879 /* Recognize the single character commands so that, e.g., "!ls"
1880 works as expected. */
1881 if (*p == '!' || *p == '|')
1884 while (valid_cmd_char_p (*p)
1885 /* Characters used by TUI specific commands. */
1886 || *p == '+' || *p == '<' || *p == '>' || *p == '$')
1892 /* See command.h. */
1895 valid_cmd_char_p (int c)
1897 /* Alas "42" is a legitimate user-defined command.
1898 In the interests of not breaking anything we preserve that. */
1900 return isalnum (c) || c == '-' || c == '_' || c == '.';
1903 /* See command.h. */
1906 valid_user_defined_cmd_name_p (const char *name)
1913 for (p = name; *p != '\0'; ++p)
1915 if (valid_cmd_char_p (*p))
1924 /* See command.h. */
1926 struct cmd_list_element *
1927 lookup_cmd_1 (const char **text, struct cmd_list_element *clist,
1928 struct cmd_list_element **result_list, std::string *default_args,
1929 int ignore_help_classes, bool lookup_for_completion_p)
1933 struct cmd_list_element *found, *c;
1934 bool found_alias = false;
1935 const char *line = *text;
1937 while (**text == ' ' || **text == '\t')
1940 /* Identify the name of the command. */
1941 len = find_command_name_length (*text);
1943 /* If nothing but whitespace, return 0. */
1947 /* *text and p now bracket the first command word to lookup (and
1948 it's length is len). We copy this into a local temporary. */
1951 command = (char *) alloca (len + 1);
1952 memcpy (command, *text, len);
1953 command[len] = '\0';
1958 found = find_cmd (command, len, clist, ignore_help_classes, &nfound);
1960 /* If nothing matches, we have a simple failure. */
1966 if (result_list != nullptr)
1967 /* Will be modified in calling routine
1968 if we know what the prefix command is. */
1970 if (default_args != nullptr)
1971 *default_args = std::string ();
1972 return CMD_LIST_AMBIGUOUS; /* Ambiguous. */
1975 /* We've matched something on this list. Move text pointer forward. */
1979 if (found->is_alias ())
1981 /* We drop the alias (abbreviation) in favor of the command it
1982 is pointing to. If the alias is deprecated, though, we need to
1983 warn the user about it before we drop it. Note that while we
1984 are warning about the alias, we may also warn about the command
1985 itself and we will adjust the appropriate DEPRECATED_WARN_USER
1988 if (found->deprecated_warn_user && !lookup_for_completion_p)
1989 deprecated_cmd_warning (line, clist);
1992 /* Return the default_args of the alias, not the default_args
1993 of the command it is pointing to. */
1994 if (default_args != nullptr)
1995 *default_args = found->default_args;
1996 found = found->alias_target;
1999 /* If we found a prefix command, keep looking. */
2001 if (found->is_prefix ())
2003 c = lookup_cmd_1 (text, *found->subcommands, result_list, default_args,
2004 ignore_help_classes, lookup_for_completion_p);
2007 /* Didn't find anything; this is as far as we got. */
2008 if (result_list != nullptr)
2009 *result_list = clist;
2010 if (!found_alias && default_args != nullptr)
2011 *default_args = found->default_args;
2014 else if (c == CMD_LIST_AMBIGUOUS)
2016 /* We've gotten this far properly, but the next step is
2017 ambiguous. We need to set the result list to the best
2018 we've found (if an inferior hasn't already set it). */
2019 if (result_list != nullptr)
2021 /* This used to say *result_list = *found->subcommands.
2022 If that was correct, need to modify the documentation
2023 at the top of this function to clarify what is
2024 supposed to be going on. */
2025 *result_list = found;
2026 /* For ambiguous commands, do not return any default_args args. */
2027 if (default_args != nullptr)
2028 *default_args = std::string ();
2039 if (result_list != nullptr)
2040 *result_list = clist;
2041 if (!found_alias && default_args != nullptr)
2042 *default_args = found->default_args;
2047 /* All this hair to move the space to the front of cmdtype */
2050 undef_cmd_error (const char *cmdtype, const char *q)
2052 error (_("Undefined %scommand: \"%s\". Try \"help%s%.*s\"."),
2055 *cmdtype ? " " : "",
2056 (int) strlen (cmdtype) - 1,
2060 /* Look up the contents of *LINE as a command in the command list LIST.
2061 LIST is a chain of struct cmd_list_element's.
2062 If it is found, return the struct cmd_list_element for that command,
2063 update *LINE to point after the command name, at the first argument
2064 and update *DEFAULT_ARGS (if DEFAULT_ARGS is not null) to the default
2065 args to prepend to the user provided args when running the command.
2066 Note that if the found cmd_list_element is found via an alias,
2067 the default args of the alias are returned.
2069 If not found, call error if ALLOW_UNKNOWN is zero
2070 otherwise (or if error returns) return zero.
2071 Call error if specified command is ambiguous,
2072 unless ALLOW_UNKNOWN is negative.
2073 CMDTYPE precedes the word "command" in the error message.
2075 If IGNORE_HELP_CLASSES is nonzero, ignore any command list
2076 elements which are actually help classes rather than commands (i.e.
2077 the function field of the struct cmd_list_element is 0). */
2079 struct cmd_list_element *
2080 lookup_cmd (const char **line, struct cmd_list_element *list,
2081 const char *cmdtype,
2082 std::string *default_args,
2083 int allow_unknown, int ignore_help_classes)
2085 struct cmd_list_element *last_list = 0;
2086 struct cmd_list_element *c;
2088 /* Note: Do not remove trailing whitespace here because this
2089 would be wrong for complete_command. Jim Kingdon */
2092 error (_("Lack of needed %scommand"), cmdtype);
2094 c = lookup_cmd_1 (line, list, &last_list, default_args, ignore_help_classes);
2101 int len = find_command_name_length (*line);
2103 q = (char *) alloca (len + 1);
2104 strncpy (q, *line, len);
2106 undef_cmd_error (cmdtype, q);
2111 else if (c == CMD_LIST_AMBIGUOUS)
2113 /* Ambigous. Local values should be off subcommands or called
2115 int local_allow_unknown = (last_list ? last_list->allow_unknown :
2117 std::string local_cmdtype
2118 = last_list ? last_list->prefixname () : cmdtype;
2119 struct cmd_list_element *local_list =
2120 (last_list ? *(last_list->subcommands) : list);
2122 if (local_allow_unknown < 0)
2125 return last_list; /* Found something. */
2127 return 0; /* Found nothing. */
2131 /* Report as error. */
2136 ((*line)[amb_len] && (*line)[amb_len] != ' '
2137 && (*line)[amb_len] != '\t');
2142 for (c = local_list; c; c = c->next)
2143 if (!strncmp (*line, c->name, amb_len))
2145 if (strlen (ambbuf) + strlen (c->name) + 6
2146 < (int) sizeof ambbuf)
2148 if (strlen (ambbuf))
2149 strcat (ambbuf, ", ");
2150 strcat (ambbuf, c->name);
2154 strcat (ambbuf, "..");
2158 error (_("Ambiguous %scommand \"%s\": %s."),
2159 local_cmdtype.c_str (), *line, ambbuf);
2164 if (c->type == set_cmd && **line != '\0' && !isspace (**line))
2165 error (_("Argument must be preceded by space."));
2167 /* We've got something. It may still not be what the caller
2168 wants (if this command *needs* a subcommand). */
2169 while (**line == ' ' || **line == '\t')
2172 if (c->is_prefix () && **line && !c->allow_unknown)
2173 undef_cmd_error (c->prefixname ().c_str (), *line);
2175 /* Seems to be what he wants. Return it. */
2181 /* See command.h. */
2183 struct cmd_list_element *
2184 lookup_cmd_exact (const char *name,
2185 struct cmd_list_element *list,
2186 bool ignore_help_classes)
2188 const char *tem = name;
2189 struct cmd_list_element *cmd = lookup_cmd (&tem, list, "", NULL, -1,
2190 ignore_help_classes);
2191 if (cmd != nullptr && strcmp (name, cmd->name) != 0)
2196 /* We are here presumably because an alias or command in TEXT is
2197 deprecated and a warning message should be generated. This
2198 function decodes TEXT and potentially generates a warning message
2201 Example for 'set endian big' which has a fictitious alias 'seb'.
2203 If alias wasn't used in TEXT, and the command is deprecated:
2204 "warning: 'set endian big' is deprecated."
2206 If alias was used, and only the alias is deprecated:
2207 "warning: 'seb' an alias for the command 'set endian big' is deprecated."
2209 If alias was used and command is deprecated (regardless of whether
2210 the alias itself is deprecated:
2212 "warning: 'set endian big' (seb) is deprecated."
2214 After the message has been sent, clear the appropriate flags in the
2215 command and/or the alias so the user is no longer bothered.
2219 deprecated_cmd_warning (const char *text, struct cmd_list_element *list)
2221 struct cmd_list_element *alias = nullptr;
2222 struct cmd_list_element *cmd = nullptr;
2224 /* Return if text doesn't evaluate to a command. We place this lookup
2225 within its own scope so that the PREFIX_CMD local is not visible
2226 later in this function. The value returned in PREFIX_CMD is based on
2227 the prefix found in TEXT, and is our case this prefix can be missing
2228 in some situations (when LIST is not the global CMDLIST).
2230 It is better for our purposes to use the prefix commands directly from
2231 the ALIAS and CMD results. */
2233 struct cmd_list_element *prefix_cmd = nullptr;
2234 if (!lookup_cmd_composition_1 (text, &alias, &prefix_cmd, &cmd, list))
2238 /* Return if nothing is deprecated. */
2239 if (!((alias != nullptr ? alias->deprecated_warn_user : 0)
2240 || cmd->deprecated_warn_user))
2243 /* Join command prefix (if any) and the command name. */
2244 std::string tmp_cmd_str;
2245 if (cmd->prefix != nullptr)
2246 tmp_cmd_str += cmd->prefix->prefixname ();
2247 tmp_cmd_str += std::string (cmd->name);
2249 /* Display the appropriate first line, this warns that the thing the user
2250 entered is deprecated. */
2251 if (alias != nullptr)
2253 /* Join the alias prefix (if any) and the alias name. */
2254 std::string tmp_alias_str;
2255 if (alias->prefix != nullptr)
2256 tmp_alias_str += alias->prefix->prefixname ();
2257 tmp_alias_str += std::string (alias->name);
2259 if (cmd->cmd_deprecated)
2260 printf_filtered (_("Warning: command '%ps' (%ps) is deprecated.\n"),
2261 styled_string (title_style.style (),
2262 tmp_cmd_str.c_str ()),
2263 styled_string (title_style.style (),
2264 tmp_alias_str.c_str ()));
2266 printf_filtered (_("Warning: '%ps', an alias for the command '%ps', "
2267 "is deprecated.\n"),
2268 styled_string (title_style.style (),
2269 tmp_alias_str.c_str ()),
2270 styled_string (title_style.style (),
2271 tmp_cmd_str.c_str ()));
2274 printf_filtered (_("Warning: command '%ps' is deprecated.\n"),
2275 styled_string (title_style.style (),
2276 tmp_cmd_str.c_str ()));
2278 /* Now display a second line indicating what the user should use instead.
2279 If it is only the alias that is deprecated, we want to indicate the
2280 new alias, otherwise we'll indicate the new command. */
2281 const char *replacement;
2282 if (alias != nullptr && !cmd->cmd_deprecated)
2283 replacement = alias->replacement;
2285 replacement = cmd->replacement;
2286 if (replacement != nullptr)
2287 printf_filtered (_("Use '%ps'.\n\n"),
2288 styled_string (title_style.style (),
2291 printf_filtered (_("No alternative known.\n\n"));
2293 /* We've warned you, now we'll keep quiet. */
2294 if (alias != nullptr)
2295 alias->deprecated_warn_user = 0;
2296 cmd->deprecated_warn_user = 0;
2299 /* Look up the contents of TEXT as a command in the command list CUR_LIST.
2300 Return 1 on success, 0 on failure.
2302 If TEXT refers to an alias, *ALIAS will point to that alias.
2304 If TEXT is a subcommand (i.e. one that is preceded by a prefix
2305 command) set *PREFIX_CMD.
2307 Set *CMD to point to the command TEXT indicates.
2309 If any of *ALIAS, *PREFIX_CMD, or *CMD cannot be determined or do not
2310 exist, they are NULL when we return.
2315 lookup_cmd_composition_1 (const char *text,
2316 struct cmd_list_element **alias,
2317 struct cmd_list_element **prefix_cmd,
2318 struct cmd_list_element **cmd,
2319 struct cmd_list_element *cur_list)
2322 *prefix_cmd = cur_list->prefix;
2325 text = skip_spaces (text);
2327 /* Go through as many command lists as we need to, to find the command
2331 /* Identify the name of the command. */
2332 int len = find_command_name_length (text);
2334 /* If nothing but whitespace, return. */
2338 /* TEXT is the start of the first command word to lookup (and
2339 it's length is LEN). We copy this into a local temporary. */
2340 std::string command (text, len);
2344 *cmd = find_cmd (command.c_str (), len, cur_list, 1, &nfound);
2346 /* We only handle the case where a single command was found. */
2347 if (*cmd == CMD_LIST_AMBIGUOUS || *cmd == nullptr)
2351 if ((*cmd)->is_alias ())
2353 /* If the command was actually an alias, we note that an
2354 alias was used (by assigning *ALIAS) and we set *CMD. */
2356 *cmd = (*cmd)->alias_target;
2361 text = skip_spaces (text);
2363 if ((*cmd)->is_prefix () && *text != '\0')
2365 cur_list = *(*cmd)->subcommands;
2373 /* Look up the contents of TEXT as a command in the command list 'cmdlist'.
2374 Return 1 on success, 0 on failure.
2376 If TEXT refers to an alias, *ALIAS will point to that alias.
2378 If TEXT is a subcommand (i.e. one that is preceded by a prefix
2379 command) set *PREFIX_CMD.
2381 Set *CMD to point to the command TEXT indicates.
2383 If any of *ALIAS, *PREFIX_CMD, or *CMD cannot be determined or do not
2384 exist, they are NULL when we return.
2389 lookup_cmd_composition (const char *text,
2390 struct cmd_list_element **alias,
2391 struct cmd_list_element **prefix_cmd,
2392 struct cmd_list_element **cmd)
2394 return lookup_cmd_composition_1 (text, alias, prefix_cmd, cmd, cmdlist);
2397 /* Helper function for SYMBOL_COMPLETION_FUNCTION. */
2399 /* Return a vector of char pointers which point to the different
2400 possible completions in LIST of TEXT.
2402 WORD points in the same buffer as TEXT, and completions should be
2403 returned relative to this position. For example, suppose TEXT is
2404 "foo" and we want to complete to "foobar". If WORD is "oo", return
2405 "oobar"; if WORD is "baz/foo", return "baz/foobar". */
2408 complete_on_cmdlist (struct cmd_list_element *list,
2409 completion_tracker &tracker,
2410 const char *text, const char *word,
2411 int ignore_help_classes)
2413 struct cmd_list_element *ptr;
2414 int textlen = strlen (text);
2416 int saw_deprecated_match = 0;
2418 /* We do one or two passes. In the first pass, we skip deprecated
2419 commands. If we see no matching commands in the first pass, and
2420 if we did happen to see a matching deprecated command, we do
2421 another loop to collect those. */
2422 for (pass = 0; pass < 2; ++pass)
2424 bool got_matches = false;
2426 for (ptr = list; ptr; ptr = ptr->next)
2427 if (!strncmp (ptr->name, text, textlen)
2428 && !ptr->abbrev_flag
2429 && (!ignore_help_classes || !ptr->is_command_class_help ()
2430 || ptr->is_prefix ()))
2434 if (ptr->cmd_deprecated)
2436 saw_deprecated_match = 1;
2441 tracker.add_completion
2442 (make_completion_match_str (ptr->name, text, word));
2449 /* If we saw no matching deprecated commands in the first pass,
2451 if (!saw_deprecated_match)
2456 /* Helper function for SYMBOL_COMPLETION_FUNCTION. */
2458 /* Add the different possible completions in ENUMLIST of TEXT.
2460 WORD points in the same buffer as TEXT, and completions should be
2461 returned relative to this position. For example, suppose TEXT is "foo"
2462 and we want to complete to "foobar". If WORD is "oo", return
2463 "oobar"; if WORD is "baz/foo", return "baz/foobar". */
2466 complete_on_enum (completion_tracker &tracker,
2467 const char *const *enumlist,
2468 const char *text, const char *word)
2470 int textlen = strlen (text);
2474 for (i = 0; (name = enumlist[i]) != NULL; i++)
2475 if (strncmp (name, text, textlen) == 0)
2476 tracker.add_completion (make_completion_match_str (name, text, word));
2479 /* Call the command function. */
2481 cmd_func (struct cmd_list_element *cmd, const char *args, int from_tty)
2483 if (!cmd->is_command_class_help ())
2485 gdb::optional<scoped_restore_tmpl<int>> restore_suppress;
2487 if (cmd->suppress_notification != NULL)
2488 restore_suppress.emplace (cmd->suppress_notification, 1);
2490 cmd->func (args, from_tty, cmd);
2493 error (_("Invalid command"));
2497 cli_user_command_p (struct cmd_list_element *cmd)
2499 return cmd->theclass == class_user && cmd->func == do_simple_func;