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
;
72 if (p
->subcommands
== NULL
)
74 else if (p
->subcommands
== subcommands
)
76 /* If we found an alias, we must return the aliased
78 return p
->is_alias () ? p
->alias_target
: p
;
81 q
= lookup_cmd_with_subcommands (subcommands
, *(p
->subcommands
));
90 print_help_for_command (struct cmd_list_element
*c
,
91 bool recurse
, struct ui_file
*stream
);
94 /* Set the callback function for the specified command. For each both
95 the commands callback and func() are set. The latter set to a
96 bounce function (unless cfunc / sfunc is NULL that is). */
99 do_const_cfunc (struct cmd_list_element
*c
, const char *args
, int from_tty
)
101 c
->function
.const_cfunc (args
, from_tty
);
105 set_cmd_cfunc (struct cmd_list_element
*cmd
, cmd_const_cfunc_ftype
*cfunc
)
110 cmd
->func
= do_const_cfunc
;
111 cmd
->function
.const_cfunc
= cfunc
;
115 do_sfunc (struct cmd_list_element
*c
, const char *args
, int from_tty
)
117 c
->function
.sfunc (args
, from_tty
, c
);
121 set_cmd_sfunc (struct cmd_list_element
*cmd
, cmd_const_sfunc_ftype
*sfunc
)
126 cmd
->func
= do_sfunc
;
127 cmd
->function
.sfunc
= sfunc
;
131 cmd_cfunc_eq (struct cmd_list_element
*cmd
, cmd_const_cfunc_ftype
*cfunc
)
133 return cmd
->func
== do_const_cfunc
&& cmd
->function
.const_cfunc
== cfunc
;
137 set_cmd_context (struct cmd_list_element
*cmd
, void *context
)
139 cmd
->context
= context
;
143 get_cmd_context (struct cmd_list_element
*cmd
)
149 set_cmd_completer (struct cmd_list_element
*cmd
, completer_ftype
*completer
)
151 cmd
->completer
= completer
; /* Ok. */
154 /* See definition in commands.h. */
157 set_cmd_completer_handle_brkchars (struct cmd_list_element
*cmd
,
158 completer_handle_brkchars_ftype
*func
)
160 cmd
->completer_handle_brkchars
= func
;
164 cmd_list_element::prefixname () const
166 if (this->subcommands
== nullptr)
167 /* Not a prefix command. */
170 std::string prefixname
;
171 if (this->prefix
!= nullptr)
172 prefixname
= this->prefix
->prefixname ();
174 prefixname
+= this->name
;
180 /* Add element named NAME.
181 Space for NAME and DOC must be allocated by the caller.
182 CLASS is the top level category into which commands are broken down
184 FUN should be the function to execute the command;
185 it will get a character string as argument, with leading
186 and trailing blanks already eliminated.
188 DOC is a documentation string for the command.
189 Its first line should be a complete sentence.
190 It should start with ? for a command that is an abbreviation
191 or with * for a command that most users don't need to know about.
193 Add this command to command list *LIST.
195 Returns a pointer to the added command (not necessarily the head
198 static struct cmd_list_element
*
199 do_add_cmd (const char *name
, enum command_class theclass
,
200 const char *doc
, struct cmd_list_element
**list
)
202 struct cmd_list_element
*c
= new struct cmd_list_element (name
, theclass
,
204 struct cmd_list_element
*p
, *iter
;
206 /* Turn each alias of the old command into an alias of the new
208 c
->aliases
= delete_cmd (name
, list
, &c
->hook_pre
, &c
->hookee_pre
,
209 &c
->hook_post
, &c
->hookee_post
);
210 for (iter
= c
->aliases
; iter
; iter
= iter
->alias_chain
)
211 iter
->alias_target
= c
;
213 c
->hook_pre
->hookee_pre
= c
;
215 c
->hookee_pre
->hook_pre
= c
;
217 c
->hook_post
->hookee_post
= c
;
219 c
->hookee_post
->hook_post
= c
;
221 if (*list
== NULL
|| strcmp ((*list
)->name
, name
) >= 0)
229 while (p
->next
&& strcmp (p
->next
->name
, name
) <= 0)
237 /* Search the prefix cmd of C, and assigns it to C->prefix.
238 See also add_prefix_cmd and update_prefix_field_of_prefixed_commands. */
239 cmd_list_element
*prefixcmd
= lookup_cmd_with_subcommands (list
, cmdlist
);
240 c
->prefix
= prefixcmd
;
246 struct cmd_list_element
*
247 add_cmd (const char *name
, enum command_class theclass
,
248 const char *doc
, struct cmd_list_element
**list
)
250 cmd_list_element
*result
= do_add_cmd (name
, theclass
, doc
, list
);
252 result
->function
.const_cfunc
= NULL
;
256 struct cmd_list_element
*
257 add_cmd (const char *name
, enum command_class theclass
,
258 cmd_const_cfunc_ftype
*fun
,
259 const char *doc
, struct cmd_list_element
**list
)
261 cmd_list_element
*result
= do_add_cmd (name
, theclass
, doc
, list
);
262 set_cmd_cfunc (result
, fun
);
266 /* Add an element with a suppress notification to the LIST of commands. */
268 struct cmd_list_element
*
269 add_cmd_suppress_notification (const char *name
, enum command_class theclass
,
270 cmd_const_cfunc_ftype
*fun
, const char *doc
,
271 struct cmd_list_element
**list
,
272 int *suppress_notification
)
274 struct cmd_list_element
*element
;
276 element
= add_cmd (name
, theclass
, fun
, doc
, list
);
277 element
->suppress_notification
= suppress_notification
;
283 /* Deprecates a command CMD.
284 REPLACEMENT is the name of the command which should be used in
285 place of this command, or NULL if no such command exists.
287 This function does not check to see if command REPLACEMENT exists
288 since gdb may not have gotten around to adding REPLACEMENT when
289 this function is called.
291 Returns a pointer to the deprecated command. */
293 struct cmd_list_element
*
294 deprecate_cmd (struct cmd_list_element
*cmd
, const char *replacement
)
296 cmd
->cmd_deprecated
= 1;
297 cmd
->deprecated_warn_user
= 1;
299 if (replacement
!= NULL
)
300 cmd
->replacement
= replacement
;
302 cmd
->replacement
= NULL
;
307 struct cmd_list_element
*
308 add_alias_cmd (const char *name
, cmd_list_element
*target
,
309 enum command_class theclass
, int abbrev_flag
,
310 struct cmd_list_element
**list
)
312 gdb_assert (target
!= nullptr);
314 struct cmd_list_element
*c
= add_cmd (name
, theclass
, target
->doc
, list
);
316 /* If TARGET->DOC can be freed, we should make another copy. */
317 if (target
->doc_allocated
)
319 c
->doc
= xstrdup (target
->doc
);
320 c
->doc_allocated
= 1;
322 /* NOTE: Both FUNC and all the FUNCTIONs need to be copied. */
323 c
->func
= target
->func
;
324 c
->function
= target
->function
;
325 c
->subcommands
= target
->subcommands
;
326 c
->allow_unknown
= target
->allow_unknown
;
327 c
->abbrev_flag
= abbrev_flag
;
328 c
->alias_target
= target
;
329 c
->alias_chain
= target
->aliases
;
335 struct cmd_list_element
*
336 add_alias_cmd (const char *name
, const char *target_name
,
337 enum command_class theclass
, int abbrev_flag
,
338 struct cmd_list_element
**list
)
340 const char *tmp
= target_name
;
341 cmd_list_element
*target
= lookup_cmd (&tmp
, *list
, "", NULL
, 1, 1);
343 return add_alias_cmd (name
, target
, theclass
, abbrev_flag
, list
);
347 /* Update the prefix field of all sub-commands of the prefix command C.
348 We must do this when a prefix command is defined as the GDB init sequence
349 does not guarantee that a prefix command is created before its sub-commands.
350 For example, break-catch-sig.c initialization runs before breakpoint.c
351 initialization, but it is breakpoint.c that creates the "catch" command used
352 by the "catch signal" command created by break-catch-sig.c. */
355 update_prefix_field_of_prefixed_commands (struct cmd_list_element
*c
)
357 for (cmd_list_element
*p
= *c
->subcommands
; p
!= NULL
; p
= p
->next
)
361 /* We must recursively update the prefix field to cover
362 e.g. 'info auto-load libthread-db' where the creation
367 In such a case, when 'auto-load' was created by do_add_cmd,
368 the 'libthread-db' prefix field could not be updated, as the
369 'auto-load' command was not yet reachable by
370 lookup_cmd_for_subcommands (list, cmdlist)
371 that searches from the top level 'cmdlist'. */
372 if (p
->subcommands
!= nullptr)
373 update_prefix_field_of_prefixed_commands (p
);
378 /* Like add_cmd but adds an element for a command prefix: a name that
379 should be followed by a subcommand to be looked up in another
380 command list. SUBCOMMANDS should be the address of the variable
381 containing that list. */
383 struct cmd_list_element
*
384 add_prefix_cmd (const char *name
, enum command_class theclass
,
385 cmd_const_cfunc_ftype
*fun
,
386 const char *doc
, struct cmd_list_element
**subcommands
,
387 int allow_unknown
, struct cmd_list_element
**list
)
389 struct cmd_list_element
*c
= add_cmd (name
, theclass
, fun
, doc
, list
);
391 c
->subcommands
= subcommands
;
392 c
->allow_unknown
= allow_unknown
;
394 /* Now that prefix command C is defined, we need to set the prefix field
395 of all prefixed commands that were defined before C itself was defined. */
396 update_prefix_field_of_prefixed_commands (c
);
401 /* A helper function for add_basic_prefix_cmd. This is a command
402 function that just forwards to help_list. */
405 do_prefix_cmd (const char *args
, int from_tty
, struct cmd_list_element
*c
)
407 /* Look past all aliases. */
408 while (c
->is_alias ())
411 help_list (*c
->subcommands
, c
->prefixname ().c_str (),
412 all_commands
, gdb_stdout
);
417 struct cmd_list_element
*
418 add_basic_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_prefix_cmd
);
429 /* A helper function for add_show_prefix_cmd. This is a command
430 function that just forwards to cmd_show_list. */
433 do_show_prefix_cmd (const char *args
, int from_tty
, struct cmd_list_element
*c
)
435 cmd_show_list (*c
->subcommands
, from_tty
);
440 struct cmd_list_element
*
441 add_show_prefix_cmd (const char *name
, enum command_class theclass
,
442 const char *doc
, struct cmd_list_element
**subcommands
,
443 int allow_unknown
, struct cmd_list_element
**list
)
445 struct cmd_list_element
*cmd
= add_prefix_cmd (name
, theclass
, nullptr,
447 allow_unknown
, list
);
448 set_cmd_sfunc (cmd
, do_show_prefix_cmd
);
452 /* Like ADD_PREFIX_CMD but sets the suppress_notification pointer on the
453 new command list element. */
455 struct cmd_list_element
*
456 add_prefix_cmd_suppress_notification
457 (const char *name
, enum command_class theclass
,
458 cmd_const_cfunc_ftype
*fun
,
459 const char *doc
, struct cmd_list_element
**subcommands
,
460 int allow_unknown
, struct cmd_list_element
**list
,
461 int *suppress_notification
)
463 struct cmd_list_element
*element
464 = add_prefix_cmd (name
, theclass
, fun
, doc
, subcommands
,
465 allow_unknown
, list
);
466 element
->suppress_notification
= suppress_notification
;
470 /* Like add_prefix_cmd but sets the abbrev_flag on the new command. */
472 struct cmd_list_element
*
473 add_abbrev_prefix_cmd (const char *name
, enum command_class theclass
,
474 cmd_const_cfunc_ftype
*fun
, const char *doc
,
475 struct cmd_list_element
**subcommands
,
476 int allow_unknown
, struct cmd_list_element
**list
)
478 struct cmd_list_element
*c
= add_cmd (name
, theclass
, fun
, doc
, list
);
480 c
->subcommands
= subcommands
;
481 c
->allow_unknown
= allow_unknown
;
486 /* This is an empty "cfunc". */
488 not_just_help_class_command (const char *args
, int from_tty
)
492 /* This is an empty "sfunc". */
495 empty_sfunc (const char *args
, int from_tty
, struct cmd_list_element
*c
)
499 /* Add element named NAME to command list LIST (the list for set/show
500 or some sublist thereof).
501 TYPE is set_cmd or show_cmd.
502 CLASS is as in add_cmd.
503 VAR_TYPE is the kind of thing we are setting.
504 VAR is address of the variable being controlled by this command.
505 DOC is the documentation string. */
507 static struct cmd_list_element
*
508 add_set_or_show_cmd (const char *name
,
510 enum command_class theclass
,
514 struct cmd_list_element
**list
)
516 struct cmd_list_element
*c
= add_cmd (name
, theclass
, doc
, list
);
518 gdb_assert (type
== set_cmd
|| type
== show_cmd
);
520 c
->var_type
= var_type
;
522 /* This needs to be something besides NULL so that this isn't
523 treated as a help class. */
524 set_cmd_sfunc (c
, empty_sfunc
);
528 /* Add element named NAME to both the command SET_LIST and SHOW_LIST.
529 CLASS is as in add_cmd. VAR_TYPE is the kind of thing we are
530 setting. VAR is address of the variable being controlled by this
531 command. SET_FUNC and SHOW_FUNC are the callback functions (if
532 non-NULL). SET_DOC, SHOW_DOC and HELP_DOC are the documentation
533 strings. PRINT the format string to print the value. SET_RESULT
534 and SHOW_RESULT, if not NULL, are set to the resulting command
538 add_setshow_cmd_full (const char *name
,
539 enum command_class theclass
,
540 var_types var_type
, void *var
,
541 const char *set_doc
, const char *show_doc
,
542 const char *help_doc
,
543 cmd_const_sfunc_ftype
*set_func
,
544 show_value_ftype
*show_func
,
545 struct cmd_list_element
**set_list
,
546 struct cmd_list_element
**show_list
,
547 struct cmd_list_element
**set_result
,
548 struct cmd_list_element
**show_result
)
550 struct cmd_list_element
*set
;
551 struct cmd_list_element
*show
;
555 if (help_doc
!= NULL
)
557 full_set_doc
= xstrprintf ("%s\n%s", set_doc
, help_doc
);
558 full_show_doc
= xstrprintf ("%s\n%s", show_doc
, help_doc
);
562 full_set_doc
= xstrdup (set_doc
);
563 full_show_doc
= xstrdup (show_doc
);
565 set
= add_set_or_show_cmd (name
, set_cmd
, theclass
, var_type
, var
,
566 full_set_doc
, set_list
);
567 set
->doc_allocated
= 1;
569 if (set_func
!= NULL
)
570 set_cmd_sfunc (set
, set_func
);
572 show
= add_set_or_show_cmd (name
, show_cmd
, theclass
, var_type
, var
,
573 full_show_doc
, show_list
);
574 show
->doc_allocated
= 1;
575 show
->show_value_func
= show_func
;
576 /* Disable the default symbol completer. Doesn't make much sense
577 for the "show" command to complete on anything. */
578 set_cmd_completer (show
, nullptr);
580 if (set_result
!= NULL
)
582 if (show_result
!= NULL
)
586 /* Add element named NAME to command list LIST (the list for set or
587 some sublist thereof). CLASS is as in add_cmd. ENUMLIST is a list
588 of strings which may follow NAME. VAR is address of the variable
589 which will contain the matching string (from ENUMLIST). */
592 add_setshow_enum_cmd (const char *name
,
593 enum command_class theclass
,
594 const char *const *enumlist
,
597 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
,
605 struct cmd_list_element
*c
, *show
;
607 add_setshow_cmd_full (name
, theclass
, var_enum
, var
,
608 set_doc
, show_doc
, help_doc
,
614 set_cmd_context (c
, context
);
615 set_cmd_context (show
, context
);
618 /* See cli-decode.h. */
619 const char * const auto_boolean_enums
[] = { "on", "off", "auto", NULL
};
621 /* Add an auto-boolean command named NAME to both the set and show
622 command list lists. CLASS is as in add_cmd. VAR is address of the
623 variable which will contain the value. DOC is the documentation
624 string. FUNC is the corresponding callback. */
626 add_setshow_auto_boolean_cmd (const char *name
,
627 enum command_class theclass
,
628 enum auto_boolean
*var
,
629 const char *set_doc
, const char *show_doc
,
630 const char *help_doc
,
631 cmd_const_sfunc_ftype
*set_func
,
632 show_value_ftype
*show_func
,
633 struct cmd_list_element
**set_list
,
634 struct cmd_list_element
**show_list
)
636 struct cmd_list_element
*c
;
638 add_setshow_cmd_full (name
, theclass
, var_auto_boolean
, var
,
639 set_doc
, show_doc
, help_doc
,
643 c
->enums
= auto_boolean_enums
;
646 /* See cli-decode.h. */
647 const char * const boolean_enums
[] = { "on", "off", NULL
};
649 /* Add element named NAME to both the set and show command LISTs (the
650 list for set/show or some sublist thereof). CLASS is as in
651 add_cmd. VAR is address of the variable which will contain the
652 value. SET_DOC and SHOW_DOC are the documentation strings.
653 Returns the new command element. */
656 add_setshow_boolean_cmd (const char *name
, enum command_class theclass
, bool *var
,
657 const char *set_doc
, const char *show_doc
,
658 const char *help_doc
,
659 cmd_const_sfunc_ftype
*set_func
,
660 show_value_ftype
*show_func
,
661 struct cmd_list_element
**set_list
,
662 struct cmd_list_element
**show_list
)
664 struct cmd_list_element
*c
;
666 add_setshow_cmd_full (name
, theclass
, var_boolean
, var
,
667 set_doc
, show_doc
, help_doc
,
671 c
->enums
= boolean_enums
;
676 /* Add element named NAME to both the set and show command LISTs (the
677 list for set/show or some sublist thereof). */
679 add_setshow_filename_cmd (const char *name
, enum command_class theclass
,
681 const char *set_doc
, const char *show_doc
,
682 const char *help_doc
,
683 cmd_const_sfunc_ftype
*set_func
,
684 show_value_ftype
*show_func
,
685 struct cmd_list_element
**set_list
,
686 struct cmd_list_element
**show_list
)
688 struct cmd_list_element
*set_result
;
690 add_setshow_cmd_full (name
, theclass
, var_filename
, var
,
691 set_doc
, show_doc
, help_doc
,
695 set_cmd_completer (set_result
, filename_completer
);
698 /* Add element named NAME to both the set and show command LISTs (the
699 list for set/show or some sublist thereof). */
701 add_setshow_string_cmd (const char *name
, enum command_class theclass
,
703 const char *set_doc
, const char *show_doc
,
704 const char *help_doc
,
705 cmd_const_sfunc_ftype
*set_func
,
706 show_value_ftype
*show_func
,
707 struct cmd_list_element
**set_list
,
708 struct cmd_list_element
**show_list
)
710 cmd_list_element
*set_cmd
;
712 add_setshow_cmd_full (name
, theclass
, var_string
, var
,
713 set_doc
, show_doc
, help_doc
,
718 /* Disable the default symbol completer. */
719 set_cmd_completer (set_cmd
, nullptr);
722 /* Add element named NAME to both the set and show command LISTs (the
723 list for set/show or some sublist thereof). */
724 struct cmd_list_element
*
725 add_setshow_string_noescape_cmd (const char *name
, enum command_class theclass
,
727 const char *set_doc
, const char *show_doc
,
728 const char *help_doc
,
729 cmd_const_sfunc_ftype
*set_func
,
730 show_value_ftype
*show_func
,
731 struct cmd_list_element
**set_list
,
732 struct cmd_list_element
**show_list
)
734 struct cmd_list_element
*set_cmd
;
736 add_setshow_cmd_full (name
, theclass
, var_string_noescape
, var
,
737 set_doc
, show_doc
, help_doc
,
742 /* Disable the default symbol completer. */
743 set_cmd_completer (set_cmd
, nullptr);
748 /* Add element named NAME to both the set and show command LISTs (the
749 list for set/show or some sublist thereof). */
751 add_setshow_optional_filename_cmd (const char *name
, enum command_class theclass
,
753 const char *set_doc
, const char *show_doc
,
754 const char *help_doc
,
755 cmd_const_sfunc_ftype
*set_func
,
756 show_value_ftype
*show_func
,
757 struct cmd_list_element
**set_list
,
758 struct cmd_list_element
**show_list
)
760 struct cmd_list_element
*set_result
;
762 add_setshow_cmd_full (name
, theclass
, var_optional_filename
, var
,
763 set_doc
, show_doc
, help_doc
,
768 set_cmd_completer (set_result
, filename_completer
);
772 /* Completes on literal "unlimited". Used by integer commands that
773 support a special "unlimited" value. */
776 integer_unlimited_completer (struct cmd_list_element
*ignore
,
777 completion_tracker
&tracker
,
778 const char *text
, const char *word
)
780 static const char * const keywords
[] =
786 complete_on_enum (tracker
, keywords
, text
, word
);
789 /* Add element named NAME to both the set and show command LISTs (the
790 list for set/show or some sublist thereof). CLASS is as in
791 add_cmd. VAR is address of the variable which will contain the
792 value. SET_DOC and SHOW_DOC are the documentation strings. This
793 function is only used in Python API. Please don't use it elsewhere. */
795 add_setshow_integer_cmd (const char *name
, enum command_class theclass
,
797 const char *set_doc
, const char *show_doc
,
798 const char *help_doc
,
799 cmd_const_sfunc_ftype
*set_func
,
800 show_value_ftype
*show_func
,
801 struct cmd_list_element
**set_list
,
802 struct cmd_list_element
**show_list
)
804 struct cmd_list_element
*set
;
806 add_setshow_cmd_full (name
, theclass
, var_integer
, var
,
807 set_doc
, show_doc
, help_doc
,
812 set_cmd_completer (set
, integer_unlimited_completer
);
815 /* Add element named NAME to both the set and show command LISTs (the
816 list for set/show or some sublist thereof). CLASS is as in
817 add_cmd. VAR is address of the variable which will contain the
818 value. SET_DOC and SHOW_DOC are the documentation strings. */
820 add_setshow_uinteger_cmd (const char *name
, enum command_class theclass
,
822 const char *set_doc
, const char *show_doc
,
823 const char *help_doc
,
824 cmd_const_sfunc_ftype
*set_func
,
825 show_value_ftype
*show_func
,
826 struct cmd_list_element
**set_list
,
827 struct cmd_list_element
**show_list
)
829 struct cmd_list_element
*set
;
831 add_setshow_cmd_full (name
, theclass
, var_uinteger
, var
,
832 set_doc
, show_doc
, help_doc
,
837 set_cmd_completer (set
, integer_unlimited_completer
);
840 /* Add element named NAME to both the set and show command LISTs (the
841 list for set/show or some sublist thereof). CLASS is as in
842 add_cmd. VAR is address of the variable which will contain the
843 value. SET_DOC and SHOW_DOC are the documentation strings. */
845 add_setshow_zinteger_cmd (const char *name
, enum command_class theclass
,
847 const char *set_doc
, const char *show_doc
,
848 const char *help_doc
,
849 cmd_const_sfunc_ftype
*set_func
,
850 show_value_ftype
*show_func
,
851 struct cmd_list_element
**set_list
,
852 struct cmd_list_element
**show_list
)
854 add_setshow_cmd_full (name
, theclass
, var_zinteger
, var
,
855 set_doc
, show_doc
, help_doc
,
862 add_setshow_zuinteger_unlimited_cmd (const char *name
,
863 enum command_class theclass
,
866 const char *show_doc
,
867 const char *help_doc
,
868 cmd_const_sfunc_ftype
*set_func
,
869 show_value_ftype
*show_func
,
870 struct cmd_list_element
**set_list
,
871 struct cmd_list_element
**show_list
)
873 struct cmd_list_element
*set
;
875 add_setshow_cmd_full (name
, theclass
, var_zuinteger_unlimited
, var
,
876 set_doc
, show_doc
, help_doc
,
881 set_cmd_completer (set
, integer_unlimited_completer
);
884 /* Add element named NAME to both the set and show command LISTs (the
885 list for set/show or some sublist thereof). CLASS is as in
886 add_cmd. VAR is address of the variable which will contain the
887 value. SET_DOC and SHOW_DOC are the documentation strings. */
889 add_setshow_zuinteger_cmd (const char *name
, enum command_class theclass
,
891 const char *set_doc
, const char *show_doc
,
892 const char *help_doc
,
893 cmd_const_sfunc_ftype
*set_func
,
894 show_value_ftype
*show_func
,
895 struct cmd_list_element
**set_list
,
896 struct cmd_list_element
**show_list
)
898 add_setshow_cmd_full (name
, theclass
, var_zuinteger
, var
,
899 set_doc
, show_doc
, help_doc
,
905 /* Remove the command named NAME from the command list. Return the
906 list commands which were aliased to the deleted command. If the
907 command had no aliases, return NULL. The various *HOOKs are set to
908 the pre- and post-hook commands for the deleted command. If the
909 command does not have a hook, the corresponding out parameter is
912 static struct cmd_list_element
*
913 delete_cmd (const char *name
, struct cmd_list_element
**list
,
914 struct cmd_list_element
**prehook
,
915 struct cmd_list_element
**prehookee
,
916 struct cmd_list_element
**posthook
,
917 struct cmd_list_element
**posthookee
)
919 struct cmd_list_element
*iter
;
920 struct cmd_list_element
**previous_chain_ptr
;
921 struct cmd_list_element
*aliases
= NULL
;
927 previous_chain_ptr
= list
;
929 for (iter
= *previous_chain_ptr
; iter
; iter
= *previous_chain_ptr
)
931 if (strcmp (iter
->name
, name
) == 0)
934 iter
->destroyer (iter
, iter
->context
);
935 if (iter
->hookee_pre
)
936 iter
->hookee_pre
->hook_pre
= 0;
937 *prehook
= iter
->hook_pre
;
938 *prehookee
= iter
->hookee_pre
;
939 if (iter
->hookee_post
)
940 iter
->hookee_post
->hook_post
= 0;
941 *posthook
= iter
->hook_post
;
942 *posthookee
= iter
->hookee_post
;
944 /* Update the link. */
945 *previous_chain_ptr
= iter
->next
;
947 aliases
= iter
->aliases
;
949 /* If this command was an alias, remove it from the list of
951 if (iter
->is_alias ())
953 struct cmd_list_element
**prevp
= &iter
->alias_target
->aliases
;
954 struct cmd_list_element
*a
= *prevp
;
958 prevp
= &a
->alias_chain
;
961 *prevp
= iter
->alias_chain
;
966 /* We won't see another command with the same name. */
970 previous_chain_ptr
= &iter
->next
;
976 /* Shorthands to the commands above. */
978 /* Add an element to the list of info subcommands. */
980 struct cmd_list_element
*
981 add_info (const char *name
, cmd_const_cfunc_ftype
*fun
, const char *doc
)
983 return add_cmd (name
, class_info
, fun
, doc
, &infolist
);
986 /* Add an alias to the list of info subcommands. */
988 struct cmd_list_element
*
989 add_info_alias (const char *name
, const char *target_name
, int abbrev_flag
)
991 return add_alias_cmd (name
, target_name
, class_run
, abbrev_flag
, &infolist
);
994 /* Add an element to the list of commands. */
996 struct cmd_list_element
*
997 add_com (const char *name
, enum command_class theclass
,
998 cmd_const_cfunc_ftype
*fun
,
1001 return add_cmd (name
, theclass
, fun
, doc
, &cmdlist
);
1004 /* Add an alias or abbreviation command to the list of commands.
1005 For aliases predefined by GDB (such as bt), THECLASS must be
1006 different of class_alias, as class_alias is used to identify
1007 user defined aliases. */
1009 struct cmd_list_element
*
1010 add_com_alias (const char *name
, const char *target_name
,
1011 command_class theclass
, int abbrev_flag
)
1013 return add_alias_cmd (name
, target_name
, theclass
, abbrev_flag
, &cmdlist
);
1016 /* Add an element with a suppress notification to the list of commands. */
1018 struct cmd_list_element
*
1019 add_com_suppress_notification (const char *name
, enum command_class theclass
,
1020 cmd_const_cfunc_ftype
*fun
, const char *doc
,
1021 int *suppress_notification
)
1023 return add_cmd_suppress_notification (name
, theclass
, fun
, doc
,
1024 &cmdlist
, suppress_notification
);
1027 /* Print the prefix of C followed by name of C in title style. */
1030 fput_command_name_styled (struct cmd_list_element
*c
, struct ui_file
*stream
)
1032 std::string prefixname
1033 = c
->prefix
== nullptr ? "" : c
->prefix
->prefixname ();
1035 fprintf_styled (stream
, title_style
.style (), "%s%s",
1036 prefixname
.c_str (), c
->name
);
1039 /* Print the definition of alias C using title style for alias
1040 and aliased command. */
1043 fput_alias_definition_styled (struct cmd_list_element
*c
,
1044 struct ui_file
*stream
)
1046 gdb_assert (c
->is_alias ());
1047 fputs_filtered (" alias ", stream
);
1048 fput_command_name_styled (c
, stream
);
1049 fprintf_filtered (stream
, " = ");
1050 fput_command_name_styled (c
->alias_target
, stream
);
1051 fprintf_filtered (stream
, " %s\n", c
->default_args
.c_str ());
1054 /* Print the definition of the aliases of CMD that have default args. */
1057 fput_aliases_definition_styled (struct cmd_list_element
*cmd
,
1058 struct ui_file
*stream
)
1060 if (cmd
->aliases
!= nullptr)
1062 for (cmd_list_element
*iter
= cmd
->aliases
;
1064 iter
= iter
->alias_chain
)
1066 if (!iter
->default_args
.empty ())
1067 fput_alias_definition_styled (iter
, stream
);
1073 /* If C has one or more aliases, style print the name of C and
1074 the name of its aliases, separated by commas.
1075 If ALWAYS_FPUT_C_NAME, print the name of C even if it has no aliases.
1076 If one or more names are printed, POSTFIX is printed after the last name.
1080 fput_command_names_styled (struct cmd_list_element
*c
,
1081 bool always_fput_c_name
, const char *postfix
,
1082 struct ui_file
*stream
)
1084 if (always_fput_c_name
|| c
->aliases
!= nullptr)
1085 fput_command_name_styled (c
, stream
);
1086 if (c
->aliases
!= nullptr)
1088 for (cmd_list_element
*iter
= c
->aliases
; iter
; iter
= iter
->alias_chain
)
1090 fputs_filtered (", ", stream
);
1092 fput_command_name_styled (iter
, stream
);
1095 if (always_fput_c_name
|| c
->aliases
!= nullptr)
1096 fputs_filtered (postfix
, stream
);
1099 /* If VERBOSE, print the full help for command C and highlight the
1100 documentation parts matching HIGHLIGHT,
1101 otherwise print only one-line help for command C. */
1104 print_doc_of_command (struct cmd_list_element
*c
, const char *prefix
,
1105 bool verbose
, compiled_regex
&highlight
,
1106 struct ui_file
*stream
)
1108 /* When printing the full documentation, add a line to separate
1109 this documentation from the previous command help, in the likely
1110 case that apropos finds several commands. */
1112 fputs_filtered ("\n", stream
);
1114 fput_command_names_styled (c
, true,
1115 verbose
? "" : " -- ", stream
);
1118 fputs_filtered ("\n", stream
);
1119 fput_aliases_definition_styled (c
, stream
);
1120 fputs_highlighted (c
->doc
, highlight
, stream
);
1121 fputs_filtered ("\n", stream
);
1125 print_doc_line (stream
, c
->doc
, false);
1126 fputs_filtered ("\n", stream
);
1127 fput_aliases_definition_styled (c
, stream
);
1131 /* Recursively walk the commandlist structures, and print out the
1132 documentation of commands that match our regex in either their
1133 name, or their documentation.
1134 If VERBOSE, prints the complete documentation and highlight the
1135 documentation parts matching REGEX, otherwise prints only
1139 apropos_cmd (struct ui_file
*stream
,
1140 struct cmd_list_element
*commandlist
,
1141 bool verbose
, compiled_regex
®ex
, const char *prefix
)
1143 struct cmd_list_element
*c
;
1146 /* Walk through the commands. */
1147 for (c
=commandlist
;c
;c
=c
->next
)
1151 /* Command aliases/abbreviations are skipped to ensure we print the
1152 doc of a command only once, when encountering the aliased
1157 returnvalue
= -1; /* Needed to avoid double printing. */
1158 if (c
->name
!= NULL
)
1160 size_t name_len
= strlen (c
->name
);
1162 /* Try to match against the name. */
1163 returnvalue
= regex
.search (c
->name
, name_len
, 0, name_len
, NULL
);
1164 if (returnvalue
>= 0)
1165 print_doc_of_command (c
, prefix
, verbose
, regex
, stream
);
1167 /* Try to match against the name of the aliases. */
1168 for (cmd_list_element
*iter
= c
->aliases
;
1169 returnvalue
< 0 && iter
;
1170 iter
= iter
->alias_chain
)
1172 name_len
= strlen (iter
->name
);
1173 returnvalue
= regex
.search (iter
->name
, name_len
, 0, name_len
, NULL
);
1174 if (returnvalue
>= 0)
1175 print_doc_of_command (c
, prefix
, verbose
, regex
, stream
);
1178 if (c
->doc
!= NULL
&& returnvalue
< 0)
1180 size_t doc_len
= strlen (c
->doc
);
1182 /* Try to match against documentation. */
1183 if (regex
.search (c
->doc
, doc_len
, 0, doc_len
, NULL
) >= 0)
1184 print_doc_of_command (c
, prefix
, verbose
, regex
, stream
);
1186 /* Check if this command has subcommands. */
1187 if (c
->subcommands
!= NULL
)
1189 /* Recursively call ourselves on the subcommand list,
1190 passing the right prefix in. */
1191 apropos_cmd (stream
, *c
->subcommands
, verbose
, regex
,
1192 c
->prefixname ().c_str ());
1197 /* This command really has to deal with two things:
1198 1) I want documentation on *this string* (usually called by
1199 "help commandname").
1201 2) I want documentation on *this list* (usually called by giving a
1202 command that requires subcommands. Also called by saying just
1205 I am going to split this into two separate commands, help_cmd and
1209 help_cmd (const char *command
, struct ui_file
*stream
)
1211 struct cmd_list_element
*c
, *alias
, *prefix_cmd
, *c_cmd
;
1215 help_list (cmdlist
, "", all_classes
, stream
);
1219 if (strcmp (command
, "all") == 0)
1225 const char *orig_command
= command
;
1226 c
= lookup_cmd (&command
, cmdlist
, "", NULL
, 0, 0);
1231 lookup_cmd_composition (orig_command
, &alias
, &prefix_cmd
, &c_cmd
);
1233 /* There are three cases here.
1234 If c->subcommands is nonzero, we have a prefix command.
1235 Print its documentation, then list its subcommands.
1237 If c->func is non NULL, we really have a command. Print its
1238 documentation and return.
1240 If c->func is NULL, we have a class name. Print its
1241 documentation (as if it were a command) and then set class to the
1242 number of this class so that the commands in the class will be
1245 /* If the user asked 'help somecommand' and there is no alias,
1246 the false indicates to not output the (single) command name. */
1247 fput_command_names_styled (c
, false, "\n", stream
);
1248 fput_aliases_definition_styled (c
, stream
);
1249 fputs_filtered (c
->doc
, stream
);
1250 fputs_filtered ("\n", stream
);
1252 if (c
->subcommands
== 0 && c
->func
!= NULL
)
1254 fprintf_filtered (stream
, "\n");
1256 /* If this is a prefix command, print it's subcommands. */
1258 help_list (*c
->subcommands
, c
->prefixname ().c_str (),
1259 all_commands
, stream
);
1261 /* If this is a class name, print all of the commands in the class. */
1262 if (c
->func
== NULL
)
1263 help_list (cmdlist
, "", c
->theclass
, stream
);
1265 if (c
->hook_pre
|| c
->hook_post
)
1266 fprintf_filtered (stream
,
1267 "\nThis command has a hook (or hooks) defined:\n");
1270 fprintf_filtered (stream
,
1271 "\tThis command is run after : %s (pre hook)\n",
1274 fprintf_filtered (stream
,
1275 "\tThis command is run before : %s (post hook)\n",
1276 c
->hook_post
->name
);
1280 * Get a specific kind of help on a command list.
1283 * CMDTYPE is the prefix to use in the title string.
1284 * CLASS is the class with which to list the nodes of this list (see
1285 * documentation for help_cmd_list below), As usual, ALL_COMMANDS for
1286 * everything, ALL_CLASSES for just classes, and non-negative for only things
1287 * in a specific class.
1288 * and STREAM is the output stream on which to print things.
1289 * If you call this routine with a class >= 0, it recurses.
1292 help_list (struct cmd_list_element
*list
, const char *cmdtype
,
1293 enum command_class theclass
, struct ui_file
*stream
)
1296 char *cmdtype1
, *cmdtype2
;
1298 /* If CMDTYPE is "foo ", CMDTYPE1 gets " foo" and CMDTYPE2 gets "foo sub".
1300 len
= strlen (cmdtype
);
1301 cmdtype1
= (char *) alloca (len
+ 1);
1303 cmdtype2
= (char *) alloca (len
+ 4);
1308 memcpy (cmdtype1
+ 1, cmdtype
, len
- 1);
1310 memcpy (cmdtype2
, cmdtype
, len
- 1);
1311 strcpy (cmdtype2
+ len
- 1, " sub");
1314 if (theclass
== all_classes
)
1315 fprintf_filtered (stream
, "List of classes of %scommands:\n\n", cmdtype2
);
1317 fprintf_filtered (stream
, "List of %scommands:\n\n", cmdtype2
);
1319 help_cmd_list (list
, theclass
, theclass
>= 0, stream
);
1321 if (theclass
== all_classes
)
1323 fprintf_filtered (stream
, "\n\
1324 Type \"help%s\" followed by a class name for a list of commands in ",
1327 fprintf_filtered (stream
, "that class.");
1329 fprintf_filtered (stream
, "\n\
1330 Type \"help all\" for the list of all commands.");
1333 fprintf_filtered (stream
, "\nType \"help%s\" followed by %scommand name ",
1334 cmdtype1
, cmdtype2
);
1336 fputs_filtered ("for ", stream
);
1338 fputs_filtered ("full ", stream
);
1340 fputs_filtered ("documentation.\n", stream
);
1341 fputs_filtered ("Type \"apropos word\" to search "
1342 "for commands related to \"word\".\n", stream
);
1343 fputs_filtered ("Type \"apropos -v word\" for full documentation", stream
);
1345 fputs_filtered (" of commands related to \"word\".\n", stream
);
1346 fputs_filtered ("Command name abbreviations are allowed if unambiguous.\n",
1351 help_all (struct ui_file
*stream
)
1353 struct cmd_list_element
*c
;
1354 int seen_unclassified
= 0;
1356 for (c
= cmdlist
; c
; c
= c
->next
)
1360 /* If this is a class name, print all of the commands in the
1363 if (c
->func
== NULL
)
1365 fprintf_filtered (stream
, "\nCommand class: %s\n\n", c
->name
);
1366 help_cmd_list (cmdlist
, c
->theclass
, true, stream
);
1370 /* While it's expected that all commands are in some class,
1371 as a safety measure, we'll print commands outside of any
1372 class at the end. */
1374 for (c
= cmdlist
; c
; c
= c
->next
)
1379 if (c
->theclass
== no_class
)
1381 if (!seen_unclassified
)
1383 fprintf_filtered (stream
, "\nUnclassified commands\n\n");
1384 seen_unclassified
= 1;
1386 print_help_for_command (c
, true, stream
);
1392 /* See cli-decode.h. */
1395 print_doc_line (struct ui_file
*stream
, const char *str
,
1396 bool for_value_prefix
)
1398 static char *line_buffer
= 0;
1399 static int line_size
;
1405 line_buffer
= (char *) xmalloc (line_size
);
1408 /* Searches for the first end of line or the end of STR. */
1410 while (*p
&& *p
!= '\n')
1412 if (p
- str
> line_size
- 1)
1414 line_size
= p
- str
+ 1;
1415 xfree (line_buffer
);
1416 line_buffer
= (char *) xmalloc (line_size
);
1418 strncpy (line_buffer
, str
, p
- str
);
1419 if (for_value_prefix
)
1421 if (islower (line_buffer
[0]))
1422 line_buffer
[0] = toupper (line_buffer
[0]);
1423 gdb_assert (p
> str
);
1424 if (line_buffer
[p
- str
- 1] == '.')
1425 line_buffer
[p
- str
- 1] = '\0';
1427 line_buffer
[p
- str
] = '\0';
1430 line_buffer
[p
- str
] = '\0';
1431 fputs_filtered (line_buffer
, stream
);
1434 /* Print one-line help for command C.
1435 If RECURSE is non-zero, also print one-line descriptions
1436 of all prefixed subcommands. */
1438 print_help_for_command (struct cmd_list_element
*c
,
1439 bool recurse
, struct ui_file
*stream
)
1441 fput_command_names_styled (c
, true, " -- ", stream
);
1442 print_doc_line (stream
, c
->doc
, false);
1443 fputs_filtered ("\n", stream
);
1444 if (!c
->default_args
.empty ())
1445 fput_alias_definition_styled (c
, stream
);
1446 fput_aliases_definition_styled (c
, stream
);
1449 && c
->subcommands
!= 0
1450 && c
->abbrev_flag
== 0)
1451 /* Subcommands of a prefix command typically have 'all_commands'
1452 as class. If we pass CLASS to recursive invocation,
1453 most often we won't see anything. */
1454 help_cmd_list (*c
->subcommands
, all_commands
, true, stream
);
1458 * Implement a help command on command list LIST.
1459 * RECURSE should be non-zero if this should be done recursively on
1460 * all sublists of LIST.
1461 * STREAM is the stream upon which the output should be written.
1462 * THECLASS should be:
1463 * A non-negative class number to list only commands in that
1464 * ALL_COMMANDS to list all commands in list.
1465 * ALL_CLASSES to list all classes in list.
1467 * Note that aliases are only shown when THECLASS is class_alias.
1468 * In the other cases, the aliases will be shown together with their
1471 * Note that RECURSE will be active on *all* sublists, not just the
1472 * ones selected by the criteria above (ie. the selection mechanism
1473 * is at the low level, not the high-level).
1477 help_cmd_list (struct cmd_list_element
*list
, enum command_class theclass
,
1478 bool recurse
, struct ui_file
*stream
)
1480 struct cmd_list_element
*c
;
1482 for (c
= list
; c
; c
= c
->next
)
1484 if (c
->abbrev_flag
== 1 || c
->cmd_deprecated
)
1486 /* Do not show abbreviations or deprecated commands. */
1490 if (c
->is_alias () && theclass
!= class_alias
)
1492 /* Do not show an alias, unless specifically showing the
1493 list of aliases: for all other classes, an alias is
1494 shown (if needed) together with its aliased command. */
1498 if (theclass
== all_commands
1499 || (theclass
== all_classes
&& c
->func
== NULL
)
1500 || (theclass
== c
->theclass
&& c
->func
!= NULL
))
1503 - showing all commands
1504 - showing all classes and C is a help class
1505 - showing commands of THECLASS and C is not the help class */
1507 /* If we show the class_alias and C is an alias, do not recurse,
1508 as this would show the (possibly very long) not very useful
1509 list of sub-commands of the aliased command. */
1510 print_help_for_command
1512 recurse
&& (theclass
!= class_alias
|| !c
->is_alias ()),
1518 && (theclass
== class_user
|| theclass
== class_alias
)
1519 && c
->subcommands
!= NULL
)
1521 /* User-defined commands or aliases may be subcommands. */
1522 help_cmd_list (*c
->subcommands
, theclass
, recurse
, stream
);
1526 /* Do not show C or recurse on C, e.g. because C does not belong to
1527 THECLASS or because C is a help class. */
1532 /* Search the input clist for 'command'. Return the command if
1533 found (or NULL if not), and return the number of commands
1536 static struct cmd_list_element
*
1537 find_cmd (const char *command
, int len
, struct cmd_list_element
*clist
,
1538 int ignore_help_classes
, int *nfound
)
1540 struct cmd_list_element
*found
, *c
;
1544 for (c
= clist
; c
; c
= c
->next
)
1545 if (!strncmp (command
, c
->name
, len
)
1546 && (!ignore_help_classes
|| c
->func
))
1550 if (c
->name
[len
] == '\0')
1559 /* Return the length of command name in TEXT. */
1562 find_command_name_length (const char *text
)
1564 const char *p
= text
;
1566 /* Treating underscores as part of command words is important
1567 so that "set args_foo()" doesn't get interpreted as
1568 "set args _foo()". */
1569 /* Some characters are only used for TUI specific commands.
1570 However, they are always allowed for the sake of consistency.
1572 Note that this is larger than the character set allowed when
1573 creating user-defined commands. */
1575 /* Recognize the single character commands so that, e.g., "!ls"
1576 works as expected. */
1577 if (*p
== '!' || *p
== '|')
1580 while (valid_cmd_char_p (*p
)
1581 /* Characters used by TUI specific commands. */
1582 || *p
== '+' || *p
== '<' || *p
== '>' || *p
== '$')
1588 /* See command.h. */
1591 valid_cmd_char_p (int c
)
1593 /* Alas "42" is a legitimate user-defined command.
1594 In the interests of not breaking anything we preserve that. */
1596 return isalnum (c
) || c
== '-' || c
== '_' || c
== '.';
1599 /* See command.h. */
1602 valid_user_defined_cmd_name_p (const char *name
)
1609 for (p
= name
; *p
!= '\0'; ++p
)
1611 if (valid_cmd_char_p (*p
))
1620 /* See command.h. */
1622 struct cmd_list_element
*
1623 lookup_cmd_1 (const char **text
, struct cmd_list_element
*clist
,
1624 struct cmd_list_element
**result_list
, std::string
*default_args
,
1625 int ignore_help_classes
, bool lookup_for_completion_p
)
1629 struct cmd_list_element
*found
, *c
;
1630 bool found_alias
= false;
1631 const char *line
= *text
;
1633 while (**text
== ' ' || **text
== '\t')
1636 /* Identify the name of the command. */
1637 len
= find_command_name_length (*text
);
1639 /* If nothing but whitespace, return 0. */
1643 /* *text and p now bracket the first command word to lookup (and
1644 it's length is len). We copy this into a local temporary. */
1647 command
= (char *) alloca (len
+ 1);
1648 memcpy (command
, *text
, len
);
1649 command
[len
] = '\0';
1654 found
= find_cmd (command
, len
, clist
, ignore_help_classes
, &nfound
);
1656 /* If nothing matches, we have a simple failure. */
1662 if (result_list
!= nullptr)
1663 /* Will be modified in calling routine
1664 if we know what the prefix command is. */
1666 if (default_args
!= nullptr)
1667 *default_args
= std::string ();
1668 return CMD_LIST_AMBIGUOUS
; /* Ambiguous. */
1671 /* We've matched something on this list. Move text pointer forward. */
1675 if (found
->is_alias ())
1677 /* We drop the alias (abbreviation) in favor of the command it
1678 is pointing to. If the alias is deprecated, though, we need to
1679 warn the user about it before we drop it. Note that while we
1680 are warning about the alias, we may also warn about the command
1681 itself and we will adjust the appropriate DEPRECATED_WARN_USER
1684 if (found
->deprecated_warn_user
&& !lookup_for_completion_p
)
1685 deprecated_cmd_warning (line
, clist
);
1688 /* Return the default_args of the alias, not the default_args
1689 of the command it is pointing to. */
1690 if (default_args
!= nullptr)
1691 *default_args
= found
->default_args
;
1692 found
= found
->alias_target
;
1695 /* If we found a prefix command, keep looking. */
1697 if (found
->subcommands
)
1699 c
= lookup_cmd_1 (text
, *found
->subcommands
, result_list
, default_args
,
1700 ignore_help_classes
, lookup_for_completion_p
);
1703 /* Didn't find anything; this is as far as we got. */
1704 if (result_list
!= nullptr)
1705 *result_list
= clist
;
1706 if (!found_alias
&& default_args
!= nullptr)
1707 *default_args
= found
->default_args
;
1710 else if (c
== CMD_LIST_AMBIGUOUS
)
1712 /* We've gotten this far properly, but the next step is
1713 ambiguous. We need to set the result list to the best
1714 we've found (if an inferior hasn't already set it). */
1715 if (result_list
!= nullptr)
1717 /* This used to say *result_list = *found->subcommands.
1718 If that was correct, need to modify the documentation
1719 at the top of this function to clarify what is
1720 supposed to be going on. */
1721 *result_list
= found
;
1722 /* For ambiguous commands, do not return any default_args args. */
1723 if (default_args
!= nullptr)
1724 *default_args
= std::string ();
1735 if (result_list
!= nullptr)
1736 *result_list
= clist
;
1737 if (!found_alias
&& default_args
!= nullptr)
1738 *default_args
= found
->default_args
;
1743 /* All this hair to move the space to the front of cmdtype */
1746 undef_cmd_error (const char *cmdtype
, const char *q
)
1748 error (_("Undefined %scommand: \"%s\". Try \"help%s%.*s\"."),
1751 *cmdtype
? " " : "",
1752 (int) strlen (cmdtype
) - 1,
1756 /* Look up the contents of *LINE as a command in the command list LIST.
1757 LIST is a chain of struct cmd_list_element's.
1758 If it is found, return the struct cmd_list_element for that command,
1759 update *LINE to point after the command name, at the first argument
1760 and update *DEFAULT_ARGS (if DEFAULT_ARGS is not null) to the default
1761 args to prepend to the user provided args when running the command.
1762 Note that if the found cmd_list_element is found via an alias,
1763 the default args of the alias are returned.
1765 If not found, call error if ALLOW_UNKNOWN is zero
1766 otherwise (or if error returns) return zero.
1767 Call error if specified command is ambiguous,
1768 unless ALLOW_UNKNOWN is negative.
1769 CMDTYPE precedes the word "command" in the error message.
1771 If IGNORE_HELP_CLASSES is nonzero, ignore any command list
1772 elements which are actually help classes rather than commands (i.e.
1773 the function field of the struct cmd_list_element is 0). */
1775 struct cmd_list_element
*
1776 lookup_cmd (const char **line
, struct cmd_list_element
*list
,
1777 const char *cmdtype
,
1778 std::string
*default_args
,
1779 int allow_unknown
, int ignore_help_classes
)
1781 struct cmd_list_element
*last_list
= 0;
1782 struct cmd_list_element
*c
;
1784 /* Note: Do not remove trailing whitespace here because this
1785 would be wrong for complete_command. Jim Kingdon */
1788 error (_("Lack of needed %scommand"), cmdtype
);
1790 c
= lookup_cmd_1 (line
, list
, &last_list
, default_args
, ignore_help_classes
);
1797 int len
= find_command_name_length (*line
);
1799 q
= (char *) alloca (len
+ 1);
1800 strncpy (q
, *line
, len
);
1802 undef_cmd_error (cmdtype
, q
);
1807 else if (c
== CMD_LIST_AMBIGUOUS
)
1809 /* Ambigous. Local values should be off subcommands or called
1811 int local_allow_unknown
= (last_list
? last_list
->allow_unknown
:
1813 std::string local_cmdtype
1814 = last_list
? last_list
->prefixname () : cmdtype
;
1815 struct cmd_list_element
*local_list
=
1816 (last_list
? *(last_list
->subcommands
) : list
);
1818 if (local_allow_unknown
< 0)
1821 return last_list
; /* Found something. */
1823 return 0; /* Found nothing. */
1827 /* Report as error. */
1832 ((*line
)[amb_len
] && (*line
)[amb_len
] != ' '
1833 && (*line
)[amb_len
] != '\t');
1838 for (c
= local_list
; c
; c
= c
->next
)
1839 if (!strncmp (*line
, c
->name
, amb_len
))
1841 if (strlen (ambbuf
) + strlen (c
->name
) + 6
1842 < (int) sizeof ambbuf
)
1844 if (strlen (ambbuf
))
1845 strcat (ambbuf
, ", ");
1846 strcat (ambbuf
, c
->name
);
1850 strcat (ambbuf
, "..");
1854 error (_("Ambiguous %scommand \"%s\": %s."),
1855 local_cmdtype
.c_str (), *line
, ambbuf
);
1860 if (c
->type
== set_cmd
&& **line
!= '\0' && !isspace (**line
))
1861 error (_("Argument must be preceded by space."));
1863 /* We've got something. It may still not be what the caller
1864 wants (if this command *needs* a subcommand). */
1865 while (**line
== ' ' || **line
== '\t')
1868 if (c
->subcommands
&& **line
&& !c
->allow_unknown
)
1869 undef_cmd_error (c
->prefixname ().c_str (), *line
);
1871 /* Seems to be what he wants. Return it. */
1877 /* See command.h. */
1879 struct cmd_list_element
*
1880 lookup_cmd_exact (const char *name
,
1881 struct cmd_list_element
*list
,
1882 bool ignore_help_classes
)
1884 const char *tem
= name
;
1885 struct cmd_list_element
*cmd
= lookup_cmd (&tem
, list
, "", NULL
, -1,
1886 ignore_help_classes
);
1887 if (cmd
!= nullptr && strcmp (name
, cmd
->name
) != 0)
1892 /* We are here presumably because an alias or command in TEXT is
1893 deprecated and a warning message should be generated. This
1894 function decodes TEXT and potentially generates a warning message
1897 Example for 'set endian big' which has a fictitious alias 'seb'.
1899 If alias wasn't used in TEXT, and the command is deprecated:
1900 "warning: 'set endian big' is deprecated."
1902 If alias was used, and only the alias is deprecated:
1903 "warning: 'seb' an alias for the command 'set endian big' is deprecated."
1905 If alias was used and command is deprecated (regardless of whether
1906 the alias itself is deprecated:
1908 "warning: 'set endian big' (seb) is deprecated."
1910 After the message has been sent, clear the appropriate flags in the
1911 command and/or the alias so the user is no longer bothered.
1915 deprecated_cmd_warning (const char *text
, struct cmd_list_element
*list
)
1917 struct cmd_list_element
*alias
= nullptr;
1918 struct cmd_list_element
*cmd
= nullptr;
1920 /* Return if text doesn't evaluate to a command. We place this lookup
1921 within its own scope so that the PREFIX_CMD local is not visible
1922 later in this function. The value returned in PREFIX_CMD is based on
1923 the prefix found in TEXT, and is our case this prefix can be missing
1924 in some situations (when LIST is not the global CMDLIST).
1926 It is better for our purposes to use the prefix commands directly from
1927 the ALIAS and CMD results. */
1929 struct cmd_list_element
*prefix_cmd
= nullptr;
1930 if (!lookup_cmd_composition_1 (text
, &alias
, &prefix_cmd
, &cmd
, list
))
1934 /* Return if nothing is deprecated. */
1935 if (!((alias
!= nullptr ? alias
->deprecated_warn_user
: 0)
1936 || cmd
->deprecated_warn_user
))
1939 /* Join command prefix (if any) and the command name. */
1940 std::string tmp_cmd_str
;
1941 if (cmd
->prefix
!= nullptr)
1942 tmp_cmd_str
+= cmd
->prefix
->prefixname ();
1943 tmp_cmd_str
+= std::string (cmd
->name
);
1945 /* Display the appropriate first line, this warns that the thing the user
1946 entered is deprecated. */
1947 if (alias
!= nullptr)
1949 /* Join the alias prefix (if any) and the alias name. */
1950 std::string tmp_alias_str
;
1951 if (alias
->prefix
!= nullptr)
1952 tmp_alias_str
+= alias
->prefix
->prefixname ();
1953 tmp_alias_str
+= std::string (alias
->name
);
1955 if (cmd
->cmd_deprecated
)
1956 printf_filtered (_("Warning: command '%ps' (%ps) is deprecated.\n"),
1957 styled_string (title_style
.style (),
1958 tmp_cmd_str
.c_str ()),
1959 styled_string (title_style
.style (),
1960 tmp_alias_str
.c_str ()));
1962 printf_filtered (_("Warning: '%ps', an alias for the command '%ps', "
1963 "is deprecated.\n"),
1964 styled_string (title_style
.style (),
1965 tmp_alias_str
.c_str ()),
1966 styled_string (title_style
.style (),
1967 tmp_cmd_str
.c_str ()));
1970 printf_filtered (_("Warning: command '%ps' is deprecated.\n"),
1971 styled_string (title_style
.style (),
1972 tmp_cmd_str
.c_str ()));
1974 /* Now display a second line indicating what the user should use instead.
1975 If it is only the alias that is deprecated, we want to indicate the
1976 new alias, otherwise we'll indicate the new command. */
1977 const char *replacement
;
1978 if (alias
!= nullptr && !cmd
->cmd_deprecated
)
1979 replacement
= alias
->replacement
;
1981 replacement
= cmd
->replacement
;
1982 if (replacement
!= nullptr)
1983 printf_filtered (_("Use '%ps'.\n\n"),
1984 styled_string (title_style
.style (),
1987 printf_filtered (_("No alternative known.\n\n"));
1989 /* We've warned you, now we'll keep quiet. */
1990 if (alias
!= nullptr)
1991 alias
->deprecated_warn_user
= 0;
1992 cmd
->deprecated_warn_user
= 0;
1995 /* Look up the contents of TEXT as a command in the command list CUR_LIST.
1996 Return 1 on success, 0 on failure.
1998 If TEXT refers to an alias, *ALIAS will point to that alias.
2000 If TEXT is a subcommand (i.e. one that is preceded by a prefix
2001 command) set *PREFIX_CMD.
2003 Set *CMD to point to the command TEXT indicates.
2005 If any of *ALIAS, *PREFIX_CMD, or *CMD cannot be determined or do not
2006 exist, they are NULL when we return.
2011 lookup_cmd_composition_1 (const char *text
,
2012 struct cmd_list_element
**alias
,
2013 struct cmd_list_element
**prefix_cmd
,
2014 struct cmd_list_element
**cmd
,
2015 struct cmd_list_element
*cur_list
)
2018 *prefix_cmd
= cur_list
->prefix
;
2021 text
= skip_spaces (text
);
2023 /* Go through as many command lists as we need to, to find the command
2027 /* Identify the name of the command. */
2028 int len
= find_command_name_length (text
);
2030 /* If nothing but whitespace, return. */
2034 /* TEXT is the start of the first command word to lookup (and
2035 it's length is LEN). We copy this into a local temporary. */
2036 std::string
command (text
, len
);
2040 *cmd
= find_cmd (command
.c_str (), len
, cur_list
, 1, &nfound
);
2042 /* We only handle the case where a single command was found. */
2043 if (*cmd
== CMD_LIST_AMBIGUOUS
|| *cmd
== nullptr)
2047 if ((*cmd
)->is_alias ())
2049 /* If the command was actually an alias, we note that an
2050 alias was used (by assigning *ALIAS) and we set *CMD. */
2052 *cmd
= (*cmd
)->alias_target
;
2057 text
= skip_spaces (text
);
2059 if ((*cmd
)->subcommands
!= nullptr && *text
!= '\0')
2061 cur_list
= *(*cmd
)->subcommands
;
2069 /* Look up the contents of TEXT as a command in the command list 'cmdlist'.
2070 Return 1 on success, 0 on failure.
2072 If TEXT refers to an alias, *ALIAS will point to that alias.
2074 If TEXT is a subcommand (i.e. one that is preceded by a prefix
2075 command) set *PREFIX_CMD.
2077 Set *CMD to point to the command TEXT indicates.
2079 If any of *ALIAS, *PREFIX_CMD, or *CMD cannot be determined or do not
2080 exist, they are NULL when we return.
2085 lookup_cmd_composition (const char *text
,
2086 struct cmd_list_element
**alias
,
2087 struct cmd_list_element
**prefix_cmd
,
2088 struct cmd_list_element
**cmd
)
2090 return lookup_cmd_composition_1 (text
, alias
, prefix_cmd
, cmd
, cmdlist
);
2093 /* Helper function for SYMBOL_COMPLETION_FUNCTION. */
2095 /* Return a vector of char pointers which point to the different
2096 possible completions in LIST of TEXT.
2098 WORD points in the same buffer as TEXT, and completions should be
2099 returned relative to this position. For example, suppose TEXT is
2100 "foo" and we want to complete to "foobar". If WORD is "oo", return
2101 "oobar"; if WORD is "baz/foo", return "baz/foobar". */
2104 complete_on_cmdlist (struct cmd_list_element
*list
,
2105 completion_tracker
&tracker
,
2106 const char *text
, const char *word
,
2107 int ignore_help_classes
)
2109 struct cmd_list_element
*ptr
;
2110 int textlen
= strlen (text
);
2112 int saw_deprecated_match
= 0;
2114 /* We do one or two passes. In the first pass, we skip deprecated
2115 commands. If we see no matching commands in the first pass, and
2116 if we did happen to see a matching deprecated command, we do
2117 another loop to collect those. */
2118 for (pass
= 0; pass
< 2; ++pass
)
2120 bool got_matches
= false;
2122 for (ptr
= list
; ptr
; ptr
= ptr
->next
)
2123 if (!strncmp (ptr
->name
, text
, textlen
)
2124 && !ptr
->abbrev_flag
2125 && (!ignore_help_classes
|| ptr
->func
2126 || ptr
->subcommands
))
2130 if (ptr
->cmd_deprecated
)
2132 saw_deprecated_match
= 1;
2137 tracker
.add_completion
2138 (make_completion_match_str (ptr
->name
, text
, word
));
2145 /* If we saw no matching deprecated commands in the first pass,
2147 if (!saw_deprecated_match
)
2152 /* Helper function for SYMBOL_COMPLETION_FUNCTION. */
2154 /* Add the different possible completions in ENUMLIST of TEXT.
2156 WORD points in the same buffer as TEXT, and completions should be
2157 returned relative to this position. For example, suppose TEXT is "foo"
2158 and we want to complete to "foobar". If WORD is "oo", return
2159 "oobar"; if WORD is "baz/foo", return "baz/foobar". */
2162 complete_on_enum (completion_tracker
&tracker
,
2163 const char *const *enumlist
,
2164 const char *text
, const char *word
)
2166 int textlen
= strlen (text
);
2170 for (i
= 0; (name
= enumlist
[i
]) != NULL
; i
++)
2171 if (strncmp (name
, text
, textlen
) == 0)
2172 tracker
.add_completion (make_completion_match_str (name
, text
, word
));
2176 /* Check function pointer. */
2178 cmd_func_p (struct cmd_list_element
*cmd
)
2180 return (cmd
->func
!= NULL
);
2184 /* Call the command function. */
2186 cmd_func (struct cmd_list_element
*cmd
, const char *args
, int from_tty
)
2188 if (cmd_func_p (cmd
))
2190 gdb::optional
<scoped_restore_tmpl
<int>> restore_suppress
;
2192 if (cmd
->suppress_notification
!= NULL
)
2193 restore_suppress
.emplace (cmd
->suppress_notification
, 1);
2195 (*cmd
->func
) (cmd
, args
, from_tty
);
2198 error (_("Invalid command"));
2202 cli_user_command_p (struct cmd_list_element
*cmd
)
2204 return (cmd
->theclass
== class_user
2205 && (cmd
->func
== do_const_cfunc
|| cmd
->func
== do_sfunc
));