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 #include "gdb_assert.h"
33 /* Prototypes for local functions */
35 static void undef_cmd_error (char *, char *);
37 static struct cmd_list_element
*find_cmd (char *command
,
39 struct cmd_list_element
*clist
,
40 int ignore_help_classes
,
43 static void help_all (struct ui_file
*stream
);
45 /* Set the callback function for the specified command. For each both
46 the commands callback and func() are set. The latter set to a
47 bounce function (unless cfunc / sfunc is NULL that is). */
50 do_cfunc (struct cmd_list_element
*c
, char *args
, int from_tty
)
52 c
->function
.cfunc (args
, from_tty
); /* Ok. */
56 set_cmd_cfunc (struct cmd_list_element
*cmd
,
57 void (*cfunc
) (char *args
, int from_tty
))
63 cmd
->function
.cfunc
= cfunc
; /* Ok. */
67 do_sfunc (struct cmd_list_element
*c
, char *args
, int from_tty
)
69 c
->function
.sfunc (args
, from_tty
, c
); /* Ok. */
73 set_cmd_sfunc (struct cmd_list_element
*cmd
,
74 void (*sfunc
) (char *args
, int from_tty
,
75 struct cmd_list_element
* c
))
81 cmd
->function
.sfunc
= sfunc
; /* Ok. */
85 cmd_cfunc_eq (struct cmd_list_element
*cmd
,
86 void (*cfunc
) (char *args
, int from_tty
))
88 return cmd
->func
== do_cfunc
&& cmd
->function
.cfunc
== cfunc
;
92 set_cmd_context (struct cmd_list_element
*cmd
, void *context
)
94 cmd
->context
= context
;
98 get_cmd_context (struct cmd_list_element
*cmd
)
104 cmd_type (struct cmd_list_element
*cmd
)
110 set_cmd_completer (struct cmd_list_element
*cmd
,
111 char **(*completer
) (char *text
, char *word
))
113 cmd
->completer
= completer
; /* Ok. */
117 /* Add element named NAME.
118 CLASS is the top level category into which commands are broken down
120 FUN should be the function to execute the command;
121 it will get a character string as argument, with leading
122 and trailing blanks already eliminated.
124 DOC is a documentation string for the command.
125 Its first line should be a complete sentence.
126 It should start with ? for a command that is an abbreviation
127 or with * for a command that most users don't need to know about.
129 Add this command to command list *LIST.
131 Returns a pointer to the added command (not necessarily the head
134 struct cmd_list_element
*
135 add_cmd (char *name
, enum command_class
class, void (*fun
) (char *, int),
136 char *doc
, struct cmd_list_element
**list
)
138 register struct cmd_list_element
*c
139 = (struct cmd_list_element
*) xmalloc (sizeof (struct cmd_list_element
));
140 struct cmd_list_element
*p
;
142 delete_cmd (name
, list
);
144 if (*list
== NULL
|| strcmp ((*list
)->name
, name
) >= 0)
152 while (p
->next
&& strcmp (p
->next
->name
, name
) <= 0)
162 set_cmd_cfunc (c
, fun
);
163 set_cmd_context (c
, NULL
);
166 c
->replacement
= NULL
;
167 c
->pre_show_hook
= NULL
;
171 c
->prefixlist
= NULL
;
172 c
->prefixname
= NULL
;
173 c
->allow_unknown
= 0;
175 set_cmd_completer (c
, make_symbol_completion_list
);
176 c
->type
= not_set_cmd
;
178 c
->var_type
= var_boolean
;
180 c
->user_commands
= NULL
;
181 c
->hookee_pre
= NULL
;
182 c
->hookee_post
= NULL
;
183 c
->cmd_pointer
= NULL
;
188 /* Same as above, except that the abbrev_flag is set. */
189 /* Note: Doesn't seem to be used anywhere currently. */
191 struct cmd_list_element
*
192 add_abbrev_cmd (char *name
, enum command_class
class, void (*fun
) (char *, int),
193 char *doc
, struct cmd_list_element
**list
)
195 register struct cmd_list_element
*c
196 = add_cmd (name
, class, fun
, doc
, list
);
202 /* Deprecates a command CMD.
203 REPLACEMENT is the name of the command which should be used in place
204 of this command, or NULL if no such command exists.
206 This function does not check to see if command REPLACEMENT exists
207 since gdb may not have gotten around to adding REPLACEMENT when this
210 Returns a pointer to the deprecated command. */
212 struct cmd_list_element
*
213 deprecate_cmd (struct cmd_list_element
*cmd
, char *replacement
)
215 cmd
->flags
|= (CMD_DEPRECATED
| DEPRECATED_WARN_USER
);
217 if (replacement
!= NULL
)
218 cmd
->replacement
= replacement
;
220 cmd
->replacement
= NULL
;
225 struct cmd_list_element
*
226 add_alias_cmd (char *name
, char *oldname
, enum command_class
class,
227 int abbrev_flag
, struct cmd_list_element
**list
)
229 /* Must do this since lookup_cmd tries to side-effect its first arg */
231 register struct cmd_list_element
*old
;
232 register struct cmd_list_element
*c
;
233 copied_name
= (char *) alloca (strlen (oldname
) + 1);
234 strcpy (copied_name
, oldname
);
235 old
= lookup_cmd (&copied_name
, *list
, "", 1, 1);
239 delete_cmd (name
, list
);
243 c
= add_cmd (name
, class, NULL
, old
->doc
, list
);
244 /* NOTE: Both FUNC and all the FUNCTIONs need to be copied. */
246 c
->function
= old
->function
;
247 c
->prefixlist
= old
->prefixlist
;
248 c
->prefixname
= old
->prefixname
;
249 c
->allow_unknown
= old
->allow_unknown
;
250 c
->abbrev_flag
= abbrev_flag
;
251 c
->cmd_pointer
= old
;
255 /* Like add_cmd but adds an element for a command prefix:
256 a name that should be followed by a subcommand to be looked up
257 in another command list. PREFIXLIST should be the address
258 of the variable containing that list. */
260 struct cmd_list_element
*
261 add_prefix_cmd (char *name
, enum command_class
class, void (*fun
) (char *, int),
262 char *doc
, struct cmd_list_element
**prefixlist
,
263 char *prefixname
, int allow_unknown
,
264 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
;
273 /* Like add_prefix_cmd but sets the abbrev_flag on the new command. */
275 struct cmd_list_element
*
276 add_abbrev_prefix_cmd (char *name
, enum command_class
class,
277 void (*fun
) (char *, int), char *doc
,
278 struct cmd_list_element
**prefixlist
, char *prefixname
,
279 int allow_unknown
, struct cmd_list_element
**list
)
281 register struct cmd_list_element
*c
= add_cmd (name
, class, fun
, doc
, list
);
282 c
->prefixlist
= prefixlist
;
283 c
->prefixname
= prefixname
;
284 c
->allow_unknown
= allow_unknown
;
289 /* This is an empty "cfunc". */
291 not_just_help_class_command (char *args
, int from_tty
)
295 /* This is an empty "sfunc". */
296 static void empty_sfunc (char *, int, struct cmd_list_element
*);
299 empty_sfunc (char *args
, int from_tty
, struct cmd_list_element
*c
)
303 /* Add element named NAME to command list LIST (the list for set/show
304 or some sublist thereof).
305 TYPE is set_cmd or show_cmd.
306 CLASS is as in add_cmd.
307 VAR_TYPE is the kind of thing we are setting.
308 VAR is address of the variable being controlled by this command.
309 DOC is the documentation string. */
311 static struct cmd_list_element
*
312 add_set_or_show_cmd (char *name
,
314 enum command_class
class,
318 struct cmd_list_element
**list
)
320 struct cmd_list_element
*c
= add_cmd (name
, class, NULL
, doc
, list
);
321 gdb_assert (type
== set_cmd
|| type
== show_cmd
);
323 c
->var_type
= var_type
;
325 /* This needs to be something besides NULL so that this isn't
326 treated as a help class. */
327 set_cmd_sfunc (c
, empty_sfunc
);
332 struct cmd_list_element
*
333 add_set_cmd (char *name
,
334 enum command_class
class,
338 struct cmd_list_element
**list
)
340 return add_set_or_show_cmd (name
, set_cmd
, class, var_type
, var
, doc
, list
);
343 /* Add element named NAME to command list LIST (the list for set
344 or some sublist thereof).
345 CLASS is as in add_cmd.
346 ENUMLIST is a list of strings which may follow NAME.
347 VAR is address of the variable which will contain the matching string
349 DOC is the documentation string. */
351 struct cmd_list_element
*
352 add_set_enum_cmd (char *name
,
353 enum command_class
class,
354 const char *enumlist
[],
357 struct cmd_list_element
**list
)
359 struct cmd_list_element
*c
360 = add_set_cmd (name
, class, var_enum
, var
, doc
, list
);
366 /* Add element named NAME to command list LIST (the list for set
367 or some sublist thereof).
368 CLASS is as in add_cmd.
369 VAR is address of the variable which will contain the value.
370 DOC is the documentation string. */
371 struct cmd_list_element
*
372 add_set_auto_boolean_cmd (char *name
,
373 enum command_class
class,
374 enum cmd_auto_boolean
*var
,
376 struct cmd_list_element
**list
)
378 static const char *auto_boolean_enums
[] = { "on", "off", "auto", NULL
};
379 struct cmd_list_element
*c
;
380 c
= add_set_cmd (name
, class, var_auto_boolean
, var
, doc
, list
);
381 c
->enums
= auto_boolean_enums
;
385 /* Add element named NAME to command list LIST (the list for set
386 or some sublist thereof).
387 CLASS is as in add_cmd.
388 VAR is address of the variable which will contain the value.
389 DOC is the documentation string. */
390 struct cmd_list_element
*
391 add_set_boolean_cmd (char *name
,
392 enum command_class
class,
395 struct cmd_list_element
**list
)
397 static const char *boolean_enums
[] = { "on", "off", NULL
};
398 struct cmd_list_element
*c
;
399 c
= add_set_cmd (name
, class, var_boolean
, var
, doc
, list
);
400 c
->enums
= boolean_enums
;
404 /* Where SETCMD has already been added, add the corresponding show
405 command to LIST and return a pointer to the added command (not
406 necessarily the head of LIST). */
407 /* NOTE: cagney/2002-03-17: The original version of add_show_from_set
408 used memcpy() to clone `set' into `show'. This ment that in
409 addition to all the needed fields (var, name, et.al.) some
410 unnecessary fields were copied (namely the callback function). The
411 function explictly copies relevant fields. For a `set' and `show'
412 command to share the same callback, the caller must set both
414 struct cmd_list_element
*
415 add_show_from_set (struct cmd_list_element
*setcmd
,
416 struct cmd_list_element
**list
)
419 const static char setstring
[] = "Set ";
421 /* Create a doc string by replacing "Set " at the start of the
422 `set'' command's doco with "Show ". */
423 gdb_assert (strncmp (setcmd
->doc
, setstring
, sizeof (setstring
) - 1) == 0);
424 doc
= concat ("Show ", setcmd
->doc
+ sizeof (setstring
) - 1, NULL
);
426 /* Insert the basic command. */
427 return add_set_or_show_cmd (setcmd
->name
, show_cmd
, setcmd
->class,
428 setcmd
->var_type
, setcmd
->var
, doc
, list
);
431 /* Remove the command named NAME from the command list. */
434 delete_cmd (char *name
, struct cmd_list_element
**list
)
436 register struct cmd_list_element
*c
;
437 struct cmd_list_element
*p
;
439 while (*list
&& STREQ ((*list
)->name
, name
))
441 if ((*list
)->hookee_pre
)
442 (*list
)->hookee_pre
->hook_pre
= 0; /* Hook slips out of its mouth */
443 if ((*list
)->hookee_post
)
444 (*list
)->hookee_post
->hook_post
= 0; /* Hook slips out of its bottom */
451 for (c
= *list
; c
->next
;)
453 if (STREQ (c
->next
->name
, name
))
455 if (c
->next
->hookee_pre
)
456 c
->next
->hookee_pre
->hook_pre
= 0; /* hooked cmd gets away. */
457 if (c
->next
->hookee_post
)
458 c
->next
->hookee_post
->hook_post
= 0; /* remove post hook */
459 /* :( no fishing metaphore */
469 /* Shorthands to the commands above. */
471 /* Add an element to the list of info subcommands. */
473 struct cmd_list_element
*
474 add_info (char *name
, void (*fun
) (char *, int), char *doc
)
476 return add_cmd (name
, no_class
, fun
, doc
, &infolist
);
479 /* Add an alias to the list of info subcommands. */
481 struct cmd_list_element
*
482 add_info_alias (char *name
, char *oldname
, int abbrev_flag
)
484 return add_alias_cmd (name
, oldname
, 0, abbrev_flag
, &infolist
);
487 /* Add an element to the list of commands. */
489 struct cmd_list_element
*
490 add_com (char *name
, enum command_class
class, void (*fun
) (char *, int),
493 return add_cmd (name
, class, fun
, doc
, &cmdlist
);
496 /* Add an alias or abbreviation command to the list of commands. */
498 struct cmd_list_element
*
499 add_com_alias (char *name
, char *oldname
, enum command_class
class,
502 return add_alias_cmd (name
, oldname
, class, abbrev_flag
, &cmdlist
);
505 /* Recursively walk the commandlist structures, and print out the
506 documentation of commands that match our regex in either their
507 name, or their documentation.
510 apropos_cmd (struct ui_file
*stream
, struct cmd_list_element
*commandlist
,
511 struct re_pattern_buffer
*regex
, char *prefix
)
513 register struct cmd_list_element
*c
;
514 int returnvalue
=1; /*Needed to avoid double printing*/
515 /* Walk through the commands */
516 for (c
=commandlist
;c
;c
=c
->next
)
520 /* Try to match against the name*/
521 returnvalue
=re_search(regex
,c
->name
,strlen(c
->name
),0,strlen(c
->name
),NULL
);
522 if (returnvalue
>= 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
);
531 returnvalue
=0; /*Set this so we don't print it again.*/
534 if (c
->doc
!= NULL
&& returnvalue
!= 0)
536 /* Try to match against documentation */
537 if (re_search(regex
,c
->doc
,strlen(c
->doc
),0,strlen(c
->doc
),NULL
) >=0)
539 /* Stolen from help_cmd_list. We don't directly use
540 * help_cmd_list because it doesn't let us print out
543 fprintf_filtered (stream
, "%s%s -- ", prefix
, c
->name
);
544 print_doc_line (stream
, c
->doc
);
545 fputs_filtered ("\n", stream
);
548 /* Check if this command has subcommands */
549 if (c
->prefixlist
!= NULL
)
551 /* Recursively call ourselves on the subcommand list,
552 passing the right prefix in.
554 apropos_cmd (stream
,*c
->prefixlist
,regex
,c
->prefixname
);
559 /* This command really has to deal with two things:
560 * 1) I want documentation on *this string* (usually called by
561 * "help commandname").
562 * 2) I want documentation on *this list* (usually called by
563 * giving a command that requires subcommands. Also called by saying
566 * I am going to split this into two seperate comamnds, help_cmd and
571 help_cmd (char *command
, struct ui_file
*stream
)
573 struct cmd_list_element
*c
;
574 extern struct cmd_list_element
*cmdlist
;
578 help_list (cmdlist
, "", all_classes
, stream
);
582 if (strcmp (command
, "all") == 0)
588 c
= lookup_cmd (&command
, cmdlist
, "", 0, 0);
593 /* There are three cases here.
594 If c->prefixlist is nonzero, we have a prefix command.
595 Print its documentation, then list its subcommands.
597 If c->func is non NULL, we really have a command. Print its
598 documentation and return.
600 If c->func is NULL, we have a class name. Print its
601 documentation (as if it were a command) and then set class to the
602 number of this class so that the commands in the class will be
605 fputs_filtered (c
->doc
, stream
);
606 fputs_filtered ("\n", stream
);
608 if (c
->prefixlist
== 0 && c
->func
!= NULL
)
610 fprintf_filtered (stream
, "\n");
612 /* If this is a prefix command, print it's subcommands */
614 help_list (*c
->prefixlist
, c
->prefixname
, all_commands
, stream
);
616 /* If this is a class name, print all of the commands in the class */
618 help_list (cmdlist
, "", c
->class, stream
);
620 if (c
->hook_pre
|| c
->hook_post
)
621 fprintf_filtered (stream
,
622 "\nThis command has a hook (or hooks) defined:\n");
625 fprintf_filtered (stream
,
626 "\tThis command is run after : %s (pre hook)\n",
629 fprintf_filtered (stream
,
630 "\tThis command is run before : %s (post hook)\n",
635 * Get a specific kind of help on a command list.
638 * CMDTYPE is the prefix to use in the title string.
639 * CLASS is the class with which to list the nodes of this list (see
640 * documentation for help_cmd_list below), As usual, ALL_COMMANDS for
641 * everything, ALL_CLASSES for just classes, and non-negative for only things
642 * in a specific class.
643 * and STREAM is the output stream on which to print things.
644 * If you call this routine with a class >= 0, it recurses.
647 help_list (struct cmd_list_element
*list
, char *cmdtype
,
648 enum command_class
class, struct ui_file
*stream
)
651 char *cmdtype1
, *cmdtype2
;
653 /* If CMDTYPE is "foo ", CMDTYPE1 gets " foo" and CMDTYPE2 gets "foo sub" */
654 len
= strlen (cmdtype
);
655 cmdtype1
= (char *) alloca (len
+ 1);
657 cmdtype2
= (char *) alloca (len
+ 4);
662 strncpy (cmdtype1
+ 1, cmdtype
, len
- 1);
664 strncpy (cmdtype2
, cmdtype
, len
- 1);
665 strcpy (cmdtype2
+ len
- 1, " sub");
668 if (class == all_classes
)
669 fprintf_filtered (stream
, "List of classes of %scommands:\n\n", cmdtype2
);
671 fprintf_filtered (stream
, "List of %scommands:\n\n", cmdtype2
);
673 help_cmd_list (list
, class, cmdtype
, (int) class >= 0, stream
);
675 if (class == all_classes
)
676 fprintf_filtered (stream
, "\n\
677 Type \"help%s\" followed by a class name for a list of commands in that class.",
680 fprintf_filtered (stream
, "\n\
681 Type \"help%s\" followed by %scommand name for full documentation.\n\
682 Command name abbreviations are allowed if unambiguous.\n",
687 help_all (struct ui_file
*stream
)
689 struct cmd_list_element
*c
;
690 extern struct cmd_list_element
*cmdlist
;
692 for (c
= cmdlist
; c
; c
= c
->next
)
696 /* If this is a prefix command, print it's subcommands */
698 help_cmd_list (*c
->prefixlist
, all_commands
, c
->prefixname
, 0, stream
);
700 /* If this is a class name, print all of the commands in the class */
701 else if (c
->func
== NULL
)
702 help_cmd_list (cmdlist
, c
->class, "", 0, stream
);
706 /* Print only the first line of STR on STREAM. */
708 print_doc_line (struct ui_file
*stream
, char *str
)
710 static char *line_buffer
= 0;
711 static int line_size
;
717 line_buffer
= (char *) xmalloc (line_size
);
721 while (*p
&& *p
!= '\n' && *p
!= '.' && *p
!= ',')
723 if (p
- str
> line_size
- 1)
725 line_size
= p
- str
+ 1;
727 line_buffer
= (char *) xmalloc (line_size
);
729 strncpy (line_buffer
, str
, p
- str
);
730 line_buffer
[p
- str
] = '\0';
731 if (islower (line_buffer
[0]))
732 line_buffer
[0] = toupper (line_buffer
[0]);
733 ui_out_text (uiout
, line_buffer
);
737 * Implement a help command on command list LIST.
738 * RECURSE should be non-zero if this should be done recursively on
739 * all sublists of LIST.
740 * PREFIX is the prefix to print before each command name.
741 * STREAM is the stream upon which the output should be written.
743 * A non-negative class number to list only commands in that
745 * ALL_COMMANDS to list all commands in list.
746 * ALL_CLASSES to list all classes in list.
748 * Note that RECURSE will be active on *all* sublists, not just the
749 * ones selected by the criteria above (ie. the selection mechanism
750 * is at the low level, not the high-level).
753 help_cmd_list (struct cmd_list_element
*list
, enum command_class
class,
754 char *prefix
, int recurse
, struct ui_file
*stream
)
756 register struct cmd_list_element
*c
;
758 for (c
= list
; c
; c
= c
->next
)
760 if (c
->abbrev_flag
== 0 &&
761 (class == all_commands
762 || (class == all_classes
&& c
->func
== NULL
)
763 || (class == c
->class && c
->func
!= NULL
)))
765 fprintf_filtered (stream
, "%s%s -- ", prefix
, c
->name
);
766 print_doc_line (stream
, c
->doc
);
767 fputs_filtered ("\n", stream
);
770 && c
->prefixlist
!= 0
771 && c
->abbrev_flag
== 0)
772 help_cmd_list (*c
->prefixlist
, class, c
->prefixname
, 1, stream
);
777 /* Search the input clist for 'command'. Return the command if
778 found (or NULL if not), and return the number of commands
781 static struct cmd_list_element
*
782 find_cmd (char *command
, int len
, struct cmd_list_element
*clist
,
783 int ignore_help_classes
, int *nfound
)
785 struct cmd_list_element
*found
, *c
;
787 found
= (struct cmd_list_element
*) NULL
;
789 for (c
= clist
; c
; c
= c
->next
)
790 if (!strncmp (command
, c
->name
, len
)
791 && (!ignore_help_classes
|| c
->func
))
795 if (c
->name
[len
] == '\0')
804 /* This routine takes a line of TEXT and a CLIST in which to start the
805 lookup. When it returns it will have incremented the text pointer past
806 the section of text it matched, set *RESULT_LIST to point to the list in
807 which the last word was matched, and will return a pointer to the cmd
808 list element which the text matches. It will return NULL if no match at
809 all was possible. It will return -1 (cast appropriately, ick) if ambigous
810 matches are possible; in this case *RESULT_LIST will be set to point to
811 the list in which there are ambiguous choices (and *TEXT will be set to
812 the ambiguous text string).
814 If the located command was an abbreviation, this routine returns the base
815 command of the abbreviation.
817 It does no error reporting whatsoever; control will always return
818 to the superior routine.
820 In the case of an ambiguous return (-1), *RESULT_LIST will be set to point
821 at the prefix_command (ie. the best match) *or* (special case) will be NULL
822 if no prefix command was ever found. For example, in the case of "info a",
823 "info" matches without ambiguity, but "a" could be "args" or "address", so
824 *RESULT_LIST is set to the cmd_list_element for "info". So in this case
825 RESULT_LIST should not be interpeted as a pointer to the beginning of a
826 list; it simply points to a specific command. In the case of an ambiguous
827 return *TEXT is advanced past the last non-ambiguous prefix (e.g.
828 "info t" can be "info types" or "info target"; upon return *TEXT has been
829 advanced past "info ").
831 If RESULT_LIST is NULL, don't set *RESULT_LIST (but don't otherwise
832 affect the operation).
834 This routine does *not* modify the text pointed to by TEXT.
836 If IGNORE_HELP_CLASSES is nonzero, ignore any command list elements which
837 are actually help classes rather than commands (i.e. the function field of
838 the struct cmd_list_element is NULL). */
840 struct cmd_list_element
*
841 lookup_cmd_1 (char **text
, struct cmd_list_element
*clist
,
842 struct cmd_list_element
**result_list
, int ignore_help_classes
)
845 int len
, tmp
, nfound
;
846 struct cmd_list_element
*found
, *c
;
849 while (**text
== ' ' || **text
== '\t')
852 /* Treating underscores as part of command words is important
853 so that "set args_foo()" doesn't get interpreted as
854 "set args _foo()". */
856 *p
&& (isalnum (*p
) || *p
== '-' || *p
== '_' ||
858 (*p
== '+' || *p
== '<' || *p
== '>' || *p
== '$')) ||
859 (xdb_commands
&& (*p
== '!' || *p
== '/' || *p
== '?')));
863 /* If nothing but whitespace, return 0. */
869 /* *text and p now bracket the first command word to lookup (and
870 it's length is len). We copy this into a local temporary */
873 command
= (char *) alloca (len
+ 1);
874 for (tmp
= 0; tmp
< len
; tmp
++)
876 char x
= (*text
)[tmp
];
884 found
= find_cmd (command
, len
, clist
, ignore_help_classes
, &nfound
);
887 ** We didn't find the command in the entered case, so lower case it
890 if (!found
|| nfound
== 0)
892 for (tmp
= 0; tmp
< len
; tmp
++)
894 char x
= command
[tmp
];
895 command
[tmp
] = isupper (x
) ? tolower (x
) : x
;
897 found
= find_cmd (command
, len
, clist
, ignore_help_classes
, &nfound
);
900 /* If nothing matches, we have a simple failure. */
906 if (result_list
!= NULL
)
907 /* Will be modified in calling routine
908 if we know what the prefix command is. */
910 return (struct cmd_list_element
*) -1; /* Ambiguous. */
913 /* We've matched something on this list. Move text pointer forward. */
917 if (found
->cmd_pointer
)
919 /* We drop the alias (abbreviation) in favor of the command it is
920 pointing to. If the alias is deprecated, though, we need to
921 warn the user about it before we drop it. Note that while we
922 are warning about the alias, we may also warn about the command
923 itself and we will adjust the appropriate DEPRECATED_WARN_USER
926 if (found
->flags
& DEPRECATED_WARN_USER
)
927 deprecated_cmd_warning (&line
);
928 found
= found
->cmd_pointer
;
930 /* If we found a prefix command, keep looking. */
932 if (found
->prefixlist
)
934 c
= lookup_cmd_1 (text
, *found
->prefixlist
, result_list
,
935 ignore_help_classes
);
938 /* Didn't find anything; this is as far as we got. */
939 if (result_list
!= NULL
)
940 *result_list
= clist
;
943 else if (c
== (struct cmd_list_element
*) -1)
945 /* We've gotten this far properly, but the next step
946 is ambiguous. We need to set the result list to the best
947 we've found (if an inferior hasn't already set it). */
948 if (result_list
!= NULL
)
950 /* This used to say *result_list = *found->prefixlist
951 If that was correct, need to modify the documentation
952 at the top of this function to clarify what is supposed
954 *result_list
= found
;
965 if (result_list
!= NULL
)
966 *result_list
= clist
;
971 /* All this hair to move the space to the front of cmdtype */
974 undef_cmd_error (char *cmdtype
, char *q
)
976 error ("Undefined %scommand: \"%s\". Try \"help%s%.*s\".",
980 (int) strlen (cmdtype
) - 1,
984 /* Look up the contents of *LINE as a command in the command list LIST.
985 LIST is a chain of struct cmd_list_element's.
986 If it is found, return the struct cmd_list_element for that command
987 and update *LINE to point after the command name, at the first argument.
988 If not found, call error if ALLOW_UNKNOWN is zero
989 otherwise (or if error returns) return zero.
990 Call error if specified command is ambiguous,
991 unless ALLOW_UNKNOWN is negative.
992 CMDTYPE precedes the word "command" in the error message.
994 If INGNORE_HELP_CLASSES is nonzero, ignore any command list
995 elements which are actually help classes rather than commands (i.e.
996 the function field of the struct cmd_list_element is 0). */
998 struct cmd_list_element
*
999 lookup_cmd (char **line
, struct cmd_list_element
*list
, char *cmdtype
,
1000 int allow_unknown
, int ignore_help_classes
)
1002 struct cmd_list_element
*last_list
= 0;
1003 struct cmd_list_element
*c
=
1004 lookup_cmd_1 (line
, list
, &last_list
, ignore_help_classes
);
1006 /* Note: Do not remove trailing whitespace here because this
1007 would be wrong for complete_command. Jim Kingdon */
1014 error ("Lack of needed %scommand", cmdtype
);
1017 char *p
= *line
, *q
;
1019 while (isalnum (*p
) || *p
== '-')
1022 q
= (char *) alloca (p
- *line
+ 1);
1023 strncpy (q
, *line
, p
- *line
);
1024 q
[p
- *line
] = '\0';
1025 undef_cmd_error (cmdtype
, q
);
1031 else if (c
== (struct cmd_list_element
*) -1)
1033 /* Ambigous. Local values should be off prefixlist or called
1035 int local_allow_unknown
= (last_list
? last_list
->allow_unknown
:
1037 char *local_cmdtype
= last_list
? last_list
->prefixname
: cmdtype
;
1038 struct cmd_list_element
*local_list
=
1039 (last_list
? *(last_list
->prefixlist
) : list
);
1041 if (local_allow_unknown
< 0)
1044 return last_list
; /* Found something. */
1046 return 0; /* Found nothing. */
1050 /* Report as error. */
1055 ((*line
)[amb_len
] && (*line
)[amb_len
] != ' '
1056 && (*line
)[amb_len
] != '\t');
1061 for (c
= local_list
; c
; c
= c
->next
)
1062 if (!strncmp (*line
, c
->name
, amb_len
))
1064 if (strlen (ambbuf
) + strlen (c
->name
) + 6 < (int) sizeof ambbuf
)
1066 if (strlen (ambbuf
))
1067 strcat (ambbuf
, ", ");
1068 strcat (ambbuf
, c
->name
);
1072 strcat (ambbuf
, "..");
1076 error ("Ambiguous %scommand \"%s\": %s.", local_cmdtype
,
1078 return 0; /* lint */
1083 /* We've got something. It may still not be what the caller
1084 wants (if this command *needs* a subcommand). */
1085 while (**line
== ' ' || **line
== '\t')
1088 if (c
->prefixlist
&& **line
&& !c
->allow_unknown
)
1089 undef_cmd_error (c
->prefixname
, *line
);
1091 /* Seems to be what he wants. Return it. */
1097 /* We are here presumably because an alias or command in *TEXT is
1098 deprecated and a warning message should be generated. This function
1099 decodes *TEXT and potentially generates a warning message as outlined
1102 Example for 'set endian big' which has a fictitious alias 'seb'.
1104 If alias wasn't used in *TEXT, and the command is deprecated:
1105 "warning: 'set endian big' is deprecated."
1107 If alias was used, and only the alias is deprecated:
1108 "warning: 'seb' an alias for the command 'set endian big' is deprecated."
1110 If alias was used and command is deprecated (regardless of whether the
1111 alias itself is deprecated:
1113 "warning: 'set endian big' (seb) is deprecated."
1115 After the message has been sent, clear the appropriate flags in the
1116 command and/or the alias so the user is no longer bothered.
1120 deprecated_cmd_warning (char **text
)
1122 struct cmd_list_element
*alias
= NULL
;
1123 struct cmd_list_element
*prefix_cmd
= NULL
;
1124 struct cmd_list_element
*cmd
= NULL
;
1125 struct cmd_list_element
*c
;
1128 if (!lookup_cmd_composition (*text
, &alias
, &prefix_cmd
, &cmd
))
1129 /* return if text doesn't evaluate to a command */
1132 if (!((alias
? (alias
->flags
& DEPRECATED_WARN_USER
) : 0)
1133 || (cmd
->flags
& DEPRECATED_WARN_USER
) ) )
1134 /* return if nothing is deprecated */
1137 printf_filtered ("Warning:");
1139 if (alias
&& !(cmd
->flags
& CMD_DEPRECATED
))
1140 printf_filtered (" '%s', an alias for the", alias
->name
);
1142 printf_filtered (" command '");
1145 printf_filtered ("%s", prefix_cmd
->prefixname
);
1147 printf_filtered ("%s", cmd
->name
);
1149 if (alias
&& (cmd
->flags
& CMD_DEPRECATED
))
1150 printf_filtered ("' (%s) is deprecated.\n", alias
->name
);
1152 printf_filtered ("' is deprecated.\n");
1155 /* if it is only the alias that is deprecated, we want to indicate the
1156 new alias, otherwise we'll indicate the new command */
1158 if (alias
&& !(cmd
->flags
& CMD_DEPRECATED
))
1160 if (alias
->replacement
)
1161 printf_filtered ("Use '%s'.\n\n", alias
->replacement
);
1163 printf_filtered ("No alternative known.\n\n");
1167 if (cmd
->replacement
)
1168 printf_filtered ("Use '%s'.\n\n", cmd
->replacement
);
1170 printf_filtered ("No alternative known.\n\n");
1173 /* We've warned you, now we'll keep quiet */
1175 alias
->flags
&= ~DEPRECATED_WARN_USER
;
1177 cmd
->flags
&= ~DEPRECATED_WARN_USER
;
1182 /* Look up the contents of LINE as a command in the command list 'cmdlist'.
1183 Return 1 on success, 0 on failure.
1185 If LINE refers to an alias, *alias will point to that alias.
1187 If LINE is a postfix command (i.e. one that is preceeded by a prefix
1188 command) set *prefix_cmd.
1190 Set *cmd to point to the command LINE indicates.
1192 If any of *alias, *prefix_cmd, or *cmd cannot be determined or do not
1193 exist, they are NULL when we return.
1197 lookup_cmd_composition (char *text
,
1198 struct cmd_list_element
**alias
,
1199 struct cmd_list_element
**prefix_cmd
,
1200 struct cmd_list_element
**cmd
)
1203 int len
, tmp
, nfound
;
1204 struct cmd_list_element
*cur_list
;
1205 struct cmd_list_element
*prev_cmd
;
1214 /* Go through as many command lists as we need to
1215 to find the command TEXT refers to. */
1219 while (*text
== ' ' || *text
== '\t')
1222 /* Treating underscores as part of command words is important
1223 so that "set args_foo()" doesn't get interpreted as
1224 "set args _foo()". */
1226 *p
&& (isalnum (*p
) || *p
== '-' || *p
== '_' ||
1228 (*p
== '+' || *p
== '<' || *p
== '>' || *p
== '$')) ||
1229 (xdb_commands
&& (*p
== '!' || *p
== '/' || *p
== '?')));
1233 /* If nothing but whitespace, return. */
1239 /* text and p now bracket the first command word to lookup (and
1240 it's length is len). We copy this into a local temporary */
1242 command
= (char *) alloca (len
+ 1);
1243 for (tmp
= 0; tmp
< len
; tmp
++)
1248 command
[len
] = '\0';
1253 *cmd
= find_cmd (command
, len
, cur_list
, 1, &nfound
);
1255 /* We didn't find the command in the entered case, so lower case it
1258 if (!*cmd
|| nfound
== 0)
1260 for (tmp
= 0; tmp
< len
; tmp
++)
1262 char x
= command
[tmp
];
1263 command
[tmp
] = isupper (x
) ? tolower (x
) : x
;
1265 *cmd
= find_cmd (command
, len
, cur_list
, 1, &nfound
);
1268 if (*cmd
== (struct cmd_list_element
*) -1)
1270 return 0; /* ambiguous */
1274 return 0; /* nothing found */
1277 if ((*cmd
)->cmd_pointer
)
1279 /* cmd was actually an alias, we note that an alias was used
1280 (by assigning *alais) and we set *cmd.
1283 *cmd
= (*cmd
)->cmd_pointer
;
1285 *prefix_cmd
= prev_cmd
;
1287 if ((*cmd
)->prefixlist
)
1288 cur_list
= *(*cmd
)->prefixlist
;
1296 /* Helper function for SYMBOL_COMPLETION_FUNCTION. */
1298 /* Return a vector of char pointers which point to the different
1299 possible completions in LIST of TEXT.
1301 WORD points in the same buffer as TEXT, and completions should be
1302 returned relative to this position. For example, suppose TEXT is "foo"
1303 and we want to complete to "foobar". If WORD is "oo", return
1304 "oobar"; if WORD is "baz/foo", return "baz/foobar". */
1307 complete_on_cmdlist (struct cmd_list_element
*list
, char *text
, char *word
)
1309 struct cmd_list_element
*ptr
;
1311 int sizeof_matchlist
;
1313 int textlen
= strlen (text
);
1315 sizeof_matchlist
= 10;
1316 matchlist
= (char **) xmalloc (sizeof_matchlist
* sizeof (char *));
1319 for (ptr
= list
; ptr
; ptr
= ptr
->next
)
1320 if (!strncmp (ptr
->name
, text
, textlen
)
1321 && !ptr
->abbrev_flag
1323 || ptr
->prefixlist
))
1325 if (matches
== sizeof_matchlist
)
1327 sizeof_matchlist
*= 2;
1328 matchlist
= (char **) xrealloc ((char *) matchlist
,
1330 * sizeof (char *)));
1333 matchlist
[matches
] = (char *)
1334 xmalloc (strlen (word
) + strlen (ptr
->name
) + 1);
1336 strcpy (matchlist
[matches
], ptr
->name
);
1337 else if (word
> text
)
1339 /* Return some portion of ptr->name. */
1340 strcpy (matchlist
[matches
], ptr
->name
+ (word
- text
));
1344 /* Return some of text plus ptr->name. */
1345 strncpy (matchlist
[matches
], word
, text
- word
);
1346 matchlist
[matches
][text
- word
] = '\0';
1347 strcat (matchlist
[matches
], ptr
->name
);
1359 matchlist
= (char **) xrealloc ((char *) matchlist
, ((matches
+ 1)
1360 * sizeof (char *)));
1361 matchlist
[matches
] = (char *) 0;
1367 /* Helper function for SYMBOL_COMPLETION_FUNCTION. */
1369 /* Return a vector of char pointers which point to the different
1370 possible completions in CMD of TEXT.
1372 WORD points in the same buffer as TEXT, and completions should be
1373 returned relative to this position. For example, suppose TEXT is "foo"
1374 and we want to complete to "foobar". If WORD is "oo", return
1375 "oobar"; if WORD is "baz/foo", return "baz/foobar". */
1378 complete_on_enum (const char *enumlist
[],
1383 int sizeof_matchlist
;
1385 int textlen
= strlen (text
);
1389 sizeof_matchlist
= 10;
1390 matchlist
= (char **) xmalloc (sizeof_matchlist
* sizeof (char *));
1393 for (i
= 0; (name
= enumlist
[i
]) != NULL
; i
++)
1394 if (strncmp (name
, text
, textlen
) == 0)
1396 if (matches
== sizeof_matchlist
)
1398 sizeof_matchlist
*= 2;
1399 matchlist
= (char **) xrealloc ((char *) matchlist
,
1401 * sizeof (char *)));
1404 matchlist
[matches
] = (char *)
1405 xmalloc (strlen (word
) + strlen (name
) + 1);
1407 strcpy (matchlist
[matches
], name
);
1408 else if (word
> text
)
1410 /* Return some portion of name. */
1411 strcpy (matchlist
[matches
], name
+ (word
- text
));
1415 /* Return some of text plus name. */
1416 strncpy (matchlist
[matches
], word
, text
- word
);
1417 matchlist
[matches
][text
- word
] = '\0';
1418 strcat (matchlist
[matches
], name
);
1430 matchlist
= (char **) xrealloc ((char *) matchlist
, ((matches
+ 1)
1431 * sizeof (char *)));
1432 matchlist
[matches
] = (char *) 0;