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 2008 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 3 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, see <http://www.gnu.org/licenses/>. */
22 #include "gdb_regex.h"
23 #include "gdb_string.h"
24 #include "completer.h"
27 #include "cli/cli-cmds.h"
28 #include "cli/cli-decode.h"
31 #include "tui/tui.h" /* For tui_active et.al. */
34 #include "gdb_assert.h"
36 /* Prototypes for local functions */
38 static void undef_cmd_error (char *, char *);
40 static struct cmd_list_element
*find_cmd (char *command
,
42 struct cmd_list_element
*clist
,
43 int ignore_help_classes
,
46 static void help_all (struct ui_file
*stream
);
49 print_help_for_command (struct cmd_list_element
*c
, char *prefix
, int recurse
,
50 struct ui_file
*stream
);
53 /* Set the callback function for the specified command. For each both
54 the commands callback and func() are set. The latter set to a
55 bounce function (unless cfunc / sfunc is NULL that is). */
58 do_cfunc (struct cmd_list_element
*c
, char *args
, int from_tty
)
60 c
->function
.cfunc (args
, from_tty
); /* Ok. */
64 set_cmd_cfunc (struct cmd_list_element
*cmd
, cmd_cfunc_ftype
*cfunc
)
70 cmd
->function
.cfunc
= cfunc
; /* Ok. */
74 do_sfunc (struct cmd_list_element
*c
, char *args
, int from_tty
)
76 c
->function
.sfunc (args
, from_tty
, c
); /* Ok. */
80 set_cmd_sfunc (struct cmd_list_element
*cmd
, cmd_sfunc_ftype
*sfunc
)
86 cmd
->function
.sfunc
= sfunc
; /* Ok. */
90 cmd_cfunc_eq (struct cmd_list_element
*cmd
,
91 void (*cfunc
) (char *args
, int from_tty
))
93 return cmd
->func
== do_cfunc
&& cmd
->function
.cfunc
== cfunc
;
97 set_cmd_context (struct cmd_list_element
*cmd
, void *context
)
99 cmd
->context
= context
;
103 get_cmd_context (struct cmd_list_element
*cmd
)
109 set_cmd_async_ok (struct cmd_list_element
*cmd
)
111 cmd
->flags
|= CMD_ASYNC_OK
;
115 get_cmd_async_ok (struct cmd_list_element
*cmd
)
117 return cmd
->flags
& CMD_ASYNC_OK
;
121 set_cmd_no_selected_thread_ok (struct cmd_list_element
*cmd
)
123 cmd
->flags
|= CMD_NO_SELECTED_THREAD_OK
;
127 get_cmd_no_selected_thread_ok (struct cmd_list_element
*cmd
)
129 return cmd
->flags
& CMD_NO_SELECTED_THREAD_OK
;
133 cmd_type (struct cmd_list_element
*cmd
)
139 set_cmd_completer (struct cmd_list_element
*cmd
,
140 char **(*completer
) (char *text
, char *word
))
142 cmd
->completer
= completer
; /* Ok. */
146 /* Add element named NAME.
147 CLASS is the top level category into which commands are broken down
149 FUN should be the function to execute the command;
150 it will get a character string as argument, with leading
151 and trailing blanks already eliminated.
153 DOC is a documentation string for the command.
154 Its first line should be a complete sentence.
155 It should start with ? for a command that is an abbreviation
156 or with * for a command that most users don't need to know about.
158 Add this command to command list *LIST.
160 Returns a pointer to the added command (not necessarily the head
163 struct cmd_list_element
*
164 add_cmd (char *name
, enum command_class
class, void (*fun
) (char *, int),
165 char *doc
, struct cmd_list_element
**list
)
167 struct cmd_list_element
*c
168 = (struct cmd_list_element
*) xmalloc (sizeof (struct cmd_list_element
));
169 struct cmd_list_element
*p
;
171 delete_cmd (name
, list
);
173 if (*list
== NULL
|| strcmp ((*list
)->name
, name
) >= 0)
181 while (p
->next
&& strcmp (p
->next
->name
, name
) <= 0)
191 set_cmd_cfunc (c
, fun
);
192 set_cmd_context (c
, NULL
);
195 c
->replacement
= NULL
;
196 c
->pre_show_hook
= NULL
;
200 c
->prefixlist
= NULL
;
201 c
->prefixname
= NULL
;
202 c
->allow_unknown
= 0;
204 set_cmd_completer (c
, make_symbol_completion_list
);
205 c
->type
= not_set_cmd
;
207 c
->var_type
= var_boolean
;
209 c
->user_commands
= NULL
;
210 c
->hookee_pre
= NULL
;
211 c
->hookee_post
= NULL
;
212 c
->cmd_pointer
= NULL
;
217 /* Deprecates a command CMD.
218 REPLACEMENT is the name of the command which should be used in place
219 of this command, or NULL if no such command exists.
221 This function does not check to see if command REPLACEMENT exists
222 since gdb may not have gotten around to adding REPLACEMENT when this
225 Returns a pointer to the deprecated command. */
227 struct cmd_list_element
*
228 deprecate_cmd (struct cmd_list_element
*cmd
, char *replacement
)
230 cmd
->flags
|= (CMD_DEPRECATED
| DEPRECATED_WARN_USER
);
232 if (replacement
!= NULL
)
233 cmd
->replacement
= replacement
;
235 cmd
->replacement
= NULL
;
240 struct cmd_list_element
*
241 add_alias_cmd (char *name
, char *oldname
, enum command_class
class,
242 int abbrev_flag
, struct cmd_list_element
**list
)
244 /* Must do this since lookup_cmd tries to side-effect its first arg */
246 struct cmd_list_element
*old
;
247 struct cmd_list_element
*c
;
248 copied_name
= (char *) alloca (strlen (oldname
) + 1);
249 strcpy (copied_name
, oldname
);
250 old
= lookup_cmd (&copied_name
, *list
, "", 1, 1);
254 delete_cmd (name
, list
);
258 c
= add_cmd (name
, class, NULL
, old
->doc
, list
);
259 /* NOTE: Both FUNC and all the FUNCTIONs need to be copied. */
261 c
->function
= old
->function
;
262 c
->prefixlist
= old
->prefixlist
;
263 c
->prefixname
= old
->prefixname
;
264 c
->allow_unknown
= old
->allow_unknown
;
265 c
->abbrev_flag
= abbrev_flag
;
266 c
->cmd_pointer
= old
;
270 /* Like add_cmd but adds an element for a command prefix:
271 a name that should be followed by a subcommand to be looked up
272 in another command list. PREFIXLIST should be the address
273 of the variable containing that list. */
275 struct cmd_list_element
*
276 add_prefix_cmd (char *name
, enum command_class
class, void (*fun
) (char *, int),
277 char *doc
, struct cmd_list_element
**prefixlist
,
278 char *prefixname
, int allow_unknown
,
279 struct cmd_list_element
**list
)
281 struct cmd_list_element
*c
= add_cmd (name
, class, fun
, doc
, list
);
282 c
->prefixlist
= prefixlist
;
283 c
->prefixname
= prefixname
;
284 c
->allow_unknown
= allow_unknown
;
288 /* Like add_prefix_cmd but sets the abbrev_flag on the new command. */
290 struct cmd_list_element
*
291 add_abbrev_prefix_cmd (char *name
, enum command_class
class,
292 void (*fun
) (char *, int), char *doc
,
293 struct cmd_list_element
**prefixlist
, char *prefixname
,
294 int allow_unknown
, struct cmd_list_element
**list
)
296 struct cmd_list_element
*c
= add_cmd (name
, class, fun
, doc
, list
);
297 c
->prefixlist
= prefixlist
;
298 c
->prefixname
= prefixname
;
299 c
->allow_unknown
= allow_unknown
;
304 /* This is an empty "cfunc". */
306 not_just_help_class_command (char *args
, int from_tty
)
310 /* This is an empty "sfunc". */
311 static void empty_sfunc (char *, int, struct cmd_list_element
*);
314 empty_sfunc (char *args
, int from_tty
, struct cmd_list_element
*c
)
318 /* Add element named NAME to command list LIST (the list for set/show
319 or some sublist thereof).
320 TYPE is set_cmd or show_cmd.
321 CLASS is as in add_cmd.
322 VAR_TYPE is the kind of thing we are setting.
323 VAR is address of the variable being controlled by this command.
324 DOC is the documentation string. */
326 static struct cmd_list_element
*
327 add_set_or_show_cmd (char *name
,
329 enum command_class
class,
333 struct cmd_list_element
**list
)
335 struct cmd_list_element
*c
= add_cmd (name
, class, NULL
, doc
, list
);
336 gdb_assert (type
== set_cmd
|| type
== show_cmd
);
338 c
->var_type
= var_type
;
340 /* This needs to be something besides NULL so that this isn't
341 treated as a help class. */
342 set_cmd_sfunc (c
, empty_sfunc
);
346 /* Add element named NAME to both the command SET_LIST and SHOW_LIST.
347 CLASS is as in add_cmd. VAR_TYPE is the kind of thing we are
348 setting. VAR is address of the variable being controlled by this
349 command. SET_FUNC and SHOW_FUNC are the callback functions (if
350 non-NULL). SET_DOC, SHOW_DOC and HELP_DOC are the documentation
351 strings. PRINT the format string to print the value. SET_RESULT
352 and SHOW_RESULT, if not NULL, are set to the resulting command
356 add_setshow_cmd_full (char *name
,
357 enum command_class
class,
358 var_types var_type
, void *var
,
359 const char *set_doc
, const char *show_doc
,
360 const char *help_doc
,
361 cmd_sfunc_ftype
*set_func
,
362 show_value_ftype
*show_func
,
363 struct cmd_list_element
**set_list
,
364 struct cmd_list_element
**show_list
,
365 struct cmd_list_element
**set_result
,
366 struct cmd_list_element
**show_result
)
368 struct cmd_list_element
*set
;
369 struct cmd_list_element
*show
;
373 if (help_doc
!= NULL
)
375 full_set_doc
= xstrprintf ("%s\n%s", set_doc
, help_doc
);
376 full_show_doc
= xstrprintf ("%s\n%s", show_doc
, help_doc
);
380 full_set_doc
= xstrdup (set_doc
);
381 full_show_doc
= xstrdup (show_doc
);
383 set
= add_set_or_show_cmd (name
, set_cmd
, class, var_type
, var
,
384 full_set_doc
, set_list
);
385 if (set_func
!= NULL
)
386 set_cmd_sfunc (set
, set_func
);
387 show
= add_set_or_show_cmd (name
, show_cmd
, class, var_type
, var
,
388 full_show_doc
, show_list
);
389 show
->show_value_func
= show_func
;
391 if (set_result
!= NULL
)
393 if (show_result
!= NULL
)
397 struct cmd_list_element
*
398 deprecated_add_set_cmd (char *name
,
399 enum command_class
class,
403 struct cmd_list_element
**list
)
405 return add_set_or_show_cmd (name
, set_cmd
, class, var_type
, var
, doc
, list
);
408 /* Add element named NAME to command list LIST (the list for set or
409 some sublist thereof). CLASS is as in add_cmd. ENUMLIST is a list
410 of strings which may follow NAME. VAR is address of the variable
411 which will contain the matching string (from ENUMLIST). */
414 add_setshow_enum_cmd (char *name
,
415 enum command_class
class,
416 const char *enumlist
[],
419 const char *show_doc
,
420 const char *help_doc
,
421 cmd_sfunc_ftype
*set_func
,
422 show_value_ftype
*show_func
,
423 struct cmd_list_element
**set_list
,
424 struct cmd_list_element
**show_list
)
426 struct cmd_list_element
*c
;
427 add_setshow_cmd_full (name
, class, var_enum
, var
,
428 set_doc
, show_doc
, help_doc
,
435 /* Add an auto-boolean command named NAME to both the set and show
436 command list lists. CLASS is as in add_cmd. VAR is address of the
437 variable which will contain the value. DOC is the documentation
438 string. FUNC is the corresponding callback. */
440 add_setshow_auto_boolean_cmd (char *name
,
441 enum command_class
class,
442 enum auto_boolean
*var
,
443 const char *set_doc
, const char *show_doc
,
444 const char *help_doc
,
445 cmd_sfunc_ftype
*set_func
,
446 show_value_ftype
*show_func
,
447 struct cmd_list_element
**set_list
,
448 struct cmd_list_element
**show_list
)
450 static const char *auto_boolean_enums
[] = { "on", "off", "auto", NULL
};
451 struct cmd_list_element
*c
;
452 add_setshow_cmd_full (name
, class, var_auto_boolean
, var
,
453 set_doc
, show_doc
, help_doc
,
457 c
->enums
= auto_boolean_enums
;
460 /* Add element named NAME to both the set and show command LISTs (the
461 list for set/show or some sublist thereof). CLASS is as in
462 add_cmd. VAR is address of the variable which will contain the
463 value. SET_DOC and SHOW_DOC are the documentation strings. */
465 add_setshow_boolean_cmd (char *name
, enum command_class
class, int *var
,
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 static const char *boolean_enums
[] = { "on", "off", NULL
};
474 struct cmd_list_element
*c
;
475 add_setshow_cmd_full (name
, class, var_boolean
, var
,
476 set_doc
, show_doc
, help_doc
,
480 c
->enums
= boolean_enums
;
483 /* Add element named NAME to both the set and show command LISTs (the
484 list for set/show or some sublist thereof). */
486 add_setshow_filename_cmd (char *name
, enum command_class
class,
488 const char *set_doc
, const char *show_doc
,
489 const char *help_doc
,
490 cmd_sfunc_ftype
*set_func
,
491 show_value_ftype
*show_func
,
492 struct cmd_list_element
**set_list
,
493 struct cmd_list_element
**show_list
)
495 struct cmd_list_element
*set_result
;
496 add_setshow_cmd_full (name
, class, var_filename
, var
,
497 set_doc
, show_doc
, help_doc
,
501 set_cmd_completer (set_result
, filename_completer
);
504 /* Add element named NAME to both the set and show command LISTs (the
505 list for set/show or some sublist thereof). */
507 add_setshow_string_cmd (char *name
, enum command_class
class,
509 const char *set_doc
, const char *show_doc
,
510 const char *help_doc
,
511 cmd_sfunc_ftype
*set_func
,
512 show_value_ftype
*show_func
,
513 struct cmd_list_element
**set_list
,
514 struct cmd_list_element
**show_list
)
516 add_setshow_cmd_full (name
, class, var_string
, var
,
517 set_doc
, show_doc
, help_doc
,
523 /* Add element named NAME to both the set and show command LISTs (the
524 list for set/show or some sublist thereof). */
526 add_setshow_string_noescape_cmd (char *name
, enum command_class
class,
528 const char *set_doc
, const char *show_doc
,
529 const char *help_doc
,
530 cmd_sfunc_ftype
*set_func
,
531 show_value_ftype
*show_func
,
532 struct cmd_list_element
**set_list
,
533 struct cmd_list_element
**show_list
)
535 add_setshow_cmd_full (name
, class, var_string_noescape
, var
,
536 set_doc
, show_doc
, help_doc
,
542 /* Add element named NAME to both the set and show command LISTs (the
543 list for set/show or some sublist thereof). */
545 add_setshow_optional_filename_cmd (char *name
, enum command_class
class,
547 const char *set_doc
, const char *show_doc
,
548 const char *help_doc
,
549 cmd_sfunc_ftype
*set_func
,
550 show_value_ftype
*show_func
,
551 struct cmd_list_element
**set_list
,
552 struct cmd_list_element
**show_list
)
554 struct cmd_list_element
*set_result
;
556 add_setshow_cmd_full (name
, class, var_optional_filename
, var
,
557 set_doc
, show_doc
, help_doc
,
562 set_cmd_completer (set_result
, filename_completer
);
566 /* Add element named NAME to both the set and show command LISTs (the
567 list for set/show or some sublist thereof). CLASS is as in
568 add_cmd. VAR is address of the variable which will contain the
569 value. SET_DOC and SHOW_DOC are the documentation strings. */
571 add_setshow_integer_cmd (char *name
, enum command_class
class,
573 const char *set_doc
, const char *show_doc
,
574 const char *help_doc
,
575 cmd_sfunc_ftype
*set_func
,
576 show_value_ftype
*show_func
,
577 struct cmd_list_element
**set_list
,
578 struct cmd_list_element
**show_list
)
580 add_setshow_cmd_full (name
, class, var_integer
, var
,
581 set_doc
, show_doc
, help_doc
,
587 /* Add element named NAME to both the set and show command LISTs (the
588 list for set/show or some sublist thereof). CLASS is as in
589 add_cmd. VAR is address of the variable which will contain the
590 value. SET_DOC and SHOW_DOC are the documentation strings. */
592 add_setshow_uinteger_cmd (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 add_setshow_cmd_full (name
, class, var_uinteger
, var
,
602 set_doc
, show_doc
, help_doc
,
608 /* Add element named NAME to both the set and show command LISTs (the
609 list for set/show or some sublist thereof). CLASS is as in
610 add_cmd. VAR is address of the variable which will contain the
611 value. SET_DOC and SHOW_DOC are the documentation strings. */
613 add_setshow_zinteger_cmd (char *name
, enum command_class
class,
615 const char *set_doc
, const char *show_doc
,
616 const char *help_doc
,
617 cmd_sfunc_ftype
*set_func
,
618 show_value_ftype
*show_func
,
619 struct cmd_list_element
**set_list
,
620 struct cmd_list_element
**show_list
)
622 add_setshow_cmd_full (name
, class, var_zinteger
, var
,
623 set_doc
, show_doc
, help_doc
,
629 /* Remove the command named NAME from the command list. */
632 delete_cmd (char *name
, struct cmd_list_element
**list
)
634 struct cmd_list_element
*c
;
635 struct cmd_list_element
*p
;
637 while (*list
&& strcmp ((*list
)->name
, name
) == 0)
639 if ((*list
)->hookee_pre
)
640 (*list
)->hookee_pre
->hook_pre
= 0; /* Hook slips out of its mouth */
641 if ((*list
)->hookee_post
)
642 (*list
)->hookee_post
->hook_post
= 0; /* Hook slips out of its bottom */
649 for (c
= *list
; c
->next
;)
651 if (strcmp (c
->next
->name
, name
) == 0)
653 if (c
->next
->hookee_pre
)
654 c
->next
->hookee_pre
->hook_pre
= 0; /* hooked cmd gets away. */
655 if (c
->next
->hookee_post
)
656 c
->next
->hookee_post
->hook_post
= 0; /* remove post hook */
657 /* :( no fishing metaphore */
667 /* Shorthands to the commands above. */
669 /* Add an element to the list of info subcommands. */
671 struct cmd_list_element
*
672 add_info (char *name
, void (*fun
) (char *, int), char *doc
)
674 return add_cmd (name
, no_class
, fun
, doc
, &infolist
);
677 /* Add an alias to the list of info subcommands. */
679 struct cmd_list_element
*
680 add_info_alias (char *name
, char *oldname
, int abbrev_flag
)
682 return add_alias_cmd (name
, oldname
, 0, abbrev_flag
, &infolist
);
685 /* Add an element to the list of commands. */
687 struct cmd_list_element
*
688 add_com (char *name
, enum command_class
class, void (*fun
) (char *, int),
691 return add_cmd (name
, class, fun
, doc
, &cmdlist
);
694 /* Add an alias or abbreviation command to the list of commands. */
696 struct cmd_list_element
*
697 add_com_alias (char *name
, char *oldname
, enum command_class
class,
700 return add_alias_cmd (name
, oldname
, class, abbrev_flag
, &cmdlist
);
703 /* Recursively walk the commandlist structures, and print out the
704 documentation of commands that match our regex in either their
705 name, or their documentation.
708 apropos_cmd (struct ui_file
*stream
, struct cmd_list_element
*commandlist
,
709 struct re_pattern_buffer
*regex
, char *prefix
)
711 struct cmd_list_element
*c
;
712 int returnvalue
=1; /*Needed to avoid double printing*/
713 /* Walk through the commands */
714 for (c
=commandlist
;c
;c
=c
->next
)
718 /* Try to match against the name*/
719 returnvalue
=re_search(regex
,c
->name
,strlen(c
->name
),0,strlen(c
->name
),NULL
);
720 if (returnvalue
>= 0)
722 print_help_for_command (c
, prefix
,
723 0 /* don't recurse */, stream
);
726 if (c
->doc
!= NULL
&& returnvalue
!= 0)
728 /* Try to match against documentation */
729 if (re_search(regex
,c
->doc
,strlen(c
->doc
),0,strlen(c
->doc
),NULL
) >=0)
731 print_help_for_command (c
, prefix
,
732 0 /* don't recurse */, stream
);
735 /* Check if this command has subcommands */
736 if (c
->prefixlist
!= NULL
)
738 /* Recursively call ourselves on the subcommand list,
739 passing the right prefix in.
741 apropos_cmd (stream
,*c
->prefixlist
,regex
,c
->prefixname
);
746 /* This command really has to deal with two things:
747 * 1) I want documentation on *this string* (usually called by
748 * "help commandname").
749 * 2) I want documentation on *this list* (usually called by
750 * giving a command that requires subcommands. Also called by saying
753 * I am going to split this into two seperate comamnds, help_cmd and
758 help_cmd (char *command
, struct ui_file
*stream
)
760 struct cmd_list_element
*c
;
761 extern struct cmd_list_element
*cmdlist
;
765 help_list (cmdlist
, "", all_classes
, stream
);
769 if (strcmp (command
, "all") == 0)
775 c
= lookup_cmd (&command
, cmdlist
, "", 0, 0);
780 /* There are three cases here.
781 If c->prefixlist is nonzero, we have a prefix command.
782 Print its documentation, then list its subcommands.
784 If c->func is non NULL, we really have a command. Print its
785 documentation and return.
787 If c->func is NULL, we have a class name. Print its
788 documentation (as if it were a command) and then set class to the
789 number of this class so that the commands in the class will be
792 fputs_filtered (c
->doc
, stream
);
793 fputs_filtered ("\n", stream
);
795 if (c
->prefixlist
== 0 && c
->func
!= NULL
)
797 fprintf_filtered (stream
, "\n");
799 /* If this is a prefix command, print it's subcommands */
801 help_list (*c
->prefixlist
, c
->prefixname
, all_commands
, stream
);
803 /* If this is a class name, print all of the commands in the class */
805 help_list (cmdlist
, "", c
->class, stream
);
807 if (c
->hook_pre
|| c
->hook_post
)
808 fprintf_filtered (stream
,
809 "\nThis command has a hook (or hooks) defined:\n");
812 fprintf_filtered (stream
,
813 "\tThis command is run after : %s (pre hook)\n",
816 fprintf_filtered (stream
,
817 "\tThis command is run before : %s (post hook)\n",
822 * Get a specific kind of help on a command list.
825 * CMDTYPE is the prefix to use in the title string.
826 * CLASS is the class with which to list the nodes of this list (see
827 * documentation for help_cmd_list below), As usual, ALL_COMMANDS for
828 * everything, ALL_CLASSES for just classes, and non-negative for only things
829 * in a specific class.
830 * and STREAM is the output stream on which to print things.
831 * If you call this routine with a class >= 0, it recurses.
834 help_list (struct cmd_list_element
*list
, char *cmdtype
,
835 enum command_class
class, struct ui_file
*stream
)
838 char *cmdtype1
, *cmdtype2
;
840 /* If CMDTYPE is "foo ", CMDTYPE1 gets " foo" and CMDTYPE2 gets "foo sub" */
841 len
= strlen (cmdtype
);
842 cmdtype1
= (char *) alloca (len
+ 1);
844 cmdtype2
= (char *) alloca (len
+ 4);
849 strncpy (cmdtype1
+ 1, cmdtype
, len
- 1);
851 strncpy (cmdtype2
, cmdtype
, len
- 1);
852 strcpy (cmdtype2
+ len
- 1, " sub");
855 if (class == all_classes
)
856 fprintf_filtered (stream
, "List of classes of %scommands:\n\n", cmdtype2
);
858 fprintf_filtered (stream
, "List of %scommands:\n\n", cmdtype2
);
860 help_cmd_list (list
, class, cmdtype
, (int) class >= 0, stream
);
862 if (class == all_classes
)
864 fprintf_filtered (stream
, "\n\
865 Type \"help%s\" followed by a class name for a list of commands in ",
868 fprintf_filtered (stream
, "that class.");
870 fprintf_filtered (stream
, "\n\
871 Type \"help all\" for the list of all commands.");
874 fprintf_filtered (stream
, "\nType \"help%s\" followed by %scommand name ",
877 fputs_filtered ("for ", stream
);
879 fputs_filtered ("full ", stream
);
881 fputs_filtered ("documentation.\n", stream
);
882 fputs_filtered ("Type \"apropos word\" to search "
883 "for commands related to \"word\".\n", stream
);
884 fputs_filtered ("Command name abbreviations are allowed if unambiguous.\n",
889 help_all (struct ui_file
*stream
)
891 struct cmd_list_element
*c
;
892 extern struct cmd_list_element
*cmdlist
;
893 int seen_unclassified
= 0;
895 for (c
= cmdlist
; c
; c
= c
->next
)
899 /* If this is a class name, print all of the commands in the class */
903 fprintf_filtered (stream
, "\nCommand class: %s\n\n", c
->name
);
904 help_cmd_list (cmdlist
, c
->class, "", 1, stream
);
908 /* While it's expected that all commands are in some class,
909 as a safety measure, we'll print commands outside of any
912 for (c
= cmdlist
; c
; c
= c
->next
)
917 if (c
->class == no_class
)
919 if (!seen_unclassified
)
921 fprintf_filtered (stream
, "\nUnclassified commands\n\n");
922 seen_unclassified
= 1;
924 print_help_for_command (c
, "", 1, stream
);
930 /* Print only the first line of STR on STREAM. */
932 print_doc_line (struct ui_file
*stream
, char *str
)
934 static char *line_buffer
= 0;
935 static int line_size
;
941 line_buffer
= (char *) xmalloc (line_size
);
945 while (*p
&& *p
!= '\n' && *p
!= '.' && *p
!= ',')
947 if (p
- str
> line_size
- 1)
949 line_size
= p
- str
+ 1;
951 line_buffer
= (char *) xmalloc (line_size
);
953 strncpy (line_buffer
, str
, p
- str
);
954 line_buffer
[p
- str
] = '\0';
955 if (islower (line_buffer
[0]))
956 line_buffer
[0] = toupper (line_buffer
[0]);
957 ui_out_text (uiout
, line_buffer
);
960 /* Print one-line help for command C.
961 If RECURSE is non-zero, also print one-line descriptions
962 of all prefixed subcommands. */
964 print_help_for_command (struct cmd_list_element
*c
, char *prefix
, int recurse
,
965 struct ui_file
*stream
)
967 fprintf_filtered (stream
, "%s%s -- ", prefix
, c
->name
);
968 print_doc_line (stream
, c
->doc
);
969 fputs_filtered ("\n", stream
);
972 && c
->prefixlist
!= 0
973 && c
->abbrev_flag
== 0)
974 /* Subcommands of a prefix command typically have 'all_commands'
975 as class. If we pass CLASS to recursive invocation,
976 most often we won't see anything. */
977 help_cmd_list (*c
->prefixlist
, all_commands
, c
->prefixname
, 1, stream
);
981 * Implement a help command on command list LIST.
982 * RECURSE should be non-zero if this should be done recursively on
983 * all sublists of LIST.
984 * PREFIX is the prefix to print before each command name.
985 * STREAM is the stream upon which the output should be written.
987 * A non-negative class number to list only commands in that
989 * ALL_COMMANDS to list all commands in list.
990 * ALL_CLASSES to list all classes in list.
992 * Note that RECURSE will be active on *all* sublists, not just the
993 * ones selected by the criteria above (ie. the selection mechanism
994 * is at the low level, not the high-level).
997 help_cmd_list (struct cmd_list_element
*list
, enum command_class
class,
998 char *prefix
, int recurse
, struct ui_file
*stream
)
1000 struct cmd_list_element
*c
;
1002 for (c
= list
; c
; c
= c
->next
)
1004 if (c
->abbrev_flag
== 0 &&
1005 (class == all_commands
1006 || (class == all_classes
&& c
->func
== NULL
)
1007 || (class == c
->class && c
->func
!= NULL
)))
1009 print_help_for_command (c
, prefix
, recurse
, stream
);
1015 /* Search the input clist for 'command'. Return the command if
1016 found (or NULL if not), and return the number of commands
1019 static struct cmd_list_element
*
1020 find_cmd (char *command
, int len
, struct cmd_list_element
*clist
,
1021 int ignore_help_classes
, int *nfound
)
1023 struct cmd_list_element
*found
, *c
;
1025 found
= (struct cmd_list_element
*) NULL
;
1027 for (c
= clist
; c
; c
= c
->next
)
1028 if (!strncmp (command
, c
->name
, len
)
1029 && (!ignore_help_classes
|| c
->func
))
1033 if (c
->name
[len
] == '\0')
1043 find_command_name_length (const char *text
)
1045 const char *p
= text
;
1047 /* Treating underscores as part of command words is important
1048 so that "set args_foo()" doesn't get interpreted as
1049 "set args _foo()". */
1050 /* Some characters are only used for TUI specific commands. However, they
1051 are always allowed for the sake of consistency.
1052 The XDB compatibility characters are only allowed when using the right
1053 mode because they clash with other GDB commands - specifically '/' is
1054 used as a suffix for print, examine and display.
1055 Note that this is larger than the character set allowed when creating
1056 user-defined commands. */
1057 while (isalnum (*p
) || *p
== '-' || *p
== '_' ||
1058 /* Characters used by TUI specific commands. */
1059 *p
== '+' || *p
== '<' || *p
== '>' || *p
== '$' ||
1060 /* Characters used for XDB compatibility. */
1061 (xdb_commands
&& (*p
== '!' || *p
== '/' || *p
== '?')))
1067 /* This routine takes a line of TEXT and a CLIST in which to start the
1068 lookup. When it returns it will have incremented the text pointer past
1069 the section of text it matched, set *RESULT_LIST to point to the list in
1070 which the last word was matched, and will return a pointer to the cmd
1071 list element which the text matches. It will return NULL if no match at
1072 all was possible. It will return -1 (cast appropriately, ick) if ambigous
1073 matches are possible; in this case *RESULT_LIST will be set to point to
1074 the list in which there are ambiguous choices (and *TEXT will be set to
1075 the ambiguous text string).
1077 If the located command was an abbreviation, this routine returns the base
1078 command of the abbreviation.
1080 It does no error reporting whatsoever; control will always return
1081 to the superior routine.
1083 In the case of an ambiguous return (-1), *RESULT_LIST will be set to point
1084 at the prefix_command (ie. the best match) *or* (special case) will be NULL
1085 if no prefix command was ever found. For example, in the case of "info a",
1086 "info" matches without ambiguity, but "a" could be "args" or "address", so
1087 *RESULT_LIST is set to the cmd_list_element for "info". So in this case
1088 RESULT_LIST should not be interpeted as a pointer to the beginning of a
1089 list; it simply points to a specific command. In the case of an ambiguous
1090 return *TEXT is advanced past the last non-ambiguous prefix (e.g.
1091 "info t" can be "info types" or "info target"; upon return *TEXT has been
1092 advanced past "info ").
1094 If RESULT_LIST is NULL, don't set *RESULT_LIST (but don't otherwise
1095 affect the operation).
1097 This routine does *not* modify the text pointed to by TEXT.
1099 If IGNORE_HELP_CLASSES is nonzero, ignore any command list elements which
1100 are actually help classes rather than commands (i.e. the function field of
1101 the struct cmd_list_element is NULL). */
1103 struct cmd_list_element
*
1104 lookup_cmd_1 (char **text
, struct cmd_list_element
*clist
,
1105 struct cmd_list_element
**result_list
, int ignore_help_classes
)
1108 int len
, tmp
, nfound
;
1109 struct cmd_list_element
*found
, *c
;
1112 while (**text
== ' ' || **text
== '\t')
1115 /* Identify the name of the command. */
1116 len
= find_command_name_length (*text
);
1118 /* If nothing but whitespace, return 0. */
1122 /* *text and p now bracket the first command word to lookup (and
1123 it's length is len). We copy this into a local temporary */
1126 command
= (char *) alloca (len
+ 1);
1127 for (tmp
= 0; tmp
< len
; tmp
++)
1129 char x
= (*text
)[tmp
];
1132 command
[len
] = '\0';
1137 found
= find_cmd (command
, len
, clist
, ignore_help_classes
, &nfound
);
1140 ** We didn't find the command in the entered case, so lower case it
1141 ** and search again.
1143 if (!found
|| nfound
== 0)
1145 for (tmp
= 0; tmp
< len
; tmp
++)
1147 char x
= command
[tmp
];
1148 command
[tmp
] = isupper (x
) ? tolower (x
) : x
;
1150 found
= find_cmd (command
, len
, clist
, ignore_help_classes
, &nfound
);
1153 /* If nothing matches, we have a simple failure. */
1159 if (result_list
!= NULL
)
1160 /* Will be modified in calling routine
1161 if we know what the prefix command is. */
1163 return (struct cmd_list_element
*) -1; /* Ambiguous. */
1166 /* We've matched something on this list. Move text pointer forward. */
1170 if (found
->cmd_pointer
)
1172 /* We drop the alias (abbreviation) in favor of the command it is
1173 pointing to. If the alias is deprecated, though, we need to
1174 warn the user about it before we drop it. Note that while we
1175 are warning about the alias, we may also warn about the command
1176 itself and we will adjust the appropriate DEPRECATED_WARN_USER
1179 if (found
->flags
& DEPRECATED_WARN_USER
)
1180 deprecated_cmd_warning (&line
);
1181 found
= found
->cmd_pointer
;
1183 /* If we found a prefix command, keep looking. */
1185 if (found
->prefixlist
)
1187 c
= lookup_cmd_1 (text
, *found
->prefixlist
, result_list
,
1188 ignore_help_classes
);
1191 /* Didn't find anything; this is as far as we got. */
1192 if (result_list
!= NULL
)
1193 *result_list
= clist
;
1196 else if (c
== (struct cmd_list_element
*) -1)
1198 /* We've gotten this far properly, but the next step
1199 is ambiguous. We need to set the result list to the best
1200 we've found (if an inferior hasn't already set it). */
1201 if (result_list
!= NULL
)
1203 /* This used to say *result_list = *found->prefixlist
1204 If that was correct, need to modify the documentation
1205 at the top of this function to clarify what is supposed
1207 *result_list
= found
;
1218 if (result_list
!= NULL
)
1219 *result_list
= clist
;
1224 /* All this hair to move the space to the front of cmdtype */
1227 undef_cmd_error (char *cmdtype
, char *q
)
1229 error (_("Undefined %scommand: \"%s\". Try \"help%s%.*s\"."),
1232 *cmdtype
? " " : "",
1233 (int) strlen (cmdtype
) - 1,
1237 /* Look up the contents of *LINE as a command in the command list LIST.
1238 LIST is a chain of struct cmd_list_element's.
1239 If it is found, return the struct cmd_list_element for that command
1240 and update *LINE to point after the command name, at the first argument.
1241 If not found, call error if ALLOW_UNKNOWN is zero
1242 otherwise (or if error returns) return zero.
1243 Call error if specified command is ambiguous,
1244 unless ALLOW_UNKNOWN is negative.
1245 CMDTYPE precedes the word "command" in the error message.
1247 If INGNORE_HELP_CLASSES is nonzero, ignore any command list
1248 elements which are actually help classes rather than commands (i.e.
1249 the function field of the struct cmd_list_element is 0). */
1251 struct cmd_list_element
*
1252 lookup_cmd (char **line
, struct cmd_list_element
*list
, char *cmdtype
,
1253 int allow_unknown
, int ignore_help_classes
)
1255 struct cmd_list_element
*last_list
= 0;
1256 struct cmd_list_element
*c
;
1258 /* Note: Do not remove trailing whitespace here because this
1259 would be wrong for complete_command. Jim Kingdon */
1262 error (_("Lack of needed %scommand"), cmdtype
);
1264 c
= lookup_cmd_1 (line
, list
, &last_list
, ignore_help_classes
);
1271 int len
= find_command_name_length (*line
);
1273 q
= (char *) alloca (len
+ 1);
1274 strncpy (q
, *line
, len
);
1276 undef_cmd_error (cmdtype
, q
);
1281 else if (c
== (struct cmd_list_element
*) -1)
1283 /* Ambigous. Local values should be off prefixlist or called
1285 int local_allow_unknown
= (last_list
? last_list
->allow_unknown
:
1287 char *local_cmdtype
= last_list
? last_list
->prefixname
: cmdtype
;
1288 struct cmd_list_element
*local_list
=
1289 (last_list
? *(last_list
->prefixlist
) : list
);
1291 if (local_allow_unknown
< 0)
1294 return last_list
; /* Found something. */
1296 return 0; /* Found nothing. */
1300 /* Report as error. */
1305 ((*line
)[amb_len
] && (*line
)[amb_len
] != ' '
1306 && (*line
)[amb_len
] != '\t');
1311 for (c
= local_list
; c
; c
= c
->next
)
1312 if (!strncmp (*line
, c
->name
, amb_len
))
1314 if (strlen (ambbuf
) + strlen (c
->name
) + 6 < (int) sizeof ambbuf
)
1316 if (strlen (ambbuf
))
1317 strcat (ambbuf
, ", ");
1318 strcat (ambbuf
, c
->name
);
1322 strcat (ambbuf
, "..");
1326 error (_("Ambiguous %scommand \"%s\": %s."), local_cmdtype
,
1328 return 0; /* lint */
1333 /* We've got something. It may still not be what the caller
1334 wants (if this command *needs* a subcommand). */
1335 while (**line
== ' ' || **line
== '\t')
1338 if (c
->prefixlist
&& **line
&& !c
->allow_unknown
)
1339 undef_cmd_error (c
->prefixname
, *line
);
1341 /* Seems to be what he wants. Return it. */
1347 /* We are here presumably because an alias or command in *TEXT is
1348 deprecated and a warning message should be generated. This function
1349 decodes *TEXT and potentially generates a warning message as outlined
1352 Example for 'set endian big' which has a fictitious alias 'seb'.
1354 If alias wasn't used in *TEXT, and the command is deprecated:
1355 "warning: 'set endian big' is deprecated."
1357 If alias was used, and only the alias is deprecated:
1358 "warning: 'seb' an alias for the command 'set endian big' is deprecated."
1360 If alias was used and command is deprecated (regardless of whether the
1361 alias itself is deprecated:
1363 "warning: 'set endian big' (seb) is deprecated."
1365 After the message has been sent, clear the appropriate flags in the
1366 command and/or the alias so the user is no longer bothered.
1370 deprecated_cmd_warning (char **text
)
1372 struct cmd_list_element
*alias
= NULL
;
1373 struct cmd_list_element
*prefix_cmd
= NULL
;
1374 struct cmd_list_element
*cmd
= NULL
;
1375 struct cmd_list_element
*c
;
1378 if (!lookup_cmd_composition (*text
, &alias
, &prefix_cmd
, &cmd
))
1379 /* return if text doesn't evaluate to a command */
1382 if (!((alias
? (alias
->flags
& DEPRECATED_WARN_USER
) : 0)
1383 || (cmd
->flags
& DEPRECATED_WARN_USER
) ) )
1384 /* return if nothing is deprecated */
1387 printf_filtered ("Warning:");
1389 if (alias
&& !(cmd
->flags
& CMD_DEPRECATED
))
1390 printf_filtered (" '%s', an alias for the", alias
->name
);
1392 printf_filtered (" command '");
1395 printf_filtered ("%s", prefix_cmd
->prefixname
);
1397 printf_filtered ("%s", cmd
->name
);
1399 if (alias
&& (cmd
->flags
& CMD_DEPRECATED
))
1400 printf_filtered ("' (%s) is deprecated.\n", alias
->name
);
1402 printf_filtered ("' is deprecated.\n");
1405 /* if it is only the alias that is deprecated, we want to indicate the
1406 new alias, otherwise we'll indicate the new command */
1408 if (alias
&& !(cmd
->flags
& CMD_DEPRECATED
))
1410 if (alias
->replacement
)
1411 printf_filtered ("Use '%s'.\n\n", alias
->replacement
);
1413 printf_filtered ("No alternative known.\n\n");
1417 if (cmd
->replacement
)
1418 printf_filtered ("Use '%s'.\n\n", cmd
->replacement
);
1420 printf_filtered ("No alternative known.\n\n");
1423 /* We've warned you, now we'll keep quiet */
1425 alias
->flags
&= ~DEPRECATED_WARN_USER
;
1427 cmd
->flags
&= ~DEPRECATED_WARN_USER
;
1432 /* Look up the contents of LINE as a command in the command list 'cmdlist'.
1433 Return 1 on success, 0 on failure.
1435 If LINE refers to an alias, *alias will point to that alias.
1437 If LINE is a postfix command (i.e. one that is preceeded by a prefix
1438 command) set *prefix_cmd.
1440 Set *cmd to point to the command LINE indicates.
1442 If any of *alias, *prefix_cmd, or *cmd cannot be determined or do not
1443 exist, they are NULL when we return.
1447 lookup_cmd_composition (char *text
,
1448 struct cmd_list_element
**alias
,
1449 struct cmd_list_element
**prefix_cmd
,
1450 struct cmd_list_element
**cmd
)
1453 int len
, tmp
, nfound
;
1454 struct cmd_list_element
*cur_list
;
1455 struct cmd_list_element
*prev_cmd
;
1464 /* Go through as many command lists as we need to
1465 to find the command TEXT refers to. */
1469 while (*text
== ' ' || *text
== '\t')
1472 /* Identify the name of the command. */
1473 len
= find_command_name_length (text
);
1475 /* If nothing but whitespace, return. */
1479 /* text is the start of the first command word to lookup (and
1480 it's length is len). We copy this into a local temporary */
1482 command
= (char *) alloca (len
+ 1);
1483 for (tmp
= 0; tmp
< len
; tmp
++)
1488 command
[len
] = '\0';
1493 *cmd
= find_cmd (command
, len
, cur_list
, 1, &nfound
);
1495 /* We didn't find the command in the entered case, so lower case it
1498 if (!*cmd
|| nfound
== 0)
1500 for (tmp
= 0; tmp
< len
; tmp
++)
1502 char x
= command
[tmp
];
1503 command
[tmp
] = isupper (x
) ? tolower (x
) : x
;
1505 *cmd
= find_cmd (command
, len
, cur_list
, 1, &nfound
);
1508 if (*cmd
== (struct cmd_list_element
*) -1)
1510 return 0; /* ambiguous */
1514 return 0; /* nothing found */
1517 if ((*cmd
)->cmd_pointer
)
1519 /* cmd was actually an alias, we note that an alias was used
1520 (by assigning *alais) and we set *cmd.
1523 *cmd
= (*cmd
)->cmd_pointer
;
1525 *prefix_cmd
= prev_cmd
;
1527 if ((*cmd
)->prefixlist
)
1528 cur_list
= *(*cmd
)->prefixlist
;
1536 /* Helper function for SYMBOL_COMPLETION_FUNCTION. */
1538 /* Return a vector of char pointers which point to the different
1539 possible completions in LIST of TEXT.
1541 WORD points in the same buffer as TEXT, and completions should be
1542 returned relative to this position. For example, suppose TEXT is "foo"
1543 and we want to complete to "foobar". If WORD is "oo", return
1544 "oobar"; if WORD is "baz/foo", return "baz/foobar". */
1547 complete_on_cmdlist (struct cmd_list_element
*list
, char *text
, char *word
)
1549 struct cmd_list_element
*ptr
;
1551 int sizeof_matchlist
;
1553 int textlen
= strlen (text
);
1555 sizeof_matchlist
= 10;
1556 matchlist
= (char **) xmalloc (sizeof_matchlist
* sizeof (char *));
1559 for (ptr
= list
; ptr
; ptr
= ptr
->next
)
1560 if (!strncmp (ptr
->name
, text
, textlen
)
1561 && !ptr
->abbrev_flag
1563 || ptr
->prefixlist
))
1565 if (matches
== sizeof_matchlist
)
1567 sizeof_matchlist
*= 2;
1568 matchlist
= (char **) xrealloc ((char *) matchlist
,
1570 * sizeof (char *)));
1573 matchlist
[matches
] = (char *)
1574 xmalloc (strlen (word
) + strlen (ptr
->name
) + 1);
1576 strcpy (matchlist
[matches
], ptr
->name
);
1577 else if (word
> text
)
1579 /* Return some portion of ptr->name. */
1580 strcpy (matchlist
[matches
], ptr
->name
+ (word
- text
));
1584 /* Return some of text plus ptr->name. */
1585 strncpy (matchlist
[matches
], word
, text
- word
);
1586 matchlist
[matches
][text
- word
] = '\0';
1587 strcat (matchlist
[matches
], ptr
->name
);
1599 matchlist
= (char **) xrealloc ((char *) matchlist
, ((matches
+ 1)
1600 * sizeof (char *)));
1601 matchlist
[matches
] = (char *) 0;
1607 /* Helper function for SYMBOL_COMPLETION_FUNCTION. */
1609 /* Return a vector of char pointers which point to the different
1610 possible completions in CMD of TEXT.
1612 WORD points in the same buffer as TEXT, and completions should be
1613 returned relative to this position. For example, suppose TEXT is "foo"
1614 and we want to complete to "foobar". If WORD is "oo", return
1615 "oobar"; if WORD is "baz/foo", return "baz/foobar". */
1618 complete_on_enum (const char *enumlist
[],
1623 int sizeof_matchlist
;
1625 int textlen
= strlen (text
);
1629 sizeof_matchlist
= 10;
1630 matchlist
= (char **) xmalloc (sizeof_matchlist
* sizeof (char *));
1633 for (i
= 0; (name
= enumlist
[i
]) != NULL
; i
++)
1634 if (strncmp (name
, text
, textlen
) == 0)
1636 if (matches
== sizeof_matchlist
)
1638 sizeof_matchlist
*= 2;
1639 matchlist
= (char **) xrealloc ((char *) matchlist
,
1641 * sizeof (char *)));
1644 matchlist
[matches
] = (char *)
1645 xmalloc (strlen (word
) + strlen (name
) + 1);
1647 strcpy (matchlist
[matches
], name
);
1648 else if (word
> text
)
1650 /* Return some portion of name. */
1651 strcpy (matchlist
[matches
], name
+ (word
- text
));
1655 /* Return some of text plus name. */
1656 strncpy (matchlist
[matches
], word
, text
- word
);
1657 matchlist
[matches
][text
- word
] = '\0';
1658 strcat (matchlist
[matches
], name
);
1670 matchlist
= (char **) xrealloc ((char *) matchlist
, ((matches
+ 1)
1671 * sizeof (char *)));
1672 matchlist
[matches
] = (char *) 0;
1679 /* check function pointer */
1681 cmd_func_p (struct cmd_list_element
*cmd
)
1683 return (cmd
->func
!= NULL
);
1687 /* call the command function */
1689 cmd_func (struct cmd_list_element
*cmd
, char *args
, int from_tty
)
1691 if (cmd_func_p (cmd
))
1692 (*cmd
->func
) (cmd
, args
, from_tty
);
1694 error (_("Invalid command"));