1 /* Handle lists of commands, their decoding and documentation, for GDB.
3 Copyright (C) 1986-2014 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"
23 #include "completer.h"
25 #include "cli/cli-cmds.h"
26 #include "cli/cli-decode.h"
27 #include "gdb_assert.h"
29 /* Prototypes for local functions. */
31 static void undef_cmd_error (const char *, const char *);
33 static struct cmd_list_element
*delete_cmd (const char *name
,
34 struct cmd_list_element
**list
,
35 struct cmd_list_element
**prehook
,
36 struct cmd_list_element
**prehookee
,
37 struct cmd_list_element
**posthook
,
38 struct cmd_list_element
**posthookee
);
40 static struct cmd_list_element
*find_cmd (const char *command
,
42 struct cmd_list_element
*clist
,
43 int ignore_help_classes
,
46 static void help_all (struct ui_file
*stream
);
48 /* Look up a command whose 'prefixlist' is KEY. Return the command if found,
49 otherwise return NULL. */
51 static struct cmd_list_element
*
52 lookup_cmd_for_prefixlist (struct cmd_list_element
**key
,
53 struct cmd_list_element
*list
)
55 struct cmd_list_element
*p
= NULL
;
57 for (p
= list
; p
!= NULL
; p
= p
->next
)
59 struct cmd_list_element
*q
;
61 if (p
->prefixlist
== NULL
)
63 else if (p
->prefixlist
== key
)
66 q
= lookup_cmd_for_prefixlist (key
, *(p
->prefixlist
));
75 set_cmd_prefix (struct cmd_list_element
*c
, struct cmd_list_element
**list
)
77 struct cmd_list_element
*p
;
79 /* Check to see if *LIST contains any element other than C. */
80 for (p
= *list
; p
!= NULL
; p
= p
->next
)
86 /* *SET_LIST only contains SET. */
87 p
= lookup_cmd_for_prefixlist (list
, setlist
);
89 c
->prefix
= p
? (p
->cmd_pointer
? p
->cmd_pointer
: p
) : p
;
92 c
->prefix
= p
->prefix
;
96 print_help_for_command (struct cmd_list_element
*c
, const char *prefix
,
97 int recurse
, struct ui_file
*stream
);
100 /* Set the callback function for the specified command. For each both
101 the commands callback and func() are set. The latter set to a
102 bounce function (unless cfunc / sfunc is NULL that is). */
105 do_cfunc (struct cmd_list_element
*c
, char *args
, int from_tty
)
107 c
->function
.cfunc (args
, from_tty
); /* Ok. */
111 set_cmd_cfunc (struct cmd_list_element
*cmd
, cmd_cfunc_ftype
*cfunc
)
116 cmd
->func
= do_cfunc
;
117 cmd
->function
.cfunc
= cfunc
; /* Ok. */
121 do_sfunc (struct cmd_list_element
*c
, char *args
, int from_tty
)
123 c
->function
.sfunc (args
, from_tty
, c
); /* Ok. */
127 set_cmd_sfunc (struct cmd_list_element
*cmd
, cmd_sfunc_ftype
*sfunc
)
132 cmd
->func
= do_sfunc
;
133 cmd
->function
.sfunc
= sfunc
; /* Ok. */
137 cmd_cfunc_eq (struct cmd_list_element
*cmd
, cmd_cfunc_ftype
*cfunc
)
139 return cmd
->func
== do_cfunc
&& cmd
->function
.cfunc
== cfunc
;
143 set_cmd_context (struct cmd_list_element
*cmd
, void *context
)
145 cmd
->context
= context
;
149 get_cmd_context (struct cmd_list_element
*cmd
)
155 cmd_type (struct cmd_list_element
*cmd
)
161 set_cmd_completer (struct cmd_list_element
*cmd
, completer_ftype
*completer
)
163 cmd
->completer
= completer
; /* Ok. */
166 /* Add element named NAME.
167 Space for NAME and DOC must be allocated by the caller.
168 CLASS is the top level category into which commands are broken down
170 FUN should be the function to execute the command;
171 it will get a character string as argument, with leading
172 and trailing blanks already eliminated.
174 DOC is a documentation string for the command.
175 Its first line should be a complete sentence.
176 It should start with ? for a command that is an abbreviation
177 or with * for a command that most users don't need to know about.
179 Add this command to command list *LIST.
181 Returns a pointer to the added command (not necessarily the head
184 struct cmd_list_element
*
185 add_cmd (const char *name
, enum command_class
class, cmd_cfunc_ftype
*fun
,
186 const char *doc
, struct cmd_list_element
**list
)
188 struct cmd_list_element
*c
189 = (struct cmd_list_element
*) xmalloc (sizeof (struct cmd_list_element
));
190 struct cmd_list_element
*p
, *iter
;
192 /* Turn each alias of the old command into an alias of the new
194 c
->aliases
= delete_cmd (name
, list
, &c
->hook_pre
, &c
->hookee_pre
,
195 &c
->hook_post
, &c
->hookee_post
);
196 for (iter
= c
->aliases
; iter
; iter
= iter
->alias_chain
)
197 iter
->cmd_pointer
= c
;
199 c
->hook_pre
->hookee_pre
= c
;
201 c
->hookee_pre
->hook_pre
= c
;
203 c
->hook_post
->hookee_post
= c
;
205 c
->hookee_post
->hook_post
= c
;
207 if (*list
== NULL
|| strcmp ((*list
)->name
, name
) >= 0)
215 while (p
->next
&& strcmp (p
->next
->name
, name
) <= 0)
225 set_cmd_cfunc (c
, fun
);
226 set_cmd_context (c
, NULL
);
228 c
->cmd_deprecated
= 0;
229 c
->deprecated_warn_user
= 0;
230 c
->malloced_replacement
= 0;
231 c
->doc_allocated
= 0;
232 c
->replacement
= NULL
;
233 c
->pre_show_hook
= NULL
;
235 c
->prefixlist
= NULL
;
236 c
->prefixname
= NULL
;
237 c
->allow_unknown
= 0;
240 set_cmd_completer (c
, make_symbol_completion_list_fn
);
242 c
->type
= not_set_cmd
;
244 c
->var_type
= var_boolean
;
246 c
->user_commands
= NULL
;
247 c
->cmd_pointer
= NULL
;
248 c
->alias_chain
= NULL
;
253 /* Deprecates a command CMD.
254 REPLACEMENT is the name of the command which should be used in
255 place of this command, or NULL if no such command exists.
257 This function does not check to see if command REPLACEMENT exists
258 since gdb may not have gotten around to adding REPLACEMENT when
259 this function is called.
261 Returns a pointer to the deprecated command. */
263 struct cmd_list_element
*
264 deprecate_cmd (struct cmd_list_element
*cmd
, const char *replacement
)
266 cmd
->cmd_deprecated
= 1;
267 cmd
->deprecated_warn_user
= 1;
269 if (replacement
!= NULL
)
270 cmd
->replacement
= replacement
;
272 cmd
->replacement
= NULL
;
277 struct cmd_list_element
*
278 add_alias_cmd (const char *name
, const char *oldname
, enum command_class
class,
279 int abbrev_flag
, struct cmd_list_element
**list
)
282 struct cmd_list_element
*old
;
283 struct cmd_list_element
*c
;
286 old
= lookup_cmd (&tmp
, *list
, "", 1, 1);
290 struct cmd_list_element
*prehook
, *prehookee
, *posthook
, *posthookee
;
291 struct cmd_list_element
*aliases
= delete_cmd (name
, list
,
292 &prehook
, &prehookee
,
293 &posthook
, &posthookee
);
295 /* If this happens, it means a programmer error somewhere. */
296 gdb_assert (!aliases
&& !prehook
&& !prehookee
297 && !posthook
&& ! posthookee
);
301 c
= add_cmd (name
, class, NULL
, old
->doc
, list
);
303 /* If OLD->DOC can be freed, we should make another copy. */
304 if (old
->doc_allocated
)
306 c
->doc
= xstrdup (old
->doc
);
307 c
->doc_allocated
= 1;
309 /* NOTE: Both FUNC and all the FUNCTIONs need to be copied. */
311 c
->function
= old
->function
;
312 c
->prefixlist
= old
->prefixlist
;
313 c
->prefixname
= old
->prefixname
;
314 c
->allow_unknown
= old
->allow_unknown
;
315 c
->abbrev_flag
= abbrev_flag
;
316 c
->cmd_pointer
= old
;
317 c
->alias_chain
= old
->aliases
;
320 set_cmd_prefix (c
, list
);
324 /* Like add_cmd but adds an element for a command prefix: a name that
325 should be followed by a subcommand to be looked up in another
326 command list. PREFIXLIST should be the address of the variable
327 containing that list. */
329 struct cmd_list_element
*
330 add_prefix_cmd (const char *name
, enum command_class
class,
331 cmd_cfunc_ftype
*fun
,
332 const char *doc
, struct cmd_list_element
**prefixlist
,
333 const char *prefixname
, int allow_unknown
,
334 struct cmd_list_element
**list
)
336 struct cmd_list_element
*c
= add_cmd (name
, class, fun
, doc
, list
);
337 struct cmd_list_element
*p
;
339 c
->prefixlist
= prefixlist
;
340 c
->prefixname
= prefixname
;
341 c
->allow_unknown
= allow_unknown
;
343 if (list
== &cmdlist
)
346 set_cmd_prefix (c
, list
);
348 /* Update the field 'prefix' of each cmd_list_element in *PREFIXLIST. */
349 for (p
= *prefixlist
; p
!= NULL
; p
= p
->next
)
355 /* Like add_prefix_cmd but sets the abbrev_flag on the new command. */
357 struct cmd_list_element
*
358 add_abbrev_prefix_cmd (const char *name
, enum command_class
class,
359 cmd_cfunc_ftype
*fun
, const char *doc
,
360 struct cmd_list_element
**prefixlist
,
361 const char *prefixname
,
362 int allow_unknown
, struct cmd_list_element
**list
)
364 struct cmd_list_element
*c
= add_cmd (name
, class, fun
, doc
, list
);
366 c
->prefixlist
= prefixlist
;
367 c
->prefixname
= prefixname
;
368 c
->allow_unknown
= allow_unknown
;
373 /* This is an empty "cfunc". */
375 not_just_help_class_command (char *args
, int from_tty
)
379 /* This is an empty "sfunc". */
380 static void empty_sfunc (char *, int, struct cmd_list_element
*);
383 empty_sfunc (char *args
, int from_tty
, struct cmd_list_element
*c
)
387 /* Add element named NAME to command list LIST (the list for set/show
388 or some sublist thereof).
389 TYPE is set_cmd or show_cmd.
390 CLASS is as in add_cmd.
391 VAR_TYPE is the kind of thing we are setting.
392 VAR is address of the variable being controlled by this command.
393 DOC is the documentation string. */
395 static struct cmd_list_element
*
396 add_set_or_show_cmd (const char *name
,
398 enum command_class
class,
402 struct cmd_list_element
**list
)
404 struct cmd_list_element
*c
= add_cmd (name
, class, NULL
, doc
, list
);
406 gdb_assert (type
== set_cmd
|| type
== show_cmd
);
408 c
->var_type
= var_type
;
410 /* This needs to be something besides NULL so that this isn't
411 treated as a help class. */
412 set_cmd_sfunc (c
, empty_sfunc
);
416 /* Add element named NAME to both the command SET_LIST and SHOW_LIST.
417 CLASS is as in add_cmd. VAR_TYPE is the kind of thing we are
418 setting. VAR is address of the variable being controlled by this
419 command. SET_FUNC and SHOW_FUNC are the callback functions (if
420 non-NULL). SET_DOC, SHOW_DOC and HELP_DOC are the documentation
421 strings. PRINT the format string to print the value. SET_RESULT
422 and SHOW_RESULT, if not NULL, are set to the resulting command
426 add_setshow_cmd_full (const char *name
,
427 enum command_class
class,
428 var_types var_type
, void *var
,
429 const char *set_doc
, const char *show_doc
,
430 const char *help_doc
,
431 cmd_sfunc_ftype
*set_func
,
432 show_value_ftype
*show_func
,
433 struct cmd_list_element
**set_list
,
434 struct cmd_list_element
**show_list
,
435 struct cmd_list_element
**set_result
,
436 struct cmd_list_element
**show_result
)
438 struct cmd_list_element
*set
;
439 struct cmd_list_element
*show
;
443 if (help_doc
!= NULL
)
445 full_set_doc
= xstrprintf ("%s\n%s", set_doc
, help_doc
);
446 full_show_doc
= xstrprintf ("%s\n%s", show_doc
, help_doc
);
450 full_set_doc
= xstrdup (set_doc
);
451 full_show_doc
= xstrdup (show_doc
);
453 set
= add_set_or_show_cmd (name
, set_cmd
, class, var_type
, var
,
454 full_set_doc
, set_list
);
455 set
->doc_allocated
= 1;
457 if (set_func
!= NULL
)
458 set_cmd_sfunc (set
, set_func
);
460 set_cmd_prefix (set
, set_list
);
462 show
= add_set_or_show_cmd (name
, show_cmd
, class, var_type
, var
,
463 full_show_doc
, show_list
);
464 show
->doc_allocated
= 1;
465 show
->show_value_func
= show_func
;
467 if (set_result
!= NULL
)
469 if (show_result
!= NULL
)
473 /* Add element named NAME to command list LIST (the list for set or
474 some sublist thereof). CLASS is as in add_cmd. ENUMLIST is a list
475 of strings which may follow NAME. VAR is address of the variable
476 which will contain the matching string (from ENUMLIST). */
479 add_setshow_enum_cmd (const char *name
,
480 enum command_class
class,
481 const char *const *enumlist
,
484 const char *show_doc
,
485 const char *help_doc
,
486 cmd_sfunc_ftype
*set_func
,
487 show_value_ftype
*show_func
,
488 struct cmd_list_element
**set_list
,
489 struct cmd_list_element
**show_list
)
491 struct cmd_list_element
*c
;
493 add_setshow_cmd_full (name
, class, var_enum
, var
,
494 set_doc
, show_doc
, help_doc
,
501 const char * const auto_boolean_enums
[] = { "on", "off", "auto", NULL
};
503 /* Add an auto-boolean command named NAME to both the set and show
504 command list lists. CLASS is as in add_cmd. VAR is address of the
505 variable which will contain the value. DOC is the documentation
506 string. FUNC is the corresponding callback. */
508 add_setshow_auto_boolean_cmd (const char *name
,
509 enum command_class
class,
510 enum auto_boolean
*var
,
511 const char *set_doc
, const char *show_doc
,
512 const char *help_doc
,
513 cmd_sfunc_ftype
*set_func
,
514 show_value_ftype
*show_func
,
515 struct cmd_list_element
**set_list
,
516 struct cmd_list_element
**show_list
)
518 struct cmd_list_element
*c
;
520 add_setshow_cmd_full (name
, class, var_auto_boolean
, var
,
521 set_doc
, show_doc
, help_doc
,
525 c
->enums
= auto_boolean_enums
;
528 /* Add element named NAME to both the set and show command LISTs (the
529 list for set/show or some sublist thereof). CLASS is as in
530 add_cmd. VAR is address of the variable which will contain the
531 value. SET_DOC and SHOW_DOC are the documentation strings. */
533 add_setshow_boolean_cmd (const char *name
, enum command_class
class, int *var
,
534 const char *set_doc
, const char *show_doc
,
535 const char *help_doc
,
536 cmd_sfunc_ftype
*set_func
,
537 show_value_ftype
*show_func
,
538 struct cmd_list_element
**set_list
,
539 struct cmd_list_element
**show_list
)
541 static const char *boolean_enums
[] = { "on", "off", NULL
};
542 struct cmd_list_element
*c
;
544 add_setshow_cmd_full (name
, class, var_boolean
, var
,
545 set_doc
, show_doc
, help_doc
,
549 c
->enums
= boolean_enums
;
552 /* Add element named NAME to both the set and show command LISTs (the
553 list for set/show or some sublist thereof). */
555 add_setshow_filename_cmd (const char *name
, enum command_class
class,
557 const char *set_doc
, const char *show_doc
,
558 const char *help_doc
,
559 cmd_sfunc_ftype
*set_func
,
560 show_value_ftype
*show_func
,
561 struct cmd_list_element
**set_list
,
562 struct cmd_list_element
**show_list
)
564 struct cmd_list_element
*set_result
;
566 add_setshow_cmd_full (name
, class, var_filename
, var
,
567 set_doc
, show_doc
, help_doc
,
571 set_cmd_completer (set_result
, filename_completer
);
574 /* Add element named NAME to both the set and show command LISTs (the
575 list for set/show or some sublist thereof). */
577 add_setshow_string_cmd (const char *name
, enum command_class
class,
579 const char *set_doc
, const char *show_doc
,
580 const char *help_doc
,
581 cmd_sfunc_ftype
*set_func
,
582 show_value_ftype
*show_func
,
583 struct cmd_list_element
**set_list
,
584 struct cmd_list_element
**show_list
)
586 add_setshow_cmd_full (name
, class, var_string
, var
,
587 set_doc
, show_doc
, help_doc
,
593 /* Add element named NAME to both the set and show command LISTs (the
594 list for set/show or some sublist thereof). */
595 struct cmd_list_element
*
596 add_setshow_string_noescape_cmd (const char *name
, enum command_class
class,
598 const char *set_doc
, const char *show_doc
,
599 const char *help_doc
,
600 cmd_sfunc_ftype
*set_func
,
601 show_value_ftype
*show_func
,
602 struct cmd_list_element
**set_list
,
603 struct cmd_list_element
**show_list
)
605 struct cmd_list_element
*set_cmd
;
607 add_setshow_cmd_full (name
, class, var_string_noescape
, var
,
608 set_doc
, show_doc
, help_doc
,
615 /* Add element named NAME to both the set and show command LISTs (the
616 list for set/show or some sublist thereof). */
618 add_setshow_optional_filename_cmd (const char *name
, enum command_class
class,
620 const char *set_doc
, const char *show_doc
,
621 const char *help_doc
,
622 cmd_sfunc_ftype
*set_func
,
623 show_value_ftype
*show_func
,
624 struct cmd_list_element
**set_list
,
625 struct cmd_list_element
**show_list
)
627 struct cmd_list_element
*set_result
;
629 add_setshow_cmd_full (name
, class, var_optional_filename
, var
,
630 set_doc
, show_doc
, help_doc
,
635 set_cmd_completer (set_result
, filename_completer
);
639 /* Completes on literal "unlimited". Used by integer commands that
640 support a special "unlimited" value. */
642 static VEC (char_ptr
) *
643 integer_unlimited_completer (struct cmd_list_element
*ignore
,
644 const char *text
, const char *word
)
646 static const char * const keywords
[] =
652 return complete_on_enum (keywords
, text
, word
);
655 /* Add element named NAME to both the set and show command LISTs (the
656 list for set/show or some sublist thereof). CLASS is as in
657 add_cmd. VAR is address of the variable which will contain the
658 value. SET_DOC and SHOW_DOC are the documentation strings. This
659 function is only used in Python API. Please don't use it elsewhere. */
661 add_setshow_integer_cmd (const char *name
, enum command_class
class,
663 const char *set_doc
, const char *show_doc
,
664 const char *help_doc
,
665 cmd_sfunc_ftype
*set_func
,
666 show_value_ftype
*show_func
,
667 struct cmd_list_element
**set_list
,
668 struct cmd_list_element
**show_list
)
670 struct cmd_list_element
*set
;
672 add_setshow_cmd_full (name
, class, var_integer
, var
,
673 set_doc
, show_doc
, help_doc
,
678 set_cmd_completer (set
, integer_unlimited_completer
);
681 /* Add element named NAME to both the set and show command LISTs (the
682 list for set/show or some sublist thereof). CLASS is as in
683 add_cmd. VAR is address of the variable which will contain the
684 value. SET_DOC and SHOW_DOC are the documentation strings. */
686 add_setshow_uinteger_cmd (const char *name
, enum command_class
class,
688 const char *set_doc
, const char *show_doc
,
689 const char *help_doc
,
690 cmd_sfunc_ftype
*set_func
,
691 show_value_ftype
*show_func
,
692 struct cmd_list_element
**set_list
,
693 struct cmd_list_element
**show_list
)
695 struct cmd_list_element
*set
;
697 add_setshow_cmd_full (name
, class, var_uinteger
, var
,
698 set_doc
, show_doc
, help_doc
,
703 set_cmd_completer (set
, integer_unlimited_completer
);
706 /* Add element named NAME to both the set and show command LISTs (the
707 list for set/show or some sublist thereof). CLASS is as in
708 add_cmd. VAR is address of the variable which will contain the
709 value. SET_DOC and SHOW_DOC are the documentation strings. */
711 add_setshow_zinteger_cmd (const char *name
, enum command_class
class,
713 const char *set_doc
, const char *show_doc
,
714 const char *help_doc
,
715 cmd_sfunc_ftype
*set_func
,
716 show_value_ftype
*show_func
,
717 struct cmd_list_element
**set_list
,
718 struct cmd_list_element
**show_list
)
720 add_setshow_cmd_full (name
, class, var_zinteger
, var
,
721 set_doc
, show_doc
, help_doc
,
728 add_setshow_zuinteger_unlimited_cmd (const char *name
,
729 enum command_class
class,
732 const char *show_doc
,
733 const char *help_doc
,
734 cmd_sfunc_ftype
*set_func
,
735 show_value_ftype
*show_func
,
736 struct cmd_list_element
**set_list
,
737 struct cmd_list_element
**show_list
)
739 struct cmd_list_element
*set
;
741 add_setshow_cmd_full (name
, class, var_zuinteger_unlimited
, var
,
742 set_doc
, show_doc
, help_doc
,
747 set_cmd_completer (set
, integer_unlimited_completer
);
750 /* Add element named NAME to both the set and show command LISTs (the
751 list for set/show or some sublist thereof). CLASS is as in
752 add_cmd. VAR is address of the variable which will contain the
753 value. SET_DOC and SHOW_DOC are the documentation strings. */
755 add_setshow_zuinteger_cmd (const char *name
, enum command_class
class,
757 const char *set_doc
, const char *show_doc
,
758 const char *help_doc
,
759 cmd_sfunc_ftype
*set_func
,
760 show_value_ftype
*show_func
,
761 struct cmd_list_element
**set_list
,
762 struct cmd_list_element
**show_list
)
764 add_setshow_cmd_full (name
, class, var_zuinteger
, var
,
765 set_doc
, show_doc
, help_doc
,
771 /* Remove the command named NAME from the command list. Return the
772 list commands which were aliased to the deleted command. If the
773 command had no aliases, return NULL. The various *HOOKs are set to
774 the pre- and post-hook commands for the deleted command. If the
775 command does not have a hook, the corresponding out parameter is
778 static struct cmd_list_element
*
779 delete_cmd (const char *name
, struct cmd_list_element
**list
,
780 struct cmd_list_element
**prehook
,
781 struct cmd_list_element
**prehookee
,
782 struct cmd_list_element
**posthook
,
783 struct cmd_list_element
**posthookee
)
785 struct cmd_list_element
*iter
;
786 struct cmd_list_element
**previous_chain_ptr
;
787 struct cmd_list_element
*aliases
= NULL
;
793 previous_chain_ptr
= list
;
795 for (iter
= *previous_chain_ptr
; iter
; iter
= *previous_chain_ptr
)
797 if (strcmp (iter
->name
, name
) == 0)
800 iter
->destroyer (iter
, iter
->context
);
801 if (iter
->hookee_pre
)
802 iter
->hookee_pre
->hook_pre
= 0;
803 *prehook
= iter
->hook_pre
;
804 *prehookee
= iter
->hookee_pre
;
805 if (iter
->hookee_post
)
806 iter
->hookee_post
->hook_post
= 0;
807 if (iter
->doc
&& iter
->doc_allocated
)
808 xfree ((char *) iter
->doc
);
809 *posthook
= iter
->hook_post
;
810 *posthookee
= iter
->hookee_post
;
812 /* Update the link. */
813 *previous_chain_ptr
= iter
->next
;
815 aliases
= iter
->aliases
;
817 /* If this command was an alias, remove it from the list of
819 if (iter
->cmd_pointer
)
821 struct cmd_list_element
**prevp
= &iter
->cmd_pointer
->aliases
;
822 struct cmd_list_element
*a
= *prevp
;
826 prevp
= &a
->alias_chain
;
829 *prevp
= iter
->alias_chain
;
834 /* We won't see another command with the same name. */
838 previous_chain_ptr
= &iter
->next
;
844 /* Shorthands to the commands above. */
846 /* Add an element to the list of info subcommands. */
848 struct cmd_list_element
*
849 add_info (const char *name
, cmd_cfunc_ftype
*fun
, const char *doc
)
851 return add_cmd (name
, no_class
, fun
, doc
, &infolist
);
854 /* Add an alias to the list of info subcommands. */
856 struct cmd_list_element
*
857 add_info_alias (const char *name
, const char *oldname
, int abbrev_flag
)
859 return add_alias_cmd (name
, oldname
, 0, abbrev_flag
, &infolist
);
862 /* Add an element to the list of commands. */
864 struct cmd_list_element
*
865 add_com (const char *name
, enum command_class
class, cmd_cfunc_ftype
*fun
,
868 return add_cmd (name
, class, fun
, doc
, &cmdlist
);
871 /* Add an alias or abbreviation command to the list of commands. */
873 struct cmd_list_element
*
874 add_com_alias (const char *name
, const char *oldname
, enum command_class
class,
877 return add_alias_cmd (name
, oldname
, class, abbrev_flag
, &cmdlist
);
880 /* Recursively walk the commandlist structures, and print out the
881 documentation of commands that match our regex in either their
882 name, or their documentation.
885 apropos_cmd (struct ui_file
*stream
,
886 struct cmd_list_element
*commandlist
,
887 struct re_pattern_buffer
*regex
, const char *prefix
)
889 struct cmd_list_element
*c
;
892 /* Walk through the commands. */
893 for (c
=commandlist
;c
;c
=c
->next
)
895 returnvalue
= -1; /* Needed to avoid double printing. */
898 /* Try to match against the name. */
899 returnvalue
= re_search (regex
, c
->name
, strlen(c
->name
),
900 0, strlen (c
->name
), NULL
);
901 if (returnvalue
>= 0)
903 print_help_for_command (c
, prefix
,
904 0 /* don't recurse */, stream
);
907 if (c
->doc
!= NULL
&& returnvalue
< 0)
909 /* Try to match against documentation. */
910 if (re_search(regex
,c
->doc
,strlen(c
->doc
),0,strlen(c
->doc
),NULL
) >=0)
912 print_help_for_command (c
, prefix
,
913 0 /* don't recurse */, stream
);
916 /* Check if this command has subcommands and is not an
917 abbreviation. We skip listing subcommands of abbreviations
918 in order to avoid duplicates in the output. */
919 if (c
->prefixlist
!= NULL
&& !c
->abbrev_flag
)
921 /* Recursively call ourselves on the subcommand list,
922 passing the right prefix in. */
923 apropos_cmd (stream
,*c
->prefixlist
,regex
,c
->prefixname
);
928 /* This command really has to deal with two things:
929 1) I want documentation on *this string* (usually called by
932 2) I want documentation on *this list* (usually called by giving a
933 command that requires subcommands. Also called by saying just
936 I am going to split this into two seperate comamnds, help_cmd and
940 help_cmd (const char *command
, struct ui_file
*stream
)
942 struct cmd_list_element
*c
;
946 help_list (cmdlist
, "", all_classes
, stream
);
950 if (strcmp (command
, "all") == 0)
956 c
= lookup_cmd (&command
, cmdlist
, "", 0, 0);
961 /* There are three cases here.
962 If c->prefixlist is nonzero, we have a prefix command.
963 Print its documentation, then list its subcommands.
965 If c->func is non NULL, we really have a command. Print its
966 documentation and return.
968 If c->func is NULL, we have a class name. Print its
969 documentation (as if it were a command) and then set class to the
970 number of this class so that the commands in the class will be
973 fputs_filtered (c
->doc
, stream
);
974 fputs_filtered ("\n", stream
);
976 if (c
->prefixlist
== 0 && c
->func
!= NULL
)
978 fprintf_filtered (stream
, "\n");
980 /* If this is a prefix command, print it's subcommands. */
982 help_list (*c
->prefixlist
, c
->prefixname
, all_commands
, stream
);
984 /* If this is a class name, print all of the commands in the class. */
986 help_list (cmdlist
, "", c
->class, stream
);
988 if (c
->hook_pre
|| c
->hook_post
)
989 fprintf_filtered (stream
,
990 "\nThis command has a hook (or hooks) defined:\n");
993 fprintf_filtered (stream
,
994 "\tThis command is run after : %s (pre hook)\n",
997 fprintf_filtered (stream
,
998 "\tThis command is run before : %s (post hook)\n",
1003 * Get a specific kind of help on a command list.
1006 * CMDTYPE is the prefix to use in the title string.
1007 * CLASS is the class with which to list the nodes of this list (see
1008 * documentation for help_cmd_list below), As usual, ALL_COMMANDS for
1009 * everything, ALL_CLASSES for just classes, and non-negative for only things
1010 * in a specific class.
1011 * and STREAM is the output stream on which to print things.
1012 * If you call this routine with a class >= 0, it recurses.
1015 help_list (struct cmd_list_element
*list
, const char *cmdtype
,
1016 enum command_class
class, struct ui_file
*stream
)
1019 char *cmdtype1
, *cmdtype2
;
1021 /* If CMDTYPE is "foo ", CMDTYPE1 gets " foo" and CMDTYPE2 gets "foo sub".
1023 len
= strlen (cmdtype
);
1024 cmdtype1
= (char *) alloca (len
+ 1);
1026 cmdtype2
= (char *) alloca (len
+ 4);
1031 strncpy (cmdtype1
+ 1, cmdtype
, len
- 1);
1033 strncpy (cmdtype2
, cmdtype
, len
- 1);
1034 strcpy (cmdtype2
+ len
- 1, " sub");
1037 if (class == all_classes
)
1038 fprintf_filtered (stream
, "List of classes of %scommands:\n\n", cmdtype2
);
1040 fprintf_filtered (stream
, "List of %scommands:\n\n", cmdtype2
);
1042 help_cmd_list (list
, class, cmdtype
, (int) class >= 0, stream
);
1044 if (class == all_classes
)
1046 fprintf_filtered (stream
, "\n\
1047 Type \"help%s\" followed by a class name for a list of commands in ",
1050 fprintf_filtered (stream
, "that class.");
1052 fprintf_filtered (stream
, "\n\
1053 Type \"help all\" for the list of all commands.");
1056 fprintf_filtered (stream
, "\nType \"help%s\" followed by %scommand name ",
1057 cmdtype1
, cmdtype2
);
1059 fputs_filtered ("for ", stream
);
1061 fputs_filtered ("full ", stream
);
1063 fputs_filtered ("documentation.\n", stream
);
1064 fputs_filtered ("Type \"apropos word\" to search "
1065 "for commands related to \"word\".\n", stream
);
1066 fputs_filtered ("Command name abbreviations are allowed if unambiguous.\n",
1071 help_all (struct ui_file
*stream
)
1073 struct cmd_list_element
*c
;
1074 int seen_unclassified
= 0;
1076 for (c
= cmdlist
; c
; c
= c
->next
)
1080 /* If this is a class name, print all of the commands in the
1083 if (c
->func
== NULL
)
1085 fprintf_filtered (stream
, "\nCommand class: %s\n\n", c
->name
);
1086 help_cmd_list (cmdlist
, c
->class, "", 1, stream
);
1090 /* While it's expected that all commands are in some class,
1091 as a safety measure, we'll print commands outside of any
1092 class at the end. */
1094 for (c
= cmdlist
; c
; c
= c
->next
)
1099 if (c
->class == no_class
)
1101 if (!seen_unclassified
)
1103 fprintf_filtered (stream
, "\nUnclassified commands\n\n");
1104 seen_unclassified
= 1;
1106 print_help_for_command (c
, "", 1, stream
);
1112 /* Print only the first line of STR on STREAM. */
1114 print_doc_line (struct ui_file
*stream
, const char *str
)
1116 static char *line_buffer
= 0;
1117 static int line_size
;
1123 line_buffer
= (char *) xmalloc (line_size
);
1126 /* Keep printing '.' or ',' not followed by a whitespace for embedded strings
1129 while (*p
&& *p
!= '\n'
1130 && ((*p
!= '.' && *p
!= ',') || (p
[1] && !isspace (p
[1]))))
1132 if (p
- str
> line_size
- 1)
1134 line_size
= p
- str
+ 1;
1135 xfree (line_buffer
);
1136 line_buffer
= (char *) xmalloc (line_size
);
1138 strncpy (line_buffer
, str
, p
- str
);
1139 line_buffer
[p
- str
] = '\0';
1140 if (islower (line_buffer
[0]))
1141 line_buffer
[0] = toupper (line_buffer
[0]);
1142 fputs_filtered (line_buffer
, stream
);
1145 /* Print one-line help for command C.
1146 If RECURSE is non-zero, also print one-line descriptions
1147 of all prefixed subcommands. */
1149 print_help_for_command (struct cmd_list_element
*c
, const char *prefix
,
1150 int recurse
, struct ui_file
*stream
)
1152 fprintf_filtered (stream
, "%s%s -- ", prefix
, c
->name
);
1153 print_doc_line (stream
, c
->doc
);
1154 fputs_filtered ("\n", stream
);
1157 && c
->prefixlist
!= 0
1158 && c
->abbrev_flag
== 0)
1159 /* Subcommands of a prefix command typically have 'all_commands'
1160 as class. If we pass CLASS to recursive invocation,
1161 most often we won't see anything. */
1162 help_cmd_list (*c
->prefixlist
, all_commands
, c
->prefixname
, 1, stream
);
1166 * Implement a help command on command list LIST.
1167 * RECURSE should be non-zero if this should be done recursively on
1168 * all sublists of LIST.
1169 * PREFIX is the prefix to print before each command name.
1170 * STREAM is the stream upon which the output should be written.
1172 * A non-negative class number to list only commands in that
1174 * ALL_COMMANDS to list all commands in list.
1175 * ALL_CLASSES to list all classes in list.
1177 * Note that RECURSE will be active on *all* sublists, not just the
1178 * ones selected by the criteria above (ie. the selection mechanism
1179 * is at the low level, not the high-level).
1182 help_cmd_list (struct cmd_list_element
*list
, enum command_class
class,
1183 const char *prefix
, int recurse
, struct ui_file
*stream
)
1185 struct cmd_list_element
*c
;
1187 for (c
= list
; c
; c
= c
->next
)
1189 if (c
->abbrev_flag
== 0
1190 && (class == all_commands
1191 || (class == all_classes
&& c
->func
== NULL
)
1192 || (class == c
->class && c
->func
!= NULL
)))
1194 print_help_for_command (c
, prefix
, recurse
, stream
);
1196 else if (c
->abbrev_flag
== 0 && recurse
1197 && class == class_user
&& c
->prefixlist
!= NULL
)
1198 /* User-defined commands may be subcommands. */
1199 help_cmd_list (*c
->prefixlist
, class, c
->prefixname
,
1205 /* Search the input clist for 'command'. Return the command if
1206 found (or NULL if not), and return the number of commands
1209 static struct cmd_list_element
*
1210 find_cmd (const char *command
, int len
, struct cmd_list_element
*clist
,
1211 int ignore_help_classes
, int *nfound
)
1213 struct cmd_list_element
*found
, *c
;
1215 found
= (struct cmd_list_element
*) NULL
;
1217 for (c
= clist
; c
; c
= c
->next
)
1218 if (!strncmp (command
, c
->name
, len
)
1219 && (!ignore_help_classes
|| c
->func
))
1223 if (c
->name
[len
] == '\0')
1233 find_command_name_length (const char *text
)
1235 const char *p
= text
;
1237 /* Treating underscores as part of command words is important
1238 so that "set args_foo()" doesn't get interpreted as
1239 "set args _foo()". */
1240 /* Some characters are only used for TUI specific commands.
1241 However, they are always allowed for the sake of consistency.
1243 The XDB compatibility characters are only allowed when using the
1244 right mode because they clash with other GDB commands -
1245 specifically '/' is used as a suffix for print, examine and
1248 Note that this is larger than the character set allowed when
1249 creating user-defined commands. */
1251 /* Recognize '!' as a single character command so that, e.g., "!ls"
1252 works as expected. */
1256 while (isalnum (*p
) || *p
== '-' || *p
== '_'
1257 /* Characters used by TUI specific commands. */
1258 || *p
== '+' || *p
== '<' || *p
== '>' || *p
== '$'
1259 /* Characters used for XDB compatibility. */
1260 || (xdb_commands
&& (*p
== '/' || *p
== '?')))
1266 /* Return TRUE if NAME is a valid user-defined command name.
1267 This is a stricter subset of all gdb commands,
1268 see find_command_name_length. */
1271 valid_user_defined_cmd_name_p (const char *name
)
1278 /* Alas "42" is a legitimate user-defined command.
1279 In the interests of not breaking anything we preserve that. */
1281 for (p
= name
; *p
!= '\0'; ++p
)
1294 /* This routine takes a line of TEXT and a CLIST in which to start the
1295 lookup. When it returns it will have incremented the text pointer past
1296 the section of text it matched, set *RESULT_LIST to point to the list in
1297 which the last word was matched, and will return a pointer to the cmd
1298 list element which the text matches. It will return NULL if no match at
1299 all was possible. It will return -1 (cast appropriately, ick) if ambigous
1300 matches are possible; in this case *RESULT_LIST will be set to point to
1301 the list in which there are ambiguous choices (and *TEXT will be set to
1302 the ambiguous text string).
1304 If the located command was an abbreviation, this routine returns the base
1305 command of the abbreviation.
1307 It does no error reporting whatsoever; control will always return
1308 to the superior routine.
1310 In the case of an ambiguous return (-1), *RESULT_LIST will be set to point
1311 at the prefix_command (ie. the best match) *or* (special case) will be NULL
1312 if no prefix command was ever found. For example, in the case of "info a",
1313 "info" matches without ambiguity, but "a" could be "args" or "address", so
1314 *RESULT_LIST is set to the cmd_list_element for "info". So in this case
1315 RESULT_LIST should not be interpeted as a pointer to the beginning of a
1316 list; it simply points to a specific command. In the case of an ambiguous
1317 return *TEXT is advanced past the last non-ambiguous prefix (e.g.
1318 "info t" can be "info types" or "info target"; upon return *TEXT has been
1319 advanced past "info ").
1321 If RESULT_LIST is NULL, don't set *RESULT_LIST (but don't otherwise
1322 affect the operation).
1324 This routine does *not* modify the text pointed to by TEXT.
1326 If IGNORE_HELP_CLASSES is nonzero, ignore any command list elements which
1327 are actually help classes rather than commands (i.e. the function field of
1328 the struct cmd_list_element is NULL). */
1330 struct cmd_list_element
*
1331 lookup_cmd_1 (const char **text
, struct cmd_list_element
*clist
,
1332 struct cmd_list_element
**result_list
, int ignore_help_classes
)
1335 int len
, tmp
, nfound
;
1336 struct cmd_list_element
*found
, *c
;
1337 const char *line
= *text
;
1339 while (**text
== ' ' || **text
== '\t')
1342 /* Identify the name of the command. */
1343 len
= find_command_name_length (*text
);
1345 /* If nothing but whitespace, return 0. */
1349 /* *text and p now bracket the first command word to lookup (and
1350 it's length is len). We copy this into a local temporary. */
1353 command
= (char *) alloca (len
+ 1);
1354 memcpy (command
, *text
, len
);
1355 command
[len
] = '\0';
1360 found
= find_cmd (command
, len
, clist
, ignore_help_classes
, &nfound
);
1362 /* We didn't find the command in the entered case, so lower case it
1363 and search again. */
1364 if (!found
|| nfound
== 0)
1366 for (tmp
= 0; tmp
< len
; tmp
++)
1368 char x
= command
[tmp
];
1370 command
[tmp
] = isupper (x
) ? tolower (x
) : x
;
1372 found
= find_cmd (command
, len
, clist
, ignore_help_classes
, &nfound
);
1375 /* If nothing matches, we have a simple failure. */
1381 if (result_list
!= NULL
)
1382 /* Will be modified in calling routine
1383 if we know what the prefix command is. */
1385 return CMD_LIST_AMBIGUOUS
; /* Ambiguous. */
1388 /* We've matched something on this list. Move text pointer forward. */
1392 if (found
->cmd_pointer
)
1394 /* We drop the alias (abbreviation) in favor of the command it
1395 is pointing to. If the alias is deprecated, though, we need to
1396 warn the user about it before we drop it. Note that while we
1397 are warning about the alias, we may also warn about the command
1398 itself and we will adjust the appropriate DEPRECATED_WARN_USER
1401 if (found
->deprecated_warn_user
)
1402 deprecated_cmd_warning (line
);
1403 found
= found
->cmd_pointer
;
1405 /* If we found a prefix command, keep looking. */
1407 if (found
->prefixlist
)
1409 c
= lookup_cmd_1 (text
, *found
->prefixlist
, result_list
,
1410 ignore_help_classes
);
1413 /* Didn't find anything; this is as far as we got. */
1414 if (result_list
!= NULL
)
1415 *result_list
= clist
;
1418 else if (c
== CMD_LIST_AMBIGUOUS
)
1420 /* We've gotten this far properly, but the next step is
1421 ambiguous. We need to set the result list to the best
1422 we've found (if an inferior hasn't already set it). */
1423 if (result_list
!= NULL
)
1425 /* This used to say *result_list = *found->prefixlist.
1426 If that was correct, need to modify the documentation
1427 at the top of this function to clarify what is
1428 supposed to be going on. */
1429 *result_list
= found
;
1440 if (result_list
!= NULL
)
1441 *result_list
= clist
;
1446 /* All this hair to move the space to the front of cmdtype */
1449 undef_cmd_error (const char *cmdtype
, const char *q
)
1451 error (_("Undefined %scommand: \"%s\". Try \"help%s%.*s\"."),
1454 *cmdtype
? " " : "",
1455 (int) strlen (cmdtype
) - 1,
1459 /* Look up the contents of *LINE as a command in the command list LIST.
1460 LIST is a chain of struct cmd_list_element's.
1461 If it is found, return the struct cmd_list_element for that command
1462 and update *LINE to point after the command name, at the first argument.
1463 If not found, call error if ALLOW_UNKNOWN is zero
1464 otherwise (or if error returns) return zero.
1465 Call error if specified command is ambiguous,
1466 unless ALLOW_UNKNOWN is negative.
1467 CMDTYPE precedes the word "command" in the error message.
1469 If INGNORE_HELP_CLASSES is nonzero, ignore any command list
1470 elements which are actually help classes rather than commands (i.e.
1471 the function field of the struct cmd_list_element is 0). */
1473 struct cmd_list_element
*
1474 lookup_cmd (const char **line
, struct cmd_list_element
*list
, char *cmdtype
,
1475 int allow_unknown
, int ignore_help_classes
)
1477 struct cmd_list_element
*last_list
= 0;
1478 struct cmd_list_element
*c
;
1480 /* Note: Do not remove trailing whitespace here because this
1481 would be wrong for complete_command. Jim Kingdon */
1484 error (_("Lack of needed %scommand"), cmdtype
);
1486 c
= lookup_cmd_1 (line
, list
, &last_list
, ignore_help_classes
);
1493 int len
= find_command_name_length (*line
);
1495 q
= (char *) alloca (len
+ 1);
1496 strncpy (q
, *line
, len
);
1498 undef_cmd_error (cmdtype
, q
);
1503 else if (c
== CMD_LIST_AMBIGUOUS
)
1505 /* Ambigous. Local values should be off prefixlist or called
1507 int local_allow_unknown
= (last_list
? last_list
->allow_unknown
:
1509 const char *local_cmdtype
= last_list
? last_list
->prefixname
: cmdtype
;
1510 struct cmd_list_element
*local_list
=
1511 (last_list
? *(last_list
->prefixlist
) : list
);
1513 if (local_allow_unknown
< 0)
1516 return last_list
; /* Found something. */
1518 return 0; /* Found nothing. */
1522 /* Report as error. */
1527 ((*line
)[amb_len
] && (*line
)[amb_len
] != ' '
1528 && (*line
)[amb_len
] != '\t');
1533 for (c
= local_list
; c
; c
= c
->next
)
1534 if (!strncmp (*line
, c
->name
, amb_len
))
1536 if (strlen (ambbuf
) + strlen (c
->name
) + 6
1537 < (int) sizeof ambbuf
)
1539 if (strlen (ambbuf
))
1540 strcat (ambbuf
, ", ");
1541 strcat (ambbuf
, c
->name
);
1545 strcat (ambbuf
, "..");
1549 error (_("Ambiguous %scommand \"%s\": %s."), local_cmdtype
,
1551 return 0; /* lint */
1556 if (c
->type
== set_cmd
&& **line
!= '\0' && !isspace (**line
))
1557 error (_("Argument must be preceded by space."));
1559 /* We've got something. It may still not be what the caller
1560 wants (if this command *needs* a subcommand). */
1561 while (**line
== ' ' || **line
== '\t')
1564 if (c
->prefixlist
&& **line
&& !c
->allow_unknown
)
1565 undef_cmd_error (c
->prefixname
, *line
);
1567 /* Seems to be what he wants. Return it. */
1573 /* We are here presumably because an alias or command in TEXT is
1574 deprecated and a warning message should be generated. This
1575 function decodes TEXT and potentially generates a warning message
1578 Example for 'set endian big' which has a fictitious alias 'seb'.
1580 If alias wasn't used in TEXT, and the command is deprecated:
1581 "warning: 'set endian big' is deprecated."
1583 If alias was used, and only the alias is deprecated:
1584 "warning: 'seb' an alias for the command 'set endian big' is deprecated."
1586 If alias was used and command is deprecated (regardless of whether
1587 the alias itself is deprecated:
1589 "warning: 'set endian big' (seb) is deprecated."
1591 After the message has been sent, clear the appropriate flags in the
1592 command and/or the alias so the user is no longer bothered.
1596 deprecated_cmd_warning (const char *text
)
1598 struct cmd_list_element
*alias
= NULL
;
1599 struct cmd_list_element
*prefix_cmd
= NULL
;
1600 struct cmd_list_element
*cmd
= NULL
;
1602 if (!lookup_cmd_composition (text
, &alias
, &prefix_cmd
, &cmd
))
1603 /* Return if text doesn't evaluate to a command. */
1606 if (!((alias
? alias
->deprecated_warn_user
: 0)
1607 || cmd
->deprecated_warn_user
) )
1608 /* Return if nothing is deprecated. */
1611 printf_filtered ("Warning:");
1613 if (alias
&& !cmd
->cmd_deprecated
)
1614 printf_filtered (" '%s', an alias for the", alias
->name
);
1616 printf_filtered (" command '");
1619 printf_filtered ("%s", prefix_cmd
->prefixname
);
1621 printf_filtered ("%s", cmd
->name
);
1623 if (alias
&& cmd
->cmd_deprecated
)
1624 printf_filtered ("' (%s) is deprecated.\n", alias
->name
);
1626 printf_filtered ("' is deprecated.\n");
1629 /* If it is only the alias that is deprecated, we want to indicate
1630 the new alias, otherwise we'll indicate the new command. */
1632 if (alias
&& !cmd
->cmd_deprecated
)
1634 if (alias
->replacement
)
1635 printf_filtered ("Use '%s'.\n\n", alias
->replacement
);
1637 printf_filtered ("No alternative known.\n\n");
1641 if (cmd
->replacement
)
1642 printf_filtered ("Use '%s'.\n\n", cmd
->replacement
);
1644 printf_filtered ("No alternative known.\n\n");
1647 /* We've warned you, now we'll keep quiet. */
1649 alias
->deprecated_warn_user
= 0;
1651 cmd
->deprecated_warn_user
= 0;
1655 /* Look up the contents of LINE as a command in the command list 'cmdlist'.
1656 Return 1 on success, 0 on failure.
1658 If LINE refers to an alias, *alias will point to that alias.
1660 If LINE is a postfix command (i.e. one that is preceded by a prefix
1661 command) set *prefix_cmd.
1663 Set *cmd to point to the command LINE indicates.
1665 If any of *alias, *prefix_cmd, or *cmd cannot be determined or do not
1666 exist, they are NULL when we return.
1670 lookup_cmd_composition (const char *text
,
1671 struct cmd_list_element
**alias
,
1672 struct cmd_list_element
**prefix_cmd
,
1673 struct cmd_list_element
**cmd
)
1676 int len
, tmp
, nfound
;
1677 struct cmd_list_element
*cur_list
;
1678 struct cmd_list_element
*prev_cmd
;
1688 /* Go through as many command lists as we need to,
1689 to find the command TEXT refers to. */
1693 while (*text
== ' ' || *text
== '\t')
1696 /* Identify the name of the command. */
1697 len
= find_command_name_length (text
);
1699 /* If nothing but whitespace, return. */
1703 /* Text is the start of the first command word to lookup (and
1704 it's length is len). We copy this into a local temporary. */
1706 command
= (char *) alloca (len
+ 1);
1707 memcpy (command
, text
, len
);
1708 command
[len
] = '\0';
1713 *cmd
= find_cmd (command
, len
, cur_list
, 1, &nfound
);
1715 /* We didn't find the command in the entered case, so lower case
1716 it and search again.
1718 if (!*cmd
|| nfound
== 0)
1720 for (tmp
= 0; tmp
< len
; tmp
++)
1722 char x
= command
[tmp
];
1724 command
[tmp
] = isupper (x
) ? tolower (x
) : x
;
1726 *cmd
= find_cmd (command
, len
, cur_list
, 1, &nfound
);
1729 if (*cmd
== CMD_LIST_AMBIGUOUS
)
1731 return 0; /* ambiguous */
1735 return 0; /* nothing found */
1738 if ((*cmd
)->cmd_pointer
)
1740 /* cmd was actually an alias, we note that an alias was
1741 used (by assigning *alais) and we set *cmd. */
1743 *cmd
= (*cmd
)->cmd_pointer
;
1745 *prefix_cmd
= prev_cmd
;
1747 if ((*cmd
)->prefixlist
)
1748 cur_list
= *(*cmd
)->prefixlist
;
1756 /* Helper function for SYMBOL_COMPLETION_FUNCTION. */
1758 /* Return a vector of char pointers which point to the different
1759 possible completions in LIST of TEXT.
1761 WORD points in the same buffer as TEXT, and completions should be
1762 returned relative to this position. For example, suppose TEXT is
1763 "foo" and we want to complete to "foobar". If WORD is "oo", return
1764 "oobar"; if WORD is "baz/foo", return "baz/foobar". */
1767 complete_on_cmdlist (struct cmd_list_element
*list
,
1768 const char *text
, const char *word
,
1769 int ignore_help_classes
)
1771 struct cmd_list_element
*ptr
;
1772 VEC (char_ptr
) *matchlist
= NULL
;
1773 int textlen
= strlen (text
);
1775 int saw_deprecated_match
= 0;
1777 /* We do one or two passes. In the first pass, we skip deprecated
1778 commands. If we see no matching commands in the first pass, and
1779 if we did happen to see a matching deprecated command, we do
1780 another loop to collect those. */
1781 for (pass
= 0; matchlist
== 0 && pass
< 2; ++pass
)
1783 for (ptr
= list
; ptr
; ptr
= ptr
->next
)
1784 if (!strncmp (ptr
->name
, text
, textlen
)
1785 && !ptr
->abbrev_flag
1786 && (!ignore_help_classes
|| ptr
->func
1787 || ptr
->prefixlist
))
1793 if (ptr
->cmd_deprecated
)
1795 saw_deprecated_match
= 1;
1800 match
= (char *) xmalloc (strlen (word
) + strlen (ptr
->name
) + 1);
1802 strcpy (match
, ptr
->name
);
1803 else if (word
> text
)
1805 /* Return some portion of ptr->name. */
1806 strcpy (match
, ptr
->name
+ (word
- text
));
1810 /* Return some of text plus ptr->name. */
1811 strncpy (match
, word
, text
- word
);
1812 match
[text
- word
] = '\0';
1813 strcat (match
, ptr
->name
);
1815 VEC_safe_push (char_ptr
, matchlist
, match
);
1817 /* If we saw no matching deprecated commands in the first pass,
1819 if (!saw_deprecated_match
)
1826 /* Helper function for SYMBOL_COMPLETION_FUNCTION. */
1828 /* Return a vector of char pointers which point to the different
1829 possible completions in CMD of TEXT.
1831 WORD points in the same buffer as TEXT, and completions should be
1832 returned relative to this position. For example, suppose TEXT is "foo"
1833 and we want to complete to "foobar". If WORD is "oo", return
1834 "oobar"; if WORD is "baz/foo", return "baz/foobar". */
1837 complete_on_enum (const char *const *enumlist
,
1838 const char *text
, const char *word
)
1840 VEC (char_ptr
) *matchlist
= NULL
;
1841 int textlen
= strlen (text
);
1845 for (i
= 0; (name
= enumlist
[i
]) != NULL
; i
++)
1846 if (strncmp (name
, text
, textlen
) == 0)
1850 match
= (char *) xmalloc (strlen (word
) + strlen (name
) + 1);
1852 strcpy (match
, name
);
1853 else if (word
> text
)
1855 /* Return some portion of name. */
1856 strcpy (match
, name
+ (word
- text
));
1860 /* Return some of text plus name. */
1861 strncpy (match
, word
, text
- word
);
1862 match
[text
- word
] = '\0';
1863 strcat (match
, name
);
1865 VEC_safe_push (char_ptr
, matchlist
, match
);
1872 /* Check function pointer. */
1874 cmd_func_p (struct cmd_list_element
*cmd
)
1876 return (cmd
->func
!= NULL
);
1880 /* Call the command function. */
1882 cmd_func (struct cmd_list_element
*cmd
, char *args
, int from_tty
)
1884 if (cmd_func_p (cmd
))
1885 (*cmd
->func
) (cmd
, args
, from_tty
);
1887 error (_("Invalid command"));