1 /* Handle lists of commands, their decoding and documentation, for GDB.
2 Copyright 1986, 1989, 1990, 1991 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"
28 /* Prototypes for local functions */
31 undef_cmd_error
PARAMS ((char *, char *));
34 show_user
PARAMS ((char *, int));
37 show_user_1
PARAMS ((struct cmd_list_element
*, GDB_FILE
*));
40 make_command
PARAMS ((char *, int));
43 shell_escape
PARAMS ((char *, int));
46 parse_binary_operation
PARAMS ((char *));
49 print_doc_line
PARAMS ((GDB_FILE
*, char *));
51 /* Add element named NAME.
52 CLASS is the top level category into which commands are broken down
54 FUN should be the function to execute the command;
55 it will get a character string as argument, with leading
56 and trailing blanks already eliminated.
58 DOC is a documentation string for the command.
59 Its first line should be a complete sentence.
60 It should start with ? for a command that is an abbreviation
61 or with * for a command that most users don't need to know about.
63 Add this command to command list *LIST. */
65 struct cmd_list_element
*
66 add_cmd (name
, class, fun
, doc
, list
)
68 enum command_class
class;
69 void (*fun
) PARAMS ((char *, int));
71 struct cmd_list_element
**list
;
73 register struct cmd_list_element
*c
74 = (struct cmd_list_element
*) xmalloc (sizeof (struct cmd_list_element
));
76 delete_cmd (name
, list
);
80 c
->function
.cfunc
= fun
;
87 c
->completer
= make_symbol_completion_list
;
88 c
->type
= not_set_cmd
;
90 c
->var_type
= var_boolean
;
92 c
->user_commands
= NULL
;
94 c
->cmd_pointer
= NULL
;
99 /* Same as above, except that the abbrev_flag is set. */
101 #if 0 /* Currently unused */
103 struct cmd_list_element
*
104 add_abbrev_cmd (name
, class, fun
, doc
, list
)
106 enum command_class
class;
107 void (*fun
) PARAMS ((char *, int));
109 struct cmd_list_element
**list
;
111 register struct cmd_list_element
*c
112 = add_cmd (name
, class, fun
, doc
, list
);
120 struct cmd_list_element
*
121 add_alias_cmd (name
, oldname
, class, abbrev_flag
, list
)
124 enum command_class
class;
126 struct cmd_list_element
**list
;
128 /* Must do this since lookup_cmd tries to side-effect its first arg */
130 register struct cmd_list_element
*old
;
131 register struct cmd_list_element
*c
;
132 copied_name
= (char *) alloca (strlen (oldname
) + 1);
133 strcpy (copied_name
, oldname
);
134 old
= lookup_cmd (&copied_name
, *list
, "", 1, 1);
138 delete_cmd (name
, list
);
142 c
= add_cmd (name
, class, old
->function
.cfunc
, old
->doc
, list
);
143 c
->prefixlist
= old
->prefixlist
;
144 c
->prefixname
= old
->prefixname
;
145 c
->allow_unknown
= old
->allow_unknown
;
146 c
->abbrev_flag
= abbrev_flag
;
147 c
->cmd_pointer
= old
;
151 /* Like add_cmd but adds an element for a command prefix:
152 a name that should be followed by a subcommand to be looked up
153 in another command list. PREFIXLIST should be the address
154 of the variable containing that list. */
156 struct cmd_list_element
*
157 add_prefix_cmd (name
, class, fun
, doc
, prefixlist
, prefixname
,
160 enum command_class
class;
161 void (*fun
) PARAMS ((char *, int));
163 struct cmd_list_element
**prefixlist
;
166 struct cmd_list_element
**list
;
168 register struct cmd_list_element
*c
= add_cmd (name
, class, fun
, doc
, list
);
169 c
->prefixlist
= prefixlist
;
170 c
->prefixname
= prefixname
;
171 c
->allow_unknown
= allow_unknown
;
175 /* Like add_prefix_cmd but sets the abbrev_flag on the new command. */
177 struct cmd_list_element
*
178 add_abbrev_prefix_cmd (name
, class, fun
, doc
, prefixlist
, prefixname
,
181 enum command_class
class;
182 void (*fun
) PARAMS ((char *, int));
184 struct cmd_list_element
**prefixlist
;
187 struct cmd_list_element
**list
;
189 register struct cmd_list_element
*c
= add_cmd (name
, class, fun
, doc
, list
);
190 c
->prefixlist
= prefixlist
;
191 c
->prefixname
= prefixname
;
192 c
->allow_unknown
= allow_unknown
;
197 /* This is an empty "cfunc". */
199 not_just_help_class_command (args
, from_tty
)
205 /* This is an empty "sfunc". */
206 static void empty_sfunc
PARAMS ((char *, int, struct cmd_list_element
*));
209 empty_sfunc (args
, from_tty
, c
)
212 struct cmd_list_element
*c
;
216 /* Add element named NAME to command list LIST (the list for set
217 or some sublist thereof).
218 CLASS is as in add_cmd.
219 VAR_TYPE is the kind of thing we are setting.
220 VAR is address of the variable being controlled by this command.
221 DOC is the documentation string. */
223 struct cmd_list_element
*
224 add_set_cmd (name
, class, var_type
, var
, doc
, list
)
226 enum command_class
class;
230 struct cmd_list_element
**list
;
232 struct cmd_list_element
*c
233 = add_cmd (name
, class, NO_FUNCTION
, doc
, list
);
236 c
->var_type
= var_type
;
238 /* This needs to be something besides NO_FUNCTION so that this isn't
239 treated as a help class. */
240 c
->function
.sfunc
= empty_sfunc
;
244 /* Add element named NAME to command list LIST (the list for set
245 or some sublist thereof).
246 CLASS is as in add_cmd.
247 ENUMLIST is a list of strings which may follow NAME.
248 VAR is address of the variable which will contain the matching string
250 DOC is the documentation string. */
252 struct cmd_list_element
*
253 add_set_enum_cmd (name
, class, enumlist
, var
, doc
, list
)
255 enum command_class
class;
259 struct cmd_list_element
**list
;
261 struct cmd_list_element
*c
262 = add_set_cmd (name
, class, var_enum
, var
, doc
, list
);
269 /* Where SETCMD has already been added, add the corresponding show
270 command to LIST and return a pointer to it. */
271 struct cmd_list_element
*
272 add_show_from_set (setcmd
, list
)
273 struct cmd_list_element
*setcmd
;
274 struct cmd_list_element
**list
;
276 struct cmd_list_element
*showcmd
=
277 (struct cmd_list_element
*) xmalloc (sizeof (struct cmd_list_element
));
279 memcpy (showcmd
, setcmd
, sizeof (struct cmd_list_element
));
280 delete_cmd (showcmd
->name
, list
);
281 showcmd
->type
= show_cmd
;
283 /* Replace "set " at start of docstring with "show ". */
284 if (setcmd
->doc
[0] == 'S' && setcmd
->doc
[1] == 'e'
285 && setcmd
->doc
[2] == 't' && setcmd
->doc
[3] == ' ')
286 showcmd
->doc
= concat ("Show ", setcmd
->doc
+ 4, NULL
);
288 fprintf_unfiltered (gdb_stderr
, "GDB internal error: Bad docstring for set command\n");
290 showcmd
->next
= *list
;
295 /* Remove the command named NAME from the command list. */
298 delete_cmd (name
, list
)
300 struct cmd_list_element
**list
;
302 register struct cmd_list_element
*c
;
303 struct cmd_list_element
*p
;
305 while (*list
&& STREQ ((*list
)->name
, name
))
308 (*list
)->hookee
->hook
= 0; /* Hook slips out of its mouth */
315 for (c
= *list
; c
->next
;)
317 if (STREQ (c
->next
->name
, name
))
320 c
->next
->hookee
->hook
= 0; /* hooked cmd gets away. */
330 /* This command really has to deal with two things:
331 * 1) I want documentation on *this string* (usually called by
332 * "help commandname").
333 * 2) I want documentation on *this list* (usually called by
334 * giving a command that requires subcommands. Also called by saying
337 * I am going to split this into two seperate comamnds, help_cmd and
342 help_cmd (command
, stream
)
346 struct cmd_list_element
*c
;
347 extern struct cmd_list_element
*cmdlist
;
351 help_list (cmdlist
, "", all_classes
, stream
);
355 c
= lookup_cmd (&command
, cmdlist
, "", 0, 0);
360 /* There are three cases here.
361 If c->prefixlist is nonzero, we have a prefix command.
362 Print its documentation, then list its subcommands.
364 If c->function is nonzero, we really have a command.
365 Print its documentation and return.
367 If c->function is zero, we have a class name.
368 Print its documentation (as if it were a command)
369 and then set class to the number of this class
370 so that the commands in the class will be listed. */
372 fputs_filtered (c
->doc
, stream
);
373 fputs_filtered ("\n", stream
);
375 if (c
->prefixlist
== 0 && c
->function
.cfunc
!= NULL
)
377 fprintf_filtered (stream
, "\n");
379 /* If this is a prefix command, print it's subcommands */
381 help_list (*c
->prefixlist
, c
->prefixname
, all_commands
, stream
);
383 /* If this is a class name, print all of the commands in the class */
384 if (c
->function
.cfunc
== NULL
)
385 help_list (cmdlist
, "", c
->class, stream
);
388 fprintf_filtered (stream
, "\nThis command has a hook defined: %s\n",
393 * Get a specific kind of help on a command list.
396 * CMDTYPE is the prefix to use in the title string.
397 * CLASS is the class with which to list the nodes of this list (see
398 * documentation for help_cmd_list below), As usual, ALL_COMMANDS for
399 * everything, ALL_CLASSES for just classes, and non-negative for only things
400 * in a specific class.
401 * and STREAM is the output stream on which to print things.
402 * If you call this routine with a class >= 0, it recurses.
405 help_list (list
, cmdtype
, class, stream
)
406 struct cmd_list_element
*list
;
408 enum command_class
class;
412 char *cmdtype1
, *cmdtype2
;
414 /* If CMDTYPE is "foo ", CMDTYPE1 gets " foo" and CMDTYPE2 gets "foo sub" */
415 len
= strlen (cmdtype
);
416 cmdtype1
= (char *) alloca (len
+ 1);
418 cmdtype2
= (char *) alloca (len
+ 4);
423 strncpy (cmdtype1
+ 1, cmdtype
, len
- 1);
425 strncpy (cmdtype2
, cmdtype
, len
- 1);
426 strcpy (cmdtype2
+ len
- 1, " sub");
429 if (class == all_classes
)
430 fprintf_filtered (stream
, "List of classes of %scommands:\n\n", cmdtype2
);
432 fprintf_filtered (stream
, "List of %scommands:\n\n", cmdtype2
);
434 help_cmd_list (list
, class, cmdtype
, (int)class >= 0, stream
);
436 if (class == all_classes
)
437 fprintf_filtered (stream
, "\n\
438 Type \"help%s\" followed by a class name for a list of commands in that class.",
441 fprintf_filtered (stream
, "\n\
442 Type \"help%s\" followed by %scommand name for full documentation.\n\
443 Command name abbreviations are allowed if unambiguous.\n",
447 /* Print only the first line of STR on STREAM. */
449 print_doc_line (stream
, str
)
453 static char *line_buffer
= 0;
454 static int line_size
;
460 line_buffer
= (char *) xmalloc (line_size
);
464 while (*p
&& *p
!= '\n' && *p
!= '.' && *p
!= ',')
466 if (p
- str
> line_size
- 1)
468 line_size
= p
- str
+ 1;
469 free ((PTR
)line_buffer
);
470 line_buffer
= (char *) xmalloc (line_size
);
472 strncpy (line_buffer
, str
, p
- str
);
473 line_buffer
[p
- str
] = '\0';
474 if (islower (line_buffer
[0]))
475 line_buffer
[0] = toupper (line_buffer
[0]);
476 fputs_filtered (line_buffer
, stream
);
480 * Implement a help command on command list LIST.
481 * RECURSE should be non-zero if this should be done recursively on
482 * all sublists of LIST.
483 * PREFIX is the prefix to print before each command name.
484 * STREAM is the stream upon which the output should be written.
486 * A non-negative class number to list only commands in that
488 * ALL_COMMANDS to list all commands in list.
489 * ALL_CLASSES to list all classes in list.
491 * Note that RECURSE will be active on *all* sublists, not just the
492 * ones selected by the criteria above (ie. the selection mechanism
493 * is at the low level, not the high-level).
496 help_cmd_list (list
, class, prefix
, recurse
, stream
)
497 struct cmd_list_element
*list
;
498 enum command_class
class;
503 register struct cmd_list_element
*c
;
505 for (c
= list
; c
; c
= c
->next
)
507 if (c
->abbrev_flag
== 0 &&
508 (class == all_commands
509 || (class == all_classes
&& c
->function
.cfunc
== NULL
)
510 || (class == c
->class && c
->function
.cfunc
!= NULL
)))
512 fprintf_filtered (stream
, "%s%s -- ", prefix
, c
->name
);
513 print_doc_line (stream
, c
->doc
);
514 fputs_filtered ("\n", stream
);
517 && c
->prefixlist
!= 0
518 && c
->abbrev_flag
== 0)
519 help_cmd_list (*c
->prefixlist
, class, c
->prefixname
, 1, stream
);
523 /* This routine takes a line of TEXT and a CLIST in which to start the
524 lookup. When it returns it will have incremented the text pointer past
525 the section of text it matched, set *RESULT_LIST to point to the list in
526 which the last word was matched, and will return a pointer to the cmd
527 list element which the text matches. It will return NULL if no match at
528 all was possible. It will return -1 (cast appropriately, ick) if ambigous
529 matches are possible; in this case *RESULT_LIST will be set to point to
530 the list in which there are ambiguous choices (and *TEXT will be set to
531 the ambiguous text string).
533 If the located command was an abbreviation, this routine returns the base
534 command of the abbreviation.
536 It does no error reporting whatsoever; control will always return
537 to the superior routine.
539 In the case of an ambiguous return (-1), *RESULT_LIST will be set to point
540 at the prefix_command (ie. the best match) *or* (special case) will be NULL
541 if no prefix command was ever found. For example, in the case of "info a",
542 "info" matches without ambiguity, but "a" could be "args" or "address", so
543 *RESULT_LIST is set to the cmd_list_element for "info". So in this case
544 RESULT_LIST should not be interpeted as a pointer to the beginning of a
545 list; it simply points to a specific command. In the case of an ambiguous
546 return *TEXT is advanced past the last non-ambiguous prefix (e.g.
547 "info t" can be "info types" or "info target"; upon return *TEXT has been
548 advanced past "info ").
550 If RESULT_LIST is NULL, don't set *RESULT_LIST (but don't otherwise
551 affect the operation).
553 This routine does *not* modify the text pointed to by TEXT.
555 If IGNORE_HELP_CLASSES is nonzero, ignore any command list elements which
556 are actually help classes rather than commands (i.e. the function field of
557 the struct cmd_list_element is NULL). */
559 struct cmd_list_element
*
560 lookup_cmd_1 (text
, clist
, result_list
, ignore_help_classes
)
562 struct cmd_list_element
*clist
, **result_list
;
563 int ignore_help_classes
;
566 int len
, tmp
, nfound
;
567 struct cmd_list_element
*found
, *c
;
569 while (**text
== ' ' || **text
== '\t')
572 /* Treating underscores as part of command words is important
573 so that "set args_foo()" doesn't get interpreted as
574 "set args _foo()". */
576 *p
&& (isalnum(*p
) || *p
== '-' || *p
== '_');
580 /* If nothing but whitespace, return 0. */
586 /* *text and p now bracket the first command word to lookup (and
587 it's length is len). We copy this into a local temporary,
588 converting to lower case as we go. */
590 command
= (char *) alloca (len
+ 1);
591 for (tmp
= 0; tmp
< len
; tmp
++)
593 char x
= (*text
)[tmp
];
594 command
[tmp
] = isupper(x
) ? tolower(x
) : x
;
601 for (c
= clist
; c
; c
= c
->next
)
602 if (!strncmp (command
, c
->name
, len
)
603 && (!ignore_help_classes
|| c
->function
.cfunc
))
607 if (c
->name
[len
] == '\0')
614 /* If nothing matches, we have a simple failure. */
620 if (result_list
!= NULL
)
621 /* Will be modified in calling routine
622 if we know what the prefix command is. */
624 return (struct cmd_list_element
*) -1; /* Ambiguous. */
627 /* We've matched something on this list. Move text pointer forward. */
631 /* If this was an abbreviation, use the base command instead. */
633 if (found
->cmd_pointer
)
634 found
= found
->cmd_pointer
;
636 /* If we found a prefix command, keep looking. */
638 if (found
->prefixlist
)
640 c
= lookup_cmd_1 (text
, *found
->prefixlist
, result_list
,
641 ignore_help_classes
);
644 /* Didn't find anything; this is as far as we got. */
645 if (result_list
!= NULL
)
646 *result_list
= clist
;
649 else if (c
== (struct cmd_list_element
*) -1)
651 /* We've gotten this far properley, but the next step
652 is ambiguous. We need to set the result list to the best
653 we've found (if an inferior hasn't already set it). */
654 if (result_list
!= NULL
)
656 /* This used to say *result_list = *found->prefixlist
657 If that was correct, need to modify the documentation
658 at the top of this function to clarify what is supposed
660 *result_list
= found
;
671 if (result_list
!= NULL
)
672 *result_list
= clist
;
677 /* All this hair to move the space to the front of cmdtype */
680 undef_cmd_error (cmdtype
, q
)
683 error ("Undefined %scommand: \"%s\". Try \"help%s%.*s\".",
691 /* Look up the contents of *LINE as a command in the command list LIST.
692 LIST is a chain of struct cmd_list_element's.
693 If it is found, return the struct cmd_list_element for that command
694 and update *LINE to point after the command name, at the first argument.
695 If not found, call error if ALLOW_UNKNOWN is zero
696 otherwise (or if error returns) return zero.
697 Call error if specified command is ambiguous,
698 unless ALLOW_UNKNOWN is negative.
699 CMDTYPE precedes the word "command" in the error message.
701 If INGNORE_HELP_CLASSES is nonzero, ignore any command list
702 elements which are actually help classes rather than commands (i.e.
703 the function field of the struct cmd_list_element is 0). */
705 struct cmd_list_element
*
706 lookup_cmd (line
, list
, cmdtype
, allow_unknown
, ignore_help_classes
)
708 struct cmd_list_element
*list
;
711 int ignore_help_classes
;
713 struct cmd_list_element
*last_list
= 0;
714 struct cmd_list_element
*c
=
715 lookup_cmd_1 (line
, list
, &last_list
, ignore_help_classes
);
717 /* This is wrong for complete_command. */
718 char *ptr
= (*line
) + strlen (*line
) - 1;
720 /* Clear off trailing whitespace. */
721 while (ptr
>= *line
&& (*ptr
== ' ' || *ptr
== '\t'))
731 error ("Lack of needed %scommand", cmdtype
);
736 while (isalnum(*p
) || *p
== '-')
739 q
= (char *) alloca (p
- *line
+ 1);
740 strncpy (q
, *line
, p
- *line
);
742 undef_cmd_error (cmdtype
, q
);
748 else if (c
== (struct cmd_list_element
*) -1)
750 /* Ambigous. Local values should be off prefixlist or called
752 int local_allow_unknown
= (last_list
? last_list
->allow_unknown
:
754 char *local_cmdtype
= last_list
? last_list
->prefixname
: cmdtype
;
755 struct cmd_list_element
*local_list
=
756 (last_list
? *(last_list
->prefixlist
) : list
);
758 if (local_allow_unknown
< 0)
761 return last_list
; /* Found something. */
763 return 0; /* Found nothing. */
767 /* Report as error. */
772 ((*line
)[amb_len
] && (*line
)[amb_len
] != ' '
773 && (*line
)[amb_len
] != '\t');
778 for (c
= local_list
; c
; c
= c
->next
)
779 if (!strncmp (*line
, c
->name
, amb_len
))
781 if (strlen (ambbuf
) + strlen (c
->name
) + 6 < (int)sizeof ambbuf
)
784 strcat (ambbuf
, ", ");
785 strcat (ambbuf
, c
->name
);
789 strcat (ambbuf
, "..");
793 error ("Ambiguous %scommand \"%s\": %s.", local_cmdtype
,
800 /* We've got something. It may still not be what the caller
801 wants (if this command *needs* a subcommand). */
802 while (**line
== ' ' || **line
== '\t')
805 if (c
->prefixlist
&& **line
&& !c
->allow_unknown
)
806 undef_cmd_error (c
->prefixname
, *line
);
808 /* Seems to be what he wants. Return it. */
815 /* Look up the contents of *LINE as a command in the command list LIST.
816 LIST is a chain of struct cmd_list_element's.
817 If it is found, return the struct cmd_list_element for that command
818 and update *LINE to point after the command name, at the first argument.
819 If not found, call error if ALLOW_UNKNOWN is zero
820 otherwise (or if error returns) return zero.
821 Call error if specified command is ambiguous,
822 unless ALLOW_UNKNOWN is negative.
823 CMDTYPE precedes the word "command" in the error message. */
825 struct cmd_list_element
*
826 lookup_cmd (line
, list
, cmdtype
, allow_unknown
)
828 struct cmd_list_element
*list
;
833 register struct cmd_list_element
*c
, *found
;
839 /* Skip leading whitespace. */
841 while (**line
== ' ' || **line
== '\t')
844 /* Clear out trailing whitespace. */
846 p
= *line
+ strlen (*line
);
847 while (p
!= *line
&& (p
[-1] == ' ' || p
[-1] == '\t'))
851 /* Find end of command name. */
854 while (*p
== '-' || isalnum(*p
))
857 /* Look up the command name.
858 If exact match, keep that.
859 Otherwise, take command abbreviated, if unique. Note that (in my
860 opinion) a null string does *not* indicate ambiguity; simply the
861 end of the argument. */
866 error ("Lack of needed %scommand", cmdtype
);
870 /* Copy over to a local buffer, converting to lowercase on the way.
871 This is in case the command being parsed is a subcommand which
872 doesn't match anything, and that's ok. We want the original
873 untouched for the routine of the original command. */
875 processed_cmd
= (char *) alloca (p
- *line
+ 1);
876 for (cmd_len
= 0; cmd_len
< p
- *line
; cmd_len
++)
878 char x
= (*line
)[cmd_len
];
880 processed_cmd
[cmd_len
] = tolower(x
);
882 processed_cmd
[cmd_len
] = x
;
884 processed_cmd
[cmd_len
] = '\0';
886 /* Check all possibilities in the current command list. */
889 for (c
= list
; c
; c
= c
->next
)
891 if (!strncmp (processed_cmd
, c
->name
, cmd_len
))
895 if (c
->name
[cmd_len
] == 0)
903 /* Report error for undefined command name. */
907 if (nfound
> 1 && allow_unknown
>= 0)
910 for (c
= list
; c
; c
= c
->next
)
911 if (!strncmp (processed_cmd
, c
->name
, cmd_len
))
913 if (strlen (ambbuf
) + strlen (c
->name
) + 6 < sizeof ambbuf
)
916 strcat (ambbuf
, ", ");
917 strcat (ambbuf
, c
->name
);
921 strcat (ambbuf
, "..");
925 error ("Ambiguous %scommand \"%s\": %s.", cmdtype
,
926 processed_cmd
, ambbuf
);
928 else if (!allow_unknown
)
929 error ("Undefined %scommand: \"%s\".", cmdtype
, processed_cmd
);
933 /* Skip whitespace before the argument. */
935 while (*p
== ' ' || *p
== '\t') p
++;
938 if (found
->prefixlist
&& *p
)
940 c
= lookup_cmd (line
, *found
->prefixlist
, found
->prefixname
,
941 found
->allow_unknown
);
950 /* Helper function for SYMBOL_COMPLETION_FUNCTION. */
952 /* Return a vector of char pointers which point to the different
953 possible completions in LIST of TEXT.
955 WORD points in the same buffer as TEXT, and completions should be
956 returned relative to this position. For example, suppose TEXT is "foo"
957 and we want to complete to "foobar". If WORD is "oo", return
958 "oobar"; if WORD is "baz/foo", return "baz/foobar". */
961 complete_on_cmdlist (list
, text
, word
)
962 struct cmd_list_element
*list
;
966 struct cmd_list_element
*ptr
;
968 int sizeof_matchlist
;
970 int textlen
= strlen (text
);
972 sizeof_matchlist
= 10;
973 matchlist
= (char **) xmalloc (sizeof_matchlist
* sizeof (char *));
976 for (ptr
= list
; ptr
; ptr
= ptr
->next
)
977 if (!strncmp (ptr
->name
, text
, textlen
)
979 && (ptr
->function
.cfunc
982 if (matches
== sizeof_matchlist
)
984 sizeof_matchlist
*= 2;
985 matchlist
= (char **) xrealloc ((char *)matchlist
,
990 matchlist
[matches
] = (char *)
991 xmalloc (strlen (word
) + strlen (ptr
->name
) + 1);
993 strcpy (matchlist
[matches
], ptr
->name
);
994 else if (word
> text
)
996 /* Return some portion of ptr->name. */
997 strcpy (matchlist
[matches
], ptr
->name
+ (word
- text
));
1001 /* Return some of text plus ptr->name. */
1002 strncpy (matchlist
[matches
], word
, text
- word
);
1003 matchlist
[matches
][text
- word
] = '\0';
1004 strcat (matchlist
[matches
], ptr
->name
);
1011 free ((PTR
)matchlist
);
1016 matchlist
= (char **) xrealloc ((char *)matchlist
, ((matches
+ 1)
1017 * sizeof (char *)));
1018 matchlist
[matches
] = (char *) 0;
1024 /* Helper function for SYMBOL_COMPLETION_FUNCTION. */
1026 /* Return a vector of char pointers which point to the different
1027 possible completions in CMD of TEXT.
1029 WORD points in the same buffer as TEXT, and completions should be
1030 returned relative to this position. For example, suppose TEXT is "foo"
1031 and we want to complete to "foobar". If WORD is "oo", return
1032 "oobar"; if WORD is "baz/foo", return "baz/foobar". */
1035 complete_on_enum (enumlist
, text
, word
)
1041 int sizeof_matchlist
;
1043 int textlen
= strlen (text
);
1047 sizeof_matchlist
= 10;
1048 matchlist
= (char **) xmalloc (sizeof_matchlist
* sizeof (char *));
1051 for (i
= 0; name
= enumlist
[i
]; i
++)
1052 if (strncmp (name
, text
, textlen
) == 0)
1054 if (matches
== sizeof_matchlist
)
1056 sizeof_matchlist
*= 2;
1057 matchlist
= (char **) xrealloc ((char *)matchlist
,
1059 * sizeof (char *)));
1062 matchlist
[matches
] = (char *)
1063 xmalloc (strlen (word
) + strlen (name
) + 1);
1065 strcpy (matchlist
[matches
], name
);
1066 else if (word
> text
)
1068 /* Return some portion of name. */
1069 strcpy (matchlist
[matches
], name
+ (word
- text
));
1073 /* Return some of text plus name. */
1074 strncpy (matchlist
[matches
], word
, text
- word
);
1075 matchlist
[matches
][text
- word
] = '\0';
1076 strcat (matchlist
[matches
], name
);
1083 free ((PTR
)matchlist
);
1088 matchlist
= (char **) xrealloc ((char *)matchlist
, ((matches
+ 1)
1089 * sizeof (char *)));
1090 matchlist
[matches
] = (char *) 0;
1097 parse_binary_operation (arg
)
1105 length
= strlen (arg
);
1107 while (arg
[length
- 1] == ' ' || arg
[length
- 1] == '\t')
1110 if (!strncmp (arg
, "on", length
)
1111 || !strncmp (arg
, "1", length
)
1112 || !strncmp (arg
, "yes", length
))
1115 if (!strncmp (arg
, "off", length
)
1116 || !strncmp (arg
, "0", length
)
1117 || !strncmp (arg
, "no", length
))
1121 error ("\"on\" or \"off\" expected.");
1126 /* Do a "set" or "show" command. ARG is NULL if no argument, or the text
1127 of the argument, and FROM_TTY is nonzero if this command is being entered
1128 directly by the user (i.e. these are just like any other
1129 command). C is the command list element for the command. */
1131 do_setshow_command (arg
, from_tty
, c
)
1134 struct cmd_list_element
*c
;
1136 if (c
->type
== set_cmd
)
1138 switch (c
->var_type
)
1149 new = (char *) xmalloc (strlen (arg
) + 2);
1151 while ((ch
= *p
++) != '\000')
1155 /* \ at end of argument is used after spaces
1156 so they won't be lost. */
1157 /* This is obsolete now that we no longer strip
1158 trailing whitespace and actually, the backslash
1159 didn't get here in my test, readline or
1160 something did something funky with a backslash
1161 right before a newline. */
1164 ch
= parse_escape (&p
);
1166 break; /* C loses */
1174 if (*(p
- 1) != '\\')
1178 new = (char *) xrealloc (new, q
- new);
1179 if (*(char **)c
->var
!= NULL
)
1180 free (*(char **)c
->var
);
1181 *(char **) c
->var
= new;
1184 case var_string_noescape
:
1187 if (*(char **)c
->var
!= NULL
)
1188 free (*(char **)c
->var
);
1189 *(char **) c
->var
= savestring (arg
, strlen (arg
));
1193 error_no_arg ("filename to set it to.");
1194 if (*(char **)c
->var
!= NULL
)
1195 free (*(char **)c
->var
);
1196 *(char **)c
->var
= tilde_expand (arg
);
1199 *(int *) c
->var
= parse_binary_operation (arg
);
1203 error_no_arg ("integer to set it to.");
1204 *(unsigned int *) c
->var
= parse_and_eval_address (arg
);
1205 if (*(unsigned int *) c
->var
== 0)
1206 *(unsigned int *) c
->var
= UINT_MAX
;
1212 error_no_arg ("integer to set it to.");
1213 val
= parse_and_eval_address (arg
);
1215 *(int *) c
->var
= INT_MAX
;
1216 else if (val
>= INT_MAX
)
1217 error ("integer %u out of range", val
);
1219 *(int *) c
->var
= val
;
1224 error_no_arg ("integer to set it to.");
1225 *(int *) c
->var
= parse_and_eval_address (arg
);
1235 p
= strchr (arg
, ' ');
1243 for (i
= 0; c
->enums
[i
]; i
++)
1244 if (strncmp (arg
, c
->enums
[i
], len
) == 0)
1246 match
= c
->enums
[i
];
1251 error ("Undefined item: \"%s\".", arg
);
1254 error ("Ambiguous item \"%s\".", arg
);
1256 *(char **)c
->var
= match
;
1260 error ("gdb internal error: bad var_type in do_setshow_command");
1263 else if (c
->type
== show_cmd
)
1265 /* Print doc minus "show" at start. */
1266 print_doc_line (gdb_stdout
, c
->doc
+ 5);
1268 fputs_filtered (" is ", gdb_stdout
);
1270 switch (c
->var_type
)
1275 fputs_filtered ("\"", gdb_stdout
);
1276 for (p
= *(unsigned char **) c
->var
; *p
!= '\0'; p
++)
1277 gdb_printchar (*p
, gdb_stdout
, '"');
1278 fputs_filtered ("\"", gdb_stdout
);
1281 case var_string_noescape
:
1284 fputs_filtered ("\"", gdb_stdout
);
1285 fputs_filtered (*(char **) c
->var
, gdb_stdout
);
1286 fputs_filtered ("\"", gdb_stdout
);
1289 fputs_filtered (*(int *) c
->var
? "on" : "off", gdb_stdout
);
1292 if (*(unsigned int *) c
->var
== UINT_MAX
) {
1293 fputs_filtered ("unlimited", gdb_stdout
);
1296 /* else fall through */
1298 fprintf_filtered (gdb_stdout
, "%u", *(unsigned int *) c
->var
);
1301 if (*(int *) c
->var
== INT_MAX
)
1303 fputs_filtered ("unlimited", gdb_stdout
);
1306 fprintf_filtered (gdb_stdout
, "%d", *(int *) c
->var
);
1310 error ("gdb internal error: bad var_type in do_setshow_command");
1312 fputs_filtered (".\n", gdb_stdout
);
1315 error ("gdb internal error: bad cmd_type in do_setshow_command");
1316 (*c
->function
.sfunc
) (NULL
, from_tty
, c
);
1319 /* Show all the settings in a list of show commands. */
1322 cmd_show_list (list
, from_tty
, prefix
)
1323 struct cmd_list_element
*list
;
1327 for (; list
!= NULL
; list
= list
->next
) {
1328 /* If we find a prefix, run its list, prefixing our output by its
1329 prefix (with "show " skipped). */
1330 if (list
->prefixlist
&& !list
->abbrev_flag
)
1331 cmd_show_list (*list
->prefixlist
, from_tty
, list
->prefixname
+ 5);
1332 if (list
->type
== show_cmd
)
1334 fputs_filtered (prefix
, gdb_stdout
);
1335 fputs_filtered (list
->name
, gdb_stdout
);
1336 fputs_filtered (": ", gdb_stdout
);
1337 do_setshow_command ((char *)NULL
, from_tty
, list
);
1344 shell_escape (arg
, from_tty
)
1349 /* FIXME: what about errors (I don't know how GO32 system() handles
1352 #else /* Can fork. */
1353 int rc
, status
, pid
;
1354 char *p
, *user_shell
;
1356 if ((user_shell
= (char *) getenv ("SHELL")) == NULL
)
1357 user_shell
= "/bin/sh";
1359 /* Get the name of the shell for arg0 */
1360 if ((p
= strrchr (user_shell
, '/')) == NULL
)
1363 p
++; /* Get past '/' */
1365 if ((pid
= fork()) == 0)
1368 execl (user_shell
, p
, 0);
1370 execl (user_shell
, p
, "-c", arg
, 0);
1372 fprintf_unfiltered (gdb_stderr
, "Cannot execute %s: %s\n", user_shell
,
1373 safe_strerror (errno
));
1374 gdb_flush (gdb_stderr
);
1379 while ((rc
= wait (&status
)) != pid
&& rc
!= -1)
1382 error ("Fork failed");
1383 #endif /* Can fork. */
1387 make_command (arg
, from_tty
)
1397 p
= xmalloc (sizeof("make ") + strlen(arg
));
1398 strcpy (p
, "make ");
1399 strcpy (p
+ sizeof("make ")-1, arg
);
1402 shell_escape (p
, from_tty
);
1406 show_user_1 (c
, stream
)
1407 struct cmd_list_element
*c
;
1410 register struct command_line
*cmdlines
;
1412 cmdlines
= c
->user_commands
;
1415 fputs_filtered ("User command ", stream
);
1416 fputs_filtered (c
->name
, stream
);
1417 fputs_filtered (":\n", stream
);
1421 print_command_line (cmdlines
, 4);
1422 cmdlines
= cmdlines
->next
;
1424 fputs_filtered ("\n", stream
);
1429 show_user (args
, from_tty
)
1433 struct cmd_list_element
*c
;
1434 extern struct cmd_list_element
*cmdlist
;
1438 c
= lookup_cmd (&args
, cmdlist
, "", 0, 1);
1439 if (c
->class != class_user
)
1440 error ("Not a user command.");
1441 show_user_1 (c
, gdb_stdout
);
1445 for (c
= cmdlist
; c
; c
= c
->next
)
1447 if (c
->class == class_user
)
1448 show_user_1 (c
, gdb_stdout
);
1454 _initialize_command ()
1456 add_com ("shell", class_support
, shell_escape
,
1457 "Execute the rest of the line as a shell command. \n\
1458 With no arguments, run an inferior shell.");
1459 add_com ("make", class_support
, make_command
,
1460 "Run the ``make'' program using the rest of the line as arguments.");
1461 add_cmd ("user", no_class
, show_user
,
1462 "Show definitions of user defined commands.\n\
1463 Argument is the name of the user defined command.\n\
1464 With no argument, show definitions of all user defined commands.", &showlist
);