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 /* Add element named NAME.
84 CLASS is the top level category into which commands are broken down
86 FUN should be the function to execute the command;
87 it will get a character string as argument, with leading
88 and trailing blanks already eliminated.
90 DOC is a documentation string for the command.
91 Its first line should be a complete sentence.
92 It should start with ? for a command that is an abbreviation
93 or with * for a command that most users don't need to know about.
95 Add this command to command list *LIST.
97 Returns a pointer to the added command (not necessarily the head
100 struct cmd_list_element
*
101 add_cmd (char *name
, enum command_class
class, void (*fun
) (char *, int),
102 char *doc
, struct cmd_list_element
**list
)
104 register struct cmd_list_element
*c
105 = (struct cmd_list_element
*) xmalloc (sizeof (struct cmd_list_element
));
106 struct cmd_list_element
*p
;
108 delete_cmd (name
, list
);
110 if (*list
== NULL
|| strcmp ((*list
)->name
, name
) >= 0)
118 while (p
->next
&& strcmp (p
->next
->name
, name
) <= 0)
128 set_cmd_cfunc (c
, fun
);
131 c
->replacement
= NULL
;
132 c
->pre_show_hook
= NULL
;
136 c
->prefixlist
= NULL
;
137 c
->prefixname
= NULL
;
138 c
->allow_unknown
= 0;
140 c
->completer
= make_symbol_completion_list
;
141 c
->type
= not_set_cmd
;
143 c
->var_type
= var_boolean
;
145 c
->user_commands
= NULL
;
146 c
->hookee_pre
= NULL
;
147 c
->hookee_post
= NULL
;
148 c
->cmd_pointer
= NULL
;
153 /* Same as above, except that the abbrev_flag is set. */
154 /* Note: Doesn't seem to be used anywhere currently. */
156 struct cmd_list_element
*
157 add_abbrev_cmd (char *name
, enum command_class
class, void (*fun
) (char *, int),
158 char *doc
, struct cmd_list_element
**list
)
160 register struct cmd_list_element
*c
161 = add_cmd (name
, class, fun
, doc
, list
);
167 /* Deprecates a command CMD.
168 REPLACEMENT is the name of the command which should be used in place
169 of this command, or NULL if no such command exists.
171 This function does not check to see if command REPLACEMENT exists
172 since gdb may not have gotten around to adding REPLACEMENT when this
175 Returns a pointer to the deprecated command. */
177 struct cmd_list_element
*
178 deprecate_cmd (struct cmd_list_element
*cmd
, char *replacement
)
180 cmd
->flags
|= (CMD_DEPRECATED
| DEPRECATED_WARN_USER
);
182 if (replacement
!= NULL
)
183 cmd
->replacement
= replacement
;
185 cmd
->replacement
= NULL
;
190 struct cmd_list_element
*
191 add_alias_cmd (char *name
, char *oldname
, enum command_class
class,
192 int abbrev_flag
, struct cmd_list_element
**list
)
194 /* Must do this since lookup_cmd tries to side-effect its first arg */
196 register struct cmd_list_element
*old
;
197 register struct cmd_list_element
*c
;
198 copied_name
= (char *) alloca (strlen (oldname
) + 1);
199 strcpy (copied_name
, oldname
);
200 old
= lookup_cmd (&copied_name
, *list
, "", 1, 1);
204 delete_cmd (name
, list
);
208 c
= add_cmd (name
, class, NULL
, old
->doc
, list
);
209 /* NOTE: Both FUNC and all the FUNCTIONs need to be copied. */
211 c
->function
= old
->function
;
212 c
->prefixlist
= old
->prefixlist
;
213 c
->prefixname
= old
->prefixname
;
214 c
->allow_unknown
= old
->allow_unknown
;
215 c
->abbrev_flag
= abbrev_flag
;
216 c
->cmd_pointer
= old
;
220 /* Like add_cmd but adds an element for a command prefix:
221 a name that should be followed by a subcommand to be looked up
222 in another command list. PREFIXLIST should be the address
223 of the variable containing that list. */
225 struct cmd_list_element
*
226 add_prefix_cmd (char *name
, enum command_class
class, void (*fun
) (char *, int),
227 char *doc
, struct cmd_list_element
**prefixlist
,
228 char *prefixname
, int allow_unknown
,
229 struct cmd_list_element
**list
)
231 register struct cmd_list_element
*c
= add_cmd (name
, class, fun
, doc
, list
);
232 c
->prefixlist
= prefixlist
;
233 c
->prefixname
= prefixname
;
234 c
->allow_unknown
= allow_unknown
;
238 /* Like add_prefix_cmd but sets the abbrev_flag on the new command. */
240 struct cmd_list_element
*
241 add_abbrev_prefix_cmd (char *name
, enum command_class
class,
242 void (*fun
) (char *, int), char *doc
,
243 struct cmd_list_element
**prefixlist
, char *prefixname
,
244 int allow_unknown
, struct cmd_list_element
**list
)
246 register struct cmd_list_element
*c
= add_cmd (name
, class, fun
, doc
, list
);
247 c
->prefixlist
= prefixlist
;
248 c
->prefixname
= prefixname
;
249 c
->allow_unknown
= allow_unknown
;
254 /* This is an empty "cfunc". */
256 not_just_help_class_command (char *args
, int from_tty
)
260 /* This is an empty "sfunc". */
261 static void empty_sfunc (char *, int, struct cmd_list_element
*);
264 empty_sfunc (char *args
, int from_tty
, struct cmd_list_element
*c
)
268 /* Add element named NAME to command list LIST (the list for set
269 or some sublist thereof).
270 CLASS is as in add_cmd.
271 VAR_TYPE is the kind of thing we are setting.
272 VAR is address of the variable being controlled by this command.
273 DOC is the documentation string. */
275 struct cmd_list_element
*
276 add_set_cmd (char *name
,
277 enum command_class
class,
281 struct cmd_list_element
**list
)
283 struct cmd_list_element
*c
= add_cmd (name
, class, NULL
, doc
, list
);
286 c
->var_type
= var_type
;
288 /* This needs to be something besides NULL so that this isn't
289 treated as a help class. */
290 set_cmd_sfunc (c
, empty_sfunc
);
294 /* Add element named NAME to command list LIST (the list for set
295 or some sublist thereof).
296 CLASS is as in add_cmd.
297 ENUMLIST is a list of strings which may follow NAME.
298 VAR is address of the variable which will contain the matching string
300 DOC is the documentation string. */
302 struct cmd_list_element
*
303 add_set_enum_cmd (char *name
,
304 enum command_class
class,
305 const char *enumlist
[],
308 struct cmd_list_element
**list
)
310 struct cmd_list_element
*c
311 = add_set_cmd (name
, class, var_enum
, var
, doc
, list
);
317 /* Add element named NAME to command list LIST (the list for set
318 or some sublist thereof).
319 CLASS is as in add_cmd.
320 VAR is address of the variable which will contain the value.
321 DOC is the documentation string. */
322 struct cmd_list_element
*
323 add_set_auto_boolean_cmd (char *name
,
324 enum command_class
class,
325 enum cmd_auto_boolean
*var
,
327 struct cmd_list_element
**list
)
329 static const char *auto_boolean_enums
[] = { "on", "off", "auto", NULL
};
330 struct cmd_list_element
*c
;
331 c
= add_set_cmd (name
, class, var_auto_boolean
, var
, doc
, list
);
332 c
->enums
= auto_boolean_enums
;
336 /* Add element named NAME to command list LIST (the list for set
337 or some sublist thereof).
338 CLASS is as in add_cmd.
339 VAR is address of the variable which will contain the value.
340 DOC is the documentation string. */
341 struct cmd_list_element
*
342 add_set_boolean_cmd (char *name
,
343 enum command_class
class,
346 struct cmd_list_element
**list
)
348 static const char *boolean_enums
[] = { "on", "off", NULL
};
349 struct cmd_list_element
*c
;
350 c
= add_set_cmd (name
, class, var_boolean
, var
, doc
, list
);
351 c
->enums
= boolean_enums
;
355 /* Where SETCMD has already been added, add the corresponding show
356 command to LIST and return a pointer to the added command (not
357 necessarily the head of LIST). */
358 struct cmd_list_element
*
359 add_show_from_set (struct cmd_list_element
*setcmd
,
360 struct cmd_list_element
**list
)
362 struct cmd_list_element
*showcmd
=
363 (struct cmd_list_element
*) xmalloc (sizeof (struct cmd_list_element
));
364 struct cmd_list_element
*p
;
366 memcpy (showcmd
, setcmd
, sizeof (struct cmd_list_element
));
367 delete_cmd (showcmd
->name
, list
);
368 showcmd
->type
= show_cmd
;
370 /* Replace "set " at start of docstring with "show ". */
371 if (setcmd
->doc
[0] == 'S' && setcmd
->doc
[1] == 'e'
372 && setcmd
->doc
[2] == 't' && setcmd
->doc
[3] == ' ')
373 showcmd
->doc
= concat ("Show ", setcmd
->doc
+ 4, NULL
);
375 fprintf_unfiltered (gdb_stderr
, "GDB internal error: Bad docstring for set command\n");
377 if (*list
== NULL
|| strcmp ((*list
)->name
, showcmd
->name
) >= 0)
379 showcmd
->next
= *list
;
385 while (p
->next
&& strcmp (p
->next
->name
, showcmd
->name
) <= 0)
389 showcmd
->next
= p
->next
;
396 /* Remove the command named NAME from the command list. */
399 delete_cmd (char *name
, struct cmd_list_element
**list
)
401 register struct cmd_list_element
*c
;
402 struct cmd_list_element
*p
;
404 while (*list
&& STREQ ((*list
)->name
, name
))
406 if ((*list
)->hookee_pre
)
407 (*list
)->hookee_pre
->hook_pre
= 0; /* Hook slips out of its mouth */
408 if ((*list
)->hookee_post
)
409 (*list
)->hookee_post
->hook_post
= 0; /* Hook slips out of its bottom */
416 for (c
= *list
; c
->next
;)
418 if (STREQ (c
->next
->name
, name
))
420 if (c
->next
->hookee_pre
)
421 c
->next
->hookee_pre
->hook_pre
= 0; /* hooked cmd gets away. */
422 if (c
->next
->hookee_post
)
423 c
->next
->hookee_post
->hook_post
= 0; /* remove post hook */
424 /* :( no fishing metaphore */
434 /* Shorthands to the commands above. */
436 /* Add an element to the list of info subcommands. */
438 struct cmd_list_element
*
439 add_info (char *name
, void (*fun
) (char *, int), char *doc
)
441 return add_cmd (name
, no_class
, fun
, doc
, &infolist
);
444 /* Add an alias to the list of info subcommands. */
446 struct cmd_list_element
*
447 add_info_alias (char *name
, char *oldname
, int abbrev_flag
)
449 return add_alias_cmd (name
, oldname
, 0, abbrev_flag
, &infolist
);
452 /* Add an element to the list of commands. */
454 struct cmd_list_element
*
455 add_com (char *name
, enum command_class
class, void (*fun
) (char *, int),
458 return add_cmd (name
, class, fun
, doc
, &cmdlist
);
461 /* Add an alias or abbreviation command to the list of commands. */
463 struct cmd_list_element
*
464 add_com_alias (char *name
, char *oldname
, enum command_class
class,
467 return add_alias_cmd (name
, oldname
, class, abbrev_flag
, &cmdlist
);
470 /* Recursively walk the commandlist structures, and print out the
471 documentation of commands that match our regex in either their
472 name, or their documentation.
475 apropos_cmd (struct ui_file
*stream
, struct cmd_list_element
*commandlist
,
476 struct re_pattern_buffer
*regex
, char *prefix
)
478 register struct cmd_list_element
*c
;
479 int returnvalue
=1; /*Needed to avoid double printing*/
480 /* Walk through the commands */
481 for (c
=commandlist
;c
;c
=c
->next
)
485 /* Try to match against the name*/
486 returnvalue
=re_search(regex
,c
->name
,strlen(c
->name
),0,strlen(c
->name
),NULL
);
487 if (returnvalue
>= 0)
489 /* Stolen from help_cmd_list. We don't directly use
490 * help_cmd_list because it doesn't let us print out
493 fprintf_filtered (stream
, "%s%s -- ", prefix
, c
->name
);
494 print_doc_line (stream
, c
->doc
);
495 fputs_filtered ("\n", stream
);
496 returnvalue
=0; /*Set this so we don't print it again.*/
499 if (c
->doc
!= NULL
&& returnvalue
!= 0)
501 /* Try to match against documentation */
502 if (re_search(regex
,c
->doc
,strlen(c
->doc
),0,strlen(c
->doc
),NULL
) >=0)
504 /* Stolen from help_cmd_list. We don't directly use
505 * help_cmd_list because it doesn't let us print out
508 fprintf_filtered (stream
, "%s%s -- ", prefix
, c
->name
);
509 print_doc_line (stream
, c
->doc
);
510 fputs_filtered ("\n", stream
);
513 /* Check if this command has subcommands */
514 if (c
->prefixlist
!= NULL
)
516 /* Recursively call ourselves on the subcommand list,
517 passing the right prefix in.
519 apropos_cmd (stream
,*c
->prefixlist
,regex
,c
->prefixname
);
524 /* This command really has to deal with two things:
525 * 1) I want documentation on *this string* (usually called by
526 * "help commandname").
527 * 2) I want documentation on *this list* (usually called by
528 * giving a command that requires subcommands. Also called by saying
531 * I am going to split this into two seperate comamnds, help_cmd and
536 help_cmd (char *command
, struct ui_file
*stream
)
538 struct cmd_list_element
*c
;
539 extern struct cmd_list_element
*cmdlist
;
543 help_list (cmdlist
, "", all_classes
, stream
);
547 if (strcmp (command
, "all") == 0)
553 c
= lookup_cmd (&command
, cmdlist
, "", 0, 0);
558 /* There are three cases here.
559 If c->prefixlist is nonzero, we have a prefix command.
560 Print its documentation, then list its subcommands.
562 If c->func is non NULL, we really have a command. Print its
563 documentation and return.
565 If c->func is NULL, we have a class name. Print its
566 documentation (as if it were a command) and then set class to the
567 number of this class so that the commands in the class will be
570 fputs_filtered (c
->doc
, stream
);
571 fputs_filtered ("\n", stream
);
573 if (c
->prefixlist
== 0 && c
->func
!= NULL
)
575 fprintf_filtered (stream
, "\n");
577 /* If this is a prefix command, print it's subcommands */
579 help_list (*c
->prefixlist
, c
->prefixname
, all_commands
, stream
);
581 /* If this is a class name, print all of the commands in the class */
583 help_list (cmdlist
, "", c
->class, stream
);
585 if (c
->hook_pre
|| c
->hook_post
)
586 fprintf_filtered (stream
,
587 "\nThis command has a hook (or hooks) defined:\n");
590 fprintf_filtered (stream
,
591 "\tThis command is run after : %s (pre hook)\n",
594 fprintf_filtered (stream
,
595 "\tThis command is run before : %s (post hook)\n",
600 * Get a specific kind of help on a command list.
603 * CMDTYPE is the prefix to use in the title string.
604 * CLASS is the class with which to list the nodes of this list (see
605 * documentation for help_cmd_list below), As usual, ALL_COMMANDS for
606 * everything, ALL_CLASSES for just classes, and non-negative for only things
607 * in a specific class.
608 * and STREAM is the output stream on which to print things.
609 * If you call this routine with a class >= 0, it recurses.
612 help_list (struct cmd_list_element
*list
, char *cmdtype
,
613 enum command_class
class, struct ui_file
*stream
)
616 char *cmdtype1
, *cmdtype2
;
618 /* If CMDTYPE is "foo ", CMDTYPE1 gets " foo" and CMDTYPE2 gets "foo sub" */
619 len
= strlen (cmdtype
);
620 cmdtype1
= (char *) alloca (len
+ 1);
622 cmdtype2
= (char *) alloca (len
+ 4);
627 strncpy (cmdtype1
+ 1, cmdtype
, len
- 1);
629 strncpy (cmdtype2
, cmdtype
, len
- 1);
630 strcpy (cmdtype2
+ len
- 1, " sub");
633 if (class == all_classes
)
634 fprintf_filtered (stream
, "List of classes of %scommands:\n\n", cmdtype2
);
636 fprintf_filtered (stream
, "List of %scommands:\n\n", cmdtype2
);
638 help_cmd_list (list
, class, cmdtype
, (int) class >= 0, stream
);
640 if (class == all_classes
)
641 fprintf_filtered (stream
, "\n\
642 Type \"help%s\" followed by a class name for a list of commands in that class.",
645 fprintf_filtered (stream
, "\n\
646 Type \"help%s\" followed by %scommand name for full documentation.\n\
647 Command name abbreviations are allowed if unambiguous.\n",
652 help_all (struct ui_file
*stream
)
654 struct cmd_list_element
*c
;
655 extern struct cmd_list_element
*cmdlist
;
657 for (c
= cmdlist
; c
; c
= c
->next
)
661 /* If this is a prefix command, print it's subcommands */
663 help_cmd_list (*c
->prefixlist
, all_commands
, c
->prefixname
, 0, stream
);
665 /* If this is a class name, print all of the commands in the class */
666 else if (c
->func
== NULL
)
667 help_cmd_list (cmdlist
, c
->class, "", 0, stream
);
671 /* Print only the first line of STR on STREAM. */
673 print_doc_line (struct ui_file
*stream
, char *str
)
675 static char *line_buffer
= 0;
676 static int line_size
;
682 line_buffer
= (char *) xmalloc (line_size
);
686 while (*p
&& *p
!= '\n' && *p
!= '.' && *p
!= ',')
688 if (p
- str
> line_size
- 1)
690 line_size
= p
- str
+ 1;
692 line_buffer
= (char *) xmalloc (line_size
);
694 strncpy (line_buffer
, str
, p
- str
);
695 line_buffer
[p
- str
] = '\0';
696 if (islower (line_buffer
[0]))
697 line_buffer
[0] = toupper (line_buffer
[0]);
698 ui_out_text (uiout
, line_buffer
);
702 * Implement a help command on command list LIST.
703 * RECURSE should be non-zero if this should be done recursively on
704 * all sublists of LIST.
705 * PREFIX is the prefix to print before each command name.
706 * STREAM is the stream upon which the output should be written.
708 * A non-negative class number to list only commands in that
710 * ALL_COMMANDS to list all commands in list.
711 * ALL_CLASSES to list all classes in list.
713 * Note that RECURSE will be active on *all* sublists, not just the
714 * ones selected by the criteria above (ie. the selection mechanism
715 * is at the low level, not the high-level).
718 help_cmd_list (struct cmd_list_element
*list
, enum command_class
class,
719 char *prefix
, int recurse
, struct ui_file
*stream
)
721 register struct cmd_list_element
*c
;
723 for (c
= list
; c
; c
= c
->next
)
725 if (c
->abbrev_flag
== 0 &&
726 (class == all_commands
727 || (class == all_classes
&& c
->func
== NULL
)
728 || (class == c
->class && c
->func
!= NULL
)))
730 fprintf_filtered (stream
, "%s%s -- ", prefix
, c
->name
);
731 print_doc_line (stream
, c
->doc
);
732 fputs_filtered ("\n", stream
);
735 && c
->prefixlist
!= 0
736 && c
->abbrev_flag
== 0)
737 help_cmd_list (*c
->prefixlist
, class, c
->prefixname
, 1, stream
);
742 /* Search the input clist for 'command'. Return the command if
743 found (or NULL if not), and return the number of commands
746 static struct cmd_list_element
*
747 find_cmd (char *command
, int len
, struct cmd_list_element
*clist
,
748 int ignore_help_classes
, int *nfound
)
750 struct cmd_list_element
*found
, *c
;
752 found
= (struct cmd_list_element
*) NULL
;
754 for (c
= clist
; c
; c
= c
->next
)
755 if (!strncmp (command
, c
->name
, len
)
756 && (!ignore_help_classes
|| c
->func
))
760 if (c
->name
[len
] == '\0')
769 /* This routine takes a line of TEXT and a CLIST in which to start the
770 lookup. When it returns it will have incremented the text pointer past
771 the section of text it matched, set *RESULT_LIST to point to the list in
772 which the last word was matched, and will return a pointer to the cmd
773 list element which the text matches. It will return NULL if no match at
774 all was possible. It will return -1 (cast appropriately, ick) if ambigous
775 matches are possible; in this case *RESULT_LIST will be set to point to
776 the list in which there are ambiguous choices (and *TEXT will be set to
777 the ambiguous text string).
779 If the located command was an abbreviation, this routine returns the base
780 command of the abbreviation.
782 It does no error reporting whatsoever; control will always return
783 to the superior routine.
785 In the case of an ambiguous return (-1), *RESULT_LIST will be set to point
786 at the prefix_command (ie. the best match) *or* (special case) will be NULL
787 if no prefix command was ever found. For example, in the case of "info a",
788 "info" matches without ambiguity, but "a" could be "args" or "address", so
789 *RESULT_LIST is set to the cmd_list_element for "info". So in this case
790 RESULT_LIST should not be interpeted as a pointer to the beginning of a
791 list; it simply points to a specific command. In the case of an ambiguous
792 return *TEXT is advanced past the last non-ambiguous prefix (e.g.
793 "info t" can be "info types" or "info target"; upon return *TEXT has been
794 advanced past "info ").
796 If RESULT_LIST is NULL, don't set *RESULT_LIST (but don't otherwise
797 affect the operation).
799 This routine does *not* modify the text pointed to by TEXT.
801 If IGNORE_HELP_CLASSES is nonzero, ignore any command list elements which
802 are actually help classes rather than commands (i.e. the function field of
803 the struct cmd_list_element is NULL). */
805 struct cmd_list_element
*
806 lookup_cmd_1 (char **text
, struct cmd_list_element
*clist
,
807 struct cmd_list_element
**result_list
, int ignore_help_classes
)
810 int len
, tmp
, nfound
;
811 struct cmd_list_element
*found
, *c
;
814 while (**text
== ' ' || **text
== '\t')
817 /* Treating underscores as part of command words is important
818 so that "set args_foo()" doesn't get interpreted as
819 "set args _foo()". */
821 *p
&& (isalnum (*p
) || *p
== '-' || *p
== '_' ||
823 (*p
== '+' || *p
== '<' || *p
== '>' || *p
== '$')) ||
824 (xdb_commands
&& (*p
== '!' || *p
== '/' || *p
== '?')));
828 /* If nothing but whitespace, return 0. */
834 /* *text and p now bracket the first command word to lookup (and
835 it's length is len). We copy this into a local temporary */
838 command
= (char *) alloca (len
+ 1);
839 for (tmp
= 0; tmp
< len
; tmp
++)
841 char x
= (*text
)[tmp
];
849 found
= find_cmd (command
, len
, clist
, ignore_help_classes
, &nfound
);
852 ** We didn't find the command in the entered case, so lower case it
855 if (!found
|| nfound
== 0)
857 for (tmp
= 0; tmp
< len
; tmp
++)
859 char x
= command
[tmp
];
860 command
[tmp
] = isupper (x
) ? tolower (x
) : x
;
862 found
= find_cmd (command
, len
, clist
, ignore_help_classes
, &nfound
);
865 /* If nothing matches, we have a simple failure. */
871 if (result_list
!= NULL
)
872 /* Will be modified in calling routine
873 if we know what the prefix command is. */
875 return (struct cmd_list_element
*) -1; /* Ambiguous. */
878 /* We've matched something on this list. Move text pointer forward. */
882 if (found
->cmd_pointer
)
884 /* We drop the alias (abbreviation) in favor of the command it is
885 pointing to. If the alias is deprecated, though, we need to
886 warn the user about it before we drop it. Note that while we
887 are warning about the alias, we may also warn about the command
888 itself and we will adjust the appropriate DEPRECATED_WARN_USER
891 if (found
->flags
& DEPRECATED_WARN_USER
)
892 deprecated_cmd_warning (&line
);
893 found
= found
->cmd_pointer
;
895 /* If we found a prefix command, keep looking. */
897 if (found
->prefixlist
)
899 c
= lookup_cmd_1 (text
, *found
->prefixlist
, result_list
,
900 ignore_help_classes
);
903 /* Didn't find anything; this is as far as we got. */
904 if (result_list
!= NULL
)
905 *result_list
= clist
;
908 else if (c
== (struct cmd_list_element
*) -1)
910 /* We've gotten this far properly, but the next step
911 is ambiguous. We need to set the result list to the best
912 we've found (if an inferior hasn't already set it). */
913 if (result_list
!= NULL
)
915 /* This used to say *result_list = *found->prefixlist
916 If that was correct, need to modify the documentation
917 at the top of this function to clarify what is supposed
919 *result_list
= found
;
930 if (result_list
!= NULL
)
931 *result_list
= clist
;
936 /* All this hair to move the space to the front of cmdtype */
939 undef_cmd_error (char *cmdtype
, char *q
)
941 error ("Undefined %scommand: \"%s\". Try \"help%s%.*s\".",
945 strlen (cmdtype
) - 1,
949 /* Look up the contents of *LINE as a command in the command list LIST.
950 LIST is a chain of struct cmd_list_element's.
951 If it is found, return the struct cmd_list_element for that command
952 and update *LINE to point after the command name, at the first argument.
953 If not found, call error if ALLOW_UNKNOWN is zero
954 otherwise (or if error returns) return zero.
955 Call error if specified command is ambiguous,
956 unless ALLOW_UNKNOWN is negative.
957 CMDTYPE precedes the word "command" in the error message.
959 If INGNORE_HELP_CLASSES is nonzero, ignore any command list
960 elements which are actually help classes rather than commands (i.e.
961 the function field of the struct cmd_list_element is 0). */
963 struct cmd_list_element
*
964 lookup_cmd (char **line
, struct cmd_list_element
*list
, char *cmdtype
,
965 int allow_unknown
, int ignore_help_classes
)
967 struct cmd_list_element
*last_list
= 0;
968 struct cmd_list_element
*c
=
969 lookup_cmd_1 (line
, list
, &last_list
, ignore_help_classes
);
971 /* Note: Do not remove trailing whitespace here because this
972 would be wrong for complete_command. Jim Kingdon */
979 error ("Lack of needed %scommand", cmdtype
);
984 while (isalnum (*p
) || *p
== '-')
987 q
= (char *) alloca (p
- *line
+ 1);
988 strncpy (q
, *line
, p
- *line
);
990 undef_cmd_error (cmdtype
, q
);
996 else if (c
== (struct cmd_list_element
*) -1)
998 /* Ambigous. Local values should be off prefixlist or called
1000 int local_allow_unknown
= (last_list
? last_list
->allow_unknown
:
1002 char *local_cmdtype
= last_list
? last_list
->prefixname
: cmdtype
;
1003 struct cmd_list_element
*local_list
=
1004 (last_list
? *(last_list
->prefixlist
) : list
);
1006 if (local_allow_unknown
< 0)
1009 return last_list
; /* Found something. */
1011 return 0; /* Found nothing. */
1015 /* Report as error. */
1020 ((*line
)[amb_len
] && (*line
)[amb_len
] != ' '
1021 && (*line
)[amb_len
] != '\t');
1026 for (c
= local_list
; c
; c
= c
->next
)
1027 if (!strncmp (*line
, c
->name
, amb_len
))
1029 if (strlen (ambbuf
) + strlen (c
->name
) + 6 < (int) sizeof ambbuf
)
1031 if (strlen (ambbuf
))
1032 strcat (ambbuf
, ", ");
1033 strcat (ambbuf
, c
->name
);
1037 strcat (ambbuf
, "..");
1041 error ("Ambiguous %scommand \"%s\": %s.", local_cmdtype
,
1043 return 0; /* lint */
1048 /* We've got something. It may still not be what the caller
1049 wants (if this command *needs* a subcommand). */
1050 while (**line
== ' ' || **line
== '\t')
1053 if (c
->prefixlist
&& **line
&& !c
->allow_unknown
)
1054 undef_cmd_error (c
->prefixname
, *line
);
1056 /* Seems to be what he wants. Return it. */
1062 /* We are here presumably because an alias or command in *TEXT is
1063 deprecated and a warning message should be generated. This function
1064 decodes *TEXT and potentially generates a warning message as outlined
1067 Example for 'set endian big' which has a fictitious alias 'seb'.
1069 If alias wasn't used in *TEXT, and the command is deprecated:
1070 "warning: 'set endian big' is deprecated."
1072 If alias was used, and only the alias is deprecated:
1073 "warning: 'seb' an alias for the command 'set endian big' is deprecated."
1075 If alias was used and command is deprecated (regardless of whether the
1076 alias itself is deprecated:
1078 "warning: 'set endian big' (seb) is deprecated."
1080 After the message has been sent, clear the appropriate flags in the
1081 command and/or the alias so the user is no longer bothered.
1085 deprecated_cmd_warning (char **text
)
1087 struct cmd_list_element
*alias
= NULL
;
1088 struct cmd_list_element
*prefix_cmd
= NULL
;
1089 struct cmd_list_element
*cmd
= NULL
;
1090 struct cmd_list_element
*c
;
1093 if (!lookup_cmd_composition (*text
, &alias
, &prefix_cmd
, &cmd
))
1094 /* return if text doesn't evaluate to a command */
1097 if (!((alias
? (alias
->flags
& DEPRECATED_WARN_USER
) : 0)
1098 || (cmd
->flags
& DEPRECATED_WARN_USER
) ) )
1099 /* return if nothing is deprecated */
1102 printf_filtered ("Warning:");
1104 if (alias
&& !(cmd
->flags
& CMD_DEPRECATED
))
1105 printf_filtered (" '%s', an alias for the", alias
->name
);
1107 printf_filtered (" command '");
1110 printf_filtered ("%s", prefix_cmd
->prefixname
);
1112 printf_filtered ("%s", cmd
->name
);
1114 if (alias
&& (cmd
->flags
& CMD_DEPRECATED
))
1115 printf_filtered ("' (%s) is deprecated.\n", alias
->name
);
1117 printf_filtered ("' is deprecated.\n");
1120 /* if it is only the alias that is deprecated, we want to indicate the
1121 new alias, otherwise we'll indicate the new command */
1123 if (alias
&& !(cmd
->flags
& CMD_DEPRECATED
))
1125 if (alias
->replacement
)
1126 printf_filtered ("Use '%s'.\n\n", alias
->replacement
);
1128 printf_filtered ("No alternative known.\n\n");
1132 if (cmd
->replacement
)
1133 printf_filtered ("Use '%s'.\n\n", cmd
->replacement
);
1135 printf_filtered ("No alternative known.\n\n");
1138 /* We've warned you, now we'll keep quiet */
1140 alias
->flags
&= ~DEPRECATED_WARN_USER
;
1142 cmd
->flags
&= ~DEPRECATED_WARN_USER
;
1147 /* Look up the contents of LINE as a command in the command list 'cmdlist'.
1148 Return 1 on success, 0 on failure.
1150 If LINE refers to an alias, *alias will point to that alias.
1152 If LINE is a postfix command (i.e. one that is preceeded by a prefix
1153 command) set *prefix_cmd.
1155 Set *cmd to point to the command LINE indicates.
1157 If any of *alias, *prefix_cmd, or *cmd cannot be determined or do not
1158 exist, they are NULL when we return.
1162 lookup_cmd_composition (char *text
,
1163 struct cmd_list_element
**alias
,
1164 struct cmd_list_element
**prefix_cmd
,
1165 struct cmd_list_element
**cmd
)
1168 int len
, tmp
, nfound
;
1169 struct cmd_list_element
*cur_list
;
1170 struct cmd_list_element
*prev_cmd
;
1179 /* Go through as many command lists as we need to
1180 to find the command TEXT refers to. */
1184 while (*text
== ' ' || *text
== '\t')
1187 /* Treating underscores as part of command words is important
1188 so that "set args_foo()" doesn't get interpreted as
1189 "set args _foo()". */
1191 *p
&& (isalnum (*p
) || *p
== '-' || *p
== '_' ||
1193 (*p
== '+' || *p
== '<' || *p
== '>' || *p
== '$')) ||
1194 (xdb_commands
&& (*p
== '!' || *p
== '/' || *p
== '?')));
1198 /* If nothing but whitespace, return. */
1204 /* text and p now bracket the first command word to lookup (and
1205 it's length is len). We copy this into a local temporary */
1207 command
= (char *) alloca (len
+ 1);
1208 for (tmp
= 0; tmp
< len
; tmp
++)
1213 command
[len
] = '\0';
1218 *cmd
= find_cmd (command
, len
, cur_list
, 1, &nfound
);
1220 /* We didn't find the command in the entered case, so lower case it
1223 if (!*cmd
|| nfound
== 0)
1225 for (tmp
= 0; tmp
< len
; tmp
++)
1227 char x
= command
[tmp
];
1228 command
[tmp
] = isupper (x
) ? tolower (x
) : x
;
1230 *cmd
= find_cmd (command
, len
, cur_list
, 1, &nfound
);
1233 if (*cmd
== (struct cmd_list_element
*) -1)
1235 return 0; /* ambiguous */
1239 return 0; /* nothing found */
1242 if ((*cmd
)->cmd_pointer
)
1244 /* cmd was actually an alias, we note that an alias was used
1245 (by assigning *alais) and we set *cmd.
1248 *cmd
= (*cmd
)->cmd_pointer
;
1250 *prefix_cmd
= prev_cmd
;
1252 if ((*cmd
)->prefixlist
)
1253 cur_list
= *(*cmd
)->prefixlist
;
1261 /* Helper function for SYMBOL_COMPLETION_FUNCTION. */
1263 /* Return a vector of char pointers which point to the different
1264 possible completions in LIST of TEXT.
1266 WORD points in the same buffer as TEXT, and completions should be
1267 returned relative to this position. For example, suppose TEXT is "foo"
1268 and we want to complete to "foobar". If WORD is "oo", return
1269 "oobar"; if WORD is "baz/foo", return "baz/foobar". */
1272 complete_on_cmdlist (struct cmd_list_element
*list
, char *text
, char *word
)
1274 struct cmd_list_element
*ptr
;
1276 int sizeof_matchlist
;
1278 int textlen
= strlen (text
);
1280 sizeof_matchlist
= 10;
1281 matchlist
= (char **) xmalloc (sizeof_matchlist
* sizeof (char *));
1284 for (ptr
= list
; ptr
; ptr
= ptr
->next
)
1285 if (!strncmp (ptr
->name
, text
, textlen
)
1286 && !ptr
->abbrev_flag
1288 || ptr
->prefixlist
))
1290 if (matches
== sizeof_matchlist
)
1292 sizeof_matchlist
*= 2;
1293 matchlist
= (char **) xrealloc ((char *) matchlist
,
1295 * sizeof (char *)));
1298 matchlist
[matches
] = (char *)
1299 xmalloc (strlen (word
) + strlen (ptr
->name
) + 1);
1301 strcpy (matchlist
[matches
], ptr
->name
);
1302 else if (word
> text
)
1304 /* Return some portion of ptr->name. */
1305 strcpy (matchlist
[matches
], ptr
->name
+ (word
- text
));
1309 /* Return some of text plus ptr->name. */
1310 strncpy (matchlist
[matches
], word
, text
- word
);
1311 matchlist
[matches
][text
- word
] = '\0';
1312 strcat (matchlist
[matches
], ptr
->name
);
1324 matchlist
= (char **) xrealloc ((char *) matchlist
, ((matches
+ 1)
1325 * sizeof (char *)));
1326 matchlist
[matches
] = (char *) 0;
1332 /* Helper function for SYMBOL_COMPLETION_FUNCTION. */
1334 /* Return a vector of char pointers which point to the different
1335 possible completions in CMD of TEXT.
1337 WORD points in the same buffer as TEXT, and completions should be
1338 returned relative to this position. For example, suppose TEXT is "foo"
1339 and we want to complete to "foobar". If WORD is "oo", return
1340 "oobar"; if WORD is "baz/foo", return "baz/foobar". */
1343 complete_on_enum (const char *enumlist
[],
1348 int sizeof_matchlist
;
1350 int textlen
= strlen (text
);
1354 sizeof_matchlist
= 10;
1355 matchlist
= (char **) xmalloc (sizeof_matchlist
* sizeof (char *));
1358 for (i
= 0; (name
= enumlist
[i
]) != NULL
; i
++)
1359 if (strncmp (name
, text
, textlen
) == 0)
1361 if (matches
== sizeof_matchlist
)
1363 sizeof_matchlist
*= 2;
1364 matchlist
= (char **) xrealloc ((char *) matchlist
,
1366 * sizeof (char *)));
1369 matchlist
[matches
] = (char *)
1370 xmalloc (strlen (word
) + strlen (name
) + 1);
1372 strcpy (matchlist
[matches
], name
);
1373 else if (word
> text
)
1375 /* Return some portion of name. */
1376 strcpy (matchlist
[matches
], name
+ (word
- text
));
1380 /* Return some of text plus name. */
1381 strncpy (matchlist
[matches
], word
, text
- word
);
1382 matchlist
[matches
][text
- word
] = '\0';
1383 strcat (matchlist
[matches
], name
);
1395 matchlist
= (char **) xrealloc ((char *) matchlist
, ((matches
+ 1)
1396 * sizeof (char *)));
1397 matchlist
[matches
] = (char *) 0;