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
);
563 /* This routine takes a line of TEXT and a CLIST in which to start the
564 lookup. When it returns it will have incremented the text pointer past
565 the section of text it matched, set *RESULT_LIST to point to the list in
566 which the last word was matched, and will return a pointer to the cmd
567 list element which the text matches. It will return NULL if no match at
568 all was possible. It will return -1 (cast appropriately, ick) if ambigous
569 matches are possible; in this case *RESULT_LIST will be set to point to
570 the list in which there are ambiguous choices (and *TEXT will be set to
571 the ambiguous text string).
573 If the located command was an abbreviation, this routine returns the base
574 command of the abbreviation.
576 It does no error reporting whatsoever; control will always return
577 to the superior routine.
579 In the case of an ambiguous return (-1), *RESULT_LIST will be set to point
580 at the prefix_command (ie. the best match) *or* (special case) will be NULL
581 if no prefix command was ever found. For example, in the case of "info a",
582 "info" matches without ambiguity, but "a" could be "args" or "address", so
583 *RESULT_LIST is set to the cmd_list_element for "info". So in this case
584 RESULT_LIST should not be interpeted as a pointer to the beginning of a
585 list; it simply points to a specific command. In the case of an ambiguous
586 return *TEXT is advanced past the last non-ambiguous prefix (e.g.
587 "info t" can be "info types" or "info target"; upon return *TEXT has been
588 advanced past "info ").
590 If RESULT_LIST is NULL, don't set *RESULT_LIST (but don't otherwise
591 affect the operation).
593 This routine does *not* modify the text pointed to by TEXT.
595 If IGNORE_HELP_CLASSES is nonzero, ignore any command list elements which
596 are actually help classes rather than commands (i.e. the function field of
597 the struct cmd_list_element is NULL). */
599 struct cmd_list_element
*
600 lookup_cmd_1 (text
, clist
, result_list
, ignore_help_classes
)
602 struct cmd_list_element
*clist
, **result_list
;
603 int ignore_help_classes
;
606 int len
, tmp
, nfound
;
607 struct cmd_list_element
*found
, *c
;
609 while (**text
== ' ' || **text
== '\t')
612 /* Treating underscores as part of command words is important
613 so that "set args_foo()" doesn't get interpreted as
614 "set args _foo()". */
616 *p
&& (isalnum(*p
) || *p
== '-' || *p
== '_');
620 /* If nothing but whitespace, return 0. */
626 /* *text and p now bracket the first command word to lookup (and
627 it's length is len). We copy this into a local temporary,
628 converting to lower case as we go. */
630 command
= (char *) alloca (len
+ 1);
631 for (tmp
= 0; tmp
< len
; tmp
++)
633 char x
= (*text
)[tmp
];
634 command
[tmp
] = isupper(x
) ? tolower(x
) : x
;
641 for (c
= clist
; c
; c
= c
->next
)
642 if (!strncmp (command
, c
->name
, len
)
643 && (!ignore_help_classes
|| c
->function
.cfunc
))
647 if (c
->name
[len
] == '\0')
654 /* If nothing matches, we have a simple failure. */
660 if (result_list
!= NULL
)
661 /* Will be modified in calling routine
662 if we know what the prefix command is. */
664 return (struct cmd_list_element
*) -1; /* Ambiguous. */
667 /* We've matched something on this list. Move text pointer forward. */
671 /* If this was an abbreviation, use the base command instead. */
673 if (found
->cmd_pointer
)
674 found
= found
->cmd_pointer
;
676 /* If we found a prefix command, keep looking. */
678 if (found
->prefixlist
)
680 c
= lookup_cmd_1 (text
, *found
->prefixlist
, result_list
,
681 ignore_help_classes
);
684 /* Didn't find anything; this is as far as we got. */
685 if (result_list
!= NULL
)
686 *result_list
= clist
;
689 else if (c
== (struct cmd_list_element
*) -1)
691 /* We've gotten this far properly, but the next step
692 is ambiguous. We need to set the result list to the best
693 we've found (if an inferior hasn't already set it). */
694 if (result_list
!= NULL
)
696 /* This used to say *result_list = *found->prefixlist
697 If that was correct, need to modify the documentation
698 at the top of this function to clarify what is supposed
700 *result_list
= found
;
711 if (result_list
!= NULL
)
712 *result_list
= clist
;
717 /* All this hair to move the space to the front of cmdtype */
720 undef_cmd_error (cmdtype
, q
)
723 error ("Undefined %scommand: \"%s\". Try \"help%s%.*s\".",
731 /* Look up the contents of *LINE as a command in the command list LIST.
732 LIST is a chain of struct cmd_list_element's.
733 If it is found, return the struct cmd_list_element for that command
734 and update *LINE to point after the command name, at the first argument.
735 If not found, call error if ALLOW_UNKNOWN is zero
736 otherwise (or if error returns) return zero.
737 Call error if specified command is ambiguous,
738 unless ALLOW_UNKNOWN is negative.
739 CMDTYPE precedes the word "command" in the error message.
741 If INGNORE_HELP_CLASSES is nonzero, ignore any command list
742 elements which are actually help classes rather than commands (i.e.
743 the function field of the struct cmd_list_element is 0). */
745 struct cmd_list_element
*
746 lookup_cmd (line
, list
, cmdtype
, allow_unknown
, ignore_help_classes
)
748 struct cmd_list_element
*list
;
751 int ignore_help_classes
;
753 struct cmd_list_element
*last_list
= 0;
754 struct cmd_list_element
*c
=
755 lookup_cmd_1 (line
, list
, &last_list
, ignore_help_classes
);
757 /* This is wrong for complete_command. */
758 char *ptr
= (*line
) + strlen (*line
) - 1;
760 /* Clear off trailing whitespace. */
761 while (ptr
>= *line
&& (*ptr
== ' ' || *ptr
== '\t'))
771 error ("Lack of needed %scommand", cmdtype
);
776 while (isalnum(*p
) || *p
== '-')
779 q
= (char *) alloca (p
- *line
+ 1);
780 strncpy (q
, *line
, p
- *line
);
782 undef_cmd_error (cmdtype
, q
);
788 else if (c
== (struct cmd_list_element
*) -1)
790 /* Ambigous. Local values should be off prefixlist or called
792 int local_allow_unknown
= (last_list
? last_list
->allow_unknown
:
794 char *local_cmdtype
= last_list
? last_list
->prefixname
: cmdtype
;
795 struct cmd_list_element
*local_list
=
796 (last_list
? *(last_list
->prefixlist
) : list
);
798 if (local_allow_unknown
< 0)
801 return last_list
; /* Found something. */
803 return 0; /* Found nothing. */
807 /* Report as error. */
812 ((*line
)[amb_len
] && (*line
)[amb_len
] != ' '
813 && (*line
)[amb_len
] != '\t');
818 for (c
= local_list
; c
; c
= c
->next
)
819 if (!strncmp (*line
, c
->name
, amb_len
))
821 if (strlen (ambbuf
) + strlen (c
->name
) + 6 < (int)sizeof ambbuf
)
824 strcat (ambbuf
, ", ");
825 strcat (ambbuf
, c
->name
);
829 strcat (ambbuf
, "..");
833 error ("Ambiguous %scommand \"%s\": %s.", local_cmdtype
,
840 /* We've got something. It may still not be what the caller
841 wants (if this command *needs* a subcommand). */
842 while (**line
== ' ' || **line
== '\t')
845 if (c
->prefixlist
&& **line
&& !c
->allow_unknown
)
846 undef_cmd_error (c
->prefixname
, *line
);
848 /* Seems to be what he wants. Return it. */
855 /* Look up the contents of *LINE as a command in the command list LIST.
856 LIST is a chain of struct cmd_list_element's.
857 If it is found, return the struct cmd_list_element for that command
858 and update *LINE to point after the command name, at the first argument.
859 If not found, call error if ALLOW_UNKNOWN is zero
860 otherwise (or if error returns) return zero.
861 Call error if specified command is ambiguous,
862 unless ALLOW_UNKNOWN is negative.
863 CMDTYPE precedes the word "command" in the error message. */
865 struct cmd_list_element
*
866 lookup_cmd (line
, list
, cmdtype
, allow_unknown
)
868 struct cmd_list_element
*list
;
873 register struct cmd_list_element
*c
, *found
;
879 /* Skip leading whitespace. */
881 while (**line
== ' ' || **line
== '\t')
884 /* Clear out trailing whitespace. */
886 p
= *line
+ strlen (*line
);
887 while (p
!= *line
&& (p
[-1] == ' ' || p
[-1] == '\t'))
891 /* Find end of command name. */
894 while (*p
== '-' || isalnum(*p
))
897 /* Look up the command name.
898 If exact match, keep that.
899 Otherwise, take command abbreviated, if unique. Note that (in my
900 opinion) a null string does *not* indicate ambiguity; simply the
901 end of the argument. */
906 error ("Lack of needed %scommand", cmdtype
);
910 /* Copy over to a local buffer, converting to lowercase on the way.
911 This is in case the command being parsed is a subcommand which
912 doesn't match anything, and that's ok. We want the original
913 untouched for the routine of the original command. */
915 processed_cmd
= (char *) alloca (p
- *line
+ 1);
916 for (cmd_len
= 0; cmd_len
< p
- *line
; cmd_len
++)
918 char x
= (*line
)[cmd_len
];
920 processed_cmd
[cmd_len
] = tolower(x
);
922 processed_cmd
[cmd_len
] = x
;
924 processed_cmd
[cmd_len
] = '\0';
926 /* Check all possibilities in the current command list. */
929 for (c
= list
; c
; c
= c
->next
)
931 if (!strncmp (processed_cmd
, c
->name
, cmd_len
))
935 if (c
->name
[cmd_len
] == 0)
943 /* Report error for undefined command name. */
947 if (nfound
> 1 && allow_unknown
>= 0)
950 for (c
= list
; c
; c
= c
->next
)
951 if (!strncmp (processed_cmd
, c
->name
, cmd_len
))
953 if (strlen (ambbuf
) + strlen (c
->name
) + 6 < sizeof ambbuf
)
956 strcat (ambbuf
, ", ");
957 strcat (ambbuf
, c
->name
);
961 strcat (ambbuf
, "..");
965 error ("Ambiguous %scommand \"%s\": %s.", cmdtype
,
966 processed_cmd
, ambbuf
);
968 else if (!allow_unknown
)
969 error ("Undefined %scommand: \"%s\".", cmdtype
, processed_cmd
);
973 /* Skip whitespace before the argument. */
975 while (*p
== ' ' || *p
== '\t') p
++;
978 if (found
->prefixlist
&& *p
)
980 c
= lookup_cmd (line
, *found
->prefixlist
, found
->prefixname
,
981 found
->allow_unknown
);
990 /* Helper function for SYMBOL_COMPLETION_FUNCTION. */
992 /* Return a vector of char pointers which point to the different
993 possible completions in LIST of TEXT.
995 WORD points in the same buffer as TEXT, and completions should be
996 returned relative to this position. For example, suppose TEXT is "foo"
997 and we want to complete to "foobar". If WORD is "oo", return
998 "oobar"; if WORD is "baz/foo", return "baz/foobar". */
1001 complete_on_cmdlist (list
, text
, word
)
1002 struct cmd_list_element
*list
;
1006 struct cmd_list_element
*ptr
;
1008 int sizeof_matchlist
;
1010 int textlen
= strlen (text
);
1012 sizeof_matchlist
= 10;
1013 matchlist
= (char **) xmalloc (sizeof_matchlist
* sizeof (char *));
1016 for (ptr
= list
; ptr
; ptr
= ptr
->next
)
1017 if (!strncmp (ptr
->name
, text
, textlen
)
1018 && !ptr
->abbrev_flag
1019 && (ptr
->function
.cfunc
1020 || ptr
->prefixlist
))
1022 if (matches
== sizeof_matchlist
)
1024 sizeof_matchlist
*= 2;
1025 matchlist
= (char **) xrealloc ((char *)matchlist
,
1027 * sizeof (char *)));
1030 matchlist
[matches
] = (char *)
1031 xmalloc (strlen (word
) + strlen (ptr
->name
) + 1);
1033 strcpy (matchlist
[matches
], ptr
->name
);
1034 else if (word
> text
)
1036 /* Return some portion of ptr->name. */
1037 strcpy (matchlist
[matches
], ptr
->name
+ (word
- text
));
1041 /* Return some of text plus ptr->name. */
1042 strncpy (matchlist
[matches
], word
, text
- word
);
1043 matchlist
[matches
][text
- word
] = '\0';
1044 strcat (matchlist
[matches
], ptr
->name
);
1051 free ((PTR
)matchlist
);
1056 matchlist
= (char **) xrealloc ((char *)matchlist
, ((matches
+ 1)
1057 * sizeof (char *)));
1058 matchlist
[matches
] = (char *) 0;
1064 /* Helper function for SYMBOL_COMPLETION_FUNCTION. */
1066 /* Return a vector of char pointers which point to the different
1067 possible completions in CMD of TEXT.
1069 WORD points in the same buffer as TEXT, and completions should be
1070 returned relative to this position. For example, suppose TEXT is "foo"
1071 and we want to complete to "foobar". If WORD is "oo", return
1072 "oobar"; if WORD is "baz/foo", return "baz/foobar". */
1075 complete_on_enum (enumlist
, text
, word
)
1081 int sizeof_matchlist
;
1083 int textlen
= strlen (text
);
1087 sizeof_matchlist
= 10;
1088 matchlist
= (char **) xmalloc (sizeof_matchlist
* sizeof (char *));
1091 for (i
= 0; (name
= enumlist
[i
]) != NULL
; i
++)
1092 if (strncmp (name
, text
, textlen
) == 0)
1094 if (matches
== sizeof_matchlist
)
1096 sizeof_matchlist
*= 2;
1097 matchlist
= (char **) xrealloc ((char *)matchlist
,
1099 * sizeof (char *)));
1102 matchlist
[matches
] = (char *)
1103 xmalloc (strlen (word
) + strlen (name
) + 1);
1105 strcpy (matchlist
[matches
], name
);
1106 else if (word
> text
)
1108 /* Return some portion of name. */
1109 strcpy (matchlist
[matches
], name
+ (word
- text
));
1113 /* Return some of text plus name. */
1114 strncpy (matchlist
[matches
], word
, text
- word
);
1115 matchlist
[matches
][text
- word
] = '\0';
1116 strcat (matchlist
[matches
], name
);
1123 free ((PTR
)matchlist
);
1128 matchlist
= (char **) xrealloc ((char *)matchlist
, ((matches
+ 1)
1129 * sizeof (char *)));
1130 matchlist
[matches
] = (char *) 0;
1137 parse_binary_operation (arg
)
1145 length
= strlen (arg
);
1147 while (arg
[length
- 1] == ' ' || arg
[length
- 1] == '\t')
1150 if (!strncmp (arg
, "on", length
)
1151 || !strncmp (arg
, "1", length
)
1152 || !strncmp (arg
, "yes", length
))
1155 if (!strncmp (arg
, "off", length
)
1156 || !strncmp (arg
, "0", length
)
1157 || !strncmp (arg
, "no", length
))
1161 error ("\"on\" or \"off\" expected.");
1166 /* Do a "set" or "show" command. ARG is NULL if no argument, or the text
1167 of the argument, and FROM_TTY is nonzero if this command is being entered
1168 directly by the user (i.e. these are just like any other
1169 command). C is the command list element for the command. */
1171 do_setshow_command (arg
, from_tty
, c
)
1174 struct cmd_list_element
*c
;
1176 if (c
->type
== set_cmd
)
1178 switch (c
->var_type
)
1189 new = (char *) xmalloc (strlen (arg
) + 2);
1191 while ((ch
= *p
++) != '\000')
1195 /* \ at end of argument is used after spaces
1196 so they won't be lost. */
1197 /* This is obsolete now that we no longer strip
1198 trailing whitespace and actually, the backslash
1199 didn't get here in my test, readline or
1200 something did something funky with a backslash
1201 right before a newline. */
1204 ch
= parse_escape (&p
);
1206 break; /* C loses */
1214 if (*(p
- 1) != '\\')
1218 new = (char *) xrealloc (new, q
- new);
1219 if (*(char **)c
->var
!= NULL
)
1220 free (*(char **)c
->var
);
1221 *(char **) c
->var
= new;
1224 case var_string_noescape
:
1227 if (*(char **)c
->var
!= NULL
)
1228 free (*(char **)c
->var
);
1229 *(char **) c
->var
= savestring (arg
, strlen (arg
));
1233 error_no_arg ("filename to set it to.");
1234 if (*(char **)c
->var
!= NULL
)
1235 free (*(char **)c
->var
);
1236 *(char **)c
->var
= tilde_expand (arg
);
1239 *(int *) c
->var
= parse_binary_operation (arg
);
1243 error_no_arg ("integer to set it to.");
1244 *(unsigned int *) c
->var
= parse_and_eval_address (arg
);
1245 if (*(unsigned int *) c
->var
== 0)
1246 *(unsigned int *) c
->var
= UINT_MAX
;
1252 error_no_arg ("integer to set it to.");
1253 val
= parse_and_eval_address (arg
);
1255 *(int *) c
->var
= INT_MAX
;
1256 else if (val
>= INT_MAX
)
1257 error ("integer %u out of range", val
);
1259 *(int *) c
->var
= val
;
1264 error_no_arg ("integer to set it to.");
1265 *(int *) c
->var
= parse_and_eval_address (arg
);
1275 /* if no argument was supplied, print an informative error message */
1279 strcpy (msg
, "Requires an argument. Valid arguments are ");
1280 for (i
= 0; c
->enums
[i
]; i
++)
1284 strcat (msg
, c
->enums
[i
]);
1290 p
= strchr (arg
, ' ');
1298 for (i
= 0; c
->enums
[i
]; i
++)
1299 if (strncmp (arg
, c
->enums
[i
], len
) == 0)
1301 match
= c
->enums
[i
];
1306 error ("Undefined item: \"%s\".", arg
);
1309 error ("Ambiguous item \"%s\".", arg
);
1311 *(char **)c
->var
= match
;
1315 error ("gdb internal error: bad var_type in do_setshow_command");
1318 else if (c
->type
== show_cmd
)
1320 /* Print doc minus "show" at start. */
1321 print_doc_line (gdb_stdout
, c
->doc
+ 5);
1323 fputs_filtered (" is ", gdb_stdout
);
1325 switch (c
->var_type
)
1331 fputs_filtered ("\"", gdb_stdout
);
1332 if (*(unsigned char **)c
->var
)
1333 for (p
= *(unsigned char **) c
->var
; *p
!= '\0'; p
++)
1334 gdb_printchar (*p
, gdb_stdout
, '"');
1335 fputs_filtered ("\"", gdb_stdout
);
1338 case var_string_noescape
:
1341 fputs_filtered ("\"", gdb_stdout
);
1342 if (*(char **)c
->var
)
1343 fputs_filtered (*(char **) c
->var
, gdb_stdout
);
1344 fputs_filtered ("\"", gdb_stdout
);
1347 fputs_filtered (*(int *) c
->var
? "on" : "off", gdb_stdout
);
1350 if (*(unsigned int *) c
->var
== UINT_MAX
) {
1351 fputs_filtered ("unlimited", gdb_stdout
);
1354 /* else fall through */
1356 fprintf_filtered (gdb_stdout
, "%u", *(unsigned int *) c
->var
);
1359 if (*(int *) c
->var
== INT_MAX
)
1361 fputs_filtered ("unlimited", gdb_stdout
);
1364 fprintf_filtered (gdb_stdout
, "%d", *(int *) c
->var
);
1368 error ("gdb internal error: bad var_type in do_setshow_command");
1370 fputs_filtered (".\n", gdb_stdout
);
1373 error ("gdb internal error: bad cmd_type in do_setshow_command");
1374 (*c
->function
.sfunc
) (NULL
, from_tty
, c
);
1377 /* Show all the settings in a list of show commands. */
1380 cmd_show_list (list
, from_tty
, prefix
)
1381 struct cmd_list_element
*list
;
1385 for (; list
!= NULL
; list
= list
->next
) {
1386 /* If we find a prefix, run its list, prefixing our output by its
1387 prefix (with "show " skipped). */
1388 if (list
->prefixlist
&& !list
->abbrev_flag
)
1389 cmd_show_list (*list
->prefixlist
, from_tty
, list
->prefixname
+ 5);
1390 if (list
->type
== show_cmd
)
1392 fputs_filtered (prefix
, gdb_stdout
);
1393 fputs_filtered (list
->name
, gdb_stdout
);
1394 fputs_filtered (": ", gdb_stdout
);
1395 do_setshow_command ((char *)NULL
, from_tty
, list
);
1402 shell_escape (arg
, from_tty
)
1407 /* FIXME: what about errors (I don't know how GO32 system() handles
1410 #else /* Can fork. */
1411 int rc
, status
, pid
;
1412 char *p
, *user_shell
;
1414 if ((user_shell
= (char *) getenv ("SHELL")) == NULL
)
1415 user_shell
= "/bin/sh";
1417 /* Get the name of the shell for arg0 */
1418 if ((p
= strrchr (user_shell
, '/')) == NULL
)
1421 p
++; /* Get past '/' */
1423 if ((pid
= fork()) == 0)
1426 execl (user_shell
, p
, 0);
1428 execl (user_shell
, p
, "-c", arg
, 0);
1430 fprintf_unfiltered (gdb_stderr
, "Cannot execute %s: %s\n", user_shell
,
1431 safe_strerror (errno
));
1432 gdb_flush (gdb_stderr
);
1437 while ((rc
= wait (&status
)) != pid
&& rc
!= -1)
1440 error ("Fork failed");
1441 #endif /* Can fork. */
1445 make_command (arg
, from_tty
)
1455 p
= xmalloc (sizeof("make ") + strlen(arg
));
1456 strcpy (p
, "make ");
1457 strcpy (p
+ sizeof("make ")-1, arg
);
1460 shell_escape (p
, from_tty
);
1464 show_user_1 (c
, stream
)
1465 struct cmd_list_element
*c
;
1468 register struct command_line
*cmdlines
;
1470 cmdlines
= c
->user_commands
;
1473 fputs_filtered ("User command ", stream
);
1474 fputs_filtered (c
->name
, stream
);
1475 fputs_filtered (":\n", stream
);
1479 print_command_line (cmdlines
, 4);
1480 cmdlines
= cmdlines
->next
;
1482 fputs_filtered ("\n", stream
);
1487 show_user (args
, from_tty
)
1491 struct cmd_list_element
*c
;
1492 extern struct cmd_list_element
*cmdlist
;
1496 c
= lookup_cmd (&args
, cmdlist
, "", 0, 1);
1497 if (c
->class != class_user
)
1498 error ("Not a user command.");
1499 show_user_1 (c
, gdb_stdout
);
1503 for (c
= cmdlist
; c
; c
= c
->next
)
1505 if (c
->class == class_user
)
1506 show_user_1 (c
, gdb_stdout
);
1512 _initialize_command ()
1514 add_com ("shell", class_support
, shell_escape
,
1515 "Execute the rest of the line as a shell command. \n\
1516 With no arguments, run an inferior shell.");
1517 add_com ("make", class_support
, make_command
,
1518 "Run the ``make'' program using the rest of the line as arguments.");
1519 add_cmd ("user", no_class
, show_user
,
1520 "Show definitions of user defined commands.\n\
1521 Argument is the name of the user defined command.\n\
1522 With no argument, show definitions of all user defined commands.", &showlist
);