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_all (struct ui_file
*stream
);
48 /* Look up a command whose 'prefixlist' is KEY. Return the command if found,
49 otherwise return NULL. */
51 static struct cmd_list_element
*
52 lookup_cmd_for_prefixlist (struct cmd_list_element
**key
,
53 struct cmd_list_element
*list
)
55 struct cmd_list_element
*p
= NULL
;
57 for (p
= list
; p
!= NULL
; p
= p
->next
)
59 struct cmd_list_element
*q
;
61 if (p
->prefixlist
== NULL
)
63 else if (p
->prefixlist
== key
)
65 /* If we found an alias, we must return the aliased
67 return p
->cmd_pointer
? p
->cmd_pointer
: p
;
70 q
= lookup_cmd_for_prefixlist (key
, *(p
->prefixlist
));
79 print_help_for_command (struct cmd_list_element
*c
, const char *prefix
,
80 int recurse
, struct ui_file
*stream
);
83 /* Set the callback function for the specified command. For each both
84 the commands callback and func() are set. The latter set to a
85 bounce function (unless cfunc / sfunc is NULL that is). */
88 do_const_cfunc (struct cmd_list_element
*c
, const char *args
, int from_tty
)
90 c
->function
.const_cfunc (args
, from_tty
);
94 set_cmd_cfunc (struct cmd_list_element
*cmd
, cmd_const_cfunc_ftype
*cfunc
)
99 cmd
->func
= do_const_cfunc
;
100 cmd
->function
.const_cfunc
= cfunc
;
104 do_sfunc (struct cmd_list_element
*c
, const char *args
, int from_tty
)
106 c
->function
.sfunc (args
, from_tty
, c
);
110 set_cmd_sfunc (struct cmd_list_element
*cmd
, cmd_const_sfunc_ftype
*sfunc
)
115 cmd
->func
= do_sfunc
;
116 cmd
->function
.sfunc
= sfunc
;
120 cmd_cfunc_eq (struct cmd_list_element
*cmd
, cmd_const_cfunc_ftype
*cfunc
)
122 return cmd
->func
== do_const_cfunc
&& cmd
->function
.const_cfunc
== cfunc
;
126 set_cmd_context (struct cmd_list_element
*cmd
, void *context
)
128 cmd
->context
= context
;
132 get_cmd_context (struct cmd_list_element
*cmd
)
138 cmd_type (struct cmd_list_element
*cmd
)
144 set_cmd_completer (struct cmd_list_element
*cmd
, completer_ftype
*completer
)
146 cmd
->completer
= completer
; /* Ok. */
149 /* See definition in commands.h. */
152 set_cmd_completer_handle_brkchars (struct cmd_list_element
*cmd
,
153 completer_handle_brkchars_ftype
*func
)
155 cmd
->completer_handle_brkchars
= func
;
158 /* Add element named NAME.
159 Space for NAME and DOC must be allocated by the caller.
160 CLASS is the top level category into which commands are broken down
162 FUN should be the function to execute the command;
163 it will get a character string as argument, with leading
164 and trailing blanks already eliminated.
166 DOC is a documentation string for the command.
167 Its first line should be a complete sentence.
168 It should start with ? for a command that is an abbreviation
169 or with * for a command that most users don't need to know about.
171 Add this command to command list *LIST.
173 Returns a pointer to the added command (not necessarily the head
176 static struct cmd_list_element
*
177 do_add_cmd (const char *name
, enum command_class theclass
,
178 const char *doc
, struct cmd_list_element
**list
)
180 struct cmd_list_element
*c
= new struct cmd_list_element (name
, theclass
,
182 struct cmd_list_element
*p
, *iter
;
184 /* Turn each alias of the old command into an alias of the new
186 c
->aliases
= delete_cmd (name
, list
, &c
->hook_pre
, &c
->hookee_pre
,
187 &c
->hook_post
, &c
->hookee_post
);
188 for (iter
= c
->aliases
; iter
; iter
= iter
->alias_chain
)
189 iter
->cmd_pointer
= c
;
191 c
->hook_pre
->hookee_pre
= c
;
193 c
->hookee_pre
->hook_pre
= c
;
195 c
->hook_post
->hookee_post
= c
;
197 c
->hookee_post
->hook_post
= c
;
199 if (*list
== NULL
|| strcmp ((*list
)->name
, name
) >= 0)
207 while (p
->next
&& strcmp (p
->next
->name
, name
) <= 0)
215 /* Search the prefix cmd of C, and assigns it to C->prefix.
216 See also add_prefix_cmd and update_prefix_field_of_prefixed_commands. */
217 struct cmd_list_element
*prefixcmd
= lookup_cmd_for_prefixlist (list
,
219 c
->prefix
= prefixcmd
;
225 struct cmd_list_element
*
226 add_cmd (const char *name
, enum command_class theclass
,
227 const char *doc
, struct cmd_list_element
**list
)
229 cmd_list_element
*result
= do_add_cmd (name
, theclass
, doc
, list
);
231 result
->function
.const_cfunc
= NULL
;
235 struct cmd_list_element
*
236 add_cmd (const char *name
, enum command_class theclass
,
237 cmd_const_cfunc_ftype
*fun
,
238 const char *doc
, struct cmd_list_element
**list
)
240 cmd_list_element
*result
= do_add_cmd (name
, theclass
, doc
, list
);
241 set_cmd_cfunc (result
, fun
);
245 /* Add an element with a suppress notification to the LIST of commands. */
247 struct cmd_list_element
*
248 add_cmd_suppress_notification (const char *name
, enum command_class theclass
,
249 cmd_const_cfunc_ftype
*fun
, const char *doc
,
250 struct cmd_list_element
**list
,
251 int *suppress_notification
)
253 struct cmd_list_element
*element
;
255 element
= add_cmd (name
, theclass
, fun
, doc
, list
);
256 element
->suppress_notification
= suppress_notification
;
262 /* Deprecates a command CMD.
263 REPLACEMENT is the name of the command which should be used in
264 place of this command, or NULL if no such command exists.
266 This function does not check to see if command REPLACEMENT exists
267 since gdb may not have gotten around to adding REPLACEMENT when
268 this function is called.
270 Returns a pointer to the deprecated command. */
272 struct cmd_list_element
*
273 deprecate_cmd (struct cmd_list_element
*cmd
, const char *replacement
)
275 cmd
->cmd_deprecated
= 1;
276 cmd
->deprecated_warn_user
= 1;
278 if (replacement
!= NULL
)
279 cmd
->replacement
= replacement
;
281 cmd
->replacement
= NULL
;
286 struct cmd_list_element
*
287 add_alias_cmd (const char *name
, cmd_list_element
*old
,
288 enum command_class theclass
, int abbrev_flag
,
289 struct cmd_list_element
**list
)
293 struct cmd_list_element
*prehook
, *prehookee
, *posthook
, *posthookee
;
294 struct cmd_list_element
*aliases
= delete_cmd (name
, list
,
295 &prehook
, &prehookee
,
296 &posthook
, &posthookee
);
298 /* If this happens, it means a programmer error somewhere. */
299 gdb_assert (!aliases
&& !prehook
&& !prehookee
300 && !posthook
&& ! posthookee
);
304 struct cmd_list_element
*c
= add_cmd (name
, theclass
, old
->doc
, list
);
306 /* If OLD->DOC can be freed, we should make another copy. */
307 if (old
->doc_allocated
)
309 c
->doc
= xstrdup (old
->doc
);
310 c
->doc_allocated
= 1;
312 /* NOTE: Both FUNC and all the FUNCTIONs need to be copied. */
314 c
->function
= old
->function
;
315 c
->prefixlist
= old
->prefixlist
;
316 c
->prefixname
= old
->prefixname
;
317 c
->allow_unknown
= old
->allow_unknown
;
318 c
->abbrev_flag
= abbrev_flag
;
319 c
->cmd_pointer
= old
;
320 c
->alias_chain
= old
->aliases
;
326 struct cmd_list_element
*
327 add_alias_cmd (const char *name
, const char *oldname
,
328 enum command_class theclass
, int abbrev_flag
,
329 struct cmd_list_element
**list
)
332 struct cmd_list_element
*old
;
335 old
= lookup_cmd (&tmp
, *list
, "", 1, 1);
337 return add_alias_cmd (name
, old
, theclass
, abbrev_flag
, list
);
341 /* Update the prefix field of all sub-commands of the prefix command C.
342 We must do this when a prefix command is defined as the GDB init sequence
343 does not guarantee that a prefix command is created before its sub-commands.
344 For example, break-catch-sig.c initialization runs before breakpoint.c
345 initialization, but it is breakpoint.c that creates the "catch" command used
346 by the "catch signal" command created by break-catch-sig.c. */
349 update_prefix_field_of_prefixed_commands (struct cmd_list_element
*c
)
351 for (cmd_list_element
*p
= *c
->prefixlist
; p
!= NULL
; p
= p
->next
)
355 /* We must recursively update the prefix field to cover
356 e.g. 'info auto-load libthread-db' where the creation
361 In such a case, when 'auto-load' was created by do_add_cmd,
362 the 'libthread-db' prefix field could not be updated, as the
363 'auto-load' command was not yet reachable by
364 lookup_cmd_for_prefixlist (list, cmdlist)
365 that searches from the top level 'cmdlist'. */
366 if (p
->prefixlist
!= nullptr)
367 update_prefix_field_of_prefixed_commands (p
);
372 /* Like add_cmd but adds an element for a command prefix: a name that
373 should be followed by a subcommand to be looked up in another
374 command list. PREFIXLIST should be the address of the variable
375 containing that list. */
377 struct cmd_list_element
*
378 add_prefix_cmd (const char *name
, enum command_class theclass
,
379 cmd_const_cfunc_ftype
*fun
,
380 const char *doc
, struct cmd_list_element
**prefixlist
,
381 const char *prefixname
, int allow_unknown
,
382 struct cmd_list_element
**list
)
384 struct cmd_list_element
*c
= add_cmd (name
, theclass
, fun
, doc
, list
);
386 c
->prefixlist
= prefixlist
;
387 c
->prefixname
= prefixname
;
388 c
->allow_unknown
= allow_unknown
;
390 /* Now that prefix command C is defined, we need to set the prefix field
391 of all prefixed commands that were defined before C itself was defined. */
392 update_prefix_field_of_prefixed_commands (c
);
397 /* A helper function for add_basic_prefix_cmd. This is a command
398 function that just forwards to help_list. */
401 do_prefix_cmd (const char *args
, int from_tty
, struct cmd_list_element
*c
)
403 /* Look past all aliases. */
404 while (c
->cmd_pointer
!= nullptr)
407 help_list (*c
->prefixlist
, c
->prefixname
, all_commands
, gdb_stdout
);
412 struct cmd_list_element
*
413 add_basic_prefix_cmd (const char *name
, enum command_class theclass
,
414 const char *doc
, struct cmd_list_element
**prefixlist
,
415 const char *prefixname
, int allow_unknown
,
416 struct cmd_list_element
**list
)
418 struct cmd_list_element
*cmd
= add_prefix_cmd (name
, theclass
, nullptr,
419 doc
, prefixlist
, prefixname
,
420 allow_unknown
, list
);
421 set_cmd_sfunc (cmd
, do_prefix_cmd
);
425 /* A helper function for add_show_prefix_cmd. This is a command
426 function that just forwards to cmd_show_list. */
429 do_show_prefix_cmd (const char *args
, int from_tty
, struct cmd_list_element
*c
)
431 cmd_show_list (*c
->prefixlist
, from_tty
);
436 struct cmd_list_element
*
437 add_show_prefix_cmd (const char *name
, enum command_class theclass
,
438 const char *doc
, struct cmd_list_element
**prefixlist
,
439 const char *prefixname
, int allow_unknown
,
440 struct cmd_list_element
**list
)
442 struct cmd_list_element
*cmd
= add_prefix_cmd (name
, theclass
, nullptr,
443 doc
, prefixlist
, prefixname
,
444 allow_unknown
, list
);
445 set_cmd_sfunc (cmd
, do_show_prefix_cmd
);
449 /* Like ADD_PREFIX_CMD but sets the suppress_notification pointer on the
450 new command list element. */
452 struct cmd_list_element
*
453 add_prefix_cmd_suppress_notification
454 (const char *name
, enum command_class theclass
,
455 cmd_const_cfunc_ftype
*fun
,
456 const char *doc
, struct cmd_list_element
**prefixlist
,
457 const char *prefixname
, int allow_unknown
,
458 struct cmd_list_element
**list
,
459 int *suppress_notification
)
461 struct cmd_list_element
*element
462 = add_prefix_cmd (name
, theclass
, fun
, doc
, prefixlist
,
463 prefixname
, allow_unknown
, list
);
464 element
->suppress_notification
= suppress_notification
;
468 /* Like add_prefix_cmd but sets the abbrev_flag on the new command. */
470 struct cmd_list_element
*
471 add_abbrev_prefix_cmd (const char *name
, enum command_class theclass
,
472 cmd_const_cfunc_ftype
*fun
, const char *doc
,
473 struct cmd_list_element
**prefixlist
,
474 const char *prefixname
,
475 int allow_unknown
, struct cmd_list_element
**list
)
477 struct cmd_list_element
*c
= add_cmd (name
, theclass
, fun
, doc
, list
);
479 c
->prefixlist
= prefixlist
;
480 c
->prefixname
= prefixname
;
481 c
->allow_unknown
= allow_unknown
;
486 /* This is an empty "cfunc". */
488 not_just_help_class_command (const char *args
, int from_tty
)
492 /* This is an empty "sfunc". */
495 empty_sfunc (const char *args
, int from_tty
, struct cmd_list_element
*c
)
499 /* Add element named NAME to command list LIST (the list for set/show
500 or some sublist thereof).
501 TYPE is set_cmd or show_cmd.
502 CLASS is as in add_cmd.
503 VAR_TYPE is the kind of thing we are setting.
504 VAR is address of the variable being controlled by this command.
505 DOC is the documentation string. */
507 static struct cmd_list_element
*
508 add_set_or_show_cmd (const char *name
,
510 enum command_class theclass
,
514 struct cmd_list_element
**list
)
516 struct cmd_list_element
*c
= add_cmd (name
, theclass
, doc
, list
);
518 gdb_assert (type
== set_cmd
|| type
== show_cmd
);
520 c
->var_type
= var_type
;
522 /* This needs to be something besides NULL so that this isn't
523 treated as a help class. */
524 set_cmd_sfunc (c
, empty_sfunc
);
528 /* Add element named NAME to both the command SET_LIST and SHOW_LIST.
529 CLASS is as in add_cmd. VAR_TYPE is the kind of thing we are
530 setting. VAR is address of the variable being controlled by this
531 command. SET_FUNC and SHOW_FUNC are the callback functions (if
532 non-NULL). SET_DOC, SHOW_DOC and HELP_DOC are the documentation
533 strings. PRINT the format string to print the value. SET_RESULT
534 and SHOW_RESULT, if not NULL, are set to the resulting command
538 add_setshow_cmd_full (const char *name
,
539 enum command_class theclass
,
540 var_types var_type
, void *var
,
541 const char *set_doc
, const char *show_doc
,
542 const char *help_doc
,
543 cmd_const_sfunc_ftype
*set_func
,
544 show_value_ftype
*show_func
,
545 struct cmd_list_element
**set_list
,
546 struct cmd_list_element
**show_list
,
547 struct cmd_list_element
**set_result
,
548 struct cmd_list_element
**show_result
)
550 struct cmd_list_element
*set
;
551 struct cmd_list_element
*show
;
555 if (help_doc
!= NULL
)
557 full_set_doc
= xstrprintf ("%s\n%s", set_doc
, help_doc
);
558 full_show_doc
= xstrprintf ("%s\n%s", show_doc
, help_doc
);
562 full_set_doc
= xstrdup (set_doc
);
563 full_show_doc
= xstrdup (show_doc
);
565 set
= add_set_or_show_cmd (name
, set_cmd
, theclass
, var_type
, var
,
566 full_set_doc
, set_list
);
567 set
->doc_allocated
= 1;
569 if (set_func
!= NULL
)
570 set_cmd_sfunc (set
, set_func
);
572 show
= add_set_or_show_cmd (name
, show_cmd
, theclass
, var_type
, var
,
573 full_show_doc
, show_list
);
574 show
->doc_allocated
= 1;
575 show
->show_value_func
= show_func
;
576 /* Disable the default symbol completer. Doesn't make much sense
577 for the "show" command to complete on anything. */
578 set_cmd_completer (show
, nullptr);
580 if (set_result
!= NULL
)
582 if (show_result
!= NULL
)
586 /* Add element named NAME to command list LIST (the list for set or
587 some sublist thereof). CLASS is as in add_cmd. ENUMLIST is a list
588 of strings which may follow NAME. VAR is address of the variable
589 which will contain the matching string (from ENUMLIST). */
592 add_setshow_enum_cmd (const char *name
,
593 enum command_class theclass
,
594 const char *const *enumlist
,
597 const char *show_doc
,
598 const char *help_doc
,
599 cmd_const_sfunc_ftype
*set_func
,
600 show_value_ftype
*show_func
,
601 struct cmd_list_element
**set_list
,
602 struct cmd_list_element
**show_list
,
605 struct cmd_list_element
*c
, *show
;
607 add_setshow_cmd_full (name
, theclass
, var_enum
, var
,
608 set_doc
, show_doc
, help_doc
,
614 set_cmd_context (c
, context
);
615 set_cmd_context (show
, context
);
618 /* See cli-decode.h. */
619 const char * const auto_boolean_enums
[] = { "on", "off", "auto", NULL
};
621 /* Add an auto-boolean command named NAME to both the set and show
622 command list lists. CLASS is as in add_cmd. VAR is address of the
623 variable which will contain the value. DOC is the documentation
624 string. FUNC is the corresponding callback. */
626 add_setshow_auto_boolean_cmd (const char *name
,
627 enum command_class theclass
,
628 enum auto_boolean
*var
,
629 const char *set_doc
, const char *show_doc
,
630 const char *help_doc
,
631 cmd_const_sfunc_ftype
*set_func
,
632 show_value_ftype
*show_func
,
633 struct cmd_list_element
**set_list
,
634 struct cmd_list_element
**show_list
)
636 struct cmd_list_element
*c
;
638 add_setshow_cmd_full (name
, theclass
, var_auto_boolean
, var
,
639 set_doc
, show_doc
, help_doc
,
643 c
->enums
= auto_boolean_enums
;
646 /* See cli-decode.h. */
647 const char * const boolean_enums
[] = { "on", "off", NULL
};
649 /* Add element named NAME to both the set and show command LISTs (the
650 list for set/show or some sublist thereof). CLASS is as in
651 add_cmd. VAR is address of the variable which will contain the
652 value. SET_DOC and SHOW_DOC are the documentation strings.
653 Returns the new command element. */
656 add_setshow_boolean_cmd (const char *name
, enum command_class theclass
, bool *var
,
657 const char *set_doc
, const char *show_doc
,
658 const char *help_doc
,
659 cmd_const_sfunc_ftype
*set_func
,
660 show_value_ftype
*show_func
,
661 struct cmd_list_element
**set_list
,
662 struct cmd_list_element
**show_list
)
664 struct cmd_list_element
*c
;
666 add_setshow_cmd_full (name
, theclass
, var_boolean
, var
,
667 set_doc
, show_doc
, help_doc
,
671 c
->enums
= boolean_enums
;
676 /* Add element named NAME to both the set and show command LISTs (the
677 list for set/show or some sublist thereof). */
679 add_setshow_filename_cmd (const char *name
, enum command_class theclass
,
681 const char *set_doc
, const char *show_doc
,
682 const char *help_doc
,
683 cmd_const_sfunc_ftype
*set_func
,
684 show_value_ftype
*show_func
,
685 struct cmd_list_element
**set_list
,
686 struct cmd_list_element
**show_list
)
688 struct cmd_list_element
*set_result
;
690 add_setshow_cmd_full (name
, theclass
, var_filename
, var
,
691 set_doc
, show_doc
, help_doc
,
695 set_cmd_completer (set_result
, filename_completer
);
698 /* Add element named NAME to both the set and show command LISTs (the
699 list for set/show or some sublist thereof). */
701 add_setshow_string_cmd (const char *name
, enum command_class theclass
,
703 const char *set_doc
, const char *show_doc
,
704 const char *help_doc
,
705 cmd_const_sfunc_ftype
*set_func
,
706 show_value_ftype
*show_func
,
707 struct cmd_list_element
**set_list
,
708 struct cmd_list_element
**show_list
)
710 cmd_list_element
*set_cmd
;
712 add_setshow_cmd_full (name
, theclass
, var_string
, var
,
713 set_doc
, show_doc
, help_doc
,
718 /* Disable the default symbol completer. */
719 set_cmd_completer (set_cmd
, nullptr);
722 /* Add element named NAME to both the set and show command LISTs (the
723 list for set/show or some sublist thereof). */
724 struct cmd_list_element
*
725 add_setshow_string_noescape_cmd (const char *name
, enum command_class theclass
,
727 const char *set_doc
, const char *show_doc
,
728 const char *help_doc
,
729 cmd_const_sfunc_ftype
*set_func
,
730 show_value_ftype
*show_func
,
731 struct cmd_list_element
**set_list
,
732 struct cmd_list_element
**show_list
)
734 struct cmd_list_element
*set_cmd
;
736 add_setshow_cmd_full (name
, theclass
, var_string_noescape
, var
,
737 set_doc
, show_doc
, help_doc
,
742 /* Disable the default symbol completer. */
743 set_cmd_completer (set_cmd
, nullptr);
748 /* Add element named NAME to both the set and show command LISTs (the
749 list for set/show or some sublist thereof). */
751 add_setshow_optional_filename_cmd (const char *name
, enum command_class theclass
,
753 const char *set_doc
, const char *show_doc
,
754 const char *help_doc
,
755 cmd_const_sfunc_ftype
*set_func
,
756 show_value_ftype
*show_func
,
757 struct cmd_list_element
**set_list
,
758 struct cmd_list_element
**show_list
)
760 struct cmd_list_element
*set_result
;
762 add_setshow_cmd_full (name
, theclass
, var_optional_filename
, var
,
763 set_doc
, show_doc
, help_doc
,
768 set_cmd_completer (set_result
, filename_completer
);
772 /* Completes on literal "unlimited". Used by integer commands that
773 support a special "unlimited" value. */
776 integer_unlimited_completer (struct cmd_list_element
*ignore
,
777 completion_tracker
&tracker
,
778 const char *text
, const char *word
)
780 static const char * const keywords
[] =
786 complete_on_enum (tracker
, keywords
, text
, word
);
789 /* Add element named NAME to both the set and show command LISTs (the
790 list for set/show or some sublist thereof). CLASS is as in
791 add_cmd. VAR is address of the variable which will contain the
792 value. SET_DOC and SHOW_DOC are the documentation strings. This
793 function is only used in Python API. Please don't use it elsewhere. */
795 add_setshow_integer_cmd (const char *name
, enum command_class theclass
,
797 const char *set_doc
, const char *show_doc
,
798 const char *help_doc
,
799 cmd_const_sfunc_ftype
*set_func
,
800 show_value_ftype
*show_func
,
801 struct cmd_list_element
**set_list
,
802 struct cmd_list_element
**show_list
)
804 struct cmd_list_element
*set
;
806 add_setshow_cmd_full (name
, theclass
, var_integer
, var
,
807 set_doc
, show_doc
, help_doc
,
812 set_cmd_completer (set
, integer_unlimited_completer
);
815 /* Add element named NAME to both the set and show command LISTs (the
816 list for set/show or some sublist thereof). CLASS is as in
817 add_cmd. VAR is address of the variable which will contain the
818 value. SET_DOC and SHOW_DOC are the documentation strings. */
820 add_setshow_uinteger_cmd (const char *name
, enum command_class theclass
,
822 const char *set_doc
, const char *show_doc
,
823 const char *help_doc
,
824 cmd_const_sfunc_ftype
*set_func
,
825 show_value_ftype
*show_func
,
826 struct cmd_list_element
**set_list
,
827 struct cmd_list_element
**show_list
)
829 struct cmd_list_element
*set
;
831 add_setshow_cmd_full (name
, theclass
, var_uinteger
, var
,
832 set_doc
, show_doc
, help_doc
,
837 set_cmd_completer (set
, integer_unlimited_completer
);
840 /* Add element named NAME to both the set and show command LISTs (the
841 list for set/show or some sublist thereof). CLASS is as in
842 add_cmd. VAR is address of the variable which will contain the
843 value. SET_DOC and SHOW_DOC are the documentation strings. */
845 add_setshow_zinteger_cmd (const char *name
, enum command_class theclass
,
847 const char *set_doc
, const char *show_doc
,
848 const char *help_doc
,
849 cmd_const_sfunc_ftype
*set_func
,
850 show_value_ftype
*show_func
,
851 struct cmd_list_element
**set_list
,
852 struct cmd_list_element
**show_list
)
854 add_setshow_cmd_full (name
, theclass
, var_zinteger
, var
,
855 set_doc
, show_doc
, help_doc
,
862 add_setshow_zuinteger_unlimited_cmd (const char *name
,
863 enum command_class theclass
,
866 const char *show_doc
,
867 const char *help_doc
,
868 cmd_const_sfunc_ftype
*set_func
,
869 show_value_ftype
*show_func
,
870 struct cmd_list_element
**set_list
,
871 struct cmd_list_element
**show_list
)
873 struct cmd_list_element
*set
;
875 add_setshow_cmd_full (name
, theclass
, var_zuinteger_unlimited
, var
,
876 set_doc
, show_doc
, help_doc
,
881 set_cmd_completer (set
, integer_unlimited_completer
);
884 /* Add element named NAME to both the set and show command LISTs (the
885 list for set/show or some sublist thereof). CLASS is as in
886 add_cmd. VAR is address of the variable which will contain the
887 value. SET_DOC and SHOW_DOC are the documentation strings. */
889 add_setshow_zuinteger_cmd (const char *name
, enum command_class theclass
,
891 const char *set_doc
, const char *show_doc
,
892 const char *help_doc
,
893 cmd_const_sfunc_ftype
*set_func
,
894 show_value_ftype
*show_func
,
895 struct cmd_list_element
**set_list
,
896 struct cmd_list_element
**show_list
)
898 add_setshow_cmd_full (name
, theclass
, var_zuinteger
, var
,
899 set_doc
, show_doc
, help_doc
,
905 /* Remove the command named NAME from the command list. Return the
906 list commands which were aliased to the deleted command. If the
907 command had no aliases, return NULL. The various *HOOKs are set to
908 the pre- and post-hook commands for the deleted command. If the
909 command does not have a hook, the corresponding out parameter is
912 static struct cmd_list_element
*
913 delete_cmd (const char *name
, struct cmd_list_element
**list
,
914 struct cmd_list_element
**prehook
,
915 struct cmd_list_element
**prehookee
,
916 struct cmd_list_element
**posthook
,
917 struct cmd_list_element
**posthookee
)
919 struct cmd_list_element
*iter
;
920 struct cmd_list_element
**previous_chain_ptr
;
921 struct cmd_list_element
*aliases
= NULL
;
927 previous_chain_ptr
= list
;
929 for (iter
= *previous_chain_ptr
; iter
; iter
= *previous_chain_ptr
)
931 if (strcmp (iter
->name
, name
) == 0)
934 iter
->destroyer (iter
, iter
->context
);
935 if (iter
->hookee_pre
)
936 iter
->hookee_pre
->hook_pre
= 0;
937 *prehook
= iter
->hook_pre
;
938 *prehookee
= iter
->hookee_pre
;
939 if (iter
->hookee_post
)
940 iter
->hookee_post
->hook_post
= 0;
941 *posthook
= iter
->hook_post
;
942 *posthookee
= iter
->hookee_post
;
944 /* Update the link. */
945 *previous_chain_ptr
= iter
->next
;
947 aliases
= iter
->aliases
;
949 /* If this command was an alias, remove it from the list of
951 if (iter
->cmd_pointer
)
953 struct cmd_list_element
**prevp
= &iter
->cmd_pointer
->aliases
;
954 struct cmd_list_element
*a
= *prevp
;
958 prevp
= &a
->alias_chain
;
961 *prevp
= iter
->alias_chain
;
966 /* We won't see another command with the same name. */
970 previous_chain_ptr
= &iter
->next
;
976 /* Shorthands to the commands above. */
978 /* Add an element to the list of info subcommands. */
980 struct cmd_list_element
*
981 add_info (const char *name
, cmd_const_cfunc_ftype
*fun
, const char *doc
)
983 return add_cmd (name
, class_info
, fun
, doc
, &infolist
);
986 /* Add an alias to the list of info subcommands. */
988 struct cmd_list_element
*
989 add_info_alias (const char *name
, const char *oldname
, int abbrev_flag
)
991 return add_alias_cmd (name
, oldname
, class_run
, abbrev_flag
, &infolist
);
994 /* Add an element to the list of commands. */
996 struct cmd_list_element
*
997 add_com (const char *name
, enum command_class theclass
,
998 cmd_const_cfunc_ftype
*fun
,
1001 return add_cmd (name
, theclass
, fun
, doc
, &cmdlist
);
1004 /* Add an alias or abbreviation command to the list of commands. */
1006 struct cmd_list_element
*
1007 add_com_alias (const char *name
, const char *oldname
, enum command_class theclass
,
1010 return add_alias_cmd (name
, oldname
, theclass
, abbrev_flag
, &cmdlist
);
1013 /* Add an element with a suppress notification to the list of commands. */
1015 struct cmd_list_element
*
1016 add_com_suppress_notification (const char *name
, enum command_class theclass
,
1017 cmd_const_cfunc_ftype
*fun
, const char *doc
,
1018 int *suppress_notification
)
1020 return add_cmd_suppress_notification (name
, theclass
, fun
, doc
,
1021 &cmdlist
, suppress_notification
);
1024 /* If VERBOSE, print the full help for command C and highlight the
1025 documentation parts matching HIGHLIGHT,
1026 otherwise print only one-line help for command C. */
1029 print_doc_of_command (struct cmd_list_element
*c
, const char *prefix
,
1030 bool verbose
, compiled_regex
&highlight
,
1031 struct ui_file
*stream
)
1033 /* When printing the full documentation, add a line to separate
1034 this documentation from the previous command help, in the likely
1035 case that apropos finds several commands. */
1037 fputs_filtered ("\n", stream
);
1039 fprintf_styled (stream
, title_style
.style (),
1040 "%s%s", prefix
, c
->name
);
1041 fputs_filtered (" -- ", stream
);
1043 fputs_highlighted (c
->doc
, highlight
, stream
);
1045 print_doc_line (stream
, c
->doc
, false);
1046 fputs_filtered ("\n", stream
);
1049 /* Recursively walk the commandlist structures, and print out the
1050 documentation of commands that match our regex in either their
1051 name, or their documentation.
1052 If VERBOSE, prints the complete documentation and highlight the
1053 documentation parts matching REGEX, otherwise prints only
1057 apropos_cmd (struct ui_file
*stream
,
1058 struct cmd_list_element
*commandlist
,
1059 bool verbose
, compiled_regex
®ex
, const char *prefix
)
1061 struct cmd_list_element
*c
;
1064 /* Walk through the commands. */
1065 for (c
=commandlist
;c
;c
=c
->next
)
1067 returnvalue
= -1; /* Needed to avoid double printing. */
1068 if (c
->name
!= NULL
)
1070 size_t name_len
= strlen (c
->name
);
1072 /* Try to match against the name. */
1073 returnvalue
= regex
.search (c
->name
, name_len
, 0, name_len
, NULL
);
1074 if (returnvalue
>= 0)
1075 print_doc_of_command (c
, prefix
, verbose
, regex
, stream
);
1077 if (c
->doc
!= NULL
&& returnvalue
< 0)
1079 size_t doc_len
= strlen (c
->doc
);
1081 /* Try to match against documentation. */
1082 if (regex
.search (c
->doc
, doc_len
, 0, doc_len
, NULL
) >= 0)
1083 print_doc_of_command (c
, prefix
, verbose
, regex
, stream
);
1085 /* Check if this command has subcommands and is not an
1086 abbreviation. We skip listing subcommands of abbreviations
1087 in order to avoid duplicates in the output. */
1088 if (c
->prefixlist
!= NULL
&& !c
->abbrev_flag
)
1090 /* Recursively call ourselves on the subcommand list,
1091 passing the right prefix in. */
1092 apropos_cmd (stream
, *c
->prefixlist
, verbose
, regex
, c
->prefixname
);
1097 /* This command really has to deal with two things:
1098 1) I want documentation on *this string* (usually called by
1099 "help commandname").
1101 2) I want documentation on *this list* (usually called by giving a
1102 command that requires subcommands. Also called by saying just
1105 I am going to split this into two separate commands, help_cmd and
1109 help_cmd (const char *command
, struct ui_file
*stream
)
1111 struct cmd_list_element
*c
;
1115 help_list (cmdlist
, "", all_classes
, stream
);
1119 if (strcmp (command
, "all") == 0)
1125 c
= lookup_cmd (&command
, cmdlist
, "", 0, 0);
1130 /* There are three cases here.
1131 If c->prefixlist is nonzero, we have a prefix command.
1132 Print its documentation, then list its subcommands.
1134 If c->func is non NULL, we really have a command. Print its
1135 documentation and return.
1137 If c->func is NULL, we have a class name. Print its
1138 documentation (as if it were a command) and then set class to the
1139 number of this class so that the commands in the class will be
1142 fputs_filtered (c
->doc
, stream
);
1143 fputs_filtered ("\n", stream
);
1145 if (c
->prefixlist
== 0 && c
->func
!= NULL
)
1147 fprintf_filtered (stream
, "\n");
1149 /* If this is a prefix command, print it's subcommands. */
1151 help_list (*c
->prefixlist
, c
->prefixname
, all_commands
, stream
);
1153 /* If this is a class name, print all of the commands in the class. */
1154 if (c
->func
== NULL
)
1155 help_list (cmdlist
, "", c
->theclass
, stream
);
1157 if (c
->hook_pre
|| c
->hook_post
)
1158 fprintf_filtered (stream
,
1159 "\nThis command has a hook (or hooks) defined:\n");
1162 fprintf_filtered (stream
,
1163 "\tThis command is run after : %s (pre hook)\n",
1166 fprintf_filtered (stream
,
1167 "\tThis command is run before : %s (post hook)\n",
1168 c
->hook_post
->name
);
1172 * Get a specific kind of help on a command list.
1175 * CMDTYPE is the prefix to use in the title string.
1176 * CLASS is the class with which to list the nodes of this list (see
1177 * documentation for help_cmd_list below), As usual, ALL_COMMANDS for
1178 * everything, ALL_CLASSES for just classes, and non-negative for only things
1179 * in a specific class.
1180 * and STREAM is the output stream on which to print things.
1181 * If you call this routine with a class >= 0, it recurses.
1184 help_list (struct cmd_list_element
*list
, const char *cmdtype
,
1185 enum command_class theclass
, struct ui_file
*stream
)
1188 char *cmdtype1
, *cmdtype2
;
1190 /* If CMDTYPE is "foo ", CMDTYPE1 gets " foo" and CMDTYPE2 gets "foo sub".
1192 len
= strlen (cmdtype
);
1193 cmdtype1
= (char *) alloca (len
+ 1);
1195 cmdtype2
= (char *) alloca (len
+ 4);
1200 memcpy (cmdtype1
+ 1, cmdtype
, len
- 1);
1202 memcpy (cmdtype2
, cmdtype
, len
- 1);
1203 strcpy (cmdtype2
+ len
- 1, " sub");
1206 if (theclass
== all_classes
)
1207 fprintf_filtered (stream
, "List of classes of %scommands:\n\n", cmdtype2
);
1209 fprintf_filtered (stream
, "List of %scommands:\n\n", cmdtype2
);
1211 help_cmd_list (list
, theclass
, cmdtype
, (int) theclass
>= 0, stream
);
1213 if (theclass
== all_classes
)
1215 fprintf_filtered (stream
, "\n\
1216 Type \"help%s\" followed by a class name for a list of commands in ",
1219 fprintf_filtered (stream
, "that class.");
1221 fprintf_filtered (stream
, "\n\
1222 Type \"help all\" for the list of all commands.");
1225 fprintf_filtered (stream
, "\nType \"help%s\" followed by %scommand name ",
1226 cmdtype1
, cmdtype2
);
1228 fputs_filtered ("for ", stream
);
1230 fputs_filtered ("full ", stream
);
1232 fputs_filtered ("documentation.\n", stream
);
1233 fputs_filtered ("Type \"apropos word\" to search "
1234 "for commands related to \"word\".\n", stream
);
1235 fputs_filtered ("Type \"apropos -v word\" for full documentation", stream
);
1237 fputs_filtered (" of commands related to \"word\".\n", stream
);
1238 fputs_filtered ("Command name abbreviations are allowed if unambiguous.\n",
1243 help_all (struct ui_file
*stream
)
1245 struct cmd_list_element
*c
;
1246 int seen_unclassified
= 0;
1248 for (c
= cmdlist
; c
; c
= c
->next
)
1252 /* If this is a class name, print all of the commands in the
1255 if (c
->func
== NULL
)
1257 fprintf_filtered (stream
, "\nCommand class: %s\n\n", c
->name
);
1258 help_cmd_list (cmdlist
, c
->theclass
, "", 1, stream
);
1262 /* While it's expected that all commands are in some class,
1263 as a safety measure, we'll print commands outside of any
1264 class at the end. */
1266 for (c
= cmdlist
; c
; c
= c
->next
)
1271 if (c
->theclass
== no_class
)
1273 if (!seen_unclassified
)
1275 fprintf_filtered (stream
, "\nUnclassified commands\n\n");
1276 seen_unclassified
= 1;
1278 print_help_for_command (c
, "", 1, stream
);
1284 /* See cli-decode.h. */
1287 print_doc_line (struct ui_file
*stream
, const char *str
,
1288 bool for_value_prefix
)
1290 static char *line_buffer
= 0;
1291 static int line_size
;
1297 line_buffer
= (char *) xmalloc (line_size
);
1300 /* Searches for the first end of line or the end of STR. */
1302 while (*p
&& *p
!= '\n')
1304 if (p
- str
> line_size
- 1)
1306 line_size
= p
- str
+ 1;
1307 xfree (line_buffer
);
1308 line_buffer
= (char *) xmalloc (line_size
);
1310 strncpy (line_buffer
, str
, p
- str
);
1311 if (for_value_prefix
)
1313 if (islower (line_buffer
[0]))
1314 line_buffer
[0] = toupper (line_buffer
[0]);
1315 gdb_assert (p
> str
);
1316 if (line_buffer
[p
- str
- 1] == '.')
1317 line_buffer
[p
- str
- 1] = '\0';
1319 line_buffer
[p
- str
] = '\0';
1322 line_buffer
[p
- str
] = '\0';
1323 fputs_filtered (line_buffer
, stream
);
1326 /* Print one-line help for command C.
1327 If RECURSE is non-zero, also print one-line descriptions
1328 of all prefixed subcommands. */
1330 print_help_for_command (struct cmd_list_element
*c
, const char *prefix
,
1331 int recurse
, struct ui_file
*stream
)
1333 fprintf_styled (stream
, title_style
.style (),
1334 "%s%s", prefix
, c
->name
);
1335 fputs_filtered (" -- ", stream
);
1336 print_doc_line (stream
, c
->doc
, false);
1337 fputs_filtered ("\n", stream
);
1340 && c
->prefixlist
!= 0
1341 && c
->abbrev_flag
== 0)
1342 /* Subcommands of a prefix command typically have 'all_commands'
1343 as class. If we pass CLASS to recursive invocation,
1344 most often we won't see anything. */
1345 help_cmd_list (*c
->prefixlist
, all_commands
, c
->prefixname
, 1, stream
);
1349 * Implement a help command on command list LIST.
1350 * RECURSE should be non-zero if this should be done recursively on
1351 * all sublists of LIST.
1352 * PREFIX is the prefix to print before each command name.
1353 * STREAM is the stream upon which the output should be written.
1354 * THECLASS should be:
1355 * A non-negative class number to list only commands in that
1357 * ALL_COMMANDS to list all commands in list.
1358 * ALL_CLASSES to list all classes in list.
1360 * Note that RECURSE will be active on *all* sublists, not just the
1361 * ones selected by the criteria above (ie. the selection mechanism
1362 * is at the low level, not the high-level).
1365 help_cmd_list (struct cmd_list_element
*list
, enum command_class theclass
,
1366 const char *prefix
, int recurse
, struct ui_file
*stream
)
1368 struct cmd_list_element
*c
;
1370 for (c
= list
; c
; c
= c
->next
)
1372 if (c
->abbrev_flag
== 0
1373 && !c
->cmd_deprecated
1374 && (theclass
== all_commands
1375 || (theclass
== all_classes
&& c
->func
== NULL
)
1376 || (theclass
== c
->theclass
&& c
->func
!= NULL
)))
1378 print_help_for_command (c
, prefix
, recurse
, stream
);
1380 else if (c
->abbrev_flag
== 0
1382 && !c
->cmd_deprecated
1383 && theclass
== class_user
&& c
->prefixlist
!= NULL
)
1384 /* User-defined commands may be subcommands. */
1385 help_cmd_list (*c
->prefixlist
, theclass
, c
->prefixname
,
1391 /* Search the input clist for 'command'. Return the command if
1392 found (or NULL if not), and return the number of commands
1395 static struct cmd_list_element
*
1396 find_cmd (const char *command
, int len
, struct cmd_list_element
*clist
,
1397 int ignore_help_classes
, int *nfound
)
1399 struct cmd_list_element
*found
, *c
;
1403 for (c
= clist
; c
; c
= c
->next
)
1404 if (!strncmp (command
, c
->name
, len
)
1405 && (!ignore_help_classes
|| c
->func
))
1409 if (c
->name
[len
] == '\0')
1418 /* Return the length of command name in TEXT. */
1421 find_command_name_length (const char *text
)
1423 const char *p
= text
;
1425 /* Treating underscores as part of command words is important
1426 so that "set args_foo()" doesn't get interpreted as
1427 "set args _foo()". */
1428 /* Some characters are only used for TUI specific commands.
1429 However, they are always allowed for the sake of consistency.
1431 Note that this is larger than the character set allowed when
1432 creating user-defined commands. */
1434 /* Recognize the single character commands so that, e.g., "!ls"
1435 works as expected. */
1436 if (*p
== '!' || *p
== '|')
1439 while (valid_cmd_char_p (*p
)
1440 /* Characters used by TUI specific commands. */
1441 || *p
== '+' || *p
== '<' || *p
== '>' || *p
== '$')
1447 /* See command.h. */
1450 valid_cmd_char_p (int c
)
1452 /* Alas "42" is a legitimate user-defined command.
1453 In the interests of not breaking anything we preserve that. */
1455 return isalnum (c
) || c
== '-' || c
== '_' || c
== '.';
1458 /* See command.h. */
1461 valid_user_defined_cmd_name_p (const char *name
)
1468 for (p
= name
; *p
!= '\0'; ++p
)
1470 if (valid_cmd_char_p (*p
))
1479 /* This routine takes a line of TEXT and a CLIST in which to start the
1480 lookup. When it returns it will have incremented the text pointer past
1481 the section of text it matched, set *RESULT_LIST to point to the list in
1482 which the last word was matched, and will return a pointer to the cmd
1483 list element which the text matches. It will return NULL if no match at
1484 all was possible. It will return -1 (cast appropriately, ick) if ambigous
1485 matches are possible; in this case *RESULT_LIST will be set to point to
1486 the list in which there are ambiguous choices (and *TEXT will be set to
1487 the ambiguous text string).
1489 If the located command was an abbreviation, this routine returns the base
1490 command of the abbreviation.
1492 It does no error reporting whatsoever; control will always return
1493 to the superior routine.
1495 In the case of an ambiguous return (-1), *RESULT_LIST will be set to point
1496 at the prefix_command (ie. the best match) *or* (special case) will be NULL
1497 if no prefix command was ever found. For example, in the case of "info a",
1498 "info" matches without ambiguity, but "a" could be "args" or "address", so
1499 *RESULT_LIST is set to the cmd_list_element for "info". So in this case
1500 RESULT_LIST should not be interpreted as a pointer to the beginning of a
1501 list; it simply points to a specific command. In the case of an ambiguous
1502 return *TEXT is advanced past the last non-ambiguous prefix (e.g.
1503 "info t" can be "info types" or "info target"; upon return *TEXT has been
1504 advanced past "info ").
1506 If RESULT_LIST is NULL, don't set *RESULT_LIST (but don't otherwise
1507 affect the operation).
1509 This routine does *not* modify the text pointed to by TEXT.
1511 If IGNORE_HELP_CLASSES is nonzero, ignore any command list elements which
1512 are actually help classes rather than commands (i.e. the function field of
1513 the struct cmd_list_element is NULL). */
1515 struct cmd_list_element
*
1516 lookup_cmd_1 (const char **text
, struct cmd_list_element
*clist
,
1517 struct cmd_list_element
**result_list
, int ignore_help_classes
)
1521 struct cmd_list_element
*found
, *c
;
1522 const char *line
= *text
;
1524 while (**text
== ' ' || **text
== '\t')
1527 /* Identify the name of the command. */
1528 len
= find_command_name_length (*text
);
1530 /* If nothing but whitespace, return 0. */
1534 /* *text and p now bracket the first command word to lookup (and
1535 it's length is len). We copy this into a local temporary. */
1538 command
= (char *) alloca (len
+ 1);
1539 memcpy (command
, *text
, len
);
1540 command
[len
] = '\0';
1545 found
= find_cmd (command
, len
, clist
, ignore_help_classes
, &nfound
);
1547 /* If nothing matches, we have a simple failure. */
1553 if (result_list
!= NULL
)
1554 /* Will be modified in calling routine
1555 if we know what the prefix command is. */
1557 return CMD_LIST_AMBIGUOUS
; /* Ambiguous. */
1560 /* We've matched something on this list. Move text pointer forward. */
1564 if (found
->cmd_pointer
)
1566 /* We drop the alias (abbreviation) in favor of the command it
1567 is pointing to. If the alias is deprecated, though, we need to
1568 warn the user about it before we drop it. Note that while we
1569 are warning about the alias, we may also warn about the command
1570 itself and we will adjust the appropriate DEPRECATED_WARN_USER
1573 if (found
->deprecated_warn_user
)
1574 deprecated_cmd_warning (line
);
1575 found
= found
->cmd_pointer
;
1577 /* If we found a prefix command, keep looking. */
1579 if (found
->prefixlist
)
1581 c
= lookup_cmd_1 (text
, *found
->prefixlist
, result_list
,
1582 ignore_help_classes
);
1585 /* Didn't find anything; this is as far as we got. */
1586 if (result_list
!= NULL
)
1587 *result_list
= clist
;
1590 else if (c
== CMD_LIST_AMBIGUOUS
)
1592 /* We've gotten this far properly, but the next step is
1593 ambiguous. We need to set the result list to the best
1594 we've found (if an inferior hasn't already set it). */
1595 if (result_list
!= NULL
)
1597 /* This used to say *result_list = *found->prefixlist.
1598 If that was correct, need to modify the documentation
1599 at the top of this function to clarify what is
1600 supposed to be going on. */
1601 *result_list
= found
;
1612 if (result_list
!= NULL
)
1613 *result_list
= clist
;
1618 /* All this hair to move the space to the front of cmdtype */
1621 undef_cmd_error (const char *cmdtype
, const char *q
)
1623 error (_("Undefined %scommand: \"%s\". Try \"help%s%.*s\"."),
1626 *cmdtype
? " " : "",
1627 (int) strlen (cmdtype
) - 1,
1631 /* Look up the contents of *LINE as a command in the command list LIST.
1632 LIST is a chain of struct cmd_list_element's.
1633 If it is found, return the struct cmd_list_element for that command
1634 and update *LINE to point after the command name, at the first argument.
1635 If not found, call error if ALLOW_UNKNOWN is zero
1636 otherwise (or if error returns) return zero.
1637 Call error if specified command is ambiguous,
1638 unless ALLOW_UNKNOWN is negative.
1639 CMDTYPE precedes the word "command" in the error message.
1641 If IGNORE_HELP_CLASSES is nonzero, ignore any command list
1642 elements which are actually help classes rather than commands (i.e.
1643 the function field of the struct cmd_list_element is 0). */
1645 struct cmd_list_element
*
1646 lookup_cmd (const char **line
, struct cmd_list_element
*list
,
1647 const char *cmdtype
,
1648 int allow_unknown
, int ignore_help_classes
)
1650 struct cmd_list_element
*last_list
= 0;
1651 struct cmd_list_element
*c
;
1653 /* Note: Do not remove trailing whitespace here because this
1654 would be wrong for complete_command. Jim Kingdon */
1657 error (_("Lack of needed %scommand"), cmdtype
);
1659 c
= lookup_cmd_1 (line
, list
, &last_list
, ignore_help_classes
);
1666 int len
= find_command_name_length (*line
);
1668 q
= (char *) alloca (len
+ 1);
1669 strncpy (q
, *line
, len
);
1671 undef_cmd_error (cmdtype
, q
);
1676 else if (c
== CMD_LIST_AMBIGUOUS
)
1678 /* Ambigous. Local values should be off prefixlist or called
1680 int local_allow_unknown
= (last_list
? last_list
->allow_unknown
:
1682 const char *local_cmdtype
= last_list
? last_list
->prefixname
: cmdtype
;
1683 struct cmd_list_element
*local_list
=
1684 (last_list
? *(last_list
->prefixlist
) : list
);
1686 if (local_allow_unknown
< 0)
1689 return last_list
; /* Found something. */
1691 return 0; /* Found nothing. */
1695 /* Report as error. */
1700 ((*line
)[amb_len
] && (*line
)[amb_len
] != ' '
1701 && (*line
)[amb_len
] != '\t');
1706 for (c
= local_list
; c
; c
= c
->next
)
1707 if (!strncmp (*line
, c
->name
, amb_len
))
1709 if (strlen (ambbuf
) + strlen (c
->name
) + 6
1710 < (int) sizeof ambbuf
)
1712 if (strlen (ambbuf
))
1713 strcat (ambbuf
, ", ");
1714 strcat (ambbuf
, c
->name
);
1718 strcat (ambbuf
, "..");
1722 error (_("Ambiguous %scommand \"%s\": %s."), local_cmdtype
,
1728 if (c
->type
== set_cmd
&& **line
!= '\0' && !isspace (**line
))
1729 error (_("Argument must be preceded by space."));
1731 /* We've got something. It may still not be what the caller
1732 wants (if this command *needs* a subcommand). */
1733 while (**line
== ' ' || **line
== '\t')
1736 if (c
->prefixlist
&& **line
&& !c
->allow_unknown
)
1737 undef_cmd_error (c
->prefixname
, *line
);
1739 /* Seems to be what he wants. Return it. */
1745 /* We are here presumably because an alias or command in TEXT is
1746 deprecated and a warning message should be generated. This
1747 function decodes TEXT and potentially generates a warning message
1750 Example for 'set endian big' which has a fictitious alias 'seb'.
1752 If alias wasn't used in TEXT, and the command is deprecated:
1753 "warning: 'set endian big' is deprecated."
1755 If alias was used, and only the alias is deprecated:
1756 "warning: 'seb' an alias for the command 'set endian big' is deprecated."
1758 If alias was used and command is deprecated (regardless of whether
1759 the alias itself is deprecated:
1761 "warning: 'set endian big' (seb) is deprecated."
1763 After the message has been sent, clear the appropriate flags in the
1764 command and/or the alias so the user is no longer bothered.
1768 deprecated_cmd_warning (const char *text
)
1770 struct cmd_list_element
*alias
= NULL
;
1771 struct cmd_list_element
*prefix_cmd
= NULL
;
1772 struct cmd_list_element
*cmd
= NULL
;
1774 if (!lookup_cmd_composition (text
, &alias
, &prefix_cmd
, &cmd
))
1775 /* Return if text doesn't evaluate to a command. */
1778 if (!((alias
? alias
->deprecated_warn_user
: 0)
1779 || cmd
->deprecated_warn_user
) )
1780 /* Return if nothing is deprecated. */
1783 printf_filtered ("Warning:");
1785 if (alias
&& !cmd
->cmd_deprecated
)
1786 printf_filtered (" '%s', an alias for the", alias
->name
);
1788 printf_filtered (" command '");
1791 printf_filtered ("%s", prefix_cmd
->prefixname
);
1793 printf_filtered ("%s", cmd
->name
);
1795 if (alias
&& cmd
->cmd_deprecated
)
1796 printf_filtered ("' (%s) is deprecated.\n", alias
->name
);
1798 printf_filtered ("' is deprecated.\n");
1801 /* If it is only the alias that is deprecated, we want to indicate
1802 the new alias, otherwise we'll indicate the new command. */
1804 if (alias
&& !cmd
->cmd_deprecated
)
1806 if (alias
->replacement
)
1807 printf_filtered ("Use '%s'.\n\n", alias
->replacement
);
1809 printf_filtered ("No alternative known.\n\n");
1813 if (cmd
->replacement
)
1814 printf_filtered ("Use '%s'.\n\n", cmd
->replacement
);
1816 printf_filtered ("No alternative known.\n\n");
1819 /* We've warned you, now we'll keep quiet. */
1821 alias
->deprecated_warn_user
= 0;
1823 cmd
->deprecated_warn_user
= 0;
1827 /* Look up the contents of TEXT as a command in the command list 'cmdlist'.
1828 Return 1 on success, 0 on failure.
1830 If TEXT refers to an alias, *ALIAS will point to that alias.
1832 If TEXT is a subcommand (i.e. one that is preceded by a prefix
1833 command) set *PREFIX_CMD.
1835 Set *CMD to point to the command TEXT indicates.
1837 If any of *ALIAS, *PREFIX_CMD, or *CMD cannot be determined or do not
1838 exist, they are NULL when we return.
1842 lookup_cmd_composition (const char *text
,
1843 struct cmd_list_element
**alias
,
1844 struct cmd_list_element
**prefix_cmd
,
1845 struct cmd_list_element
**cmd
)
1849 struct cmd_list_element
*cur_list
;
1850 struct cmd_list_element
*prev_cmd
;
1858 text
= skip_spaces (text
);
1862 /* Go through as many command lists as we need to,
1863 to find the command TEXT refers to. */
1867 /* Identify the name of the command. */
1868 len
= find_command_name_length (text
);
1870 /* If nothing but whitespace, return. */
1874 /* TEXT is the start of the first command word to lookup (and
1875 it's length is LEN). We copy this into a local temporary. */
1877 command
= (char *) alloca (len
+ 1);
1878 memcpy (command
, text
, len
);
1879 command
[len
] = '\0';
1884 *cmd
= find_cmd (command
, len
, cur_list
, 1, &nfound
);
1886 if (*cmd
== CMD_LIST_AMBIGUOUS
)
1888 return 0; /* ambiguous */
1892 return 0; /* nothing found */
1895 if ((*cmd
)->cmd_pointer
)
1897 /* cmd was actually an alias, we note that an alias was
1898 used (by assigning *ALIAS) and we set *CMD. */
1900 *cmd
= (*cmd
)->cmd_pointer
;
1902 *prefix_cmd
= prev_cmd
;
1906 text
= skip_spaces (text
);
1908 if ((*cmd
)->prefixlist
&& *text
!= '\0')
1909 cur_list
= *(*cmd
)->prefixlist
;
1915 /* Helper function for SYMBOL_COMPLETION_FUNCTION. */
1917 /* Return a vector of char pointers which point to the different
1918 possible completions in LIST of TEXT.
1920 WORD points in the same buffer as TEXT, and completions should be
1921 returned relative to this position. For example, suppose TEXT is
1922 "foo" and we want to complete to "foobar". If WORD is "oo", return
1923 "oobar"; if WORD is "baz/foo", return "baz/foobar". */
1926 complete_on_cmdlist (struct cmd_list_element
*list
,
1927 completion_tracker
&tracker
,
1928 const char *text
, const char *word
,
1929 int ignore_help_classes
)
1931 struct cmd_list_element
*ptr
;
1932 int textlen
= strlen (text
);
1934 int saw_deprecated_match
= 0;
1936 /* We do one or two passes. In the first pass, we skip deprecated
1937 commands. If we see no matching commands in the first pass, and
1938 if we did happen to see a matching deprecated command, we do
1939 another loop to collect those. */
1940 for (pass
= 0; pass
< 2; ++pass
)
1942 bool got_matches
= false;
1944 for (ptr
= list
; ptr
; ptr
= ptr
->next
)
1945 if (!strncmp (ptr
->name
, text
, textlen
)
1946 && !ptr
->abbrev_flag
1947 && (!ignore_help_classes
|| ptr
->func
1948 || ptr
->prefixlist
))
1952 if (ptr
->cmd_deprecated
)
1954 saw_deprecated_match
= 1;
1959 tracker
.add_completion
1960 (make_completion_match_str (ptr
->name
, text
, word
));
1967 /* If we saw no matching deprecated commands in the first pass,
1969 if (!saw_deprecated_match
)
1974 /* Helper function for SYMBOL_COMPLETION_FUNCTION. */
1976 /* Add the different possible completions in ENUMLIST of TEXT.
1978 WORD points in the same buffer as TEXT, and completions should be
1979 returned relative to this position. For example, suppose TEXT is "foo"
1980 and we want to complete to "foobar". If WORD is "oo", return
1981 "oobar"; if WORD is "baz/foo", return "baz/foobar". */
1984 complete_on_enum (completion_tracker
&tracker
,
1985 const char *const *enumlist
,
1986 const char *text
, const char *word
)
1988 int textlen
= strlen (text
);
1992 for (i
= 0; (name
= enumlist
[i
]) != NULL
; i
++)
1993 if (strncmp (name
, text
, textlen
) == 0)
1994 tracker
.add_completion (make_completion_match_str (name
, text
, word
));
1998 /* Check function pointer. */
2000 cmd_func_p (struct cmd_list_element
*cmd
)
2002 return (cmd
->func
!= NULL
);
2006 /* Call the command function. */
2008 cmd_func (struct cmd_list_element
*cmd
, const char *args
, int from_tty
)
2010 if (cmd_func_p (cmd
))
2012 gdb::optional
<scoped_restore_tmpl
<int>> restore_suppress
;
2014 if (cmd
->suppress_notification
!= NULL
)
2015 restore_suppress
.emplace (cmd
->suppress_notification
, 1);
2017 (*cmd
->func
) (cmd
, args
, from_tty
);
2020 error (_("Invalid command"));
2024 cli_user_command_p (struct cmd_list_element
*cmd
)
2026 return (cmd
->theclass
== class_user
2027 && (cmd
->func
== do_const_cfunc
|| cmd
->func
== do_sfunc
));