1 /* Handle lists of commands, their decoding and documentation, for GDB.
3 Copyright (C) 1986-2017 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 "common/gdb_optional.h"
28 /* Prototypes for local functions. */
30 static void undef_cmd_error (const char *, const char *);
32 static struct cmd_list_element
*delete_cmd (const char *name
,
33 struct cmd_list_element
**list
,
34 struct cmd_list_element
**prehook
,
35 struct cmd_list_element
**prehookee
,
36 struct cmd_list_element
**posthook
,
37 struct cmd_list_element
**posthookee
);
39 static struct cmd_list_element
*find_cmd (const char *command
,
41 struct cmd_list_element
*clist
,
42 int ignore_help_classes
,
45 static void help_all (struct ui_file
*stream
);
47 /* Look up a command whose 'prefixlist' is KEY. Return the command if found,
48 otherwise return NULL. */
50 static struct cmd_list_element
*
51 lookup_cmd_for_prefixlist (struct cmd_list_element
**key
,
52 struct cmd_list_element
*list
)
54 struct cmd_list_element
*p
= NULL
;
56 for (p
= list
; p
!= NULL
; p
= p
->next
)
58 struct cmd_list_element
*q
;
60 if (p
->prefixlist
== NULL
)
62 else if (p
->prefixlist
== key
)
65 q
= lookup_cmd_for_prefixlist (key
, *(p
->prefixlist
));
74 set_cmd_prefix (struct cmd_list_element
*c
, struct cmd_list_element
**list
)
76 struct cmd_list_element
*p
;
78 /* Check to see if *LIST contains any element other than C. */
79 for (p
= *list
; p
!= NULL
; p
= p
->next
)
85 /* *SET_LIST only contains SET. */
86 p
= lookup_cmd_for_prefixlist (list
, setlist
);
88 c
->prefix
= p
? (p
->cmd_pointer
? p
->cmd_pointer
: p
) : p
;
91 c
->prefix
= p
->prefix
;
95 print_help_for_command (struct cmd_list_element
*c
, const char *prefix
,
96 int recurse
, struct ui_file
*stream
);
99 /* Set the callback function for the specified command. For each both
100 the commands callback and func() are set. The latter set to a
101 bounce function (unless cfunc / sfunc is NULL that is). */
104 do_cfunc (struct cmd_list_element
*c
, char *args
, int from_tty
)
106 c
->function
.cfunc (args
, from_tty
); /* Ok. */
110 set_cmd_cfunc (struct cmd_list_element
*cmd
, cmd_cfunc_ftype
*cfunc
)
115 cmd
->func
= do_cfunc
;
116 cmd
->function
.cfunc
= cfunc
; /* Ok. */
120 do_sfunc (struct cmd_list_element
*c
, char *args
, int from_tty
)
122 c
->function
.sfunc (args
, from_tty
, c
); /* Ok. */
126 set_cmd_sfunc (struct cmd_list_element
*cmd
, cmd_sfunc_ftype
*sfunc
)
131 cmd
->func
= do_sfunc
;
132 cmd
->function
.sfunc
= sfunc
; /* Ok. */
136 cmd_cfunc_eq (struct cmd_list_element
*cmd
, cmd_cfunc_ftype
*cfunc
)
138 return cmd
->func
== do_cfunc
&& cmd
->function
.cfunc
== cfunc
;
142 set_cmd_context (struct cmd_list_element
*cmd
, void *context
)
144 cmd
->context
= context
;
148 get_cmd_context (struct cmd_list_element
*cmd
)
154 cmd_type (struct cmd_list_element
*cmd
)
160 set_cmd_completer (struct cmd_list_element
*cmd
, completer_ftype
*completer
)
162 cmd
->completer
= completer
; /* Ok. */
165 /* See definition in commands.h. */
168 set_cmd_completer_handle_brkchars (struct cmd_list_element
*cmd
,
169 completer_ftype_void
*completer_handle_brkchars
)
171 cmd
->completer_handle_brkchars
= completer_handle_brkchars
;
174 /* Add element named NAME.
175 Space for NAME and DOC must be allocated by the caller.
176 CLASS is the top level category into which commands are broken down
178 FUN should be the function to execute the command;
179 it will get a character string as argument, with leading
180 and trailing blanks already eliminated.
182 DOC is a documentation string for the command.
183 Its first line should be a complete sentence.
184 It should start with ? for a command that is an abbreviation
185 or with * for a command that most users don't need to know about.
187 Add this command to command list *LIST.
189 Returns a pointer to the added command (not necessarily the head
192 struct cmd_list_element
*
193 add_cmd (const char *name
, enum command_class theclass
, cmd_cfunc_ftype
*fun
,
194 const char *doc
, struct cmd_list_element
**list
)
196 struct cmd_list_element
*c
= XNEW (struct cmd_list_element
);
197 struct cmd_list_element
*p
, *iter
;
199 /* Turn each alias of the old command into an alias of the new
201 c
->aliases
= delete_cmd (name
, list
, &c
->hook_pre
, &c
->hookee_pre
,
202 &c
->hook_post
, &c
->hookee_post
);
203 for (iter
= c
->aliases
; iter
; iter
= iter
->alias_chain
)
204 iter
->cmd_pointer
= c
;
206 c
->hook_pre
->hookee_pre
= c
;
208 c
->hookee_pre
->hook_pre
= c
;
210 c
->hook_post
->hookee_post
= c
;
212 c
->hookee_post
->hook_post
= c
;
214 if (*list
== NULL
|| strcmp ((*list
)->name
, name
) >= 0)
222 while (p
->next
&& strcmp (p
->next
->name
, name
) <= 0)
231 c
->theclass
= theclass
;
232 set_cmd_cfunc (c
, fun
);
233 set_cmd_context (c
, NULL
);
235 c
->cmd_deprecated
= 0;
236 c
->deprecated_warn_user
= 0;
237 c
->malloced_replacement
= 0;
238 c
->doc_allocated
= 0;
239 c
->replacement
= NULL
;
240 c
->pre_show_hook
= NULL
;
242 c
->prefixlist
= NULL
;
243 c
->prefixname
= NULL
;
244 c
->allow_unknown
= 0;
247 set_cmd_completer (c
, make_symbol_completion_list_fn
);
248 c
->completer_handle_brkchars
= NULL
;
250 c
->type
= not_set_cmd
;
252 c
->var_type
= var_boolean
;
254 c
->user_commands
= NULL
;
255 c
->cmd_pointer
= NULL
;
256 c
->alias_chain
= NULL
;
257 c
->suppress_notification
= NULL
;
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
, NULL
, 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
;
323 set_cmd_prefix (c
, list
);
327 struct cmd_list_element
*
328 add_alias_cmd (const char *name
, const char *oldname
,
329 enum command_class theclass
, int abbrev_flag
,
330 struct cmd_list_element
**list
)
333 struct cmd_list_element
*old
;
336 old
= lookup_cmd (&tmp
, *list
, "", 1, 1);
338 return add_alias_cmd (name
, old
, theclass
, abbrev_flag
, list
);
342 /* Like add_cmd but adds an element for a command prefix: a name that
343 should be followed by a subcommand to be looked up in another
344 command list. PREFIXLIST should be the address of the variable
345 containing that list. */
347 struct cmd_list_element
*
348 add_prefix_cmd (const char *name
, enum command_class theclass
,
349 cmd_cfunc_ftype
*fun
,
350 const char *doc
, struct cmd_list_element
**prefixlist
,
351 const char *prefixname
, int allow_unknown
,
352 struct cmd_list_element
**list
)
354 struct cmd_list_element
*c
= add_cmd (name
, theclass
, fun
, doc
, list
);
355 struct cmd_list_element
*p
;
357 c
->prefixlist
= prefixlist
;
358 c
->prefixname
= prefixname
;
359 c
->allow_unknown
= allow_unknown
;
361 if (list
== &cmdlist
)
364 set_cmd_prefix (c
, list
);
366 /* Update the field 'prefix' of each cmd_list_element in *PREFIXLIST. */
367 for (p
= *prefixlist
; p
!= NULL
; p
= p
->next
)
373 /* Like add_prefix_cmd but sets the abbrev_flag on the new command. */
375 struct cmd_list_element
*
376 add_abbrev_prefix_cmd (const char *name
, enum command_class theclass
,
377 cmd_cfunc_ftype
*fun
, const char *doc
,
378 struct cmd_list_element
**prefixlist
,
379 const char *prefixname
,
380 int allow_unknown
, struct cmd_list_element
**list
)
382 struct cmd_list_element
*c
= add_cmd (name
, theclass
, fun
, doc
, list
);
384 c
->prefixlist
= prefixlist
;
385 c
->prefixname
= prefixname
;
386 c
->allow_unknown
= allow_unknown
;
391 /* This is an empty "cfunc". */
393 not_just_help_class_command (char *args
, int from_tty
)
397 /* This is an empty "sfunc". */
398 static void empty_sfunc (char *, int, struct cmd_list_element
*);
401 empty_sfunc (char *args
, int from_tty
, struct cmd_list_element
*c
)
405 /* Add element named NAME to command list LIST (the list for set/show
406 or some sublist thereof).
407 TYPE is set_cmd or show_cmd.
408 CLASS is as in add_cmd.
409 VAR_TYPE is the kind of thing we are setting.
410 VAR is address of the variable being controlled by this command.
411 DOC is the documentation string. */
413 static struct cmd_list_element
*
414 add_set_or_show_cmd (const char *name
,
416 enum command_class theclass
,
420 struct cmd_list_element
**list
)
422 struct cmd_list_element
*c
= add_cmd (name
, theclass
, NULL
, doc
, list
);
424 gdb_assert (type
== set_cmd
|| type
== show_cmd
);
426 c
->var_type
= var_type
;
428 /* This needs to be something besides NULL so that this isn't
429 treated as a help class. */
430 set_cmd_sfunc (c
, empty_sfunc
);
434 /* Add element named NAME to both the command SET_LIST and SHOW_LIST.
435 CLASS is as in add_cmd. VAR_TYPE is the kind of thing we are
436 setting. VAR is address of the variable being controlled by this
437 command. SET_FUNC and SHOW_FUNC are the callback functions (if
438 non-NULL). SET_DOC, SHOW_DOC and HELP_DOC are the documentation
439 strings. PRINT the format string to print the value. SET_RESULT
440 and SHOW_RESULT, if not NULL, are set to the resulting command
444 add_setshow_cmd_full (const char *name
,
445 enum command_class theclass
,
446 var_types var_type
, void *var
,
447 const char *set_doc
, const char *show_doc
,
448 const char *help_doc
,
449 cmd_sfunc_ftype
*set_func
,
450 show_value_ftype
*show_func
,
451 struct cmd_list_element
**set_list
,
452 struct cmd_list_element
**show_list
,
453 struct cmd_list_element
**set_result
,
454 struct cmd_list_element
**show_result
)
456 struct cmd_list_element
*set
;
457 struct cmd_list_element
*show
;
461 if (help_doc
!= NULL
)
463 full_set_doc
= xstrprintf ("%s\n%s", set_doc
, help_doc
);
464 full_show_doc
= xstrprintf ("%s\n%s", show_doc
, help_doc
);
468 full_set_doc
= xstrdup (set_doc
);
469 full_show_doc
= xstrdup (show_doc
);
471 set
= add_set_or_show_cmd (name
, set_cmd
, theclass
, var_type
, var
,
472 full_set_doc
, set_list
);
473 set
->doc_allocated
= 1;
475 if (set_func
!= NULL
)
476 set_cmd_sfunc (set
, set_func
);
478 set_cmd_prefix (set
, set_list
);
480 show
= add_set_or_show_cmd (name
, show_cmd
, theclass
, var_type
, var
,
481 full_show_doc
, show_list
);
482 show
->doc_allocated
= 1;
483 show
->show_value_func
= show_func
;
485 if (set_result
!= NULL
)
487 if (show_result
!= NULL
)
491 /* Add element named NAME to command list LIST (the list for set or
492 some sublist thereof). CLASS is as in add_cmd. ENUMLIST is a list
493 of strings which may follow NAME. VAR is address of the variable
494 which will contain the matching string (from ENUMLIST). */
497 add_setshow_enum_cmd (const char *name
,
498 enum command_class theclass
,
499 const char *const *enumlist
,
502 const char *show_doc
,
503 const char *help_doc
,
504 cmd_sfunc_ftype
*set_func
,
505 show_value_ftype
*show_func
,
506 struct cmd_list_element
**set_list
,
507 struct cmd_list_element
**show_list
)
509 struct cmd_list_element
*c
;
511 add_setshow_cmd_full (name
, theclass
, var_enum
, var
,
512 set_doc
, show_doc
, help_doc
,
519 const char * const auto_boolean_enums
[] = { "on", "off", "auto", NULL
};
521 /* Add an auto-boolean command named NAME to both the set and show
522 command list lists. CLASS is as in add_cmd. VAR is address of the
523 variable which will contain the value. DOC is the documentation
524 string. FUNC is the corresponding callback. */
526 add_setshow_auto_boolean_cmd (const char *name
,
527 enum command_class theclass
,
528 enum auto_boolean
*var
,
529 const char *set_doc
, const char *show_doc
,
530 const char *help_doc
,
531 cmd_sfunc_ftype
*set_func
,
532 show_value_ftype
*show_func
,
533 struct cmd_list_element
**set_list
,
534 struct cmd_list_element
**show_list
)
536 struct cmd_list_element
*c
;
538 add_setshow_cmd_full (name
, theclass
, var_auto_boolean
, var
,
539 set_doc
, show_doc
, help_doc
,
543 c
->enums
= auto_boolean_enums
;
546 /* Add element named NAME to both the set and show command LISTs (the
547 list for set/show or some sublist thereof). CLASS is as in
548 add_cmd. VAR is address of the variable which will contain the
549 value. SET_DOC and SHOW_DOC are the documentation strings. */
551 add_setshow_boolean_cmd (const char *name
, enum command_class theclass
, int *var
,
552 const char *set_doc
, const char *show_doc
,
553 const char *help_doc
,
554 cmd_sfunc_ftype
*set_func
,
555 show_value_ftype
*show_func
,
556 struct cmd_list_element
**set_list
,
557 struct cmd_list_element
**show_list
)
559 static const char *boolean_enums
[] = { "on", "off", NULL
};
560 struct cmd_list_element
*c
;
562 add_setshow_cmd_full (name
, theclass
, var_boolean
, var
,
563 set_doc
, show_doc
, help_doc
,
567 c
->enums
= boolean_enums
;
570 /* Add element named NAME to both the set and show command LISTs (the
571 list for set/show or some sublist thereof). */
573 add_setshow_filename_cmd (const char *name
, enum command_class theclass
,
575 const char *set_doc
, const char *show_doc
,
576 const char *help_doc
,
577 cmd_sfunc_ftype
*set_func
,
578 show_value_ftype
*show_func
,
579 struct cmd_list_element
**set_list
,
580 struct cmd_list_element
**show_list
)
582 struct cmd_list_element
*set_result
;
584 add_setshow_cmd_full (name
, theclass
, var_filename
, var
,
585 set_doc
, show_doc
, help_doc
,
589 set_cmd_completer (set_result
, filename_completer
);
592 /* Add element named NAME to both the set and show command LISTs (the
593 list for set/show or some sublist thereof). */
595 add_setshow_string_cmd (const char *name
, enum command_class theclass
,
597 const char *set_doc
, const char *show_doc
,
598 const char *help_doc
,
599 cmd_sfunc_ftype
*set_func
,
600 show_value_ftype
*show_func
,
601 struct cmd_list_element
**set_list
,
602 struct cmd_list_element
**show_list
)
604 add_setshow_cmd_full (name
, theclass
, var_string
, var
,
605 set_doc
, show_doc
, help_doc
,
611 /* Add element named NAME to both the set and show command LISTs (the
612 list for set/show or some sublist thereof). */
613 struct cmd_list_element
*
614 add_setshow_string_noescape_cmd (const char *name
, enum command_class theclass
,
616 const char *set_doc
, const char *show_doc
,
617 const char *help_doc
,
618 cmd_sfunc_ftype
*set_func
,
619 show_value_ftype
*show_func
,
620 struct cmd_list_element
**set_list
,
621 struct cmd_list_element
**show_list
)
623 struct cmd_list_element
*set_cmd
;
625 add_setshow_cmd_full (name
, theclass
, var_string_noescape
, var
,
626 set_doc
, show_doc
, help_doc
,
633 /* Add element named NAME to both the set and show command LISTs (the
634 list for set/show or some sublist thereof). */
636 add_setshow_optional_filename_cmd (const char *name
, enum command_class theclass
,
638 const char *set_doc
, const char *show_doc
,
639 const char *help_doc
,
640 cmd_sfunc_ftype
*set_func
,
641 show_value_ftype
*show_func
,
642 struct cmd_list_element
**set_list
,
643 struct cmd_list_element
**show_list
)
645 struct cmd_list_element
*set_result
;
647 add_setshow_cmd_full (name
, theclass
, var_optional_filename
, var
,
648 set_doc
, show_doc
, help_doc
,
653 set_cmd_completer (set_result
, filename_completer
);
657 /* Completes on literal "unlimited". Used by integer commands that
658 support a special "unlimited" value. */
660 static VEC (char_ptr
) *
661 integer_unlimited_completer (struct cmd_list_element
*ignore
,
662 const char *text
, const char *word
)
664 static const char * const keywords
[] =
670 return complete_on_enum (keywords
, text
, word
);
673 /* Add element named NAME to both the set and show command LISTs (the
674 list for set/show or some sublist thereof). CLASS is as in
675 add_cmd. VAR is address of the variable which will contain the
676 value. SET_DOC and SHOW_DOC are the documentation strings. This
677 function is only used in Python API. Please don't use it elsewhere. */
679 add_setshow_integer_cmd (const char *name
, enum command_class theclass
,
681 const char *set_doc
, const char *show_doc
,
682 const char *help_doc
,
683 cmd_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
;
690 add_setshow_cmd_full (name
, theclass
, var_integer
, var
,
691 set_doc
, show_doc
, help_doc
,
696 set_cmd_completer (set
, integer_unlimited_completer
);
699 /* Add element named NAME to both the set and show command LISTs (the
700 list for set/show or some sublist thereof). CLASS is as in
701 add_cmd. VAR is address of the variable which will contain the
702 value. SET_DOC and SHOW_DOC are the documentation strings. */
704 add_setshow_uinteger_cmd (const char *name
, enum command_class theclass
,
706 const char *set_doc
, const char *show_doc
,
707 const char *help_doc
,
708 cmd_sfunc_ftype
*set_func
,
709 show_value_ftype
*show_func
,
710 struct cmd_list_element
**set_list
,
711 struct cmd_list_element
**show_list
)
713 struct cmd_list_element
*set
;
715 add_setshow_cmd_full (name
, theclass
, var_uinteger
, var
,
716 set_doc
, show_doc
, help_doc
,
721 set_cmd_completer (set
, integer_unlimited_completer
);
724 /* Add element named NAME to both the set and show command LISTs (the
725 list for set/show or some sublist thereof). CLASS is as in
726 add_cmd. VAR is address of the variable which will contain the
727 value. SET_DOC and SHOW_DOC are the documentation strings. */
729 add_setshow_zinteger_cmd (const char *name
, enum command_class theclass
,
731 const char *set_doc
, const char *show_doc
,
732 const char *help_doc
,
733 cmd_sfunc_ftype
*set_func
,
734 show_value_ftype
*show_func
,
735 struct cmd_list_element
**set_list
,
736 struct cmd_list_element
**show_list
)
738 add_setshow_cmd_full (name
, theclass
, var_zinteger
, var
,
739 set_doc
, show_doc
, help_doc
,
746 add_setshow_zuinteger_unlimited_cmd (const char *name
,
747 enum command_class theclass
,
750 const char *show_doc
,
751 const char *help_doc
,
752 cmd_sfunc_ftype
*set_func
,
753 show_value_ftype
*show_func
,
754 struct cmd_list_element
**set_list
,
755 struct cmd_list_element
**show_list
)
757 struct cmd_list_element
*set
;
759 add_setshow_cmd_full (name
, theclass
, var_zuinteger_unlimited
, var
,
760 set_doc
, show_doc
, help_doc
,
765 set_cmd_completer (set
, integer_unlimited_completer
);
768 /* Add element named NAME to both the set and show command LISTs (the
769 list for set/show or some sublist thereof). CLASS is as in
770 add_cmd. VAR is address of the variable which will contain the
771 value. SET_DOC and SHOW_DOC are the documentation strings. */
773 add_setshow_zuinteger_cmd (const char *name
, enum command_class theclass
,
775 const char *set_doc
, const char *show_doc
,
776 const char *help_doc
,
777 cmd_sfunc_ftype
*set_func
,
778 show_value_ftype
*show_func
,
779 struct cmd_list_element
**set_list
,
780 struct cmd_list_element
**show_list
)
782 add_setshow_cmd_full (name
, theclass
, var_zuinteger
, var
,
783 set_doc
, show_doc
, help_doc
,
789 /* Remove the command named NAME from the command list. Return the
790 list commands which were aliased to the deleted command. If the
791 command had no aliases, return NULL. The various *HOOKs are set to
792 the pre- and post-hook commands for the deleted command. If the
793 command does not have a hook, the corresponding out parameter is
796 static struct cmd_list_element
*
797 delete_cmd (const char *name
, struct cmd_list_element
**list
,
798 struct cmd_list_element
**prehook
,
799 struct cmd_list_element
**prehookee
,
800 struct cmd_list_element
**posthook
,
801 struct cmd_list_element
**posthookee
)
803 struct cmd_list_element
*iter
;
804 struct cmd_list_element
**previous_chain_ptr
;
805 struct cmd_list_element
*aliases
= NULL
;
811 previous_chain_ptr
= list
;
813 for (iter
= *previous_chain_ptr
; iter
; iter
= *previous_chain_ptr
)
815 if (strcmp (iter
->name
, name
) == 0)
818 iter
->destroyer (iter
, iter
->context
);
819 if (iter
->hookee_pre
)
820 iter
->hookee_pre
->hook_pre
= 0;
821 *prehook
= iter
->hook_pre
;
822 *prehookee
= iter
->hookee_pre
;
823 if (iter
->hookee_post
)
824 iter
->hookee_post
->hook_post
= 0;
825 if (iter
->doc
&& iter
->doc_allocated
)
826 xfree ((char *) iter
->doc
);
827 *posthook
= iter
->hook_post
;
828 *posthookee
= iter
->hookee_post
;
830 /* Update the link. */
831 *previous_chain_ptr
= iter
->next
;
833 aliases
= iter
->aliases
;
835 /* If this command was an alias, remove it from the list of
837 if (iter
->cmd_pointer
)
839 struct cmd_list_element
**prevp
= &iter
->cmd_pointer
->aliases
;
840 struct cmd_list_element
*a
= *prevp
;
844 prevp
= &a
->alias_chain
;
847 *prevp
= iter
->alias_chain
;
852 /* We won't see another command with the same name. */
856 previous_chain_ptr
= &iter
->next
;
862 /* Shorthands to the commands above. */
864 /* Add an element to the list of info subcommands. */
866 struct cmd_list_element
*
867 add_info (const char *name
, cmd_cfunc_ftype
*fun
, const char *doc
)
869 return add_cmd (name
, class_info
, fun
, doc
, &infolist
);
872 /* Add an alias to the list of info subcommands. */
874 struct cmd_list_element
*
875 add_info_alias (const char *name
, const char *oldname
, int abbrev_flag
)
877 return add_alias_cmd (name
, oldname
, class_run
, abbrev_flag
, &infolist
);
880 /* Add an element to the list of commands. */
882 struct cmd_list_element
*
883 add_com (const char *name
, enum command_class theclass
, cmd_cfunc_ftype
*fun
,
886 return add_cmd (name
, theclass
, fun
, doc
, &cmdlist
);
889 /* Add an alias or abbreviation command to the list of commands. */
891 struct cmd_list_element
*
892 add_com_alias (const char *name
, const char *oldname
, enum command_class theclass
,
895 return add_alias_cmd (name
, oldname
, theclass
, abbrev_flag
, &cmdlist
);
898 /* Add an element with a suppress notification to the list of commands. */
900 struct cmd_list_element
*
901 add_com_suppress_notification (const char *name
, enum command_class theclass
,
902 cmd_cfunc_ftype
*fun
, const char *doc
,
903 int *suppress_notification
)
905 struct cmd_list_element
*element
;
907 element
= add_cmd (name
, theclass
, fun
, doc
, &cmdlist
);
908 element
->suppress_notification
= suppress_notification
;
913 /* Recursively walk the commandlist structures, and print out the
914 documentation of commands that match our regex in either their
915 name, or their documentation.
918 apropos_cmd (struct ui_file
*stream
,
919 struct cmd_list_element
*commandlist
,
920 compiled_regex
®ex
, const char *prefix
)
922 struct cmd_list_element
*c
;
925 /* Walk through the commands. */
926 for (c
=commandlist
;c
;c
=c
->next
)
928 returnvalue
= -1; /* Needed to avoid double printing. */
931 size_t name_len
= strlen (c
->name
);
933 /* Try to match against the name. */
934 returnvalue
= regex
.search (c
->name
, name_len
, 0, name_len
, NULL
);
935 if (returnvalue
>= 0)
937 print_help_for_command (c
, prefix
,
938 0 /* don't recurse */, stream
);
941 if (c
->doc
!= NULL
&& returnvalue
< 0)
943 size_t doc_len
= strlen (c
->doc
);
945 /* Try to match against documentation. */
946 if (regex
.search (c
->doc
, doc_len
, 0, doc_len
, NULL
) >= 0)
948 print_help_for_command (c
, prefix
,
949 0 /* don't recurse */, stream
);
952 /* Check if this command has subcommands and is not an
953 abbreviation. We skip listing subcommands of abbreviations
954 in order to avoid duplicates in the output. */
955 if (c
->prefixlist
!= NULL
&& !c
->abbrev_flag
)
957 /* Recursively call ourselves on the subcommand list,
958 passing the right prefix in. */
959 apropos_cmd (stream
,*c
->prefixlist
,regex
,c
->prefixname
);
964 /* This command really has to deal with two things:
965 1) I want documentation on *this string* (usually called by
968 2) I want documentation on *this list* (usually called by giving a
969 command that requires subcommands. Also called by saying just
972 I am going to split this into two seperate comamnds, help_cmd and
976 help_cmd (const char *command
, struct ui_file
*stream
)
978 struct cmd_list_element
*c
;
982 help_list (cmdlist
, "", all_classes
, stream
);
986 if (strcmp (command
, "all") == 0)
992 c
= lookup_cmd (&command
, cmdlist
, "", 0, 0);
997 /* There are three cases here.
998 If c->prefixlist is nonzero, we have a prefix command.
999 Print its documentation, then list its subcommands.
1001 If c->func is non NULL, we really have a command. Print its
1002 documentation and return.
1004 If c->func is NULL, we have a class name. Print its
1005 documentation (as if it were a command) and then set class to the
1006 number of this class so that the commands in the class will be
1009 fputs_filtered (c
->doc
, stream
);
1010 fputs_filtered ("\n", stream
);
1012 if (c
->prefixlist
== 0 && c
->func
!= NULL
)
1014 fprintf_filtered (stream
, "\n");
1016 /* If this is a prefix command, print it's subcommands. */
1018 help_list (*c
->prefixlist
, c
->prefixname
, all_commands
, stream
);
1020 /* If this is a class name, print all of the commands in the class. */
1021 if (c
->func
== NULL
)
1022 help_list (cmdlist
, "", c
->theclass
, stream
);
1024 if (c
->hook_pre
|| c
->hook_post
)
1025 fprintf_filtered (stream
,
1026 "\nThis command has a hook (or hooks) defined:\n");
1029 fprintf_filtered (stream
,
1030 "\tThis command is run after : %s (pre hook)\n",
1033 fprintf_filtered (stream
,
1034 "\tThis command is run before : %s (post hook)\n",
1035 c
->hook_post
->name
);
1039 * Get a specific kind of help on a command list.
1042 * CMDTYPE is the prefix to use in the title string.
1043 * CLASS is the class with which to list the nodes of this list (see
1044 * documentation for help_cmd_list below), As usual, ALL_COMMANDS for
1045 * everything, ALL_CLASSES for just classes, and non-negative for only things
1046 * in a specific class.
1047 * and STREAM is the output stream on which to print things.
1048 * If you call this routine with a class >= 0, it recurses.
1051 help_list (struct cmd_list_element
*list
, const char *cmdtype
,
1052 enum command_class theclass
, struct ui_file
*stream
)
1055 char *cmdtype1
, *cmdtype2
;
1057 /* If CMDTYPE is "foo ", CMDTYPE1 gets " foo" and CMDTYPE2 gets "foo sub".
1059 len
= strlen (cmdtype
);
1060 cmdtype1
= (char *) alloca (len
+ 1);
1062 cmdtype2
= (char *) alloca (len
+ 4);
1067 strncpy (cmdtype1
+ 1, cmdtype
, len
- 1);
1069 strncpy (cmdtype2
, cmdtype
, len
- 1);
1070 strcpy (cmdtype2
+ len
- 1, " sub");
1073 if (theclass
== all_classes
)
1074 fprintf_filtered (stream
, "List of classes of %scommands:\n\n", cmdtype2
);
1076 fprintf_filtered (stream
, "List of %scommands:\n\n", cmdtype2
);
1078 help_cmd_list (list
, theclass
, cmdtype
, (int) theclass
>= 0, stream
);
1080 if (theclass
== all_classes
)
1082 fprintf_filtered (stream
, "\n\
1083 Type \"help%s\" followed by a class name for a list of commands in ",
1086 fprintf_filtered (stream
, "that class.");
1088 fprintf_filtered (stream
, "\n\
1089 Type \"help all\" for the list of all commands.");
1092 fprintf_filtered (stream
, "\nType \"help%s\" followed by %scommand name ",
1093 cmdtype1
, cmdtype2
);
1095 fputs_filtered ("for ", stream
);
1097 fputs_filtered ("full ", stream
);
1099 fputs_filtered ("documentation.\n", stream
);
1100 fputs_filtered ("Type \"apropos word\" to search "
1101 "for commands related to \"word\".\n", stream
);
1102 fputs_filtered ("Command name abbreviations are allowed if unambiguous.\n",
1107 help_all (struct ui_file
*stream
)
1109 struct cmd_list_element
*c
;
1110 int seen_unclassified
= 0;
1112 for (c
= cmdlist
; c
; c
= c
->next
)
1116 /* If this is a class name, print all of the commands in the
1119 if (c
->func
== NULL
)
1121 fprintf_filtered (stream
, "\nCommand class: %s\n\n", c
->name
);
1122 help_cmd_list (cmdlist
, c
->theclass
, "", 1, stream
);
1126 /* While it's expected that all commands are in some class,
1127 as a safety measure, we'll print commands outside of any
1128 class at the end. */
1130 for (c
= cmdlist
; c
; c
= c
->next
)
1135 if (c
->theclass
== no_class
)
1137 if (!seen_unclassified
)
1139 fprintf_filtered (stream
, "\nUnclassified commands\n\n");
1140 seen_unclassified
= 1;
1142 print_help_for_command (c
, "", 1, stream
);
1148 /* Print only the first line of STR on STREAM. */
1150 print_doc_line (struct ui_file
*stream
, const char *str
)
1152 static char *line_buffer
= 0;
1153 static int line_size
;
1159 line_buffer
= (char *) xmalloc (line_size
);
1162 /* Keep printing '.' or ',' not followed by a whitespace for embedded strings
1165 while (*p
&& *p
!= '\n'
1166 && ((*p
!= '.' && *p
!= ',') || (p
[1] && !isspace (p
[1]))))
1168 if (p
- str
> line_size
- 1)
1170 line_size
= p
- str
+ 1;
1171 xfree (line_buffer
);
1172 line_buffer
= (char *) xmalloc (line_size
);
1174 strncpy (line_buffer
, str
, p
- str
);
1175 line_buffer
[p
- str
] = '\0';
1176 if (islower (line_buffer
[0]))
1177 line_buffer
[0] = toupper (line_buffer
[0]);
1178 fputs_filtered (line_buffer
, stream
);
1181 /* Print one-line help for command C.
1182 If RECURSE is non-zero, also print one-line descriptions
1183 of all prefixed subcommands. */
1185 print_help_for_command (struct cmd_list_element
*c
, const char *prefix
,
1186 int recurse
, struct ui_file
*stream
)
1188 fprintf_filtered (stream
, "%s%s -- ", prefix
, c
->name
);
1189 print_doc_line (stream
, c
->doc
);
1190 fputs_filtered ("\n", stream
);
1193 && c
->prefixlist
!= 0
1194 && c
->abbrev_flag
== 0)
1195 /* Subcommands of a prefix command typically have 'all_commands'
1196 as class. If we pass CLASS to recursive invocation,
1197 most often we won't see anything. */
1198 help_cmd_list (*c
->prefixlist
, all_commands
, c
->prefixname
, 1, stream
);
1202 * Implement a help command on command list LIST.
1203 * RECURSE should be non-zero if this should be done recursively on
1204 * all sublists of LIST.
1205 * PREFIX is the prefix to print before each command name.
1206 * STREAM is the stream upon which the output should be written.
1207 * THECLASS should be:
1208 * A non-negative class number to list only commands in that
1210 * ALL_COMMANDS to list all commands in list.
1211 * ALL_CLASSES to list all classes in list.
1213 * Note that RECURSE will be active on *all* sublists, not just the
1214 * ones selected by the criteria above (ie. the selection mechanism
1215 * is at the low level, not the high-level).
1218 help_cmd_list (struct cmd_list_element
*list
, enum command_class theclass
,
1219 const char *prefix
, int recurse
, struct ui_file
*stream
)
1221 struct cmd_list_element
*c
;
1223 for (c
= list
; c
; c
= c
->next
)
1225 if (c
->abbrev_flag
== 0
1226 && !c
->cmd_deprecated
1227 && (theclass
== all_commands
1228 || (theclass
== all_classes
&& c
->func
== NULL
)
1229 || (theclass
== c
->theclass
&& c
->func
!= NULL
)))
1231 print_help_for_command (c
, prefix
, recurse
, stream
);
1233 else if (c
->abbrev_flag
== 0
1235 && !c
->cmd_deprecated
1236 && theclass
== class_user
&& c
->prefixlist
!= NULL
)
1237 /* User-defined commands may be subcommands. */
1238 help_cmd_list (*c
->prefixlist
, theclass
, c
->prefixname
,
1244 /* Search the input clist for 'command'. Return the command if
1245 found (or NULL if not), and return the number of commands
1248 static struct cmd_list_element
*
1249 find_cmd (const char *command
, int len
, struct cmd_list_element
*clist
,
1250 int ignore_help_classes
, int *nfound
)
1252 struct cmd_list_element
*found
, *c
;
1256 for (c
= clist
; c
; c
= c
->next
)
1257 if (!strncmp (command
, c
->name
, len
)
1258 && (!ignore_help_classes
|| c
->func
))
1262 if (c
->name
[len
] == '\0')
1271 /* Return the length of command name in TEXT. */
1274 find_command_name_length (const char *text
)
1276 const char *p
= text
;
1278 /* Treating underscores as part of command words is important
1279 so that "set args_foo()" doesn't get interpreted as
1280 "set args _foo()". */
1281 /* Some characters are only used for TUI specific commands.
1282 However, they are always allowed for the sake of consistency.
1284 Note that this is larger than the character set allowed when
1285 creating user-defined commands. */
1287 /* Recognize '!' as a single character command so that, e.g., "!ls"
1288 works as expected. */
1292 while (isalnum (*p
) || *p
== '-' || *p
== '_'
1293 /* Characters used by TUI specific commands. */
1294 || *p
== '+' || *p
== '<' || *p
== '>' || *p
== '$')
1300 /* Return TRUE if NAME is a valid user-defined command name.
1301 This is a stricter subset of all gdb commands,
1302 see find_command_name_length. */
1305 valid_user_defined_cmd_name_p (const char *name
)
1312 /* Alas "42" is a legitimate user-defined command.
1313 In the interests of not breaking anything we preserve that. */
1315 for (p
= name
; *p
!= '\0'; ++p
)
1328 /* This routine takes a line of TEXT and a CLIST in which to start the
1329 lookup. When it returns it will have incremented the text pointer past
1330 the section of text it matched, set *RESULT_LIST to point to the list in
1331 which the last word was matched, and will return a pointer to the cmd
1332 list element which the text matches. It will return NULL if no match at
1333 all was possible. It will return -1 (cast appropriately, ick) if ambigous
1334 matches are possible; in this case *RESULT_LIST will be set to point to
1335 the list in which there are ambiguous choices (and *TEXT will be set to
1336 the ambiguous text string).
1338 If the located command was an abbreviation, this routine returns the base
1339 command of the abbreviation.
1341 It does no error reporting whatsoever; control will always return
1342 to the superior routine.
1344 In the case of an ambiguous return (-1), *RESULT_LIST will be set to point
1345 at the prefix_command (ie. the best match) *or* (special case) will be NULL
1346 if no prefix command was ever found. For example, in the case of "info a",
1347 "info" matches without ambiguity, but "a" could be "args" or "address", so
1348 *RESULT_LIST is set to the cmd_list_element for "info". So in this case
1349 RESULT_LIST should not be interpreted as a pointer to the beginning of a
1350 list; it simply points to a specific command. In the case of an ambiguous
1351 return *TEXT is advanced past the last non-ambiguous prefix (e.g.
1352 "info t" can be "info types" or "info target"; upon return *TEXT has been
1353 advanced past "info ").
1355 If RESULT_LIST is NULL, don't set *RESULT_LIST (but don't otherwise
1356 affect the operation).
1358 This routine does *not* modify the text pointed to by TEXT.
1360 If IGNORE_HELP_CLASSES is nonzero, ignore any command list elements which
1361 are actually help classes rather than commands (i.e. the function field of
1362 the struct cmd_list_element is NULL). */
1364 struct cmd_list_element
*
1365 lookup_cmd_1 (const char **text
, struct cmd_list_element
*clist
,
1366 struct cmd_list_element
**result_list
, int ignore_help_classes
)
1369 int len
, tmp
, nfound
;
1370 struct cmd_list_element
*found
, *c
;
1371 const char *line
= *text
;
1373 while (**text
== ' ' || **text
== '\t')
1376 /* Identify the name of the command. */
1377 len
= find_command_name_length (*text
);
1379 /* If nothing but whitespace, return 0. */
1383 /* *text and p now bracket the first command word to lookup (and
1384 it's length is len). We copy this into a local temporary. */
1387 command
= (char *) alloca (len
+ 1);
1388 memcpy (command
, *text
, len
);
1389 command
[len
] = '\0';
1394 found
= find_cmd (command
, len
, clist
, ignore_help_classes
, &nfound
);
1396 /* If nothing matches, we have a simple failure. */
1402 if (result_list
!= NULL
)
1403 /* Will be modified in calling routine
1404 if we know what the prefix command is. */
1406 return CMD_LIST_AMBIGUOUS
; /* Ambiguous. */
1409 /* We've matched something on this list. Move text pointer forward. */
1413 if (found
->cmd_pointer
)
1415 /* We drop the alias (abbreviation) in favor of the command it
1416 is pointing to. If the alias is deprecated, though, we need to
1417 warn the user about it before we drop it. Note that while we
1418 are warning about the alias, we may also warn about the command
1419 itself and we will adjust the appropriate DEPRECATED_WARN_USER
1422 if (found
->deprecated_warn_user
)
1423 deprecated_cmd_warning (line
);
1424 found
= found
->cmd_pointer
;
1426 /* If we found a prefix command, keep looking. */
1428 if (found
->prefixlist
)
1430 c
= lookup_cmd_1 (text
, *found
->prefixlist
, result_list
,
1431 ignore_help_classes
);
1434 /* Didn't find anything; this is as far as we got. */
1435 if (result_list
!= NULL
)
1436 *result_list
= clist
;
1439 else if (c
== CMD_LIST_AMBIGUOUS
)
1441 /* We've gotten this far properly, but the next step is
1442 ambiguous. We need to set the result list to the best
1443 we've found (if an inferior hasn't already set it). */
1444 if (result_list
!= NULL
)
1446 /* This used to say *result_list = *found->prefixlist.
1447 If that was correct, need to modify the documentation
1448 at the top of this function to clarify what is
1449 supposed to be going on. */
1450 *result_list
= found
;
1461 if (result_list
!= NULL
)
1462 *result_list
= clist
;
1467 /* All this hair to move the space to the front of cmdtype */
1470 undef_cmd_error (const char *cmdtype
, const char *q
)
1472 error (_("Undefined %scommand: \"%s\". Try \"help%s%.*s\"."),
1475 *cmdtype
? " " : "",
1476 (int) strlen (cmdtype
) - 1,
1480 /* Look up the contents of *LINE as a command in the command list LIST.
1481 LIST is a chain of struct cmd_list_element's.
1482 If it is found, return the struct cmd_list_element for that command
1483 and update *LINE to point after the command name, at the first argument.
1484 If not found, call error if ALLOW_UNKNOWN is zero
1485 otherwise (or if error returns) return zero.
1486 Call error if specified command is ambiguous,
1487 unless ALLOW_UNKNOWN is negative.
1488 CMDTYPE precedes the word "command" in the error message.
1490 If INGNORE_HELP_CLASSES is nonzero, ignore any command list
1491 elements which are actually help classes rather than commands (i.e.
1492 the function field of the struct cmd_list_element is 0). */
1494 struct cmd_list_element
*
1495 lookup_cmd (const char **line
, struct cmd_list_element
*list
,
1496 const char *cmdtype
,
1497 int allow_unknown
, int ignore_help_classes
)
1499 struct cmd_list_element
*last_list
= 0;
1500 struct cmd_list_element
*c
;
1502 /* Note: Do not remove trailing whitespace here because this
1503 would be wrong for complete_command. Jim Kingdon */
1506 error (_("Lack of needed %scommand"), cmdtype
);
1508 c
= lookup_cmd_1 (line
, list
, &last_list
, ignore_help_classes
);
1515 int len
= find_command_name_length (*line
);
1517 q
= (char *) alloca (len
+ 1);
1518 strncpy (q
, *line
, len
);
1520 undef_cmd_error (cmdtype
, q
);
1525 else if (c
== CMD_LIST_AMBIGUOUS
)
1527 /* Ambigous. Local values should be off prefixlist or called
1529 int local_allow_unknown
= (last_list
? last_list
->allow_unknown
:
1531 const char *local_cmdtype
= last_list
? last_list
->prefixname
: cmdtype
;
1532 struct cmd_list_element
*local_list
=
1533 (last_list
? *(last_list
->prefixlist
) : list
);
1535 if (local_allow_unknown
< 0)
1538 return last_list
; /* Found something. */
1540 return 0; /* Found nothing. */
1544 /* Report as error. */
1549 ((*line
)[amb_len
] && (*line
)[amb_len
] != ' '
1550 && (*line
)[amb_len
] != '\t');
1555 for (c
= local_list
; c
; c
= c
->next
)
1556 if (!strncmp (*line
, c
->name
, amb_len
))
1558 if (strlen (ambbuf
) + strlen (c
->name
) + 6
1559 < (int) sizeof ambbuf
)
1561 if (strlen (ambbuf
))
1562 strcat (ambbuf
, ", ");
1563 strcat (ambbuf
, c
->name
);
1567 strcat (ambbuf
, "..");
1571 error (_("Ambiguous %scommand \"%s\": %s."), local_cmdtype
,
1573 return 0; /* lint */
1578 if (c
->type
== set_cmd
&& **line
!= '\0' && !isspace (**line
))
1579 error (_("Argument must be preceded by space."));
1581 /* We've got something. It may still not be what the caller
1582 wants (if this command *needs* a subcommand). */
1583 while (**line
== ' ' || **line
== '\t')
1586 if (c
->prefixlist
&& **line
&& !c
->allow_unknown
)
1587 undef_cmd_error (c
->prefixname
, *line
);
1589 /* Seems to be what he wants. Return it. */
1595 /* We are here presumably because an alias or command in TEXT is
1596 deprecated and a warning message should be generated. This
1597 function decodes TEXT and potentially generates a warning message
1600 Example for 'set endian big' which has a fictitious alias 'seb'.
1602 If alias wasn't used in TEXT, and the command is deprecated:
1603 "warning: 'set endian big' is deprecated."
1605 If alias was used, and only the alias is deprecated:
1606 "warning: 'seb' an alias for the command 'set endian big' is deprecated."
1608 If alias was used and command is deprecated (regardless of whether
1609 the alias itself is deprecated:
1611 "warning: 'set endian big' (seb) is deprecated."
1613 After the message has been sent, clear the appropriate flags in the
1614 command and/or the alias so the user is no longer bothered.
1618 deprecated_cmd_warning (const char *text
)
1620 struct cmd_list_element
*alias
= NULL
;
1621 struct cmd_list_element
*prefix_cmd
= NULL
;
1622 struct cmd_list_element
*cmd
= NULL
;
1624 if (!lookup_cmd_composition (text
, &alias
, &prefix_cmd
, &cmd
))
1625 /* Return if text doesn't evaluate to a command. */
1628 if (!((alias
? alias
->deprecated_warn_user
: 0)
1629 || cmd
->deprecated_warn_user
) )
1630 /* Return if nothing is deprecated. */
1633 printf_filtered ("Warning:");
1635 if (alias
&& !cmd
->cmd_deprecated
)
1636 printf_filtered (" '%s', an alias for the", alias
->name
);
1638 printf_filtered (" command '");
1641 printf_filtered ("%s", prefix_cmd
->prefixname
);
1643 printf_filtered ("%s", cmd
->name
);
1645 if (alias
&& cmd
->cmd_deprecated
)
1646 printf_filtered ("' (%s) is deprecated.\n", alias
->name
);
1648 printf_filtered ("' is deprecated.\n");
1651 /* If it is only the alias that is deprecated, we want to indicate
1652 the new alias, otherwise we'll indicate the new command. */
1654 if (alias
&& !cmd
->cmd_deprecated
)
1656 if (alias
->replacement
)
1657 printf_filtered ("Use '%s'.\n\n", alias
->replacement
);
1659 printf_filtered ("No alternative known.\n\n");
1663 if (cmd
->replacement
)
1664 printf_filtered ("Use '%s'.\n\n", cmd
->replacement
);
1666 printf_filtered ("No alternative known.\n\n");
1669 /* We've warned you, now we'll keep quiet. */
1671 alias
->deprecated_warn_user
= 0;
1673 cmd
->deprecated_warn_user
= 0;
1677 /* Look up the contents of LINE as a command in the command list 'cmdlist'.
1678 Return 1 on success, 0 on failure.
1680 If LINE refers to an alias, *alias will point to that alias.
1682 If LINE is a postfix command (i.e. one that is preceded by a prefix
1683 command) set *prefix_cmd.
1685 Set *cmd to point to the command LINE indicates.
1687 If any of *alias, *prefix_cmd, or *cmd cannot be determined or do not
1688 exist, they are NULL when we return.
1692 lookup_cmd_composition (const char *text
,
1693 struct cmd_list_element
**alias
,
1694 struct cmd_list_element
**prefix_cmd
,
1695 struct cmd_list_element
**cmd
)
1698 int len
, tmp
, nfound
;
1699 struct cmd_list_element
*cur_list
;
1700 struct cmd_list_element
*prev_cmd
;
1710 /* Go through as many command lists as we need to,
1711 to find the command TEXT refers to. */
1715 while (*text
== ' ' || *text
== '\t')
1718 /* Identify the name of the command. */
1719 len
= find_command_name_length (text
);
1721 /* If nothing but whitespace, return. */
1725 /* Text is the start of the first command word to lookup (and
1726 it's length is len). We copy this into a local temporary. */
1728 command
= (char *) alloca (len
+ 1);
1729 memcpy (command
, text
, len
);
1730 command
[len
] = '\0';
1735 *cmd
= find_cmd (command
, len
, cur_list
, 1, &nfound
);
1737 if (*cmd
== CMD_LIST_AMBIGUOUS
)
1739 return 0; /* ambiguous */
1743 return 0; /* nothing found */
1746 if ((*cmd
)->cmd_pointer
)
1748 /* cmd was actually an alias, we note that an alias was
1749 used (by assigning *alais) and we set *cmd. */
1751 *cmd
= (*cmd
)->cmd_pointer
;
1753 *prefix_cmd
= prev_cmd
;
1755 if ((*cmd
)->prefixlist
)
1756 cur_list
= *(*cmd
)->prefixlist
;
1764 /* Helper function for SYMBOL_COMPLETION_FUNCTION. */
1766 /* Return a vector of char pointers which point to the different
1767 possible completions in LIST of TEXT.
1769 WORD points in the same buffer as TEXT, and completions should be
1770 returned relative to this position. For example, suppose TEXT is
1771 "foo" and we want to complete to "foobar". If WORD is "oo", return
1772 "oobar"; if WORD is "baz/foo", return "baz/foobar". */
1775 complete_on_cmdlist (struct cmd_list_element
*list
,
1776 const char *text
, const char *word
,
1777 int ignore_help_classes
)
1779 struct cmd_list_element
*ptr
;
1780 VEC (char_ptr
) *matchlist
= NULL
;
1781 int textlen
= strlen (text
);
1783 int saw_deprecated_match
= 0;
1785 /* We do one or two passes. In the first pass, we skip deprecated
1786 commands. If we see no matching commands in the first pass, and
1787 if we did happen to see a matching deprecated command, we do
1788 another loop to collect those. */
1789 for (pass
= 0; matchlist
== 0 && pass
< 2; ++pass
)
1791 for (ptr
= list
; ptr
; ptr
= ptr
->next
)
1792 if (!strncmp (ptr
->name
, text
, textlen
)
1793 && !ptr
->abbrev_flag
1794 && (!ignore_help_classes
|| ptr
->func
1795 || ptr
->prefixlist
))
1801 if (ptr
->cmd_deprecated
)
1803 saw_deprecated_match
= 1;
1808 match
= (char *) xmalloc (strlen (word
) + strlen (ptr
->name
) + 1);
1810 strcpy (match
, ptr
->name
);
1811 else if (word
> text
)
1813 /* Return some portion of ptr->name. */
1814 strcpy (match
, ptr
->name
+ (word
- text
));
1818 /* Return some of text plus ptr->name. */
1819 strncpy (match
, word
, text
- word
);
1820 match
[text
- word
] = '\0';
1821 strcat (match
, ptr
->name
);
1823 VEC_safe_push (char_ptr
, matchlist
, match
);
1825 /* If we saw no matching deprecated commands in the first pass,
1827 if (!saw_deprecated_match
)
1834 /* Helper function for SYMBOL_COMPLETION_FUNCTION. */
1836 /* Return a vector of char pointers which point to the different
1837 possible completions in CMD of TEXT.
1839 WORD points in the same buffer as TEXT, and completions should be
1840 returned relative to this position. For example, suppose TEXT is "foo"
1841 and we want to complete to "foobar". If WORD is "oo", return
1842 "oobar"; if WORD is "baz/foo", return "baz/foobar". */
1845 complete_on_enum (const char *const *enumlist
,
1846 const char *text
, const char *word
)
1848 VEC (char_ptr
) *matchlist
= NULL
;
1849 int textlen
= strlen (text
);
1853 for (i
= 0; (name
= enumlist
[i
]) != NULL
; i
++)
1854 if (strncmp (name
, text
, textlen
) == 0)
1858 match
= (char *) xmalloc (strlen (word
) + strlen (name
) + 1);
1860 strcpy (match
, name
);
1861 else if (word
> text
)
1863 /* Return some portion of name. */
1864 strcpy (match
, name
+ (word
- text
));
1868 /* Return some of text plus name. */
1869 strncpy (match
, word
, text
- word
);
1870 match
[text
- word
] = '\0';
1871 strcat (match
, name
);
1873 VEC_safe_push (char_ptr
, matchlist
, match
);
1880 /* Check function pointer. */
1882 cmd_func_p (struct cmd_list_element
*cmd
)
1884 return (cmd
->func
!= NULL
);
1888 /* Call the command function. */
1890 cmd_func (struct cmd_list_element
*cmd
, char *args
, int from_tty
)
1892 if (cmd_func_p (cmd
))
1894 gdb::optional
<scoped_restore_tmpl
<int>> restore_suppress
;
1896 if (cmd
->suppress_notification
!= NULL
)
1897 restore_suppress
.emplace (cmd
->suppress_notification
, 1);
1899 (*cmd
->func
) (cmd
, args
, from_tty
);
1902 error (_("Invalid command"));
1906 cli_user_command_p (struct cmd_list_element
*cmd
)
1908 return (cmd
->theclass
== class_user
1909 && (cmd
->func
== do_cfunc
|| cmd
->func
== do_sfunc
));