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"
28 #include "cli/cli-cmds.h"
29 #include "cli/cli-decode.h"
31 /* Prototypes for local functions */
33 static void undef_cmd_error (char *, char *);
35 static struct cmd_list_element
*find_cmd (char *command
,
37 struct cmd_list_element
*clist
,
38 int ignore_help_classes
,
41 static void help_all (struct ui_file
*stream
);
43 /* Set the callback function for the specified command. For each both
44 the commands callback and func() are set. The latter set to a
45 bounce function (unless cfunc / sfunc is NULL that is). */
48 do_cfunc (struct cmd_list_element
*c
, char *args
, int from_tty
)
50 c
->function
.cfunc (args
, from_tty
); /* Ok. */
54 set_cmd_cfunc (struct cmd_list_element
*cmd
,
55 void (*cfunc
) (char *args
, int from_tty
))
61 cmd
->function
.cfunc
= cfunc
; /* Ok. */
65 do_sfunc (struct cmd_list_element
*c
, char *args
, int from_tty
)
67 c
->function
.sfunc (args
, from_tty
, c
); /* Ok. */
71 set_cmd_sfunc (struct cmd_list_element
*cmd
,
72 void (*sfunc
) (char *args
, int from_tty
,
73 struct cmd_list_element
* c
))
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
302 or some sublist thereof).
303 CLASS is as in add_cmd.
304 VAR_TYPE is the kind of thing we are setting.
305 VAR is address of the variable being controlled by this command.
306 DOC is the documentation string. */
308 struct cmd_list_element
*
309 add_set_cmd (char *name
,
310 enum command_class
class,
314 struct cmd_list_element
**list
)
316 struct cmd_list_element
*c
= add_cmd (name
, class, NULL
, doc
, list
);
319 c
->var_type
= var_type
;
321 /* This needs to be something besides NULL so that this isn't
322 treated as a help class. */
323 set_cmd_sfunc (c
, empty_sfunc
);
327 /* Add element named NAME to command list LIST (the list for set
328 or some sublist thereof).
329 CLASS is as in add_cmd.
330 ENUMLIST is a list of strings which may follow NAME.
331 VAR is address of the variable which will contain the matching string
333 DOC is the documentation string. */
335 struct cmd_list_element
*
336 add_set_enum_cmd (char *name
,
337 enum command_class
class,
338 const char *enumlist
[],
341 struct cmd_list_element
**list
)
343 struct cmd_list_element
*c
344 = add_set_cmd (name
, class, var_enum
, var
, doc
, list
);
350 /* Add element named NAME to command list LIST (the list for set
351 or some sublist thereof).
352 CLASS is as in add_cmd.
353 VAR is address of the variable which will contain the value.
354 DOC is the documentation string. */
355 struct cmd_list_element
*
356 add_set_auto_boolean_cmd (char *name
,
357 enum command_class
class,
358 enum cmd_auto_boolean
*var
,
360 struct cmd_list_element
**list
)
362 static const char *auto_boolean_enums
[] = { "on", "off", "auto", NULL
};
363 struct cmd_list_element
*c
;
364 c
= add_set_cmd (name
, class, var_auto_boolean
, var
, doc
, list
);
365 c
->enums
= auto_boolean_enums
;
369 /* Add element named NAME to command list LIST (the list for set
370 or some sublist thereof).
371 CLASS is as in add_cmd.
372 VAR is address of the variable which will contain the value.
373 DOC is the documentation string. */
374 struct cmd_list_element
*
375 add_set_boolean_cmd (char *name
,
376 enum command_class
class,
379 struct cmd_list_element
**list
)
381 static const char *boolean_enums
[] = { "on", "off", NULL
};
382 struct cmd_list_element
*c
;
383 c
= add_set_cmd (name
, class, var_boolean
, var
, doc
, list
);
384 c
->enums
= boolean_enums
;
388 /* Where SETCMD has already been added, add the corresponding show
389 command to LIST and return a pointer to the added command (not
390 necessarily the head of LIST). */
391 struct cmd_list_element
*
392 add_show_from_set (struct cmd_list_element
*setcmd
,
393 struct cmd_list_element
**list
)
395 struct cmd_list_element
*showcmd
=
396 (struct cmd_list_element
*) xmalloc (sizeof (struct cmd_list_element
));
397 struct cmd_list_element
*p
;
399 memcpy (showcmd
, setcmd
, sizeof (struct cmd_list_element
));
400 delete_cmd (showcmd
->name
, list
);
401 showcmd
->type
= show_cmd
;
403 /* Replace "set " at start of docstring with "show ". */
404 if (setcmd
->doc
[0] == 'S' && setcmd
->doc
[1] == 'e'
405 && setcmd
->doc
[2] == 't' && setcmd
->doc
[3] == ' ')
406 showcmd
->doc
= concat ("Show ", setcmd
->doc
+ 4, NULL
);
408 fprintf_unfiltered (gdb_stderr
, "GDB internal error: Bad docstring for set command\n");
410 if (*list
== NULL
|| strcmp ((*list
)->name
, showcmd
->name
) >= 0)
412 showcmd
->next
= *list
;
418 while (p
->next
&& strcmp (p
->next
->name
, showcmd
->name
) <= 0)
422 showcmd
->next
= p
->next
;
429 /* Remove the command named NAME from the command list. */
432 delete_cmd (char *name
, struct cmd_list_element
**list
)
434 register struct cmd_list_element
*c
;
435 struct cmd_list_element
*p
;
437 while (*list
&& STREQ ((*list
)->name
, name
))
439 if ((*list
)->hookee_pre
)
440 (*list
)->hookee_pre
->hook_pre
= 0; /* Hook slips out of its mouth */
441 if ((*list
)->hookee_post
)
442 (*list
)->hookee_post
->hook_post
= 0; /* Hook slips out of its bottom */
449 for (c
= *list
; c
->next
;)
451 if (STREQ (c
->next
->name
, name
))
453 if (c
->next
->hookee_pre
)
454 c
->next
->hookee_pre
->hook_pre
= 0; /* hooked cmd gets away. */
455 if (c
->next
->hookee_post
)
456 c
->next
->hookee_post
->hook_post
= 0; /* remove post hook */
457 /* :( no fishing metaphore */
467 /* Shorthands to the commands above. */
469 /* Add an element to the list of info subcommands. */
471 struct cmd_list_element
*
472 add_info (char *name
, void (*fun
) (char *, int), char *doc
)
474 return add_cmd (name
, no_class
, fun
, doc
, &infolist
);
477 /* Add an alias to the list of info subcommands. */
479 struct cmd_list_element
*
480 add_info_alias (char *name
, char *oldname
, int abbrev_flag
)
482 return add_alias_cmd (name
, oldname
, 0, abbrev_flag
, &infolist
);
485 /* Add an element to the list of commands. */
487 struct cmd_list_element
*
488 add_com (char *name
, enum command_class
class, void (*fun
) (char *, int),
491 return add_cmd (name
, class, fun
, doc
, &cmdlist
);
494 /* Add an alias or abbreviation command to the list of commands. */
496 struct cmd_list_element
*
497 add_com_alias (char *name
, char *oldname
, enum command_class
class,
500 return add_alias_cmd (name
, oldname
, class, abbrev_flag
, &cmdlist
);
503 /* Recursively walk the commandlist structures, and print out the
504 documentation of commands that match our regex in either their
505 name, or their documentation.
508 apropos_cmd (struct ui_file
*stream
, struct cmd_list_element
*commandlist
,
509 struct re_pattern_buffer
*regex
, char *prefix
)
511 register struct cmd_list_element
*c
;
512 int returnvalue
=1; /*Needed to avoid double printing*/
513 /* Walk through the commands */
514 for (c
=commandlist
;c
;c
=c
->next
)
518 /* Try to match against the name*/
519 returnvalue
=re_search(regex
,c
->name
,strlen(c
->name
),0,strlen(c
->name
),NULL
);
520 if (returnvalue
>= 0)
522 /* Stolen from help_cmd_list. We don't directly use
523 * help_cmd_list because it doesn't let us print out
526 fprintf_filtered (stream
, "%s%s -- ", prefix
, c
->name
);
527 print_doc_line (stream
, c
->doc
);
528 fputs_filtered ("\n", stream
);
529 returnvalue
=0; /*Set this so we don't print it again.*/
532 if (c
->doc
!= NULL
&& returnvalue
!= 0)
534 /* Try to match against documentation */
535 if (re_search(regex
,c
->doc
,strlen(c
->doc
),0,strlen(c
->doc
),NULL
) >=0)
537 /* Stolen from help_cmd_list. We don't directly use
538 * help_cmd_list because it doesn't let us print out
541 fprintf_filtered (stream
, "%s%s -- ", prefix
, c
->name
);
542 print_doc_line (stream
, c
->doc
);
543 fputs_filtered ("\n", stream
);
546 /* Check if this command has subcommands */
547 if (c
->prefixlist
!= NULL
)
549 /* Recursively call ourselves on the subcommand list,
550 passing the right prefix in.
552 apropos_cmd (stream
,*c
->prefixlist
,regex
,c
->prefixname
);
557 /* This command really has to deal with two things:
558 * 1) I want documentation on *this string* (usually called by
559 * "help commandname").
560 * 2) I want documentation on *this list* (usually called by
561 * giving a command that requires subcommands. Also called by saying
564 * I am going to split this into two seperate comamnds, help_cmd and
569 help_cmd (char *command
, struct ui_file
*stream
)
571 struct cmd_list_element
*c
;
572 extern struct cmd_list_element
*cmdlist
;
576 help_list (cmdlist
, "", all_classes
, stream
);
580 if (strcmp (command
, "all") == 0)
586 c
= lookup_cmd (&command
, cmdlist
, "", 0, 0);
591 /* There are three cases here.
592 If c->prefixlist is nonzero, we have a prefix command.
593 Print its documentation, then list its subcommands.
595 If c->func is non NULL, we really have a command. Print its
596 documentation and return.
598 If c->func is NULL, we have a class name. Print its
599 documentation (as if it were a command) and then set class to the
600 number of this class so that the commands in the class will be
603 fputs_filtered (c
->doc
, stream
);
604 fputs_filtered ("\n", stream
);
606 if (c
->prefixlist
== 0 && c
->func
!= NULL
)
608 fprintf_filtered (stream
, "\n");
610 /* If this is a prefix command, print it's subcommands */
612 help_list (*c
->prefixlist
, c
->prefixname
, all_commands
, stream
);
614 /* If this is a class name, print all of the commands in the class */
616 help_list (cmdlist
, "", c
->class, stream
);
618 if (c
->hook_pre
|| c
->hook_post
)
619 fprintf_filtered (stream
,
620 "\nThis command has a hook (or hooks) defined:\n");
623 fprintf_filtered (stream
,
624 "\tThis command is run after : %s (pre hook)\n",
627 fprintf_filtered (stream
,
628 "\tThis command is run before : %s (post hook)\n",
633 * Get a specific kind of help on a command list.
636 * CMDTYPE is the prefix to use in the title string.
637 * CLASS is the class with which to list the nodes of this list (see
638 * documentation for help_cmd_list below), As usual, ALL_COMMANDS for
639 * everything, ALL_CLASSES for just classes, and non-negative for only things
640 * in a specific class.
641 * and STREAM is the output stream on which to print things.
642 * If you call this routine with a class >= 0, it recurses.
645 help_list (struct cmd_list_element
*list
, char *cmdtype
,
646 enum command_class
class, struct ui_file
*stream
)
649 char *cmdtype1
, *cmdtype2
;
651 /* If CMDTYPE is "foo ", CMDTYPE1 gets " foo" and CMDTYPE2 gets "foo sub" */
652 len
= strlen (cmdtype
);
653 cmdtype1
= (char *) alloca (len
+ 1);
655 cmdtype2
= (char *) alloca (len
+ 4);
660 strncpy (cmdtype1
+ 1, cmdtype
, len
- 1);
662 strncpy (cmdtype2
, cmdtype
, len
- 1);
663 strcpy (cmdtype2
+ len
- 1, " sub");
666 if (class == all_classes
)
667 fprintf_filtered (stream
, "List of classes of %scommands:\n\n", cmdtype2
);
669 fprintf_filtered (stream
, "List of %scommands:\n\n", cmdtype2
);
671 help_cmd_list (list
, class, cmdtype
, (int) class >= 0, stream
);
673 if (class == all_classes
)
674 fprintf_filtered (stream
, "\n\
675 Type \"help%s\" followed by a class name for a list of commands in that class.",
678 fprintf_filtered (stream
, "\n\
679 Type \"help%s\" followed by %scommand name for full documentation.\n\
680 Command name abbreviations are allowed if unambiguous.\n",
685 help_all (struct ui_file
*stream
)
687 struct cmd_list_element
*c
;
688 extern struct cmd_list_element
*cmdlist
;
690 for (c
= cmdlist
; c
; c
= c
->next
)
694 /* If this is a prefix command, print it's subcommands */
696 help_cmd_list (*c
->prefixlist
, all_commands
, c
->prefixname
, 0, stream
);
698 /* If this is a class name, print all of the commands in the class */
699 else if (c
->func
== NULL
)
700 help_cmd_list (cmdlist
, c
->class, "", 0, stream
);
704 /* Print only the first line of STR on STREAM. */
706 print_doc_line (struct ui_file
*stream
, char *str
)
708 static char *line_buffer
= 0;
709 static int line_size
;
715 line_buffer
= (char *) xmalloc (line_size
);
719 while (*p
&& *p
!= '\n' && *p
!= '.' && *p
!= ',')
721 if (p
- str
> line_size
- 1)
723 line_size
= p
- str
+ 1;
725 line_buffer
= (char *) xmalloc (line_size
);
727 strncpy (line_buffer
, str
, p
- str
);
728 line_buffer
[p
- str
] = '\0';
729 if (islower (line_buffer
[0]))
730 line_buffer
[0] = toupper (line_buffer
[0]);
731 ui_out_text (uiout
, line_buffer
);
735 * Implement a help command on command list LIST.
736 * RECURSE should be non-zero if this should be done recursively on
737 * all sublists of LIST.
738 * PREFIX is the prefix to print before each command name.
739 * STREAM is the stream upon which the output should be written.
741 * A non-negative class number to list only commands in that
743 * ALL_COMMANDS to list all commands in list.
744 * ALL_CLASSES to list all classes in list.
746 * Note that RECURSE will be active on *all* sublists, not just the
747 * ones selected by the criteria above (ie. the selection mechanism
748 * is at the low level, not the high-level).
751 help_cmd_list (struct cmd_list_element
*list
, enum command_class
class,
752 char *prefix
, int recurse
, struct ui_file
*stream
)
754 register struct cmd_list_element
*c
;
756 for (c
= list
; c
; c
= c
->next
)
758 if (c
->abbrev_flag
== 0 &&
759 (class == all_commands
760 || (class == all_classes
&& c
->func
== NULL
)
761 || (class == c
->class && c
->func
!= NULL
)))
763 fprintf_filtered (stream
, "%s%s -- ", prefix
, c
->name
);
764 print_doc_line (stream
, c
->doc
);
765 fputs_filtered ("\n", stream
);
768 && c
->prefixlist
!= 0
769 && c
->abbrev_flag
== 0)
770 help_cmd_list (*c
->prefixlist
, class, c
->prefixname
, 1, stream
);
775 /* Search the input clist for 'command'. Return the command if
776 found (or NULL if not), and return the number of commands
779 static struct cmd_list_element
*
780 find_cmd (char *command
, int len
, struct cmd_list_element
*clist
,
781 int ignore_help_classes
, int *nfound
)
783 struct cmd_list_element
*found
, *c
;
785 found
= (struct cmd_list_element
*) NULL
;
787 for (c
= clist
; c
; c
= c
->next
)
788 if (!strncmp (command
, c
->name
, len
)
789 && (!ignore_help_classes
|| c
->func
))
793 if (c
->name
[len
] == '\0')
802 /* This routine takes a line of TEXT and a CLIST in which to start the
803 lookup. When it returns it will have incremented the text pointer past
804 the section of text it matched, set *RESULT_LIST to point to the list in
805 which the last word was matched, and will return a pointer to the cmd
806 list element which the text matches. It will return NULL if no match at
807 all was possible. It will return -1 (cast appropriately, ick) if ambigous
808 matches are possible; in this case *RESULT_LIST will be set to point to
809 the list in which there are ambiguous choices (and *TEXT will be set to
810 the ambiguous text string).
812 If the located command was an abbreviation, this routine returns the base
813 command of the abbreviation.
815 It does no error reporting whatsoever; control will always return
816 to the superior routine.
818 In the case of an ambiguous return (-1), *RESULT_LIST will be set to point
819 at the prefix_command (ie. the best match) *or* (special case) will be NULL
820 if no prefix command was ever found. For example, in the case of "info a",
821 "info" matches without ambiguity, but "a" could be "args" or "address", so
822 *RESULT_LIST is set to the cmd_list_element for "info". So in this case
823 RESULT_LIST should not be interpeted as a pointer to the beginning of a
824 list; it simply points to a specific command. In the case of an ambiguous
825 return *TEXT is advanced past the last non-ambiguous prefix (e.g.
826 "info t" can be "info types" or "info target"; upon return *TEXT has been
827 advanced past "info ").
829 If RESULT_LIST is NULL, don't set *RESULT_LIST (but don't otherwise
830 affect the operation).
832 This routine does *not* modify the text pointed to by TEXT.
834 If IGNORE_HELP_CLASSES is nonzero, ignore any command list elements which
835 are actually help classes rather than commands (i.e. the function field of
836 the struct cmd_list_element is NULL). */
838 struct cmd_list_element
*
839 lookup_cmd_1 (char **text
, struct cmd_list_element
*clist
,
840 struct cmd_list_element
**result_list
, int ignore_help_classes
)
843 int len
, tmp
, nfound
;
844 struct cmd_list_element
*found
, *c
;
847 while (**text
== ' ' || **text
== '\t')
850 /* Treating underscores as part of command words is important
851 so that "set args_foo()" doesn't get interpreted as
852 "set args _foo()". */
854 *p
&& (isalnum (*p
) || *p
== '-' || *p
== '_' ||
856 (*p
== '+' || *p
== '<' || *p
== '>' || *p
== '$')) ||
857 (xdb_commands
&& (*p
== '!' || *p
== '/' || *p
== '?')));
861 /* If nothing but whitespace, return 0. */
867 /* *text and p now bracket the first command word to lookup (and
868 it's length is len). We copy this into a local temporary */
871 command
= (char *) alloca (len
+ 1);
872 for (tmp
= 0; tmp
< len
; tmp
++)
874 char x
= (*text
)[tmp
];
882 found
= find_cmd (command
, len
, clist
, ignore_help_classes
, &nfound
);
885 ** We didn't find the command in the entered case, so lower case it
888 if (!found
|| nfound
== 0)
890 for (tmp
= 0; tmp
< len
; tmp
++)
892 char x
= command
[tmp
];
893 command
[tmp
] = isupper (x
) ? tolower (x
) : x
;
895 found
= find_cmd (command
, len
, clist
, ignore_help_classes
, &nfound
);
898 /* If nothing matches, we have a simple failure. */
904 if (result_list
!= NULL
)
905 /* Will be modified in calling routine
906 if we know what the prefix command is. */
908 return (struct cmd_list_element
*) -1; /* Ambiguous. */
911 /* We've matched something on this list. Move text pointer forward. */
915 if (found
->cmd_pointer
)
917 /* We drop the alias (abbreviation) in favor of the command it is
918 pointing to. If the alias is deprecated, though, we need to
919 warn the user about it before we drop it. Note that while we
920 are warning about the alias, we may also warn about the command
921 itself and we will adjust the appropriate DEPRECATED_WARN_USER
924 if (found
->flags
& DEPRECATED_WARN_USER
)
925 deprecated_cmd_warning (&line
);
926 found
= found
->cmd_pointer
;
928 /* If we found a prefix command, keep looking. */
930 if (found
->prefixlist
)
932 c
= lookup_cmd_1 (text
, *found
->prefixlist
, result_list
,
933 ignore_help_classes
);
936 /* Didn't find anything; this is as far as we got. */
937 if (result_list
!= NULL
)
938 *result_list
= clist
;
941 else if (c
== (struct cmd_list_element
*) -1)
943 /* We've gotten this far properly, but the next step
944 is ambiguous. We need to set the result list to the best
945 we've found (if an inferior hasn't already set it). */
946 if (result_list
!= NULL
)
948 /* This used to say *result_list = *found->prefixlist
949 If that was correct, need to modify the documentation
950 at the top of this function to clarify what is supposed
952 *result_list
= found
;
963 if (result_list
!= NULL
)
964 *result_list
= clist
;
969 /* All this hair to move the space to the front of cmdtype */
972 undef_cmd_error (char *cmdtype
, char *q
)
974 error ("Undefined %scommand: \"%s\". Try \"help%s%.*s\".",
978 strlen (cmdtype
) - 1,
982 /* Look up the contents of *LINE as a command in the command list LIST.
983 LIST is a chain of struct cmd_list_element's.
984 If it is found, return the struct cmd_list_element for that command
985 and update *LINE to point after the command name, at the first argument.
986 If not found, call error if ALLOW_UNKNOWN is zero
987 otherwise (or if error returns) return zero.
988 Call error if specified command is ambiguous,
989 unless ALLOW_UNKNOWN is negative.
990 CMDTYPE precedes the word "command" in the error message.
992 If INGNORE_HELP_CLASSES is nonzero, ignore any command list
993 elements which are actually help classes rather than commands (i.e.
994 the function field of the struct cmd_list_element is 0). */
996 struct cmd_list_element
*
997 lookup_cmd (char **line
, struct cmd_list_element
*list
, char *cmdtype
,
998 int allow_unknown
, int ignore_help_classes
)
1000 struct cmd_list_element
*last_list
= 0;
1001 struct cmd_list_element
*c
=
1002 lookup_cmd_1 (line
, list
, &last_list
, ignore_help_classes
);
1004 /* Note: Do not remove trailing whitespace here because this
1005 would be wrong for complete_command. Jim Kingdon */
1012 error ("Lack of needed %scommand", cmdtype
);
1015 char *p
= *line
, *q
;
1017 while (isalnum (*p
) || *p
== '-')
1020 q
= (char *) alloca (p
- *line
+ 1);
1021 strncpy (q
, *line
, p
- *line
);
1022 q
[p
- *line
] = '\0';
1023 undef_cmd_error (cmdtype
, q
);
1029 else if (c
== (struct cmd_list_element
*) -1)
1031 /* Ambigous. Local values should be off prefixlist or called
1033 int local_allow_unknown
= (last_list
? last_list
->allow_unknown
:
1035 char *local_cmdtype
= last_list
? last_list
->prefixname
: cmdtype
;
1036 struct cmd_list_element
*local_list
=
1037 (last_list
? *(last_list
->prefixlist
) : list
);
1039 if (local_allow_unknown
< 0)
1042 return last_list
; /* Found something. */
1044 return 0; /* Found nothing. */
1048 /* Report as error. */
1053 ((*line
)[amb_len
] && (*line
)[amb_len
] != ' '
1054 && (*line
)[amb_len
] != '\t');
1059 for (c
= local_list
; c
; c
= c
->next
)
1060 if (!strncmp (*line
, c
->name
, amb_len
))
1062 if (strlen (ambbuf
) + strlen (c
->name
) + 6 < (int) sizeof ambbuf
)
1064 if (strlen (ambbuf
))
1065 strcat (ambbuf
, ", ");
1066 strcat (ambbuf
, c
->name
);
1070 strcat (ambbuf
, "..");
1074 error ("Ambiguous %scommand \"%s\": %s.", local_cmdtype
,
1076 return 0; /* lint */
1081 /* We've got something. It may still not be what the caller
1082 wants (if this command *needs* a subcommand). */
1083 while (**line
== ' ' || **line
== '\t')
1086 if (c
->prefixlist
&& **line
&& !c
->allow_unknown
)
1087 undef_cmd_error (c
->prefixname
, *line
);
1089 /* Seems to be what he wants. Return it. */
1095 /* We are here presumably because an alias or command in *TEXT is
1096 deprecated and a warning message should be generated. This function
1097 decodes *TEXT and potentially generates a warning message as outlined
1100 Example for 'set endian big' which has a fictitious alias 'seb'.
1102 If alias wasn't used in *TEXT, and the command is deprecated:
1103 "warning: 'set endian big' is deprecated."
1105 If alias was used, and only the alias is deprecated:
1106 "warning: 'seb' an alias for the command 'set endian big' is deprecated."
1108 If alias was used and command is deprecated (regardless of whether the
1109 alias itself is deprecated:
1111 "warning: 'set endian big' (seb) is deprecated."
1113 After the message has been sent, clear the appropriate flags in the
1114 command and/or the alias so the user is no longer bothered.
1118 deprecated_cmd_warning (char **text
)
1120 struct cmd_list_element
*alias
= NULL
;
1121 struct cmd_list_element
*prefix_cmd
= NULL
;
1122 struct cmd_list_element
*cmd
= NULL
;
1123 struct cmd_list_element
*c
;
1126 if (!lookup_cmd_composition (*text
, &alias
, &prefix_cmd
, &cmd
))
1127 /* return if text doesn't evaluate to a command */
1130 if (!((alias
? (alias
->flags
& DEPRECATED_WARN_USER
) : 0)
1131 || (cmd
->flags
& DEPRECATED_WARN_USER
) ) )
1132 /* return if nothing is deprecated */
1135 printf_filtered ("Warning:");
1137 if (alias
&& !(cmd
->flags
& CMD_DEPRECATED
))
1138 printf_filtered (" '%s', an alias for the", alias
->name
);
1140 printf_filtered (" command '");
1143 printf_filtered ("%s", prefix_cmd
->prefixname
);
1145 printf_filtered ("%s", cmd
->name
);
1147 if (alias
&& (cmd
->flags
& CMD_DEPRECATED
))
1148 printf_filtered ("' (%s) is deprecated.\n", alias
->name
);
1150 printf_filtered ("' is deprecated.\n");
1153 /* if it is only the alias that is deprecated, we want to indicate the
1154 new alias, otherwise we'll indicate the new command */
1156 if (alias
&& !(cmd
->flags
& CMD_DEPRECATED
))
1158 if (alias
->replacement
)
1159 printf_filtered ("Use '%s'.\n\n", alias
->replacement
);
1161 printf_filtered ("No alternative known.\n\n");
1165 if (cmd
->replacement
)
1166 printf_filtered ("Use '%s'.\n\n", cmd
->replacement
);
1168 printf_filtered ("No alternative known.\n\n");
1171 /* We've warned you, now we'll keep quiet */
1173 alias
->flags
&= ~DEPRECATED_WARN_USER
;
1175 cmd
->flags
&= ~DEPRECATED_WARN_USER
;
1180 /* Look up the contents of LINE as a command in the command list 'cmdlist'.
1181 Return 1 on success, 0 on failure.
1183 If LINE refers to an alias, *alias will point to that alias.
1185 If LINE is a postfix command (i.e. one that is preceeded by a prefix
1186 command) set *prefix_cmd.
1188 Set *cmd to point to the command LINE indicates.
1190 If any of *alias, *prefix_cmd, or *cmd cannot be determined or do not
1191 exist, they are NULL when we return.
1195 lookup_cmd_composition (char *text
,
1196 struct cmd_list_element
**alias
,
1197 struct cmd_list_element
**prefix_cmd
,
1198 struct cmd_list_element
**cmd
)
1201 int len
, tmp
, nfound
;
1202 struct cmd_list_element
*cur_list
;
1203 struct cmd_list_element
*prev_cmd
;
1212 /* Go through as many command lists as we need to
1213 to find the command TEXT refers to. */
1217 while (*text
== ' ' || *text
== '\t')
1220 /* Treating underscores as part of command words is important
1221 so that "set args_foo()" doesn't get interpreted as
1222 "set args _foo()". */
1224 *p
&& (isalnum (*p
) || *p
== '-' || *p
== '_' ||
1226 (*p
== '+' || *p
== '<' || *p
== '>' || *p
== '$')) ||
1227 (xdb_commands
&& (*p
== '!' || *p
== '/' || *p
== '?')));
1231 /* If nothing but whitespace, return. */
1237 /* text and p now bracket the first command word to lookup (and
1238 it's length is len). We copy this into a local temporary */
1240 command
= (char *) alloca (len
+ 1);
1241 for (tmp
= 0; tmp
< len
; tmp
++)
1246 command
[len
] = '\0';
1251 *cmd
= find_cmd (command
, len
, cur_list
, 1, &nfound
);
1253 /* We didn't find the command in the entered case, so lower case it
1256 if (!*cmd
|| nfound
== 0)
1258 for (tmp
= 0; tmp
< len
; tmp
++)
1260 char x
= command
[tmp
];
1261 command
[tmp
] = isupper (x
) ? tolower (x
) : x
;
1263 *cmd
= find_cmd (command
, len
, cur_list
, 1, &nfound
);
1266 if (*cmd
== (struct cmd_list_element
*) -1)
1268 return 0; /* ambiguous */
1272 return 0; /* nothing found */
1275 if ((*cmd
)->cmd_pointer
)
1277 /* cmd was actually an alias, we note that an alias was used
1278 (by assigning *alais) and we set *cmd.
1281 *cmd
= (*cmd
)->cmd_pointer
;
1283 *prefix_cmd
= prev_cmd
;
1285 if ((*cmd
)->prefixlist
)
1286 cur_list
= *(*cmd
)->prefixlist
;
1294 /* Helper function for SYMBOL_COMPLETION_FUNCTION. */
1296 /* Return a vector of char pointers which point to the different
1297 possible completions in LIST of TEXT.
1299 WORD points in the same buffer as TEXT, and completions should be
1300 returned relative to this position. For example, suppose TEXT is "foo"
1301 and we want to complete to "foobar". If WORD is "oo", return
1302 "oobar"; if WORD is "baz/foo", return "baz/foobar". */
1305 complete_on_cmdlist (struct cmd_list_element
*list
, char *text
, char *word
)
1307 struct cmd_list_element
*ptr
;
1309 int sizeof_matchlist
;
1311 int textlen
= strlen (text
);
1313 sizeof_matchlist
= 10;
1314 matchlist
= (char **) xmalloc (sizeof_matchlist
* sizeof (char *));
1317 for (ptr
= list
; ptr
; ptr
= ptr
->next
)
1318 if (!strncmp (ptr
->name
, text
, textlen
)
1319 && !ptr
->abbrev_flag
1321 || ptr
->prefixlist
))
1323 if (matches
== sizeof_matchlist
)
1325 sizeof_matchlist
*= 2;
1326 matchlist
= (char **) xrealloc ((char *) matchlist
,
1328 * sizeof (char *)));
1331 matchlist
[matches
] = (char *)
1332 xmalloc (strlen (word
) + strlen (ptr
->name
) + 1);
1334 strcpy (matchlist
[matches
], ptr
->name
);
1335 else if (word
> text
)
1337 /* Return some portion of ptr->name. */
1338 strcpy (matchlist
[matches
], ptr
->name
+ (word
- text
));
1342 /* Return some of text plus ptr->name. */
1343 strncpy (matchlist
[matches
], word
, text
- word
);
1344 matchlist
[matches
][text
- word
] = '\0';
1345 strcat (matchlist
[matches
], ptr
->name
);
1357 matchlist
= (char **) xrealloc ((char *) matchlist
, ((matches
+ 1)
1358 * sizeof (char *)));
1359 matchlist
[matches
] = (char *) 0;
1365 /* Helper function for SYMBOL_COMPLETION_FUNCTION. */
1367 /* Return a vector of char pointers which point to the different
1368 possible completions in CMD of TEXT.
1370 WORD points in the same buffer as TEXT, and completions should be
1371 returned relative to this position. For example, suppose TEXT is "foo"
1372 and we want to complete to "foobar". If WORD is "oo", return
1373 "oobar"; if WORD is "baz/foo", return "baz/foobar". */
1376 complete_on_enum (const char *enumlist
[],
1381 int sizeof_matchlist
;
1383 int textlen
= strlen (text
);
1387 sizeof_matchlist
= 10;
1388 matchlist
= (char **) xmalloc (sizeof_matchlist
* sizeof (char *));
1391 for (i
= 0; (name
= enumlist
[i
]) != NULL
; i
++)
1392 if (strncmp (name
, text
, textlen
) == 0)
1394 if (matches
== sizeof_matchlist
)
1396 sizeof_matchlist
*= 2;
1397 matchlist
= (char **) xrealloc ((char *) matchlist
,
1399 * sizeof (char *)));
1402 matchlist
[matches
] = (char *)
1403 xmalloc (strlen (word
) + strlen (name
) + 1);
1405 strcpy (matchlist
[matches
], name
);
1406 else if (word
> text
)
1408 /* Return some portion of name. */
1409 strcpy (matchlist
[matches
], name
+ (word
- text
));
1413 /* Return some of text plus name. */
1414 strncpy (matchlist
[matches
], word
, text
- word
);
1415 matchlist
[matches
][text
- word
] = '\0';
1416 strcat (matchlist
[matches
], name
);
1428 matchlist
= (char **) xrealloc ((char *) matchlist
, ((matches
+ 1)
1429 * sizeof (char *)));
1430 matchlist
[matches
] = (char *) 0;