1 /* Handle lists of commands, their decoding and documentation, for GDB.
2 Copyright 1986, 1989, 1990, 1991, 1998, 2000, 2001
3 Free Software Foundation, Inc.
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA. */
23 #include "gnu-regex.h"
29 #include "cli/cli-cmds.h"
30 #include "cli/cli-decode.h"
32 /* Prototypes for local functions */
34 static void undef_cmd_error (char *, char *);
36 static struct cmd_list_element
*find_cmd (char *command
,
38 struct cmd_list_element
*clist
,
39 int ignore_help_classes
,
42 static void help_all (struct ui_file
*stream
);
44 /* Add element named NAME.
45 CLASS is the top level category into which commands are broken down
47 FUN should be the function to execute the command;
48 it will get a character string as argument, with leading
49 and trailing blanks already eliminated.
51 DOC is a documentation string for the command.
52 Its first line should be a complete sentence.
53 It should start with ? for a command that is an abbreviation
54 or with * for a command that most users don't need to know about.
56 Add this command to command list *LIST.
58 Returns a pointer to the added command (not necessarily the head
61 struct cmd_list_element
*
62 add_cmd (char *name
, enum command_class
class, void (*fun
) (char *, int),
63 char *doc
, struct cmd_list_element
**list
)
65 register struct cmd_list_element
*c
66 = (struct cmd_list_element
*) xmalloc (sizeof (struct cmd_list_element
));
67 struct cmd_list_element
*p
;
69 delete_cmd (name
, list
);
71 if (*list
== NULL
|| strcmp ((*list
)->name
, name
) >= 0)
79 while (p
->next
&& strcmp (p
->next
->name
, name
) <= 0)
89 c
->function
.cfunc
= fun
;
92 c
->replacement
= NULL
;
100 c
->completer
= make_symbol_completion_list
;
101 c
->type
= not_set_cmd
;
103 c
->var_type
= var_boolean
;
105 c
->user_commands
= NULL
;
106 c
->hookee_pre
= NULL
;
107 c
->hookee_post
= NULL
;
108 c
->cmd_pointer
= NULL
;
113 /* Same as above, except that the abbrev_flag is set. */
114 /* Note: Doesn't seem to be used anywhere currently. */
116 struct cmd_list_element
*
117 add_abbrev_cmd (char *name
, enum command_class
class, void (*fun
) (char *, int),
118 char *doc
, struct cmd_list_element
**list
)
120 register struct cmd_list_element
*c
121 = add_cmd (name
, class, fun
, doc
, list
);
127 /* Deprecates a command CMD.
128 REPLACEMENT is the name of the command which should be used in place
129 of this command, or NULL if no such command exists.
131 This function does not check to see if command REPLACEMENT exists
132 since gdb may not have gotten around to adding REPLACEMENT when this
135 Returns a pointer to the deprecated command. */
137 struct cmd_list_element
*
138 deprecate_cmd (struct cmd_list_element
*cmd
, char *replacement
)
140 cmd
->flags
|= (CMD_DEPRECATED
| DEPRECATED_WARN_USER
);
142 if (replacement
!= NULL
)
143 cmd
->replacement
= replacement
;
145 cmd
->replacement
= NULL
;
150 struct cmd_list_element
*
151 add_alias_cmd (char *name
, char *oldname
, enum command_class
class,
152 int abbrev_flag
, struct cmd_list_element
**list
)
154 /* Must do this since lookup_cmd tries to side-effect its first arg */
156 register struct cmd_list_element
*old
;
157 register struct cmd_list_element
*c
;
158 copied_name
= (char *) alloca (strlen (oldname
) + 1);
159 strcpy (copied_name
, oldname
);
160 old
= lookup_cmd (&copied_name
, *list
, "", 1, 1);
164 delete_cmd (name
, list
);
168 c
= add_cmd (name
, class, old
->function
.cfunc
, old
->doc
, list
);
169 c
->prefixlist
= old
->prefixlist
;
170 c
->prefixname
= old
->prefixname
;
171 c
->allow_unknown
= old
->allow_unknown
;
172 c
->abbrev_flag
= abbrev_flag
;
173 c
->cmd_pointer
= old
;
177 /* Like add_cmd but adds an element for a command prefix:
178 a name that should be followed by a subcommand to be looked up
179 in another command list. PREFIXLIST should be the address
180 of the variable containing that list. */
182 struct cmd_list_element
*
183 add_prefix_cmd (char *name
, enum command_class
class, void (*fun
) (char *, int),
184 char *doc
, struct cmd_list_element
**prefixlist
,
185 char *prefixname
, int allow_unknown
,
186 struct cmd_list_element
**list
)
188 register struct cmd_list_element
*c
= add_cmd (name
, class, fun
, doc
, list
);
189 c
->prefixlist
= prefixlist
;
190 c
->prefixname
= prefixname
;
191 c
->allow_unknown
= allow_unknown
;
195 /* Like add_prefix_cmd but sets the abbrev_flag on the new command. */
197 struct cmd_list_element
*
198 add_abbrev_prefix_cmd (char *name
, enum command_class
class,
199 void (*fun
) (char *, int), char *doc
,
200 struct cmd_list_element
**prefixlist
, char *prefixname
,
201 int allow_unknown
, struct cmd_list_element
**list
)
203 register struct cmd_list_element
*c
= add_cmd (name
, class, fun
, doc
, list
);
204 c
->prefixlist
= prefixlist
;
205 c
->prefixname
= prefixname
;
206 c
->allow_unknown
= allow_unknown
;
211 /* This is an empty "cfunc". */
213 not_just_help_class_command (char *args
, int from_tty
)
217 /* This is an empty "sfunc". */
218 static void empty_sfunc (char *, int, struct cmd_list_element
*);
221 empty_sfunc (char *args
, int from_tty
, struct cmd_list_element
*c
)
225 /* Add element named NAME to command list LIST (the list for set
226 or some sublist thereof).
227 CLASS is as in add_cmd.
228 VAR_TYPE is the kind of thing we are setting.
229 VAR is address of the variable being controlled by this command.
230 DOC is the documentation string. */
232 struct cmd_list_element
*
233 add_set_cmd (char *name
,
234 enum command_class
class,
238 struct cmd_list_element
**list
)
240 struct cmd_list_element
*c
241 = add_cmd (name
, class, NO_FUNCTION
, doc
, list
);
244 c
->var_type
= var_type
;
246 /* This needs to be something besides NO_FUNCTION so that this isn't
247 treated as a help class. */
248 c
->function
.sfunc
= empty_sfunc
;
252 /* Add element named NAME to command list LIST (the list for set
253 or some sublist thereof).
254 CLASS is as in add_cmd.
255 ENUMLIST is a list of strings which may follow NAME.
256 VAR is address of the variable which will contain the matching string
258 DOC is the documentation string. */
260 struct cmd_list_element
*
261 add_set_enum_cmd (char *name
,
262 enum command_class
class,
263 const char *enumlist
[],
266 struct cmd_list_element
**list
)
268 struct cmd_list_element
*c
269 = add_set_cmd (name
, class, var_enum
, var
, doc
, list
);
275 /* Add element named NAME to command list LIST (the list for set
276 or some sublist thereof).
277 CLASS is as in add_cmd.
278 VAR is address of the variable which will contain the value.
279 DOC is the documentation string. */
280 struct cmd_list_element
*
281 add_set_auto_boolean_cmd (char *name
,
282 enum command_class
class,
283 enum cmd_auto_boolean
*var
,
285 struct cmd_list_element
**list
)
287 static const char *auto_boolean_enums
[] = { "on", "off", "auto", NULL
};
288 struct cmd_list_element
*c
;
289 c
= add_set_cmd (name
, class, var_auto_boolean
, var
, doc
, list
);
290 c
->enums
= auto_boolean_enums
;
294 /* Where SETCMD has already been added, add the corresponding show
295 command to LIST and return a pointer to the added command (not
296 necessarily the head of LIST). */
297 struct cmd_list_element
*
298 add_show_from_set (struct cmd_list_element
*setcmd
,
299 struct cmd_list_element
**list
)
301 struct cmd_list_element
*showcmd
=
302 (struct cmd_list_element
*) xmalloc (sizeof (struct cmd_list_element
));
303 struct cmd_list_element
*p
;
305 memcpy (showcmd
, setcmd
, sizeof (struct cmd_list_element
));
306 delete_cmd (showcmd
->name
, list
);
307 showcmd
->type
= show_cmd
;
309 /* Replace "set " at start of docstring with "show ". */
310 if (setcmd
->doc
[0] == 'S' && setcmd
->doc
[1] == 'e'
311 && setcmd
->doc
[2] == 't' && setcmd
->doc
[3] == ' ')
312 showcmd
->doc
= concat ("Show ", setcmd
->doc
+ 4, NULL
);
314 fprintf_unfiltered (gdb_stderr
, "GDB internal error: Bad docstring for set command\n");
316 if (*list
== NULL
|| strcmp ((*list
)->name
, showcmd
->name
) >= 0)
318 showcmd
->next
= *list
;
324 while (p
->next
&& strcmp (p
->next
->name
, showcmd
->name
) <= 0)
328 showcmd
->next
= p
->next
;
335 /* Remove the command named NAME from the command list. */
338 delete_cmd (char *name
, struct cmd_list_element
**list
)
340 register struct cmd_list_element
*c
;
341 struct cmd_list_element
*p
;
343 while (*list
&& STREQ ((*list
)->name
, name
))
345 if ((*list
)->hookee_pre
)
346 (*list
)->hookee_pre
->hook_pre
= 0; /* Hook slips out of its mouth */
347 if ((*list
)->hookee_post
)
348 (*list
)->hookee_post
->hook_post
= 0; /* Hook slips out of its bottom */
355 for (c
= *list
; c
->next
;)
357 if (STREQ (c
->next
->name
, name
))
359 if (c
->next
->hookee_pre
)
360 c
->next
->hookee_pre
->hook_pre
= 0; /* hooked cmd gets away. */
361 if (c
->next
->hookee_post
)
362 c
->next
->hookee_post
->hook_post
= 0; /* remove post hook */
363 /* :( no fishing metaphore */
373 /* Shorthands to the commands above. */
375 /* Add an element to the list of info subcommands. */
377 struct cmd_list_element
*
378 add_info (char *name
, void (*fun
) (char *, int), char *doc
)
380 return add_cmd (name
, no_class
, fun
, doc
, &infolist
);
383 /* Add an alias to the list of info subcommands. */
385 struct cmd_list_element
*
386 add_info_alias (char *name
, char *oldname
, int abbrev_flag
)
388 return add_alias_cmd (name
, oldname
, 0, abbrev_flag
, &infolist
);
391 /* Add an element to the list of commands. */
393 struct cmd_list_element
*
394 add_com (char *name
, enum command_class
class, void (*fun
) (char *, int),
397 return add_cmd (name
, class, fun
, doc
, &cmdlist
);
400 /* Add an alias or abbreviation command to the list of commands. */
402 struct cmd_list_element
*
403 add_com_alias (char *name
, char *oldname
, enum command_class
class,
406 return add_alias_cmd (name
, oldname
, class, abbrev_flag
, &cmdlist
);
409 /* Recursively walk the commandlist structures, and print out the
410 documentation of commands that match our regex in either their
411 name, or their documentation.
414 apropos_cmd (struct ui_file
*stream
, struct cmd_list_element
*commandlist
,
415 struct re_pattern_buffer
*regex
, char *prefix
)
417 register struct cmd_list_element
*c
;
418 int returnvalue
=1; /*Needed to avoid double printing*/
419 /* Walk through the commands */
420 for (c
=commandlist
;c
;c
=c
->next
)
424 /* Try to match against the name*/
425 returnvalue
=re_search(regex
,c
->name
,strlen(c
->name
),0,strlen(c
->name
),NULL
);
426 if (returnvalue
>= 0)
428 /* Stolen from help_cmd_list. We don't directly use
429 * help_cmd_list because it doesn't let us print out
432 fprintf_filtered (stream
, "%s%s -- ", prefix
, c
->name
);
433 print_doc_line (stream
, c
->doc
);
434 fputs_filtered ("\n", stream
);
435 returnvalue
=0; /*Set this so we don't print it again.*/
438 if (c
->doc
!= NULL
&& returnvalue
!= 0)
440 /* Try to match against documentation */
441 if (re_search(regex
,c
->doc
,strlen(c
->doc
),0,strlen(c
->doc
),NULL
) >=0)
443 /* Stolen from help_cmd_list. We don't directly use
444 * help_cmd_list because it doesn't let us print out
447 fprintf_filtered (stream
, "%s%s -- ", prefix
, c
->name
);
448 print_doc_line (stream
, c
->doc
);
449 fputs_filtered ("\n", stream
);
452 /* Check if this command has subcommands */
453 if (c
->prefixlist
!= NULL
)
455 /* Recursively call ourselves on the subcommand list,
456 passing the right prefix in.
458 apropos_cmd (stream
,*c
->prefixlist
,regex
,c
->prefixname
);
463 /* This command really has to deal with two things:
464 * 1) I want documentation on *this string* (usually called by
465 * "help commandname").
466 * 2) I want documentation on *this list* (usually called by
467 * giving a command that requires subcommands. Also called by saying
470 * I am going to split this into two seperate comamnds, help_cmd and
475 help_cmd (char *command
, struct ui_file
*stream
)
477 struct cmd_list_element
*c
;
478 extern struct cmd_list_element
*cmdlist
;
482 help_list (cmdlist
, "", all_classes
, stream
);
486 if (strcmp (command
, "all") == 0)
492 c
= lookup_cmd (&command
, cmdlist
, "", 0, 0);
497 /* There are three cases here.
498 If c->prefixlist is nonzero, we have a prefix command.
499 Print its documentation, then list its subcommands.
501 If c->function is nonzero, we really have a command.
502 Print its documentation and return.
504 If c->function is zero, we have a class name.
505 Print its documentation (as if it were a command)
506 and then set class to the number of this class
507 so that the commands in the class will be listed. */
509 fputs_filtered (c
->doc
, stream
);
510 fputs_filtered ("\n", stream
);
512 if (c
->prefixlist
== 0 && c
->function
.cfunc
!= NULL
)
514 fprintf_filtered (stream
, "\n");
516 /* If this is a prefix command, print it's subcommands */
518 help_list (*c
->prefixlist
, c
->prefixname
, all_commands
, stream
);
520 /* If this is a class name, print all of the commands in the class */
521 if (c
->function
.cfunc
== NULL
)
522 help_list (cmdlist
, "", c
->class, stream
);
524 if (c
->hook_pre
|| c
->hook_post
)
525 fprintf_filtered (stream
,
526 "\nThis command has a hook (or hooks) defined:\n");
529 fprintf_filtered (stream
,
530 "\tThis command is run after : %s (pre hook)\n",
533 fprintf_filtered (stream
,
534 "\tThis command is run before : %s (post hook)\n",
539 * Get a specific kind of help on a command list.
542 * CMDTYPE is the prefix to use in the title string.
543 * CLASS is the class with which to list the nodes of this list (see
544 * documentation for help_cmd_list below), As usual, ALL_COMMANDS for
545 * everything, ALL_CLASSES for just classes, and non-negative for only things
546 * in a specific class.
547 * and STREAM is the output stream on which to print things.
548 * If you call this routine with a class >= 0, it recurses.
551 help_list (struct cmd_list_element
*list
, char *cmdtype
,
552 enum command_class
class, struct ui_file
*stream
)
555 char *cmdtype1
, *cmdtype2
;
557 /* If CMDTYPE is "foo ", CMDTYPE1 gets " foo" and CMDTYPE2 gets "foo sub" */
558 len
= strlen (cmdtype
);
559 cmdtype1
= (char *) alloca (len
+ 1);
561 cmdtype2
= (char *) alloca (len
+ 4);
566 strncpy (cmdtype1
+ 1, cmdtype
, len
- 1);
568 strncpy (cmdtype2
, cmdtype
, len
- 1);
569 strcpy (cmdtype2
+ len
- 1, " sub");
572 if (class == all_classes
)
573 fprintf_filtered (stream
, "List of classes of %scommands:\n\n", cmdtype2
);
575 fprintf_filtered (stream
, "List of %scommands:\n\n", cmdtype2
);
577 help_cmd_list (list
, class, cmdtype
, (int) class >= 0, stream
);
579 if (class == all_classes
)
580 fprintf_filtered (stream
, "\n\
581 Type \"help%s\" followed by a class name for a list of commands in that class.",
584 fprintf_filtered (stream
, "\n\
585 Type \"help%s\" followed by %scommand name for full documentation.\n\
586 Command name abbreviations are allowed if unambiguous.\n",
591 help_all (struct ui_file
*stream
)
593 struct cmd_list_element
*c
;
594 extern struct cmd_list_element
*cmdlist
;
596 for (c
= cmdlist
; c
; c
= c
->next
)
600 /* If this is a prefix command, print it's subcommands */
602 help_cmd_list (*c
->prefixlist
, all_commands
, c
->prefixname
, 0, stream
);
604 /* If this is a class name, print all of the commands in the class */
605 else if (c
->function
.cfunc
== NULL
)
606 help_cmd_list (cmdlist
, c
->class, "", 0, stream
);
610 /* Print only the first line of STR on STREAM. */
612 print_doc_line (struct ui_file
*stream
, char *str
)
614 static char *line_buffer
= 0;
615 static int line_size
;
621 line_buffer
= (char *) xmalloc (line_size
);
625 while (*p
&& *p
!= '\n' && *p
!= '.' && *p
!= ',')
627 if (p
- str
> line_size
- 1)
629 line_size
= p
- str
+ 1;
631 line_buffer
= (char *) xmalloc (line_size
);
633 strncpy (line_buffer
, str
, p
- str
);
634 line_buffer
[p
- str
] = '\0';
635 if (islower (line_buffer
[0]))
636 line_buffer
[0] = toupper (line_buffer
[0]);
638 ui_out_text (uiout
, line_buffer
);
640 fputs_filtered (line_buffer
, stream
);
645 * Implement a help command on command list LIST.
646 * RECURSE should be non-zero if this should be done recursively on
647 * all sublists of LIST.
648 * PREFIX is the prefix to print before each command name.
649 * STREAM is the stream upon which the output should be written.
651 * A non-negative class number to list only commands in that
653 * ALL_COMMANDS to list all commands in list.
654 * ALL_CLASSES to list all classes in list.
656 * Note that RECURSE will be active on *all* sublists, not just the
657 * ones selected by the criteria above (ie. the selection mechanism
658 * is at the low level, not the high-level).
661 help_cmd_list (struct cmd_list_element
*list
, enum command_class
class,
662 char *prefix
, int recurse
, struct ui_file
*stream
)
664 register struct cmd_list_element
*c
;
666 for (c
= list
; c
; c
= c
->next
)
668 if (c
->abbrev_flag
== 0 &&
669 (class == all_commands
670 || (class == all_classes
&& c
->function
.cfunc
== NULL
)
671 || (class == c
->class && c
->function
.cfunc
!= NULL
)))
673 fprintf_filtered (stream
, "%s%s -- ", prefix
, c
->name
);
674 print_doc_line (stream
, c
->doc
);
675 fputs_filtered ("\n", stream
);
678 && c
->prefixlist
!= 0
679 && c
->abbrev_flag
== 0)
680 help_cmd_list (*c
->prefixlist
, class, c
->prefixname
, 1, stream
);
685 /* Search the input clist for 'command'. Return the command if
686 found (or NULL if not), and return the number of commands
689 static struct cmd_list_element
*
690 find_cmd (char *command
, int len
, struct cmd_list_element
*clist
,
691 int ignore_help_classes
, int *nfound
)
693 struct cmd_list_element
*found
, *c
;
695 found
= (struct cmd_list_element
*) NULL
;
697 for (c
= clist
; c
; c
= c
->next
)
698 if (!strncmp (command
, c
->name
, len
)
699 && (!ignore_help_classes
|| c
->function
.cfunc
))
703 if (c
->name
[len
] == '\0')
712 /* This routine takes a line of TEXT and a CLIST in which to start the
713 lookup. When it returns it will have incremented the text pointer past
714 the section of text it matched, set *RESULT_LIST to point to the list in
715 which the last word was matched, and will return a pointer to the cmd
716 list element which the text matches. It will return NULL if no match at
717 all was possible. It will return -1 (cast appropriately, ick) if ambigous
718 matches are possible; in this case *RESULT_LIST will be set to point to
719 the list in which there are ambiguous choices (and *TEXT will be set to
720 the ambiguous text string).
722 If the located command was an abbreviation, this routine returns the base
723 command of the abbreviation.
725 It does no error reporting whatsoever; control will always return
726 to the superior routine.
728 In the case of an ambiguous return (-1), *RESULT_LIST will be set to point
729 at the prefix_command (ie. the best match) *or* (special case) will be NULL
730 if no prefix command was ever found. For example, in the case of "info a",
731 "info" matches without ambiguity, but "a" could be "args" or "address", so
732 *RESULT_LIST is set to the cmd_list_element for "info". So in this case
733 RESULT_LIST should not be interpeted as a pointer to the beginning of a
734 list; it simply points to a specific command. In the case of an ambiguous
735 return *TEXT is advanced past the last non-ambiguous prefix (e.g.
736 "info t" can be "info types" or "info target"; upon return *TEXT has been
737 advanced past "info ").
739 If RESULT_LIST is NULL, don't set *RESULT_LIST (but don't otherwise
740 affect the operation).
742 This routine does *not* modify the text pointed to by TEXT.
744 If IGNORE_HELP_CLASSES is nonzero, ignore any command list elements which
745 are actually help classes rather than commands (i.e. the function field of
746 the struct cmd_list_element is NULL). */
748 struct cmd_list_element
*
749 lookup_cmd_1 (char **text
, struct cmd_list_element
*clist
,
750 struct cmd_list_element
**result_list
, int ignore_help_classes
)
753 int len
, tmp
, nfound
;
754 struct cmd_list_element
*found
, *c
;
757 while (**text
== ' ' || **text
== '\t')
760 /* Treating underscores as part of command words is important
761 so that "set args_foo()" doesn't get interpreted as
762 "set args _foo()". */
764 *p
&& (isalnum (*p
) || *p
== '-' || *p
== '_' ||
766 (*p
== '+' || *p
== '<' || *p
== '>' || *p
== '$')) ||
767 (xdb_commands
&& (*p
== '!' || *p
== '/' || *p
== '?')));
771 /* If nothing but whitespace, return 0. */
777 /* *text and p now bracket the first command word to lookup (and
778 it's length is len). We copy this into a local temporary */
781 command
= (char *) alloca (len
+ 1);
782 for (tmp
= 0; tmp
< len
; tmp
++)
784 char x
= (*text
)[tmp
];
792 found
= find_cmd (command
, len
, clist
, ignore_help_classes
, &nfound
);
795 ** We didn't find the command in the entered case, so lower case it
798 if (!found
|| nfound
== 0)
800 for (tmp
= 0; tmp
< len
; tmp
++)
802 char x
= command
[tmp
];
803 command
[tmp
] = isupper (x
) ? tolower (x
) : x
;
805 found
= find_cmd (command
, len
, clist
, ignore_help_classes
, &nfound
);
808 /* If nothing matches, we have a simple failure. */
814 if (result_list
!= NULL
)
815 /* Will be modified in calling routine
816 if we know what the prefix command is. */
818 return (struct cmd_list_element
*) -1; /* Ambiguous. */
821 /* We've matched something on this list. Move text pointer forward. */
825 if (found
->cmd_pointer
)
827 /* We drop the alias (abbreviation) in favor of the command it is
828 pointing to. If the alias is deprecated, though, we need to
829 warn the user about it before we drop it. Note that while we
830 are warning about the alias, we may also warn about the command
831 itself and we will adjust the appropriate DEPRECATED_WARN_USER
834 if (found
->flags
& DEPRECATED_WARN_USER
)
835 deprecated_cmd_warning (&line
);
836 found
= found
->cmd_pointer
;
838 /* If we found a prefix command, keep looking. */
840 if (found
->prefixlist
)
842 c
= lookup_cmd_1 (text
, *found
->prefixlist
, result_list
,
843 ignore_help_classes
);
846 /* Didn't find anything; this is as far as we got. */
847 if (result_list
!= NULL
)
848 *result_list
= clist
;
851 else if (c
== (struct cmd_list_element
*) -1)
853 /* We've gotten this far properly, but the next step
854 is ambiguous. We need to set the result list to the best
855 we've found (if an inferior hasn't already set it). */
856 if (result_list
!= NULL
)
858 /* This used to say *result_list = *found->prefixlist
859 If that was correct, need to modify the documentation
860 at the top of this function to clarify what is supposed
862 *result_list
= found
;
873 if (result_list
!= NULL
)
874 *result_list
= clist
;
879 /* All this hair to move the space to the front of cmdtype */
882 undef_cmd_error (char *cmdtype
, char *q
)
884 error ("Undefined %scommand: \"%s\". Try \"help%s%.*s\".",
888 strlen (cmdtype
) - 1,
892 /* Look up the contents of *LINE as a command in the command list LIST.
893 LIST is a chain of struct cmd_list_element's.
894 If it is found, return the struct cmd_list_element for that command
895 and update *LINE to point after the command name, at the first argument.
896 If not found, call error if ALLOW_UNKNOWN is zero
897 otherwise (or if error returns) return zero.
898 Call error if specified command is ambiguous,
899 unless ALLOW_UNKNOWN is negative.
900 CMDTYPE precedes the word "command" in the error message.
902 If INGNORE_HELP_CLASSES is nonzero, ignore any command list
903 elements which are actually help classes rather than commands (i.e.
904 the function field of the struct cmd_list_element is 0). */
906 struct cmd_list_element
*
907 lookup_cmd (char **line
, struct cmd_list_element
*list
, char *cmdtype
,
908 int allow_unknown
, int ignore_help_classes
)
910 struct cmd_list_element
*last_list
= 0;
911 struct cmd_list_element
*c
=
912 lookup_cmd_1 (line
, list
, &last_list
, ignore_help_classes
);
914 /* Note: Do not remove trailing whitespace here because this
915 would be wrong for complete_command. Jim Kingdon */
922 error ("Lack of needed %scommand", cmdtype
);
927 while (isalnum (*p
) || *p
== '-')
930 q
= (char *) alloca (p
- *line
+ 1);
931 strncpy (q
, *line
, p
- *line
);
933 undef_cmd_error (cmdtype
, q
);
939 else if (c
== (struct cmd_list_element
*) -1)
941 /* Ambigous. Local values should be off prefixlist or called
943 int local_allow_unknown
= (last_list
? last_list
->allow_unknown
:
945 char *local_cmdtype
= last_list
? last_list
->prefixname
: cmdtype
;
946 struct cmd_list_element
*local_list
=
947 (last_list
? *(last_list
->prefixlist
) : list
);
949 if (local_allow_unknown
< 0)
952 return last_list
; /* Found something. */
954 return 0; /* Found nothing. */
958 /* Report as error. */
963 ((*line
)[amb_len
] && (*line
)[amb_len
] != ' '
964 && (*line
)[amb_len
] != '\t');
969 for (c
= local_list
; c
; c
= c
->next
)
970 if (!strncmp (*line
, c
->name
, amb_len
))
972 if (strlen (ambbuf
) + strlen (c
->name
) + 6 < (int) sizeof ambbuf
)
975 strcat (ambbuf
, ", ");
976 strcat (ambbuf
, c
->name
);
980 strcat (ambbuf
, "..");
984 error ("Ambiguous %scommand \"%s\": %s.", local_cmdtype
,
991 /* We've got something. It may still not be what the caller
992 wants (if this command *needs* a subcommand). */
993 while (**line
== ' ' || **line
== '\t')
996 if (c
->prefixlist
&& **line
&& !c
->allow_unknown
)
997 undef_cmd_error (c
->prefixname
, *line
);
999 /* Seems to be what he wants. Return it. */
1005 /* We are here presumably because an alias or command in *TEXT is
1006 deprecated and a warning message should be generated. This function
1007 decodes *TEXT and potentially generates a warning message as outlined
1010 Example for 'set endian big' which has a fictitious alias 'seb'.
1012 If alias wasn't used in *TEXT, and the command is deprecated:
1013 "warning: 'set endian big' is deprecated."
1015 If alias was used, and only the alias is deprecated:
1016 "warning: 'seb' an alias for the command 'set endian big' is deprecated."
1018 If alias was used and command is deprecated (regardless of whether the
1019 alias itself is deprecated:
1021 "warning: 'set endian big' (seb) is deprecated."
1023 After the message has been sent, clear the appropriate flags in the
1024 command and/or the alias so the user is no longer bothered.
1028 deprecated_cmd_warning (char **text
)
1030 struct cmd_list_element
*alias
= NULL
;
1031 struct cmd_list_element
*prefix_cmd
= NULL
;
1032 struct cmd_list_element
*cmd
= NULL
;
1033 struct cmd_list_element
*c
;
1036 if (!lookup_cmd_composition (*text
, &alias
, &prefix_cmd
, &cmd
))
1037 /* return if text doesn't evaluate to a command */
1040 if (!((alias
? (alias
->flags
& DEPRECATED_WARN_USER
) : 0)
1041 || (cmd
->flags
& DEPRECATED_WARN_USER
) ) )
1042 /* return if nothing is deprecated */
1045 printf_filtered ("Warning:");
1047 if (alias
&& !(cmd
->flags
& CMD_DEPRECATED
))
1048 printf_filtered (" '%s', an alias for the", alias
->name
);
1050 printf_filtered (" command '");
1053 printf_filtered ("%s", prefix_cmd
->prefixname
);
1055 printf_filtered ("%s", cmd
->name
);
1057 if (alias
&& (cmd
->flags
& CMD_DEPRECATED
))
1058 printf_filtered ("' (%s) is deprecated.\n", alias
->name
);
1060 printf_filtered ("' is deprecated.\n");
1063 /* if it is only the alias that is deprecated, we want to indicate the
1064 new alias, otherwise we'll indicate the new command */
1066 if (alias
&& !(cmd
->flags
& CMD_DEPRECATED
))
1068 if (alias
->replacement
)
1069 printf_filtered ("Use '%s'.\n\n", alias
->replacement
);
1071 printf_filtered ("No alternative known.\n\n");
1075 if (cmd
->replacement
)
1076 printf_filtered ("Use '%s'.\n\n", cmd
->replacement
);
1078 printf_filtered ("No alternative known.\n\n");
1081 /* We've warned you, now we'll keep quiet */
1083 alias
->flags
&= ~DEPRECATED_WARN_USER
;
1085 cmd
->flags
&= ~DEPRECATED_WARN_USER
;
1090 /* Look up the contents of LINE as a command in the command list 'cmdlist'.
1091 Return 1 on success, 0 on failure.
1093 If LINE refers to an alias, *alias will point to that alias.
1095 If LINE is a postfix command (i.e. one that is preceeded by a prefix
1096 command) set *prefix_cmd.
1098 Set *cmd to point to the command LINE indicates.
1100 If any of *alias, *prefix_cmd, or *cmd cannot be determined or do not
1101 exist, they are NULL when we return.
1105 lookup_cmd_composition (char *text
,
1106 struct cmd_list_element
**alias
,
1107 struct cmd_list_element
**prefix_cmd
,
1108 struct cmd_list_element
**cmd
)
1111 int len
, tmp
, nfound
;
1112 struct cmd_list_element
*cur_list
;
1113 struct cmd_list_element
*prev_cmd
;
1122 /* Go through as many command lists as we need to
1123 to find the command TEXT refers to. */
1127 while (*text
== ' ' || *text
== '\t')
1130 /* Treating underscores as part of command words is important
1131 so that "set args_foo()" doesn't get interpreted as
1132 "set args _foo()". */
1134 *p
&& (isalnum (*p
) || *p
== '-' || *p
== '_' ||
1136 (*p
== '+' || *p
== '<' || *p
== '>' || *p
== '$')) ||
1137 (xdb_commands
&& (*p
== '!' || *p
== '/' || *p
== '?')));
1141 /* If nothing but whitespace, return. */
1147 /* text and p now bracket the first command word to lookup (and
1148 it's length is len). We copy this into a local temporary */
1150 command
= (char *) alloca (len
+ 1);
1151 for (tmp
= 0; tmp
< len
; tmp
++)
1156 command
[len
] = '\0';
1161 *cmd
= find_cmd (command
, len
, cur_list
, 1, &nfound
);
1163 /* We didn't find the command in the entered case, so lower case it
1166 if (!*cmd
|| nfound
== 0)
1168 for (tmp
= 0; tmp
< len
; tmp
++)
1170 char x
= command
[tmp
];
1171 command
[tmp
] = isupper (x
) ? tolower (x
) : x
;
1173 *cmd
= find_cmd (command
, len
, cur_list
, 1, &nfound
);
1176 if (*cmd
== (struct cmd_list_element
*) -1)
1178 return 0; /* ambiguous */
1182 return 0; /* nothing found */
1185 if ((*cmd
)->cmd_pointer
)
1187 /* cmd was actually an alias, we note that an alias was used
1188 (by assigning *alais) and we set *cmd.
1191 *cmd
= (*cmd
)->cmd_pointer
;
1193 *prefix_cmd
= prev_cmd
;
1195 if ((*cmd
)->prefixlist
)
1196 cur_list
= *(*cmd
)->prefixlist
;
1204 /* Helper function for SYMBOL_COMPLETION_FUNCTION. */
1206 /* Return a vector of char pointers which point to the different
1207 possible completions in LIST of TEXT.
1209 WORD points in the same buffer as TEXT, and completions should be
1210 returned relative to this position. For example, suppose TEXT is "foo"
1211 and we want to complete to "foobar". If WORD is "oo", return
1212 "oobar"; if WORD is "baz/foo", return "baz/foobar". */
1215 complete_on_cmdlist (struct cmd_list_element
*list
, char *text
, char *word
)
1217 struct cmd_list_element
*ptr
;
1219 int sizeof_matchlist
;
1221 int textlen
= strlen (text
);
1223 sizeof_matchlist
= 10;
1224 matchlist
= (char **) xmalloc (sizeof_matchlist
* sizeof (char *));
1227 for (ptr
= list
; ptr
; ptr
= ptr
->next
)
1228 if (!strncmp (ptr
->name
, text
, textlen
)
1229 && !ptr
->abbrev_flag
1230 && (ptr
->function
.cfunc
1231 || ptr
->prefixlist
))
1233 if (matches
== sizeof_matchlist
)
1235 sizeof_matchlist
*= 2;
1236 matchlist
= (char **) xrealloc ((char *) matchlist
,
1238 * sizeof (char *)));
1241 matchlist
[matches
] = (char *)
1242 xmalloc (strlen (word
) + strlen (ptr
->name
) + 1);
1244 strcpy (matchlist
[matches
], ptr
->name
);
1245 else if (word
> text
)
1247 /* Return some portion of ptr->name. */
1248 strcpy (matchlist
[matches
], ptr
->name
+ (word
- text
));
1252 /* Return some of text plus ptr->name. */
1253 strncpy (matchlist
[matches
], word
, text
- word
);
1254 matchlist
[matches
][text
- word
] = '\0';
1255 strcat (matchlist
[matches
], ptr
->name
);
1267 matchlist
= (char **) xrealloc ((char *) matchlist
, ((matches
+ 1)
1268 * sizeof (char *)));
1269 matchlist
[matches
] = (char *) 0;
1275 /* Helper function for SYMBOL_COMPLETION_FUNCTION. */
1277 /* Return a vector of char pointers which point to the different
1278 possible completions in CMD of TEXT.
1280 WORD points in the same buffer as TEXT, and completions should be
1281 returned relative to this position. For example, suppose TEXT is "foo"
1282 and we want to complete to "foobar". If WORD is "oo", return
1283 "oobar"; if WORD is "baz/foo", return "baz/foobar". */
1286 complete_on_enum (const char *enumlist
[],
1291 int sizeof_matchlist
;
1293 int textlen
= strlen (text
);
1297 sizeof_matchlist
= 10;
1298 matchlist
= (char **) xmalloc (sizeof_matchlist
* sizeof (char *));
1301 for (i
= 0; (name
= enumlist
[i
]) != NULL
; i
++)
1302 if (strncmp (name
, text
, textlen
) == 0)
1304 if (matches
== sizeof_matchlist
)
1306 sizeof_matchlist
*= 2;
1307 matchlist
= (char **) xrealloc ((char *) matchlist
,
1309 * sizeof (char *)));
1312 matchlist
[matches
] = (char *)
1313 xmalloc (strlen (word
) + strlen (name
) + 1);
1315 strcpy (matchlist
[matches
], name
);
1316 else if (word
> text
)
1318 /* Return some portion of name. */
1319 strcpy (matchlist
[matches
], name
+ (word
- text
));
1323 /* Return some of text plus name. */
1324 strncpy (matchlist
[matches
], word
, text
- word
);
1325 matchlist
[matches
][text
- word
] = '\0';
1326 strcat (matchlist
[matches
], name
);
1338 matchlist
= (char **) xrealloc ((char *) matchlist
, ((matches
+ 1)
1339 * sizeof (char *)));
1340 matchlist
[matches
] = (char *) 0;