1 /* Handle lists of commands, their decoding and documentation, for GDB.
2 Copyright 1986, 1989, 1990, 1991, 1998, 2000 Free Software Foundation, Inc.
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place - Suite 330,
17 Boston, MA 02111-1307, USA. */
22 #include "gnu-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
;
99 c
->completer
= make_symbol_completion_list
;
100 c
->type
= not_set_cmd
;
102 c
->var_type
= var_boolean
;
104 c
->user_commands
= NULL
;
105 c
->hookee_pre
= NULL
;
106 c
->hookee_post
= NULL
;
107 c
->cmd_pointer
= NULL
;
112 /* Same as above, except that the abbrev_flag is set. */
113 /* Note: Doesn't seem to be used anywhere currently. */
115 struct cmd_list_element
*
116 add_abbrev_cmd (char *name
, enum command_class
class, void (*fun
) (char *, int),
117 char *doc
, struct cmd_list_element
**list
)
119 register struct cmd_list_element
*c
120 = add_cmd (name
, class, fun
, doc
, list
);
126 /* Deprecates a command CMD.
127 REPLACEMENT is the name of the command which should be used in place
128 of this command, or NULL if no such command exists.
130 This function does not check to see if command REPLACEMENT exists
131 since gdb may not have gotten around to adding REPLACEMENT when this
134 Returns a pointer to the deprecated command. */
136 struct cmd_list_element
*
137 deprecate_cmd (struct cmd_list_element
*cmd
, char *replacement
)
139 cmd
->flags
|= (CMD_DEPRECATED
| DEPRECATED_WARN_USER
);
141 if (replacement
!= NULL
)
142 cmd
->replacement
= replacement
;
144 cmd
->replacement
= NULL
;
149 struct cmd_list_element
*
150 add_alias_cmd (char *name
, char *oldname
, enum command_class
class,
151 int abbrev_flag
, struct cmd_list_element
**list
)
153 /* Must do this since lookup_cmd tries to side-effect its first arg */
155 register struct cmd_list_element
*old
;
156 register struct cmd_list_element
*c
;
157 copied_name
= (char *) alloca (strlen (oldname
) + 1);
158 strcpy (copied_name
, oldname
);
159 old
= lookup_cmd (&copied_name
, *list
, "", 1, 1);
163 delete_cmd (name
, list
);
167 c
= add_cmd (name
, class, old
->function
.cfunc
, old
->doc
, list
);
168 c
->prefixlist
= old
->prefixlist
;
169 c
->prefixname
= old
->prefixname
;
170 c
->allow_unknown
= old
->allow_unknown
;
171 c
->abbrev_flag
= abbrev_flag
;
172 c
->cmd_pointer
= old
;
176 /* Like add_cmd but adds an element for a command prefix:
177 a name that should be followed by a subcommand to be looked up
178 in another command list. PREFIXLIST should be the address
179 of the variable containing that list. */
181 struct cmd_list_element
*
182 add_prefix_cmd (char *name
, enum command_class
class, void (*fun
) (char *, int),
183 char *doc
, struct cmd_list_element
**prefixlist
,
184 char *prefixname
, int allow_unknown
,
185 struct cmd_list_element
**list
)
187 register struct cmd_list_element
*c
= add_cmd (name
, class, fun
, doc
, list
);
188 c
->prefixlist
= prefixlist
;
189 c
->prefixname
= prefixname
;
190 c
->allow_unknown
= allow_unknown
;
194 /* Like add_prefix_cmd but sets the abbrev_flag on the new command. */
196 struct cmd_list_element
*
197 add_abbrev_prefix_cmd (char *name
, enum command_class
class,
198 void (*fun
) (char *, int), char *doc
,
199 struct cmd_list_element
**prefixlist
, char *prefixname
,
200 int allow_unknown
, struct cmd_list_element
**list
)
202 register struct cmd_list_element
*c
= add_cmd (name
, class, fun
, doc
, list
);
203 c
->prefixlist
= prefixlist
;
204 c
->prefixname
= prefixname
;
205 c
->allow_unknown
= allow_unknown
;
210 /* This is an empty "cfunc". */
212 not_just_help_class_command (char *args
, int from_tty
)
216 /* This is an empty "sfunc". */
217 static void empty_sfunc (char *, int, struct cmd_list_element
*);
220 empty_sfunc (char *args
, int from_tty
, struct cmd_list_element
*c
)
224 /* Add element named NAME to command list LIST (the list for set
225 or some sublist thereof).
226 CLASS is as in add_cmd.
227 VAR_TYPE is the kind of thing we are setting.
228 VAR is address of the variable being controlled by this command.
229 DOC is the documentation string. */
231 struct cmd_list_element
*
232 add_set_cmd (char *name
,
233 enum command_class
class,
237 struct cmd_list_element
**list
)
239 struct cmd_list_element
*c
240 = add_cmd (name
, class, NO_FUNCTION
, doc
, list
);
243 c
->var_type
= var_type
;
245 /* This needs to be something besides NO_FUNCTION so that this isn't
246 treated as a help class. */
247 c
->function
.sfunc
= empty_sfunc
;
251 /* Add element named NAME to command list LIST (the list for set
252 or some sublist thereof).
253 CLASS is as in add_cmd.
254 ENUMLIST is a list of strings which may follow NAME.
255 VAR is address of the variable which will contain the matching string
257 DOC is the documentation string. */
259 struct cmd_list_element
*
260 add_set_enum_cmd (char *name
,
261 enum command_class
class,
262 const char *enumlist
[],
265 struct cmd_list_element
**list
)
267 struct cmd_list_element
*c
268 = add_set_cmd (name
, class, var_enum
, var
, doc
, list
);
274 /* Add element named NAME to command list LIST (the list for set
275 or some sublist thereof).
276 CLASS is as in add_cmd.
277 VAR is address of the variable which will contain the value.
278 DOC is the documentation string. */
279 struct cmd_list_element
*
280 add_set_auto_boolean_cmd (char *name
,
281 enum command_class
class,
282 enum cmd_auto_boolean
*var
,
284 struct cmd_list_element
**list
)
286 static const char *auto_boolean_enums
[] = { "on", "off", "auto", NULL
};
287 struct cmd_list_element
*c
;
288 c
= add_set_cmd (name
, class, var_auto_boolean
, var
, doc
, list
);
289 c
->enums
= auto_boolean_enums
;
293 /* Where SETCMD has already been added, add the corresponding show
294 command to LIST and return a pointer to the added command (not
295 necessarily the head of LIST). */
296 struct cmd_list_element
*
297 add_show_from_set (struct cmd_list_element
*setcmd
,
298 struct cmd_list_element
**list
)
300 struct cmd_list_element
*showcmd
=
301 (struct cmd_list_element
*) xmalloc (sizeof (struct cmd_list_element
));
302 struct cmd_list_element
*p
;
304 memcpy (showcmd
, setcmd
, sizeof (struct cmd_list_element
));
305 delete_cmd (showcmd
->name
, list
);
306 showcmd
->type
= show_cmd
;
308 /* Replace "set " at start of docstring with "show ". */
309 if (setcmd
->doc
[0] == 'S' && setcmd
->doc
[1] == 'e'
310 && setcmd
->doc
[2] == 't' && setcmd
->doc
[3] == ' ')
311 showcmd
->doc
= concat ("Show ", setcmd
->doc
+ 4, NULL
);
313 fprintf_unfiltered (gdb_stderr
, "GDB internal error: Bad docstring for set command\n");
315 if (*list
== NULL
|| STRCMP ((*list
)->name
, showcmd
->name
) >= 0)
317 showcmd
->next
= *list
;
323 while (p
->next
&& STRCMP (p
->next
->name
, showcmd
->name
) <= 0)
327 showcmd
->next
= p
->next
;
334 /* Remove the command named NAME from the command list. */
337 delete_cmd (char *name
, struct cmd_list_element
**list
)
339 register struct cmd_list_element
*c
;
340 struct cmd_list_element
*p
;
342 while (*list
&& STREQ ((*list
)->name
, name
))
344 if ((*list
)->hookee_pre
)
345 (*list
)->hookee_pre
->hook_pre
= 0; /* Hook slips out of its mouth */
346 if ((*list
)->hookee_post
)
347 (*list
)->hookee_post
->hook_post
= 0; /* Hook slips out of its bottom */
354 for (c
= *list
; c
->next
;)
356 if (STREQ (c
->next
->name
, name
))
358 if (c
->next
->hookee_pre
)
359 c
->next
->hookee_pre
->hook_pre
= 0; /* hooked cmd gets away. */
360 if (c
->next
->hookee_post
)
361 c
->next
->hookee_post
->hook_post
= 0; /* remove post hook */
362 /* :( no fishing metaphore */
364 free ((PTR
) c
->next
);
372 /* Shorthands to the commands above. */
374 /* Add an element to the list of info subcommands. */
376 struct cmd_list_element
*
377 add_info (char *name
, void (*fun
) (char *, int), char *doc
)
379 return add_cmd (name
, no_class
, fun
, doc
, &infolist
);
382 /* Add an alias to the list of info subcommands. */
384 struct cmd_list_element
*
385 add_info_alias (char *name
, char *oldname
, int abbrev_flag
)
387 return add_alias_cmd (name
, oldname
, 0, abbrev_flag
, &infolist
);
390 /* Add an element to the list of commands. */
392 struct cmd_list_element
*
393 add_com (char *name
, enum command_class
class, void (*fun
) (char *, int),
396 return add_cmd (name
, class, fun
, doc
, &cmdlist
);
399 /* Add an alias or abbreviation command to the list of commands. */
401 struct cmd_list_element
*
402 add_com_alias (char *name
, char *oldname
, enum command_class
class,
405 return add_alias_cmd (name
, oldname
, class, abbrev_flag
, &cmdlist
);
408 /* Recursively walk the commandlist structures, and print out the
409 documentation of commands that match our regex in either their
410 name, or their documentation.
413 apropos_cmd (struct ui_file
*stream
, struct cmd_list_element
*commandlist
,
414 struct re_pattern_buffer
*regex
, char *prefix
)
416 register struct cmd_list_element
*c
;
417 int returnvalue
=1; /*Needed to avoid double printing*/
418 /* Walk through the commands */
419 for (c
=commandlist
;c
;c
=c
->next
)
423 /* Try to match against the name*/
424 returnvalue
=re_search(regex
,c
->name
,strlen(c
->name
),0,strlen(c
->name
),NULL
);
425 if (returnvalue
>= 0)
427 /* Stolen from help_cmd_list. We don't directly use
428 * help_cmd_list because it doesn't let us print out
431 fprintf_filtered (stream
, "%s%s -- ", prefix
, c
->name
);
432 print_doc_line (stream
, c
->doc
);
433 fputs_filtered ("\n", stream
);
434 returnvalue
=0; /*Set this so we don't print it again.*/
437 if (c
->doc
!= NULL
&& returnvalue
!= 0)
439 /* Try to match against documentation */
440 if (re_search(regex
,c
->doc
,strlen(c
->doc
),0,strlen(c
->doc
),NULL
) >=0)
442 /* Stolen from help_cmd_list. We don't directly use
443 * help_cmd_list because it doesn't let us print out
446 fprintf_filtered (stream
, "%s%s -- ", prefix
, c
->name
);
447 print_doc_line (stream
, c
->doc
);
448 fputs_filtered ("\n", stream
);
451 /* Check if this command has subcommands */
452 if (c
->prefixlist
!= NULL
)
454 /* Recursively call ourselves on the subcommand list,
455 passing the right prefix in.
457 apropos_cmd (stream
,*c
->prefixlist
,regex
,c
->prefixname
);
462 /* This command really has to deal with two things:
463 * 1) I want documentation on *this string* (usually called by
464 * "help commandname").
465 * 2) I want documentation on *this list* (usually called by
466 * giving a command that requires subcommands. Also called by saying
469 * I am going to split this into two seperate comamnds, help_cmd and
474 help_cmd (char *command
, struct ui_file
*stream
)
476 struct cmd_list_element
*c
;
477 extern struct cmd_list_element
*cmdlist
;
481 help_list (cmdlist
, "", all_classes
, stream
);
485 if (strcmp (command
, "all") == 0)
491 c
= lookup_cmd (&command
, cmdlist
, "", 0, 0);
496 /* There are three cases here.
497 If c->prefixlist is nonzero, we have a prefix command.
498 Print its documentation, then list its subcommands.
500 If c->function is nonzero, we really have a command.
501 Print its documentation and return.
503 If c->function is zero, we have a class name.
504 Print its documentation (as if it were a command)
505 and then set class to the number of this class
506 so that the commands in the class will be listed. */
508 fputs_filtered (c
->doc
, stream
);
509 fputs_filtered ("\n", stream
);
511 if (c
->prefixlist
== 0 && c
->function
.cfunc
!= NULL
)
513 fprintf_filtered (stream
, "\n");
515 /* If this is a prefix command, print it's subcommands */
517 help_list (*c
->prefixlist
, c
->prefixname
, all_commands
, stream
);
519 /* If this is a class name, print all of the commands in the class */
520 if (c
->function
.cfunc
== NULL
)
521 help_list (cmdlist
, "", c
->class, stream
);
523 if (c
->hook_pre
|| c
->hook_post
)
524 fprintf_filtered (stream
,
525 "\nThis command has a hook (or hooks) defined:\n");
528 fprintf_filtered (stream
,
529 "\tThis command is run after : %s (pre hook)\n",
532 fprintf_filtered (stream
,
533 "\tThis command is run before : %s (post hook)\n",
538 * Get a specific kind of help on a command list.
541 * CMDTYPE is the prefix to use in the title string.
542 * CLASS is the class with which to list the nodes of this list (see
543 * documentation for help_cmd_list below), As usual, ALL_COMMANDS for
544 * everything, ALL_CLASSES for just classes, and non-negative for only things
545 * in a specific class.
546 * and STREAM is the output stream on which to print things.
547 * If you call this routine with a class >= 0, it recurses.
550 help_list (struct cmd_list_element
*list
, char *cmdtype
,
551 enum command_class
class, struct ui_file
*stream
)
554 char *cmdtype1
, *cmdtype2
;
556 /* If CMDTYPE is "foo ", CMDTYPE1 gets " foo" and CMDTYPE2 gets "foo sub" */
557 len
= strlen (cmdtype
);
558 cmdtype1
= (char *) alloca (len
+ 1);
560 cmdtype2
= (char *) alloca (len
+ 4);
565 strncpy (cmdtype1
+ 1, cmdtype
, len
- 1);
567 strncpy (cmdtype2
, cmdtype
, len
- 1);
568 strcpy (cmdtype2
+ len
- 1, " sub");
571 if (class == all_classes
)
572 fprintf_filtered (stream
, "List of classes of %scommands:\n\n", cmdtype2
);
574 fprintf_filtered (stream
, "List of %scommands:\n\n", cmdtype2
);
576 help_cmd_list (list
, class, cmdtype
, (int) class >= 0, stream
);
578 if (class == all_classes
)
579 fprintf_filtered (stream
, "\n\
580 Type \"help%s\" followed by a class name for a list of commands in that class.",
583 fprintf_filtered (stream
, "\n\
584 Type \"help%s\" followed by %scommand name for full documentation.\n\
585 Command name abbreviations are allowed if unambiguous.\n",
590 help_all (struct ui_file
*stream
)
592 struct cmd_list_element
*c
;
593 extern struct cmd_list_element
*cmdlist
;
595 for (c
= cmdlist
; c
; c
= c
->next
)
599 /* If this is a prefix command, print it's subcommands */
601 help_cmd_list (*c
->prefixlist
, all_commands
, c
->prefixname
, 0, stream
);
603 /* If this is a class name, print all of the commands in the class */
604 else if (c
->function
.cfunc
== NULL
)
605 help_cmd_list (cmdlist
, c
->class, "", 0, stream
);
609 /* Print only the first line of STR on STREAM. */
611 print_doc_line (struct ui_file
*stream
, char *str
)
613 static char *line_buffer
= 0;
614 static int line_size
;
620 line_buffer
= (char *) xmalloc (line_size
);
624 while (*p
&& *p
!= '\n' && *p
!= '.' && *p
!= ',')
626 if (p
- str
> line_size
- 1)
628 line_size
= p
- str
+ 1;
629 free ((PTR
) line_buffer
);
630 line_buffer
= (char *) xmalloc (line_size
);
632 strncpy (line_buffer
, str
, p
- str
);
633 line_buffer
[p
- str
] = '\0';
634 if (islower (line_buffer
[0]))
635 line_buffer
[0] = toupper (line_buffer
[0]);
637 ui_out_text (uiout
, line_buffer
);
639 fputs_filtered (line_buffer
, stream
);
644 * Implement a help command on command list LIST.
645 * RECURSE should be non-zero if this should be done recursively on
646 * all sublists of LIST.
647 * PREFIX is the prefix to print before each command name.
648 * STREAM is the stream upon which the output should be written.
650 * A non-negative class number to list only commands in that
652 * ALL_COMMANDS to list all commands in list.
653 * ALL_CLASSES to list all classes in list.
655 * Note that RECURSE will be active on *all* sublists, not just the
656 * ones selected by the criteria above (ie. the selection mechanism
657 * is at the low level, not the high-level).
660 help_cmd_list (struct cmd_list_element
*list
, enum command_class
class,
661 char *prefix
, int recurse
, struct ui_file
*stream
)
663 register struct cmd_list_element
*c
;
665 for (c
= list
; c
; c
= c
->next
)
667 if (c
->abbrev_flag
== 0 &&
668 (class == all_commands
669 || (class == all_classes
&& c
->function
.cfunc
== NULL
)
670 || (class == c
->class && c
->function
.cfunc
!= NULL
)))
672 fprintf_filtered (stream
, "%s%s -- ", prefix
, c
->name
);
673 print_doc_line (stream
, c
->doc
);
674 fputs_filtered ("\n", stream
);
677 && c
->prefixlist
!= 0
678 && c
->abbrev_flag
== 0)
679 help_cmd_list (*c
->prefixlist
, class, c
->prefixname
, 1, stream
);
684 /* Search the input clist for 'command'. Return the command if
685 found (or NULL if not), and return the number of commands
688 static struct cmd_list_element
*
689 find_cmd (char *command
, int len
, struct cmd_list_element
*clist
,
690 int ignore_help_classes
, int *nfound
)
692 struct cmd_list_element
*found
, *c
;
694 found
= (struct cmd_list_element
*) NULL
;
696 for (c
= clist
; c
; c
= c
->next
)
697 if (!strncmp (command
, c
->name
, len
)
698 && (!ignore_help_classes
|| c
->function
.cfunc
))
702 if (c
->name
[len
] == '\0')
711 /* This routine takes a line of TEXT and a CLIST in which to start the
712 lookup. When it returns it will have incremented the text pointer past
713 the section of text it matched, set *RESULT_LIST to point to the list in
714 which the last word was matched, and will return a pointer to the cmd
715 list element which the text matches. It will return NULL if no match at
716 all was possible. It will return -1 (cast appropriately, ick) if ambigous
717 matches are possible; in this case *RESULT_LIST will be set to point to
718 the list in which there are ambiguous choices (and *TEXT will be set to
719 the ambiguous text string).
721 If the located command was an abbreviation, this routine returns the base
722 command of the abbreviation.
724 It does no error reporting whatsoever; control will always return
725 to the superior routine.
727 In the case of an ambiguous return (-1), *RESULT_LIST will be set to point
728 at the prefix_command (ie. the best match) *or* (special case) will be NULL
729 if no prefix command was ever found. For example, in the case of "info a",
730 "info" matches without ambiguity, but "a" could be "args" or "address", so
731 *RESULT_LIST is set to the cmd_list_element for "info". So in this case
732 RESULT_LIST should not be interpeted as a pointer to the beginning of a
733 list; it simply points to a specific command. In the case of an ambiguous
734 return *TEXT is advanced past the last non-ambiguous prefix (e.g.
735 "info t" can be "info types" or "info target"; upon return *TEXT has been
736 advanced past "info ").
738 If RESULT_LIST is NULL, don't set *RESULT_LIST (but don't otherwise
739 affect the operation).
741 This routine does *not* modify the text pointed to by TEXT.
743 If IGNORE_HELP_CLASSES is nonzero, ignore any command list elements which
744 are actually help classes rather than commands (i.e. the function field of
745 the struct cmd_list_element is NULL). */
747 struct cmd_list_element
*
748 lookup_cmd_1 (char **text
, struct cmd_list_element
*clist
,
749 struct cmd_list_element
**result_list
, int ignore_help_classes
)
752 int len
, tmp
, nfound
;
753 struct cmd_list_element
*found
, *c
;
756 while (**text
== ' ' || **text
== '\t')
759 /* Treating underscores as part of command words is important
760 so that "set args_foo()" doesn't get interpreted as
761 "set args _foo()". */
763 *p
&& (isalnum (*p
) || *p
== '-' || *p
== '_' ||
765 (*p
== '+' || *p
== '<' || *p
== '>' || *p
== '$')) ||
766 (xdb_commands
&& (*p
== '!' || *p
== '/' || *p
== '?')));
770 /* If nothing but whitespace, return 0. */
776 /* *text and p now bracket the first command word to lookup (and
777 it's length is len). We copy this into a local temporary */
780 command
= (char *) alloca (len
+ 1);
781 for (tmp
= 0; tmp
< len
; tmp
++)
783 char x
= (*text
)[tmp
];
791 found
= find_cmd (command
, len
, clist
, ignore_help_classes
, &nfound
);
794 ** We didn't find the command in the entered case, so lower case it
797 if (!found
|| nfound
== 0)
799 for (tmp
= 0; tmp
< len
; tmp
++)
801 char x
= command
[tmp
];
802 command
[tmp
] = isupper (x
) ? tolower (x
) : x
;
804 found
= find_cmd (command
, len
, clist
, ignore_help_classes
, &nfound
);
807 /* If nothing matches, we have a simple failure. */
813 if (result_list
!= NULL
)
814 /* Will be modified in calling routine
815 if we know what the prefix command is. */
817 return (struct cmd_list_element
*) -1; /* Ambiguous. */
820 /* We've matched something on this list. Move text pointer forward. */
824 if (found
->cmd_pointer
)
826 /* We drop the alias (abbreviation) in favor of the command it is
827 pointing to. If the alias is deprecated, though, we need to
828 warn the user about it before we drop it. Note that while we
829 are warning about the alias, we may also warn about the command
830 itself and we will adjust the appropriate DEPRECATED_WARN_USER
833 if (found
->flags
& DEPRECATED_WARN_USER
)
834 deprecated_cmd_warning (&line
);
835 found
= found
->cmd_pointer
;
837 /* If we found a prefix command, keep looking. */
839 if (found
->prefixlist
)
841 c
= lookup_cmd_1 (text
, *found
->prefixlist
, result_list
,
842 ignore_help_classes
);
845 /* Didn't find anything; this is as far as we got. */
846 if (result_list
!= NULL
)
847 *result_list
= clist
;
850 else if (c
== (struct cmd_list_element
*) -1)
852 /* We've gotten this far properly, but the next step
853 is ambiguous. We need to set the result list to the best
854 we've found (if an inferior hasn't already set it). */
855 if (result_list
!= NULL
)
857 /* This used to say *result_list = *found->prefixlist
858 If that was correct, need to modify the documentation
859 at the top of this function to clarify what is supposed
861 *result_list
= found
;
872 if (result_list
!= NULL
)
873 *result_list
= clist
;
878 /* All this hair to move the space to the front of cmdtype */
881 undef_cmd_error (char *cmdtype
, char *q
)
883 error ("Undefined %scommand: \"%s\". Try \"help%s%.*s\".",
887 strlen (cmdtype
) - 1,
891 /* Look up the contents of *LINE as a command in the command list LIST.
892 LIST is a chain of struct cmd_list_element's.
893 If it is found, return the struct cmd_list_element for that command
894 and update *LINE to point after the command name, at the first argument.
895 If not found, call error if ALLOW_UNKNOWN is zero
896 otherwise (or if error returns) return zero.
897 Call error if specified command is ambiguous,
898 unless ALLOW_UNKNOWN is negative.
899 CMDTYPE precedes the word "command" in the error message.
901 If INGNORE_HELP_CLASSES is nonzero, ignore any command list
902 elements which are actually help classes rather than commands (i.e.
903 the function field of the struct cmd_list_element is 0). */
905 struct cmd_list_element
*
906 lookup_cmd (char **line
, struct cmd_list_element
*list
, char *cmdtype
,
907 int allow_unknown
, int ignore_help_classes
)
909 struct cmd_list_element
*last_list
= 0;
910 struct cmd_list_element
*c
=
911 lookup_cmd_1 (line
, list
, &last_list
, ignore_help_classes
);
913 /* Note: Do not remove trailing whitespace here because this
914 would be wrong for complete_command. Jim Kingdon */
921 error ("Lack of needed %scommand", cmdtype
);
926 while (isalnum (*p
) || *p
== '-')
929 q
= (char *) alloca (p
- *line
+ 1);
930 strncpy (q
, *line
, p
- *line
);
932 undef_cmd_error (cmdtype
, q
);
938 else if (c
== (struct cmd_list_element
*) -1)
940 /* Ambigous. Local values should be off prefixlist or called
942 int local_allow_unknown
= (last_list
? last_list
->allow_unknown
:
944 char *local_cmdtype
= last_list
? last_list
->prefixname
: cmdtype
;
945 struct cmd_list_element
*local_list
=
946 (last_list
? *(last_list
->prefixlist
) : list
);
948 if (local_allow_unknown
< 0)
951 return last_list
; /* Found something. */
953 return 0; /* Found nothing. */
957 /* Report as error. */
962 ((*line
)[amb_len
] && (*line
)[amb_len
] != ' '
963 && (*line
)[amb_len
] != '\t');
968 for (c
= local_list
; c
; c
= c
->next
)
969 if (!strncmp (*line
, c
->name
, amb_len
))
971 if (strlen (ambbuf
) + strlen (c
->name
) + 6 < (int) sizeof ambbuf
)
974 strcat (ambbuf
, ", ");
975 strcat (ambbuf
, c
->name
);
979 strcat (ambbuf
, "..");
983 error ("Ambiguous %scommand \"%s\": %s.", local_cmdtype
,
990 /* We've got something. It may still not be what the caller
991 wants (if this command *needs* a subcommand). */
992 while (**line
== ' ' || **line
== '\t')
995 if (c
->prefixlist
&& **line
&& !c
->allow_unknown
)
996 undef_cmd_error (c
->prefixname
, *line
);
998 /* Seems to be what he wants. Return it. */
1004 /* We are here presumably because an alias or command in *TEXT is
1005 deprecated and a warning message should be generated. This function
1006 decodes *TEXT and potentially generates a warning message as outlined
1009 Example for 'set endian big' which has a fictitious alias 'seb'.
1011 If alias wasn't used in *TEXT, and the command is deprecated:
1012 "warning: 'set endian big' is deprecated."
1014 If alias was used, and only the alias is deprecated:
1015 "warning: 'seb' an alias for the command 'set endian big' is deprecated."
1017 If alias was used and command is deprecated (regardless of whether the
1018 alias itself is deprecated:
1020 "warning: 'set endian big' (seb) is deprecated."
1022 After the message has been sent, clear the appropriate flags in the
1023 command and/or the alias so the user is no longer bothered.
1027 deprecated_cmd_warning (char **text
)
1029 struct cmd_list_element
*alias
= NULL
;
1030 struct cmd_list_element
*prefix_cmd
= NULL
;
1031 struct cmd_list_element
*cmd
= NULL
;
1032 struct cmd_list_element
*c
;
1035 if (!lookup_cmd_composition (*text
, &alias
, &prefix_cmd
, &cmd
))
1036 /* return if text doesn't evaluate to a command */
1039 if (!((alias
? (alias
->flags
& DEPRECATED_WARN_USER
) : 0)
1040 || (cmd
->flags
& DEPRECATED_WARN_USER
) ) )
1041 /* return if nothing is deprecated */
1044 printf_filtered ("Warning:");
1046 if (alias
&& !(cmd
->flags
& CMD_DEPRECATED
))
1047 printf_filtered (" '%s', an alias for the", alias
->name
);
1049 printf_filtered (" command '");
1052 printf_filtered ("%s", prefix_cmd
->prefixname
);
1054 printf_filtered ("%s", cmd
->name
);
1056 if (alias
&& (cmd
->flags
& CMD_DEPRECATED
))
1057 printf_filtered ("' (%s) is deprecated.\n", alias
->name
);
1059 printf_filtered ("' is deprecated.\n");
1062 /* if it is only the alias that is deprecated, we want to indicate the
1063 new alias, otherwise we'll indicate the new command */
1065 if (alias
&& !(cmd
->flags
& CMD_DEPRECATED
))
1067 if (alias
->replacement
)
1068 printf_filtered ("Use '%s'.\n\n", alias
->replacement
);
1070 printf_filtered ("No alternative known.\n\n");
1074 if (cmd
->replacement
)
1075 printf_filtered ("Use '%s'.\n\n", cmd
->replacement
);
1077 printf_filtered ("No alternative known.\n\n");
1080 /* We've warned you, now we'll keep quiet */
1082 alias
->flags
&= ~DEPRECATED_WARN_USER
;
1084 cmd
->flags
&= ~DEPRECATED_WARN_USER
;
1089 /* Look up the contents of LINE as a command in the command list 'cmdlist'.
1090 Return 1 on success, 0 on failure.
1092 If LINE refers to an alias, *alias will point to that alias.
1094 If LINE is a postfix command (i.e. one that is preceeded by a prefix
1095 command) set *prefix_cmd.
1097 Set *cmd to point to the command LINE indicates.
1099 If any of *alias, *prefix_cmd, or *cmd cannot be determined or do not
1100 exist, they are NULL when we return.
1104 lookup_cmd_composition (char *text
,
1105 struct cmd_list_element
**alias
,
1106 struct cmd_list_element
**prefix_cmd
,
1107 struct cmd_list_element
**cmd
)
1110 int len
, tmp
, nfound
;
1111 struct cmd_list_element
*cur_list
;
1112 struct cmd_list_element
*prev_cmd
;
1121 /* Go through as many command lists as we need to
1122 to find the command TEXT refers to. */
1126 while (*text
== ' ' || *text
== '\t')
1129 /* Treating underscores as part of command words is important
1130 so that "set args_foo()" doesn't get interpreted as
1131 "set args _foo()". */
1133 *p
&& (isalnum (*p
) || *p
== '-' || *p
== '_' ||
1135 (*p
== '+' || *p
== '<' || *p
== '>' || *p
== '$')) ||
1136 (xdb_commands
&& (*p
== '!' || *p
== '/' || *p
== '?')));
1140 /* If nothing but whitespace, return. */
1146 /* text and p now bracket the first command word to lookup (and
1147 it's length is len). We copy this into a local temporary */
1149 command
= (char *) alloca (len
+ 1);
1150 for (tmp
= 0; tmp
< len
; tmp
++)
1155 command
[len
] = '\0';
1160 *cmd
= find_cmd (command
, len
, cur_list
, 1, &nfound
);
1162 /* We didn't find the command in the entered case, so lower case it
1165 if (!*cmd
|| nfound
== 0)
1167 for (tmp
= 0; tmp
< len
; tmp
++)
1169 char x
= command
[tmp
];
1170 command
[tmp
] = isupper (x
) ? tolower (x
) : x
;
1172 *cmd
= find_cmd (command
, len
, cur_list
, 1, &nfound
);
1175 if (*cmd
== (struct cmd_list_element
*) -1)
1177 return 0; /* ambiguous */
1181 return 0; /* nothing found */
1184 if ((*cmd
)->cmd_pointer
)
1186 /* cmd was actually an alias, we note that an alias was used
1187 (by assigning *alais) and we set *cmd.
1190 *cmd
= (*cmd
)->cmd_pointer
;
1192 *prefix_cmd
= prev_cmd
;
1194 if ((*cmd
)->prefixlist
)
1195 cur_list
= *(*cmd
)->prefixlist
;
1203 /* Helper function for SYMBOL_COMPLETION_FUNCTION. */
1205 /* Return a vector of char pointers which point to the different
1206 possible completions in LIST of TEXT.
1208 WORD points in the same buffer as TEXT, and completions should be
1209 returned relative to this position. For example, suppose TEXT is "foo"
1210 and we want to complete to "foobar". If WORD is "oo", return
1211 "oobar"; if WORD is "baz/foo", return "baz/foobar". */
1214 complete_on_cmdlist (struct cmd_list_element
*list
, char *text
, char *word
)
1216 struct cmd_list_element
*ptr
;
1218 int sizeof_matchlist
;
1220 int textlen
= strlen (text
);
1222 sizeof_matchlist
= 10;
1223 matchlist
= (char **) xmalloc (sizeof_matchlist
* sizeof (char *));
1226 for (ptr
= list
; ptr
; ptr
= ptr
->next
)
1227 if (!strncmp (ptr
->name
, text
, textlen
)
1228 && !ptr
->abbrev_flag
1229 && (ptr
->function
.cfunc
1230 || ptr
->prefixlist
))
1232 if (matches
== sizeof_matchlist
)
1234 sizeof_matchlist
*= 2;
1235 matchlist
= (char **) xrealloc ((char *) matchlist
,
1237 * sizeof (char *)));
1240 matchlist
[matches
] = (char *)
1241 xmalloc (strlen (word
) + strlen (ptr
->name
) + 1);
1243 strcpy (matchlist
[matches
], ptr
->name
);
1244 else if (word
> text
)
1246 /* Return some portion of ptr->name. */
1247 strcpy (matchlist
[matches
], ptr
->name
+ (word
- text
));
1251 /* Return some of text plus ptr->name. */
1252 strncpy (matchlist
[matches
], word
, text
- word
);
1253 matchlist
[matches
][text
- word
] = '\0';
1254 strcat (matchlist
[matches
], ptr
->name
);
1261 free ((PTR
) matchlist
);
1266 matchlist
= (char **) xrealloc ((char *) matchlist
, ((matches
+ 1)
1267 * sizeof (char *)));
1268 matchlist
[matches
] = (char *) 0;
1274 /* Helper function for SYMBOL_COMPLETION_FUNCTION. */
1276 /* Return a vector of char pointers which point to the different
1277 possible completions in CMD of TEXT.
1279 WORD points in the same buffer as TEXT, and completions should be
1280 returned relative to this position. For example, suppose TEXT is "foo"
1281 and we want to complete to "foobar". If WORD is "oo", return
1282 "oobar"; if WORD is "baz/foo", return "baz/foobar". */
1285 complete_on_enum (const char *enumlist
[],
1290 int sizeof_matchlist
;
1292 int textlen
= strlen (text
);
1296 sizeof_matchlist
= 10;
1297 matchlist
= (char **) xmalloc (sizeof_matchlist
* sizeof (char *));
1300 for (i
= 0; (name
= enumlist
[i
]) != NULL
; i
++)
1301 if (strncmp (name
, text
, textlen
) == 0)
1303 if (matches
== sizeof_matchlist
)
1305 sizeof_matchlist
*= 2;
1306 matchlist
= (char **) xrealloc ((char *) matchlist
,
1308 * sizeof (char *)));
1311 matchlist
[matches
] = (char *)
1312 xmalloc (strlen (word
) + strlen (name
) + 1);
1314 strcpy (matchlist
[matches
], name
);
1315 else if (word
> text
)
1317 /* Return some portion of name. */
1318 strcpy (matchlist
[matches
], name
+ (word
- text
));
1322 /* Return some of text plus name. */
1323 strncpy (matchlist
[matches
], word
, text
- word
);
1324 matchlist
[matches
][text
- word
] = '\0';
1325 strcat (matchlist
[matches
], name
);
1332 free ((PTR
) matchlist
);
1337 matchlist
= (char **) xrealloc ((char *) matchlist
, ((matches
+ 1)
1338 * sizeof (char *)));
1339 matchlist
[matches
] = (char *) 0;