1 /* Handle lists of commands, their decoding and documentation, for GDB.
3 Copyright (C) 1986-2013 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
, char *prefix
, int recurse
,
97 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
,
138 void (*cfunc
) (char *args
, int from_tty
))
140 return cmd
->func
== do_cfunc
&& cmd
->function
.cfunc
== cfunc
;
144 set_cmd_context (struct cmd_list_element
*cmd
, void *context
)
146 cmd
->context
= context
;
150 get_cmd_context (struct cmd_list_element
*cmd
)
156 cmd_type (struct cmd_list_element
*cmd
)
162 set_cmd_completer (struct cmd_list_element
*cmd
, completer_ftype
*completer
)
164 cmd
->completer
= completer
; /* Ok. */
167 /* Add element named NAME.
168 Space for NAME and DOC must be allocated by the caller.
169 CLASS is the top level category into which commands are broken down
171 FUN should be the function to execute the command;
172 it will get a character string as argument, with leading
173 and trailing blanks already eliminated.
175 DOC is a documentation string for the command.
176 Its first line should be a complete sentence.
177 It should start with ? for a command that is an abbreviation
178 or with * for a command that most users don't need to know about.
180 Add this command to command list *LIST.
182 Returns a pointer to the added command (not necessarily the head
185 struct cmd_list_element
*
186 add_cmd (const char *name
, enum command_class
class, void (*fun
) (char *, int),
187 char *doc
, struct cmd_list_element
**list
)
189 struct cmd_list_element
*c
190 = (struct cmd_list_element
*) xmalloc (sizeof (struct cmd_list_element
));
191 struct cmd_list_element
*p
, *iter
;
193 /* Turn each alias of the old command into an alias of the new
195 c
->aliases
= delete_cmd (name
, list
, &c
->hook_pre
, &c
->hookee_pre
,
196 &c
->hook_post
, &c
->hookee_post
);
197 for (iter
= c
->aliases
; iter
; iter
= iter
->alias_chain
)
198 iter
->cmd_pointer
= c
;
200 c
->hook_pre
->hookee_pre
= c
;
202 c
->hookee_pre
->hook_pre
= c
;
204 c
->hook_post
->hookee_post
= c
;
206 c
->hookee_post
->hook_post
= c
;
208 if (*list
== NULL
|| strcmp ((*list
)->name
, name
) >= 0)
216 while (p
->next
&& strcmp (p
->next
->name
, name
) <= 0)
226 set_cmd_cfunc (c
, fun
);
227 set_cmd_context (c
, NULL
);
230 c
->replacement
= NULL
;
231 c
->pre_show_hook
= NULL
;
233 c
->prefixlist
= NULL
;
234 c
->prefixname
= NULL
;
235 c
->allow_unknown
= 0;
238 set_cmd_completer (c
, make_symbol_completion_list_fn
);
240 c
->type
= not_set_cmd
;
242 c
->var_type
= var_boolean
;
244 c
->user_commands
= NULL
;
245 c
->cmd_pointer
= NULL
;
246 c
->alias_chain
= NULL
;
251 /* Deprecates a command CMD.
252 REPLACEMENT is the name of the command which should be used in
253 place of this command, or NULL if no such command exists.
255 This function does not check to see if command REPLACEMENT exists
256 since gdb may not have gotten around to adding REPLACEMENT when
257 this function is called.
259 Returns a pointer to the deprecated command. */
261 struct cmd_list_element
*
262 deprecate_cmd (struct cmd_list_element
*cmd
, char *replacement
)
264 cmd
->flags
|= (CMD_DEPRECATED
| DEPRECATED_WARN_USER
);
266 if (replacement
!= NULL
)
267 cmd
->replacement
= replacement
;
269 cmd
->replacement
= NULL
;
274 struct cmd_list_element
*
275 add_alias_cmd (const char *name
, const char *oldname
, enum command_class
class,
276 int abbrev_flag
, struct cmd_list_element
**list
)
279 struct cmd_list_element
*old
;
280 struct cmd_list_element
*c
;
283 old
= lookup_cmd (&tmp
, *list
, "", 1, 1);
287 struct cmd_list_element
*prehook
, *prehookee
, *posthook
, *posthookee
;
288 struct cmd_list_element
*aliases
= delete_cmd (name
, list
,
289 &prehook
, &prehookee
,
290 &posthook
, &posthookee
);
292 /* If this happens, it means a programmer error somewhere. */
293 gdb_assert (!aliases
&& !prehook
&& !prehookee
294 && !posthook
&& ! posthookee
);
298 c
= add_cmd (name
, class, NULL
, old
->doc
, list
);
300 /* If OLD->DOC can be freed, we should make another copy. */
301 if ((old
->flags
& DOC_ALLOCATED
) != 0)
303 c
->doc
= xstrdup (old
->doc
);
304 c
->flags
|= DOC_ALLOCATED
;
306 /* NOTE: Both FUNC and all the FUNCTIONs need to be copied. */
308 c
->function
= old
->function
;
309 c
->prefixlist
= old
->prefixlist
;
310 c
->prefixname
= old
->prefixname
;
311 c
->allow_unknown
= old
->allow_unknown
;
312 c
->abbrev_flag
= abbrev_flag
;
313 c
->cmd_pointer
= old
;
314 c
->alias_chain
= old
->aliases
;
317 set_cmd_prefix (c
, list
);
321 /* Like add_cmd but adds an element for a command prefix: a name that
322 should be followed by a subcommand to be looked up in another
323 command list. PREFIXLIST should be the address of the variable
324 containing that list. */
326 struct cmd_list_element
*
327 add_prefix_cmd (const char *name
, enum command_class
class,
328 void (*fun
) (char *, int),
329 char *doc
, struct cmd_list_element
**prefixlist
,
330 char *prefixname
, int allow_unknown
,
331 struct cmd_list_element
**list
)
333 struct cmd_list_element
*c
= add_cmd (name
, class, fun
, doc
, list
);
334 struct cmd_list_element
*p
;
336 c
->prefixlist
= prefixlist
;
337 c
->prefixname
= prefixname
;
338 c
->allow_unknown
= allow_unknown
;
340 if (list
== &cmdlist
)
343 set_cmd_prefix (c
, list
);
345 /* Update the field 'prefix' of each cmd_list_element in *PREFIXLIST. */
346 for (p
= *prefixlist
; p
!= NULL
; p
= p
->next
)
352 /* Like add_prefix_cmd but sets the abbrev_flag on the new command. */
354 struct cmd_list_element
*
355 add_abbrev_prefix_cmd (const char *name
, enum command_class
class,
356 void (*fun
) (char *, int), char *doc
,
357 struct cmd_list_element
**prefixlist
, char *prefixname
,
358 int allow_unknown
, struct cmd_list_element
**list
)
360 struct cmd_list_element
*c
= add_cmd (name
, class, fun
, doc
, list
);
362 c
->prefixlist
= prefixlist
;
363 c
->prefixname
= prefixname
;
364 c
->allow_unknown
= allow_unknown
;
369 /* This is an empty "cfunc". */
371 not_just_help_class_command (char *args
, int from_tty
)
375 /* This is an empty "sfunc". */
376 static void empty_sfunc (char *, int, struct cmd_list_element
*);
379 empty_sfunc (char *args
, int from_tty
, struct cmd_list_element
*c
)
383 /* Add element named NAME to command list LIST (the list for set/show
384 or some sublist thereof).
385 TYPE is set_cmd or show_cmd.
386 CLASS is as in add_cmd.
387 VAR_TYPE is the kind of thing we are setting.
388 VAR is address of the variable being controlled by this command.
389 DOC is the documentation string. */
391 static struct cmd_list_element
*
392 add_set_or_show_cmd (const char *name
,
394 enum command_class
class,
398 struct cmd_list_element
**list
)
400 struct cmd_list_element
*c
= add_cmd (name
, class, NULL
, doc
, list
);
402 gdb_assert (type
== set_cmd
|| type
== show_cmd
);
404 c
->var_type
= var_type
;
406 /* This needs to be something besides NULL so that this isn't
407 treated as a help class. */
408 set_cmd_sfunc (c
, empty_sfunc
);
412 /* Add element named NAME to both the command SET_LIST and SHOW_LIST.
413 CLASS is as in add_cmd. VAR_TYPE is the kind of thing we are
414 setting. VAR is address of the variable being controlled by this
415 command. SET_FUNC and SHOW_FUNC are the callback functions (if
416 non-NULL). SET_DOC, SHOW_DOC and HELP_DOC are the documentation
417 strings. PRINT the format string to print the value. SET_RESULT
418 and SHOW_RESULT, if not NULL, are set to the resulting command
422 add_setshow_cmd_full (const char *name
,
423 enum command_class
class,
424 var_types var_type
, void *var
,
425 const char *set_doc
, const char *show_doc
,
426 const char *help_doc
,
427 cmd_sfunc_ftype
*set_func
,
428 show_value_ftype
*show_func
,
429 struct cmd_list_element
**set_list
,
430 struct cmd_list_element
**show_list
,
431 struct cmd_list_element
**set_result
,
432 struct cmd_list_element
**show_result
)
434 struct cmd_list_element
*set
;
435 struct cmd_list_element
*show
;
439 if (help_doc
!= NULL
)
441 full_set_doc
= xstrprintf ("%s\n%s", set_doc
, help_doc
);
442 full_show_doc
= xstrprintf ("%s\n%s", show_doc
, help_doc
);
446 full_set_doc
= xstrdup (set_doc
);
447 full_show_doc
= xstrdup (show_doc
);
449 set
= add_set_or_show_cmd (name
, set_cmd
, class, var_type
, var
,
450 full_set_doc
, set_list
);
451 set
->flags
|= DOC_ALLOCATED
;
453 if (set_func
!= NULL
)
454 set_cmd_sfunc (set
, set_func
);
456 set_cmd_prefix (set
, set_list
);
458 show
= add_set_or_show_cmd (name
, show_cmd
, class, var_type
, var
,
459 full_show_doc
, show_list
);
460 show
->flags
|= DOC_ALLOCATED
;
461 show
->show_value_func
= show_func
;
463 if (set_result
!= NULL
)
465 if (show_result
!= NULL
)
469 /* Add element named NAME to command list LIST (the list for set or
470 some sublist thereof). CLASS is as in add_cmd. ENUMLIST is a list
471 of strings which may follow NAME. VAR is address of the variable
472 which will contain the matching string (from ENUMLIST). */
475 add_setshow_enum_cmd (const char *name
,
476 enum command_class
class,
477 const char *const *enumlist
,
480 const char *show_doc
,
481 const char *help_doc
,
482 cmd_sfunc_ftype
*set_func
,
483 show_value_ftype
*show_func
,
484 struct cmd_list_element
**set_list
,
485 struct cmd_list_element
**show_list
)
487 struct cmd_list_element
*c
;
489 add_setshow_cmd_full (name
, class, var_enum
, var
,
490 set_doc
, show_doc
, help_doc
,
497 const char * const auto_boolean_enums
[] = { "on", "off", "auto", NULL
};
499 /* Add an auto-boolean command named NAME to both the set and show
500 command list lists. CLASS is as in add_cmd. VAR is address of the
501 variable which will contain the value. DOC is the documentation
502 string. FUNC is the corresponding callback. */
504 add_setshow_auto_boolean_cmd (const char *name
,
505 enum command_class
class,
506 enum auto_boolean
*var
,
507 const char *set_doc
, const char *show_doc
,
508 const char *help_doc
,
509 cmd_sfunc_ftype
*set_func
,
510 show_value_ftype
*show_func
,
511 struct cmd_list_element
**set_list
,
512 struct cmd_list_element
**show_list
)
514 struct cmd_list_element
*c
;
516 add_setshow_cmd_full (name
, class, var_auto_boolean
, var
,
517 set_doc
, show_doc
, help_doc
,
521 c
->enums
= auto_boolean_enums
;
524 /* Add element named NAME to both the set and show command LISTs (the
525 list for set/show or some sublist thereof). CLASS is as in
526 add_cmd. VAR is address of the variable which will contain the
527 value. SET_DOC and SHOW_DOC are the documentation strings. */
529 add_setshow_boolean_cmd (const char *name
, enum command_class
class, int *var
,
530 const char *set_doc
, const char *show_doc
,
531 const char *help_doc
,
532 cmd_sfunc_ftype
*set_func
,
533 show_value_ftype
*show_func
,
534 struct cmd_list_element
**set_list
,
535 struct cmd_list_element
**show_list
)
537 static const char *boolean_enums
[] = { "on", "off", NULL
};
538 struct cmd_list_element
*c
;
540 add_setshow_cmd_full (name
, class, var_boolean
, var
,
541 set_doc
, show_doc
, help_doc
,
545 c
->enums
= boolean_enums
;
548 /* Add element named NAME to both the set and show command LISTs (the
549 list for set/show or some sublist thereof). */
551 add_setshow_filename_cmd (const char *name
, enum command_class
class,
553 const char *set_doc
, const char *show_doc
,
554 const char *help_doc
,
555 cmd_sfunc_ftype
*set_func
,
556 show_value_ftype
*show_func
,
557 struct cmd_list_element
**set_list
,
558 struct cmd_list_element
**show_list
)
560 struct cmd_list_element
*set_result
;
562 add_setshow_cmd_full (name
, class, var_filename
, var
,
563 set_doc
, show_doc
, help_doc
,
567 set_cmd_completer (set_result
, filename_completer
);
570 /* Add element named NAME to both the set and show command LISTs (the
571 list for set/show or some sublist thereof). */
573 add_setshow_string_cmd (const char *name
, enum command_class
class,
575 const char *set_doc
, const char *show_doc
,
576 const char *help_doc
,
577 cmd_sfunc_ftype
*set_func
,
578 show_value_ftype
*show_func
,
579 struct cmd_list_element
**set_list
,
580 struct cmd_list_element
**show_list
)
582 add_setshow_cmd_full (name
, class, var_string
, var
,
583 set_doc
, show_doc
, help_doc
,
589 /* Add element named NAME to both the set and show command LISTs (the
590 list for set/show or some sublist thereof). */
591 struct cmd_list_element
*
592 add_setshow_string_noescape_cmd (const char *name
, enum command_class
class,
594 const char *set_doc
, const char *show_doc
,
595 const char *help_doc
,
596 cmd_sfunc_ftype
*set_func
,
597 show_value_ftype
*show_func
,
598 struct cmd_list_element
**set_list
,
599 struct cmd_list_element
**show_list
)
601 struct cmd_list_element
*set_cmd
;
603 add_setshow_cmd_full (name
, class, var_string_noescape
, var
,
604 set_doc
, show_doc
, help_doc
,
611 /* Add element named NAME to both the set and show command LISTs (the
612 list for set/show or some sublist thereof). */
614 add_setshow_optional_filename_cmd (const char *name
, enum command_class
class,
616 const char *set_doc
, const char *show_doc
,
617 const char *help_doc
,
618 cmd_sfunc_ftype
*set_func
,
619 show_value_ftype
*show_func
,
620 struct cmd_list_element
**set_list
,
621 struct cmd_list_element
**show_list
)
623 struct cmd_list_element
*set_result
;
625 add_setshow_cmd_full (name
, class, var_optional_filename
, var
,
626 set_doc
, show_doc
, help_doc
,
631 set_cmd_completer (set_result
, filename_completer
);
635 /* Completes on literal "unlimited". Used by integer commands that
636 support a special "unlimited" value. */
638 static VEC (char_ptr
) *
639 integer_unlimited_completer (struct cmd_list_element
*ignore
,
640 const char *text
, const char *word
)
642 static const char * const keywords
[] =
648 return complete_on_enum (keywords
, text
, word
);
651 /* Add element named NAME to both the set and show command LISTs (the
652 list for set/show or some sublist thereof). CLASS is as in
653 add_cmd. VAR is address of the variable which will contain the
654 value. SET_DOC and SHOW_DOC are the documentation strings. This
655 function is only used in Python API. Please don't use it elsewhere. */
657 add_setshow_integer_cmd (const char *name
, enum command_class
class,
659 const char *set_doc
, const char *show_doc
,
660 const char *help_doc
,
661 cmd_sfunc_ftype
*set_func
,
662 show_value_ftype
*show_func
,
663 struct cmd_list_element
**set_list
,
664 struct cmd_list_element
**show_list
)
666 struct cmd_list_element
*set
;
668 add_setshow_cmd_full (name
, class, var_integer
, var
,
669 set_doc
, show_doc
, help_doc
,
674 set_cmd_completer (set
, integer_unlimited_completer
);
677 /* Add element named NAME to both the set and show command LISTs (the
678 list for set/show or some sublist thereof). CLASS is as in
679 add_cmd. VAR is address of the variable which will contain the
680 value. SET_DOC and SHOW_DOC are the documentation strings. */
682 add_setshow_uinteger_cmd (const char *name
, enum command_class
class,
684 const char *set_doc
, const char *show_doc
,
685 const char *help_doc
,
686 cmd_sfunc_ftype
*set_func
,
687 show_value_ftype
*show_func
,
688 struct cmd_list_element
**set_list
,
689 struct cmd_list_element
**show_list
)
691 struct cmd_list_element
*set
;
693 add_setshow_cmd_full (name
, class, var_uinteger
, var
,
694 set_doc
, show_doc
, help_doc
,
699 set_cmd_completer (set
, integer_unlimited_completer
);
702 /* Add element named NAME to both the set and show command LISTs (the
703 list for set/show or some sublist thereof). CLASS is as in
704 add_cmd. VAR is address of the variable which will contain the
705 value. SET_DOC and SHOW_DOC are the documentation strings. */
707 add_setshow_zinteger_cmd (const char *name
, enum command_class
class,
709 const char *set_doc
, const char *show_doc
,
710 const char *help_doc
,
711 cmd_sfunc_ftype
*set_func
,
712 show_value_ftype
*show_func
,
713 struct cmd_list_element
**set_list
,
714 struct cmd_list_element
**show_list
)
716 add_setshow_cmd_full (name
, class, var_zinteger
, var
,
717 set_doc
, show_doc
, help_doc
,
724 add_setshow_zuinteger_unlimited_cmd (const char *name
,
725 enum command_class
class,
728 const char *show_doc
,
729 const char *help_doc
,
730 cmd_sfunc_ftype
*set_func
,
731 show_value_ftype
*show_func
,
732 struct cmd_list_element
**set_list
,
733 struct cmd_list_element
**show_list
)
735 struct cmd_list_element
*set
;
737 add_setshow_cmd_full (name
, class, var_zuinteger_unlimited
, var
,
738 set_doc
, show_doc
, help_doc
,
743 set_cmd_completer (set
, integer_unlimited_completer
);
746 /* Add element named NAME to both the set and show command LISTs (the
747 list for set/show or some sublist thereof). CLASS is as in
748 add_cmd. VAR is address of the variable which will contain the
749 value. SET_DOC and SHOW_DOC are the documentation strings. */
751 add_setshow_zuinteger_cmd (const char *name
, enum command_class
class,
753 const char *set_doc
, const char *show_doc
,
754 const char *help_doc
,
755 cmd_sfunc_ftype
*set_func
,
756 show_value_ftype
*show_func
,
757 struct cmd_list_element
**set_list
,
758 struct cmd_list_element
**show_list
)
760 add_setshow_cmd_full (name
, class, var_zuinteger
, var
,
761 set_doc
, show_doc
, help_doc
,
767 /* Remove the command named NAME from the command list. Return the
768 list commands which were aliased to the deleted command. If the
769 command had no aliases, return NULL. The various *HOOKs are set to
770 the pre- and post-hook commands for the deleted command. If the
771 command does not have a hook, the corresponding out parameter is
774 static struct cmd_list_element
*
775 delete_cmd (const char *name
, struct cmd_list_element
**list
,
776 struct cmd_list_element
**prehook
,
777 struct cmd_list_element
**prehookee
,
778 struct cmd_list_element
**posthook
,
779 struct cmd_list_element
**posthookee
)
781 struct cmd_list_element
*iter
;
782 struct cmd_list_element
**previous_chain_ptr
;
783 struct cmd_list_element
*aliases
= NULL
;
789 previous_chain_ptr
= list
;
791 for (iter
= *previous_chain_ptr
; iter
; iter
= *previous_chain_ptr
)
793 if (strcmp (iter
->name
, name
) == 0)
796 iter
->destroyer (iter
, iter
->context
);
797 if (iter
->hookee_pre
)
798 iter
->hookee_pre
->hook_pre
= 0;
799 *prehook
= iter
->hook_pre
;
800 *prehookee
= iter
->hookee_pre
;
801 if (iter
->hookee_post
)
802 iter
->hookee_post
->hook_post
= 0;
803 if (iter
->doc
&& (iter
->flags
& DOC_ALLOCATED
) != 0)
805 *posthook
= iter
->hook_post
;
806 *posthookee
= iter
->hookee_post
;
808 /* Update the link. */
809 *previous_chain_ptr
= iter
->next
;
811 aliases
= iter
->aliases
;
813 /* If this command was an alias, remove it from the list of
815 if (iter
->cmd_pointer
)
817 struct cmd_list_element
**prevp
= &iter
->cmd_pointer
->aliases
;
818 struct cmd_list_element
*a
= *prevp
;
822 prevp
= &a
->alias_chain
;
825 *prevp
= iter
->alias_chain
;
830 /* We won't see another command with the same name. */
834 previous_chain_ptr
= &iter
->next
;
840 /* Shorthands to the commands above. */
842 /* Add an element to the list of info subcommands. */
844 struct cmd_list_element
*
845 add_info (const char *name
, void (*fun
) (char *, int), char *doc
)
847 return add_cmd (name
, no_class
, fun
, doc
, &infolist
);
850 /* Add an alias to the list of info subcommands. */
852 struct cmd_list_element
*
853 add_info_alias (const char *name
, char *oldname
, int abbrev_flag
)
855 return add_alias_cmd (name
, oldname
, 0, abbrev_flag
, &infolist
);
858 /* Add an element to the list of commands. */
860 struct cmd_list_element
*
861 add_com (const char *name
, enum command_class
class, void (*fun
) (char *, int),
864 return add_cmd (name
, class, fun
, doc
, &cmdlist
);
867 /* Add an alias or abbreviation command to the list of commands. */
869 struct cmd_list_element
*
870 add_com_alias (const char *name
, const char *oldname
, enum command_class
class,
873 return add_alias_cmd (name
, oldname
, class, abbrev_flag
, &cmdlist
);
876 /* Recursively walk the commandlist structures, and print out the
877 documentation of commands that match our regex in either their
878 name, or their documentation.
881 apropos_cmd (struct ui_file
*stream
,
882 struct cmd_list_element
*commandlist
,
883 struct re_pattern_buffer
*regex
, char *prefix
)
885 struct cmd_list_element
*c
;
888 /* Walk through the commands. */
889 for (c
=commandlist
;c
;c
=c
->next
)
891 returnvalue
= -1; /* Needed to avoid double printing. */
894 /* Try to match against the name. */
895 returnvalue
= re_search (regex
, c
->name
, strlen(c
->name
),
896 0, strlen (c
->name
), NULL
);
897 if (returnvalue
>= 0)
899 print_help_for_command (c
, prefix
,
900 0 /* don't recurse */, stream
);
903 if (c
->doc
!= NULL
&& returnvalue
< 0)
905 /* Try to match against documentation. */
906 if (re_search(regex
,c
->doc
,strlen(c
->doc
),0,strlen(c
->doc
),NULL
) >=0)
908 print_help_for_command (c
, prefix
,
909 0 /* don't recurse */, stream
);
912 /* Check if this command has subcommands and is not an
913 abbreviation. We skip listing subcommands of abbreviations
914 in order to avoid duplicates in the output. */
915 if (c
->prefixlist
!= NULL
&& !c
->abbrev_flag
)
917 /* Recursively call ourselves on the subcommand list,
918 passing the right prefix in. */
919 apropos_cmd (stream
,*c
->prefixlist
,regex
,c
->prefixname
);
924 /* This command really has to deal with two things:
925 1) I want documentation on *this string* (usually called by
928 2) I want documentation on *this list* (usually called by giving a
929 command that requires subcommands. Also called by saying just
932 I am going to split this into two seperate comamnds, help_cmd and
936 help_cmd (char *arg
, struct ui_file
*stream
)
938 struct cmd_list_element
*c
;
939 const char *command
= arg
;
943 help_list (cmdlist
, "", all_classes
, stream
);
947 if (strcmp (command
, "all") == 0)
953 c
= lookup_cmd (&command
, cmdlist
, "", 0, 0);
958 /* There are three cases here.
959 If c->prefixlist is nonzero, we have a prefix command.
960 Print its documentation, then list its subcommands.
962 If c->func is non NULL, we really have a command. Print its
963 documentation and return.
965 If c->func is NULL, we have a class name. Print its
966 documentation (as if it were a command) and then set class to the
967 number of this class so that the commands in the class will be
970 fputs_filtered (c
->doc
, stream
);
971 fputs_filtered ("\n", stream
);
973 if (c
->prefixlist
== 0 && c
->func
!= NULL
)
975 fprintf_filtered (stream
, "\n");
977 /* If this is a prefix command, print it's subcommands. */
979 help_list (*c
->prefixlist
, c
->prefixname
, all_commands
, stream
);
981 /* If this is a class name, print all of the commands in the class. */
983 help_list (cmdlist
, "", c
->class, stream
);
985 if (c
->hook_pre
|| c
->hook_post
)
986 fprintf_filtered (stream
,
987 "\nThis command has a hook (or hooks) defined:\n");
990 fprintf_filtered (stream
,
991 "\tThis command is run after : %s (pre hook)\n",
994 fprintf_filtered (stream
,
995 "\tThis command is run before : %s (post hook)\n",
1000 * Get a specific kind of help on a command list.
1003 * CMDTYPE is the prefix to use in the title string.
1004 * CLASS is the class with which to list the nodes of this list (see
1005 * documentation for help_cmd_list below), As usual, ALL_COMMANDS for
1006 * everything, ALL_CLASSES for just classes, and non-negative for only things
1007 * in a specific class.
1008 * and STREAM is the output stream on which to print things.
1009 * If you call this routine with a class >= 0, it recurses.
1012 help_list (struct cmd_list_element
*list
, char *cmdtype
,
1013 enum command_class
class, struct ui_file
*stream
)
1016 char *cmdtype1
, *cmdtype2
;
1018 /* If CMDTYPE is "foo ", CMDTYPE1 gets " foo" and CMDTYPE2 gets "foo sub".
1020 len
= strlen (cmdtype
);
1021 cmdtype1
= (char *) alloca (len
+ 1);
1023 cmdtype2
= (char *) alloca (len
+ 4);
1028 strncpy (cmdtype1
+ 1, cmdtype
, len
- 1);
1030 strncpy (cmdtype2
, cmdtype
, len
- 1);
1031 strcpy (cmdtype2
+ len
- 1, " sub");
1034 if (class == all_classes
)
1035 fprintf_filtered (stream
, "List of classes of %scommands:\n\n", cmdtype2
);
1037 fprintf_filtered (stream
, "List of %scommands:\n\n", cmdtype2
);
1039 help_cmd_list (list
, class, cmdtype
, (int) class >= 0, stream
);
1041 if (class == all_classes
)
1043 fprintf_filtered (stream
, "\n\
1044 Type \"help%s\" followed by a class name for a list of commands in ",
1047 fprintf_filtered (stream
, "that class.");
1049 fprintf_filtered (stream
, "\n\
1050 Type \"help all\" for the list of all commands.");
1053 fprintf_filtered (stream
, "\nType \"help%s\" followed by %scommand name ",
1054 cmdtype1
, cmdtype2
);
1056 fputs_filtered ("for ", stream
);
1058 fputs_filtered ("full ", stream
);
1060 fputs_filtered ("documentation.\n", stream
);
1061 fputs_filtered ("Type \"apropos word\" to search "
1062 "for commands related to \"word\".\n", stream
);
1063 fputs_filtered ("Command name abbreviations are allowed if unambiguous.\n",
1068 help_all (struct ui_file
*stream
)
1070 struct cmd_list_element
*c
;
1071 int seen_unclassified
= 0;
1073 for (c
= cmdlist
; c
; c
= c
->next
)
1077 /* If this is a class name, print all of the commands in the
1080 if (c
->func
== NULL
)
1082 fprintf_filtered (stream
, "\nCommand class: %s\n\n", c
->name
);
1083 help_cmd_list (cmdlist
, c
->class, "", 1, stream
);
1087 /* While it's expected that all commands are in some class,
1088 as a safety measure, we'll print commands outside of any
1089 class at the end. */
1091 for (c
= cmdlist
; c
; c
= c
->next
)
1096 if (c
->class == no_class
)
1098 if (!seen_unclassified
)
1100 fprintf_filtered (stream
, "\nUnclassified commands\n\n");
1101 seen_unclassified
= 1;
1103 print_help_for_command (c
, "", 1, stream
);
1109 /* Print only the first line of STR on STREAM. */
1111 print_doc_line (struct ui_file
*stream
, char *str
)
1113 static char *line_buffer
= 0;
1114 static int line_size
;
1120 line_buffer
= (char *) xmalloc (line_size
);
1123 /* Keep printing '.' or ',' not followed by a whitespace for embedded strings
1126 while (*p
&& *p
!= '\n'
1127 && ((*p
!= '.' && *p
!= ',') || (p
[1] && !isspace (p
[1]))))
1129 if (p
- str
> line_size
- 1)
1131 line_size
= p
- str
+ 1;
1132 xfree (line_buffer
);
1133 line_buffer
= (char *) xmalloc (line_size
);
1135 strncpy (line_buffer
, str
, p
- str
);
1136 line_buffer
[p
- str
] = '\0';
1137 if (islower (line_buffer
[0]))
1138 line_buffer
[0] = toupper (line_buffer
[0]);
1139 fputs_filtered (line_buffer
, stream
);
1142 /* Print one-line help for command C.
1143 If RECURSE is non-zero, also print one-line descriptions
1144 of all prefixed subcommands. */
1146 print_help_for_command (struct cmd_list_element
*c
, char *prefix
, int recurse
,
1147 struct ui_file
*stream
)
1149 fprintf_filtered (stream
, "%s%s -- ", prefix
, c
->name
);
1150 print_doc_line (stream
, c
->doc
);
1151 fputs_filtered ("\n", stream
);
1154 && c
->prefixlist
!= 0
1155 && c
->abbrev_flag
== 0)
1156 /* Subcommands of a prefix command typically have 'all_commands'
1157 as class. If we pass CLASS to recursive invocation,
1158 most often we won't see anything. */
1159 help_cmd_list (*c
->prefixlist
, all_commands
, c
->prefixname
, 1, stream
);
1163 * Implement a help command on command list LIST.
1164 * RECURSE should be non-zero if this should be done recursively on
1165 * all sublists of LIST.
1166 * PREFIX is the prefix to print before each command name.
1167 * STREAM is the stream upon which the output should be written.
1169 * A non-negative class number to list only commands in that
1171 * ALL_COMMANDS to list all commands in list.
1172 * ALL_CLASSES to list all classes in list.
1174 * Note that RECURSE will be active on *all* sublists, not just the
1175 * ones selected by the criteria above (ie. the selection mechanism
1176 * is at the low level, not the high-level).
1179 help_cmd_list (struct cmd_list_element
*list
, enum command_class
class,
1180 char *prefix
, int recurse
, struct ui_file
*stream
)
1182 struct cmd_list_element
*c
;
1184 for (c
= list
; c
; c
= c
->next
)
1186 if (c
->abbrev_flag
== 0
1187 && (class == all_commands
1188 || (class == all_classes
&& c
->func
== NULL
)
1189 || (class == c
->class && c
->func
!= NULL
)))
1191 print_help_for_command (c
, prefix
, recurse
, stream
);
1193 else if (c
->abbrev_flag
== 0 && recurse
1194 && class == class_user
&& c
->prefixlist
!= NULL
)
1195 /* User-defined commands may be subcommands. */
1196 help_cmd_list (*c
->prefixlist
, class, c
->prefixname
,
1202 /* Search the input clist for 'command'. Return the command if
1203 found (or NULL if not), and return the number of commands
1206 static struct cmd_list_element
*
1207 find_cmd (const char *command
, int len
, struct cmd_list_element
*clist
,
1208 int ignore_help_classes
, int *nfound
)
1210 struct cmd_list_element
*found
, *c
;
1212 found
= (struct cmd_list_element
*) NULL
;
1214 for (c
= clist
; c
; c
= c
->next
)
1215 if (!strncmp (command
, c
->name
, len
)
1216 && (!ignore_help_classes
|| c
->func
))
1220 if (c
->name
[len
] == '\0')
1230 find_command_name_length (const char *text
)
1232 const char *p
= text
;
1234 /* Treating underscores as part of command words is important
1235 so that "set args_foo()" doesn't get interpreted as
1236 "set args _foo()". */
1237 /* Some characters are only used for TUI specific commands.
1238 However, they are always allowed for the sake of consistency.
1240 The XDB compatibility characters are only allowed when using the
1241 right mode because they clash with other GDB commands -
1242 specifically '/' is used as a suffix for print, examine and
1245 Note that this is larger than the character set allowed when
1246 creating user-defined commands. */
1248 /* Recognize '!' as a single character command so that, e.g., "!ls"
1249 works as expected. */
1253 while (isalnum (*p
) || *p
== '-' || *p
== '_'
1254 /* Characters used by TUI specific commands. */
1255 || *p
== '+' || *p
== '<' || *p
== '>' || *p
== '$'
1256 /* Characters used for XDB compatibility. */
1257 || (xdb_commands
&& (*p
== '/' || *p
== '?')))
1263 /* Return TRUE if NAME is a valid user-defined command name.
1264 This is a stricter subset of all gdb commands,
1265 see find_command_name_length. */
1268 valid_user_defined_cmd_name_p (const char *name
)
1275 /* Alas "42" is a legitimate user-defined command.
1276 In the interests of not breaking anything we preserve that. */
1278 for (p
= name
; *p
!= '\0'; ++p
)
1291 /* This routine takes a line of TEXT and a CLIST in which to start the
1292 lookup. When it returns it will have incremented the text pointer past
1293 the section of text it matched, set *RESULT_LIST to point to the list in
1294 which the last word was matched, and will return a pointer to the cmd
1295 list element which the text matches. It will return NULL if no match at
1296 all was possible. It will return -1 (cast appropriately, ick) if ambigous
1297 matches are possible; in this case *RESULT_LIST will be set to point to
1298 the list in which there are ambiguous choices (and *TEXT will be set to
1299 the ambiguous text string).
1301 If the located command was an abbreviation, this routine returns the base
1302 command of the abbreviation.
1304 It does no error reporting whatsoever; control will always return
1305 to the superior routine.
1307 In the case of an ambiguous return (-1), *RESULT_LIST will be set to point
1308 at the prefix_command (ie. the best match) *or* (special case) will be NULL
1309 if no prefix command was ever found. For example, in the case of "info a",
1310 "info" matches without ambiguity, but "a" could be "args" or "address", so
1311 *RESULT_LIST is set to the cmd_list_element for "info". So in this case
1312 RESULT_LIST should not be interpeted as a pointer to the beginning of a
1313 list; it simply points to a specific command. In the case of an ambiguous
1314 return *TEXT is advanced past the last non-ambiguous prefix (e.g.
1315 "info t" can be "info types" or "info target"; upon return *TEXT has been
1316 advanced past "info ").
1318 If RESULT_LIST is NULL, don't set *RESULT_LIST (but don't otherwise
1319 affect the operation).
1321 This routine does *not* modify the text pointed to by TEXT.
1323 If IGNORE_HELP_CLASSES is nonzero, ignore any command list elements which
1324 are actually help classes rather than commands (i.e. the function field of
1325 the struct cmd_list_element is NULL). */
1327 struct cmd_list_element
*
1328 lookup_cmd_1 (const char **text
, struct cmd_list_element
*clist
,
1329 struct cmd_list_element
**result_list
, int ignore_help_classes
)
1332 int len
, tmp
, nfound
;
1333 struct cmd_list_element
*found
, *c
;
1334 const char *line
= *text
;
1336 while (**text
== ' ' || **text
== '\t')
1339 /* Identify the name of the command. */
1340 len
= find_command_name_length (*text
);
1342 /* If nothing but whitespace, return 0. */
1346 /* *text and p now bracket the first command word to lookup (and
1347 it's length is len). We copy this into a local temporary. */
1350 command
= (char *) alloca (len
+ 1);
1351 memcpy (command
, *text
, len
);
1352 command
[len
] = '\0';
1357 found
= find_cmd (command
, len
, clist
, ignore_help_classes
, &nfound
);
1359 /* We didn't find the command in the entered case, so lower case it
1360 and search again. */
1361 if (!found
|| nfound
== 0)
1363 for (tmp
= 0; tmp
< len
; tmp
++)
1365 char x
= command
[tmp
];
1367 command
[tmp
] = isupper (x
) ? tolower (x
) : x
;
1369 found
= find_cmd (command
, len
, clist
, ignore_help_classes
, &nfound
);
1372 /* If nothing matches, we have a simple failure. */
1378 if (result_list
!= NULL
)
1379 /* Will be modified in calling routine
1380 if we know what the prefix command is. */
1382 return CMD_LIST_AMBIGUOUS
; /* Ambiguous. */
1385 /* We've matched something on this list. Move text pointer forward. */
1389 if (found
->cmd_pointer
)
1391 /* We drop the alias (abbreviation) in favor of the command it
1392 is pointing to. If the alias is deprecated, though, we need to
1393 warn the user about it before we drop it. Note that while we
1394 are warning about the alias, we may also warn about the command
1395 itself and we will adjust the appropriate DEPRECATED_WARN_USER
1398 if (found
->flags
& DEPRECATED_WARN_USER
)
1399 deprecated_cmd_warning (line
);
1400 found
= found
->cmd_pointer
;
1402 /* If we found a prefix command, keep looking. */
1404 if (found
->prefixlist
)
1406 c
= lookup_cmd_1 (text
, *found
->prefixlist
, result_list
,
1407 ignore_help_classes
);
1410 /* Didn't find anything; this is as far as we got. */
1411 if (result_list
!= NULL
)
1412 *result_list
= clist
;
1415 else if (c
== CMD_LIST_AMBIGUOUS
)
1417 /* We've gotten this far properly, but the next step is
1418 ambiguous. We need to set the result list to the best
1419 we've found (if an inferior hasn't already set it). */
1420 if (result_list
!= NULL
)
1422 /* This used to say *result_list = *found->prefixlist.
1423 If that was correct, need to modify the documentation
1424 at the top of this function to clarify what is
1425 supposed to be going on. */
1426 *result_list
= found
;
1437 if (result_list
!= NULL
)
1438 *result_list
= clist
;
1443 /* All this hair to move the space to the front of cmdtype */
1446 undef_cmd_error (const char *cmdtype
, const char *q
)
1448 error (_("Undefined %scommand: \"%s\". Try \"help%s%.*s\"."),
1451 *cmdtype
? " " : "",
1452 (int) strlen (cmdtype
) - 1,
1456 /* Look up the contents of *LINE as a command in the command list LIST.
1457 LIST is a chain of struct cmd_list_element's.
1458 If it is found, return the struct cmd_list_element for that command
1459 and update *LINE to point after the command name, at the first argument.
1460 If not found, call error if ALLOW_UNKNOWN is zero
1461 otherwise (or if error returns) return zero.
1462 Call error if specified command is ambiguous,
1463 unless ALLOW_UNKNOWN is negative.
1464 CMDTYPE precedes the word "command" in the error message.
1466 If INGNORE_HELP_CLASSES is nonzero, ignore any command list
1467 elements which are actually help classes rather than commands (i.e.
1468 the function field of the struct cmd_list_element is 0). */
1470 struct cmd_list_element
*
1471 lookup_cmd (const char **line
, struct cmd_list_element
*list
, char *cmdtype
,
1472 int allow_unknown
, int ignore_help_classes
)
1474 struct cmd_list_element
*last_list
= 0;
1475 struct cmd_list_element
*c
;
1477 /* Note: Do not remove trailing whitespace here because this
1478 would be wrong for complete_command. Jim Kingdon */
1481 error (_("Lack of needed %scommand"), cmdtype
);
1483 c
= lookup_cmd_1 (line
, list
, &last_list
, ignore_help_classes
);
1490 int len
= find_command_name_length (*line
);
1492 q
= (char *) alloca (len
+ 1);
1493 strncpy (q
, *line
, len
);
1495 undef_cmd_error (cmdtype
, q
);
1500 else if (c
== CMD_LIST_AMBIGUOUS
)
1502 /* Ambigous. Local values should be off prefixlist or called
1504 int local_allow_unknown
= (last_list
? last_list
->allow_unknown
:
1506 char *local_cmdtype
= last_list
? last_list
->prefixname
: cmdtype
;
1507 struct cmd_list_element
*local_list
=
1508 (last_list
? *(last_list
->prefixlist
) : list
);
1510 if (local_allow_unknown
< 0)
1513 return last_list
; /* Found something. */
1515 return 0; /* Found nothing. */
1519 /* Report as error. */
1524 ((*line
)[amb_len
] && (*line
)[amb_len
] != ' '
1525 && (*line
)[amb_len
] != '\t');
1530 for (c
= local_list
; c
; c
= c
->next
)
1531 if (!strncmp (*line
, c
->name
, amb_len
))
1533 if (strlen (ambbuf
) + strlen (c
->name
) + 6
1534 < (int) sizeof ambbuf
)
1536 if (strlen (ambbuf
))
1537 strcat (ambbuf
, ", ");
1538 strcat (ambbuf
, c
->name
);
1542 strcat (ambbuf
, "..");
1546 error (_("Ambiguous %scommand \"%s\": %s."), local_cmdtype
,
1548 return 0; /* lint */
1553 if (c
->type
== set_cmd
&& **line
!= '\0' && !isspace (**line
))
1554 error (_("Argument must be preceded by space."));
1556 /* We've got something. It may still not be what the caller
1557 wants (if this command *needs* a subcommand). */
1558 while (**line
== ' ' || **line
== '\t')
1561 if (c
->prefixlist
&& **line
&& !c
->allow_unknown
)
1562 undef_cmd_error (c
->prefixname
, *line
);
1564 /* Seems to be what he wants. Return it. */
1570 /* We are here presumably because an alias or command in TEXT is
1571 deprecated and a warning message should be generated. This
1572 function decodes TEXT and potentially generates a warning message
1575 Example for 'set endian big' which has a fictitious alias 'seb'.
1577 If alias wasn't used in TEXT, and the command is deprecated:
1578 "warning: 'set endian big' is deprecated."
1580 If alias was used, and only the alias is deprecated:
1581 "warning: 'seb' an alias for the command 'set endian big' is deprecated."
1583 If alias was used and command is deprecated (regardless of whether
1584 the alias itself is deprecated:
1586 "warning: 'set endian big' (seb) is deprecated."
1588 After the message has been sent, clear the appropriate flags in the
1589 command and/or the alias so the user is no longer bothered.
1593 deprecated_cmd_warning (const char *text
)
1595 struct cmd_list_element
*alias
= NULL
;
1596 struct cmd_list_element
*prefix_cmd
= NULL
;
1597 struct cmd_list_element
*cmd
= NULL
;
1599 if (!lookup_cmd_composition (text
, &alias
, &prefix_cmd
, &cmd
))
1600 /* Return if text doesn't evaluate to a command. */
1603 if (!((alias
? (alias
->flags
& DEPRECATED_WARN_USER
) : 0)
1604 || (cmd
->flags
& DEPRECATED_WARN_USER
) ) )
1605 /* Return if nothing is deprecated. */
1608 printf_filtered ("Warning:");
1610 if (alias
&& !(cmd
->flags
& CMD_DEPRECATED
))
1611 printf_filtered (" '%s', an alias for the", alias
->name
);
1613 printf_filtered (" command '");
1616 printf_filtered ("%s", prefix_cmd
->prefixname
);
1618 printf_filtered ("%s", cmd
->name
);
1620 if (alias
&& (cmd
->flags
& CMD_DEPRECATED
))
1621 printf_filtered ("' (%s) is deprecated.\n", alias
->name
);
1623 printf_filtered ("' is deprecated.\n");
1626 /* If it is only the alias that is deprecated, we want to indicate
1627 the new alias, otherwise we'll indicate the new command. */
1629 if (alias
&& !(cmd
->flags
& CMD_DEPRECATED
))
1631 if (alias
->replacement
)
1632 printf_filtered ("Use '%s'.\n\n", alias
->replacement
);
1634 printf_filtered ("No alternative known.\n\n");
1638 if (cmd
->replacement
)
1639 printf_filtered ("Use '%s'.\n\n", cmd
->replacement
);
1641 printf_filtered ("No alternative known.\n\n");
1644 /* We've warned you, now we'll keep quiet. */
1646 alias
->flags
&= ~DEPRECATED_WARN_USER
;
1648 cmd
->flags
&= ~DEPRECATED_WARN_USER
;
1652 /* Look up the contents of LINE as a command in the command list 'cmdlist'.
1653 Return 1 on success, 0 on failure.
1655 If LINE refers to an alias, *alias will point to that alias.
1657 If LINE is a postfix command (i.e. one that is preceded by a prefix
1658 command) set *prefix_cmd.
1660 Set *cmd to point to the command LINE indicates.
1662 If any of *alias, *prefix_cmd, or *cmd cannot be determined or do not
1663 exist, they are NULL when we return.
1667 lookup_cmd_composition (const char *text
,
1668 struct cmd_list_element
**alias
,
1669 struct cmd_list_element
**prefix_cmd
,
1670 struct cmd_list_element
**cmd
)
1673 int len
, tmp
, nfound
;
1674 struct cmd_list_element
*cur_list
;
1675 struct cmd_list_element
*prev_cmd
;
1685 /* Go through as many command lists as we need to,
1686 to find the command TEXT refers to. */
1690 while (*text
== ' ' || *text
== '\t')
1693 /* Identify the name of the command. */
1694 len
= find_command_name_length (text
);
1696 /* If nothing but whitespace, return. */
1700 /* Text is the start of the first command word to lookup (and
1701 it's length is len). We copy this into a local temporary. */
1703 command
= (char *) alloca (len
+ 1);
1704 memcpy (command
, text
, len
);
1705 command
[len
] = '\0';
1710 *cmd
= find_cmd (command
, len
, cur_list
, 1, &nfound
);
1712 /* We didn't find the command in the entered case, so lower case
1713 it and search again.
1715 if (!*cmd
|| nfound
== 0)
1717 for (tmp
= 0; tmp
< len
; tmp
++)
1719 char x
= command
[tmp
];
1721 command
[tmp
] = isupper (x
) ? tolower (x
) : x
;
1723 *cmd
= find_cmd (command
, len
, cur_list
, 1, &nfound
);
1726 if (*cmd
== CMD_LIST_AMBIGUOUS
)
1728 return 0; /* ambiguous */
1732 return 0; /* nothing found */
1735 if ((*cmd
)->cmd_pointer
)
1737 /* cmd was actually an alias, we note that an alias was
1738 used (by assigning *alais) and we set *cmd. */
1740 *cmd
= (*cmd
)->cmd_pointer
;
1742 *prefix_cmd
= prev_cmd
;
1744 if ((*cmd
)->prefixlist
)
1745 cur_list
= *(*cmd
)->prefixlist
;
1753 /* Helper function for SYMBOL_COMPLETION_FUNCTION. */
1755 /* Return a vector of char pointers which point to the different
1756 possible completions in LIST of TEXT.
1758 WORD points in the same buffer as TEXT, and completions should be
1759 returned relative to this position. For example, suppose TEXT is
1760 "foo" and we want to complete to "foobar". If WORD is "oo", return
1761 "oobar"; if WORD is "baz/foo", return "baz/foobar". */
1764 complete_on_cmdlist (struct cmd_list_element
*list
,
1765 const char *text
, const char *word
,
1766 int ignore_help_classes
)
1768 struct cmd_list_element
*ptr
;
1769 VEC (char_ptr
) *matchlist
= NULL
;
1770 int textlen
= strlen (text
);
1772 int saw_deprecated_match
= 0;
1774 /* We do one or two passes. In the first pass, we skip deprecated
1775 commands. If we see no matching commands in the first pass, and
1776 if we did happen to see a matching deprecated command, we do
1777 another loop to collect those. */
1778 for (pass
= 0; matchlist
== 0 && pass
< 2; ++pass
)
1780 for (ptr
= list
; ptr
; ptr
= ptr
->next
)
1781 if (!strncmp (ptr
->name
, text
, textlen
)
1782 && !ptr
->abbrev_flag
1783 && (!ignore_help_classes
|| ptr
->func
1784 || ptr
->prefixlist
))
1790 if ((ptr
->flags
& CMD_DEPRECATED
) != 0)
1792 saw_deprecated_match
= 1;
1797 match
= (char *) xmalloc (strlen (word
) + strlen (ptr
->name
) + 1);
1799 strcpy (match
, ptr
->name
);
1800 else if (word
> text
)
1802 /* Return some portion of ptr->name. */
1803 strcpy (match
, ptr
->name
+ (word
- text
));
1807 /* Return some of text plus ptr->name. */
1808 strncpy (match
, word
, text
- word
);
1809 match
[text
- word
] = '\0';
1810 strcat (match
, ptr
->name
);
1812 VEC_safe_push (char_ptr
, matchlist
, match
);
1814 /* If we saw no matching deprecated commands in the first pass,
1816 if (!saw_deprecated_match
)
1823 /* Helper function for SYMBOL_COMPLETION_FUNCTION. */
1825 /* Return a vector of char pointers which point to the different
1826 possible completions in CMD of TEXT.
1828 WORD points in the same buffer as TEXT, and completions should be
1829 returned relative to this position. For example, suppose TEXT is "foo"
1830 and we want to complete to "foobar". If WORD is "oo", return
1831 "oobar"; if WORD is "baz/foo", return "baz/foobar". */
1834 complete_on_enum (const char *const *enumlist
,
1835 const char *text
, const char *word
)
1837 VEC (char_ptr
) *matchlist
= NULL
;
1838 int textlen
= strlen (text
);
1842 for (i
= 0; (name
= enumlist
[i
]) != NULL
; i
++)
1843 if (strncmp (name
, text
, textlen
) == 0)
1847 match
= (char *) xmalloc (strlen (word
) + strlen (name
) + 1);
1849 strcpy (match
, name
);
1850 else if (word
> text
)
1852 /* Return some portion of name. */
1853 strcpy (match
, name
+ (word
- text
));
1857 /* Return some of text plus name. */
1858 strncpy (match
, word
, text
- word
);
1859 match
[text
- word
] = '\0';
1860 strcat (match
, name
);
1862 VEC_safe_push (char_ptr
, matchlist
, match
);
1869 /* Check function pointer. */
1871 cmd_func_p (struct cmd_list_element
*cmd
)
1873 return (cmd
->func
!= NULL
);
1877 /* Call the command function. */
1879 cmd_func (struct cmd_list_element
*cmd
, char *args
, int from_tty
)
1881 if (cmd_func_p (cmd
))
1882 (*cmd
->func
) (cmd
, args
, from_tty
);
1884 error (_("Invalid command"));