1 /* Handle lists of commands, their decoding and documentation, for GDB.
2 Copyright 1986, 1989, 1990, 1991, 1998 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"
29 #ifdef HAVE_SYS_WAIT_H
36 /* FIXME: this should be auto-configured! */
41 /* Prototypes for local functions */
43 static void undef_cmd_error
PARAMS ((char *, char *));
45 static void show_user
PARAMS ((char *, int));
47 static void show_user_1
PARAMS ((struct cmd_list_element
*, GDB_FILE
*));
49 static void make_command
PARAMS ((char *, int));
51 static void shell_escape
PARAMS ((char *, int));
53 static int parse_binary_operation
PARAMS ((char *));
55 static void print_doc_line
PARAMS ((GDB_FILE
*, char *));
57 static struct cmd_list_element
*find_cmd
PARAMS ((char *command
,
59 struct cmd_list_element
* clist
,
60 int ignore_help_classes
,
63 void _initialize_command
PARAMS ((void));
65 /* Add element named NAME.
66 CLASS is the top level category into which commands are broken down
68 FUN should be the function to execute the command;
69 it will get a character string as argument, with leading
70 and trailing blanks already eliminated.
72 DOC is a documentation string for the command.
73 Its first line should be a complete sentence.
74 It should start with ? for a command that is an abbreviation
75 or with * for a command that most users don't need to know about.
77 Add this command to command list *LIST.
79 Returns a pointer to the added command (not necessarily the head
82 struct cmd_list_element
*
83 add_cmd (name
, class, fun
, doc
, list
)
85 enum command_class
class;
86 void (*fun
) PARAMS ((char *, int));
88 struct cmd_list_element
**list
;
90 register struct cmd_list_element
*c
91 = (struct cmd_list_element
*) xmalloc (sizeof (struct cmd_list_element
));
92 struct cmd_list_element
*p
;
94 delete_cmd (name
, list
);
96 if (*list
== NULL
|| STRCMP ((*list
)->name
, name
) >= 0)
104 while (p
->next
&& STRCMP (p
->next
->name
, name
) <= 0)
114 c
->function
.cfunc
= fun
;
117 c
->prefixlist
= NULL
;
118 c
->prefixname
= NULL
;
119 c
->allow_unknown
= 0;
121 c
->completer
= make_symbol_completion_list
;
122 c
->type
= not_set_cmd
;
124 c
->var_type
= var_boolean
;
126 c
->user_commands
= NULL
;
128 c
->cmd_pointer
= NULL
;
133 /* Same as above, except that the abbrev_flag is set. */
135 #if 0 /* Currently unused */
137 struct cmd_list_element
*
138 add_abbrev_cmd (name
, class, fun
, doc
, list
)
140 enum command_class
class;
141 void (*fun
) PARAMS ((char *, int));
143 struct cmd_list_element
**list
;
145 register struct cmd_list_element
*c
146 = add_cmd (name
, class, fun
, doc
, list
);
154 struct cmd_list_element
*
155 add_alias_cmd (name
, oldname
, class, abbrev_flag
, list
)
158 enum command_class
class;
160 struct cmd_list_element
**list
;
162 /* Must do this since lookup_cmd tries to side-effect its first arg */
164 register struct cmd_list_element
*old
;
165 register struct cmd_list_element
*c
;
166 copied_name
= (char *) alloca (strlen (oldname
) + 1);
167 strcpy (copied_name
, oldname
);
168 old
= lookup_cmd (&copied_name
, *list
, "", 1, 1);
172 delete_cmd (name
, list
);
176 c
= add_cmd (name
, class, old
->function
.cfunc
, old
->doc
, list
);
177 c
->prefixlist
= old
->prefixlist
;
178 c
->prefixname
= old
->prefixname
;
179 c
->allow_unknown
= old
->allow_unknown
;
180 c
->abbrev_flag
= abbrev_flag
;
181 c
->cmd_pointer
= old
;
185 /* Like add_cmd but adds an element for a command prefix:
186 a name that should be followed by a subcommand to be looked up
187 in another command list. PREFIXLIST should be the address
188 of the variable containing that list. */
190 struct cmd_list_element
*
191 add_prefix_cmd (name
, class, fun
, doc
, prefixlist
, prefixname
,
194 enum command_class
class;
195 void (*fun
) PARAMS ((char *, int));
197 struct cmd_list_element
**prefixlist
;
200 struct cmd_list_element
**list
;
202 register struct cmd_list_element
*c
= add_cmd (name
, class, fun
, doc
, list
);
203 c
->prefixlist
= prefixlist
;
204 c
->prefixname
= prefixname
;
205 c
->allow_unknown
= allow_unknown
;
209 /* Like add_prefix_cmd but sets the abbrev_flag on the new command. */
211 struct cmd_list_element
*
212 add_abbrev_prefix_cmd (name
, class, fun
, doc
, prefixlist
, prefixname
,
215 enum command_class
class;
216 void (*fun
) PARAMS ((char *, int));
218 struct cmd_list_element
**prefixlist
;
221 struct cmd_list_element
**list
;
223 register struct cmd_list_element
*c
= add_cmd (name
, class, fun
, doc
, list
);
224 c
->prefixlist
= prefixlist
;
225 c
->prefixname
= prefixname
;
226 c
->allow_unknown
= allow_unknown
;
231 /* This is an empty "cfunc". */
233 not_just_help_class_command (args
, from_tty
)
239 /* This is an empty "sfunc". */
240 static void empty_sfunc
PARAMS ((char *, int, struct cmd_list_element
*));
243 empty_sfunc (args
, from_tty
, c
)
246 struct cmd_list_element
*c
;
250 /* Add element named NAME to command list LIST (the list for set
251 or some sublist thereof).
252 CLASS is as in add_cmd.
253 VAR_TYPE is the kind of thing we are setting.
254 VAR is address of the variable being controlled by this command.
255 DOC is the documentation string. */
257 struct cmd_list_element
*
258 add_set_cmd (name
, class, var_type
, var
, doc
, list
)
260 enum command_class
class;
264 struct cmd_list_element
**list
;
266 struct cmd_list_element
*c
267 = add_cmd (name
, class, NO_FUNCTION
, doc
, list
);
270 c
->var_type
= var_type
;
272 /* This needs to be something besides NO_FUNCTION so that this isn't
273 treated as a help class. */
274 c
->function
.sfunc
= empty_sfunc
;
278 /* Add element named NAME to command list LIST (the list for set
279 or some sublist thereof).
280 CLASS is as in add_cmd.
281 ENUMLIST is a list of strings which may follow NAME.
282 VAR is address of the variable which will contain the matching string
284 DOC is the documentation string. */
286 struct cmd_list_element
*
287 add_set_enum_cmd (name
, class, enumlist
, var
, doc
, list
)
289 enum command_class
class;
293 struct cmd_list_element
**list
;
295 struct cmd_list_element
*c
296 = add_set_cmd (name
, class, var_enum
, var
, doc
, list
);
302 /* Where SETCMD has already been added, add the corresponding show
303 command to LIST and return a pointer to the added command (not
304 necessarily the head of LIST). */
305 struct cmd_list_element
*
306 add_show_from_set (setcmd
, list
)
307 struct cmd_list_element
*setcmd
;
308 struct cmd_list_element
**list
;
310 struct cmd_list_element
*showcmd
=
311 (struct cmd_list_element
*) xmalloc (sizeof (struct cmd_list_element
));
312 struct cmd_list_element
*p
;
314 memcpy (showcmd
, setcmd
, sizeof (struct cmd_list_element
));
315 delete_cmd (showcmd
->name
, list
);
316 showcmd
->type
= show_cmd
;
318 /* Replace "set " at start of docstring with "show ". */
319 if (setcmd
->doc
[0] == 'S' && setcmd
->doc
[1] == 'e'
320 && setcmd
->doc
[2] == 't' && setcmd
->doc
[3] == ' ')
321 showcmd
->doc
= concat ("Show ", setcmd
->doc
+ 4, NULL
);
323 fprintf_unfiltered (gdb_stderr
, "GDB internal error: Bad docstring for set command\n");
325 if (*list
== NULL
|| STRCMP ((*list
)->name
, showcmd
->name
) >= 0)
327 showcmd
->next
= *list
;
333 while (p
->next
&& STRCMP (p
->next
->name
, showcmd
->name
) <= 0)
337 showcmd
->next
= p
->next
;
344 /* Remove the command named NAME from the command list. */
347 delete_cmd (name
, list
)
349 struct cmd_list_element
**list
;
351 register struct cmd_list_element
*c
;
352 struct cmd_list_element
*p
;
354 while (*list
&& STREQ ((*list
)->name
, name
))
357 (*list
)->hookee
->hook
= 0; /* Hook slips out of its mouth */
364 for (c
= *list
; c
->next
;)
366 if (STREQ (c
->next
->name
, name
))
369 c
->next
->hookee
->hook
= 0; /* hooked cmd gets away. */
371 free ((PTR
) c
->next
);
379 /* This command really has to deal with two things:
380 * 1) I want documentation on *this string* (usually called by
381 * "help commandname").
382 * 2) I want documentation on *this list* (usually called by
383 * giving a command that requires subcommands. Also called by saying
386 * I am going to split this into two seperate comamnds, help_cmd and
391 help_cmd (command
, stream
)
395 struct cmd_list_element
*c
;
396 extern struct cmd_list_element
*cmdlist
;
400 help_list (cmdlist
, "", all_classes
, stream
);
404 c
= lookup_cmd (&command
, cmdlist
, "", 0, 0);
409 /* There are three cases here.
410 If c->prefixlist is nonzero, we have a prefix command.
411 Print its documentation, then list its subcommands.
413 If c->function is nonzero, we really have a command.
414 Print its documentation and return.
416 If c->function is zero, we have a class name.
417 Print its documentation (as if it were a command)
418 and then set class to the number of this class
419 so that the commands in the class will be listed. */
421 fputs_filtered (c
->doc
, stream
);
422 fputs_filtered ("\n", stream
);
424 if (c
->prefixlist
== 0 && c
->function
.cfunc
!= NULL
)
426 fprintf_filtered (stream
, "\n");
428 /* If this is a prefix command, print it's subcommands */
430 help_list (*c
->prefixlist
, c
->prefixname
, all_commands
, stream
);
432 /* If this is a class name, print all of the commands in the class */
433 if (c
->function
.cfunc
== NULL
)
434 help_list (cmdlist
, "", c
->class, stream
);
437 fprintf_filtered (stream
, "\nThis command has a hook defined: %s\n",
442 * Get a specific kind of help on a command list.
445 * CMDTYPE is the prefix to use in the title string.
446 * CLASS is the class with which to list the nodes of this list (see
447 * documentation for help_cmd_list below), As usual, ALL_COMMANDS for
448 * everything, ALL_CLASSES for just classes, and non-negative for only things
449 * in a specific class.
450 * and STREAM is the output stream on which to print things.
451 * If you call this routine with a class >= 0, it recurses.
454 help_list (list
, cmdtype
, class, stream
)
455 struct cmd_list_element
*list
;
457 enum command_class
class;
461 char *cmdtype1
, *cmdtype2
;
463 /* If CMDTYPE is "foo ", CMDTYPE1 gets " foo" and CMDTYPE2 gets "foo sub" */
464 len
= strlen (cmdtype
);
465 cmdtype1
= (char *) alloca (len
+ 1);
467 cmdtype2
= (char *) alloca (len
+ 4);
472 strncpy (cmdtype1
+ 1, cmdtype
, len
- 1);
474 strncpy (cmdtype2
, cmdtype
, len
- 1);
475 strcpy (cmdtype2
+ len
- 1, " sub");
478 if (class == all_classes
)
479 fprintf_filtered (stream
, "List of classes of %scommands:\n\n", cmdtype2
);
481 fprintf_filtered (stream
, "List of %scommands:\n\n", cmdtype2
);
483 help_cmd_list (list
, class, cmdtype
, (int) class >= 0, stream
);
485 if (class == all_classes
)
486 fprintf_filtered (stream
, "\n\
487 Type \"help%s\" followed by a class name for a list of commands in that class.",
490 fprintf_filtered (stream
, "\n\
491 Type \"help%s\" followed by %scommand name for full documentation.\n\
492 Command name abbreviations are allowed if unambiguous.\n",
496 /* Print only the first line of STR on STREAM. */
498 print_doc_line (stream
, str
)
502 static char *line_buffer
= 0;
503 static int line_size
;
509 line_buffer
= (char *) xmalloc (line_size
);
513 while (*p
&& *p
!= '\n' && *p
!= '.' && *p
!= ',')
515 if (p
- str
> line_size
- 1)
517 line_size
= p
- str
+ 1;
518 free ((PTR
) line_buffer
);
519 line_buffer
= (char *) xmalloc (line_size
);
521 strncpy (line_buffer
, str
, p
- str
);
522 line_buffer
[p
- str
] = '\0';
523 if (islower (line_buffer
[0]))
524 line_buffer
[0] = toupper (line_buffer
[0]);
525 fputs_filtered (line_buffer
, stream
);
529 * Implement a help command on command list LIST.
530 * RECURSE should be non-zero if this should be done recursively on
531 * all sublists of LIST.
532 * PREFIX is the prefix to print before each command name.
533 * STREAM is the stream upon which the output should be written.
535 * A non-negative class number to list only commands in that
537 * ALL_COMMANDS to list all commands in list.
538 * ALL_CLASSES to list all classes in list.
540 * Note that RECURSE will be active on *all* sublists, not just the
541 * ones selected by the criteria above (ie. the selection mechanism
542 * is at the low level, not the high-level).
545 help_cmd_list (list
, class, prefix
, recurse
, stream
)
546 struct cmd_list_element
*list
;
547 enum command_class
class;
552 register struct cmd_list_element
*c
;
554 for (c
= list
; c
; c
= c
->next
)
556 if (c
->abbrev_flag
== 0 &&
557 (class == all_commands
558 || (class == all_classes
&& c
->function
.cfunc
== NULL
)
559 || (class == c
->class && c
->function
.cfunc
!= NULL
)))
561 fprintf_filtered (stream
, "%s%s -- ", prefix
, c
->name
);
562 print_doc_line (stream
, c
->doc
);
563 fputs_filtered ("\n", stream
);
566 && c
->prefixlist
!= 0
567 && c
->abbrev_flag
== 0)
568 help_cmd_list (*c
->prefixlist
, class, c
->prefixname
, 1, stream
);
573 /* Search the input clist for 'command'. Return the command if
574 found (or NULL if not), and return the number of commands
577 static struct cmd_list_element
*
578 find_cmd (command
, len
, clist
, ignore_help_classes
, nfound
)
581 struct cmd_list_element
*clist
;
582 int ignore_help_classes
;
585 struct cmd_list_element
*found
, *c
;
587 found
= (struct cmd_list_element
*) NULL
;
589 for (c
= clist
; c
; c
= c
->next
)
590 if (!strncmp (command
, c
->name
, len
)
591 && (!ignore_help_classes
|| c
->function
.cfunc
))
595 if (c
->name
[len
] == '\0')
604 /* This routine takes a line of TEXT and a CLIST in which to start the
605 lookup. When it returns it will have incremented the text pointer past
606 the section of text it matched, set *RESULT_LIST to point to the list in
607 which the last word was matched, and will return a pointer to the cmd
608 list element which the text matches. It will return NULL if no match at
609 all was possible. It will return -1 (cast appropriately, ick) if ambigous
610 matches are possible; in this case *RESULT_LIST will be set to point to
611 the list in which there are ambiguous choices (and *TEXT will be set to
612 the ambiguous text string).
614 If the located command was an abbreviation, this routine returns the base
615 command of the abbreviation.
617 It does no error reporting whatsoever; control will always return
618 to the superior routine.
620 In the case of an ambiguous return (-1), *RESULT_LIST will be set to point
621 at the prefix_command (ie. the best match) *or* (special case) will be NULL
622 if no prefix command was ever found. For example, in the case of "info a",
623 "info" matches without ambiguity, but "a" could be "args" or "address", so
624 *RESULT_LIST is set to the cmd_list_element for "info". So in this case
625 RESULT_LIST should not be interpeted as a pointer to the beginning of a
626 list; it simply points to a specific command. In the case of an ambiguous
627 return *TEXT is advanced past the last non-ambiguous prefix (e.g.
628 "info t" can be "info types" or "info target"; upon return *TEXT has been
629 advanced past "info ").
631 If RESULT_LIST is NULL, don't set *RESULT_LIST (but don't otherwise
632 affect the operation).
634 This routine does *not* modify the text pointed to by TEXT.
636 If IGNORE_HELP_CLASSES is nonzero, ignore any command list elements which
637 are actually help classes rather than commands (i.e. the function field of
638 the struct cmd_list_element is NULL). */
640 struct cmd_list_element
*
641 lookup_cmd_1 (text
, clist
, result_list
, ignore_help_classes
)
643 struct cmd_list_element
*clist
, **result_list
;
644 int ignore_help_classes
;
647 int len
, tmp
, nfound
;
648 struct cmd_list_element
*found
, *c
;
650 while (**text
== ' ' || **text
== '\t')
653 /* Treating underscores as part of command words is important
654 so that "set args_foo()" doesn't get interpreted as
655 "set args _foo()". */
657 *p
&& (isalnum (*p
) || *p
== '-' || *p
== '_' ||
659 (*p
== '+' || *p
== '<' || *p
== '>' || *p
== '$')) ||
660 (xdb_commands
&& (*p
== '!' || *p
== '/' || *p
== '?')));
664 /* If nothing but whitespace, return 0. */
670 /* *text and p now bracket the first command word to lookup (and
671 it's length is len). We copy this into a local temporary */
674 command
= (char *) alloca (len
+ 1);
675 for (tmp
= 0; tmp
< len
; tmp
++)
677 char x
= (*text
)[tmp
];
685 found
= find_cmd (command
, len
, clist
, ignore_help_classes
, &nfound
);
688 ** We didn't find the command in the entered case, so lower case it
691 if (!found
|| nfound
== 0)
693 for (tmp
= 0; tmp
< len
; tmp
++)
695 char x
= command
[tmp
];
696 command
[tmp
] = isupper (x
) ? tolower (x
) : x
;
698 found
= find_cmd (command
, len
, clist
, ignore_help_classes
, &nfound
);
701 /* If nothing matches, we have a simple failure. */
707 if (result_list
!= NULL
)
708 /* Will be modified in calling routine
709 if we know what the prefix command is. */
711 return (struct cmd_list_element
*) -1; /* Ambiguous. */
714 /* We've matched something on this list. Move text pointer forward. */
718 /* If this was an abbreviation, use the base command instead. */
720 if (found
->cmd_pointer
)
721 found
= found
->cmd_pointer
;
723 /* If we found a prefix command, keep looking. */
725 if (found
->prefixlist
)
727 c
= lookup_cmd_1 (text
, *found
->prefixlist
, result_list
,
728 ignore_help_classes
);
731 /* Didn't find anything; this is as far as we got. */
732 if (result_list
!= NULL
)
733 *result_list
= clist
;
736 else if (c
== (struct cmd_list_element
*) -1)
738 /* We've gotten this far properly, but the next step
739 is ambiguous. We need to set the result list to the best
740 we've found (if an inferior hasn't already set it). */
741 if (result_list
!= NULL
)
743 /* This used to say *result_list = *found->prefixlist
744 If that was correct, need to modify the documentation
745 at the top of this function to clarify what is supposed
747 *result_list
= found
;
758 if (result_list
!= NULL
)
759 *result_list
= clist
;
764 /* All this hair to move the space to the front of cmdtype */
767 undef_cmd_error (cmdtype
, q
)
770 error ("Undefined %scommand: \"%s\". Try \"help%s%.*s\".",
774 strlen (cmdtype
) - 1,
778 /* Look up the contents of *LINE as a command in the command list LIST.
779 LIST is a chain of struct cmd_list_element's.
780 If it is found, return the struct cmd_list_element for that command
781 and update *LINE to point after the command name, at the first argument.
782 If not found, call error if ALLOW_UNKNOWN is zero
783 otherwise (or if error returns) return zero.
784 Call error if specified command is ambiguous,
785 unless ALLOW_UNKNOWN is negative.
786 CMDTYPE precedes the word "command" in the error message.
788 If INGNORE_HELP_CLASSES is nonzero, ignore any command list
789 elements which are actually help classes rather than commands (i.e.
790 the function field of the struct cmd_list_element is 0). */
792 struct cmd_list_element
*
793 lookup_cmd (line
, list
, cmdtype
, allow_unknown
, ignore_help_classes
)
795 struct cmd_list_element
*list
;
798 int ignore_help_classes
;
800 struct cmd_list_element
*last_list
= 0;
801 struct cmd_list_element
*c
=
802 lookup_cmd_1 (line
, list
, &last_list
, ignore_help_classes
);
804 /* This is wrong for complete_command. */
805 char *ptr
= (*line
) + strlen (*line
) - 1;
807 /* Clear off trailing whitespace. */
808 while (ptr
>= *line
&& (*ptr
== ' ' || *ptr
== '\t'))
818 error ("Lack of needed %scommand", cmdtype
);
823 while (isalnum (*p
) || *p
== '-')
826 q
= (char *) alloca (p
- *line
+ 1);
827 strncpy (q
, *line
, p
- *line
);
829 undef_cmd_error (cmdtype
, q
);
835 else if (c
== (struct cmd_list_element
*) -1)
837 /* Ambigous. Local values should be off prefixlist or called
839 int local_allow_unknown
= (last_list
? last_list
->allow_unknown
:
841 char *local_cmdtype
= last_list
? last_list
->prefixname
: cmdtype
;
842 struct cmd_list_element
*local_list
=
843 (last_list
? *(last_list
->prefixlist
) : list
);
845 if (local_allow_unknown
< 0)
848 return last_list
; /* Found something. */
850 return 0; /* Found nothing. */
854 /* Report as error. */
859 ((*line
)[amb_len
] && (*line
)[amb_len
] != ' '
860 && (*line
)[amb_len
] != '\t');
865 for (c
= local_list
; c
; c
= c
->next
)
866 if (!strncmp (*line
, c
->name
, amb_len
))
868 if (strlen (ambbuf
) + strlen (c
->name
) + 6 < (int) sizeof ambbuf
)
871 strcat (ambbuf
, ", ");
872 strcat (ambbuf
, c
->name
);
876 strcat (ambbuf
, "..");
880 error ("Ambiguous %scommand \"%s\": %s.", local_cmdtype
,
887 /* We've got something. It may still not be what the caller
888 wants (if this command *needs* a subcommand). */
889 while (**line
== ' ' || **line
== '\t')
892 if (c
->prefixlist
&& **line
&& !c
->allow_unknown
)
893 undef_cmd_error (c
->prefixname
, *line
);
895 /* Seems to be what he wants. Return it. */
902 /* Look up the contents of *LINE as a command in the command list LIST.
903 LIST is a chain of struct cmd_list_element's.
904 If it is found, return the struct cmd_list_element for that command
905 and update *LINE to point after the command name, at the first argument.
906 If not found, call error if ALLOW_UNKNOWN is zero
907 otherwise (or if error returns) return zero.
908 Call error if specified command is ambiguous,
909 unless ALLOW_UNKNOWN is negative.
910 CMDTYPE precedes the word "command" in the error message. */
912 struct cmd_list_element
*
913 lookup_cmd (line
, list
, cmdtype
, allow_unknown
)
915 struct cmd_list_element
*list
;
920 register struct cmd_list_element
*c
, *found
;
926 /* Skip leading whitespace. */
928 while (**line
== ' ' || **line
== '\t')
931 /* Clear out trailing whitespace. */
933 p
= *line
+ strlen (*line
);
934 while (p
!= *line
&& (p
[-1] == ' ' || p
[-1] == '\t'))
938 /* Find end of command name. */
941 while (*p
== '-' || isalnum (*p
))
944 /* Look up the command name.
945 If exact match, keep that.
946 Otherwise, take command abbreviated, if unique. Note that (in my
947 opinion) a null string does *not* indicate ambiguity; simply the
948 end of the argument. */
953 error ("Lack of needed %scommand", cmdtype
);
957 /* Copy over to a local buffer, converting to lowercase on the way.
958 This is in case the command being parsed is a subcommand which
959 doesn't match anything, and that's ok. We want the original
960 untouched for the routine of the original command. */
962 processed_cmd
= (char *) alloca (p
- *line
+ 1);
963 for (cmd_len
= 0; cmd_len
< p
- *line
; cmd_len
++)
965 char x
= (*line
)[cmd_len
];
967 processed_cmd
[cmd_len
] = tolower (x
);
969 processed_cmd
[cmd_len
] = x
;
971 processed_cmd
[cmd_len
] = '\0';
973 /* Check all possibilities in the current command list. */
976 for (c
= list
; c
; c
= c
->next
)
978 if (!strncmp (processed_cmd
, c
->name
, cmd_len
))
982 if (c
->name
[cmd_len
] == 0)
990 /* Report error for undefined command name. */
994 if (nfound
> 1 && allow_unknown
>= 0)
997 for (c
= list
; c
; c
= c
->next
)
998 if (!strncmp (processed_cmd
, c
->name
, cmd_len
))
1000 if (strlen (ambbuf
) + strlen (c
->name
) + 6 < sizeof ambbuf
)
1002 if (strlen (ambbuf
))
1003 strcat (ambbuf
, ", ");
1004 strcat (ambbuf
, c
->name
);
1008 strcat (ambbuf
, "..");
1012 error ("Ambiguous %scommand \"%s\": %s.", cmdtype
,
1013 processed_cmd
, ambbuf
);
1015 else if (!allow_unknown
)
1016 error ("Undefined %scommand: \"%s\".", cmdtype
, processed_cmd
);
1020 /* Skip whitespace before the argument. */
1022 while (*p
== ' ' || *p
== '\t')
1026 if (found
->prefixlist
&& *p
)
1028 c
= lookup_cmd (line
, *found
->prefixlist
, found
->prefixname
,
1029 found
->allow_unknown
);
1038 /* Helper function for SYMBOL_COMPLETION_FUNCTION. */
1040 /* Return a vector of char pointers which point to the different
1041 possible completions in LIST of TEXT.
1043 WORD points in the same buffer as TEXT, and completions should be
1044 returned relative to this position. For example, suppose TEXT is "foo"
1045 and we want to complete to "foobar". If WORD is "oo", return
1046 "oobar"; if WORD is "baz/foo", return "baz/foobar". */
1049 complete_on_cmdlist (list
, text
, word
)
1050 struct cmd_list_element
*list
;
1054 struct cmd_list_element
*ptr
;
1056 int sizeof_matchlist
;
1058 int textlen
= strlen (text
);
1060 sizeof_matchlist
= 10;
1061 matchlist
= (char **) xmalloc (sizeof_matchlist
* sizeof (char *));
1064 for (ptr
= list
; ptr
; ptr
= ptr
->next
)
1065 if (!strncmp (ptr
->name
, text
, textlen
)
1066 && !ptr
->abbrev_flag
1067 && (ptr
->function
.cfunc
1068 || ptr
->prefixlist
))
1070 if (matches
== sizeof_matchlist
)
1072 sizeof_matchlist
*= 2;
1073 matchlist
= (char **) xrealloc ((char *) matchlist
,
1075 * sizeof (char *)));
1078 matchlist
[matches
] = (char *)
1079 xmalloc (strlen (word
) + strlen (ptr
->name
) + 1);
1081 strcpy (matchlist
[matches
], ptr
->name
);
1082 else if (word
> text
)
1084 /* Return some portion of ptr->name. */
1085 strcpy (matchlist
[matches
], ptr
->name
+ (word
- text
));
1089 /* Return some of text plus ptr->name. */
1090 strncpy (matchlist
[matches
], word
, text
- word
);
1091 matchlist
[matches
][text
- word
] = '\0';
1092 strcat (matchlist
[matches
], ptr
->name
);
1099 free ((PTR
) matchlist
);
1104 matchlist
= (char **) xrealloc ((char *) matchlist
, ((matches
+ 1)
1105 * sizeof (char *)));
1106 matchlist
[matches
] = (char *) 0;
1112 /* Helper function for SYMBOL_COMPLETION_FUNCTION. */
1114 /* Return a vector of char pointers which point to the different
1115 possible completions in CMD of TEXT.
1117 WORD points in the same buffer as TEXT, and completions should be
1118 returned relative to this position. For example, suppose TEXT is "foo"
1119 and we want to complete to "foobar". If WORD is "oo", return
1120 "oobar"; if WORD is "baz/foo", return "baz/foobar". */
1123 complete_on_enum (enumlist
, text
, word
)
1129 int sizeof_matchlist
;
1131 int textlen
= strlen (text
);
1135 sizeof_matchlist
= 10;
1136 matchlist
= (char **) xmalloc (sizeof_matchlist
* sizeof (char *));
1139 for (i
= 0; (name
= enumlist
[i
]) != NULL
; i
++)
1140 if (strncmp (name
, text
, textlen
) == 0)
1142 if (matches
== sizeof_matchlist
)
1144 sizeof_matchlist
*= 2;
1145 matchlist
= (char **) xrealloc ((char *) matchlist
,
1147 * sizeof (char *)));
1150 matchlist
[matches
] = (char *)
1151 xmalloc (strlen (word
) + strlen (name
) + 1);
1153 strcpy (matchlist
[matches
], name
);
1154 else if (word
> text
)
1156 /* Return some portion of name. */
1157 strcpy (matchlist
[matches
], name
+ (word
- text
));
1161 /* Return some of text plus name. */
1162 strncpy (matchlist
[matches
], word
, text
- word
);
1163 matchlist
[matches
][text
- word
] = '\0';
1164 strcat (matchlist
[matches
], name
);
1171 free ((PTR
) matchlist
);
1176 matchlist
= (char **) xrealloc ((char *) matchlist
, ((matches
+ 1)
1177 * sizeof (char *)));
1178 matchlist
[matches
] = (char *) 0;
1185 parse_binary_operation (arg
)
1193 length
= strlen (arg
);
1195 while (arg
[length
- 1] == ' ' || arg
[length
- 1] == '\t')
1198 if (!strncmp (arg
, "on", length
)
1199 || !strncmp (arg
, "1", length
)
1200 || !strncmp (arg
, "yes", length
))
1202 else if (!strncmp (arg
, "off", length
)
1203 || !strncmp (arg
, "0", length
)
1204 || !strncmp (arg
, "no", length
))
1208 error ("\"on\" or \"off\" expected.");
1213 /* Do a "set" or "show" command. ARG is NULL if no argument, or the text
1214 of the argument, and FROM_TTY is nonzero if this command is being entered
1215 directly by the user (i.e. these are just like any other
1216 command). C is the command list element for the command. */
1218 do_setshow_command (arg
, from_tty
, c
)
1221 struct cmd_list_element
*c
;
1223 if (c
->type
== set_cmd
)
1225 switch (c
->var_type
)
1236 new = (char *) xmalloc (strlen (arg
) + 2);
1239 while ((ch
= *p
++) != '\000')
1243 /* \ at end of argument is used after spaces
1244 so they won't be lost. */
1245 /* This is obsolete now that we no longer strip
1246 trailing whitespace and actually, the backslash
1247 didn't get here in my test, readline or
1248 something did something funky with a backslash
1249 right before a newline. */
1252 ch
= parse_escape (&p
);
1254 break; /* C loses */
1262 if (*(p
- 1) != '\\')
1266 new = (char *) xrealloc (new, q
- new);
1267 if (*(char **) c
->var
!= NULL
)
1268 free (*(char **) c
->var
);
1269 *(char **) c
->var
= new;
1272 case var_string_noescape
:
1275 if (*(char **) c
->var
!= NULL
)
1276 free (*(char **) c
->var
);
1277 *(char **) c
->var
= savestring (arg
, strlen (arg
));
1281 error_no_arg ("filename to set it to.");
1282 if (*(char **) c
->var
!= NULL
)
1283 free (*(char **) c
->var
);
1284 *(char **) c
->var
= tilde_expand (arg
);
1287 *(int *) c
->var
= parse_binary_operation (arg
);
1291 error_no_arg ("integer to set it to.");
1292 *(unsigned int *) c
->var
= parse_and_eval_address (arg
);
1293 if (*(unsigned int *) c
->var
== 0)
1294 *(unsigned int *) c
->var
= UINT_MAX
;
1300 error_no_arg ("integer to set it to.");
1301 val
= parse_and_eval_address (arg
);
1303 *(int *) c
->var
= INT_MAX
;
1304 else if (val
>= INT_MAX
)
1305 error ("integer %u out of range", val
);
1307 *(int *) c
->var
= val
;
1312 error_no_arg ("integer to set it to.");
1313 *(int *) c
->var
= parse_and_eval_address (arg
);
1323 /* if no argument was supplied, print an informative error message */
1327 strcpy (msg
, "Requires an argument. Valid arguments are ");
1328 for (i
= 0; c
->enums
[i
]; i
++)
1332 strcat (msg
, c
->enums
[i
]);
1338 p
= strchr (arg
, ' ');
1346 for (i
= 0; c
->enums
[i
]; i
++)
1347 if (strncmp (arg
, c
->enums
[i
], len
) == 0)
1349 match
= c
->enums
[i
];
1354 error ("Undefined item: \"%s\".", arg
);
1357 error ("Ambiguous item \"%s\".", arg
);
1359 *(char **) c
->var
= match
;
1363 error ("gdb internal error: bad var_type in do_setshow_command");
1366 else if (c
->type
== show_cmd
)
1368 /* Print doc minus "show" at start. */
1369 print_doc_line (gdb_stdout
, c
->doc
+ 5);
1371 fputs_filtered (" is ", gdb_stdout
);
1373 switch (c
->var_type
)
1379 fputs_filtered ("\"", gdb_stdout
);
1380 if (*(unsigned char **) c
->var
)
1381 fputstr_filtered (*(unsigned char **) c
->var
, '"', gdb_stdout
);
1382 fputs_filtered ("\"", gdb_stdout
);
1385 case var_string_noescape
:
1388 fputs_filtered ("\"", gdb_stdout
);
1389 if (*(char **) c
->var
)
1390 fputs_filtered (*(char **) c
->var
, gdb_stdout
);
1391 fputs_filtered ("\"", gdb_stdout
);
1394 fputs_filtered (*(int *) c
->var
? "on" : "off", gdb_stdout
);
1397 if (*(unsigned int *) c
->var
== UINT_MAX
)
1399 fputs_filtered ("unlimited", gdb_stdout
);
1402 /* else fall through */
1404 fprintf_filtered (gdb_stdout
, "%u", *(unsigned int *) c
->var
);
1407 if (*(int *) c
->var
== INT_MAX
)
1409 fputs_filtered ("unlimited", gdb_stdout
);
1412 fprintf_filtered (gdb_stdout
, "%d", *(int *) c
->var
);
1416 error ("gdb internal error: bad var_type in do_setshow_command");
1418 fputs_filtered (".\n", gdb_stdout
);
1421 error ("gdb internal error: bad cmd_type in do_setshow_command");
1422 (*c
->function
.sfunc
) (NULL
, from_tty
, c
);
1423 if (c
->type
== set_cmd
&& set_hook
)
1427 /* Show all the settings in a list of show commands. */
1430 cmd_show_list (list
, from_tty
, prefix
)
1431 struct cmd_list_element
*list
;
1435 for (; list
!= NULL
; list
= list
->next
)
1437 /* If we find a prefix, run its list, prefixing our output by its
1438 prefix (with "show " skipped). */
1439 if (list
->prefixlist
&& !list
->abbrev_flag
)
1440 cmd_show_list (*list
->prefixlist
, from_tty
, list
->prefixname
+ 5);
1441 if (list
->type
== show_cmd
)
1443 fputs_filtered (prefix
, gdb_stdout
);
1444 fputs_filtered (list
->name
, gdb_stdout
);
1445 fputs_filtered (": ", gdb_stdout
);
1446 do_setshow_command ((char *) NULL
, from_tty
, list
);
1453 shell_escape (arg
, from_tty
)
1458 /* If ARG is NULL, they want an inferior shell, but `system' just
1459 reports if the shell is available when passed a NULL arg. */
1460 int rc
= system (arg
? arg
: "");
1463 arg
= "inferior shell";
1467 fprintf_unfiltered (gdb_stderr
, "Cannot execute %s: %s\n", arg
,
1468 safe_strerror (errno
));
1469 gdb_flush (gdb_stderr
);
1473 fprintf_unfiltered (gdb_stderr
, "%s exited with status %d\n", arg
, rc
);
1474 gdb_flush (gdb_stderr
);
1477 /* Make sure to return to the directory GDB thinks it is, in case the
1478 shell command we just ran changed it. */
1479 chdir (current_directory
);
1481 #else /* Can fork. */
1482 int rc
, status
, pid
;
1483 char *p
, *user_shell
;
1485 if ((user_shell
= (char *) getenv ("SHELL")) == NULL
)
1486 user_shell
= "/bin/sh";
1488 /* Get the name of the shell for arg0 */
1489 if ((p
= strrchr (user_shell
, '/')) == NULL
)
1492 p
++; /* Get past '/' */
1494 if ((pid
= fork ()) == 0)
1497 execl (user_shell
, p
, 0);
1499 execl (user_shell
, p
, "-c", arg
, 0);
1501 fprintf_unfiltered (gdb_stderr
, "Cannot execute %s: %s\n", user_shell
,
1502 safe_strerror (errno
));
1503 gdb_flush (gdb_stderr
);
1508 while ((rc
= wait (&status
)) != pid
&& rc
!= -1)
1511 error ("Fork failed");
1512 #endif /* Can fork. */
1516 make_command (arg
, from_tty
)
1526 p
= xmalloc (sizeof ("make ") + strlen (arg
));
1527 strcpy (p
, "make ");
1528 strcpy (p
+ sizeof ("make ") - 1, arg
);
1531 shell_escape (p
, from_tty
);
1535 show_user_1 (c
, stream
)
1536 struct cmd_list_element
*c
;
1539 register struct command_line
*cmdlines
;
1541 cmdlines
= c
->user_commands
;
1544 fputs_filtered ("User command ", stream
);
1545 fputs_filtered (c
->name
, stream
);
1546 fputs_filtered (":\n", stream
);
1550 print_command_line (cmdlines
, 4, stream
);
1551 cmdlines
= cmdlines
->next
;
1553 fputs_filtered ("\n", stream
);
1558 show_user (args
, from_tty
)
1562 struct cmd_list_element
*c
;
1563 extern struct cmd_list_element
*cmdlist
;
1567 c
= lookup_cmd (&args
, cmdlist
, "", 0, 1);
1568 if (c
->class != class_user
)
1569 error ("Not a user command.");
1570 show_user_1 (c
, gdb_stdout
);
1574 for (c
= cmdlist
; c
; c
= c
->next
)
1576 if (c
->class == class_user
)
1577 show_user_1 (c
, gdb_stdout
);
1583 _initialize_command ()
1585 add_com ("shell", class_support
, shell_escape
,
1586 "Execute the rest of the line as a shell command. \n\
1587 With no arguments, run an inferior shell.");
1590 add_com_alias ("!", "shell", class_support
, 0);
1592 add_com ("make", class_support
, make_command
,
1593 "Run the ``make'' program using the rest of the line as arguments.");
1594 add_cmd ("user", no_class
, show_user
,
1595 "Show definitions of user defined commands.\n\
1596 Argument is the name of the user defined command.\n\
1597 With no argument, show definitions of all user defined commands.", &showlist
);