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 "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 struct cmd_list_element
*delete_cmd (const char *name
,
34 struct cmd_list_element
**list
,
35 struct cmd_list_element
**prehook
,
36 struct cmd_list_element
**prehookee
,
37 struct cmd_list_element
**posthook
,
38 struct cmd_list_element
**posthookee
);
40 static struct cmd_list_element
*find_cmd (const char *command
,
42 struct cmd_list_element
*clist
,
43 int ignore_help_classes
,
46 static void help_cmd_list (struct cmd_list_element
*list
,
47 enum command_class theclass
,
49 struct ui_file
*stream
);
51 static void help_all (struct ui_file
*stream
);
53 static int lookup_cmd_composition_1 (const char *text
,
54 struct cmd_list_element
**alias
,
55 struct cmd_list_element
**prefix_cmd
,
56 struct cmd_list_element
**cmd
,
57 struct cmd_list_element
*cur_list
);
59 /* Look up a command whose 'subcommands' field is SUBCOMMANDS. Return the
60 command if found, otherwise return NULL. */
62 static struct cmd_list_element
*
63 lookup_cmd_with_subcommands (cmd_list_element
**subcommands
,
64 cmd_list_element
*list
)
66 struct cmd_list_element
*p
= NULL
;
68 for (p
= list
; p
!= NULL
; p
= p
->next
)
70 struct cmd_list_element
*q
;
75 else if (p
->subcommands
== subcommands
)
77 /* If we found an alias, we must return the aliased
79 return p
->is_alias () ? p
->alias_target
: p
;
82 q
= lookup_cmd_with_subcommands (subcommands
, *(p
->subcommands
));
91 print_help_for_command (struct cmd_list_element
*c
,
92 bool recurse
, struct ui_file
*stream
);
95 /* Set the callback function for the specified command. For each both
96 the commands callback and func() are set. The latter set to a
97 bounce function (unless cfunc / sfunc is NULL that is). */
100 do_const_cfunc (struct cmd_list_element
*c
, const char *args
, int from_tty
)
102 c
->function
.const_cfunc (args
, from_tty
);
106 set_cmd_cfunc (struct cmd_list_element
*cmd
, cmd_const_cfunc_ftype
*cfunc
)
111 cmd
->func
= do_const_cfunc
;
112 cmd
->function
.const_cfunc
= cfunc
;
116 do_sfunc (struct cmd_list_element
*c
, const char *args
, int from_tty
)
118 c
->function
.sfunc (args
, from_tty
, c
);
122 set_cmd_sfunc (struct cmd_list_element
*cmd
, cmd_const_sfunc_ftype
*sfunc
)
127 cmd
->func
= do_sfunc
;
128 cmd
->function
.sfunc
= sfunc
;
132 cmd_cfunc_eq (struct cmd_list_element
*cmd
, cmd_const_cfunc_ftype
*cfunc
)
134 return cmd
->func
== do_const_cfunc
&& cmd
->function
.const_cfunc
== cfunc
;
138 set_cmd_completer (struct cmd_list_element
*cmd
, completer_ftype
*completer
)
140 cmd
->completer
= completer
; /* Ok. */
143 /* See definition in commands.h. */
146 set_cmd_completer_handle_brkchars (struct cmd_list_element
*cmd
,
147 completer_handle_brkchars_ftype
*func
)
149 cmd
->completer_handle_brkchars
= func
;
153 cmd_list_element::prefixname () const
155 if (!this->is_prefix ())
156 /* Not a prefix command. */
159 std::string prefixname
;
160 if (this->prefix
!= nullptr)
161 prefixname
= this->prefix
->prefixname ();
163 prefixname
+= this->name
;
169 /* Add element named NAME.
170 Space for NAME and DOC must be allocated by the caller.
171 CLASS is the top level category into which commands are broken down
173 FUN should be the function to execute the command;
174 it will get a character string as argument, with leading
175 and trailing blanks already eliminated.
177 DOC is a documentation string for the command.
178 Its first line should be a complete sentence.
179 It should start with ? for a command that is an abbreviation
180 or with * for a command that most users don't need to know about.
182 Add this command to command list *LIST.
184 Returns a pointer to the added command (not necessarily the head
187 static struct cmd_list_element
*
188 do_add_cmd (const char *name
, enum command_class theclass
,
189 const char *doc
, struct cmd_list_element
**list
)
191 struct cmd_list_element
*c
= new struct cmd_list_element (name
, theclass
,
193 struct cmd_list_element
*p
, *iter
;
195 /* Turn each alias of the old command into an alias of the new
197 c
->aliases
= delete_cmd (name
, list
, &c
->hook_pre
, &c
->hookee_pre
,
198 &c
->hook_post
, &c
->hookee_post
);
199 for (iter
= c
->aliases
; iter
; iter
= iter
->alias_chain
)
200 iter
->alias_target
= c
;
202 c
->hook_pre
->hookee_pre
= c
;
204 c
->hookee_pre
->hook_pre
= c
;
206 c
->hook_post
->hookee_post
= c
;
208 c
->hookee_post
->hook_post
= c
;
210 if (*list
== NULL
|| strcmp ((*list
)->name
, name
) >= 0)
218 while (p
->next
&& strcmp (p
->next
->name
, name
) <= 0)
226 /* Search the prefix cmd of C, and assigns it to C->prefix.
227 See also add_prefix_cmd and update_prefix_field_of_prefixed_commands. */
228 cmd_list_element
*prefixcmd
= lookup_cmd_with_subcommands (list
, cmdlist
);
229 c
->prefix
= prefixcmd
;
235 struct cmd_list_element
*
236 add_cmd (const char *name
, enum command_class theclass
,
237 const char *doc
, struct cmd_list_element
**list
)
239 cmd_list_element
*result
= do_add_cmd (name
, theclass
, doc
, list
);
241 result
->function
.const_cfunc
= NULL
;
245 struct cmd_list_element
*
246 add_cmd (const char *name
, enum command_class theclass
,
247 cmd_const_cfunc_ftype
*fun
,
248 const char *doc
, struct cmd_list_element
**list
)
250 cmd_list_element
*result
= do_add_cmd (name
, theclass
, doc
, list
);
251 set_cmd_cfunc (result
, fun
);
255 /* Add an element with a suppress notification to the LIST of commands. */
257 struct cmd_list_element
*
258 add_cmd_suppress_notification (const char *name
, enum command_class theclass
,
259 cmd_const_cfunc_ftype
*fun
, const char *doc
,
260 struct cmd_list_element
**list
,
261 int *suppress_notification
)
263 struct cmd_list_element
*element
;
265 element
= add_cmd (name
, theclass
, fun
, doc
, list
);
266 element
->suppress_notification
= suppress_notification
;
272 /* Deprecates a command CMD.
273 REPLACEMENT is the name of the command which should be used in
274 place of this command, or NULL if no such command exists.
276 This function does not check to see if command REPLACEMENT exists
277 since gdb may not have gotten around to adding REPLACEMENT when
278 this function is called.
280 Returns a pointer to the deprecated command. */
282 struct cmd_list_element
*
283 deprecate_cmd (struct cmd_list_element
*cmd
, const char *replacement
)
285 cmd
->cmd_deprecated
= 1;
286 cmd
->deprecated_warn_user
= 1;
288 if (replacement
!= NULL
)
289 cmd
->replacement
= replacement
;
291 cmd
->replacement
= NULL
;
296 struct cmd_list_element
*
297 add_alias_cmd (const char *name
, cmd_list_element
*target
,
298 enum command_class theclass
, int abbrev_flag
,
299 struct cmd_list_element
**list
)
301 gdb_assert (target
!= nullptr);
303 struct cmd_list_element
*c
= add_cmd (name
, theclass
, target
->doc
, list
);
305 /* If TARGET->DOC can be freed, we should make another copy. */
306 if (target
->doc_allocated
)
308 c
->doc
= xstrdup (target
->doc
);
309 c
->doc_allocated
= 1;
311 /* NOTE: Both FUNC and all the FUNCTIONs need to be copied. */
312 c
->func
= target
->func
;
313 c
->function
= target
->function
;
314 c
->subcommands
= target
->subcommands
;
315 c
->allow_unknown
= target
->allow_unknown
;
316 c
->abbrev_flag
= abbrev_flag
;
317 c
->alias_target
= target
;
318 c
->alias_chain
= target
->aliases
;
324 /* Update the prefix field of all sub-commands of the prefix command C.
325 We must do this when a prefix command is defined as the GDB init sequence
326 does not guarantee that a prefix command is created before its sub-commands.
327 For example, break-catch-sig.c initialization runs before breakpoint.c
328 initialization, but it is breakpoint.c that creates the "catch" command used
329 by the "catch signal" command created by break-catch-sig.c. */
332 update_prefix_field_of_prefixed_commands (struct cmd_list_element
*c
)
334 for (cmd_list_element
*p
= *c
->subcommands
; p
!= NULL
; p
= p
->next
)
338 /* We must recursively update the prefix field to cover
339 e.g. 'info auto-load libthread-db' where the creation
344 In such a case, when 'auto-load' was created by do_add_cmd,
345 the 'libthread-db' prefix field could not be updated, as the
346 'auto-load' command was not yet reachable by
347 lookup_cmd_for_subcommands (list, cmdlist)
348 that searches from the top level 'cmdlist'. */
350 update_prefix_field_of_prefixed_commands (p
);
355 /* Like add_cmd but adds an element for a command prefix: a name that
356 should be followed by a subcommand to be looked up in another
357 command list. SUBCOMMANDS should be the address of the variable
358 containing that list. */
360 struct cmd_list_element
*
361 add_prefix_cmd (const char *name
, enum command_class theclass
,
362 cmd_const_cfunc_ftype
*fun
,
363 const char *doc
, struct cmd_list_element
**subcommands
,
364 int allow_unknown
, struct cmd_list_element
**list
)
366 struct cmd_list_element
*c
= add_cmd (name
, theclass
, fun
, doc
, list
);
368 c
->subcommands
= subcommands
;
369 c
->allow_unknown
= allow_unknown
;
371 /* Now that prefix command C is defined, we need to set the prefix field
372 of all prefixed commands that were defined before C itself was defined. */
373 update_prefix_field_of_prefixed_commands (c
);
378 /* A helper function for add_basic_prefix_cmd. This is a command
379 function that just forwards to help_list. */
382 do_prefix_cmd (const char *args
, int from_tty
, struct cmd_list_element
*c
)
384 /* Look past all aliases. */
385 while (c
->is_alias ())
388 help_list (*c
->subcommands
, c
->prefixname ().c_str (),
389 all_commands
, gdb_stdout
);
394 struct cmd_list_element
*
395 add_basic_prefix_cmd (const char *name
, enum command_class theclass
,
396 const char *doc
, struct cmd_list_element
**subcommands
,
397 int allow_unknown
, struct cmd_list_element
**list
)
399 struct cmd_list_element
*cmd
= add_prefix_cmd (name
, theclass
, nullptr,
401 allow_unknown
, list
);
402 set_cmd_sfunc (cmd
, do_prefix_cmd
);
406 /* A helper function for add_show_prefix_cmd. This is a command
407 function that just forwards to cmd_show_list. */
410 do_show_prefix_cmd (const char *args
, int from_tty
, struct cmd_list_element
*c
)
412 cmd_show_list (*c
->subcommands
, from_tty
);
417 struct cmd_list_element
*
418 add_show_prefix_cmd (const char *name
, enum command_class theclass
,
419 const char *doc
, struct cmd_list_element
**subcommands
,
420 int allow_unknown
, struct cmd_list_element
**list
)
422 struct cmd_list_element
*cmd
= add_prefix_cmd (name
, theclass
, nullptr,
424 allow_unknown
, list
);
425 set_cmd_sfunc (cmd
, do_show_prefix_cmd
);
429 /* Like ADD_PREFIX_CMD but sets the suppress_notification pointer on the
430 new command list element. */
432 struct cmd_list_element
*
433 add_prefix_cmd_suppress_notification
434 (const char *name
, enum command_class theclass
,
435 cmd_const_cfunc_ftype
*fun
,
436 const char *doc
, struct cmd_list_element
**subcommands
,
437 int allow_unknown
, struct cmd_list_element
**list
,
438 int *suppress_notification
)
440 struct cmd_list_element
*element
441 = add_prefix_cmd (name
, theclass
, fun
, doc
, subcommands
,
442 allow_unknown
, list
);
443 element
->suppress_notification
= suppress_notification
;
447 /* Like add_prefix_cmd but sets the abbrev_flag on the new command. */
449 struct cmd_list_element
*
450 add_abbrev_prefix_cmd (const char *name
, enum command_class theclass
,
451 cmd_const_cfunc_ftype
*fun
, const char *doc
,
452 struct cmd_list_element
**subcommands
,
453 int allow_unknown
, struct cmd_list_element
**list
)
455 struct cmd_list_element
*c
= add_cmd (name
, theclass
, fun
, doc
, list
);
457 c
->subcommands
= subcommands
;
458 c
->allow_unknown
= allow_unknown
;
463 /* This is an empty "cfunc". */
465 not_just_help_class_command (const char *args
, int from_tty
)
469 /* This is an empty "sfunc". */
472 empty_sfunc (const char *args
, int from_tty
, struct cmd_list_element
*c
)
476 /* Add element named NAME to command list LIST (the list for set/show
477 or some sublist thereof).
478 TYPE is set_cmd or show_cmd.
479 CLASS is as in add_cmd.
480 VAR_TYPE is the kind of thing we are setting.
481 VAR is address of the variable being controlled by this command.
482 DOC is the documentation string. */
484 static struct cmd_list_element
*
485 add_set_or_show_cmd (const char *name
,
487 enum command_class theclass
,
491 struct cmd_list_element
**list
)
493 struct cmd_list_element
*c
= add_cmd (name
, theclass
, doc
, list
);
495 gdb_assert (type
== set_cmd
|| type
== show_cmd
);
497 c
->var_type
= var_type
;
499 /* This needs to be something besides NULL so that this isn't
500 treated as a help class. */
501 set_cmd_sfunc (c
, empty_sfunc
);
505 /* Add element named NAME to both the command SET_LIST and SHOW_LIST.
506 CLASS is as in add_cmd. VAR_TYPE is the kind of thing we are
507 setting. VAR is address of the variable being controlled by this
508 command. SET_FUNC and SHOW_FUNC are the callback functions (if
509 non-NULL). SET_DOC, SHOW_DOC and HELP_DOC are the documentation
512 Return the newly created set and show commands. */
514 static set_show_commands
515 add_setshow_cmd_full (const char *name
,
516 enum command_class theclass
,
517 var_types var_type
, void *var
,
518 const char *set_doc
, const char *show_doc
,
519 const char *help_doc
,
520 cmd_const_sfunc_ftype
*set_func
,
521 show_value_ftype
*show_func
,
522 struct cmd_list_element
**set_list
,
523 struct cmd_list_element
**show_list
)
525 struct cmd_list_element
*set
;
526 struct cmd_list_element
*show
;
530 if (help_doc
!= NULL
)
532 full_set_doc
= xstrprintf ("%s\n%s", set_doc
, help_doc
);
533 full_show_doc
= xstrprintf ("%s\n%s", show_doc
, help_doc
);
537 full_set_doc
= xstrdup (set_doc
);
538 full_show_doc
= xstrdup (show_doc
);
540 set
= add_set_or_show_cmd (name
, set_cmd
, theclass
, var_type
, var
,
541 full_set_doc
, set_list
);
542 set
->doc_allocated
= 1;
544 if (set_func
!= NULL
)
545 set_cmd_sfunc (set
, set_func
);
547 show
= add_set_or_show_cmd (name
, show_cmd
, theclass
, var_type
, var
,
548 full_show_doc
, show_list
);
549 show
->doc_allocated
= 1;
550 show
->show_value_func
= show_func
;
551 /* Disable the default symbol completer. Doesn't make much sense
552 for the "show" command to complete on anything. */
553 set_cmd_completer (show
, nullptr);
558 /* Add element named NAME to command list LIST (the list for set or
559 some sublist thereof). CLASS is as in add_cmd. ENUMLIST is a list
560 of strings which may follow NAME. VAR is address of the variable
561 which will contain the matching string (from ENUMLIST). */
564 add_setshow_enum_cmd (const char *name
,
565 enum command_class theclass
,
566 const char *const *enumlist
,
569 const char *show_doc
,
570 const char *help_doc
,
571 cmd_const_sfunc_ftype
*set_func
,
572 show_value_ftype
*show_func
,
573 struct cmd_list_element
**set_list
,
574 struct cmd_list_element
**show_list
)
576 set_show_commands commands
577 = add_setshow_cmd_full (name
, theclass
, var_enum
, var
,
578 set_doc
, show_doc
, help_doc
,
580 set_list
, show_list
);
581 commands
.set
->enums
= enumlist
;
585 /* See cli-decode.h. */
586 const char * const auto_boolean_enums
[] = { "on", "off", "auto", NULL
};
588 /* Add an auto-boolean command named NAME to both the set and show
589 command list lists. CLASS is as in add_cmd. VAR is address of the
590 variable which will contain the value. DOC is the documentation
591 string. FUNC is the corresponding callback. */
594 add_setshow_auto_boolean_cmd (const char *name
,
595 enum command_class theclass
,
596 enum auto_boolean
*var
,
597 const char *set_doc
, const char *show_doc
,
598 const char *help_doc
,
599 cmd_const_sfunc_ftype
*set_func
,
600 show_value_ftype
*show_func
,
601 struct cmd_list_element
**set_list
,
602 struct cmd_list_element
**show_list
)
604 set_show_commands commands
605 = add_setshow_cmd_full (name
, theclass
, var_auto_boolean
, var
,
606 set_doc
, show_doc
, help_doc
,
608 set_list
, show_list
);
610 commands
.set
->enums
= auto_boolean_enums
;
615 /* See cli-decode.h. */
616 const char * const boolean_enums
[] = { "on", "off", NULL
};
618 /* Add element named NAME to both the set and show command LISTs (the
619 list for set/show or some sublist thereof). CLASS is as in
620 add_cmd. VAR is address of the variable which will contain the
621 value. SET_DOC and SHOW_DOC are the documentation strings.
622 Returns the new command element. */
625 add_setshow_boolean_cmd (const char *name
, enum command_class theclass
, bool *var
,
626 const char *set_doc
, const char *show_doc
,
627 const char *help_doc
,
628 cmd_const_sfunc_ftype
*set_func
,
629 show_value_ftype
*show_func
,
630 struct cmd_list_element
**set_list
,
631 struct cmd_list_element
**show_list
)
633 set_show_commands commands
634 = add_setshow_cmd_full (name
, theclass
, var_boolean
, var
,
635 set_doc
, show_doc
, help_doc
,
637 set_list
, show_list
);
639 commands
.set
->enums
= boolean_enums
;
644 /* Add element named NAME to both the set and show command LISTs (the
645 list for set/show or some sublist thereof). */
648 add_setshow_filename_cmd (const char *name
, enum command_class theclass
,
650 const char *set_doc
, const char *show_doc
,
651 const char *help_doc
,
652 cmd_const_sfunc_ftype
*set_func
,
653 show_value_ftype
*show_func
,
654 struct cmd_list_element
**set_list
,
655 struct cmd_list_element
**show_list
)
657 set_show_commands commands
658 = add_setshow_cmd_full (name
, theclass
, var_filename
, var
,
659 set_doc
, show_doc
, help_doc
,
661 set_list
, show_list
);
663 set_cmd_completer (commands
.set
, filename_completer
);
668 /* Add element named NAME to both the set and show command LISTs (the
669 list for set/show or some sublist thereof). */
672 add_setshow_string_cmd (const char *name
, enum command_class theclass
,
674 const char *set_doc
, const char *show_doc
,
675 const char *help_doc
,
676 cmd_const_sfunc_ftype
*set_func
,
677 show_value_ftype
*show_func
,
678 struct cmd_list_element
**set_list
,
679 struct cmd_list_element
**show_list
)
681 set_show_commands commands
682 = add_setshow_cmd_full (name
, theclass
, var_string
, var
,
683 set_doc
, show_doc
, help_doc
,
685 set_list
, show_list
);
687 /* Disable the default symbol completer. */
688 set_cmd_completer (commands
.set
, nullptr);
693 /* Add element named NAME to both the set and show command LISTs (the
694 list for set/show or some sublist thereof). */
697 add_setshow_string_noescape_cmd (const char *name
, enum command_class theclass
,
699 const char *set_doc
, const char *show_doc
,
700 const char *help_doc
,
701 cmd_const_sfunc_ftype
*set_func
,
702 show_value_ftype
*show_func
,
703 struct cmd_list_element
**set_list
,
704 struct cmd_list_element
**show_list
)
706 set_show_commands commands
707 = add_setshow_cmd_full (name
, theclass
, var_string_noescape
, var
,
708 set_doc
, show_doc
, help_doc
,
710 set_list
, show_list
);
712 /* Disable the default symbol completer. */
713 set_cmd_completer (commands
.set
, nullptr);
718 /* Add element named NAME to both the set and show command LISTs (the
719 list for set/show or some sublist thereof). */
722 add_setshow_optional_filename_cmd (const char *name
, enum command_class theclass
,
724 const char *set_doc
, const char *show_doc
,
725 const char *help_doc
,
726 cmd_const_sfunc_ftype
*set_func
,
727 show_value_ftype
*show_func
,
728 struct cmd_list_element
**set_list
,
729 struct cmd_list_element
**show_list
)
731 set_show_commands commands
732 = add_setshow_cmd_full (name
, theclass
, var_optional_filename
, var
,
733 set_doc
, show_doc
, help_doc
,
735 set_list
, show_list
);
737 set_cmd_completer (commands
.set
, filename_completer
);
742 /* Completes on literal "unlimited". Used by integer commands that
743 support a special "unlimited" value. */
746 integer_unlimited_completer (struct cmd_list_element
*ignore
,
747 completion_tracker
&tracker
,
748 const char *text
, const char *word
)
750 static const char * const keywords
[] =
756 complete_on_enum (tracker
, keywords
, text
, word
);
759 /* Add element named NAME to both the set and show command LISTs (the
760 list for set/show or some sublist thereof). CLASS is as in
761 add_cmd. VAR is address of the variable which will contain the
762 value. SET_DOC and SHOW_DOC are the documentation strings. This
763 function is only used in Python API. Please don't use it elsewhere. */
766 add_setshow_integer_cmd (const char *name
, enum command_class theclass
,
768 const char *set_doc
, const char *show_doc
,
769 const char *help_doc
,
770 cmd_const_sfunc_ftype
*set_func
,
771 show_value_ftype
*show_func
,
772 struct cmd_list_element
**set_list
,
773 struct cmd_list_element
**show_list
)
775 set_show_commands commands
776 = add_setshow_cmd_full (name
, theclass
, var_integer
, var
,
777 set_doc
, show_doc
, help_doc
,
779 set_list
, show_list
);
781 set_cmd_completer (commands
.set
, integer_unlimited_completer
);
786 /* Add element named NAME to both the set and show command LISTs (the
787 list for set/show or some sublist thereof). CLASS is as in
788 add_cmd. VAR is address of the variable which will contain the
789 value. SET_DOC and SHOW_DOC are the documentation strings. */
792 add_setshow_uinteger_cmd (const char *name
, enum command_class theclass
,
794 const char *set_doc
, const char *show_doc
,
795 const char *help_doc
,
796 cmd_const_sfunc_ftype
*set_func
,
797 show_value_ftype
*show_func
,
798 struct cmd_list_element
**set_list
,
799 struct cmd_list_element
**show_list
)
801 set_show_commands commands
802 = add_setshow_cmd_full (name
, theclass
, var_uinteger
, var
,
803 set_doc
, show_doc
, help_doc
,
805 set_list
, show_list
);
807 set_cmd_completer (commands
.set
, integer_unlimited_completer
);
812 /* Add element named NAME to both the set and show command LISTs (the
813 list for set/show or some sublist thereof). CLASS is as in
814 add_cmd. VAR is address of the variable which will contain the
815 value. SET_DOC and SHOW_DOC are the documentation strings. */
818 add_setshow_zinteger_cmd (const char *name
, enum command_class theclass
,
820 const char *set_doc
, const char *show_doc
,
821 const char *help_doc
,
822 cmd_const_sfunc_ftype
*set_func
,
823 show_value_ftype
*show_func
,
824 struct cmd_list_element
**set_list
,
825 struct cmd_list_element
**show_list
)
827 return add_setshow_cmd_full (name
, theclass
, var_zinteger
, var
,
828 set_doc
, show_doc
, help_doc
,
830 set_list
, show_list
);
834 add_setshow_zuinteger_unlimited_cmd (const char *name
,
835 enum command_class theclass
,
838 const char *show_doc
,
839 const char *help_doc
,
840 cmd_const_sfunc_ftype
*set_func
,
841 show_value_ftype
*show_func
,
842 struct cmd_list_element
**set_list
,
843 struct cmd_list_element
**show_list
)
845 set_show_commands commands
846 = add_setshow_cmd_full (name
, theclass
, var_zuinteger_unlimited
, var
,
847 set_doc
, show_doc
, help_doc
,
849 set_list
, show_list
);
851 set_cmd_completer (commands
.set
, integer_unlimited_completer
);
856 /* Add element named NAME to both the set and show command LISTs (the
857 list for set/show or some sublist thereof). CLASS is as in
858 add_cmd. VAR is address of the variable which will contain the
859 value. SET_DOC and SHOW_DOC are the documentation strings. */
862 add_setshow_zuinteger_cmd (const char *name
, enum command_class theclass
,
864 const char *set_doc
, const char *show_doc
,
865 const char *help_doc
,
866 cmd_const_sfunc_ftype
*set_func
,
867 show_value_ftype
*show_func
,
868 struct cmd_list_element
**set_list
,
869 struct cmd_list_element
**show_list
)
871 return add_setshow_cmd_full (name
, theclass
, var_zuinteger
, var
,
872 set_doc
, show_doc
, help_doc
,
874 set_list
, show_list
);
877 /* Remove the command named NAME from the command list. Return the
878 list commands which were aliased to the deleted command. If the
879 command had no aliases, return NULL. The various *HOOKs are set to
880 the pre- and post-hook commands for the deleted command. If the
881 command does not have a hook, the corresponding out parameter is
884 static struct cmd_list_element
*
885 delete_cmd (const char *name
, struct cmd_list_element
**list
,
886 struct cmd_list_element
**prehook
,
887 struct cmd_list_element
**prehookee
,
888 struct cmd_list_element
**posthook
,
889 struct cmd_list_element
**posthookee
)
891 struct cmd_list_element
*iter
;
892 struct cmd_list_element
**previous_chain_ptr
;
893 struct cmd_list_element
*aliases
= NULL
;
899 previous_chain_ptr
= list
;
901 for (iter
= *previous_chain_ptr
; iter
; iter
= *previous_chain_ptr
)
903 if (strcmp (iter
->name
, name
) == 0)
906 iter
->destroyer (iter
, iter
->context ());
908 if (iter
->hookee_pre
)
909 iter
->hookee_pre
->hook_pre
= 0;
910 *prehook
= iter
->hook_pre
;
911 *prehookee
= iter
->hookee_pre
;
912 if (iter
->hookee_post
)
913 iter
->hookee_post
->hook_post
= 0;
914 *posthook
= iter
->hook_post
;
915 *posthookee
= iter
->hookee_post
;
917 /* Update the link. */
918 *previous_chain_ptr
= iter
->next
;
920 aliases
= iter
->aliases
;
922 /* If this command was an alias, remove it from the list of
924 if (iter
->is_alias ())
926 struct cmd_list_element
**prevp
= &iter
->alias_target
->aliases
;
927 struct cmd_list_element
*a
= *prevp
;
931 prevp
= &a
->alias_chain
;
934 *prevp
= iter
->alias_chain
;
939 /* We won't see another command with the same name. */
943 previous_chain_ptr
= &iter
->next
;
949 /* Shorthands to the commands above. */
951 /* Add an element to the list of info subcommands. */
953 struct cmd_list_element
*
954 add_info (const char *name
, cmd_const_cfunc_ftype
*fun
, const char *doc
)
956 return add_cmd (name
, class_info
, fun
, doc
, &infolist
);
959 /* Add an alias to the list of info subcommands. */
962 add_info_alias (const char *name
, cmd_list_element
*target
, int abbrev_flag
)
964 return add_alias_cmd (name
, target
, class_run
, abbrev_flag
, &infolist
);
967 /* Add an element to the list of commands. */
969 struct cmd_list_element
*
970 add_com (const char *name
, enum command_class theclass
,
971 cmd_const_cfunc_ftype
*fun
,
974 return add_cmd (name
, theclass
, fun
, doc
, &cmdlist
);
977 /* Add an alias or abbreviation command to the list of commands.
978 For aliases predefined by GDB (such as bt), THECLASS must be
979 different of class_alias, as class_alias is used to identify
980 user defined aliases. */
983 add_com_alias (const char *name
, cmd_list_element
*target
,
984 command_class theclass
, int abbrev_flag
)
986 return add_alias_cmd (name
, target
, theclass
, abbrev_flag
, &cmdlist
);
989 /* Add an element with a suppress notification to the list of commands. */
991 struct cmd_list_element
*
992 add_com_suppress_notification (const char *name
, enum command_class theclass
,
993 cmd_const_cfunc_ftype
*fun
, const char *doc
,
994 int *suppress_notification
)
996 return add_cmd_suppress_notification (name
, theclass
, fun
, doc
,
997 &cmdlist
, suppress_notification
);
1000 /* Print the prefix of C followed by name of C in title style. */
1003 fput_command_name_styled (struct cmd_list_element
*c
, struct ui_file
*stream
)
1005 std::string prefixname
1006 = c
->prefix
== nullptr ? "" : c
->prefix
->prefixname ();
1008 fprintf_styled (stream
, title_style
.style (), "%s%s",
1009 prefixname
.c_str (), c
->name
);
1012 /* Print the definition of alias C using title style for alias
1013 and aliased command. */
1016 fput_alias_definition_styled (struct cmd_list_element
*c
,
1017 struct ui_file
*stream
)
1019 gdb_assert (c
->is_alias ());
1020 fputs_filtered (" alias ", stream
);
1021 fput_command_name_styled (c
, stream
);
1022 fprintf_filtered (stream
, " = ");
1023 fput_command_name_styled (c
->alias_target
, stream
);
1024 fprintf_filtered (stream
, " %s\n", c
->default_args
.c_str ());
1027 /* Print the definition of the aliases of CMD that have default args. */
1030 fput_aliases_definition_styled (struct cmd_list_element
*cmd
,
1031 struct ui_file
*stream
)
1033 if (cmd
->aliases
!= nullptr)
1035 for (cmd_list_element
*iter
= cmd
->aliases
;
1037 iter
= iter
->alias_chain
)
1039 if (!iter
->default_args
.empty ())
1040 fput_alias_definition_styled (iter
, stream
);
1046 /* If C has one or more aliases, style print the name of C and
1047 the name of its aliases, separated by commas.
1048 If ALWAYS_FPUT_C_NAME, print the name of C even if it has no aliases.
1049 If one or more names are printed, POSTFIX is printed after the last name.
1053 fput_command_names_styled (struct cmd_list_element
*c
,
1054 bool always_fput_c_name
, const char *postfix
,
1055 struct ui_file
*stream
)
1057 if (always_fput_c_name
|| c
->aliases
!= nullptr)
1058 fput_command_name_styled (c
, stream
);
1059 if (c
->aliases
!= nullptr)
1061 for (cmd_list_element
*iter
= c
->aliases
; iter
; iter
= iter
->alias_chain
)
1063 fputs_filtered (", ", stream
);
1065 fput_command_name_styled (iter
, stream
);
1068 if (always_fput_c_name
|| c
->aliases
!= nullptr)
1069 fputs_filtered (postfix
, stream
);
1072 /* If VERBOSE, print the full help for command C and highlight the
1073 documentation parts matching HIGHLIGHT,
1074 otherwise print only one-line help for command C. */
1077 print_doc_of_command (struct cmd_list_element
*c
, const char *prefix
,
1078 bool verbose
, compiled_regex
&highlight
,
1079 struct ui_file
*stream
)
1081 /* When printing the full documentation, add a line to separate
1082 this documentation from the previous command help, in the likely
1083 case that apropos finds several commands. */
1085 fputs_filtered ("\n", stream
);
1087 fput_command_names_styled (c
, true,
1088 verbose
? "" : " -- ", stream
);
1091 fputs_filtered ("\n", stream
);
1092 fput_aliases_definition_styled (c
, stream
);
1093 fputs_highlighted (c
->doc
, highlight
, stream
);
1094 fputs_filtered ("\n", stream
);
1098 print_doc_line (stream
, c
->doc
, false);
1099 fputs_filtered ("\n", stream
);
1100 fput_aliases_definition_styled (c
, stream
);
1104 /* Recursively walk the commandlist structures, and print out the
1105 documentation of commands that match our regex in either their
1106 name, or their documentation.
1107 If VERBOSE, prints the complete documentation and highlight the
1108 documentation parts matching REGEX, otherwise prints only
1112 apropos_cmd (struct ui_file
*stream
,
1113 struct cmd_list_element
*commandlist
,
1114 bool verbose
, compiled_regex
®ex
, const char *prefix
)
1116 struct cmd_list_element
*c
;
1119 /* Walk through the commands. */
1120 for (c
=commandlist
;c
;c
=c
->next
)
1124 /* Command aliases/abbreviations are skipped to ensure we print the
1125 doc of a command only once, when encountering the aliased
1130 returnvalue
= -1; /* Needed to avoid double printing. */
1131 if (c
->name
!= NULL
)
1133 size_t name_len
= strlen (c
->name
);
1135 /* Try to match against the name. */
1136 returnvalue
= regex
.search (c
->name
, name_len
, 0, name_len
, NULL
);
1137 if (returnvalue
>= 0)
1138 print_doc_of_command (c
, prefix
, verbose
, regex
, stream
);
1140 /* Try to match against the name of the aliases. */
1141 for (cmd_list_element
*iter
= c
->aliases
;
1142 returnvalue
< 0 && iter
;
1143 iter
= iter
->alias_chain
)
1145 name_len
= strlen (iter
->name
);
1146 returnvalue
= regex
.search (iter
->name
, name_len
, 0, name_len
, NULL
);
1147 if (returnvalue
>= 0)
1148 print_doc_of_command (c
, prefix
, verbose
, regex
, stream
);
1151 if (c
->doc
!= NULL
&& returnvalue
< 0)
1153 size_t doc_len
= strlen (c
->doc
);
1155 /* Try to match against documentation. */
1156 if (regex
.search (c
->doc
, doc_len
, 0, doc_len
, NULL
) >= 0)
1157 print_doc_of_command (c
, prefix
, verbose
, regex
, stream
);
1159 /* Check if this command has subcommands. */
1160 if (c
->is_prefix ())
1162 /* Recursively call ourselves on the subcommand list,
1163 passing the right prefix in. */
1164 apropos_cmd (stream
, *c
->subcommands
, verbose
, regex
,
1165 c
->prefixname ().c_str ());
1170 /* This command really has to deal with two things:
1171 1) I want documentation on *this string* (usually called by
1172 "help commandname").
1174 2) I want documentation on *this list* (usually called by giving a
1175 command that requires subcommands. Also called by saying just
1178 I am going to split this into two separate commands, help_cmd and
1182 help_cmd (const char *command
, struct ui_file
*stream
)
1184 struct cmd_list_element
*c
, *alias
, *prefix_cmd
, *c_cmd
;
1188 help_list (cmdlist
, "", all_classes
, stream
);
1192 if (strcmp (command
, "all") == 0)
1198 const char *orig_command
= command
;
1199 c
= lookup_cmd (&command
, cmdlist
, "", NULL
, 0, 0);
1204 lookup_cmd_composition (orig_command
, &alias
, &prefix_cmd
, &c_cmd
);
1206 /* There are three cases here.
1207 If c->subcommands is nonzero, we have a prefix command.
1208 Print its documentation, then list its subcommands.
1210 If c->func is non NULL, we really have a command. Print its
1211 documentation and return.
1213 If c->func is NULL, we have a class name. Print its
1214 documentation (as if it were a command) and then set class to the
1215 number of this class so that the commands in the class will be
1218 /* If the user asked 'help somecommand' and there is no alias,
1219 the false indicates to not output the (single) command name. */
1220 fput_command_names_styled (c
, false, "\n", stream
);
1221 fput_aliases_definition_styled (c
, stream
);
1222 fputs_filtered (c
->doc
, stream
);
1223 fputs_filtered ("\n", stream
);
1225 if (!c
->is_prefix () && !c
->is_command_class_help ())
1228 fprintf_filtered (stream
, "\n");
1230 /* If this is a prefix command, print it's subcommands. */
1231 if (c
->is_prefix ())
1232 help_list (*c
->subcommands
, c
->prefixname ().c_str (),
1233 all_commands
, stream
);
1235 /* If this is a class name, print all of the commands in the class. */
1236 if (c
->is_command_class_help ())
1237 help_list (cmdlist
, "", c
->theclass
, stream
);
1239 if (c
->hook_pre
|| c
->hook_post
)
1240 fprintf_filtered (stream
,
1241 "\nThis command has a hook (or hooks) defined:\n");
1244 fprintf_filtered (stream
,
1245 "\tThis command is run after : %s (pre hook)\n",
1248 fprintf_filtered (stream
,
1249 "\tThis command is run before : %s (post hook)\n",
1250 c
->hook_post
->name
);
1254 * Get a specific kind of help on a command list.
1257 * CMDTYPE is the prefix to use in the title string.
1258 * CLASS is the class with which to list the nodes of this list (see
1259 * documentation for help_cmd_list below), As usual, ALL_COMMANDS for
1260 * everything, ALL_CLASSES for just classes, and non-negative for only things
1261 * in a specific class.
1262 * and STREAM is the output stream on which to print things.
1263 * If you call this routine with a class >= 0, it recurses.
1266 help_list (struct cmd_list_element
*list
, const char *cmdtype
,
1267 enum command_class theclass
, struct ui_file
*stream
)
1270 char *cmdtype1
, *cmdtype2
;
1272 /* If CMDTYPE is "foo ", CMDTYPE1 gets " foo" and CMDTYPE2 gets "foo sub".
1274 len
= strlen (cmdtype
);
1275 cmdtype1
= (char *) alloca (len
+ 1);
1277 cmdtype2
= (char *) alloca (len
+ 4);
1282 memcpy (cmdtype1
+ 1, cmdtype
, len
- 1);
1284 memcpy (cmdtype2
, cmdtype
, len
- 1);
1285 strcpy (cmdtype2
+ len
- 1, " sub");
1288 if (theclass
== all_classes
)
1289 fprintf_filtered (stream
, "List of classes of %scommands:\n\n", cmdtype2
);
1291 fprintf_filtered (stream
, "List of %scommands:\n\n", cmdtype2
);
1293 help_cmd_list (list
, theclass
, theclass
>= 0, stream
);
1295 if (theclass
== all_classes
)
1297 fprintf_filtered (stream
, "\n\
1298 Type \"help%s\" followed by a class name for a list of commands in ",
1301 fprintf_filtered (stream
, "that class.");
1303 fprintf_filtered (stream
, "\n\
1304 Type \"help all\" for the list of all commands.");
1307 fprintf_filtered (stream
, "\nType \"help%s\" followed by %scommand name ",
1308 cmdtype1
, cmdtype2
);
1310 fputs_filtered ("for ", stream
);
1312 fputs_filtered ("full ", stream
);
1314 fputs_filtered ("documentation.\n", stream
);
1315 fputs_filtered ("Type \"apropos word\" to search "
1316 "for commands related to \"word\".\n", stream
);
1317 fputs_filtered ("Type \"apropos -v word\" for full documentation", stream
);
1319 fputs_filtered (" of commands related to \"word\".\n", stream
);
1320 fputs_filtered ("Command name abbreviations are allowed if unambiguous.\n",
1325 help_all (struct ui_file
*stream
)
1327 struct cmd_list_element
*c
;
1328 int seen_unclassified
= 0;
1330 for (c
= cmdlist
; c
; c
= c
->next
)
1334 /* If this is a class name, print all of the commands in the
1337 if (c
->is_command_class_help ())
1339 fprintf_filtered (stream
, "\nCommand class: %s\n\n", c
->name
);
1340 help_cmd_list (cmdlist
, c
->theclass
, true, stream
);
1344 /* While it's expected that all commands are in some class,
1345 as a safety measure, we'll print commands outside of any
1346 class at the end. */
1348 for (c
= cmdlist
; c
; c
= c
->next
)
1353 if (c
->theclass
== no_class
)
1355 if (!seen_unclassified
)
1357 fprintf_filtered (stream
, "\nUnclassified commands\n\n");
1358 seen_unclassified
= 1;
1360 print_help_for_command (c
, true, stream
);
1366 /* See cli-decode.h. */
1369 print_doc_line (struct ui_file
*stream
, const char *str
,
1370 bool for_value_prefix
)
1372 static char *line_buffer
= 0;
1373 static int line_size
;
1379 line_buffer
= (char *) xmalloc (line_size
);
1382 /* Searches for the first end of line or the end of STR. */
1384 while (*p
&& *p
!= '\n')
1386 if (p
- str
> line_size
- 1)
1388 line_size
= p
- str
+ 1;
1389 xfree (line_buffer
);
1390 line_buffer
= (char *) xmalloc (line_size
);
1392 strncpy (line_buffer
, str
, p
- str
);
1393 if (for_value_prefix
)
1395 if (islower (line_buffer
[0]))
1396 line_buffer
[0] = toupper (line_buffer
[0]);
1397 gdb_assert (p
> str
);
1398 if (line_buffer
[p
- str
- 1] == '.')
1399 line_buffer
[p
- str
- 1] = '\0';
1401 line_buffer
[p
- str
] = '\0';
1404 line_buffer
[p
- str
] = '\0';
1405 fputs_filtered (line_buffer
, stream
);
1408 /* Print one-line help for command C.
1409 If RECURSE is non-zero, also print one-line descriptions
1410 of all prefixed subcommands. */
1412 print_help_for_command (struct cmd_list_element
*c
,
1413 bool recurse
, struct ui_file
*stream
)
1415 fput_command_names_styled (c
, true, " -- ", stream
);
1416 print_doc_line (stream
, c
->doc
, false);
1417 fputs_filtered ("\n", stream
);
1418 if (!c
->default_args
.empty ())
1419 fput_alias_definition_styled (c
, stream
);
1420 fput_aliases_definition_styled (c
, stream
);
1424 && c
->abbrev_flag
== 0)
1425 /* Subcommands of a prefix command typically have 'all_commands'
1426 as class. If we pass CLASS to recursive invocation,
1427 most often we won't see anything. */
1428 help_cmd_list (*c
->subcommands
, all_commands
, true, stream
);
1432 * Implement a help command on command list LIST.
1433 * RECURSE should be non-zero if this should be done recursively on
1434 * all sublists of LIST.
1435 * STREAM is the stream upon which the output should be written.
1436 * THECLASS should be:
1437 * A non-negative class number to list only commands in that
1438 * ALL_COMMANDS to list all commands in list.
1439 * ALL_CLASSES to list all classes in list.
1441 * Note that aliases are only shown when THECLASS is class_alias.
1442 * In the other cases, the aliases will be shown together with their
1445 * Note that RECURSE will be active on *all* sublists, not just the
1446 * ones selected by the criteria above (ie. the selection mechanism
1447 * is at the low level, not the high-level).
1451 help_cmd_list (struct cmd_list_element
*list
, enum command_class theclass
,
1452 bool recurse
, struct ui_file
*stream
)
1454 struct cmd_list_element
*c
;
1456 for (c
= list
; c
; c
= c
->next
)
1458 if (c
->abbrev_flag
== 1 || c
->cmd_deprecated
)
1460 /* Do not show abbreviations or deprecated commands. */
1464 if (c
->is_alias () && theclass
!= class_alias
)
1466 /* Do not show an alias, unless specifically showing the
1467 list of aliases: for all other classes, an alias is
1468 shown (if needed) together with its aliased command. */
1472 if (theclass
== all_commands
1473 || (theclass
== all_classes
&& c
->is_command_class_help ())
1474 || (theclass
== c
->theclass
&& !c
->is_command_class_help ()))
1477 - showing all commands
1478 - showing all classes and C is a help class
1479 - showing commands of THECLASS and C is not the help class */
1481 /* If we show the class_alias and C is an alias, do not recurse,
1482 as this would show the (possibly very long) not very useful
1483 list of sub-commands of the aliased command. */
1484 print_help_for_command
1486 recurse
&& (theclass
!= class_alias
|| !c
->is_alias ()),
1492 && (theclass
== class_user
|| theclass
== class_alias
)
1495 /* User-defined commands or aliases may be subcommands. */
1496 help_cmd_list (*c
->subcommands
, theclass
, recurse
, stream
);
1500 /* Do not show C or recurse on C, e.g. because C does not belong to
1501 THECLASS or because C is a help class. */
1506 /* Search the input clist for 'command'. Return the command if
1507 found (or NULL if not), and return the number of commands
1510 static struct cmd_list_element
*
1511 find_cmd (const char *command
, int len
, struct cmd_list_element
*clist
,
1512 int ignore_help_classes
, int *nfound
)
1514 struct cmd_list_element
*found
, *c
;
1518 for (c
= clist
; c
; c
= c
->next
)
1519 if (!strncmp (command
, c
->name
, len
)
1520 && (!ignore_help_classes
|| !c
->is_command_class_help ()))
1524 if (c
->name
[len
] == '\0')
1533 /* Return the length of command name in TEXT. */
1536 find_command_name_length (const char *text
)
1538 const char *p
= text
;
1540 /* Treating underscores as part of command words is important
1541 so that "set args_foo()" doesn't get interpreted as
1542 "set args _foo()". */
1543 /* Some characters are only used for TUI specific commands.
1544 However, they are always allowed for the sake of consistency.
1546 Note that this is larger than the character set allowed when
1547 creating user-defined commands. */
1549 /* Recognize the single character commands so that, e.g., "!ls"
1550 works as expected. */
1551 if (*p
== '!' || *p
== '|')
1554 while (valid_cmd_char_p (*p
)
1555 /* Characters used by TUI specific commands. */
1556 || *p
== '+' || *p
== '<' || *p
== '>' || *p
== '$')
1562 /* See command.h. */
1565 valid_cmd_char_p (int c
)
1567 /* Alas "42" is a legitimate user-defined command.
1568 In the interests of not breaking anything we preserve that. */
1570 return isalnum (c
) || c
== '-' || c
== '_' || c
== '.';
1573 /* See command.h. */
1576 valid_user_defined_cmd_name_p (const char *name
)
1583 for (p
= name
; *p
!= '\0'; ++p
)
1585 if (valid_cmd_char_p (*p
))
1594 /* See command.h. */
1596 struct cmd_list_element
*
1597 lookup_cmd_1 (const char **text
, struct cmd_list_element
*clist
,
1598 struct cmd_list_element
**result_list
, std::string
*default_args
,
1599 int ignore_help_classes
, bool lookup_for_completion_p
)
1603 struct cmd_list_element
*found
, *c
;
1604 bool found_alias
= false;
1605 const char *line
= *text
;
1607 while (**text
== ' ' || **text
== '\t')
1610 /* Identify the name of the command. */
1611 len
= find_command_name_length (*text
);
1613 /* If nothing but whitespace, return 0. */
1617 /* *text and p now bracket the first command word to lookup (and
1618 it's length is len). We copy this into a local temporary. */
1621 command
= (char *) alloca (len
+ 1);
1622 memcpy (command
, *text
, len
);
1623 command
[len
] = '\0';
1628 found
= find_cmd (command
, len
, clist
, ignore_help_classes
, &nfound
);
1630 /* If nothing matches, we have a simple failure. */
1636 if (result_list
!= nullptr)
1637 /* Will be modified in calling routine
1638 if we know what the prefix command is. */
1640 if (default_args
!= nullptr)
1641 *default_args
= std::string ();
1642 return CMD_LIST_AMBIGUOUS
; /* Ambiguous. */
1645 /* We've matched something on this list. Move text pointer forward. */
1649 if (found
->is_alias ())
1651 /* We drop the alias (abbreviation) in favor of the command it
1652 is pointing to. If the alias is deprecated, though, we need to
1653 warn the user about it before we drop it. Note that while we
1654 are warning about the alias, we may also warn about the command
1655 itself and we will adjust the appropriate DEPRECATED_WARN_USER
1658 if (found
->deprecated_warn_user
&& !lookup_for_completion_p
)
1659 deprecated_cmd_warning (line
, clist
);
1662 /* Return the default_args of the alias, not the default_args
1663 of the command it is pointing to. */
1664 if (default_args
!= nullptr)
1665 *default_args
= found
->default_args
;
1666 found
= found
->alias_target
;
1669 /* If we found a prefix command, keep looking. */
1671 if (found
->is_prefix ())
1673 c
= lookup_cmd_1 (text
, *found
->subcommands
, result_list
, default_args
,
1674 ignore_help_classes
, lookup_for_completion_p
);
1677 /* Didn't find anything; this is as far as we got. */
1678 if (result_list
!= nullptr)
1679 *result_list
= clist
;
1680 if (!found_alias
&& default_args
!= nullptr)
1681 *default_args
= found
->default_args
;
1684 else if (c
== CMD_LIST_AMBIGUOUS
)
1686 /* We've gotten this far properly, but the next step is
1687 ambiguous. We need to set the result list to the best
1688 we've found (if an inferior hasn't already set it). */
1689 if (result_list
!= nullptr)
1691 /* This used to say *result_list = *found->subcommands.
1692 If that was correct, need to modify the documentation
1693 at the top of this function to clarify what is
1694 supposed to be going on. */
1695 *result_list
= found
;
1696 /* For ambiguous commands, do not return any default_args args. */
1697 if (default_args
!= nullptr)
1698 *default_args
= std::string ();
1709 if (result_list
!= nullptr)
1710 *result_list
= clist
;
1711 if (!found_alias
&& default_args
!= nullptr)
1712 *default_args
= found
->default_args
;
1717 /* All this hair to move the space to the front of cmdtype */
1720 undef_cmd_error (const char *cmdtype
, const char *q
)
1722 error (_("Undefined %scommand: \"%s\". Try \"help%s%.*s\"."),
1725 *cmdtype
? " " : "",
1726 (int) strlen (cmdtype
) - 1,
1730 /* Look up the contents of *LINE as a command in the command list LIST.
1731 LIST is a chain of struct cmd_list_element's.
1732 If it is found, return the struct cmd_list_element for that command,
1733 update *LINE to point after the command name, at the first argument
1734 and update *DEFAULT_ARGS (if DEFAULT_ARGS is not null) to the default
1735 args to prepend to the user provided args when running the command.
1736 Note that if the found cmd_list_element is found via an alias,
1737 the default args of the alias are returned.
1739 If not found, call error if ALLOW_UNKNOWN is zero
1740 otherwise (or if error returns) return zero.
1741 Call error if specified command is ambiguous,
1742 unless ALLOW_UNKNOWN is negative.
1743 CMDTYPE precedes the word "command" in the error message.
1745 If IGNORE_HELP_CLASSES is nonzero, ignore any command list
1746 elements which are actually help classes rather than commands (i.e.
1747 the function field of the struct cmd_list_element is 0). */
1749 struct cmd_list_element
*
1750 lookup_cmd (const char **line
, struct cmd_list_element
*list
,
1751 const char *cmdtype
,
1752 std::string
*default_args
,
1753 int allow_unknown
, int ignore_help_classes
)
1755 struct cmd_list_element
*last_list
= 0;
1756 struct cmd_list_element
*c
;
1758 /* Note: Do not remove trailing whitespace here because this
1759 would be wrong for complete_command. Jim Kingdon */
1762 error (_("Lack of needed %scommand"), cmdtype
);
1764 c
= lookup_cmd_1 (line
, list
, &last_list
, default_args
, ignore_help_classes
);
1771 int len
= find_command_name_length (*line
);
1773 q
= (char *) alloca (len
+ 1);
1774 strncpy (q
, *line
, len
);
1776 undef_cmd_error (cmdtype
, q
);
1781 else if (c
== CMD_LIST_AMBIGUOUS
)
1783 /* Ambigous. Local values should be off subcommands or called
1785 int local_allow_unknown
= (last_list
? last_list
->allow_unknown
:
1787 std::string local_cmdtype
1788 = last_list
? last_list
->prefixname () : cmdtype
;
1789 struct cmd_list_element
*local_list
=
1790 (last_list
? *(last_list
->subcommands
) : list
);
1792 if (local_allow_unknown
< 0)
1795 return last_list
; /* Found something. */
1797 return 0; /* Found nothing. */
1801 /* Report as error. */
1806 ((*line
)[amb_len
] && (*line
)[amb_len
] != ' '
1807 && (*line
)[amb_len
] != '\t');
1812 for (c
= local_list
; c
; c
= c
->next
)
1813 if (!strncmp (*line
, c
->name
, amb_len
))
1815 if (strlen (ambbuf
) + strlen (c
->name
) + 6
1816 < (int) sizeof ambbuf
)
1818 if (strlen (ambbuf
))
1819 strcat (ambbuf
, ", ");
1820 strcat (ambbuf
, c
->name
);
1824 strcat (ambbuf
, "..");
1828 error (_("Ambiguous %scommand \"%s\": %s."),
1829 local_cmdtype
.c_str (), *line
, ambbuf
);
1834 if (c
->type
== set_cmd
&& **line
!= '\0' && !isspace (**line
))
1835 error (_("Argument must be preceded by space."));
1837 /* We've got something. It may still not be what the caller
1838 wants (if this command *needs* a subcommand). */
1839 while (**line
== ' ' || **line
== '\t')
1842 if (c
->is_prefix () && **line
&& !c
->allow_unknown
)
1843 undef_cmd_error (c
->prefixname ().c_str (), *line
);
1845 /* Seems to be what he wants. Return it. */
1851 /* See command.h. */
1853 struct cmd_list_element
*
1854 lookup_cmd_exact (const char *name
,
1855 struct cmd_list_element
*list
,
1856 bool ignore_help_classes
)
1858 const char *tem
= name
;
1859 struct cmd_list_element
*cmd
= lookup_cmd (&tem
, list
, "", NULL
, -1,
1860 ignore_help_classes
);
1861 if (cmd
!= nullptr && strcmp (name
, cmd
->name
) != 0)
1866 /* We are here presumably because an alias or command in TEXT is
1867 deprecated and a warning message should be generated. This
1868 function decodes TEXT and potentially generates a warning message
1871 Example for 'set endian big' which has a fictitious alias 'seb'.
1873 If alias wasn't used in TEXT, and the command is deprecated:
1874 "warning: 'set endian big' is deprecated."
1876 If alias was used, and only the alias is deprecated:
1877 "warning: 'seb' an alias for the command 'set endian big' is deprecated."
1879 If alias was used and command is deprecated (regardless of whether
1880 the alias itself is deprecated:
1882 "warning: 'set endian big' (seb) is deprecated."
1884 After the message has been sent, clear the appropriate flags in the
1885 command and/or the alias so the user is no longer bothered.
1889 deprecated_cmd_warning (const char *text
, struct cmd_list_element
*list
)
1891 struct cmd_list_element
*alias
= nullptr;
1892 struct cmd_list_element
*cmd
= nullptr;
1894 /* Return if text doesn't evaluate to a command. We place this lookup
1895 within its own scope so that the PREFIX_CMD local is not visible
1896 later in this function. The value returned in PREFIX_CMD is based on
1897 the prefix found in TEXT, and is our case this prefix can be missing
1898 in some situations (when LIST is not the global CMDLIST).
1900 It is better for our purposes to use the prefix commands directly from
1901 the ALIAS and CMD results. */
1903 struct cmd_list_element
*prefix_cmd
= nullptr;
1904 if (!lookup_cmd_composition_1 (text
, &alias
, &prefix_cmd
, &cmd
, list
))
1908 /* Return if nothing is deprecated. */
1909 if (!((alias
!= nullptr ? alias
->deprecated_warn_user
: 0)
1910 || cmd
->deprecated_warn_user
))
1913 /* Join command prefix (if any) and the command name. */
1914 std::string tmp_cmd_str
;
1915 if (cmd
->prefix
!= nullptr)
1916 tmp_cmd_str
+= cmd
->prefix
->prefixname ();
1917 tmp_cmd_str
+= std::string (cmd
->name
);
1919 /* Display the appropriate first line, this warns that the thing the user
1920 entered is deprecated. */
1921 if (alias
!= nullptr)
1923 /* Join the alias prefix (if any) and the alias name. */
1924 std::string tmp_alias_str
;
1925 if (alias
->prefix
!= nullptr)
1926 tmp_alias_str
+= alias
->prefix
->prefixname ();
1927 tmp_alias_str
+= std::string (alias
->name
);
1929 if (cmd
->cmd_deprecated
)
1930 printf_filtered (_("Warning: command '%ps' (%ps) is deprecated.\n"),
1931 styled_string (title_style
.style (),
1932 tmp_cmd_str
.c_str ()),
1933 styled_string (title_style
.style (),
1934 tmp_alias_str
.c_str ()));
1936 printf_filtered (_("Warning: '%ps', an alias for the command '%ps', "
1937 "is deprecated.\n"),
1938 styled_string (title_style
.style (),
1939 tmp_alias_str
.c_str ()),
1940 styled_string (title_style
.style (),
1941 tmp_cmd_str
.c_str ()));
1944 printf_filtered (_("Warning: command '%ps' is deprecated.\n"),
1945 styled_string (title_style
.style (),
1946 tmp_cmd_str
.c_str ()));
1948 /* Now display a second line indicating what the user should use instead.
1949 If it is only the alias that is deprecated, we want to indicate the
1950 new alias, otherwise we'll indicate the new command. */
1951 const char *replacement
;
1952 if (alias
!= nullptr && !cmd
->cmd_deprecated
)
1953 replacement
= alias
->replacement
;
1955 replacement
= cmd
->replacement
;
1956 if (replacement
!= nullptr)
1957 printf_filtered (_("Use '%ps'.\n\n"),
1958 styled_string (title_style
.style (),
1961 printf_filtered (_("No alternative known.\n\n"));
1963 /* We've warned you, now we'll keep quiet. */
1964 if (alias
!= nullptr)
1965 alias
->deprecated_warn_user
= 0;
1966 cmd
->deprecated_warn_user
= 0;
1969 /* Look up the contents of TEXT as a command in the command list CUR_LIST.
1970 Return 1 on success, 0 on failure.
1972 If TEXT refers to an alias, *ALIAS will point to that alias.
1974 If TEXT is a subcommand (i.e. one that is preceded by a prefix
1975 command) set *PREFIX_CMD.
1977 Set *CMD to point to the command TEXT indicates.
1979 If any of *ALIAS, *PREFIX_CMD, or *CMD cannot be determined or do not
1980 exist, they are NULL when we return.
1985 lookup_cmd_composition_1 (const char *text
,
1986 struct cmd_list_element
**alias
,
1987 struct cmd_list_element
**prefix_cmd
,
1988 struct cmd_list_element
**cmd
,
1989 struct cmd_list_element
*cur_list
)
1992 *prefix_cmd
= cur_list
->prefix
;
1995 text
= skip_spaces (text
);
1997 /* Go through as many command lists as we need to, to find the command
2001 /* Identify the name of the command. */
2002 int len
= find_command_name_length (text
);
2004 /* If nothing but whitespace, return. */
2008 /* TEXT is the start of the first command word to lookup (and
2009 it's length is LEN). We copy this into a local temporary. */
2010 std::string
command (text
, len
);
2014 *cmd
= find_cmd (command
.c_str (), len
, cur_list
, 1, &nfound
);
2016 /* We only handle the case where a single command was found. */
2017 if (*cmd
== CMD_LIST_AMBIGUOUS
|| *cmd
== nullptr)
2021 if ((*cmd
)->is_alias ())
2023 /* If the command was actually an alias, we note that an
2024 alias was used (by assigning *ALIAS) and we set *CMD. */
2026 *cmd
= (*cmd
)->alias_target
;
2031 text
= skip_spaces (text
);
2033 if ((*cmd
)->is_prefix () && *text
!= '\0')
2035 cur_list
= *(*cmd
)->subcommands
;
2043 /* Look up the contents of TEXT as a command in the command list 'cmdlist'.
2044 Return 1 on success, 0 on failure.
2046 If TEXT refers to an alias, *ALIAS will point to that alias.
2048 If TEXT is a subcommand (i.e. one that is preceded by a prefix
2049 command) set *PREFIX_CMD.
2051 Set *CMD to point to the command TEXT indicates.
2053 If any of *ALIAS, *PREFIX_CMD, or *CMD cannot be determined or do not
2054 exist, they are NULL when we return.
2059 lookup_cmd_composition (const char *text
,
2060 struct cmd_list_element
**alias
,
2061 struct cmd_list_element
**prefix_cmd
,
2062 struct cmd_list_element
**cmd
)
2064 return lookup_cmd_composition_1 (text
, alias
, prefix_cmd
, cmd
, cmdlist
);
2067 /* Helper function for SYMBOL_COMPLETION_FUNCTION. */
2069 /* Return a vector of char pointers which point to the different
2070 possible completions in LIST of TEXT.
2072 WORD points in the same buffer as TEXT, and completions should be
2073 returned relative to this position. For example, suppose TEXT is
2074 "foo" and we want to complete to "foobar". If WORD is "oo", return
2075 "oobar"; if WORD is "baz/foo", return "baz/foobar". */
2078 complete_on_cmdlist (struct cmd_list_element
*list
,
2079 completion_tracker
&tracker
,
2080 const char *text
, const char *word
,
2081 int ignore_help_classes
)
2083 struct cmd_list_element
*ptr
;
2084 int textlen
= strlen (text
);
2086 int saw_deprecated_match
= 0;
2088 /* We do one or two passes. In the first pass, we skip deprecated
2089 commands. If we see no matching commands in the first pass, and
2090 if we did happen to see a matching deprecated command, we do
2091 another loop to collect those. */
2092 for (pass
= 0; pass
< 2; ++pass
)
2094 bool got_matches
= false;
2096 for (ptr
= list
; ptr
; ptr
= ptr
->next
)
2097 if (!strncmp (ptr
->name
, text
, textlen
)
2098 && !ptr
->abbrev_flag
2099 && (!ignore_help_classes
|| !ptr
->is_command_class_help ()
2100 || ptr
->is_prefix ()))
2104 if (ptr
->cmd_deprecated
)
2106 saw_deprecated_match
= 1;
2111 tracker
.add_completion
2112 (make_completion_match_str (ptr
->name
, text
, word
));
2119 /* If we saw no matching deprecated commands in the first pass,
2121 if (!saw_deprecated_match
)
2126 /* Helper function for SYMBOL_COMPLETION_FUNCTION. */
2128 /* Add the different possible completions in ENUMLIST of TEXT.
2130 WORD points in the same buffer as TEXT, and completions should be
2131 returned relative to this position. For example, suppose TEXT is "foo"
2132 and we want to complete to "foobar". If WORD is "oo", return
2133 "oobar"; if WORD is "baz/foo", return "baz/foobar". */
2136 complete_on_enum (completion_tracker
&tracker
,
2137 const char *const *enumlist
,
2138 const char *text
, const char *word
)
2140 int textlen
= strlen (text
);
2144 for (i
= 0; (name
= enumlist
[i
]) != NULL
; i
++)
2145 if (strncmp (name
, text
, textlen
) == 0)
2146 tracker
.add_completion (make_completion_match_str (name
, text
, word
));
2149 /* Call the command function. */
2151 cmd_func (struct cmd_list_element
*cmd
, const char *args
, int from_tty
)
2153 if (!cmd
->is_command_class_help ())
2155 gdb::optional
<scoped_restore_tmpl
<int>> restore_suppress
;
2157 if (cmd
->suppress_notification
!= NULL
)
2158 restore_suppress
.emplace (cmd
->suppress_notification
, 1);
2160 (*cmd
->func
) (cmd
, args
, from_tty
);
2163 error (_("Invalid command"));
2167 cli_user_command_p (struct cmd_list_element
*cmd
)
2169 return (cmd
->theclass
== class_user
2170 && (cmd
->func
== do_const_cfunc
|| cmd
->func
== do_sfunc
));