1 /* Handle lists of commands, their decoding and documentation, for GDB.
2 Copyright 1986, 1989, 1990, 1991, 1998, 2000 Free Software Foundation, Inc.
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place - Suite 330,
17 Boston, MA 02111-1307, USA. */
24 #include "gdb_string.h"
30 #include "gnu-regex.h"
31 /* FIXME: this should be auto-configured! */
36 /* Prototypes for local functions */
38 static void undef_cmd_error
PARAMS ((char *, char *));
40 static void show_user
PARAMS ((char *, int));
42 static void show_user_1 (struct cmd_list_element
*, struct ui_file
*);
44 static void make_command
PARAMS ((char *, int));
46 static void shell_escape
PARAMS ((char *, int));
48 static int parse_binary_operation
PARAMS ((char *));
50 static void print_doc_line (struct ui_file
*, char *);
52 static struct cmd_list_element
*find_cmd
PARAMS ((char *command
,
54 struct cmd_list_element
* clist
,
55 int ignore_help_classes
,
57 static void apropos_cmd_helper (struct ui_file
*, struct cmd_list_element
*,
58 struct re_pattern_buffer
*, char *);
60 void apropos_command (char *, int);
62 void _initialize_command
PARAMS ((void));
64 /* Add element named NAME.
65 CLASS is the top level category into which commands are broken down
67 FUN should be the function to execute the command;
68 it will get a character string as argument, with leading
69 and trailing blanks already eliminated.
71 DOC is a documentation string for the command.
72 Its first line should be a complete sentence.
73 It should start with ? for a command that is an abbreviation
74 or with * for a command that most users don't need to know about.
76 Add this command to command list *LIST.
78 Returns a pointer to the added command (not necessarily the head
81 struct cmd_list_element
*
82 add_cmd (name
, class, fun
, doc
, list
)
84 enum command_class
class;
85 void (*fun
) PARAMS ((char *, int));
87 struct cmd_list_element
**list
;
89 register struct cmd_list_element
*c
90 = (struct cmd_list_element
*) xmalloc (sizeof (struct cmd_list_element
));
91 struct cmd_list_element
*p
;
93 delete_cmd (name
, list
);
95 if (*list
== NULL
|| STRCMP ((*list
)->name
, name
) >= 0)
103 while (p
->next
&& STRCMP (p
->next
->name
, name
) <= 0)
113 c
->function
.cfunc
= fun
;
116 c
->prefixlist
= NULL
;
117 c
->prefixname
= NULL
;
118 c
->allow_unknown
= 0;
120 c
->completer
= make_symbol_completion_list
;
121 c
->type
= not_set_cmd
;
123 c
->var_type
= var_boolean
;
125 c
->user_commands
= NULL
;
127 c
->cmd_pointer
= NULL
;
132 /* Same as above, except that the abbrev_flag is set. */
134 #if 0 /* Currently unused */
136 struct cmd_list_element
*
137 add_abbrev_cmd (name
, class, fun
, doc
, list
)
139 enum command_class
class;
140 void (*fun
) PARAMS ((char *, int));
142 struct cmd_list_element
**list
;
144 register struct cmd_list_element
*c
145 = add_cmd (name
, class, fun
, doc
, list
);
153 struct cmd_list_element
*
154 add_alias_cmd (name
, oldname
, class, abbrev_flag
, list
)
157 enum command_class
class;
159 struct cmd_list_element
**list
;
161 /* Must do this since lookup_cmd tries to side-effect its first arg */
163 register struct cmd_list_element
*old
;
164 register struct cmd_list_element
*c
;
165 copied_name
= (char *) alloca (strlen (oldname
) + 1);
166 strcpy (copied_name
, oldname
);
167 old
= lookup_cmd (&copied_name
, *list
, "", 1, 1);
171 delete_cmd (name
, list
);
175 c
= add_cmd (name
, class, old
->function
.cfunc
, old
->doc
, list
);
176 c
->prefixlist
= old
->prefixlist
;
177 c
->prefixname
= old
->prefixname
;
178 c
->allow_unknown
= old
->allow_unknown
;
179 c
->abbrev_flag
= abbrev_flag
;
180 c
->cmd_pointer
= old
;
184 /* Like add_cmd but adds an element for a command prefix:
185 a name that should be followed by a subcommand to be looked up
186 in another command list. PREFIXLIST should be the address
187 of the variable containing that list. */
189 struct cmd_list_element
*
190 add_prefix_cmd (name
, class, fun
, doc
, prefixlist
, prefixname
,
193 enum command_class
class;
194 void (*fun
) PARAMS ((char *, int));
196 struct cmd_list_element
**prefixlist
;
199 struct cmd_list_element
**list
;
201 register struct cmd_list_element
*c
= add_cmd (name
, class, fun
, doc
, list
);
202 c
->prefixlist
= prefixlist
;
203 c
->prefixname
= prefixname
;
204 c
->allow_unknown
= allow_unknown
;
208 /* Like add_prefix_cmd but sets the abbrev_flag on the new command. */
210 struct cmd_list_element
*
211 add_abbrev_prefix_cmd (name
, class, fun
, doc
, prefixlist
, prefixname
,
214 enum command_class
class;
215 void (*fun
) PARAMS ((char *, int));
217 struct cmd_list_element
**prefixlist
;
220 struct cmd_list_element
**list
;
222 register struct cmd_list_element
*c
= add_cmd (name
, class, fun
, doc
, list
);
223 c
->prefixlist
= prefixlist
;
224 c
->prefixname
= prefixname
;
225 c
->allow_unknown
= allow_unknown
;
230 /* This is an empty "cfunc". */
232 not_just_help_class_command (args
, from_tty
)
238 /* This is an empty "sfunc". */
239 static void empty_sfunc
PARAMS ((char *, int, struct cmd_list_element
*));
242 empty_sfunc (args
, from_tty
, c
)
245 struct cmd_list_element
*c
;
249 /* Add element named NAME to command list LIST (the list for set
250 or some sublist thereof).
251 CLASS is as in add_cmd.
252 VAR_TYPE is the kind of thing we are setting.
253 VAR is address of the variable being controlled by this command.
254 DOC is the documentation string. */
256 struct cmd_list_element
*
257 add_set_cmd (name
, class, var_type
, var
, doc
, list
)
259 enum command_class
class;
263 struct cmd_list_element
**list
;
265 struct cmd_list_element
*c
266 = add_cmd (name
, class, NO_FUNCTION
, doc
, list
);
269 c
->var_type
= var_type
;
271 /* This needs to be something besides NO_FUNCTION so that this isn't
272 treated as a help class. */
273 c
->function
.sfunc
= empty_sfunc
;
277 /* Add element named NAME to command list LIST (the list for set
278 or some sublist thereof).
279 CLASS is as in add_cmd.
280 ENUMLIST is a list of strings which may follow NAME.
281 VAR is address of the variable which will contain the matching string
283 DOC is the documentation string. */
285 struct cmd_list_element
*
286 add_set_enum_cmd (name
, class, enumlist
, var
, doc
, list
)
288 enum command_class
class;
292 struct cmd_list_element
**list
;
294 struct cmd_list_element
*c
295 = add_set_cmd (name
, class, var_enum
, var
, doc
, list
);
301 /* Where SETCMD has already been added, add the corresponding show
302 command to LIST and return a pointer to the added command (not
303 necessarily the head of LIST). */
304 struct cmd_list_element
*
305 add_show_from_set (setcmd
, list
)
306 struct cmd_list_element
*setcmd
;
307 struct cmd_list_element
**list
;
309 struct cmd_list_element
*showcmd
=
310 (struct cmd_list_element
*) xmalloc (sizeof (struct cmd_list_element
));
311 struct cmd_list_element
*p
;
313 memcpy (showcmd
, setcmd
, sizeof (struct cmd_list_element
));
314 delete_cmd (showcmd
->name
, list
);
315 showcmd
->type
= show_cmd
;
317 /* Replace "set " at start of docstring with "show ". */
318 if (setcmd
->doc
[0] == 'S' && setcmd
->doc
[1] == 'e'
319 && setcmd
->doc
[2] == 't' && setcmd
->doc
[3] == ' ')
320 showcmd
->doc
= concat ("Show ", setcmd
->doc
+ 4, NULL
);
322 fprintf_unfiltered (gdb_stderr
, "GDB internal error: Bad docstring for set command\n");
324 if (*list
== NULL
|| STRCMP ((*list
)->name
, showcmd
->name
) >= 0)
326 showcmd
->next
= *list
;
332 while (p
->next
&& STRCMP (p
->next
->name
, showcmd
->name
) <= 0)
336 showcmd
->next
= p
->next
;
343 /* Remove the command named NAME from the command list. */
346 delete_cmd (name
, list
)
348 struct cmd_list_element
**list
;
350 register struct cmd_list_element
*c
;
351 struct cmd_list_element
*p
;
353 while (*list
&& STREQ ((*list
)->name
, name
))
356 (*list
)->hookee
->hook
= 0; /* Hook slips out of its mouth */
363 for (c
= *list
; c
->next
;)
365 if (STREQ (c
->next
->name
, name
))
368 c
->next
->hookee
->hook
= 0; /* hooked cmd gets away. */
370 free ((PTR
) c
->next
);
377 /* Recursively walk the commandlist structures, and print out the
378 documentation of commands that match our regex in either their
379 name, or their documentation.
382 apropos_cmd_helper (struct ui_file
*stream
, struct cmd_list_element
*commandlist
,
383 struct re_pattern_buffer
*regex
, char *prefix
)
385 register struct cmd_list_element
*c
;
386 int returnvalue
=1; /*Needed to avoid double printing*/
387 /* Walk through the commands */
388 for (c
=commandlist
;c
;c
=c
->next
)
392 /* Try to match against the name*/
393 returnvalue
=re_search(regex
,c
->name
,strlen(c
->name
),0,strlen(c
->name
),NULL
);
394 if (returnvalue
>= 0)
396 /* Stolen from help_cmd_list. We don't directly use
397 * help_cmd_list because it doesn't let us print out
400 fprintf_filtered (stream
, "%s%s -- ", prefix
, c
->name
);
401 print_doc_line (stream
, c
->doc
);
402 fputs_filtered ("\n", stream
);
403 returnvalue
=0; /*Set this so we don't print it again.*/
406 if (c
->doc
!= NULL
&& returnvalue
!= 0)
408 /* Try to match against documentation */
409 if (re_search(regex
,c
->doc
,strlen(c
->doc
),0,strlen(c
->doc
),NULL
) >=0)
411 /* Stolen from help_cmd_list. We don't directly use
412 * help_cmd_list because it doesn't let us print out
415 fprintf_filtered (stream
, "%s%s -- ", prefix
, c
->name
);
416 print_doc_line (stream
, c
->doc
);
417 fputs_filtered ("\n", stream
);
420 /* Check if this command has subcommands */
421 if (c
->prefixlist
!= NULL
)
423 /* Recursively call ourselves on the subcommand list,
424 passing the right prefix in.
426 apropos_cmd_helper(stream
,*c
->prefixlist
,regex
,c
->prefixname
);
430 /* Search through names of commands and documentations for a certain
434 apropos_command (char *searchstr
, int from_tty
)
436 extern struct cmd_list_element
*cmdlist
; /*This is the main command list*/
438 char *pattern_fastmap
;
439 char errorbuffer
[512];
440 pattern_fastmap
=calloc(256,sizeof(char));
441 if (searchstr
== NULL
)
442 error("REGEXP string is empty");
444 if (regcomp(&pattern
,searchstr
,REG_ICASE
) == 0)
446 pattern
.fastmap
=pattern_fastmap
;
447 re_compile_fastmap(&pattern
);
448 apropos_cmd_helper(gdb_stdout
,cmdlist
,&pattern
,"");
452 regerror(regcomp(&pattern
,searchstr
,REG_ICASE
),NULL
,errorbuffer
,512);
453 error("Error in regular expression:%s",errorbuffer
);
455 free(pattern_fastmap
);
459 /* This command really has to deal with two things:
460 * 1) I want documentation on *this string* (usually called by
461 * "help commandname").
462 * 2) I want documentation on *this list* (usually called by
463 * giving a command that requires subcommands. Also called by saying
466 * I am going to split this into two seperate comamnds, help_cmd and
471 help_cmd (command
, stream
)
473 struct ui_file
*stream
;
475 struct cmd_list_element
*c
;
476 extern struct cmd_list_element
*cmdlist
;
480 help_list (cmdlist
, "", all_classes
, stream
);
484 c
= lookup_cmd (&command
, cmdlist
, "", 0, 0);
489 /* There are three cases here.
490 If c->prefixlist is nonzero, we have a prefix command.
491 Print its documentation, then list its subcommands.
493 If c->function is nonzero, we really have a command.
494 Print its documentation and return.
496 If c->function is zero, we have a class name.
497 Print its documentation (as if it were a command)
498 and then set class to the number of this class
499 so that the commands in the class will be listed. */
501 fputs_filtered (c
->doc
, stream
);
502 fputs_filtered ("\n", stream
);
504 if (c
->prefixlist
== 0 && c
->function
.cfunc
!= NULL
)
506 fprintf_filtered (stream
, "\n");
508 /* If this is a prefix command, print it's subcommands */
510 help_list (*c
->prefixlist
, c
->prefixname
, all_commands
, stream
);
512 /* If this is a class name, print all of the commands in the class */
513 if (c
->function
.cfunc
== NULL
)
514 help_list (cmdlist
, "", c
->class, stream
);
517 fprintf_filtered (stream
, "\nThis command has a hook defined: %s\n",
522 * Get a specific kind of help on a command list.
525 * CMDTYPE is the prefix to use in the title string.
526 * CLASS is the class with which to list the nodes of this list (see
527 * documentation for help_cmd_list below), As usual, ALL_COMMANDS for
528 * everything, ALL_CLASSES for just classes, and non-negative for only things
529 * in a specific class.
530 * and STREAM is the output stream on which to print things.
531 * If you call this routine with a class >= 0, it recurses.
534 help_list (list
, cmdtype
, class, stream
)
535 struct cmd_list_element
*list
;
537 enum command_class
class;
538 struct ui_file
*stream
;
541 char *cmdtype1
, *cmdtype2
;
543 /* If CMDTYPE is "foo ", CMDTYPE1 gets " foo" and CMDTYPE2 gets "foo sub" */
544 len
= strlen (cmdtype
);
545 cmdtype1
= (char *) alloca (len
+ 1);
547 cmdtype2
= (char *) alloca (len
+ 4);
552 strncpy (cmdtype1
+ 1, cmdtype
, len
- 1);
554 strncpy (cmdtype2
, cmdtype
, len
- 1);
555 strcpy (cmdtype2
+ len
- 1, " sub");
558 if (class == all_classes
)
559 fprintf_filtered (stream
, "List of classes of %scommands:\n\n", cmdtype2
);
561 fprintf_filtered (stream
, "List of %scommands:\n\n", cmdtype2
);
563 help_cmd_list (list
, class, cmdtype
, (int) class >= 0, stream
);
565 if (class == all_classes
)
566 fprintf_filtered (stream
, "\n\
567 Type \"help%s\" followed by a class name for a list of commands in that class.",
570 fprintf_filtered (stream
, "\n\
571 Type \"help%s\" followed by %scommand name for full documentation.\n\
572 Command name abbreviations are allowed if unambiguous.\n",
576 /* Print only the first line of STR on STREAM. */
578 print_doc_line (stream
, str
)
579 struct ui_file
*stream
;
582 static char *line_buffer
= 0;
583 static int line_size
;
589 line_buffer
= (char *) xmalloc (line_size
);
593 while (*p
&& *p
!= '\n' && *p
!= '.' && *p
!= ',')
595 if (p
- str
> line_size
- 1)
597 line_size
= p
- str
+ 1;
598 free ((PTR
) line_buffer
);
599 line_buffer
= (char *) xmalloc (line_size
);
601 strncpy (line_buffer
, str
, p
- str
);
602 line_buffer
[p
- str
] = '\0';
603 if (islower (line_buffer
[0]))
604 line_buffer
[0] = toupper (line_buffer
[0]);
606 ui_out_text (uiout
, line_buffer
);
608 fputs_filtered (line_buffer
, stream
);
613 * Implement a help command on command list LIST.
614 * RECURSE should be non-zero if this should be done recursively on
615 * all sublists of LIST.
616 * PREFIX is the prefix to print before each command name.
617 * STREAM is the stream upon which the output should be written.
619 * A non-negative class number to list only commands in that
621 * ALL_COMMANDS to list all commands in list.
622 * ALL_CLASSES to list all classes in list.
624 * Note that RECURSE will be active on *all* sublists, not just the
625 * ones selected by the criteria above (ie. the selection mechanism
626 * is at the low level, not the high-level).
629 help_cmd_list (list
, class, prefix
, recurse
, stream
)
630 struct cmd_list_element
*list
;
631 enum command_class
class;
634 struct ui_file
*stream
;
636 register struct cmd_list_element
*c
;
638 for (c
= list
; c
; c
= c
->next
)
640 if (c
->abbrev_flag
== 0 &&
641 (class == all_commands
642 || (class == all_classes
&& c
->function
.cfunc
== NULL
)
643 || (class == c
->class && c
->function
.cfunc
!= NULL
)))
645 fprintf_filtered (stream
, "%s%s -- ", prefix
, c
->name
);
646 print_doc_line (stream
, c
->doc
);
647 fputs_filtered ("\n", stream
);
650 && c
->prefixlist
!= 0
651 && c
->abbrev_flag
== 0)
652 help_cmd_list (*c
->prefixlist
, class, c
->prefixname
, 1, stream
);
657 /* Search the input clist for 'command'. Return the command if
658 found (or NULL if not), and return the number of commands
661 static struct cmd_list_element
*
662 find_cmd (command
, len
, clist
, ignore_help_classes
, nfound
)
665 struct cmd_list_element
*clist
;
666 int ignore_help_classes
;
669 struct cmd_list_element
*found
, *c
;
671 found
= (struct cmd_list_element
*) NULL
;
673 for (c
= clist
; c
; c
= c
->next
)
674 if (!strncmp (command
, c
->name
, len
)
675 && (!ignore_help_classes
|| c
->function
.cfunc
))
679 if (c
->name
[len
] == '\0')
688 /* This routine takes a line of TEXT and a CLIST in which to start the
689 lookup. When it returns it will have incremented the text pointer past
690 the section of text it matched, set *RESULT_LIST to point to the list in
691 which the last word was matched, and will return a pointer to the cmd
692 list element which the text matches. It will return NULL if no match at
693 all was possible. It will return -1 (cast appropriately, ick) if ambigous
694 matches are possible; in this case *RESULT_LIST will be set to point to
695 the list in which there are ambiguous choices (and *TEXT will be set to
696 the ambiguous text string).
698 If the located command was an abbreviation, this routine returns the base
699 command of the abbreviation.
701 It does no error reporting whatsoever; control will always return
702 to the superior routine.
704 In the case of an ambiguous return (-1), *RESULT_LIST will be set to point
705 at the prefix_command (ie. the best match) *or* (special case) will be NULL
706 if no prefix command was ever found. For example, in the case of "info a",
707 "info" matches without ambiguity, but "a" could be "args" or "address", so
708 *RESULT_LIST is set to the cmd_list_element for "info". So in this case
709 RESULT_LIST should not be interpeted as a pointer to the beginning of a
710 list; it simply points to a specific command. In the case of an ambiguous
711 return *TEXT is advanced past the last non-ambiguous prefix (e.g.
712 "info t" can be "info types" or "info target"; upon return *TEXT has been
713 advanced past "info ").
715 If RESULT_LIST is NULL, don't set *RESULT_LIST (but don't otherwise
716 affect the operation).
718 This routine does *not* modify the text pointed to by TEXT.
720 If IGNORE_HELP_CLASSES is nonzero, ignore any command list elements which
721 are actually help classes rather than commands (i.e. the function field of
722 the struct cmd_list_element is NULL). */
724 struct cmd_list_element
*
725 lookup_cmd_1 (text
, clist
, result_list
, ignore_help_classes
)
727 struct cmd_list_element
*clist
, **result_list
;
728 int ignore_help_classes
;
731 int len
, tmp
, nfound
;
732 struct cmd_list_element
*found
, *c
;
734 while (**text
== ' ' || **text
== '\t')
737 /* Treating underscores as part of command words is important
738 so that "set args_foo()" doesn't get interpreted as
739 "set args _foo()". */
741 *p
&& (isalnum (*p
) || *p
== '-' || *p
== '_' ||
743 (*p
== '+' || *p
== '<' || *p
== '>' || *p
== '$')) ||
744 (xdb_commands
&& (*p
== '!' || *p
== '/' || *p
== '?')));
748 /* If nothing but whitespace, return 0. */
754 /* *text and p now bracket the first command word to lookup (and
755 it's length is len). We copy this into a local temporary */
758 command
= (char *) alloca (len
+ 1);
759 for (tmp
= 0; tmp
< len
; tmp
++)
761 char x
= (*text
)[tmp
];
769 found
= find_cmd (command
, len
, clist
, ignore_help_classes
, &nfound
);
772 ** We didn't find the command in the entered case, so lower case it
775 if (!found
|| nfound
== 0)
777 for (tmp
= 0; tmp
< len
; tmp
++)
779 char x
= command
[tmp
];
780 command
[tmp
] = isupper (x
) ? tolower (x
) : x
;
782 found
= find_cmd (command
, len
, clist
, ignore_help_classes
, &nfound
);
785 /* If nothing matches, we have a simple failure. */
791 if (result_list
!= NULL
)
792 /* Will be modified in calling routine
793 if we know what the prefix command is. */
795 return (struct cmd_list_element
*) -1; /* Ambiguous. */
798 /* We've matched something on this list. Move text pointer forward. */
802 /* If this was an abbreviation, use the base command instead. */
804 if (found
->cmd_pointer
)
805 found
= found
->cmd_pointer
;
807 /* If we found a prefix command, keep looking. */
809 if (found
->prefixlist
)
811 c
= lookup_cmd_1 (text
, *found
->prefixlist
, result_list
,
812 ignore_help_classes
);
815 /* Didn't find anything; this is as far as we got. */
816 if (result_list
!= NULL
)
817 *result_list
= clist
;
820 else if (c
== (struct cmd_list_element
*) -1)
822 /* We've gotten this far properly, but the next step
823 is ambiguous. We need to set the result list to the best
824 we've found (if an inferior hasn't already set it). */
825 if (result_list
!= NULL
)
827 /* This used to say *result_list = *found->prefixlist
828 If that was correct, need to modify the documentation
829 at the top of this function to clarify what is supposed
831 *result_list
= found
;
842 if (result_list
!= NULL
)
843 *result_list
= clist
;
848 /* All this hair to move the space to the front of cmdtype */
851 undef_cmd_error (cmdtype
, q
)
854 error ("Undefined %scommand: \"%s\". Try \"help%s%.*s\".",
858 strlen (cmdtype
) - 1,
862 /* Look up the contents of *LINE as a command in the command list LIST.
863 LIST is a chain of struct cmd_list_element's.
864 If it is found, return the struct cmd_list_element for that command
865 and update *LINE to point after the command name, at the first argument.
866 If not found, call error if ALLOW_UNKNOWN is zero
867 otherwise (or if error returns) return zero.
868 Call error if specified command is ambiguous,
869 unless ALLOW_UNKNOWN is negative.
870 CMDTYPE precedes the word "command" in the error message.
872 If INGNORE_HELP_CLASSES is nonzero, ignore any command list
873 elements which are actually help classes rather than commands (i.e.
874 the function field of the struct cmd_list_element is 0). */
876 struct cmd_list_element
*
877 lookup_cmd (line
, list
, cmdtype
, allow_unknown
, ignore_help_classes
)
879 struct cmd_list_element
*list
;
882 int ignore_help_classes
;
884 struct cmd_list_element
*last_list
= 0;
885 struct cmd_list_element
*c
=
886 lookup_cmd_1 (line
, list
, &last_list
, ignore_help_classes
);
888 /* This is wrong for complete_command. */
889 char *ptr
= (*line
) + strlen (*line
) - 1;
891 /* Clear off trailing whitespace. */
892 while (ptr
>= *line
&& (*ptr
== ' ' || *ptr
== '\t'))
902 error ("Lack of needed %scommand", cmdtype
);
907 while (isalnum (*p
) || *p
== '-')
910 q
= (char *) alloca (p
- *line
+ 1);
911 strncpy (q
, *line
, p
- *line
);
913 undef_cmd_error (cmdtype
, q
);
919 else if (c
== (struct cmd_list_element
*) -1)
921 /* Ambigous. Local values should be off prefixlist or called
923 int local_allow_unknown
= (last_list
? last_list
->allow_unknown
:
925 char *local_cmdtype
= last_list
? last_list
->prefixname
: cmdtype
;
926 struct cmd_list_element
*local_list
=
927 (last_list
? *(last_list
->prefixlist
) : list
);
929 if (local_allow_unknown
< 0)
932 return last_list
; /* Found something. */
934 return 0; /* Found nothing. */
938 /* Report as error. */
943 ((*line
)[amb_len
] && (*line
)[amb_len
] != ' '
944 && (*line
)[amb_len
] != '\t');
949 for (c
= local_list
; c
; c
= c
->next
)
950 if (!strncmp (*line
, c
->name
, amb_len
))
952 if (strlen (ambbuf
) + strlen (c
->name
) + 6 < (int) sizeof ambbuf
)
955 strcat (ambbuf
, ", ");
956 strcat (ambbuf
, c
->name
);
960 strcat (ambbuf
, "..");
964 error ("Ambiguous %scommand \"%s\": %s.", local_cmdtype
,
971 /* We've got something. It may still not be what the caller
972 wants (if this command *needs* a subcommand). */
973 while (**line
== ' ' || **line
== '\t')
976 if (c
->prefixlist
&& **line
&& !c
->allow_unknown
)
977 undef_cmd_error (c
->prefixname
, *line
);
979 /* Seems to be what he wants. Return it. */
986 /* Look up the contents of *LINE as a command in the command list LIST.
987 LIST is a chain of struct cmd_list_element's.
988 If it is found, return the struct cmd_list_element for that command
989 and update *LINE to point after the command name, at the first argument.
990 If not found, call error if ALLOW_UNKNOWN is zero
991 otherwise (or if error returns) return zero.
992 Call error if specified command is ambiguous,
993 unless ALLOW_UNKNOWN is negative.
994 CMDTYPE precedes the word "command" in the error message. */
996 struct cmd_list_element
*
997 lookup_cmd (line
, list
, cmdtype
, allow_unknown
)
999 struct cmd_list_element
*list
;
1004 register struct cmd_list_element
*c
, *found
;
1007 char *processed_cmd
;
1010 /* Skip leading whitespace. */
1012 while (**line
== ' ' || **line
== '\t')
1015 /* Clear out trailing whitespace. */
1017 p
= *line
+ strlen (*line
);
1018 while (p
!= *line
&& (p
[-1] == ' ' || p
[-1] == '\t'))
1022 /* Find end of command name. */
1025 while (*p
== '-' || isalnum (*p
))
1028 /* Look up the command name.
1029 If exact match, keep that.
1030 Otherwise, take command abbreviated, if unique. Note that (in my
1031 opinion) a null string does *not* indicate ambiguity; simply the
1032 end of the argument. */
1037 error ("Lack of needed %scommand", cmdtype
);
1041 /* Copy over to a local buffer, converting to lowercase on the way.
1042 This is in case the command being parsed is a subcommand which
1043 doesn't match anything, and that's ok. We want the original
1044 untouched for the routine of the original command. */
1046 processed_cmd
= (char *) alloca (p
- *line
+ 1);
1047 for (cmd_len
= 0; cmd_len
< p
- *line
; cmd_len
++)
1049 char x
= (*line
)[cmd_len
];
1051 processed_cmd
[cmd_len
] = tolower (x
);
1053 processed_cmd
[cmd_len
] = x
;
1055 processed_cmd
[cmd_len
] = '\0';
1057 /* Check all possibilities in the current command list. */
1060 for (c
= list
; c
; c
= c
->next
)
1062 if (!strncmp (processed_cmd
, c
->name
, cmd_len
))
1066 if (c
->name
[cmd_len
] == 0)
1074 /* Report error for undefined command name. */
1078 if (nfound
> 1 && allow_unknown
>= 0)
1081 for (c
= list
; c
; c
= c
->next
)
1082 if (!strncmp (processed_cmd
, c
->name
, cmd_len
))
1084 if (strlen (ambbuf
) + strlen (c
->name
) + 6 < sizeof ambbuf
)
1086 if (strlen (ambbuf
))
1087 strcat (ambbuf
, ", ");
1088 strcat (ambbuf
, c
->name
);
1092 strcat (ambbuf
, "..");
1096 error ("Ambiguous %scommand \"%s\": %s.", cmdtype
,
1097 processed_cmd
, ambbuf
);
1099 else if (!allow_unknown
)
1100 error ("Undefined %scommand: \"%s\".", cmdtype
, processed_cmd
);
1104 /* Skip whitespace before the argument. */
1106 while (*p
== ' ' || *p
== '\t')
1110 if (found
->prefixlist
&& *p
)
1112 c
= lookup_cmd (line
, *found
->prefixlist
, found
->prefixname
,
1113 found
->allow_unknown
);
1122 /* Helper function for SYMBOL_COMPLETION_FUNCTION. */
1124 /* Return a vector of char pointers which point to the different
1125 possible completions in LIST of TEXT.
1127 WORD points in the same buffer as TEXT, and completions should be
1128 returned relative to this position. For example, suppose TEXT is "foo"
1129 and we want to complete to "foobar". If WORD is "oo", return
1130 "oobar"; if WORD is "baz/foo", return "baz/foobar". */
1133 complete_on_cmdlist (list
, text
, word
)
1134 struct cmd_list_element
*list
;
1138 struct cmd_list_element
*ptr
;
1140 int sizeof_matchlist
;
1142 int textlen
= strlen (text
);
1144 sizeof_matchlist
= 10;
1145 matchlist
= (char **) xmalloc (sizeof_matchlist
* sizeof (char *));
1148 for (ptr
= list
; ptr
; ptr
= ptr
->next
)
1149 if (!strncmp (ptr
->name
, text
, textlen
)
1150 && !ptr
->abbrev_flag
1151 && (ptr
->function
.cfunc
1152 || ptr
->prefixlist
))
1154 if (matches
== sizeof_matchlist
)
1156 sizeof_matchlist
*= 2;
1157 matchlist
= (char **) xrealloc ((char *) matchlist
,
1159 * sizeof (char *)));
1162 matchlist
[matches
] = (char *)
1163 xmalloc (strlen (word
) + strlen (ptr
->name
) + 1);
1165 strcpy (matchlist
[matches
], ptr
->name
);
1166 else if (word
> text
)
1168 /* Return some portion of ptr->name. */
1169 strcpy (matchlist
[matches
], ptr
->name
+ (word
- text
));
1173 /* Return some of text plus ptr->name. */
1174 strncpy (matchlist
[matches
], word
, text
- word
);
1175 matchlist
[matches
][text
- word
] = '\0';
1176 strcat (matchlist
[matches
], ptr
->name
);
1183 free ((PTR
) matchlist
);
1188 matchlist
= (char **) xrealloc ((char *) matchlist
, ((matches
+ 1)
1189 * sizeof (char *)));
1190 matchlist
[matches
] = (char *) 0;
1196 /* Helper function for SYMBOL_COMPLETION_FUNCTION. */
1198 /* Return a vector of char pointers which point to the different
1199 possible completions in CMD of TEXT.
1201 WORD points in the same buffer as TEXT, and completions should be
1202 returned relative to this position. For example, suppose TEXT is "foo"
1203 and we want to complete to "foobar". If WORD is "oo", return
1204 "oobar"; if WORD is "baz/foo", return "baz/foobar". */
1207 complete_on_enum (enumlist
, text
, word
)
1213 int sizeof_matchlist
;
1215 int textlen
= strlen (text
);
1219 sizeof_matchlist
= 10;
1220 matchlist
= (char **) xmalloc (sizeof_matchlist
* sizeof (char *));
1223 for (i
= 0; (name
= enumlist
[i
]) != NULL
; i
++)
1224 if (strncmp (name
, text
, textlen
) == 0)
1226 if (matches
== sizeof_matchlist
)
1228 sizeof_matchlist
*= 2;
1229 matchlist
= (char **) xrealloc ((char *) matchlist
,
1231 * sizeof (char *)));
1234 matchlist
[matches
] = (char *)
1235 xmalloc (strlen (word
) + strlen (name
) + 1);
1237 strcpy (matchlist
[matches
], name
);
1238 else if (word
> text
)
1240 /* Return some portion of name. */
1241 strcpy (matchlist
[matches
], name
+ (word
- text
));
1245 /* Return some of text plus name. */
1246 strncpy (matchlist
[matches
], word
, text
- word
);
1247 matchlist
[matches
][text
- word
] = '\0';
1248 strcat (matchlist
[matches
], name
);
1255 free ((PTR
) matchlist
);
1260 matchlist
= (char **) xrealloc ((char *) matchlist
, ((matches
+ 1)
1261 * sizeof (char *)));
1262 matchlist
[matches
] = (char *) 0;
1269 parse_binary_operation (arg
)
1277 length
= strlen (arg
);
1279 while (arg
[length
- 1] == ' ' || arg
[length
- 1] == '\t')
1282 if (!strncmp (arg
, "on", length
)
1283 || !strncmp (arg
, "1", length
)
1284 || !strncmp (arg
, "yes", length
))
1286 else if (!strncmp (arg
, "off", length
)
1287 || !strncmp (arg
, "0", length
)
1288 || !strncmp (arg
, "no", length
))
1292 error ("\"on\" or \"off\" expected.");
1297 /* Do a "set" or "show" command. ARG is NULL if no argument, or the text
1298 of the argument, and FROM_TTY is nonzero if this command is being entered
1299 directly by the user (i.e. these are just like any other
1300 command). C is the command list element for the command. */
1302 do_setshow_command (arg
, from_tty
, c
)
1305 struct cmd_list_element
*c
;
1307 if (c
->type
== set_cmd
)
1309 switch (c
->var_type
)
1320 new = (char *) xmalloc (strlen (arg
) + 2);
1323 while ((ch
= *p
++) != '\000')
1327 /* \ at end of argument is used after spaces
1328 so they won't be lost. */
1329 /* This is obsolete now that we no longer strip
1330 trailing whitespace and actually, the backslash
1331 didn't get here in my test, readline or
1332 something did something funky with a backslash
1333 right before a newline. */
1336 ch
= parse_escape (&p
);
1338 break; /* C loses */
1346 if (*(p
- 1) != '\\')
1350 new = (char *) xrealloc (new, q
- new);
1351 if (*(char **) c
->var
!= NULL
)
1352 free (*(char **) c
->var
);
1353 *(char **) c
->var
= new;
1356 case var_string_noescape
:
1359 if (*(char **) c
->var
!= NULL
)
1360 free (*(char **) c
->var
);
1361 *(char **) c
->var
= savestring (arg
, strlen (arg
));
1365 error_no_arg ("filename to set it to.");
1366 if (*(char **) c
->var
!= NULL
)
1367 free (*(char **) c
->var
);
1368 *(char **) c
->var
= tilde_expand (arg
);
1371 *(int *) c
->var
= parse_binary_operation (arg
);
1375 error_no_arg ("integer to set it to.");
1376 *(unsigned int *) c
->var
= parse_and_eval_address (arg
);
1377 if (*(unsigned int *) c
->var
== 0)
1378 *(unsigned int *) c
->var
= UINT_MAX
;
1384 error_no_arg ("integer to set it to.");
1385 val
= parse_and_eval_address (arg
);
1387 *(int *) c
->var
= INT_MAX
;
1388 else if (val
>= INT_MAX
)
1389 error ("integer %u out of range", val
);
1391 *(int *) c
->var
= val
;
1396 error_no_arg ("integer to set it to.");
1397 *(int *) c
->var
= parse_and_eval_address (arg
);
1407 /* if no argument was supplied, print an informative error message */
1411 strcpy (msg
, "Requires an argument. Valid arguments are ");
1412 for (i
= 0; c
->enums
[i
]; i
++)
1416 strcat (msg
, c
->enums
[i
]);
1422 p
= strchr (arg
, ' ');
1430 for (i
= 0; c
->enums
[i
]; i
++)
1431 if (strncmp (arg
, c
->enums
[i
], len
) == 0)
1433 match
= c
->enums
[i
];
1438 error ("Undefined item: \"%s\".", arg
);
1441 error ("Ambiguous item \"%s\".", arg
);
1443 *(char **) c
->var
= match
;
1447 error ("gdb internal error: bad var_type in do_setshow_command");
1450 else if (c
->type
== show_cmd
)
1453 struct cleanup
*old_chain
;
1454 struct ui_stream
*stb
;
1457 stb
= ui_out_stream_new (uiout
);
1458 old_chain
= make_cleanup ((make_cleanup_func
) ui_out_stream_delete
, stb
);
1461 /* Print doc minus "show" at start. */
1462 print_doc_line (gdb_stdout
, c
->doc
+ 5);
1465 ui_out_text (uiout
, " is ");
1466 ui_out_wrap_hint (uiout
, " ");
1468 switch (c
->var_type
)
1474 if (*(unsigned char **) c
->var
)
1475 fputstr_filtered (*(unsigned char **) c
->var
, '"', stb
->stream
);
1479 case var_string_noescape
:
1482 if (*(char **) c
->var
)
1483 fputs_filtered (*(char **) c
->var
, stb
->stream
);
1487 fputs_filtered (*(int *) c
->var
? "on" : "off", stb
->stream
);
1490 if (*(unsigned int *) c
->var
== UINT_MAX
)
1492 fputs_filtered ("unlimited", stb
->stream
);
1495 /* else fall through */
1497 fprintf_filtered (stb
->stream
, "%u", *(unsigned int *) c
->var
);
1500 if (*(int *) c
->var
== INT_MAX
)
1502 fputs_filtered ("unlimited", stb
->stream
);
1505 fprintf_filtered (stb
->stream
, "%d", *(int *) c
->var
);
1509 error ("gdb internal error: bad var_type in do_setshow_command");
1512 ui_out_text (uiout
, "\"");
1513 ui_out_field_stream (uiout
, "value", stb
);
1515 ui_out_text (uiout
, "\"");
1516 ui_out_text (uiout
, ".\n");
1517 do_cleanups (old_chain
);
1519 fputs_filtered (" is ", gdb_stdout
);
1521 switch (c
->var_type
)
1525 fputs_filtered ("\"", gdb_stdout
);
1526 if (*(unsigned char **) c
->var
)
1527 fputstr_filtered (*(unsigned char **) c
->var
, '"', gdb_stdout
);
1528 fputs_filtered ("\"", gdb_stdout
);
1531 case var_string_noescape
:
1534 fputs_filtered ("\"", gdb_stdout
);
1535 if (*(char **) c
->var
)
1536 fputs_filtered (*(char **) c
->var
, gdb_stdout
);
1537 fputs_filtered ("\"", gdb_stdout
);
1540 fputs_filtered (*(int *) c
->var
? "on" : "off", gdb_stdout
);
1543 if (*(unsigned int *) c
->var
== UINT_MAX
)
1545 fputs_filtered ("unlimited", gdb_stdout
);
1548 /* else fall through */
1550 fprintf_filtered (gdb_stdout
, "%u", *(unsigned int *) c
->var
);
1553 if (*(int *) c
->var
== INT_MAX
)
1555 fputs_filtered ("unlimited", gdb_stdout
);
1558 fprintf_filtered (gdb_stdout
, "%d", *(int *) c
->var
);
1562 error ("gdb internal error: bad var_type in do_setshow_command");
1564 fputs_filtered (".\n", gdb_stdout
);
1568 error ("gdb internal error: bad cmd_type in do_setshow_command");
1569 (*c
->function
.sfunc
) (NULL
, from_tty
, c
);
1570 if (c
->type
== set_cmd
&& set_hook
)
1574 /* Show all the settings in a list of show commands. */
1577 cmd_show_list (list
, from_tty
, prefix
)
1578 struct cmd_list_element
*list
;
1583 ui_out_list_begin (uiout
, "showlist");
1585 for (; list
!= NULL
; list
= list
->next
)
1587 /* If we find a prefix, run its list, prefixing our output by its
1588 prefix (with "show " skipped). */
1590 if (list
->prefixlist
&& !list
->abbrev_flag
)
1592 ui_out_list_begin (uiout
, "optionlist");
1593 ui_out_field_string (uiout
, "prefix", list
->prefixname
+ 5);
1594 cmd_show_list (*list
->prefixlist
, from_tty
, list
->prefixname
+ 5);
1595 ui_out_list_end (uiout
);
1597 if (list
->type
== show_cmd
)
1599 ui_out_list_begin (uiout
, "option");
1600 ui_out_text (uiout
, prefix
);
1601 ui_out_field_string (uiout
, "name", list
->name
);
1602 ui_out_text (uiout
, ": ");
1603 do_setshow_command ((char *) NULL
, from_tty
, list
);
1604 ui_out_list_end (uiout
);
1607 if (list
->prefixlist
&& !list
->abbrev_flag
)
1608 cmd_show_list (*list
->prefixlist
, from_tty
, list
->prefixname
+ 5);
1609 if (list
->type
== show_cmd
)
1611 fputs_filtered (prefix
, gdb_stdout
);
1612 fputs_filtered (list
->name
, gdb_stdout
);
1613 fputs_filtered (": ", gdb_stdout
);
1614 do_setshow_command ((char *) NULL
, from_tty
, list
);
1619 ui_out_list_end (uiout
);
1625 shell_escape (arg
, from_tty
)
1630 /* If ARG is NULL, they want an inferior shell, but `system' just
1631 reports if the shell is available when passed a NULL arg. */
1632 int rc
= system (arg
? arg
: "");
1635 arg
= "inferior shell";
1639 fprintf_unfiltered (gdb_stderr
, "Cannot execute %s: %s\n", arg
,
1640 safe_strerror (errno
));
1641 gdb_flush (gdb_stderr
);
1645 fprintf_unfiltered (gdb_stderr
, "%s exited with status %d\n", arg
, rc
);
1646 gdb_flush (gdb_stderr
);
1649 /* Make sure to return to the directory GDB thinks it is, in case the
1650 shell command we just ran changed it. */
1651 chdir (current_directory
);
1653 #else /* Can fork. */
1654 int rc
, status
, pid
;
1655 char *p
, *user_shell
;
1657 if ((user_shell
= (char *) getenv ("SHELL")) == NULL
)
1658 user_shell
= "/bin/sh";
1660 /* Get the name of the shell for arg0 */
1661 if ((p
= strrchr (user_shell
, '/')) == NULL
)
1664 p
++; /* Get past '/' */
1666 if ((pid
= fork ()) == 0)
1669 execl (user_shell
, p
, 0);
1671 execl (user_shell
, p
, "-c", arg
, 0);
1673 fprintf_unfiltered (gdb_stderr
, "Cannot execute %s: %s\n", user_shell
,
1674 safe_strerror (errno
));
1675 gdb_flush (gdb_stderr
);
1680 while ((rc
= wait (&status
)) != pid
&& rc
!= -1)
1683 error ("Fork failed");
1684 #endif /* Can fork. */
1688 make_command (arg
, from_tty
)
1698 p
= xmalloc (sizeof ("make ") + strlen (arg
));
1699 strcpy (p
, "make ");
1700 strcpy (p
+ sizeof ("make ") - 1, arg
);
1703 shell_escape (p
, from_tty
);
1707 show_user_1 (c
, stream
)
1708 struct cmd_list_element
*c
;
1709 struct ui_file
*stream
;
1711 register struct command_line
*cmdlines
;
1713 cmdlines
= c
->user_commands
;
1716 fputs_filtered ("User command ", stream
);
1717 fputs_filtered (c
->name
, stream
);
1718 fputs_filtered (":\n", stream
);
1721 print_command_lines (uiout
, cmdlines
, 1);
1722 fputs_filtered ("\n", stream
);
1726 print_command_line (cmdlines
, 4, stream
);
1727 cmdlines
= cmdlines
->next
;
1729 fputs_filtered ("\n", stream
);
1735 show_user (args
, from_tty
)
1739 struct cmd_list_element
*c
;
1740 extern struct cmd_list_element
*cmdlist
;
1744 c
= lookup_cmd (&args
, cmdlist
, "", 0, 1);
1745 if (c
->class != class_user
)
1746 error ("Not a user command.");
1747 show_user_1 (c
, gdb_stdout
);
1751 for (c
= cmdlist
; c
; c
= c
->next
)
1753 if (c
->class == class_user
)
1754 show_user_1 (c
, gdb_stdout
);
1760 _initialize_command ()
1762 add_com ("shell", class_support
, shell_escape
,
1763 "Execute the rest of the line as a shell command. \n\
1764 With no arguments, run an inferior shell.");
1766 /* NOTE: cagney/2000-03-20: Being able to enter ``(gdb) !ls'' would
1767 be a really useful feature. Unfortunatly, the below wont do
1768 this. Instead it adds support for the form ``(gdb) ! ls''
1769 (i.e. the space is required). If the ``!'' command below is
1770 added the complains about no ``!'' command would be replaced by
1771 complains about how the ``!'' command is broken :-) */
1773 add_com_alias ("!", "shell", class_support
, 0);
1775 add_com ("make", class_support
, make_command
,
1776 "Run the ``make'' program using the rest of the line as arguments.");
1777 add_cmd ("user", no_class
, show_user
,
1778 "Show definitions of user defined commands.\n\
1779 Argument is the name of the user defined command.\n\
1780 With no argument, show definitions of all user defined commands.", &showlist
);
1781 add_com ("apropos", class_support
, apropos_command
, "Search for commands matching a REGEXP");