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 set_cmd_completer (struct cmd_list_element
*cmd
, completer_ftype
*completer
)
145 cmd
->completer
= completer
; /* Ok. */
148 /* See definition in commands.h. */
151 set_cmd_completer_handle_brkchars (struct cmd_list_element
*cmd
,
152 completer_handle_brkchars_ftype
*func
)
154 cmd
->completer_handle_brkchars
= func
;
157 /* Add element named NAME.
158 Space for NAME and DOC must be allocated by the caller.
159 CLASS is the top level category into which commands are broken down
161 FUN should be the function to execute the command;
162 it will get a character string as argument, with leading
163 and trailing blanks already eliminated.
165 DOC is a documentation string for the command.
166 Its first line should be a complete sentence.
167 It should start with ? for a command that is an abbreviation
168 or with * for a command that most users don't need to know about.
170 Add this command to command list *LIST.
172 Returns a pointer to the added command (not necessarily the head
175 static struct cmd_list_element
*
176 do_add_cmd (const char *name
, enum command_class theclass
,
177 const char *doc
, struct cmd_list_element
**list
)
179 struct cmd_list_element
*c
= new struct cmd_list_element (name
, theclass
,
181 struct cmd_list_element
*p
, *iter
;
183 /* Turn each alias of the old command into an alias of the new
185 c
->aliases
= delete_cmd (name
, list
, &c
->hook_pre
, &c
->hookee_pre
,
186 &c
->hook_post
, &c
->hookee_post
);
187 for (iter
= c
->aliases
; iter
; iter
= iter
->alias_chain
)
188 iter
->cmd_pointer
= c
;
190 c
->hook_pre
->hookee_pre
= c
;
192 c
->hookee_pre
->hook_pre
= c
;
194 c
->hook_post
->hookee_post
= c
;
196 c
->hookee_post
->hook_post
= c
;
198 if (*list
== NULL
|| strcmp ((*list
)->name
, name
) >= 0)
206 while (p
->next
&& strcmp (p
->next
->name
, name
) <= 0)
214 /* Search the prefix cmd of C, and assigns it to C->prefix.
215 See also add_prefix_cmd and update_prefix_field_of_prefixed_commands. */
216 struct cmd_list_element
*prefixcmd
= lookup_cmd_for_prefixlist (list
,
218 c
->prefix
= prefixcmd
;
224 struct cmd_list_element
*
225 add_cmd (const char *name
, enum command_class theclass
,
226 const char *doc
, struct cmd_list_element
**list
)
228 cmd_list_element
*result
= do_add_cmd (name
, theclass
, doc
, list
);
230 result
->function
.const_cfunc
= NULL
;
234 struct cmd_list_element
*
235 add_cmd (const char *name
, enum command_class theclass
,
236 cmd_const_cfunc_ftype
*fun
,
237 const char *doc
, struct cmd_list_element
**list
)
239 cmd_list_element
*result
= do_add_cmd (name
, theclass
, doc
, list
);
240 set_cmd_cfunc (result
, fun
);
244 /* Add an element with a suppress notification to the LIST of commands. */
246 struct cmd_list_element
*
247 add_cmd_suppress_notification (const char *name
, enum command_class theclass
,
248 cmd_const_cfunc_ftype
*fun
, const char *doc
,
249 struct cmd_list_element
**list
,
250 int *suppress_notification
)
252 struct cmd_list_element
*element
;
254 element
= add_cmd (name
, theclass
, fun
, doc
, list
);
255 element
->suppress_notification
= suppress_notification
;
261 /* Deprecates a command CMD.
262 REPLACEMENT is the name of the command which should be used in
263 place of this command, or NULL if no such command exists.
265 This function does not check to see if command REPLACEMENT exists
266 since gdb may not have gotten around to adding REPLACEMENT when
267 this function is called.
269 Returns a pointer to the deprecated command. */
271 struct cmd_list_element
*
272 deprecate_cmd (struct cmd_list_element
*cmd
, const char *replacement
)
274 cmd
->cmd_deprecated
= 1;
275 cmd
->deprecated_warn_user
= 1;
277 if (replacement
!= NULL
)
278 cmd
->replacement
= replacement
;
280 cmd
->replacement
= NULL
;
285 struct cmd_list_element
*
286 add_alias_cmd (const char *name
, cmd_list_element
*old
,
287 enum command_class theclass
, int abbrev_flag
,
288 struct cmd_list_element
**list
)
292 struct cmd_list_element
*prehook
, *prehookee
, *posthook
, *posthookee
;
293 struct cmd_list_element
*aliases
= delete_cmd (name
, list
,
294 &prehook
, &prehookee
,
295 &posthook
, &posthookee
);
297 /* If this happens, it means a programmer error somewhere. */
298 gdb_assert (!aliases
&& !prehook
&& !prehookee
299 && !posthook
&& ! posthookee
);
303 struct cmd_list_element
*c
= add_cmd (name
, theclass
, old
->doc
, list
);
305 /* If OLD->DOC can be freed, we should make another copy. */
306 if (old
->doc_allocated
)
308 c
->doc
= xstrdup (old
->doc
);
309 c
->doc_allocated
= 1;
311 /* NOTE: Both FUNC and all the FUNCTIONs need to be copied. */
313 c
->function
= old
->function
;
314 c
->prefixlist
= old
->prefixlist
;
315 c
->prefixname
= old
->prefixname
;
316 c
->allow_unknown
= old
->allow_unknown
;
317 c
->abbrev_flag
= abbrev_flag
;
318 c
->cmd_pointer
= old
;
319 c
->alias_chain
= old
->aliases
;
325 struct cmd_list_element
*
326 add_alias_cmd (const char *name
, const char *oldname
,
327 enum command_class theclass
, int abbrev_flag
,
328 struct cmd_list_element
**list
)
331 struct cmd_list_element
*old
;
334 old
= lookup_cmd (&tmp
, *list
, "", NULL
, 1, 1);
336 return add_alias_cmd (name
, old
, theclass
, abbrev_flag
, list
);
340 /* Update the prefix field of all sub-commands of the prefix command C.
341 We must do this when a prefix command is defined as the GDB init sequence
342 does not guarantee that a prefix command is created before its sub-commands.
343 For example, break-catch-sig.c initialization runs before breakpoint.c
344 initialization, but it is breakpoint.c that creates the "catch" command used
345 by the "catch signal" command created by break-catch-sig.c. */
348 update_prefix_field_of_prefixed_commands (struct cmd_list_element
*c
)
350 for (cmd_list_element
*p
= *c
->prefixlist
; p
!= NULL
; p
= p
->next
)
354 /* We must recursively update the prefix field to cover
355 e.g. 'info auto-load libthread-db' where the creation
360 In such a case, when 'auto-load' was created by do_add_cmd,
361 the 'libthread-db' prefix field could not be updated, as the
362 'auto-load' command was not yet reachable by
363 lookup_cmd_for_prefixlist (list, cmdlist)
364 that searches from the top level 'cmdlist'. */
365 if (p
->prefixlist
!= nullptr)
366 update_prefix_field_of_prefixed_commands (p
);
371 /* Like add_cmd but adds an element for a command prefix: a name that
372 should be followed by a subcommand to be looked up in another
373 command list. PREFIXLIST should be the address of the variable
374 containing that list. */
376 struct cmd_list_element
*
377 add_prefix_cmd (const char *name
, enum command_class theclass
,
378 cmd_const_cfunc_ftype
*fun
,
379 const char *doc
, struct cmd_list_element
**prefixlist
,
380 const char *prefixname
, int allow_unknown
,
381 struct cmd_list_element
**list
)
383 struct cmd_list_element
*c
= add_cmd (name
, theclass
, fun
, doc
, list
);
385 c
->prefixlist
= prefixlist
;
386 c
->prefixname
= prefixname
;
387 c
->allow_unknown
= allow_unknown
;
389 /* Now that prefix command C is defined, we need to set the prefix field
390 of all prefixed commands that were defined before C itself was defined. */
391 update_prefix_field_of_prefixed_commands (c
);
396 /* A helper function for add_basic_prefix_cmd. This is a command
397 function that just forwards to help_list. */
400 do_prefix_cmd (const char *args
, int from_tty
, struct cmd_list_element
*c
)
402 /* Look past all aliases. */
403 while (c
->cmd_pointer
!= nullptr)
406 help_list (*c
->prefixlist
, c
->prefixname
, all_commands
, gdb_stdout
);
411 struct cmd_list_element
*
412 add_basic_prefix_cmd (const char *name
, enum command_class theclass
,
413 const char *doc
, struct cmd_list_element
**prefixlist
,
414 const char *prefixname
, int allow_unknown
,
415 struct cmd_list_element
**list
)
417 struct cmd_list_element
*cmd
= add_prefix_cmd (name
, theclass
, nullptr,
418 doc
, prefixlist
, prefixname
,
419 allow_unknown
, list
);
420 set_cmd_sfunc (cmd
, do_prefix_cmd
);
424 /* A helper function for add_show_prefix_cmd. This is a command
425 function that just forwards to cmd_show_list. */
428 do_show_prefix_cmd (const char *args
, int from_tty
, struct cmd_list_element
*c
)
430 cmd_show_list (*c
->prefixlist
, from_tty
);
435 struct cmd_list_element
*
436 add_show_prefix_cmd (const char *name
, enum command_class theclass
,
437 const char *doc
, struct cmd_list_element
**prefixlist
,
438 const char *prefixname
, int allow_unknown
,
439 struct cmd_list_element
**list
)
441 struct cmd_list_element
*cmd
= add_prefix_cmd (name
, theclass
, nullptr,
442 doc
, prefixlist
, prefixname
,
443 allow_unknown
, list
);
444 set_cmd_sfunc (cmd
, do_show_prefix_cmd
);
448 /* Like ADD_PREFIX_CMD but sets the suppress_notification pointer on the
449 new command list element. */
451 struct cmd_list_element
*
452 add_prefix_cmd_suppress_notification
453 (const char *name
, enum command_class theclass
,
454 cmd_const_cfunc_ftype
*fun
,
455 const char *doc
, struct cmd_list_element
**prefixlist
,
456 const char *prefixname
, int allow_unknown
,
457 struct cmd_list_element
**list
,
458 int *suppress_notification
)
460 struct cmd_list_element
*element
461 = add_prefix_cmd (name
, theclass
, fun
, doc
, prefixlist
,
462 prefixname
, allow_unknown
, list
);
463 element
->suppress_notification
= suppress_notification
;
467 /* Like add_prefix_cmd but sets the abbrev_flag on the new command. */
469 struct cmd_list_element
*
470 add_abbrev_prefix_cmd (const char *name
, enum command_class theclass
,
471 cmd_const_cfunc_ftype
*fun
, const char *doc
,
472 struct cmd_list_element
**prefixlist
,
473 const char *prefixname
,
474 int allow_unknown
, struct cmd_list_element
**list
)
476 struct cmd_list_element
*c
= add_cmd (name
, theclass
, fun
, doc
, list
);
478 c
->prefixlist
= prefixlist
;
479 c
->prefixname
= prefixname
;
480 c
->allow_unknown
= allow_unknown
;
485 /* This is an empty "cfunc". */
487 not_just_help_class_command (const char *args
, int from_tty
)
491 /* This is an empty "sfunc". */
494 empty_sfunc (const char *args
, int from_tty
, struct cmd_list_element
*c
)
498 /* Add element named NAME to command list LIST (the list for set/show
499 or some sublist thereof).
500 TYPE is set_cmd or show_cmd.
501 CLASS is as in add_cmd.
502 VAR_TYPE is the kind of thing we are setting.
503 VAR is address of the variable being controlled by this command.
504 DOC is the documentation string. */
506 static struct cmd_list_element
*
507 add_set_or_show_cmd (const char *name
,
509 enum command_class theclass
,
513 struct cmd_list_element
**list
)
515 struct cmd_list_element
*c
= add_cmd (name
, theclass
, doc
, list
);
517 gdb_assert (type
== set_cmd
|| type
== show_cmd
);
519 c
->var_type
= var_type
;
521 /* This needs to be something besides NULL so that this isn't
522 treated as a help class. */
523 set_cmd_sfunc (c
, empty_sfunc
);
527 /* Add element named NAME to both the command SET_LIST and SHOW_LIST.
528 CLASS is as in add_cmd. VAR_TYPE is the kind of thing we are
529 setting. VAR is address of the variable being controlled by this
530 command. SET_FUNC and SHOW_FUNC are the callback functions (if
531 non-NULL). SET_DOC, SHOW_DOC and HELP_DOC are the documentation
532 strings. PRINT the format string to print the value. SET_RESULT
533 and SHOW_RESULT, if not NULL, are set to the resulting command
537 add_setshow_cmd_full (const char *name
,
538 enum command_class theclass
,
539 var_types var_type
, void *var
,
540 const char *set_doc
, const char *show_doc
,
541 const char *help_doc
,
542 cmd_const_sfunc_ftype
*set_func
,
543 show_value_ftype
*show_func
,
544 struct cmd_list_element
**set_list
,
545 struct cmd_list_element
**show_list
,
546 struct cmd_list_element
**set_result
,
547 struct cmd_list_element
**show_result
)
549 struct cmd_list_element
*set
;
550 struct cmd_list_element
*show
;
554 if (help_doc
!= NULL
)
556 full_set_doc
= xstrprintf ("%s\n%s", set_doc
, help_doc
);
557 full_show_doc
= xstrprintf ("%s\n%s", show_doc
, help_doc
);
561 full_set_doc
= xstrdup (set_doc
);
562 full_show_doc
= xstrdup (show_doc
);
564 set
= add_set_or_show_cmd (name
, set_cmd
, theclass
, var_type
, var
,
565 full_set_doc
, set_list
);
566 set
->doc_allocated
= 1;
568 if (set_func
!= NULL
)
569 set_cmd_sfunc (set
, set_func
);
571 show
= add_set_or_show_cmd (name
, show_cmd
, theclass
, var_type
, var
,
572 full_show_doc
, show_list
);
573 show
->doc_allocated
= 1;
574 show
->show_value_func
= show_func
;
575 /* Disable the default symbol completer. Doesn't make much sense
576 for the "show" command to complete on anything. */
577 set_cmd_completer (show
, nullptr);
579 if (set_result
!= NULL
)
581 if (show_result
!= NULL
)
585 /* Add element named NAME to command list LIST (the list for set or
586 some sublist thereof). CLASS is as in add_cmd. ENUMLIST is a list
587 of strings which may follow NAME. VAR is address of the variable
588 which will contain the matching string (from ENUMLIST). */
591 add_setshow_enum_cmd (const char *name
,
592 enum command_class theclass
,
593 const char *const *enumlist
,
596 const char *show_doc
,
597 const char *help_doc
,
598 cmd_const_sfunc_ftype
*set_func
,
599 show_value_ftype
*show_func
,
600 struct cmd_list_element
**set_list
,
601 struct cmd_list_element
**show_list
,
604 struct cmd_list_element
*c
, *show
;
606 add_setshow_cmd_full (name
, theclass
, var_enum
, var
,
607 set_doc
, show_doc
, help_doc
,
613 set_cmd_context (c
, context
);
614 set_cmd_context (show
, context
);
617 /* See cli-decode.h. */
618 const char * const auto_boolean_enums
[] = { "on", "off", "auto", NULL
};
620 /* Add an auto-boolean command named NAME to both the set and show
621 command list lists. CLASS is as in add_cmd. VAR is address of the
622 variable which will contain the value. DOC is the documentation
623 string. FUNC is the corresponding callback. */
625 add_setshow_auto_boolean_cmd (const char *name
,
626 enum command_class theclass
,
627 enum auto_boolean
*var
,
628 const char *set_doc
, const char *show_doc
,
629 const char *help_doc
,
630 cmd_const_sfunc_ftype
*set_func
,
631 show_value_ftype
*show_func
,
632 struct cmd_list_element
**set_list
,
633 struct cmd_list_element
**show_list
)
635 struct cmd_list_element
*c
;
637 add_setshow_cmd_full (name
, theclass
, var_auto_boolean
, var
,
638 set_doc
, show_doc
, help_doc
,
642 c
->enums
= auto_boolean_enums
;
645 /* See cli-decode.h. */
646 const char * const boolean_enums
[] = { "on", "off", NULL
};
648 /* Add element named NAME to both the set and show command LISTs (the
649 list for set/show or some sublist thereof). CLASS is as in
650 add_cmd. VAR is address of the variable which will contain the
651 value. SET_DOC and SHOW_DOC are the documentation strings.
652 Returns the new command element. */
655 add_setshow_boolean_cmd (const char *name
, enum command_class theclass
, bool *var
,
656 const char *set_doc
, const char *show_doc
,
657 const char *help_doc
,
658 cmd_const_sfunc_ftype
*set_func
,
659 show_value_ftype
*show_func
,
660 struct cmd_list_element
**set_list
,
661 struct cmd_list_element
**show_list
)
663 struct cmd_list_element
*c
;
665 add_setshow_cmd_full (name
, theclass
, var_boolean
, var
,
666 set_doc
, show_doc
, help_doc
,
670 c
->enums
= boolean_enums
;
675 /* Add element named NAME to both the set and show command LISTs (the
676 list for set/show or some sublist thereof). */
678 add_setshow_filename_cmd (const char *name
, enum command_class theclass
,
680 const char *set_doc
, const char *show_doc
,
681 const char *help_doc
,
682 cmd_const_sfunc_ftype
*set_func
,
683 show_value_ftype
*show_func
,
684 struct cmd_list_element
**set_list
,
685 struct cmd_list_element
**show_list
)
687 struct cmd_list_element
*set_result
;
689 add_setshow_cmd_full (name
, theclass
, var_filename
, var
,
690 set_doc
, show_doc
, help_doc
,
694 set_cmd_completer (set_result
, filename_completer
);
697 /* Add element named NAME to both the set and show command LISTs (the
698 list for set/show or some sublist thereof). */
700 add_setshow_string_cmd (const char *name
, enum command_class theclass
,
702 const char *set_doc
, const char *show_doc
,
703 const char *help_doc
,
704 cmd_const_sfunc_ftype
*set_func
,
705 show_value_ftype
*show_func
,
706 struct cmd_list_element
**set_list
,
707 struct cmd_list_element
**show_list
)
709 cmd_list_element
*set_cmd
;
711 add_setshow_cmd_full (name
, theclass
, var_string
, var
,
712 set_doc
, show_doc
, help_doc
,
717 /* Disable the default symbol completer. */
718 set_cmd_completer (set_cmd
, nullptr);
721 /* Add element named NAME to both the set and show command LISTs (the
722 list for set/show or some sublist thereof). */
723 struct cmd_list_element
*
724 add_setshow_string_noescape_cmd (const char *name
, enum command_class theclass
,
726 const char *set_doc
, const char *show_doc
,
727 const char *help_doc
,
728 cmd_const_sfunc_ftype
*set_func
,
729 show_value_ftype
*show_func
,
730 struct cmd_list_element
**set_list
,
731 struct cmd_list_element
**show_list
)
733 struct cmd_list_element
*set_cmd
;
735 add_setshow_cmd_full (name
, theclass
, var_string_noescape
, var
,
736 set_doc
, show_doc
, help_doc
,
741 /* Disable the default symbol completer. */
742 set_cmd_completer (set_cmd
, nullptr);
747 /* Add element named NAME to both the set and show command LISTs (the
748 list for set/show or some sublist thereof). */
750 add_setshow_optional_filename_cmd (const char *name
, enum command_class theclass
,
752 const char *set_doc
, const char *show_doc
,
753 const char *help_doc
,
754 cmd_const_sfunc_ftype
*set_func
,
755 show_value_ftype
*show_func
,
756 struct cmd_list_element
**set_list
,
757 struct cmd_list_element
**show_list
)
759 struct cmd_list_element
*set_result
;
761 add_setshow_cmd_full (name
, theclass
, var_optional_filename
, var
,
762 set_doc
, show_doc
, help_doc
,
767 set_cmd_completer (set_result
, filename_completer
);
771 /* Completes on literal "unlimited". Used by integer commands that
772 support a special "unlimited" value. */
775 integer_unlimited_completer (struct cmd_list_element
*ignore
,
776 completion_tracker
&tracker
,
777 const char *text
, const char *word
)
779 static const char * const keywords
[] =
785 complete_on_enum (tracker
, keywords
, text
, word
);
788 /* Add element named NAME to both the set and show command LISTs (the
789 list for set/show or some sublist thereof). CLASS is as in
790 add_cmd. VAR is address of the variable which will contain the
791 value. SET_DOC and SHOW_DOC are the documentation strings. This
792 function is only used in Python API. Please don't use it elsewhere. */
794 add_setshow_integer_cmd (const char *name
, enum command_class theclass
,
796 const char *set_doc
, const char *show_doc
,
797 const char *help_doc
,
798 cmd_const_sfunc_ftype
*set_func
,
799 show_value_ftype
*show_func
,
800 struct cmd_list_element
**set_list
,
801 struct cmd_list_element
**show_list
)
803 struct cmd_list_element
*set
;
805 add_setshow_cmd_full (name
, theclass
, var_integer
, var
,
806 set_doc
, show_doc
, help_doc
,
811 set_cmd_completer (set
, integer_unlimited_completer
);
814 /* Add element named NAME to both the set and show command LISTs (the
815 list for set/show or some sublist thereof). CLASS is as in
816 add_cmd. VAR is address of the variable which will contain the
817 value. SET_DOC and SHOW_DOC are the documentation strings. */
819 add_setshow_uinteger_cmd (const char *name
, enum command_class theclass
,
821 const char *set_doc
, const char *show_doc
,
822 const char *help_doc
,
823 cmd_const_sfunc_ftype
*set_func
,
824 show_value_ftype
*show_func
,
825 struct cmd_list_element
**set_list
,
826 struct cmd_list_element
**show_list
)
828 struct cmd_list_element
*set
;
830 add_setshow_cmd_full (name
, theclass
, var_uinteger
, var
,
831 set_doc
, show_doc
, help_doc
,
836 set_cmd_completer (set
, integer_unlimited_completer
);
839 /* Add element named NAME to both the set and show command LISTs (the
840 list for set/show or some sublist thereof). CLASS is as in
841 add_cmd. VAR is address of the variable which will contain the
842 value. SET_DOC and SHOW_DOC are the documentation strings. */
844 add_setshow_zinteger_cmd (const char *name
, enum command_class theclass
,
846 const char *set_doc
, const char *show_doc
,
847 const char *help_doc
,
848 cmd_const_sfunc_ftype
*set_func
,
849 show_value_ftype
*show_func
,
850 struct cmd_list_element
**set_list
,
851 struct cmd_list_element
**show_list
)
853 add_setshow_cmd_full (name
, theclass
, var_zinteger
, var
,
854 set_doc
, show_doc
, help_doc
,
861 add_setshow_zuinteger_unlimited_cmd (const char *name
,
862 enum command_class theclass
,
865 const char *show_doc
,
866 const char *help_doc
,
867 cmd_const_sfunc_ftype
*set_func
,
868 show_value_ftype
*show_func
,
869 struct cmd_list_element
**set_list
,
870 struct cmd_list_element
**show_list
)
872 struct cmd_list_element
*set
;
874 add_setshow_cmd_full (name
, theclass
, var_zuinteger_unlimited
, var
,
875 set_doc
, show_doc
, help_doc
,
880 set_cmd_completer (set
, integer_unlimited_completer
);
883 /* Add element named NAME to both the set and show command LISTs (the
884 list for set/show or some sublist thereof). CLASS is as in
885 add_cmd. VAR is address of the variable which will contain the
886 value. SET_DOC and SHOW_DOC are the documentation strings. */
888 add_setshow_zuinteger_cmd (const char *name
, enum command_class theclass
,
890 const char *set_doc
, const char *show_doc
,
891 const char *help_doc
,
892 cmd_const_sfunc_ftype
*set_func
,
893 show_value_ftype
*show_func
,
894 struct cmd_list_element
**set_list
,
895 struct cmd_list_element
**show_list
)
897 add_setshow_cmd_full (name
, theclass
, var_zuinteger
, var
,
898 set_doc
, show_doc
, help_doc
,
904 /* Remove the command named NAME from the command list. Return the
905 list commands which were aliased to the deleted command. If the
906 command had no aliases, return NULL. The various *HOOKs are set to
907 the pre- and post-hook commands for the deleted command. If the
908 command does not have a hook, the corresponding out parameter is
911 static struct cmd_list_element
*
912 delete_cmd (const char *name
, struct cmd_list_element
**list
,
913 struct cmd_list_element
**prehook
,
914 struct cmd_list_element
**prehookee
,
915 struct cmd_list_element
**posthook
,
916 struct cmd_list_element
**posthookee
)
918 struct cmd_list_element
*iter
;
919 struct cmd_list_element
**previous_chain_ptr
;
920 struct cmd_list_element
*aliases
= NULL
;
926 previous_chain_ptr
= list
;
928 for (iter
= *previous_chain_ptr
; iter
; iter
= *previous_chain_ptr
)
930 if (strcmp (iter
->name
, name
) == 0)
933 iter
->destroyer (iter
, iter
->context
);
934 if (iter
->hookee_pre
)
935 iter
->hookee_pre
->hook_pre
= 0;
936 *prehook
= iter
->hook_pre
;
937 *prehookee
= iter
->hookee_pre
;
938 if (iter
->hookee_post
)
939 iter
->hookee_post
->hook_post
= 0;
940 *posthook
= iter
->hook_post
;
941 *posthookee
= iter
->hookee_post
;
943 /* Update the link. */
944 *previous_chain_ptr
= iter
->next
;
946 aliases
= iter
->aliases
;
948 /* If this command was an alias, remove it from the list of
950 if (iter
->cmd_pointer
)
952 struct cmd_list_element
**prevp
= &iter
->cmd_pointer
->aliases
;
953 struct cmd_list_element
*a
= *prevp
;
957 prevp
= &a
->alias_chain
;
960 *prevp
= iter
->alias_chain
;
965 /* We won't see another command with the same name. */
969 previous_chain_ptr
= &iter
->next
;
975 /* Shorthands to the commands above. */
977 /* Add an element to the list of info subcommands. */
979 struct cmd_list_element
*
980 add_info (const char *name
, cmd_const_cfunc_ftype
*fun
, const char *doc
)
982 return add_cmd (name
, class_info
, fun
, doc
, &infolist
);
985 /* Add an alias to the list of info subcommands. */
987 struct cmd_list_element
*
988 add_info_alias (const char *name
, const char *oldname
, int abbrev_flag
)
990 return add_alias_cmd (name
, oldname
, class_run
, abbrev_flag
, &infolist
);
993 /* Add an element to the list of commands. */
995 struct cmd_list_element
*
996 add_com (const char *name
, enum command_class theclass
,
997 cmd_const_cfunc_ftype
*fun
,
1000 return add_cmd (name
, theclass
, fun
, doc
, &cmdlist
);
1003 /* Add an alias or abbreviation command to the list of commands.
1004 For aliases predefined by GDB (such as bt), THECLASS must be
1005 different of class_alias, as class_alias is used to identify
1006 user defined aliases. */
1008 struct cmd_list_element
*
1009 add_com_alias (const char *name
, const char *oldname
, enum command_class theclass
,
1012 return add_alias_cmd (name
, oldname
, theclass
, abbrev_flag
, &cmdlist
);
1015 /* Add an element with a suppress notification to the list of commands. */
1017 struct cmd_list_element
*
1018 add_com_suppress_notification (const char *name
, enum command_class theclass
,
1019 cmd_const_cfunc_ftype
*fun
, const char *doc
,
1020 int *suppress_notification
)
1022 return add_cmd_suppress_notification (name
, theclass
, fun
, doc
,
1023 &cmdlist
, suppress_notification
);
1026 /* Print the prefix of C followed by name of C in title style. */
1029 fput_command_name_styled (struct cmd_list_element
*c
, struct ui_file
*stream
)
1031 const char *prefixname
1032 = c
->prefix
== nullptr ? "" : c
->prefix
->prefixname
;
1034 fprintf_styled (stream
, title_style
.style (), "%s%s", prefixname
, c
->name
);
1037 /* Print the definition of alias C using title style for alias
1038 and aliased command. */
1041 fput_alias_definition_styled (struct cmd_list_element
*c
,
1042 struct ui_file
*stream
)
1044 gdb_assert (c
->cmd_pointer
!= nullptr);
1045 fputs_filtered (" alias ", stream
);
1046 fput_command_name_styled (c
, stream
);
1047 fprintf_filtered (stream
, " = ");
1048 fput_command_name_styled (c
->cmd_pointer
, stream
);
1049 fprintf_filtered (stream
, " %s\n", c
->default_args
.c_str ());
1052 /* Print the definition of the aliases of CMD that have default args. */
1055 fput_aliases_definition_styled (struct cmd_list_element
*cmd
,
1056 struct ui_file
*stream
)
1058 if (cmd
->aliases
!= nullptr)
1060 for (cmd_list_element
*iter
= cmd
->aliases
;
1062 iter
= iter
->alias_chain
)
1064 if (!iter
->default_args
.empty ())
1065 fput_alias_definition_styled (iter
, stream
);
1071 /* If C has one or more aliases, style print the name of C and
1072 the name of its aliases, separated by commas.
1073 If ALWAYS_FPUT_C_NAME, print the name of C even if it has no aliases.
1074 If one or more names are printed, POSTFIX is printed after the last name.
1078 fput_command_names_styled (struct cmd_list_element
*c
,
1079 bool always_fput_c_name
, const char *postfix
,
1080 struct ui_file
*stream
)
1082 if (always_fput_c_name
|| c
->aliases
!= nullptr)
1083 fput_command_name_styled (c
, stream
);
1084 if (c
->aliases
!= nullptr)
1086 for (cmd_list_element
*iter
= c
->aliases
; iter
; iter
= iter
->alias_chain
)
1088 fputs_filtered (", ", stream
);
1090 fput_command_name_styled (iter
, stream
);
1093 if (always_fput_c_name
|| c
->aliases
!= nullptr)
1094 fputs_filtered (postfix
, stream
);
1097 /* If VERBOSE, print the full help for command C and highlight the
1098 documentation parts matching HIGHLIGHT,
1099 otherwise print only one-line help for command C. */
1102 print_doc_of_command (struct cmd_list_element
*c
, const char *prefix
,
1103 bool verbose
, compiled_regex
&highlight
,
1104 struct ui_file
*stream
)
1106 /* When printing the full documentation, add a line to separate
1107 this documentation from the previous command help, in the likely
1108 case that apropos finds several commands. */
1110 fputs_filtered ("\n", stream
);
1112 fput_command_names_styled (c
, true,
1113 verbose
? "" : " -- ", stream
);
1116 fputs_filtered ("\n", stream
);
1117 fput_aliases_definition_styled (c
, stream
);
1118 fputs_highlighted (c
->doc
, highlight
, stream
);
1119 fputs_filtered ("\n", stream
);
1123 print_doc_line (stream
, c
->doc
, false);
1124 fputs_filtered ("\n", stream
);
1125 fput_aliases_definition_styled (c
, stream
);
1129 /* Recursively walk the commandlist structures, and print out the
1130 documentation of commands that match our regex in either their
1131 name, or their documentation.
1132 If VERBOSE, prints the complete documentation and highlight the
1133 documentation parts matching REGEX, otherwise prints only
1137 apropos_cmd (struct ui_file
*stream
,
1138 struct cmd_list_element
*commandlist
,
1139 bool verbose
, compiled_regex
®ex
, const char *prefix
)
1141 struct cmd_list_element
*c
;
1144 /* Walk through the commands. */
1145 for (c
=commandlist
;c
;c
=c
->next
)
1147 if (c
->cmd_pointer
!= nullptr)
1149 /* Command aliases/abbreviations are skipped to ensure we print the
1150 doc of a command only once, when encountering the aliased
1155 returnvalue
= -1; /* Needed to avoid double printing. */
1156 if (c
->name
!= NULL
)
1158 size_t name_len
= strlen (c
->name
);
1160 /* Try to match against the name. */
1161 returnvalue
= regex
.search (c
->name
, name_len
, 0, name_len
, NULL
);
1162 if (returnvalue
>= 0)
1163 print_doc_of_command (c
, prefix
, verbose
, regex
, stream
);
1165 /* Try to match against the name of the aliases. */
1166 for (cmd_list_element
*iter
= c
->aliases
;
1167 returnvalue
< 0 && iter
;
1168 iter
= iter
->alias_chain
)
1170 name_len
= strlen (iter
->name
);
1171 returnvalue
= regex
.search (iter
->name
, name_len
, 0, name_len
, NULL
);
1172 if (returnvalue
>= 0)
1173 print_doc_of_command (c
, prefix
, verbose
, regex
, stream
);
1176 if (c
->doc
!= NULL
&& returnvalue
< 0)
1178 size_t doc_len
= strlen (c
->doc
);
1180 /* Try to match against documentation. */
1181 if (regex
.search (c
->doc
, doc_len
, 0, doc_len
, NULL
) >= 0)
1182 print_doc_of_command (c
, prefix
, verbose
, regex
, stream
);
1184 /* Check if this command has subcommands. */
1185 if (c
->prefixlist
!= NULL
)
1187 /* Recursively call ourselves on the subcommand list,
1188 passing the right prefix in. */
1189 apropos_cmd (stream
, *c
->prefixlist
, verbose
, regex
, c
->prefixname
);
1194 /* This command really has to deal with two things:
1195 1) I want documentation on *this string* (usually called by
1196 "help commandname").
1198 2) I want documentation on *this list* (usually called by giving a
1199 command that requires subcommands. Also called by saying just
1202 I am going to split this into two separate commands, help_cmd and
1206 help_cmd (const char *command
, struct ui_file
*stream
)
1208 struct cmd_list_element
*c
, *alias
, *prefix_cmd
, *c_cmd
;
1212 help_list (cmdlist
, "", all_classes
, stream
);
1216 if (strcmp (command
, "all") == 0)
1222 const char *orig_command
= command
;
1223 c
= lookup_cmd (&command
, cmdlist
, "", NULL
, 0, 0);
1228 lookup_cmd_composition (orig_command
, &alias
, &prefix_cmd
, &c_cmd
);
1230 /* There are three cases here.
1231 If c->prefixlist is nonzero, we have a prefix command.
1232 Print its documentation, then list its subcommands.
1234 If c->func is non NULL, we really have a command. Print its
1235 documentation and return.
1237 If c->func is NULL, we have a class name. Print its
1238 documentation (as if it were a command) and then set class to the
1239 number of this class so that the commands in the class will be
1242 /* If the user asked 'help somecommand' and there is no alias,
1243 the false indicates to not output the (single) command name. */
1244 fput_command_names_styled (c
, false, "\n", stream
);
1245 fput_aliases_definition_styled (c
, stream
);
1246 fputs_filtered (c
->doc
, stream
);
1247 fputs_filtered ("\n", stream
);
1249 if (c
->prefixlist
== 0 && c
->func
!= NULL
)
1251 fprintf_filtered (stream
, "\n");
1253 /* If this is a prefix command, print it's subcommands. */
1255 help_list (*c
->prefixlist
, c
->prefixname
, all_commands
, stream
);
1257 /* If this is a class name, print all of the commands in the class. */
1258 if (c
->func
== NULL
)
1259 help_list (cmdlist
, "", c
->theclass
, stream
);
1261 if (c
->hook_pre
|| c
->hook_post
)
1262 fprintf_filtered (stream
,
1263 "\nThis command has a hook (or hooks) defined:\n");
1266 fprintf_filtered (stream
,
1267 "\tThis command is run after : %s (pre hook)\n",
1270 fprintf_filtered (stream
,
1271 "\tThis command is run before : %s (post hook)\n",
1272 c
->hook_post
->name
);
1276 * Get a specific kind of help on a command list.
1279 * CMDTYPE is the prefix to use in the title string.
1280 * CLASS is the class with which to list the nodes of this list (see
1281 * documentation for help_cmd_list below), As usual, ALL_COMMANDS for
1282 * everything, ALL_CLASSES for just classes, and non-negative for only things
1283 * in a specific class.
1284 * and STREAM is the output stream on which to print things.
1285 * If you call this routine with a class >= 0, it recurses.
1288 help_list (struct cmd_list_element
*list
, const char *cmdtype
,
1289 enum command_class theclass
, struct ui_file
*stream
)
1292 char *cmdtype1
, *cmdtype2
;
1294 /* If CMDTYPE is "foo ", CMDTYPE1 gets " foo" and CMDTYPE2 gets "foo sub".
1296 len
= strlen (cmdtype
);
1297 cmdtype1
= (char *) alloca (len
+ 1);
1299 cmdtype2
= (char *) alloca (len
+ 4);
1304 memcpy (cmdtype1
+ 1, cmdtype
, len
- 1);
1306 memcpy (cmdtype2
, cmdtype
, len
- 1);
1307 strcpy (cmdtype2
+ len
- 1, " sub");
1310 if (theclass
== all_classes
)
1311 fprintf_filtered (stream
, "List of classes of %scommands:\n\n", cmdtype2
);
1313 fprintf_filtered (stream
, "List of %scommands:\n\n", cmdtype2
);
1315 help_cmd_list (list
, theclass
, theclass
>= 0, stream
);
1317 if (theclass
== all_classes
)
1319 fprintf_filtered (stream
, "\n\
1320 Type \"help%s\" followed by a class name for a list of commands in ",
1323 fprintf_filtered (stream
, "that class.");
1325 fprintf_filtered (stream
, "\n\
1326 Type \"help all\" for the list of all commands.");
1329 fprintf_filtered (stream
, "\nType \"help%s\" followed by %scommand name ",
1330 cmdtype1
, cmdtype2
);
1332 fputs_filtered ("for ", stream
);
1334 fputs_filtered ("full ", stream
);
1336 fputs_filtered ("documentation.\n", stream
);
1337 fputs_filtered ("Type \"apropos word\" to search "
1338 "for commands related to \"word\".\n", stream
);
1339 fputs_filtered ("Type \"apropos -v word\" for full documentation", stream
);
1341 fputs_filtered (" of commands related to \"word\".\n", stream
);
1342 fputs_filtered ("Command name abbreviations are allowed if unambiguous.\n",
1347 help_all (struct ui_file
*stream
)
1349 struct cmd_list_element
*c
;
1350 int seen_unclassified
= 0;
1352 for (c
= cmdlist
; c
; c
= c
->next
)
1356 /* If this is a class name, print all of the commands in the
1359 if (c
->func
== NULL
)
1361 fprintf_filtered (stream
, "\nCommand class: %s\n\n", c
->name
);
1362 help_cmd_list (cmdlist
, c
->theclass
, true, stream
);
1366 /* While it's expected that all commands are in some class,
1367 as a safety measure, we'll print commands outside of any
1368 class at the end. */
1370 for (c
= cmdlist
; c
; c
= c
->next
)
1375 if (c
->theclass
== no_class
)
1377 if (!seen_unclassified
)
1379 fprintf_filtered (stream
, "\nUnclassified commands\n\n");
1380 seen_unclassified
= 1;
1382 print_help_for_command (c
, true, stream
);
1388 /* See cli-decode.h. */
1391 print_doc_line (struct ui_file
*stream
, const char *str
,
1392 bool for_value_prefix
)
1394 static char *line_buffer
= 0;
1395 static int line_size
;
1401 line_buffer
= (char *) xmalloc (line_size
);
1404 /* Searches for the first end of line or the end of STR. */
1406 while (*p
&& *p
!= '\n')
1408 if (p
- str
> line_size
- 1)
1410 line_size
= p
- str
+ 1;
1411 xfree (line_buffer
);
1412 line_buffer
= (char *) xmalloc (line_size
);
1414 strncpy (line_buffer
, str
, p
- str
);
1415 if (for_value_prefix
)
1417 if (islower (line_buffer
[0]))
1418 line_buffer
[0] = toupper (line_buffer
[0]);
1419 gdb_assert (p
> str
);
1420 if (line_buffer
[p
- str
- 1] == '.')
1421 line_buffer
[p
- str
- 1] = '\0';
1423 line_buffer
[p
- str
] = '\0';
1426 line_buffer
[p
- str
] = '\0';
1427 fputs_filtered (line_buffer
, stream
);
1430 /* Print one-line help for command C.
1431 If RECURSE is non-zero, also print one-line descriptions
1432 of all prefixed subcommands. */
1434 print_help_for_command (struct cmd_list_element
*c
,
1435 bool recurse
, struct ui_file
*stream
)
1437 fput_command_names_styled (c
, true, " -- ", stream
);
1438 print_doc_line (stream
, c
->doc
, false);
1439 fputs_filtered ("\n", stream
);
1440 if (!c
->default_args
.empty ())
1441 fput_alias_definition_styled (c
, stream
);
1442 fput_aliases_definition_styled (c
, stream
);
1445 && c
->prefixlist
!= 0
1446 && c
->abbrev_flag
== 0)
1447 /* Subcommands of a prefix command typically have 'all_commands'
1448 as class. If we pass CLASS to recursive invocation,
1449 most often we won't see anything. */
1450 help_cmd_list (*c
->prefixlist
, all_commands
, true, stream
);
1454 * Implement a help command on command list LIST.
1455 * RECURSE should be non-zero if this should be done recursively on
1456 * all sublists of LIST.
1457 * STREAM is the stream upon which the output should be written.
1458 * THECLASS should be:
1459 * A non-negative class number to list only commands in that
1460 * ALL_COMMANDS to list all commands in list.
1461 * ALL_CLASSES to list all classes in list.
1463 * Note that aliases are only shown when THECLASS is class_alias.
1464 * In the other cases, the aliases will be shown together with their
1467 * Note that RECURSE will be active on *all* sublists, not just the
1468 * ones selected by the criteria above (ie. the selection mechanism
1469 * is at the low level, not the high-level).
1473 help_cmd_list (struct cmd_list_element
*list
, enum command_class theclass
,
1474 bool recurse
, struct ui_file
*stream
)
1476 struct cmd_list_element
*c
;
1478 for (c
= list
; c
; c
= c
->next
)
1480 if (c
->abbrev_flag
== 1 || c
->cmd_deprecated
)
1482 /* Do not show abbreviations or deprecated commands. */
1486 if (c
->cmd_pointer
!= nullptr && theclass
!= class_alias
)
1488 /* Do not show an alias, unless specifically showing the
1489 list of aliases: for all other classes, an alias is
1490 shown (if needed) together with its aliased command. */
1494 if (theclass
== all_commands
1495 || (theclass
== all_classes
&& c
->func
== NULL
)
1496 || (theclass
== c
->theclass
&& c
->func
!= NULL
))
1499 - showing all commands
1500 - showing all classes and C is a help class
1501 - showing commands of THECLASS and C is not the help class */
1503 /* If we show the class_alias and C is an alias, do not recurse,
1504 as this would show the (possibly very long) not very useful
1505 list of sub-commands of the aliased command. */
1506 print_help_for_command
1508 recurse
&& (theclass
!= class_alias
|| c
->cmd_pointer
== nullptr),
1514 && (theclass
== class_user
|| theclass
== class_alias
)
1515 && c
->prefixlist
!= NULL
)
1517 /* User-defined commands or aliases may be subcommands. */
1518 help_cmd_list (*c
->prefixlist
, theclass
, recurse
, stream
);
1522 /* Do not show C or recurse on C, e.g. because C does not belong to
1523 THECLASS or because C is a help class. */
1528 /* Search the input clist for 'command'. Return the command if
1529 found (or NULL if not), and return the number of commands
1532 static struct cmd_list_element
*
1533 find_cmd (const char *command
, int len
, struct cmd_list_element
*clist
,
1534 int ignore_help_classes
, int *nfound
)
1536 struct cmd_list_element
*found
, *c
;
1540 for (c
= clist
; c
; c
= c
->next
)
1541 if (!strncmp (command
, c
->name
, len
)
1542 && (!ignore_help_classes
|| c
->func
))
1546 if (c
->name
[len
] == '\0')
1555 /* Return the length of command name in TEXT. */
1558 find_command_name_length (const char *text
)
1560 const char *p
= text
;
1562 /* Treating underscores as part of command words is important
1563 so that "set args_foo()" doesn't get interpreted as
1564 "set args _foo()". */
1565 /* Some characters are only used for TUI specific commands.
1566 However, they are always allowed for the sake of consistency.
1568 Note that this is larger than the character set allowed when
1569 creating user-defined commands. */
1571 /* Recognize the single character commands so that, e.g., "!ls"
1572 works as expected. */
1573 if (*p
== '!' || *p
== '|')
1576 while (valid_cmd_char_p (*p
)
1577 /* Characters used by TUI specific commands. */
1578 || *p
== '+' || *p
== '<' || *p
== '>' || *p
== '$')
1584 /* See command.h. */
1587 valid_cmd_char_p (int c
)
1589 /* Alas "42" is a legitimate user-defined command.
1590 In the interests of not breaking anything we preserve that. */
1592 return isalnum (c
) || c
== '-' || c
== '_' || c
== '.';
1595 /* See command.h. */
1598 valid_user_defined_cmd_name_p (const char *name
)
1605 for (p
= name
; *p
!= '\0'; ++p
)
1607 if (valid_cmd_char_p (*p
))
1616 /* This routine takes a line of TEXT and a CLIST in which to start the
1617 lookup. When it returns it will have incremented the text pointer past
1618 the section of text it matched, set *RESULT_LIST to point to the list in
1619 which the last word was matched, and will return a pointer to the cmd
1620 list element which the text matches. It will return NULL if no match at
1621 all was possible. It will return -1 (cast appropriately, ick) if ambigous
1622 matches are possible; in this case *RESULT_LIST will be set to point to
1623 the list in which there are ambiguous choices (and *TEXT will be set to
1624 the ambiguous text string).
1626 if DEFAULT_ARGS is not null, *DEFAULT_ARGS is set to the found command
1627 default args (possibly empty).
1629 If the located command was an abbreviation, this routine returns the base
1630 command of the abbreviation. Note that *DEFAULT_ARGS will contain the
1631 default args defined for the alias.
1633 It does no error reporting whatsoever; control will always return
1634 to the superior routine.
1636 In the case of an ambiguous return (-1), *RESULT_LIST will be set to point
1637 at the prefix_command (ie. the best match) *or* (special case) will be NULL
1638 if no prefix command was ever found. For example, in the case of "info a",
1639 "info" matches without ambiguity, but "a" could be "args" or "address", so
1640 *RESULT_LIST is set to the cmd_list_element for "info". So in this case
1641 RESULT_LIST should not be interpreted as a pointer to the beginning of a
1642 list; it simply points to a specific command. In the case of an ambiguous
1643 return *TEXT is advanced past the last non-ambiguous prefix (e.g.
1644 "info t" can be "info types" or "info target"; upon return *TEXT has been
1645 advanced past "info ").
1647 If RESULT_LIST is NULL, don't set *RESULT_LIST (but don't otherwise
1648 affect the operation).
1650 This routine does *not* modify the text pointed to by TEXT.
1652 If IGNORE_HELP_CLASSES is nonzero, ignore any command list elements which
1653 are actually help classes rather than commands (i.e. the function field of
1654 the struct cmd_list_element is NULL). */
1656 struct cmd_list_element
*
1657 lookup_cmd_1 (const char **text
, struct cmd_list_element
*clist
,
1658 struct cmd_list_element
**result_list
, std::string
*default_args
,
1659 int ignore_help_classes
)
1663 struct cmd_list_element
*found
, *c
;
1664 bool found_alias
= false;
1665 const char *line
= *text
;
1667 while (**text
== ' ' || **text
== '\t')
1670 /* Identify the name of the command. */
1671 len
= find_command_name_length (*text
);
1673 /* If nothing but whitespace, return 0. */
1677 /* *text and p now bracket the first command word to lookup (and
1678 it's length is len). We copy this into a local temporary. */
1681 command
= (char *) alloca (len
+ 1);
1682 memcpy (command
, *text
, len
);
1683 command
[len
] = '\0';
1688 found
= find_cmd (command
, len
, clist
, ignore_help_classes
, &nfound
);
1690 /* If nothing matches, we have a simple failure. */
1696 if (result_list
!= nullptr)
1697 /* Will be modified in calling routine
1698 if we know what the prefix command is. */
1700 if (default_args
!= nullptr)
1701 *default_args
= std::string ();
1702 return CMD_LIST_AMBIGUOUS
; /* Ambiguous. */
1705 /* We've matched something on this list. Move text pointer forward. */
1709 if (found
->cmd_pointer
)
1711 /* We drop the alias (abbreviation) in favor of the command it
1712 is pointing to. If the alias is deprecated, though, we need to
1713 warn the user about it before we drop it. Note that while we
1714 are warning about the alias, we may also warn about the command
1715 itself and we will adjust the appropriate DEPRECATED_WARN_USER
1718 if (found
->deprecated_warn_user
)
1719 deprecated_cmd_warning (line
);
1721 /* Return the default_args of the alias, not the default_args
1722 of the command it is pointing to. */
1723 if (default_args
!= nullptr)
1724 *default_args
= found
->default_args
;
1725 found
= found
->cmd_pointer
;
1728 /* If we found a prefix command, keep looking. */
1730 if (found
->prefixlist
)
1732 c
= lookup_cmd_1 (text
, *found
->prefixlist
, result_list
,
1733 default_args
, ignore_help_classes
);
1736 /* Didn't find anything; this is as far as we got. */
1737 if (result_list
!= nullptr)
1738 *result_list
= clist
;
1739 if (!found_alias
&& default_args
!= nullptr)
1740 *default_args
= found
->default_args
;
1743 else if (c
== CMD_LIST_AMBIGUOUS
)
1745 /* We've gotten this far properly, but the next step is
1746 ambiguous. We need to set the result list to the best
1747 we've found (if an inferior hasn't already set it). */
1748 if (result_list
!= nullptr)
1750 /* This used to say *result_list = *found->prefixlist.
1751 If that was correct, need to modify the documentation
1752 at the top of this function to clarify what is
1753 supposed to be going on. */
1754 *result_list
= found
;
1755 /* For ambiguous commands, do not return any default_args args. */
1756 if (default_args
!= nullptr)
1757 *default_args
= std::string ();
1768 if (result_list
!= nullptr)
1769 *result_list
= clist
;
1770 if (!found_alias
&& default_args
!= nullptr)
1771 *default_args
= found
->default_args
;
1776 /* All this hair to move the space to the front of cmdtype */
1779 undef_cmd_error (const char *cmdtype
, const char *q
)
1781 error (_("Undefined %scommand: \"%s\". Try \"help%s%.*s\"."),
1784 *cmdtype
? " " : "",
1785 (int) strlen (cmdtype
) - 1,
1789 /* Look up the contents of *LINE as a command in the command list LIST.
1790 LIST is a chain of struct cmd_list_element's.
1791 If it is found, return the struct cmd_list_element for that command,
1792 update *LINE to point after the command name, at the first argument
1793 and update *DEFAULT_ARGS (if DEFAULT_ARGS is not null) to the default
1794 args to prepend to the user provided args when running the command.
1795 Note that if the found cmd_list_element is found via an alias,
1796 the default args of the alias are returned.
1798 If not found, call error if ALLOW_UNKNOWN is zero
1799 otherwise (or if error returns) return zero.
1800 Call error if specified command is ambiguous,
1801 unless ALLOW_UNKNOWN is negative.
1802 CMDTYPE precedes the word "command" in the error message.
1804 If IGNORE_HELP_CLASSES is nonzero, ignore any command list
1805 elements which are actually help classes rather than commands (i.e.
1806 the function field of the struct cmd_list_element is 0). */
1808 struct cmd_list_element
*
1809 lookup_cmd (const char **line
, struct cmd_list_element
*list
,
1810 const char *cmdtype
,
1811 std::string
*default_args
,
1812 int allow_unknown
, int ignore_help_classes
)
1814 struct cmd_list_element
*last_list
= 0;
1815 struct cmd_list_element
*c
;
1817 /* Note: Do not remove trailing whitespace here because this
1818 would be wrong for complete_command. Jim Kingdon */
1821 error (_("Lack of needed %scommand"), cmdtype
);
1823 c
= lookup_cmd_1 (line
, list
, &last_list
, default_args
, ignore_help_classes
);
1830 int len
= find_command_name_length (*line
);
1832 q
= (char *) alloca (len
+ 1);
1833 strncpy (q
, *line
, len
);
1835 undef_cmd_error (cmdtype
, q
);
1840 else if (c
== CMD_LIST_AMBIGUOUS
)
1842 /* Ambigous. Local values should be off prefixlist or called
1844 int local_allow_unknown
= (last_list
? last_list
->allow_unknown
:
1846 const char *local_cmdtype
= last_list
? last_list
->prefixname
: cmdtype
;
1847 struct cmd_list_element
*local_list
=
1848 (last_list
? *(last_list
->prefixlist
) : list
);
1850 if (local_allow_unknown
< 0)
1853 return last_list
; /* Found something. */
1855 return 0; /* Found nothing. */
1859 /* Report as error. */
1864 ((*line
)[amb_len
] && (*line
)[amb_len
] != ' '
1865 && (*line
)[amb_len
] != '\t');
1870 for (c
= local_list
; c
; c
= c
->next
)
1871 if (!strncmp (*line
, c
->name
, amb_len
))
1873 if (strlen (ambbuf
) + strlen (c
->name
) + 6
1874 < (int) sizeof ambbuf
)
1876 if (strlen (ambbuf
))
1877 strcat (ambbuf
, ", ");
1878 strcat (ambbuf
, c
->name
);
1882 strcat (ambbuf
, "..");
1886 error (_("Ambiguous %scommand \"%s\": %s."), local_cmdtype
,
1892 if (c
->type
== set_cmd
&& **line
!= '\0' && !isspace (**line
))
1893 error (_("Argument must be preceded by space."));
1895 /* We've got something. It may still not be what the caller
1896 wants (if this command *needs* a subcommand). */
1897 while (**line
== ' ' || **line
== '\t')
1900 if (c
->prefixlist
&& **line
&& !c
->allow_unknown
)
1901 undef_cmd_error (c
->prefixname
, *line
);
1903 /* Seems to be what he wants. Return it. */
1909 /* We are here presumably because an alias or command in TEXT is
1910 deprecated and a warning message should be generated. This
1911 function decodes TEXT and potentially generates a warning message
1914 Example for 'set endian big' which has a fictitious alias 'seb'.
1916 If alias wasn't used in TEXT, and the command is deprecated:
1917 "warning: 'set endian big' is deprecated."
1919 If alias was used, and only the alias is deprecated:
1920 "warning: 'seb' an alias for the command 'set endian big' is deprecated."
1922 If alias was used and command is deprecated (regardless of whether
1923 the alias itself is deprecated:
1925 "warning: 'set endian big' (seb) is deprecated."
1927 After the message has been sent, clear the appropriate flags in the
1928 command and/or the alias so the user is no longer bothered.
1932 deprecated_cmd_warning (const char *text
)
1934 struct cmd_list_element
*alias
= NULL
;
1935 struct cmd_list_element
*prefix_cmd
= NULL
;
1936 struct cmd_list_element
*cmd
= NULL
;
1938 if (!lookup_cmd_composition (text
, &alias
, &prefix_cmd
, &cmd
))
1939 /* Return if text doesn't evaluate to a command. */
1942 if (!((alias
? alias
->deprecated_warn_user
: 0)
1943 || cmd
->deprecated_warn_user
) )
1944 /* Return if nothing is deprecated. */
1947 printf_filtered ("Warning:");
1949 if (alias
&& !cmd
->cmd_deprecated
)
1950 printf_filtered (" '%s', an alias for the", alias
->name
);
1952 printf_filtered (" command '");
1955 printf_filtered ("%s", prefix_cmd
->prefixname
);
1957 printf_filtered ("%s", cmd
->name
);
1959 if (alias
&& cmd
->cmd_deprecated
)
1960 printf_filtered ("' (%s) is deprecated.\n", alias
->name
);
1962 printf_filtered ("' is deprecated.\n");
1965 /* If it is only the alias that is deprecated, we want to indicate
1966 the new alias, otherwise we'll indicate the new command. */
1968 if (alias
&& !cmd
->cmd_deprecated
)
1970 if (alias
->replacement
)
1971 printf_filtered ("Use '%s'.\n\n", alias
->replacement
);
1973 printf_filtered ("No alternative known.\n\n");
1977 if (cmd
->replacement
)
1978 printf_filtered ("Use '%s'.\n\n", cmd
->replacement
);
1980 printf_filtered ("No alternative known.\n\n");
1983 /* We've warned you, now we'll keep quiet. */
1985 alias
->deprecated_warn_user
= 0;
1987 cmd
->deprecated_warn_user
= 0;
1991 /* Look up the contents of TEXT as a command in the command list 'cmdlist'.
1992 Return 1 on success, 0 on failure.
1994 If TEXT refers to an alias, *ALIAS will point to that alias.
1996 If TEXT is a subcommand (i.e. one that is preceded by a prefix
1997 command) set *PREFIX_CMD.
1999 Set *CMD to point to the command TEXT indicates.
2001 If any of *ALIAS, *PREFIX_CMD, or *CMD cannot be determined or do not
2002 exist, they are NULL when we return.
2006 lookup_cmd_composition (const char *text
,
2007 struct cmd_list_element
**alias
,
2008 struct cmd_list_element
**prefix_cmd
,
2009 struct cmd_list_element
**cmd
)
2013 struct cmd_list_element
*cur_list
;
2014 struct cmd_list_element
*prev_cmd
;
2022 text
= skip_spaces (text
);
2026 /* Go through as many command lists as we need to,
2027 to find the command TEXT refers to. */
2031 /* Identify the name of the command. */
2032 len
= find_command_name_length (text
);
2034 /* If nothing but whitespace, return. */
2038 /* TEXT is the start of the first command word to lookup (and
2039 it's length is LEN). We copy this into a local temporary. */
2041 command
= (char *) alloca (len
+ 1);
2042 memcpy (command
, text
, len
);
2043 command
[len
] = '\0';
2048 *cmd
= find_cmd (command
, len
, cur_list
, 1, &nfound
);
2050 if (*cmd
== CMD_LIST_AMBIGUOUS
)
2052 return 0; /* ambiguous */
2056 return 0; /* nothing found */
2059 if ((*cmd
)->cmd_pointer
)
2061 /* cmd was actually an alias, we note that an alias was
2062 used (by assigning *ALIAS) and we set *CMD. */
2064 *cmd
= (*cmd
)->cmd_pointer
;
2066 *prefix_cmd
= prev_cmd
;
2070 text
= skip_spaces (text
);
2072 if ((*cmd
)->prefixlist
&& *text
!= '\0')
2073 cur_list
= *(*cmd
)->prefixlist
;
2079 /* Helper function for SYMBOL_COMPLETION_FUNCTION. */
2081 /* Return a vector of char pointers which point to the different
2082 possible completions in LIST of TEXT.
2084 WORD points in the same buffer as TEXT, and completions should be
2085 returned relative to this position. For example, suppose TEXT is
2086 "foo" and we want to complete to "foobar". If WORD is "oo", return
2087 "oobar"; if WORD is "baz/foo", return "baz/foobar". */
2090 complete_on_cmdlist (struct cmd_list_element
*list
,
2091 completion_tracker
&tracker
,
2092 const char *text
, const char *word
,
2093 int ignore_help_classes
)
2095 struct cmd_list_element
*ptr
;
2096 int textlen
= strlen (text
);
2098 int saw_deprecated_match
= 0;
2100 /* We do one or two passes. In the first pass, we skip deprecated
2101 commands. If we see no matching commands in the first pass, and
2102 if we did happen to see a matching deprecated command, we do
2103 another loop to collect those. */
2104 for (pass
= 0; pass
< 2; ++pass
)
2106 bool got_matches
= false;
2108 for (ptr
= list
; ptr
; ptr
= ptr
->next
)
2109 if (!strncmp (ptr
->name
, text
, textlen
)
2110 && !ptr
->abbrev_flag
2111 && (!ignore_help_classes
|| ptr
->func
2112 || ptr
->prefixlist
))
2116 if (ptr
->cmd_deprecated
)
2118 saw_deprecated_match
= 1;
2123 tracker
.add_completion
2124 (make_completion_match_str (ptr
->name
, text
, word
));
2131 /* If we saw no matching deprecated commands in the first pass,
2133 if (!saw_deprecated_match
)
2138 /* Helper function for SYMBOL_COMPLETION_FUNCTION. */
2140 /* Add the different possible completions in ENUMLIST of TEXT.
2142 WORD points in the same buffer as TEXT, and completions should be
2143 returned relative to this position. For example, suppose TEXT is "foo"
2144 and we want to complete to "foobar". If WORD is "oo", return
2145 "oobar"; if WORD is "baz/foo", return "baz/foobar". */
2148 complete_on_enum (completion_tracker
&tracker
,
2149 const char *const *enumlist
,
2150 const char *text
, const char *word
)
2152 int textlen
= strlen (text
);
2156 for (i
= 0; (name
= enumlist
[i
]) != NULL
; i
++)
2157 if (strncmp (name
, text
, textlen
) == 0)
2158 tracker
.add_completion (make_completion_match_str (name
, text
, word
));
2162 /* Check function pointer. */
2164 cmd_func_p (struct cmd_list_element
*cmd
)
2166 return (cmd
->func
!= NULL
);
2170 /* Call the command function. */
2172 cmd_func (struct cmd_list_element
*cmd
, const char *args
, int from_tty
)
2174 if (cmd_func_p (cmd
))
2176 gdb::optional
<scoped_restore_tmpl
<int>> restore_suppress
;
2178 if (cmd
->suppress_notification
!= NULL
)
2179 restore_suppress
.emplace (cmd
->suppress_notification
, 1);
2181 (*cmd
->func
) (cmd
, args
, from_tty
);
2184 error (_("Invalid command"));
2188 cli_user_command_p (struct cmd_list_element
*cmd
)
2190 return (cmd
->theclass
== class_user
2191 && (cmd
->func
== do_const_cfunc
|| cmd
->func
== do_sfunc
));