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, Boston, MA 02111-1307, USA. */
23 #include "gdb_string.h"
31 # ifdef HAVE_SYS_WAIT_H
32 # include <sys/wait.h>
38 /* Prototypes for local functions */
40 static void undef_cmd_error
PARAMS ((char *, char *));
42 static void show_user
PARAMS ((char *, int));
44 static void show_user_1
PARAMS ((struct cmd_list_element
*, GDB_FILE
*));
46 static void make_command
PARAMS ((char *, int));
48 static void shell_escape
PARAMS ((char *, int));
50 static int parse_binary_operation
PARAMS ((char *));
52 static void print_doc_line
PARAMS ((GDB_FILE
*, char *));
54 void _initialize_command
PARAMS ((void));
56 /* Add element named NAME.
57 CLASS is the top level category into which commands are broken down
59 FUN should be the function to execute the command;
60 it will get a character string as argument, with leading
61 and trailing blanks already eliminated.
63 DOC is a documentation string for the command.
64 Its first line should be a complete sentence.
65 It should start with ? for a command that is an abbreviation
66 or with * for a command that most users don't need to know about.
68 Add this command to command list *LIST.
70 Returns a pointer to the added command (not necessarily the head
73 struct cmd_list_element
*
74 add_cmd (name
, class, fun
, doc
, list
)
76 enum command_class
class;
77 void (*fun
) PARAMS ((char *, int));
79 struct cmd_list_element
**list
;
81 register struct cmd_list_element
*c
82 = (struct cmd_list_element
*) xmalloc (sizeof (struct cmd_list_element
));
83 struct cmd_list_element
*p
;
85 delete_cmd (name
, list
);
87 if (*list
== NULL
|| STRCMP ((*list
)->name
, name
) >= 0)
95 while (p
->next
&& STRCMP (p
->next
->name
, name
) <= 0)
105 c
->function
.cfunc
= fun
;
108 c
->prefixlist
= NULL
;
109 c
->prefixname
= NULL
;
110 c
->allow_unknown
= 0;
112 c
->completer
= make_symbol_completion_list
;
113 c
->type
= not_set_cmd
;
115 c
->var_type
= var_boolean
;
117 c
->user_commands
= NULL
;
119 c
->cmd_pointer
= NULL
;
124 /* Same as above, except that the abbrev_flag is set. */
126 #if 0 /* Currently unused */
128 struct cmd_list_element
*
129 add_abbrev_cmd (name
, class, fun
, doc
, list
)
131 enum command_class
class;
132 void (*fun
) PARAMS ((char *, int));
134 struct cmd_list_element
**list
;
136 register struct cmd_list_element
*c
137 = add_cmd (name
, class, fun
, doc
, list
);
145 struct cmd_list_element
*
146 add_alias_cmd (name
, oldname
, class, abbrev_flag
, list
)
149 enum command_class
class;
151 struct cmd_list_element
**list
;
153 /* Must do this since lookup_cmd tries to side-effect its first arg */
155 register struct cmd_list_element
*old
;
156 register struct cmd_list_element
*c
;
157 copied_name
= (char *) alloca (strlen (oldname
) + 1);
158 strcpy (copied_name
, oldname
);
159 old
= lookup_cmd (&copied_name
, *list
, "", 1, 1);
163 delete_cmd (name
, list
);
167 c
= add_cmd (name
, class, old
->function
.cfunc
, old
->doc
, list
);
168 c
->prefixlist
= old
->prefixlist
;
169 c
->prefixname
= old
->prefixname
;
170 c
->allow_unknown
= old
->allow_unknown
;
171 c
->abbrev_flag
= abbrev_flag
;
172 c
->cmd_pointer
= old
;
176 /* Like add_cmd but adds an element for a command prefix:
177 a name that should be followed by a subcommand to be looked up
178 in another command list. PREFIXLIST should be the address
179 of the variable containing that list. */
181 struct cmd_list_element
*
182 add_prefix_cmd (name
, class, fun
, doc
, prefixlist
, prefixname
,
185 enum command_class
class;
186 void (*fun
) PARAMS ((char *, int));
188 struct cmd_list_element
**prefixlist
;
191 struct cmd_list_element
**list
;
193 register struct cmd_list_element
*c
= add_cmd (name
, class, fun
, doc
, list
);
194 c
->prefixlist
= prefixlist
;
195 c
->prefixname
= prefixname
;
196 c
->allow_unknown
= allow_unknown
;
200 /* Like add_prefix_cmd but sets the abbrev_flag on the new command. */
202 struct cmd_list_element
*
203 add_abbrev_prefix_cmd (name
, class, fun
, doc
, prefixlist
, prefixname
,
206 enum command_class
class;
207 void (*fun
) PARAMS ((char *, int));
209 struct cmd_list_element
**prefixlist
;
212 struct cmd_list_element
**list
;
214 register struct cmd_list_element
*c
= add_cmd (name
, class, fun
, doc
, list
);
215 c
->prefixlist
= prefixlist
;
216 c
->prefixname
= prefixname
;
217 c
->allow_unknown
= allow_unknown
;
222 /* This is an empty "cfunc". */
224 not_just_help_class_command (args
, from_tty
)
230 /* This is an empty "sfunc". */
231 static void empty_sfunc
PARAMS ((char *, int, struct cmd_list_element
*));
234 empty_sfunc (args
, from_tty
, c
)
237 struct cmd_list_element
*c
;
241 /* Add element named NAME to command list LIST (the list for set
242 or some sublist thereof).
243 CLASS is as in add_cmd.
244 VAR_TYPE is the kind of thing we are setting.
245 VAR is address of the variable being controlled by this command.
246 DOC is the documentation string. */
248 struct cmd_list_element
*
249 add_set_cmd (name
, class, var_type
, var
, doc
, list
)
251 enum command_class
class;
255 struct cmd_list_element
**list
;
257 struct cmd_list_element
*c
258 = add_cmd (name
, class, NO_FUNCTION
, doc
, list
);
261 c
->var_type
= var_type
;
263 /* This needs to be something besides NO_FUNCTION so that this isn't
264 treated as a help class. */
265 c
->function
.sfunc
= empty_sfunc
;
269 /* Add element named NAME to command list LIST (the list for set
270 or some sublist thereof).
271 CLASS is as in add_cmd.
272 ENUMLIST is a list of strings which may follow NAME.
273 VAR is address of the variable which will contain the matching string
275 DOC is the documentation string. */
277 struct cmd_list_element
*
278 add_set_enum_cmd (name
, class, enumlist
, var
, doc
, list
)
280 enum command_class
class;
284 struct cmd_list_element
**list
;
286 struct cmd_list_element
*c
287 = add_set_cmd (name
, class, var_enum
, var
, doc
, list
);
293 /* Where SETCMD has already been added, add the corresponding show
294 command to LIST and return a pointer to the added command (not
295 necessarily the head of LIST). */
296 struct cmd_list_element
*
297 add_show_from_set (setcmd
, list
)
298 struct cmd_list_element
*setcmd
;
299 struct cmd_list_element
**list
;
301 struct cmd_list_element
*showcmd
=
302 (struct cmd_list_element
*) xmalloc (sizeof (struct cmd_list_element
));
303 struct cmd_list_element
*p
;
305 memcpy (showcmd
, setcmd
, sizeof (struct cmd_list_element
));
306 delete_cmd (showcmd
->name
, list
);
307 showcmd
->type
= show_cmd
;
309 /* Replace "set " at start of docstring with "show ". */
310 if (setcmd
->doc
[0] == 'S' && setcmd
->doc
[1] == 'e'
311 && setcmd
->doc
[2] == 't' && setcmd
->doc
[3] == ' ')
312 showcmd
->doc
= concat ("Show ", setcmd
->doc
+ 4, NULL
);
314 fprintf_unfiltered (gdb_stderr
, "GDB internal error: Bad docstring for set command\n");
316 if (*list
== NULL
|| STRCMP ((*list
)->name
, showcmd
->name
) >= 0)
318 showcmd
->next
= *list
;
324 while (p
->next
&& STRCMP (p
->next
->name
, showcmd
->name
) <= 0)
328 showcmd
->next
= p
->next
;
335 /* Remove the command named NAME from the command list. */
338 delete_cmd (name
, list
)
340 struct cmd_list_element
**list
;
342 register struct cmd_list_element
*c
;
343 struct cmd_list_element
*p
;
345 while (*list
&& STREQ ((*list
)->name
, name
))
348 (*list
)->hookee
->hook
= 0; /* Hook slips out of its mouth */
355 for (c
= *list
; c
->next
;)
357 if (STREQ (c
->next
->name
, name
))
360 c
->next
->hookee
->hook
= 0; /* hooked cmd gets away. */
370 /* This command really has to deal with two things:
371 * 1) I want documentation on *this string* (usually called by
372 * "help commandname").
373 * 2) I want documentation on *this list* (usually called by
374 * giving a command that requires subcommands. Also called by saying
377 * I am going to split this into two seperate comamnds, help_cmd and
382 help_cmd (command
, stream
)
386 struct cmd_list_element
*c
;
387 extern struct cmd_list_element
*cmdlist
;
391 help_list (cmdlist
, "", all_classes
, stream
);
395 c
= lookup_cmd (&command
, cmdlist
, "", 0, 0);
400 /* There are three cases here.
401 If c->prefixlist is nonzero, we have a prefix command.
402 Print its documentation, then list its subcommands.
404 If c->function is nonzero, we really have a command.
405 Print its documentation and return.
407 If c->function is zero, we have a class name.
408 Print its documentation (as if it were a command)
409 and then set class to the number of this class
410 so that the commands in the class will be listed. */
412 fputs_filtered (c
->doc
, stream
);
413 fputs_filtered ("\n", stream
);
415 if (c
->prefixlist
== 0 && c
->function
.cfunc
!= NULL
)
417 fprintf_filtered (stream
, "\n");
419 /* If this is a prefix command, print it's subcommands */
421 help_list (*c
->prefixlist
, c
->prefixname
, all_commands
, stream
);
423 /* If this is a class name, print all of the commands in the class */
424 if (c
->function
.cfunc
== NULL
)
425 help_list (cmdlist
, "", c
->class, stream
);
428 fprintf_filtered (stream
, "\nThis command has a hook defined: %s\n",
433 * Get a specific kind of help on a command list.
436 * CMDTYPE is the prefix to use in the title string.
437 * CLASS is the class with which to list the nodes of this list (see
438 * documentation for help_cmd_list below), As usual, ALL_COMMANDS for
439 * everything, ALL_CLASSES for just classes, and non-negative for only things
440 * in a specific class.
441 * and STREAM is the output stream on which to print things.
442 * If you call this routine with a class >= 0, it recurses.
445 help_list (list
, cmdtype
, class, stream
)
446 struct cmd_list_element
*list
;
448 enum command_class
class;
452 char *cmdtype1
, *cmdtype2
;
454 /* If CMDTYPE is "foo ", CMDTYPE1 gets " foo" and CMDTYPE2 gets "foo sub" */
455 len
= strlen (cmdtype
);
456 cmdtype1
= (char *) alloca (len
+ 1);
458 cmdtype2
= (char *) alloca (len
+ 4);
463 strncpy (cmdtype1
+ 1, cmdtype
, len
- 1);
465 strncpy (cmdtype2
, cmdtype
, len
- 1);
466 strcpy (cmdtype2
+ len
- 1, " sub");
469 if (class == all_classes
)
470 fprintf_filtered (stream
, "List of classes of %scommands:\n\n", cmdtype2
);
472 fprintf_filtered (stream
, "List of %scommands:\n\n", cmdtype2
);
474 help_cmd_list (list
, class, cmdtype
, (int)class >= 0, stream
);
476 if (class == all_classes
)
477 fprintf_filtered (stream
, "\n\
478 Type \"help%s\" followed by a class name for a list of commands in that class.",
481 fprintf_filtered (stream
, "\n\
482 Type \"help%s\" followed by %scommand name for full documentation.\n\
483 Command name abbreviations are allowed if unambiguous.\n",
487 /* Print only the first line of STR on STREAM. */
489 print_doc_line (stream
, str
)
493 static char *line_buffer
= 0;
494 static int line_size
;
500 line_buffer
= (char *) xmalloc (line_size
);
504 while (*p
&& *p
!= '\n' && *p
!= '.' && *p
!= ',')
506 if (p
- str
> line_size
- 1)
508 line_size
= p
- str
+ 1;
509 free ((PTR
)line_buffer
);
510 line_buffer
= (char *) xmalloc (line_size
);
512 strncpy (line_buffer
, str
, p
- str
);
513 line_buffer
[p
- str
] = '\0';
514 if (islower (line_buffer
[0]))
515 line_buffer
[0] = toupper (line_buffer
[0]);
516 fputs_filtered (line_buffer
, stream
);
520 * Implement a help command on command list LIST.
521 * RECURSE should be non-zero if this should be done recursively on
522 * all sublists of LIST.
523 * PREFIX is the prefix to print before each command name.
524 * STREAM is the stream upon which the output should be written.
526 * A non-negative class number to list only commands in that
528 * ALL_COMMANDS to list all commands in list.
529 * ALL_CLASSES to list all classes in list.
531 * Note that RECURSE will be active on *all* sublists, not just the
532 * ones selected by the criteria above (ie. the selection mechanism
533 * is at the low level, not the high-level).
536 help_cmd_list (list
, class, prefix
, recurse
, stream
)
537 struct cmd_list_element
*list
;
538 enum command_class
class;
543 register struct cmd_list_element
*c
;
545 for (c
= list
; c
; c
= c
->next
)
547 if (c
->abbrev_flag
== 0 &&
548 (class == all_commands
549 || (class == all_classes
&& c
->function
.cfunc
== NULL
)
550 || (class == c
->class && c
->function
.cfunc
!= NULL
)))
552 fprintf_filtered (stream
, "%s%s -- ", prefix
, c
->name
);
553 print_doc_line (stream
, c
->doc
);
554 fputs_filtered ("\n", stream
);
557 && c
->prefixlist
!= 0
558 && c
->abbrev_flag
== 0)
559 help_cmd_list (*c
->prefixlist
, class, c
->prefixname
, 1, stream
);
564 /* Search the input clist for 'command'. Return the command if
565 found (or NULL if not), and return the number of commands
568 static struct cmd_list_element
*
569 find_cmd(command
, len
, clist
, ignore_help_classes
, nfound
)
571 struct cmd_list_element
*clist
;
572 int ignore_help_classes
;
575 struct cmd_list_element
*found
, *c
;
577 found
= (struct cmd_list_element
*)NULL
;
579 for (c
= clist
; c
; c
= c
->next
)
580 if (!strncmp (command
, c
->name
, len
)
581 && (!ignore_help_classes
|| c
->function
.cfunc
))
585 if (c
->name
[len
] == '\0')
594 /* This routine takes a line of TEXT and a CLIST in which to start the
595 lookup. When it returns it will have incremented the text pointer past
596 the section of text it matched, set *RESULT_LIST to point to the list in
597 which the last word was matched, and will return a pointer to the cmd
598 list element which the text matches. It will return NULL if no match at
599 all was possible. It will return -1 (cast appropriately, ick) if ambigous
600 matches are possible; in this case *RESULT_LIST will be set to point to
601 the list in which there are ambiguous choices (and *TEXT will be set to
602 the ambiguous text string).
604 If the located command was an abbreviation, this routine returns the base
605 command of the abbreviation.
607 It does no error reporting whatsoever; control will always return
608 to the superior routine.
610 In the case of an ambiguous return (-1), *RESULT_LIST will be set to point
611 at the prefix_command (ie. the best match) *or* (special case) will be NULL
612 if no prefix command was ever found. For example, in the case of "info a",
613 "info" matches without ambiguity, but "a" could be "args" or "address", so
614 *RESULT_LIST is set to the cmd_list_element for "info". So in this case
615 RESULT_LIST should not be interpeted as a pointer to the beginning of a
616 list; it simply points to a specific command. In the case of an ambiguous
617 return *TEXT is advanced past the last non-ambiguous prefix (e.g.
618 "info t" can be "info types" or "info target"; upon return *TEXT has been
619 advanced past "info ").
621 If RESULT_LIST is NULL, don't set *RESULT_LIST (but don't otherwise
622 affect the operation).
624 This routine does *not* modify the text pointed to by TEXT.
626 If IGNORE_HELP_CLASSES is nonzero, ignore any command list elements which
627 are actually help classes rather than commands (i.e. the function field of
628 the struct cmd_list_element is NULL). */
630 struct cmd_list_element
*
631 lookup_cmd_1 (text
, clist
, result_list
, ignore_help_classes
)
633 struct cmd_list_element
*clist
, **result_list
;
634 int ignore_help_classes
;
637 int len
, tmp
, nfound
;
638 struct cmd_list_element
*found
, *c
;
640 while (**text
== ' ' || **text
== '\t')
643 /* Treating underscores as part of command words is important
644 so that "set args_foo()" doesn't get interpreted as
645 "set args _foo()". */
647 *p
&& (isalnum(*p
) || *p
== '-' || *p
== '_' ||
649 (*p
== '+' || *p
== '<' || *p
== '>' || *p
== '$')) ||
650 (xdb_commands
&& (*p
== '!' || *p
== '/' || *p
== '?')));
654 /* If nothing but whitespace, return 0. */
660 /* *text and p now bracket the first command word to lookup (and
661 it's length is len). We copy this into a local temporary */
664 command
= (char *) alloca (len
+ 1);
665 for (tmp
= 0; tmp
< len
; tmp
++)
667 char x
= (*text
)[tmp
];
675 found
= find_cmd(command
, len
, clist
, ignore_help_classes
, &nfound
);
678 ** We didn't find the command in the entered case, so lower case it
681 if (!found
|| nfound
== 0)
683 for (tmp
= 0; tmp
< len
; tmp
++)
685 char x
= command
[tmp
];
686 command
[tmp
] = isupper(x
) ? tolower(x
) : x
;
688 found
= find_cmd(command
, len
, clist
, ignore_help_classes
, &nfound
);
691 /* If nothing matches, we have a simple failure. */
697 if (result_list
!= NULL
)
698 /* Will be modified in calling routine
699 if we know what the prefix command is. */
701 return (struct cmd_list_element
*) -1; /* Ambiguous. */
704 /* We've matched something on this list. Move text pointer forward. */
708 /* If this was an abbreviation, use the base command instead. */
710 if (found
->cmd_pointer
)
711 found
= found
->cmd_pointer
;
713 /* If we found a prefix command, keep looking. */
715 if (found
->prefixlist
)
717 c
= lookup_cmd_1 (text
, *found
->prefixlist
, result_list
,
718 ignore_help_classes
);
721 /* Didn't find anything; this is as far as we got. */
722 if (result_list
!= NULL
)
723 *result_list
= clist
;
726 else if (c
== (struct cmd_list_element
*) -1)
728 /* We've gotten this far properly, but the next step
729 is ambiguous. We need to set the result list to the best
730 we've found (if an inferior hasn't already set it). */
731 if (result_list
!= NULL
)
733 /* This used to say *result_list = *found->prefixlist
734 If that was correct, need to modify the documentation
735 at the top of this function to clarify what is supposed
737 *result_list
= found
;
748 if (result_list
!= NULL
)
749 *result_list
= clist
;
754 /* All this hair to move the space to the front of cmdtype */
757 undef_cmd_error (cmdtype
, q
)
760 error ("Undefined %scommand: \"%s\". Try \"help%s%.*s\".",
768 /* Look up the contents of *LINE as a command in the command list LIST.
769 LIST is a chain of struct cmd_list_element's.
770 If it is found, return the struct cmd_list_element for that command
771 and update *LINE to point after the command name, at the first argument.
772 If not found, call error if ALLOW_UNKNOWN is zero
773 otherwise (or if error returns) return zero.
774 Call error if specified command is ambiguous,
775 unless ALLOW_UNKNOWN is negative.
776 CMDTYPE precedes the word "command" in the error message.
778 If INGNORE_HELP_CLASSES is nonzero, ignore any command list
779 elements which are actually help classes rather than commands (i.e.
780 the function field of the struct cmd_list_element is 0). */
782 struct cmd_list_element
*
783 lookup_cmd (line
, list
, cmdtype
, allow_unknown
, ignore_help_classes
)
785 struct cmd_list_element
*list
;
788 int ignore_help_classes
;
790 struct cmd_list_element
*last_list
= 0;
791 struct cmd_list_element
*c
=
792 lookup_cmd_1 (line
, list
, &last_list
, ignore_help_classes
);
794 /* This is wrong for complete_command. */
795 char *ptr
= (*line
) + strlen (*line
) - 1;
797 /* Clear off trailing whitespace. */
798 while (ptr
>= *line
&& (*ptr
== ' ' || *ptr
== '\t'))
808 error ("Lack of needed %scommand", cmdtype
);
813 while (isalnum(*p
) || *p
== '-')
816 q
= (char *) alloca (p
- *line
+ 1);
817 strncpy (q
, *line
, p
- *line
);
819 undef_cmd_error (cmdtype
, q
);
825 else if (c
== (struct cmd_list_element
*) -1)
827 /* Ambigous. Local values should be off prefixlist or called
829 int local_allow_unknown
= (last_list
? last_list
->allow_unknown
:
831 char *local_cmdtype
= last_list
? last_list
->prefixname
: cmdtype
;
832 struct cmd_list_element
*local_list
=
833 (last_list
? *(last_list
->prefixlist
) : list
);
835 if (local_allow_unknown
< 0)
838 return last_list
; /* Found something. */
840 return 0; /* Found nothing. */
844 /* Report as error. */
849 ((*line
)[amb_len
] && (*line
)[amb_len
] != ' '
850 && (*line
)[amb_len
] != '\t');
855 for (c
= local_list
; c
; c
= c
->next
)
856 if (!strncmp (*line
, c
->name
, amb_len
))
858 if (strlen (ambbuf
) + strlen (c
->name
) + 6 < (int)sizeof ambbuf
)
861 strcat (ambbuf
, ", ");
862 strcat (ambbuf
, c
->name
);
866 strcat (ambbuf
, "..");
870 error ("Ambiguous %scommand \"%s\": %s.", local_cmdtype
,
877 /* We've got something. It may still not be what the caller
878 wants (if this command *needs* a subcommand). */
879 while (**line
== ' ' || **line
== '\t')
882 if (c
->prefixlist
&& **line
&& !c
->allow_unknown
)
883 undef_cmd_error (c
->prefixname
, *line
);
885 /* Seems to be what he wants. Return it. */
892 /* Look up the contents of *LINE as a command in the command list LIST.
893 LIST is a chain of struct cmd_list_element's.
894 If it is found, return the struct cmd_list_element for that command
895 and update *LINE to point after the command name, at the first argument.
896 If not found, call error if ALLOW_UNKNOWN is zero
897 otherwise (or if error returns) return zero.
898 Call error if specified command is ambiguous,
899 unless ALLOW_UNKNOWN is negative.
900 CMDTYPE precedes the word "command" in the error message. */
902 struct cmd_list_element
*
903 lookup_cmd (line
, list
, cmdtype
, allow_unknown
)
905 struct cmd_list_element
*list
;
910 register struct cmd_list_element
*c
, *found
;
916 /* Skip leading whitespace. */
918 while (**line
== ' ' || **line
== '\t')
921 /* Clear out trailing whitespace. */
923 p
= *line
+ strlen (*line
);
924 while (p
!= *line
&& (p
[-1] == ' ' || p
[-1] == '\t'))
928 /* Find end of command name. */
931 while (*p
== '-' || isalnum(*p
))
934 /* Look up the command name.
935 If exact match, keep that.
936 Otherwise, take command abbreviated, if unique. Note that (in my
937 opinion) a null string does *not* indicate ambiguity; simply the
938 end of the argument. */
943 error ("Lack of needed %scommand", cmdtype
);
947 /* Copy over to a local buffer, converting to lowercase on the way.
948 This is in case the command being parsed is a subcommand which
949 doesn't match anything, and that's ok. We want the original
950 untouched for the routine of the original command. */
952 processed_cmd
= (char *) alloca (p
- *line
+ 1);
953 for (cmd_len
= 0; cmd_len
< p
- *line
; cmd_len
++)
955 char x
= (*line
)[cmd_len
];
957 processed_cmd
[cmd_len
] = tolower(x
);
959 processed_cmd
[cmd_len
] = x
;
961 processed_cmd
[cmd_len
] = '\0';
963 /* Check all possibilities in the current command list. */
966 for (c
= list
; c
; c
= c
->next
)
968 if (!strncmp (processed_cmd
, c
->name
, cmd_len
))
972 if (c
->name
[cmd_len
] == 0)
980 /* Report error for undefined command name. */
984 if (nfound
> 1 && allow_unknown
>= 0)
987 for (c
= list
; c
; c
= c
->next
)
988 if (!strncmp (processed_cmd
, c
->name
, cmd_len
))
990 if (strlen (ambbuf
) + strlen (c
->name
) + 6 < sizeof ambbuf
)
993 strcat (ambbuf
, ", ");
994 strcat (ambbuf
, c
->name
);
998 strcat (ambbuf
, "..");
1002 error ("Ambiguous %scommand \"%s\": %s.", cmdtype
,
1003 processed_cmd
, ambbuf
);
1005 else if (!allow_unknown
)
1006 error ("Undefined %scommand: \"%s\".", cmdtype
, processed_cmd
);
1010 /* Skip whitespace before the argument. */
1012 while (*p
== ' ' || *p
== '\t') p
++;
1015 if (found
->prefixlist
&& *p
)
1017 c
= lookup_cmd (line
, *found
->prefixlist
, found
->prefixname
,
1018 found
->allow_unknown
);
1027 /* Helper function for SYMBOL_COMPLETION_FUNCTION. */
1029 /* Return a vector of char pointers which point to the different
1030 possible completions in LIST of TEXT.
1032 WORD points in the same buffer as TEXT, and completions should be
1033 returned relative to this position. For example, suppose TEXT is "foo"
1034 and we want to complete to "foobar". If WORD is "oo", return
1035 "oobar"; if WORD is "baz/foo", return "baz/foobar". */
1038 complete_on_cmdlist (list
, text
, word
)
1039 struct cmd_list_element
*list
;
1043 struct cmd_list_element
*ptr
;
1045 int sizeof_matchlist
;
1047 int textlen
= strlen (text
);
1049 sizeof_matchlist
= 10;
1050 matchlist
= (char **) xmalloc (sizeof_matchlist
* sizeof (char *));
1053 for (ptr
= list
; ptr
; ptr
= ptr
->next
)
1054 if (!strncmp (ptr
->name
, text
, textlen
)
1055 && !ptr
->abbrev_flag
1056 && (ptr
->function
.cfunc
1057 || ptr
->prefixlist
))
1059 if (matches
== sizeof_matchlist
)
1061 sizeof_matchlist
*= 2;
1062 matchlist
= (char **) xrealloc ((char *)matchlist
,
1064 * sizeof (char *)));
1067 matchlist
[matches
] = (char *)
1068 xmalloc (strlen (word
) + strlen (ptr
->name
) + 1);
1070 strcpy (matchlist
[matches
], ptr
->name
);
1071 else if (word
> text
)
1073 /* Return some portion of ptr->name. */
1074 strcpy (matchlist
[matches
], ptr
->name
+ (word
- text
));
1078 /* Return some of text plus ptr->name. */
1079 strncpy (matchlist
[matches
], word
, text
- word
);
1080 matchlist
[matches
][text
- word
] = '\0';
1081 strcat (matchlist
[matches
], ptr
->name
);
1088 free ((PTR
)matchlist
);
1093 matchlist
= (char **) xrealloc ((char *)matchlist
, ((matches
+ 1)
1094 * sizeof (char *)));
1095 matchlist
[matches
] = (char *) 0;
1101 /* Helper function for SYMBOL_COMPLETION_FUNCTION. */
1103 /* Return a vector of char pointers which point to the different
1104 possible completions in CMD of TEXT.
1106 WORD points in the same buffer as TEXT, and completions should be
1107 returned relative to this position. For example, suppose TEXT is "foo"
1108 and we want to complete to "foobar". If WORD is "oo", return
1109 "oobar"; if WORD is "baz/foo", return "baz/foobar". */
1112 complete_on_enum (enumlist
, text
, word
)
1118 int sizeof_matchlist
;
1120 int textlen
= strlen (text
);
1124 sizeof_matchlist
= 10;
1125 matchlist
= (char **) xmalloc (sizeof_matchlist
* sizeof (char *));
1128 for (i
= 0; (name
= enumlist
[i
]) != NULL
; i
++)
1129 if (strncmp (name
, text
, textlen
) == 0)
1131 if (matches
== sizeof_matchlist
)
1133 sizeof_matchlist
*= 2;
1134 matchlist
= (char **) xrealloc ((char *)matchlist
,
1136 * sizeof (char *)));
1139 matchlist
[matches
] = (char *)
1140 xmalloc (strlen (word
) + strlen (name
) + 1);
1142 strcpy (matchlist
[matches
], name
);
1143 else if (word
> text
)
1145 /* Return some portion of name. */
1146 strcpy (matchlist
[matches
], name
+ (word
- text
));
1150 /* Return some of text plus name. */
1151 strncpy (matchlist
[matches
], word
, text
- word
);
1152 matchlist
[matches
][text
- word
] = '\0';
1153 strcat (matchlist
[matches
], name
);
1160 free ((PTR
)matchlist
);
1165 matchlist
= (char **) xrealloc ((char *)matchlist
, ((matches
+ 1)
1166 * sizeof (char *)));
1167 matchlist
[matches
] = (char *) 0;
1174 parse_binary_operation (arg
)
1182 length
= strlen (arg
);
1184 while (arg
[length
- 1] == ' ' || arg
[length
- 1] == '\t')
1187 if (!strncmp (arg
, "on", length
)
1188 || !strncmp (arg
, "1", length
)
1189 || !strncmp (arg
, "yes", length
))
1192 if (!strncmp (arg
, "off", length
)
1193 || !strncmp (arg
, "0", length
)
1194 || !strncmp (arg
, "no", length
))
1198 error ("\"on\" or \"off\" expected.");
1203 /* Do a "set" or "show" command. ARG is NULL if no argument, or the text
1204 of the argument, and FROM_TTY is nonzero if this command is being entered
1205 directly by the user (i.e. these are just like any other
1206 command). C is the command list element for the command. */
1208 do_setshow_command (arg
, from_tty
, c
)
1211 struct cmd_list_element
*c
;
1213 if (c
->type
== set_cmd
)
1215 switch (c
->var_type
)
1226 new = (char *) xmalloc (strlen (arg
) + 2);
1228 while ((ch
= *p
++) != '\000')
1232 /* \ at end of argument is used after spaces
1233 so they won't be lost. */
1234 /* This is obsolete now that we no longer strip
1235 trailing whitespace and actually, the backslash
1236 didn't get here in my test, readline or
1237 something did something funky with a backslash
1238 right before a newline. */
1241 ch
= parse_escape (&p
);
1243 break; /* C loses */
1251 if (*(p
- 1) != '\\')
1255 new = (char *) xrealloc (new, q
- new);
1256 if (*(char **)c
->var
!= NULL
)
1257 free (*(char **)c
->var
);
1258 *(char **) c
->var
= new;
1261 case var_string_noescape
:
1264 if (*(char **)c
->var
!= NULL
)
1265 free (*(char **)c
->var
);
1266 *(char **) c
->var
= savestring (arg
, strlen (arg
));
1270 error_no_arg ("filename to set it to.");
1271 if (*(char **)c
->var
!= NULL
)
1272 free (*(char **)c
->var
);
1273 *(char **)c
->var
= tilde_expand (arg
);
1276 *(int *) c
->var
= parse_binary_operation (arg
);
1280 error_no_arg ("integer to set it to.");
1281 *(unsigned int *) c
->var
= parse_and_eval_address (arg
);
1282 if (*(unsigned int *) c
->var
== 0)
1283 *(unsigned int *) c
->var
= UINT_MAX
;
1289 error_no_arg ("integer to set it to.");
1290 val
= parse_and_eval_address (arg
);
1292 *(int *) c
->var
= INT_MAX
;
1293 else if (val
>= INT_MAX
)
1294 error ("integer %u out of range", val
);
1296 *(int *) c
->var
= val
;
1301 error_no_arg ("integer to set it to.");
1302 *(int *) c
->var
= parse_and_eval_address (arg
);
1312 /* if no argument was supplied, print an informative error message */
1316 strcpy (msg
, "Requires an argument. Valid arguments are ");
1317 for (i
= 0; c
->enums
[i
]; i
++)
1321 strcat (msg
, c
->enums
[i
]);
1327 p
= strchr (arg
, ' ');
1335 for (i
= 0; c
->enums
[i
]; i
++)
1336 if (strncmp (arg
, c
->enums
[i
], len
) == 0)
1338 match
= c
->enums
[i
];
1343 error ("Undefined item: \"%s\".", arg
);
1346 error ("Ambiguous item \"%s\".", arg
);
1348 *(char **)c
->var
= match
;
1352 error ("gdb internal error: bad var_type in do_setshow_command");
1355 else if (c
->type
== show_cmd
)
1357 /* Print doc minus "show" at start. */
1358 print_doc_line (gdb_stdout
, c
->doc
+ 5);
1360 fputs_filtered (" is ", gdb_stdout
);
1362 switch (c
->var_type
)
1368 fputs_filtered ("\"", gdb_stdout
);
1369 if (*(unsigned char **)c
->var
)
1370 for (p
= *(unsigned char **) c
->var
; *p
!= '\0'; p
++)
1371 gdb_printchar (*p
, gdb_stdout
, '"');
1372 fputs_filtered ("\"", gdb_stdout
);
1375 case var_string_noescape
:
1378 fputs_filtered ("\"", gdb_stdout
);
1379 if (*(char **)c
->var
)
1380 fputs_filtered (*(char **) c
->var
, gdb_stdout
);
1381 fputs_filtered ("\"", gdb_stdout
);
1384 fputs_filtered (*(int *) c
->var
? "on" : "off", gdb_stdout
);
1387 if (*(unsigned int *) c
->var
== UINT_MAX
) {
1388 fputs_filtered ("unlimited", gdb_stdout
);
1391 /* else fall through */
1393 fprintf_filtered (gdb_stdout
, "%u", *(unsigned int *) c
->var
);
1396 if (*(int *) c
->var
== INT_MAX
)
1398 fputs_filtered ("unlimited", gdb_stdout
);
1401 fprintf_filtered (gdb_stdout
, "%d", *(int *) c
->var
);
1405 error ("gdb internal error: bad var_type in do_setshow_command");
1407 fputs_filtered (".\n", gdb_stdout
);
1410 error ("gdb internal error: bad cmd_type in do_setshow_command");
1411 (*c
->function
.sfunc
) (NULL
, from_tty
, c
);
1414 /* Show all the settings in a list of show commands. */
1417 cmd_show_list (list
, from_tty
, prefix
)
1418 struct cmd_list_element
*list
;
1422 for (; list
!= NULL
; list
= list
->next
) {
1423 /* If we find a prefix, run its list, prefixing our output by its
1424 prefix (with "show " skipped). */
1425 if (list
->prefixlist
&& !list
->abbrev_flag
)
1426 cmd_show_list (*list
->prefixlist
, from_tty
, list
->prefixname
+ 5);
1427 if (list
->type
== show_cmd
)
1429 fputs_filtered (prefix
, gdb_stdout
);
1430 fputs_filtered (list
->name
, gdb_stdout
);
1431 fputs_filtered (": ", gdb_stdout
);
1432 do_setshow_command ((char *)NULL
, from_tty
, list
);
1439 shell_escape (arg
, from_tty
)
1444 /* FIXME: what about errors (I don't know how GO32 system() handles
1447 #else /* Can fork. */
1448 int rc
, status
, pid
;
1449 char *p
, *user_shell
;
1451 if ((user_shell
= (char *) getenv ("SHELL")) == NULL
)
1452 user_shell
= "/bin/sh";
1454 /* Get the name of the shell for arg0 */
1455 if ((p
= strrchr (user_shell
, '/')) == NULL
)
1458 p
++; /* Get past '/' */
1460 if ((pid
= fork()) == 0)
1463 execl (user_shell
, p
, 0);
1465 execl (user_shell
, p
, "-c", arg
, 0);
1467 fprintf_unfiltered (gdb_stderr
, "Cannot execute %s: %s\n", user_shell
,
1468 safe_strerror (errno
));
1469 gdb_flush (gdb_stderr
);
1474 while ((rc
= wait (&status
)) != pid
&& rc
!= -1)
1477 error ("Fork failed");
1478 #endif /* Can fork. */
1482 make_command (arg
, from_tty
)
1492 p
= xmalloc (sizeof("make ") + strlen(arg
));
1493 strcpy (p
, "make ");
1494 strcpy (p
+ sizeof("make ")-1, arg
);
1497 shell_escape (p
, from_tty
);
1501 show_user_1 (c
, stream
)
1502 struct cmd_list_element
*c
;
1505 register struct command_line
*cmdlines
;
1507 cmdlines
= c
->user_commands
;
1510 fputs_filtered ("User command ", stream
);
1511 fputs_filtered (c
->name
, stream
);
1512 fputs_filtered (":\n", stream
);
1516 print_command_line (cmdlines
, 4);
1517 cmdlines
= cmdlines
->next
;
1519 fputs_filtered ("\n", stream
);
1524 show_user (args
, from_tty
)
1528 struct cmd_list_element
*c
;
1529 extern struct cmd_list_element
*cmdlist
;
1533 c
= lookup_cmd (&args
, cmdlist
, "", 0, 1);
1534 if (c
->class != class_user
)
1535 error ("Not a user command.");
1536 show_user_1 (c
, gdb_stdout
);
1540 for (c
= cmdlist
; c
; c
= c
->next
)
1542 if (c
->class == class_user
)
1543 show_user_1 (c
, gdb_stdout
);
1549 _initialize_command ()
1551 add_com ("shell", class_support
, shell_escape
,
1552 "Execute the rest of the line as a shell command. \n\
1553 With no arguments, run an inferior shell.");
1556 add_com_alias("!", "shell", class_support
, 0);
1558 add_com ("make", class_support
, make_command
,
1559 "Run the ``make'' program using the rest of the line as arguments.");
1560 add_cmd ("user", no_class
, show_user
,
1561 "Show definitions of user defined commands.\n\
1562 Argument is the name of the user defined command.\n\
1563 With no argument, show definitions of all user defined commands.", &showlist
);