1 /* Handle lists of commands, their decoding and documentation, for GDB.
3 Copyright (C) 1986-2015 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"
27 /* Prototypes for local functions. */
29 static void undef_cmd_error (const char *, const char *);
31 static struct cmd_list_element
*delete_cmd (const char *name
,
32 struct cmd_list_element
**list
,
33 struct cmd_list_element
**prehook
,
34 struct cmd_list_element
**prehookee
,
35 struct cmd_list_element
**posthook
,
36 struct cmd_list_element
**posthookee
);
38 static struct cmd_list_element
*find_cmd (const char *command
,
40 struct cmd_list_element
*clist
,
41 int ignore_help_classes
,
44 static void help_all (struct ui_file
*stream
);
46 /* Look up a command whose 'prefixlist' is KEY. Return the command if found,
47 otherwise return NULL. */
49 static struct cmd_list_element
*
50 lookup_cmd_for_prefixlist (struct cmd_list_element
**key
,
51 struct cmd_list_element
*list
)
53 struct cmd_list_element
*p
= NULL
;
55 for (p
= list
; p
!= NULL
; p
= p
->next
)
57 struct cmd_list_element
*q
;
59 if (p
->prefixlist
== NULL
)
61 else if (p
->prefixlist
== key
)
64 q
= lookup_cmd_for_prefixlist (key
, *(p
->prefixlist
));
73 set_cmd_prefix (struct cmd_list_element
*c
, struct cmd_list_element
**list
)
75 struct cmd_list_element
*p
;
77 /* Check to see if *LIST contains any element other than C. */
78 for (p
= *list
; p
!= NULL
; p
= p
->next
)
84 /* *SET_LIST only contains SET. */
85 p
= lookup_cmd_for_prefixlist (list
, setlist
);
87 c
->prefix
= p
? (p
->cmd_pointer
? p
->cmd_pointer
: p
) : p
;
90 c
->prefix
= p
->prefix
;
94 print_help_for_command (struct cmd_list_element
*c
, const char *prefix
,
95 int recurse
, struct ui_file
*stream
);
98 /* Set the callback function for the specified command. For each both
99 the commands callback and func() are set. The latter set to a
100 bounce function (unless cfunc / sfunc is NULL that is). */
103 do_cfunc (struct cmd_list_element
*c
, char *args
, int from_tty
)
105 c
->function
.cfunc (args
, from_tty
); /* Ok. */
109 set_cmd_cfunc (struct cmd_list_element
*cmd
, cmd_cfunc_ftype
*cfunc
)
114 cmd
->func
= do_cfunc
;
115 cmd
->function
.cfunc
= cfunc
; /* Ok. */
119 do_sfunc (struct cmd_list_element
*c
, char *args
, int from_tty
)
121 c
->function
.sfunc (args
, from_tty
, c
); /* Ok. */
125 set_cmd_sfunc (struct cmd_list_element
*cmd
, cmd_sfunc_ftype
*sfunc
)
130 cmd
->func
= do_sfunc
;
131 cmd
->function
.sfunc
= sfunc
; /* Ok. */
135 cmd_cfunc_eq (struct cmd_list_element
*cmd
, cmd_cfunc_ftype
*cfunc
)
137 return cmd
->func
== do_cfunc
&& cmd
->function
.cfunc
== cfunc
;
141 set_cmd_context (struct cmd_list_element
*cmd
, void *context
)
143 cmd
->context
= context
;
147 get_cmd_context (struct cmd_list_element
*cmd
)
153 cmd_type (struct cmd_list_element
*cmd
)
159 set_cmd_completer (struct cmd_list_element
*cmd
, completer_ftype
*completer
)
161 cmd
->completer
= completer
; /* Ok. */
164 /* See definition in commands.h. */
167 set_cmd_completer_handle_brkchars (struct cmd_list_element
*cmd
,
168 completer_ftype_void
*completer_handle_brkchars
)
170 cmd
->completer_handle_brkchars
= completer_handle_brkchars
;
173 /* Add element named NAME.
174 Space for NAME and DOC must be allocated by the caller.
175 CLASS is the top level category into which commands are broken down
177 FUN should be the function to execute the command;
178 it will get a character string as argument, with leading
179 and trailing blanks already eliminated.
181 DOC is a documentation string for the command.
182 Its first line should be a complete sentence.
183 It should start with ? for a command that is an abbreviation
184 or with * for a command that most users don't need to know about.
186 Add this command to command list *LIST.
188 Returns a pointer to the added command (not necessarily the head
191 struct cmd_list_element
*
192 add_cmd (const char *name
, enum command_class theclass
, cmd_cfunc_ftype
*fun
,
193 const char *doc
, struct cmd_list_element
**list
)
195 struct cmd_list_element
*c
196 = (struct cmd_list_element
*) xmalloc (sizeof (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
;
261 /* Deprecates a command CMD.
262 REPLACEMENT is the name of the command which should be used in
263 place of this command, or NULL if no such command exists.
265 This function does not check to see if command REPLACEMENT exists
266 since gdb may not have gotten around to adding REPLACEMENT when
267 this function is called.
269 Returns a pointer to the deprecated command. */
271 struct cmd_list_element
*
272 deprecate_cmd (struct cmd_list_element
*cmd
, const char *replacement
)
274 cmd
->cmd_deprecated
= 1;
275 cmd
->deprecated_warn_user
= 1;
277 if (replacement
!= NULL
)
278 cmd
->replacement
= replacement
;
280 cmd
->replacement
= NULL
;
285 struct cmd_list_element
*
286 add_alias_cmd (const char *name
, const char *oldname
, enum command_class theclass
,
287 int abbrev_flag
, struct cmd_list_element
**list
)
290 struct cmd_list_element
*old
;
291 struct cmd_list_element
*c
;
294 old
= lookup_cmd (&tmp
, *list
, "", 1, 1);
298 struct cmd_list_element
*prehook
, *prehookee
, *posthook
, *posthookee
;
299 struct cmd_list_element
*aliases
= delete_cmd (name
, list
,
300 &prehook
, &prehookee
,
301 &posthook
, &posthookee
);
303 /* If this happens, it means a programmer error somewhere. */
304 gdb_assert (!aliases
&& !prehook
&& !prehookee
305 && !posthook
&& ! posthookee
);
309 c
= add_cmd (name
, theclass
, NULL
, old
->doc
, list
);
311 /* If OLD->DOC can be freed, we should make another copy. */
312 if (old
->doc_allocated
)
314 c
->doc
= xstrdup (old
->doc
);
315 c
->doc_allocated
= 1;
317 /* NOTE: Both FUNC and all the FUNCTIONs need to be copied. */
319 c
->function
= old
->function
;
320 c
->prefixlist
= old
->prefixlist
;
321 c
->prefixname
= old
->prefixname
;
322 c
->allow_unknown
= old
->allow_unknown
;
323 c
->abbrev_flag
= abbrev_flag
;
324 c
->cmd_pointer
= old
;
325 c
->alias_chain
= old
->aliases
;
328 set_cmd_prefix (c
, list
);
332 /* Like add_cmd but adds an element for a command prefix: a name that
333 should be followed by a subcommand to be looked up in another
334 command list. PREFIXLIST should be the address of the variable
335 containing that list. */
337 struct cmd_list_element
*
338 add_prefix_cmd (const char *name
, enum command_class theclass
,
339 cmd_cfunc_ftype
*fun
,
340 const char *doc
, struct cmd_list_element
**prefixlist
,
341 const char *prefixname
, int allow_unknown
,
342 struct cmd_list_element
**list
)
344 struct cmd_list_element
*c
= add_cmd (name
, theclass
, fun
, doc
, list
);
345 struct cmd_list_element
*p
;
347 c
->prefixlist
= prefixlist
;
348 c
->prefixname
= prefixname
;
349 c
->allow_unknown
= allow_unknown
;
351 if (list
== &cmdlist
)
354 set_cmd_prefix (c
, list
);
356 /* Update the field 'prefix' of each cmd_list_element in *PREFIXLIST. */
357 for (p
= *prefixlist
; p
!= NULL
; p
= p
->next
)
363 /* Like add_prefix_cmd but sets the abbrev_flag on the new command. */
365 struct cmd_list_element
*
366 add_abbrev_prefix_cmd (const char *name
, enum command_class theclass
,
367 cmd_cfunc_ftype
*fun
, const char *doc
,
368 struct cmd_list_element
**prefixlist
,
369 const char *prefixname
,
370 int allow_unknown
, struct cmd_list_element
**list
)
372 struct cmd_list_element
*c
= add_cmd (name
, theclass
, fun
, doc
, list
);
374 c
->prefixlist
= prefixlist
;
375 c
->prefixname
= prefixname
;
376 c
->allow_unknown
= allow_unknown
;
381 /* This is an empty "cfunc". */
383 not_just_help_class_command (char *args
, int from_tty
)
387 /* This is an empty "sfunc". */
388 static void empty_sfunc (char *, int, struct cmd_list_element
*);
391 empty_sfunc (char *args
, int from_tty
, struct cmd_list_element
*c
)
395 /* Add element named NAME to command list LIST (the list for set/show
396 or some sublist thereof).
397 TYPE is set_cmd or show_cmd.
398 CLASS is as in add_cmd.
399 VAR_TYPE is the kind of thing we are setting.
400 VAR is address of the variable being controlled by this command.
401 DOC is the documentation string. */
403 static struct cmd_list_element
*
404 add_set_or_show_cmd (const char *name
,
406 enum command_class theclass
,
410 struct cmd_list_element
**list
)
412 struct cmd_list_element
*c
= add_cmd (name
, theclass
, NULL
, doc
, list
);
414 gdb_assert (type
== set_cmd
|| type
== show_cmd
);
416 c
->var_type
= var_type
;
418 /* This needs to be something besides NULL so that this isn't
419 treated as a help class. */
420 set_cmd_sfunc (c
, empty_sfunc
);
424 /* Add element named NAME to both the command SET_LIST and SHOW_LIST.
425 CLASS is as in add_cmd. VAR_TYPE is the kind of thing we are
426 setting. VAR is address of the variable being controlled by this
427 command. SET_FUNC and SHOW_FUNC are the callback functions (if
428 non-NULL). SET_DOC, SHOW_DOC and HELP_DOC are the documentation
429 strings. PRINT the format string to print the value. SET_RESULT
430 and SHOW_RESULT, if not NULL, are set to the resulting command
434 add_setshow_cmd_full (const char *name
,
435 enum command_class theclass
,
436 var_types var_type
, void *var
,
437 const char *set_doc
, const char *show_doc
,
438 const char *help_doc
,
439 cmd_sfunc_ftype
*set_func
,
440 show_value_ftype
*show_func
,
441 struct cmd_list_element
**set_list
,
442 struct cmd_list_element
**show_list
,
443 struct cmd_list_element
**set_result
,
444 struct cmd_list_element
**show_result
)
446 struct cmd_list_element
*set
;
447 struct cmd_list_element
*show
;
451 if (help_doc
!= NULL
)
453 full_set_doc
= xstrprintf ("%s\n%s", set_doc
, help_doc
);
454 full_show_doc
= xstrprintf ("%s\n%s", show_doc
, help_doc
);
458 full_set_doc
= xstrdup (set_doc
);
459 full_show_doc
= xstrdup (show_doc
);
461 set
= add_set_or_show_cmd (name
, set_cmd
, theclass
, var_type
, var
,
462 full_set_doc
, set_list
);
463 set
->doc_allocated
= 1;
465 if (set_func
!= NULL
)
466 set_cmd_sfunc (set
, set_func
);
468 set_cmd_prefix (set
, set_list
);
470 show
= add_set_or_show_cmd (name
, show_cmd
, theclass
, var_type
, var
,
471 full_show_doc
, show_list
);
472 show
->doc_allocated
= 1;
473 show
->show_value_func
= show_func
;
475 if (set_result
!= NULL
)
477 if (show_result
!= NULL
)
481 /* Add element named NAME to command list LIST (the list for set or
482 some sublist thereof). CLASS is as in add_cmd. ENUMLIST is a list
483 of strings which may follow NAME. VAR is address of the variable
484 which will contain the matching string (from ENUMLIST). */
487 add_setshow_enum_cmd (const char *name
,
488 enum command_class theclass
,
489 const char *const *enumlist
,
492 const char *show_doc
,
493 const char *help_doc
,
494 cmd_sfunc_ftype
*set_func
,
495 show_value_ftype
*show_func
,
496 struct cmd_list_element
**set_list
,
497 struct cmd_list_element
**show_list
)
499 struct cmd_list_element
*c
;
501 add_setshow_cmd_full (name
, theclass
, var_enum
, var
,
502 set_doc
, show_doc
, help_doc
,
509 const char * const auto_boolean_enums
[] = { "on", "off", "auto", NULL
};
511 /* Add an auto-boolean command named NAME to both the set and show
512 command list lists. CLASS is as in add_cmd. VAR is address of the
513 variable which will contain the value. DOC is the documentation
514 string. FUNC is the corresponding callback. */
516 add_setshow_auto_boolean_cmd (const char *name
,
517 enum command_class theclass
,
518 enum auto_boolean
*var
,
519 const char *set_doc
, const char *show_doc
,
520 const char *help_doc
,
521 cmd_sfunc_ftype
*set_func
,
522 show_value_ftype
*show_func
,
523 struct cmd_list_element
**set_list
,
524 struct cmd_list_element
**show_list
)
526 struct cmd_list_element
*c
;
528 add_setshow_cmd_full (name
, theclass
, var_auto_boolean
, var
,
529 set_doc
, show_doc
, help_doc
,
533 c
->enums
= auto_boolean_enums
;
536 /* Add element named NAME to both the set and show command LISTs (the
537 list for set/show or some sublist thereof). CLASS is as in
538 add_cmd. VAR is address of the variable which will contain the
539 value. SET_DOC and SHOW_DOC are the documentation strings. */
541 add_setshow_boolean_cmd (const char *name
, enum command_class theclass
, int *var
,
542 const char *set_doc
, const char *show_doc
,
543 const char *help_doc
,
544 cmd_sfunc_ftype
*set_func
,
545 show_value_ftype
*show_func
,
546 struct cmd_list_element
**set_list
,
547 struct cmd_list_element
**show_list
)
549 static const char *boolean_enums
[] = { "on", "off", NULL
};
550 struct cmd_list_element
*c
;
552 add_setshow_cmd_full (name
, theclass
, var_boolean
, var
,
553 set_doc
, show_doc
, help_doc
,
557 c
->enums
= boolean_enums
;
560 /* Add element named NAME to both the set and show command LISTs (the
561 list for set/show or some sublist thereof). */
563 add_setshow_filename_cmd (const char *name
, enum command_class theclass
,
565 const char *set_doc
, const char *show_doc
,
566 const char *help_doc
,
567 cmd_sfunc_ftype
*set_func
,
568 show_value_ftype
*show_func
,
569 struct cmd_list_element
**set_list
,
570 struct cmd_list_element
**show_list
)
572 struct cmd_list_element
*set_result
;
574 add_setshow_cmd_full (name
, theclass
, var_filename
, var
,
575 set_doc
, show_doc
, help_doc
,
579 set_cmd_completer (set_result
, filename_completer
);
582 /* Add element named NAME to both the set and show command LISTs (the
583 list for set/show or some sublist thereof). */
585 add_setshow_string_cmd (const char *name
, enum command_class theclass
,
587 const char *set_doc
, const char *show_doc
,
588 const char *help_doc
,
589 cmd_sfunc_ftype
*set_func
,
590 show_value_ftype
*show_func
,
591 struct cmd_list_element
**set_list
,
592 struct cmd_list_element
**show_list
)
594 add_setshow_cmd_full (name
, theclass
, var_string
, var
,
595 set_doc
, show_doc
, help_doc
,
601 /* Add element named NAME to both the set and show command LISTs (the
602 list for set/show or some sublist thereof). */
603 struct cmd_list_element
*
604 add_setshow_string_noescape_cmd (const char *name
, enum command_class theclass
,
606 const char *set_doc
, const char *show_doc
,
607 const char *help_doc
,
608 cmd_sfunc_ftype
*set_func
,
609 show_value_ftype
*show_func
,
610 struct cmd_list_element
**set_list
,
611 struct cmd_list_element
**show_list
)
613 struct cmd_list_element
*set_cmd
;
615 add_setshow_cmd_full (name
, theclass
, var_string_noescape
, var
,
616 set_doc
, show_doc
, help_doc
,
623 /* Add element named NAME to both the set and show command LISTs (the
624 list for set/show or some sublist thereof). */
626 add_setshow_optional_filename_cmd (const char *name
, enum command_class theclass
,
628 const char *set_doc
, const char *show_doc
,
629 const char *help_doc
,
630 cmd_sfunc_ftype
*set_func
,
631 show_value_ftype
*show_func
,
632 struct cmd_list_element
**set_list
,
633 struct cmd_list_element
**show_list
)
635 struct cmd_list_element
*set_result
;
637 add_setshow_cmd_full (name
, theclass
, var_optional_filename
, var
,
638 set_doc
, show_doc
, help_doc
,
643 set_cmd_completer (set_result
, filename_completer
);
647 /* Completes on literal "unlimited". Used by integer commands that
648 support a special "unlimited" value. */
650 static VEC (char_ptr
) *
651 integer_unlimited_completer (struct cmd_list_element
*ignore
,
652 const char *text
, const char *word
)
654 static const char * const keywords
[] =
660 return complete_on_enum (keywords
, text
, word
);
663 /* Add element named NAME to both the set and show command LISTs (the
664 list for set/show or some sublist thereof). CLASS is as in
665 add_cmd. VAR is address of the variable which will contain the
666 value. SET_DOC and SHOW_DOC are the documentation strings. This
667 function is only used in Python API. Please don't use it elsewhere. */
669 add_setshow_integer_cmd (const char *name
, enum command_class theclass
,
671 const char *set_doc
, const char *show_doc
,
672 const char *help_doc
,
673 cmd_sfunc_ftype
*set_func
,
674 show_value_ftype
*show_func
,
675 struct cmd_list_element
**set_list
,
676 struct cmd_list_element
**show_list
)
678 struct cmd_list_element
*set
;
680 add_setshow_cmd_full (name
, theclass
, var_integer
, var
,
681 set_doc
, show_doc
, help_doc
,
686 set_cmd_completer (set
, integer_unlimited_completer
);
689 /* Add element named NAME to both the set and show command LISTs (the
690 list for set/show or some sublist thereof). CLASS is as in
691 add_cmd. VAR is address of the variable which will contain the
692 value. SET_DOC and SHOW_DOC are the documentation strings. */
694 add_setshow_uinteger_cmd (const char *name
, enum command_class theclass
,
696 const char *set_doc
, const char *show_doc
,
697 const char *help_doc
,
698 cmd_sfunc_ftype
*set_func
,
699 show_value_ftype
*show_func
,
700 struct cmd_list_element
**set_list
,
701 struct cmd_list_element
**show_list
)
703 struct cmd_list_element
*set
;
705 add_setshow_cmd_full (name
, theclass
, var_uinteger
, var
,
706 set_doc
, show_doc
, help_doc
,
711 set_cmd_completer (set
, integer_unlimited_completer
);
714 /* Add element named NAME to both the set and show command LISTs (the
715 list for set/show or some sublist thereof). CLASS is as in
716 add_cmd. VAR is address of the variable which will contain the
717 value. SET_DOC and SHOW_DOC are the documentation strings. */
719 add_setshow_zinteger_cmd (const char *name
, enum command_class theclass
,
721 const char *set_doc
, const char *show_doc
,
722 const char *help_doc
,
723 cmd_sfunc_ftype
*set_func
,
724 show_value_ftype
*show_func
,
725 struct cmd_list_element
**set_list
,
726 struct cmd_list_element
**show_list
)
728 add_setshow_cmd_full (name
, theclass
, var_zinteger
, var
,
729 set_doc
, show_doc
, help_doc
,
736 add_setshow_zuinteger_unlimited_cmd (const char *name
,
737 enum command_class theclass
,
740 const char *show_doc
,
741 const char *help_doc
,
742 cmd_sfunc_ftype
*set_func
,
743 show_value_ftype
*show_func
,
744 struct cmd_list_element
**set_list
,
745 struct cmd_list_element
**show_list
)
747 struct cmd_list_element
*set
;
749 add_setshow_cmd_full (name
, theclass
, var_zuinteger_unlimited
, var
,
750 set_doc
, show_doc
, help_doc
,
755 set_cmd_completer (set
, integer_unlimited_completer
);
758 /* Add element named NAME to both the set and show command LISTs (the
759 list for set/show or some sublist thereof). CLASS is as in
760 add_cmd. VAR is address of the variable which will contain the
761 value. SET_DOC and SHOW_DOC are the documentation strings. */
763 add_setshow_zuinteger_cmd (const char *name
, enum command_class theclass
,
765 const char *set_doc
, const char *show_doc
,
766 const char *help_doc
,
767 cmd_sfunc_ftype
*set_func
,
768 show_value_ftype
*show_func
,
769 struct cmd_list_element
**set_list
,
770 struct cmd_list_element
**show_list
)
772 add_setshow_cmd_full (name
, theclass
, var_zuinteger
, var
,
773 set_doc
, show_doc
, help_doc
,
779 /* Remove the command named NAME from the command list. Return the
780 list commands which were aliased to the deleted command. If the
781 command had no aliases, return NULL. The various *HOOKs are set to
782 the pre- and post-hook commands for the deleted command. If the
783 command does not have a hook, the corresponding out parameter is
786 static struct cmd_list_element
*
787 delete_cmd (const char *name
, struct cmd_list_element
**list
,
788 struct cmd_list_element
**prehook
,
789 struct cmd_list_element
**prehookee
,
790 struct cmd_list_element
**posthook
,
791 struct cmd_list_element
**posthookee
)
793 struct cmd_list_element
*iter
;
794 struct cmd_list_element
**previous_chain_ptr
;
795 struct cmd_list_element
*aliases
= NULL
;
801 previous_chain_ptr
= list
;
803 for (iter
= *previous_chain_ptr
; iter
; iter
= *previous_chain_ptr
)
805 if (strcmp (iter
->name
, name
) == 0)
808 iter
->destroyer (iter
, iter
->context
);
809 if (iter
->hookee_pre
)
810 iter
->hookee_pre
->hook_pre
= 0;
811 *prehook
= iter
->hook_pre
;
812 *prehookee
= iter
->hookee_pre
;
813 if (iter
->hookee_post
)
814 iter
->hookee_post
->hook_post
= 0;
815 if (iter
->doc
&& iter
->doc_allocated
)
816 xfree ((char *) iter
->doc
);
817 *posthook
= iter
->hook_post
;
818 *posthookee
= iter
->hookee_post
;
820 /* Update the link. */
821 *previous_chain_ptr
= iter
->next
;
823 aliases
= iter
->aliases
;
825 /* If this command was an alias, remove it from the list of
827 if (iter
->cmd_pointer
)
829 struct cmd_list_element
**prevp
= &iter
->cmd_pointer
->aliases
;
830 struct cmd_list_element
*a
= *prevp
;
834 prevp
= &a
->alias_chain
;
837 *prevp
= iter
->alias_chain
;
842 /* We won't see another command with the same name. */
846 previous_chain_ptr
= &iter
->next
;
852 /* Shorthands to the commands above. */
854 /* Add an element to the list of info subcommands. */
856 struct cmd_list_element
*
857 add_info (const char *name
, cmd_cfunc_ftype
*fun
, const char *doc
)
859 return add_cmd (name
, no_class
, fun
, doc
, &infolist
);
862 /* Add an alias to the list of info subcommands. */
864 struct cmd_list_element
*
865 add_info_alias (const char *name
, const char *oldname
, int abbrev_flag
)
867 return add_alias_cmd (name
, oldname
, 0, abbrev_flag
, &infolist
);
870 /* Add an element to the list of commands. */
872 struct cmd_list_element
*
873 add_com (const char *name
, enum command_class theclass
, cmd_cfunc_ftype
*fun
,
876 return add_cmd (name
, theclass
, fun
, doc
, &cmdlist
);
879 /* Add an alias or abbreviation command to the list of commands. */
881 struct cmd_list_element
*
882 add_com_alias (const char *name
, const char *oldname
, enum command_class theclass
,
885 return add_alias_cmd (name
, oldname
, theclass
, abbrev_flag
, &cmdlist
);
888 /* Recursively walk the commandlist structures, and print out the
889 documentation of commands that match our regex in either their
890 name, or their documentation.
893 apropos_cmd (struct ui_file
*stream
,
894 struct cmd_list_element
*commandlist
,
895 struct re_pattern_buffer
*regex
, const char *prefix
)
897 struct cmd_list_element
*c
;
900 /* Walk through the commands. */
901 for (c
=commandlist
;c
;c
=c
->next
)
903 returnvalue
= -1; /* Needed to avoid double printing. */
906 /* Try to match against the name. */
907 returnvalue
= re_search (regex
, c
->name
, strlen(c
->name
),
908 0, strlen (c
->name
), NULL
);
909 if (returnvalue
>= 0)
911 print_help_for_command (c
, prefix
,
912 0 /* don't recurse */, stream
);
915 if (c
->doc
!= NULL
&& returnvalue
< 0)
917 /* Try to match against documentation. */
918 if (re_search(regex
,c
->doc
,strlen(c
->doc
),0,strlen(c
->doc
),NULL
) >=0)
920 print_help_for_command (c
, prefix
,
921 0 /* don't recurse */, stream
);
924 /* Check if this command has subcommands and is not an
925 abbreviation. We skip listing subcommands of abbreviations
926 in order to avoid duplicates in the output. */
927 if (c
->prefixlist
!= NULL
&& !c
->abbrev_flag
)
929 /* Recursively call ourselves on the subcommand list,
930 passing the right prefix in. */
931 apropos_cmd (stream
,*c
->prefixlist
,regex
,c
->prefixname
);
936 /* This command really has to deal with two things:
937 1) I want documentation on *this string* (usually called by
940 2) I want documentation on *this list* (usually called by giving a
941 command that requires subcommands. Also called by saying just
944 I am going to split this into two seperate comamnds, help_cmd and
948 help_cmd (const char *command
, struct ui_file
*stream
)
950 struct cmd_list_element
*c
;
954 help_list (cmdlist
, "", all_classes
, stream
);
958 if (strcmp (command
, "all") == 0)
964 c
= lookup_cmd (&command
, cmdlist
, "", 0, 0);
969 /* There are three cases here.
970 If c->prefixlist is nonzero, we have a prefix command.
971 Print its documentation, then list its subcommands.
973 If c->func is non NULL, we really have a command. Print its
974 documentation and return.
976 If c->func is NULL, we have a class name. Print its
977 documentation (as if it were a command) and then set class to the
978 number of this class so that the commands in the class will be
981 fputs_filtered (c
->doc
, stream
);
982 fputs_filtered ("\n", stream
);
984 if (c
->prefixlist
== 0 && c
->func
!= NULL
)
986 fprintf_filtered (stream
, "\n");
988 /* If this is a prefix command, print it's subcommands. */
990 help_list (*c
->prefixlist
, c
->prefixname
, all_commands
, stream
);
992 /* If this is a class name, print all of the commands in the class. */
994 help_list (cmdlist
, "", c
->theclass
, stream
);
996 if (c
->hook_pre
|| c
->hook_post
)
997 fprintf_filtered (stream
,
998 "\nThis command has a hook (or hooks) defined:\n");
1001 fprintf_filtered (stream
,
1002 "\tThis command is run after : %s (pre hook)\n",
1005 fprintf_filtered (stream
,
1006 "\tThis command is run before : %s (post hook)\n",
1007 c
->hook_post
->name
);
1011 * Get a specific kind of help on a command list.
1014 * CMDTYPE is the prefix to use in the title string.
1015 * CLASS is the class with which to list the nodes of this list (see
1016 * documentation for help_cmd_list below), As usual, ALL_COMMANDS for
1017 * everything, ALL_CLASSES for just classes, and non-negative for only things
1018 * in a specific class.
1019 * and STREAM is the output stream on which to print things.
1020 * If you call this routine with a class >= 0, it recurses.
1023 help_list (struct cmd_list_element
*list
, const char *cmdtype
,
1024 enum command_class theclass
, struct ui_file
*stream
)
1027 char *cmdtype1
, *cmdtype2
;
1029 /* If CMDTYPE is "foo ", CMDTYPE1 gets " foo" and CMDTYPE2 gets "foo sub".
1031 len
= strlen (cmdtype
);
1032 cmdtype1
= (char *) alloca (len
+ 1);
1034 cmdtype2
= (char *) alloca (len
+ 4);
1039 strncpy (cmdtype1
+ 1, cmdtype
, len
- 1);
1041 strncpy (cmdtype2
, cmdtype
, len
- 1);
1042 strcpy (cmdtype2
+ len
- 1, " sub");
1045 if (theclass
== all_classes
)
1046 fprintf_filtered (stream
, "List of classes of %scommands:\n\n", cmdtype2
);
1048 fprintf_filtered (stream
, "List of %scommands:\n\n", cmdtype2
);
1050 help_cmd_list (list
, theclass
, cmdtype
, (int) theclass
>= 0, stream
);
1052 if (theclass
== all_classes
)
1054 fprintf_filtered (stream
, "\n\
1055 Type \"help%s\" followed by a class name for a list of commands in ",
1058 fprintf_filtered (stream
, "that class.");
1060 fprintf_filtered (stream
, "\n\
1061 Type \"help all\" for the list of all commands.");
1064 fprintf_filtered (stream
, "\nType \"help%s\" followed by %scommand name ",
1065 cmdtype1
, cmdtype2
);
1067 fputs_filtered ("for ", stream
);
1069 fputs_filtered ("full ", stream
);
1071 fputs_filtered ("documentation.\n", stream
);
1072 fputs_filtered ("Type \"apropos word\" to search "
1073 "for commands related to \"word\".\n", stream
);
1074 fputs_filtered ("Command name abbreviations are allowed if unambiguous.\n",
1079 help_all (struct ui_file
*stream
)
1081 struct cmd_list_element
*c
;
1082 int seen_unclassified
= 0;
1084 for (c
= cmdlist
; c
; c
= c
->next
)
1088 /* If this is a class name, print all of the commands in the
1091 if (c
->func
== NULL
)
1093 fprintf_filtered (stream
, "\nCommand class: %s\n\n", c
->name
);
1094 help_cmd_list (cmdlist
, c
->theclass
, "", 1, stream
);
1098 /* While it's expected that all commands are in some class,
1099 as a safety measure, we'll print commands outside of any
1100 class at the end. */
1102 for (c
= cmdlist
; c
; c
= c
->next
)
1107 if (c
->theclass
== no_class
)
1109 if (!seen_unclassified
)
1111 fprintf_filtered (stream
, "\nUnclassified commands\n\n");
1112 seen_unclassified
= 1;
1114 print_help_for_command (c
, "", 1, stream
);
1120 /* Print only the first line of STR on STREAM. */
1122 print_doc_line (struct ui_file
*stream
, const char *str
)
1124 static char *line_buffer
= 0;
1125 static int line_size
;
1131 line_buffer
= (char *) xmalloc (line_size
);
1134 /* Keep printing '.' or ',' not followed by a whitespace for embedded strings
1137 while (*p
&& *p
!= '\n'
1138 && ((*p
!= '.' && *p
!= ',') || (p
[1] && !isspace (p
[1]))))
1140 if (p
- str
> line_size
- 1)
1142 line_size
= p
- str
+ 1;
1143 xfree (line_buffer
);
1144 line_buffer
= (char *) xmalloc (line_size
);
1146 strncpy (line_buffer
, str
, p
- str
);
1147 line_buffer
[p
- str
] = '\0';
1148 if (islower (line_buffer
[0]))
1149 line_buffer
[0] = toupper (line_buffer
[0]);
1150 fputs_filtered (line_buffer
, stream
);
1153 /* Print one-line help for command C.
1154 If RECURSE is non-zero, also print one-line descriptions
1155 of all prefixed subcommands. */
1157 print_help_for_command (struct cmd_list_element
*c
, const char *prefix
,
1158 int recurse
, struct ui_file
*stream
)
1160 fprintf_filtered (stream
, "%s%s -- ", prefix
, c
->name
);
1161 print_doc_line (stream
, c
->doc
);
1162 fputs_filtered ("\n", stream
);
1165 && c
->prefixlist
!= 0
1166 && c
->abbrev_flag
== 0)
1167 /* Subcommands of a prefix command typically have 'all_commands'
1168 as class. If we pass CLASS to recursive invocation,
1169 most often we won't see anything. */
1170 help_cmd_list (*c
->prefixlist
, all_commands
, c
->prefixname
, 1, stream
);
1174 * Implement a help command on command list LIST.
1175 * RECURSE should be non-zero if this should be done recursively on
1176 * all sublists of LIST.
1177 * PREFIX is the prefix to print before each command name.
1178 * STREAM is the stream upon which the output should be written.
1180 * A non-negative class number to list only commands in that
1182 * ALL_COMMANDS to list all commands in list.
1183 * ALL_CLASSES to list all classes in list.
1185 * Note that RECURSE will be active on *all* sublists, not just the
1186 * ones selected by the criteria above (ie. the selection mechanism
1187 * is at the low level, not the high-level).
1190 help_cmd_list (struct cmd_list_element
*list
, enum command_class theclass
,
1191 const char *prefix
, int recurse
, struct ui_file
*stream
)
1193 struct cmd_list_element
*c
;
1195 for (c
= list
; c
; c
= c
->next
)
1197 if (c
->abbrev_flag
== 0
1198 && (theclass
== all_commands
1199 || (theclass
== all_classes
&& c
->func
== NULL
)
1200 || (theclass
== c
->theclass
&& c
->func
!= NULL
)))
1202 print_help_for_command (c
, prefix
, recurse
, stream
);
1204 else if (c
->abbrev_flag
== 0 && recurse
1205 && theclass
== class_user
&& c
->prefixlist
!= NULL
)
1206 /* User-defined commands may be subcommands. */
1207 help_cmd_list (*c
->prefixlist
, theclass
, c
->prefixname
,
1213 /* Search the input clist for 'command'. Return the command if
1214 found (or NULL if not), and return the number of commands
1217 static struct cmd_list_element
*
1218 find_cmd (const char *command
, int len
, struct cmd_list_element
*clist
,
1219 int ignore_help_classes
, int *nfound
)
1221 struct cmd_list_element
*found
, *c
;
1223 found
= (struct cmd_list_element
*) NULL
;
1225 for (c
= clist
; c
; c
= c
->next
)
1226 if (!strncmp (command
, c
->name
, len
)
1227 && (!ignore_help_classes
|| c
->func
))
1231 if (c
->name
[len
] == '\0')
1241 find_command_name_length (const char *text
)
1243 const char *p
= text
;
1245 /* Treating underscores as part of command words is important
1246 so that "set args_foo()" doesn't get interpreted as
1247 "set args _foo()". */
1248 /* Some characters are only used for TUI specific commands.
1249 However, they are always allowed for the sake of consistency.
1251 Note that this is larger than the character set allowed when
1252 creating user-defined commands. */
1254 /* Recognize '!' as a single character command so that, e.g., "!ls"
1255 works as expected. */
1259 while (isalnum (*p
) || *p
== '-' || *p
== '_'
1260 /* Characters used by TUI specific commands. */
1261 || *p
== '+' || *p
== '<' || *p
== '>' || *p
== '$')
1267 /* Return TRUE if NAME is a valid user-defined command name.
1268 This is a stricter subset of all gdb commands,
1269 see find_command_name_length. */
1272 valid_user_defined_cmd_name_p (const char *name
)
1279 /* Alas "42" is a legitimate user-defined command.
1280 In the interests of not breaking anything we preserve that. */
1282 for (p
= name
; *p
!= '\0'; ++p
)
1295 /* This routine takes a line of TEXT and a CLIST in which to start the
1296 lookup. When it returns it will have incremented the text pointer past
1297 the section of text it matched, set *RESULT_LIST to point to the list in
1298 which the last word was matched, and will return a pointer to the cmd
1299 list element which the text matches. It will return NULL if no match at
1300 all was possible. It will return -1 (cast appropriately, ick) if ambigous
1301 matches are possible; in this case *RESULT_LIST will be set to point to
1302 the list in which there are ambiguous choices (and *TEXT will be set to
1303 the ambiguous text string).
1305 If the located command was an abbreviation, this routine returns the base
1306 command of the abbreviation.
1308 It does no error reporting whatsoever; control will always return
1309 to the superior routine.
1311 In the case of an ambiguous return (-1), *RESULT_LIST will be set to point
1312 at the prefix_command (ie. the best match) *or* (special case) will be NULL
1313 if no prefix command was ever found. For example, in the case of "info a",
1314 "info" matches without ambiguity, but "a" could be "args" or "address", so
1315 *RESULT_LIST is set to the cmd_list_element for "info". So in this case
1316 RESULT_LIST should not be interpeted as a pointer to the beginning of a
1317 list; it simply points to a specific command. In the case of an ambiguous
1318 return *TEXT is advanced past the last non-ambiguous prefix (e.g.
1319 "info t" can be "info types" or "info target"; upon return *TEXT has been
1320 advanced past "info ").
1322 If RESULT_LIST is NULL, don't set *RESULT_LIST (but don't otherwise
1323 affect the operation).
1325 This routine does *not* modify the text pointed to by TEXT.
1327 If IGNORE_HELP_CLASSES is nonzero, ignore any command list elements which
1328 are actually help classes rather than commands (i.e. the function field of
1329 the struct cmd_list_element is NULL). */
1331 struct cmd_list_element
*
1332 lookup_cmd_1 (const char **text
, struct cmd_list_element
*clist
,
1333 struct cmd_list_element
**result_list
, int ignore_help_classes
)
1336 int len
, tmp
, nfound
;
1337 struct cmd_list_element
*found
, *c
;
1338 const char *line
= *text
;
1340 while (**text
== ' ' || **text
== '\t')
1343 /* Identify the name of the command. */
1344 len
= find_command_name_length (*text
);
1346 /* If nothing but whitespace, return 0. */
1350 /* *text and p now bracket the first command word to lookup (and
1351 it's length is len). We copy this into a local temporary. */
1354 command
= (char *) alloca (len
+ 1);
1355 memcpy (command
, *text
, len
);
1356 command
[len
] = '\0';
1361 found
= find_cmd (command
, len
, clist
, ignore_help_classes
, &nfound
);
1363 /* We didn't find the command in the entered case, so lower case it
1364 and search again. */
1365 if (!found
|| nfound
== 0)
1367 for (tmp
= 0; tmp
< len
; tmp
++)
1369 char x
= command
[tmp
];
1371 command
[tmp
] = isupper (x
) ? tolower (x
) : x
;
1373 found
= find_cmd (command
, len
, clist
, ignore_help_classes
, &nfound
);
1376 /* If nothing matches, we have a simple failure. */
1382 if (result_list
!= NULL
)
1383 /* Will be modified in calling routine
1384 if we know what the prefix command is. */
1386 return CMD_LIST_AMBIGUOUS
; /* Ambiguous. */
1389 /* We've matched something on this list. Move text pointer forward. */
1393 if (found
->cmd_pointer
)
1395 /* We drop the alias (abbreviation) in favor of the command it
1396 is pointing to. If the alias is deprecated, though, we need to
1397 warn the user about it before we drop it. Note that while we
1398 are warning about the alias, we may also warn about the command
1399 itself and we will adjust the appropriate DEPRECATED_WARN_USER
1402 if (found
->deprecated_warn_user
)
1403 deprecated_cmd_warning (line
);
1404 found
= found
->cmd_pointer
;
1406 /* If we found a prefix command, keep looking. */
1408 if (found
->prefixlist
)
1410 c
= lookup_cmd_1 (text
, *found
->prefixlist
, result_list
,
1411 ignore_help_classes
);
1414 /* Didn't find anything; this is as far as we got. */
1415 if (result_list
!= NULL
)
1416 *result_list
= clist
;
1419 else if (c
== CMD_LIST_AMBIGUOUS
)
1421 /* We've gotten this far properly, but the next step is
1422 ambiguous. We need to set the result list to the best
1423 we've found (if an inferior hasn't already set it). */
1424 if (result_list
!= NULL
)
1426 /* This used to say *result_list = *found->prefixlist.
1427 If that was correct, need to modify the documentation
1428 at the top of this function to clarify what is
1429 supposed to be going on. */
1430 *result_list
= found
;
1441 if (result_list
!= NULL
)
1442 *result_list
= clist
;
1447 /* All this hair to move the space to the front of cmdtype */
1450 undef_cmd_error (const char *cmdtype
, const char *q
)
1452 error (_("Undefined %scommand: \"%s\". Try \"help%s%.*s\"."),
1455 *cmdtype
? " " : "",
1456 (int) strlen (cmdtype
) - 1,
1460 /* Look up the contents of *LINE as a command in the command list LIST.
1461 LIST is a chain of struct cmd_list_element's.
1462 If it is found, return the struct cmd_list_element for that command
1463 and update *LINE to point after the command name, at the first argument.
1464 If not found, call error if ALLOW_UNKNOWN is zero
1465 otherwise (or if error returns) return zero.
1466 Call error if specified command is ambiguous,
1467 unless ALLOW_UNKNOWN is negative.
1468 CMDTYPE precedes the word "command" in the error message.
1470 If INGNORE_HELP_CLASSES is nonzero, ignore any command list
1471 elements which are actually help classes rather than commands (i.e.
1472 the function field of the struct cmd_list_element is 0). */
1474 struct cmd_list_element
*
1475 lookup_cmd (const char **line
, struct cmd_list_element
*list
, char *cmdtype
,
1476 int allow_unknown
, int ignore_help_classes
)
1478 struct cmd_list_element
*last_list
= 0;
1479 struct cmd_list_element
*c
;
1481 /* Note: Do not remove trailing whitespace here because this
1482 would be wrong for complete_command. Jim Kingdon */
1485 error (_("Lack of needed %scommand"), cmdtype
);
1487 c
= lookup_cmd_1 (line
, list
, &last_list
, ignore_help_classes
);
1494 int len
= find_command_name_length (*line
);
1496 q
= (char *) alloca (len
+ 1);
1497 strncpy (q
, *line
, len
);
1499 undef_cmd_error (cmdtype
, q
);
1504 else if (c
== CMD_LIST_AMBIGUOUS
)
1506 /* Ambigous. Local values should be off prefixlist or called
1508 int local_allow_unknown
= (last_list
? last_list
->allow_unknown
:
1510 const char *local_cmdtype
= last_list
? last_list
->prefixname
: cmdtype
;
1511 struct cmd_list_element
*local_list
=
1512 (last_list
? *(last_list
->prefixlist
) : list
);
1514 if (local_allow_unknown
< 0)
1517 return last_list
; /* Found something. */
1519 return 0; /* Found nothing. */
1523 /* Report as error. */
1528 ((*line
)[amb_len
] && (*line
)[amb_len
] != ' '
1529 && (*line
)[amb_len
] != '\t');
1534 for (c
= local_list
; c
; c
= c
->next
)
1535 if (!strncmp (*line
, c
->name
, amb_len
))
1537 if (strlen (ambbuf
) + strlen (c
->name
) + 6
1538 < (int) sizeof ambbuf
)
1540 if (strlen (ambbuf
))
1541 strcat (ambbuf
, ", ");
1542 strcat (ambbuf
, c
->name
);
1546 strcat (ambbuf
, "..");
1550 error (_("Ambiguous %scommand \"%s\": %s."), local_cmdtype
,
1552 return 0; /* lint */
1557 if (c
->type
== set_cmd
&& **line
!= '\0' && !isspace (**line
))
1558 error (_("Argument must be preceded by space."));
1560 /* We've got something. It may still not be what the caller
1561 wants (if this command *needs* a subcommand). */
1562 while (**line
== ' ' || **line
== '\t')
1565 if (c
->prefixlist
&& **line
&& !c
->allow_unknown
)
1566 undef_cmd_error (c
->prefixname
, *line
);
1568 /* Seems to be what he wants. Return it. */
1574 /* We are here presumably because an alias or command in TEXT is
1575 deprecated and a warning message should be generated. This
1576 function decodes TEXT and potentially generates a warning message
1579 Example for 'set endian big' which has a fictitious alias 'seb'.
1581 If alias wasn't used in TEXT, and the command is deprecated:
1582 "warning: 'set endian big' is deprecated."
1584 If alias was used, and only the alias is deprecated:
1585 "warning: 'seb' an alias for the command 'set endian big' is deprecated."
1587 If alias was used and command is deprecated (regardless of whether
1588 the alias itself is deprecated:
1590 "warning: 'set endian big' (seb) is deprecated."
1592 After the message has been sent, clear the appropriate flags in the
1593 command and/or the alias so the user is no longer bothered.
1597 deprecated_cmd_warning (const char *text
)
1599 struct cmd_list_element
*alias
= NULL
;
1600 struct cmd_list_element
*prefix_cmd
= NULL
;
1601 struct cmd_list_element
*cmd
= NULL
;
1603 if (!lookup_cmd_composition (text
, &alias
, &prefix_cmd
, &cmd
))
1604 /* Return if text doesn't evaluate to a command. */
1607 if (!((alias
? alias
->deprecated_warn_user
: 0)
1608 || cmd
->deprecated_warn_user
) )
1609 /* Return if nothing is deprecated. */
1612 printf_filtered ("Warning:");
1614 if (alias
&& !cmd
->cmd_deprecated
)
1615 printf_filtered (" '%s', an alias for the", alias
->name
);
1617 printf_filtered (" command '");
1620 printf_filtered ("%s", prefix_cmd
->prefixname
);
1622 printf_filtered ("%s", cmd
->name
);
1624 if (alias
&& cmd
->cmd_deprecated
)
1625 printf_filtered ("' (%s) is deprecated.\n", alias
->name
);
1627 printf_filtered ("' is deprecated.\n");
1630 /* If it is only the alias that is deprecated, we want to indicate
1631 the new alias, otherwise we'll indicate the new command. */
1633 if (alias
&& !cmd
->cmd_deprecated
)
1635 if (alias
->replacement
)
1636 printf_filtered ("Use '%s'.\n\n", alias
->replacement
);
1638 printf_filtered ("No alternative known.\n\n");
1642 if (cmd
->replacement
)
1643 printf_filtered ("Use '%s'.\n\n", cmd
->replacement
);
1645 printf_filtered ("No alternative known.\n\n");
1648 /* We've warned you, now we'll keep quiet. */
1650 alias
->deprecated_warn_user
= 0;
1652 cmd
->deprecated_warn_user
= 0;
1656 /* Look up the contents of LINE as a command in the command list 'cmdlist'.
1657 Return 1 on success, 0 on failure.
1659 If LINE refers to an alias, *alias will point to that alias.
1661 If LINE is a postfix command (i.e. one that is preceded by a prefix
1662 command) set *prefix_cmd.
1664 Set *cmd to point to the command LINE indicates.
1666 If any of *alias, *prefix_cmd, or *cmd cannot be determined or do not
1667 exist, they are NULL when we return.
1671 lookup_cmd_composition (const char *text
,
1672 struct cmd_list_element
**alias
,
1673 struct cmd_list_element
**prefix_cmd
,
1674 struct cmd_list_element
**cmd
)
1677 int len
, tmp
, nfound
;
1678 struct cmd_list_element
*cur_list
;
1679 struct cmd_list_element
*prev_cmd
;
1689 /* Go through as many command lists as we need to,
1690 to find the command TEXT refers to. */
1694 while (*text
== ' ' || *text
== '\t')
1697 /* Identify the name of the command. */
1698 len
= find_command_name_length (text
);
1700 /* If nothing but whitespace, return. */
1704 /* Text is the start of the first command word to lookup (and
1705 it's length is len). We copy this into a local temporary. */
1707 command
= (char *) alloca (len
+ 1);
1708 memcpy (command
, text
, len
);
1709 command
[len
] = '\0';
1714 *cmd
= find_cmd (command
, len
, cur_list
, 1, &nfound
);
1716 /* We didn't find the command in the entered case, so lower case
1717 it and search again.
1719 if (!*cmd
|| nfound
== 0)
1721 for (tmp
= 0; tmp
< len
; tmp
++)
1723 char x
= command
[tmp
];
1725 command
[tmp
] = isupper (x
) ? tolower (x
) : x
;
1727 *cmd
= find_cmd (command
, len
, cur_list
, 1, &nfound
);
1730 if (*cmd
== CMD_LIST_AMBIGUOUS
)
1732 return 0; /* ambiguous */
1736 return 0; /* nothing found */
1739 if ((*cmd
)->cmd_pointer
)
1741 /* cmd was actually an alias, we note that an alias was
1742 used (by assigning *alais) and we set *cmd. */
1744 *cmd
= (*cmd
)->cmd_pointer
;
1746 *prefix_cmd
= prev_cmd
;
1748 if ((*cmd
)->prefixlist
)
1749 cur_list
= *(*cmd
)->prefixlist
;
1757 /* Helper function for SYMBOL_COMPLETION_FUNCTION. */
1759 /* Return a vector of char pointers which point to the different
1760 possible completions in LIST of TEXT.
1762 WORD points in the same buffer as TEXT, and completions should be
1763 returned relative to this position. For example, suppose TEXT is
1764 "foo" and we want to complete to "foobar". If WORD is "oo", return
1765 "oobar"; if WORD is "baz/foo", return "baz/foobar". */
1768 complete_on_cmdlist (struct cmd_list_element
*list
,
1769 const char *text
, const char *word
,
1770 int ignore_help_classes
)
1772 struct cmd_list_element
*ptr
;
1773 VEC (char_ptr
) *matchlist
= NULL
;
1774 int textlen
= strlen (text
);
1776 int saw_deprecated_match
= 0;
1778 /* We do one or two passes. In the first pass, we skip deprecated
1779 commands. If we see no matching commands in the first pass, and
1780 if we did happen to see a matching deprecated command, we do
1781 another loop to collect those. */
1782 for (pass
= 0; matchlist
== 0 && pass
< 2; ++pass
)
1784 for (ptr
= list
; ptr
; ptr
= ptr
->next
)
1785 if (!strncmp (ptr
->name
, text
, textlen
)
1786 && !ptr
->abbrev_flag
1787 && (!ignore_help_classes
|| ptr
->func
1788 || ptr
->prefixlist
))
1794 if (ptr
->cmd_deprecated
)
1796 saw_deprecated_match
= 1;
1801 match
= (char *) xmalloc (strlen (word
) + strlen (ptr
->name
) + 1);
1803 strcpy (match
, ptr
->name
);
1804 else if (word
> text
)
1806 /* Return some portion of ptr->name. */
1807 strcpy (match
, ptr
->name
+ (word
- text
));
1811 /* Return some of text plus ptr->name. */
1812 strncpy (match
, word
, text
- word
);
1813 match
[text
- word
] = '\0';
1814 strcat (match
, ptr
->name
);
1816 VEC_safe_push (char_ptr
, matchlist
, match
);
1818 /* If we saw no matching deprecated commands in the first pass,
1820 if (!saw_deprecated_match
)
1827 /* Helper function for SYMBOL_COMPLETION_FUNCTION. */
1829 /* Return a vector of char pointers which point to the different
1830 possible completions in CMD of TEXT.
1832 WORD points in the same buffer as TEXT, and completions should be
1833 returned relative to this position. For example, suppose TEXT is "foo"
1834 and we want to complete to "foobar". If WORD is "oo", return
1835 "oobar"; if WORD is "baz/foo", return "baz/foobar". */
1838 complete_on_enum (const char *const *enumlist
,
1839 const char *text
, const char *word
)
1841 VEC (char_ptr
) *matchlist
= NULL
;
1842 int textlen
= strlen (text
);
1846 for (i
= 0; (name
= enumlist
[i
]) != NULL
; i
++)
1847 if (strncmp (name
, text
, textlen
) == 0)
1851 match
= (char *) xmalloc (strlen (word
) + strlen (name
) + 1);
1853 strcpy (match
, name
);
1854 else if (word
> text
)
1856 /* Return some portion of name. */
1857 strcpy (match
, name
+ (word
- text
));
1861 /* Return some of text plus name. */
1862 strncpy (match
, word
, text
- word
);
1863 match
[text
- word
] = '\0';
1864 strcat (match
, name
);
1866 VEC_safe_push (char_ptr
, matchlist
, match
);
1873 /* Check function pointer. */
1875 cmd_func_p (struct cmd_list_element
*cmd
)
1877 return (cmd
->func
!= NULL
);
1881 /* Call the command function. */
1883 cmd_func (struct cmd_list_element
*cmd
, char *args
, int from_tty
)
1885 if (cmd_func_p (cmd
))
1886 (*cmd
->func
) (cmd
, args
, from_tty
);
1888 error (_("Invalid command"));
1892 cli_user_command_p (struct cmd_list_element
*cmd
)
1894 return (cmd
->theclass
== class_user
1895 && (cmd
->func
== do_cfunc
|| cmd
->func
== do_sfunc
));