* Contribute CGEN simulator build support code.
[binutils-gdb.git] / gdb / command.c
blob56fb85ea463cfeddcf4c7e2b86c8350b0243772a
1 /* Handle lists of commands, their decoding and documentation, for GDB.
2 Copyright 1986, 1989, 1990, 1991, 1998, 2000 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,
17 Boston, MA 02111-1307, USA. */
19 #include "defs.h"
20 #include "gdbcmd.h"
21 #include "symtab.h"
22 #include "value.h"
23 #include <ctype.h>
24 #include "gdb_string.h"
25 #ifdef UI_OUT
26 #include "ui-out.h"
27 #endif
29 #include "gdb_wait.h"
30 #include "gnu-regex.h"
31 /* FIXME: this should be auto-configured! */
32 #ifdef __MSDOS__
33 # define CANT_FORK
34 #endif
36 /* Prototypes for local functions */
38 static void undef_cmd_error (char *, char *);
40 static void show_user (char *, int);
42 static void show_user_1 (struct cmd_list_element *, struct ui_file *);
44 static void make_command (char *, int);
46 static void shell_escape (char *, int);
48 static int parse_binary_operation (char *);
50 static void print_doc_line (struct ui_file *, char *);
52 static struct cmd_list_element *find_cmd (char *command,
53 int len,
54 struct cmd_list_element *clist,
55 int ignore_help_classes,
56 int *nfound);
57 static void apropos_cmd_helper (struct ui_file *, struct cmd_list_element *,
58 struct re_pattern_buffer *, char *);
60 static void help_all (struct ui_file *stream);
62 void apropos_command (char *, int);
64 void _initialize_command (void);
66 /* Add element named NAME.
67 CLASS is the top level category into which commands are broken down
68 for "help" purposes.
69 FUN should be the function to execute the command;
70 it will get a character string as argument, with leading
71 and trailing blanks already eliminated.
73 DOC is a documentation string for the command.
74 Its first line should be a complete sentence.
75 It should start with ? for a command that is an abbreviation
76 or with * for a command that most users don't need to know about.
78 Add this command to command list *LIST.
80 Returns a pointer to the added command (not necessarily the head
81 of *LIST). */
83 struct cmd_list_element *
84 add_cmd (char *name, enum command_class class, void (*fun) (char *, int),
85 char *doc, struct cmd_list_element **list)
87 register struct cmd_list_element *c
88 = (struct cmd_list_element *) xmalloc (sizeof (struct cmd_list_element));
89 struct cmd_list_element *p;
91 delete_cmd (name, list);
93 if (*list == NULL || STRCMP ((*list)->name, name) >= 0)
95 c->next = *list;
96 *list = c;
98 else
100 p = *list;
101 while (p->next && STRCMP (p->next->name, name) <= 0)
103 p = p->next;
105 c->next = p->next;
106 p->next = c;
109 c->name = name;
110 c->class = class;
111 c->function.cfunc = fun;
112 c->doc = doc;
113 c->flags = 0;
114 c->replacement = NULL;
115 c->hook = NULL;
116 c->prefixlist = NULL;
117 c->prefixname = NULL;
118 c->allow_unknown = 0;
119 c->abbrev_flag = 0;
120 c->completer = make_symbol_completion_list;
121 c->type = not_set_cmd;
122 c->var = NULL;
123 c->var_type = var_boolean;
124 c->enums = NULL;
125 c->user_commands = NULL;
126 c->hookee = NULL;
127 c->cmd_pointer = NULL;
129 return c;
133 /* Deprecates a command CMD.
134 REPLACEMENT is the name of the command which should be used in place
135 of this command, or NULL if no such command exists.
137 This function does not check to see if command REPLACEMENT exists
138 since gdb may not have gotten around to adding REPLACEMENT when this
139 function is called.
141 Returns a pointer to the deprecated command. */
143 struct cmd_list_element *
144 deprecate_cmd (struct cmd_list_element *cmd, char *replacement)
146 cmd->flags |= (CMD_DEPRECATED | DEPRECATED_WARN_USER);
148 if (replacement != NULL)
149 cmd->replacement = replacement;
150 else
151 cmd->replacement = NULL;
153 return cmd;
157 /* Same as above, except that the abbrev_flag is set. */
159 #if 0 /* Currently unused */
161 struct cmd_list_element *
162 add_abbrev_cmd (char *name, enum command_class class, void (*fun) (char *, int),
163 char *doc, struct cmd_list_element **list)
165 register struct cmd_list_element *c
166 = add_cmd (name, class, fun, doc, list);
168 c->abbrev_flag = 1;
169 return c;
172 #endif
174 struct cmd_list_element *
175 add_alias_cmd (char *name, char *oldname, enum command_class class,
176 int abbrev_flag, struct cmd_list_element **list)
178 /* Must do this since lookup_cmd tries to side-effect its first arg */
179 char *copied_name;
180 register struct cmd_list_element *old;
181 register struct cmd_list_element *c;
182 copied_name = (char *) alloca (strlen (oldname) + 1);
183 strcpy (copied_name, oldname);
184 old = lookup_cmd (&copied_name, *list, "", 1, 1);
186 if (old == 0)
188 delete_cmd (name, list);
189 return 0;
192 c = add_cmd (name, class, old->function.cfunc, old->doc, list);
193 c->prefixlist = old->prefixlist;
194 c->prefixname = old->prefixname;
195 c->allow_unknown = old->allow_unknown;
196 c->abbrev_flag = abbrev_flag;
197 c->cmd_pointer = old;
198 return c;
201 /* Like add_cmd but adds an element for a command prefix:
202 a name that should be followed by a subcommand to be looked up
203 in another command list. PREFIXLIST should be the address
204 of the variable containing that list. */
206 struct cmd_list_element *
207 add_prefix_cmd (char *name, enum command_class class, void (*fun) (char *, int),
208 char *doc, struct cmd_list_element **prefixlist,
209 char *prefixname, int allow_unknown,
210 struct cmd_list_element **list)
212 register struct cmd_list_element *c = add_cmd (name, class, fun, doc, list);
213 c->prefixlist = prefixlist;
214 c->prefixname = prefixname;
215 c->allow_unknown = allow_unknown;
216 return c;
219 /* Like add_prefix_cmd but sets the abbrev_flag on the new command. */
221 struct cmd_list_element *
222 add_abbrev_prefix_cmd (char *name, enum command_class class,
223 void (*fun) (char *, int), char *doc,
224 struct cmd_list_element **prefixlist, char *prefixname,
225 int allow_unknown, struct cmd_list_element **list)
227 register struct cmd_list_element *c = add_cmd (name, class, fun, doc, list);
228 c->prefixlist = prefixlist;
229 c->prefixname = prefixname;
230 c->allow_unknown = allow_unknown;
231 c->abbrev_flag = 1;
232 return c;
235 /* This is an empty "cfunc". */
236 void
237 not_just_help_class_command (char *args, int from_tty)
241 /* This is an empty "sfunc". */
242 static void empty_sfunc (char *, int, struct cmd_list_element *);
244 static void
245 empty_sfunc (char *args, int from_tty, struct cmd_list_element *c)
249 /* Add element named NAME to command list LIST (the list for set
250 or some sublist thereof).
251 CLASS is as in add_cmd.
252 VAR_TYPE is the kind of thing we are setting.
253 VAR is address of the variable being controlled by this command.
254 DOC is the documentation string. */
256 struct cmd_list_element *
257 add_set_cmd (char *name,
258 enum command_class class,
259 var_types var_type,
260 void *var,
261 char *doc,
262 struct cmd_list_element **list)
264 struct cmd_list_element *c
265 = add_cmd (name, class, NO_FUNCTION, doc, list);
267 c->type = set_cmd;
268 c->var_type = var_type;
269 c->var = var;
270 /* This needs to be something besides NO_FUNCTION so that this isn't
271 treated as a help class. */
272 c->function.sfunc = empty_sfunc;
273 return c;
276 /* Add element named NAME to command list LIST (the list for set
277 or some sublist thereof).
278 CLASS is as in add_cmd.
279 ENUMLIST is a list of strings which may follow NAME.
280 VAR is address of the variable which will contain the matching string
281 (from ENUMLIST).
282 DOC is the documentation string. */
284 struct cmd_list_element *
285 add_set_enum_cmd (char *name,
286 enum command_class class,
287 const char *enumlist[],
288 const char **var,
289 char *doc,
290 struct cmd_list_element **list)
292 struct cmd_list_element *c
293 = add_set_cmd (name, class, var_enum, var, doc, list);
294 c->enums = enumlist;
296 return c;
299 /* Add element named NAME to command list LIST (the list for set
300 or some sublist thereof).
301 CLASS is as in add_cmd.
302 VAR is address of the variable which will contain the value.
303 DOC is the documentation string. */
304 struct cmd_list_element *
305 add_set_auto_boolean_cmd (char *name,
306 enum command_class class,
307 enum cmd_auto_boolean *var,
308 char *doc,
309 struct cmd_list_element **list)
311 static const char *auto_boolean_enums[] = { "on", "off", "auto", NULL };
312 struct cmd_list_element *c;
313 c = add_set_cmd (name, class, var_auto_boolean, var, doc, list);
314 c->enums = auto_boolean_enums;
315 return c;
318 /* Where SETCMD has already been added, add the corresponding show
319 command to LIST and return a pointer to the added command (not
320 necessarily the head of LIST). */
321 struct cmd_list_element *
322 add_show_from_set (struct cmd_list_element *setcmd,
323 struct cmd_list_element **list)
325 struct cmd_list_element *showcmd =
326 (struct cmd_list_element *) xmalloc (sizeof (struct cmd_list_element));
327 struct cmd_list_element *p;
329 memcpy (showcmd, setcmd, sizeof (struct cmd_list_element));
330 delete_cmd (showcmd->name, list);
331 showcmd->type = show_cmd;
333 /* Replace "set " at start of docstring with "show ". */
334 if (setcmd->doc[0] == 'S' && setcmd->doc[1] == 'e'
335 && setcmd->doc[2] == 't' && setcmd->doc[3] == ' ')
336 showcmd->doc = concat ("Show ", setcmd->doc + 4, NULL);
337 else
338 fprintf_unfiltered (gdb_stderr, "GDB internal error: Bad docstring for set command\n");
340 if (*list == NULL || STRCMP ((*list)->name, showcmd->name) >= 0)
342 showcmd->next = *list;
343 *list = showcmd;
345 else
347 p = *list;
348 while (p->next && STRCMP (p->next->name, showcmd->name) <= 0)
350 p = p->next;
352 showcmd->next = p->next;
353 p->next = showcmd;
356 return showcmd;
359 /* Remove the command named NAME from the command list. */
361 void
362 delete_cmd (char *name, struct cmd_list_element **list)
364 register struct cmd_list_element *c;
365 struct cmd_list_element *p;
367 while (*list && STREQ ((*list)->name, name))
369 if ((*list)->hookee)
370 (*list)->hookee->hook = 0; /* Hook slips out of its mouth */
371 p = (*list)->next;
372 free ((PTR) * list);
373 *list = p;
376 if (*list)
377 for (c = *list; c->next;)
379 if (STREQ (c->next->name, name))
381 if (c->next->hookee)
382 c->next->hookee->hook = 0; /* hooked cmd gets away. */
383 p = c->next->next;
384 free ((PTR) c->next);
385 c->next = p;
387 else
388 c = c->next;
391 /* Recursively walk the commandlist structures, and print out the
392 documentation of commands that match our regex in either their
393 name, or their documentation.
395 static void
396 apropos_cmd_helper (struct ui_file *stream, struct cmd_list_element *commandlist,
397 struct re_pattern_buffer *regex, char *prefix)
399 register struct cmd_list_element *c;
400 int returnvalue=1; /*Needed to avoid double printing*/
401 /* Walk through the commands */
402 for (c=commandlist;c;c=c->next)
404 if (c->name != NULL)
406 /* Try to match against the name*/
407 returnvalue=re_search(regex,c->name,strlen(c->name),0,strlen(c->name),NULL);
408 if (returnvalue >= 0)
410 /* Stolen from help_cmd_list. We don't directly use
411 * help_cmd_list because it doesn't let us print out
412 * single commands
414 fprintf_filtered (stream, "%s%s -- ", prefix, c->name);
415 print_doc_line (stream, c->doc);
416 fputs_filtered ("\n", stream);
417 returnvalue=0; /*Set this so we don't print it again.*/
420 if (c->doc != NULL && returnvalue != 0)
422 /* Try to match against documentation */
423 if (re_search(regex,c->doc,strlen(c->doc),0,strlen(c->doc),NULL) >=0)
425 /* Stolen from help_cmd_list. We don't directly use
426 * help_cmd_list because it doesn't let us print out
427 * single commands
429 fprintf_filtered (stream, "%s%s -- ", prefix, c->name);
430 print_doc_line (stream, c->doc);
431 fputs_filtered ("\n", stream);
434 /* Check if this command has subcommands */
435 if (c->prefixlist != NULL)
437 /* Recursively call ourselves on the subcommand list,
438 passing the right prefix in.
440 apropos_cmd_helper(stream,*c->prefixlist,regex,c->prefixname);
444 /* Search through names of commands and documentations for a certain
445 regular expression.
447 void
448 apropos_command (char *searchstr, int from_tty)
450 extern struct cmd_list_element *cmdlist; /*This is the main command list*/
451 regex_t pattern;
452 char *pattern_fastmap;
453 char errorbuffer[512];
454 pattern_fastmap=calloc(256,sizeof(char));
455 if (searchstr == NULL)
456 error("REGEXP string is empty");
458 if (regcomp(&pattern,searchstr,REG_ICASE) == 0)
460 pattern.fastmap=pattern_fastmap;
461 re_compile_fastmap(&pattern);
462 apropos_cmd_helper(gdb_stdout,cmdlist,&pattern,"");
464 else
466 regerror(regcomp(&pattern,searchstr,REG_ICASE),NULL,errorbuffer,512);
467 error("Error in regular expression:%s",errorbuffer);
469 free(pattern_fastmap);
473 /* This command really has to deal with two things:
474 * 1) I want documentation on *this string* (usually called by
475 * "help commandname").
476 * 2) I want documentation on *this list* (usually called by
477 * giving a command that requires subcommands. Also called by saying
478 * just "help".)
480 * I am going to split this into two seperate comamnds, help_cmd and
481 * help_list.
484 void
485 help_cmd (char *command, struct ui_file *stream)
487 struct cmd_list_element *c;
488 extern struct cmd_list_element *cmdlist;
490 if (!command)
492 help_list (cmdlist, "", all_classes, stream);
493 return;
496 if (strcmp (command, "all") == 0)
498 help_all (stream);
499 return;
502 c = lookup_cmd (&command, cmdlist, "", 0, 0);
504 if (c == 0)
505 return;
507 /* There are three cases here.
508 If c->prefixlist is nonzero, we have a prefix command.
509 Print its documentation, then list its subcommands.
511 If c->function is nonzero, we really have a command.
512 Print its documentation and return.
514 If c->function is zero, we have a class name.
515 Print its documentation (as if it were a command)
516 and then set class to the number of this class
517 so that the commands in the class will be listed. */
519 fputs_filtered (c->doc, stream);
520 fputs_filtered ("\n", stream);
522 if (c->prefixlist == 0 && c->function.cfunc != NULL)
523 return;
524 fprintf_filtered (stream, "\n");
526 /* If this is a prefix command, print it's subcommands */
527 if (c->prefixlist)
528 help_list (*c->prefixlist, c->prefixname, all_commands, stream);
530 /* If this is a class name, print all of the commands in the class */
531 if (c->function.cfunc == NULL)
532 help_list (cmdlist, "", c->class, stream);
534 if (c->hook)
535 fprintf_filtered (stream, "\nThis command has a hook defined: %s\n",
536 c->hook->name);
540 * Get a specific kind of help on a command list.
542 * LIST is the list.
543 * CMDTYPE is the prefix to use in the title string.
544 * CLASS is the class with which to list the nodes of this list (see
545 * documentation for help_cmd_list below), As usual, ALL_COMMANDS for
546 * everything, ALL_CLASSES for just classes, and non-negative for only things
547 * in a specific class.
548 * and STREAM is the output stream on which to print things.
549 * If you call this routine with a class >= 0, it recurses.
551 void
552 help_list (struct cmd_list_element *list, char *cmdtype,
553 enum command_class class, struct ui_file *stream)
555 int len;
556 char *cmdtype1, *cmdtype2;
558 /* If CMDTYPE is "foo ", CMDTYPE1 gets " foo" and CMDTYPE2 gets "foo sub" */
559 len = strlen (cmdtype);
560 cmdtype1 = (char *) alloca (len + 1);
561 cmdtype1[0] = 0;
562 cmdtype2 = (char *) alloca (len + 4);
563 cmdtype2[0] = 0;
564 if (len)
566 cmdtype1[0] = ' ';
567 strncpy (cmdtype1 + 1, cmdtype, len - 1);
568 cmdtype1[len] = 0;
569 strncpy (cmdtype2, cmdtype, len - 1);
570 strcpy (cmdtype2 + len - 1, " sub");
573 if (class == all_classes)
574 fprintf_filtered (stream, "List of classes of %scommands:\n\n", cmdtype2);
575 else
576 fprintf_filtered (stream, "List of %scommands:\n\n", cmdtype2);
578 help_cmd_list (list, class, cmdtype, (int) class >= 0, stream);
580 if (class == all_classes)
581 fprintf_filtered (stream, "\n\
582 Type \"help%s\" followed by a class name for a list of commands in that class.",
583 cmdtype1);
585 fprintf_filtered (stream, "\n\
586 Type \"help%s\" followed by %scommand name for full documentation.\n\
587 Command name abbreviations are allowed if unambiguous.\n",
588 cmdtype1, cmdtype2);
591 static void
592 help_all (struct ui_file *stream)
594 struct cmd_list_element *c;
595 extern struct cmd_list_element *cmdlist;
597 for (c = cmdlist; c; c = c->next)
599 if (c->abbrev_flag)
600 continue;
601 /* If this is a prefix command, print it's subcommands */
602 if (c->prefixlist)
603 help_cmd_list (*c->prefixlist, all_commands, c->prefixname, 0, stream);
605 /* If this is a class name, print all of the commands in the class */
606 else if (c->function.cfunc == NULL)
607 help_cmd_list (cmdlist, c->class, "", 0, stream);
611 /* Print only the first line of STR on STREAM. */
612 static void
613 print_doc_line (struct ui_file *stream, char *str)
615 static char *line_buffer = 0;
616 static int line_size;
617 register char *p;
619 if (!line_buffer)
621 line_size = 80;
622 line_buffer = (char *) xmalloc (line_size);
625 p = str;
626 while (*p && *p != '\n' && *p != '.' && *p != ',')
627 p++;
628 if (p - str > line_size - 1)
630 line_size = p - str + 1;
631 free ((PTR) line_buffer);
632 line_buffer = (char *) xmalloc (line_size);
634 strncpy (line_buffer, str, p - str);
635 line_buffer[p - str] = '\0';
636 if (islower (line_buffer[0]))
637 line_buffer[0] = toupper (line_buffer[0]);
638 #ifdef UI_OUT
639 ui_out_text (uiout, line_buffer);
640 #else
641 fputs_filtered (line_buffer, stream);
642 #endif
646 * Implement a help command on command list LIST.
647 * RECURSE should be non-zero if this should be done recursively on
648 * all sublists of LIST.
649 * PREFIX is the prefix to print before each command name.
650 * STREAM is the stream upon which the output should be written.
651 * CLASS should be:
652 * A non-negative class number to list only commands in that
653 * class.
654 * ALL_COMMANDS to list all commands in list.
655 * ALL_CLASSES to list all classes in list.
657 * Note that RECURSE will be active on *all* sublists, not just the
658 * ones selected by the criteria above (ie. the selection mechanism
659 * is at the low level, not the high-level).
661 void
662 help_cmd_list (struct cmd_list_element *list, enum command_class class,
663 char *prefix, int recurse, struct ui_file *stream)
665 register struct cmd_list_element *c;
667 for (c = list; c; c = c->next)
669 if (c->abbrev_flag == 0 &&
670 (class == all_commands
671 || (class == all_classes && c->function.cfunc == NULL)
672 || (class == c->class && c->function.cfunc != NULL)))
674 fprintf_filtered (stream, "%s%s -- ", prefix, c->name);
675 print_doc_line (stream, c->doc);
676 fputs_filtered ("\n", stream);
678 if (recurse
679 && c->prefixlist != 0
680 && c->abbrev_flag == 0)
681 help_cmd_list (*c->prefixlist, class, c->prefixname, 1, stream);
686 /* Search the input clist for 'command'. Return the command if
687 found (or NULL if not), and return the number of commands
688 found in nfound */
690 static struct cmd_list_element *
691 find_cmd (char *command, int len, struct cmd_list_element *clist,
692 int ignore_help_classes, int *nfound)
694 struct cmd_list_element *found, *c;
696 found = (struct cmd_list_element *) NULL;
697 *nfound = 0;
698 for (c = clist; c; c = c->next)
699 if (!strncmp (command, c->name, len)
700 && (!ignore_help_classes || c->function.cfunc))
702 found = c;
703 (*nfound)++;
704 if (c->name[len] == '\0')
706 *nfound = 1;
707 break;
710 return found;
713 /* This routine takes a line of TEXT and a CLIST in which to start the
714 lookup. When it returns it will have incremented the text pointer past
715 the section of text it matched, set *RESULT_LIST to point to the list in
716 which the last word was matched, and will return a pointer to the cmd
717 list element which the text matches. It will return NULL if no match at
718 all was possible. It will return -1 (cast appropriately, ick) if ambigous
719 matches are possible; in this case *RESULT_LIST will be set to point to
720 the list in which there are ambiguous choices (and *TEXT will be set to
721 the ambiguous text string).
723 If the located command was an abbreviation, this routine returns the base
724 command of the abbreviation.
726 It does no error reporting whatsoever; control will always return
727 to the superior routine.
729 In the case of an ambiguous return (-1), *RESULT_LIST will be set to point
730 at the prefix_command (ie. the best match) *or* (special case) will be NULL
731 if no prefix command was ever found. For example, in the case of "info a",
732 "info" matches without ambiguity, but "a" could be "args" or "address", so
733 *RESULT_LIST is set to the cmd_list_element for "info". So in this case
734 RESULT_LIST should not be interpeted as a pointer to the beginning of a
735 list; it simply points to a specific command. In the case of an ambiguous
736 return *TEXT is advanced past the last non-ambiguous prefix (e.g.
737 "info t" can be "info types" or "info target"; upon return *TEXT has been
738 advanced past "info ").
740 If RESULT_LIST is NULL, don't set *RESULT_LIST (but don't otherwise
741 affect the operation).
743 This routine does *not* modify the text pointed to by TEXT.
745 If IGNORE_HELP_CLASSES is nonzero, ignore any command list elements which
746 are actually help classes rather than commands (i.e. the function field of
747 the struct cmd_list_element is NULL). */
749 struct cmd_list_element *
750 lookup_cmd_1 (char **text, struct cmd_list_element *clist,
751 struct cmd_list_element **result_list, int ignore_help_classes)
753 char *p, *command;
754 int len, tmp, nfound;
755 struct cmd_list_element *found, *c;
756 char *line = *text;
758 while (**text == ' ' || **text == '\t')
759 (*text)++;
761 /* Treating underscores as part of command words is important
762 so that "set args_foo()" doesn't get interpreted as
763 "set args _foo()". */
764 for (p = *text;
765 *p && (isalnum (*p) || *p == '-' || *p == '_' ||
766 (tui_version &&
767 (*p == '+' || *p == '<' || *p == '>' || *p == '$')) ||
768 (xdb_commands && (*p == '!' || *p == '/' || *p == '?')));
769 p++)
772 /* If nothing but whitespace, return 0. */
773 if (p == *text)
774 return 0;
776 len = p - *text;
778 /* *text and p now bracket the first command word to lookup (and
779 it's length is len). We copy this into a local temporary */
782 command = (char *) alloca (len + 1);
783 for (tmp = 0; tmp < len; tmp++)
785 char x = (*text)[tmp];
786 command[tmp] = x;
788 command[len] = '\0';
790 /* Look it up. */
791 found = 0;
792 nfound = 0;
793 found = find_cmd (command, len, clist, ignore_help_classes, &nfound);
796 ** We didn't find the command in the entered case, so lower case it
797 ** and search again.
799 if (!found || nfound == 0)
801 for (tmp = 0; tmp < len; tmp++)
803 char x = command[tmp];
804 command[tmp] = isupper (x) ? tolower (x) : x;
806 found = find_cmd (command, len, clist, ignore_help_classes, &nfound);
809 /* If nothing matches, we have a simple failure. */
810 if (nfound == 0)
811 return 0;
813 if (nfound > 1)
815 if (result_list != NULL)
816 /* Will be modified in calling routine
817 if we know what the prefix command is. */
818 *result_list = 0;
819 return (struct cmd_list_element *) -1; /* Ambiguous. */
822 /* We've matched something on this list. Move text pointer forward. */
824 *text = p;
826 if (found->cmd_pointer)
828 /* We drop the alias (abbreviation) in favor of the command it is
829 pointing to. If the alias is deprecated, though, we need to
830 warn the user about it before we drop it. Note that while we
831 are warning about the alias, we may also warn about the command
832 itself and we will adjust the appropriate DEPRECATED_WARN_USER
833 flags */
835 if (found->flags & DEPRECATED_WARN_USER)
836 deprecated_cmd_warning (&line);
837 found = found->cmd_pointer;
839 /* If we found a prefix command, keep looking. */
841 if (found->prefixlist)
843 c = lookup_cmd_1 (text, *found->prefixlist, result_list,
844 ignore_help_classes);
845 if (!c)
847 /* Didn't find anything; this is as far as we got. */
848 if (result_list != NULL)
849 *result_list = clist;
850 return found;
852 else if (c == (struct cmd_list_element *) -1)
854 /* We've gotten this far properly, but the next step
855 is ambiguous. We need to set the result list to the best
856 we've found (if an inferior hasn't already set it). */
857 if (result_list != NULL)
858 if (!*result_list)
859 /* This used to say *result_list = *found->prefixlist
860 If that was correct, need to modify the documentation
861 at the top of this function to clarify what is supposed
862 to be going on. */
863 *result_list = found;
864 return c;
866 else
868 /* We matched! */
869 return c;
872 else
874 if (result_list != NULL)
875 *result_list = clist;
876 return found;
880 /* All this hair to move the space to the front of cmdtype */
882 static void
883 undef_cmd_error (char *cmdtype, char *q)
885 error ("Undefined %scommand: \"%s\". Try \"help%s%.*s\".",
886 cmdtype,
888 *cmdtype ? " " : "",
889 strlen (cmdtype) - 1,
890 cmdtype);
893 /* Look up the contents of *LINE as a command in the command list LIST.
894 LIST is a chain of struct cmd_list_element's.
895 If it is found, return the struct cmd_list_element for that command
896 and update *LINE to point after the command name, at the first argument.
897 If not found, call error if ALLOW_UNKNOWN is zero
898 otherwise (or if error returns) return zero.
899 Call error if specified command is ambiguous,
900 unless ALLOW_UNKNOWN is negative.
901 CMDTYPE precedes the word "command" in the error message.
903 If INGNORE_HELP_CLASSES is nonzero, ignore any command list
904 elements which are actually help classes rather than commands (i.e.
905 the function field of the struct cmd_list_element is 0). */
907 struct cmd_list_element *
908 lookup_cmd (char **line, struct cmd_list_element *list, char *cmdtype,
909 int allow_unknown, int ignore_help_classes)
911 struct cmd_list_element *last_list = 0;
912 struct cmd_list_element *c =
913 lookup_cmd_1 (line, list, &last_list, ignore_help_classes);
914 #if 0
915 /* This is wrong for complete_command. */
916 char *ptr = (*line) + strlen (*line) - 1;
918 /* Clear off trailing whitespace. */
919 while (ptr >= *line && (*ptr == ' ' || *ptr == '\t'))
920 ptr--;
921 *(ptr + 1) = '\0';
922 #endif
924 if (!c)
926 if (!allow_unknown)
928 if (!*line)
929 error ("Lack of needed %scommand", cmdtype);
930 else
932 char *p = *line, *q;
934 while (isalnum (*p) || *p == '-')
935 p++;
937 q = (char *) alloca (p - *line + 1);
938 strncpy (q, *line, p - *line);
939 q[p - *line] = '\0';
940 undef_cmd_error (cmdtype, q);
943 else
944 return 0;
946 else if (c == (struct cmd_list_element *) -1)
948 /* Ambigous. Local values should be off prefixlist or called
949 values. */
950 int local_allow_unknown = (last_list ? last_list->allow_unknown :
951 allow_unknown);
952 char *local_cmdtype = last_list ? last_list->prefixname : cmdtype;
953 struct cmd_list_element *local_list =
954 (last_list ? *(last_list->prefixlist) : list);
956 if (local_allow_unknown < 0)
958 if (last_list)
959 return last_list; /* Found something. */
960 else
961 return 0; /* Found nothing. */
963 else
965 /* Report as error. */
966 int amb_len;
967 char ambbuf[100];
969 for (amb_len = 0;
970 ((*line)[amb_len] && (*line)[amb_len] != ' '
971 && (*line)[amb_len] != '\t');
972 amb_len++)
975 ambbuf[0] = 0;
976 for (c = local_list; c; c = c->next)
977 if (!strncmp (*line, c->name, amb_len))
979 if (strlen (ambbuf) + strlen (c->name) + 6 < (int) sizeof ambbuf)
981 if (strlen (ambbuf))
982 strcat (ambbuf, ", ");
983 strcat (ambbuf, c->name);
985 else
987 strcat (ambbuf, "..");
988 break;
991 error ("Ambiguous %scommand \"%s\": %s.", local_cmdtype,
992 *line, ambbuf);
993 return 0; /* lint */
996 else
998 /* We've got something. It may still not be what the caller
999 wants (if this command *needs* a subcommand). */
1000 while (**line == ' ' || **line == '\t')
1001 (*line)++;
1003 if (c->prefixlist && **line && !c->allow_unknown)
1004 undef_cmd_error (c->prefixname, *line);
1006 /* Seems to be what he wants. Return it. */
1007 return c;
1009 return 0;
1012 /* We are here presumably because an alias or command in *TEXT is
1013 deprecated and a warning message should be generated. This function
1014 decodes *TEXT and potentially generates a warning message as outlined
1015 below.
1017 Example for 'set endian big' which has a fictitious alias 'seb'.
1019 If alias wasn't used in *TEXT, and the command is deprecated:
1020 "warning: 'set endian big' is deprecated."
1022 If alias was used, and only the alias is deprecated:
1023 "warning: 'seb' an alias for the command 'set endian big' is deprecated."
1025 If alias was used and command is deprecated (regardless of whether the
1026 alias itself is deprecated:
1028 "warning: 'set endian big' (seb) is deprecated."
1030 After the message has been sent, clear the appropriate flags in the
1031 command and/or the alias so the user is no longer bothered.
1034 void
1035 deprecated_cmd_warning (char **text)
1037 struct cmd_list_element *alias = NULL;
1038 struct cmd_list_element *prefix_cmd = NULL;
1039 struct cmd_list_element *cmd = NULL;
1040 struct cmd_list_element *c;
1041 char *type;
1043 if (!lookup_cmd_composition (*text, &alias, &prefix_cmd, &cmd))
1044 /* return if text doesn't evaluate to a command */
1045 return;
1047 if (!((alias ? (alias->flags & DEPRECATED_WARN_USER) : 0)
1048 || (cmd->flags & DEPRECATED_WARN_USER) ) )
1049 /* return if nothing is deprecated */
1050 return;
1052 printf_filtered ("Warning:");
1054 if (alias && !(cmd->flags & CMD_DEPRECATED))
1055 printf_filtered (" '%s', an alias for the", alias->name);
1057 printf_filtered (" command '");
1059 if (prefix_cmd)
1060 printf_filtered ("%s", prefix_cmd->prefixname);
1062 printf_filtered ("%s", cmd->name);
1064 if (alias && (cmd->flags & CMD_DEPRECATED))
1065 printf_filtered ("' (%s) is deprecated.\n", alias->name);
1066 else
1067 printf_filtered ("' is deprecated.\n");
1070 /* if it is only the alias that is deprecated, we want to indicate the
1071 new alias, otherwise we'll indicate the new command */
1073 if (alias && !(cmd->flags & CMD_DEPRECATED))
1075 if (alias->replacement)
1076 printf_filtered ("Use '%s'.\n\n", alias->replacement);
1077 else
1078 printf_filtered ("No alternative known.\n\n");
1080 else
1082 if (cmd->replacement)
1083 printf_filtered ("Use '%s'.\n\n", cmd->replacement);
1084 else
1085 printf_filtered ("No alternative known.\n\n");
1088 /* We've warned you, now we'll keep quiet */
1089 if (alias)
1090 alias->flags &= ~DEPRECATED_WARN_USER;
1092 cmd->flags &= ~DEPRECATED_WARN_USER;
1097 /* Look up the contents of LINE as a command in the command list 'cmdlist'.
1098 Return 1 on success, 0 on failure.
1100 If LINE refers to an alias, *alias will point to that alias.
1102 If LINE is a postfix command (i.e. one that is preceeded by a prefix
1103 command) set *prefix_cmd.
1105 Set *cmd to point to the command LINE indicates.
1107 If any of *alias, *prefix_cmd, or *cmd cannot be determined or do not
1108 exist, they are NULL when we return.
1112 lookup_cmd_composition (char *text,
1113 struct cmd_list_element **alias,
1114 struct cmd_list_element **prefix_cmd,
1115 struct cmd_list_element **cmd)
1117 char *p, *command;
1118 int len, tmp, nfound;
1119 struct cmd_list_element *cur_list;
1120 struct cmd_list_element *prev_cmd;
1121 *alias = NULL;
1122 *prefix_cmd = NULL;
1123 *cmd = NULL;
1125 cur_list = cmdlist;
1127 while (1)
1129 /* Go through as many command lists as we need to
1130 to find the command TEXT refers to. */
1132 prev_cmd = *cmd;
1134 while (*text == ' ' || *text == '\t')
1135 (text)++;
1137 /* Treating underscores as part of command words is important
1138 so that "set args_foo()" doesn't get interpreted as
1139 "set args _foo()". */
1140 for (p = text;
1141 *p && (isalnum (*p) || *p == '-' || *p == '_' ||
1142 (tui_version &&
1143 (*p == '+' || *p == '<' || *p == '>' || *p == '$')) ||
1144 (xdb_commands && (*p == '!' || *p == '/' || *p == '?')));
1145 p++)
1148 /* If nothing but whitespace, return. */
1149 if (p == text)
1150 return 0;
1152 len = p - text;
1154 /* text and p now bracket the first command word to lookup (and
1155 it's length is len). We copy this into a local temporary */
1157 command = (char *) alloca (len + 1);
1158 for (tmp = 0; tmp < len; tmp++)
1160 char x = text[tmp];
1161 command[tmp] = x;
1163 command[len] = '\0';
1165 /* Look it up. */
1166 *cmd = 0;
1167 nfound = 0;
1168 *cmd = find_cmd (command, len, cur_list, 1, &nfound);
1170 /* We didn't find the command in the entered case, so lower case it
1171 and search again.
1173 if (!*cmd || nfound == 0)
1175 for (tmp = 0; tmp < len; tmp++)
1177 char x = command[tmp];
1178 command[tmp] = isupper (x) ? tolower (x) : x;
1180 *cmd = find_cmd (command, len, cur_list, 1, &nfound);
1183 if (*cmd == (struct cmd_list_element *) -1)
1185 return 0; /* ambiguous */
1188 if (*cmd == NULL)
1189 return 0; /* nothing found */
1190 else
1192 if ((*cmd)->cmd_pointer)
1194 /* cmd was actually an alias, we note that an alias was used
1195 (by assigning *alais) and we set *cmd.
1197 *alias = *cmd;
1198 *cmd = (*cmd)->cmd_pointer;
1200 *prefix_cmd = prev_cmd;
1202 if ((*cmd)->prefixlist)
1203 cur_list = *(*cmd)->prefixlist;
1204 else
1205 return 1;
1207 text = p;
1214 #if 0
1215 /* Look up the contents of *LINE as a command in the command list LIST.
1216 LIST is a chain of struct cmd_list_element's.
1217 If it is found, return the struct cmd_list_element for that command
1218 and update *LINE to point after the command name, at the first argument.
1219 If not found, call error if ALLOW_UNKNOWN is zero
1220 otherwise (or if error returns) return zero.
1221 Call error if specified command is ambiguous,
1222 unless ALLOW_UNKNOWN is negative.
1223 CMDTYPE precedes the word "command" in the error message. */
1225 struct cmd_list_element *
1226 lookup_cmd (char **line, struct cmd_list_element *list, char *cmdtype,
1227 int allow_unknown)
1229 register char *p;
1230 register struct cmd_list_element *c, *found;
1231 int nfound;
1232 char ambbuf[100];
1233 char *processed_cmd;
1234 int i, cmd_len;
1236 /* Skip leading whitespace. */
1238 while (**line == ' ' || **line == '\t')
1239 (*line)++;
1241 /* Clear out trailing whitespace. */
1243 p = *line + strlen (*line);
1244 while (p != *line && (p[-1] == ' ' || p[-1] == '\t'))
1245 p--;
1246 *p = 0;
1248 /* Find end of command name. */
1250 p = *line;
1251 while (*p == '-' || isalnum (*p))
1252 p++;
1254 /* Look up the command name.
1255 If exact match, keep that.
1256 Otherwise, take command abbreviated, if unique. Note that (in my
1257 opinion) a null string does *not* indicate ambiguity; simply the
1258 end of the argument. */
1260 if (p == *line)
1262 if (!allow_unknown)
1263 error ("Lack of needed %scommand", cmdtype);
1264 return 0;
1267 /* Copy over to a local buffer, converting to lowercase on the way.
1268 This is in case the command being parsed is a subcommand which
1269 doesn't match anything, and that's ok. We want the original
1270 untouched for the routine of the original command. */
1272 processed_cmd = (char *) alloca (p - *line + 1);
1273 for (cmd_len = 0; cmd_len < p - *line; cmd_len++)
1275 char x = (*line)[cmd_len];
1276 if (isupper (x))
1277 processed_cmd[cmd_len] = tolower (x);
1278 else
1279 processed_cmd[cmd_len] = x;
1281 processed_cmd[cmd_len] = '\0';
1283 /* Check all possibilities in the current command list. */
1284 found = 0;
1285 nfound = 0;
1286 for (c = list; c; c = c->next)
1288 if (!strncmp (processed_cmd, c->name, cmd_len))
1290 found = c;
1291 nfound++;
1292 if (c->name[cmd_len] == 0)
1294 nfound = 1;
1295 break;
1300 /* Report error for undefined command name. */
1302 if (nfound != 1)
1304 if (nfound > 1 && allow_unknown >= 0)
1306 ambbuf[0] = 0;
1307 for (c = list; c; c = c->next)
1308 if (!strncmp (processed_cmd, c->name, cmd_len))
1310 if (strlen (ambbuf) + strlen (c->name) + 6 < sizeof ambbuf)
1312 if (strlen (ambbuf))
1313 strcat (ambbuf, ", ");
1314 strcat (ambbuf, c->name);
1316 else
1318 strcat (ambbuf, "..");
1319 break;
1322 error ("Ambiguous %scommand \"%s\": %s.", cmdtype,
1323 processed_cmd, ambbuf);
1325 else if (!allow_unknown)
1326 error ("Undefined %scommand: \"%s\".", cmdtype, processed_cmd);
1327 return 0;
1330 /* Skip whitespace before the argument. */
1332 while (*p == ' ' || *p == '\t')
1333 p++;
1334 *line = p;
1336 if (found->prefixlist && *p)
1338 c = lookup_cmd (line, *found->prefixlist, found->prefixname,
1339 found->allow_unknown);
1340 if (c)
1341 return c;
1344 return found;
1346 #endif
1348 /* Helper function for SYMBOL_COMPLETION_FUNCTION. */
1350 /* Return a vector of char pointers which point to the different
1351 possible completions in LIST of TEXT.
1353 WORD points in the same buffer as TEXT, and completions should be
1354 returned relative to this position. For example, suppose TEXT is "foo"
1355 and we want to complete to "foobar". If WORD is "oo", return
1356 "oobar"; if WORD is "baz/foo", return "baz/foobar". */
1358 char **
1359 complete_on_cmdlist (struct cmd_list_element *list, char *text, char *word)
1361 struct cmd_list_element *ptr;
1362 char **matchlist;
1363 int sizeof_matchlist;
1364 int matches;
1365 int textlen = strlen (text);
1367 sizeof_matchlist = 10;
1368 matchlist = (char **) xmalloc (sizeof_matchlist * sizeof (char *));
1369 matches = 0;
1371 for (ptr = list; ptr; ptr = ptr->next)
1372 if (!strncmp (ptr->name, text, textlen)
1373 && !ptr->abbrev_flag
1374 && (ptr->function.cfunc
1375 || ptr->prefixlist))
1377 if (matches == sizeof_matchlist)
1379 sizeof_matchlist *= 2;
1380 matchlist = (char **) xrealloc ((char *) matchlist,
1381 (sizeof_matchlist
1382 * sizeof (char *)));
1385 matchlist[matches] = (char *)
1386 xmalloc (strlen (word) + strlen (ptr->name) + 1);
1387 if (word == text)
1388 strcpy (matchlist[matches], ptr->name);
1389 else if (word > text)
1391 /* Return some portion of ptr->name. */
1392 strcpy (matchlist[matches], ptr->name + (word - text));
1394 else
1396 /* Return some of text plus ptr->name. */
1397 strncpy (matchlist[matches], word, text - word);
1398 matchlist[matches][text - word] = '\0';
1399 strcat (matchlist[matches], ptr->name);
1401 ++matches;
1404 if (matches == 0)
1406 free ((PTR) matchlist);
1407 matchlist = 0;
1409 else
1411 matchlist = (char **) xrealloc ((char *) matchlist, ((matches + 1)
1412 * sizeof (char *)));
1413 matchlist[matches] = (char *) 0;
1416 return matchlist;
1419 /* Helper function for SYMBOL_COMPLETION_FUNCTION. */
1421 /* Return a vector of char pointers which point to the different
1422 possible completions in CMD of TEXT.
1424 WORD points in the same buffer as TEXT, and completions should be
1425 returned relative to this position. For example, suppose TEXT is "foo"
1426 and we want to complete to "foobar". If WORD is "oo", return
1427 "oobar"; if WORD is "baz/foo", return "baz/foobar". */
1429 char **
1430 complete_on_enum (const char *enumlist[],
1431 char *text,
1432 char *word)
1434 char **matchlist;
1435 int sizeof_matchlist;
1436 int matches;
1437 int textlen = strlen (text);
1438 int i;
1439 const char *name;
1441 sizeof_matchlist = 10;
1442 matchlist = (char **) xmalloc (sizeof_matchlist * sizeof (char *));
1443 matches = 0;
1445 for (i = 0; (name = enumlist[i]) != NULL; i++)
1446 if (strncmp (name, text, textlen) == 0)
1448 if (matches == sizeof_matchlist)
1450 sizeof_matchlist *= 2;
1451 matchlist = (char **) xrealloc ((char *) matchlist,
1452 (sizeof_matchlist
1453 * sizeof (char *)));
1456 matchlist[matches] = (char *)
1457 xmalloc (strlen (word) + strlen (name) + 1);
1458 if (word == text)
1459 strcpy (matchlist[matches], name);
1460 else if (word > text)
1462 /* Return some portion of name. */
1463 strcpy (matchlist[matches], name + (word - text));
1465 else
1467 /* Return some of text plus name. */
1468 strncpy (matchlist[matches], word, text - word);
1469 matchlist[matches][text - word] = '\0';
1470 strcat (matchlist[matches], name);
1472 ++matches;
1475 if (matches == 0)
1477 free ((PTR) matchlist);
1478 matchlist = 0;
1480 else
1482 matchlist = (char **) xrealloc ((char *) matchlist, ((matches + 1)
1483 * sizeof (char *)));
1484 matchlist[matches] = (char *) 0;
1487 return matchlist;
1490 static enum cmd_auto_boolean
1491 parse_auto_binary_operation (const char *arg)
1493 if (arg != NULL && *arg != '\0')
1495 int length = strlen (arg);
1496 while (isspace (arg[length - 1]) && length > 0)
1497 length--;
1498 if (strncmp (arg, "on", length) == 0
1499 || strncmp (arg, "1", length) == 0
1500 || strncmp (arg, "yes", length) == 0
1501 || strncmp (arg, "enable", length) == 0)
1502 return CMD_AUTO_BOOLEAN_TRUE;
1503 else if (strncmp (arg, "off", length) == 0
1504 || strncmp (arg, "0", length) == 0
1505 || strncmp (arg, "no", length) == 0
1506 || strncmp (arg, "disable", length) == 0)
1507 return CMD_AUTO_BOOLEAN_FALSE;
1508 else if (strncmp (arg, "auto", length) == 0
1509 || (strncmp (arg, "-1", length) == 0 && length > 1))
1510 return CMD_AUTO_BOOLEAN_AUTO;
1512 error ("\"on\", \"off\" or \"auto\" expected.");
1513 return CMD_AUTO_BOOLEAN_AUTO; /* pacify GCC */
1516 static int
1517 parse_binary_operation (char *arg)
1519 int length;
1521 if (!arg || !*arg)
1522 return 1;
1524 length = strlen (arg);
1526 while (arg[length - 1] == ' ' || arg[length - 1] == '\t')
1527 length--;
1529 if (strncmp (arg, "on", length) == 0
1530 || strncmp (arg, "1", length) == 0
1531 || strncmp (arg, "yes", length) == 0
1532 || strncmp (arg, "enable", length) == 0)
1533 return 1;
1534 else if (strncmp (arg, "off", length) == 0
1535 || strncmp (arg, "0", length) == 0
1536 || strncmp (arg, "no", length) == 0
1537 || strncmp (arg, "disable", length) == 0)
1538 return 0;
1539 else
1541 error ("\"on\" or \"off\" expected.");
1542 return 0;
1546 /* Do a "set" or "show" command. ARG is NULL if no argument, or the text
1547 of the argument, and FROM_TTY is nonzero if this command is being entered
1548 directly by the user (i.e. these are just like any other
1549 command). C is the command list element for the command. */
1550 void
1551 do_setshow_command (char *arg, int from_tty, struct cmd_list_element *c)
1553 if (c->type == set_cmd)
1555 switch (c->var_type)
1557 case var_string:
1559 char *new;
1560 char *p;
1561 char *q;
1562 int ch;
1564 if (arg == NULL)
1565 arg = "";
1566 new = (char *) xmalloc (strlen (arg) + 2);
1567 p = arg;
1568 q = new;
1569 while ((ch = *p++) != '\000')
1571 if (ch == '\\')
1573 /* \ at end of argument is used after spaces
1574 so they won't be lost. */
1575 /* This is obsolete now that we no longer strip
1576 trailing whitespace and actually, the backslash
1577 didn't get here in my test, readline or
1578 something did something funky with a backslash
1579 right before a newline. */
1580 if (*p == 0)
1581 break;
1582 ch = parse_escape (&p);
1583 if (ch == 0)
1584 break; /* C loses */
1585 else if (ch > 0)
1586 *q++ = ch;
1588 else
1589 *q++ = ch;
1591 #if 0
1592 if (*(p - 1) != '\\')
1593 *q++ = ' ';
1594 #endif
1595 *q++ = '\0';
1596 new = (char *) xrealloc (new, q - new);
1597 if (*(char **) c->var != NULL)
1598 free (*(char **) c->var);
1599 *(char **) c->var = new;
1601 break;
1602 case var_string_noescape:
1603 if (arg == NULL)
1604 arg = "";
1605 if (*(char **) c->var != NULL)
1606 free (*(char **) c->var);
1607 *(char **) c->var = savestring (arg, strlen (arg));
1608 break;
1609 case var_filename:
1610 if (arg == NULL)
1611 error_no_arg ("filename to set it to.");
1612 if (*(char **) c->var != NULL)
1613 free (*(char **) c->var);
1614 *(char **) c->var = tilde_expand (arg);
1615 break;
1616 case var_boolean:
1617 *(int *) c->var = parse_binary_operation (arg);
1618 break;
1619 case var_auto_boolean:
1620 *(enum cmd_auto_boolean *) c->var = parse_auto_binary_operation (arg);
1621 break;
1622 case var_uinteger:
1623 if (arg == NULL)
1624 error_no_arg ("integer to set it to.");
1625 *(unsigned int *) c->var = parse_and_eval_address (arg);
1626 if (*(unsigned int *) c->var == 0)
1627 *(unsigned int *) c->var = UINT_MAX;
1628 break;
1629 case var_integer:
1631 unsigned int val;
1632 if (arg == NULL)
1633 error_no_arg ("integer to set it to.");
1634 val = parse_and_eval_address (arg);
1635 if (val == 0)
1636 *(int *) c->var = INT_MAX;
1637 else if (val >= INT_MAX)
1638 error ("integer %u out of range", val);
1639 else
1640 *(int *) c->var = val;
1641 break;
1643 case var_zinteger:
1644 if (arg == NULL)
1645 error_no_arg ("integer to set it to.");
1646 *(int *) c->var = parse_and_eval_address (arg);
1647 break;
1648 case var_enum:
1650 int i;
1651 int len;
1652 int nmatches;
1653 const char *match = NULL;
1654 char *p;
1656 /* if no argument was supplied, print an informative error message */
1657 if (arg == NULL)
1659 char msg[1024];
1660 strcpy (msg, "Requires an argument. Valid arguments are ");
1661 for (i = 0; c->enums[i]; i++)
1663 if (i != 0)
1664 strcat (msg, ", ");
1665 strcat (msg, c->enums[i]);
1667 strcat (msg, ".");
1668 error (msg);
1671 p = strchr (arg, ' ');
1673 if (p)
1674 len = p - arg;
1675 else
1676 len = strlen (arg);
1678 nmatches = 0;
1679 for (i = 0; c->enums[i]; i++)
1680 if (strncmp (arg, c->enums[i], len) == 0)
1682 if (c->enums[i][len] == '\0')
1684 match = c->enums[i];
1685 nmatches = 1;
1686 break; /* exact match. */
1688 else
1690 match = c->enums[i];
1691 nmatches++;
1695 if (nmatches <= 0)
1696 error ("Undefined item: \"%s\".", arg);
1698 if (nmatches > 1)
1699 error ("Ambiguous item \"%s\".", arg);
1701 *(const char **) c->var = match;
1703 break;
1704 default:
1705 error ("gdb internal error: bad var_type in do_setshow_command");
1708 else if (c->type == show_cmd)
1710 #ifdef UI_OUT
1711 struct cleanup *old_chain;
1712 struct ui_stream *stb;
1713 int quote;
1715 stb = ui_out_stream_new (uiout);
1716 old_chain = make_cleanup_ui_out_stream_delete (stb);
1717 #endif /* UI_OUT */
1719 /* Print doc minus "show" at start. */
1720 print_doc_line (gdb_stdout, c->doc + 5);
1722 #ifdef UI_OUT
1723 ui_out_text (uiout, " is ");
1724 ui_out_wrap_hint (uiout, " ");
1725 quote = 0;
1726 switch (c->var_type)
1728 case var_string:
1730 unsigned char *p;
1732 if (*(unsigned char **) c->var)
1733 fputstr_filtered (*(unsigned char **) c->var, '"', stb->stream);
1734 quote = 1;
1736 break;
1737 case var_string_noescape:
1738 case var_filename:
1739 case var_enum:
1740 if (*(char **) c->var)
1741 fputs_filtered (*(char **) c->var, stb->stream);
1742 quote = 1;
1743 break;
1744 case var_boolean:
1745 fputs_filtered (*(int *) c->var ? "on" : "off", stb->stream);
1746 break;
1747 case var_auto_boolean:
1748 switch (*(enum cmd_auto_boolean*) c->var)
1750 case CMD_AUTO_BOOLEAN_TRUE:
1751 fputs_filtered ("on", stb->stream);
1752 break;
1753 case CMD_AUTO_BOOLEAN_FALSE:
1754 fputs_filtered ("off", stb->stream);
1755 break;
1756 case CMD_AUTO_BOOLEAN_AUTO:
1757 fputs_filtered ("auto", stb->stream);
1758 break;
1759 default:
1760 internal_error ("do_setshow_command: invalid var_auto_boolean");
1761 break;
1763 break;
1764 case var_uinteger:
1765 if (*(unsigned int *) c->var == UINT_MAX)
1767 fputs_filtered ("unlimited", stb->stream);
1768 break;
1770 /* else fall through */
1771 case var_zinteger:
1772 fprintf_filtered (stb->stream, "%u", *(unsigned int *) c->var);
1773 break;
1774 case var_integer:
1775 if (*(int *) c->var == INT_MAX)
1777 fputs_filtered ("unlimited", stb->stream);
1779 else
1780 fprintf_filtered (stb->stream, "%d", *(int *) c->var);
1781 break;
1783 default:
1784 error ("gdb internal error: bad var_type in do_setshow_command");
1786 if (quote)
1787 ui_out_text (uiout, "\"");
1788 ui_out_field_stream (uiout, "value", stb);
1789 if (quote)
1790 ui_out_text (uiout, "\"");
1791 ui_out_text (uiout, ".\n");
1792 do_cleanups (old_chain);
1793 #else
1794 fputs_filtered (" is ", gdb_stdout);
1795 wrap_here (" ");
1796 switch (c->var_type)
1798 case var_string:
1800 fputs_filtered ("\"", gdb_stdout);
1801 if (*(unsigned char **) c->var)
1802 fputstr_filtered (*(unsigned char **) c->var, '"', gdb_stdout);
1803 fputs_filtered ("\"", gdb_stdout);
1805 break;
1806 case var_string_noescape:
1807 case var_filename:
1808 case var_enum:
1809 fputs_filtered ("\"", gdb_stdout);
1810 if (*(char **) c->var)
1811 fputs_filtered (*(char **) c->var, gdb_stdout);
1812 fputs_filtered ("\"", gdb_stdout);
1813 break;
1814 case var_boolean:
1815 fputs_filtered (*(int *) c->var ? "on" : "off", gdb_stdout);
1816 break;
1817 case var_auto_boolean:
1818 switch (*(enum cmd_auto_boolean*) c->var)
1820 case CMD_AUTO_BOOLEAN_TRUE:
1821 fputs_filtered ("on", gdb_stdout);
1822 break;
1823 case CMD_AUTO_BOOLEAN_FALSE:
1824 fputs_filtered ("off", gdb_stdout);
1825 break;
1826 case CMD_AUTO_BOOLEAN_AUTO:
1827 fputs_filtered ("auto", gdb_stdout);
1828 break;
1829 default:
1830 internal_error ("do_setshow_command: invalid var_auto_boolean");
1831 break;
1833 break;
1834 case var_uinteger:
1835 if (*(unsigned int *) c->var == UINT_MAX)
1837 fputs_filtered ("unlimited", gdb_stdout);
1838 break;
1840 /* else fall through */
1841 case var_zinteger:
1842 fprintf_filtered (gdb_stdout, "%u", *(unsigned int *) c->var);
1843 break;
1844 case var_integer:
1845 if (*(int *) c->var == INT_MAX)
1847 fputs_filtered ("unlimited", gdb_stdout);
1849 else
1850 fprintf_filtered (gdb_stdout, "%d", *(int *) c->var);
1851 break;
1853 default:
1854 error ("gdb internal error: bad var_type in do_setshow_command");
1856 fputs_filtered (".\n", gdb_stdout);
1857 #endif
1859 else
1860 error ("gdb internal error: bad cmd_type in do_setshow_command");
1861 (*c->function.sfunc) (NULL, from_tty, c);
1862 if (c->type == set_cmd && set_hook)
1863 set_hook (c);
1866 /* Show all the settings in a list of show commands. */
1868 void
1869 cmd_show_list (struct cmd_list_element *list, int from_tty, char *prefix)
1871 #ifdef UI_OUT
1872 ui_out_list_begin (uiout, "showlist");
1873 #endif
1874 for (; list != NULL; list = list->next)
1876 /* If we find a prefix, run its list, prefixing our output by its
1877 prefix (with "show " skipped). */
1878 #ifdef UI_OUT
1879 if (list->prefixlist && !list->abbrev_flag)
1881 ui_out_list_begin (uiout, "optionlist");
1882 ui_out_field_string (uiout, "prefix", list->prefixname + 5);
1883 cmd_show_list (*list->prefixlist, from_tty, list->prefixname + 5);
1884 ui_out_list_end (uiout);
1886 if (list->type == show_cmd)
1888 ui_out_list_begin (uiout, "option");
1889 ui_out_text (uiout, prefix);
1890 ui_out_field_string (uiout, "name", list->name);
1891 ui_out_text (uiout, ": ");
1892 do_setshow_command ((char *) NULL, from_tty, list);
1893 ui_out_list_end (uiout);
1895 #else
1896 if (list->prefixlist && !list->abbrev_flag)
1897 cmd_show_list (*list->prefixlist, from_tty, list->prefixname + 5);
1898 if (list->type == show_cmd)
1900 fputs_filtered (prefix, gdb_stdout);
1901 fputs_filtered (list->name, gdb_stdout);
1902 fputs_filtered (": ", gdb_stdout);
1903 do_setshow_command ((char *) NULL, from_tty, list);
1905 #endif
1907 #ifdef UI_OUT
1908 ui_out_list_end (uiout);
1909 #endif
1912 /* ARGSUSED */
1913 static void
1914 shell_escape (char *arg, int from_tty)
1916 #ifdef CANT_FORK
1917 /* If ARG is NULL, they want an inferior shell, but `system' just
1918 reports if the shell is available when passed a NULL arg. */
1919 int rc = system (arg ? arg : "");
1921 if (!arg)
1922 arg = "inferior shell";
1924 if (rc == -1)
1926 fprintf_unfiltered (gdb_stderr, "Cannot execute %s: %s\n", arg,
1927 safe_strerror (errno));
1928 gdb_flush (gdb_stderr);
1930 else if (rc)
1932 fprintf_unfiltered (gdb_stderr, "%s exited with status %d\n", arg, rc);
1933 gdb_flush (gdb_stderr);
1935 #ifdef __DJGPP__
1936 /* Make sure to return to the directory GDB thinks it is, in case the
1937 shell command we just ran changed it. */
1938 chdir (current_directory);
1939 #endif
1940 #else /* Can fork. */
1941 int rc, status, pid;
1942 char *p, *user_shell;
1944 if ((user_shell = (char *) getenv ("SHELL")) == NULL)
1945 user_shell = "/bin/sh";
1947 /* Get the name of the shell for arg0 */
1948 if ((p = strrchr (user_shell, '/')) == NULL)
1949 p = user_shell;
1950 else
1951 p++; /* Get past '/' */
1953 if ((pid = fork ()) == 0)
1955 if (!arg)
1956 execl (user_shell, p, 0);
1957 else
1958 execl (user_shell, p, "-c", arg, 0);
1960 fprintf_unfiltered (gdb_stderr, "Cannot execute %s: %s\n", user_shell,
1961 safe_strerror (errno));
1962 gdb_flush (gdb_stderr);
1963 _exit (0177);
1966 if (pid != -1)
1967 while ((rc = wait (&status)) != pid && rc != -1)
1969 else
1970 error ("Fork failed");
1971 #endif /* Can fork. */
1974 static void
1975 make_command (char *arg, int from_tty)
1977 char *p;
1979 if (arg == 0)
1980 p = "make";
1981 else
1983 p = xmalloc (sizeof ("make ") + strlen (arg));
1984 strcpy (p, "make ");
1985 strcpy (p + sizeof ("make ") - 1, arg);
1988 shell_escape (p, from_tty);
1991 static void
1992 show_user_1 (struct cmd_list_element *c, struct ui_file *stream)
1994 register struct command_line *cmdlines;
1996 cmdlines = c->user_commands;
1997 if (!cmdlines)
1998 return;
1999 fputs_filtered ("User command ", stream);
2000 fputs_filtered (c->name, stream);
2001 fputs_filtered (":\n", stream);
2003 #ifdef UI_OUT
2004 print_command_lines (uiout, cmdlines, 1);
2005 fputs_filtered ("\n", stream);
2006 #else
2007 while (cmdlines)
2009 print_command_line (cmdlines, 4, stream);
2010 cmdlines = cmdlines->next;
2012 fputs_filtered ("\n", stream);
2013 #endif
2016 /* ARGSUSED */
2017 static void
2018 show_user (char *args, int from_tty)
2020 struct cmd_list_element *c;
2021 extern struct cmd_list_element *cmdlist;
2023 if (args)
2025 c = lookup_cmd (&args, cmdlist, "", 0, 1);
2026 if (c->class != class_user)
2027 error ("Not a user command.");
2028 show_user_1 (c, gdb_stdout);
2030 else
2032 for (c = cmdlist; c; c = c->next)
2034 if (c->class == class_user)
2035 show_user_1 (c, gdb_stdout);
2040 void
2041 _initialize_command (void)
2043 add_com ("shell", class_support, shell_escape,
2044 "Execute the rest of the line as a shell command. \n\
2045 With no arguments, run an inferior shell.");
2047 /* NOTE: cagney/2000-03-20: Being able to enter ``(gdb) !ls'' would
2048 be a really useful feature. Unfortunatly, the below wont do
2049 this. Instead it adds support for the form ``(gdb) ! ls''
2050 (i.e. the space is required). If the ``!'' command below is
2051 added the complains about no ``!'' command would be replaced by
2052 complains about how the ``!'' command is broken :-) */
2053 if (xdb_commands)
2054 add_com_alias ("!", "shell", class_support, 0);
2056 add_com ("make", class_support, make_command,
2057 "Run the ``make'' program using the rest of the line as arguments.");
2058 add_cmd ("user", no_class, show_user,
2059 "Show definitions of user defined commands.\n\
2060 Argument is the name of the user defined command.\n\
2061 With no argument, show definitions of all user defined commands.", &showlist);
2062 add_com ("apropos", class_support, apropos_command, "Search for commands matching a REGEXP");