1 /* Handle lists of commands, their decoding and documentation, for GDB.
3 Copyright (C) 1986-2021 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_context (struct cmd_list_element
*cmd
, void *context
)
140 cmd
->context
= context
;
144 get_cmd_context (struct cmd_list_element
*cmd
)
150 set_cmd_completer (struct cmd_list_element
*cmd
, completer_ftype
*completer
)
152 cmd
->completer
= completer
; /* Ok. */
155 /* See definition in commands.h. */
158 set_cmd_completer_handle_brkchars (struct cmd_list_element
*cmd
,
159 completer_handle_brkchars_ftype
*func
)
161 cmd
->completer_handle_brkchars
= func
;
165 cmd_list_element::prefixname () const
167 if (!this->is_prefix ())
168 /* Not a prefix command. */
171 std::string prefixname
;
172 if (this->prefix
!= nullptr)
173 prefixname
= this->prefix
->prefixname ();
175 prefixname
+= this->name
;
181 /* Add element named NAME.
182 Space for NAME and DOC must be allocated by the caller.
183 CLASS is the top level category into which commands are broken down
185 FUN should be the function to execute the command;
186 it will get a character string as argument, with leading
187 and trailing blanks already eliminated.
189 DOC is a documentation string for the command.
190 Its first line should be a complete sentence.
191 It should start with ? for a command that is an abbreviation
192 or with * for a command that most users don't need to know about.
194 Add this command to command list *LIST.
196 Returns a pointer to the added command (not necessarily the head
199 static struct cmd_list_element
*
200 do_add_cmd (const char *name
, enum command_class theclass
,
201 const char *doc
, struct cmd_list_element
**list
)
203 struct cmd_list_element
*c
= new struct cmd_list_element (name
, theclass
,
205 struct cmd_list_element
*p
, *iter
;
207 /* Turn each alias of the old command into an alias of the new
209 c
->aliases
= delete_cmd (name
, list
, &c
->hook_pre
, &c
->hookee_pre
,
210 &c
->hook_post
, &c
->hookee_post
);
211 for (iter
= c
->aliases
; iter
; iter
= iter
->alias_chain
)
212 iter
->alias_target
= c
;
214 c
->hook_pre
->hookee_pre
= c
;
216 c
->hookee_pre
->hook_pre
= c
;
218 c
->hook_post
->hookee_post
= c
;
220 c
->hookee_post
->hook_post
= c
;
222 if (*list
== NULL
|| strcmp ((*list
)->name
, name
) >= 0)
230 while (p
->next
&& strcmp (p
->next
->name
, name
) <= 0)
238 /* Search the prefix cmd of C, and assigns it to C->prefix.
239 See also add_prefix_cmd and update_prefix_field_of_prefixed_commands. */
240 cmd_list_element
*prefixcmd
= lookup_cmd_with_subcommands (list
, cmdlist
);
241 c
->prefix
= prefixcmd
;
247 struct cmd_list_element
*
248 add_cmd (const char *name
, enum command_class theclass
,
249 const char *doc
, struct cmd_list_element
**list
)
251 cmd_list_element
*result
= do_add_cmd (name
, theclass
, doc
, list
);
253 result
->function
.const_cfunc
= NULL
;
257 struct cmd_list_element
*
258 add_cmd (const char *name
, enum command_class theclass
,
259 cmd_const_cfunc_ftype
*fun
,
260 const char *doc
, struct cmd_list_element
**list
)
262 cmd_list_element
*result
= do_add_cmd (name
, theclass
, doc
, list
);
263 set_cmd_cfunc (result
, fun
);
267 /* Add an element with a suppress notification to the LIST of commands. */
269 struct cmd_list_element
*
270 add_cmd_suppress_notification (const char *name
, enum command_class theclass
,
271 cmd_const_cfunc_ftype
*fun
, const char *doc
,
272 struct cmd_list_element
**list
,
273 int *suppress_notification
)
275 struct cmd_list_element
*element
;
277 element
= add_cmd (name
, theclass
, fun
, doc
, list
);
278 element
->suppress_notification
= suppress_notification
;
284 /* Deprecates a command CMD.
285 REPLACEMENT is the name of the command which should be used in
286 place of this command, or NULL if no such command exists.
288 This function does not check to see if command REPLACEMENT exists
289 since gdb may not have gotten around to adding REPLACEMENT when
290 this function is called.
292 Returns a pointer to the deprecated command. */
294 struct cmd_list_element
*
295 deprecate_cmd (struct cmd_list_element
*cmd
, const char *replacement
)
297 cmd
->cmd_deprecated
= 1;
298 cmd
->deprecated_warn_user
= 1;
300 if (replacement
!= NULL
)
301 cmd
->replacement
= replacement
;
303 cmd
->replacement
= NULL
;
308 struct cmd_list_element
*
309 add_alias_cmd (const char *name
, cmd_list_element
*target
,
310 enum command_class theclass
, int abbrev_flag
,
311 struct cmd_list_element
**list
)
313 gdb_assert (target
!= nullptr);
315 struct cmd_list_element
*c
= add_cmd (name
, theclass
, target
->doc
, list
);
317 /* If TARGET->DOC can be freed, we should make another copy. */
318 if (target
->doc_allocated
)
320 c
->doc
= xstrdup (target
->doc
);
321 c
->doc_allocated
= 1;
323 /* NOTE: Both FUNC and all the FUNCTIONs need to be copied. */
324 c
->func
= target
->func
;
325 c
->function
= target
->function
;
326 c
->subcommands
= target
->subcommands
;
327 c
->allow_unknown
= target
->allow_unknown
;
328 c
->abbrev_flag
= abbrev_flag
;
329 c
->alias_target
= target
;
330 c
->alias_chain
= target
->aliases
;
336 struct cmd_list_element
*
337 add_alias_cmd (const char *name
, const char *target_name
,
338 enum command_class theclass
, int abbrev_flag
,
339 struct cmd_list_element
**list
)
341 const char *tmp
= target_name
;
342 cmd_list_element
*target
= lookup_cmd (&tmp
, *list
, "", NULL
, 1, 1);
344 return add_alias_cmd (name
, target
, theclass
, abbrev_flag
, list
);
348 /* Update the prefix field of all sub-commands of the prefix command C.
349 We must do this when a prefix command is defined as the GDB init sequence
350 does not guarantee that a prefix command is created before its sub-commands.
351 For example, break-catch-sig.c initialization runs before breakpoint.c
352 initialization, but it is breakpoint.c that creates the "catch" command used
353 by the "catch signal" command created by break-catch-sig.c. */
356 update_prefix_field_of_prefixed_commands (struct cmd_list_element
*c
)
358 for (cmd_list_element
*p
= *c
->subcommands
; p
!= NULL
; p
= p
->next
)
362 /* We must recursively update the prefix field to cover
363 e.g. 'info auto-load libthread-db' where the creation
368 In such a case, when 'auto-load' was created by do_add_cmd,
369 the 'libthread-db' prefix field could not be updated, as the
370 'auto-load' command was not yet reachable by
371 lookup_cmd_for_subcommands (list, cmdlist)
372 that searches from the top level 'cmdlist'. */
374 update_prefix_field_of_prefixed_commands (p
);
379 /* Like add_cmd but adds an element for a command prefix: a name that
380 should be followed by a subcommand to be looked up in another
381 command list. SUBCOMMANDS should be the address of the variable
382 containing that list. */
384 struct cmd_list_element
*
385 add_prefix_cmd (const char *name
, enum command_class theclass
,
386 cmd_const_cfunc_ftype
*fun
,
387 const char *doc
, struct cmd_list_element
**subcommands
,
388 int allow_unknown
, struct cmd_list_element
**list
)
390 struct cmd_list_element
*c
= add_cmd (name
, theclass
, fun
, doc
, list
);
392 c
->subcommands
= subcommands
;
393 c
->allow_unknown
= allow_unknown
;
395 /* Now that prefix command C is defined, we need to set the prefix field
396 of all prefixed commands that were defined before C itself was defined. */
397 update_prefix_field_of_prefixed_commands (c
);
402 /* A helper function for add_basic_prefix_cmd. This is a command
403 function that just forwards to help_list. */
406 do_prefix_cmd (const char *args
, int from_tty
, struct cmd_list_element
*c
)
408 /* Look past all aliases. */
409 while (c
->is_alias ())
412 help_list (*c
->subcommands
, c
->prefixname ().c_str (),
413 all_commands
, gdb_stdout
);
418 struct cmd_list_element
*
419 add_basic_prefix_cmd (const char *name
, enum command_class theclass
,
420 const char *doc
, struct cmd_list_element
**subcommands
,
421 int allow_unknown
, struct cmd_list_element
**list
)
423 struct cmd_list_element
*cmd
= add_prefix_cmd (name
, theclass
, nullptr,
425 allow_unknown
, list
);
426 set_cmd_sfunc (cmd
, do_prefix_cmd
);
430 /* A helper function for add_show_prefix_cmd. This is a command
431 function that just forwards to cmd_show_list. */
434 do_show_prefix_cmd (const char *args
, int from_tty
, struct cmd_list_element
*c
)
436 cmd_show_list (*c
->subcommands
, from_tty
);
441 struct cmd_list_element
*
442 add_show_prefix_cmd (const char *name
, enum command_class theclass
,
443 const char *doc
, struct cmd_list_element
**subcommands
,
444 int allow_unknown
, struct cmd_list_element
**list
)
446 struct cmd_list_element
*cmd
= add_prefix_cmd (name
, theclass
, nullptr,
448 allow_unknown
, list
);
449 set_cmd_sfunc (cmd
, do_show_prefix_cmd
);
453 /* Like ADD_PREFIX_CMD but sets the suppress_notification pointer on the
454 new command list element. */
456 struct cmd_list_element
*
457 add_prefix_cmd_suppress_notification
458 (const char *name
, enum command_class theclass
,
459 cmd_const_cfunc_ftype
*fun
,
460 const char *doc
, struct cmd_list_element
**subcommands
,
461 int allow_unknown
, struct cmd_list_element
**list
,
462 int *suppress_notification
)
464 struct cmd_list_element
*element
465 = add_prefix_cmd (name
, theclass
, fun
, doc
, subcommands
,
466 allow_unknown
, list
);
467 element
->suppress_notification
= suppress_notification
;
471 /* Like add_prefix_cmd but sets the abbrev_flag on the new command. */
473 struct cmd_list_element
*
474 add_abbrev_prefix_cmd (const char *name
, enum command_class theclass
,
475 cmd_const_cfunc_ftype
*fun
, const char *doc
,
476 struct cmd_list_element
**subcommands
,
477 int allow_unknown
, struct cmd_list_element
**list
)
479 struct cmd_list_element
*c
= add_cmd (name
, theclass
, fun
, doc
, list
);
481 c
->subcommands
= subcommands
;
482 c
->allow_unknown
= allow_unknown
;
487 /* This is an empty "cfunc". */
489 not_just_help_class_command (const char *args
, int from_tty
)
493 /* This is an empty "sfunc". */
496 empty_sfunc (const char *args
, int from_tty
, struct cmd_list_element
*c
)
500 /* Add element named NAME to command list LIST (the list for set/show
501 or some sublist thereof).
502 TYPE is set_cmd or show_cmd.
503 CLASS is as in add_cmd.
504 VAR_TYPE is the kind of thing we are setting.
505 VAR is address of the variable being controlled by this command.
506 DOC is the documentation string. */
508 static struct cmd_list_element
*
509 add_set_or_show_cmd (const char *name
,
511 enum command_class theclass
,
515 struct cmd_list_element
**list
)
517 struct cmd_list_element
*c
= add_cmd (name
, theclass
, doc
, list
);
519 gdb_assert (type
== set_cmd
|| type
== show_cmd
);
521 c
->var_type
= var_type
;
523 /* This needs to be something besides NULL so that this isn't
524 treated as a help class. */
525 set_cmd_sfunc (c
, empty_sfunc
);
529 /* Add element named NAME to both the command SET_LIST and SHOW_LIST.
530 CLASS is as in add_cmd. VAR_TYPE is the kind of thing we are
531 setting. VAR is address of the variable being controlled by this
532 command. SET_FUNC and SHOW_FUNC are the callback functions (if
533 non-NULL). SET_DOC, SHOW_DOC and HELP_DOC are the documentation
534 strings. PRINT the format string to print the value. SET_RESULT
535 and SHOW_RESULT, if not NULL, are set to the resulting command
539 add_setshow_cmd_full (const char *name
,
540 enum command_class theclass
,
541 var_types var_type
, void *var
,
542 const char *set_doc
, const char *show_doc
,
543 const char *help_doc
,
544 cmd_const_sfunc_ftype
*set_func
,
545 show_value_ftype
*show_func
,
546 struct cmd_list_element
**set_list
,
547 struct cmd_list_element
**show_list
,
548 struct cmd_list_element
**set_result
,
549 struct cmd_list_element
**show_result
)
551 struct cmd_list_element
*set
;
552 struct cmd_list_element
*show
;
556 if (help_doc
!= NULL
)
558 full_set_doc
= xstrprintf ("%s\n%s", set_doc
, help_doc
);
559 full_show_doc
= xstrprintf ("%s\n%s", show_doc
, help_doc
);
563 full_set_doc
= xstrdup (set_doc
);
564 full_show_doc
= xstrdup (show_doc
);
566 set
= add_set_or_show_cmd (name
, set_cmd
, theclass
, var_type
, var
,
567 full_set_doc
, set_list
);
568 set
->doc_allocated
= 1;
570 if (set_func
!= NULL
)
571 set_cmd_sfunc (set
, set_func
);
573 show
= add_set_or_show_cmd (name
, show_cmd
, theclass
, var_type
, var
,
574 full_show_doc
, show_list
);
575 show
->doc_allocated
= 1;
576 show
->show_value_func
= show_func
;
577 /* Disable the default symbol completer. Doesn't make much sense
578 for the "show" command to complete on anything. */
579 set_cmd_completer (show
, nullptr);
581 if (set_result
!= NULL
)
583 if (show_result
!= NULL
)
587 /* Add element named NAME to command list LIST (the list for set or
588 some sublist thereof). CLASS is as in add_cmd. ENUMLIST is a list
589 of strings which may follow NAME. VAR is address of the variable
590 which will contain the matching string (from ENUMLIST). */
593 add_setshow_enum_cmd (const char *name
,
594 enum command_class theclass
,
595 const char *const *enumlist
,
598 const char *show_doc
,
599 const char *help_doc
,
600 cmd_const_sfunc_ftype
*set_func
,
601 show_value_ftype
*show_func
,
602 struct cmd_list_element
**set_list
,
603 struct cmd_list_element
**show_list
,
606 struct cmd_list_element
*c
, *show
;
608 add_setshow_cmd_full (name
, theclass
, var_enum
, var
,
609 set_doc
, show_doc
, help_doc
,
615 set_cmd_context (c
, context
);
616 set_cmd_context (show
, context
);
619 /* See cli-decode.h. */
620 const char * const auto_boolean_enums
[] = { "on", "off", "auto", NULL
};
622 /* Add an auto-boolean command named NAME to both the set and show
623 command list lists. CLASS is as in add_cmd. VAR is address of the
624 variable which will contain the value. DOC is the documentation
625 string. FUNC is the corresponding callback. */
627 add_setshow_auto_boolean_cmd (const char *name
,
628 enum command_class theclass
,
629 enum auto_boolean
*var
,
630 const char *set_doc
, const char *show_doc
,
631 const char *help_doc
,
632 cmd_const_sfunc_ftype
*set_func
,
633 show_value_ftype
*show_func
,
634 struct cmd_list_element
**set_list
,
635 struct cmd_list_element
**show_list
)
637 struct cmd_list_element
*c
;
639 add_setshow_cmd_full (name
, theclass
, var_auto_boolean
, var
,
640 set_doc
, show_doc
, help_doc
,
644 c
->enums
= auto_boolean_enums
;
647 /* See cli-decode.h. */
648 const char * const boolean_enums
[] = { "on", "off", NULL
};
650 /* Add element named NAME to both the set and show command LISTs (the
651 list for set/show or some sublist thereof). CLASS is as in
652 add_cmd. VAR is address of the variable which will contain the
653 value. SET_DOC and SHOW_DOC are the documentation strings.
654 Returns the new command element. */
657 add_setshow_boolean_cmd (const char *name
, enum command_class theclass
, bool *var
,
658 const char *set_doc
, const char *show_doc
,
659 const char *help_doc
,
660 cmd_const_sfunc_ftype
*set_func
,
661 show_value_ftype
*show_func
,
662 struct cmd_list_element
**set_list
,
663 struct cmd_list_element
**show_list
)
665 struct cmd_list_element
*c
;
667 add_setshow_cmd_full (name
, theclass
, var_boolean
, var
,
668 set_doc
, show_doc
, help_doc
,
672 c
->enums
= boolean_enums
;
677 /* Add element named NAME to both the set and show command LISTs (the
678 list for set/show or some sublist thereof). */
680 add_setshow_filename_cmd (const char *name
, enum command_class theclass
,
682 const char *set_doc
, const char *show_doc
,
683 const char *help_doc
,
684 cmd_const_sfunc_ftype
*set_func
,
685 show_value_ftype
*show_func
,
686 struct cmd_list_element
**set_list
,
687 struct cmd_list_element
**show_list
)
689 struct cmd_list_element
*set_result
;
691 add_setshow_cmd_full (name
, theclass
, var_filename
, var
,
692 set_doc
, show_doc
, help_doc
,
696 set_cmd_completer (set_result
, filename_completer
);
699 /* Add element named NAME to both the set and show command LISTs (the
700 list for set/show or some sublist thereof). */
702 add_setshow_string_cmd (const char *name
, enum command_class theclass
,
704 const char *set_doc
, const char *show_doc
,
705 const char *help_doc
,
706 cmd_const_sfunc_ftype
*set_func
,
707 show_value_ftype
*show_func
,
708 struct cmd_list_element
**set_list
,
709 struct cmd_list_element
**show_list
)
711 cmd_list_element
*set_cmd
;
713 add_setshow_cmd_full (name
, theclass
, var_string
, var
,
714 set_doc
, show_doc
, help_doc
,
719 /* Disable the default symbol completer. */
720 set_cmd_completer (set_cmd
, nullptr);
723 /* Add element named NAME to both the set and show command LISTs (the
724 list for set/show or some sublist thereof). */
725 struct cmd_list_element
*
726 add_setshow_string_noescape_cmd (const char *name
, enum command_class theclass
,
728 const char *set_doc
, const char *show_doc
,
729 const char *help_doc
,
730 cmd_const_sfunc_ftype
*set_func
,
731 show_value_ftype
*show_func
,
732 struct cmd_list_element
**set_list
,
733 struct cmd_list_element
**show_list
)
735 struct cmd_list_element
*set_cmd
;
737 add_setshow_cmd_full (name
, theclass
, var_string_noescape
, var
,
738 set_doc
, show_doc
, help_doc
,
743 /* Disable the default symbol completer. */
744 set_cmd_completer (set_cmd
, nullptr);
749 /* Add element named NAME to both the set and show command LISTs (the
750 list for set/show or some sublist thereof). */
752 add_setshow_optional_filename_cmd (const char *name
, enum command_class theclass
,
754 const char *set_doc
, const char *show_doc
,
755 const char *help_doc
,
756 cmd_const_sfunc_ftype
*set_func
,
757 show_value_ftype
*show_func
,
758 struct cmd_list_element
**set_list
,
759 struct cmd_list_element
**show_list
)
761 struct cmd_list_element
*set_result
;
763 add_setshow_cmd_full (name
, theclass
, var_optional_filename
, var
,
764 set_doc
, show_doc
, help_doc
,
769 set_cmd_completer (set_result
, filename_completer
);
773 /* Completes on literal "unlimited". Used by integer commands that
774 support a special "unlimited" value. */
777 integer_unlimited_completer (struct cmd_list_element
*ignore
,
778 completion_tracker
&tracker
,
779 const char *text
, const char *word
)
781 static const char * const keywords
[] =
787 complete_on_enum (tracker
, keywords
, text
, word
);
790 /* Add element named NAME to both the set and show command LISTs (the
791 list for set/show or some sublist thereof). CLASS is as in
792 add_cmd. VAR is address of the variable which will contain the
793 value. SET_DOC and SHOW_DOC are the documentation strings. This
794 function is only used in Python API. Please don't use it elsewhere. */
796 add_setshow_integer_cmd (const char *name
, enum command_class theclass
,
798 const char *set_doc
, const char *show_doc
,
799 const char *help_doc
,
800 cmd_const_sfunc_ftype
*set_func
,
801 show_value_ftype
*show_func
,
802 struct cmd_list_element
**set_list
,
803 struct cmd_list_element
**show_list
)
805 struct cmd_list_element
*set
;
807 add_setshow_cmd_full (name
, theclass
, var_integer
, var
,
808 set_doc
, show_doc
, help_doc
,
813 set_cmd_completer (set
, integer_unlimited_completer
);
816 /* Add element named NAME to both the set and show command LISTs (the
817 list for set/show or some sublist thereof). CLASS is as in
818 add_cmd. VAR is address of the variable which will contain the
819 value. SET_DOC and SHOW_DOC are the documentation strings. */
821 add_setshow_uinteger_cmd (const char *name
, enum command_class theclass
,
823 const char *set_doc
, const char *show_doc
,
824 const char *help_doc
,
825 cmd_const_sfunc_ftype
*set_func
,
826 show_value_ftype
*show_func
,
827 struct cmd_list_element
**set_list
,
828 struct cmd_list_element
**show_list
)
830 struct cmd_list_element
*set
;
832 add_setshow_cmd_full (name
, theclass
, var_uinteger
, var
,
833 set_doc
, show_doc
, help_doc
,
838 set_cmd_completer (set
, integer_unlimited_completer
);
841 /* Add element named NAME to both the set and show command LISTs (the
842 list for set/show or some sublist thereof). CLASS is as in
843 add_cmd. VAR is address of the variable which will contain the
844 value. SET_DOC and SHOW_DOC are the documentation strings. */
846 add_setshow_zinteger_cmd (const char *name
, enum command_class theclass
,
848 const char *set_doc
, const char *show_doc
,
849 const char *help_doc
,
850 cmd_const_sfunc_ftype
*set_func
,
851 show_value_ftype
*show_func
,
852 struct cmd_list_element
**set_list
,
853 struct cmd_list_element
**show_list
)
855 add_setshow_cmd_full (name
, theclass
, var_zinteger
, var
,
856 set_doc
, show_doc
, help_doc
,
863 add_setshow_zuinteger_unlimited_cmd (const char *name
,
864 enum command_class theclass
,
867 const char *show_doc
,
868 const char *help_doc
,
869 cmd_const_sfunc_ftype
*set_func
,
870 show_value_ftype
*show_func
,
871 struct cmd_list_element
**set_list
,
872 struct cmd_list_element
**show_list
)
874 struct cmd_list_element
*set
;
876 add_setshow_cmd_full (name
, theclass
, var_zuinteger_unlimited
, var
,
877 set_doc
, show_doc
, help_doc
,
882 set_cmd_completer (set
, integer_unlimited_completer
);
885 /* Add element named NAME to both the set and show command LISTs (the
886 list for set/show or some sublist thereof). CLASS is as in
887 add_cmd. VAR is address of the variable which will contain the
888 value. SET_DOC and SHOW_DOC are the documentation strings. */
890 add_setshow_zuinteger_cmd (const char *name
, enum command_class theclass
,
892 const char *set_doc
, const char *show_doc
,
893 const char *help_doc
,
894 cmd_const_sfunc_ftype
*set_func
,
895 show_value_ftype
*show_func
,
896 struct cmd_list_element
**set_list
,
897 struct cmd_list_element
**show_list
)
899 add_setshow_cmd_full (name
, theclass
, var_zuinteger
, var
,
900 set_doc
, show_doc
, help_doc
,
906 /* Remove the command named NAME from the command list. Return the
907 list commands which were aliased to the deleted command. If the
908 command had no aliases, return NULL. The various *HOOKs are set to
909 the pre- and post-hook commands for the deleted command. If the
910 command does not have a hook, the corresponding out parameter is
913 static struct cmd_list_element
*
914 delete_cmd (const char *name
, struct cmd_list_element
**list
,
915 struct cmd_list_element
**prehook
,
916 struct cmd_list_element
**prehookee
,
917 struct cmd_list_element
**posthook
,
918 struct cmd_list_element
**posthookee
)
920 struct cmd_list_element
*iter
;
921 struct cmd_list_element
**previous_chain_ptr
;
922 struct cmd_list_element
*aliases
= NULL
;
928 previous_chain_ptr
= list
;
930 for (iter
= *previous_chain_ptr
; iter
; iter
= *previous_chain_ptr
)
932 if (strcmp (iter
->name
, name
) == 0)
935 iter
->destroyer (iter
, iter
->context
);
936 if (iter
->hookee_pre
)
937 iter
->hookee_pre
->hook_pre
= 0;
938 *prehook
= iter
->hook_pre
;
939 *prehookee
= iter
->hookee_pre
;
940 if (iter
->hookee_post
)
941 iter
->hookee_post
->hook_post
= 0;
942 *posthook
= iter
->hook_post
;
943 *posthookee
= iter
->hookee_post
;
945 /* Update the link. */
946 *previous_chain_ptr
= iter
->next
;
948 aliases
= iter
->aliases
;
950 /* If this command was an alias, remove it from the list of
952 if (iter
->is_alias ())
954 struct cmd_list_element
**prevp
= &iter
->alias_target
->aliases
;
955 struct cmd_list_element
*a
= *prevp
;
959 prevp
= &a
->alias_chain
;
962 *prevp
= iter
->alias_chain
;
967 /* We won't see another command with the same name. */
971 previous_chain_ptr
= &iter
->next
;
977 /* Shorthands to the commands above. */
979 /* Add an element to the list of info subcommands. */
981 struct cmd_list_element
*
982 add_info (const char *name
, cmd_const_cfunc_ftype
*fun
, const char *doc
)
984 return add_cmd (name
, class_info
, fun
, doc
, &infolist
);
987 /* Add an alias to the list of info subcommands. */
989 struct cmd_list_element
*
990 add_info_alias (const char *name
, const char *target_name
, int abbrev_flag
)
992 return add_alias_cmd (name
, target_name
, class_run
, abbrev_flag
, &infolist
);
995 /* Add an element to the list of commands. */
997 struct cmd_list_element
*
998 add_com (const char *name
, enum command_class theclass
,
999 cmd_const_cfunc_ftype
*fun
,
1002 return add_cmd (name
, theclass
, fun
, doc
, &cmdlist
);
1005 /* Add an alias or abbreviation command to the list of commands.
1006 For aliases predefined by GDB (such as bt), THECLASS must be
1007 different of class_alias, as class_alias is used to identify
1008 user defined aliases. */
1010 struct cmd_list_element
*
1011 add_com_alias (const char *name
, const char *target_name
,
1012 command_class theclass
, int abbrev_flag
)
1014 return add_alias_cmd (name
, target_name
, theclass
, abbrev_flag
, &cmdlist
);
1017 /* Add an element with a suppress notification to the list of commands. */
1019 struct cmd_list_element
*
1020 add_com_suppress_notification (const char *name
, enum command_class theclass
,
1021 cmd_const_cfunc_ftype
*fun
, const char *doc
,
1022 int *suppress_notification
)
1024 return add_cmd_suppress_notification (name
, theclass
, fun
, doc
,
1025 &cmdlist
, suppress_notification
);
1028 /* Print the prefix of C followed by name of C in title style. */
1031 fput_command_name_styled (struct cmd_list_element
*c
, struct ui_file
*stream
)
1033 std::string prefixname
1034 = c
->prefix
== nullptr ? "" : c
->prefix
->prefixname ();
1036 fprintf_styled (stream
, title_style
.style (), "%s%s",
1037 prefixname
.c_str (), c
->name
);
1040 /* Print the definition of alias C using title style for alias
1041 and aliased command. */
1044 fput_alias_definition_styled (struct cmd_list_element
*c
,
1045 struct ui_file
*stream
)
1047 gdb_assert (c
->is_alias ());
1048 fputs_filtered (" alias ", stream
);
1049 fput_command_name_styled (c
, stream
);
1050 fprintf_filtered (stream
, " = ");
1051 fput_command_name_styled (c
->alias_target
, stream
);
1052 fprintf_filtered (stream
, " %s\n", c
->default_args
.c_str ());
1055 /* Print the definition of the aliases of CMD that have default args. */
1058 fput_aliases_definition_styled (struct cmd_list_element
*cmd
,
1059 struct ui_file
*stream
)
1061 if (cmd
->aliases
!= nullptr)
1063 for (cmd_list_element
*iter
= cmd
->aliases
;
1065 iter
= iter
->alias_chain
)
1067 if (!iter
->default_args
.empty ())
1068 fput_alias_definition_styled (iter
, stream
);
1074 /* If C has one or more aliases, style print the name of C and
1075 the name of its aliases, separated by commas.
1076 If ALWAYS_FPUT_C_NAME, print the name of C even if it has no aliases.
1077 If one or more names are printed, POSTFIX is printed after the last name.
1081 fput_command_names_styled (struct cmd_list_element
*c
,
1082 bool always_fput_c_name
, const char *postfix
,
1083 struct ui_file
*stream
)
1085 if (always_fput_c_name
|| c
->aliases
!= nullptr)
1086 fput_command_name_styled (c
, stream
);
1087 if (c
->aliases
!= nullptr)
1089 for (cmd_list_element
*iter
= c
->aliases
; iter
; iter
= iter
->alias_chain
)
1091 fputs_filtered (", ", stream
);
1093 fput_command_name_styled (iter
, stream
);
1096 if (always_fput_c_name
|| c
->aliases
!= nullptr)
1097 fputs_filtered (postfix
, stream
);
1100 /* If VERBOSE, print the full help for command C and highlight the
1101 documentation parts matching HIGHLIGHT,
1102 otherwise print only one-line help for command C. */
1105 print_doc_of_command (struct cmd_list_element
*c
, const char *prefix
,
1106 bool verbose
, compiled_regex
&highlight
,
1107 struct ui_file
*stream
)
1109 /* When printing the full documentation, add a line to separate
1110 this documentation from the previous command help, in the likely
1111 case that apropos finds several commands. */
1113 fputs_filtered ("\n", stream
);
1115 fput_command_names_styled (c
, true,
1116 verbose
? "" : " -- ", stream
);
1119 fputs_filtered ("\n", stream
);
1120 fput_aliases_definition_styled (c
, stream
);
1121 fputs_highlighted (c
->doc
, highlight
, stream
);
1122 fputs_filtered ("\n", stream
);
1126 print_doc_line (stream
, c
->doc
, false);
1127 fputs_filtered ("\n", stream
);
1128 fput_aliases_definition_styled (c
, stream
);
1132 /* Recursively walk the commandlist structures, and print out the
1133 documentation of commands that match our regex in either their
1134 name, or their documentation.
1135 If VERBOSE, prints the complete documentation and highlight the
1136 documentation parts matching REGEX, otherwise prints only
1140 apropos_cmd (struct ui_file
*stream
,
1141 struct cmd_list_element
*commandlist
,
1142 bool verbose
, compiled_regex
®ex
, const char *prefix
)
1144 struct cmd_list_element
*c
;
1147 /* Walk through the commands. */
1148 for (c
=commandlist
;c
;c
=c
->next
)
1152 /* Command aliases/abbreviations are skipped to ensure we print the
1153 doc of a command only once, when encountering the aliased
1158 returnvalue
= -1; /* Needed to avoid double printing. */
1159 if (c
->name
!= NULL
)
1161 size_t name_len
= strlen (c
->name
);
1163 /* Try to match against the name. */
1164 returnvalue
= regex
.search (c
->name
, name_len
, 0, name_len
, NULL
);
1165 if (returnvalue
>= 0)
1166 print_doc_of_command (c
, prefix
, verbose
, regex
, stream
);
1168 /* Try to match against the name of the aliases. */
1169 for (cmd_list_element
*iter
= c
->aliases
;
1170 returnvalue
< 0 && iter
;
1171 iter
= iter
->alias_chain
)
1173 name_len
= strlen (iter
->name
);
1174 returnvalue
= regex
.search (iter
->name
, name_len
, 0, name_len
, NULL
);
1175 if (returnvalue
>= 0)
1176 print_doc_of_command (c
, prefix
, verbose
, regex
, stream
);
1179 if (c
->doc
!= NULL
&& returnvalue
< 0)
1181 size_t doc_len
= strlen (c
->doc
);
1183 /* Try to match against documentation. */
1184 if (regex
.search (c
->doc
, doc_len
, 0, doc_len
, NULL
) >= 0)
1185 print_doc_of_command (c
, prefix
, verbose
, regex
, stream
);
1187 /* Check if this command has subcommands. */
1188 if (c
->is_prefix ())
1190 /* Recursively call ourselves on the subcommand list,
1191 passing the right prefix in. */
1192 apropos_cmd (stream
, *c
->subcommands
, verbose
, regex
,
1193 c
->prefixname ().c_str ());
1198 /* This command really has to deal with two things:
1199 1) I want documentation on *this string* (usually called by
1200 "help commandname").
1202 2) I want documentation on *this list* (usually called by giving a
1203 command that requires subcommands. Also called by saying just
1206 I am going to split this into two separate commands, help_cmd and
1210 help_cmd (const char *command
, struct ui_file
*stream
)
1212 struct cmd_list_element
*c
, *alias
, *prefix_cmd
, *c_cmd
;
1216 help_list (cmdlist
, "", all_classes
, stream
);
1220 if (strcmp (command
, "all") == 0)
1226 const char *orig_command
= command
;
1227 c
= lookup_cmd (&command
, cmdlist
, "", NULL
, 0, 0);
1232 lookup_cmd_composition (orig_command
, &alias
, &prefix_cmd
, &c_cmd
);
1234 /* There are three cases here.
1235 If c->subcommands is nonzero, we have a prefix command.
1236 Print its documentation, then list its subcommands.
1238 If c->func is non NULL, we really have a command. Print its
1239 documentation and return.
1241 If c->func is NULL, we have a class name. Print its
1242 documentation (as if it were a command) and then set class to the
1243 number of this class so that the commands in the class will be
1246 /* If the user asked 'help somecommand' and there is no alias,
1247 the false indicates to not output the (single) command name. */
1248 fput_command_names_styled (c
, false, "\n", stream
);
1249 fput_aliases_definition_styled (c
, stream
);
1250 fputs_filtered (c
->doc
, stream
);
1251 fputs_filtered ("\n", stream
);
1253 if (!c
->is_prefix () && c
->func
!= NULL
)
1256 fprintf_filtered (stream
, "\n");
1258 /* If this is a prefix command, print it's subcommands. */
1259 if (c
->is_prefix ())
1260 help_list (*c
->subcommands
, c
->prefixname ().c_str (),
1261 all_commands
, stream
);
1263 /* If this is a class name, print all of the commands in the class. */
1264 if (c
->func
== NULL
)
1265 help_list (cmdlist
, "", c
->theclass
, stream
);
1267 if (c
->hook_pre
|| c
->hook_post
)
1268 fprintf_filtered (stream
,
1269 "\nThis command has a hook (or hooks) defined:\n");
1272 fprintf_filtered (stream
,
1273 "\tThis command is run after : %s (pre hook)\n",
1276 fprintf_filtered (stream
,
1277 "\tThis command is run before : %s (post hook)\n",
1278 c
->hook_post
->name
);
1282 * Get a specific kind of help on a command list.
1285 * CMDTYPE is the prefix to use in the title string.
1286 * CLASS is the class with which to list the nodes of this list (see
1287 * documentation for help_cmd_list below), As usual, ALL_COMMANDS for
1288 * everything, ALL_CLASSES for just classes, and non-negative for only things
1289 * in a specific class.
1290 * and STREAM is the output stream on which to print things.
1291 * If you call this routine with a class >= 0, it recurses.
1294 help_list (struct cmd_list_element
*list
, const char *cmdtype
,
1295 enum command_class theclass
, struct ui_file
*stream
)
1298 char *cmdtype1
, *cmdtype2
;
1300 /* If CMDTYPE is "foo ", CMDTYPE1 gets " foo" and CMDTYPE2 gets "foo sub".
1302 len
= strlen (cmdtype
);
1303 cmdtype1
= (char *) alloca (len
+ 1);
1305 cmdtype2
= (char *) alloca (len
+ 4);
1310 memcpy (cmdtype1
+ 1, cmdtype
, len
- 1);
1312 memcpy (cmdtype2
, cmdtype
, len
- 1);
1313 strcpy (cmdtype2
+ len
- 1, " sub");
1316 if (theclass
== all_classes
)
1317 fprintf_filtered (stream
, "List of classes of %scommands:\n\n", cmdtype2
);
1319 fprintf_filtered (stream
, "List of %scommands:\n\n", cmdtype2
);
1321 help_cmd_list (list
, theclass
, theclass
>= 0, stream
);
1323 if (theclass
== all_classes
)
1325 fprintf_filtered (stream
, "\n\
1326 Type \"help%s\" followed by a class name for a list of commands in ",
1329 fprintf_filtered (stream
, "that class.");
1331 fprintf_filtered (stream
, "\n\
1332 Type \"help all\" for the list of all commands.");
1335 fprintf_filtered (stream
, "\nType \"help%s\" followed by %scommand name ",
1336 cmdtype1
, cmdtype2
);
1338 fputs_filtered ("for ", stream
);
1340 fputs_filtered ("full ", stream
);
1342 fputs_filtered ("documentation.\n", stream
);
1343 fputs_filtered ("Type \"apropos word\" to search "
1344 "for commands related to \"word\".\n", stream
);
1345 fputs_filtered ("Type \"apropos -v word\" for full documentation", stream
);
1347 fputs_filtered (" of commands related to \"word\".\n", stream
);
1348 fputs_filtered ("Command name abbreviations are allowed if unambiguous.\n",
1353 help_all (struct ui_file
*stream
)
1355 struct cmd_list_element
*c
;
1356 int seen_unclassified
= 0;
1358 for (c
= cmdlist
; c
; c
= c
->next
)
1362 /* If this is a class name, print all of the commands in the
1365 if (c
->func
== NULL
)
1367 fprintf_filtered (stream
, "\nCommand class: %s\n\n", c
->name
);
1368 help_cmd_list (cmdlist
, c
->theclass
, true, stream
);
1372 /* While it's expected that all commands are in some class,
1373 as a safety measure, we'll print commands outside of any
1374 class at the end. */
1376 for (c
= cmdlist
; c
; c
= c
->next
)
1381 if (c
->theclass
== no_class
)
1383 if (!seen_unclassified
)
1385 fprintf_filtered (stream
, "\nUnclassified commands\n\n");
1386 seen_unclassified
= 1;
1388 print_help_for_command (c
, true, stream
);
1394 /* See cli-decode.h. */
1397 print_doc_line (struct ui_file
*stream
, const char *str
,
1398 bool for_value_prefix
)
1400 static char *line_buffer
= 0;
1401 static int line_size
;
1407 line_buffer
= (char *) xmalloc (line_size
);
1410 /* Searches for the first end of line or the end of STR. */
1412 while (*p
&& *p
!= '\n')
1414 if (p
- str
> line_size
- 1)
1416 line_size
= p
- str
+ 1;
1417 xfree (line_buffer
);
1418 line_buffer
= (char *) xmalloc (line_size
);
1420 strncpy (line_buffer
, str
, p
- str
);
1421 if (for_value_prefix
)
1423 if (islower (line_buffer
[0]))
1424 line_buffer
[0] = toupper (line_buffer
[0]);
1425 gdb_assert (p
> str
);
1426 if (line_buffer
[p
- str
- 1] == '.')
1427 line_buffer
[p
- str
- 1] = '\0';
1429 line_buffer
[p
- str
] = '\0';
1432 line_buffer
[p
- str
] = '\0';
1433 fputs_filtered (line_buffer
, stream
);
1436 /* Print one-line help for command C.
1437 If RECURSE is non-zero, also print one-line descriptions
1438 of all prefixed subcommands. */
1440 print_help_for_command (struct cmd_list_element
*c
,
1441 bool recurse
, struct ui_file
*stream
)
1443 fput_command_names_styled (c
, true, " -- ", stream
);
1444 print_doc_line (stream
, c
->doc
, false);
1445 fputs_filtered ("\n", stream
);
1446 if (!c
->default_args
.empty ())
1447 fput_alias_definition_styled (c
, stream
);
1448 fput_aliases_definition_styled (c
, stream
);
1452 && c
->abbrev_flag
== 0)
1453 /* Subcommands of a prefix command typically have 'all_commands'
1454 as class. If we pass CLASS to recursive invocation,
1455 most often we won't see anything. */
1456 help_cmd_list (*c
->subcommands
, all_commands
, true, stream
);
1460 * Implement a help command on command list LIST.
1461 * RECURSE should be non-zero if this should be done recursively on
1462 * all sublists of LIST.
1463 * STREAM is the stream upon which the output should be written.
1464 * THECLASS should be:
1465 * A non-negative class number to list only commands in that
1466 * ALL_COMMANDS to list all commands in list.
1467 * ALL_CLASSES to list all classes in list.
1469 * Note that aliases are only shown when THECLASS is class_alias.
1470 * In the other cases, the aliases will be shown together with their
1473 * Note that RECURSE will be active on *all* sublists, not just the
1474 * ones selected by the criteria above (ie. the selection mechanism
1475 * is at the low level, not the high-level).
1479 help_cmd_list (struct cmd_list_element
*list
, enum command_class theclass
,
1480 bool recurse
, struct ui_file
*stream
)
1482 struct cmd_list_element
*c
;
1484 for (c
= list
; c
; c
= c
->next
)
1486 if (c
->abbrev_flag
== 1 || c
->cmd_deprecated
)
1488 /* Do not show abbreviations or deprecated commands. */
1492 if (c
->is_alias () && theclass
!= class_alias
)
1494 /* Do not show an alias, unless specifically showing the
1495 list of aliases: for all other classes, an alias is
1496 shown (if needed) together with its aliased command. */
1500 if (theclass
== all_commands
1501 || (theclass
== all_classes
&& c
->func
== NULL
)
1502 || (theclass
== c
->theclass
&& c
->func
!= NULL
))
1505 - showing all commands
1506 - showing all classes and C is a help class
1507 - showing commands of THECLASS and C is not the help class */
1509 /* If we show the class_alias and C is an alias, do not recurse,
1510 as this would show the (possibly very long) not very useful
1511 list of sub-commands of the aliased command. */
1512 print_help_for_command
1514 recurse
&& (theclass
!= class_alias
|| !c
->is_alias ()),
1520 && (theclass
== class_user
|| theclass
== class_alias
)
1523 /* User-defined commands or aliases may be subcommands. */
1524 help_cmd_list (*c
->subcommands
, theclass
, recurse
, stream
);
1528 /* Do not show C or recurse on C, e.g. because C does not belong to
1529 THECLASS or because C is a help class. */
1534 /* Search the input clist for 'command'. Return the command if
1535 found (or NULL if not), and return the number of commands
1538 static struct cmd_list_element
*
1539 find_cmd (const char *command
, int len
, struct cmd_list_element
*clist
,
1540 int ignore_help_classes
, int *nfound
)
1542 struct cmd_list_element
*found
, *c
;
1546 for (c
= clist
; c
; c
= c
->next
)
1547 if (!strncmp (command
, c
->name
, len
)
1548 && (!ignore_help_classes
|| c
->func
))
1552 if (c
->name
[len
] == '\0')
1561 /* Return the length of command name in TEXT. */
1564 find_command_name_length (const char *text
)
1566 const char *p
= text
;
1568 /* Treating underscores as part of command words is important
1569 so that "set args_foo()" doesn't get interpreted as
1570 "set args _foo()". */
1571 /* Some characters are only used for TUI specific commands.
1572 However, they are always allowed for the sake of consistency.
1574 Note that this is larger than the character set allowed when
1575 creating user-defined commands. */
1577 /* Recognize the single character commands so that, e.g., "!ls"
1578 works as expected. */
1579 if (*p
== '!' || *p
== '|')
1582 while (valid_cmd_char_p (*p
)
1583 /* Characters used by TUI specific commands. */
1584 || *p
== '+' || *p
== '<' || *p
== '>' || *p
== '$')
1590 /* See command.h. */
1593 valid_cmd_char_p (int c
)
1595 /* Alas "42" is a legitimate user-defined command.
1596 In the interests of not breaking anything we preserve that. */
1598 return isalnum (c
) || c
== '-' || c
== '_' || c
== '.';
1601 /* See command.h. */
1604 valid_user_defined_cmd_name_p (const char *name
)
1611 for (p
= name
; *p
!= '\0'; ++p
)
1613 if (valid_cmd_char_p (*p
))
1622 /* See command.h. */
1624 struct cmd_list_element
*
1625 lookup_cmd_1 (const char **text
, struct cmd_list_element
*clist
,
1626 struct cmd_list_element
**result_list
, std::string
*default_args
,
1627 int ignore_help_classes
, bool lookup_for_completion_p
)
1631 struct cmd_list_element
*found
, *c
;
1632 bool found_alias
= false;
1633 const char *line
= *text
;
1635 while (**text
== ' ' || **text
== '\t')
1638 /* Identify the name of the command. */
1639 len
= find_command_name_length (*text
);
1641 /* If nothing but whitespace, return 0. */
1645 /* *text and p now bracket the first command word to lookup (and
1646 it's length is len). We copy this into a local temporary. */
1649 command
= (char *) alloca (len
+ 1);
1650 memcpy (command
, *text
, len
);
1651 command
[len
] = '\0';
1656 found
= find_cmd (command
, len
, clist
, ignore_help_classes
, &nfound
);
1658 /* If nothing matches, we have a simple failure. */
1664 if (result_list
!= nullptr)
1665 /* Will be modified in calling routine
1666 if we know what the prefix command is. */
1668 if (default_args
!= nullptr)
1669 *default_args
= std::string ();
1670 return CMD_LIST_AMBIGUOUS
; /* Ambiguous. */
1673 /* We've matched something on this list. Move text pointer forward. */
1677 if (found
->is_alias ())
1679 /* We drop the alias (abbreviation) in favor of the command it
1680 is pointing to. If the alias is deprecated, though, we need to
1681 warn the user about it before we drop it. Note that while we
1682 are warning about the alias, we may also warn about the command
1683 itself and we will adjust the appropriate DEPRECATED_WARN_USER
1686 if (found
->deprecated_warn_user
&& !lookup_for_completion_p
)
1687 deprecated_cmd_warning (line
, clist
);
1690 /* Return the default_args of the alias, not the default_args
1691 of the command it is pointing to. */
1692 if (default_args
!= nullptr)
1693 *default_args
= found
->default_args
;
1694 found
= found
->alias_target
;
1697 /* If we found a prefix command, keep looking. */
1699 if (found
->is_prefix ())
1701 c
= lookup_cmd_1 (text
, *found
->subcommands
, result_list
, default_args
,
1702 ignore_help_classes
, lookup_for_completion_p
);
1705 /* Didn't find anything; this is as far as we got. */
1706 if (result_list
!= nullptr)
1707 *result_list
= clist
;
1708 if (!found_alias
&& default_args
!= nullptr)
1709 *default_args
= found
->default_args
;
1712 else if (c
== CMD_LIST_AMBIGUOUS
)
1714 /* We've gotten this far properly, but the next step is
1715 ambiguous. We need to set the result list to the best
1716 we've found (if an inferior hasn't already set it). */
1717 if (result_list
!= nullptr)
1719 /* This used to say *result_list = *found->subcommands.
1720 If that was correct, need to modify the documentation
1721 at the top of this function to clarify what is
1722 supposed to be going on. */
1723 *result_list
= found
;
1724 /* For ambiguous commands, do not return any default_args args. */
1725 if (default_args
!= nullptr)
1726 *default_args
= std::string ();
1737 if (result_list
!= nullptr)
1738 *result_list
= clist
;
1739 if (!found_alias
&& default_args
!= nullptr)
1740 *default_args
= found
->default_args
;
1745 /* All this hair to move the space to the front of cmdtype */
1748 undef_cmd_error (const char *cmdtype
, const char *q
)
1750 error (_("Undefined %scommand: \"%s\". Try \"help%s%.*s\"."),
1753 *cmdtype
? " " : "",
1754 (int) strlen (cmdtype
) - 1,
1758 /* Look up the contents of *LINE as a command in the command list LIST.
1759 LIST is a chain of struct cmd_list_element's.
1760 If it is found, return the struct cmd_list_element for that command,
1761 update *LINE to point after the command name, at the first argument
1762 and update *DEFAULT_ARGS (if DEFAULT_ARGS is not null) to the default
1763 args to prepend to the user provided args when running the command.
1764 Note that if the found cmd_list_element is found via an alias,
1765 the default args of the alias are returned.
1767 If not found, call error if ALLOW_UNKNOWN is zero
1768 otherwise (or if error returns) return zero.
1769 Call error if specified command is ambiguous,
1770 unless ALLOW_UNKNOWN is negative.
1771 CMDTYPE precedes the word "command" in the error message.
1773 If IGNORE_HELP_CLASSES is nonzero, ignore any command list
1774 elements which are actually help classes rather than commands (i.e.
1775 the function field of the struct cmd_list_element is 0). */
1777 struct cmd_list_element
*
1778 lookup_cmd (const char **line
, struct cmd_list_element
*list
,
1779 const char *cmdtype
,
1780 std::string
*default_args
,
1781 int allow_unknown
, int ignore_help_classes
)
1783 struct cmd_list_element
*last_list
= 0;
1784 struct cmd_list_element
*c
;
1786 /* Note: Do not remove trailing whitespace here because this
1787 would be wrong for complete_command. Jim Kingdon */
1790 error (_("Lack of needed %scommand"), cmdtype
);
1792 c
= lookup_cmd_1 (line
, list
, &last_list
, default_args
, ignore_help_classes
);
1799 int len
= find_command_name_length (*line
);
1801 q
= (char *) alloca (len
+ 1);
1802 strncpy (q
, *line
, len
);
1804 undef_cmd_error (cmdtype
, q
);
1809 else if (c
== CMD_LIST_AMBIGUOUS
)
1811 /* Ambigous. Local values should be off subcommands or called
1813 int local_allow_unknown
= (last_list
? last_list
->allow_unknown
:
1815 std::string local_cmdtype
1816 = last_list
? last_list
->prefixname () : cmdtype
;
1817 struct cmd_list_element
*local_list
=
1818 (last_list
? *(last_list
->subcommands
) : list
);
1820 if (local_allow_unknown
< 0)
1823 return last_list
; /* Found something. */
1825 return 0; /* Found nothing. */
1829 /* Report as error. */
1834 ((*line
)[amb_len
] && (*line
)[amb_len
] != ' '
1835 && (*line
)[amb_len
] != '\t');
1840 for (c
= local_list
; c
; c
= c
->next
)
1841 if (!strncmp (*line
, c
->name
, amb_len
))
1843 if (strlen (ambbuf
) + strlen (c
->name
) + 6
1844 < (int) sizeof ambbuf
)
1846 if (strlen (ambbuf
))
1847 strcat (ambbuf
, ", ");
1848 strcat (ambbuf
, c
->name
);
1852 strcat (ambbuf
, "..");
1856 error (_("Ambiguous %scommand \"%s\": %s."),
1857 local_cmdtype
.c_str (), *line
, ambbuf
);
1862 if (c
->type
== set_cmd
&& **line
!= '\0' && !isspace (**line
))
1863 error (_("Argument must be preceded by space."));
1865 /* We've got something. It may still not be what the caller
1866 wants (if this command *needs* a subcommand). */
1867 while (**line
== ' ' || **line
== '\t')
1870 if (c
->is_prefix () && **line
&& !c
->allow_unknown
)
1871 undef_cmd_error (c
->prefixname ().c_str (), *line
);
1873 /* Seems to be what he wants. Return it. */
1879 /* See command.h. */
1881 struct cmd_list_element
*
1882 lookup_cmd_exact (const char *name
,
1883 struct cmd_list_element
*list
,
1884 bool ignore_help_classes
)
1886 const char *tem
= name
;
1887 struct cmd_list_element
*cmd
= lookup_cmd (&tem
, list
, "", NULL
, -1,
1888 ignore_help_classes
);
1889 if (cmd
!= nullptr && strcmp (name
, cmd
->name
) != 0)
1894 /* We are here presumably because an alias or command in TEXT is
1895 deprecated and a warning message should be generated. This
1896 function decodes TEXT and potentially generates a warning message
1899 Example for 'set endian big' which has a fictitious alias 'seb'.
1901 If alias wasn't used in TEXT, and the command is deprecated:
1902 "warning: 'set endian big' is deprecated."
1904 If alias was used, and only the alias is deprecated:
1905 "warning: 'seb' an alias for the command 'set endian big' is deprecated."
1907 If alias was used and command is deprecated (regardless of whether
1908 the alias itself is deprecated:
1910 "warning: 'set endian big' (seb) is deprecated."
1912 After the message has been sent, clear the appropriate flags in the
1913 command and/or the alias so the user is no longer bothered.
1917 deprecated_cmd_warning (const char *text
, struct cmd_list_element
*list
)
1919 struct cmd_list_element
*alias
= nullptr;
1920 struct cmd_list_element
*cmd
= nullptr;
1922 /* Return if text doesn't evaluate to a command. We place this lookup
1923 within its own scope so that the PREFIX_CMD local is not visible
1924 later in this function. The value returned in PREFIX_CMD is based on
1925 the prefix found in TEXT, and is our case this prefix can be missing
1926 in some situations (when LIST is not the global CMDLIST).
1928 It is better for our purposes to use the prefix commands directly from
1929 the ALIAS and CMD results. */
1931 struct cmd_list_element
*prefix_cmd
= nullptr;
1932 if (!lookup_cmd_composition_1 (text
, &alias
, &prefix_cmd
, &cmd
, list
))
1936 /* Return if nothing is deprecated. */
1937 if (!((alias
!= nullptr ? alias
->deprecated_warn_user
: 0)
1938 || cmd
->deprecated_warn_user
))
1941 /* Join command prefix (if any) and the command name. */
1942 std::string tmp_cmd_str
;
1943 if (cmd
->prefix
!= nullptr)
1944 tmp_cmd_str
+= cmd
->prefix
->prefixname ();
1945 tmp_cmd_str
+= std::string (cmd
->name
);
1947 /* Display the appropriate first line, this warns that the thing the user
1948 entered is deprecated. */
1949 if (alias
!= nullptr)
1951 /* Join the alias prefix (if any) and the alias name. */
1952 std::string tmp_alias_str
;
1953 if (alias
->prefix
!= nullptr)
1954 tmp_alias_str
+= alias
->prefix
->prefixname ();
1955 tmp_alias_str
+= std::string (alias
->name
);
1957 if (cmd
->cmd_deprecated
)
1958 printf_filtered (_("Warning: command '%ps' (%ps) is deprecated.\n"),
1959 styled_string (title_style
.style (),
1960 tmp_cmd_str
.c_str ()),
1961 styled_string (title_style
.style (),
1962 tmp_alias_str
.c_str ()));
1964 printf_filtered (_("Warning: '%ps', an alias for the command '%ps', "
1965 "is deprecated.\n"),
1966 styled_string (title_style
.style (),
1967 tmp_alias_str
.c_str ()),
1968 styled_string (title_style
.style (),
1969 tmp_cmd_str
.c_str ()));
1972 printf_filtered (_("Warning: command '%ps' is deprecated.\n"),
1973 styled_string (title_style
.style (),
1974 tmp_cmd_str
.c_str ()));
1976 /* Now display a second line indicating what the user should use instead.
1977 If it is only the alias that is deprecated, we want to indicate the
1978 new alias, otherwise we'll indicate the new command. */
1979 const char *replacement
;
1980 if (alias
!= nullptr && !cmd
->cmd_deprecated
)
1981 replacement
= alias
->replacement
;
1983 replacement
= cmd
->replacement
;
1984 if (replacement
!= nullptr)
1985 printf_filtered (_("Use '%ps'.\n\n"),
1986 styled_string (title_style
.style (),
1989 printf_filtered (_("No alternative known.\n\n"));
1991 /* We've warned you, now we'll keep quiet. */
1992 if (alias
!= nullptr)
1993 alias
->deprecated_warn_user
= 0;
1994 cmd
->deprecated_warn_user
= 0;
1997 /* Look up the contents of TEXT as a command in the command list CUR_LIST.
1998 Return 1 on success, 0 on failure.
2000 If TEXT refers to an alias, *ALIAS will point to that alias.
2002 If TEXT is a subcommand (i.e. one that is preceded by a prefix
2003 command) set *PREFIX_CMD.
2005 Set *CMD to point to the command TEXT indicates.
2007 If any of *ALIAS, *PREFIX_CMD, or *CMD cannot be determined or do not
2008 exist, they are NULL when we return.
2013 lookup_cmd_composition_1 (const char *text
,
2014 struct cmd_list_element
**alias
,
2015 struct cmd_list_element
**prefix_cmd
,
2016 struct cmd_list_element
**cmd
,
2017 struct cmd_list_element
*cur_list
)
2020 *prefix_cmd
= cur_list
->prefix
;
2023 text
= skip_spaces (text
);
2025 /* Go through as many command lists as we need to, to find the command
2029 /* Identify the name of the command. */
2030 int len
= find_command_name_length (text
);
2032 /* If nothing but whitespace, return. */
2036 /* TEXT is the start of the first command word to lookup (and
2037 it's length is LEN). We copy this into a local temporary. */
2038 std::string
command (text
, len
);
2042 *cmd
= find_cmd (command
.c_str (), len
, cur_list
, 1, &nfound
);
2044 /* We only handle the case where a single command was found. */
2045 if (*cmd
== CMD_LIST_AMBIGUOUS
|| *cmd
== nullptr)
2049 if ((*cmd
)->is_alias ())
2051 /* If the command was actually an alias, we note that an
2052 alias was used (by assigning *ALIAS) and we set *CMD. */
2054 *cmd
= (*cmd
)->alias_target
;
2059 text
= skip_spaces (text
);
2061 if ((*cmd
)->is_prefix () && *text
!= '\0')
2063 cur_list
= *(*cmd
)->subcommands
;
2071 /* Look up the contents of TEXT as a command in the command list 'cmdlist'.
2072 Return 1 on success, 0 on failure.
2074 If TEXT refers to an alias, *ALIAS will point to that alias.
2076 If TEXT is a subcommand (i.e. one that is preceded by a prefix
2077 command) set *PREFIX_CMD.
2079 Set *CMD to point to the command TEXT indicates.
2081 If any of *ALIAS, *PREFIX_CMD, or *CMD cannot be determined or do not
2082 exist, they are NULL when we return.
2087 lookup_cmd_composition (const char *text
,
2088 struct cmd_list_element
**alias
,
2089 struct cmd_list_element
**prefix_cmd
,
2090 struct cmd_list_element
**cmd
)
2092 return lookup_cmd_composition_1 (text
, alias
, prefix_cmd
, cmd
, cmdlist
);
2095 /* Helper function for SYMBOL_COMPLETION_FUNCTION. */
2097 /* Return a vector of char pointers which point to the different
2098 possible completions in LIST of TEXT.
2100 WORD points in the same buffer as TEXT, and completions should be
2101 returned relative to this position. For example, suppose TEXT is
2102 "foo" and we want to complete to "foobar". If WORD is "oo", return
2103 "oobar"; if WORD is "baz/foo", return "baz/foobar". */
2106 complete_on_cmdlist (struct cmd_list_element
*list
,
2107 completion_tracker
&tracker
,
2108 const char *text
, const char *word
,
2109 int ignore_help_classes
)
2111 struct cmd_list_element
*ptr
;
2112 int textlen
= strlen (text
);
2114 int saw_deprecated_match
= 0;
2116 /* We do one or two passes. In the first pass, we skip deprecated
2117 commands. If we see no matching commands in the first pass, and
2118 if we did happen to see a matching deprecated command, we do
2119 another loop to collect those. */
2120 for (pass
= 0; pass
< 2; ++pass
)
2122 bool got_matches
= false;
2124 for (ptr
= list
; ptr
; ptr
= ptr
->next
)
2125 if (!strncmp (ptr
->name
, text
, textlen
)
2126 && !ptr
->abbrev_flag
2127 && (!ignore_help_classes
|| ptr
->func
2128 || ptr
->is_prefix ()))
2132 if (ptr
->cmd_deprecated
)
2134 saw_deprecated_match
= 1;
2139 tracker
.add_completion
2140 (make_completion_match_str (ptr
->name
, text
, word
));
2147 /* If we saw no matching deprecated commands in the first pass,
2149 if (!saw_deprecated_match
)
2154 /* Helper function for SYMBOL_COMPLETION_FUNCTION. */
2156 /* Add the different possible completions in ENUMLIST of TEXT.
2158 WORD points in the same buffer as TEXT, and completions should be
2159 returned relative to this position. For example, suppose TEXT is "foo"
2160 and we want to complete to "foobar". If WORD is "oo", return
2161 "oobar"; if WORD is "baz/foo", return "baz/foobar". */
2164 complete_on_enum (completion_tracker
&tracker
,
2165 const char *const *enumlist
,
2166 const char *text
, const char *word
)
2168 int textlen
= strlen (text
);
2172 for (i
= 0; (name
= enumlist
[i
]) != NULL
; i
++)
2173 if (strncmp (name
, text
, textlen
) == 0)
2174 tracker
.add_completion (make_completion_match_str (name
, text
, word
));
2178 /* Check function pointer. */
2180 cmd_func_p (struct cmd_list_element
*cmd
)
2182 return (cmd
->func
!= NULL
);
2186 /* Call the command function. */
2188 cmd_func (struct cmd_list_element
*cmd
, const char *args
, int from_tty
)
2190 if (cmd_func_p (cmd
))
2192 gdb::optional
<scoped_restore_tmpl
<int>> restore_suppress
;
2194 if (cmd
->suppress_notification
!= NULL
)
2195 restore_suppress
.emplace (cmd
->suppress_notification
, 1);
2197 (*cmd
->func
) (cmd
, args
, from_tty
);
2200 error (_("Invalid command"));
2204 cli_user_command_p (struct cmd_list_element
*cmd
)
2206 return (cmd
->theclass
== class_user
2207 && (cmd
->func
== do_const_cfunc
|| cmd
->func
== do_sfunc
));