1 /* Handle lists of commands, their decoding and documentation, for GDB.
3 Copyright 1986, 1989, 1990, 1991, 1998, 2000, 2001, 2002 Free
4 Software Foundation, Inc.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
24 #include "gdb_regex.h"
28 #include "cli/cli-cmds.h"
29 #include "cli/cli-decode.h"
31 /* Prototypes for local functions */
33 static void undef_cmd_error (char *, char *);
35 static struct cmd_list_element
*find_cmd (char *command
,
37 struct cmd_list_element
*clist
,
38 int ignore_help_classes
,
41 static void help_all (struct ui_file
*stream
);
43 /* Set the callback function for the specified command. For each both
44 the commands callback and func() are set. The latter set to a
45 bounce function (unless cfunc / sfunc is NULL that is). */
48 do_cfunc (struct cmd_list_element
*c
, char *args
, int from_tty
)
50 c
->function
.cfunc (args
, from_tty
); /* Ok. */
54 set_cmd_cfunc (struct cmd_list_element
*cmd
,
55 void (*cfunc
) (char *args
, int from_tty
))
61 cmd
->function
.cfunc
= cfunc
; /* Ok. */
65 do_sfunc (struct cmd_list_element
*c
, char *args
, int from_tty
)
67 c
->function
.sfunc (args
, from_tty
, c
); /* Ok. */
71 set_cmd_sfunc (struct cmd_list_element
*cmd
,
72 void (*sfunc
) (char *args
, int from_tty
,
73 struct cmd_list_element
* c
))
79 cmd
->function
.sfunc
= sfunc
; /* Ok. */
83 cmd_cfunc_eq (struct cmd_list_element
*cmd
,
84 void (*cfunc
) (char *args
, int from_tty
))
86 return cmd
->func
== do_cfunc
&& cmd
->function
.cfunc
== cfunc
;
90 cmd_type (struct cmd_list_element
*cmd
)
96 set_cmd_completer (struct cmd_list_element
*cmd
,
97 char **(*completer
) (char *text
, char *word
))
99 cmd
->completer
= completer
; /* Ok. */
103 /* Add element named NAME.
104 CLASS is the top level category into which commands are broken down
106 FUN should be the function to execute the command;
107 it will get a character string as argument, with leading
108 and trailing blanks already eliminated.
110 DOC is a documentation string for the command.
111 Its first line should be a complete sentence.
112 It should start with ? for a command that is an abbreviation
113 or with * for a command that most users don't need to know about.
115 Add this command to command list *LIST.
117 Returns a pointer to the added command (not necessarily the head
120 struct cmd_list_element
*
121 add_cmd (char *name
, enum command_class
class, void (*fun
) (char *, int),
122 char *doc
, struct cmd_list_element
**list
)
124 register struct cmd_list_element
*c
125 = (struct cmd_list_element
*) xmalloc (sizeof (struct cmd_list_element
));
126 struct cmd_list_element
*p
;
128 delete_cmd (name
, list
);
130 if (*list
== NULL
|| strcmp ((*list
)->name
, name
) >= 0)
138 while (p
->next
&& strcmp (p
->next
->name
, name
) <= 0)
148 set_cmd_cfunc (c
, fun
);
151 c
->replacement
= NULL
;
152 c
->pre_show_hook
= NULL
;
156 c
->prefixlist
= NULL
;
157 c
->prefixname
= NULL
;
158 c
->allow_unknown
= 0;
160 set_cmd_completer (c
, make_symbol_completion_list
);
161 c
->type
= not_set_cmd
;
163 c
->var_type
= var_boolean
;
165 c
->user_commands
= NULL
;
166 c
->hookee_pre
= NULL
;
167 c
->hookee_post
= NULL
;
168 c
->cmd_pointer
= NULL
;
173 /* Same as above, except that the abbrev_flag is set. */
174 /* Note: Doesn't seem to be used anywhere currently. */
176 struct cmd_list_element
*
177 add_abbrev_cmd (char *name
, enum command_class
class, void (*fun
) (char *, int),
178 char *doc
, struct cmd_list_element
**list
)
180 register struct cmd_list_element
*c
181 = add_cmd (name
, class, fun
, doc
, list
);
187 /* Deprecates a command CMD.
188 REPLACEMENT is the name of the command which should be used in place
189 of this command, or NULL if no such command exists.
191 This function does not check to see if command REPLACEMENT exists
192 since gdb may not have gotten around to adding REPLACEMENT when this
195 Returns a pointer to the deprecated command. */
197 struct cmd_list_element
*
198 deprecate_cmd (struct cmd_list_element
*cmd
, char *replacement
)
200 cmd
->flags
|= (CMD_DEPRECATED
| DEPRECATED_WARN_USER
);
202 if (replacement
!= NULL
)
203 cmd
->replacement
= replacement
;
205 cmd
->replacement
= NULL
;
210 struct cmd_list_element
*
211 add_alias_cmd (char *name
, char *oldname
, enum command_class
class,
212 int abbrev_flag
, struct cmd_list_element
**list
)
214 /* Must do this since lookup_cmd tries to side-effect its first arg */
216 register struct cmd_list_element
*old
;
217 register struct cmd_list_element
*c
;
218 copied_name
= (char *) alloca (strlen (oldname
) + 1);
219 strcpy (copied_name
, oldname
);
220 old
= lookup_cmd (&copied_name
, *list
, "", 1, 1);
224 delete_cmd (name
, list
);
228 c
= add_cmd (name
, class, NULL
, old
->doc
, list
);
229 /* NOTE: Both FUNC and all the FUNCTIONs need to be copied. */
231 c
->function
= old
->function
;
232 c
->prefixlist
= old
->prefixlist
;
233 c
->prefixname
= old
->prefixname
;
234 c
->allow_unknown
= old
->allow_unknown
;
235 c
->abbrev_flag
= abbrev_flag
;
236 c
->cmd_pointer
= old
;
240 /* Like add_cmd but adds an element for a command prefix:
241 a name that should be followed by a subcommand to be looked up
242 in another command list. PREFIXLIST should be the address
243 of the variable containing that list. */
245 struct cmd_list_element
*
246 add_prefix_cmd (char *name
, enum command_class
class, void (*fun
) (char *, int),
247 char *doc
, struct cmd_list_element
**prefixlist
,
248 char *prefixname
, int allow_unknown
,
249 struct cmd_list_element
**list
)
251 register struct cmd_list_element
*c
= add_cmd (name
, class, fun
, doc
, list
);
252 c
->prefixlist
= prefixlist
;
253 c
->prefixname
= prefixname
;
254 c
->allow_unknown
= allow_unknown
;
258 /* Like add_prefix_cmd but sets the abbrev_flag on the new command. */
260 struct cmd_list_element
*
261 add_abbrev_prefix_cmd (char *name
, enum command_class
class,
262 void (*fun
) (char *, int), char *doc
,
263 struct cmd_list_element
**prefixlist
, char *prefixname
,
264 int allow_unknown
, struct cmd_list_element
**list
)
266 register struct cmd_list_element
*c
= add_cmd (name
, class, fun
, doc
, list
);
267 c
->prefixlist
= prefixlist
;
268 c
->prefixname
= prefixname
;
269 c
->allow_unknown
= allow_unknown
;
274 /* This is an empty "cfunc". */
276 not_just_help_class_command (char *args
, int from_tty
)
280 /* This is an empty "sfunc". */
281 static void empty_sfunc (char *, int, struct cmd_list_element
*);
284 empty_sfunc (char *args
, int from_tty
, struct cmd_list_element
*c
)
288 /* Add element named NAME to command list LIST (the list for set
289 or some sublist thereof).
290 CLASS is as in add_cmd.
291 VAR_TYPE is the kind of thing we are setting.
292 VAR is address of the variable being controlled by this command.
293 DOC is the documentation string. */
295 struct cmd_list_element
*
296 add_set_cmd (char *name
,
297 enum command_class
class,
301 struct cmd_list_element
**list
)
303 struct cmd_list_element
*c
= add_cmd (name
, class, NULL
, doc
, list
);
306 c
->var_type
= var_type
;
308 /* This needs to be something besides NULL so that this isn't
309 treated as a help class. */
310 set_cmd_sfunc (c
, empty_sfunc
);
314 /* Add element named NAME to command list LIST (the list for set
315 or some sublist thereof).
316 CLASS is as in add_cmd.
317 ENUMLIST is a list of strings which may follow NAME.
318 VAR is address of the variable which will contain the matching string
320 DOC is the documentation string. */
322 struct cmd_list_element
*
323 add_set_enum_cmd (char *name
,
324 enum command_class
class,
325 const char *enumlist
[],
328 struct cmd_list_element
**list
)
330 struct cmd_list_element
*c
331 = add_set_cmd (name
, class, var_enum
, var
, doc
, list
);
337 /* Add element named NAME to command list LIST (the list for set
338 or some sublist thereof).
339 CLASS is as in add_cmd.
340 VAR is address of the variable which will contain the value.
341 DOC is the documentation string. */
342 struct cmd_list_element
*
343 add_set_auto_boolean_cmd (char *name
,
344 enum command_class
class,
345 enum cmd_auto_boolean
*var
,
347 struct cmd_list_element
**list
)
349 static const char *auto_boolean_enums
[] = { "on", "off", "auto", NULL
};
350 struct cmd_list_element
*c
;
351 c
= add_set_cmd (name
, class, var_auto_boolean
, var
, doc
, list
);
352 c
->enums
= auto_boolean_enums
;
356 /* Add element named NAME to command list LIST (the list for set
357 or some sublist thereof).
358 CLASS is as in add_cmd.
359 VAR is address of the variable which will contain the value.
360 DOC is the documentation string. */
361 struct cmd_list_element
*
362 add_set_boolean_cmd (char *name
,
363 enum command_class
class,
366 struct cmd_list_element
**list
)
368 static const char *boolean_enums
[] = { "on", "off", NULL
};
369 struct cmd_list_element
*c
;
370 c
= add_set_cmd (name
, class, var_boolean
, var
, doc
, list
);
371 c
->enums
= boolean_enums
;
375 /* Where SETCMD has already been added, add the corresponding show
376 command to LIST and return a pointer to the added command (not
377 necessarily the head of LIST). */
378 struct cmd_list_element
*
379 add_show_from_set (struct cmd_list_element
*setcmd
,
380 struct cmd_list_element
**list
)
382 struct cmd_list_element
*showcmd
=
383 (struct cmd_list_element
*) xmalloc (sizeof (struct cmd_list_element
));
384 struct cmd_list_element
*p
;
386 memcpy (showcmd
, setcmd
, sizeof (struct cmd_list_element
));
387 delete_cmd (showcmd
->name
, list
);
388 showcmd
->type
= show_cmd
;
390 /* Replace "set " at start of docstring with "show ". */
391 if (setcmd
->doc
[0] == 'S' && setcmd
->doc
[1] == 'e'
392 && setcmd
->doc
[2] == 't' && setcmd
->doc
[3] == ' ')
393 showcmd
->doc
= concat ("Show ", setcmd
->doc
+ 4, NULL
);
395 fprintf_unfiltered (gdb_stderr
, "GDB internal error: Bad docstring for set command\n");
397 if (*list
== NULL
|| strcmp ((*list
)->name
, showcmd
->name
) >= 0)
399 showcmd
->next
= *list
;
405 while (p
->next
&& strcmp (p
->next
->name
, showcmd
->name
) <= 0)
409 showcmd
->next
= p
->next
;
416 /* Remove the command named NAME from the command list. */
419 delete_cmd (char *name
, struct cmd_list_element
**list
)
421 register struct cmd_list_element
*c
;
422 struct cmd_list_element
*p
;
424 while (*list
&& STREQ ((*list
)->name
, name
))
426 if ((*list
)->hookee_pre
)
427 (*list
)->hookee_pre
->hook_pre
= 0; /* Hook slips out of its mouth */
428 if ((*list
)->hookee_post
)
429 (*list
)->hookee_post
->hook_post
= 0; /* Hook slips out of its bottom */
436 for (c
= *list
; c
->next
;)
438 if (STREQ (c
->next
->name
, name
))
440 if (c
->next
->hookee_pre
)
441 c
->next
->hookee_pre
->hook_pre
= 0; /* hooked cmd gets away. */
442 if (c
->next
->hookee_post
)
443 c
->next
->hookee_post
->hook_post
= 0; /* remove post hook */
444 /* :( no fishing metaphore */
454 /* Shorthands to the commands above. */
456 /* Add an element to the list of info subcommands. */
458 struct cmd_list_element
*
459 add_info (char *name
, void (*fun
) (char *, int), char *doc
)
461 return add_cmd (name
, no_class
, fun
, doc
, &infolist
);
464 /* Add an alias to the list of info subcommands. */
466 struct cmd_list_element
*
467 add_info_alias (char *name
, char *oldname
, int abbrev_flag
)
469 return add_alias_cmd (name
, oldname
, 0, abbrev_flag
, &infolist
);
472 /* Add an element to the list of commands. */
474 struct cmd_list_element
*
475 add_com (char *name
, enum command_class
class, void (*fun
) (char *, int),
478 return add_cmd (name
, class, fun
, doc
, &cmdlist
);
481 /* Add an alias or abbreviation command to the list of commands. */
483 struct cmd_list_element
*
484 add_com_alias (char *name
, char *oldname
, enum command_class
class,
487 return add_alias_cmd (name
, oldname
, class, abbrev_flag
, &cmdlist
);
490 /* Recursively walk the commandlist structures, and print out the
491 documentation of commands that match our regex in either their
492 name, or their documentation.
495 apropos_cmd (struct ui_file
*stream
, struct cmd_list_element
*commandlist
,
496 struct re_pattern_buffer
*regex
, char *prefix
)
498 register struct cmd_list_element
*c
;
499 int returnvalue
=1; /*Needed to avoid double printing*/
500 /* Walk through the commands */
501 for (c
=commandlist
;c
;c
=c
->next
)
505 /* Try to match against the name*/
506 returnvalue
=re_search(regex
,c
->name
,strlen(c
->name
),0,strlen(c
->name
),NULL
);
507 if (returnvalue
>= 0)
509 /* Stolen from help_cmd_list. We don't directly use
510 * help_cmd_list because it doesn't let us print out
513 fprintf_filtered (stream
, "%s%s -- ", prefix
, c
->name
);
514 print_doc_line (stream
, c
->doc
);
515 fputs_filtered ("\n", stream
);
516 returnvalue
=0; /*Set this so we don't print it again.*/
519 if (c
->doc
!= NULL
&& returnvalue
!= 0)
521 /* Try to match against documentation */
522 if (re_search(regex
,c
->doc
,strlen(c
->doc
),0,strlen(c
->doc
),NULL
) >=0)
524 /* Stolen from help_cmd_list. We don't directly use
525 * help_cmd_list because it doesn't let us print out
528 fprintf_filtered (stream
, "%s%s -- ", prefix
, c
->name
);
529 print_doc_line (stream
, c
->doc
);
530 fputs_filtered ("\n", stream
);
533 /* Check if this command has subcommands */
534 if (c
->prefixlist
!= NULL
)
536 /* Recursively call ourselves on the subcommand list,
537 passing the right prefix in.
539 apropos_cmd (stream
,*c
->prefixlist
,regex
,c
->prefixname
);
544 /* This command really has to deal with two things:
545 * 1) I want documentation on *this string* (usually called by
546 * "help commandname").
547 * 2) I want documentation on *this list* (usually called by
548 * giving a command that requires subcommands. Also called by saying
551 * I am going to split this into two seperate comamnds, help_cmd and
556 help_cmd (char *command
, struct ui_file
*stream
)
558 struct cmd_list_element
*c
;
559 extern struct cmd_list_element
*cmdlist
;
563 help_list (cmdlist
, "", all_classes
, stream
);
567 if (strcmp (command
, "all") == 0)
573 c
= lookup_cmd (&command
, cmdlist
, "", 0, 0);
578 /* There are three cases here.
579 If c->prefixlist is nonzero, we have a prefix command.
580 Print its documentation, then list its subcommands.
582 If c->func is non NULL, we really have a command. Print its
583 documentation and return.
585 If c->func is NULL, we have a class name. Print its
586 documentation (as if it were a command) and then set class to the
587 number of this class so that the commands in the class will be
590 fputs_filtered (c
->doc
, stream
);
591 fputs_filtered ("\n", stream
);
593 if (c
->prefixlist
== 0 && c
->func
!= NULL
)
595 fprintf_filtered (stream
, "\n");
597 /* If this is a prefix command, print it's subcommands */
599 help_list (*c
->prefixlist
, c
->prefixname
, all_commands
, stream
);
601 /* If this is a class name, print all of the commands in the class */
603 help_list (cmdlist
, "", c
->class, stream
);
605 if (c
->hook_pre
|| c
->hook_post
)
606 fprintf_filtered (stream
,
607 "\nThis command has a hook (or hooks) defined:\n");
610 fprintf_filtered (stream
,
611 "\tThis command is run after : %s (pre hook)\n",
614 fprintf_filtered (stream
,
615 "\tThis command is run before : %s (post hook)\n",
620 * Get a specific kind of help on a command list.
623 * CMDTYPE is the prefix to use in the title string.
624 * CLASS is the class with which to list the nodes of this list (see
625 * documentation for help_cmd_list below), As usual, ALL_COMMANDS for
626 * everything, ALL_CLASSES for just classes, and non-negative for only things
627 * in a specific class.
628 * and STREAM is the output stream on which to print things.
629 * If you call this routine with a class >= 0, it recurses.
632 help_list (struct cmd_list_element
*list
, char *cmdtype
,
633 enum command_class
class, struct ui_file
*stream
)
636 char *cmdtype1
, *cmdtype2
;
638 /* If CMDTYPE is "foo ", CMDTYPE1 gets " foo" and CMDTYPE2 gets "foo sub" */
639 len
= strlen (cmdtype
);
640 cmdtype1
= (char *) alloca (len
+ 1);
642 cmdtype2
= (char *) alloca (len
+ 4);
647 strncpy (cmdtype1
+ 1, cmdtype
, len
- 1);
649 strncpy (cmdtype2
, cmdtype
, len
- 1);
650 strcpy (cmdtype2
+ len
- 1, " sub");
653 if (class == all_classes
)
654 fprintf_filtered (stream
, "List of classes of %scommands:\n\n", cmdtype2
);
656 fprintf_filtered (stream
, "List of %scommands:\n\n", cmdtype2
);
658 help_cmd_list (list
, class, cmdtype
, (int) class >= 0, stream
);
660 if (class == all_classes
)
661 fprintf_filtered (stream
, "\n\
662 Type \"help%s\" followed by a class name for a list of commands in that class.",
665 fprintf_filtered (stream
, "\n\
666 Type \"help%s\" followed by %scommand name for full documentation.\n\
667 Command name abbreviations are allowed if unambiguous.\n",
672 help_all (struct ui_file
*stream
)
674 struct cmd_list_element
*c
;
675 extern struct cmd_list_element
*cmdlist
;
677 for (c
= cmdlist
; c
; c
= c
->next
)
681 /* If this is a prefix command, print it's subcommands */
683 help_cmd_list (*c
->prefixlist
, all_commands
, c
->prefixname
, 0, stream
);
685 /* If this is a class name, print all of the commands in the class */
686 else if (c
->func
== NULL
)
687 help_cmd_list (cmdlist
, c
->class, "", 0, stream
);
691 /* Print only the first line of STR on STREAM. */
693 print_doc_line (struct ui_file
*stream
, char *str
)
695 static char *line_buffer
= 0;
696 static int line_size
;
702 line_buffer
= (char *) xmalloc (line_size
);
706 while (*p
&& *p
!= '\n' && *p
!= '.' && *p
!= ',')
708 if (p
- str
> line_size
- 1)
710 line_size
= p
- str
+ 1;
712 line_buffer
= (char *) xmalloc (line_size
);
714 strncpy (line_buffer
, str
, p
- str
);
715 line_buffer
[p
- str
] = '\0';
716 if (islower (line_buffer
[0]))
717 line_buffer
[0] = toupper (line_buffer
[0]);
718 ui_out_text (uiout
, line_buffer
);
722 * Implement a help command on command list LIST.
723 * RECURSE should be non-zero if this should be done recursively on
724 * all sublists of LIST.
725 * PREFIX is the prefix to print before each command name.
726 * STREAM is the stream upon which the output should be written.
728 * A non-negative class number to list only commands in that
730 * ALL_COMMANDS to list all commands in list.
731 * ALL_CLASSES to list all classes in list.
733 * Note that RECURSE will be active on *all* sublists, not just the
734 * ones selected by the criteria above (ie. the selection mechanism
735 * is at the low level, not the high-level).
738 help_cmd_list (struct cmd_list_element
*list
, enum command_class
class,
739 char *prefix
, int recurse
, struct ui_file
*stream
)
741 register struct cmd_list_element
*c
;
743 for (c
= list
; c
; c
= c
->next
)
745 if (c
->abbrev_flag
== 0 &&
746 (class == all_commands
747 || (class == all_classes
&& c
->func
== NULL
)
748 || (class == c
->class && c
->func
!= NULL
)))
750 fprintf_filtered (stream
, "%s%s -- ", prefix
, c
->name
);
751 print_doc_line (stream
, c
->doc
);
752 fputs_filtered ("\n", stream
);
755 && c
->prefixlist
!= 0
756 && c
->abbrev_flag
== 0)
757 help_cmd_list (*c
->prefixlist
, class, c
->prefixname
, 1, stream
);
762 /* Search the input clist for 'command'. Return the command if
763 found (or NULL if not), and return the number of commands
766 static struct cmd_list_element
*
767 find_cmd (char *command
, int len
, struct cmd_list_element
*clist
,
768 int ignore_help_classes
, int *nfound
)
770 struct cmd_list_element
*found
, *c
;
772 found
= (struct cmd_list_element
*) NULL
;
774 for (c
= clist
; c
; c
= c
->next
)
775 if (!strncmp (command
, c
->name
, len
)
776 && (!ignore_help_classes
|| c
->func
))
780 if (c
->name
[len
] == '\0')
789 /* This routine takes a line of TEXT and a CLIST in which to start the
790 lookup. When it returns it will have incremented the text pointer past
791 the section of text it matched, set *RESULT_LIST to point to the list in
792 which the last word was matched, and will return a pointer to the cmd
793 list element which the text matches. It will return NULL if no match at
794 all was possible. It will return -1 (cast appropriately, ick) if ambigous
795 matches are possible; in this case *RESULT_LIST will be set to point to
796 the list in which there are ambiguous choices (and *TEXT will be set to
797 the ambiguous text string).
799 If the located command was an abbreviation, this routine returns the base
800 command of the abbreviation.
802 It does no error reporting whatsoever; control will always return
803 to the superior routine.
805 In the case of an ambiguous return (-1), *RESULT_LIST will be set to point
806 at the prefix_command (ie. the best match) *or* (special case) will be NULL
807 if no prefix command was ever found. For example, in the case of "info a",
808 "info" matches without ambiguity, but "a" could be "args" or "address", so
809 *RESULT_LIST is set to the cmd_list_element for "info". So in this case
810 RESULT_LIST should not be interpeted as a pointer to the beginning of a
811 list; it simply points to a specific command. In the case of an ambiguous
812 return *TEXT is advanced past the last non-ambiguous prefix (e.g.
813 "info t" can be "info types" or "info target"; upon return *TEXT has been
814 advanced past "info ").
816 If RESULT_LIST is NULL, don't set *RESULT_LIST (but don't otherwise
817 affect the operation).
819 This routine does *not* modify the text pointed to by TEXT.
821 If IGNORE_HELP_CLASSES is nonzero, ignore any command list elements which
822 are actually help classes rather than commands (i.e. the function field of
823 the struct cmd_list_element is NULL). */
825 struct cmd_list_element
*
826 lookup_cmd_1 (char **text
, struct cmd_list_element
*clist
,
827 struct cmd_list_element
**result_list
, int ignore_help_classes
)
830 int len
, tmp
, nfound
;
831 struct cmd_list_element
*found
, *c
;
834 while (**text
== ' ' || **text
== '\t')
837 /* Treating underscores as part of command words is important
838 so that "set args_foo()" doesn't get interpreted as
839 "set args _foo()". */
841 *p
&& (isalnum (*p
) || *p
== '-' || *p
== '_' ||
843 (*p
== '+' || *p
== '<' || *p
== '>' || *p
== '$')) ||
844 (xdb_commands
&& (*p
== '!' || *p
== '/' || *p
== '?')));
848 /* If nothing but whitespace, return 0. */
854 /* *text and p now bracket the first command word to lookup (and
855 it's length is len). We copy this into a local temporary */
858 command
= (char *) alloca (len
+ 1);
859 for (tmp
= 0; tmp
< len
; tmp
++)
861 char x
= (*text
)[tmp
];
869 found
= find_cmd (command
, len
, clist
, ignore_help_classes
, &nfound
);
872 ** We didn't find the command in the entered case, so lower case it
875 if (!found
|| nfound
== 0)
877 for (tmp
= 0; tmp
< len
; tmp
++)
879 char x
= command
[tmp
];
880 command
[tmp
] = isupper (x
) ? tolower (x
) : x
;
882 found
= find_cmd (command
, len
, clist
, ignore_help_classes
, &nfound
);
885 /* If nothing matches, we have a simple failure. */
891 if (result_list
!= NULL
)
892 /* Will be modified in calling routine
893 if we know what the prefix command is. */
895 return (struct cmd_list_element
*) -1; /* Ambiguous. */
898 /* We've matched something on this list. Move text pointer forward. */
902 if (found
->cmd_pointer
)
904 /* We drop the alias (abbreviation) in favor of the command it is
905 pointing to. If the alias is deprecated, though, we need to
906 warn the user about it before we drop it. Note that while we
907 are warning about the alias, we may also warn about the command
908 itself and we will adjust the appropriate DEPRECATED_WARN_USER
911 if (found
->flags
& DEPRECATED_WARN_USER
)
912 deprecated_cmd_warning (&line
);
913 found
= found
->cmd_pointer
;
915 /* If we found a prefix command, keep looking. */
917 if (found
->prefixlist
)
919 c
= lookup_cmd_1 (text
, *found
->prefixlist
, result_list
,
920 ignore_help_classes
);
923 /* Didn't find anything; this is as far as we got. */
924 if (result_list
!= NULL
)
925 *result_list
= clist
;
928 else if (c
== (struct cmd_list_element
*) -1)
930 /* We've gotten this far properly, but the next step
931 is ambiguous. We need to set the result list to the best
932 we've found (if an inferior hasn't already set it). */
933 if (result_list
!= NULL
)
935 /* This used to say *result_list = *found->prefixlist
936 If that was correct, need to modify the documentation
937 at the top of this function to clarify what is supposed
939 *result_list
= found
;
950 if (result_list
!= NULL
)
951 *result_list
= clist
;
956 /* All this hair to move the space to the front of cmdtype */
959 undef_cmd_error (char *cmdtype
, char *q
)
961 error ("Undefined %scommand: \"%s\". Try \"help%s%.*s\".",
965 strlen (cmdtype
) - 1,
969 /* Look up the contents of *LINE as a command in the command list LIST.
970 LIST is a chain of struct cmd_list_element's.
971 If it is found, return the struct cmd_list_element for that command
972 and update *LINE to point after the command name, at the first argument.
973 If not found, call error if ALLOW_UNKNOWN is zero
974 otherwise (or if error returns) return zero.
975 Call error if specified command is ambiguous,
976 unless ALLOW_UNKNOWN is negative.
977 CMDTYPE precedes the word "command" in the error message.
979 If INGNORE_HELP_CLASSES is nonzero, ignore any command list
980 elements which are actually help classes rather than commands (i.e.
981 the function field of the struct cmd_list_element is 0). */
983 struct cmd_list_element
*
984 lookup_cmd (char **line
, struct cmd_list_element
*list
, char *cmdtype
,
985 int allow_unknown
, int ignore_help_classes
)
987 struct cmd_list_element
*last_list
= 0;
988 struct cmd_list_element
*c
=
989 lookup_cmd_1 (line
, list
, &last_list
, ignore_help_classes
);
991 /* Note: Do not remove trailing whitespace here because this
992 would be wrong for complete_command. Jim Kingdon */
999 error ("Lack of needed %scommand", cmdtype
);
1002 char *p
= *line
, *q
;
1004 while (isalnum (*p
) || *p
== '-')
1007 q
= (char *) alloca (p
- *line
+ 1);
1008 strncpy (q
, *line
, p
- *line
);
1009 q
[p
- *line
] = '\0';
1010 undef_cmd_error (cmdtype
, q
);
1016 else if (c
== (struct cmd_list_element
*) -1)
1018 /* Ambigous. Local values should be off prefixlist or called
1020 int local_allow_unknown
= (last_list
? last_list
->allow_unknown
:
1022 char *local_cmdtype
= last_list
? last_list
->prefixname
: cmdtype
;
1023 struct cmd_list_element
*local_list
=
1024 (last_list
? *(last_list
->prefixlist
) : list
);
1026 if (local_allow_unknown
< 0)
1029 return last_list
; /* Found something. */
1031 return 0; /* Found nothing. */
1035 /* Report as error. */
1040 ((*line
)[amb_len
] && (*line
)[amb_len
] != ' '
1041 && (*line
)[amb_len
] != '\t');
1046 for (c
= local_list
; c
; c
= c
->next
)
1047 if (!strncmp (*line
, c
->name
, amb_len
))
1049 if (strlen (ambbuf
) + strlen (c
->name
) + 6 < (int) sizeof ambbuf
)
1051 if (strlen (ambbuf
))
1052 strcat (ambbuf
, ", ");
1053 strcat (ambbuf
, c
->name
);
1057 strcat (ambbuf
, "..");
1061 error ("Ambiguous %scommand \"%s\": %s.", local_cmdtype
,
1063 return 0; /* lint */
1068 /* We've got something. It may still not be what the caller
1069 wants (if this command *needs* a subcommand). */
1070 while (**line
== ' ' || **line
== '\t')
1073 if (c
->prefixlist
&& **line
&& !c
->allow_unknown
)
1074 undef_cmd_error (c
->prefixname
, *line
);
1076 /* Seems to be what he wants. Return it. */
1082 /* We are here presumably because an alias or command in *TEXT is
1083 deprecated and a warning message should be generated. This function
1084 decodes *TEXT and potentially generates a warning message as outlined
1087 Example for 'set endian big' which has a fictitious alias 'seb'.
1089 If alias wasn't used in *TEXT, and the command is deprecated:
1090 "warning: 'set endian big' is deprecated."
1092 If alias was used, and only the alias is deprecated:
1093 "warning: 'seb' an alias for the command 'set endian big' is deprecated."
1095 If alias was used and command is deprecated (regardless of whether the
1096 alias itself is deprecated:
1098 "warning: 'set endian big' (seb) is deprecated."
1100 After the message has been sent, clear the appropriate flags in the
1101 command and/or the alias so the user is no longer bothered.
1105 deprecated_cmd_warning (char **text
)
1107 struct cmd_list_element
*alias
= NULL
;
1108 struct cmd_list_element
*prefix_cmd
= NULL
;
1109 struct cmd_list_element
*cmd
= NULL
;
1110 struct cmd_list_element
*c
;
1113 if (!lookup_cmd_composition (*text
, &alias
, &prefix_cmd
, &cmd
))
1114 /* return if text doesn't evaluate to a command */
1117 if (!((alias
? (alias
->flags
& DEPRECATED_WARN_USER
) : 0)
1118 || (cmd
->flags
& DEPRECATED_WARN_USER
) ) )
1119 /* return if nothing is deprecated */
1122 printf_filtered ("Warning:");
1124 if (alias
&& !(cmd
->flags
& CMD_DEPRECATED
))
1125 printf_filtered (" '%s', an alias for the", alias
->name
);
1127 printf_filtered (" command '");
1130 printf_filtered ("%s", prefix_cmd
->prefixname
);
1132 printf_filtered ("%s", cmd
->name
);
1134 if (alias
&& (cmd
->flags
& CMD_DEPRECATED
))
1135 printf_filtered ("' (%s) is deprecated.\n", alias
->name
);
1137 printf_filtered ("' is deprecated.\n");
1140 /* if it is only the alias that is deprecated, we want to indicate the
1141 new alias, otherwise we'll indicate the new command */
1143 if (alias
&& !(cmd
->flags
& CMD_DEPRECATED
))
1145 if (alias
->replacement
)
1146 printf_filtered ("Use '%s'.\n\n", alias
->replacement
);
1148 printf_filtered ("No alternative known.\n\n");
1152 if (cmd
->replacement
)
1153 printf_filtered ("Use '%s'.\n\n", cmd
->replacement
);
1155 printf_filtered ("No alternative known.\n\n");
1158 /* We've warned you, now we'll keep quiet */
1160 alias
->flags
&= ~DEPRECATED_WARN_USER
;
1162 cmd
->flags
&= ~DEPRECATED_WARN_USER
;
1167 /* Look up the contents of LINE as a command in the command list 'cmdlist'.
1168 Return 1 on success, 0 on failure.
1170 If LINE refers to an alias, *alias will point to that alias.
1172 If LINE is a postfix command (i.e. one that is preceeded by a prefix
1173 command) set *prefix_cmd.
1175 Set *cmd to point to the command LINE indicates.
1177 If any of *alias, *prefix_cmd, or *cmd cannot be determined or do not
1178 exist, they are NULL when we return.
1182 lookup_cmd_composition (char *text
,
1183 struct cmd_list_element
**alias
,
1184 struct cmd_list_element
**prefix_cmd
,
1185 struct cmd_list_element
**cmd
)
1188 int len
, tmp
, nfound
;
1189 struct cmd_list_element
*cur_list
;
1190 struct cmd_list_element
*prev_cmd
;
1199 /* Go through as many command lists as we need to
1200 to find the command TEXT refers to. */
1204 while (*text
== ' ' || *text
== '\t')
1207 /* Treating underscores as part of command words is important
1208 so that "set args_foo()" doesn't get interpreted as
1209 "set args _foo()". */
1211 *p
&& (isalnum (*p
) || *p
== '-' || *p
== '_' ||
1213 (*p
== '+' || *p
== '<' || *p
== '>' || *p
== '$')) ||
1214 (xdb_commands
&& (*p
== '!' || *p
== '/' || *p
== '?')));
1218 /* If nothing but whitespace, return. */
1224 /* text and p now bracket the first command word to lookup (and
1225 it's length is len). We copy this into a local temporary */
1227 command
= (char *) alloca (len
+ 1);
1228 for (tmp
= 0; tmp
< len
; tmp
++)
1233 command
[len
] = '\0';
1238 *cmd
= find_cmd (command
, len
, cur_list
, 1, &nfound
);
1240 /* We didn't find the command in the entered case, so lower case it
1243 if (!*cmd
|| nfound
== 0)
1245 for (tmp
= 0; tmp
< len
; tmp
++)
1247 char x
= command
[tmp
];
1248 command
[tmp
] = isupper (x
) ? tolower (x
) : x
;
1250 *cmd
= find_cmd (command
, len
, cur_list
, 1, &nfound
);
1253 if (*cmd
== (struct cmd_list_element
*) -1)
1255 return 0; /* ambiguous */
1259 return 0; /* nothing found */
1262 if ((*cmd
)->cmd_pointer
)
1264 /* cmd was actually an alias, we note that an alias was used
1265 (by assigning *alais) and we set *cmd.
1268 *cmd
= (*cmd
)->cmd_pointer
;
1270 *prefix_cmd
= prev_cmd
;
1272 if ((*cmd
)->prefixlist
)
1273 cur_list
= *(*cmd
)->prefixlist
;
1281 /* Helper function for SYMBOL_COMPLETION_FUNCTION. */
1283 /* Return a vector of char pointers which point to the different
1284 possible completions in LIST of TEXT.
1286 WORD points in the same buffer as TEXT, and completions should be
1287 returned relative to this position. For example, suppose TEXT is "foo"
1288 and we want to complete to "foobar". If WORD is "oo", return
1289 "oobar"; if WORD is "baz/foo", return "baz/foobar". */
1292 complete_on_cmdlist (struct cmd_list_element
*list
, char *text
, char *word
)
1294 struct cmd_list_element
*ptr
;
1296 int sizeof_matchlist
;
1298 int textlen
= strlen (text
);
1300 sizeof_matchlist
= 10;
1301 matchlist
= (char **) xmalloc (sizeof_matchlist
* sizeof (char *));
1304 for (ptr
= list
; ptr
; ptr
= ptr
->next
)
1305 if (!strncmp (ptr
->name
, text
, textlen
)
1306 && !ptr
->abbrev_flag
1308 || ptr
->prefixlist
))
1310 if (matches
== sizeof_matchlist
)
1312 sizeof_matchlist
*= 2;
1313 matchlist
= (char **) xrealloc ((char *) matchlist
,
1315 * sizeof (char *)));
1318 matchlist
[matches
] = (char *)
1319 xmalloc (strlen (word
) + strlen (ptr
->name
) + 1);
1321 strcpy (matchlist
[matches
], ptr
->name
);
1322 else if (word
> text
)
1324 /* Return some portion of ptr->name. */
1325 strcpy (matchlist
[matches
], ptr
->name
+ (word
- text
));
1329 /* Return some of text plus ptr->name. */
1330 strncpy (matchlist
[matches
], word
, text
- word
);
1331 matchlist
[matches
][text
- word
] = '\0';
1332 strcat (matchlist
[matches
], ptr
->name
);
1344 matchlist
= (char **) xrealloc ((char *) matchlist
, ((matches
+ 1)
1345 * sizeof (char *)));
1346 matchlist
[matches
] = (char *) 0;
1352 /* Helper function for SYMBOL_COMPLETION_FUNCTION. */
1354 /* Return a vector of char pointers which point to the different
1355 possible completions in CMD of TEXT.
1357 WORD points in the same buffer as TEXT, and completions should be
1358 returned relative to this position. For example, suppose TEXT is "foo"
1359 and we want to complete to "foobar". If WORD is "oo", return
1360 "oobar"; if WORD is "baz/foo", return "baz/foobar". */
1363 complete_on_enum (const char *enumlist
[],
1368 int sizeof_matchlist
;
1370 int textlen
= strlen (text
);
1374 sizeof_matchlist
= 10;
1375 matchlist
= (char **) xmalloc (sizeof_matchlist
* sizeof (char *));
1378 for (i
= 0; (name
= enumlist
[i
]) != NULL
; i
++)
1379 if (strncmp (name
, text
, textlen
) == 0)
1381 if (matches
== sizeof_matchlist
)
1383 sizeof_matchlist
*= 2;
1384 matchlist
= (char **) xrealloc ((char *) matchlist
,
1386 * sizeof (char *)));
1389 matchlist
[matches
] = (char *)
1390 xmalloc (strlen (word
) + strlen (name
) + 1);
1392 strcpy (matchlist
[matches
], name
);
1393 else if (word
> text
)
1395 /* Return some portion of name. */
1396 strcpy (matchlist
[matches
], name
+ (word
- text
));
1400 /* Return some of text plus name. */
1401 strncpy (matchlist
[matches
], word
, text
- word
);
1402 matchlist
[matches
][text
- word
] = '\0';
1403 strcat (matchlist
[matches
], name
);
1415 matchlist
= (char **) xrealloc ((char *) matchlist
, ((matches
+ 1)
1416 * sizeof (char *)));
1417 matchlist
[matches
] = (char *) 0;