1 /* Handle lists of commands, their decoding and documentation, for GDB.
3 Copyright (C) 1986-2020 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 /* Look up a command whose 'prefixlist' is KEY. Return the command if found,
54 otherwise return NULL. */
56 static struct cmd_list_element
*
57 lookup_cmd_for_prefixlist (struct cmd_list_element
**key
,
58 struct cmd_list_element
*list
)
60 struct cmd_list_element
*p
= NULL
;
62 for (p
= list
; p
!= NULL
; p
= p
->next
)
64 struct cmd_list_element
*q
;
66 if (p
->prefixlist
== NULL
)
68 else if (p
->prefixlist
== key
)
70 /* If we found an alias, we must return the aliased
72 return p
->cmd_pointer
? p
->cmd_pointer
: p
;
75 q
= lookup_cmd_for_prefixlist (key
, *(p
->prefixlist
));
84 print_help_for_command (struct cmd_list_element
*c
,
85 bool recurse
, struct ui_file
*stream
);
88 /* Set the callback function for the specified command. For each both
89 the commands callback and func() are set. The latter set to a
90 bounce function (unless cfunc / sfunc is NULL that is). */
93 do_const_cfunc (struct cmd_list_element
*c
, const char *args
, int from_tty
)
95 c
->function
.const_cfunc (args
, from_tty
);
99 set_cmd_cfunc (struct cmd_list_element
*cmd
, cmd_const_cfunc_ftype
*cfunc
)
104 cmd
->func
= do_const_cfunc
;
105 cmd
->function
.const_cfunc
= cfunc
;
109 do_sfunc (struct cmd_list_element
*c
, const char *args
, int from_tty
)
111 c
->function
.sfunc (args
, from_tty
, c
);
115 set_cmd_sfunc (struct cmd_list_element
*cmd
, cmd_const_sfunc_ftype
*sfunc
)
120 cmd
->func
= do_sfunc
;
121 cmd
->function
.sfunc
= sfunc
;
125 cmd_cfunc_eq (struct cmd_list_element
*cmd
, cmd_const_cfunc_ftype
*cfunc
)
127 return cmd
->func
== do_const_cfunc
&& cmd
->function
.const_cfunc
== cfunc
;
131 set_cmd_context (struct cmd_list_element
*cmd
, void *context
)
133 cmd
->context
= context
;
137 get_cmd_context (struct cmd_list_element
*cmd
)
143 cmd_type (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
;
163 /* Add element named NAME.
164 Space for NAME and DOC must be allocated by the caller.
165 CLASS is the top level category into which commands are broken down
167 FUN should be the function to execute the command;
168 it will get a character string as argument, with leading
169 and trailing blanks already eliminated.
171 DOC is a documentation string for the command.
172 Its first line should be a complete sentence.
173 It should start with ? for a command that is an abbreviation
174 or with * for a command that most users don't need to know about.
176 Add this command to command list *LIST.
178 Returns a pointer to the added command (not necessarily the head
181 static struct cmd_list_element
*
182 do_add_cmd (const char *name
, enum command_class theclass
,
183 const char *doc
, struct cmd_list_element
**list
)
185 struct cmd_list_element
*c
= new struct cmd_list_element (name
, theclass
,
187 struct cmd_list_element
*p
, *iter
;
189 /* Turn each alias of the old command into an alias of the new
191 c
->aliases
= delete_cmd (name
, list
, &c
->hook_pre
, &c
->hookee_pre
,
192 &c
->hook_post
, &c
->hookee_post
);
193 for (iter
= c
->aliases
; iter
; iter
= iter
->alias_chain
)
194 iter
->cmd_pointer
= c
;
196 c
->hook_pre
->hookee_pre
= c
;
198 c
->hookee_pre
->hook_pre
= c
;
200 c
->hook_post
->hookee_post
= c
;
202 c
->hookee_post
->hook_post
= c
;
204 if (*list
== NULL
|| strcmp ((*list
)->name
, name
) >= 0)
212 while (p
->next
&& strcmp (p
->next
->name
, name
) <= 0)
220 /* Search the prefix cmd of C, and assigns it to C->prefix.
221 See also add_prefix_cmd and update_prefix_field_of_prefixed_commands. */
222 struct cmd_list_element
*prefixcmd
= lookup_cmd_for_prefixlist (list
,
224 c
->prefix
= prefixcmd
;
230 struct cmd_list_element
*
231 add_cmd (const char *name
, enum command_class theclass
,
232 const char *doc
, struct cmd_list_element
**list
)
234 cmd_list_element
*result
= do_add_cmd (name
, theclass
, doc
, list
);
236 result
->function
.const_cfunc
= NULL
;
240 struct cmd_list_element
*
241 add_cmd (const char *name
, enum command_class theclass
,
242 cmd_const_cfunc_ftype
*fun
,
243 const char *doc
, struct cmd_list_element
**list
)
245 cmd_list_element
*result
= do_add_cmd (name
, theclass
, doc
, list
);
246 set_cmd_cfunc (result
, fun
);
250 /* Add an element with a suppress notification to the LIST of commands. */
252 struct cmd_list_element
*
253 add_cmd_suppress_notification (const char *name
, enum command_class theclass
,
254 cmd_const_cfunc_ftype
*fun
, const char *doc
,
255 struct cmd_list_element
**list
,
256 int *suppress_notification
)
258 struct cmd_list_element
*element
;
260 element
= add_cmd (name
, theclass
, fun
, doc
, list
);
261 element
->suppress_notification
= suppress_notification
;
267 /* Deprecates a command CMD.
268 REPLACEMENT is the name of the command which should be used in
269 place of this command, or NULL if no such command exists.
271 This function does not check to see if command REPLACEMENT exists
272 since gdb may not have gotten around to adding REPLACEMENT when
273 this function is called.
275 Returns a pointer to the deprecated command. */
277 struct cmd_list_element
*
278 deprecate_cmd (struct cmd_list_element
*cmd
, const char *replacement
)
280 cmd
->cmd_deprecated
= 1;
281 cmd
->deprecated_warn_user
= 1;
283 if (replacement
!= NULL
)
284 cmd
->replacement
= replacement
;
286 cmd
->replacement
= NULL
;
291 struct cmd_list_element
*
292 add_alias_cmd (const char *name
, cmd_list_element
*old
,
293 enum command_class theclass
, int abbrev_flag
,
294 struct cmd_list_element
**list
)
298 struct cmd_list_element
*prehook
, *prehookee
, *posthook
, *posthookee
;
299 struct cmd_list_element
*aliases
= delete_cmd (name
, list
,
300 &prehook
, &prehookee
,
301 &posthook
, &posthookee
);
303 /* If this happens, it means a programmer error somewhere. */
304 gdb_assert (!aliases
&& !prehook
&& !prehookee
305 && !posthook
&& ! posthookee
);
309 struct cmd_list_element
*c
= add_cmd (name
, theclass
, old
->doc
, list
);
311 /* If OLD->DOC can be freed, we should make another copy. */
312 if (old
->doc_allocated
)
314 c
->doc
= xstrdup (old
->doc
);
315 c
->doc_allocated
= 1;
317 /* NOTE: Both FUNC and all the FUNCTIONs need to be copied. */
319 c
->function
= old
->function
;
320 c
->prefixlist
= old
->prefixlist
;
321 c
->prefixname
= old
->prefixname
;
322 c
->allow_unknown
= old
->allow_unknown
;
323 c
->abbrev_flag
= abbrev_flag
;
324 c
->cmd_pointer
= old
;
325 c
->alias_chain
= old
->aliases
;
331 struct cmd_list_element
*
332 add_alias_cmd (const char *name
, const char *oldname
,
333 enum command_class theclass
, int abbrev_flag
,
334 struct cmd_list_element
**list
)
337 struct cmd_list_element
*old
;
340 old
= lookup_cmd (&tmp
, *list
, "", 1, 1);
342 return add_alias_cmd (name
, old
, theclass
, abbrev_flag
, list
);
346 /* Update the prefix field of all sub-commands of the prefix command C.
347 We must do this when a prefix command is defined as the GDB init sequence
348 does not guarantee that a prefix command is created before its sub-commands.
349 For example, break-catch-sig.c initialization runs before breakpoint.c
350 initialization, but it is breakpoint.c that creates the "catch" command used
351 by the "catch signal" command created by break-catch-sig.c. */
354 update_prefix_field_of_prefixed_commands (struct cmd_list_element
*c
)
356 for (cmd_list_element
*p
= *c
->prefixlist
; p
!= NULL
; p
= p
->next
)
360 /* We must recursively update the prefix field to cover
361 e.g. 'info auto-load libthread-db' where the creation
366 In such a case, when 'auto-load' was created by do_add_cmd,
367 the 'libthread-db' prefix field could not be updated, as the
368 'auto-load' command was not yet reachable by
369 lookup_cmd_for_prefixlist (list, cmdlist)
370 that searches from the top level 'cmdlist'. */
371 if (p
->prefixlist
!= nullptr)
372 update_prefix_field_of_prefixed_commands (p
);
377 /* Like add_cmd but adds an element for a command prefix: a name that
378 should be followed by a subcommand to be looked up in another
379 command list. PREFIXLIST should be the address of the variable
380 containing that list. */
382 struct cmd_list_element
*
383 add_prefix_cmd (const char *name
, enum command_class theclass
,
384 cmd_const_cfunc_ftype
*fun
,
385 const char *doc
, struct cmd_list_element
**prefixlist
,
386 const char *prefixname
, int allow_unknown
,
387 struct cmd_list_element
**list
)
389 struct cmd_list_element
*c
= add_cmd (name
, theclass
, fun
, doc
, list
);
391 c
->prefixlist
= prefixlist
;
392 c
->prefixname
= prefixname
;
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
->cmd_pointer
!= nullptr)
412 help_list (*c
->prefixlist
, c
->prefixname
, 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
**prefixlist
,
420 const char *prefixname
, int allow_unknown
,
421 struct cmd_list_element
**list
)
423 struct cmd_list_element
*cmd
= add_prefix_cmd (name
, theclass
, nullptr,
424 doc
, prefixlist
, prefixname
,
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
->prefixlist
, 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
**prefixlist
,
444 const char *prefixname
, int allow_unknown
,
445 struct cmd_list_element
**list
)
447 struct cmd_list_element
*cmd
= add_prefix_cmd (name
, theclass
, nullptr,
448 doc
, prefixlist
, prefixname
,
449 allow_unknown
, list
);
450 set_cmd_sfunc (cmd
, do_show_prefix_cmd
);
454 /* Like ADD_PREFIX_CMD but sets the suppress_notification pointer on the
455 new command list element. */
457 struct cmd_list_element
*
458 add_prefix_cmd_suppress_notification
459 (const char *name
, enum command_class theclass
,
460 cmd_const_cfunc_ftype
*fun
,
461 const char *doc
, struct cmd_list_element
**prefixlist
,
462 const char *prefixname
, int allow_unknown
,
463 struct cmd_list_element
**list
,
464 int *suppress_notification
)
466 struct cmd_list_element
*element
467 = add_prefix_cmd (name
, theclass
, fun
, doc
, prefixlist
,
468 prefixname
, allow_unknown
, list
);
469 element
->suppress_notification
= suppress_notification
;
473 /* Like add_prefix_cmd but sets the abbrev_flag on the new command. */
475 struct cmd_list_element
*
476 add_abbrev_prefix_cmd (const char *name
, enum command_class theclass
,
477 cmd_const_cfunc_ftype
*fun
, const char *doc
,
478 struct cmd_list_element
**prefixlist
,
479 const char *prefixname
,
480 int allow_unknown
, struct cmd_list_element
**list
)
482 struct cmd_list_element
*c
= add_cmd (name
, theclass
, fun
, doc
, list
);
484 c
->prefixlist
= prefixlist
;
485 c
->prefixname
= prefixname
;
486 c
->allow_unknown
= allow_unknown
;
491 /* This is an empty "cfunc". */
493 not_just_help_class_command (const char *args
, int from_tty
)
497 /* This is an empty "sfunc". */
500 empty_sfunc (const char *args
, int from_tty
, struct cmd_list_element
*c
)
504 /* Add element named NAME to command list LIST (the list for set/show
505 or some sublist thereof).
506 TYPE is set_cmd or show_cmd.
507 CLASS is as in add_cmd.
508 VAR_TYPE is the kind of thing we are setting.
509 VAR is address of the variable being controlled by this command.
510 DOC is the documentation string. */
512 static struct cmd_list_element
*
513 add_set_or_show_cmd (const char *name
,
515 enum command_class theclass
,
519 struct cmd_list_element
**list
)
521 struct cmd_list_element
*c
= add_cmd (name
, theclass
, doc
, list
);
523 gdb_assert (type
== set_cmd
|| type
== show_cmd
);
525 c
->var_type
= var_type
;
527 /* This needs to be something besides NULL so that this isn't
528 treated as a help class. */
529 set_cmd_sfunc (c
, empty_sfunc
);
533 /* Add element named NAME to both the command SET_LIST and SHOW_LIST.
534 CLASS is as in add_cmd. VAR_TYPE is the kind of thing we are
535 setting. VAR is address of the variable being controlled by this
536 command. SET_FUNC and SHOW_FUNC are the callback functions (if
537 non-NULL). SET_DOC, SHOW_DOC and HELP_DOC are the documentation
538 strings. PRINT the format string to print the value. SET_RESULT
539 and SHOW_RESULT, if not NULL, are set to the resulting command
543 add_setshow_cmd_full (const char *name
,
544 enum command_class theclass
,
545 var_types var_type
, void *var
,
546 const char *set_doc
, const char *show_doc
,
547 const char *help_doc
,
548 cmd_const_sfunc_ftype
*set_func
,
549 show_value_ftype
*show_func
,
550 struct cmd_list_element
**set_list
,
551 struct cmd_list_element
**show_list
,
552 struct cmd_list_element
**set_result
,
553 struct cmd_list_element
**show_result
)
555 struct cmd_list_element
*set
;
556 struct cmd_list_element
*show
;
560 if (help_doc
!= NULL
)
562 full_set_doc
= xstrprintf ("%s\n%s", set_doc
, help_doc
);
563 full_show_doc
= xstrprintf ("%s\n%s", show_doc
, help_doc
);
567 full_set_doc
= xstrdup (set_doc
);
568 full_show_doc
= xstrdup (show_doc
);
570 set
= add_set_or_show_cmd (name
, set_cmd
, theclass
, var_type
, var
,
571 full_set_doc
, set_list
);
572 set
->doc_allocated
= 1;
574 if (set_func
!= NULL
)
575 set_cmd_sfunc (set
, set_func
);
577 show
= add_set_or_show_cmd (name
, show_cmd
, theclass
, var_type
, var
,
578 full_show_doc
, show_list
);
579 show
->doc_allocated
= 1;
580 show
->show_value_func
= show_func
;
581 /* Disable the default symbol completer. Doesn't make much sense
582 for the "show" command to complete on anything. */
583 set_cmd_completer (show
, nullptr);
585 if (set_result
!= NULL
)
587 if (show_result
!= NULL
)
591 /* Add element named NAME to command list LIST (the list for set or
592 some sublist thereof). CLASS is as in add_cmd. ENUMLIST is a list
593 of strings which may follow NAME. VAR is address of the variable
594 which will contain the matching string (from ENUMLIST). */
597 add_setshow_enum_cmd (const char *name
,
598 enum command_class theclass
,
599 const char *const *enumlist
,
602 const char *show_doc
,
603 const char *help_doc
,
604 cmd_const_sfunc_ftype
*set_func
,
605 show_value_ftype
*show_func
,
606 struct cmd_list_element
**set_list
,
607 struct cmd_list_element
**show_list
,
610 struct cmd_list_element
*c
, *show
;
612 add_setshow_cmd_full (name
, theclass
, var_enum
, var
,
613 set_doc
, show_doc
, help_doc
,
619 set_cmd_context (c
, context
);
620 set_cmd_context (show
, context
);
623 /* See cli-decode.h. */
624 const char * const auto_boolean_enums
[] = { "on", "off", "auto", NULL
};
626 /* Add an auto-boolean command named NAME to both the set and show
627 command list lists. CLASS is as in add_cmd. VAR is address of the
628 variable which will contain the value. DOC is the documentation
629 string. FUNC is the corresponding callback. */
631 add_setshow_auto_boolean_cmd (const char *name
,
632 enum command_class theclass
,
633 enum auto_boolean
*var
,
634 const char *set_doc
, const char *show_doc
,
635 const char *help_doc
,
636 cmd_const_sfunc_ftype
*set_func
,
637 show_value_ftype
*show_func
,
638 struct cmd_list_element
**set_list
,
639 struct cmd_list_element
**show_list
)
641 struct cmd_list_element
*c
;
643 add_setshow_cmd_full (name
, theclass
, var_auto_boolean
, var
,
644 set_doc
, show_doc
, help_doc
,
648 c
->enums
= auto_boolean_enums
;
651 /* See cli-decode.h. */
652 const char * const boolean_enums
[] = { "on", "off", NULL
};
654 /* Add element named NAME to both the set and show command LISTs (the
655 list for set/show or some sublist thereof). CLASS is as in
656 add_cmd. VAR is address of the variable which will contain the
657 value. SET_DOC and SHOW_DOC are the documentation strings.
658 Returns the new command element. */
661 add_setshow_boolean_cmd (const char *name
, enum command_class theclass
, bool *var
,
662 const char *set_doc
, const char *show_doc
,
663 const char *help_doc
,
664 cmd_const_sfunc_ftype
*set_func
,
665 show_value_ftype
*show_func
,
666 struct cmd_list_element
**set_list
,
667 struct cmd_list_element
**show_list
)
669 struct cmd_list_element
*c
;
671 add_setshow_cmd_full (name
, theclass
, var_boolean
, var
,
672 set_doc
, show_doc
, help_doc
,
676 c
->enums
= boolean_enums
;
681 /* Add element named NAME to both the set and show command LISTs (the
682 list for set/show or some sublist thereof). */
684 add_setshow_filename_cmd (const char *name
, enum command_class theclass
,
686 const char *set_doc
, const char *show_doc
,
687 const char *help_doc
,
688 cmd_const_sfunc_ftype
*set_func
,
689 show_value_ftype
*show_func
,
690 struct cmd_list_element
**set_list
,
691 struct cmd_list_element
**show_list
)
693 struct cmd_list_element
*set_result
;
695 add_setshow_cmd_full (name
, theclass
, var_filename
, var
,
696 set_doc
, show_doc
, help_doc
,
700 set_cmd_completer (set_result
, filename_completer
);
703 /* Add element named NAME to both the set and show command LISTs (the
704 list for set/show or some sublist thereof). */
706 add_setshow_string_cmd (const char *name
, enum command_class theclass
,
708 const char *set_doc
, const char *show_doc
,
709 const char *help_doc
,
710 cmd_const_sfunc_ftype
*set_func
,
711 show_value_ftype
*show_func
,
712 struct cmd_list_element
**set_list
,
713 struct cmd_list_element
**show_list
)
715 cmd_list_element
*set_cmd
;
717 add_setshow_cmd_full (name
, theclass
, var_string
, var
,
718 set_doc
, show_doc
, help_doc
,
723 /* Disable the default symbol completer. */
724 set_cmd_completer (set_cmd
, nullptr);
727 /* Add element named NAME to both the set and show command LISTs (the
728 list for set/show or some sublist thereof). */
729 struct cmd_list_element
*
730 add_setshow_string_noescape_cmd (const char *name
, enum command_class theclass
,
732 const char *set_doc
, const char *show_doc
,
733 const char *help_doc
,
734 cmd_const_sfunc_ftype
*set_func
,
735 show_value_ftype
*show_func
,
736 struct cmd_list_element
**set_list
,
737 struct cmd_list_element
**show_list
)
739 struct cmd_list_element
*set_cmd
;
741 add_setshow_cmd_full (name
, theclass
, var_string_noescape
, var
,
742 set_doc
, show_doc
, help_doc
,
747 /* Disable the default symbol completer. */
748 set_cmd_completer (set_cmd
, nullptr);
753 /* Add element named NAME to both the set and show command LISTs (the
754 list for set/show or some sublist thereof). */
756 add_setshow_optional_filename_cmd (const char *name
, enum command_class theclass
,
758 const char *set_doc
, const char *show_doc
,
759 const char *help_doc
,
760 cmd_const_sfunc_ftype
*set_func
,
761 show_value_ftype
*show_func
,
762 struct cmd_list_element
**set_list
,
763 struct cmd_list_element
**show_list
)
765 struct cmd_list_element
*set_result
;
767 add_setshow_cmd_full (name
, theclass
, var_optional_filename
, var
,
768 set_doc
, show_doc
, help_doc
,
773 set_cmd_completer (set_result
, filename_completer
);
777 /* Completes on literal "unlimited". Used by integer commands that
778 support a special "unlimited" value. */
781 integer_unlimited_completer (struct cmd_list_element
*ignore
,
782 completion_tracker
&tracker
,
783 const char *text
, const char *word
)
785 static const char * const keywords
[] =
791 complete_on_enum (tracker
, keywords
, text
, word
);
794 /* Add element named NAME to both the set and show command LISTs (the
795 list for set/show or some sublist thereof). CLASS is as in
796 add_cmd. VAR is address of the variable which will contain the
797 value. SET_DOC and SHOW_DOC are the documentation strings. This
798 function is only used in Python API. Please don't use it elsewhere. */
800 add_setshow_integer_cmd (const char *name
, enum command_class theclass
,
802 const char *set_doc
, const char *show_doc
,
803 const char *help_doc
,
804 cmd_const_sfunc_ftype
*set_func
,
805 show_value_ftype
*show_func
,
806 struct cmd_list_element
**set_list
,
807 struct cmd_list_element
**show_list
)
809 struct cmd_list_element
*set
;
811 add_setshow_cmd_full (name
, theclass
, var_integer
, var
,
812 set_doc
, show_doc
, help_doc
,
817 set_cmd_completer (set
, integer_unlimited_completer
);
820 /* Add element named NAME to both the set and show command LISTs (the
821 list for set/show or some sublist thereof). CLASS is as in
822 add_cmd. VAR is address of the variable which will contain the
823 value. SET_DOC and SHOW_DOC are the documentation strings. */
825 add_setshow_uinteger_cmd (const char *name
, enum command_class theclass
,
827 const char *set_doc
, const char *show_doc
,
828 const char *help_doc
,
829 cmd_const_sfunc_ftype
*set_func
,
830 show_value_ftype
*show_func
,
831 struct cmd_list_element
**set_list
,
832 struct cmd_list_element
**show_list
)
834 struct cmd_list_element
*set
;
836 add_setshow_cmd_full (name
, theclass
, var_uinteger
, var
,
837 set_doc
, show_doc
, help_doc
,
842 set_cmd_completer (set
, integer_unlimited_completer
);
845 /* Add element named NAME to both the set and show command LISTs (the
846 list for set/show or some sublist thereof). CLASS is as in
847 add_cmd. VAR is address of the variable which will contain the
848 value. SET_DOC and SHOW_DOC are the documentation strings. */
850 add_setshow_zinteger_cmd (const char *name
, enum command_class theclass
,
852 const char *set_doc
, const char *show_doc
,
853 const char *help_doc
,
854 cmd_const_sfunc_ftype
*set_func
,
855 show_value_ftype
*show_func
,
856 struct cmd_list_element
**set_list
,
857 struct cmd_list_element
**show_list
)
859 add_setshow_cmd_full (name
, theclass
, var_zinteger
, var
,
860 set_doc
, show_doc
, help_doc
,
867 add_setshow_zuinteger_unlimited_cmd (const char *name
,
868 enum command_class theclass
,
871 const char *show_doc
,
872 const char *help_doc
,
873 cmd_const_sfunc_ftype
*set_func
,
874 show_value_ftype
*show_func
,
875 struct cmd_list_element
**set_list
,
876 struct cmd_list_element
**show_list
)
878 struct cmd_list_element
*set
;
880 add_setshow_cmd_full (name
, theclass
, var_zuinteger_unlimited
, var
,
881 set_doc
, show_doc
, help_doc
,
886 set_cmd_completer (set
, integer_unlimited_completer
);
889 /* Add element named NAME to both the set and show command LISTs (the
890 list for set/show or some sublist thereof). CLASS is as in
891 add_cmd. VAR is address of the variable which will contain the
892 value. SET_DOC and SHOW_DOC are the documentation strings. */
894 add_setshow_zuinteger_cmd (const char *name
, enum command_class theclass
,
896 const char *set_doc
, const char *show_doc
,
897 const char *help_doc
,
898 cmd_const_sfunc_ftype
*set_func
,
899 show_value_ftype
*show_func
,
900 struct cmd_list_element
**set_list
,
901 struct cmd_list_element
**show_list
)
903 add_setshow_cmd_full (name
, theclass
, var_zuinteger
, var
,
904 set_doc
, show_doc
, help_doc
,
910 /* Remove the command named NAME from the command list. Return the
911 list commands which were aliased to the deleted command. If the
912 command had no aliases, return NULL. The various *HOOKs are set to
913 the pre- and post-hook commands for the deleted command. If the
914 command does not have a hook, the corresponding out parameter is
917 static struct cmd_list_element
*
918 delete_cmd (const char *name
, struct cmd_list_element
**list
,
919 struct cmd_list_element
**prehook
,
920 struct cmd_list_element
**prehookee
,
921 struct cmd_list_element
**posthook
,
922 struct cmd_list_element
**posthookee
)
924 struct cmd_list_element
*iter
;
925 struct cmd_list_element
**previous_chain_ptr
;
926 struct cmd_list_element
*aliases
= NULL
;
932 previous_chain_ptr
= list
;
934 for (iter
= *previous_chain_ptr
; iter
; iter
= *previous_chain_ptr
)
936 if (strcmp (iter
->name
, name
) == 0)
939 iter
->destroyer (iter
, iter
->context
);
940 if (iter
->hookee_pre
)
941 iter
->hookee_pre
->hook_pre
= 0;
942 *prehook
= iter
->hook_pre
;
943 *prehookee
= iter
->hookee_pre
;
944 if (iter
->hookee_post
)
945 iter
->hookee_post
->hook_post
= 0;
946 *posthook
= iter
->hook_post
;
947 *posthookee
= iter
->hookee_post
;
949 /* Update the link. */
950 *previous_chain_ptr
= iter
->next
;
952 aliases
= iter
->aliases
;
954 /* If this command was an alias, remove it from the list of
956 if (iter
->cmd_pointer
)
958 struct cmd_list_element
**prevp
= &iter
->cmd_pointer
->aliases
;
959 struct cmd_list_element
*a
= *prevp
;
963 prevp
= &a
->alias_chain
;
966 *prevp
= iter
->alias_chain
;
971 /* We won't see another command with the same name. */
975 previous_chain_ptr
= &iter
->next
;
981 /* Shorthands to the commands above. */
983 /* Add an element to the list of info subcommands. */
985 struct cmd_list_element
*
986 add_info (const char *name
, cmd_const_cfunc_ftype
*fun
, const char *doc
)
988 return add_cmd (name
, class_info
, fun
, doc
, &infolist
);
991 /* Add an alias to the list of info subcommands. */
993 struct cmd_list_element
*
994 add_info_alias (const char *name
, const char *oldname
, int abbrev_flag
)
996 return add_alias_cmd (name
, oldname
, class_run
, abbrev_flag
, &infolist
);
999 /* Add an element to the list of commands. */
1001 struct cmd_list_element
*
1002 add_com (const char *name
, enum command_class theclass
,
1003 cmd_const_cfunc_ftype
*fun
,
1006 return add_cmd (name
, theclass
, fun
, doc
, &cmdlist
);
1009 /* Add an alias or abbreviation command to the list of commands. */
1011 struct cmd_list_element
*
1012 add_com_alias (const char *name
, const char *oldname
, enum command_class theclass
,
1015 return add_alias_cmd (name
, oldname
, theclass
, abbrev_flag
, &cmdlist
);
1018 /* Add an element with a suppress notification to the list of commands. */
1020 struct cmd_list_element
*
1021 add_com_suppress_notification (const char *name
, enum command_class theclass
,
1022 cmd_const_cfunc_ftype
*fun
, const char *doc
,
1023 int *suppress_notification
)
1025 return add_cmd_suppress_notification (name
, theclass
, fun
, doc
,
1026 &cmdlist
, suppress_notification
);
1029 /* Print the prefix of C followed by name of C in title style. */
1032 fput_command_name_styled (struct cmd_list_element
*c
, struct ui_file
*stream
)
1034 const char *prefixname
1035 = c
->prefix
== nullptr ? "" : c
->prefix
->prefixname
;
1037 fprintf_styled (stream
, title_style
.style (), "%s%s", prefixname
, c
->name
);
1040 /* If C has one or more aliases, style print the name of C and
1041 the name of its aliases, separated by commas.
1042 If ALWAYS_FPUT_C_NAME, print the name of C even if it has no aliases.
1043 If one or more names are printed, POSTFIX is printed after the last name.
1047 fput_command_names_styled (struct cmd_list_element
*c
,
1048 bool always_fput_c_name
, const char *postfix
,
1049 struct ui_file
*stream
)
1051 if (always_fput_c_name
|| c
->aliases
!= nullptr)
1052 fput_command_name_styled (c
, stream
);
1053 if (c
->aliases
!= nullptr)
1055 for (cmd_list_element
*iter
= c
->aliases
; iter
; iter
= iter
->alias_chain
)
1057 fputs_filtered (", ", stream
);
1059 fput_command_name_styled (iter
, stream
);
1062 if (always_fput_c_name
|| c
->aliases
!= nullptr)
1063 fputs_filtered (postfix
, stream
);
1066 /* If VERBOSE, print the full help for command C and highlight the
1067 documentation parts matching HIGHLIGHT,
1068 otherwise print only one-line help for command C. */
1071 print_doc_of_command (struct cmd_list_element
*c
, const char *prefix
,
1072 bool verbose
, compiled_regex
&highlight
,
1073 struct ui_file
*stream
)
1075 /* When printing the full documentation, add a line to separate
1076 this documentation from the previous command help, in the likely
1077 case that apropos finds several commands. */
1079 fputs_filtered ("\n", stream
);
1081 fprintf_styled (stream
, title_style
.style (),
1082 "%s%s", prefix
, c
->name
);
1083 fputs_filtered (" -- ", stream
);
1085 fputs_highlighted (c
->doc
, highlight
, stream
);
1087 print_doc_line (stream
, c
->doc
, false);
1088 fputs_filtered ("\n", stream
);
1091 /* Recursively walk the commandlist structures, and print out the
1092 documentation of commands that match our regex in either their
1093 name, or their documentation.
1094 If VERBOSE, prints the complete documentation and highlight the
1095 documentation parts matching REGEX, otherwise prints only
1099 apropos_cmd (struct ui_file
*stream
,
1100 struct cmd_list_element
*commandlist
,
1101 bool verbose
, compiled_regex
®ex
, const char *prefix
)
1103 struct cmd_list_element
*c
;
1106 /* Walk through the commands. */
1107 for (c
=commandlist
;c
;c
=c
->next
)
1109 returnvalue
= -1; /* Needed to avoid double printing. */
1110 if (c
->name
!= NULL
)
1112 size_t name_len
= strlen (c
->name
);
1114 /* Try to match against the name. */
1115 returnvalue
= regex
.search (c
->name
, name_len
, 0, name_len
, NULL
);
1116 if (returnvalue
>= 0)
1117 print_doc_of_command (c
, prefix
, verbose
, regex
, stream
);
1119 if (c
->doc
!= NULL
&& returnvalue
< 0)
1121 size_t doc_len
= strlen (c
->doc
);
1123 /* Try to match against documentation. */
1124 if (regex
.search (c
->doc
, doc_len
, 0, doc_len
, NULL
) >= 0)
1125 print_doc_of_command (c
, prefix
, verbose
, regex
, stream
);
1127 /* Check if this command has subcommands and is not an
1128 abbreviation. We skip listing subcommands of abbreviations
1129 in order to avoid duplicates in the output. */
1130 if (c
->prefixlist
!= NULL
&& !c
->abbrev_flag
)
1132 /* Recursively call ourselves on the subcommand list,
1133 passing the right prefix in. */
1134 apropos_cmd (stream
, *c
->prefixlist
, verbose
, regex
, c
->prefixname
);
1139 /* This command really has to deal with two things:
1140 1) I want documentation on *this string* (usually called by
1141 "help commandname").
1143 2) I want documentation on *this list* (usually called by giving a
1144 command that requires subcommands. Also called by saying just
1147 I am going to split this into two separate commands, help_cmd and
1151 help_cmd (const char *command
, struct ui_file
*stream
)
1153 struct cmd_list_element
*c
;
1157 help_list (cmdlist
, "", all_classes
, stream
);
1161 if (strcmp (command
, "all") == 0)
1167 c
= lookup_cmd (&command
, cmdlist
, "", 0, 0);
1172 /* There are three cases here.
1173 If c->prefixlist is nonzero, we have a prefix command.
1174 Print its documentation, then list its subcommands.
1176 If c->func is non NULL, we really have a command. Print its
1177 documentation and return.
1179 If c->func is NULL, we have a class name. Print its
1180 documentation (as if it were a command) and then set class to the
1181 number of this class so that the commands in the class will be
1184 fputs_filtered (c
->doc
, stream
);
1185 fputs_filtered ("\n", stream
);
1187 if (c
->prefixlist
== 0 && c
->func
!= NULL
)
1189 fprintf_filtered (stream
, "\n");
1191 /* If this is a prefix command, print it's subcommands. */
1193 help_list (*c
->prefixlist
, c
->prefixname
, all_commands
, stream
);
1195 /* If this is a class name, print all of the commands in the class. */
1196 if (c
->func
== NULL
)
1197 help_list (cmdlist
, "", c
->theclass
, stream
);
1199 if (c
->hook_pre
|| c
->hook_post
)
1200 fprintf_filtered (stream
,
1201 "\nThis command has a hook (or hooks) defined:\n");
1204 fprintf_filtered (stream
,
1205 "\tThis command is run after : %s (pre hook)\n",
1208 fprintf_filtered (stream
,
1209 "\tThis command is run before : %s (post hook)\n",
1210 c
->hook_post
->name
);
1214 * Get a specific kind of help on a command list.
1217 * CMDTYPE is the prefix to use in the title string.
1218 * CLASS is the class with which to list the nodes of this list (see
1219 * documentation for help_cmd_list below), As usual, ALL_COMMANDS for
1220 * everything, ALL_CLASSES for just classes, and non-negative for only things
1221 * in a specific class.
1222 * and STREAM is the output stream on which to print things.
1223 * If you call this routine with a class >= 0, it recurses.
1226 help_list (struct cmd_list_element
*list
, const char *cmdtype
,
1227 enum command_class theclass
, struct ui_file
*stream
)
1230 char *cmdtype1
, *cmdtype2
;
1232 /* If CMDTYPE is "foo ", CMDTYPE1 gets " foo" and CMDTYPE2 gets "foo sub".
1234 len
= strlen (cmdtype
);
1235 cmdtype1
= (char *) alloca (len
+ 1);
1237 cmdtype2
= (char *) alloca (len
+ 4);
1242 memcpy (cmdtype1
+ 1, cmdtype
, len
- 1);
1244 memcpy (cmdtype2
, cmdtype
, len
- 1);
1245 strcpy (cmdtype2
+ len
- 1, " sub");
1248 if (theclass
== all_classes
)
1249 fprintf_filtered (stream
, "List of classes of %scommands:\n\n", cmdtype2
);
1251 fprintf_filtered (stream
, "List of %scommands:\n\n", cmdtype2
);
1253 help_cmd_list (list
, theclass
, theclass
>= 0, stream
);
1255 if (theclass
== all_classes
)
1257 fprintf_filtered (stream
, "\n\
1258 Type \"help%s\" followed by a class name for a list of commands in ",
1261 fprintf_filtered (stream
, "that class.");
1263 fprintf_filtered (stream
, "\n\
1264 Type \"help all\" for the list of all commands.");
1267 fprintf_filtered (stream
, "\nType \"help%s\" followed by %scommand name ",
1268 cmdtype1
, cmdtype2
);
1270 fputs_filtered ("for ", stream
);
1272 fputs_filtered ("full ", stream
);
1274 fputs_filtered ("documentation.\n", stream
);
1275 fputs_filtered ("Type \"apropos word\" to search "
1276 "for commands related to \"word\".\n", stream
);
1277 fputs_filtered ("Type \"apropos -v word\" for full documentation", stream
);
1279 fputs_filtered (" of commands related to \"word\".\n", stream
);
1280 fputs_filtered ("Command name abbreviations are allowed if unambiguous.\n",
1285 help_all (struct ui_file
*stream
)
1287 struct cmd_list_element
*c
;
1288 int seen_unclassified
= 0;
1290 for (c
= cmdlist
; c
; c
= c
->next
)
1294 /* If this is a class name, print all of the commands in the
1297 if (c
->func
== NULL
)
1299 fprintf_filtered (stream
, "\nCommand class: %s\n\n", c
->name
);
1300 help_cmd_list (cmdlist
, c
->theclass
, true, stream
);
1304 /* While it's expected that all commands are in some class,
1305 as a safety measure, we'll print commands outside of any
1306 class at the end. */
1308 for (c
= cmdlist
; c
; c
= c
->next
)
1313 if (c
->theclass
== no_class
)
1315 if (!seen_unclassified
)
1317 fprintf_filtered (stream
, "\nUnclassified commands\n\n");
1318 seen_unclassified
= 1;
1320 print_help_for_command (c
, 1, stream
);
1326 /* See cli-decode.h. */
1329 print_doc_line (struct ui_file
*stream
, const char *str
,
1330 bool for_value_prefix
)
1332 static char *line_buffer
= 0;
1333 static int line_size
;
1339 line_buffer
= (char *) xmalloc (line_size
);
1342 /* Searches for the first end of line or the end of STR. */
1344 while (*p
&& *p
!= '\n')
1346 if (p
- str
> line_size
- 1)
1348 line_size
= p
- str
+ 1;
1349 xfree (line_buffer
);
1350 line_buffer
= (char *) xmalloc (line_size
);
1352 strncpy (line_buffer
, str
, p
- str
);
1353 if (for_value_prefix
)
1355 if (islower (line_buffer
[0]))
1356 line_buffer
[0] = toupper (line_buffer
[0]);
1357 gdb_assert (p
> str
);
1358 if (line_buffer
[p
- str
- 1] == '.')
1359 line_buffer
[p
- str
- 1] = '\0';
1361 line_buffer
[p
- str
] = '\0';
1364 line_buffer
[p
- str
] = '\0';
1365 fputs_filtered (line_buffer
, stream
);
1368 /* Print one-line help for command C.
1369 If RECURSE is non-zero, also print one-line descriptions
1370 of all prefixed subcommands. */
1372 print_help_for_command (struct cmd_list_element
*c
,
1373 bool recurse
, struct ui_file
*stream
)
1375 fput_command_names_styled (c
, true, " -- ", stream
);
1376 print_doc_line (stream
, c
->doc
, false);
1377 fputs_filtered ("\n", stream
);
1380 && c
->prefixlist
!= 0
1381 && c
->abbrev_flag
== 0)
1382 /* Subcommands of a prefix command typically have 'all_commands'
1383 as class. If we pass CLASS to recursive invocation,
1384 most often we won't see anything. */
1385 help_cmd_list (*c
->prefixlist
, all_commands
, true, stream
);
1389 * Implement a help command on command list LIST.
1390 * RECURSE should be non-zero if this should be done recursively on
1391 * all sublists of LIST.
1392 * STREAM is the stream upon which the output should be written.
1393 * THECLASS should be:
1394 * A non-negative class number to list only commands in that
1395 * ALL_COMMANDS to list all commands in list.
1396 * ALL_CLASSES to list all classes in list.
1398 * Note that aliases are only shown when THECLASS is class_alias.
1399 * In the other cases, the aliases will be shown together with their
1402 * Note that RECURSE will be active on *all* sublists, not just the
1403 * ones selected by the criteria above (ie. the selection mechanism
1404 * is at the low level, not the high-level).
1408 help_cmd_list (struct cmd_list_element
*list
, enum command_class theclass
,
1409 bool recurse
, struct ui_file
*stream
)
1411 struct cmd_list_element
*c
;
1413 for (c
= list
; c
; c
= c
->next
)
1415 if (c
->abbrev_flag
== 1 || c
->cmd_deprecated
)
1417 /* Do not show abbreviations or deprecated commands. */
1421 if (c
->cmd_pointer
!= nullptr && theclass
!= class_alias
)
1423 /* Do not show an alias, unless specifically showing the
1424 list of aliases: for all other classes, an alias is
1425 shown (if needed) together with its aliased command. */
1429 if (theclass
== all_commands
1430 || (theclass
== all_classes
&& c
->func
== NULL
)
1431 || (theclass
== c
->theclass
&& c
->func
!= NULL
))
1434 - showing all commands
1435 - showing all classes and C is a help class
1436 - showing commands of THECLASS and C is not the help class */
1438 /* If we show the class_alias and C is an alias, do not recurse,
1439 as this would show the (possibly very long) not very useful
1440 list of sub-commands of the aliased command. */
1441 print_help_for_command
1443 recurse
&& (theclass
!= class_alias
|| c
->cmd_pointer
== nullptr),
1449 && (theclass
== class_user
|| theclass
== class_alias
)
1450 && c
->prefixlist
!= NULL
)
1452 /* User-defined commands or aliases may be subcommands. */
1453 help_cmd_list (*c
->prefixlist
, theclass
, recurse
, stream
);
1457 /* Do not show C or recurse on C, e.g. because C does not belong to
1458 THECLASS or because C is a help class. */
1463 /* Search the input clist for 'command'. Return the command if
1464 found (or NULL if not), and return the number of commands
1467 static struct cmd_list_element
*
1468 find_cmd (const char *command
, int len
, struct cmd_list_element
*clist
,
1469 int ignore_help_classes
, int *nfound
)
1471 struct cmd_list_element
*found
, *c
;
1475 for (c
= clist
; c
; c
= c
->next
)
1476 if (!strncmp (command
, c
->name
, len
)
1477 && (!ignore_help_classes
|| c
->func
))
1481 if (c
->name
[len
] == '\0')
1490 /* Return the length of command name in TEXT. */
1493 find_command_name_length (const char *text
)
1495 const char *p
= text
;
1497 /* Treating underscores as part of command words is important
1498 so that "set args_foo()" doesn't get interpreted as
1499 "set args _foo()". */
1500 /* Some characters are only used for TUI specific commands.
1501 However, they are always allowed for the sake of consistency.
1503 Note that this is larger than the character set allowed when
1504 creating user-defined commands. */
1506 /* Recognize the single character commands so that, e.g., "!ls"
1507 works as expected. */
1508 if (*p
== '!' || *p
== '|')
1511 while (valid_cmd_char_p (*p
)
1512 /* Characters used by TUI specific commands. */
1513 || *p
== '+' || *p
== '<' || *p
== '>' || *p
== '$')
1519 /* See command.h. */
1522 valid_cmd_char_p (int c
)
1524 /* Alas "42" is a legitimate user-defined command.
1525 In the interests of not breaking anything we preserve that. */
1527 return isalnum (c
) || c
== '-' || c
== '_' || c
== '.';
1530 /* See command.h. */
1533 valid_user_defined_cmd_name_p (const char *name
)
1540 for (p
= name
; *p
!= '\0'; ++p
)
1542 if (valid_cmd_char_p (*p
))
1551 /* This routine takes a line of TEXT and a CLIST in which to start the
1552 lookup. When it returns it will have incremented the text pointer past
1553 the section of text it matched, set *RESULT_LIST to point to the list in
1554 which the last word was matched, and will return a pointer to the cmd
1555 list element which the text matches. It will return NULL if no match at
1556 all was possible. It will return -1 (cast appropriately, ick) if ambigous
1557 matches are possible; in this case *RESULT_LIST will be set to point to
1558 the list in which there are ambiguous choices (and *TEXT will be set to
1559 the ambiguous text string).
1561 If the located command was an abbreviation, this routine returns the base
1562 command of the abbreviation.
1564 It does no error reporting whatsoever; control will always return
1565 to the superior routine.
1567 In the case of an ambiguous return (-1), *RESULT_LIST will be set to point
1568 at the prefix_command (ie. the best match) *or* (special case) will be NULL
1569 if no prefix command was ever found. For example, in the case of "info a",
1570 "info" matches without ambiguity, but "a" could be "args" or "address", so
1571 *RESULT_LIST is set to the cmd_list_element for "info". So in this case
1572 RESULT_LIST should not be interpreted as a pointer to the beginning of a
1573 list; it simply points to a specific command. In the case of an ambiguous
1574 return *TEXT is advanced past the last non-ambiguous prefix (e.g.
1575 "info t" can be "info types" or "info target"; upon return *TEXT has been
1576 advanced past "info ").
1578 If RESULT_LIST is NULL, don't set *RESULT_LIST (but don't otherwise
1579 affect the operation).
1581 This routine does *not* modify the text pointed to by TEXT.
1583 If IGNORE_HELP_CLASSES is nonzero, ignore any command list elements which
1584 are actually help classes rather than commands (i.e. the function field of
1585 the struct cmd_list_element is NULL). */
1587 struct cmd_list_element
*
1588 lookup_cmd_1 (const char **text
, struct cmd_list_element
*clist
,
1589 struct cmd_list_element
**result_list
, int ignore_help_classes
)
1593 struct cmd_list_element
*found
, *c
;
1594 const char *line
= *text
;
1596 while (**text
== ' ' || **text
== '\t')
1599 /* Identify the name of the command. */
1600 len
= find_command_name_length (*text
);
1602 /* If nothing but whitespace, return 0. */
1606 /* *text and p now bracket the first command word to lookup (and
1607 it's length is len). We copy this into a local temporary. */
1610 command
= (char *) alloca (len
+ 1);
1611 memcpy (command
, *text
, len
);
1612 command
[len
] = '\0';
1617 found
= find_cmd (command
, len
, clist
, ignore_help_classes
, &nfound
);
1619 /* If nothing matches, we have a simple failure. */
1625 if (result_list
!= NULL
)
1626 /* Will be modified in calling routine
1627 if we know what the prefix command is. */
1629 return CMD_LIST_AMBIGUOUS
; /* Ambiguous. */
1632 /* We've matched something on this list. Move text pointer forward. */
1636 if (found
->cmd_pointer
)
1638 /* We drop the alias (abbreviation) in favor of the command it
1639 is pointing to. If the alias is deprecated, though, we need to
1640 warn the user about it before we drop it. Note that while we
1641 are warning about the alias, we may also warn about the command
1642 itself and we will adjust the appropriate DEPRECATED_WARN_USER
1645 if (found
->deprecated_warn_user
)
1646 deprecated_cmd_warning (line
);
1647 found
= found
->cmd_pointer
;
1649 /* If we found a prefix command, keep looking. */
1651 if (found
->prefixlist
)
1653 c
= lookup_cmd_1 (text
, *found
->prefixlist
, result_list
,
1654 ignore_help_classes
);
1657 /* Didn't find anything; this is as far as we got. */
1658 if (result_list
!= NULL
)
1659 *result_list
= clist
;
1662 else if (c
== CMD_LIST_AMBIGUOUS
)
1664 /* We've gotten this far properly, but the next step is
1665 ambiguous. We need to set the result list to the best
1666 we've found (if an inferior hasn't already set it). */
1667 if (result_list
!= NULL
)
1669 /* This used to say *result_list = *found->prefixlist.
1670 If that was correct, need to modify the documentation
1671 at the top of this function to clarify what is
1672 supposed to be going on. */
1673 *result_list
= found
;
1684 if (result_list
!= NULL
)
1685 *result_list
= clist
;
1690 /* All this hair to move the space to the front of cmdtype */
1693 undef_cmd_error (const char *cmdtype
, const char *q
)
1695 error (_("Undefined %scommand: \"%s\". Try \"help%s%.*s\"."),
1698 *cmdtype
? " " : "",
1699 (int) strlen (cmdtype
) - 1,
1703 /* Look up the contents of *LINE as a command in the command list LIST.
1704 LIST is a chain of struct cmd_list_element's.
1705 If it is found, return the struct cmd_list_element for that command
1706 and update *LINE to point after the command name, at the first argument.
1707 If not found, call error if ALLOW_UNKNOWN is zero
1708 otherwise (or if error returns) return zero.
1709 Call error if specified command is ambiguous,
1710 unless ALLOW_UNKNOWN is negative.
1711 CMDTYPE precedes the word "command" in the error message.
1713 If IGNORE_HELP_CLASSES is nonzero, ignore any command list
1714 elements which are actually help classes rather than commands (i.e.
1715 the function field of the struct cmd_list_element is 0). */
1717 struct cmd_list_element
*
1718 lookup_cmd (const char **line
, struct cmd_list_element
*list
,
1719 const char *cmdtype
,
1720 int allow_unknown
, int ignore_help_classes
)
1722 struct cmd_list_element
*last_list
= 0;
1723 struct cmd_list_element
*c
;
1725 /* Note: Do not remove trailing whitespace here because this
1726 would be wrong for complete_command. Jim Kingdon */
1729 error (_("Lack of needed %scommand"), cmdtype
);
1731 c
= lookup_cmd_1 (line
, list
, &last_list
, ignore_help_classes
);
1738 int len
= find_command_name_length (*line
);
1740 q
= (char *) alloca (len
+ 1);
1741 strncpy (q
, *line
, len
);
1743 undef_cmd_error (cmdtype
, q
);
1748 else if (c
== CMD_LIST_AMBIGUOUS
)
1750 /* Ambigous. Local values should be off prefixlist or called
1752 int local_allow_unknown
= (last_list
? last_list
->allow_unknown
:
1754 const char *local_cmdtype
= last_list
? last_list
->prefixname
: cmdtype
;
1755 struct cmd_list_element
*local_list
=
1756 (last_list
? *(last_list
->prefixlist
) : list
);
1758 if (local_allow_unknown
< 0)
1761 return last_list
; /* Found something. */
1763 return 0; /* Found nothing. */
1767 /* Report as error. */
1772 ((*line
)[amb_len
] && (*line
)[amb_len
] != ' '
1773 && (*line
)[amb_len
] != '\t');
1778 for (c
= local_list
; c
; c
= c
->next
)
1779 if (!strncmp (*line
, c
->name
, amb_len
))
1781 if (strlen (ambbuf
) + strlen (c
->name
) + 6
1782 < (int) sizeof ambbuf
)
1784 if (strlen (ambbuf
))
1785 strcat (ambbuf
, ", ");
1786 strcat (ambbuf
, c
->name
);
1790 strcat (ambbuf
, "..");
1794 error (_("Ambiguous %scommand \"%s\": %s."), local_cmdtype
,
1800 if (c
->type
== set_cmd
&& **line
!= '\0' && !isspace (**line
))
1801 error (_("Argument must be preceded by space."));
1803 /* We've got something. It may still not be what the caller
1804 wants (if this command *needs* a subcommand). */
1805 while (**line
== ' ' || **line
== '\t')
1808 if (c
->prefixlist
&& **line
&& !c
->allow_unknown
)
1809 undef_cmd_error (c
->prefixname
, *line
);
1811 /* Seems to be what he wants. Return it. */
1817 /* We are here presumably because an alias or command in TEXT is
1818 deprecated and a warning message should be generated. This
1819 function decodes TEXT and potentially generates a warning message
1822 Example for 'set endian big' which has a fictitious alias 'seb'.
1824 If alias wasn't used in TEXT, and the command is deprecated:
1825 "warning: 'set endian big' is deprecated."
1827 If alias was used, and only the alias is deprecated:
1828 "warning: 'seb' an alias for the command 'set endian big' is deprecated."
1830 If alias was used and command is deprecated (regardless of whether
1831 the alias itself is deprecated:
1833 "warning: 'set endian big' (seb) is deprecated."
1835 After the message has been sent, clear the appropriate flags in the
1836 command and/or the alias so the user is no longer bothered.
1840 deprecated_cmd_warning (const char *text
)
1842 struct cmd_list_element
*alias
= NULL
;
1843 struct cmd_list_element
*prefix_cmd
= NULL
;
1844 struct cmd_list_element
*cmd
= NULL
;
1846 if (!lookup_cmd_composition (text
, &alias
, &prefix_cmd
, &cmd
))
1847 /* Return if text doesn't evaluate to a command. */
1850 if (!((alias
? alias
->deprecated_warn_user
: 0)
1851 || cmd
->deprecated_warn_user
) )
1852 /* Return if nothing is deprecated. */
1855 printf_filtered ("Warning:");
1857 if (alias
&& !cmd
->cmd_deprecated
)
1858 printf_filtered (" '%s', an alias for the", alias
->name
);
1860 printf_filtered (" command '");
1863 printf_filtered ("%s", prefix_cmd
->prefixname
);
1865 printf_filtered ("%s", cmd
->name
);
1867 if (alias
&& cmd
->cmd_deprecated
)
1868 printf_filtered ("' (%s) is deprecated.\n", alias
->name
);
1870 printf_filtered ("' is deprecated.\n");
1873 /* If it is only the alias that is deprecated, we want to indicate
1874 the new alias, otherwise we'll indicate the new command. */
1876 if (alias
&& !cmd
->cmd_deprecated
)
1878 if (alias
->replacement
)
1879 printf_filtered ("Use '%s'.\n\n", alias
->replacement
);
1881 printf_filtered ("No alternative known.\n\n");
1885 if (cmd
->replacement
)
1886 printf_filtered ("Use '%s'.\n\n", cmd
->replacement
);
1888 printf_filtered ("No alternative known.\n\n");
1891 /* We've warned you, now we'll keep quiet. */
1893 alias
->deprecated_warn_user
= 0;
1895 cmd
->deprecated_warn_user
= 0;
1899 /* Look up the contents of TEXT as a command in the command list 'cmdlist'.
1900 Return 1 on success, 0 on failure.
1902 If TEXT refers to an alias, *ALIAS will point to that alias.
1904 If TEXT is a subcommand (i.e. one that is preceded by a prefix
1905 command) set *PREFIX_CMD.
1907 Set *CMD to point to the command TEXT indicates.
1909 If any of *ALIAS, *PREFIX_CMD, or *CMD cannot be determined or do not
1910 exist, they are NULL when we return.
1914 lookup_cmd_composition (const char *text
,
1915 struct cmd_list_element
**alias
,
1916 struct cmd_list_element
**prefix_cmd
,
1917 struct cmd_list_element
**cmd
)
1921 struct cmd_list_element
*cur_list
;
1922 struct cmd_list_element
*prev_cmd
;
1930 text
= skip_spaces (text
);
1934 /* Go through as many command lists as we need to,
1935 to find the command TEXT refers to. */
1939 /* Identify the name of the command. */
1940 len
= find_command_name_length (text
);
1942 /* If nothing but whitespace, return. */
1946 /* TEXT is the start of the first command word to lookup (and
1947 it's length is LEN). We copy this into a local temporary. */
1949 command
= (char *) alloca (len
+ 1);
1950 memcpy (command
, text
, len
);
1951 command
[len
] = '\0';
1956 *cmd
= find_cmd (command
, len
, cur_list
, 1, &nfound
);
1958 if (*cmd
== CMD_LIST_AMBIGUOUS
)
1960 return 0; /* ambiguous */
1964 return 0; /* nothing found */
1967 if ((*cmd
)->cmd_pointer
)
1969 /* cmd was actually an alias, we note that an alias was
1970 used (by assigning *ALIAS) and we set *CMD. */
1972 *cmd
= (*cmd
)->cmd_pointer
;
1974 *prefix_cmd
= prev_cmd
;
1978 text
= skip_spaces (text
);
1980 if ((*cmd
)->prefixlist
&& *text
!= '\0')
1981 cur_list
= *(*cmd
)->prefixlist
;
1987 /* Helper function for SYMBOL_COMPLETION_FUNCTION. */
1989 /* Return a vector of char pointers which point to the different
1990 possible completions in LIST of TEXT.
1992 WORD points in the same buffer as TEXT, and completions should be
1993 returned relative to this position. For example, suppose TEXT is
1994 "foo" and we want to complete to "foobar". If WORD is "oo", return
1995 "oobar"; if WORD is "baz/foo", return "baz/foobar". */
1998 complete_on_cmdlist (struct cmd_list_element
*list
,
1999 completion_tracker
&tracker
,
2000 const char *text
, const char *word
,
2001 int ignore_help_classes
)
2003 struct cmd_list_element
*ptr
;
2004 int textlen
= strlen (text
);
2006 int saw_deprecated_match
= 0;
2008 /* We do one or two passes. In the first pass, we skip deprecated
2009 commands. If we see no matching commands in the first pass, and
2010 if we did happen to see a matching deprecated command, we do
2011 another loop to collect those. */
2012 for (pass
= 0; pass
< 2; ++pass
)
2014 bool got_matches
= false;
2016 for (ptr
= list
; ptr
; ptr
= ptr
->next
)
2017 if (!strncmp (ptr
->name
, text
, textlen
)
2018 && !ptr
->abbrev_flag
2019 && (!ignore_help_classes
|| ptr
->func
2020 || ptr
->prefixlist
))
2024 if (ptr
->cmd_deprecated
)
2026 saw_deprecated_match
= 1;
2031 tracker
.add_completion
2032 (make_completion_match_str (ptr
->name
, text
, word
));
2039 /* If we saw no matching deprecated commands in the first pass,
2041 if (!saw_deprecated_match
)
2046 /* Helper function for SYMBOL_COMPLETION_FUNCTION. */
2048 /* Add the different possible completions in ENUMLIST of TEXT.
2050 WORD points in the same buffer as TEXT, and completions should be
2051 returned relative to this position. For example, suppose TEXT is "foo"
2052 and we want to complete to "foobar". If WORD is "oo", return
2053 "oobar"; if WORD is "baz/foo", return "baz/foobar". */
2056 complete_on_enum (completion_tracker
&tracker
,
2057 const char *const *enumlist
,
2058 const char *text
, const char *word
)
2060 int textlen
= strlen (text
);
2064 for (i
= 0; (name
= enumlist
[i
]) != NULL
; i
++)
2065 if (strncmp (name
, text
, textlen
) == 0)
2066 tracker
.add_completion (make_completion_match_str (name
, text
, word
));
2070 /* Check function pointer. */
2072 cmd_func_p (struct cmd_list_element
*cmd
)
2074 return (cmd
->func
!= NULL
);
2078 /* Call the command function. */
2080 cmd_func (struct cmd_list_element
*cmd
, const char *args
, int from_tty
)
2082 if (cmd_func_p (cmd
))
2084 gdb::optional
<scoped_restore_tmpl
<int>> restore_suppress
;
2086 if (cmd
->suppress_notification
!= NULL
)
2087 restore_suppress
.emplace (cmd
->suppress_notification
, 1);
2089 (*cmd
->func
) (cmd
, args
, from_tty
);
2092 error (_("Invalid command"));
2096 cli_user_command_p (struct cmd_list_element
*cmd
)
2098 return (cmd
->theclass
== class_user
2099 && (cmd
->func
== do_const_cfunc
|| cmd
->func
== do_sfunc
));