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 static struct cmd_list_element
*find_cmd
PARAMS ((char *command
,
56 struct cmd_list_element
*clist
,
57 int ignore_help_classes
,
60 void _initialize_command
PARAMS ((void));
62 /* Add element named NAME.
63 CLASS is the top level category into which commands are broken down
65 FUN should be the function to execute the command;
66 it will get a character string as argument, with leading
67 and trailing blanks already eliminated.
69 DOC is a documentation string for the command.
70 Its first line should be a complete sentence.
71 It should start with ? for a command that is an abbreviation
72 or with * for a command that most users don't need to know about.
74 Add this command to command list *LIST.
76 Returns a pointer to the added command (not necessarily the head
79 struct cmd_list_element
*
80 add_cmd (name
, class, fun
, doc
, list
)
82 enum command_class
class;
83 void (*fun
) PARAMS ((char *, int));
85 struct cmd_list_element
**list
;
87 register struct cmd_list_element
*c
88 = (struct cmd_list_element
*) xmalloc (sizeof (struct cmd_list_element
));
89 struct cmd_list_element
*p
;
91 delete_cmd (name
, list
);
93 if (*list
== NULL
|| STRCMP ((*list
)->name
, name
) >= 0)
101 while (p
->next
&& STRCMP (p
->next
->name
, name
) <= 0)
111 c
->function
.cfunc
= fun
;
114 c
->prefixlist
= NULL
;
115 c
->prefixname
= NULL
;
116 c
->allow_unknown
= 0;
118 c
->completer
= make_symbol_completion_list
;
119 c
->type
= not_set_cmd
;
121 c
->var_type
= var_boolean
;
123 c
->user_commands
= NULL
;
125 c
->cmd_pointer
= NULL
;
130 /* Same as above, except that the abbrev_flag is set. */
132 #if 0 /* Currently unused */
134 struct cmd_list_element
*
135 add_abbrev_cmd (name
, class, fun
, doc
, list
)
137 enum command_class
class;
138 void (*fun
) PARAMS ((char *, int));
140 struct cmd_list_element
**list
;
142 register struct cmd_list_element
*c
143 = add_cmd (name
, class, fun
, doc
, list
);
151 struct cmd_list_element
*
152 add_alias_cmd (name
, oldname
, class, abbrev_flag
, list
)
155 enum command_class
class;
157 struct cmd_list_element
**list
;
159 /* Must do this since lookup_cmd tries to side-effect its first arg */
161 register struct cmd_list_element
*old
;
162 register struct cmd_list_element
*c
;
163 copied_name
= (char *) alloca (strlen (oldname
) + 1);
164 strcpy (copied_name
, oldname
);
165 old
= lookup_cmd (&copied_name
, *list
, "", 1, 1);
169 delete_cmd (name
, list
);
173 c
= add_cmd (name
, class, old
->function
.cfunc
, old
->doc
, list
);
174 c
->prefixlist
= old
->prefixlist
;
175 c
->prefixname
= old
->prefixname
;
176 c
->allow_unknown
= old
->allow_unknown
;
177 c
->abbrev_flag
= abbrev_flag
;
178 c
->cmd_pointer
= old
;
182 /* Like add_cmd but adds an element for a command prefix:
183 a name that should be followed by a subcommand to be looked up
184 in another command list. PREFIXLIST should be the address
185 of the variable containing that list. */
187 struct cmd_list_element
*
188 add_prefix_cmd (name
, class, fun
, doc
, prefixlist
, prefixname
,
191 enum command_class
class;
192 void (*fun
) PARAMS ((char *, int));
194 struct cmd_list_element
**prefixlist
;
197 struct cmd_list_element
**list
;
199 register struct cmd_list_element
*c
= add_cmd (name
, class, fun
, doc
, list
);
200 c
->prefixlist
= prefixlist
;
201 c
->prefixname
= prefixname
;
202 c
->allow_unknown
= allow_unknown
;
206 /* Like add_prefix_cmd but sets the abbrev_flag on the new command. */
208 struct cmd_list_element
*
209 add_abbrev_prefix_cmd (name
, class, fun
, doc
, prefixlist
, prefixname
,
212 enum command_class
class;
213 void (*fun
) PARAMS ((char *, int));
215 struct cmd_list_element
**prefixlist
;
218 struct cmd_list_element
**list
;
220 register struct cmd_list_element
*c
= add_cmd (name
, class, fun
, doc
, list
);
221 c
->prefixlist
= prefixlist
;
222 c
->prefixname
= prefixname
;
223 c
->allow_unknown
= allow_unknown
;
228 /* This is an empty "cfunc". */
230 not_just_help_class_command (args
, from_tty
)
236 /* This is an empty "sfunc". */
237 static void empty_sfunc
PARAMS ((char *, int, struct cmd_list_element
*));
240 empty_sfunc (args
, from_tty
, c
)
243 struct cmd_list_element
*c
;
247 /* Add element named NAME to command list LIST (the list for set
248 or some sublist thereof).
249 CLASS is as in add_cmd.
250 VAR_TYPE is the kind of thing we are setting.
251 VAR is address of the variable being controlled by this command.
252 DOC is the documentation string. */
254 struct cmd_list_element
*
255 add_set_cmd (name
, class, var_type
, var
, doc
, list
)
257 enum command_class
class;
261 struct cmd_list_element
**list
;
263 struct cmd_list_element
*c
264 = add_cmd (name
, class, NO_FUNCTION
, doc
, list
);
267 c
->var_type
= var_type
;
269 /* This needs to be something besides NO_FUNCTION so that this isn't
270 treated as a help class. */
271 c
->function
.sfunc
= empty_sfunc
;
275 /* Add element named NAME to command list LIST (the list for set
276 or some sublist thereof).
277 CLASS is as in add_cmd.
278 ENUMLIST is a list of strings which may follow NAME.
279 VAR is address of the variable which will contain the matching string
281 DOC is the documentation string. */
283 struct cmd_list_element
*
284 add_set_enum_cmd (name
, class, enumlist
, var
, doc
, list
)
286 enum command_class
class;
290 struct cmd_list_element
**list
;
292 struct cmd_list_element
*c
293 = add_set_cmd (name
, class, var_enum
, var
, doc
, list
);
299 /* Where SETCMD has already been added, add the corresponding show
300 command to LIST and return a pointer to the added command (not
301 necessarily the head of LIST). */
302 struct cmd_list_element
*
303 add_show_from_set (setcmd
, list
)
304 struct cmd_list_element
*setcmd
;
305 struct cmd_list_element
**list
;
307 struct cmd_list_element
*showcmd
=
308 (struct cmd_list_element
*) xmalloc (sizeof (struct cmd_list_element
));
309 struct cmd_list_element
*p
;
311 memcpy (showcmd
, setcmd
, sizeof (struct cmd_list_element
));
312 delete_cmd (showcmd
->name
, list
);
313 showcmd
->type
= show_cmd
;
315 /* Replace "set " at start of docstring with "show ". */
316 if (setcmd
->doc
[0] == 'S' && setcmd
->doc
[1] == 'e'
317 && setcmd
->doc
[2] == 't' && setcmd
->doc
[3] == ' ')
318 showcmd
->doc
= concat ("Show ", setcmd
->doc
+ 4, NULL
);
320 fprintf_unfiltered (gdb_stderr
, "GDB internal error: Bad docstring for set command\n");
322 if (*list
== NULL
|| STRCMP ((*list
)->name
, showcmd
->name
) >= 0)
324 showcmd
->next
= *list
;
330 while (p
->next
&& STRCMP (p
->next
->name
, showcmd
->name
) <= 0)
334 showcmd
->next
= p
->next
;
341 /* Remove the command named NAME from the command list. */
344 delete_cmd (name
, list
)
346 struct cmd_list_element
**list
;
348 register struct cmd_list_element
*c
;
349 struct cmd_list_element
*p
;
351 while (*list
&& STREQ ((*list
)->name
, name
))
354 (*list
)->hookee
->hook
= 0; /* Hook slips out of its mouth */
361 for (c
= *list
; c
->next
;)
363 if (STREQ (c
->next
->name
, name
))
366 c
->next
->hookee
->hook
= 0; /* hooked cmd gets away. */
376 /* This command really has to deal with two things:
377 * 1) I want documentation on *this string* (usually called by
378 * "help commandname").
379 * 2) I want documentation on *this list* (usually called by
380 * giving a command that requires subcommands. Also called by saying
383 * I am going to split this into two seperate comamnds, help_cmd and
388 help_cmd (command
, stream
)
392 struct cmd_list_element
*c
;
393 extern struct cmd_list_element
*cmdlist
;
397 help_list (cmdlist
, "", all_classes
, stream
);
401 c
= lookup_cmd (&command
, cmdlist
, "", 0, 0);
406 /* There are three cases here.
407 If c->prefixlist is nonzero, we have a prefix command.
408 Print its documentation, then list its subcommands.
410 If c->function is nonzero, we really have a command.
411 Print its documentation and return.
413 If c->function is zero, we have a class name.
414 Print its documentation (as if it were a command)
415 and then set class to the number of this class
416 so that the commands in the class will be listed. */
418 fputs_filtered (c
->doc
, stream
);
419 fputs_filtered ("\n", stream
);
421 if (c
->prefixlist
== 0 && c
->function
.cfunc
!= NULL
)
423 fprintf_filtered (stream
, "\n");
425 /* If this is a prefix command, print it's subcommands */
427 help_list (*c
->prefixlist
, c
->prefixname
, all_commands
, stream
);
429 /* If this is a class name, print all of the commands in the class */
430 if (c
->function
.cfunc
== NULL
)
431 help_list (cmdlist
, "", c
->class, stream
);
434 fprintf_filtered (stream
, "\nThis command has a hook defined: %s\n",
439 * Get a specific kind of help on a command list.
442 * CMDTYPE is the prefix to use in the title string.
443 * CLASS is the class with which to list the nodes of this list (see
444 * documentation for help_cmd_list below), As usual, ALL_COMMANDS for
445 * everything, ALL_CLASSES for just classes, and non-negative for only things
446 * in a specific class.
447 * and STREAM is the output stream on which to print things.
448 * If you call this routine with a class >= 0, it recurses.
451 help_list (list
, cmdtype
, class, stream
)
452 struct cmd_list_element
*list
;
454 enum command_class
class;
458 char *cmdtype1
, *cmdtype2
;
460 /* If CMDTYPE is "foo ", CMDTYPE1 gets " foo" and CMDTYPE2 gets "foo sub" */
461 len
= strlen (cmdtype
);
462 cmdtype1
= (char *) alloca (len
+ 1);
464 cmdtype2
= (char *) alloca (len
+ 4);
469 strncpy (cmdtype1
+ 1, cmdtype
, len
- 1);
471 strncpy (cmdtype2
, cmdtype
, len
- 1);
472 strcpy (cmdtype2
+ len
- 1, " sub");
475 if (class == all_classes
)
476 fprintf_filtered (stream
, "List of classes of %scommands:\n\n", cmdtype2
);
478 fprintf_filtered (stream
, "List of %scommands:\n\n", cmdtype2
);
480 help_cmd_list (list
, class, cmdtype
, (int)class >= 0, stream
);
482 if (class == all_classes
)
483 fprintf_filtered (stream
, "\n\
484 Type \"help%s\" followed by a class name for a list of commands in that class.",
487 fprintf_filtered (stream
, "\n\
488 Type \"help%s\" followed by %scommand name for full documentation.\n\
489 Command name abbreviations are allowed if unambiguous.\n",
493 /* Print only the first line of STR on STREAM. */
495 print_doc_line (stream
, str
)
499 static char *line_buffer
= 0;
500 static int line_size
;
506 line_buffer
= (char *) xmalloc (line_size
);
510 while (*p
&& *p
!= '\n' && *p
!= '.' && *p
!= ',')
512 if (p
- str
> line_size
- 1)
514 line_size
= p
- str
+ 1;
515 free ((PTR
)line_buffer
);
516 line_buffer
= (char *) xmalloc (line_size
);
518 strncpy (line_buffer
, str
, p
- str
);
519 line_buffer
[p
- str
] = '\0';
520 if (islower (line_buffer
[0]))
521 line_buffer
[0] = toupper (line_buffer
[0]);
522 fputs_filtered (line_buffer
, stream
);
526 * Implement a help command on command list LIST.
527 * RECURSE should be non-zero if this should be done recursively on
528 * all sublists of LIST.
529 * PREFIX is the prefix to print before each command name.
530 * STREAM is the stream upon which the output should be written.
532 * A non-negative class number to list only commands in that
534 * ALL_COMMANDS to list all commands in list.
535 * ALL_CLASSES to list all classes in list.
537 * Note that RECURSE will be active on *all* sublists, not just the
538 * ones selected by the criteria above (ie. the selection mechanism
539 * is at the low level, not the high-level).
542 help_cmd_list (list
, class, prefix
, recurse
, stream
)
543 struct cmd_list_element
*list
;
544 enum command_class
class;
549 register struct cmd_list_element
*c
;
551 for (c
= list
; c
; c
= c
->next
)
553 if (c
->abbrev_flag
== 0 &&
554 (class == all_commands
555 || (class == all_classes
&& c
->function
.cfunc
== NULL
)
556 || (class == c
->class && c
->function
.cfunc
!= NULL
)))
558 fprintf_filtered (stream
, "%s%s -- ", prefix
, c
->name
);
559 print_doc_line (stream
, c
->doc
);
560 fputs_filtered ("\n", stream
);
563 && c
->prefixlist
!= 0
564 && c
->abbrev_flag
== 0)
565 help_cmd_list (*c
->prefixlist
, class, c
->prefixname
, 1, stream
);
570 /* Search the input clist for 'command'. Return the command if
571 found (or NULL if not), and return the number of commands
574 static struct cmd_list_element
*
575 find_cmd(command
, len
, clist
, ignore_help_classes
, nfound
)
578 struct cmd_list_element
*clist
;
579 int ignore_help_classes
;
582 struct cmd_list_element
*found
, *c
;
584 found
= (struct cmd_list_element
*)NULL
;
586 for (c
= clist
; c
; c
= c
->next
)
587 if (!strncmp (command
, c
->name
, len
)
588 && (!ignore_help_classes
|| c
->function
.cfunc
))
592 if (c
->name
[len
] == '\0')
601 /* This routine takes a line of TEXT and a CLIST in which to start the
602 lookup. When it returns it will have incremented the text pointer past
603 the section of text it matched, set *RESULT_LIST to point to the list in
604 which the last word was matched, and will return a pointer to the cmd
605 list element which the text matches. It will return NULL if no match at
606 all was possible. It will return -1 (cast appropriately, ick) if ambigous
607 matches are possible; in this case *RESULT_LIST will be set to point to
608 the list in which there are ambiguous choices (and *TEXT will be set to
609 the ambiguous text string).
611 If the located command was an abbreviation, this routine returns the base
612 command of the abbreviation.
614 It does no error reporting whatsoever; control will always return
615 to the superior routine.
617 In the case of an ambiguous return (-1), *RESULT_LIST will be set to point
618 at the prefix_command (ie. the best match) *or* (special case) will be NULL
619 if no prefix command was ever found. For example, in the case of "info a",
620 "info" matches without ambiguity, but "a" could be "args" or "address", so
621 *RESULT_LIST is set to the cmd_list_element for "info". So in this case
622 RESULT_LIST should not be interpeted as a pointer to the beginning of a
623 list; it simply points to a specific command. In the case of an ambiguous
624 return *TEXT is advanced past the last non-ambiguous prefix (e.g.
625 "info t" can be "info types" or "info target"; upon return *TEXT has been
626 advanced past "info ").
628 If RESULT_LIST is NULL, don't set *RESULT_LIST (but don't otherwise
629 affect the operation).
631 This routine does *not* modify the text pointed to by TEXT.
633 If IGNORE_HELP_CLASSES is nonzero, ignore any command list elements which
634 are actually help classes rather than commands (i.e. the function field of
635 the struct cmd_list_element is NULL). */
637 struct cmd_list_element
*
638 lookup_cmd_1 (text
, clist
, result_list
, ignore_help_classes
)
640 struct cmd_list_element
*clist
, **result_list
;
641 int ignore_help_classes
;
644 int len
, tmp
, nfound
;
645 struct cmd_list_element
*found
, *c
;
647 while (**text
== ' ' || **text
== '\t')
650 /* Treating underscores as part of command words is important
651 so that "set args_foo()" doesn't get interpreted as
652 "set args _foo()". */
654 *p
&& (isalnum(*p
) || *p
== '-' || *p
== '_' ||
656 (*p
== '+' || *p
== '<' || *p
== '>' || *p
== '$')) ||
657 (xdb_commands
&& (*p
== '!' || *p
== '/' || *p
== '?')));
661 /* If nothing but whitespace, return 0. */
667 /* *text and p now bracket the first command word to lookup (and
668 it's length is len). We copy this into a local temporary */
671 command
= (char *) alloca (len
+ 1);
672 for (tmp
= 0; tmp
< len
; tmp
++)
674 char x
= (*text
)[tmp
];
682 found
= find_cmd(command
, len
, clist
, ignore_help_classes
, &nfound
);
685 ** We didn't find the command in the entered case, so lower case it
688 if (!found
|| nfound
== 0)
690 for (tmp
= 0; tmp
< len
; tmp
++)
692 char x
= command
[tmp
];
693 command
[tmp
] = isupper(x
) ? tolower(x
) : x
;
695 found
= find_cmd(command
, len
, clist
, ignore_help_classes
, &nfound
);
698 /* If nothing matches, we have a simple failure. */
704 if (result_list
!= NULL
)
705 /* Will be modified in calling routine
706 if we know what the prefix command is. */
708 return (struct cmd_list_element
*) -1; /* Ambiguous. */
711 /* We've matched something on this list. Move text pointer forward. */
715 /* If this was an abbreviation, use the base command instead. */
717 if (found
->cmd_pointer
)
718 found
= found
->cmd_pointer
;
720 /* If we found a prefix command, keep looking. */
722 if (found
->prefixlist
)
724 c
= lookup_cmd_1 (text
, *found
->prefixlist
, result_list
,
725 ignore_help_classes
);
728 /* Didn't find anything; this is as far as we got. */
729 if (result_list
!= NULL
)
730 *result_list
= clist
;
733 else if (c
== (struct cmd_list_element
*) -1)
735 /* We've gotten this far properly, but the next step
736 is ambiguous. We need to set the result list to the best
737 we've found (if an inferior hasn't already set it). */
738 if (result_list
!= NULL
)
740 /* This used to say *result_list = *found->prefixlist
741 If that was correct, need to modify the documentation
742 at the top of this function to clarify what is supposed
744 *result_list
= found
;
755 if (result_list
!= NULL
)
756 *result_list
= clist
;
761 /* All this hair to move the space to the front of cmdtype */
764 undef_cmd_error (cmdtype
, q
)
767 error ("Undefined %scommand: \"%s\". Try \"help%s%.*s\".",
775 /* Look up the contents of *LINE as a command in the command list LIST.
776 LIST is a chain of struct cmd_list_element's.
777 If it is found, return the struct cmd_list_element for that command
778 and update *LINE to point after the command name, at the first argument.
779 If not found, call error if ALLOW_UNKNOWN is zero
780 otherwise (or if error returns) return zero.
781 Call error if specified command is ambiguous,
782 unless ALLOW_UNKNOWN is negative.
783 CMDTYPE precedes the word "command" in the error message.
785 If INGNORE_HELP_CLASSES is nonzero, ignore any command list
786 elements which are actually help classes rather than commands (i.e.
787 the function field of the struct cmd_list_element is 0). */
789 struct cmd_list_element
*
790 lookup_cmd (line
, list
, cmdtype
, allow_unknown
, ignore_help_classes
)
792 struct cmd_list_element
*list
;
795 int ignore_help_classes
;
797 struct cmd_list_element
*last_list
= 0;
798 struct cmd_list_element
*c
=
799 lookup_cmd_1 (line
, list
, &last_list
, ignore_help_classes
);
801 /* This is wrong for complete_command. */
802 char *ptr
= (*line
) + strlen (*line
) - 1;
804 /* Clear off trailing whitespace. */
805 while (ptr
>= *line
&& (*ptr
== ' ' || *ptr
== '\t'))
815 error ("Lack of needed %scommand", cmdtype
);
820 while (isalnum(*p
) || *p
== '-')
823 q
= (char *) alloca (p
- *line
+ 1);
824 strncpy (q
, *line
, p
- *line
);
826 undef_cmd_error (cmdtype
, q
);
832 else if (c
== (struct cmd_list_element
*) -1)
834 /* Ambigous. Local values should be off prefixlist or called
836 int local_allow_unknown
= (last_list
? last_list
->allow_unknown
:
838 char *local_cmdtype
= last_list
? last_list
->prefixname
: cmdtype
;
839 struct cmd_list_element
*local_list
=
840 (last_list
? *(last_list
->prefixlist
) : list
);
842 if (local_allow_unknown
< 0)
845 return last_list
; /* Found something. */
847 return 0; /* Found nothing. */
851 /* Report as error. */
856 ((*line
)[amb_len
] && (*line
)[amb_len
] != ' '
857 && (*line
)[amb_len
] != '\t');
862 for (c
= local_list
; c
; c
= c
->next
)
863 if (!strncmp (*line
, c
->name
, amb_len
))
865 if (strlen (ambbuf
) + strlen (c
->name
) + 6 < (int)sizeof ambbuf
)
868 strcat (ambbuf
, ", ");
869 strcat (ambbuf
, c
->name
);
873 strcat (ambbuf
, "..");
877 error ("Ambiguous %scommand \"%s\": %s.", local_cmdtype
,
884 /* We've got something. It may still not be what the caller
885 wants (if this command *needs* a subcommand). */
886 while (**line
== ' ' || **line
== '\t')
889 if (c
->prefixlist
&& **line
&& !c
->allow_unknown
)
890 undef_cmd_error (c
->prefixname
, *line
);
892 /* Seems to be what he wants. Return it. */
899 /* Look up the contents of *LINE as a command in the command list LIST.
900 LIST is a chain of struct cmd_list_element's.
901 If it is found, return the struct cmd_list_element for that command
902 and update *LINE to point after the command name, at the first argument.
903 If not found, call error if ALLOW_UNKNOWN is zero
904 otherwise (or if error returns) return zero.
905 Call error if specified command is ambiguous,
906 unless ALLOW_UNKNOWN is negative.
907 CMDTYPE precedes the word "command" in the error message. */
909 struct cmd_list_element
*
910 lookup_cmd (line
, list
, cmdtype
, allow_unknown
)
912 struct cmd_list_element
*list
;
917 register struct cmd_list_element
*c
, *found
;
923 /* Skip leading whitespace. */
925 while (**line
== ' ' || **line
== '\t')
928 /* Clear out trailing whitespace. */
930 p
= *line
+ strlen (*line
);
931 while (p
!= *line
&& (p
[-1] == ' ' || p
[-1] == '\t'))
935 /* Find end of command name. */
938 while (*p
== '-' || isalnum(*p
))
941 /* Look up the command name.
942 If exact match, keep that.
943 Otherwise, take command abbreviated, if unique. Note that (in my
944 opinion) a null string does *not* indicate ambiguity; simply the
945 end of the argument. */
950 error ("Lack of needed %scommand", cmdtype
);
954 /* Copy over to a local buffer, converting to lowercase on the way.
955 This is in case the command being parsed is a subcommand which
956 doesn't match anything, and that's ok. We want the original
957 untouched for the routine of the original command. */
959 processed_cmd
= (char *) alloca (p
- *line
+ 1);
960 for (cmd_len
= 0; cmd_len
< p
- *line
; cmd_len
++)
962 char x
= (*line
)[cmd_len
];
964 processed_cmd
[cmd_len
] = tolower(x
);
966 processed_cmd
[cmd_len
] = x
;
968 processed_cmd
[cmd_len
] = '\0';
970 /* Check all possibilities in the current command list. */
973 for (c
= list
; c
; c
= c
->next
)
975 if (!strncmp (processed_cmd
, c
->name
, cmd_len
))
979 if (c
->name
[cmd_len
] == 0)
987 /* Report error for undefined command name. */
991 if (nfound
> 1 && allow_unknown
>= 0)
994 for (c
= list
; c
; c
= c
->next
)
995 if (!strncmp (processed_cmd
, c
->name
, cmd_len
))
997 if (strlen (ambbuf
) + strlen (c
->name
) + 6 < sizeof ambbuf
)
1000 strcat (ambbuf
, ", ");
1001 strcat (ambbuf
, c
->name
);
1005 strcat (ambbuf
, "..");
1009 error ("Ambiguous %scommand \"%s\": %s.", cmdtype
,
1010 processed_cmd
, ambbuf
);
1012 else if (!allow_unknown
)
1013 error ("Undefined %scommand: \"%s\".", cmdtype
, processed_cmd
);
1017 /* Skip whitespace before the argument. */
1019 while (*p
== ' ' || *p
== '\t') p
++;
1022 if (found
->prefixlist
&& *p
)
1024 c
= lookup_cmd (line
, *found
->prefixlist
, found
->prefixname
,
1025 found
->allow_unknown
);
1034 /* Helper function for SYMBOL_COMPLETION_FUNCTION. */
1036 /* Return a vector of char pointers which point to the different
1037 possible completions in LIST of TEXT.
1039 WORD points in the same buffer as TEXT, and completions should be
1040 returned relative to this position. For example, suppose TEXT is "foo"
1041 and we want to complete to "foobar". If WORD is "oo", return
1042 "oobar"; if WORD is "baz/foo", return "baz/foobar". */
1045 complete_on_cmdlist (list
, text
, word
)
1046 struct cmd_list_element
*list
;
1050 struct cmd_list_element
*ptr
;
1052 int sizeof_matchlist
;
1054 int textlen
= strlen (text
);
1056 sizeof_matchlist
= 10;
1057 matchlist
= (char **) xmalloc (sizeof_matchlist
* sizeof (char *));
1060 for (ptr
= list
; ptr
; ptr
= ptr
->next
)
1061 if (!strncmp (ptr
->name
, text
, textlen
)
1062 && !ptr
->abbrev_flag
1063 && (ptr
->function
.cfunc
1064 || ptr
->prefixlist
))
1066 if (matches
== sizeof_matchlist
)
1068 sizeof_matchlist
*= 2;
1069 matchlist
= (char **) xrealloc ((char *)matchlist
,
1071 * sizeof (char *)));
1074 matchlist
[matches
] = (char *)
1075 xmalloc (strlen (word
) + strlen (ptr
->name
) + 1);
1077 strcpy (matchlist
[matches
], ptr
->name
);
1078 else if (word
> text
)
1080 /* Return some portion of ptr->name. */
1081 strcpy (matchlist
[matches
], ptr
->name
+ (word
- text
));
1085 /* Return some of text plus ptr->name. */
1086 strncpy (matchlist
[matches
], word
, text
- word
);
1087 matchlist
[matches
][text
- word
] = '\0';
1088 strcat (matchlist
[matches
], ptr
->name
);
1095 free ((PTR
)matchlist
);
1100 matchlist
= (char **) xrealloc ((char *)matchlist
, ((matches
+ 1)
1101 * sizeof (char *)));
1102 matchlist
[matches
] = (char *) 0;
1108 /* Helper function for SYMBOL_COMPLETION_FUNCTION. */
1110 /* Return a vector of char pointers which point to the different
1111 possible completions in CMD of TEXT.
1113 WORD points in the same buffer as TEXT, and completions should be
1114 returned relative to this position. For example, suppose TEXT is "foo"
1115 and we want to complete to "foobar". If WORD is "oo", return
1116 "oobar"; if WORD is "baz/foo", return "baz/foobar". */
1119 complete_on_enum (enumlist
, text
, word
)
1125 int sizeof_matchlist
;
1127 int textlen
= strlen (text
);
1131 sizeof_matchlist
= 10;
1132 matchlist
= (char **) xmalloc (sizeof_matchlist
* sizeof (char *));
1135 for (i
= 0; (name
= enumlist
[i
]) != NULL
; i
++)
1136 if (strncmp (name
, text
, textlen
) == 0)
1138 if (matches
== sizeof_matchlist
)
1140 sizeof_matchlist
*= 2;
1141 matchlist
= (char **) xrealloc ((char *)matchlist
,
1143 * sizeof (char *)));
1146 matchlist
[matches
] = (char *)
1147 xmalloc (strlen (word
) + strlen (name
) + 1);
1149 strcpy (matchlist
[matches
], name
);
1150 else if (word
> text
)
1152 /* Return some portion of name. */
1153 strcpy (matchlist
[matches
], name
+ (word
- text
));
1157 /* Return some of text plus name. */
1158 strncpy (matchlist
[matches
], word
, text
- word
);
1159 matchlist
[matches
][text
- word
] = '\0';
1160 strcat (matchlist
[matches
], name
);
1167 free ((PTR
)matchlist
);
1172 matchlist
= (char **) xrealloc ((char *)matchlist
, ((matches
+ 1)
1173 * sizeof (char *)));
1174 matchlist
[matches
] = (char *) 0;
1181 parse_binary_operation (arg
)
1189 length
= strlen (arg
);
1191 while (arg
[length
- 1] == ' ' || arg
[length
- 1] == '\t')
1194 if (!strncmp (arg
, "on", length
)
1195 || !strncmp (arg
, "1", length
)
1196 || !strncmp (arg
, "yes", length
))
1199 if (!strncmp (arg
, "off", length
)
1200 || !strncmp (arg
, "0", length
)
1201 || !strncmp (arg
, "no", length
))
1205 error ("\"on\" or \"off\" expected.");
1210 /* Do a "set" or "show" command. ARG is NULL if no argument, or the text
1211 of the argument, and FROM_TTY is nonzero if this command is being entered
1212 directly by the user (i.e. these are just like any other
1213 command). C is the command list element for the command. */
1215 do_setshow_command (arg
, from_tty
, c
)
1218 struct cmd_list_element
*c
;
1220 if (c
->type
== set_cmd
)
1222 switch (c
->var_type
)
1233 new = (char *) xmalloc (strlen (arg
) + 2);
1235 while ((ch
= *p
++) != '\000')
1239 /* \ at end of argument is used after spaces
1240 so they won't be lost. */
1241 /* This is obsolete now that we no longer strip
1242 trailing whitespace and actually, the backslash
1243 didn't get here in my test, readline or
1244 something did something funky with a backslash
1245 right before a newline. */
1248 ch
= parse_escape (&p
);
1250 break; /* C loses */
1258 if (*(p
- 1) != '\\')
1262 new = (char *) xrealloc (new, q
- new);
1263 if (*(char **)c
->var
!= NULL
)
1264 free (*(char **)c
->var
);
1265 *(char **) c
->var
= new;
1268 case var_string_noescape
:
1271 if (*(char **)c
->var
!= NULL
)
1272 free (*(char **)c
->var
);
1273 *(char **) c
->var
= savestring (arg
, strlen (arg
));
1277 error_no_arg ("filename to set it to.");
1278 if (*(char **)c
->var
!= NULL
)
1279 free (*(char **)c
->var
);
1280 *(char **)c
->var
= tilde_expand (arg
);
1283 *(int *) c
->var
= parse_binary_operation (arg
);
1287 error_no_arg ("integer to set it to.");
1288 *(unsigned int *) c
->var
= parse_and_eval_address (arg
);
1289 if (*(unsigned int *) c
->var
== 0)
1290 *(unsigned int *) c
->var
= UINT_MAX
;
1296 error_no_arg ("integer to set it to.");
1297 val
= parse_and_eval_address (arg
);
1299 *(int *) c
->var
= INT_MAX
;
1300 else if (val
>= INT_MAX
)
1301 error ("integer %u out of range", val
);
1303 *(int *) c
->var
= val
;
1308 error_no_arg ("integer to set it to.");
1309 *(int *) c
->var
= parse_and_eval_address (arg
);
1319 /* if no argument was supplied, print an informative error message */
1323 strcpy (msg
, "Requires an argument. Valid arguments are ");
1324 for (i
= 0; c
->enums
[i
]; i
++)
1328 strcat (msg
, c
->enums
[i
]);
1334 p
= strchr (arg
, ' ');
1342 for (i
= 0; c
->enums
[i
]; i
++)
1343 if (strncmp (arg
, c
->enums
[i
], len
) == 0)
1345 match
= c
->enums
[i
];
1350 error ("Undefined item: \"%s\".", arg
);
1353 error ("Ambiguous item \"%s\".", arg
);
1355 *(char **)c
->var
= match
;
1359 error ("gdb internal error: bad var_type in do_setshow_command");
1362 else if (c
->type
== show_cmd
)
1364 /* Print doc minus "show" at start. */
1365 print_doc_line (gdb_stdout
, c
->doc
+ 5);
1367 fputs_filtered (" is ", gdb_stdout
);
1369 switch (c
->var_type
)
1375 fputs_filtered ("\"", gdb_stdout
);
1376 if (*(unsigned char **)c
->var
)
1377 for (p
= *(unsigned char **) c
->var
; *p
!= '\0'; p
++)
1378 gdb_printchar (*p
, gdb_stdout
, '"');
1379 fputs_filtered ("\"", gdb_stdout
);
1382 case var_string_noescape
:
1385 fputs_filtered ("\"", gdb_stdout
);
1386 if (*(char **)c
->var
)
1387 fputs_filtered (*(char **) c
->var
, gdb_stdout
);
1388 fputs_filtered ("\"", gdb_stdout
);
1391 fputs_filtered (*(int *) c
->var
? "on" : "off", gdb_stdout
);
1394 if (*(unsigned int *) c
->var
== UINT_MAX
) {
1395 fputs_filtered ("unlimited", gdb_stdout
);
1398 /* else fall through */
1400 fprintf_filtered (gdb_stdout
, "%u", *(unsigned int *) c
->var
);
1403 if (*(int *) c
->var
== INT_MAX
)
1405 fputs_filtered ("unlimited", gdb_stdout
);
1408 fprintf_filtered (gdb_stdout
, "%d", *(int *) c
->var
);
1412 error ("gdb internal error: bad var_type in do_setshow_command");
1414 fputs_filtered (".\n", gdb_stdout
);
1417 error ("gdb internal error: bad cmd_type in do_setshow_command");
1418 (*c
->function
.sfunc
) (NULL
, from_tty
, c
);
1421 /* Show all the settings in a list of show commands. */
1424 cmd_show_list (list
, from_tty
, prefix
)
1425 struct cmd_list_element
*list
;
1429 for (; list
!= NULL
; list
= list
->next
) {
1430 /* If we find a prefix, run its list, prefixing our output by its
1431 prefix (with "show " skipped). */
1432 if (list
->prefixlist
&& !list
->abbrev_flag
)
1433 cmd_show_list (*list
->prefixlist
, from_tty
, list
->prefixname
+ 5);
1434 if (list
->type
== show_cmd
)
1436 fputs_filtered (prefix
, gdb_stdout
);
1437 fputs_filtered (list
->name
, gdb_stdout
);
1438 fputs_filtered (": ", gdb_stdout
);
1439 do_setshow_command ((char *)NULL
, from_tty
, list
);
1446 shell_escape (arg
, from_tty
)
1451 /* FIXME: what about errors (I don't know how GO32 system() handles
1454 #else /* Can fork. */
1455 int rc
, status
, pid
;
1456 char *p
, *user_shell
;
1458 if ((user_shell
= (char *) getenv ("SHELL")) == NULL
)
1459 user_shell
= "/bin/sh";
1461 /* Get the name of the shell for arg0 */
1462 if ((p
= strrchr (user_shell
, '/')) == NULL
)
1465 p
++; /* Get past '/' */
1467 if ((pid
= fork()) == 0)
1470 execl (user_shell
, p
, 0);
1472 execl (user_shell
, p
, "-c", arg
, 0);
1474 fprintf_unfiltered (gdb_stderr
, "Cannot execute %s: %s\n", user_shell
,
1475 safe_strerror (errno
));
1476 gdb_flush (gdb_stderr
);
1481 while ((rc
= wait (&status
)) != pid
&& rc
!= -1)
1484 error ("Fork failed");
1485 #endif /* Can fork. */
1489 make_command (arg
, from_tty
)
1499 p
= xmalloc (sizeof("make ") + strlen(arg
));
1500 strcpy (p
, "make ");
1501 strcpy (p
+ sizeof("make ")-1, arg
);
1504 shell_escape (p
, from_tty
);
1508 show_user_1 (c
, stream
)
1509 struct cmd_list_element
*c
;
1512 register struct command_line
*cmdlines
;
1514 cmdlines
= c
->user_commands
;
1517 fputs_filtered ("User command ", stream
);
1518 fputs_filtered (c
->name
, stream
);
1519 fputs_filtered (":\n", stream
);
1523 print_command_line (cmdlines
, 4, stream
);
1524 cmdlines
= cmdlines
->next
;
1526 fputs_filtered ("\n", stream
);
1531 show_user (args
, from_tty
)
1535 struct cmd_list_element
*c
;
1536 extern struct cmd_list_element
*cmdlist
;
1540 c
= lookup_cmd (&args
, cmdlist
, "", 0, 1);
1541 if (c
->class != class_user
)
1542 error ("Not a user command.");
1543 show_user_1 (c
, gdb_stdout
);
1547 for (c
= cmdlist
; c
; c
= c
->next
)
1549 if (c
->class == class_user
)
1550 show_user_1 (c
, gdb_stdout
);
1556 _initialize_command ()
1558 add_com ("shell", class_support
, shell_escape
,
1559 "Execute the rest of the line as a shell command. \n\
1560 With no arguments, run an inferior shell.");
1563 add_com_alias("!", "shell", class_support
, 0);
1565 add_com ("make", class_support
, make_command
,
1566 "Run the ``make'' program using the rest of the line as arguments.");
1567 add_cmd ("user", no_class
, show_user
,
1568 "Show definitions of user defined commands.\n\
1569 Argument is the name of the user defined command.\n\
1570 With no argument, show definitions of all user defined commands.", &showlist
);