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 set_cmd_completer (struct cmd_list_element
*cmd
,
91 char **(*completer
) (char *text
, char *word
))
93 cmd
->completer
= completer
; /* Ok. */
97 /* Add element named NAME.
98 CLASS is the top level category into which commands are broken down
100 FUN should be the function to execute the command;
101 it will get a character string as argument, with leading
102 and trailing blanks already eliminated.
104 DOC is a documentation string for the command.
105 Its first line should be a complete sentence.
106 It should start with ? for a command that is an abbreviation
107 or with * for a command that most users don't need to know about.
109 Add this command to command list *LIST.
111 Returns a pointer to the added command (not necessarily the head
114 struct cmd_list_element
*
115 add_cmd (char *name
, enum command_class
class, void (*fun
) (char *, int),
116 char *doc
, struct cmd_list_element
**list
)
118 register struct cmd_list_element
*c
119 = (struct cmd_list_element
*) xmalloc (sizeof (struct cmd_list_element
));
120 struct cmd_list_element
*p
;
122 delete_cmd (name
, list
);
124 if (*list
== NULL
|| strcmp ((*list
)->name
, name
) >= 0)
132 while (p
->next
&& strcmp (p
->next
->name
, name
) <= 0)
142 set_cmd_cfunc (c
, fun
);
145 c
->replacement
= NULL
;
146 c
->pre_show_hook
= NULL
;
150 c
->prefixlist
= NULL
;
151 c
->prefixname
= NULL
;
152 c
->allow_unknown
= 0;
154 set_cmd_completer (c
, make_symbol_completion_list
);
155 c
->type
= not_set_cmd
;
157 c
->var_type
= var_boolean
;
159 c
->user_commands
= NULL
;
160 c
->hookee_pre
= NULL
;
161 c
->hookee_post
= NULL
;
162 c
->cmd_pointer
= NULL
;
167 /* Same as above, except that the abbrev_flag is set. */
168 /* Note: Doesn't seem to be used anywhere currently. */
170 struct cmd_list_element
*
171 add_abbrev_cmd (char *name
, enum command_class
class, void (*fun
) (char *, int),
172 char *doc
, struct cmd_list_element
**list
)
174 register struct cmd_list_element
*c
175 = add_cmd (name
, class, fun
, doc
, list
);
181 /* Deprecates a command CMD.
182 REPLACEMENT is the name of the command which should be used in place
183 of this command, or NULL if no such command exists.
185 This function does not check to see if command REPLACEMENT exists
186 since gdb may not have gotten around to adding REPLACEMENT when this
189 Returns a pointer to the deprecated command. */
191 struct cmd_list_element
*
192 deprecate_cmd (struct cmd_list_element
*cmd
, char *replacement
)
194 cmd
->flags
|= (CMD_DEPRECATED
| DEPRECATED_WARN_USER
);
196 if (replacement
!= NULL
)
197 cmd
->replacement
= replacement
;
199 cmd
->replacement
= NULL
;
204 struct cmd_list_element
*
205 add_alias_cmd (char *name
, char *oldname
, enum command_class
class,
206 int abbrev_flag
, struct cmd_list_element
**list
)
208 /* Must do this since lookup_cmd tries to side-effect its first arg */
210 register struct cmd_list_element
*old
;
211 register struct cmd_list_element
*c
;
212 copied_name
= (char *) alloca (strlen (oldname
) + 1);
213 strcpy (copied_name
, oldname
);
214 old
= lookup_cmd (&copied_name
, *list
, "", 1, 1);
218 delete_cmd (name
, list
);
222 c
= add_cmd (name
, class, NULL
, old
->doc
, list
);
223 /* NOTE: Both FUNC and all the FUNCTIONs need to be copied. */
225 c
->function
= old
->function
;
226 c
->prefixlist
= old
->prefixlist
;
227 c
->prefixname
= old
->prefixname
;
228 c
->allow_unknown
= old
->allow_unknown
;
229 c
->abbrev_flag
= abbrev_flag
;
230 c
->cmd_pointer
= old
;
234 /* Like add_cmd but adds an element for a command prefix:
235 a name that should be followed by a subcommand to be looked up
236 in another command list. PREFIXLIST should be the address
237 of the variable containing that list. */
239 struct cmd_list_element
*
240 add_prefix_cmd (char *name
, enum command_class
class, void (*fun
) (char *, int),
241 char *doc
, struct cmd_list_element
**prefixlist
,
242 char *prefixname
, int allow_unknown
,
243 struct cmd_list_element
**list
)
245 register struct cmd_list_element
*c
= add_cmd (name
, class, fun
, doc
, list
);
246 c
->prefixlist
= prefixlist
;
247 c
->prefixname
= prefixname
;
248 c
->allow_unknown
= allow_unknown
;
252 /* Like add_prefix_cmd but sets the abbrev_flag on the new command. */
254 struct cmd_list_element
*
255 add_abbrev_prefix_cmd (char *name
, enum command_class
class,
256 void (*fun
) (char *, int), char *doc
,
257 struct cmd_list_element
**prefixlist
, char *prefixname
,
258 int allow_unknown
, struct cmd_list_element
**list
)
260 register struct cmd_list_element
*c
= add_cmd (name
, class, fun
, doc
, list
);
261 c
->prefixlist
= prefixlist
;
262 c
->prefixname
= prefixname
;
263 c
->allow_unknown
= allow_unknown
;
268 /* This is an empty "cfunc". */
270 not_just_help_class_command (char *args
, int from_tty
)
274 /* This is an empty "sfunc". */
275 static void empty_sfunc (char *, int, struct cmd_list_element
*);
278 empty_sfunc (char *args
, int from_tty
, struct cmd_list_element
*c
)
282 /* Add element named NAME to command list LIST (the list for set
283 or some sublist thereof).
284 CLASS is as in add_cmd.
285 VAR_TYPE is the kind of thing we are setting.
286 VAR is address of the variable being controlled by this command.
287 DOC is the documentation string. */
289 struct cmd_list_element
*
290 add_set_cmd (char *name
,
291 enum command_class
class,
295 struct cmd_list_element
**list
)
297 struct cmd_list_element
*c
= add_cmd (name
, class, NULL
, doc
, list
);
300 c
->var_type
= var_type
;
302 /* This needs to be something besides NULL so that this isn't
303 treated as a help class. */
304 set_cmd_sfunc (c
, empty_sfunc
);
308 /* Add element named NAME to command list LIST (the list for set
309 or some sublist thereof).
310 CLASS is as in add_cmd.
311 ENUMLIST is a list of strings which may follow NAME.
312 VAR is address of the variable which will contain the matching string
314 DOC is the documentation string. */
316 struct cmd_list_element
*
317 add_set_enum_cmd (char *name
,
318 enum command_class
class,
319 const char *enumlist
[],
322 struct cmd_list_element
**list
)
324 struct cmd_list_element
*c
325 = add_set_cmd (name
, class, var_enum
, var
, doc
, list
);
331 /* Add element named NAME to command list LIST (the list for set
332 or some sublist thereof).
333 CLASS is as in add_cmd.
334 VAR is address of the variable which will contain the value.
335 DOC is the documentation string. */
336 struct cmd_list_element
*
337 add_set_auto_boolean_cmd (char *name
,
338 enum command_class
class,
339 enum cmd_auto_boolean
*var
,
341 struct cmd_list_element
**list
)
343 static const char *auto_boolean_enums
[] = { "on", "off", "auto", NULL
};
344 struct cmd_list_element
*c
;
345 c
= add_set_cmd (name
, class, var_auto_boolean
, var
, doc
, list
);
346 c
->enums
= auto_boolean_enums
;
350 /* Add element named NAME to command list LIST (the list for set
351 or some sublist thereof).
352 CLASS is as in add_cmd.
353 VAR is address of the variable which will contain the value.
354 DOC is the documentation string. */
355 struct cmd_list_element
*
356 add_set_boolean_cmd (char *name
,
357 enum command_class
class,
360 struct cmd_list_element
**list
)
362 static const char *boolean_enums
[] = { "on", "off", NULL
};
363 struct cmd_list_element
*c
;
364 c
= add_set_cmd (name
, class, var_boolean
, var
, doc
, list
);
365 c
->enums
= boolean_enums
;
369 /* Where SETCMD has already been added, add the corresponding show
370 command to LIST and return a pointer to the added command (not
371 necessarily the head of LIST). */
372 struct cmd_list_element
*
373 add_show_from_set (struct cmd_list_element
*setcmd
,
374 struct cmd_list_element
**list
)
376 struct cmd_list_element
*showcmd
=
377 (struct cmd_list_element
*) xmalloc (sizeof (struct cmd_list_element
));
378 struct cmd_list_element
*p
;
380 memcpy (showcmd
, setcmd
, sizeof (struct cmd_list_element
));
381 delete_cmd (showcmd
->name
, list
);
382 showcmd
->type
= show_cmd
;
384 /* Replace "set " at start of docstring with "show ". */
385 if (setcmd
->doc
[0] == 'S' && setcmd
->doc
[1] == 'e'
386 && setcmd
->doc
[2] == 't' && setcmd
->doc
[3] == ' ')
387 showcmd
->doc
= concat ("Show ", setcmd
->doc
+ 4, NULL
);
389 fprintf_unfiltered (gdb_stderr
, "GDB internal error: Bad docstring for set command\n");
391 if (*list
== NULL
|| strcmp ((*list
)->name
, showcmd
->name
) >= 0)
393 showcmd
->next
= *list
;
399 while (p
->next
&& strcmp (p
->next
->name
, showcmd
->name
) <= 0)
403 showcmd
->next
= p
->next
;
410 /* Remove the command named NAME from the command list. */
413 delete_cmd (char *name
, struct cmd_list_element
**list
)
415 register struct cmd_list_element
*c
;
416 struct cmd_list_element
*p
;
418 while (*list
&& STREQ ((*list
)->name
, name
))
420 if ((*list
)->hookee_pre
)
421 (*list
)->hookee_pre
->hook_pre
= 0; /* Hook slips out of its mouth */
422 if ((*list
)->hookee_post
)
423 (*list
)->hookee_post
->hook_post
= 0; /* Hook slips out of its bottom */
430 for (c
= *list
; c
->next
;)
432 if (STREQ (c
->next
->name
, name
))
434 if (c
->next
->hookee_pre
)
435 c
->next
->hookee_pre
->hook_pre
= 0; /* hooked cmd gets away. */
436 if (c
->next
->hookee_post
)
437 c
->next
->hookee_post
->hook_post
= 0; /* remove post hook */
438 /* :( no fishing metaphore */
448 /* Shorthands to the commands above. */
450 /* Add an element to the list of info subcommands. */
452 struct cmd_list_element
*
453 add_info (char *name
, void (*fun
) (char *, int), char *doc
)
455 return add_cmd (name
, no_class
, fun
, doc
, &infolist
);
458 /* Add an alias to the list of info subcommands. */
460 struct cmd_list_element
*
461 add_info_alias (char *name
, char *oldname
, int abbrev_flag
)
463 return add_alias_cmd (name
, oldname
, 0, abbrev_flag
, &infolist
);
466 /* Add an element to the list of commands. */
468 struct cmd_list_element
*
469 add_com (char *name
, enum command_class
class, void (*fun
) (char *, int),
472 return add_cmd (name
, class, fun
, doc
, &cmdlist
);
475 /* Add an alias or abbreviation command to the list of commands. */
477 struct cmd_list_element
*
478 add_com_alias (char *name
, char *oldname
, enum command_class
class,
481 return add_alias_cmd (name
, oldname
, class, abbrev_flag
, &cmdlist
);
484 /* Recursively walk the commandlist structures, and print out the
485 documentation of commands that match our regex in either their
486 name, or their documentation.
489 apropos_cmd (struct ui_file
*stream
, struct cmd_list_element
*commandlist
,
490 struct re_pattern_buffer
*regex
, char *prefix
)
492 register struct cmd_list_element
*c
;
493 int returnvalue
=1; /*Needed to avoid double printing*/
494 /* Walk through the commands */
495 for (c
=commandlist
;c
;c
=c
->next
)
499 /* Try to match against the name*/
500 returnvalue
=re_search(regex
,c
->name
,strlen(c
->name
),0,strlen(c
->name
),NULL
);
501 if (returnvalue
>= 0)
503 /* Stolen from help_cmd_list. We don't directly use
504 * help_cmd_list because it doesn't let us print out
507 fprintf_filtered (stream
, "%s%s -- ", prefix
, c
->name
);
508 print_doc_line (stream
, c
->doc
);
509 fputs_filtered ("\n", stream
);
510 returnvalue
=0; /*Set this so we don't print it again.*/
513 if (c
->doc
!= NULL
&& returnvalue
!= 0)
515 /* Try to match against documentation */
516 if (re_search(regex
,c
->doc
,strlen(c
->doc
),0,strlen(c
->doc
),NULL
) >=0)
518 /* Stolen from help_cmd_list. We don't directly use
519 * help_cmd_list because it doesn't let us print out
522 fprintf_filtered (stream
, "%s%s -- ", prefix
, c
->name
);
523 print_doc_line (stream
, c
->doc
);
524 fputs_filtered ("\n", stream
);
527 /* Check if this command has subcommands */
528 if (c
->prefixlist
!= NULL
)
530 /* Recursively call ourselves on the subcommand list,
531 passing the right prefix in.
533 apropos_cmd (stream
,*c
->prefixlist
,regex
,c
->prefixname
);
538 /* This command really has to deal with two things:
539 * 1) I want documentation on *this string* (usually called by
540 * "help commandname").
541 * 2) I want documentation on *this list* (usually called by
542 * giving a command that requires subcommands. Also called by saying
545 * I am going to split this into two seperate comamnds, help_cmd and
550 help_cmd (char *command
, struct ui_file
*stream
)
552 struct cmd_list_element
*c
;
553 extern struct cmd_list_element
*cmdlist
;
557 help_list (cmdlist
, "", all_classes
, stream
);
561 if (strcmp (command
, "all") == 0)
567 c
= lookup_cmd (&command
, cmdlist
, "", 0, 0);
572 /* There are three cases here.
573 If c->prefixlist is nonzero, we have a prefix command.
574 Print its documentation, then list its subcommands.
576 If c->func is non NULL, we really have a command. Print its
577 documentation and return.
579 If c->func is NULL, we have a class name. Print its
580 documentation (as if it were a command) and then set class to the
581 number of this class so that the commands in the class will be
584 fputs_filtered (c
->doc
, stream
);
585 fputs_filtered ("\n", stream
);
587 if (c
->prefixlist
== 0 && c
->func
!= NULL
)
589 fprintf_filtered (stream
, "\n");
591 /* If this is a prefix command, print it's subcommands */
593 help_list (*c
->prefixlist
, c
->prefixname
, all_commands
, stream
);
595 /* If this is a class name, print all of the commands in the class */
597 help_list (cmdlist
, "", c
->class, stream
);
599 if (c
->hook_pre
|| c
->hook_post
)
600 fprintf_filtered (stream
,
601 "\nThis command has a hook (or hooks) defined:\n");
604 fprintf_filtered (stream
,
605 "\tThis command is run after : %s (pre hook)\n",
608 fprintf_filtered (stream
,
609 "\tThis command is run before : %s (post hook)\n",
614 * Get a specific kind of help on a command list.
617 * CMDTYPE is the prefix to use in the title string.
618 * CLASS is the class with which to list the nodes of this list (see
619 * documentation for help_cmd_list below), As usual, ALL_COMMANDS for
620 * everything, ALL_CLASSES for just classes, and non-negative for only things
621 * in a specific class.
622 * and STREAM is the output stream on which to print things.
623 * If you call this routine with a class >= 0, it recurses.
626 help_list (struct cmd_list_element
*list
, char *cmdtype
,
627 enum command_class
class, struct ui_file
*stream
)
630 char *cmdtype1
, *cmdtype2
;
632 /* If CMDTYPE is "foo ", CMDTYPE1 gets " foo" and CMDTYPE2 gets "foo sub" */
633 len
= strlen (cmdtype
);
634 cmdtype1
= (char *) alloca (len
+ 1);
636 cmdtype2
= (char *) alloca (len
+ 4);
641 strncpy (cmdtype1
+ 1, cmdtype
, len
- 1);
643 strncpy (cmdtype2
, cmdtype
, len
- 1);
644 strcpy (cmdtype2
+ len
- 1, " sub");
647 if (class == all_classes
)
648 fprintf_filtered (stream
, "List of classes of %scommands:\n\n", cmdtype2
);
650 fprintf_filtered (stream
, "List of %scommands:\n\n", cmdtype2
);
652 help_cmd_list (list
, class, cmdtype
, (int) class >= 0, stream
);
654 if (class == all_classes
)
655 fprintf_filtered (stream
, "\n\
656 Type \"help%s\" followed by a class name for a list of commands in that class.",
659 fprintf_filtered (stream
, "\n\
660 Type \"help%s\" followed by %scommand name for full documentation.\n\
661 Command name abbreviations are allowed if unambiguous.\n",
666 help_all (struct ui_file
*stream
)
668 struct cmd_list_element
*c
;
669 extern struct cmd_list_element
*cmdlist
;
671 for (c
= cmdlist
; c
; c
= c
->next
)
675 /* If this is a prefix command, print it's subcommands */
677 help_cmd_list (*c
->prefixlist
, all_commands
, c
->prefixname
, 0, stream
);
679 /* If this is a class name, print all of the commands in the class */
680 else if (c
->func
== NULL
)
681 help_cmd_list (cmdlist
, c
->class, "", 0, stream
);
685 /* Print only the first line of STR on STREAM. */
687 print_doc_line (struct ui_file
*stream
, char *str
)
689 static char *line_buffer
= 0;
690 static int line_size
;
696 line_buffer
= (char *) xmalloc (line_size
);
700 while (*p
&& *p
!= '\n' && *p
!= '.' && *p
!= ',')
702 if (p
- str
> line_size
- 1)
704 line_size
= p
- str
+ 1;
706 line_buffer
= (char *) xmalloc (line_size
);
708 strncpy (line_buffer
, str
, p
- str
);
709 line_buffer
[p
- str
] = '\0';
710 if (islower (line_buffer
[0]))
711 line_buffer
[0] = toupper (line_buffer
[0]);
712 ui_out_text (uiout
, line_buffer
);
716 * Implement a help command on command list LIST.
717 * RECURSE should be non-zero if this should be done recursively on
718 * all sublists of LIST.
719 * PREFIX is the prefix to print before each command name.
720 * STREAM is the stream upon which the output should be written.
722 * A non-negative class number to list only commands in that
724 * ALL_COMMANDS to list all commands in list.
725 * ALL_CLASSES to list all classes in list.
727 * Note that RECURSE will be active on *all* sublists, not just the
728 * ones selected by the criteria above (ie. the selection mechanism
729 * is at the low level, not the high-level).
732 help_cmd_list (struct cmd_list_element
*list
, enum command_class
class,
733 char *prefix
, int recurse
, struct ui_file
*stream
)
735 register struct cmd_list_element
*c
;
737 for (c
= list
; c
; c
= c
->next
)
739 if (c
->abbrev_flag
== 0 &&
740 (class == all_commands
741 || (class == all_classes
&& c
->func
== NULL
)
742 || (class == c
->class && c
->func
!= NULL
)))
744 fprintf_filtered (stream
, "%s%s -- ", prefix
, c
->name
);
745 print_doc_line (stream
, c
->doc
);
746 fputs_filtered ("\n", stream
);
749 && c
->prefixlist
!= 0
750 && c
->abbrev_flag
== 0)
751 help_cmd_list (*c
->prefixlist
, class, c
->prefixname
, 1, stream
);
756 /* Search the input clist for 'command'. Return the command if
757 found (or NULL if not), and return the number of commands
760 static struct cmd_list_element
*
761 find_cmd (char *command
, int len
, struct cmd_list_element
*clist
,
762 int ignore_help_classes
, int *nfound
)
764 struct cmd_list_element
*found
, *c
;
766 found
= (struct cmd_list_element
*) NULL
;
768 for (c
= clist
; c
; c
= c
->next
)
769 if (!strncmp (command
, c
->name
, len
)
770 && (!ignore_help_classes
|| c
->func
))
774 if (c
->name
[len
] == '\0')
783 /* This routine takes a line of TEXT and a CLIST in which to start the
784 lookup. When it returns it will have incremented the text pointer past
785 the section of text it matched, set *RESULT_LIST to point to the list in
786 which the last word was matched, and will return a pointer to the cmd
787 list element which the text matches. It will return NULL if no match at
788 all was possible. It will return -1 (cast appropriately, ick) if ambigous
789 matches are possible; in this case *RESULT_LIST will be set to point to
790 the list in which there are ambiguous choices (and *TEXT will be set to
791 the ambiguous text string).
793 If the located command was an abbreviation, this routine returns the base
794 command of the abbreviation.
796 It does no error reporting whatsoever; control will always return
797 to the superior routine.
799 In the case of an ambiguous return (-1), *RESULT_LIST will be set to point
800 at the prefix_command (ie. the best match) *or* (special case) will be NULL
801 if no prefix command was ever found. For example, in the case of "info a",
802 "info" matches without ambiguity, but "a" could be "args" or "address", so
803 *RESULT_LIST is set to the cmd_list_element for "info". So in this case
804 RESULT_LIST should not be interpeted as a pointer to the beginning of a
805 list; it simply points to a specific command. In the case of an ambiguous
806 return *TEXT is advanced past the last non-ambiguous prefix (e.g.
807 "info t" can be "info types" or "info target"; upon return *TEXT has been
808 advanced past "info ").
810 If RESULT_LIST is NULL, don't set *RESULT_LIST (but don't otherwise
811 affect the operation).
813 This routine does *not* modify the text pointed to by TEXT.
815 If IGNORE_HELP_CLASSES is nonzero, ignore any command list elements which
816 are actually help classes rather than commands (i.e. the function field of
817 the struct cmd_list_element is NULL). */
819 struct cmd_list_element
*
820 lookup_cmd_1 (char **text
, struct cmd_list_element
*clist
,
821 struct cmd_list_element
**result_list
, int ignore_help_classes
)
824 int len
, tmp
, nfound
;
825 struct cmd_list_element
*found
, *c
;
828 while (**text
== ' ' || **text
== '\t')
831 /* Treating underscores as part of command words is important
832 so that "set args_foo()" doesn't get interpreted as
833 "set args _foo()". */
835 *p
&& (isalnum (*p
) || *p
== '-' || *p
== '_' ||
837 (*p
== '+' || *p
== '<' || *p
== '>' || *p
== '$')) ||
838 (xdb_commands
&& (*p
== '!' || *p
== '/' || *p
== '?')));
842 /* If nothing but whitespace, return 0. */
848 /* *text and p now bracket the first command word to lookup (and
849 it's length is len). We copy this into a local temporary */
852 command
= (char *) alloca (len
+ 1);
853 for (tmp
= 0; tmp
< len
; tmp
++)
855 char x
= (*text
)[tmp
];
863 found
= find_cmd (command
, len
, clist
, ignore_help_classes
, &nfound
);
866 ** We didn't find the command in the entered case, so lower case it
869 if (!found
|| nfound
== 0)
871 for (tmp
= 0; tmp
< len
; tmp
++)
873 char x
= command
[tmp
];
874 command
[tmp
] = isupper (x
) ? tolower (x
) : x
;
876 found
= find_cmd (command
, len
, clist
, ignore_help_classes
, &nfound
);
879 /* If nothing matches, we have a simple failure. */
885 if (result_list
!= NULL
)
886 /* Will be modified in calling routine
887 if we know what the prefix command is. */
889 return (struct cmd_list_element
*) -1; /* Ambiguous. */
892 /* We've matched something on this list. Move text pointer forward. */
896 if (found
->cmd_pointer
)
898 /* We drop the alias (abbreviation) in favor of the command it is
899 pointing to. If the alias is deprecated, though, we need to
900 warn the user about it before we drop it. Note that while we
901 are warning about the alias, we may also warn about the command
902 itself and we will adjust the appropriate DEPRECATED_WARN_USER
905 if (found
->flags
& DEPRECATED_WARN_USER
)
906 deprecated_cmd_warning (&line
);
907 found
= found
->cmd_pointer
;
909 /* If we found a prefix command, keep looking. */
911 if (found
->prefixlist
)
913 c
= lookup_cmd_1 (text
, *found
->prefixlist
, result_list
,
914 ignore_help_classes
);
917 /* Didn't find anything; this is as far as we got. */
918 if (result_list
!= NULL
)
919 *result_list
= clist
;
922 else if (c
== (struct cmd_list_element
*) -1)
924 /* We've gotten this far properly, but the next step
925 is ambiguous. We need to set the result list to the best
926 we've found (if an inferior hasn't already set it). */
927 if (result_list
!= NULL
)
929 /* This used to say *result_list = *found->prefixlist
930 If that was correct, need to modify the documentation
931 at the top of this function to clarify what is supposed
933 *result_list
= found
;
944 if (result_list
!= NULL
)
945 *result_list
= clist
;
950 /* All this hair to move the space to the front of cmdtype */
953 undef_cmd_error (char *cmdtype
, char *q
)
955 error ("Undefined %scommand: \"%s\". Try \"help%s%.*s\".",
959 strlen (cmdtype
) - 1,
963 /* Look up the contents of *LINE as a command in the command list LIST.
964 LIST is a chain of struct cmd_list_element's.
965 If it is found, return the struct cmd_list_element for that command
966 and update *LINE to point after the command name, at the first argument.
967 If not found, call error if ALLOW_UNKNOWN is zero
968 otherwise (or if error returns) return zero.
969 Call error if specified command is ambiguous,
970 unless ALLOW_UNKNOWN is negative.
971 CMDTYPE precedes the word "command" in the error message.
973 If INGNORE_HELP_CLASSES is nonzero, ignore any command list
974 elements which are actually help classes rather than commands (i.e.
975 the function field of the struct cmd_list_element is 0). */
977 struct cmd_list_element
*
978 lookup_cmd (char **line
, struct cmd_list_element
*list
, char *cmdtype
,
979 int allow_unknown
, int ignore_help_classes
)
981 struct cmd_list_element
*last_list
= 0;
982 struct cmd_list_element
*c
=
983 lookup_cmd_1 (line
, list
, &last_list
, ignore_help_classes
);
985 /* Note: Do not remove trailing whitespace here because this
986 would be wrong for complete_command. Jim Kingdon */
993 error ("Lack of needed %scommand", cmdtype
);
998 while (isalnum (*p
) || *p
== '-')
1001 q
= (char *) alloca (p
- *line
+ 1);
1002 strncpy (q
, *line
, p
- *line
);
1003 q
[p
- *line
] = '\0';
1004 undef_cmd_error (cmdtype
, q
);
1010 else if (c
== (struct cmd_list_element
*) -1)
1012 /* Ambigous. Local values should be off prefixlist or called
1014 int local_allow_unknown
= (last_list
? last_list
->allow_unknown
:
1016 char *local_cmdtype
= last_list
? last_list
->prefixname
: cmdtype
;
1017 struct cmd_list_element
*local_list
=
1018 (last_list
? *(last_list
->prefixlist
) : list
);
1020 if (local_allow_unknown
< 0)
1023 return last_list
; /* Found something. */
1025 return 0; /* Found nothing. */
1029 /* Report as error. */
1034 ((*line
)[amb_len
] && (*line
)[amb_len
] != ' '
1035 && (*line
)[amb_len
] != '\t');
1040 for (c
= local_list
; c
; c
= c
->next
)
1041 if (!strncmp (*line
, c
->name
, amb_len
))
1043 if (strlen (ambbuf
) + strlen (c
->name
) + 6 < (int) sizeof ambbuf
)
1045 if (strlen (ambbuf
))
1046 strcat (ambbuf
, ", ");
1047 strcat (ambbuf
, c
->name
);
1051 strcat (ambbuf
, "..");
1055 error ("Ambiguous %scommand \"%s\": %s.", local_cmdtype
,
1057 return 0; /* lint */
1062 /* We've got something. It may still not be what the caller
1063 wants (if this command *needs* a subcommand). */
1064 while (**line
== ' ' || **line
== '\t')
1067 if (c
->prefixlist
&& **line
&& !c
->allow_unknown
)
1068 undef_cmd_error (c
->prefixname
, *line
);
1070 /* Seems to be what he wants. Return it. */
1076 /* We are here presumably because an alias or command in *TEXT is
1077 deprecated and a warning message should be generated. This function
1078 decodes *TEXT and potentially generates a warning message as outlined
1081 Example for 'set endian big' which has a fictitious alias 'seb'.
1083 If alias wasn't used in *TEXT, and the command is deprecated:
1084 "warning: 'set endian big' is deprecated."
1086 If alias was used, and only the alias is deprecated:
1087 "warning: 'seb' an alias for the command 'set endian big' is deprecated."
1089 If alias was used and command is deprecated (regardless of whether the
1090 alias itself is deprecated:
1092 "warning: 'set endian big' (seb) is deprecated."
1094 After the message has been sent, clear the appropriate flags in the
1095 command and/or the alias so the user is no longer bothered.
1099 deprecated_cmd_warning (char **text
)
1101 struct cmd_list_element
*alias
= NULL
;
1102 struct cmd_list_element
*prefix_cmd
= NULL
;
1103 struct cmd_list_element
*cmd
= NULL
;
1104 struct cmd_list_element
*c
;
1107 if (!lookup_cmd_composition (*text
, &alias
, &prefix_cmd
, &cmd
))
1108 /* return if text doesn't evaluate to a command */
1111 if (!((alias
? (alias
->flags
& DEPRECATED_WARN_USER
) : 0)
1112 || (cmd
->flags
& DEPRECATED_WARN_USER
) ) )
1113 /* return if nothing is deprecated */
1116 printf_filtered ("Warning:");
1118 if (alias
&& !(cmd
->flags
& CMD_DEPRECATED
))
1119 printf_filtered (" '%s', an alias for the", alias
->name
);
1121 printf_filtered (" command '");
1124 printf_filtered ("%s", prefix_cmd
->prefixname
);
1126 printf_filtered ("%s", cmd
->name
);
1128 if (alias
&& (cmd
->flags
& CMD_DEPRECATED
))
1129 printf_filtered ("' (%s) is deprecated.\n", alias
->name
);
1131 printf_filtered ("' is deprecated.\n");
1134 /* if it is only the alias that is deprecated, we want to indicate the
1135 new alias, otherwise we'll indicate the new command */
1137 if (alias
&& !(cmd
->flags
& CMD_DEPRECATED
))
1139 if (alias
->replacement
)
1140 printf_filtered ("Use '%s'.\n\n", alias
->replacement
);
1142 printf_filtered ("No alternative known.\n\n");
1146 if (cmd
->replacement
)
1147 printf_filtered ("Use '%s'.\n\n", cmd
->replacement
);
1149 printf_filtered ("No alternative known.\n\n");
1152 /* We've warned you, now we'll keep quiet */
1154 alias
->flags
&= ~DEPRECATED_WARN_USER
;
1156 cmd
->flags
&= ~DEPRECATED_WARN_USER
;
1161 /* Look up the contents of LINE as a command in the command list 'cmdlist'.
1162 Return 1 on success, 0 on failure.
1164 If LINE refers to an alias, *alias will point to that alias.
1166 If LINE is a postfix command (i.e. one that is preceeded by a prefix
1167 command) set *prefix_cmd.
1169 Set *cmd to point to the command LINE indicates.
1171 If any of *alias, *prefix_cmd, or *cmd cannot be determined or do not
1172 exist, they are NULL when we return.
1176 lookup_cmd_composition (char *text
,
1177 struct cmd_list_element
**alias
,
1178 struct cmd_list_element
**prefix_cmd
,
1179 struct cmd_list_element
**cmd
)
1182 int len
, tmp
, nfound
;
1183 struct cmd_list_element
*cur_list
;
1184 struct cmd_list_element
*prev_cmd
;
1193 /* Go through as many command lists as we need to
1194 to find the command TEXT refers to. */
1198 while (*text
== ' ' || *text
== '\t')
1201 /* Treating underscores as part of command words is important
1202 so that "set args_foo()" doesn't get interpreted as
1203 "set args _foo()". */
1205 *p
&& (isalnum (*p
) || *p
== '-' || *p
== '_' ||
1207 (*p
== '+' || *p
== '<' || *p
== '>' || *p
== '$')) ||
1208 (xdb_commands
&& (*p
== '!' || *p
== '/' || *p
== '?')));
1212 /* If nothing but whitespace, return. */
1218 /* text and p now bracket the first command word to lookup (and
1219 it's length is len). We copy this into a local temporary */
1221 command
= (char *) alloca (len
+ 1);
1222 for (tmp
= 0; tmp
< len
; tmp
++)
1227 command
[len
] = '\0';
1232 *cmd
= find_cmd (command
, len
, cur_list
, 1, &nfound
);
1234 /* We didn't find the command in the entered case, so lower case it
1237 if (!*cmd
|| nfound
== 0)
1239 for (tmp
= 0; tmp
< len
; tmp
++)
1241 char x
= command
[tmp
];
1242 command
[tmp
] = isupper (x
) ? tolower (x
) : x
;
1244 *cmd
= find_cmd (command
, len
, cur_list
, 1, &nfound
);
1247 if (*cmd
== (struct cmd_list_element
*) -1)
1249 return 0; /* ambiguous */
1253 return 0; /* nothing found */
1256 if ((*cmd
)->cmd_pointer
)
1258 /* cmd was actually an alias, we note that an alias was used
1259 (by assigning *alais) and we set *cmd.
1262 *cmd
= (*cmd
)->cmd_pointer
;
1264 *prefix_cmd
= prev_cmd
;
1266 if ((*cmd
)->prefixlist
)
1267 cur_list
= *(*cmd
)->prefixlist
;
1275 /* Helper function for SYMBOL_COMPLETION_FUNCTION. */
1277 /* Return a vector of char pointers which point to the different
1278 possible completions in LIST of TEXT.
1280 WORD points in the same buffer as TEXT, and completions should be
1281 returned relative to this position. For example, suppose TEXT is "foo"
1282 and we want to complete to "foobar". If WORD is "oo", return
1283 "oobar"; if WORD is "baz/foo", return "baz/foobar". */
1286 complete_on_cmdlist (struct cmd_list_element
*list
, char *text
, char *word
)
1288 struct cmd_list_element
*ptr
;
1290 int sizeof_matchlist
;
1292 int textlen
= strlen (text
);
1294 sizeof_matchlist
= 10;
1295 matchlist
= (char **) xmalloc (sizeof_matchlist
* sizeof (char *));
1298 for (ptr
= list
; ptr
; ptr
= ptr
->next
)
1299 if (!strncmp (ptr
->name
, text
, textlen
)
1300 && !ptr
->abbrev_flag
1302 || ptr
->prefixlist
))
1304 if (matches
== sizeof_matchlist
)
1306 sizeof_matchlist
*= 2;
1307 matchlist
= (char **) xrealloc ((char *) matchlist
,
1309 * sizeof (char *)));
1312 matchlist
[matches
] = (char *)
1313 xmalloc (strlen (word
) + strlen (ptr
->name
) + 1);
1315 strcpy (matchlist
[matches
], ptr
->name
);
1316 else if (word
> text
)
1318 /* Return some portion of ptr->name. */
1319 strcpy (matchlist
[matches
], ptr
->name
+ (word
- text
));
1323 /* Return some of text plus ptr->name. */
1324 strncpy (matchlist
[matches
], word
, text
- word
);
1325 matchlist
[matches
][text
- word
] = '\0';
1326 strcat (matchlist
[matches
], ptr
->name
);
1338 matchlist
= (char **) xrealloc ((char *) matchlist
, ((matches
+ 1)
1339 * sizeof (char *)));
1340 matchlist
[matches
] = (char *) 0;
1346 /* Helper function for SYMBOL_COMPLETION_FUNCTION. */
1348 /* Return a vector of char pointers which point to the different
1349 possible completions in CMD of TEXT.
1351 WORD points in the same buffer as TEXT, and completions should be
1352 returned relative to this position. For example, suppose TEXT is "foo"
1353 and we want to complete to "foobar". If WORD is "oo", return
1354 "oobar"; if WORD is "baz/foo", return "baz/foobar". */
1357 complete_on_enum (const char *enumlist
[],
1362 int sizeof_matchlist
;
1364 int textlen
= strlen (text
);
1368 sizeof_matchlist
= 10;
1369 matchlist
= (char **) xmalloc (sizeof_matchlist
* sizeof (char *));
1372 for (i
= 0; (name
= enumlist
[i
]) != NULL
; i
++)
1373 if (strncmp (name
, text
, textlen
) == 0)
1375 if (matches
== sizeof_matchlist
)
1377 sizeof_matchlist
*= 2;
1378 matchlist
= (char **) xrealloc ((char *) matchlist
,
1380 * sizeof (char *)));
1383 matchlist
[matches
] = (char *)
1384 xmalloc (strlen (word
) + strlen (name
) + 1);
1386 strcpy (matchlist
[matches
], name
);
1387 else if (word
> text
)
1389 /* Return some portion of name. */
1390 strcpy (matchlist
[matches
], name
+ (word
- text
));
1394 /* Return some of text plus name. */
1395 strncpy (matchlist
[matches
], word
, text
- word
);
1396 matchlist
[matches
][text
- word
] = '\0';
1397 strcat (matchlist
[matches
], name
);
1409 matchlist
= (char **) xrealloc ((char *) matchlist
, ((matches
+ 1)
1410 * sizeof (char *)));
1411 matchlist
[matches
] = (char *) 0;