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. */
24 #include "gdb_string.h"
32 # ifdef HAVE_SYS_WAIT_H
33 # include <sys/wait.h>
37 /* Prototypes for local functions */
40 undef_cmd_error PARAMS ((char *, char *));
43 show_user PARAMS ((char *, int));
46 show_user_1 PARAMS ((struct cmd_list_element *, GDB_FILE *));
49 make_command PARAMS ((char *, int));
52 shell_escape PARAMS ((char *, int));
55 parse_binary_operation PARAMS ((char *));
58 print_doc_line PARAMS ((GDB_FILE *, char *));
60 /* Add element named NAME.
61 CLASS is the top level category into which commands are broken down
63 FUN should be the function to execute the command;
64 it will get a character string as argument, with leading
65 and trailing blanks already eliminated.
67 DOC is a documentation string for the command.
68 Its first line should be a complete sentence.
69 It should start with ? for a command that is an abbreviation
70 or with * for a command that most users don't need to know about.
72 Add this command to command list *LIST.
74 Returns a pointer to the added command (not necessarily the head
77 struct cmd_list_element *
78 add_cmd (name, class, fun, doc, list)
80 enum command_class class;
81 void (*fun) PARAMS ((char *, int));
83 struct cmd_list_element **list;
85 register struct cmd_list_element *c
86 = (struct cmd_list_element *) xmalloc (sizeof (struct cmd_list_element));
87 struct cmd_list_element *p;
89 delete_cmd (name, list);
91 if (*list == NULL || STRCMP ((*list)->name, name) >= 0)
99 while (p->next && STRCMP (p->next->name, name) <= 0)
109 c->function.cfunc = fun;
112 c->prefixlist = NULL;
113 c->prefixname = NULL;
114 c->allow_unknown = 0;
116 c->completer = make_symbol_completion_list;
117 c->type = not_set_cmd;
119 c->var_type = var_boolean;
121 c->user_commands = NULL;
123 c->cmd_pointer = NULL;
128 /* Same as above, except that the abbrev_flag is set. */
130 #if 0 /* Currently unused */
132 struct cmd_list_element *
133 add_abbrev_cmd (name, class, fun, doc, list)
135 enum command_class class;
136 void (*fun) PARAMS ((char *, int));
138 struct cmd_list_element **list;
140 register struct cmd_list_element *c
141 = add_cmd (name, class, fun, doc, list);
149 struct cmd_list_element *
150 add_alias_cmd (name, oldname, class, abbrev_flag, list)
153 enum command_class class;
155 struct cmd_list_element **list;
157 /* Must do this since lookup_cmd tries to side-effect its first arg */
159 register struct cmd_list_element *old;
160 register struct cmd_list_element *c;
161 copied_name = (char *) alloca (strlen (oldname) + 1);
162 strcpy (copied_name, oldname);
163 old = lookup_cmd (&copied_name, *list, "", 1, 1);
167 delete_cmd (name, list);
171 c = add_cmd (name, class, old->function.cfunc, old->doc, list);
172 c->prefixlist = old->prefixlist;
173 c->prefixname = old->prefixname;
174 c->allow_unknown = old->allow_unknown;
175 c->abbrev_flag = abbrev_flag;
176 c->cmd_pointer = old;
180 /* Like add_cmd but adds an element for a command prefix:
181 a name that should be followed by a subcommand to be looked up
182 in another command list. PREFIXLIST should be the address
183 of the variable containing that list. */
185 struct cmd_list_element *
186 add_prefix_cmd (name, class, fun, doc, prefixlist, prefixname,
189 enum command_class class;
190 void (*fun) PARAMS ((char *, int));
192 struct cmd_list_element **prefixlist;
195 struct cmd_list_element **list;
197 register struct cmd_list_element *c = add_cmd (name, class, fun, doc, list);
198 c->prefixlist = prefixlist;
199 c->prefixname = prefixname;
200 c->allow_unknown = allow_unknown;
204 /* Like add_prefix_cmd but sets the abbrev_flag on the new command. */
206 struct cmd_list_element *
207 add_abbrev_prefix_cmd (name, class, fun, doc, prefixlist, prefixname,
210 enum command_class class;
211 void (*fun) PARAMS ((char *, int));
213 struct cmd_list_element **prefixlist;
216 struct cmd_list_element **list;
218 register struct cmd_list_element *c = add_cmd (name, class, fun, doc, list);
219 c->prefixlist = prefixlist;
220 c->prefixname = prefixname;
221 c->allow_unknown = allow_unknown;
226 /* This is an empty "cfunc". */
228 not_just_help_class_command (args, from_tty)
234 /* This is an empty "sfunc". */
235 static void empty_sfunc PARAMS ((char *, int, struct cmd_list_element *));
238 empty_sfunc (args, from_tty, c)
241 struct cmd_list_element *c;
245 /* Add element named NAME to command list LIST (the list for set
246 or some sublist thereof).
247 CLASS is as in add_cmd.
248 VAR_TYPE is the kind of thing we are setting.
249 VAR is address of the variable being controlled by this command.
250 DOC is the documentation string. */
252 struct cmd_list_element *
253 add_set_cmd (name, class, var_type, var, doc, list)
255 enum command_class class;
259 struct cmd_list_element **list;
261 struct cmd_list_element *c
262 = add_cmd (name, class, NO_FUNCTION, doc, list);
265 c->var_type = var_type;
267 /* This needs to be something besides NO_FUNCTION so that this isn't
268 treated as a help class. */
269 c->function.sfunc = empty_sfunc;
273 /* Add element named NAME to command list LIST (the list for set
274 or some sublist thereof).
275 CLASS is as in add_cmd.
276 ENUMLIST is a list of strings which may follow NAME.
277 VAR is address of the variable which will contain the matching string
279 DOC is the documentation string. */
281 struct cmd_list_element *
282 add_set_enum_cmd (name, class, enumlist, var, doc, list)
284 enum command_class class;
288 struct cmd_list_element **list;
290 struct cmd_list_element *c
291 = add_set_cmd (name, class, var_enum, var, doc, list);
297 /* Where SETCMD has already been added, add the corresponding show
298 command to LIST and return a pointer to the added command (not
299 necessarily the head of LIST). */
300 struct cmd_list_element *
301 add_show_from_set (setcmd, list)
302 struct cmd_list_element *setcmd;
303 struct cmd_list_element **list;
305 struct cmd_list_element *showcmd =
306 (struct cmd_list_element *) xmalloc (sizeof (struct cmd_list_element));
307 struct cmd_list_element *p;
309 memcpy (showcmd, setcmd, sizeof (struct cmd_list_element));
310 delete_cmd (showcmd->name, list);
311 showcmd->type = show_cmd;
313 /* Replace "set " at start of docstring with "show ". */
314 if (setcmd->doc[0] == 'S' && setcmd->doc[1] == 'e'
315 && setcmd->doc[2] == 't' && setcmd->doc[3] == ' ')
316 showcmd->doc = concat ("Show ", setcmd->doc + 4, NULL);
318 fprintf_unfiltered (gdb_stderr, "GDB internal error: Bad docstring for set command\n");
320 if (*list == NULL || STRCMP ((*list)->name, showcmd->name) >= 0)
322 showcmd->next = *list;
328 while (p->next && STRCMP (p->next->name, showcmd->name) <= 0)
332 showcmd->next = p->next;
339 /* Remove the command named NAME from the command list. */
342 delete_cmd (name, list)
344 struct cmd_list_element **list;
346 register struct cmd_list_element *c;
347 struct cmd_list_element *p;
349 while (*list && STREQ ((*list)->name, name))
352 (*list)->hookee->hook = 0; /* Hook slips out of its mouth */
359 for (c = *list; c->next;)
361 if (STREQ (c->next->name, name))
364 c->next->hookee->hook = 0; /* hooked cmd gets away. */
374 /* This command really has to deal with two things:
375 * 1) I want documentation on *this string* (usually called by
376 * "help commandname").
377 * 2) I want documentation on *this list* (usually called by
378 * giving a command that requires subcommands. Also called by saying
381 * I am going to split this into two seperate comamnds, help_cmd and
386 help_cmd (command, stream)
390 struct cmd_list_element *c;
391 extern struct cmd_list_element *cmdlist;
395 help_list (cmdlist, "", all_classes, stream);
399 c = lookup_cmd (&command, cmdlist, "", 0, 0);
404 /* There are three cases here.
405 If c->prefixlist is nonzero, we have a prefix command.
406 Print its documentation, then list its subcommands.
408 If c->function is nonzero, we really have a command.
409 Print its documentation and return.
411 If c->function is zero, we have a class name.
412 Print its documentation (as if it were a command)
413 and then set class to the number of this class
414 so that the commands in the class will be listed. */
416 fputs_filtered (c->doc, stream);
417 fputs_filtered ("\n", stream);
419 if (c->prefixlist == 0 && c->function.cfunc != NULL)
421 fprintf_filtered (stream, "\n");
423 /* If this is a prefix command, print it's subcommands */
425 help_list (*c->prefixlist, c->prefixname, all_commands, stream);
427 /* If this is a class name, print all of the commands in the class */
428 if (c->function.cfunc == NULL)
429 help_list (cmdlist, "", c->class, stream);
432 fprintf_filtered (stream, "\nThis command has a hook defined: %s\n",
437 * Get a specific kind of help on a command list.
440 * CMDTYPE is the prefix to use in the title string.
441 * CLASS is the class with which to list the nodes of this list (see
442 * documentation for help_cmd_list below), As usual, ALL_COMMANDS for
443 * everything, ALL_CLASSES for just classes, and non-negative for only things
444 * in a specific class.
445 * and STREAM is the output stream on which to print things.
446 * If you call this routine with a class >= 0, it recurses.
449 help_list (list, cmdtype, class, stream)
450 struct cmd_list_element *list;
452 enum command_class class;
456 char *cmdtype1, *cmdtype2;
458 /* If CMDTYPE is "foo ", CMDTYPE1 gets " foo" and CMDTYPE2 gets "foo sub" */
459 len = strlen (cmdtype);
460 cmdtype1 = (char *) alloca (len + 1);
462 cmdtype2 = (char *) alloca (len + 4);
467 strncpy (cmdtype1 + 1, cmdtype, len - 1);
469 strncpy (cmdtype2, cmdtype, len - 1);
470 strcpy (cmdtype2 + len - 1, " sub");
473 if (class == all_classes)
474 fprintf_filtered (stream, "List of classes of %scommands:\n\n", cmdtype2);
476 fprintf_filtered (stream, "List of %scommands:\n\n", cmdtype2);
478 help_cmd_list (list, class, cmdtype, (int)class >= 0, stream);
480 if (class == all_classes)
481 fprintf_filtered (stream, "\n\
482 Type \"help%s\" followed by a class name for a list of commands in that class.",
485 fprintf_filtered (stream, "\n\
486 Type \"help%s\" followed by %scommand name for full documentation.\n\
487 Command name abbreviations are allowed if unambiguous.\n",
491 /* Print only the first line of STR on STREAM. */
493 print_doc_line (stream, str)
497 static char *line_buffer = 0;
498 static int line_size;
504 line_buffer = (char *) xmalloc (line_size);
508 while (*p && *p != '\n' && *p != '.' && *p != ',')
510 if (p - str > line_size - 1)
512 line_size = p - str + 1;
513 free ((PTR)line_buffer);
514 line_buffer = (char *) xmalloc (line_size);
516 strncpy (line_buffer, str, p - str);
517 line_buffer[p - str] = '\0';
518 if (islower (line_buffer[0]))
519 line_buffer[0] = toupper (line_buffer[0]);
520 fputs_filtered (line_buffer, stream);
524 * Implement a help command on command list LIST.
525 * RECURSE should be non-zero if this should be done recursively on
526 * all sublists of LIST.
527 * PREFIX is the prefix to print before each command name.
528 * STREAM is the stream upon which the output should be written.
530 * A non-negative class number to list only commands in that
532 * ALL_COMMANDS to list all commands in list.
533 * ALL_CLASSES to list all classes in list.
535 * Note that RECURSE will be active on *all* sublists, not just the
536 * ones selected by the criteria above (ie. the selection mechanism
537 * is at the low level, not the high-level).
540 help_cmd_list (list, class, prefix, recurse, stream)
541 struct cmd_list_element *list;
542 enum command_class class;
547 register struct cmd_list_element *c;
549 for (c = list; c; c = c->next)
551 if (c->abbrev_flag == 0 &&
552 (class == all_commands
553 || (class == all_classes && c->function.cfunc == NULL)
554 || (class == c->class && c->function.cfunc != NULL)))
556 fprintf_filtered (stream, "%s%s -- ", prefix, c->name);
557 print_doc_line (stream, c->doc);
558 fputs_filtered ("\n", stream);
561 && c->prefixlist != 0
562 && c->abbrev_flag == 0)
563 help_cmd_list (*c->prefixlist, class, c->prefixname, 1, stream);
567 /* This routine takes a line of TEXT and a CLIST in which to start the
568 lookup. When it returns it will have incremented the text pointer past
569 the section of text it matched, set *RESULT_LIST to point to the list in
570 which the last word was matched, and will return a pointer to the cmd
571 list element which the text matches. It will return NULL if no match at
572 all was possible. It will return -1 (cast appropriately, ick) if ambigous
573 matches are possible; in this case *RESULT_LIST will be set to point to
574 the list in which there are ambiguous choices (and *TEXT will be set to
575 the ambiguous text string).
577 If the located command was an abbreviation, this routine returns the base
578 command of the abbreviation.
580 It does no error reporting whatsoever; control will always return
581 to the superior routine.
583 In the case of an ambiguous return (-1), *RESULT_LIST will be set to point
584 at the prefix_command (ie. the best match) *or* (special case) will be NULL
585 if no prefix command was ever found. For example, in the case of "info a",
586 "info" matches without ambiguity, but "a" could be "args" or "address", so
587 *RESULT_LIST is set to the cmd_list_element for "info". So in this case
588 RESULT_LIST should not be interpeted as a pointer to the beginning of a
589 list; it simply points to a specific command. In the case of an ambiguous
590 return *TEXT is advanced past the last non-ambiguous prefix (e.g.
591 "info t" can be "info types" or "info target"; upon return *TEXT has been
592 advanced past "info ").
594 If RESULT_LIST is NULL, don't set *RESULT_LIST (but don't otherwise
595 affect the operation).
597 This routine does *not* modify the text pointed to by TEXT.
599 If IGNORE_HELP_CLASSES is nonzero, ignore any command list elements which
600 are actually help classes rather than commands (i.e. the function field of
601 the struct cmd_list_element is NULL). */
603 struct cmd_list_element *
604 lookup_cmd_1 (text, clist, result_list, ignore_help_classes)
606 struct cmd_list_element *clist, **result_list;
607 int ignore_help_classes;
610 int len, tmp, nfound;
611 struct cmd_list_element *found, *c;
613 while (**text == ' ' || **text == '\t')
616 /* Treating underscores as part of command words is important
617 so that "set args_foo()" doesn't get interpreted as
618 "set args _foo()". */
620 *p && (isalnum(*p) || *p == '-' || *p == '_');
624 /* If nothing but whitespace, return 0. */
630 /* *text and p now bracket the first command word to lookup (and
631 it's length is len). We copy this into a local temporary,
632 converting to lower case as we go. */
634 command = (char *) alloca (len + 1);
635 for (tmp = 0; tmp < len; tmp++)
637 char x = (*text)[tmp];
638 command[tmp] = isupper(x) ? tolower(x) : x;
645 for (c = clist; c; c = c->next)
646 if (!strncmp (command, c->name, len)
647 && (!ignore_help_classes || c->function.cfunc))
651 if (c->name[len] == '\0')
658 /* If nothing matches, we have a simple failure. */
664 if (result_list != NULL)
665 /* Will be modified in calling routine
666 if we know what the prefix command is. */
668 return (struct cmd_list_element *) -1; /* Ambiguous. */
671 /* We've matched something on this list. Move text pointer forward. */
675 /* If this was an abbreviation, use the base command instead. */
677 if (found->cmd_pointer)
678 found = found->cmd_pointer;
680 /* If we found a prefix command, keep looking. */
682 if (found->prefixlist)
684 c = lookup_cmd_1 (text, *found->prefixlist, result_list,
685 ignore_help_classes);
688 /* Didn't find anything; this is as far as we got. */
689 if (result_list != NULL)
690 *result_list = clist;
693 else if (c == (struct cmd_list_element *) -1)
695 /* We've gotten this far properly, but the next step
696 is ambiguous. We need to set the result list to the best
697 we've found (if an inferior hasn't already set it). */
698 if (result_list != NULL)
700 /* This used to say *result_list = *found->prefixlist
701 If that was correct, need to modify the documentation
702 at the top of this function to clarify what is supposed
704 *result_list = found;
715 if (result_list != NULL)
716 *result_list = clist;
721 /* All this hair to move the space to the front of cmdtype */
724 undef_cmd_error (cmdtype, q)
727 error ("Undefined %scommand: \"%s\". Try \"help%s%.*s\".",
735 /* Look up the contents of *LINE as a command in the command list LIST.
736 LIST is a chain of struct cmd_list_element's.
737 If it is found, return the struct cmd_list_element for that command
738 and update *LINE to point after the command name, at the first argument.
739 If not found, call error if ALLOW_UNKNOWN is zero
740 otherwise (or if error returns) return zero.
741 Call error if specified command is ambiguous,
742 unless ALLOW_UNKNOWN is negative.
743 CMDTYPE precedes the word "command" in the error message.
745 If INGNORE_HELP_CLASSES is nonzero, ignore any command list
746 elements which are actually help classes rather than commands (i.e.
747 the function field of the struct cmd_list_element is 0). */
749 struct cmd_list_element *
750 lookup_cmd (line, list, cmdtype, allow_unknown, ignore_help_classes)
752 struct cmd_list_element *list;
755 int ignore_help_classes;
757 struct cmd_list_element *last_list = 0;
758 struct cmd_list_element *c =
759 lookup_cmd_1 (line, list, &last_list, ignore_help_classes);
761 /* This is wrong for complete_command. */
762 char *ptr = (*line) + strlen (*line) - 1;
764 /* Clear off trailing whitespace. */
765 while (ptr >= *line && (*ptr == ' ' || *ptr == '\t'))
775 error ("Lack of needed %scommand", cmdtype);
780 while (isalnum(*p) || *p == '-')
783 q = (char *) alloca (p - *line + 1);
784 strncpy (q, *line, p - *line);
786 undef_cmd_error (cmdtype, q);
792 else if (c == (struct cmd_list_element *) -1)
794 /* Ambigous. Local values should be off prefixlist or called
796 int local_allow_unknown = (last_list ? last_list->allow_unknown :
798 char *local_cmdtype = last_list ? last_list->prefixname : cmdtype;
799 struct cmd_list_element *local_list =
800 (last_list ? *(last_list->prefixlist) : list);
802 if (local_allow_unknown < 0)
805 return last_list; /* Found something. */
807 return 0; /* Found nothing. */
811 /* Report as error. */
816 ((*line)[amb_len] && (*line)[amb_len] != ' '
817 && (*line)[amb_len] != '\t');
822 for (c = local_list; c; c = c->next)
823 if (!strncmp (*line, c->name, amb_len))
825 if (strlen (ambbuf) + strlen (c->name) + 6 < (int)sizeof ambbuf)
828 strcat (ambbuf, ", ");
829 strcat (ambbuf, c->name);
833 strcat (ambbuf, "..");
837 error ("Ambiguous %scommand \"%s\": %s.", local_cmdtype,
844 /* We've got something. It may still not be what the caller
845 wants (if this command *needs* a subcommand). */
846 while (**line == ' ' || **line == '\t')
849 if (c->prefixlist && **line && !c->allow_unknown)
850 undef_cmd_error (c->prefixname, *line);
852 /* Seems to be what he wants. Return it. */
859 /* Look up the contents of *LINE as a command in the command list LIST.
860 LIST is a chain of struct cmd_list_element's.
861 If it is found, return the struct cmd_list_element for that command
862 and update *LINE to point after the command name, at the first argument.
863 If not found, call error if ALLOW_UNKNOWN is zero
864 otherwise (or if error returns) return zero.
865 Call error if specified command is ambiguous,
866 unless ALLOW_UNKNOWN is negative.
867 CMDTYPE precedes the word "command" in the error message. */
869 struct cmd_list_element *
870 lookup_cmd (line, list, cmdtype, allow_unknown)
872 struct cmd_list_element *list;
877 register struct cmd_list_element *c, *found;
883 /* Skip leading whitespace. */
885 while (**line == ' ' || **line == '\t')
888 /* Clear out trailing whitespace. */
890 p = *line + strlen (*line);
891 while (p != *line && (p[-1] == ' ' || p[-1] == '\t'))
895 /* Find end of command name. */
898 while (*p == '-' || isalnum(*p))
901 /* Look up the command name.
902 If exact match, keep that.
903 Otherwise, take command abbreviated, if unique. Note that (in my
904 opinion) a null string does *not* indicate ambiguity; simply the
905 end of the argument. */
910 error ("Lack of needed %scommand", cmdtype);
914 /* Copy over to a local buffer, converting to lowercase on the way.
915 This is in case the command being parsed is a subcommand which
916 doesn't match anything, and that's ok. We want the original
917 untouched for the routine of the original command. */
919 processed_cmd = (char *) alloca (p - *line + 1);
920 for (cmd_len = 0; cmd_len < p - *line; cmd_len++)
922 char x = (*line)[cmd_len];
924 processed_cmd[cmd_len] = tolower(x);
926 processed_cmd[cmd_len] = x;
928 processed_cmd[cmd_len] = '\0';
930 /* Check all possibilities in the current command list. */
933 for (c = list; c; c = c->next)
935 if (!strncmp (processed_cmd, c->name, cmd_len))
939 if (c->name[cmd_len] == 0)
947 /* Report error for undefined command name. */
951 if (nfound > 1 && allow_unknown >= 0)
954 for (c = list; c; c = c->next)
955 if (!strncmp (processed_cmd, c->name, cmd_len))
957 if (strlen (ambbuf) + strlen (c->name) + 6 < sizeof ambbuf)
960 strcat (ambbuf, ", ");
961 strcat (ambbuf, c->name);
965 strcat (ambbuf, "..");
969 error ("Ambiguous %scommand \"%s\": %s.", cmdtype,
970 processed_cmd, ambbuf);
972 else if (!allow_unknown)
973 error ("Undefined %scommand: \"%s\".", cmdtype, processed_cmd);
977 /* Skip whitespace before the argument. */
979 while (*p == ' ' || *p == '\t') p++;
982 if (found->prefixlist && *p)
984 c = lookup_cmd (line, *found->prefixlist, found->prefixname,
985 found->allow_unknown);
994 /* Helper function for SYMBOL_COMPLETION_FUNCTION. */
996 /* Return a vector of char pointers which point to the different
997 possible completions in LIST of TEXT.
999 WORD points in the same buffer as TEXT, and completions should be
1000 returned relative to this position. For example, suppose TEXT is "foo"
1001 and we want to complete to "foobar". If WORD is "oo", return
1002 "oobar"; if WORD is "baz/foo", return "baz/foobar". */
1005 complete_on_cmdlist (list, text, word)
1006 struct cmd_list_element *list;
1010 struct cmd_list_element *ptr;
1012 int sizeof_matchlist;
1014 int textlen = strlen (text);
1016 sizeof_matchlist = 10;
1017 matchlist = (char **) xmalloc (sizeof_matchlist * sizeof (char *));
1020 for (ptr = list; ptr; ptr = ptr->next)
1021 if (!strncmp (ptr->name, text, textlen)
1022 && !ptr->abbrev_flag
1023 && (ptr->function.cfunc
1024 || ptr->prefixlist))
1026 if (matches == sizeof_matchlist)
1028 sizeof_matchlist *= 2;
1029 matchlist = (char **) xrealloc ((char *)matchlist,
1031 * sizeof (char *)));
1034 matchlist[matches] = (char *)
1035 xmalloc (strlen (word) + strlen (ptr->name) + 1);
1037 strcpy (matchlist[matches], ptr->name);
1038 else if (word > text)
1040 /* Return some portion of ptr->name. */
1041 strcpy (matchlist[matches], ptr->name + (word - text));
1045 /* Return some of text plus ptr->name. */
1046 strncpy (matchlist[matches], word, text - word);
1047 matchlist[matches][text - word] = '\0';
1048 strcat (matchlist[matches], ptr->name);
1055 free ((PTR)matchlist);
1060 matchlist = (char **) xrealloc ((char *)matchlist, ((matches + 1)
1061 * sizeof (char *)));
1062 matchlist[matches] = (char *) 0;
1068 /* Helper function for SYMBOL_COMPLETION_FUNCTION. */
1070 /* Return a vector of char pointers which point to the different
1071 possible completions in CMD of TEXT.
1073 WORD points in the same buffer as TEXT, and completions should be
1074 returned relative to this position. For example, suppose TEXT is "foo"
1075 and we want to complete to "foobar". If WORD is "oo", return
1076 "oobar"; if WORD is "baz/foo", return "baz/foobar". */
1079 complete_on_enum (enumlist, text, word)
1085 int sizeof_matchlist;
1087 int textlen = strlen (text);
1091 sizeof_matchlist = 10;
1092 matchlist = (char **) xmalloc (sizeof_matchlist * sizeof (char *));
1095 for (i = 0; (name = enumlist[i]) != NULL; i++)
1096 if (strncmp (name, text, textlen) == 0)
1098 if (matches == sizeof_matchlist)
1100 sizeof_matchlist *= 2;
1101 matchlist = (char **) xrealloc ((char *)matchlist,
1103 * sizeof (char *)));
1106 matchlist[matches] = (char *)
1107 xmalloc (strlen (word) + strlen (name) + 1);
1109 strcpy (matchlist[matches], name);
1110 else if (word > text)
1112 /* Return some portion of name. */
1113 strcpy (matchlist[matches], name + (word - text));
1117 /* Return some of text plus name. */
1118 strncpy (matchlist[matches], word, text - word);
1119 matchlist[matches][text - word] = '\0';
1120 strcat (matchlist[matches], name);
1127 free ((PTR)matchlist);
1132 matchlist = (char **) xrealloc ((char *)matchlist, ((matches + 1)
1133 * sizeof (char *)));
1134 matchlist[matches] = (char *) 0;
1141 parse_binary_operation (arg)
1149 length = strlen (arg);
1151 while (arg[length - 1] == ' ' || arg[length - 1] == '\t')
1154 if (!strncmp (arg, "on", length)
1155 || !strncmp (arg, "1", length)
1156 || !strncmp (arg, "yes", length))
1159 if (!strncmp (arg, "off", length)
1160 || !strncmp (arg, "0", length)
1161 || !strncmp (arg, "no", length))
1165 error ("\"on\" or \"off\" expected.");
1170 /* Do a "set" or "show" command. ARG is NULL if no argument, or the text
1171 of the argument, and FROM_TTY is nonzero if this command is being entered
1172 directly by the user (i.e. these are just like any other
1173 command). C is the command list element for the command. */
1175 do_setshow_command (arg, from_tty, c)
1178 struct cmd_list_element *c;
1180 if (c->type == set_cmd)
1182 switch (c->var_type)
1193 new = (char *) xmalloc (strlen (arg) + 2);
1195 while ((ch = *p++) != '\000')
1199 /* \ at end of argument is used after spaces
1200 so they won't be lost. */
1201 /* This is obsolete now that we no longer strip
1202 trailing whitespace and actually, the backslash
1203 didn't get here in my test, readline or
1204 something did something funky with a backslash
1205 right before a newline. */
1208 ch = parse_escape (&p);
1210 break; /* C loses */
1218 if (*(p - 1) != '\\')
1222 new = (char *) xrealloc (new, q - new);
1223 if (*(char **)c->var != NULL)
1224 free (*(char **)c->var);
1225 *(char **) c->var = new;
1228 case var_string_noescape:
1231 if (*(char **)c->var != NULL)
1232 free (*(char **)c->var);
1233 *(char **) c->var = savestring (arg, strlen (arg));
1237 error_no_arg ("filename to set it to.");
1238 if (*(char **)c->var != NULL)
1239 free (*(char **)c->var);
1240 *(char **)c->var = tilde_expand (arg);
1243 *(int *) c->var = parse_binary_operation (arg);
1247 error_no_arg ("integer to set it to.");
1248 *(unsigned int *) c->var = parse_and_eval_address (arg);
1249 if (*(unsigned int *) c->var == 0)
1250 *(unsigned int *) c->var = UINT_MAX;
1256 error_no_arg ("integer to set it to.");
1257 val = parse_and_eval_address (arg);
1259 *(int *) c->var = INT_MAX;
1260 else if (val >= INT_MAX)
1261 error ("integer %u out of range", val);
1263 *(int *) c->var = val;
1268 error_no_arg ("integer to set it to.");
1269 *(int *) c->var = parse_and_eval_address (arg);
1279 /* if no argument was supplied, print an informative error message */
1283 strcpy (msg, "Requires an argument. Valid arguments are ");
1284 for (i = 0; c->enums[i]; i++)
1288 strcat (msg, c->enums[i]);
1294 p = strchr (arg, ' ');
1302 for (i = 0; c->enums[i]; i++)
1303 if (strncmp (arg, c->enums[i], len) == 0)
1305 match = c->enums[i];
1310 error ("Undefined item: \"%s\".", arg);
1313 error ("Ambiguous item \"%s\".", arg);
1315 *(char **)c->var = match;
1319 error ("gdb internal error: bad var_type in do_setshow_command");
1322 else if (c->type == show_cmd)
1324 /* Print doc minus "show" at start. */
1325 print_doc_line (gdb_stdout, c->doc + 5);
1327 fputs_filtered (" is ", gdb_stdout);
1329 switch (c->var_type)
1335 fputs_filtered ("\"", gdb_stdout);
1336 if (*(unsigned char **)c->var)
1337 for (p = *(unsigned char **) c->var; *p != '\0'; p++)
1338 gdb_printchar (*p, gdb_stdout, '"');
1339 fputs_filtered ("\"", gdb_stdout);
1342 case var_string_noescape:
1345 fputs_filtered ("\"", gdb_stdout);
1346 if (*(char **)c->var)
1347 fputs_filtered (*(char **) c->var, gdb_stdout);
1348 fputs_filtered ("\"", gdb_stdout);
1351 fputs_filtered (*(int *) c->var ? "on" : "off", gdb_stdout);
1354 if (*(unsigned int *) c->var == UINT_MAX) {
1355 fputs_filtered ("unlimited", gdb_stdout);
1358 /* else fall through */
1360 fprintf_filtered (gdb_stdout, "%u", *(unsigned int *) c->var);
1363 if (*(int *) c->var == INT_MAX)
1365 fputs_filtered ("unlimited", gdb_stdout);
1368 fprintf_filtered (gdb_stdout, "%d", *(int *) c->var);
1372 error ("gdb internal error: bad var_type in do_setshow_command");
1374 fputs_filtered (".\n", gdb_stdout);
1377 error ("gdb internal error: bad cmd_type in do_setshow_command");
1378 (*c->function.sfunc) (NULL, from_tty, c);
1381 /* Show all the settings in a list of show commands. */
1384 cmd_show_list (list, from_tty, prefix)
1385 struct cmd_list_element *list;
1389 for (; list != NULL; list = list->next) {
1390 /* If we find a prefix, run its list, prefixing our output by its
1391 prefix (with "show " skipped). */
1392 if (list->prefixlist && !list->abbrev_flag)
1393 cmd_show_list (*list->prefixlist, from_tty, list->prefixname + 5);
1394 if (list->type == show_cmd)
1396 fputs_filtered (prefix, gdb_stdout);
1397 fputs_filtered (list->name, gdb_stdout);
1398 fputs_filtered (": ", gdb_stdout);
1399 do_setshow_command ((char *)NULL, from_tty, list);
1406 shell_escape (arg, from_tty)
1411 /* FIXME: what about errors (I don't know how GO32 system() handles
1414 #else /* Can fork. */
1415 int rc, status, pid;
1416 char *p, *user_shell;
1418 if ((user_shell = (char *) getenv ("SHELL")) == NULL)
1419 user_shell = "/bin/sh";
1421 /* Get the name of the shell for arg0 */
1422 if ((p = strrchr (user_shell, '/')) == NULL)
1425 p++; /* Get past '/' */
1427 if ((pid = fork()) == 0)
1430 execl (user_shell, p, 0);
1432 execl (user_shell, p, "-c", arg, 0);
1434 fprintf_unfiltered (gdb_stderr, "Cannot execute %s: %s\n", user_shell,
1435 safe_strerror (errno));
1436 gdb_flush (gdb_stderr);
1441 while ((rc = wait (&status)) != pid && rc != -1)
1444 error ("Fork failed");
1445 #endif /* Can fork. */
1449 make_command (arg, from_tty)
1459 p = xmalloc (sizeof("make ") + strlen(arg));
1460 strcpy (p, "make ");
1461 strcpy (p + sizeof("make ")-1, arg);
1464 shell_escape (p, from_tty);
1468 show_user_1 (c, stream)
1469 struct cmd_list_element *c;
1472 register struct command_line *cmdlines;
1474 cmdlines = c->user_commands;
1477 fputs_filtered ("User command ", stream);
1478 fputs_filtered (c->name, stream);
1479 fputs_filtered (":\n", stream);
1483 print_command_line (cmdlines, 4);
1484 cmdlines = cmdlines->next;
1486 fputs_filtered ("\n", stream);
1491 show_user (args, from_tty)
1495 struct cmd_list_element *c;
1496 extern struct cmd_list_element *cmdlist;
1500 c = lookup_cmd (&args, cmdlist, "", 0, 1);
1501 if (c->class != class_user)
1502 error ("Not a user command.");
1503 show_user_1 (c, gdb_stdout);
1507 for (c = cmdlist; c; c = c->next)
1509 if (c->class == class_user)
1510 show_user_1 (c, gdb_stdout);
1516 _initialize_command ()
1518 add_com ("shell", class_support, shell_escape,
1519 "Execute the rest of the line as a shell command. \n\
1520 With no arguments, run an inferior shell.");
1521 add_com ("make", class_support, make_command,
1522 "Run the ``make'' program using the rest of the line as arguments.");
1523 add_cmd ("user", no_class, show_user,
1524 "Show definitions of user defined commands.\n\
1525 Argument is the name of the user defined command.\n\
1526 With no argument, show definitions of all user defined commands.", &showlist);