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
)
66 q
= lookup_cmd_for_prefixlist (key
, *(p
->prefixlist
));
75 set_cmd_prefix (struct cmd_list_element
*c
, struct cmd_list_element
**list
)
77 struct cmd_list_element
*p
;
79 /* Check to see if *LIST contains any element other than C. */
80 for (p
= *list
; p
!= NULL
; p
= p
->next
)
86 /* *SET_LIST only contains SET. */
87 p
= lookup_cmd_for_prefixlist (list
, setlist
);
89 c
->prefix
= p
? (p
->cmd_pointer
? p
->cmd_pointer
: p
) : p
;
92 c
->prefix
= p
->prefix
;
96 print_help_for_command (struct cmd_list_element
*c
, const char *prefix
,
97 int recurse
, struct ui_file
*stream
);
100 /* Set the callback function for the specified command. For each both
101 the commands callback and func() are set. The latter set to a
102 bounce function (unless cfunc / sfunc is NULL that is). */
105 do_const_cfunc (struct cmd_list_element
*c
, const char *args
, int from_tty
)
107 c
->function
.const_cfunc (args
, from_tty
);
111 set_cmd_cfunc (struct cmd_list_element
*cmd
, cmd_const_cfunc_ftype
*cfunc
)
116 cmd
->func
= do_const_cfunc
;
117 cmd
->function
.const_cfunc
= cfunc
;
121 do_sfunc (struct cmd_list_element
*c
, const char *args
, int from_tty
)
123 c
->function
.sfunc (args
, from_tty
, c
);
127 set_cmd_sfunc (struct cmd_list_element
*cmd
, cmd_const_sfunc_ftype
*sfunc
)
132 cmd
->func
= do_sfunc
;
133 cmd
->function
.sfunc
= sfunc
;
137 cmd_cfunc_eq (struct cmd_list_element
*cmd
, cmd_const_cfunc_ftype
*cfunc
)
139 return cmd
->func
== do_const_cfunc
&& cmd
->function
.const_cfunc
== cfunc
;
143 set_cmd_context (struct cmd_list_element
*cmd
, void *context
)
145 cmd
->context
= context
;
149 get_cmd_context (struct cmd_list_element
*cmd
)
155 cmd_type (struct cmd_list_element
*cmd
)
161 set_cmd_completer (struct cmd_list_element
*cmd
, completer_ftype
*completer
)
163 cmd
->completer
= completer
; /* Ok. */
166 /* See definition in commands.h. */
169 set_cmd_completer_handle_brkchars (struct cmd_list_element
*cmd
,
170 completer_handle_brkchars_ftype
*func
)
172 cmd
->completer_handle_brkchars
= func
;
175 /* Add element named NAME.
176 Space for NAME and DOC must be allocated by the caller.
177 CLASS is the top level category into which commands are broken down
179 FUN should be the function to execute the command;
180 it will get a character string as argument, with leading
181 and trailing blanks already eliminated.
183 DOC is a documentation string for the command.
184 Its first line should be a complete sentence.
185 It should start with ? for a command that is an abbreviation
186 or with * for a command that most users don't need to know about.
188 Add this command to command list *LIST.
190 Returns a pointer to the added command (not necessarily the head
193 static struct cmd_list_element
*
194 do_add_cmd (const char *name
, enum command_class theclass
,
195 const char *doc
, struct cmd_list_element
**list
)
197 struct cmd_list_element
*c
= new struct cmd_list_element (name
, theclass
,
199 struct cmd_list_element
*p
, *iter
;
201 /* Turn each alias of the old command into an alias of the new
203 c
->aliases
= delete_cmd (name
, list
, &c
->hook_pre
, &c
->hookee_pre
,
204 &c
->hook_post
, &c
->hookee_post
);
205 for (iter
= c
->aliases
; iter
; iter
= iter
->alias_chain
)
206 iter
->cmd_pointer
= c
;
208 c
->hook_pre
->hookee_pre
= c
;
210 c
->hookee_pre
->hook_pre
= c
;
212 c
->hook_post
->hookee_post
= c
;
214 c
->hookee_post
->hook_post
= c
;
216 if (*list
== NULL
|| strcmp ((*list
)->name
, name
) >= 0)
224 while (p
->next
&& strcmp (p
->next
->name
, name
) <= 0)
235 struct cmd_list_element
*
236 add_cmd (const char *name
, enum command_class theclass
,
237 const char *doc
, struct cmd_list_element
**list
)
239 cmd_list_element
*result
= do_add_cmd (name
, theclass
, doc
, list
);
241 result
->function
.const_cfunc
= NULL
;
245 struct cmd_list_element
*
246 add_cmd (const char *name
, enum command_class theclass
,
247 cmd_const_cfunc_ftype
*fun
,
248 const char *doc
, struct cmd_list_element
**list
)
250 cmd_list_element
*result
= do_add_cmd (name
, theclass
, doc
, list
);
251 set_cmd_cfunc (result
, fun
);
255 /* Add an element with a suppress notification to the LIST of commands. */
257 struct cmd_list_element
*
258 add_cmd_suppress_notification (const char *name
, enum command_class theclass
,
259 cmd_const_cfunc_ftype
*fun
, const char *doc
,
260 struct cmd_list_element
**list
,
261 int *suppress_notification
)
263 struct cmd_list_element
*element
;
265 element
= add_cmd (name
, theclass
, fun
, doc
, list
);
266 element
->suppress_notification
= suppress_notification
;
272 /* Deprecates a command CMD.
273 REPLACEMENT is the name of the command which should be used in
274 place of this command, or NULL if no such command exists.
276 This function does not check to see if command REPLACEMENT exists
277 since gdb may not have gotten around to adding REPLACEMENT when
278 this function is called.
280 Returns a pointer to the deprecated command. */
282 struct cmd_list_element
*
283 deprecate_cmd (struct cmd_list_element
*cmd
, const char *replacement
)
285 cmd
->cmd_deprecated
= 1;
286 cmd
->deprecated_warn_user
= 1;
288 if (replacement
!= NULL
)
289 cmd
->replacement
= replacement
;
291 cmd
->replacement
= NULL
;
296 struct cmd_list_element
*
297 add_alias_cmd (const char *name
, cmd_list_element
*old
,
298 enum command_class theclass
, int abbrev_flag
,
299 struct cmd_list_element
**list
)
303 struct cmd_list_element
*prehook
, *prehookee
, *posthook
, *posthookee
;
304 struct cmd_list_element
*aliases
= delete_cmd (name
, list
,
305 &prehook
, &prehookee
,
306 &posthook
, &posthookee
);
308 /* If this happens, it means a programmer error somewhere. */
309 gdb_assert (!aliases
&& !prehook
&& !prehookee
310 && !posthook
&& ! posthookee
);
314 struct cmd_list_element
*c
= add_cmd (name
, theclass
, old
->doc
, list
);
316 /* If OLD->DOC can be freed, we should make another copy. */
317 if (old
->doc_allocated
)
319 c
->doc
= xstrdup (old
->doc
);
320 c
->doc_allocated
= 1;
322 /* NOTE: Both FUNC and all the FUNCTIONs need to be copied. */
324 c
->function
= old
->function
;
325 c
->prefixlist
= old
->prefixlist
;
326 c
->prefixname
= old
->prefixname
;
327 c
->allow_unknown
= old
->allow_unknown
;
328 c
->abbrev_flag
= abbrev_flag
;
329 c
->cmd_pointer
= old
;
330 c
->alias_chain
= old
->aliases
;
333 set_cmd_prefix (c
, list
);
337 struct cmd_list_element
*
338 add_alias_cmd (const char *name
, const char *oldname
,
339 enum command_class theclass
, int abbrev_flag
,
340 struct cmd_list_element
**list
)
343 struct cmd_list_element
*old
;
346 old
= lookup_cmd (&tmp
, *list
, "", 1, 1);
348 return add_alias_cmd (name
, old
, theclass
, abbrev_flag
, list
);
352 /* Like add_cmd but adds an element for a command prefix: a name that
353 should be followed by a subcommand to be looked up in another
354 command list. PREFIXLIST should be the address of the variable
355 containing that list. */
357 struct cmd_list_element
*
358 add_prefix_cmd (const char *name
, enum command_class theclass
,
359 cmd_const_cfunc_ftype
*fun
,
360 const char *doc
, struct cmd_list_element
**prefixlist
,
361 const char *prefixname
, int allow_unknown
,
362 struct cmd_list_element
**list
)
364 struct cmd_list_element
*c
= add_cmd (name
, theclass
, fun
, doc
, list
);
365 struct cmd_list_element
*p
;
367 c
->prefixlist
= prefixlist
;
368 c
->prefixname
= prefixname
;
369 c
->allow_unknown
= allow_unknown
;
371 if (list
== &cmdlist
)
374 set_cmd_prefix (c
, list
);
376 /* Update the field 'prefix' of each cmd_list_element in *PREFIXLIST. */
377 for (p
= *prefixlist
; p
!= NULL
; p
= p
->next
)
383 /* A helper function for add_basic_prefix_cmd. This is a command
384 function that just forwards to help_list. */
387 do_prefix_cmd (const char *args
, int from_tty
, struct cmd_list_element
*c
)
389 /* Look past all aliases. */
390 while (c
->cmd_pointer
!= nullptr)
393 help_list (*c
->prefixlist
, c
->prefixname
, all_commands
, gdb_stdout
);
398 struct cmd_list_element
*
399 add_basic_prefix_cmd (const char *name
, enum command_class theclass
,
400 const char *doc
, struct cmd_list_element
**prefixlist
,
401 const char *prefixname
, int allow_unknown
,
402 struct cmd_list_element
**list
)
404 struct cmd_list_element
*cmd
= add_prefix_cmd (name
, theclass
, nullptr,
405 doc
, prefixlist
, prefixname
,
406 allow_unknown
, list
);
407 set_cmd_sfunc (cmd
, do_prefix_cmd
);
411 /* A helper function for add_show_prefix_cmd. This is a command
412 function that just forwards to cmd_show_list. */
415 do_show_prefix_cmd (const char *args
, int from_tty
, struct cmd_list_element
*c
)
417 cmd_show_list (*c
->prefixlist
, from_tty
, "");
422 struct cmd_list_element
*
423 add_show_prefix_cmd (const char *name
, enum command_class theclass
,
424 const char *doc
, struct cmd_list_element
**prefixlist
,
425 const char *prefixname
, int allow_unknown
,
426 struct cmd_list_element
**list
)
428 struct cmd_list_element
*cmd
= add_prefix_cmd (name
, theclass
, nullptr,
429 doc
, prefixlist
, prefixname
,
430 allow_unknown
, list
);
431 set_cmd_sfunc (cmd
, do_show_prefix_cmd
);
435 /* Like ADD_PREFIX_CMD but sets the suppress_notification pointer on the
436 new command list element. */
438 struct cmd_list_element
*
439 add_prefix_cmd_suppress_notification
440 (const char *name
, enum command_class theclass
,
441 cmd_const_cfunc_ftype
*fun
,
442 const char *doc
, struct cmd_list_element
**prefixlist
,
443 const char *prefixname
, int allow_unknown
,
444 struct cmd_list_element
**list
,
445 int *suppress_notification
)
447 struct cmd_list_element
*element
448 = add_prefix_cmd (name
, theclass
, fun
, doc
, prefixlist
,
449 prefixname
, allow_unknown
, list
);
450 element
->suppress_notification
= suppress_notification
;
454 /* Like add_prefix_cmd but sets the abbrev_flag on the new command. */
456 struct cmd_list_element
*
457 add_abbrev_prefix_cmd (const char *name
, enum command_class theclass
,
458 cmd_const_cfunc_ftype
*fun
, const char *doc
,
459 struct cmd_list_element
**prefixlist
,
460 const char *prefixname
,
461 int allow_unknown
, struct cmd_list_element
**list
)
463 struct cmd_list_element
*c
= add_cmd (name
, theclass
, fun
, doc
, list
);
465 c
->prefixlist
= prefixlist
;
466 c
->prefixname
= prefixname
;
467 c
->allow_unknown
= allow_unknown
;
472 /* This is an empty "cfunc". */
474 not_just_help_class_command (const char *args
, int from_tty
)
478 /* This is an empty "sfunc". */
481 empty_sfunc (const char *args
, int from_tty
, struct cmd_list_element
*c
)
485 /* Add element named NAME to command list LIST (the list for set/show
486 or some sublist thereof).
487 TYPE is set_cmd or show_cmd.
488 CLASS is as in add_cmd.
489 VAR_TYPE is the kind of thing we are setting.
490 VAR is address of the variable being controlled by this command.
491 DOC is the documentation string. */
493 static struct cmd_list_element
*
494 add_set_or_show_cmd (const char *name
,
496 enum command_class theclass
,
500 struct cmd_list_element
**list
)
502 struct cmd_list_element
*c
= add_cmd (name
, theclass
, doc
, list
);
504 gdb_assert (type
== set_cmd
|| type
== show_cmd
);
506 c
->var_type
= var_type
;
508 /* This needs to be something besides NULL so that this isn't
509 treated as a help class. */
510 set_cmd_sfunc (c
, empty_sfunc
);
514 /* Add element named NAME to both the command SET_LIST and SHOW_LIST.
515 CLASS is as in add_cmd. VAR_TYPE is the kind of thing we are
516 setting. VAR is address of the variable being controlled by this
517 command. SET_FUNC and SHOW_FUNC are the callback functions (if
518 non-NULL). SET_DOC, SHOW_DOC and HELP_DOC are the documentation
519 strings. PRINT the format string to print the value. SET_RESULT
520 and SHOW_RESULT, if not NULL, are set to the resulting command
524 add_setshow_cmd_full (const char *name
,
525 enum command_class theclass
,
526 var_types var_type
, void *var
,
527 const char *set_doc
, const char *show_doc
,
528 const char *help_doc
,
529 cmd_const_sfunc_ftype
*set_func
,
530 show_value_ftype
*show_func
,
531 struct cmd_list_element
**set_list
,
532 struct cmd_list_element
**show_list
,
533 struct cmd_list_element
**set_result
,
534 struct cmd_list_element
**show_result
)
536 struct cmd_list_element
*set
;
537 struct cmd_list_element
*show
;
541 if (help_doc
!= NULL
)
543 full_set_doc
= xstrprintf ("%s\n%s", set_doc
, help_doc
);
544 full_show_doc
= xstrprintf ("%s\n%s", show_doc
, help_doc
);
548 full_set_doc
= xstrdup (set_doc
);
549 full_show_doc
= xstrdup (show_doc
);
551 set
= add_set_or_show_cmd (name
, set_cmd
, theclass
, var_type
, var
,
552 full_set_doc
, set_list
);
553 set
->doc_allocated
= 1;
555 if (set_func
!= NULL
)
556 set_cmd_sfunc (set
, set_func
);
558 set_cmd_prefix (set
, set_list
);
560 show
= add_set_or_show_cmd (name
, show_cmd
, theclass
, var_type
, var
,
561 full_show_doc
, show_list
);
562 show
->doc_allocated
= 1;
563 show
->show_value_func
= show_func
;
564 /* Disable the default symbol completer. Doesn't make much sense
565 for the "show" command to complete on anything. */
566 set_cmd_completer (show
, nullptr);
568 if (set_result
!= NULL
)
570 if (show_result
!= NULL
)
574 /* Add element named NAME to command list LIST (the list for set or
575 some sublist thereof). CLASS is as in add_cmd. ENUMLIST is a list
576 of strings which may follow NAME. VAR is address of the variable
577 which will contain the matching string (from ENUMLIST). */
580 add_setshow_enum_cmd (const char *name
,
581 enum command_class theclass
,
582 const char *const *enumlist
,
585 const char *show_doc
,
586 const char *help_doc
,
587 cmd_const_sfunc_ftype
*set_func
,
588 show_value_ftype
*show_func
,
589 struct cmd_list_element
**set_list
,
590 struct cmd_list_element
**show_list
,
593 struct cmd_list_element
*c
, *show
;
595 add_setshow_cmd_full (name
, theclass
, var_enum
, var
,
596 set_doc
, show_doc
, help_doc
,
602 set_cmd_context (c
, context
);
603 set_cmd_context (show
, context
);
606 /* See cli-decode.h. */
607 const char * const auto_boolean_enums
[] = { "on", "off", "auto", NULL
};
609 /* Add an auto-boolean command named NAME to both the set and show
610 command list lists. CLASS is as in add_cmd. VAR is address of the
611 variable which will contain the value. DOC is the documentation
612 string. FUNC is the corresponding callback. */
614 add_setshow_auto_boolean_cmd (const char *name
,
615 enum command_class theclass
,
616 enum auto_boolean
*var
,
617 const char *set_doc
, const char *show_doc
,
618 const char *help_doc
,
619 cmd_const_sfunc_ftype
*set_func
,
620 show_value_ftype
*show_func
,
621 struct cmd_list_element
**set_list
,
622 struct cmd_list_element
**show_list
)
624 struct cmd_list_element
*c
;
626 add_setshow_cmd_full (name
, theclass
, var_auto_boolean
, var
,
627 set_doc
, show_doc
, help_doc
,
631 c
->enums
= auto_boolean_enums
;
634 /* See cli-decode.h. */
635 const char * const boolean_enums
[] = { "on", "off", NULL
};
637 /* Add element named NAME to both the set and show command LISTs (the
638 list for set/show or some sublist thereof). CLASS is as in
639 add_cmd. VAR is address of the variable which will contain the
640 value. SET_DOC and SHOW_DOC are the documentation strings.
641 Returns the new command element. */
644 add_setshow_boolean_cmd (const char *name
, enum command_class theclass
, bool *var
,
645 const char *set_doc
, const char *show_doc
,
646 const char *help_doc
,
647 cmd_const_sfunc_ftype
*set_func
,
648 show_value_ftype
*show_func
,
649 struct cmd_list_element
**set_list
,
650 struct cmd_list_element
**show_list
)
652 struct cmd_list_element
*c
;
654 add_setshow_cmd_full (name
, theclass
, var_boolean
, var
,
655 set_doc
, show_doc
, help_doc
,
659 c
->enums
= boolean_enums
;
664 /* Add element named NAME to both the set and show command LISTs (the
665 list for set/show or some sublist thereof). */
667 add_setshow_filename_cmd (const char *name
, enum command_class theclass
,
669 const char *set_doc
, const char *show_doc
,
670 const char *help_doc
,
671 cmd_const_sfunc_ftype
*set_func
,
672 show_value_ftype
*show_func
,
673 struct cmd_list_element
**set_list
,
674 struct cmd_list_element
**show_list
)
676 struct cmd_list_element
*set_result
;
678 add_setshow_cmd_full (name
, theclass
, var_filename
, var
,
679 set_doc
, show_doc
, help_doc
,
683 set_cmd_completer (set_result
, filename_completer
);
686 /* Add element named NAME to both the set and show command LISTs (the
687 list for set/show or some sublist thereof). */
689 add_setshow_string_cmd (const char *name
, enum command_class theclass
,
691 const char *set_doc
, const char *show_doc
,
692 const char *help_doc
,
693 cmd_const_sfunc_ftype
*set_func
,
694 show_value_ftype
*show_func
,
695 struct cmd_list_element
**set_list
,
696 struct cmd_list_element
**show_list
)
698 cmd_list_element
*set_cmd
;
700 add_setshow_cmd_full (name
, theclass
, var_string
, var
,
701 set_doc
, show_doc
, help_doc
,
706 /* Disable the default symbol completer. */
707 set_cmd_completer (set_cmd
, nullptr);
710 /* Add element named NAME to both the set and show command LISTs (the
711 list for set/show or some sublist thereof). */
712 struct cmd_list_element
*
713 add_setshow_string_noescape_cmd (const char *name
, enum command_class theclass
,
715 const char *set_doc
, const char *show_doc
,
716 const char *help_doc
,
717 cmd_const_sfunc_ftype
*set_func
,
718 show_value_ftype
*show_func
,
719 struct cmd_list_element
**set_list
,
720 struct cmd_list_element
**show_list
)
722 struct cmd_list_element
*set_cmd
;
724 add_setshow_cmd_full (name
, theclass
, var_string_noescape
, var
,
725 set_doc
, show_doc
, help_doc
,
730 /* Disable the default symbol completer. */
731 set_cmd_completer (set_cmd
, nullptr);
736 /* Add element named NAME to both the set and show command LISTs (the
737 list for set/show or some sublist thereof). */
739 add_setshow_optional_filename_cmd (const char *name
, enum command_class theclass
,
741 const char *set_doc
, const char *show_doc
,
742 const char *help_doc
,
743 cmd_const_sfunc_ftype
*set_func
,
744 show_value_ftype
*show_func
,
745 struct cmd_list_element
**set_list
,
746 struct cmd_list_element
**show_list
)
748 struct cmd_list_element
*set_result
;
750 add_setshow_cmd_full (name
, theclass
, var_optional_filename
, var
,
751 set_doc
, show_doc
, help_doc
,
756 set_cmd_completer (set_result
, filename_completer
);
760 /* Completes on literal "unlimited". Used by integer commands that
761 support a special "unlimited" value. */
764 integer_unlimited_completer (struct cmd_list_element
*ignore
,
765 completion_tracker
&tracker
,
766 const char *text
, const char *word
)
768 static const char * const keywords
[] =
774 complete_on_enum (tracker
, keywords
, text
, word
);
777 /* Add element named NAME to both the set and show command LISTs (the
778 list for set/show or some sublist thereof). CLASS is as in
779 add_cmd. VAR is address of the variable which will contain the
780 value. SET_DOC and SHOW_DOC are the documentation strings. This
781 function is only used in Python API. Please don't use it elsewhere. */
783 add_setshow_integer_cmd (const char *name
, enum command_class theclass
,
785 const char *set_doc
, const char *show_doc
,
786 const char *help_doc
,
787 cmd_const_sfunc_ftype
*set_func
,
788 show_value_ftype
*show_func
,
789 struct cmd_list_element
**set_list
,
790 struct cmd_list_element
**show_list
)
792 struct cmd_list_element
*set
;
794 add_setshow_cmd_full (name
, theclass
, var_integer
, var
,
795 set_doc
, show_doc
, help_doc
,
800 set_cmd_completer (set
, integer_unlimited_completer
);
803 /* Add element named NAME to both the set and show command LISTs (the
804 list for set/show or some sublist thereof). CLASS is as in
805 add_cmd. VAR is address of the variable which will contain the
806 value. SET_DOC and SHOW_DOC are the documentation strings. */
808 add_setshow_uinteger_cmd (const char *name
, enum command_class theclass
,
810 const char *set_doc
, const char *show_doc
,
811 const char *help_doc
,
812 cmd_const_sfunc_ftype
*set_func
,
813 show_value_ftype
*show_func
,
814 struct cmd_list_element
**set_list
,
815 struct cmd_list_element
**show_list
)
817 struct cmd_list_element
*set
;
819 add_setshow_cmd_full (name
, theclass
, var_uinteger
, var
,
820 set_doc
, show_doc
, help_doc
,
825 set_cmd_completer (set
, integer_unlimited_completer
);
828 /* Add element named NAME to both the set and show command LISTs (the
829 list for set/show or some sublist thereof). CLASS is as in
830 add_cmd. VAR is address of the variable which will contain the
831 value. SET_DOC and SHOW_DOC are the documentation strings. */
833 add_setshow_zinteger_cmd (const char *name
, enum command_class theclass
,
835 const char *set_doc
, const char *show_doc
,
836 const char *help_doc
,
837 cmd_const_sfunc_ftype
*set_func
,
838 show_value_ftype
*show_func
,
839 struct cmd_list_element
**set_list
,
840 struct cmd_list_element
**show_list
)
842 add_setshow_cmd_full (name
, theclass
, var_zinteger
, var
,
843 set_doc
, show_doc
, help_doc
,
850 add_setshow_zuinteger_unlimited_cmd (const char *name
,
851 enum command_class theclass
,
854 const char *show_doc
,
855 const char *help_doc
,
856 cmd_const_sfunc_ftype
*set_func
,
857 show_value_ftype
*show_func
,
858 struct cmd_list_element
**set_list
,
859 struct cmd_list_element
**show_list
)
861 struct cmd_list_element
*set
;
863 add_setshow_cmd_full (name
, theclass
, var_zuinteger_unlimited
, var
,
864 set_doc
, show_doc
, help_doc
,
869 set_cmd_completer (set
, integer_unlimited_completer
);
872 /* Add element named NAME to both the set and show command LISTs (the
873 list for set/show or some sublist thereof). CLASS is as in
874 add_cmd. VAR is address of the variable which will contain the
875 value. SET_DOC and SHOW_DOC are the documentation strings. */
877 add_setshow_zuinteger_cmd (const char *name
, enum command_class theclass
,
879 const char *set_doc
, const char *show_doc
,
880 const char *help_doc
,
881 cmd_const_sfunc_ftype
*set_func
,
882 show_value_ftype
*show_func
,
883 struct cmd_list_element
**set_list
,
884 struct cmd_list_element
**show_list
)
886 add_setshow_cmd_full (name
, theclass
, var_zuinteger
, var
,
887 set_doc
, show_doc
, help_doc
,
893 /* Remove the command named NAME from the command list. Return the
894 list commands which were aliased to the deleted command. If the
895 command had no aliases, return NULL. The various *HOOKs are set to
896 the pre- and post-hook commands for the deleted command. If the
897 command does not have a hook, the corresponding out parameter is
900 static struct cmd_list_element
*
901 delete_cmd (const char *name
, struct cmd_list_element
**list
,
902 struct cmd_list_element
**prehook
,
903 struct cmd_list_element
**prehookee
,
904 struct cmd_list_element
**posthook
,
905 struct cmd_list_element
**posthookee
)
907 struct cmd_list_element
*iter
;
908 struct cmd_list_element
**previous_chain_ptr
;
909 struct cmd_list_element
*aliases
= NULL
;
915 previous_chain_ptr
= list
;
917 for (iter
= *previous_chain_ptr
; iter
; iter
= *previous_chain_ptr
)
919 if (strcmp (iter
->name
, name
) == 0)
922 iter
->destroyer (iter
, iter
->context
);
923 if (iter
->hookee_pre
)
924 iter
->hookee_pre
->hook_pre
= 0;
925 *prehook
= iter
->hook_pre
;
926 *prehookee
= iter
->hookee_pre
;
927 if (iter
->hookee_post
)
928 iter
->hookee_post
->hook_post
= 0;
929 *posthook
= iter
->hook_post
;
930 *posthookee
= iter
->hookee_post
;
932 /* Update the link. */
933 *previous_chain_ptr
= iter
->next
;
935 aliases
= iter
->aliases
;
937 /* If this command was an alias, remove it from the list of
939 if (iter
->cmd_pointer
)
941 struct cmd_list_element
**prevp
= &iter
->cmd_pointer
->aliases
;
942 struct cmd_list_element
*a
= *prevp
;
946 prevp
= &a
->alias_chain
;
949 *prevp
= iter
->alias_chain
;
954 /* We won't see another command with the same name. */
958 previous_chain_ptr
= &iter
->next
;
964 /* Shorthands to the commands above. */
966 /* Add an element to the list of info subcommands. */
968 struct cmd_list_element
*
969 add_info (const char *name
, cmd_const_cfunc_ftype
*fun
, const char *doc
)
971 return add_cmd (name
, class_info
, fun
, doc
, &infolist
);
974 /* Add an alias to the list of info subcommands. */
976 struct cmd_list_element
*
977 add_info_alias (const char *name
, const char *oldname
, int abbrev_flag
)
979 return add_alias_cmd (name
, oldname
, class_run
, abbrev_flag
, &infolist
);
982 /* Add an element to the list of commands. */
984 struct cmd_list_element
*
985 add_com (const char *name
, enum command_class theclass
,
986 cmd_const_cfunc_ftype
*fun
,
989 return add_cmd (name
, theclass
, fun
, doc
, &cmdlist
);
992 /* Add an alias or abbreviation command to the list of commands. */
994 struct cmd_list_element
*
995 add_com_alias (const char *name
, const char *oldname
, enum command_class theclass
,
998 return add_alias_cmd (name
, oldname
, theclass
, abbrev_flag
, &cmdlist
);
1001 /* Add an element with a suppress notification to the list of commands. */
1003 struct cmd_list_element
*
1004 add_com_suppress_notification (const char *name
, enum command_class theclass
,
1005 cmd_const_cfunc_ftype
*fun
, const char *doc
,
1006 int *suppress_notification
)
1008 return add_cmd_suppress_notification (name
, theclass
, fun
, doc
,
1009 &cmdlist
, suppress_notification
);
1012 /* If VERBOSE, print the full help for command C and highlight the
1013 documentation parts matching HIGHLIGHT,
1014 otherwise print only one-line help for command C. */
1017 print_doc_of_command (struct cmd_list_element
*c
, const char *prefix
,
1018 bool verbose
, compiled_regex
&highlight
,
1019 struct ui_file
*stream
)
1021 /* When printing the full documentation, add a line to separate
1022 this documentation from the previous command help, in the likely
1023 case that apropos finds several commands. */
1025 fputs_filtered ("\n", stream
);
1027 fprintf_styled (stream
, title_style
.style (),
1028 "%s%s", prefix
, c
->name
);
1029 fputs_filtered (" -- ", stream
);
1031 fputs_highlighted (c
->doc
, highlight
, stream
);
1033 print_doc_line (stream
, c
->doc
, false);
1034 fputs_filtered ("\n", stream
);
1037 /* Recursively walk the commandlist structures, and print out the
1038 documentation of commands that match our regex in either their
1039 name, or their documentation.
1040 If VERBOSE, prints the complete documentation and highlight the
1041 documentation parts matching REGEX, otherwise prints only
1045 apropos_cmd (struct ui_file
*stream
,
1046 struct cmd_list_element
*commandlist
,
1047 bool verbose
, compiled_regex
®ex
, const char *prefix
)
1049 struct cmd_list_element
*c
;
1052 /* Walk through the commands. */
1053 for (c
=commandlist
;c
;c
=c
->next
)
1055 returnvalue
= -1; /* Needed to avoid double printing. */
1056 if (c
->name
!= NULL
)
1058 size_t name_len
= strlen (c
->name
);
1060 /* Try to match against the name. */
1061 returnvalue
= regex
.search (c
->name
, name_len
, 0, name_len
, NULL
);
1062 if (returnvalue
>= 0)
1063 print_doc_of_command (c
, prefix
, verbose
, regex
, stream
);
1065 if (c
->doc
!= NULL
&& returnvalue
< 0)
1067 size_t doc_len
= strlen (c
->doc
);
1069 /* Try to match against documentation. */
1070 if (regex
.search (c
->doc
, doc_len
, 0, doc_len
, NULL
) >= 0)
1071 print_doc_of_command (c
, prefix
, verbose
, regex
, stream
);
1073 /* Check if this command has subcommands and is not an
1074 abbreviation. We skip listing subcommands of abbreviations
1075 in order to avoid duplicates in the output. */
1076 if (c
->prefixlist
!= NULL
&& !c
->abbrev_flag
)
1078 /* Recursively call ourselves on the subcommand list,
1079 passing the right prefix in. */
1080 apropos_cmd (stream
, *c
->prefixlist
, verbose
, regex
, c
->prefixname
);
1085 /* This command really has to deal with two things:
1086 1) I want documentation on *this string* (usually called by
1087 "help commandname").
1089 2) I want documentation on *this list* (usually called by giving a
1090 command that requires subcommands. Also called by saying just
1093 I am going to split this into two separate commands, help_cmd and
1097 help_cmd (const char *command
, struct ui_file
*stream
)
1099 struct cmd_list_element
*c
;
1103 help_list (cmdlist
, "", all_classes
, stream
);
1107 if (strcmp (command
, "all") == 0)
1113 c
= lookup_cmd (&command
, cmdlist
, "", 0, 0);
1118 /* There are three cases here.
1119 If c->prefixlist is nonzero, we have a prefix command.
1120 Print its documentation, then list its subcommands.
1122 If c->func is non NULL, we really have a command. Print its
1123 documentation and return.
1125 If c->func is NULL, we have a class name. Print its
1126 documentation (as if it were a command) and then set class to the
1127 number of this class so that the commands in the class will be
1130 fputs_filtered (c
->doc
, stream
);
1131 fputs_filtered ("\n", stream
);
1133 if (c
->prefixlist
== 0 && c
->func
!= NULL
)
1135 fprintf_filtered (stream
, "\n");
1137 /* If this is a prefix command, print it's subcommands. */
1139 help_list (*c
->prefixlist
, c
->prefixname
, all_commands
, stream
);
1141 /* If this is a class name, print all of the commands in the class. */
1142 if (c
->func
== NULL
)
1143 help_list (cmdlist
, "", c
->theclass
, stream
);
1145 if (c
->hook_pre
|| c
->hook_post
)
1146 fprintf_filtered (stream
,
1147 "\nThis command has a hook (or hooks) defined:\n");
1150 fprintf_filtered (stream
,
1151 "\tThis command is run after : %s (pre hook)\n",
1154 fprintf_filtered (stream
,
1155 "\tThis command is run before : %s (post hook)\n",
1156 c
->hook_post
->name
);
1160 * Get a specific kind of help on a command list.
1163 * CMDTYPE is the prefix to use in the title string.
1164 * CLASS is the class with which to list the nodes of this list (see
1165 * documentation for help_cmd_list below), As usual, ALL_COMMANDS for
1166 * everything, ALL_CLASSES for just classes, and non-negative for only things
1167 * in a specific class.
1168 * and STREAM is the output stream on which to print things.
1169 * If you call this routine with a class >= 0, it recurses.
1172 help_list (struct cmd_list_element
*list
, const char *cmdtype
,
1173 enum command_class theclass
, struct ui_file
*stream
)
1176 char *cmdtype1
, *cmdtype2
;
1178 /* If CMDTYPE is "foo ", CMDTYPE1 gets " foo" and CMDTYPE2 gets "foo sub".
1180 len
= strlen (cmdtype
);
1181 cmdtype1
= (char *) alloca (len
+ 1);
1183 cmdtype2
= (char *) alloca (len
+ 4);
1188 memcpy (cmdtype1
+ 1, cmdtype
, len
- 1);
1190 memcpy (cmdtype2
, cmdtype
, len
- 1);
1191 strcpy (cmdtype2
+ len
- 1, " sub");
1194 if (theclass
== all_classes
)
1195 fprintf_filtered (stream
, "List of classes of %scommands:\n\n", cmdtype2
);
1197 fprintf_filtered (stream
, "List of %scommands:\n\n", cmdtype2
);
1199 help_cmd_list (list
, theclass
, cmdtype
, (int) theclass
>= 0, stream
);
1201 if (theclass
== all_classes
)
1203 fprintf_filtered (stream
, "\n\
1204 Type \"help%s\" followed by a class name for a list of commands in ",
1207 fprintf_filtered (stream
, "that class.");
1209 fprintf_filtered (stream
, "\n\
1210 Type \"help all\" for the list of all commands.");
1213 fprintf_filtered (stream
, "\nType \"help%s\" followed by %scommand name ",
1214 cmdtype1
, cmdtype2
);
1216 fputs_filtered ("for ", stream
);
1218 fputs_filtered ("full ", stream
);
1220 fputs_filtered ("documentation.\n", stream
);
1221 fputs_filtered ("Type \"apropos word\" to search "
1222 "for commands related to \"word\".\n", stream
);
1223 fputs_filtered ("Type \"apropos -v word\" for full documentation", stream
);
1225 fputs_filtered (" of commands related to \"word\".\n", stream
);
1226 fputs_filtered ("Command name abbreviations are allowed if unambiguous.\n",
1231 help_all (struct ui_file
*stream
)
1233 struct cmd_list_element
*c
;
1234 int seen_unclassified
= 0;
1236 for (c
= cmdlist
; c
; c
= c
->next
)
1240 /* If this is a class name, print all of the commands in the
1243 if (c
->func
== NULL
)
1245 fprintf_filtered (stream
, "\nCommand class: %s\n\n", c
->name
);
1246 help_cmd_list (cmdlist
, c
->theclass
, "", 1, stream
);
1250 /* While it's expected that all commands are in some class,
1251 as a safety measure, we'll print commands outside of any
1252 class at the end. */
1254 for (c
= cmdlist
; c
; c
= c
->next
)
1259 if (c
->theclass
== no_class
)
1261 if (!seen_unclassified
)
1263 fprintf_filtered (stream
, "\nUnclassified commands\n\n");
1264 seen_unclassified
= 1;
1266 print_help_for_command (c
, "", 1, stream
);
1272 /* See cli-decode.h. */
1275 print_doc_line (struct ui_file
*stream
, const char *str
,
1276 bool for_value_prefix
)
1278 static char *line_buffer
= 0;
1279 static int line_size
;
1285 line_buffer
= (char *) xmalloc (line_size
);
1288 /* Searches for the first end of line or the end of STR. */
1290 while (*p
&& *p
!= '\n')
1292 if (p
- str
> line_size
- 1)
1294 line_size
= p
- str
+ 1;
1295 xfree (line_buffer
);
1296 line_buffer
= (char *) xmalloc (line_size
);
1298 strncpy (line_buffer
, str
, p
- str
);
1299 if (for_value_prefix
)
1301 if (islower (line_buffer
[0]))
1302 line_buffer
[0] = toupper (line_buffer
[0]);
1303 gdb_assert (p
> str
);
1304 if (line_buffer
[p
- str
- 1] == '.')
1305 line_buffer
[p
- str
- 1] = '\0';
1307 line_buffer
[p
- str
] = '\0';
1310 line_buffer
[p
- str
] = '\0';
1311 fputs_filtered (line_buffer
, stream
);
1314 /* Print one-line help for command C.
1315 If RECURSE is non-zero, also print one-line descriptions
1316 of all prefixed subcommands. */
1318 print_help_for_command (struct cmd_list_element
*c
, const char *prefix
,
1319 int recurse
, struct ui_file
*stream
)
1321 fprintf_styled (stream
, title_style
.style (),
1322 "%s%s", prefix
, c
->name
);
1323 fputs_filtered (" -- ", stream
);
1324 print_doc_line (stream
, c
->doc
, false);
1325 fputs_filtered ("\n", stream
);
1328 && c
->prefixlist
!= 0
1329 && c
->abbrev_flag
== 0)
1330 /* Subcommands of a prefix command typically have 'all_commands'
1331 as class. If we pass CLASS to recursive invocation,
1332 most often we won't see anything. */
1333 help_cmd_list (*c
->prefixlist
, all_commands
, c
->prefixname
, 1, stream
);
1337 * Implement a help command on command list LIST.
1338 * RECURSE should be non-zero if this should be done recursively on
1339 * all sublists of LIST.
1340 * PREFIX is the prefix to print before each command name.
1341 * STREAM is the stream upon which the output should be written.
1342 * THECLASS should be:
1343 * A non-negative class number to list only commands in that
1345 * ALL_COMMANDS to list all commands in list.
1346 * ALL_CLASSES to list all classes in list.
1348 * Note that RECURSE will be active on *all* sublists, not just the
1349 * ones selected by the criteria above (ie. the selection mechanism
1350 * is at the low level, not the high-level).
1353 help_cmd_list (struct cmd_list_element
*list
, enum command_class theclass
,
1354 const char *prefix
, int recurse
, struct ui_file
*stream
)
1356 struct cmd_list_element
*c
;
1358 for (c
= list
; c
; c
= c
->next
)
1360 if (c
->abbrev_flag
== 0
1361 && !c
->cmd_deprecated
1362 && (theclass
== all_commands
1363 || (theclass
== all_classes
&& c
->func
== NULL
)
1364 || (theclass
== c
->theclass
&& c
->func
!= NULL
)))
1366 print_help_for_command (c
, prefix
, recurse
, stream
);
1368 else if (c
->abbrev_flag
== 0
1370 && !c
->cmd_deprecated
1371 && theclass
== class_user
&& c
->prefixlist
!= NULL
)
1372 /* User-defined commands may be subcommands. */
1373 help_cmd_list (*c
->prefixlist
, theclass
, c
->prefixname
,
1379 /* Search the input clist for 'command'. Return the command if
1380 found (or NULL if not), and return the number of commands
1383 static struct cmd_list_element
*
1384 find_cmd (const char *command
, int len
, struct cmd_list_element
*clist
,
1385 int ignore_help_classes
, int *nfound
)
1387 struct cmd_list_element
*found
, *c
;
1391 for (c
= clist
; c
; c
= c
->next
)
1392 if (!strncmp (command
, c
->name
, len
)
1393 && (!ignore_help_classes
|| c
->func
))
1397 if (c
->name
[len
] == '\0')
1406 /* Return the length of command name in TEXT. */
1409 find_command_name_length (const char *text
)
1411 const char *p
= text
;
1413 /* Treating underscores as part of command words is important
1414 so that "set args_foo()" doesn't get interpreted as
1415 "set args _foo()". */
1416 /* Some characters are only used for TUI specific commands.
1417 However, they are always allowed for the sake of consistency.
1419 Note that this is larger than the character set allowed when
1420 creating user-defined commands. */
1422 /* Recognize the single character commands so that, e.g., "!ls"
1423 works as expected. */
1424 if (*p
== '!' || *p
== '|')
1427 while (valid_cmd_char_p (*p
)
1428 /* Characters used by TUI specific commands. */
1429 || *p
== '+' || *p
== '<' || *p
== '>' || *p
== '$')
1435 /* See command.h. */
1438 valid_cmd_char_p (int c
)
1440 /* Alas "42" is a legitimate user-defined command.
1441 In the interests of not breaking anything we preserve that. */
1443 return isalnum (c
) || c
== '-' || c
== '_' || c
== '.';
1446 /* See command.h. */
1449 valid_user_defined_cmd_name_p (const char *name
)
1456 for (p
= name
; *p
!= '\0'; ++p
)
1458 if (valid_cmd_char_p (*p
))
1467 /* This routine takes a line of TEXT and a CLIST in which to start the
1468 lookup. When it returns it will have incremented the text pointer past
1469 the section of text it matched, set *RESULT_LIST to point to the list in
1470 which the last word was matched, and will return a pointer to the cmd
1471 list element which the text matches. It will return NULL if no match at
1472 all was possible. It will return -1 (cast appropriately, ick) if ambigous
1473 matches are possible; in this case *RESULT_LIST will be set to point to
1474 the list in which there are ambiguous choices (and *TEXT will be set to
1475 the ambiguous text string).
1477 If the located command was an abbreviation, this routine returns the base
1478 command of the abbreviation.
1480 It does no error reporting whatsoever; control will always return
1481 to the superior routine.
1483 In the case of an ambiguous return (-1), *RESULT_LIST will be set to point
1484 at the prefix_command (ie. the best match) *or* (special case) will be NULL
1485 if no prefix command was ever found. For example, in the case of "info a",
1486 "info" matches without ambiguity, but "a" could be "args" or "address", so
1487 *RESULT_LIST is set to the cmd_list_element for "info". So in this case
1488 RESULT_LIST should not be interpreted as a pointer to the beginning of a
1489 list; it simply points to a specific command. In the case of an ambiguous
1490 return *TEXT is advanced past the last non-ambiguous prefix (e.g.
1491 "info t" can be "info types" or "info target"; upon return *TEXT has been
1492 advanced past "info ").
1494 If RESULT_LIST is NULL, don't set *RESULT_LIST (but don't otherwise
1495 affect the operation).
1497 This routine does *not* modify the text pointed to by TEXT.
1499 If IGNORE_HELP_CLASSES is nonzero, ignore any command list elements which
1500 are actually help classes rather than commands (i.e. the function field of
1501 the struct cmd_list_element is NULL). */
1503 struct cmd_list_element
*
1504 lookup_cmd_1 (const char **text
, struct cmd_list_element
*clist
,
1505 struct cmd_list_element
**result_list
, int ignore_help_classes
)
1509 struct cmd_list_element
*found
, *c
;
1510 const char *line
= *text
;
1512 while (**text
== ' ' || **text
== '\t')
1515 /* Identify the name of the command. */
1516 len
= find_command_name_length (*text
);
1518 /* If nothing but whitespace, return 0. */
1522 /* *text and p now bracket the first command word to lookup (and
1523 it's length is len). We copy this into a local temporary. */
1526 command
= (char *) alloca (len
+ 1);
1527 memcpy (command
, *text
, len
);
1528 command
[len
] = '\0';
1533 found
= find_cmd (command
, len
, clist
, ignore_help_classes
, &nfound
);
1535 /* If nothing matches, we have a simple failure. */
1541 if (result_list
!= NULL
)
1542 /* Will be modified in calling routine
1543 if we know what the prefix command is. */
1545 return CMD_LIST_AMBIGUOUS
; /* Ambiguous. */
1548 /* We've matched something on this list. Move text pointer forward. */
1552 if (found
->cmd_pointer
)
1554 /* We drop the alias (abbreviation) in favor of the command it
1555 is pointing to. If the alias is deprecated, though, we need to
1556 warn the user about it before we drop it. Note that while we
1557 are warning about the alias, we may also warn about the command
1558 itself and we will adjust the appropriate DEPRECATED_WARN_USER
1561 if (found
->deprecated_warn_user
)
1562 deprecated_cmd_warning (line
);
1563 found
= found
->cmd_pointer
;
1565 /* If we found a prefix command, keep looking. */
1567 if (found
->prefixlist
)
1569 c
= lookup_cmd_1 (text
, *found
->prefixlist
, result_list
,
1570 ignore_help_classes
);
1573 /* Didn't find anything; this is as far as we got. */
1574 if (result_list
!= NULL
)
1575 *result_list
= clist
;
1578 else if (c
== CMD_LIST_AMBIGUOUS
)
1580 /* We've gotten this far properly, but the next step is
1581 ambiguous. We need to set the result list to the best
1582 we've found (if an inferior hasn't already set it). */
1583 if (result_list
!= NULL
)
1585 /* This used to say *result_list = *found->prefixlist.
1586 If that was correct, need to modify the documentation
1587 at the top of this function to clarify what is
1588 supposed to be going on. */
1589 *result_list
= found
;
1600 if (result_list
!= NULL
)
1601 *result_list
= clist
;
1606 /* All this hair to move the space to the front of cmdtype */
1609 undef_cmd_error (const char *cmdtype
, const char *q
)
1611 error (_("Undefined %scommand: \"%s\". Try \"help%s%.*s\"."),
1614 *cmdtype
? " " : "",
1615 (int) strlen (cmdtype
) - 1,
1619 /* Look up the contents of *LINE as a command in the command list LIST.
1620 LIST is a chain of struct cmd_list_element's.
1621 If it is found, return the struct cmd_list_element for that command
1622 and update *LINE to point after the command name, at the first argument.
1623 If not found, call error if ALLOW_UNKNOWN is zero
1624 otherwise (or if error returns) return zero.
1625 Call error if specified command is ambiguous,
1626 unless ALLOW_UNKNOWN is negative.
1627 CMDTYPE precedes the word "command" in the error message.
1629 If IGNORE_HELP_CLASSES is nonzero, ignore any command list
1630 elements which are actually help classes rather than commands (i.e.
1631 the function field of the struct cmd_list_element is 0). */
1633 struct cmd_list_element
*
1634 lookup_cmd (const char **line
, struct cmd_list_element
*list
,
1635 const char *cmdtype
,
1636 int allow_unknown
, int ignore_help_classes
)
1638 struct cmd_list_element
*last_list
= 0;
1639 struct cmd_list_element
*c
;
1641 /* Note: Do not remove trailing whitespace here because this
1642 would be wrong for complete_command. Jim Kingdon */
1645 error (_("Lack of needed %scommand"), cmdtype
);
1647 c
= lookup_cmd_1 (line
, list
, &last_list
, ignore_help_classes
);
1654 int len
= find_command_name_length (*line
);
1656 q
= (char *) alloca (len
+ 1);
1657 strncpy (q
, *line
, len
);
1659 undef_cmd_error (cmdtype
, q
);
1664 else if (c
== CMD_LIST_AMBIGUOUS
)
1666 /* Ambigous. Local values should be off prefixlist or called
1668 int local_allow_unknown
= (last_list
? last_list
->allow_unknown
:
1670 const char *local_cmdtype
= last_list
? last_list
->prefixname
: cmdtype
;
1671 struct cmd_list_element
*local_list
=
1672 (last_list
? *(last_list
->prefixlist
) : list
);
1674 if (local_allow_unknown
< 0)
1677 return last_list
; /* Found something. */
1679 return 0; /* Found nothing. */
1683 /* Report as error. */
1688 ((*line
)[amb_len
] && (*line
)[amb_len
] != ' '
1689 && (*line
)[amb_len
] != '\t');
1694 for (c
= local_list
; c
; c
= c
->next
)
1695 if (!strncmp (*line
, c
->name
, amb_len
))
1697 if (strlen (ambbuf
) + strlen (c
->name
) + 6
1698 < (int) sizeof ambbuf
)
1700 if (strlen (ambbuf
))
1701 strcat (ambbuf
, ", ");
1702 strcat (ambbuf
, c
->name
);
1706 strcat (ambbuf
, "..");
1710 error (_("Ambiguous %scommand \"%s\": %s."), local_cmdtype
,
1716 if (c
->type
== set_cmd
&& **line
!= '\0' && !isspace (**line
))
1717 error (_("Argument must be preceded by space."));
1719 /* We've got something. It may still not be what the caller
1720 wants (if this command *needs* a subcommand). */
1721 while (**line
== ' ' || **line
== '\t')
1724 if (c
->prefixlist
&& **line
&& !c
->allow_unknown
)
1725 undef_cmd_error (c
->prefixname
, *line
);
1727 /* Seems to be what he wants. Return it. */
1733 /* We are here presumably because an alias or command in TEXT is
1734 deprecated and a warning message should be generated. This
1735 function decodes TEXT and potentially generates a warning message
1738 Example for 'set endian big' which has a fictitious alias 'seb'.
1740 If alias wasn't used in TEXT, and the command is deprecated:
1741 "warning: 'set endian big' is deprecated."
1743 If alias was used, and only the alias is deprecated:
1744 "warning: 'seb' an alias for the command 'set endian big' is deprecated."
1746 If alias was used and command is deprecated (regardless of whether
1747 the alias itself is deprecated:
1749 "warning: 'set endian big' (seb) is deprecated."
1751 After the message has been sent, clear the appropriate flags in the
1752 command and/or the alias so the user is no longer bothered.
1756 deprecated_cmd_warning (const char *text
)
1758 struct cmd_list_element
*alias
= NULL
;
1759 struct cmd_list_element
*prefix_cmd
= NULL
;
1760 struct cmd_list_element
*cmd
= NULL
;
1762 if (!lookup_cmd_composition (text
, &alias
, &prefix_cmd
, &cmd
))
1763 /* Return if text doesn't evaluate to a command. */
1766 if (!((alias
? alias
->deprecated_warn_user
: 0)
1767 || cmd
->deprecated_warn_user
) )
1768 /* Return if nothing is deprecated. */
1771 printf_filtered ("Warning:");
1773 if (alias
&& !cmd
->cmd_deprecated
)
1774 printf_filtered (" '%s', an alias for the", alias
->name
);
1776 printf_filtered (" command '");
1779 printf_filtered ("%s", prefix_cmd
->prefixname
);
1781 printf_filtered ("%s", cmd
->name
);
1783 if (alias
&& cmd
->cmd_deprecated
)
1784 printf_filtered ("' (%s) is deprecated.\n", alias
->name
);
1786 printf_filtered ("' is deprecated.\n");
1789 /* If it is only the alias that is deprecated, we want to indicate
1790 the new alias, otherwise we'll indicate the new command. */
1792 if (alias
&& !cmd
->cmd_deprecated
)
1794 if (alias
->replacement
)
1795 printf_filtered ("Use '%s'.\n\n", alias
->replacement
);
1797 printf_filtered ("No alternative known.\n\n");
1801 if (cmd
->replacement
)
1802 printf_filtered ("Use '%s'.\n\n", cmd
->replacement
);
1804 printf_filtered ("No alternative known.\n\n");
1807 /* We've warned you, now we'll keep quiet. */
1809 alias
->deprecated_warn_user
= 0;
1811 cmd
->deprecated_warn_user
= 0;
1815 /* Look up the contents of LINE as a command in the command list 'cmdlist'.
1816 Return 1 on success, 0 on failure.
1818 If LINE refers to an alias, *alias will point to that alias.
1820 If LINE is a postfix command (i.e. one that is preceded by a prefix
1821 command) set *prefix_cmd.
1823 Set *cmd to point to the command LINE indicates.
1825 If any of *alias, *prefix_cmd, or *cmd cannot be determined or do not
1826 exist, they are NULL when we return.
1830 lookup_cmd_composition (const char *text
,
1831 struct cmd_list_element
**alias
,
1832 struct cmd_list_element
**prefix_cmd
,
1833 struct cmd_list_element
**cmd
)
1837 struct cmd_list_element
*cur_list
;
1838 struct cmd_list_element
*prev_cmd
;
1848 /* Go through as many command lists as we need to,
1849 to find the command TEXT refers to. */
1853 while (*text
== ' ' || *text
== '\t')
1856 /* Identify the name of the command. */
1857 len
= find_command_name_length (text
);
1859 /* If nothing but whitespace, return. */
1863 /* Text is the start of the first command word to lookup (and
1864 it's length is len). We copy this into a local temporary. */
1866 command
= (char *) alloca (len
+ 1);
1867 memcpy (command
, text
, len
);
1868 command
[len
] = '\0';
1873 *cmd
= find_cmd (command
, len
, cur_list
, 1, &nfound
);
1875 if (*cmd
== CMD_LIST_AMBIGUOUS
)
1877 return 0; /* ambiguous */
1881 return 0; /* nothing found */
1884 if ((*cmd
)->cmd_pointer
)
1886 /* cmd was actually an alias, we note that an alias was
1887 used (by assigning *alais) and we set *cmd. */
1889 *cmd
= (*cmd
)->cmd_pointer
;
1891 *prefix_cmd
= prev_cmd
;
1893 if ((*cmd
)->prefixlist
)
1894 cur_list
= *(*cmd
)->prefixlist
;
1902 /* Helper function for SYMBOL_COMPLETION_FUNCTION. */
1904 /* Return a vector of char pointers which point to the different
1905 possible completions in LIST of TEXT.
1907 WORD points in the same buffer as TEXT, and completions should be
1908 returned relative to this position. For example, suppose TEXT is
1909 "foo" and we want to complete to "foobar". If WORD is "oo", return
1910 "oobar"; if WORD is "baz/foo", return "baz/foobar". */
1913 complete_on_cmdlist (struct cmd_list_element
*list
,
1914 completion_tracker
&tracker
,
1915 const char *text
, const char *word
,
1916 int ignore_help_classes
)
1918 struct cmd_list_element
*ptr
;
1919 int textlen
= strlen (text
);
1921 int saw_deprecated_match
= 0;
1923 /* We do one or two passes. In the first pass, we skip deprecated
1924 commands. If we see no matching commands in the first pass, and
1925 if we did happen to see a matching deprecated command, we do
1926 another loop to collect those. */
1927 for (pass
= 0; pass
< 2; ++pass
)
1929 bool got_matches
= false;
1931 for (ptr
= list
; ptr
; ptr
= ptr
->next
)
1932 if (!strncmp (ptr
->name
, text
, textlen
)
1933 && !ptr
->abbrev_flag
1934 && (!ignore_help_classes
|| ptr
->func
1935 || ptr
->prefixlist
))
1939 if (ptr
->cmd_deprecated
)
1941 saw_deprecated_match
= 1;
1946 tracker
.add_completion
1947 (make_completion_match_str (ptr
->name
, text
, word
));
1954 /* If we saw no matching deprecated commands in the first pass,
1956 if (!saw_deprecated_match
)
1961 /* Helper function for SYMBOL_COMPLETION_FUNCTION. */
1963 /* Add the different possible completions in ENUMLIST of TEXT.
1965 WORD points in the same buffer as TEXT, and completions should be
1966 returned relative to this position. For example, suppose TEXT is "foo"
1967 and we want to complete to "foobar". If WORD is "oo", return
1968 "oobar"; if WORD is "baz/foo", return "baz/foobar". */
1971 complete_on_enum (completion_tracker
&tracker
,
1972 const char *const *enumlist
,
1973 const char *text
, const char *word
)
1975 int textlen
= strlen (text
);
1979 for (i
= 0; (name
= enumlist
[i
]) != NULL
; i
++)
1980 if (strncmp (name
, text
, textlen
) == 0)
1981 tracker
.add_completion (make_completion_match_str (name
, text
, word
));
1985 /* Check function pointer. */
1987 cmd_func_p (struct cmd_list_element
*cmd
)
1989 return (cmd
->func
!= NULL
);
1993 /* Call the command function. */
1995 cmd_func (struct cmd_list_element
*cmd
, const char *args
, int from_tty
)
1997 if (cmd_func_p (cmd
))
1999 gdb::optional
<scoped_restore_tmpl
<int>> restore_suppress
;
2001 if (cmd
->suppress_notification
!= NULL
)
2002 restore_suppress
.emplace (cmd
->suppress_notification
, 1);
2004 (*cmd
->func
) (cmd
, args
, from_tty
);
2007 error (_("Invalid command"));
2011 cli_user_command_p (struct cmd_list_element
*cmd
)
2013 return (cmd
->theclass
== class_user
2014 && (cmd
->func
== do_const_cfunc
|| cmd
->func
== do_sfunc
));