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., 675 Mass Ave, Cambridge, MA 02139, USA. */
25 /* Prototypes for local functions */
28 undef_cmd_error
PARAMS ((char *, char *));
31 show_user
PARAMS ((char *, int));
34 show_user_1
PARAMS ((struct cmd_list_element
*, GDB_FILE
*));
37 make_command
PARAMS ((char *, int));
40 shell_escape
PARAMS ((char *, int));
43 parse_binary_operation
PARAMS ((char *));
46 print_doc_line
PARAMS ((GDB_FILE
*, char *));
48 /* Add element named NAME.
49 CLASS is the top level category into which commands are broken down
51 FUN should be the function to execute the command;
52 it will get a character string as argument, with leading
53 and trailing blanks already eliminated.
55 DOC is a documentation string for the command.
56 Its first line should be a complete sentence.
57 It should start with ? for a command that is an abbreviation
58 or with * for a command that most users don't need to know about.
60 Add this command to command list *LIST. */
62 struct cmd_list_element
*
63 add_cmd (name
, class, fun
, doc
, list
)
65 enum command_class
class;
66 void (*fun
) PARAMS ((char *, int));
68 struct cmd_list_element
**list
;
70 register struct cmd_list_element
*c
71 = (struct cmd_list_element
*) xmalloc (sizeof (struct cmd_list_element
));
73 delete_cmd (name
, list
);
77 c
->function
.cfunc
= fun
;
80 c
->prefixname
= (char *)NULL
;
86 c
->type
= not_set_cmd
;
87 c
->completer
= make_symbol_completion_list
;
89 c
->var_type
= var_boolean
;
95 /* Same as above, except that the abbrev_flag is set. */
97 #if 0 /* Currently unused */
99 struct cmd_list_element
*
100 add_abbrev_cmd (name
, class, fun
, doc
, list
)
102 enum command_class
class;
103 void (*fun
) PARAMS ((char *, int));
105 struct cmd_list_element
**list
;
107 register struct cmd_list_element
*c
108 = add_cmd (name
, class, fun
, doc
, list
);
116 struct cmd_list_element
*
117 add_alias_cmd (name
, oldname
, class, abbrev_flag
, list
)
120 enum command_class
class;
122 struct cmd_list_element
**list
;
124 /* Must do this since lookup_cmd tries to side-effect its first arg */
126 register struct cmd_list_element
*old
;
127 register struct cmd_list_element
*c
;
128 copied_name
= (char *) alloca (strlen (oldname
) + 1);
129 strcpy (copied_name
, oldname
);
130 old
= lookup_cmd (&copied_name
, *list
, "", 1, 1);
134 delete_cmd (name
, list
);
138 c
= add_cmd (name
, class, old
->function
.cfunc
, old
->doc
, list
);
139 c
->prefixlist
= old
->prefixlist
;
140 c
->prefixname
= old
->prefixname
;
141 c
->allow_unknown
= old
->allow_unknown
;
142 c
->abbrev_flag
= abbrev_flag
;
143 c
->cmd_pointer
= old
;
147 /* Like add_cmd but adds an element for a command prefix:
148 a name that should be followed by a subcommand to be looked up
149 in another command list. PREFIXLIST should be the address
150 of the variable containing that list. */
152 struct cmd_list_element
*
153 add_prefix_cmd (name
, class, fun
, doc
, prefixlist
, prefixname
,
156 enum command_class
class;
157 void (*fun
) PARAMS ((char *, int));
159 struct cmd_list_element
**prefixlist
;
162 struct cmd_list_element
**list
;
164 register struct cmd_list_element
*c
= add_cmd (name
, class, fun
, doc
, list
);
165 c
->prefixlist
= prefixlist
;
166 c
->prefixname
= prefixname
;
167 c
->allow_unknown
= allow_unknown
;
171 /* Like add_prefix_cmd but sets the abbrev_flag on the new command. */
173 struct cmd_list_element
*
174 add_abbrev_prefix_cmd (name
, class, fun
, doc
, prefixlist
, prefixname
,
177 enum command_class
class;
178 void (*fun
) PARAMS ((char *, int));
180 struct cmd_list_element
**prefixlist
;
183 struct cmd_list_element
**list
;
185 register struct cmd_list_element
*c
= add_cmd (name
, class, fun
, doc
, list
);
186 c
->prefixlist
= prefixlist
;
187 c
->prefixname
= prefixname
;
188 c
->allow_unknown
= allow_unknown
;
195 not_just_help_class_command (args
, from_tty
, c
)
198 struct cmd_list_element
*c
;
202 /* Add element named NAME to command list LIST (the list for set
203 or some sublist thereof).
204 CLASS is as in add_cmd.
205 VAR_TYPE is the kind of thing we are setting.
206 VAR is address of the variable being controlled by this command.
207 DOC is the documentation string. */
209 struct cmd_list_element
*
210 add_set_cmd (name
, class, var_type
, var
, doc
, list
)
212 enum command_class
class;
216 struct cmd_list_element
**list
;
218 struct cmd_list_element
*c
219 = add_cmd (name
, class, NO_FUNCTION
, doc
, list
);
222 c
->var_type
= var_type
;
224 /* This needs to be something besides NO_FUNCTION so that this isn't
225 treated as a help class. */
226 c
->function
.sfunc
= not_just_help_class_command
;
230 /* Where SETCMD has already been added, add the corresponding show
231 command to LIST and return a pointer to it. */
232 struct cmd_list_element
*
233 add_show_from_set (setcmd
, list
)
234 struct cmd_list_element
*setcmd
;
235 struct cmd_list_element
**list
;
237 struct cmd_list_element
*showcmd
=
238 (struct cmd_list_element
*) xmalloc (sizeof (struct cmd_list_element
));
240 memcpy (showcmd
, setcmd
, sizeof (struct cmd_list_element
));
241 delete_cmd (showcmd
->name
, list
);
242 showcmd
->type
= show_cmd
;
244 /* Replace "set " at start of docstring with "show ". */
245 if (setcmd
->doc
[0] == 'S' && setcmd
->doc
[1] == 'e'
246 && setcmd
->doc
[2] == 't' && setcmd
->doc
[3] == ' ')
247 showcmd
->doc
= concat ("Show ", setcmd
->doc
+ 4, NULL
);
249 fprintf_unfiltered (gdb_stderr
, "GDB internal error: Bad docstring for set command\n");
251 showcmd
->next
= *list
;
256 /* Remove the command named NAME from the command list. */
259 delete_cmd (name
, list
)
261 struct cmd_list_element
**list
;
263 register struct cmd_list_element
*c
;
264 struct cmd_list_element
*p
;
266 while (*list
&& STREQ ((*list
)->name
, name
))
269 (*list
)->hookee
->hook
= 0; /* Hook slips out of its mouth */
276 for (c
= *list
; c
->next
;)
278 if (STREQ (c
->next
->name
, name
))
281 c
->next
->hookee
->hook
= 0; /* hooked cmd gets away. */
291 /* This command really has to deal with two things:
292 * 1) I want documentation on *this string* (usually called by
293 * "help commandname").
294 * 2) I want documentation on *this list* (usually called by
295 * giving a command that requires subcommands. Also called by saying
298 * I am going to split this into two seperate comamnds, help_cmd and
303 help_cmd (command
, stream
)
307 struct cmd_list_element
*c
;
308 extern struct cmd_list_element
*cmdlist
;
312 help_list (cmdlist
, "", all_classes
, stream
);
316 c
= lookup_cmd (&command
, cmdlist
, "", 0, 0);
321 /* There are three cases here.
322 If c->prefixlist is nonzero, we have a prefix command.
323 Print its documentation, then list its subcommands.
325 If c->function is nonzero, we really have a command.
326 Print its documentation and return.
328 If c->function is zero, we have a class name.
329 Print its documentation (as if it were a command)
330 and then set class to the number of this class
331 so that the commands in the class will be listed. */
333 fputs_filtered (c
->doc
, stream
);
334 fputs_filtered ("\n", stream
);
336 if (c
->prefixlist
== 0 && c
->function
.cfunc
!= NULL
)
338 fprintf_filtered (stream
, "\n");
340 /* If this is a prefix command, print it's subcommands */
342 help_list (*c
->prefixlist
, c
->prefixname
, all_commands
, stream
);
344 /* If this is a class name, print all of the commands in the class */
345 if (c
->function
.cfunc
== NULL
)
346 help_list (cmdlist
, "", c
->class, stream
);
349 fprintf_filtered (stream
, "\nThis command has a hook defined: %s\n",
354 * Get a specific kind of help on a command list.
357 * CMDTYPE is the prefix to use in the title string.
358 * CLASS is the class with which to list the nodes of this list (see
359 * documentation for help_cmd_list below), As usual, ALL_COMMANDS for
360 * everything, ALL_CLASSES for just classes, and non-negative for only things
361 * in a specific class.
362 * and STREAM is the output stream on which to print things.
363 * If you call this routine with a class >= 0, it recurses.
366 help_list (list
, cmdtype
, class, stream
)
367 struct cmd_list_element
*list
;
369 enum command_class
class;
373 char *cmdtype1
, *cmdtype2
;
375 /* If CMDTYPE is "foo ", CMDTYPE1 gets " foo" and CMDTYPE2 gets "foo sub" */
376 len
= strlen (cmdtype
);
377 cmdtype1
= (char *) alloca (len
+ 1);
379 cmdtype2
= (char *) alloca (len
+ 4);
384 strncpy (cmdtype1
+ 1, cmdtype
, len
- 1);
386 strncpy (cmdtype2
, cmdtype
, len
- 1);
387 strcpy (cmdtype2
+ len
- 1, " sub");
390 if (class == all_classes
)
391 fprintf_filtered (stream
, "List of classes of %scommands:\n\n", cmdtype2
);
393 fprintf_filtered (stream
, "List of %scommands:\n\n", cmdtype2
);
395 help_cmd_list (list
, class, cmdtype
, (int)class >= 0, stream
);
397 if (class == all_classes
)
398 fprintf_filtered (stream
, "\n\
399 Type \"help%s\" followed by a class name for a list of commands in that class.",
402 fprintf_filtered (stream
, "\n\
403 Type \"help%s\" followed by %scommand name for full documentation.\n\
404 Command name abbreviations are allowed if unambiguous.\n",
408 /* Print only the first line of STR on STREAM. */
410 print_doc_line (stream
, str
)
414 static char *line_buffer
= 0;
415 static int line_size
;
421 line_buffer
= (char *) xmalloc (line_size
);
425 while (*p
&& *p
!= '\n' && *p
!= '.' && *p
!= ',')
427 if (p
- str
> line_size
- 1)
429 line_size
= p
- str
+ 1;
430 free ((PTR
)line_buffer
);
431 line_buffer
= (char *) xmalloc (line_size
);
433 strncpy (line_buffer
, str
, p
- str
);
434 line_buffer
[p
- str
] = '\0';
435 if (islower (line_buffer
[0]))
436 line_buffer
[0] = toupper (line_buffer
[0]);
437 fputs_filtered (line_buffer
, stream
);
441 * Implement a help command on command list LIST.
442 * RECURSE should be non-zero if this should be done recursively on
443 * all sublists of LIST.
444 * PREFIX is the prefix to print before each command name.
445 * STREAM is the stream upon which the output should be written.
447 * A non-negative class number to list only commands in that
449 * ALL_COMMANDS to list all commands in list.
450 * ALL_CLASSES to list all classes in list.
452 * Note that RECURSE will be active on *all* sublists, not just the
453 * ones selected by the criteria above (ie. the selection mechanism
454 * is at the low level, not the high-level).
457 help_cmd_list (list
, class, prefix
, recurse
, stream
)
458 struct cmd_list_element
*list
;
459 enum command_class
class;
464 register struct cmd_list_element
*c
;
466 for (c
= list
; c
; c
= c
->next
)
468 if (c
->abbrev_flag
== 0 &&
469 (class == all_commands
470 || (class == all_classes
&& c
->function
.cfunc
== NULL
)
471 || (class == c
->class && c
->function
.cfunc
!= NULL
)))
473 fprintf_filtered (stream
, "%s%s -- ", prefix
, c
->name
);
474 print_doc_line (stream
, c
->doc
);
475 fputs_filtered ("\n", stream
);
478 && c
->prefixlist
!= 0
479 && c
->abbrev_flag
== 0)
480 help_cmd_list (*c
->prefixlist
, class, c
->prefixname
, 1, stream
);
484 /* This routine takes a line of TEXT and a CLIST in which to start the
485 lookup. When it returns it will have incremented the text pointer past
486 the section of text it matched, set *RESULT_LIST to point to the list in
487 which the last word was matched, and will return a pointer to the cmd
488 list element which the text matches. It will return NULL if no match at
489 all was possible. It will return -1 (cast appropriately, ick) if ambigous
490 matches are possible; in this case *RESULT_LIST will be set to point to
491 the list in which there are ambiguous choices (and *TEXT will be set to
492 the ambiguous text string).
494 If the located command was an abbreviation, this routine returns the base
495 command of the abbreviation.
497 It does no error reporting whatsoever; control will always return
498 to the superior routine.
500 In the case of an ambiguous return (-1), *RESULT_LIST will be set to point
501 at the prefix_command (ie. the best match) *or* (special case) will be NULL
502 if no prefix command was ever found. For example, in the case of "info a",
503 "info" matches without ambiguity, but "a" could be "args" or "address", so
504 *RESULT_LIST is set to the cmd_list_element for "info". So in this case
505 RESULT_LIST should not be interpeted as a pointer to the beginning of a
506 list; it simply points to a specific command. In the case of an ambiguous
507 return *TEXT is advanced past the last non-ambiguous prefix (e.g.
508 "info t" can be "info types" or "info target"; upon return *TEXT has been
509 advanced past "info ").
511 If RESULT_LIST is NULL, don't set *RESULT_LIST (but don't otherwise
512 affect the operation).
514 This routine does *not* modify the text pointed to by TEXT.
516 If IGNORE_HELP_CLASSES is nonzero, ignore any command list elements which
517 are actually help classes rather than commands (i.e. the function field of
518 the struct cmd_list_element is NULL). */
520 struct cmd_list_element
*
521 lookup_cmd_1 (text
, clist
, result_list
, ignore_help_classes
)
523 struct cmd_list_element
*clist
, **result_list
;
524 int ignore_help_classes
;
527 int len
, tmp
, nfound
;
528 struct cmd_list_element
*found
, *c
;
530 while (**text
== ' ' || **text
== '\t')
533 /* Treating underscores as part of command words is important
534 so that "set args_foo()" doesn't get interpreted as
535 "set args _foo()". */
537 *p
&& (isalnum(*p
) || *p
== '-' || *p
== '_');
541 /* If nothing but whitespace, return 0. */
547 /* *text and p now bracket the first command word to lookup (and
548 it's length is len). We copy this into a local temporary,
549 converting to lower case as we go. */
551 command
= (char *) alloca (len
+ 1);
552 for (tmp
= 0; tmp
< len
; tmp
++)
554 char x
= (*text
)[tmp
];
555 command
[tmp
] = isupper(x
) ? tolower(x
) : x
;
562 for (c
= clist
; c
; c
= c
->next
)
563 if (!strncmp (command
, c
->name
, len
)
564 && (!ignore_help_classes
|| c
->function
.cfunc
))
568 if (c
->name
[len
] == '\0')
575 /* If nothing matches, we have a simple failure. */
581 if (result_list
!= NULL
)
582 /* Will be modified in calling routine
583 if we know what the prefix command is. */
585 return (struct cmd_list_element
*) -1; /* Ambiguous. */
588 /* We've matched something on this list. Move text pointer forward. */
592 /* If this was an abbreviation, use the base command instead. */
594 if (found
->cmd_pointer
)
595 found
= found
->cmd_pointer
;
597 /* If we found a prefix command, keep looking. */
599 if (found
->prefixlist
)
601 c
= lookup_cmd_1 (text
, *found
->prefixlist
, result_list
,
602 ignore_help_classes
);
605 /* Didn't find anything; this is as far as we got. */
606 if (result_list
!= NULL
)
607 *result_list
= clist
;
610 else if (c
== (struct cmd_list_element
*) -1)
612 /* We've gotten this far properley, but the next step
613 is ambiguous. We need to set the result list to the best
614 we've found (if an inferior hasn't already set it). */
615 if (result_list
!= NULL
)
617 /* This used to say *result_list = *found->prefixlist
618 If that was correct, need to modify the documentation
619 at the top of this function to clarify what is supposed
621 *result_list
= found
;
632 if (result_list
!= NULL
)
633 *result_list
= clist
;
638 /* All this hair to move the space to the front of cmdtype */
641 undef_cmd_error (cmdtype
, q
)
644 error ("Undefined %scommand: \"%s\". Try \"help%s%.*s\".",
652 /* Look up the contents of *LINE as a command in the command list LIST.
653 LIST is a chain of struct cmd_list_element's.
654 If it is found, return the struct cmd_list_element for that command
655 and update *LINE to point after the command name, at the first argument.
656 If not found, call error if ALLOW_UNKNOWN is zero
657 otherwise (or if error returns) return zero.
658 Call error if specified command is ambiguous,
659 unless ALLOW_UNKNOWN is negative.
660 CMDTYPE precedes the word "command" in the error message.
662 If INGNORE_HELP_CLASSES is nonzero, ignore any command list
663 elements which are actually help classes rather than commands (i.e.
664 the function field of the struct cmd_list_element is 0). */
666 struct cmd_list_element
*
667 lookup_cmd (line
, list
, cmdtype
, allow_unknown
, ignore_help_classes
)
669 struct cmd_list_element
*list
;
672 int ignore_help_classes
;
674 struct cmd_list_element
*last_list
= 0;
675 struct cmd_list_element
*c
=
676 lookup_cmd_1 (line
, list
, &last_list
, ignore_help_classes
);
677 char *ptr
= (*line
) + strlen (*line
) - 1;
679 /* Clear off trailing whitespace. */
680 while (ptr
>= *line
&& (*ptr
== ' ' || *ptr
== '\t'))
689 error ("Lack of needed %scommand", cmdtype
);
694 while (isalnum(*p
) || *p
== '-')
697 q
= (char *) alloca (p
- *line
+ 1);
698 strncpy (q
, *line
, p
- *line
);
700 undef_cmd_error (cmdtype
, q
);
706 else if (c
== (struct cmd_list_element
*) -1)
708 /* Ambigous. Local values should be off prefixlist or called
710 int local_allow_unknown
= (last_list
? last_list
->allow_unknown
:
712 char *local_cmdtype
= last_list
? last_list
->prefixname
: cmdtype
;
713 struct cmd_list_element
*local_list
=
714 (last_list
? *(last_list
->prefixlist
) : list
);
716 if (local_allow_unknown
< 0)
719 return last_list
; /* Found something. */
721 return 0; /* Found nothing. */
725 /* Report as error. */
730 ((*line
)[amb_len
] && (*line
)[amb_len
] != ' '
731 && (*line
)[amb_len
] != '\t');
736 for (c
= local_list
; c
; c
= c
->next
)
737 if (!strncmp (*line
, c
->name
, amb_len
))
739 if (strlen (ambbuf
) + strlen (c
->name
) + 6 < (int)sizeof ambbuf
)
742 strcat (ambbuf
, ", ");
743 strcat (ambbuf
, c
->name
);
747 strcat (ambbuf
, "..");
751 error ("Ambiguous %scommand \"%s\": %s.", local_cmdtype
,
758 /* We've got something. It may still not be what the caller
759 wants (if this command *needs* a subcommand). */
760 while (**line
== ' ' || **line
== '\t')
763 if (c
->prefixlist
&& **line
&& !c
->allow_unknown
)
764 undef_cmd_error (c
->prefixname
, *line
);
766 /* Seems to be what he wants. Return it. */
773 /* Look up the contents of *LINE as a command in the command list LIST.
774 LIST is a chain of struct cmd_list_element's.
775 If it is found, return the struct cmd_list_element for that command
776 and update *LINE to point after the command name, at the first argument.
777 If not found, call error if ALLOW_UNKNOWN is zero
778 otherwise (or if error returns) return zero.
779 Call error if specified command is ambiguous,
780 unless ALLOW_UNKNOWN is negative.
781 CMDTYPE precedes the word "command" in the error message. */
783 struct cmd_list_element
*
784 lookup_cmd (line
, list
, cmdtype
, allow_unknown
)
786 struct cmd_list_element
*list
;
791 register struct cmd_list_element
*c
, *found
;
797 /* Skip leading whitespace. */
799 while (**line
== ' ' || **line
== '\t')
802 /* Clear out trailing whitespace. */
804 p
= *line
+ strlen (*line
);
805 while (p
!= *line
&& (p
[-1] == ' ' || p
[-1] == '\t'))
809 /* Find end of command name. */
812 while (*p
== '-' || isalnum(*p
))
815 /* Look up the command name.
816 If exact match, keep that.
817 Otherwise, take command abbreviated, if unique. Note that (in my
818 opinion) a null string does *not* indicate ambiguity; simply the
819 end of the argument. */
824 error ("Lack of needed %scommand", cmdtype
);
828 /* Copy over to a local buffer, converting to lowercase on the way.
829 This is in case the command being parsed is a subcommand which
830 doesn't match anything, and that's ok. We want the original
831 untouched for the routine of the original command. */
833 processed_cmd
= (char *) alloca (p
- *line
+ 1);
834 for (cmd_len
= 0; cmd_len
< p
- *line
; cmd_len
++)
836 char x
= (*line
)[cmd_len
];
838 processed_cmd
[cmd_len
] = tolower(x
);
840 processed_cmd
[cmd_len
] = x
;
842 processed_cmd
[cmd_len
] = '\0';
844 /* Check all possibilities in the current command list. */
847 for (c
= list
; c
; c
= c
->next
)
849 if (!strncmp (processed_cmd
, c
->name
, cmd_len
))
853 if (c
->name
[cmd_len
] == 0)
861 /* Report error for undefined command name. */
865 if (nfound
> 1 && allow_unknown
>= 0)
868 for (c
= list
; c
; c
= c
->next
)
869 if (!strncmp (processed_cmd
, c
->name
, cmd_len
))
871 if (strlen (ambbuf
) + strlen (c
->name
) + 6 < sizeof ambbuf
)
874 strcat (ambbuf
, ", ");
875 strcat (ambbuf
, c
->name
);
879 strcat (ambbuf
, "..");
883 error ("Ambiguous %scommand \"%s\": %s.", cmdtype
,
884 processed_cmd
, ambbuf
);
886 else if (!allow_unknown
)
887 error ("Undefined %scommand: \"%s\".", cmdtype
, processed_cmd
);
891 /* Skip whitespace before the argument. */
893 while (*p
== ' ' || *p
== '\t') p
++;
896 if (found
->prefixlist
&& *p
)
898 c
= lookup_cmd (line
, *found
->prefixlist
, found
->prefixname
,
899 found
->allow_unknown
);
908 /* Helper function for SYMBOL_COMPLETION_FUNCTION. */
910 /* Return a vector of char pointers which point to the different
911 possible completions in LIST of TEXT.
913 WORD points in the same buffer as TEXT, and completions should be
914 returned relative to this position. For example, suppose TEXT is "foo"
915 and we want to complete to "foobar". If WORD is "oo", return
916 "oobar"; if WORD is "baz/foo", return "baz/foobar". */
919 complete_on_cmdlist (list
, text
, word
)
920 struct cmd_list_element
*list
;
924 struct cmd_list_element
*ptr
;
926 int sizeof_matchlist
;
928 int textlen
= strlen (text
);
930 sizeof_matchlist
= 10;
931 matchlist
= (char **) xmalloc (sizeof_matchlist
* sizeof (char *));
934 for (ptr
= list
; ptr
; ptr
= ptr
->next
)
935 if (!strncmp (ptr
->name
, text
, textlen
)
937 && (ptr
->function
.cfunc
940 if (matches
== sizeof_matchlist
)
942 sizeof_matchlist
*= 2;
943 matchlist
= (char **) xrealloc ((char *)matchlist
,
948 matchlist
[matches
] = (char *)
949 xmalloc (strlen (word
) + strlen (ptr
->name
) + 1);
951 strcpy (matchlist
[matches
], ptr
->name
);
952 else if (word
> text
)
954 /* Return some portion of ptr->name. */
955 strcpy (matchlist
[matches
], ptr
->name
+ (word
- text
));
959 /* Return some of text plus ptr->name. */
960 strncpy (matchlist
[matches
], word
, text
- word
);
961 matchlist
[matches
][text
- word
] = '\0';
962 strcat (matchlist
[matches
], ptr
->name
);
969 free ((PTR
)matchlist
);
974 matchlist
= (char **) xrealloc ((char *)matchlist
, ((matches
+ 1)
976 matchlist
[matches
] = (char *) 0;
983 parse_binary_operation (arg
)
991 length
= strlen (arg
);
993 while (arg
[length
- 1] == ' ' || arg
[length
- 1] == '\t')
996 if (!strncmp (arg
, "on", length
)
997 || !strncmp (arg
, "1", length
)
998 || !strncmp (arg
, "yes", length
))
1001 if (!strncmp (arg
, "off", length
)
1002 || !strncmp (arg
, "0", length
)
1003 || !strncmp (arg
, "no", length
))
1007 error ("\"on\" or \"off\" expected.");
1012 /* Do a "set" or "show" command. ARG is NULL if no argument, or the text
1013 of the argument, and FROM_TTY is nonzero if this command is being entered
1014 directly by the user (i.e. these are just like any other
1015 command). C is the command list element for the command. */
1017 do_setshow_command (arg
, from_tty
, c
)
1020 struct cmd_list_element
*c
;
1022 if (c
->type
== set_cmd
)
1024 switch (c
->var_type
)
1035 new = (char *) xmalloc (strlen (arg
) + 2);
1037 while ((ch
= *p
++) != '\000')
1041 /* \ at end of argument is used after spaces
1042 so they won't be lost. */
1045 ch
= parse_escape (&p
);
1047 break; /* C loses */
1054 if (*(p
- 1) != '\\')
1057 new = (char *) xrealloc (new, q
- new);
1058 if (*(char **)c
->var
!= NULL
)
1059 free (*(char **)c
->var
);
1060 *(char **) c
->var
= new;
1063 case var_string_noescape
:
1066 if (*(char **)c
->var
!= NULL
)
1067 free (*(char **)c
->var
);
1068 *(char **) c
->var
= savestring (arg
, strlen (arg
));
1072 error_no_arg ("filename to set it to.");
1073 if (*(char **)c
->var
!= NULL
)
1074 free (*(char **)c
->var
);
1075 *(char **)c
->var
= tilde_expand (arg
);
1078 *(int *) c
->var
= parse_binary_operation (arg
);
1082 error_no_arg ("integer to set it to.");
1083 *(unsigned int *) c
->var
= parse_and_eval_address (arg
);
1084 if (*(unsigned int *) c
->var
== 0)
1085 *(unsigned int *) c
->var
= UINT_MAX
;
1091 error_no_arg ("integer to set it to.");
1092 val
= parse_and_eval_address (arg
);
1094 *(int *) c
->var
= INT_MAX
;
1095 else if (val
>= INT_MAX
)
1096 error ("integer %u out of range", val
);
1098 *(int *) c
->var
= val
;
1103 error_no_arg ("integer to set it to.");
1104 *(int *) c
->var
= parse_and_eval_address (arg
);
1107 error ("gdb internal error: bad var_type in do_setshow_command");
1110 else if (c
->type
== show_cmd
)
1112 /* Print doc minus "show" at start. */
1113 print_doc_line (gdb_stdout
, c
->doc
+ 5);
1115 fputs_filtered (" is ", gdb_stdout
);
1117 switch (c
->var_type
)
1122 fputs_filtered ("\"", gdb_stdout
);
1123 for (p
= *(unsigned char **) c
->var
; *p
!= '\0'; p
++)
1124 gdb_printchar (*p
, gdb_stdout
, '"');
1125 fputs_filtered ("\"", gdb_stdout
);
1128 case var_string_noescape
:
1130 fputs_filtered ("\"", gdb_stdout
);
1131 fputs_filtered (*(char **) c
->var
, gdb_stdout
);
1132 fputs_filtered ("\"", gdb_stdout
);
1135 fputs_filtered (*(int *) c
->var
? "on" : "off", gdb_stdout
);
1138 if (*(unsigned int *) c
->var
== UINT_MAX
) {
1139 fputs_filtered ("unlimited", gdb_stdout
);
1142 /* else fall through */
1144 fprintf_filtered (gdb_stdout
, "%u", *(unsigned int *) c
->var
);
1147 if (*(int *) c
->var
== INT_MAX
)
1149 fputs_filtered ("unlimited", gdb_stdout
);
1152 fprintf_filtered (gdb_stdout
, "%d", *(int *) c
->var
);
1156 error ("gdb internal error: bad var_type in do_setshow_command");
1158 fputs_filtered (".\n", gdb_stdout
);
1161 error ("gdb internal error: bad cmd_type in do_setshow_command");
1162 (*c
->function
.sfunc
) (NULL
, from_tty
, c
);
1165 /* Show all the settings in a list of show commands. */
1168 cmd_show_list (list
, from_tty
, prefix
)
1169 struct cmd_list_element
*list
;
1173 for (; list
!= NULL
; list
= list
->next
) {
1174 /* If we find a prefix, run its list, prefixing our output by its
1175 prefix (with "show " skipped). */
1176 if (list
->prefixlist
&& !list
->abbrev_flag
)
1177 cmd_show_list (*list
->prefixlist
, from_tty
, list
->prefixname
+ 5);
1178 if (list
->type
== show_cmd
)
1180 fputs_filtered (prefix
, gdb_stdout
);
1181 fputs_filtered (list
->name
, gdb_stdout
);
1182 fputs_filtered (": ", gdb_stdout
);
1183 do_setshow_command ((char *)NULL
, from_tty
, list
);
1190 shell_escape (arg
, from_tty
)
1195 /* FIXME: what about errors (I don't know how GO32 system() handles
1198 #else /* Can fork. */
1199 int rc
, status
, pid
;
1200 char *p
, *user_shell
;
1202 if ((user_shell
= (char *) getenv ("SHELL")) == NULL
)
1203 user_shell
= "/bin/sh";
1205 /* Get the name of the shell for arg0 */
1206 if ((p
= strrchr (user_shell
, '/')) == NULL
)
1209 p
++; /* Get past '/' */
1211 if ((pid
= fork()) == 0)
1214 execl (user_shell
, p
, 0);
1216 execl (user_shell
, p
, "-c", arg
, 0);
1218 fprintf_unfiltered (gdb_stderr
, "Cannot execute %s: %s\n", user_shell
,
1219 safe_strerror (errno
));
1220 gdb_flush (gdb_stderr
);
1225 while ((rc
= wait (&status
)) != pid
&& rc
!= -1)
1228 error ("Fork failed");
1229 #endif /* Can fork. */
1233 make_command (arg
, from_tty
)
1243 p
= xmalloc (sizeof("make ") + strlen(arg
));
1244 strcpy (p
, "make ");
1245 strcpy (p
+ sizeof("make ")-1, arg
);
1248 shell_escape (p
, from_tty
);
1252 show_user_1 (c
, stream
)
1253 struct cmd_list_element
*c
;
1256 register struct command_line
*cmdlines
;
1258 cmdlines
= c
->user_commands
;
1261 fputs_filtered ("User command ", stream
);
1262 fputs_filtered (c
->name
, stream
);
1263 fputs_filtered (":\n", stream
);
1266 fputs_filtered (cmdlines
->line
, stream
);
1267 fputs_filtered ("\n", stream
);
1268 cmdlines
= cmdlines
->next
;
1270 fputs_filtered ("\n", stream
);
1275 show_user (args
, from_tty
)
1279 struct cmd_list_element
*c
;
1280 extern struct cmd_list_element
*cmdlist
;
1284 c
= lookup_cmd (&args
, cmdlist
, "", 0, 1);
1285 if (c
->class != class_user
)
1286 error ("Not a user command.");
1287 show_user_1 (c
, gdb_stdout
);
1291 for (c
= cmdlist
; c
; c
= c
->next
)
1293 if (c
->class == class_user
)
1294 show_user_1 (c
, gdb_stdout
);
1300 _initialize_command ()
1302 add_com ("shell", class_support
, shell_escape
,
1303 "Execute the rest of the line as a shell command. \n\
1304 With no arguments, run an inferior shell.");
1305 add_com ("make", class_support
, make_command
,
1306 "Run the ``make'' program using the rest of the line as arguments.");
1307 add_cmd ("user", no_class
, show_user
,
1308 "Show definitions of user defined commands.\n\
1309 Argument is the name of the user defined command.\n\
1310 With no argument, show definitions of all user defined commands.", &showlist
);