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 "gdb_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 /* 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]);
657 ui_out_text (uiout
, line_buffer
);
659 fputs_filtered (line_buffer
, stream
);
664 * Implement a help command on command list LIST.
665 * RECURSE should be non-zero if this should be done recursively on
666 * all sublists of LIST.
667 * PREFIX is the prefix to print before each command name.
668 * STREAM is the stream upon which the output should be written.
670 * A non-negative class number to list only commands in that
672 * ALL_COMMANDS to list all commands in list.
673 * ALL_CLASSES to list all classes in list.
675 * Note that RECURSE will be active on *all* sublists, not just the
676 * ones selected by the criteria above (ie. the selection mechanism
677 * is at the low level, not the high-level).
680 help_cmd_list (struct cmd_list_element
*list
, enum command_class
class,
681 char *prefix
, int recurse
, struct ui_file
*stream
)
683 register struct cmd_list_element
*c
;
685 for (c
= list
; c
; c
= c
->next
)
687 if (c
->abbrev_flag
== 0 &&
688 (class == all_commands
689 || (class == all_classes
&& c
->function
.cfunc
== NULL
)
690 || (class == c
->class && c
->function
.cfunc
!= NULL
)))
692 fprintf_filtered (stream
, "%s%s -- ", prefix
, c
->name
);
693 print_doc_line (stream
, c
->doc
);
694 fputs_filtered ("\n", stream
);
697 && c
->prefixlist
!= 0
698 && c
->abbrev_flag
== 0)
699 help_cmd_list (*c
->prefixlist
, class, c
->prefixname
, 1, stream
);
704 /* Search the input clist for 'command'. Return the command if
705 found (or NULL if not), and return the number of commands
708 static struct cmd_list_element
*
709 find_cmd (char *command
, int len
, struct cmd_list_element
*clist
,
710 int ignore_help_classes
, int *nfound
)
712 struct cmd_list_element
*found
, *c
;
714 found
= (struct cmd_list_element
*) NULL
;
716 for (c
= clist
; c
; c
= c
->next
)
717 if (!strncmp (command
, c
->name
, len
)
718 && (!ignore_help_classes
|| c
->function
.cfunc
))
722 if (c
->name
[len
] == '\0')
731 /* This routine takes a line of TEXT and a CLIST in which to start the
732 lookup. When it returns it will have incremented the text pointer past
733 the section of text it matched, set *RESULT_LIST to point to the list in
734 which the last word was matched, and will return a pointer to the cmd
735 list element which the text matches. It will return NULL if no match at
736 all was possible. It will return -1 (cast appropriately, ick) if ambigous
737 matches are possible; in this case *RESULT_LIST will be set to point to
738 the list in which there are ambiguous choices (and *TEXT will be set to
739 the ambiguous text string).
741 If the located command was an abbreviation, this routine returns the base
742 command of the abbreviation.
744 It does no error reporting whatsoever; control will always return
745 to the superior routine.
747 In the case of an ambiguous return (-1), *RESULT_LIST will be set to point
748 at the prefix_command (ie. the best match) *or* (special case) will be NULL
749 if no prefix command was ever found. For example, in the case of "info a",
750 "info" matches without ambiguity, but "a" could be "args" or "address", so
751 *RESULT_LIST is set to the cmd_list_element for "info". So in this case
752 RESULT_LIST should not be interpeted as a pointer to the beginning of a
753 list; it simply points to a specific command. In the case of an ambiguous
754 return *TEXT is advanced past the last non-ambiguous prefix (e.g.
755 "info t" can be "info types" or "info target"; upon return *TEXT has been
756 advanced past "info ").
758 If RESULT_LIST is NULL, don't set *RESULT_LIST (but don't otherwise
759 affect the operation).
761 This routine does *not* modify the text pointed to by TEXT.
763 If IGNORE_HELP_CLASSES is nonzero, ignore any command list elements which
764 are actually help classes rather than commands (i.e. the function field of
765 the struct cmd_list_element is NULL). */
767 struct cmd_list_element
*
768 lookup_cmd_1 (char **text
, struct cmd_list_element
*clist
,
769 struct cmd_list_element
**result_list
, int ignore_help_classes
)
772 int len
, tmp
, nfound
;
773 struct cmd_list_element
*found
, *c
;
776 while (**text
== ' ' || **text
== '\t')
779 /* Treating underscores as part of command words is important
780 so that "set args_foo()" doesn't get interpreted as
781 "set args _foo()". */
783 *p
&& (isalnum (*p
) || *p
== '-' || *p
== '_' ||
785 (*p
== '+' || *p
== '<' || *p
== '>' || *p
== '$')) ||
786 (xdb_commands
&& (*p
== '!' || *p
== '/' || *p
== '?')));
790 /* If nothing but whitespace, return 0. */
796 /* *text and p now bracket the first command word to lookup (and
797 it's length is len). We copy this into a local temporary */
800 command
= (char *) alloca (len
+ 1);
801 for (tmp
= 0; tmp
< len
; tmp
++)
803 char x
= (*text
)[tmp
];
811 found
= find_cmd (command
, len
, clist
, ignore_help_classes
, &nfound
);
814 ** We didn't find the command in the entered case, so lower case it
817 if (!found
|| nfound
== 0)
819 for (tmp
= 0; tmp
< len
; tmp
++)
821 char x
= command
[tmp
];
822 command
[tmp
] = isupper (x
) ? tolower (x
) : x
;
824 found
= find_cmd (command
, len
, clist
, ignore_help_classes
, &nfound
);
827 /* If nothing matches, we have a simple failure. */
833 if (result_list
!= NULL
)
834 /* Will be modified in calling routine
835 if we know what the prefix command is. */
837 return (struct cmd_list_element
*) -1; /* Ambiguous. */
840 /* We've matched something on this list. Move text pointer forward. */
844 if (found
->cmd_pointer
)
846 /* We drop the alias (abbreviation) in favor of the command it is
847 pointing to. If the alias is deprecated, though, we need to
848 warn the user about it before we drop it. Note that while we
849 are warning about the alias, we may also warn about the command
850 itself and we will adjust the appropriate DEPRECATED_WARN_USER
853 if (found
->flags
& DEPRECATED_WARN_USER
)
854 deprecated_cmd_warning (&line
);
855 found
= found
->cmd_pointer
;
857 /* If we found a prefix command, keep looking. */
859 if (found
->prefixlist
)
861 c
= lookup_cmd_1 (text
, *found
->prefixlist
, result_list
,
862 ignore_help_classes
);
865 /* Didn't find anything; this is as far as we got. */
866 if (result_list
!= NULL
)
867 *result_list
= clist
;
870 else if (c
== (struct cmd_list_element
*) -1)
872 /* We've gotten this far properly, but the next step
873 is ambiguous. We need to set the result list to the best
874 we've found (if an inferior hasn't already set it). */
875 if (result_list
!= NULL
)
877 /* This used to say *result_list = *found->prefixlist
878 If that was correct, need to modify the documentation
879 at the top of this function to clarify what is supposed
881 *result_list
= found
;
892 if (result_list
!= NULL
)
893 *result_list
= clist
;
898 /* All this hair to move the space to the front of cmdtype */
901 undef_cmd_error (char *cmdtype
, char *q
)
903 error ("Undefined %scommand: \"%s\". Try \"help%s%.*s\".",
907 strlen (cmdtype
) - 1,
911 /* Look up the contents of *LINE as a command in the command list LIST.
912 LIST is a chain of struct cmd_list_element's.
913 If it is found, return the struct cmd_list_element for that command
914 and update *LINE to point after the command name, at the first argument.
915 If not found, call error if ALLOW_UNKNOWN is zero
916 otherwise (or if error returns) return zero.
917 Call error if specified command is ambiguous,
918 unless ALLOW_UNKNOWN is negative.
919 CMDTYPE precedes the word "command" in the error message.
921 If INGNORE_HELP_CLASSES is nonzero, ignore any command list
922 elements which are actually help classes rather than commands (i.e.
923 the function field of the struct cmd_list_element is 0). */
925 struct cmd_list_element
*
926 lookup_cmd (char **line
, struct cmd_list_element
*list
, char *cmdtype
,
927 int allow_unknown
, int ignore_help_classes
)
929 struct cmd_list_element
*last_list
= 0;
930 struct cmd_list_element
*c
=
931 lookup_cmd_1 (line
, list
, &last_list
, ignore_help_classes
);
933 /* Note: Do not remove trailing whitespace here because this
934 would be wrong for complete_command. Jim Kingdon */
941 error ("Lack of needed %scommand", cmdtype
);
946 while (isalnum (*p
) || *p
== '-')
949 q
= (char *) alloca (p
- *line
+ 1);
950 strncpy (q
, *line
, p
- *line
);
952 undef_cmd_error (cmdtype
, q
);
958 else if (c
== (struct cmd_list_element
*) -1)
960 /* Ambigous. Local values should be off prefixlist or called
962 int local_allow_unknown
= (last_list
? last_list
->allow_unknown
:
964 char *local_cmdtype
= last_list
? last_list
->prefixname
: cmdtype
;
965 struct cmd_list_element
*local_list
=
966 (last_list
? *(last_list
->prefixlist
) : list
);
968 if (local_allow_unknown
< 0)
971 return last_list
; /* Found something. */
973 return 0; /* Found nothing. */
977 /* Report as error. */
982 ((*line
)[amb_len
] && (*line
)[amb_len
] != ' '
983 && (*line
)[amb_len
] != '\t');
988 for (c
= local_list
; c
; c
= c
->next
)
989 if (!strncmp (*line
, c
->name
, amb_len
))
991 if (strlen (ambbuf
) + strlen (c
->name
) + 6 < (int) sizeof ambbuf
)
994 strcat (ambbuf
, ", ");
995 strcat (ambbuf
, c
->name
);
999 strcat (ambbuf
, "..");
1003 error ("Ambiguous %scommand \"%s\": %s.", local_cmdtype
,
1005 return 0; /* lint */
1010 /* We've got something. It may still not be what the caller
1011 wants (if this command *needs* a subcommand). */
1012 while (**line
== ' ' || **line
== '\t')
1015 if (c
->prefixlist
&& **line
&& !c
->allow_unknown
)
1016 undef_cmd_error (c
->prefixname
, *line
);
1018 /* Seems to be what he wants. Return it. */
1024 /* We are here presumably because an alias or command in *TEXT is
1025 deprecated and a warning message should be generated. This function
1026 decodes *TEXT and potentially generates a warning message as outlined
1029 Example for 'set endian big' which has a fictitious alias 'seb'.
1031 If alias wasn't used in *TEXT, and the command is deprecated:
1032 "warning: 'set endian big' is deprecated."
1034 If alias was used, and only the alias is deprecated:
1035 "warning: 'seb' an alias for the command 'set endian big' is deprecated."
1037 If alias was used and command is deprecated (regardless of whether the
1038 alias itself is deprecated:
1040 "warning: 'set endian big' (seb) is deprecated."
1042 After the message has been sent, clear the appropriate flags in the
1043 command and/or the alias so the user is no longer bothered.
1047 deprecated_cmd_warning (char **text
)
1049 struct cmd_list_element
*alias
= NULL
;
1050 struct cmd_list_element
*prefix_cmd
= NULL
;
1051 struct cmd_list_element
*cmd
= NULL
;
1052 struct cmd_list_element
*c
;
1055 if (!lookup_cmd_composition (*text
, &alias
, &prefix_cmd
, &cmd
))
1056 /* return if text doesn't evaluate to a command */
1059 if (!((alias
? (alias
->flags
& DEPRECATED_WARN_USER
) : 0)
1060 || (cmd
->flags
& DEPRECATED_WARN_USER
) ) )
1061 /* return if nothing is deprecated */
1064 printf_filtered ("Warning:");
1066 if (alias
&& !(cmd
->flags
& CMD_DEPRECATED
))
1067 printf_filtered (" '%s', an alias for the", alias
->name
);
1069 printf_filtered (" command '");
1072 printf_filtered ("%s", prefix_cmd
->prefixname
);
1074 printf_filtered ("%s", cmd
->name
);
1076 if (alias
&& (cmd
->flags
& CMD_DEPRECATED
))
1077 printf_filtered ("' (%s) is deprecated.\n", alias
->name
);
1079 printf_filtered ("' is deprecated.\n");
1082 /* if it is only the alias that is deprecated, we want to indicate the
1083 new alias, otherwise we'll indicate the new command */
1085 if (alias
&& !(cmd
->flags
& CMD_DEPRECATED
))
1087 if (alias
->replacement
)
1088 printf_filtered ("Use '%s'.\n\n", alias
->replacement
);
1090 printf_filtered ("No alternative known.\n\n");
1094 if (cmd
->replacement
)
1095 printf_filtered ("Use '%s'.\n\n", cmd
->replacement
);
1097 printf_filtered ("No alternative known.\n\n");
1100 /* We've warned you, now we'll keep quiet */
1102 alias
->flags
&= ~DEPRECATED_WARN_USER
;
1104 cmd
->flags
&= ~DEPRECATED_WARN_USER
;
1109 /* Look up the contents of LINE as a command in the command list 'cmdlist'.
1110 Return 1 on success, 0 on failure.
1112 If LINE refers to an alias, *alias will point to that alias.
1114 If LINE is a postfix command (i.e. one that is preceeded by a prefix
1115 command) set *prefix_cmd.
1117 Set *cmd to point to the command LINE indicates.
1119 If any of *alias, *prefix_cmd, or *cmd cannot be determined or do not
1120 exist, they are NULL when we return.
1124 lookup_cmd_composition (char *text
,
1125 struct cmd_list_element
**alias
,
1126 struct cmd_list_element
**prefix_cmd
,
1127 struct cmd_list_element
**cmd
)
1130 int len
, tmp
, nfound
;
1131 struct cmd_list_element
*cur_list
;
1132 struct cmd_list_element
*prev_cmd
;
1141 /* Go through as many command lists as we need to
1142 to find the command TEXT refers to. */
1146 while (*text
== ' ' || *text
== '\t')
1149 /* Treating underscores as part of command words is important
1150 so that "set args_foo()" doesn't get interpreted as
1151 "set args _foo()". */
1153 *p
&& (isalnum (*p
) || *p
== '-' || *p
== '_' ||
1155 (*p
== '+' || *p
== '<' || *p
== '>' || *p
== '$')) ||
1156 (xdb_commands
&& (*p
== '!' || *p
== '/' || *p
== '?')));
1160 /* If nothing but whitespace, return. */
1166 /* text and p now bracket the first command word to lookup (and
1167 it's length is len). We copy this into a local temporary */
1169 command
= (char *) alloca (len
+ 1);
1170 for (tmp
= 0; tmp
< len
; tmp
++)
1175 command
[len
] = '\0';
1180 *cmd
= find_cmd (command
, len
, cur_list
, 1, &nfound
);
1182 /* We didn't find the command in the entered case, so lower case it
1185 if (!*cmd
|| nfound
== 0)
1187 for (tmp
= 0; tmp
< len
; tmp
++)
1189 char x
= command
[tmp
];
1190 command
[tmp
] = isupper (x
) ? tolower (x
) : x
;
1192 *cmd
= find_cmd (command
, len
, cur_list
, 1, &nfound
);
1195 if (*cmd
== (struct cmd_list_element
*) -1)
1197 return 0; /* ambiguous */
1201 return 0; /* nothing found */
1204 if ((*cmd
)->cmd_pointer
)
1206 /* cmd was actually an alias, we note that an alias was used
1207 (by assigning *alais) and we set *cmd.
1210 *cmd
= (*cmd
)->cmd_pointer
;
1212 *prefix_cmd
= prev_cmd
;
1214 if ((*cmd
)->prefixlist
)
1215 cur_list
= *(*cmd
)->prefixlist
;
1223 /* Helper function for SYMBOL_COMPLETION_FUNCTION. */
1225 /* Return a vector of char pointers which point to the different
1226 possible completions in LIST of TEXT.
1228 WORD points in the same buffer as TEXT, and completions should be
1229 returned relative to this position. For example, suppose TEXT is "foo"
1230 and we want to complete to "foobar". If WORD is "oo", return
1231 "oobar"; if WORD is "baz/foo", return "baz/foobar". */
1234 complete_on_cmdlist (struct cmd_list_element
*list
, char *text
, char *word
)
1236 struct cmd_list_element
*ptr
;
1238 int sizeof_matchlist
;
1240 int textlen
= strlen (text
);
1242 sizeof_matchlist
= 10;
1243 matchlist
= (char **) xmalloc (sizeof_matchlist
* sizeof (char *));
1246 for (ptr
= list
; ptr
; ptr
= ptr
->next
)
1247 if (!strncmp (ptr
->name
, text
, textlen
)
1248 && !ptr
->abbrev_flag
1249 && (ptr
->function
.cfunc
1250 || ptr
->prefixlist
))
1252 if (matches
== sizeof_matchlist
)
1254 sizeof_matchlist
*= 2;
1255 matchlist
= (char **) xrealloc ((char *) matchlist
,
1257 * sizeof (char *)));
1260 matchlist
[matches
] = (char *)
1261 xmalloc (strlen (word
) + strlen (ptr
->name
) + 1);
1263 strcpy (matchlist
[matches
], ptr
->name
);
1264 else if (word
> text
)
1266 /* Return some portion of ptr->name. */
1267 strcpy (matchlist
[matches
], ptr
->name
+ (word
- text
));
1271 /* Return some of text plus ptr->name. */
1272 strncpy (matchlist
[matches
], word
, text
- word
);
1273 matchlist
[matches
][text
- word
] = '\0';
1274 strcat (matchlist
[matches
], ptr
->name
);
1286 matchlist
= (char **) xrealloc ((char *) matchlist
, ((matches
+ 1)
1287 * sizeof (char *)));
1288 matchlist
[matches
] = (char *) 0;
1294 /* Helper function for SYMBOL_COMPLETION_FUNCTION. */
1296 /* Return a vector of char pointers which point to the different
1297 possible completions in CMD 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_enum (const char *enumlist
[],
1310 int sizeof_matchlist
;
1312 int textlen
= strlen (text
);
1316 sizeof_matchlist
= 10;
1317 matchlist
= (char **) xmalloc (sizeof_matchlist
* sizeof (char *));
1320 for (i
= 0; (name
= enumlist
[i
]) != NULL
; i
++)
1321 if (strncmp (name
, text
, textlen
) == 0)
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 (name
) + 1);
1334 strcpy (matchlist
[matches
], name
);
1335 else if (word
> text
)
1337 /* Return some portion of name. */
1338 strcpy (matchlist
[matches
], name
+ (word
- text
));
1342 /* Return some of text plus name. */
1343 strncpy (matchlist
[matches
], word
, text
- word
);
1344 matchlist
[matches
][text
- word
] = '\0';
1345 strcat (matchlist
[matches
], name
);
1357 matchlist
= (char **) xrealloc ((char *) matchlist
, ((matches
+ 1)
1358 * sizeof (char *)));
1359 matchlist
[matches
] = (char *) 0;