1 /* Handle lists of commands, their decoding and documentation, for GDB.
3 Copyright 1986, 1989, 1990, 1991, 1998, 2000, 2001, 2002 Free
4 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., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
24 #include "gdb_regex.h"
25 #include "gdb_string.h"
29 #include "cli/cli-cmds.h"
30 #include "cli/cli-decode.h"
32 #include "gdb_assert.h"
34 /* Prototypes for local functions */
36 static void undef_cmd_error (char *, char *);
38 static struct cmd_list_element
*find_cmd (char *command
,
40 struct cmd_list_element
*clist
,
41 int ignore_help_classes
,
44 static void help_all (struct ui_file
*stream
);
46 /* Set the callback function for the specified command. For each both
47 the commands callback and func() are set. The latter set to a
48 bounce function (unless cfunc / sfunc is NULL that is). */
51 do_cfunc (struct cmd_list_element
*c
, char *args
, int from_tty
)
53 c
->function
.cfunc (args
, from_tty
); /* Ok. */
57 set_cmd_cfunc (struct cmd_list_element
*cmd
, cmd_cfunc_ftype
*cfunc
)
63 cmd
->function
.cfunc
= cfunc
; /* Ok. */
67 do_sfunc (struct cmd_list_element
*c
, char *args
, int from_tty
)
69 c
->function
.sfunc (args
, from_tty
, c
); /* Ok. */
73 set_cmd_sfunc (struct cmd_list_element
*cmd
, cmd_sfunc_ftype
*sfunc
)
79 cmd
->function
.sfunc
= sfunc
; /* Ok. */
83 cmd_cfunc_eq (struct cmd_list_element
*cmd
,
84 void (*cfunc
) (char *args
, int from_tty
))
86 return cmd
->func
== do_cfunc
&& cmd
->function
.cfunc
== cfunc
;
90 set_cmd_context (struct cmd_list_element
*cmd
, void *context
)
92 cmd
->context
= context
;
96 get_cmd_context (struct cmd_list_element
*cmd
)
102 cmd_type (struct cmd_list_element
*cmd
)
108 set_cmd_completer (struct cmd_list_element
*cmd
,
109 char **(*completer
) (char *text
, char *word
))
111 cmd
->completer
= completer
; /* Ok. */
115 /* Add element named NAME.
116 CLASS is the top level category into which commands are broken down
118 FUN should be the function to execute the command;
119 it will get a character string as argument, with leading
120 and trailing blanks already eliminated.
122 DOC is a documentation string for the command.
123 Its first line should be a complete sentence.
124 It should start with ? for a command that is an abbreviation
125 or with * for a command that most users don't need to know about.
127 Add this command to command list *LIST.
129 Returns a pointer to the added command (not necessarily the head
132 struct cmd_list_element
*
133 add_cmd (char *name
, enum command_class
class, void (*fun
) (char *, int),
134 char *doc
, struct cmd_list_element
**list
)
136 register struct cmd_list_element
*c
137 = (struct cmd_list_element
*) xmalloc (sizeof (struct cmd_list_element
));
138 struct cmd_list_element
*p
;
140 delete_cmd (name
, list
);
142 if (*list
== NULL
|| strcmp ((*list
)->name
, name
) >= 0)
150 while (p
->next
&& strcmp (p
->next
->name
, name
) <= 0)
160 set_cmd_cfunc (c
, fun
);
161 set_cmd_context (c
, NULL
);
164 c
->replacement
= NULL
;
165 c
->pre_show_hook
= NULL
;
169 c
->prefixlist
= NULL
;
170 c
->prefixname
= NULL
;
171 c
->allow_unknown
= 0;
173 set_cmd_completer (c
, make_symbol_completion_list
);
174 c
->type
= not_set_cmd
;
176 c
->var_type
= var_boolean
;
178 c
->user_commands
= NULL
;
179 c
->hookee_pre
= NULL
;
180 c
->hookee_post
= NULL
;
181 c
->cmd_pointer
= NULL
;
186 /* Same as above, except that the abbrev_flag is set. */
187 /* Note: Doesn't seem to be used anywhere currently. */
189 struct cmd_list_element
*
190 add_abbrev_cmd (char *name
, enum command_class
class, void (*fun
) (char *, int),
191 char *doc
, struct cmd_list_element
**list
)
193 register struct cmd_list_element
*c
194 = add_cmd (name
, class, fun
, doc
, list
);
200 /* Deprecates a command CMD.
201 REPLACEMENT is the name of the command which should be used in place
202 of this command, or NULL if no such command exists.
204 This function does not check to see if command REPLACEMENT exists
205 since gdb may not have gotten around to adding REPLACEMENT when this
208 Returns a pointer to the deprecated command. */
210 struct cmd_list_element
*
211 deprecate_cmd (struct cmd_list_element
*cmd
, char *replacement
)
213 cmd
->flags
|= (CMD_DEPRECATED
| DEPRECATED_WARN_USER
);
215 if (replacement
!= NULL
)
216 cmd
->replacement
= replacement
;
218 cmd
->replacement
= NULL
;
223 struct cmd_list_element
*
224 add_alias_cmd (char *name
, char *oldname
, enum command_class
class,
225 int abbrev_flag
, struct cmd_list_element
**list
)
227 /* Must do this since lookup_cmd tries to side-effect its first arg */
229 register struct cmd_list_element
*old
;
230 register struct cmd_list_element
*c
;
231 copied_name
= (char *) alloca (strlen (oldname
) + 1);
232 strcpy (copied_name
, oldname
);
233 old
= lookup_cmd (&copied_name
, *list
, "", 1, 1);
237 delete_cmd (name
, list
);
241 c
= add_cmd (name
, class, NULL
, old
->doc
, list
);
242 /* NOTE: Both FUNC and all the FUNCTIONs need to be copied. */
244 c
->function
= old
->function
;
245 c
->prefixlist
= old
->prefixlist
;
246 c
->prefixname
= old
->prefixname
;
247 c
->allow_unknown
= old
->allow_unknown
;
248 c
->abbrev_flag
= abbrev_flag
;
249 c
->cmd_pointer
= old
;
253 /* Like add_cmd but adds an element for a command prefix:
254 a name that should be followed by a subcommand to be looked up
255 in another command list. PREFIXLIST should be the address
256 of the variable containing that list. */
258 struct cmd_list_element
*
259 add_prefix_cmd (char *name
, enum command_class
class, void (*fun
) (char *, int),
260 char *doc
, struct cmd_list_element
**prefixlist
,
261 char *prefixname
, int allow_unknown
,
262 struct cmd_list_element
**list
)
264 register struct cmd_list_element
*c
= add_cmd (name
, class, fun
, doc
, list
);
265 c
->prefixlist
= prefixlist
;
266 c
->prefixname
= prefixname
;
267 c
->allow_unknown
= allow_unknown
;
271 /* Like add_prefix_cmd but sets the abbrev_flag on the new command. */
273 struct cmd_list_element
*
274 add_abbrev_prefix_cmd (char *name
, enum command_class
class,
275 void (*fun
) (char *, int), char *doc
,
276 struct cmd_list_element
**prefixlist
, char *prefixname
,
277 int allow_unknown
, struct cmd_list_element
**list
)
279 register struct cmd_list_element
*c
= add_cmd (name
, class, fun
, doc
, list
);
280 c
->prefixlist
= prefixlist
;
281 c
->prefixname
= prefixname
;
282 c
->allow_unknown
= allow_unknown
;
287 /* This is an empty "cfunc". */
289 not_just_help_class_command (char *args
, int from_tty
)
293 /* This is an empty "sfunc". */
294 static void empty_sfunc (char *, int, struct cmd_list_element
*);
297 empty_sfunc (char *args
, int from_tty
, struct cmd_list_element
*c
)
301 /* Add element named NAME to command list LIST (the list for set/show
302 or some sublist thereof).
303 TYPE is set_cmd or show_cmd.
304 CLASS is as in add_cmd.
305 VAR_TYPE is the kind of thing we are setting.
306 VAR is address of the variable being controlled by this command.
307 DOC is the documentation string. */
309 static struct cmd_list_element
*
310 add_set_or_show_cmd (char *name
,
312 enum command_class
class,
316 struct cmd_list_element
**list
)
318 struct cmd_list_element
*c
= add_cmd (name
, class, NULL
, doc
, list
);
319 gdb_assert (type
== set_cmd
|| type
== show_cmd
);
321 c
->var_type
= var_type
;
323 /* This needs to be something besides NULL so that this isn't
324 treated as a help class. */
325 set_cmd_sfunc (c
, empty_sfunc
);
329 /* Add element named NAME to both the command SET_LIST and SHOW_LIST.
330 CLASS is as in add_cmd. VAR_TYPE is the kind of thing we are
331 setting. VAR is address of the variable being controlled by this
332 command. SET_FUNC and SHOW_FUNC are the callback functions (if
333 non-NULL). SET_DOC and SHOW_DOC are the documentation strings.
334 SET_RESULT and SHOW_RESULT, if not NULL, are set to the resulting
335 command structures. */
338 add_setshow_cmd_full (char *name
,
339 enum command_class
class,
340 var_types var_type
, void *var
,
341 char *set_doc
, char *show_doc
,
342 cmd_sfunc_ftype
*set_func
, cmd_sfunc_ftype
*show_func
,
343 struct cmd_list_element
**set_list
,
344 struct cmd_list_element
**show_list
,
345 struct cmd_list_element
**set_result
,
346 struct cmd_list_element
**show_result
)
348 struct cmd_list_element
*set
;
349 struct cmd_list_element
*show
;
350 set
= add_set_or_show_cmd (name
, set_cmd
, class, var_type
, var
,
352 if (set_func
!= NULL
)
353 set_cmd_sfunc (set
, set_func
);
354 show
= add_set_or_show_cmd (name
, show_cmd
, class, var_type
, var
,
355 show_doc
, show_list
);
356 if (show_func
!= NULL
)
357 set_cmd_sfunc (show
, show_func
);
359 if (set_result
!= NULL
)
361 if (show_result
!= NULL
)
365 /* Add element named NAME to both the command SET_LIST and SHOW_LIST.
366 CLASS is as in add_cmd. VAR_TYPE is the kind of thing we are
367 setting. VAR is address of the variable being controlled by this
368 command. SET_FUNC and SHOW_FUNC are the callback functions (if
369 non-NULL). SET_DOC and SHOW_DOC are the documentation strings. */
372 add_setshow_cmd (char *name
,
373 enum command_class
class,
374 var_types var_type
, void *var
,
375 char *set_doc
, char *show_doc
,
376 cmd_sfunc_ftype
*set_func
, cmd_sfunc_ftype
*show_func
,
377 struct cmd_list_element
**set_list
,
378 struct cmd_list_element
**show_list
)
380 add_setshow_cmd_full (name
, class, var_type
, var
, set_doc
, show_doc
,
381 set_func
, show_func
, set_list
, show_list
,
385 struct cmd_list_element
*
386 add_set_cmd (char *name
,
387 enum command_class
class,
391 struct cmd_list_element
**list
)
393 return add_set_or_show_cmd (name
, set_cmd
, class, var_type
, var
, doc
, list
);
396 /* Add element named NAME to command list LIST (the list for set
397 or some sublist thereof).
398 CLASS is as in add_cmd.
399 ENUMLIST is a list of strings which may follow NAME.
400 VAR is address of the variable which will contain the matching string
402 DOC is the documentation string. */
404 struct cmd_list_element
*
405 add_set_enum_cmd (char *name
,
406 enum command_class
class,
407 const char *enumlist
[],
410 struct cmd_list_element
**list
)
412 struct cmd_list_element
*c
413 = add_set_cmd (name
, class, var_enum
, var
, doc
, list
);
419 /* Add an auto-boolean command named NAME to both the set and show
420 command list lists. CLASS is as in add_cmd. VAR is address of the
421 variable which will contain the value. DOC is the documentation
422 string. FUNC is the corresponding callback. */
424 add_setshow_auto_boolean_cmd (char *name
,
425 enum command_class
class,
426 enum auto_boolean
*var
,
427 char *set_doc
, char *show_doc
,
428 cmd_sfunc_ftype
*set_func
,
429 cmd_sfunc_ftype
*show_func
,
430 struct cmd_list_element
**set_list
,
431 struct cmd_list_element
**show_list
)
433 static const char *auto_boolean_enums
[] = { "on", "off", "auto", NULL
};
434 struct cmd_list_element
*c
;
435 add_setshow_cmd_full (name
, class, var_auto_boolean
, var
,
436 set_doc
, show_doc
, set_func
, show_func
,
439 c
->enums
= auto_boolean_enums
;
442 /* Add element named NAME to both the set and show command LISTs (the
443 list for set/show or some sublist thereof). CLASS is as in
444 add_cmd. VAR is address of the variable which will contain the
445 value. SET_DOC and SHOW_DOR are the documentation strings. */
447 add_setshow_boolean_cmd (char *name
,
448 enum command_class
class,
449 int *var
, char *set_doc
, char *show_doc
,
450 cmd_sfunc_ftype
*set_func
,
451 cmd_sfunc_ftype
*show_func
,
452 struct cmd_list_element
**set_list
,
453 struct cmd_list_element
**show_list
)
455 static const char *boolean_enums
[] = { "on", "off", NULL
};
456 struct cmd_list_element
*c
;
457 add_setshow_cmd_full (name
, class, var_boolean
, var
,
462 c
->enums
= boolean_enums
;
465 /* Where SETCMD has already been added, add the corresponding show
466 command to LIST and return a pointer to the added command (not
467 necessarily the head of LIST). */
468 /* NOTE: cagney/2002-03-17: The original version of add_show_from_set
469 used memcpy() to clone `set' into `show'. This meant that in
470 addition to all the needed fields (var, name, et.al.) some
471 unnecessary fields were copied (namely the callback function). The
472 function explictly copies relevant fields. For a `set' and `show'
473 command to share the same callback, the caller must set both
475 struct cmd_list_element
*
476 add_show_from_set (struct cmd_list_element
*setcmd
,
477 struct cmd_list_element
**list
)
480 const static char setstring
[] = "Set ";
482 /* Create a doc string by replacing "Set " at the start of the
483 `set'' command's doco with "Show ". */
484 gdb_assert (strncmp (setcmd
->doc
, setstring
, sizeof (setstring
) - 1) == 0);
485 doc
= concat ("Show ", setcmd
->doc
+ sizeof (setstring
) - 1, NULL
);
487 /* Insert the basic command. */
488 return add_set_or_show_cmd (setcmd
->name
, show_cmd
, setcmd
->class,
489 setcmd
->var_type
, setcmd
->var
, doc
, list
);
492 /* Remove the command named NAME from the command list. */
495 delete_cmd (char *name
, struct cmd_list_element
**list
)
497 register struct cmd_list_element
*c
;
498 struct cmd_list_element
*p
;
500 while (*list
&& STREQ ((*list
)->name
, name
))
502 if ((*list
)->hookee_pre
)
503 (*list
)->hookee_pre
->hook_pre
= 0; /* Hook slips out of its mouth */
504 if ((*list
)->hookee_post
)
505 (*list
)->hookee_post
->hook_post
= 0; /* Hook slips out of its bottom */
512 for (c
= *list
; c
->next
;)
514 if (STREQ (c
->next
->name
, name
))
516 if (c
->next
->hookee_pre
)
517 c
->next
->hookee_pre
->hook_pre
= 0; /* hooked cmd gets away. */
518 if (c
->next
->hookee_post
)
519 c
->next
->hookee_post
->hook_post
= 0; /* remove post hook */
520 /* :( no fishing metaphore */
530 /* Shorthands to the commands above. */
532 /* Add an element to the list of info subcommands. */
534 struct cmd_list_element
*
535 add_info (char *name
, void (*fun
) (char *, int), char *doc
)
537 return add_cmd (name
, no_class
, fun
, doc
, &infolist
);
540 /* Add an alias to the list of info subcommands. */
542 struct cmd_list_element
*
543 add_info_alias (char *name
, char *oldname
, int abbrev_flag
)
545 return add_alias_cmd (name
, oldname
, 0, abbrev_flag
, &infolist
);
548 /* Add an element to the list of commands. */
550 struct cmd_list_element
*
551 add_com (char *name
, enum command_class
class, void (*fun
) (char *, int),
554 return add_cmd (name
, class, fun
, doc
, &cmdlist
);
557 /* Add an alias or abbreviation command to the list of commands. */
559 struct cmd_list_element
*
560 add_com_alias (char *name
, char *oldname
, enum command_class
class,
563 return add_alias_cmd (name
, oldname
, class, abbrev_flag
, &cmdlist
);
566 /* Recursively walk the commandlist structures, and print out the
567 documentation of commands that match our regex in either their
568 name, or their documentation.
571 apropos_cmd (struct ui_file
*stream
, struct cmd_list_element
*commandlist
,
572 struct re_pattern_buffer
*regex
, char *prefix
)
574 register struct cmd_list_element
*c
;
575 int returnvalue
=1; /*Needed to avoid double printing*/
576 /* Walk through the commands */
577 for (c
=commandlist
;c
;c
=c
->next
)
581 /* Try to match against the name*/
582 returnvalue
=re_search(regex
,c
->name
,strlen(c
->name
),0,strlen(c
->name
),NULL
);
583 if (returnvalue
>= 0)
585 /* Stolen from help_cmd_list. We don't directly use
586 * help_cmd_list because it doesn't let us print out
589 fprintf_filtered (stream
, "%s%s -- ", prefix
, c
->name
);
590 print_doc_line (stream
, c
->doc
);
591 fputs_filtered ("\n", stream
);
592 returnvalue
=0; /*Set this so we don't print it again.*/
595 if (c
->doc
!= NULL
&& returnvalue
!= 0)
597 /* Try to match against documentation */
598 if (re_search(regex
,c
->doc
,strlen(c
->doc
),0,strlen(c
->doc
),NULL
) >=0)
600 /* Stolen from help_cmd_list. We don't directly use
601 * help_cmd_list because it doesn't let us print out
604 fprintf_filtered (stream
, "%s%s -- ", prefix
, c
->name
);
605 print_doc_line (stream
, c
->doc
);
606 fputs_filtered ("\n", stream
);
609 /* Check if this command has subcommands */
610 if (c
->prefixlist
!= NULL
)
612 /* Recursively call ourselves on the subcommand list,
613 passing the right prefix in.
615 apropos_cmd (stream
,*c
->prefixlist
,regex
,c
->prefixname
);
620 /* This command really has to deal with two things:
621 * 1) I want documentation on *this string* (usually called by
622 * "help commandname").
623 * 2) I want documentation on *this list* (usually called by
624 * giving a command that requires subcommands. Also called by saying
627 * I am going to split this into two seperate comamnds, help_cmd and
632 help_cmd (char *command
, struct ui_file
*stream
)
634 struct cmd_list_element
*c
;
635 extern struct cmd_list_element
*cmdlist
;
639 help_list (cmdlist
, "", all_classes
, stream
);
643 if (strcmp (command
, "all") == 0)
649 c
= lookup_cmd (&command
, cmdlist
, "", 0, 0);
654 /* There are three cases here.
655 If c->prefixlist is nonzero, we have a prefix command.
656 Print its documentation, then list its subcommands.
658 If c->func is non NULL, we really have a command. Print its
659 documentation and return.
661 If c->func is NULL, we have a class name. Print its
662 documentation (as if it were a command) and then set class to the
663 number of this class so that the commands in the class will be
666 fputs_filtered (c
->doc
, stream
);
667 fputs_filtered ("\n", stream
);
669 if (c
->prefixlist
== 0 && c
->func
!= NULL
)
671 fprintf_filtered (stream
, "\n");
673 /* If this is a prefix command, print it's subcommands */
675 help_list (*c
->prefixlist
, c
->prefixname
, all_commands
, stream
);
677 /* If this is a class name, print all of the commands in the class */
679 help_list (cmdlist
, "", c
->class, stream
);
681 if (c
->hook_pre
|| c
->hook_post
)
682 fprintf_filtered (stream
,
683 "\nThis command has a hook (or hooks) defined:\n");
686 fprintf_filtered (stream
,
687 "\tThis command is run after : %s (pre hook)\n",
690 fprintf_filtered (stream
,
691 "\tThis command is run before : %s (post hook)\n",
696 * Get a specific kind of help on a command list.
699 * CMDTYPE is the prefix to use in the title string.
700 * CLASS is the class with which to list the nodes of this list (see
701 * documentation for help_cmd_list below), As usual, ALL_COMMANDS for
702 * everything, ALL_CLASSES for just classes, and non-negative for only things
703 * in a specific class.
704 * and STREAM is the output stream on which to print things.
705 * If you call this routine with a class >= 0, it recurses.
708 help_list (struct cmd_list_element
*list
, char *cmdtype
,
709 enum command_class
class, struct ui_file
*stream
)
712 char *cmdtype1
, *cmdtype2
;
714 /* If CMDTYPE is "foo ", CMDTYPE1 gets " foo" and CMDTYPE2 gets "foo sub" */
715 len
= strlen (cmdtype
);
716 cmdtype1
= (char *) alloca (len
+ 1);
718 cmdtype2
= (char *) alloca (len
+ 4);
723 strncpy (cmdtype1
+ 1, cmdtype
, len
- 1);
725 strncpy (cmdtype2
, cmdtype
, len
- 1);
726 strcpy (cmdtype2
+ len
- 1, " sub");
729 if (class == all_classes
)
730 fprintf_filtered (stream
, "List of classes of %scommands:\n\n", cmdtype2
);
732 fprintf_filtered (stream
, "List of %scommands:\n\n", cmdtype2
);
734 help_cmd_list (list
, class, cmdtype
, (int) class >= 0, stream
);
736 if (class == all_classes
)
738 fprintf_filtered (stream
, "\n\
739 Type \"help%s\" followed by a class name for a list of commands in ",
742 fprintf_filtered (stream
, "that class.");
745 fprintf_filtered (stream
, "\nType \"help%s\" followed by %scommand name ",
748 fputs_filtered ("for ", stream
);
750 fputs_filtered ("full ", stream
);
752 fputs_filtered ("documentation.\n", stream
);
753 fputs_filtered ("Command name abbreviations are allowed if unambiguous.\n",
758 help_all (struct ui_file
*stream
)
760 struct cmd_list_element
*c
;
761 extern struct cmd_list_element
*cmdlist
;
763 for (c
= cmdlist
; c
; c
= c
->next
)
767 /* If this is a prefix command, print it's subcommands */
769 help_cmd_list (*c
->prefixlist
, all_commands
, c
->prefixname
, 0, stream
);
771 /* If this is a class name, print all of the commands in the class */
772 else if (c
->func
== NULL
)
773 help_cmd_list (cmdlist
, c
->class, "", 0, stream
);
777 /* Print only the first line of STR on STREAM. */
779 print_doc_line (struct ui_file
*stream
, char *str
)
781 static char *line_buffer
= 0;
782 static int line_size
;
788 line_buffer
= (char *) xmalloc (line_size
);
792 while (*p
&& *p
!= '\n' && *p
!= '.' && *p
!= ',')
794 if (p
- str
> line_size
- 1)
796 line_size
= p
- str
+ 1;
798 line_buffer
= (char *) xmalloc (line_size
);
800 strncpy (line_buffer
, str
, p
- str
);
801 line_buffer
[p
- str
] = '\0';
802 if (islower (line_buffer
[0]))
803 line_buffer
[0] = toupper (line_buffer
[0]);
804 ui_out_text (uiout
, line_buffer
);
808 * Implement a help command on command list LIST.
809 * RECURSE should be non-zero if this should be done recursively on
810 * all sublists of LIST.
811 * PREFIX is the prefix to print before each command name.
812 * STREAM is the stream upon which the output should be written.
814 * A non-negative class number to list only commands in that
816 * ALL_COMMANDS to list all commands in list.
817 * ALL_CLASSES to list all classes in list.
819 * Note that RECURSE will be active on *all* sublists, not just the
820 * ones selected by the criteria above (ie. the selection mechanism
821 * is at the low level, not the high-level).
824 help_cmd_list (struct cmd_list_element
*list
, enum command_class
class,
825 char *prefix
, int recurse
, struct ui_file
*stream
)
827 register struct cmd_list_element
*c
;
829 for (c
= list
; c
; c
= c
->next
)
831 if (c
->abbrev_flag
== 0 &&
832 (class == all_commands
833 || (class == all_classes
&& c
->func
== NULL
)
834 || (class == c
->class && c
->func
!= NULL
)))
836 fprintf_filtered (stream
, "%s%s -- ", prefix
, c
->name
);
837 print_doc_line (stream
, c
->doc
);
838 fputs_filtered ("\n", stream
);
841 && c
->prefixlist
!= 0
842 && c
->abbrev_flag
== 0)
843 help_cmd_list (*c
->prefixlist
, class, c
->prefixname
, 1, stream
);
848 /* Search the input clist for 'command'. Return the command if
849 found (or NULL if not), and return the number of commands
852 static struct cmd_list_element
*
853 find_cmd (char *command
, int len
, struct cmd_list_element
*clist
,
854 int ignore_help_classes
, int *nfound
)
856 struct cmd_list_element
*found
, *c
;
858 found
= (struct cmd_list_element
*) NULL
;
860 for (c
= clist
; c
; c
= c
->next
)
861 if (!strncmp (command
, c
->name
, len
)
862 && (!ignore_help_classes
|| c
->func
))
866 if (c
->name
[len
] == '\0')
875 /* This routine takes a line of TEXT and a CLIST in which to start the
876 lookup. When it returns it will have incremented the text pointer past
877 the section of text it matched, set *RESULT_LIST to point to the list in
878 which the last word was matched, and will return a pointer to the cmd
879 list element which the text matches. It will return NULL if no match at
880 all was possible. It will return -1 (cast appropriately, ick) if ambigous
881 matches are possible; in this case *RESULT_LIST will be set to point to
882 the list in which there are ambiguous choices (and *TEXT will be set to
883 the ambiguous text string).
885 If the located command was an abbreviation, this routine returns the base
886 command of the abbreviation.
888 It does no error reporting whatsoever; control will always return
889 to the superior routine.
891 In the case of an ambiguous return (-1), *RESULT_LIST will be set to point
892 at the prefix_command (ie. the best match) *or* (special case) will be NULL
893 if no prefix command was ever found. For example, in the case of "info a",
894 "info" matches without ambiguity, but "a" could be "args" or "address", so
895 *RESULT_LIST is set to the cmd_list_element for "info". So in this case
896 RESULT_LIST should not be interpeted as a pointer to the beginning of a
897 list; it simply points to a specific command. In the case of an ambiguous
898 return *TEXT is advanced past the last non-ambiguous prefix (e.g.
899 "info t" can be "info types" or "info target"; upon return *TEXT has been
900 advanced past "info ").
902 If RESULT_LIST is NULL, don't set *RESULT_LIST (but don't otherwise
903 affect the operation).
905 This routine does *not* modify the text pointed to by TEXT.
907 If IGNORE_HELP_CLASSES is nonzero, ignore any command list elements which
908 are actually help classes rather than commands (i.e. the function field of
909 the struct cmd_list_element is NULL). */
911 struct cmd_list_element
*
912 lookup_cmd_1 (char **text
, struct cmd_list_element
*clist
,
913 struct cmd_list_element
**result_list
, int ignore_help_classes
)
916 int len
, tmp
, nfound
;
917 struct cmd_list_element
*found
, *c
;
920 while (**text
== ' ' || **text
== '\t')
923 /* Treating underscores as part of command words is important
924 so that "set args_foo()" doesn't get interpreted as
925 "set args _foo()". */
926 /* NOTE: cagney/2003-02-13 The `tui_active' was previously
929 *p
&& (isalnum (*p
) || *p
== '-' || *p
== '_' ||
932 (*p
== '+' || *p
== '<' || *p
== '>' || *p
== '$')) ||
934 (xdb_commands
&& (*p
== '!' || *p
== '/' || *p
== '?')));
938 /* If nothing but whitespace, return 0. */
944 /* *text and p now bracket the first command word to lookup (and
945 it's length is len). We copy this into a local temporary */
948 command
= (char *) alloca (len
+ 1);
949 for (tmp
= 0; tmp
< len
; tmp
++)
951 char x
= (*text
)[tmp
];
959 found
= find_cmd (command
, len
, clist
, ignore_help_classes
, &nfound
);
962 ** We didn't find the command in the entered case, so lower case it
965 if (!found
|| nfound
== 0)
967 for (tmp
= 0; tmp
< len
; tmp
++)
969 char x
= command
[tmp
];
970 command
[tmp
] = isupper (x
) ? tolower (x
) : x
;
972 found
= find_cmd (command
, len
, clist
, ignore_help_classes
, &nfound
);
975 /* If nothing matches, we have a simple failure. */
981 if (result_list
!= NULL
)
982 /* Will be modified in calling routine
983 if we know what the prefix command is. */
985 return (struct cmd_list_element
*) -1; /* Ambiguous. */
988 /* We've matched something on this list. Move text pointer forward. */
992 if (found
->cmd_pointer
)
994 /* We drop the alias (abbreviation) in favor of the command it is
995 pointing to. If the alias is deprecated, though, we need to
996 warn the user about it before we drop it. Note that while we
997 are warning about the alias, we may also warn about the command
998 itself and we will adjust the appropriate DEPRECATED_WARN_USER
1001 if (found
->flags
& DEPRECATED_WARN_USER
)
1002 deprecated_cmd_warning (&line
);
1003 found
= found
->cmd_pointer
;
1005 /* If we found a prefix command, keep looking. */
1007 if (found
->prefixlist
)
1009 c
= lookup_cmd_1 (text
, *found
->prefixlist
, result_list
,
1010 ignore_help_classes
);
1013 /* Didn't find anything; this is as far as we got. */
1014 if (result_list
!= NULL
)
1015 *result_list
= clist
;
1018 else if (c
== (struct cmd_list_element
*) -1)
1020 /* We've gotten this far properly, but the next step
1021 is ambiguous. We need to set the result list to the best
1022 we've found (if an inferior hasn't already set it). */
1023 if (result_list
!= NULL
)
1025 /* This used to say *result_list = *found->prefixlist
1026 If that was correct, need to modify the documentation
1027 at the top of this function to clarify what is supposed
1029 *result_list
= found
;
1040 if (result_list
!= NULL
)
1041 *result_list
= clist
;
1046 /* All this hair to move the space to the front of cmdtype */
1049 undef_cmd_error (char *cmdtype
, char *q
)
1051 error ("Undefined %scommand: \"%s\". Try \"help%s%.*s\".",
1054 *cmdtype
? " " : "",
1055 (int) strlen (cmdtype
) - 1,
1059 /* Look up the contents of *LINE as a command in the command list LIST.
1060 LIST is a chain of struct cmd_list_element's.
1061 If it is found, return the struct cmd_list_element for that command
1062 and update *LINE to point after the command name, at the first argument.
1063 If not found, call error if ALLOW_UNKNOWN is zero
1064 otherwise (or if error returns) return zero.
1065 Call error if specified command is ambiguous,
1066 unless ALLOW_UNKNOWN is negative.
1067 CMDTYPE precedes the word "command" in the error message.
1069 If INGNORE_HELP_CLASSES is nonzero, ignore any command list
1070 elements which are actually help classes rather than commands (i.e.
1071 the function field of the struct cmd_list_element is 0). */
1073 struct cmd_list_element
*
1074 lookup_cmd (char **line
, struct cmd_list_element
*list
, char *cmdtype
,
1075 int allow_unknown
, int ignore_help_classes
)
1077 struct cmd_list_element
*last_list
= 0;
1078 struct cmd_list_element
*c
=
1079 lookup_cmd_1 (line
, list
, &last_list
, ignore_help_classes
);
1081 /* Note: Do not remove trailing whitespace here because this
1082 would be wrong for complete_command. Jim Kingdon */
1089 error ("Lack of needed %scommand", cmdtype
);
1092 char *p
= *line
, *q
;
1094 while (isalnum (*p
) || *p
== '-')
1097 q
= (char *) alloca (p
- *line
+ 1);
1098 strncpy (q
, *line
, p
- *line
);
1099 q
[p
- *line
] = '\0';
1100 undef_cmd_error (cmdtype
, q
);
1106 else if (c
== (struct cmd_list_element
*) -1)
1108 /* Ambigous. Local values should be off prefixlist or called
1110 int local_allow_unknown
= (last_list
? last_list
->allow_unknown
:
1112 char *local_cmdtype
= last_list
? last_list
->prefixname
: cmdtype
;
1113 struct cmd_list_element
*local_list
=
1114 (last_list
? *(last_list
->prefixlist
) : list
);
1116 if (local_allow_unknown
< 0)
1119 return last_list
; /* Found something. */
1121 return 0; /* Found nothing. */
1125 /* Report as error. */
1130 ((*line
)[amb_len
] && (*line
)[amb_len
] != ' '
1131 && (*line
)[amb_len
] != '\t');
1136 for (c
= local_list
; c
; c
= c
->next
)
1137 if (!strncmp (*line
, c
->name
, amb_len
))
1139 if (strlen (ambbuf
) + strlen (c
->name
) + 6 < (int) sizeof ambbuf
)
1141 if (strlen (ambbuf
))
1142 strcat (ambbuf
, ", ");
1143 strcat (ambbuf
, c
->name
);
1147 strcat (ambbuf
, "..");
1151 error ("Ambiguous %scommand \"%s\": %s.", local_cmdtype
,
1153 return 0; /* lint */
1158 /* We've got something. It may still not be what the caller
1159 wants (if this command *needs* a subcommand). */
1160 while (**line
== ' ' || **line
== '\t')
1163 if (c
->prefixlist
&& **line
&& !c
->allow_unknown
)
1164 undef_cmd_error (c
->prefixname
, *line
);
1166 /* Seems to be what he wants. Return it. */
1172 /* We are here presumably because an alias or command in *TEXT is
1173 deprecated and a warning message should be generated. This function
1174 decodes *TEXT and potentially generates a warning message as outlined
1177 Example for 'set endian big' which has a fictitious alias 'seb'.
1179 If alias wasn't used in *TEXT, and the command is deprecated:
1180 "warning: 'set endian big' is deprecated."
1182 If alias was used, and only the alias is deprecated:
1183 "warning: 'seb' an alias for the command 'set endian big' is deprecated."
1185 If alias was used and command is deprecated (regardless of whether the
1186 alias itself is deprecated:
1188 "warning: 'set endian big' (seb) is deprecated."
1190 After the message has been sent, clear the appropriate flags in the
1191 command and/or the alias so the user is no longer bothered.
1195 deprecated_cmd_warning (char **text
)
1197 struct cmd_list_element
*alias
= NULL
;
1198 struct cmd_list_element
*prefix_cmd
= NULL
;
1199 struct cmd_list_element
*cmd
= NULL
;
1200 struct cmd_list_element
*c
;
1203 if (!lookup_cmd_composition (*text
, &alias
, &prefix_cmd
, &cmd
))
1204 /* return if text doesn't evaluate to a command */
1207 if (!((alias
? (alias
->flags
& DEPRECATED_WARN_USER
) : 0)
1208 || (cmd
->flags
& DEPRECATED_WARN_USER
) ) )
1209 /* return if nothing is deprecated */
1212 printf_filtered ("Warning:");
1214 if (alias
&& !(cmd
->flags
& CMD_DEPRECATED
))
1215 printf_filtered (" '%s', an alias for the", alias
->name
);
1217 printf_filtered (" command '");
1220 printf_filtered ("%s", prefix_cmd
->prefixname
);
1222 printf_filtered ("%s", cmd
->name
);
1224 if (alias
&& (cmd
->flags
& CMD_DEPRECATED
))
1225 printf_filtered ("' (%s) is deprecated.\n", alias
->name
);
1227 printf_filtered ("' is deprecated.\n");
1230 /* if it is only the alias that is deprecated, we want to indicate the
1231 new alias, otherwise we'll indicate the new command */
1233 if (alias
&& !(cmd
->flags
& CMD_DEPRECATED
))
1235 if (alias
->replacement
)
1236 printf_filtered ("Use '%s'.\n\n", alias
->replacement
);
1238 printf_filtered ("No alternative known.\n\n");
1242 if (cmd
->replacement
)
1243 printf_filtered ("Use '%s'.\n\n", cmd
->replacement
);
1245 printf_filtered ("No alternative known.\n\n");
1248 /* We've warned you, now we'll keep quiet */
1250 alias
->flags
&= ~DEPRECATED_WARN_USER
;
1252 cmd
->flags
&= ~DEPRECATED_WARN_USER
;
1257 /* Look up the contents of LINE as a command in the command list 'cmdlist'.
1258 Return 1 on success, 0 on failure.
1260 If LINE refers to an alias, *alias will point to that alias.
1262 If LINE is a postfix command (i.e. one that is preceeded by a prefix
1263 command) set *prefix_cmd.
1265 Set *cmd to point to the command LINE indicates.
1267 If any of *alias, *prefix_cmd, or *cmd cannot be determined or do not
1268 exist, they are NULL when we return.
1272 lookup_cmd_composition (char *text
,
1273 struct cmd_list_element
**alias
,
1274 struct cmd_list_element
**prefix_cmd
,
1275 struct cmd_list_element
**cmd
)
1278 int len
, tmp
, nfound
;
1279 struct cmd_list_element
*cur_list
;
1280 struct cmd_list_element
*prev_cmd
;
1289 /* Go through as many command lists as we need to
1290 to find the command TEXT refers to. */
1294 while (*text
== ' ' || *text
== '\t')
1297 /* Treating underscores as part of command words is important
1298 so that "set args_foo()" doesn't get interpreted as
1299 "set args _foo()". */
1300 /* NOTE: cagney/2003-02-13 The `tui_active' was previously
1303 *p
&& (isalnum (*p
) || *p
== '-' || *p
== '_' ||
1306 (*p
== '+' || *p
== '<' || *p
== '>' || *p
== '$')) ||
1308 (xdb_commands
&& (*p
== '!' || *p
== '/' || *p
== '?')));
1312 /* If nothing but whitespace, return. */
1318 /* text and p now bracket the first command word to lookup (and
1319 it's length is len). We copy this into a local temporary */
1321 command
= (char *) alloca (len
+ 1);
1322 for (tmp
= 0; tmp
< len
; tmp
++)
1327 command
[len
] = '\0';
1332 *cmd
= find_cmd (command
, len
, cur_list
, 1, &nfound
);
1334 /* We didn't find the command in the entered case, so lower case it
1337 if (!*cmd
|| nfound
== 0)
1339 for (tmp
= 0; tmp
< len
; tmp
++)
1341 char x
= command
[tmp
];
1342 command
[tmp
] = isupper (x
) ? tolower (x
) : x
;
1344 *cmd
= find_cmd (command
, len
, cur_list
, 1, &nfound
);
1347 if (*cmd
== (struct cmd_list_element
*) -1)
1349 return 0; /* ambiguous */
1353 return 0; /* nothing found */
1356 if ((*cmd
)->cmd_pointer
)
1358 /* cmd was actually an alias, we note that an alias was used
1359 (by assigning *alais) and we set *cmd.
1362 *cmd
= (*cmd
)->cmd_pointer
;
1364 *prefix_cmd
= prev_cmd
;
1366 if ((*cmd
)->prefixlist
)
1367 cur_list
= *(*cmd
)->prefixlist
;
1375 /* Helper function for SYMBOL_COMPLETION_FUNCTION. */
1377 /* Return a vector of char pointers which point to the different
1378 possible completions in LIST of TEXT.
1380 WORD points in the same buffer as TEXT, and completions should be
1381 returned relative to this position. For example, suppose TEXT is "foo"
1382 and we want to complete to "foobar". If WORD is "oo", return
1383 "oobar"; if WORD is "baz/foo", return "baz/foobar". */
1386 complete_on_cmdlist (struct cmd_list_element
*list
, char *text
, char *word
)
1388 struct cmd_list_element
*ptr
;
1390 int sizeof_matchlist
;
1392 int textlen
= strlen (text
);
1394 sizeof_matchlist
= 10;
1395 matchlist
= (char **) xmalloc (sizeof_matchlist
* sizeof (char *));
1398 for (ptr
= list
; ptr
; ptr
= ptr
->next
)
1399 if (!strncmp (ptr
->name
, text
, textlen
)
1400 && !ptr
->abbrev_flag
1402 || ptr
->prefixlist
))
1404 if (matches
== sizeof_matchlist
)
1406 sizeof_matchlist
*= 2;
1407 matchlist
= (char **) xrealloc ((char *) matchlist
,
1409 * sizeof (char *)));
1412 matchlist
[matches
] = (char *)
1413 xmalloc (strlen (word
) + strlen (ptr
->name
) + 1);
1415 strcpy (matchlist
[matches
], ptr
->name
);
1416 else if (word
> text
)
1418 /* Return some portion of ptr->name. */
1419 strcpy (matchlist
[matches
], ptr
->name
+ (word
- text
));
1423 /* Return some of text plus ptr->name. */
1424 strncpy (matchlist
[matches
], word
, text
- word
);
1425 matchlist
[matches
][text
- word
] = '\0';
1426 strcat (matchlist
[matches
], ptr
->name
);
1438 matchlist
= (char **) xrealloc ((char *) matchlist
, ((matches
+ 1)
1439 * sizeof (char *)));
1440 matchlist
[matches
] = (char *) 0;
1446 /* Helper function for SYMBOL_COMPLETION_FUNCTION. */
1448 /* Return a vector of char pointers which point to the different
1449 possible completions in CMD of TEXT.
1451 WORD points in the same buffer as TEXT, and completions should be
1452 returned relative to this position. For example, suppose TEXT is "foo"
1453 and we want to complete to "foobar". If WORD is "oo", return
1454 "oobar"; if WORD is "baz/foo", return "baz/foobar". */
1457 complete_on_enum (const char *enumlist
[],
1462 int sizeof_matchlist
;
1464 int textlen
= strlen (text
);
1468 sizeof_matchlist
= 10;
1469 matchlist
= (char **) xmalloc (sizeof_matchlist
* sizeof (char *));
1472 for (i
= 0; (name
= enumlist
[i
]) != NULL
; i
++)
1473 if (strncmp (name
, text
, textlen
) == 0)
1475 if (matches
== sizeof_matchlist
)
1477 sizeof_matchlist
*= 2;
1478 matchlist
= (char **) xrealloc ((char *) matchlist
,
1480 * sizeof (char *)));
1483 matchlist
[matches
] = (char *)
1484 xmalloc (strlen (word
) + strlen (name
) + 1);
1486 strcpy (matchlist
[matches
], name
);
1487 else if (word
> text
)
1489 /* Return some portion of name. */
1490 strcpy (matchlist
[matches
], name
+ (word
- text
));
1494 /* Return some of text plus name. */
1495 strncpy (matchlist
[matches
], word
, text
- word
);
1496 matchlist
[matches
][text
- word
] = '\0';
1497 strcat (matchlist
[matches
], name
);
1509 matchlist
= (char **) xrealloc ((char *) matchlist
, ((matches
+ 1)
1510 * sizeof (char *)));
1511 matchlist
[matches
] = (char *) 0;
1518 /* check function pointer */
1520 cmd_func_p (struct cmd_list_element
*cmd
)
1522 return (cmd
->func
!= NULL
);
1526 /* call the command function */
1528 cmd_func (struct cmd_list_element
*cmd
, char *args
, int from_tty
)
1530 if (cmd_func_p (cmd
))
1531 (*cmd
->func
) (cmd
, args
, from_tty
);
1533 error ("Invalid command");