1 /* Handle lists of commands, their decoding and documentation, for GDB.
3 Copyright (c) 1986, 1989, 1990, 1991, 1998, 2000, 2001, 2002, 2004, 2007
4 Free Software Foundation, Inc.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street, Fifth Floor,
19 Boston, MA 02110-1301, USA. */
24 #include "gdb_regex.h"
25 #include "gdb_string.h"
26 #include "completer.h"
29 #include "cli/cli-cmds.h"
30 #include "cli/cli-decode.h"
33 #include "tui/tui.h" /* For tui_active et.al. */
36 #include "gdb_assert.h"
38 /* Prototypes for local functions */
40 static void undef_cmd_error (char *, char *);
42 static struct cmd_list_element
*find_cmd (char *command
,
44 struct cmd_list_element
*clist
,
45 int ignore_help_classes
,
48 static void help_all (struct ui_file
*stream
);
51 print_help_for_command (struct cmd_list_element
*c
, char *prefix
, int recurse
,
52 struct ui_file
*stream
);
55 /* Set the callback function for the specified command. For each both
56 the commands callback and func() are set. The latter set to a
57 bounce function (unless cfunc / sfunc is NULL that is). */
60 do_cfunc (struct cmd_list_element
*c
, char *args
, int from_tty
)
62 c
->function
.cfunc (args
, from_tty
); /* Ok. */
66 set_cmd_cfunc (struct cmd_list_element
*cmd
, cmd_cfunc_ftype
*cfunc
)
72 cmd
->function
.cfunc
= cfunc
; /* Ok. */
76 do_sfunc (struct cmd_list_element
*c
, char *args
, int from_tty
)
78 c
->function
.sfunc (args
, from_tty
, c
); /* Ok. */
82 set_cmd_sfunc (struct cmd_list_element
*cmd
, cmd_sfunc_ftype
*sfunc
)
88 cmd
->function
.sfunc
= sfunc
; /* Ok. */
92 cmd_cfunc_eq (struct cmd_list_element
*cmd
,
93 void (*cfunc
) (char *args
, int from_tty
))
95 return cmd
->func
== do_cfunc
&& cmd
->function
.cfunc
== cfunc
;
99 set_cmd_context (struct cmd_list_element
*cmd
, void *context
)
101 cmd
->context
= context
;
105 get_cmd_context (struct cmd_list_element
*cmd
)
111 cmd_type (struct cmd_list_element
*cmd
)
117 set_cmd_completer (struct cmd_list_element
*cmd
,
118 char **(*completer
) (char *text
, char *word
))
120 cmd
->completer
= completer
; /* Ok. */
124 /* Add element named NAME.
125 CLASS is the top level category into which commands are broken down
127 FUN should be the function to execute the command;
128 it will get a character string as argument, with leading
129 and trailing blanks already eliminated.
131 DOC is a documentation string for the command.
132 Its first line should be a complete sentence.
133 It should start with ? for a command that is an abbreviation
134 or with * for a command that most users don't need to know about.
136 Add this command to command list *LIST.
138 Returns a pointer to the added command (not necessarily the head
141 struct cmd_list_element
*
142 add_cmd (char *name
, enum command_class
class, void (*fun
) (char *, int),
143 char *doc
, struct cmd_list_element
**list
)
145 struct cmd_list_element
*c
146 = (struct cmd_list_element
*) xmalloc (sizeof (struct cmd_list_element
));
147 struct cmd_list_element
*p
;
149 delete_cmd (name
, list
);
151 if (*list
== NULL
|| strcmp ((*list
)->name
, name
) >= 0)
159 while (p
->next
&& strcmp (p
->next
->name
, name
) <= 0)
169 set_cmd_cfunc (c
, fun
);
170 set_cmd_context (c
, NULL
);
173 c
->replacement
= NULL
;
174 c
->pre_show_hook
= NULL
;
178 c
->prefixlist
= NULL
;
179 c
->prefixname
= NULL
;
180 c
->allow_unknown
= 0;
182 set_cmd_completer (c
, make_symbol_completion_list
);
183 c
->type
= not_set_cmd
;
185 c
->var_type
= var_boolean
;
187 c
->user_commands
= NULL
;
188 c
->hookee_pre
= NULL
;
189 c
->hookee_post
= NULL
;
190 c
->cmd_pointer
= NULL
;
195 /* Deprecates a command CMD.
196 REPLACEMENT is the name of the command which should be used in place
197 of this command, or NULL if no such command exists.
199 This function does not check to see if command REPLACEMENT exists
200 since gdb may not have gotten around to adding REPLACEMENT when this
203 Returns a pointer to the deprecated command. */
205 struct cmd_list_element
*
206 deprecate_cmd (struct cmd_list_element
*cmd
, char *replacement
)
208 cmd
->flags
|= (CMD_DEPRECATED
| DEPRECATED_WARN_USER
);
210 if (replacement
!= NULL
)
211 cmd
->replacement
= replacement
;
213 cmd
->replacement
= NULL
;
218 struct cmd_list_element
*
219 add_alias_cmd (char *name
, char *oldname
, enum command_class
class,
220 int abbrev_flag
, struct cmd_list_element
**list
)
222 /* Must do this since lookup_cmd tries to side-effect its first arg */
224 struct cmd_list_element
*old
;
225 struct cmd_list_element
*c
;
226 copied_name
= (char *) alloca (strlen (oldname
) + 1);
227 strcpy (copied_name
, oldname
);
228 old
= lookup_cmd (&copied_name
, *list
, "", 1, 1);
232 delete_cmd (name
, list
);
236 c
= add_cmd (name
, class, NULL
, old
->doc
, list
);
237 /* NOTE: Both FUNC and all the FUNCTIONs need to be copied. */
239 c
->function
= old
->function
;
240 c
->prefixlist
= old
->prefixlist
;
241 c
->prefixname
= old
->prefixname
;
242 c
->allow_unknown
= old
->allow_unknown
;
243 c
->abbrev_flag
= abbrev_flag
;
244 c
->cmd_pointer
= old
;
248 /* Like add_cmd but adds an element for a command prefix:
249 a name that should be followed by a subcommand to be looked up
250 in another command list. PREFIXLIST should be the address
251 of the variable containing that list. */
253 struct cmd_list_element
*
254 add_prefix_cmd (char *name
, enum command_class
class, void (*fun
) (char *, int),
255 char *doc
, struct cmd_list_element
**prefixlist
,
256 char *prefixname
, int allow_unknown
,
257 struct cmd_list_element
**list
)
259 struct cmd_list_element
*c
= add_cmd (name
, class, fun
, doc
, list
);
260 c
->prefixlist
= prefixlist
;
261 c
->prefixname
= prefixname
;
262 c
->allow_unknown
= allow_unknown
;
266 /* Like add_prefix_cmd but sets the abbrev_flag on the new command. */
268 struct cmd_list_element
*
269 add_abbrev_prefix_cmd (char *name
, enum command_class
class,
270 void (*fun
) (char *, int), char *doc
,
271 struct cmd_list_element
**prefixlist
, char *prefixname
,
272 int allow_unknown
, struct cmd_list_element
**list
)
274 struct cmd_list_element
*c
= add_cmd (name
, class, fun
, doc
, list
);
275 c
->prefixlist
= prefixlist
;
276 c
->prefixname
= prefixname
;
277 c
->allow_unknown
= allow_unknown
;
282 /* This is an empty "cfunc". */
284 not_just_help_class_command (char *args
, int from_tty
)
288 /* This is an empty "sfunc". */
289 static void empty_sfunc (char *, int, struct cmd_list_element
*);
292 empty_sfunc (char *args
, int from_tty
, struct cmd_list_element
*c
)
296 /* Add element named NAME to command list LIST (the list for set/show
297 or some sublist thereof).
298 TYPE is set_cmd or show_cmd.
299 CLASS is as in add_cmd.
300 VAR_TYPE is the kind of thing we are setting.
301 VAR is address of the variable being controlled by this command.
302 DOC is the documentation string. */
304 static struct cmd_list_element
*
305 add_set_or_show_cmd (char *name
,
307 enum command_class
class,
311 struct cmd_list_element
**list
)
313 struct cmd_list_element
*c
= add_cmd (name
, class, NULL
, doc
, list
);
314 gdb_assert (type
== set_cmd
|| type
== show_cmd
);
316 c
->var_type
= var_type
;
318 /* This needs to be something besides NULL so that this isn't
319 treated as a help class. */
320 set_cmd_sfunc (c
, empty_sfunc
);
324 /* Add element named NAME to both the command SET_LIST and SHOW_LIST.
325 CLASS is as in add_cmd. VAR_TYPE is the kind of thing we are
326 setting. VAR is address of the variable being controlled by this
327 command. SET_FUNC and SHOW_FUNC are the callback functions (if
328 non-NULL). SET_DOC, SHOW_DOC and HELP_DOC are the documentation
329 strings. PRINT the format string to print the value. SET_RESULT
330 and SHOW_RESULT, if not NULL, are set to the resulting command
334 add_setshow_cmd_full (char *name
,
335 enum command_class
class,
336 var_types var_type
, void *var
,
337 const char *set_doc
, const char *show_doc
,
338 const char *help_doc
,
339 cmd_sfunc_ftype
*set_func
,
340 show_value_ftype
*show_func
,
341 struct cmd_list_element
**set_list
,
342 struct cmd_list_element
**show_list
,
343 struct cmd_list_element
**set_result
,
344 struct cmd_list_element
**show_result
)
346 struct cmd_list_element
*set
;
347 struct cmd_list_element
*show
;
351 if (help_doc
!= NULL
)
353 full_set_doc
= xstrprintf ("%s\n%s", set_doc
, help_doc
);
354 full_show_doc
= xstrprintf ("%s\n%s", show_doc
, help_doc
);
358 full_set_doc
= xstrdup (set_doc
);
359 full_show_doc
= xstrdup (show_doc
);
361 set
= add_set_or_show_cmd (name
, set_cmd
, class, var_type
, var
,
362 full_set_doc
, set_list
);
363 if (set_func
!= NULL
)
364 set_cmd_sfunc (set
, set_func
);
365 show
= add_set_or_show_cmd (name
, show_cmd
, class, var_type
, var
,
366 full_show_doc
, show_list
);
367 show
->show_value_func
= show_func
;
369 if (set_result
!= NULL
)
371 if (show_result
!= NULL
)
375 struct cmd_list_element
*
376 deprecated_add_set_cmd (char *name
,
377 enum command_class
class,
381 struct cmd_list_element
**list
)
383 return add_set_or_show_cmd (name
, set_cmd
, class, var_type
, var
, doc
, list
);
386 /* Add element named NAME to command list LIST (the list for set or
387 some sublist thereof). CLASS is as in add_cmd. ENUMLIST is a list
388 of strings which may follow NAME. VAR is address of the variable
389 which will contain the matching string (from ENUMLIST). */
392 add_setshow_enum_cmd (char *name
,
393 enum command_class
class,
394 const char *enumlist
[],
397 const char *show_doc
,
398 const char *help_doc
,
399 cmd_sfunc_ftype
*set_func
,
400 show_value_ftype
*show_func
,
401 struct cmd_list_element
**set_list
,
402 struct cmd_list_element
**show_list
)
404 struct cmd_list_element
*c
;
405 add_setshow_cmd_full (name
, class, var_enum
, var
,
406 set_doc
, show_doc
, help_doc
,
413 /* Add an auto-boolean command named NAME to both the set and show
414 command list lists. CLASS is as in add_cmd. VAR is address of the
415 variable which will contain the value. DOC is the documentation
416 string. FUNC is the corresponding callback. */
418 add_setshow_auto_boolean_cmd (char *name
,
419 enum command_class
class,
420 enum auto_boolean
*var
,
421 const char *set_doc
, const char *show_doc
,
422 const char *help_doc
,
423 cmd_sfunc_ftype
*set_func
,
424 show_value_ftype
*show_func
,
425 struct cmd_list_element
**set_list
,
426 struct cmd_list_element
**show_list
)
428 static const char *auto_boolean_enums
[] = { "on", "off", "auto", NULL
};
429 struct cmd_list_element
*c
;
430 add_setshow_cmd_full (name
, class, var_auto_boolean
, var
,
431 set_doc
, show_doc
, help_doc
,
435 c
->enums
= auto_boolean_enums
;
438 /* Add element named NAME to both the set and show command LISTs (the
439 list for set/show or some sublist thereof). CLASS is as in
440 add_cmd. VAR is address of the variable which will contain the
441 value. SET_DOC and SHOW_DOC are the documentation strings. */
443 add_setshow_boolean_cmd (char *name
, enum command_class
class, int *var
,
444 const char *set_doc
, const char *show_doc
,
445 const char *help_doc
,
446 cmd_sfunc_ftype
*set_func
,
447 show_value_ftype
*show_func
,
448 struct cmd_list_element
**set_list
,
449 struct cmd_list_element
**show_list
)
451 static const char *boolean_enums
[] = { "on", "off", NULL
};
452 struct cmd_list_element
*c
;
453 add_setshow_cmd_full (name
, class, var_boolean
, var
,
454 set_doc
, show_doc
, help_doc
,
458 c
->enums
= boolean_enums
;
461 /* Add element named NAME to both the set and show command LISTs (the
462 list for set/show or some sublist thereof). */
464 add_setshow_filename_cmd (char *name
, enum command_class
class,
466 const char *set_doc
, const char *show_doc
,
467 const char *help_doc
,
468 cmd_sfunc_ftype
*set_func
,
469 show_value_ftype
*show_func
,
470 struct cmd_list_element
**set_list
,
471 struct cmd_list_element
**show_list
)
473 struct cmd_list_element
*set_result
;
474 add_setshow_cmd_full (name
, class, var_filename
, var
,
475 set_doc
, show_doc
, help_doc
,
479 set_cmd_completer (set_result
, filename_completer
);
482 /* Add element named NAME to both the set and show command LISTs (the
483 list for set/show or some sublist thereof). */
485 add_setshow_string_cmd (char *name
, enum command_class
class,
487 const char *set_doc
, const char *show_doc
,
488 const char *help_doc
,
489 cmd_sfunc_ftype
*set_func
,
490 show_value_ftype
*show_func
,
491 struct cmd_list_element
**set_list
,
492 struct cmd_list_element
**show_list
)
494 add_setshow_cmd_full (name
, class, var_string
, var
,
495 set_doc
, show_doc
, help_doc
,
501 /* Add element named NAME to both the set and show command LISTs (the
502 list for set/show or some sublist thereof). */
504 add_setshow_string_noescape_cmd (char *name
, enum command_class
class,
506 const char *set_doc
, const char *show_doc
,
507 const char *help_doc
,
508 cmd_sfunc_ftype
*set_func
,
509 show_value_ftype
*show_func
,
510 struct cmd_list_element
**set_list
,
511 struct cmd_list_element
**show_list
)
513 add_setshow_cmd_full (name
, class, var_string_noescape
, var
,
514 set_doc
, show_doc
, help_doc
,
520 /* Add element named NAME to both the set and show command LISTs (the
521 list for set/show or some sublist thereof). */
523 add_setshow_optional_filename_cmd (char *name
, enum command_class
class,
525 const char *set_doc
, const char *show_doc
,
526 const char *help_doc
,
527 cmd_sfunc_ftype
*set_func
,
528 show_value_ftype
*show_func
,
529 struct cmd_list_element
**set_list
,
530 struct cmd_list_element
**show_list
)
532 add_setshow_cmd_full (name
, class, var_optional_filename
, var
,
533 set_doc
, show_doc
, help_doc
,
539 /* Add element named NAME to both the set and show command LISTs (the
540 list for set/show or some sublist thereof). CLASS is as in
541 add_cmd. VAR is address of the variable which will contain the
542 value. SET_DOC and SHOW_DOC are the documentation strings. */
544 add_setshow_integer_cmd (char *name
, enum command_class
class,
546 const char *set_doc
, const char *show_doc
,
547 const char *help_doc
,
548 cmd_sfunc_ftype
*set_func
,
549 show_value_ftype
*show_func
,
550 struct cmd_list_element
**set_list
,
551 struct cmd_list_element
**show_list
)
553 add_setshow_cmd_full (name
, class, var_integer
, var
,
554 set_doc
, show_doc
, help_doc
,
560 /* Add element named NAME to both the set and show command LISTs (the
561 list for set/show or some sublist thereof). CLASS is as in
562 add_cmd. VAR is address of the variable which will contain the
563 value. SET_DOC and SHOW_DOC are the documentation strings. */
565 add_setshow_uinteger_cmd (char *name
, enum command_class
class,
567 const char *set_doc
, const char *show_doc
,
568 const char *help_doc
,
569 cmd_sfunc_ftype
*set_func
,
570 show_value_ftype
*show_func
,
571 struct cmd_list_element
**set_list
,
572 struct cmd_list_element
**show_list
)
574 add_setshow_cmd_full (name
, class, var_uinteger
, var
,
575 set_doc
, show_doc
, help_doc
,
581 /* Add element named NAME to both the set and show command LISTs (the
582 list for set/show or some sublist thereof). CLASS is as in
583 add_cmd. VAR is address of the variable which will contain the
584 value. SET_DOC and SHOW_DOC are the documentation strings. */
586 add_setshow_zinteger_cmd (char *name
, enum command_class
class,
588 const char *set_doc
, const char *show_doc
,
589 const char *help_doc
,
590 cmd_sfunc_ftype
*set_func
,
591 show_value_ftype
*show_func
,
592 struct cmd_list_element
**set_list
,
593 struct cmd_list_element
**show_list
)
595 add_setshow_cmd_full (name
, class, var_zinteger
, var
,
596 set_doc
, show_doc
, help_doc
,
602 /* Remove the command named NAME from the command list. */
605 delete_cmd (char *name
, struct cmd_list_element
**list
)
607 struct cmd_list_element
*c
;
608 struct cmd_list_element
*p
;
610 while (*list
&& strcmp ((*list
)->name
, name
) == 0)
612 if ((*list
)->hookee_pre
)
613 (*list
)->hookee_pre
->hook_pre
= 0; /* Hook slips out of its mouth */
614 if ((*list
)->hookee_post
)
615 (*list
)->hookee_post
->hook_post
= 0; /* Hook slips out of its bottom */
622 for (c
= *list
; c
->next
;)
624 if (strcmp (c
->next
->name
, name
) == 0)
626 if (c
->next
->hookee_pre
)
627 c
->next
->hookee_pre
->hook_pre
= 0; /* hooked cmd gets away. */
628 if (c
->next
->hookee_post
)
629 c
->next
->hookee_post
->hook_post
= 0; /* remove post hook */
630 /* :( no fishing metaphore */
640 /* Shorthands to the commands above. */
642 /* Add an element to the list of info subcommands. */
644 struct cmd_list_element
*
645 add_info (char *name
, void (*fun
) (char *, int), char *doc
)
647 return add_cmd (name
, no_class
, fun
, doc
, &infolist
);
650 /* Add an alias to the list of info subcommands. */
652 struct cmd_list_element
*
653 add_info_alias (char *name
, char *oldname
, int abbrev_flag
)
655 return add_alias_cmd (name
, oldname
, 0, abbrev_flag
, &infolist
);
658 /* Add an element to the list of commands. */
660 struct cmd_list_element
*
661 add_com (char *name
, enum command_class
class, void (*fun
) (char *, int),
664 return add_cmd (name
, class, fun
, doc
, &cmdlist
);
667 /* Add an alias or abbreviation command to the list of commands. */
669 struct cmd_list_element
*
670 add_com_alias (char *name
, char *oldname
, enum command_class
class,
673 return add_alias_cmd (name
, oldname
, class, abbrev_flag
, &cmdlist
);
676 /* Recursively walk the commandlist structures, and print out the
677 documentation of commands that match our regex in either their
678 name, or their documentation.
681 apropos_cmd (struct ui_file
*stream
, struct cmd_list_element
*commandlist
,
682 struct re_pattern_buffer
*regex
, char *prefix
)
684 struct cmd_list_element
*c
;
685 int returnvalue
=1; /*Needed to avoid double printing*/
686 /* Walk through the commands */
687 for (c
=commandlist
;c
;c
=c
->next
)
691 /* Try to match against the name*/
692 returnvalue
=re_search(regex
,c
->name
,strlen(c
->name
),0,strlen(c
->name
),NULL
);
693 if (returnvalue
>= 0)
695 print_help_for_command (c
, prefix
,
696 0 /* don't recurse */, stream
);
699 if (c
->doc
!= NULL
&& returnvalue
!= 0)
701 /* Try to match against documentation */
702 if (re_search(regex
,c
->doc
,strlen(c
->doc
),0,strlen(c
->doc
),NULL
) >=0)
704 print_help_for_command (c
, prefix
,
705 0 /* don't recurse */, stream
);
708 /* Check if this command has subcommands */
709 if (c
->prefixlist
!= NULL
)
711 /* Recursively call ourselves on the subcommand list,
712 passing the right prefix in.
714 apropos_cmd (stream
,*c
->prefixlist
,regex
,c
->prefixname
);
719 /* This command really has to deal with two things:
720 * 1) I want documentation on *this string* (usually called by
721 * "help commandname").
722 * 2) I want documentation on *this list* (usually called by
723 * giving a command that requires subcommands. Also called by saying
726 * I am going to split this into two seperate comamnds, help_cmd and
731 help_cmd (char *command
, struct ui_file
*stream
)
733 struct cmd_list_element
*c
;
734 extern struct cmd_list_element
*cmdlist
;
738 help_list (cmdlist
, "", all_classes
, stream
);
742 if (strcmp (command
, "all") == 0)
748 c
= lookup_cmd (&command
, cmdlist
, "", 0, 0);
753 /* There are three cases here.
754 If c->prefixlist is nonzero, we have a prefix command.
755 Print its documentation, then list its subcommands.
757 If c->func is non NULL, we really have a command. Print its
758 documentation and return.
760 If c->func is NULL, we have a class name. Print its
761 documentation (as if it were a command) and then set class to the
762 number of this class so that the commands in the class will be
765 fputs_filtered (c
->doc
, stream
);
766 fputs_filtered ("\n", stream
);
768 if (c
->prefixlist
== 0 && c
->func
!= NULL
)
770 fprintf_filtered (stream
, "\n");
772 /* If this is a prefix command, print it's subcommands */
774 help_list (*c
->prefixlist
, c
->prefixname
, all_commands
, stream
);
776 /* If this is a class name, print all of the commands in the class */
778 help_list (cmdlist
, "", c
->class, stream
);
780 if (c
->hook_pre
|| c
->hook_post
)
781 fprintf_filtered (stream
,
782 "\nThis command has a hook (or hooks) defined:\n");
785 fprintf_filtered (stream
,
786 "\tThis command is run after : %s (pre hook)\n",
789 fprintf_filtered (stream
,
790 "\tThis command is run before : %s (post hook)\n",
795 * Get a specific kind of help on a command list.
798 * CMDTYPE is the prefix to use in the title string.
799 * CLASS is the class with which to list the nodes of this list (see
800 * documentation for help_cmd_list below), As usual, ALL_COMMANDS for
801 * everything, ALL_CLASSES for just classes, and non-negative for only things
802 * in a specific class.
803 * and STREAM is the output stream on which to print things.
804 * If you call this routine with a class >= 0, it recurses.
807 help_list (struct cmd_list_element
*list
, char *cmdtype
,
808 enum command_class
class, struct ui_file
*stream
)
811 char *cmdtype1
, *cmdtype2
;
813 /* If CMDTYPE is "foo ", CMDTYPE1 gets " foo" and CMDTYPE2 gets "foo sub" */
814 len
= strlen (cmdtype
);
815 cmdtype1
= (char *) alloca (len
+ 1);
817 cmdtype2
= (char *) alloca (len
+ 4);
822 strncpy (cmdtype1
+ 1, cmdtype
, len
- 1);
824 strncpy (cmdtype2
, cmdtype
, len
- 1);
825 strcpy (cmdtype2
+ len
- 1, " sub");
828 if (class == all_classes
)
829 fprintf_filtered (stream
, "List of classes of %scommands:\n\n", cmdtype2
);
831 fprintf_filtered (stream
, "List of %scommands:\n\n", cmdtype2
);
833 help_cmd_list (list
, class, cmdtype
, (int) class >= 0, stream
);
835 if (class == all_classes
)
837 fprintf_filtered (stream
, "\n\
838 Type \"help%s\" followed by a class name for a list of commands in ",
841 fprintf_filtered (stream
, "that class.");
843 fprintf_filtered (stream
, "\n\
844 Type \"help all\" for the list of all commands.");
847 fprintf_filtered (stream
, "\nType \"help%s\" followed by %scommand name ",
850 fputs_filtered ("for ", stream
);
852 fputs_filtered ("full ", stream
);
854 fputs_filtered ("documentation.\n", stream
);
855 fputs_filtered ("Type \"apropos word\" to search "
856 "for commands related to \"word\".\n", stream
);
857 fputs_filtered ("Command name abbreviations are allowed if unambiguous.\n",
862 help_all (struct ui_file
*stream
)
864 struct cmd_list_element
*c
;
865 extern struct cmd_list_element
*cmdlist
;
866 int seen_unclassified
= 0;
868 for (c
= cmdlist
; c
; c
= c
->next
)
872 /* If this is a class name, print all of the commands in the class */
876 fprintf_filtered (stream
, "\nCommand class: %s\n\n", c
->name
);
877 help_cmd_list (cmdlist
, c
->class, "", 1, stream
);
881 /* While it's expected that all commands are in some class,
882 as a safety measure, we'll print commands outside of any
885 for (c
= cmdlist
; c
; c
= c
->next
)
890 if (c
->class == no_class
)
892 if (!seen_unclassified
)
894 fprintf_filtered (stream
, "\nUnclassified commands\n\n");
895 seen_unclassified
= 1;
897 print_help_for_command (c
, "", 1, stream
);
903 /* Print only the first line of STR on STREAM. */
905 print_doc_line (struct ui_file
*stream
, char *str
)
907 static char *line_buffer
= 0;
908 static int line_size
;
914 line_buffer
= (char *) xmalloc (line_size
);
918 while (*p
&& *p
!= '\n' && *p
!= '.' && *p
!= ',')
920 if (p
- str
> line_size
- 1)
922 line_size
= p
- str
+ 1;
924 line_buffer
= (char *) xmalloc (line_size
);
926 strncpy (line_buffer
, str
, p
- str
);
927 line_buffer
[p
- str
] = '\0';
928 if (islower (line_buffer
[0]))
929 line_buffer
[0] = toupper (line_buffer
[0]);
930 ui_out_text (uiout
, line_buffer
);
933 /* Print one-line help for command C.
934 If RECURSE is non-zero, also print one-line descriptions
935 of all prefixed subcommands. */
937 print_help_for_command (struct cmd_list_element
*c
, char *prefix
, int recurse
,
938 struct ui_file
*stream
)
940 fprintf_filtered (stream
, "%s%s -- ", prefix
, c
->name
);
941 print_doc_line (stream
, c
->doc
);
942 fputs_filtered ("\n", stream
);
945 && c
->prefixlist
!= 0
946 && c
->abbrev_flag
== 0)
947 /* Subcommands of a prefix command typically have 'all_commands'
948 as class. If we pass CLASS to recursive invocation,
949 most often we won't see anything. */
950 help_cmd_list (*c
->prefixlist
, all_commands
, c
->prefixname
, 1, stream
);
954 * Implement a help command on command list LIST.
955 * RECURSE should be non-zero if this should be done recursively on
956 * all sublists of LIST.
957 * PREFIX is the prefix to print before each command name.
958 * STREAM is the stream upon which the output should be written.
960 * A non-negative class number to list only commands in that
962 * ALL_COMMANDS to list all commands in list.
963 * ALL_CLASSES to list all classes in list.
965 * Note that RECURSE will be active on *all* sublists, not just the
966 * ones selected by the criteria above (ie. the selection mechanism
967 * is at the low level, not the high-level).
970 help_cmd_list (struct cmd_list_element
*list
, enum command_class
class,
971 char *prefix
, int recurse
, struct ui_file
*stream
)
973 struct cmd_list_element
*c
;
975 for (c
= list
; c
; c
= c
->next
)
977 if (c
->abbrev_flag
== 0 &&
978 (class == all_commands
979 || (class == all_classes
&& c
->func
== NULL
)
980 || (class == c
->class && c
->func
!= NULL
)))
982 print_help_for_command (c
, prefix
, recurse
, stream
);
988 /* Search the input clist for 'command'. Return the command if
989 found (or NULL if not), and return the number of commands
992 static struct cmd_list_element
*
993 find_cmd (char *command
, int len
, struct cmd_list_element
*clist
,
994 int ignore_help_classes
, int *nfound
)
996 struct cmd_list_element
*found
, *c
;
998 found
= (struct cmd_list_element
*) NULL
;
1000 for (c
= clist
; c
; c
= c
->next
)
1001 if (!strncmp (command
, c
->name
, len
)
1002 && (!ignore_help_classes
|| c
->func
))
1006 if (c
->name
[len
] == '\0')
1016 find_command_name_length (const char *text
)
1018 const char *p
= text
;
1020 /* Treating underscores as part of command words is important
1021 so that "set args_foo()" doesn't get interpreted as
1022 "set args _foo()". */
1023 /* Some characters are only used for TUI specific commands. However, they
1024 are always allowed for the sake of consistency.
1025 The XDB compatibility characters are only allowed when using the right
1026 mode because they clash with other GDB commands - specifically '/' is
1027 used as a suffix for print, examine and display.
1028 Note that this is larger than the character set allowed when creating
1029 user-defined commands. */
1030 while (isalnum (*p
) || *p
== '-' || *p
== '_' ||
1031 /* Characters used by TUI specific commands. */
1032 *p
== '+' || *p
== '<' || *p
== '>' || *p
== '$' ||
1033 /* Characters used for XDB compatibility. */
1034 (xdb_commands
&& (*p
== '!' || *p
== '/' || *p
== '?')))
1040 /* This routine takes a line of TEXT and a CLIST in which to start the
1041 lookup. When it returns it will have incremented the text pointer past
1042 the section of text it matched, set *RESULT_LIST to point to the list in
1043 which the last word was matched, and will return a pointer to the cmd
1044 list element which the text matches. It will return NULL if no match at
1045 all was possible. It will return -1 (cast appropriately, ick) if ambigous
1046 matches are possible; in this case *RESULT_LIST will be set to point to
1047 the list in which there are ambiguous choices (and *TEXT will be set to
1048 the ambiguous text string).
1050 If the located command was an abbreviation, this routine returns the base
1051 command of the abbreviation.
1053 It does no error reporting whatsoever; control will always return
1054 to the superior routine.
1056 In the case of an ambiguous return (-1), *RESULT_LIST will be set to point
1057 at the prefix_command (ie. the best match) *or* (special case) will be NULL
1058 if no prefix command was ever found. For example, in the case of "info a",
1059 "info" matches without ambiguity, but "a" could be "args" or "address", so
1060 *RESULT_LIST is set to the cmd_list_element for "info". So in this case
1061 RESULT_LIST should not be interpeted as a pointer to the beginning of a
1062 list; it simply points to a specific command. In the case of an ambiguous
1063 return *TEXT is advanced past the last non-ambiguous prefix (e.g.
1064 "info t" can be "info types" or "info target"; upon return *TEXT has been
1065 advanced past "info ").
1067 If RESULT_LIST is NULL, don't set *RESULT_LIST (but don't otherwise
1068 affect the operation).
1070 This routine does *not* modify the text pointed to by TEXT.
1072 If IGNORE_HELP_CLASSES is nonzero, ignore any command list elements which
1073 are actually help classes rather than commands (i.e. the function field of
1074 the struct cmd_list_element is NULL). */
1076 struct cmd_list_element
*
1077 lookup_cmd_1 (char **text
, struct cmd_list_element
*clist
,
1078 struct cmd_list_element
**result_list
, int ignore_help_classes
)
1081 int len
, tmp
, nfound
;
1082 struct cmd_list_element
*found
, *c
;
1085 while (**text
== ' ' || **text
== '\t')
1088 /* Identify the name of the command. */
1089 len
= find_command_name_length (*text
);
1091 /* If nothing but whitespace, return 0. */
1095 /* *text and p now bracket the first command word to lookup (and
1096 it's length is len). We copy this into a local temporary */
1099 command
= (char *) alloca (len
+ 1);
1100 for (tmp
= 0; tmp
< len
; tmp
++)
1102 char x
= (*text
)[tmp
];
1105 command
[len
] = '\0';
1110 found
= find_cmd (command
, len
, clist
, ignore_help_classes
, &nfound
);
1113 ** We didn't find the command in the entered case, so lower case it
1114 ** and search again.
1116 if (!found
|| nfound
== 0)
1118 for (tmp
= 0; tmp
< len
; tmp
++)
1120 char x
= command
[tmp
];
1121 command
[tmp
] = isupper (x
) ? tolower (x
) : x
;
1123 found
= find_cmd (command
, len
, clist
, ignore_help_classes
, &nfound
);
1126 /* If nothing matches, we have a simple failure. */
1132 if (result_list
!= NULL
)
1133 /* Will be modified in calling routine
1134 if we know what the prefix command is. */
1136 return (struct cmd_list_element
*) -1; /* Ambiguous. */
1139 /* We've matched something on this list. Move text pointer forward. */
1143 if (found
->cmd_pointer
)
1145 /* We drop the alias (abbreviation) in favor of the command it is
1146 pointing to. If the alias is deprecated, though, we need to
1147 warn the user about it before we drop it. Note that while we
1148 are warning about the alias, we may also warn about the command
1149 itself and we will adjust the appropriate DEPRECATED_WARN_USER
1152 if (found
->flags
& DEPRECATED_WARN_USER
)
1153 deprecated_cmd_warning (&line
);
1154 found
= found
->cmd_pointer
;
1156 /* If we found a prefix command, keep looking. */
1158 if (found
->prefixlist
)
1160 c
= lookup_cmd_1 (text
, *found
->prefixlist
, result_list
,
1161 ignore_help_classes
);
1164 /* Didn't find anything; this is as far as we got. */
1165 if (result_list
!= NULL
)
1166 *result_list
= clist
;
1169 else if (c
== (struct cmd_list_element
*) -1)
1171 /* We've gotten this far properly, but the next step
1172 is ambiguous. We need to set the result list to the best
1173 we've found (if an inferior hasn't already set it). */
1174 if (result_list
!= NULL
)
1176 /* This used to say *result_list = *found->prefixlist
1177 If that was correct, need to modify the documentation
1178 at the top of this function to clarify what is supposed
1180 *result_list
= found
;
1191 if (result_list
!= NULL
)
1192 *result_list
= clist
;
1197 /* All this hair to move the space to the front of cmdtype */
1200 undef_cmd_error (char *cmdtype
, char *q
)
1202 error (_("Undefined %scommand: \"%s\". Try \"help%s%.*s\"."),
1205 *cmdtype
? " " : "",
1206 (int) strlen (cmdtype
) - 1,
1210 /* Look up the contents of *LINE as a command in the command list LIST.
1211 LIST is a chain of struct cmd_list_element's.
1212 If it is found, return the struct cmd_list_element for that command
1213 and update *LINE to point after the command name, at the first argument.
1214 If not found, call error if ALLOW_UNKNOWN is zero
1215 otherwise (or if error returns) return zero.
1216 Call error if specified command is ambiguous,
1217 unless ALLOW_UNKNOWN is negative.
1218 CMDTYPE precedes the word "command" in the error message.
1220 If INGNORE_HELP_CLASSES is nonzero, ignore any command list
1221 elements which are actually help classes rather than commands (i.e.
1222 the function field of the struct cmd_list_element is 0). */
1224 struct cmd_list_element
*
1225 lookup_cmd (char **line
, struct cmd_list_element
*list
, char *cmdtype
,
1226 int allow_unknown
, int ignore_help_classes
)
1228 struct cmd_list_element
*last_list
= 0;
1229 struct cmd_list_element
*c
=
1230 lookup_cmd_1 (line
, list
, &last_list
, ignore_help_classes
);
1232 /* Note: Do not remove trailing whitespace here because this
1233 would be wrong for complete_command. Jim Kingdon */
1240 error (_("Lack of needed %scommand"), cmdtype
);
1244 int len
= find_command_name_length (*line
);
1246 q
= (char *) alloca (len
+ 1);
1247 strncpy (q
, *line
, len
);
1249 undef_cmd_error (cmdtype
, q
);
1255 else if (c
== (struct cmd_list_element
*) -1)
1257 /* Ambigous. Local values should be off prefixlist or called
1259 int local_allow_unknown
= (last_list
? last_list
->allow_unknown
:
1261 char *local_cmdtype
= last_list
? last_list
->prefixname
: cmdtype
;
1262 struct cmd_list_element
*local_list
=
1263 (last_list
? *(last_list
->prefixlist
) : list
);
1265 if (local_allow_unknown
< 0)
1268 return last_list
; /* Found something. */
1270 return 0; /* Found nothing. */
1274 /* Report as error. */
1279 ((*line
)[amb_len
] && (*line
)[amb_len
] != ' '
1280 && (*line
)[amb_len
] != '\t');
1285 for (c
= local_list
; c
; c
= c
->next
)
1286 if (!strncmp (*line
, c
->name
, amb_len
))
1288 if (strlen (ambbuf
) + strlen (c
->name
) + 6 < (int) sizeof ambbuf
)
1290 if (strlen (ambbuf
))
1291 strcat (ambbuf
, ", ");
1292 strcat (ambbuf
, c
->name
);
1296 strcat (ambbuf
, "..");
1300 error (_("Ambiguous %scommand \"%s\": %s."), local_cmdtype
,
1302 return 0; /* lint */
1307 /* We've got something. It may still not be what the caller
1308 wants (if this command *needs* a subcommand). */
1309 while (**line
== ' ' || **line
== '\t')
1312 if (c
->prefixlist
&& **line
&& !c
->allow_unknown
)
1313 undef_cmd_error (c
->prefixname
, *line
);
1315 /* Seems to be what he wants. Return it. */
1321 /* We are here presumably because an alias or command in *TEXT is
1322 deprecated and a warning message should be generated. This function
1323 decodes *TEXT and potentially generates a warning message as outlined
1326 Example for 'set endian big' which has a fictitious alias 'seb'.
1328 If alias wasn't used in *TEXT, and the command is deprecated:
1329 "warning: 'set endian big' is deprecated."
1331 If alias was used, and only the alias is deprecated:
1332 "warning: 'seb' an alias for the command 'set endian big' is deprecated."
1334 If alias was used and command is deprecated (regardless of whether the
1335 alias itself is deprecated:
1337 "warning: 'set endian big' (seb) is deprecated."
1339 After the message has been sent, clear the appropriate flags in the
1340 command and/or the alias so the user is no longer bothered.
1344 deprecated_cmd_warning (char **text
)
1346 struct cmd_list_element
*alias
= NULL
;
1347 struct cmd_list_element
*prefix_cmd
= NULL
;
1348 struct cmd_list_element
*cmd
= NULL
;
1349 struct cmd_list_element
*c
;
1352 if (!lookup_cmd_composition (*text
, &alias
, &prefix_cmd
, &cmd
))
1353 /* return if text doesn't evaluate to a command */
1356 if (!((alias
? (alias
->flags
& DEPRECATED_WARN_USER
) : 0)
1357 || (cmd
->flags
& DEPRECATED_WARN_USER
) ) )
1358 /* return if nothing is deprecated */
1361 printf_filtered ("Warning:");
1363 if (alias
&& !(cmd
->flags
& CMD_DEPRECATED
))
1364 printf_filtered (" '%s', an alias for the", alias
->name
);
1366 printf_filtered (" command '");
1369 printf_filtered ("%s", prefix_cmd
->prefixname
);
1371 printf_filtered ("%s", cmd
->name
);
1373 if (alias
&& (cmd
->flags
& CMD_DEPRECATED
))
1374 printf_filtered ("' (%s) is deprecated.\n", alias
->name
);
1376 printf_filtered ("' is deprecated.\n");
1379 /* if it is only the alias that is deprecated, we want to indicate the
1380 new alias, otherwise we'll indicate the new command */
1382 if (alias
&& !(cmd
->flags
& CMD_DEPRECATED
))
1384 if (alias
->replacement
)
1385 printf_filtered ("Use '%s'.\n\n", alias
->replacement
);
1387 printf_filtered ("No alternative known.\n\n");
1391 if (cmd
->replacement
)
1392 printf_filtered ("Use '%s'.\n\n", cmd
->replacement
);
1394 printf_filtered ("No alternative known.\n\n");
1397 /* We've warned you, now we'll keep quiet */
1399 alias
->flags
&= ~DEPRECATED_WARN_USER
;
1401 cmd
->flags
&= ~DEPRECATED_WARN_USER
;
1406 /* Look up the contents of LINE as a command in the command list 'cmdlist'.
1407 Return 1 on success, 0 on failure.
1409 If LINE refers to an alias, *alias will point to that alias.
1411 If LINE is a postfix command (i.e. one that is preceeded by a prefix
1412 command) set *prefix_cmd.
1414 Set *cmd to point to the command LINE indicates.
1416 If any of *alias, *prefix_cmd, or *cmd cannot be determined or do not
1417 exist, they are NULL when we return.
1421 lookup_cmd_composition (char *text
,
1422 struct cmd_list_element
**alias
,
1423 struct cmd_list_element
**prefix_cmd
,
1424 struct cmd_list_element
**cmd
)
1427 int len
, tmp
, nfound
;
1428 struct cmd_list_element
*cur_list
;
1429 struct cmd_list_element
*prev_cmd
;
1438 /* Go through as many command lists as we need to
1439 to find the command TEXT refers to. */
1443 while (*text
== ' ' || *text
== '\t')
1446 /* Identify the name of the command. */
1447 len
= find_command_name_length (text
);
1449 /* If nothing but whitespace, return. */
1453 /* text is the start of the first command word to lookup (and
1454 it's length is len). We copy this into a local temporary */
1456 command
= (char *) alloca (len
+ 1);
1457 for (tmp
= 0; tmp
< len
; tmp
++)
1462 command
[len
] = '\0';
1467 *cmd
= find_cmd (command
, len
, cur_list
, 1, &nfound
);
1469 /* We didn't find the command in the entered case, so lower case it
1472 if (!*cmd
|| nfound
== 0)
1474 for (tmp
= 0; tmp
< len
; tmp
++)
1476 char x
= command
[tmp
];
1477 command
[tmp
] = isupper (x
) ? tolower (x
) : x
;
1479 *cmd
= find_cmd (command
, len
, cur_list
, 1, &nfound
);
1482 if (*cmd
== (struct cmd_list_element
*) -1)
1484 return 0; /* ambiguous */
1488 return 0; /* nothing found */
1491 if ((*cmd
)->cmd_pointer
)
1493 /* cmd was actually an alias, we note that an alias was used
1494 (by assigning *alais) and we set *cmd.
1497 *cmd
= (*cmd
)->cmd_pointer
;
1499 *prefix_cmd
= prev_cmd
;
1501 if ((*cmd
)->prefixlist
)
1502 cur_list
= *(*cmd
)->prefixlist
;
1510 /* Helper function for SYMBOL_COMPLETION_FUNCTION. */
1512 /* Return a vector of char pointers which point to the different
1513 possible completions in LIST of TEXT.
1515 WORD points in the same buffer as TEXT, and completions should be
1516 returned relative to this position. For example, suppose TEXT is "foo"
1517 and we want to complete to "foobar". If WORD is "oo", return
1518 "oobar"; if WORD is "baz/foo", return "baz/foobar". */
1521 complete_on_cmdlist (struct cmd_list_element
*list
, char *text
, char *word
)
1523 struct cmd_list_element
*ptr
;
1525 int sizeof_matchlist
;
1527 int textlen
= strlen (text
);
1529 sizeof_matchlist
= 10;
1530 matchlist
= (char **) xmalloc (sizeof_matchlist
* sizeof (char *));
1533 for (ptr
= list
; ptr
; ptr
= ptr
->next
)
1534 if (!strncmp (ptr
->name
, text
, textlen
)
1535 && !ptr
->abbrev_flag
1537 || ptr
->prefixlist
))
1539 if (matches
== sizeof_matchlist
)
1541 sizeof_matchlist
*= 2;
1542 matchlist
= (char **) xrealloc ((char *) matchlist
,
1544 * sizeof (char *)));
1547 matchlist
[matches
] = (char *)
1548 xmalloc (strlen (word
) + strlen (ptr
->name
) + 1);
1550 strcpy (matchlist
[matches
], ptr
->name
);
1551 else if (word
> text
)
1553 /* Return some portion of ptr->name. */
1554 strcpy (matchlist
[matches
], ptr
->name
+ (word
- text
));
1558 /* Return some of text plus ptr->name. */
1559 strncpy (matchlist
[matches
], word
, text
- word
);
1560 matchlist
[matches
][text
- word
] = '\0';
1561 strcat (matchlist
[matches
], ptr
->name
);
1573 matchlist
= (char **) xrealloc ((char *) matchlist
, ((matches
+ 1)
1574 * sizeof (char *)));
1575 matchlist
[matches
] = (char *) 0;
1581 /* Helper function for SYMBOL_COMPLETION_FUNCTION. */
1583 /* Return a vector of char pointers which point to the different
1584 possible completions in CMD of TEXT.
1586 WORD points in the same buffer as TEXT, and completions should be
1587 returned relative to this position. For example, suppose TEXT is "foo"
1588 and we want to complete to "foobar". If WORD is "oo", return
1589 "oobar"; if WORD is "baz/foo", return "baz/foobar". */
1592 complete_on_enum (const char *enumlist
[],
1597 int sizeof_matchlist
;
1599 int textlen
= strlen (text
);
1603 sizeof_matchlist
= 10;
1604 matchlist
= (char **) xmalloc (sizeof_matchlist
* sizeof (char *));
1607 for (i
= 0; (name
= enumlist
[i
]) != NULL
; i
++)
1608 if (strncmp (name
, text
, textlen
) == 0)
1610 if (matches
== sizeof_matchlist
)
1612 sizeof_matchlist
*= 2;
1613 matchlist
= (char **) xrealloc ((char *) matchlist
,
1615 * sizeof (char *)));
1618 matchlist
[matches
] = (char *)
1619 xmalloc (strlen (word
) + strlen (name
) + 1);
1621 strcpy (matchlist
[matches
], name
);
1622 else if (word
> text
)
1624 /* Return some portion of name. */
1625 strcpy (matchlist
[matches
], name
+ (word
- text
));
1629 /* Return some of text plus name. */
1630 strncpy (matchlist
[matches
], word
, text
- word
);
1631 matchlist
[matches
][text
- word
] = '\0';
1632 strcat (matchlist
[matches
], name
);
1644 matchlist
= (char **) xrealloc ((char *) matchlist
, ((matches
+ 1)
1645 * sizeof (char *)));
1646 matchlist
[matches
] = (char *) 0;
1653 /* check function pointer */
1655 cmd_func_p (struct cmd_list_element
*cmd
)
1657 return (cmd
->func
!= NULL
);
1661 /* call the command function */
1663 cmd_func (struct cmd_list_element
*cmd
, char *args
, int from_tty
)
1665 if (cmd_func_p (cmd
))
1666 (*cmd
->func
) (cmd
, args
, from_tty
);
1668 error (_("Invalid command"));