1 /* Handle lists of commands, their decoding and documentation, for GDB.
3 Copyright (C) 1986-2016 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
= XNEW (struct cmd_list_element
);
196 struct cmd_list_element
*p
, *iter
;
198 /* Turn each alias of the old command into an alias of the new
200 c
->aliases
= delete_cmd (name
, list
, &c
->hook_pre
, &c
->hookee_pre
,
201 &c
->hook_post
, &c
->hookee_post
);
202 for (iter
= c
->aliases
; iter
; iter
= iter
->alias_chain
)
203 iter
->cmd_pointer
= c
;
205 c
->hook_pre
->hookee_pre
= c
;
207 c
->hookee_pre
->hook_pre
= c
;
209 c
->hook_post
->hookee_post
= c
;
211 c
->hookee_post
->hook_post
= c
;
213 if (*list
== NULL
|| strcmp ((*list
)->name
, name
) >= 0)
221 while (p
->next
&& strcmp (p
->next
->name
, name
) <= 0)
230 c
->theclass
= theclass
;
231 set_cmd_cfunc (c
, fun
);
232 set_cmd_context (c
, NULL
);
234 c
->cmd_deprecated
= 0;
235 c
->deprecated_warn_user
= 0;
236 c
->malloced_replacement
= 0;
237 c
->doc_allocated
= 0;
238 c
->replacement
= NULL
;
239 c
->pre_show_hook
= NULL
;
241 c
->prefixlist
= NULL
;
242 c
->prefixname
= NULL
;
243 c
->allow_unknown
= 0;
246 set_cmd_completer (c
, make_symbol_completion_list_fn
);
247 c
->completer_handle_brkchars
= NULL
;
249 c
->type
= not_set_cmd
;
251 c
->var_type
= var_boolean
;
253 c
->user_commands
= NULL
;
254 c
->cmd_pointer
= NULL
;
255 c
->alias_chain
= NULL
;
260 /* Deprecates a command CMD.
261 REPLACEMENT is the name of the command which should be used in
262 place of this command, or NULL if no such command exists.
264 This function does not check to see if command REPLACEMENT exists
265 since gdb may not have gotten around to adding REPLACEMENT when
266 this function is called.
268 Returns a pointer to the deprecated command. */
270 struct cmd_list_element
*
271 deprecate_cmd (struct cmd_list_element
*cmd
, const char *replacement
)
273 cmd
->cmd_deprecated
= 1;
274 cmd
->deprecated_warn_user
= 1;
276 if (replacement
!= NULL
)
277 cmd
->replacement
= replacement
;
279 cmd
->replacement
= NULL
;
284 struct cmd_list_element
*
285 add_alias_cmd (const char *name
, const char *oldname
, enum command_class theclass
,
286 int abbrev_flag
, struct cmd_list_element
**list
)
289 struct cmd_list_element
*old
;
290 struct cmd_list_element
*c
;
293 old
= lookup_cmd (&tmp
, *list
, "", 1, 1);
297 struct cmd_list_element
*prehook
, *prehookee
, *posthook
, *posthookee
;
298 struct cmd_list_element
*aliases
= delete_cmd (name
, list
,
299 &prehook
, &prehookee
,
300 &posthook
, &posthookee
);
302 /* If this happens, it means a programmer error somewhere. */
303 gdb_assert (!aliases
&& !prehook
&& !prehookee
304 && !posthook
&& ! posthookee
);
308 c
= add_cmd (name
, theclass
, NULL
, old
->doc
, list
);
310 /* If OLD->DOC can be freed, we should make another copy. */
311 if (old
->doc_allocated
)
313 c
->doc
= xstrdup (old
->doc
);
314 c
->doc_allocated
= 1;
316 /* NOTE: Both FUNC and all the FUNCTIONs need to be copied. */
318 c
->function
= old
->function
;
319 c
->prefixlist
= old
->prefixlist
;
320 c
->prefixname
= old
->prefixname
;
321 c
->allow_unknown
= old
->allow_unknown
;
322 c
->abbrev_flag
= abbrev_flag
;
323 c
->cmd_pointer
= old
;
324 c
->alias_chain
= old
->aliases
;
327 set_cmd_prefix (c
, list
);
331 /* Like add_cmd but adds an element for a command prefix: a name that
332 should be followed by a subcommand to be looked up in another
333 command list. PREFIXLIST should be the address of the variable
334 containing that list. */
336 struct cmd_list_element
*
337 add_prefix_cmd (const char *name
, enum command_class theclass
,
338 cmd_cfunc_ftype
*fun
,
339 const char *doc
, struct cmd_list_element
**prefixlist
,
340 const char *prefixname
, int allow_unknown
,
341 struct cmd_list_element
**list
)
343 struct cmd_list_element
*c
= add_cmd (name
, theclass
, fun
, doc
, list
);
344 struct cmd_list_element
*p
;
346 c
->prefixlist
= prefixlist
;
347 c
->prefixname
= prefixname
;
348 c
->allow_unknown
= allow_unknown
;
350 if (list
== &cmdlist
)
353 set_cmd_prefix (c
, list
);
355 /* Update the field 'prefix' of each cmd_list_element in *PREFIXLIST. */
356 for (p
= *prefixlist
; p
!= NULL
; p
= p
->next
)
362 /* Like add_prefix_cmd but sets the abbrev_flag on the new command. */
364 struct cmd_list_element
*
365 add_abbrev_prefix_cmd (const char *name
, enum command_class theclass
,
366 cmd_cfunc_ftype
*fun
, const char *doc
,
367 struct cmd_list_element
**prefixlist
,
368 const char *prefixname
,
369 int allow_unknown
, struct cmd_list_element
**list
)
371 struct cmd_list_element
*c
= add_cmd (name
, theclass
, fun
, doc
, list
);
373 c
->prefixlist
= prefixlist
;
374 c
->prefixname
= prefixname
;
375 c
->allow_unknown
= allow_unknown
;
380 /* This is an empty "cfunc". */
382 not_just_help_class_command (char *args
, int from_tty
)
386 /* This is an empty "sfunc". */
387 static void empty_sfunc (char *, int, struct cmd_list_element
*);
390 empty_sfunc (char *args
, int from_tty
, struct cmd_list_element
*c
)
394 /* Add element named NAME to command list LIST (the list for set/show
395 or some sublist thereof).
396 TYPE is set_cmd or show_cmd.
397 CLASS is as in add_cmd.
398 VAR_TYPE is the kind of thing we are setting.
399 VAR is address of the variable being controlled by this command.
400 DOC is the documentation string. */
402 static struct cmd_list_element
*
403 add_set_or_show_cmd (const char *name
,
405 enum command_class theclass
,
409 struct cmd_list_element
**list
)
411 struct cmd_list_element
*c
= add_cmd (name
, theclass
, NULL
, doc
, list
);
413 gdb_assert (type
== set_cmd
|| type
== show_cmd
);
415 c
->var_type
= var_type
;
417 /* This needs to be something besides NULL so that this isn't
418 treated as a help class. */
419 set_cmd_sfunc (c
, empty_sfunc
);
423 /* Add element named NAME to both the command SET_LIST and SHOW_LIST.
424 CLASS is as in add_cmd. VAR_TYPE is the kind of thing we are
425 setting. VAR is address of the variable being controlled by this
426 command. SET_FUNC and SHOW_FUNC are the callback functions (if
427 non-NULL). SET_DOC, SHOW_DOC and HELP_DOC are the documentation
428 strings. PRINT the format string to print the value. SET_RESULT
429 and SHOW_RESULT, if not NULL, are set to the resulting command
433 add_setshow_cmd_full (const char *name
,
434 enum command_class theclass
,
435 var_types var_type
, void *var
,
436 const char *set_doc
, const char *show_doc
,
437 const char *help_doc
,
438 cmd_sfunc_ftype
*set_func
,
439 show_value_ftype
*show_func
,
440 struct cmd_list_element
**set_list
,
441 struct cmd_list_element
**show_list
,
442 struct cmd_list_element
**set_result
,
443 struct cmd_list_element
**show_result
)
445 struct cmd_list_element
*set
;
446 struct cmd_list_element
*show
;
450 if (help_doc
!= NULL
)
452 full_set_doc
= xstrprintf ("%s\n%s", set_doc
, help_doc
);
453 full_show_doc
= xstrprintf ("%s\n%s", show_doc
, help_doc
);
457 full_set_doc
= xstrdup (set_doc
);
458 full_show_doc
= xstrdup (show_doc
);
460 set
= add_set_or_show_cmd (name
, set_cmd
, theclass
, var_type
, var
,
461 full_set_doc
, set_list
);
462 set
->doc_allocated
= 1;
464 if (set_func
!= NULL
)
465 set_cmd_sfunc (set
, set_func
);
467 set_cmd_prefix (set
, set_list
);
469 show
= add_set_or_show_cmd (name
, show_cmd
, theclass
, var_type
, var
,
470 full_show_doc
, show_list
);
471 show
->doc_allocated
= 1;
472 show
->show_value_func
= show_func
;
474 if (set_result
!= NULL
)
476 if (show_result
!= NULL
)
480 /* Add element named NAME to command list LIST (the list for set or
481 some sublist thereof). CLASS is as in add_cmd. ENUMLIST is a list
482 of strings which may follow NAME. VAR is address of the variable
483 which will contain the matching string (from ENUMLIST). */
486 add_setshow_enum_cmd (const char *name
,
487 enum command_class theclass
,
488 const char *const *enumlist
,
491 const char *show_doc
,
492 const char *help_doc
,
493 cmd_sfunc_ftype
*set_func
,
494 show_value_ftype
*show_func
,
495 struct cmd_list_element
**set_list
,
496 struct cmd_list_element
**show_list
)
498 struct cmd_list_element
*c
;
500 add_setshow_cmd_full (name
, theclass
, var_enum
, var
,
501 set_doc
, show_doc
, help_doc
,
508 const char * const auto_boolean_enums
[] = { "on", "off", "auto", NULL
};
510 /* Add an auto-boolean command named NAME to both the set and show
511 command list lists. CLASS is as in add_cmd. VAR is address of the
512 variable which will contain the value. DOC is the documentation
513 string. FUNC is the corresponding callback. */
515 add_setshow_auto_boolean_cmd (const char *name
,
516 enum command_class theclass
,
517 enum auto_boolean
*var
,
518 const char *set_doc
, const char *show_doc
,
519 const char *help_doc
,
520 cmd_sfunc_ftype
*set_func
,
521 show_value_ftype
*show_func
,
522 struct cmd_list_element
**set_list
,
523 struct cmd_list_element
**show_list
)
525 struct cmd_list_element
*c
;
527 add_setshow_cmd_full (name
, theclass
, var_auto_boolean
, var
,
528 set_doc
, show_doc
, help_doc
,
532 c
->enums
= auto_boolean_enums
;
535 /* Add element named NAME to both the set and show command LISTs (the
536 list for set/show or some sublist thereof). CLASS is as in
537 add_cmd. VAR is address of the variable which will contain the
538 value. SET_DOC and SHOW_DOC are the documentation strings. */
540 add_setshow_boolean_cmd (const char *name
, enum command_class theclass
, int *var
,
541 const char *set_doc
, const char *show_doc
,
542 const char *help_doc
,
543 cmd_sfunc_ftype
*set_func
,
544 show_value_ftype
*show_func
,
545 struct cmd_list_element
**set_list
,
546 struct cmd_list_element
**show_list
)
548 static const char *boolean_enums
[] = { "on", "off", NULL
};
549 struct cmd_list_element
*c
;
551 add_setshow_cmd_full (name
, theclass
, var_boolean
, var
,
552 set_doc
, show_doc
, help_doc
,
556 c
->enums
= boolean_enums
;
559 /* Add element named NAME to both the set and show command LISTs (the
560 list for set/show or some sublist thereof). */
562 add_setshow_filename_cmd (const char *name
, enum command_class theclass
,
564 const char *set_doc
, const char *show_doc
,
565 const char *help_doc
,
566 cmd_sfunc_ftype
*set_func
,
567 show_value_ftype
*show_func
,
568 struct cmd_list_element
**set_list
,
569 struct cmd_list_element
**show_list
)
571 struct cmd_list_element
*set_result
;
573 add_setshow_cmd_full (name
, theclass
, var_filename
, var
,
574 set_doc
, show_doc
, help_doc
,
578 set_cmd_completer (set_result
, filename_completer
);
581 /* Add element named NAME to both the set and show command LISTs (the
582 list for set/show or some sublist thereof). */
584 add_setshow_string_cmd (const char *name
, enum command_class theclass
,
586 const char *set_doc
, const char *show_doc
,
587 const char *help_doc
,
588 cmd_sfunc_ftype
*set_func
,
589 show_value_ftype
*show_func
,
590 struct cmd_list_element
**set_list
,
591 struct cmd_list_element
**show_list
)
593 add_setshow_cmd_full (name
, theclass
, var_string
, var
,
594 set_doc
, show_doc
, help_doc
,
600 /* Add element named NAME to both the set and show command LISTs (the
601 list for set/show or some sublist thereof). */
602 struct cmd_list_element
*
603 add_setshow_string_noescape_cmd (const char *name
, enum command_class theclass
,
605 const char *set_doc
, const char *show_doc
,
606 const char *help_doc
,
607 cmd_sfunc_ftype
*set_func
,
608 show_value_ftype
*show_func
,
609 struct cmd_list_element
**set_list
,
610 struct cmd_list_element
**show_list
)
612 struct cmd_list_element
*set_cmd
;
614 add_setshow_cmd_full (name
, theclass
, var_string_noescape
, var
,
615 set_doc
, show_doc
, help_doc
,
622 /* Add element named NAME to both the set and show command LISTs (the
623 list for set/show or some sublist thereof). */
625 add_setshow_optional_filename_cmd (const char *name
, enum command_class theclass
,
627 const char *set_doc
, const char *show_doc
,
628 const char *help_doc
,
629 cmd_sfunc_ftype
*set_func
,
630 show_value_ftype
*show_func
,
631 struct cmd_list_element
**set_list
,
632 struct cmd_list_element
**show_list
)
634 struct cmd_list_element
*set_result
;
636 add_setshow_cmd_full (name
, theclass
, var_optional_filename
, var
,
637 set_doc
, show_doc
, help_doc
,
642 set_cmd_completer (set_result
, filename_completer
);
646 /* Completes on literal "unlimited". Used by integer commands that
647 support a special "unlimited" value. */
649 static VEC (char_ptr
) *
650 integer_unlimited_completer (struct cmd_list_element
*ignore
,
651 const char *text
, const char *word
)
653 static const char * const keywords
[] =
659 return complete_on_enum (keywords
, text
, word
);
662 /* Add element named NAME to both the set and show command LISTs (the
663 list for set/show or some sublist thereof). CLASS is as in
664 add_cmd. VAR is address of the variable which will contain the
665 value. SET_DOC and SHOW_DOC are the documentation strings. This
666 function is only used in Python API. Please don't use it elsewhere. */
668 add_setshow_integer_cmd (const char *name
, enum command_class theclass
,
670 const char *set_doc
, const char *show_doc
,
671 const char *help_doc
,
672 cmd_sfunc_ftype
*set_func
,
673 show_value_ftype
*show_func
,
674 struct cmd_list_element
**set_list
,
675 struct cmd_list_element
**show_list
)
677 struct cmd_list_element
*set
;
679 add_setshow_cmd_full (name
, theclass
, var_integer
, var
,
680 set_doc
, show_doc
, help_doc
,
685 set_cmd_completer (set
, integer_unlimited_completer
);
688 /* Add element named NAME to both the set and show command LISTs (the
689 list for set/show or some sublist thereof). CLASS is as in
690 add_cmd. VAR is address of the variable which will contain the
691 value. SET_DOC and SHOW_DOC are the documentation strings. */
693 add_setshow_uinteger_cmd (const char *name
, enum command_class theclass
,
695 const char *set_doc
, const char *show_doc
,
696 const char *help_doc
,
697 cmd_sfunc_ftype
*set_func
,
698 show_value_ftype
*show_func
,
699 struct cmd_list_element
**set_list
,
700 struct cmd_list_element
**show_list
)
702 struct cmd_list_element
*set
;
704 add_setshow_cmd_full (name
, theclass
, var_uinteger
, var
,
705 set_doc
, show_doc
, help_doc
,
710 set_cmd_completer (set
, integer_unlimited_completer
);
713 /* Add element named NAME to both the set and show command LISTs (the
714 list for set/show or some sublist thereof). CLASS is as in
715 add_cmd. VAR is address of the variable which will contain the
716 value. SET_DOC and SHOW_DOC are the documentation strings. */
718 add_setshow_zinteger_cmd (const char *name
, enum command_class theclass
,
720 const char *set_doc
, const char *show_doc
,
721 const char *help_doc
,
722 cmd_sfunc_ftype
*set_func
,
723 show_value_ftype
*show_func
,
724 struct cmd_list_element
**set_list
,
725 struct cmd_list_element
**show_list
)
727 add_setshow_cmd_full (name
, theclass
, var_zinteger
, var
,
728 set_doc
, show_doc
, help_doc
,
735 add_setshow_zuinteger_unlimited_cmd (const char *name
,
736 enum command_class theclass
,
739 const char *show_doc
,
740 const char *help_doc
,
741 cmd_sfunc_ftype
*set_func
,
742 show_value_ftype
*show_func
,
743 struct cmd_list_element
**set_list
,
744 struct cmd_list_element
**show_list
)
746 struct cmd_list_element
*set
;
748 add_setshow_cmd_full (name
, theclass
, var_zuinteger_unlimited
, var
,
749 set_doc
, show_doc
, help_doc
,
754 set_cmd_completer (set
, integer_unlimited_completer
);
757 /* Add element named NAME to both the set and show command LISTs (the
758 list for set/show or some sublist thereof). CLASS is as in
759 add_cmd. VAR is address of the variable which will contain the
760 value. SET_DOC and SHOW_DOC are the documentation strings. */
762 add_setshow_zuinteger_cmd (const char *name
, enum command_class theclass
,
764 const char *set_doc
, const char *show_doc
,
765 const char *help_doc
,
766 cmd_sfunc_ftype
*set_func
,
767 show_value_ftype
*show_func
,
768 struct cmd_list_element
**set_list
,
769 struct cmd_list_element
**show_list
)
771 add_setshow_cmd_full (name
, theclass
, var_zuinteger
, var
,
772 set_doc
, show_doc
, help_doc
,
778 /* Remove the command named NAME from the command list. Return the
779 list commands which were aliased to the deleted command. If the
780 command had no aliases, return NULL. The various *HOOKs are set to
781 the pre- and post-hook commands for the deleted command. If the
782 command does not have a hook, the corresponding out parameter is
785 static struct cmd_list_element
*
786 delete_cmd (const char *name
, struct cmd_list_element
**list
,
787 struct cmd_list_element
**prehook
,
788 struct cmd_list_element
**prehookee
,
789 struct cmd_list_element
**posthook
,
790 struct cmd_list_element
**posthookee
)
792 struct cmd_list_element
*iter
;
793 struct cmd_list_element
**previous_chain_ptr
;
794 struct cmd_list_element
*aliases
= NULL
;
800 previous_chain_ptr
= list
;
802 for (iter
= *previous_chain_ptr
; iter
; iter
= *previous_chain_ptr
)
804 if (strcmp (iter
->name
, name
) == 0)
807 iter
->destroyer (iter
, iter
->context
);
808 if (iter
->hookee_pre
)
809 iter
->hookee_pre
->hook_pre
= 0;
810 *prehook
= iter
->hook_pre
;
811 *prehookee
= iter
->hookee_pre
;
812 if (iter
->hookee_post
)
813 iter
->hookee_post
->hook_post
= 0;
814 if (iter
->doc
&& iter
->doc_allocated
)
815 xfree ((char *) iter
->doc
);
816 *posthook
= iter
->hook_post
;
817 *posthookee
= iter
->hookee_post
;
819 /* Update the link. */
820 *previous_chain_ptr
= iter
->next
;
822 aliases
= iter
->aliases
;
824 /* If this command was an alias, remove it from the list of
826 if (iter
->cmd_pointer
)
828 struct cmd_list_element
**prevp
= &iter
->cmd_pointer
->aliases
;
829 struct cmd_list_element
*a
= *prevp
;
833 prevp
= &a
->alias_chain
;
836 *prevp
= iter
->alias_chain
;
841 /* We won't see another command with the same name. */
845 previous_chain_ptr
= &iter
->next
;
851 /* Shorthands to the commands above. */
853 /* Add an element to the list of info subcommands. */
855 struct cmd_list_element
*
856 add_info (const char *name
, cmd_cfunc_ftype
*fun
, const char *doc
)
858 return add_cmd (name
, class_info
, fun
, doc
, &infolist
);
861 /* Add an alias to the list of info subcommands. */
863 struct cmd_list_element
*
864 add_info_alias (const char *name
, const char *oldname
, int abbrev_flag
)
866 return add_alias_cmd (name
, oldname
, class_run
, abbrev_flag
, &infolist
);
869 /* Add an element to the list of commands. */
871 struct cmd_list_element
*
872 add_com (const char *name
, enum command_class theclass
, cmd_cfunc_ftype
*fun
,
875 return add_cmd (name
, theclass
, fun
, doc
, &cmdlist
);
878 /* Add an alias or abbreviation command to the list of commands. */
880 struct cmd_list_element
*
881 add_com_alias (const char *name
, const char *oldname
, enum command_class theclass
,
884 return add_alias_cmd (name
, oldname
, theclass
, abbrev_flag
, &cmdlist
);
887 /* Recursively walk the commandlist structures, and print out the
888 documentation of commands that match our regex in either their
889 name, or their documentation.
892 apropos_cmd (struct ui_file
*stream
,
893 struct cmd_list_element
*commandlist
,
894 struct re_pattern_buffer
*regex
, const char *prefix
)
896 struct cmd_list_element
*c
;
899 /* Walk through the commands. */
900 for (c
=commandlist
;c
;c
=c
->next
)
902 returnvalue
= -1; /* Needed to avoid double printing. */
905 /* Try to match against the name. */
906 returnvalue
= re_search (regex
, c
->name
, strlen(c
->name
),
907 0, strlen (c
->name
), NULL
);
908 if (returnvalue
>= 0)
910 print_help_for_command (c
, prefix
,
911 0 /* don't recurse */, stream
);
914 if (c
->doc
!= NULL
&& returnvalue
< 0)
916 /* Try to match against documentation. */
917 if (re_search(regex
,c
->doc
,strlen(c
->doc
),0,strlen(c
->doc
),NULL
) >=0)
919 print_help_for_command (c
, prefix
,
920 0 /* don't recurse */, stream
);
923 /* Check if this command has subcommands and is not an
924 abbreviation. We skip listing subcommands of abbreviations
925 in order to avoid duplicates in the output. */
926 if (c
->prefixlist
!= NULL
&& !c
->abbrev_flag
)
928 /* Recursively call ourselves on the subcommand list,
929 passing the right prefix in. */
930 apropos_cmd (stream
,*c
->prefixlist
,regex
,c
->prefixname
);
935 /* This command really has to deal with two things:
936 1) I want documentation on *this string* (usually called by
939 2) I want documentation on *this list* (usually called by giving a
940 command that requires subcommands. Also called by saying just
943 I am going to split this into two seperate comamnds, help_cmd and
947 help_cmd (const char *command
, struct ui_file
*stream
)
949 struct cmd_list_element
*c
;
953 help_list (cmdlist
, "", all_classes
, stream
);
957 if (strcmp (command
, "all") == 0)
963 c
= lookup_cmd (&command
, cmdlist
, "", 0, 0);
968 /* There are three cases here.
969 If c->prefixlist is nonzero, we have a prefix command.
970 Print its documentation, then list its subcommands.
972 If c->func is non NULL, we really have a command. Print its
973 documentation and return.
975 If c->func is NULL, we have a class name. Print its
976 documentation (as if it were a command) and then set class to the
977 number of this class so that the commands in the class will be
980 fputs_filtered (c
->doc
, stream
);
981 fputs_filtered ("\n", stream
);
983 if (c
->prefixlist
== 0 && c
->func
!= NULL
)
985 fprintf_filtered (stream
, "\n");
987 /* If this is a prefix command, print it's subcommands. */
989 help_list (*c
->prefixlist
, c
->prefixname
, all_commands
, stream
);
991 /* If this is a class name, print all of the commands in the class. */
993 help_list (cmdlist
, "", c
->theclass
, stream
);
995 if (c
->hook_pre
|| c
->hook_post
)
996 fprintf_filtered (stream
,
997 "\nThis command has a hook (or hooks) defined:\n");
1000 fprintf_filtered (stream
,
1001 "\tThis command is run after : %s (pre hook)\n",
1004 fprintf_filtered (stream
,
1005 "\tThis command is run before : %s (post hook)\n",
1006 c
->hook_post
->name
);
1010 * Get a specific kind of help on a command list.
1013 * CMDTYPE is the prefix to use in the title string.
1014 * CLASS is the class with which to list the nodes of this list (see
1015 * documentation for help_cmd_list below), As usual, ALL_COMMANDS for
1016 * everything, ALL_CLASSES for just classes, and non-negative for only things
1017 * in a specific class.
1018 * and STREAM is the output stream on which to print things.
1019 * If you call this routine with a class >= 0, it recurses.
1022 help_list (struct cmd_list_element
*list
, const char *cmdtype
,
1023 enum command_class theclass
, struct ui_file
*stream
)
1026 char *cmdtype1
, *cmdtype2
;
1028 /* If CMDTYPE is "foo ", CMDTYPE1 gets " foo" and CMDTYPE2 gets "foo sub".
1030 len
= strlen (cmdtype
);
1031 cmdtype1
= (char *) alloca (len
+ 1);
1033 cmdtype2
= (char *) alloca (len
+ 4);
1038 strncpy (cmdtype1
+ 1, cmdtype
, len
- 1);
1040 strncpy (cmdtype2
, cmdtype
, len
- 1);
1041 strcpy (cmdtype2
+ len
- 1, " sub");
1044 if (theclass
== all_classes
)
1045 fprintf_filtered (stream
, "List of classes of %scommands:\n\n", cmdtype2
);
1047 fprintf_filtered (stream
, "List of %scommands:\n\n", cmdtype2
);
1049 help_cmd_list (list
, theclass
, cmdtype
, (int) theclass
>= 0, stream
);
1051 if (theclass
== all_classes
)
1053 fprintf_filtered (stream
, "\n\
1054 Type \"help%s\" followed by a class name for a list of commands in ",
1057 fprintf_filtered (stream
, "that class.");
1059 fprintf_filtered (stream
, "\n\
1060 Type \"help all\" for the list of all commands.");
1063 fprintf_filtered (stream
, "\nType \"help%s\" followed by %scommand name ",
1064 cmdtype1
, cmdtype2
);
1066 fputs_filtered ("for ", stream
);
1068 fputs_filtered ("full ", stream
);
1070 fputs_filtered ("documentation.\n", stream
);
1071 fputs_filtered ("Type \"apropos word\" to search "
1072 "for commands related to \"word\".\n", stream
);
1073 fputs_filtered ("Command name abbreviations are allowed if unambiguous.\n",
1078 help_all (struct ui_file
*stream
)
1080 struct cmd_list_element
*c
;
1081 int seen_unclassified
= 0;
1083 for (c
= cmdlist
; c
; c
= c
->next
)
1087 /* If this is a class name, print all of the commands in the
1090 if (c
->func
== NULL
)
1092 fprintf_filtered (stream
, "\nCommand class: %s\n\n", c
->name
);
1093 help_cmd_list (cmdlist
, c
->theclass
, "", 1, stream
);
1097 /* While it's expected that all commands are in some class,
1098 as a safety measure, we'll print commands outside of any
1099 class at the end. */
1101 for (c
= cmdlist
; c
; c
= c
->next
)
1106 if (c
->theclass
== no_class
)
1108 if (!seen_unclassified
)
1110 fprintf_filtered (stream
, "\nUnclassified commands\n\n");
1111 seen_unclassified
= 1;
1113 print_help_for_command (c
, "", 1, stream
);
1119 /* Print only the first line of STR on STREAM. */
1121 print_doc_line (struct ui_file
*stream
, const char *str
)
1123 static char *line_buffer
= 0;
1124 static int line_size
;
1130 line_buffer
= (char *) xmalloc (line_size
);
1133 /* Keep printing '.' or ',' not followed by a whitespace for embedded strings
1136 while (*p
&& *p
!= '\n'
1137 && ((*p
!= '.' && *p
!= ',') || (p
[1] && !isspace (p
[1]))))
1139 if (p
- str
> line_size
- 1)
1141 line_size
= p
- str
+ 1;
1142 xfree (line_buffer
);
1143 line_buffer
= (char *) xmalloc (line_size
);
1145 strncpy (line_buffer
, str
, p
- str
);
1146 line_buffer
[p
- str
] = '\0';
1147 if (islower (line_buffer
[0]))
1148 line_buffer
[0] = toupper (line_buffer
[0]);
1149 fputs_filtered (line_buffer
, stream
);
1152 /* Print one-line help for command C.
1153 If RECURSE is non-zero, also print one-line descriptions
1154 of all prefixed subcommands. */
1156 print_help_for_command (struct cmd_list_element
*c
, const char *prefix
,
1157 int recurse
, struct ui_file
*stream
)
1159 fprintf_filtered (stream
, "%s%s -- ", prefix
, c
->name
);
1160 print_doc_line (stream
, c
->doc
);
1161 fputs_filtered ("\n", stream
);
1164 && c
->prefixlist
!= 0
1165 && c
->abbrev_flag
== 0)
1166 /* Subcommands of a prefix command typically have 'all_commands'
1167 as class. If we pass CLASS to recursive invocation,
1168 most often we won't see anything. */
1169 help_cmd_list (*c
->prefixlist
, all_commands
, c
->prefixname
, 1, stream
);
1173 * Implement a help command on command list LIST.
1174 * RECURSE should be non-zero if this should be done recursively on
1175 * all sublists of LIST.
1176 * PREFIX is the prefix to print before each command name.
1177 * STREAM is the stream upon which the output should be written.
1178 * THECLASS should be:
1179 * A non-negative class number to list only commands in that
1181 * ALL_COMMANDS to list all commands in list.
1182 * ALL_CLASSES to list all classes in list.
1184 * Note that RECURSE will be active on *all* sublists, not just the
1185 * ones selected by the criteria above (ie. the selection mechanism
1186 * is at the low level, not the high-level).
1189 help_cmd_list (struct cmd_list_element
*list
, enum command_class theclass
,
1190 const char *prefix
, int recurse
, struct ui_file
*stream
)
1192 struct cmd_list_element
*c
;
1194 for (c
= list
; c
; c
= c
->next
)
1196 if (c
->abbrev_flag
== 0
1197 && !c
->cmd_deprecated
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
1206 && !c
->cmd_deprecated
1207 && theclass
== class_user
&& c
->prefixlist
!= NULL
)
1208 /* User-defined commands may be subcommands. */
1209 help_cmd_list (*c
->prefixlist
, theclass
, c
->prefixname
,
1215 /* Search the input clist for 'command'. Return the command if
1216 found (or NULL if not), and return the number of commands
1219 static struct cmd_list_element
*
1220 find_cmd (const char *command
, int len
, struct cmd_list_element
*clist
,
1221 int ignore_help_classes
, int *nfound
)
1223 struct cmd_list_element
*found
, *c
;
1227 for (c
= clist
; c
; c
= c
->next
)
1228 if (!strncmp (command
, c
->name
, len
)
1229 && (!ignore_help_classes
|| c
->func
))
1233 if (c
->name
[len
] == '\0')
1243 find_command_name_length (const char *text
)
1245 const char *p
= text
;
1247 /* Treating underscores as part of command words is important
1248 so that "set args_foo()" doesn't get interpreted as
1249 "set args _foo()". */
1250 /* Some characters are only used for TUI specific commands.
1251 However, they are always allowed for the sake of consistency.
1253 Note that this is larger than the character set allowed when
1254 creating user-defined commands. */
1256 /* Recognize '!' as a single character command so that, e.g., "!ls"
1257 works as expected. */
1261 while (isalnum (*p
) || *p
== '-' || *p
== '_'
1262 /* Characters used by TUI specific commands. */
1263 || *p
== '+' || *p
== '<' || *p
== '>' || *p
== '$')
1269 /* Return TRUE if NAME is a valid user-defined command name.
1270 This is a stricter subset of all gdb commands,
1271 see find_command_name_length. */
1274 valid_user_defined_cmd_name_p (const char *name
)
1281 /* Alas "42" is a legitimate user-defined command.
1282 In the interests of not breaking anything we preserve that. */
1284 for (p
= name
; *p
!= '\0'; ++p
)
1297 /* This routine takes a line of TEXT and a CLIST in which to start the
1298 lookup. When it returns it will have incremented the text pointer past
1299 the section of text it matched, set *RESULT_LIST to point to the list in
1300 which the last word was matched, and will return a pointer to the cmd
1301 list element which the text matches. It will return NULL if no match at
1302 all was possible. It will return -1 (cast appropriately, ick) if ambigous
1303 matches are possible; in this case *RESULT_LIST will be set to point to
1304 the list in which there are ambiguous choices (and *TEXT will be set to
1305 the ambiguous text string).
1307 If the located command was an abbreviation, this routine returns the base
1308 command of the abbreviation.
1310 It does no error reporting whatsoever; control will always return
1311 to the superior routine.
1313 In the case of an ambiguous return (-1), *RESULT_LIST will be set to point
1314 at the prefix_command (ie. the best match) *or* (special case) will be NULL
1315 if no prefix command was ever found. For example, in the case of "info a",
1316 "info" matches without ambiguity, but "a" could be "args" or "address", so
1317 *RESULT_LIST is set to the cmd_list_element for "info". So in this case
1318 RESULT_LIST should not be interpeted as a pointer to the beginning of a
1319 list; it simply points to a specific command. In the case of an ambiguous
1320 return *TEXT is advanced past the last non-ambiguous prefix (e.g.
1321 "info t" can be "info types" or "info target"; upon return *TEXT has been
1322 advanced past "info ").
1324 If RESULT_LIST is NULL, don't set *RESULT_LIST (but don't otherwise
1325 affect the operation).
1327 This routine does *not* modify the text pointed to by TEXT.
1329 If IGNORE_HELP_CLASSES is nonzero, ignore any command list elements which
1330 are actually help classes rather than commands (i.e. the function field of
1331 the struct cmd_list_element is NULL). */
1333 struct cmd_list_element
*
1334 lookup_cmd_1 (const char **text
, struct cmd_list_element
*clist
,
1335 struct cmd_list_element
**result_list
, int ignore_help_classes
)
1338 int len
, tmp
, nfound
;
1339 struct cmd_list_element
*found
, *c
;
1340 const char *line
= *text
;
1342 while (**text
== ' ' || **text
== '\t')
1345 /* Identify the name of the command. */
1346 len
= find_command_name_length (*text
);
1348 /* If nothing but whitespace, return 0. */
1352 /* *text and p now bracket the first command word to lookup (and
1353 it's length is len). We copy this into a local temporary. */
1356 command
= (char *) alloca (len
+ 1);
1357 memcpy (command
, *text
, len
);
1358 command
[len
] = '\0';
1363 found
= find_cmd (command
, len
, clist
, ignore_help_classes
, &nfound
);
1365 /* We didn't find the command in the entered case, so lower case it
1366 and search again. */
1367 if (!found
|| nfound
== 0)
1369 for (tmp
= 0; tmp
< len
; tmp
++)
1371 char x
= command
[tmp
];
1373 command
[tmp
] = isupper (x
) ? tolower (x
) : x
;
1375 found
= find_cmd (command
, len
, clist
, ignore_help_classes
, &nfound
);
1378 /* If nothing matches, we have a simple failure. */
1384 if (result_list
!= NULL
)
1385 /* Will be modified in calling routine
1386 if we know what the prefix command is. */
1388 return CMD_LIST_AMBIGUOUS
; /* Ambiguous. */
1391 /* We've matched something on this list. Move text pointer forward. */
1395 if (found
->cmd_pointer
)
1397 /* We drop the alias (abbreviation) in favor of the command it
1398 is pointing to. If the alias is deprecated, though, we need to
1399 warn the user about it before we drop it. Note that while we
1400 are warning about the alias, we may also warn about the command
1401 itself and we will adjust the appropriate DEPRECATED_WARN_USER
1404 if (found
->deprecated_warn_user
)
1405 deprecated_cmd_warning (line
);
1406 found
= found
->cmd_pointer
;
1408 /* If we found a prefix command, keep looking. */
1410 if (found
->prefixlist
)
1412 c
= lookup_cmd_1 (text
, *found
->prefixlist
, result_list
,
1413 ignore_help_classes
);
1416 /* Didn't find anything; this is as far as we got. */
1417 if (result_list
!= NULL
)
1418 *result_list
= clist
;
1421 else if (c
== CMD_LIST_AMBIGUOUS
)
1423 /* We've gotten this far properly, but the next step is
1424 ambiguous. We need to set the result list to the best
1425 we've found (if an inferior hasn't already set it). */
1426 if (result_list
!= NULL
)
1428 /* This used to say *result_list = *found->prefixlist.
1429 If that was correct, need to modify the documentation
1430 at the top of this function to clarify what is
1431 supposed to be going on. */
1432 *result_list
= found
;
1443 if (result_list
!= NULL
)
1444 *result_list
= clist
;
1449 /* All this hair to move the space to the front of cmdtype */
1452 undef_cmd_error (const char *cmdtype
, const char *q
)
1454 error (_("Undefined %scommand: \"%s\". Try \"help%s%.*s\"."),
1457 *cmdtype
? " " : "",
1458 (int) strlen (cmdtype
) - 1,
1462 /* Look up the contents of *LINE as a command in the command list LIST.
1463 LIST is a chain of struct cmd_list_element's.
1464 If it is found, return the struct cmd_list_element for that command
1465 and update *LINE to point after the command name, at the first argument.
1466 If not found, call error if ALLOW_UNKNOWN is zero
1467 otherwise (or if error returns) return zero.
1468 Call error if specified command is ambiguous,
1469 unless ALLOW_UNKNOWN is negative.
1470 CMDTYPE precedes the word "command" in the error message.
1472 If INGNORE_HELP_CLASSES is nonzero, ignore any command list
1473 elements which are actually help classes rather than commands (i.e.
1474 the function field of the struct cmd_list_element is 0). */
1476 struct cmd_list_element
*
1477 lookup_cmd (const char **line
, struct cmd_list_element
*list
, char *cmdtype
,
1478 int allow_unknown
, int ignore_help_classes
)
1480 struct cmd_list_element
*last_list
= 0;
1481 struct cmd_list_element
*c
;
1483 /* Note: Do not remove trailing whitespace here because this
1484 would be wrong for complete_command. Jim Kingdon */
1487 error (_("Lack of needed %scommand"), cmdtype
);
1489 c
= lookup_cmd_1 (line
, list
, &last_list
, ignore_help_classes
);
1496 int len
= find_command_name_length (*line
);
1498 q
= (char *) alloca (len
+ 1);
1499 strncpy (q
, *line
, len
);
1501 undef_cmd_error (cmdtype
, q
);
1506 else if (c
== CMD_LIST_AMBIGUOUS
)
1508 /* Ambigous. Local values should be off prefixlist or called
1510 int local_allow_unknown
= (last_list
? last_list
->allow_unknown
:
1512 const char *local_cmdtype
= last_list
? last_list
->prefixname
: cmdtype
;
1513 struct cmd_list_element
*local_list
=
1514 (last_list
? *(last_list
->prefixlist
) : list
);
1516 if (local_allow_unknown
< 0)
1519 return last_list
; /* Found something. */
1521 return 0; /* Found nothing. */
1525 /* Report as error. */
1530 ((*line
)[amb_len
] && (*line
)[amb_len
] != ' '
1531 && (*line
)[amb_len
] != '\t');
1536 for (c
= local_list
; c
; c
= c
->next
)
1537 if (!strncmp (*line
, c
->name
, amb_len
))
1539 if (strlen (ambbuf
) + strlen (c
->name
) + 6
1540 < (int) sizeof ambbuf
)
1542 if (strlen (ambbuf
))
1543 strcat (ambbuf
, ", ");
1544 strcat (ambbuf
, c
->name
);
1548 strcat (ambbuf
, "..");
1552 error (_("Ambiguous %scommand \"%s\": %s."), local_cmdtype
,
1554 return 0; /* lint */
1559 if (c
->type
== set_cmd
&& **line
!= '\0' && !isspace (**line
))
1560 error (_("Argument must be preceded by space."));
1562 /* We've got something. It may still not be what the caller
1563 wants (if this command *needs* a subcommand). */
1564 while (**line
== ' ' || **line
== '\t')
1567 if (c
->prefixlist
&& **line
&& !c
->allow_unknown
)
1568 undef_cmd_error (c
->prefixname
, *line
);
1570 /* Seems to be what he wants. Return it. */
1576 /* We are here presumably because an alias or command in TEXT is
1577 deprecated and a warning message should be generated. This
1578 function decodes TEXT and potentially generates a warning message
1581 Example for 'set endian big' which has a fictitious alias 'seb'.
1583 If alias wasn't used in TEXT, and the command is deprecated:
1584 "warning: 'set endian big' is deprecated."
1586 If alias was used, and only the alias is deprecated:
1587 "warning: 'seb' an alias for the command 'set endian big' is deprecated."
1589 If alias was used and command is deprecated (regardless of whether
1590 the alias itself is deprecated:
1592 "warning: 'set endian big' (seb) is deprecated."
1594 After the message has been sent, clear the appropriate flags in the
1595 command and/or the alias so the user is no longer bothered.
1599 deprecated_cmd_warning (const char *text
)
1601 struct cmd_list_element
*alias
= NULL
;
1602 struct cmd_list_element
*prefix_cmd
= NULL
;
1603 struct cmd_list_element
*cmd
= NULL
;
1605 if (!lookup_cmd_composition (text
, &alias
, &prefix_cmd
, &cmd
))
1606 /* Return if text doesn't evaluate to a command. */
1609 if (!((alias
? alias
->deprecated_warn_user
: 0)
1610 || cmd
->deprecated_warn_user
) )
1611 /* Return if nothing is deprecated. */
1614 printf_filtered ("Warning:");
1616 if (alias
&& !cmd
->cmd_deprecated
)
1617 printf_filtered (" '%s', an alias for the", alias
->name
);
1619 printf_filtered (" command '");
1622 printf_filtered ("%s", prefix_cmd
->prefixname
);
1624 printf_filtered ("%s", cmd
->name
);
1626 if (alias
&& cmd
->cmd_deprecated
)
1627 printf_filtered ("' (%s) is deprecated.\n", alias
->name
);
1629 printf_filtered ("' is deprecated.\n");
1632 /* If it is only the alias that is deprecated, we want to indicate
1633 the new alias, otherwise we'll indicate the new command. */
1635 if (alias
&& !cmd
->cmd_deprecated
)
1637 if (alias
->replacement
)
1638 printf_filtered ("Use '%s'.\n\n", alias
->replacement
);
1640 printf_filtered ("No alternative known.\n\n");
1644 if (cmd
->replacement
)
1645 printf_filtered ("Use '%s'.\n\n", cmd
->replacement
);
1647 printf_filtered ("No alternative known.\n\n");
1650 /* We've warned you, now we'll keep quiet. */
1652 alias
->deprecated_warn_user
= 0;
1654 cmd
->deprecated_warn_user
= 0;
1658 /* Look up the contents of LINE as a command in the command list 'cmdlist'.
1659 Return 1 on success, 0 on failure.
1661 If LINE refers to an alias, *alias will point to that alias.
1663 If LINE is a postfix command (i.e. one that is preceded by a prefix
1664 command) set *prefix_cmd.
1666 Set *cmd to point to the command LINE indicates.
1668 If any of *alias, *prefix_cmd, or *cmd cannot be determined or do not
1669 exist, they are NULL when we return.
1673 lookup_cmd_composition (const char *text
,
1674 struct cmd_list_element
**alias
,
1675 struct cmd_list_element
**prefix_cmd
,
1676 struct cmd_list_element
**cmd
)
1679 int len
, tmp
, nfound
;
1680 struct cmd_list_element
*cur_list
;
1681 struct cmd_list_element
*prev_cmd
;
1691 /* Go through as many command lists as we need to,
1692 to find the command TEXT refers to. */
1696 while (*text
== ' ' || *text
== '\t')
1699 /* Identify the name of the command. */
1700 len
= find_command_name_length (text
);
1702 /* If nothing but whitespace, return. */
1706 /* Text is the start of the first command word to lookup (and
1707 it's length is len). We copy this into a local temporary. */
1709 command
= (char *) alloca (len
+ 1);
1710 memcpy (command
, text
, len
);
1711 command
[len
] = '\0';
1716 *cmd
= find_cmd (command
, len
, cur_list
, 1, &nfound
);
1718 /* We didn't find the command in the entered case, so lower case
1719 it and search again.
1721 if (!*cmd
|| nfound
== 0)
1723 for (tmp
= 0; tmp
< len
; tmp
++)
1725 char x
= command
[tmp
];
1727 command
[tmp
] = isupper (x
) ? tolower (x
) : x
;
1729 *cmd
= find_cmd (command
, len
, cur_list
, 1, &nfound
);
1732 if (*cmd
== CMD_LIST_AMBIGUOUS
)
1734 return 0; /* ambiguous */
1738 return 0; /* nothing found */
1741 if ((*cmd
)->cmd_pointer
)
1743 /* cmd was actually an alias, we note that an alias was
1744 used (by assigning *alais) and we set *cmd. */
1746 *cmd
= (*cmd
)->cmd_pointer
;
1748 *prefix_cmd
= prev_cmd
;
1750 if ((*cmd
)->prefixlist
)
1751 cur_list
= *(*cmd
)->prefixlist
;
1759 /* Helper function for SYMBOL_COMPLETION_FUNCTION. */
1761 /* Return a vector of char pointers which point to the different
1762 possible completions in LIST of TEXT.
1764 WORD points in the same buffer as TEXT, and completions should be
1765 returned relative to this position. For example, suppose TEXT is
1766 "foo" and we want to complete to "foobar". If WORD is "oo", return
1767 "oobar"; if WORD is "baz/foo", return "baz/foobar". */
1770 complete_on_cmdlist (struct cmd_list_element
*list
,
1771 const char *text
, const char *word
,
1772 int ignore_help_classes
)
1774 struct cmd_list_element
*ptr
;
1775 VEC (char_ptr
) *matchlist
= NULL
;
1776 int textlen
= strlen (text
);
1778 int saw_deprecated_match
= 0;
1780 /* We do one or two passes. In the first pass, we skip deprecated
1781 commands. If we see no matching commands in the first pass, and
1782 if we did happen to see a matching deprecated command, we do
1783 another loop to collect those. */
1784 for (pass
= 0; matchlist
== 0 && pass
< 2; ++pass
)
1786 for (ptr
= list
; ptr
; ptr
= ptr
->next
)
1787 if (!strncmp (ptr
->name
, text
, textlen
)
1788 && !ptr
->abbrev_flag
1789 && (!ignore_help_classes
|| ptr
->func
1790 || ptr
->prefixlist
))
1796 if (ptr
->cmd_deprecated
)
1798 saw_deprecated_match
= 1;
1803 match
= (char *) xmalloc (strlen (word
) + strlen (ptr
->name
) + 1);
1805 strcpy (match
, ptr
->name
);
1806 else if (word
> text
)
1808 /* Return some portion of ptr->name. */
1809 strcpy (match
, ptr
->name
+ (word
- text
));
1813 /* Return some of text plus ptr->name. */
1814 strncpy (match
, word
, text
- word
);
1815 match
[text
- word
] = '\0';
1816 strcat (match
, ptr
->name
);
1818 VEC_safe_push (char_ptr
, matchlist
, match
);
1820 /* If we saw no matching deprecated commands in the first pass,
1822 if (!saw_deprecated_match
)
1829 /* Helper function for SYMBOL_COMPLETION_FUNCTION. */
1831 /* Return a vector of char pointers which point to the different
1832 possible completions in CMD of TEXT.
1834 WORD points in the same buffer as TEXT, and completions should be
1835 returned relative to this position. For example, suppose TEXT is "foo"
1836 and we want to complete to "foobar". If WORD is "oo", return
1837 "oobar"; if WORD is "baz/foo", return "baz/foobar". */
1840 complete_on_enum (const char *const *enumlist
,
1841 const char *text
, const char *word
)
1843 VEC (char_ptr
) *matchlist
= NULL
;
1844 int textlen
= strlen (text
);
1848 for (i
= 0; (name
= enumlist
[i
]) != NULL
; i
++)
1849 if (strncmp (name
, text
, textlen
) == 0)
1853 match
= (char *) xmalloc (strlen (word
) + strlen (name
) + 1);
1855 strcpy (match
, name
);
1856 else if (word
> text
)
1858 /* Return some portion of name. */
1859 strcpy (match
, name
+ (word
- text
));
1863 /* Return some of text plus name. */
1864 strncpy (match
, word
, text
- word
);
1865 match
[text
- word
] = '\0';
1866 strcat (match
, name
);
1868 VEC_safe_push (char_ptr
, matchlist
, match
);
1875 /* Check function pointer. */
1877 cmd_func_p (struct cmd_list_element
*cmd
)
1879 return (cmd
->func
!= NULL
);
1883 /* Call the command function. */
1885 cmd_func (struct cmd_list_element
*cmd
, char *args
, int from_tty
)
1887 if (cmd_func_p (cmd
))
1888 (*cmd
->func
) (cmd
, args
, from_tty
);
1890 error (_("Invalid command"));
1894 cli_user_command_p (struct cmd_list_element
*cmd
)
1896 return (cmd
->theclass
== class_user
1897 && (cmd
->func
== do_cfunc
|| cmd
->func
== do_sfunc
));