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
;
193 /* This is an empty "cfunc". */
195 not_just_help_class_command (args
, from_tty
)
201 /* This is an empty "sfunc". */
202 static void empty_sfunc
PARAMS ((char *, int, struct cmd_list_element
*));
205 empty_sfunc (args
, from_tty
, c
)
208 struct cmd_list_element
*c
;
212 /* Add element named NAME to command list LIST (the list for set
213 or some sublist thereof).
214 CLASS is as in add_cmd.
215 VAR_TYPE is the kind of thing we are setting.
216 VAR is address of the variable being controlled by this command.
217 DOC is the documentation string. */
219 struct cmd_list_element
*
220 add_set_cmd (name
, class, var_type
, var
, doc
, list
)
222 enum command_class
class;
226 struct cmd_list_element
**list
;
228 struct cmd_list_element
*c
229 = add_cmd (name
, class, NO_FUNCTION
, doc
, list
);
232 c
->var_type
= var_type
;
234 /* This needs to be something besides NO_FUNCTION so that this isn't
235 treated as a help class. */
236 c
->function
.sfunc
= empty_sfunc
;
240 /* Add element named NAME to command list LIST (the list for set
241 or some sublist thereof).
242 CLASS is as in add_cmd.
243 ENUMLIST is a list of strings which may follow NAME.
244 VAR is address of the variable which will contain the matching string
246 DOC is the documentation string. */
248 struct cmd_list_element
*
249 add_set_enum_cmd (name
, class, enumlist
, var
, doc
, list
)
251 enum command_class
class;
255 struct cmd_list_element
**list
;
257 struct cmd_list_element
*c
258 = add_set_cmd (name
, class, var_enum
, var
, doc
, list
);
265 /* Where SETCMD has already been added, add the corresponding show
266 command to LIST and return a pointer to it. */
267 struct cmd_list_element
*
268 add_show_from_set (setcmd
, list
)
269 struct cmd_list_element
*setcmd
;
270 struct cmd_list_element
**list
;
272 struct cmd_list_element
*showcmd
=
273 (struct cmd_list_element
*) xmalloc (sizeof (struct cmd_list_element
));
275 memcpy (showcmd
, setcmd
, sizeof (struct cmd_list_element
));
276 delete_cmd (showcmd
->name
, list
);
277 showcmd
->type
= show_cmd
;
279 /* Replace "set " at start of docstring with "show ". */
280 if (setcmd
->doc
[0] == 'S' && setcmd
->doc
[1] == 'e'
281 && setcmd
->doc
[2] == 't' && setcmd
->doc
[3] == ' ')
282 showcmd
->doc
= concat ("Show ", setcmd
->doc
+ 4, NULL
);
284 fprintf_unfiltered (gdb_stderr
, "GDB internal error: Bad docstring for set command\n");
286 showcmd
->next
= *list
;
291 /* Remove the command named NAME from the command list. */
294 delete_cmd (name
, list
)
296 struct cmd_list_element
**list
;
298 register struct cmd_list_element
*c
;
299 struct cmd_list_element
*p
;
301 while (*list
&& STREQ ((*list
)->name
, name
))
304 (*list
)->hookee
->hook
= 0; /* Hook slips out of its mouth */
311 for (c
= *list
; c
->next
;)
313 if (STREQ (c
->next
->name
, name
))
316 c
->next
->hookee
->hook
= 0; /* hooked cmd gets away. */
326 /* This command really has to deal with two things:
327 * 1) I want documentation on *this string* (usually called by
328 * "help commandname").
329 * 2) I want documentation on *this list* (usually called by
330 * giving a command that requires subcommands. Also called by saying
333 * I am going to split this into two seperate comamnds, help_cmd and
338 help_cmd (command
, stream
)
342 struct cmd_list_element
*c
;
343 extern struct cmd_list_element
*cmdlist
;
347 help_list (cmdlist
, "", all_classes
, stream
);
351 c
= lookup_cmd (&command
, cmdlist
, "", 0, 0);
356 /* There are three cases here.
357 If c->prefixlist is nonzero, we have a prefix command.
358 Print its documentation, then list its subcommands.
360 If c->function is nonzero, we really have a command.
361 Print its documentation and return.
363 If c->function is zero, we have a class name.
364 Print its documentation (as if it were a command)
365 and then set class to the number of this class
366 so that the commands in the class will be listed. */
368 fputs_filtered (c
->doc
, stream
);
369 fputs_filtered ("\n", stream
);
371 if (c
->prefixlist
== 0 && c
->function
.cfunc
!= NULL
)
373 fprintf_filtered (stream
, "\n");
375 /* If this is a prefix command, print it's subcommands */
377 help_list (*c
->prefixlist
, c
->prefixname
, all_commands
, stream
);
379 /* If this is a class name, print all of the commands in the class */
380 if (c
->function
.cfunc
== NULL
)
381 help_list (cmdlist
, "", c
->class, stream
);
384 fprintf_filtered (stream
, "\nThis command has a hook defined: %s\n",
389 * Get a specific kind of help on a command list.
392 * CMDTYPE is the prefix to use in the title string.
393 * CLASS is the class with which to list the nodes of this list (see
394 * documentation for help_cmd_list below), As usual, ALL_COMMANDS for
395 * everything, ALL_CLASSES for just classes, and non-negative for only things
396 * in a specific class.
397 * and STREAM is the output stream on which to print things.
398 * If you call this routine with a class >= 0, it recurses.
401 help_list (list
, cmdtype
, class, stream
)
402 struct cmd_list_element
*list
;
404 enum command_class
class;
408 char *cmdtype1
, *cmdtype2
;
410 /* If CMDTYPE is "foo ", CMDTYPE1 gets " foo" and CMDTYPE2 gets "foo sub" */
411 len
= strlen (cmdtype
);
412 cmdtype1
= (char *) alloca (len
+ 1);
414 cmdtype2
= (char *) alloca (len
+ 4);
419 strncpy (cmdtype1
+ 1, cmdtype
, len
- 1);
421 strncpy (cmdtype2
, cmdtype
, len
- 1);
422 strcpy (cmdtype2
+ len
- 1, " sub");
425 if (class == all_classes
)
426 fprintf_filtered (stream
, "List of classes of %scommands:\n\n", cmdtype2
);
428 fprintf_filtered (stream
, "List of %scommands:\n\n", cmdtype2
);
430 help_cmd_list (list
, class, cmdtype
, (int)class >= 0, stream
);
432 if (class == all_classes
)
433 fprintf_filtered (stream
, "\n\
434 Type \"help%s\" followed by a class name for a list of commands in that class.",
437 fprintf_filtered (stream
, "\n\
438 Type \"help%s\" followed by %scommand name for full documentation.\n\
439 Command name abbreviations are allowed if unambiguous.\n",
443 /* Print only the first line of STR on STREAM. */
445 print_doc_line (stream
, str
)
449 static char *line_buffer
= 0;
450 static int line_size
;
456 line_buffer
= (char *) xmalloc (line_size
);
460 while (*p
&& *p
!= '\n' && *p
!= '.' && *p
!= ',')
462 if (p
- str
> line_size
- 1)
464 line_size
= p
- str
+ 1;
465 free ((PTR
)line_buffer
);
466 line_buffer
= (char *) xmalloc (line_size
);
468 strncpy (line_buffer
, str
, p
- str
);
469 line_buffer
[p
- str
] = '\0';
470 if (islower (line_buffer
[0]))
471 line_buffer
[0] = toupper (line_buffer
[0]);
472 fputs_filtered (line_buffer
, stream
);
476 * Implement a help command on command list LIST.
477 * RECURSE should be non-zero if this should be done recursively on
478 * all sublists of LIST.
479 * PREFIX is the prefix to print before each command name.
480 * STREAM is the stream upon which the output should be written.
482 * A non-negative class number to list only commands in that
484 * ALL_COMMANDS to list all commands in list.
485 * ALL_CLASSES to list all classes in list.
487 * Note that RECURSE will be active on *all* sublists, not just the
488 * ones selected by the criteria above (ie. the selection mechanism
489 * is at the low level, not the high-level).
492 help_cmd_list (list
, class, prefix
, recurse
, stream
)
493 struct cmd_list_element
*list
;
494 enum command_class
class;
499 register struct cmd_list_element
*c
;
501 for (c
= list
; c
; c
= c
->next
)
503 if (c
->abbrev_flag
== 0 &&
504 (class == all_commands
505 || (class == all_classes
&& c
->function
.cfunc
== NULL
)
506 || (class == c
->class && c
->function
.cfunc
!= NULL
)))
508 fprintf_filtered (stream
, "%s%s -- ", prefix
, c
->name
);
509 print_doc_line (stream
, c
->doc
);
510 fputs_filtered ("\n", stream
);
513 && c
->prefixlist
!= 0
514 && c
->abbrev_flag
== 0)
515 help_cmd_list (*c
->prefixlist
, class, c
->prefixname
, 1, stream
);
519 /* This routine takes a line of TEXT and a CLIST in which to start the
520 lookup. When it returns it will have incremented the text pointer past
521 the section of text it matched, set *RESULT_LIST to point to the list in
522 which the last word was matched, and will return a pointer to the cmd
523 list element which the text matches. It will return NULL if no match at
524 all was possible. It will return -1 (cast appropriately, ick) if ambigous
525 matches are possible; in this case *RESULT_LIST will be set to point to
526 the list in which there are ambiguous choices (and *TEXT will be set to
527 the ambiguous text string).
529 If the located command was an abbreviation, this routine returns the base
530 command of the abbreviation.
532 It does no error reporting whatsoever; control will always return
533 to the superior routine.
535 In the case of an ambiguous return (-1), *RESULT_LIST will be set to point
536 at the prefix_command (ie. the best match) *or* (special case) will be NULL
537 if no prefix command was ever found. For example, in the case of "info a",
538 "info" matches without ambiguity, but "a" could be "args" or "address", so
539 *RESULT_LIST is set to the cmd_list_element for "info". So in this case
540 RESULT_LIST should not be interpeted as a pointer to the beginning of a
541 list; it simply points to a specific command. In the case of an ambiguous
542 return *TEXT is advanced past the last non-ambiguous prefix (e.g.
543 "info t" can be "info types" or "info target"; upon return *TEXT has been
544 advanced past "info ").
546 If RESULT_LIST is NULL, don't set *RESULT_LIST (but don't otherwise
547 affect the operation).
549 This routine does *not* modify the text pointed to by TEXT.
551 If IGNORE_HELP_CLASSES is nonzero, ignore any command list elements which
552 are actually help classes rather than commands (i.e. the function field of
553 the struct cmd_list_element is NULL). */
555 struct cmd_list_element
*
556 lookup_cmd_1 (text
, clist
, result_list
, ignore_help_classes
)
558 struct cmd_list_element
*clist
, **result_list
;
559 int ignore_help_classes
;
562 int len
, tmp
, nfound
;
563 struct cmd_list_element
*found
, *c
;
565 while (**text
== ' ' || **text
== '\t')
568 /* Treating underscores as part of command words is important
569 so that "set args_foo()" doesn't get interpreted as
570 "set args _foo()". */
572 *p
&& (isalnum(*p
) || *p
== '-' || *p
== '_');
576 /* If nothing but whitespace, return 0. */
582 /* *text and p now bracket the first command word to lookup (and
583 it's length is len). We copy this into a local temporary,
584 converting to lower case as we go. */
586 command
= (char *) alloca (len
+ 1);
587 for (tmp
= 0; tmp
< len
; tmp
++)
589 char x
= (*text
)[tmp
];
590 command
[tmp
] = isupper(x
) ? tolower(x
) : x
;
597 for (c
= clist
; c
; c
= c
->next
)
598 if (!strncmp (command
, c
->name
, len
)
599 && (!ignore_help_classes
|| c
->function
.cfunc
))
603 if (c
->name
[len
] == '\0')
610 /* If nothing matches, we have a simple failure. */
616 if (result_list
!= NULL
)
617 /* Will be modified in calling routine
618 if we know what the prefix command is. */
620 return (struct cmd_list_element
*) -1; /* Ambiguous. */
623 /* We've matched something on this list. Move text pointer forward. */
627 /* If this was an abbreviation, use the base command instead. */
629 if (found
->cmd_pointer
)
630 found
= found
->cmd_pointer
;
632 /* If we found a prefix command, keep looking. */
634 if (found
->prefixlist
)
636 c
= lookup_cmd_1 (text
, *found
->prefixlist
, result_list
,
637 ignore_help_classes
);
640 /* Didn't find anything; this is as far as we got. */
641 if (result_list
!= NULL
)
642 *result_list
= clist
;
645 else if (c
== (struct cmd_list_element
*) -1)
647 /* We've gotten this far properley, but the next step
648 is ambiguous. We need to set the result list to the best
649 we've found (if an inferior hasn't already set it). */
650 if (result_list
!= NULL
)
652 /* This used to say *result_list = *found->prefixlist
653 If that was correct, need to modify the documentation
654 at the top of this function to clarify what is supposed
656 *result_list
= found
;
667 if (result_list
!= NULL
)
668 *result_list
= clist
;
673 /* All this hair to move the space to the front of cmdtype */
676 undef_cmd_error (cmdtype
, q
)
679 error ("Undefined %scommand: \"%s\". Try \"help%s%.*s\".",
687 /* Look up the contents of *LINE as a command in the command list LIST.
688 LIST is a chain of struct cmd_list_element's.
689 If it is found, return the struct cmd_list_element for that command
690 and update *LINE to point after the command name, at the first argument.
691 If not found, call error if ALLOW_UNKNOWN is zero
692 otherwise (or if error returns) return zero.
693 Call error if specified command is ambiguous,
694 unless ALLOW_UNKNOWN is negative.
695 CMDTYPE precedes the word "command" in the error message.
697 If INGNORE_HELP_CLASSES is nonzero, ignore any command list
698 elements which are actually help classes rather than commands (i.e.
699 the function field of the struct cmd_list_element is 0). */
701 struct cmd_list_element
*
702 lookup_cmd (line
, list
, cmdtype
, allow_unknown
, ignore_help_classes
)
704 struct cmd_list_element
*list
;
707 int ignore_help_classes
;
709 struct cmd_list_element
*last_list
= 0;
710 struct cmd_list_element
*c
=
711 lookup_cmd_1 (line
, list
, &last_list
, ignore_help_classes
);
713 /* This is wrong for complete_command. */
714 char *ptr
= (*line
) + strlen (*line
) - 1;
716 /* Clear off trailing whitespace. */
717 while (ptr
>= *line
&& (*ptr
== ' ' || *ptr
== '\t'))
727 error ("Lack of needed %scommand", cmdtype
);
732 while (isalnum(*p
) || *p
== '-')
735 q
= (char *) alloca (p
- *line
+ 1);
736 strncpy (q
, *line
, p
- *line
);
738 undef_cmd_error (cmdtype
, q
);
744 else if (c
== (struct cmd_list_element
*) -1)
746 /* Ambigous. Local values should be off prefixlist or called
748 int local_allow_unknown
= (last_list
? last_list
->allow_unknown
:
750 char *local_cmdtype
= last_list
? last_list
->prefixname
: cmdtype
;
751 struct cmd_list_element
*local_list
=
752 (last_list
? *(last_list
->prefixlist
) : list
);
754 if (local_allow_unknown
< 0)
757 return last_list
; /* Found something. */
759 return 0; /* Found nothing. */
763 /* Report as error. */
768 ((*line
)[amb_len
] && (*line
)[amb_len
] != ' '
769 && (*line
)[amb_len
] != '\t');
774 for (c
= local_list
; c
; c
= c
->next
)
775 if (!strncmp (*line
, c
->name
, amb_len
))
777 if (strlen (ambbuf
) + strlen (c
->name
) + 6 < (int)sizeof ambbuf
)
780 strcat (ambbuf
, ", ");
781 strcat (ambbuf
, c
->name
);
785 strcat (ambbuf
, "..");
789 error ("Ambiguous %scommand \"%s\": %s.", local_cmdtype
,
796 /* We've got something. It may still not be what the caller
797 wants (if this command *needs* a subcommand). */
798 while (**line
== ' ' || **line
== '\t')
801 if (c
->prefixlist
&& **line
&& !c
->allow_unknown
)
802 undef_cmd_error (c
->prefixname
, *line
);
804 /* Seems to be what he wants. Return it. */
811 /* Look up the contents of *LINE as a command in the command list LIST.
812 LIST is a chain of struct cmd_list_element's.
813 If it is found, return the struct cmd_list_element for that command
814 and update *LINE to point after the command name, at the first argument.
815 If not found, call error if ALLOW_UNKNOWN is zero
816 otherwise (or if error returns) return zero.
817 Call error if specified command is ambiguous,
818 unless ALLOW_UNKNOWN is negative.
819 CMDTYPE precedes the word "command" in the error message. */
821 struct cmd_list_element
*
822 lookup_cmd (line
, list
, cmdtype
, allow_unknown
)
824 struct cmd_list_element
*list
;
829 register struct cmd_list_element
*c
, *found
;
835 /* Skip leading whitespace. */
837 while (**line
== ' ' || **line
== '\t')
840 /* Clear out trailing whitespace. */
842 p
= *line
+ strlen (*line
);
843 while (p
!= *line
&& (p
[-1] == ' ' || p
[-1] == '\t'))
847 /* Find end of command name. */
850 while (*p
== '-' || isalnum(*p
))
853 /* Look up the command name.
854 If exact match, keep that.
855 Otherwise, take command abbreviated, if unique. Note that (in my
856 opinion) a null string does *not* indicate ambiguity; simply the
857 end of the argument. */
862 error ("Lack of needed %scommand", cmdtype
);
866 /* Copy over to a local buffer, converting to lowercase on the way.
867 This is in case the command being parsed is a subcommand which
868 doesn't match anything, and that's ok. We want the original
869 untouched for the routine of the original command. */
871 processed_cmd
= (char *) alloca (p
- *line
+ 1);
872 for (cmd_len
= 0; cmd_len
< p
- *line
; cmd_len
++)
874 char x
= (*line
)[cmd_len
];
876 processed_cmd
[cmd_len
] = tolower(x
);
878 processed_cmd
[cmd_len
] = x
;
880 processed_cmd
[cmd_len
] = '\0';
882 /* Check all possibilities in the current command list. */
885 for (c
= list
; c
; c
= c
->next
)
887 if (!strncmp (processed_cmd
, c
->name
, cmd_len
))
891 if (c
->name
[cmd_len
] == 0)
899 /* Report error for undefined command name. */
903 if (nfound
> 1 && allow_unknown
>= 0)
906 for (c
= list
; c
; c
= c
->next
)
907 if (!strncmp (processed_cmd
, c
->name
, cmd_len
))
909 if (strlen (ambbuf
) + strlen (c
->name
) + 6 < sizeof ambbuf
)
912 strcat (ambbuf
, ", ");
913 strcat (ambbuf
, c
->name
);
917 strcat (ambbuf
, "..");
921 error ("Ambiguous %scommand \"%s\": %s.", cmdtype
,
922 processed_cmd
, ambbuf
);
924 else if (!allow_unknown
)
925 error ("Undefined %scommand: \"%s\".", cmdtype
, processed_cmd
);
929 /* Skip whitespace before the argument. */
931 while (*p
== ' ' || *p
== '\t') p
++;
934 if (found
->prefixlist
&& *p
)
936 c
= lookup_cmd (line
, *found
->prefixlist
, found
->prefixname
,
937 found
->allow_unknown
);
946 /* Helper function for SYMBOL_COMPLETION_FUNCTION. */
948 /* Return a vector of char pointers which point to the different
949 possible completions in LIST of TEXT.
951 WORD points in the same buffer as TEXT, and completions should be
952 returned relative to this position. For example, suppose TEXT is "foo"
953 and we want to complete to "foobar". If WORD is "oo", return
954 "oobar"; if WORD is "baz/foo", return "baz/foobar". */
957 complete_on_cmdlist (list
, text
, word
)
958 struct cmd_list_element
*list
;
962 struct cmd_list_element
*ptr
;
964 int sizeof_matchlist
;
966 int textlen
= strlen (text
);
968 sizeof_matchlist
= 10;
969 matchlist
= (char **) xmalloc (sizeof_matchlist
* sizeof (char *));
972 for (ptr
= list
; ptr
; ptr
= ptr
->next
)
973 if (!strncmp (ptr
->name
, text
, textlen
)
975 && (ptr
->function
.cfunc
978 if (matches
== sizeof_matchlist
)
980 sizeof_matchlist
*= 2;
981 matchlist
= (char **) xrealloc ((char *)matchlist
,
986 matchlist
[matches
] = (char *)
987 xmalloc (strlen (word
) + strlen (ptr
->name
) + 1);
989 strcpy (matchlist
[matches
], ptr
->name
);
990 else if (word
> text
)
992 /* Return some portion of ptr->name. */
993 strcpy (matchlist
[matches
], ptr
->name
+ (word
- text
));
997 /* Return some of text plus ptr->name. */
998 strncpy (matchlist
[matches
], word
, text
- word
);
999 matchlist
[matches
][text
- word
] = '\0';
1000 strcat (matchlist
[matches
], ptr
->name
);
1007 free ((PTR
)matchlist
);
1012 matchlist
= (char **) xrealloc ((char *)matchlist
, ((matches
+ 1)
1013 * sizeof (char *)));
1014 matchlist
[matches
] = (char *) 0;
1020 /* Helper function for SYMBOL_COMPLETION_FUNCTION. */
1022 /* Return a vector of char pointers which point to the different
1023 possible completions in CMD of TEXT.
1025 WORD points in the same buffer as TEXT, and completions should be
1026 returned relative to this position. For example, suppose TEXT is "foo"
1027 and we want to complete to "foobar". If WORD is "oo", return
1028 "oobar"; if WORD is "baz/foo", return "baz/foobar". */
1031 complete_on_enum (enumlist
, text
, word
)
1037 int sizeof_matchlist
;
1039 int textlen
= strlen (text
);
1043 sizeof_matchlist
= 10;
1044 matchlist
= (char **) xmalloc (sizeof_matchlist
* sizeof (char *));
1047 for (i
= 0; name
= enumlist
[i
]; i
++)
1048 if (strncmp (name
, text
, textlen
) == 0)
1050 if (matches
== sizeof_matchlist
)
1052 sizeof_matchlist
*= 2;
1053 matchlist
= (char **) xrealloc ((char *)matchlist
,
1055 * sizeof (char *)));
1058 matchlist
[matches
] = (char *)
1059 xmalloc (strlen (word
) + strlen (name
) + 1);
1061 strcpy (matchlist
[matches
], name
);
1062 else if (word
> text
)
1064 /* Return some portion of name. */
1065 strcpy (matchlist
[matches
], name
+ (word
- text
));
1069 /* Return some of text plus name. */
1070 strncpy (matchlist
[matches
], word
, text
- word
);
1071 matchlist
[matches
][text
- word
] = '\0';
1072 strcat (matchlist
[matches
], name
);
1079 free ((PTR
)matchlist
);
1084 matchlist
= (char **) xrealloc ((char *)matchlist
, ((matches
+ 1)
1085 * sizeof (char *)));
1086 matchlist
[matches
] = (char *) 0;
1093 parse_binary_operation (arg
)
1101 length
= strlen (arg
);
1103 while (arg
[length
- 1] == ' ' || arg
[length
- 1] == '\t')
1106 if (!strncmp (arg
, "on", length
)
1107 || !strncmp (arg
, "1", length
)
1108 || !strncmp (arg
, "yes", length
))
1111 if (!strncmp (arg
, "off", length
)
1112 || !strncmp (arg
, "0", length
)
1113 || !strncmp (arg
, "no", length
))
1117 error ("\"on\" or \"off\" expected.");
1122 /* Do a "set" or "show" command. ARG is NULL if no argument, or the text
1123 of the argument, and FROM_TTY is nonzero if this command is being entered
1124 directly by the user (i.e. these are just like any other
1125 command). C is the command list element for the command. */
1127 do_setshow_command (arg
, from_tty
, c
)
1130 struct cmd_list_element
*c
;
1132 if (c
->type
== set_cmd
)
1134 switch (c
->var_type
)
1145 new = (char *) xmalloc (strlen (arg
) + 2);
1147 while ((ch
= *p
++) != '\000')
1151 /* \ at end of argument is used after spaces
1152 so they won't be lost. */
1153 /* This is obsolete now that we no longer strip
1154 trailing whitespace and actually, the backslash
1155 didn't get here in my test, readline or
1156 something did something funky with a backslash
1157 right before a newline. */
1160 ch
= parse_escape (&p
);
1162 break; /* C loses */
1170 if (*(p
- 1) != '\\')
1174 new = (char *) xrealloc (new, q
- new);
1175 if (*(char **)c
->var
!= NULL
)
1176 free (*(char **)c
->var
);
1177 *(char **) c
->var
= new;
1180 case var_string_noescape
:
1183 if (*(char **)c
->var
!= NULL
)
1184 free (*(char **)c
->var
);
1185 *(char **) c
->var
= savestring (arg
, strlen (arg
));
1189 error_no_arg ("filename to set it to.");
1190 if (*(char **)c
->var
!= NULL
)
1191 free (*(char **)c
->var
);
1192 *(char **)c
->var
= tilde_expand (arg
);
1195 *(int *) c
->var
= parse_binary_operation (arg
);
1199 error_no_arg ("integer to set it to.");
1200 *(unsigned int *) c
->var
= parse_and_eval_address (arg
);
1201 if (*(unsigned int *) c
->var
== 0)
1202 *(unsigned int *) c
->var
= UINT_MAX
;
1208 error_no_arg ("integer to set it to.");
1209 val
= parse_and_eval_address (arg
);
1211 *(int *) c
->var
= INT_MAX
;
1212 else if (val
>= INT_MAX
)
1213 error ("integer %u out of range", val
);
1215 *(int *) c
->var
= val
;
1220 error_no_arg ("integer to set it to.");
1221 *(int *) c
->var
= parse_and_eval_address (arg
);
1231 p
= strchr (arg
, ' ');
1239 for (i
= 0; c
->enums
[i
]; i
++)
1240 if (strncmp (arg
, c
->enums
[i
], len
) == 0)
1242 match
= c
->enums
[i
];
1247 error ("Undefined item: \"%s\".", arg
);
1250 error ("Ambiguous item \"%s\".", arg
);
1252 *(char **)c
->var
= match
;
1256 error ("gdb internal error: bad var_type in do_setshow_command");
1259 else if (c
->type
== show_cmd
)
1261 /* Print doc minus "show" at start. */
1262 print_doc_line (gdb_stdout
, c
->doc
+ 5);
1264 fputs_filtered (" is ", gdb_stdout
);
1266 switch (c
->var_type
)
1271 fputs_filtered ("\"", gdb_stdout
);
1272 for (p
= *(unsigned char **) c
->var
; *p
!= '\0'; p
++)
1273 gdb_printchar (*p
, gdb_stdout
, '"');
1274 fputs_filtered ("\"", gdb_stdout
);
1277 case var_string_noescape
:
1280 fputs_filtered ("\"", gdb_stdout
);
1281 fputs_filtered (*(char **) c
->var
, gdb_stdout
);
1282 fputs_filtered ("\"", gdb_stdout
);
1285 fputs_filtered (*(int *) c
->var
? "on" : "off", gdb_stdout
);
1288 if (*(unsigned int *) c
->var
== UINT_MAX
) {
1289 fputs_filtered ("unlimited", gdb_stdout
);
1292 /* else fall through */
1294 fprintf_filtered (gdb_stdout
, "%u", *(unsigned int *) c
->var
);
1297 if (*(int *) c
->var
== INT_MAX
)
1299 fputs_filtered ("unlimited", gdb_stdout
);
1302 fprintf_filtered (gdb_stdout
, "%d", *(int *) c
->var
);
1306 error ("gdb internal error: bad var_type in do_setshow_command");
1308 fputs_filtered (".\n", gdb_stdout
);
1311 error ("gdb internal error: bad cmd_type in do_setshow_command");
1312 (*c
->function
.sfunc
) (NULL
, from_tty
, c
);
1315 /* Show all the settings in a list of show commands. */
1318 cmd_show_list (list
, from_tty
, prefix
)
1319 struct cmd_list_element
*list
;
1323 for (; list
!= NULL
; list
= list
->next
) {
1324 /* If we find a prefix, run its list, prefixing our output by its
1325 prefix (with "show " skipped). */
1326 if (list
->prefixlist
&& !list
->abbrev_flag
)
1327 cmd_show_list (*list
->prefixlist
, from_tty
, list
->prefixname
+ 5);
1328 if (list
->type
== show_cmd
)
1330 fputs_filtered (prefix
, gdb_stdout
);
1331 fputs_filtered (list
->name
, gdb_stdout
);
1332 fputs_filtered (": ", gdb_stdout
);
1333 do_setshow_command ((char *)NULL
, from_tty
, list
);
1340 shell_escape (arg
, from_tty
)
1345 /* FIXME: what about errors (I don't know how GO32 system() handles
1348 #else /* Can fork. */
1349 int rc
, status
, pid
;
1350 char *p
, *user_shell
;
1352 if ((user_shell
= (char *) getenv ("SHELL")) == NULL
)
1353 user_shell
= "/bin/sh";
1355 /* Get the name of the shell for arg0 */
1356 if ((p
= strrchr (user_shell
, '/')) == NULL
)
1359 p
++; /* Get past '/' */
1361 if ((pid
= fork()) == 0)
1364 execl (user_shell
, p
, 0);
1366 execl (user_shell
, p
, "-c", arg
, 0);
1368 fprintf_unfiltered (gdb_stderr
, "Cannot execute %s: %s\n", user_shell
,
1369 safe_strerror (errno
));
1370 gdb_flush (gdb_stderr
);
1375 while ((rc
= wait (&status
)) != pid
&& rc
!= -1)
1378 error ("Fork failed");
1379 #endif /* Can fork. */
1383 make_command (arg
, from_tty
)
1393 p
= xmalloc (sizeof("make ") + strlen(arg
));
1394 strcpy (p
, "make ");
1395 strcpy (p
+ sizeof("make ")-1, arg
);
1398 shell_escape (p
, from_tty
);
1402 show_user_1 (c
, stream
)
1403 struct cmd_list_element
*c
;
1406 register struct command_line
*cmdlines
;
1408 cmdlines
= c
->user_commands
;
1411 fputs_filtered ("User command ", stream
);
1412 fputs_filtered (c
->name
, stream
);
1413 fputs_filtered (":\n", stream
);
1417 print_command_line (cmdlines
, 4);
1418 cmdlines
= cmdlines
->next
;
1420 fputs_filtered ("\n", stream
);
1425 show_user (args
, from_tty
)
1429 struct cmd_list_element
*c
;
1430 extern struct cmd_list_element
*cmdlist
;
1434 c
= lookup_cmd (&args
, cmdlist
, "", 0, 1);
1435 if (c
->class != class_user
)
1436 error ("Not a user command.");
1437 show_user_1 (c
, gdb_stdout
);
1441 for (c
= cmdlist
; c
; c
= c
->next
)
1443 if (c
->class == class_user
)
1444 show_user_1 (c
, gdb_stdout
);
1450 _initialize_command ()
1452 add_com ("shell", class_support
, shell_escape
,
1453 "Execute the rest of the line as a shell command. \n\
1454 With no arguments, run an inferior shell.");
1455 add_com ("make", class_support
, make_command
,
1456 "Run the ``make'' program using the rest of the line as arguments.");
1457 add_cmd ("user", no_class
, show_user
,
1458 "Show definitions of user defined commands.\n\
1459 Argument is the name of the user defined command.\n\
1460 With no argument, show definitions of all user defined commands.", &showlist
);