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 /* Add element named NAME.
44 CLASS is the top level category into which commands are broken down
46 FUN should be the function to execute the command;
47 it will get a character string as argument, with leading
48 and trailing blanks already eliminated.
50 DOC is a documentation string for the command.
51 Its first line should be a complete sentence.
52 It should start with ? for a command that is an abbreviation
53 or with * for a command that most users don't need to know about.
55 Add this command to command list *LIST.
57 Returns a pointer to the added command (not necessarily the head
60 struct cmd_list_element
*
61 add_cmd (char *name
, enum command_class
class, void (*fun
) (char *, int),
62 char *doc
, struct cmd_list_element
**list
)
64 register struct cmd_list_element
*c
65 = (struct cmd_list_element
*) xmalloc (sizeof (struct cmd_list_element
));
66 struct cmd_list_element
*p
;
68 delete_cmd (name
, list
);
70 if (*list
== NULL
|| strcmp ((*list
)->name
, name
) >= 0)
78 while (p
->next
&& strcmp (p
->next
->name
, name
) <= 0)
88 c
->function
.cfunc
= fun
;
91 c
->replacement
= NULL
;
92 c
->pre_show_hook
= 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 /* Add element named NAME to command list LIST (the list for set
295 or some sublist thereof).
296 CLASS is as in add_cmd.
297 VAR is address of the variable which will contain the value.
298 DOC is the documentation string. */
299 struct cmd_list_element
*
300 add_set_boolean_cmd (char *name
,
301 enum command_class
class,
304 struct cmd_list_element
**list
)
306 static const char *boolean_enums
[] = { "on", "off", NULL
};
307 struct cmd_list_element
*c
;
308 c
= add_set_cmd (name
, class, var_boolean
, var
, doc
, list
);
309 c
->enums
= boolean_enums
;
313 /* Where SETCMD has already been added, add the corresponding show
314 command to LIST and return a pointer to the added command (not
315 necessarily the head of LIST). */
316 struct cmd_list_element
*
317 add_show_from_set (struct cmd_list_element
*setcmd
,
318 struct cmd_list_element
**list
)
320 struct cmd_list_element
*showcmd
=
321 (struct cmd_list_element
*) xmalloc (sizeof (struct cmd_list_element
));
322 struct cmd_list_element
*p
;
324 memcpy (showcmd
, setcmd
, sizeof (struct cmd_list_element
));
325 delete_cmd (showcmd
->name
, list
);
326 showcmd
->type
= show_cmd
;
328 /* Replace "set " at start of docstring with "show ". */
329 if (setcmd
->doc
[0] == 'S' && setcmd
->doc
[1] == 'e'
330 && setcmd
->doc
[2] == 't' && setcmd
->doc
[3] == ' ')
331 showcmd
->doc
= concat ("Show ", setcmd
->doc
+ 4, NULL
);
333 fprintf_unfiltered (gdb_stderr
, "GDB internal error: Bad docstring for set command\n");
335 if (*list
== NULL
|| strcmp ((*list
)->name
, showcmd
->name
) >= 0)
337 showcmd
->next
= *list
;
343 while (p
->next
&& strcmp (p
->next
->name
, showcmd
->name
) <= 0)
347 showcmd
->next
= p
->next
;
354 /* Remove the command named NAME from the command list. */
357 delete_cmd (char *name
, struct cmd_list_element
**list
)
359 register struct cmd_list_element
*c
;
360 struct cmd_list_element
*p
;
362 while (*list
&& STREQ ((*list
)->name
, name
))
364 if ((*list
)->hookee_pre
)
365 (*list
)->hookee_pre
->hook_pre
= 0; /* Hook slips out of its mouth */
366 if ((*list
)->hookee_post
)
367 (*list
)->hookee_post
->hook_post
= 0; /* Hook slips out of its bottom */
374 for (c
= *list
; c
->next
;)
376 if (STREQ (c
->next
->name
, name
))
378 if (c
->next
->hookee_pre
)
379 c
->next
->hookee_pre
->hook_pre
= 0; /* hooked cmd gets away. */
380 if (c
->next
->hookee_post
)
381 c
->next
->hookee_post
->hook_post
= 0; /* remove post hook */
382 /* :( no fishing metaphore */
392 /* Shorthands to the commands above. */
394 /* Add an element to the list of info subcommands. */
396 struct cmd_list_element
*
397 add_info (char *name
, void (*fun
) (char *, int), char *doc
)
399 return add_cmd (name
, no_class
, fun
, doc
, &infolist
);
402 /* Add an alias to the list of info subcommands. */
404 struct cmd_list_element
*
405 add_info_alias (char *name
, char *oldname
, int abbrev_flag
)
407 return add_alias_cmd (name
, oldname
, 0, abbrev_flag
, &infolist
);
410 /* Add an element to the list of commands. */
412 struct cmd_list_element
*
413 add_com (char *name
, enum command_class
class, void (*fun
) (char *, int),
416 return add_cmd (name
, class, fun
, doc
, &cmdlist
);
419 /* Add an alias or abbreviation command to the list of commands. */
421 struct cmd_list_element
*
422 add_com_alias (char *name
, char *oldname
, enum command_class
class,
425 return add_alias_cmd (name
, oldname
, class, abbrev_flag
, &cmdlist
);
428 /* Recursively walk the commandlist structures, and print out the
429 documentation of commands that match our regex in either their
430 name, or their documentation.
433 apropos_cmd (struct ui_file
*stream
, struct cmd_list_element
*commandlist
,
434 struct re_pattern_buffer
*regex
, char *prefix
)
436 register struct cmd_list_element
*c
;
437 int returnvalue
=1; /*Needed to avoid double printing*/
438 /* Walk through the commands */
439 for (c
=commandlist
;c
;c
=c
->next
)
443 /* Try to match against the name*/
444 returnvalue
=re_search(regex
,c
->name
,strlen(c
->name
),0,strlen(c
->name
),NULL
);
445 if (returnvalue
>= 0)
447 /* Stolen from help_cmd_list. We don't directly use
448 * help_cmd_list because it doesn't let us print out
451 fprintf_filtered (stream
, "%s%s -- ", prefix
, c
->name
);
452 print_doc_line (stream
, c
->doc
);
453 fputs_filtered ("\n", stream
);
454 returnvalue
=0; /*Set this so we don't print it again.*/
457 if (c
->doc
!= NULL
&& returnvalue
!= 0)
459 /* Try to match against documentation */
460 if (re_search(regex
,c
->doc
,strlen(c
->doc
),0,strlen(c
->doc
),NULL
) >=0)
462 /* Stolen from help_cmd_list. We don't directly use
463 * help_cmd_list because it doesn't let us print out
466 fprintf_filtered (stream
, "%s%s -- ", prefix
, c
->name
);
467 print_doc_line (stream
, c
->doc
);
468 fputs_filtered ("\n", stream
);
471 /* Check if this command has subcommands */
472 if (c
->prefixlist
!= NULL
)
474 /* Recursively call ourselves on the subcommand list,
475 passing the right prefix in.
477 apropos_cmd (stream
,*c
->prefixlist
,regex
,c
->prefixname
);
482 /* This command really has to deal with two things:
483 * 1) I want documentation on *this string* (usually called by
484 * "help commandname").
485 * 2) I want documentation on *this list* (usually called by
486 * giving a command that requires subcommands. Also called by saying
489 * I am going to split this into two seperate comamnds, help_cmd and
494 help_cmd (char *command
, struct ui_file
*stream
)
496 struct cmd_list_element
*c
;
497 extern struct cmd_list_element
*cmdlist
;
501 help_list (cmdlist
, "", all_classes
, stream
);
505 if (strcmp (command
, "all") == 0)
511 c
= lookup_cmd (&command
, cmdlist
, "", 0, 0);
516 /* There are three cases here.
517 If c->prefixlist is nonzero, we have a prefix command.
518 Print its documentation, then list its subcommands.
520 If c->function is nonzero, we really have a command.
521 Print its documentation and return.
523 If c->function is zero, we have a class name.
524 Print its documentation (as if it were a command)
525 and then set class to the number of this class
526 so that the commands in the class will be listed. */
528 fputs_filtered (c
->doc
, stream
);
529 fputs_filtered ("\n", stream
);
531 if (c
->prefixlist
== 0 && c
->function
.cfunc
!= NULL
)
533 fprintf_filtered (stream
, "\n");
535 /* If this is a prefix command, print it's subcommands */
537 help_list (*c
->prefixlist
, c
->prefixname
, all_commands
, stream
);
539 /* If this is a class name, print all of the commands in the class */
540 if (c
->function
.cfunc
== NULL
)
541 help_list (cmdlist
, "", c
->class, stream
);
543 if (c
->hook_pre
|| c
->hook_post
)
544 fprintf_filtered (stream
,
545 "\nThis command has a hook (or hooks) defined:\n");
548 fprintf_filtered (stream
,
549 "\tThis command is run after : %s (pre hook)\n",
552 fprintf_filtered (stream
,
553 "\tThis command is run before : %s (post hook)\n",
558 * Get a specific kind of help on a command list.
561 * CMDTYPE is the prefix to use in the title string.
562 * CLASS is the class with which to list the nodes of this list (see
563 * documentation for help_cmd_list below), As usual, ALL_COMMANDS for
564 * everything, ALL_CLASSES for just classes, and non-negative for only things
565 * in a specific class.
566 * and STREAM is the output stream on which to print things.
567 * If you call this routine with a class >= 0, it recurses.
570 help_list (struct cmd_list_element
*list
, char *cmdtype
,
571 enum command_class
class, struct ui_file
*stream
)
574 char *cmdtype1
, *cmdtype2
;
576 /* If CMDTYPE is "foo ", CMDTYPE1 gets " foo" and CMDTYPE2 gets "foo sub" */
577 len
= strlen (cmdtype
);
578 cmdtype1
= (char *) alloca (len
+ 1);
580 cmdtype2
= (char *) alloca (len
+ 4);
585 strncpy (cmdtype1
+ 1, cmdtype
, len
- 1);
587 strncpy (cmdtype2
, cmdtype
, len
- 1);
588 strcpy (cmdtype2
+ len
- 1, " sub");
591 if (class == all_classes
)
592 fprintf_filtered (stream
, "List of classes of %scommands:\n\n", cmdtype2
);
594 fprintf_filtered (stream
, "List of %scommands:\n\n", cmdtype2
);
596 help_cmd_list (list
, class, cmdtype
, (int) class >= 0, stream
);
598 if (class == all_classes
)
599 fprintf_filtered (stream
, "\n\
600 Type \"help%s\" followed by a class name for a list of commands in that class.",
603 fprintf_filtered (stream
, "\n\
604 Type \"help%s\" followed by %scommand name for full documentation.\n\
605 Command name abbreviations are allowed if unambiguous.\n",
610 help_all (struct ui_file
*stream
)
612 struct cmd_list_element
*c
;
613 extern struct cmd_list_element
*cmdlist
;
615 for (c
= cmdlist
; c
; c
= c
->next
)
619 /* If this is a prefix command, print it's subcommands */
621 help_cmd_list (*c
->prefixlist
, all_commands
, c
->prefixname
, 0, stream
);
623 /* If this is a class name, print all of the commands in the class */
624 else if (c
->function
.cfunc
== NULL
)
625 help_cmd_list (cmdlist
, c
->class, "", 0, stream
);
629 /* Print only the first line of STR on STREAM. */
631 print_doc_line (struct ui_file
*stream
, char *str
)
633 static char *line_buffer
= 0;
634 static int line_size
;
640 line_buffer
= (char *) xmalloc (line_size
);
644 while (*p
&& *p
!= '\n' && *p
!= '.' && *p
!= ',')
646 if (p
- str
> line_size
- 1)
648 line_size
= p
- str
+ 1;
650 line_buffer
= (char *) xmalloc (line_size
);
652 strncpy (line_buffer
, str
, p
- str
);
653 line_buffer
[p
- str
] = '\0';
654 if (islower (line_buffer
[0]))
655 line_buffer
[0] = toupper (line_buffer
[0]);
656 ui_out_text (uiout
, line_buffer
);
660 * Implement a help command on command list LIST.
661 * RECURSE should be non-zero if this should be done recursively on
662 * all sublists of LIST.
663 * PREFIX is the prefix to print before each command name.
664 * STREAM is the stream upon which the output should be written.
666 * A non-negative class number to list only commands in that
668 * ALL_COMMANDS to list all commands in list.
669 * ALL_CLASSES to list all classes in list.
671 * Note that RECURSE will be active on *all* sublists, not just the
672 * ones selected by the criteria above (ie. the selection mechanism
673 * is at the low level, not the high-level).
676 help_cmd_list (struct cmd_list_element
*list
, enum command_class
class,
677 char *prefix
, int recurse
, struct ui_file
*stream
)
679 register struct cmd_list_element
*c
;
681 for (c
= list
; c
; c
= c
->next
)
683 if (c
->abbrev_flag
== 0 &&
684 (class == all_commands
685 || (class == all_classes
&& c
->function
.cfunc
== NULL
)
686 || (class == c
->class && c
->function
.cfunc
!= NULL
)))
688 fprintf_filtered (stream
, "%s%s -- ", prefix
, c
->name
);
689 print_doc_line (stream
, c
->doc
);
690 fputs_filtered ("\n", stream
);
693 && c
->prefixlist
!= 0
694 && c
->abbrev_flag
== 0)
695 help_cmd_list (*c
->prefixlist
, class, c
->prefixname
, 1, stream
);
700 /* Search the input clist for 'command'. Return the command if
701 found (or NULL if not), and return the number of commands
704 static struct cmd_list_element
*
705 find_cmd (char *command
, int len
, struct cmd_list_element
*clist
,
706 int ignore_help_classes
, int *nfound
)
708 struct cmd_list_element
*found
, *c
;
710 found
= (struct cmd_list_element
*) NULL
;
712 for (c
= clist
; c
; c
= c
->next
)
713 if (!strncmp (command
, c
->name
, len
)
714 && (!ignore_help_classes
|| c
->function
.cfunc
))
718 if (c
->name
[len
] == '\0')
727 /* This routine takes a line of TEXT and a CLIST in which to start the
728 lookup. When it returns it will have incremented the text pointer past
729 the section of text it matched, set *RESULT_LIST to point to the list in
730 which the last word was matched, and will return a pointer to the cmd
731 list element which the text matches. It will return NULL if no match at
732 all was possible. It will return -1 (cast appropriately, ick) if ambigous
733 matches are possible; in this case *RESULT_LIST will be set to point to
734 the list in which there are ambiguous choices (and *TEXT will be set to
735 the ambiguous text string).
737 If the located command was an abbreviation, this routine returns the base
738 command of the abbreviation.
740 It does no error reporting whatsoever; control will always return
741 to the superior routine.
743 In the case of an ambiguous return (-1), *RESULT_LIST will be set to point
744 at the prefix_command (ie. the best match) *or* (special case) will be NULL
745 if no prefix command was ever found. For example, in the case of "info a",
746 "info" matches without ambiguity, but "a" could be "args" or "address", so
747 *RESULT_LIST is set to the cmd_list_element for "info". So in this case
748 RESULT_LIST should not be interpeted as a pointer to the beginning of a
749 list; it simply points to a specific command. In the case of an ambiguous
750 return *TEXT is advanced past the last non-ambiguous prefix (e.g.
751 "info t" can be "info types" or "info target"; upon return *TEXT has been
752 advanced past "info ").
754 If RESULT_LIST is NULL, don't set *RESULT_LIST (but don't otherwise
755 affect the operation).
757 This routine does *not* modify the text pointed to by TEXT.
759 If IGNORE_HELP_CLASSES is nonzero, ignore any command list elements which
760 are actually help classes rather than commands (i.e. the function field of
761 the struct cmd_list_element is NULL). */
763 struct cmd_list_element
*
764 lookup_cmd_1 (char **text
, struct cmd_list_element
*clist
,
765 struct cmd_list_element
**result_list
, int ignore_help_classes
)
768 int len
, tmp
, nfound
;
769 struct cmd_list_element
*found
, *c
;
772 while (**text
== ' ' || **text
== '\t')
775 /* Treating underscores as part of command words is important
776 so that "set args_foo()" doesn't get interpreted as
777 "set args _foo()". */
779 *p
&& (isalnum (*p
) || *p
== '-' || *p
== '_' ||
781 (*p
== '+' || *p
== '<' || *p
== '>' || *p
== '$')) ||
782 (xdb_commands
&& (*p
== '!' || *p
== '/' || *p
== '?')));
786 /* If nothing but whitespace, return 0. */
792 /* *text and p now bracket the first command word to lookup (and
793 it's length is len). We copy this into a local temporary */
796 command
= (char *) alloca (len
+ 1);
797 for (tmp
= 0; tmp
< len
; tmp
++)
799 char x
= (*text
)[tmp
];
807 found
= find_cmd (command
, len
, clist
, ignore_help_classes
, &nfound
);
810 ** We didn't find the command in the entered case, so lower case it
813 if (!found
|| nfound
== 0)
815 for (tmp
= 0; tmp
< len
; tmp
++)
817 char x
= command
[tmp
];
818 command
[tmp
] = isupper (x
) ? tolower (x
) : x
;
820 found
= find_cmd (command
, len
, clist
, ignore_help_classes
, &nfound
);
823 /* If nothing matches, we have a simple failure. */
829 if (result_list
!= NULL
)
830 /* Will be modified in calling routine
831 if we know what the prefix command is. */
833 return (struct cmd_list_element
*) -1; /* Ambiguous. */
836 /* We've matched something on this list. Move text pointer forward. */
840 if (found
->cmd_pointer
)
842 /* We drop the alias (abbreviation) in favor of the command it is
843 pointing to. If the alias is deprecated, though, we need to
844 warn the user about it before we drop it. Note that while we
845 are warning about the alias, we may also warn about the command
846 itself and we will adjust the appropriate DEPRECATED_WARN_USER
849 if (found
->flags
& DEPRECATED_WARN_USER
)
850 deprecated_cmd_warning (&line
);
851 found
= found
->cmd_pointer
;
853 /* If we found a prefix command, keep looking. */
855 if (found
->prefixlist
)
857 c
= lookup_cmd_1 (text
, *found
->prefixlist
, result_list
,
858 ignore_help_classes
);
861 /* Didn't find anything; this is as far as we got. */
862 if (result_list
!= NULL
)
863 *result_list
= clist
;
866 else if (c
== (struct cmd_list_element
*) -1)
868 /* We've gotten this far properly, but the next step
869 is ambiguous. We need to set the result list to the best
870 we've found (if an inferior hasn't already set it). */
871 if (result_list
!= NULL
)
873 /* This used to say *result_list = *found->prefixlist
874 If that was correct, need to modify the documentation
875 at the top of this function to clarify what is supposed
877 *result_list
= found
;
888 if (result_list
!= NULL
)
889 *result_list
= clist
;
894 /* All this hair to move the space to the front of cmdtype */
897 undef_cmd_error (char *cmdtype
, char *q
)
899 error ("Undefined %scommand: \"%s\". Try \"help%s%.*s\".",
903 strlen (cmdtype
) - 1,
907 /* Look up the contents of *LINE as a command in the command list LIST.
908 LIST is a chain of struct cmd_list_element's.
909 If it is found, return the struct cmd_list_element for that command
910 and update *LINE to point after the command name, at the first argument.
911 If not found, call error if ALLOW_UNKNOWN is zero
912 otherwise (or if error returns) return zero.
913 Call error if specified command is ambiguous,
914 unless ALLOW_UNKNOWN is negative.
915 CMDTYPE precedes the word "command" in the error message.
917 If INGNORE_HELP_CLASSES is nonzero, ignore any command list
918 elements which are actually help classes rather than commands (i.e.
919 the function field of the struct cmd_list_element is 0). */
921 struct cmd_list_element
*
922 lookup_cmd (char **line
, struct cmd_list_element
*list
, char *cmdtype
,
923 int allow_unknown
, int ignore_help_classes
)
925 struct cmd_list_element
*last_list
= 0;
926 struct cmd_list_element
*c
=
927 lookup_cmd_1 (line
, list
, &last_list
, ignore_help_classes
);
929 /* Note: Do not remove trailing whitespace here because this
930 would be wrong for complete_command. Jim Kingdon */
937 error ("Lack of needed %scommand", cmdtype
);
942 while (isalnum (*p
) || *p
== '-')
945 q
= (char *) alloca (p
- *line
+ 1);
946 strncpy (q
, *line
, p
- *line
);
948 undef_cmd_error (cmdtype
, q
);
954 else if (c
== (struct cmd_list_element
*) -1)
956 /* Ambigous. Local values should be off prefixlist or called
958 int local_allow_unknown
= (last_list
? last_list
->allow_unknown
:
960 char *local_cmdtype
= last_list
? last_list
->prefixname
: cmdtype
;
961 struct cmd_list_element
*local_list
=
962 (last_list
? *(last_list
->prefixlist
) : list
);
964 if (local_allow_unknown
< 0)
967 return last_list
; /* Found something. */
969 return 0; /* Found nothing. */
973 /* Report as error. */
978 ((*line
)[amb_len
] && (*line
)[amb_len
] != ' '
979 && (*line
)[amb_len
] != '\t');
984 for (c
= local_list
; c
; c
= c
->next
)
985 if (!strncmp (*line
, c
->name
, amb_len
))
987 if (strlen (ambbuf
) + strlen (c
->name
) + 6 < (int) sizeof ambbuf
)
990 strcat (ambbuf
, ", ");
991 strcat (ambbuf
, c
->name
);
995 strcat (ambbuf
, "..");
999 error ("Ambiguous %scommand \"%s\": %s.", local_cmdtype
,
1001 return 0; /* lint */
1006 /* We've got something. It may still not be what the caller
1007 wants (if this command *needs* a subcommand). */
1008 while (**line
== ' ' || **line
== '\t')
1011 if (c
->prefixlist
&& **line
&& !c
->allow_unknown
)
1012 undef_cmd_error (c
->prefixname
, *line
);
1014 /* Seems to be what he wants. Return it. */
1020 /* We are here presumably because an alias or command in *TEXT is
1021 deprecated and a warning message should be generated. This function
1022 decodes *TEXT and potentially generates a warning message as outlined
1025 Example for 'set endian big' which has a fictitious alias 'seb'.
1027 If alias wasn't used in *TEXT, and the command is deprecated:
1028 "warning: 'set endian big' is deprecated."
1030 If alias was used, and only the alias is deprecated:
1031 "warning: 'seb' an alias for the command 'set endian big' is deprecated."
1033 If alias was used and command is deprecated (regardless of whether the
1034 alias itself is deprecated:
1036 "warning: 'set endian big' (seb) is deprecated."
1038 After the message has been sent, clear the appropriate flags in the
1039 command and/or the alias so the user is no longer bothered.
1043 deprecated_cmd_warning (char **text
)
1045 struct cmd_list_element
*alias
= NULL
;
1046 struct cmd_list_element
*prefix_cmd
= NULL
;
1047 struct cmd_list_element
*cmd
= NULL
;
1048 struct cmd_list_element
*c
;
1051 if (!lookup_cmd_composition (*text
, &alias
, &prefix_cmd
, &cmd
))
1052 /* return if text doesn't evaluate to a command */
1055 if (!((alias
? (alias
->flags
& DEPRECATED_WARN_USER
) : 0)
1056 || (cmd
->flags
& DEPRECATED_WARN_USER
) ) )
1057 /* return if nothing is deprecated */
1060 printf_filtered ("Warning:");
1062 if (alias
&& !(cmd
->flags
& CMD_DEPRECATED
))
1063 printf_filtered (" '%s', an alias for the", alias
->name
);
1065 printf_filtered (" command '");
1068 printf_filtered ("%s", prefix_cmd
->prefixname
);
1070 printf_filtered ("%s", cmd
->name
);
1072 if (alias
&& (cmd
->flags
& CMD_DEPRECATED
))
1073 printf_filtered ("' (%s) is deprecated.\n", alias
->name
);
1075 printf_filtered ("' is deprecated.\n");
1078 /* if it is only the alias that is deprecated, we want to indicate the
1079 new alias, otherwise we'll indicate the new command */
1081 if (alias
&& !(cmd
->flags
& CMD_DEPRECATED
))
1083 if (alias
->replacement
)
1084 printf_filtered ("Use '%s'.\n\n", alias
->replacement
);
1086 printf_filtered ("No alternative known.\n\n");
1090 if (cmd
->replacement
)
1091 printf_filtered ("Use '%s'.\n\n", cmd
->replacement
);
1093 printf_filtered ("No alternative known.\n\n");
1096 /* We've warned you, now we'll keep quiet */
1098 alias
->flags
&= ~DEPRECATED_WARN_USER
;
1100 cmd
->flags
&= ~DEPRECATED_WARN_USER
;
1105 /* Look up the contents of LINE as a command in the command list 'cmdlist'.
1106 Return 1 on success, 0 on failure.
1108 If LINE refers to an alias, *alias will point to that alias.
1110 If LINE is a postfix command (i.e. one that is preceeded by a prefix
1111 command) set *prefix_cmd.
1113 Set *cmd to point to the command LINE indicates.
1115 If any of *alias, *prefix_cmd, or *cmd cannot be determined or do not
1116 exist, they are NULL when we return.
1120 lookup_cmd_composition (char *text
,
1121 struct cmd_list_element
**alias
,
1122 struct cmd_list_element
**prefix_cmd
,
1123 struct cmd_list_element
**cmd
)
1126 int len
, tmp
, nfound
;
1127 struct cmd_list_element
*cur_list
;
1128 struct cmd_list_element
*prev_cmd
;
1137 /* Go through as many command lists as we need to
1138 to find the command TEXT refers to. */
1142 while (*text
== ' ' || *text
== '\t')
1145 /* Treating underscores as part of command words is important
1146 so that "set args_foo()" doesn't get interpreted as
1147 "set args _foo()". */
1149 *p
&& (isalnum (*p
) || *p
== '-' || *p
== '_' ||
1151 (*p
== '+' || *p
== '<' || *p
== '>' || *p
== '$')) ||
1152 (xdb_commands
&& (*p
== '!' || *p
== '/' || *p
== '?')));
1156 /* If nothing but whitespace, return. */
1162 /* text and p now bracket the first command word to lookup (and
1163 it's length is len). We copy this into a local temporary */
1165 command
= (char *) alloca (len
+ 1);
1166 for (tmp
= 0; tmp
< len
; tmp
++)
1171 command
[len
] = '\0';
1176 *cmd
= find_cmd (command
, len
, cur_list
, 1, &nfound
);
1178 /* We didn't find the command in the entered case, so lower case it
1181 if (!*cmd
|| nfound
== 0)
1183 for (tmp
= 0; tmp
< len
; tmp
++)
1185 char x
= command
[tmp
];
1186 command
[tmp
] = isupper (x
) ? tolower (x
) : x
;
1188 *cmd
= find_cmd (command
, len
, cur_list
, 1, &nfound
);
1191 if (*cmd
== (struct cmd_list_element
*) -1)
1193 return 0; /* ambiguous */
1197 return 0; /* nothing found */
1200 if ((*cmd
)->cmd_pointer
)
1202 /* cmd was actually an alias, we note that an alias was used
1203 (by assigning *alais) and we set *cmd.
1206 *cmd
= (*cmd
)->cmd_pointer
;
1208 *prefix_cmd
= prev_cmd
;
1210 if ((*cmd
)->prefixlist
)
1211 cur_list
= *(*cmd
)->prefixlist
;
1219 /* Helper function for SYMBOL_COMPLETION_FUNCTION. */
1221 /* Return a vector of char pointers which point to the different
1222 possible completions in LIST of TEXT.
1224 WORD points in the same buffer as TEXT, and completions should be
1225 returned relative to this position. For example, suppose TEXT is "foo"
1226 and we want to complete to "foobar". If WORD is "oo", return
1227 "oobar"; if WORD is "baz/foo", return "baz/foobar". */
1230 complete_on_cmdlist (struct cmd_list_element
*list
, char *text
, char *word
)
1232 struct cmd_list_element
*ptr
;
1234 int sizeof_matchlist
;
1236 int textlen
= strlen (text
);
1238 sizeof_matchlist
= 10;
1239 matchlist
= (char **) xmalloc (sizeof_matchlist
* sizeof (char *));
1242 for (ptr
= list
; ptr
; ptr
= ptr
->next
)
1243 if (!strncmp (ptr
->name
, text
, textlen
)
1244 && !ptr
->abbrev_flag
1245 && (ptr
->function
.cfunc
1246 || ptr
->prefixlist
))
1248 if (matches
== sizeof_matchlist
)
1250 sizeof_matchlist
*= 2;
1251 matchlist
= (char **) xrealloc ((char *) matchlist
,
1253 * sizeof (char *)));
1256 matchlist
[matches
] = (char *)
1257 xmalloc (strlen (word
) + strlen (ptr
->name
) + 1);
1259 strcpy (matchlist
[matches
], ptr
->name
);
1260 else if (word
> text
)
1262 /* Return some portion of ptr->name. */
1263 strcpy (matchlist
[matches
], ptr
->name
+ (word
- text
));
1267 /* Return some of text plus ptr->name. */
1268 strncpy (matchlist
[matches
], word
, text
- word
);
1269 matchlist
[matches
][text
- word
] = '\0';
1270 strcat (matchlist
[matches
], ptr
->name
);
1282 matchlist
= (char **) xrealloc ((char *) matchlist
, ((matches
+ 1)
1283 * sizeof (char *)));
1284 matchlist
[matches
] = (char *) 0;
1290 /* Helper function for SYMBOL_COMPLETION_FUNCTION. */
1292 /* Return a vector of char pointers which point to the different
1293 possible completions in CMD of TEXT.
1295 WORD points in the same buffer as TEXT, and completions should be
1296 returned relative to this position. For example, suppose TEXT is "foo"
1297 and we want to complete to "foobar". If WORD is "oo", return
1298 "oobar"; if WORD is "baz/foo", return "baz/foobar". */
1301 complete_on_enum (const char *enumlist
[],
1306 int sizeof_matchlist
;
1308 int textlen
= strlen (text
);
1312 sizeof_matchlist
= 10;
1313 matchlist
= (char **) xmalloc (sizeof_matchlist
* sizeof (char *));
1316 for (i
= 0; (name
= enumlist
[i
]) != NULL
; i
++)
1317 if (strncmp (name
, text
, textlen
) == 0)
1319 if (matches
== sizeof_matchlist
)
1321 sizeof_matchlist
*= 2;
1322 matchlist
= (char **) xrealloc ((char *) matchlist
,
1324 * sizeof (char *)));
1327 matchlist
[matches
] = (char *)
1328 xmalloc (strlen (word
) + strlen (name
) + 1);
1330 strcpy (matchlist
[matches
], name
);
1331 else if (word
> text
)
1333 /* Return some portion of name. */
1334 strcpy (matchlist
[matches
], name
+ (word
- text
));
1338 /* Return some of text plus name. */
1339 strncpy (matchlist
[matches
], word
, text
- word
);
1340 matchlist
[matches
][text
- word
] = '\0';
1341 strcat (matchlist
[matches
], name
);
1353 matchlist
= (char **) xrealloc ((char *) matchlist
, ((matches
+ 1)
1354 * sizeof (char *)));
1355 matchlist
[matches
] = (char *) 0;