This commit was generated by cvs2svn to track changes on a CVS vendor
[platform/upstream/binutils.git] / gdb / command.c
1 /* Handle lists of commands, their decoding and documentation, for GDB.
2    Copyright 1986, 1989, 1990, 1991, 1998, 2000 Free Software Foundation, Inc.
3
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.
8
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.
13
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.  */
18
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
28
29 #include "gdb_wait.h"
30
31 /* FIXME: this should be auto-configured!  */
32 #ifdef __MSDOS__
33 # define CANT_FORK
34 #endif
35
36 /* Prototypes for local functions */
37
38 static void undef_cmd_error PARAMS ((char *, char *));
39
40 static void show_user PARAMS ((char *, int));
41
42 static void show_user_1 (struct cmd_list_element *, struct ui_file *);
43
44 static void make_command PARAMS ((char *, int));
45
46 static void shell_escape PARAMS ((char *, int));
47
48 static int parse_binary_operation PARAMS ((char *));
49
50 static void print_doc_line (struct ui_file *, char *);
51
52 static struct cmd_list_element *find_cmd PARAMS ((char *command,
53                                                   int len,
54                                             struct cmd_list_element * clist,
55                                                   int ignore_help_classes,
56                                                   int *nfound));
57
58 void _initialize_command PARAMS ((void));
59
60 /* Add element named NAME.
61    CLASS is the top level category into which commands are broken down
62    for "help" purposes.
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.
66
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.
71
72    Add this command to command list *LIST.  
73
74    Returns a pointer to the added command (not necessarily the head 
75    of *LIST). */
76
77 struct cmd_list_element *
78 add_cmd (name, class, fun, doc, list)
79      char *name;
80      enum command_class class;
81      void (*fun) PARAMS ((char *, int));
82      char *doc;
83      struct cmd_list_element **list;
84 {
85   register struct cmd_list_element *c
86   = (struct cmd_list_element *) xmalloc (sizeof (struct cmd_list_element));
87   struct cmd_list_element *p;
88
89   delete_cmd (name, list);
90
91   if (*list == NULL || STRCMP ((*list)->name, name) >= 0)
92     {
93       c->next = *list;
94       *list = c;
95     }
96   else
97     {
98       p = *list;
99       while (p->next && STRCMP (p->next->name, name) <= 0)
100         {
101           p = p->next;
102         }
103       c->next = p->next;
104       p->next = c;
105     }
106
107   c->name = name;
108   c->class = class;
109   c->function.cfunc = fun;
110   c->doc = doc;
111   c->hook = NULL;
112   c->prefixlist = NULL;
113   c->prefixname = NULL;
114   c->allow_unknown = 0;
115   c->abbrev_flag = 0;
116   c->completer = make_symbol_completion_list;
117   c->type = not_set_cmd;
118   c->var = NULL;
119   c->var_type = var_boolean;
120   c->enums = NULL;
121   c->user_commands = NULL;
122   c->hookee = NULL;
123   c->cmd_pointer = NULL;
124
125   return c;
126 }
127
128 /* Same as above, except that the abbrev_flag is set. */
129
130 #if 0                           /* Currently unused */
131
132 struct cmd_list_element *
133 add_abbrev_cmd (name, class, fun, doc, list)
134      char *name;
135      enum command_class class;
136      void (*fun) PARAMS ((char *, int));
137      char *doc;
138      struct cmd_list_element **list;
139 {
140   register struct cmd_list_element *c
141   = add_cmd (name, class, fun, doc, list);
142
143   c->abbrev_flag = 1;
144   return c;
145 }
146
147 #endif
148
149 struct cmd_list_element *
150 add_alias_cmd (name, oldname, class, abbrev_flag, list)
151      char *name;
152      char *oldname;
153      enum command_class class;
154      int abbrev_flag;
155      struct cmd_list_element **list;
156 {
157   /* Must do this since lookup_cmd tries to side-effect its first arg */
158   char *copied_name;
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);
164
165   if (old == 0)
166     {
167       delete_cmd (name, list);
168       return 0;
169     }
170
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;
177   return c;
178 }
179
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.  */
184
185 struct cmd_list_element *
186 add_prefix_cmd (name, class, fun, doc, prefixlist, prefixname,
187                 allow_unknown, list)
188      char *name;
189      enum command_class class;
190      void (*fun) PARAMS ((char *, int));
191      char *doc;
192      struct cmd_list_element **prefixlist;
193      char *prefixname;
194      int allow_unknown;
195      struct cmd_list_element **list;
196 {
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;
201   return c;
202 }
203
204 /* Like add_prefix_cmd but sets the abbrev_flag on the new command. */
205
206 struct cmd_list_element *
207 add_abbrev_prefix_cmd (name, class, fun, doc, prefixlist, prefixname,
208                        allow_unknown, list)
209      char *name;
210      enum command_class class;
211      void (*fun) PARAMS ((char *, int));
212      char *doc;
213      struct cmd_list_element **prefixlist;
214      char *prefixname;
215      int allow_unknown;
216      struct cmd_list_element **list;
217 {
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;
222   c->abbrev_flag = 1;
223   return c;
224 }
225
226 /* This is an empty "cfunc".  */
227 void
228 not_just_help_class_command (args, from_tty)
229      char *args;
230      int from_tty;
231 {
232 }
233
234 /* This is an empty "sfunc".  */
235 static void empty_sfunc PARAMS ((char *, int, struct cmd_list_element *));
236
237 static void
238 empty_sfunc (args, from_tty, c)
239      char *args;
240      int from_tty;
241      struct cmd_list_element *c;
242 {
243 }
244
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.  */
251
252 struct cmd_list_element *
253 add_set_cmd (name, class, var_type, var, doc, list)
254      char *name;
255      enum command_class class;
256      var_types var_type;
257      char *var;
258      char *doc;
259      struct cmd_list_element **list;
260 {
261   struct cmd_list_element *c
262   = add_cmd (name, class, NO_FUNCTION, doc, list);
263
264   c->type = set_cmd;
265   c->var_type = var_type;
266   c->var = var;
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;
270   return c;
271 }
272
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
278    (from ENUMLIST).
279    DOC is the documentation string.  */
280
281 struct cmd_list_element *
282 add_set_enum_cmd (name, class, enumlist, var, doc, list)
283      char *name;
284      enum command_class class;
285      char *enumlist[];
286      char *var;
287      char *doc;
288      struct cmd_list_element **list;
289 {
290   struct cmd_list_element *c
291   = add_set_cmd (name, class, var_enum, var, doc, list);
292   c->enums = enumlist;
293
294   return c;
295 }
296
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;
304 {
305   struct cmd_list_element *showcmd =
306   (struct cmd_list_element *) xmalloc (sizeof (struct cmd_list_element));
307   struct cmd_list_element *p;
308
309   memcpy (showcmd, setcmd, sizeof (struct cmd_list_element));
310   delete_cmd (showcmd->name, list);
311   showcmd->type = show_cmd;
312
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);
317   else
318     fprintf_unfiltered (gdb_stderr, "GDB internal error: Bad docstring for set command\n");
319
320   if (*list == NULL || STRCMP ((*list)->name, showcmd->name) >= 0)
321     {
322       showcmd->next = *list;
323       *list = showcmd;
324     }
325   else
326     {
327       p = *list;
328       while (p->next && STRCMP (p->next->name, showcmd->name) <= 0)
329         {
330           p = p->next;
331         }
332       showcmd->next = p->next;
333       p->next = showcmd;
334     }
335
336   return showcmd;
337 }
338
339 /* Remove the command named NAME from the command list.  */
340
341 void
342 delete_cmd (name, list)
343      char *name;
344      struct cmd_list_element **list;
345 {
346   register struct cmd_list_element *c;
347   struct cmd_list_element *p;
348
349   while (*list && STREQ ((*list)->name, name))
350     {
351       if ((*list)->hookee)
352         (*list)->hookee->hook = 0;      /* Hook slips out of its mouth */
353       p = (*list)->next;
354       free ((PTR) * list);
355       *list = p;
356     }
357
358   if (*list)
359     for (c = *list; c->next;)
360       {
361         if (STREQ (c->next->name, name))
362           {
363             if (c->next->hookee)
364               c->next->hookee->hook = 0;        /* hooked cmd gets away.  */
365             p = c->next->next;
366             free ((PTR) c->next);
367             c->next = p;
368           }
369         else
370           c = c->next;
371       }
372 }
373
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
379  * just "help".)
380  *
381  *   I am going to split this into two seperate comamnds, help_cmd and
382  * help_list. 
383  */
384
385 void
386 help_cmd (command, stream)
387      char *command;
388      struct ui_file *stream;
389 {
390   struct cmd_list_element *c;
391   extern struct cmd_list_element *cmdlist;
392
393   if (!command)
394     {
395       help_list (cmdlist, "", all_classes, stream);
396       return;
397     }
398
399   c = lookup_cmd (&command, cmdlist, "", 0, 0);
400
401   if (c == 0)
402     return;
403
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.
407
408      If c->function is nonzero, we really have a command.
409      Print its documentation and return.
410
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.  */
415
416   fputs_filtered (c->doc, stream);
417   fputs_filtered ("\n", stream);
418
419   if (c->prefixlist == 0 && c->function.cfunc != NULL)
420     return;
421   fprintf_filtered (stream, "\n");
422
423   /* If this is a prefix command, print it's subcommands */
424   if (c->prefixlist)
425     help_list (*c->prefixlist, c->prefixname, all_commands, stream);
426
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);
430
431   if (c->hook)
432     fprintf_filtered (stream, "\nThis command has a hook defined: %s\n",
433                       c->hook->name);
434 }
435
436 /*
437  * Get a specific kind of help on a command list.
438  *
439  * LIST is the 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.
447  */
448 void
449 help_list (list, cmdtype, class, stream)
450      struct cmd_list_element *list;
451      char *cmdtype;
452      enum command_class class;
453      struct ui_file *stream;
454 {
455   int len;
456   char *cmdtype1, *cmdtype2;
457
458   /* If CMDTYPE is "foo ", CMDTYPE1 gets " foo" and CMDTYPE2 gets "foo sub"  */
459   len = strlen (cmdtype);
460   cmdtype1 = (char *) alloca (len + 1);
461   cmdtype1[0] = 0;
462   cmdtype2 = (char *) alloca (len + 4);
463   cmdtype2[0] = 0;
464   if (len)
465     {
466       cmdtype1[0] = ' ';
467       strncpy (cmdtype1 + 1, cmdtype, len - 1);
468       cmdtype1[len] = 0;
469       strncpy (cmdtype2, cmdtype, len - 1);
470       strcpy (cmdtype2 + len - 1, " sub");
471     }
472
473   if (class == all_classes)
474     fprintf_filtered (stream, "List of classes of %scommands:\n\n", cmdtype2);
475   else
476     fprintf_filtered (stream, "List of %scommands:\n\n", cmdtype2);
477
478   help_cmd_list (list, class, cmdtype, (int) class >= 0, stream);
479
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.",
483                       cmdtype1);
484
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",
488                     cmdtype1, cmdtype2);
489 }
490
491 /* Print only the first line of STR on STREAM.  */
492 static void
493 print_doc_line (stream, str)
494      struct ui_file *stream;
495      char *str;
496 {
497   static char *line_buffer = 0;
498   static int line_size;
499   register char *p;
500
501   if (!line_buffer)
502     {
503       line_size = 80;
504       line_buffer = (char *) xmalloc (line_size);
505     }
506
507   p = str;
508   while (*p && *p != '\n' && *p != '.' && *p != ',')
509     p++;
510   if (p - str > line_size - 1)
511     {
512       line_size = p - str + 1;
513       free ((PTR) line_buffer);
514       line_buffer = (char *) xmalloc (line_size);
515     }
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 #ifdef UI_OUT
521   ui_out_text (uiout, line_buffer);
522 #else
523   fputs_filtered (line_buffer, stream);
524 #endif
525 }
526
527 /*
528  * Implement a help command on command list LIST.
529  * RECURSE should be non-zero if this should be done recursively on
530  * all sublists of LIST.
531  * PREFIX is the prefix to print before each command name.
532  * STREAM is the stream upon which the output should be written.
533  * CLASS should be:
534  *      A non-negative class number to list only commands in that
535  * class.
536  *      ALL_COMMANDS to list all commands in list.
537  *      ALL_CLASSES  to list all classes in list.
538  *
539  *   Note that RECURSE will be active on *all* sublists, not just the
540  * ones selected by the criteria above (ie. the selection mechanism
541  * is at the low level, not the high-level).
542  */
543 void
544 help_cmd_list (list, class, prefix, recurse, stream)
545      struct cmd_list_element *list;
546      enum command_class class;
547      char *prefix;
548      int recurse;
549      struct ui_file *stream;
550 {
551   register struct cmd_list_element *c;
552
553   for (c = list; c; c = c->next)
554     {
555       if (c->abbrev_flag == 0 &&
556           (class == all_commands
557            || (class == all_classes && c->function.cfunc == NULL)
558            || (class == c->class && c->function.cfunc != NULL)))
559         {
560           fprintf_filtered (stream, "%s%s -- ", prefix, c->name);
561           print_doc_line (stream, c->doc);
562           fputs_filtered ("\n", stream);
563         }
564       if (recurse
565           && c->prefixlist != 0
566           && c->abbrev_flag == 0)
567         help_cmd_list (*c->prefixlist, class, c->prefixname, 1, stream);
568     }
569 }
570 \f
571
572 /* Search the input clist for 'command'.  Return the command if
573    found (or NULL if not), and return the number of commands
574    found in nfound */
575
576 static struct cmd_list_element *
577 find_cmd (command, len, clist, ignore_help_classes, nfound)
578      char *command;
579      int len;
580      struct cmd_list_element *clist;
581      int ignore_help_classes;
582      int *nfound;
583 {
584   struct cmd_list_element *found, *c;
585
586   found = (struct cmd_list_element *) NULL;
587   *nfound = 0;
588   for (c = clist; c; c = c->next)
589     if (!strncmp (command, c->name, len)
590         && (!ignore_help_classes || c->function.cfunc))
591       {
592         found = c;
593         (*nfound)++;
594         if (c->name[len] == '\0')
595           {
596             *nfound = 1;
597             break;
598           }
599       }
600   return found;
601 }
602
603 /* This routine takes a line of TEXT and a CLIST in which to start the
604    lookup.  When it returns it will have incremented the text pointer past
605    the section of text it matched, set *RESULT_LIST to point to the list in
606    which the last word was matched, and will return a pointer to the cmd
607    list element which the text matches.  It will return NULL if no match at
608    all was possible.  It will return -1 (cast appropriately, ick) if ambigous
609    matches are possible; in this case *RESULT_LIST will be set to point to
610    the list in which there are ambiguous choices (and *TEXT will be set to
611    the ambiguous text string).
612
613    If the located command was an abbreviation, this routine returns the base
614    command of the abbreviation.
615
616    It does no error reporting whatsoever; control will always return
617    to the superior routine.
618
619    In the case of an ambiguous return (-1), *RESULT_LIST will be set to point
620    at the prefix_command (ie. the best match) *or* (special case) will be NULL
621    if no prefix command was ever found.  For example, in the case of "info a",
622    "info" matches without ambiguity, but "a" could be "args" or "address", so
623    *RESULT_LIST is set to the cmd_list_element for "info".  So in this case
624    RESULT_LIST should not be interpeted as a pointer to the beginning of a
625    list; it simply points to a specific command.  In the case of an ambiguous
626    return *TEXT is advanced past the last non-ambiguous prefix (e.g.
627    "info t" can be "info types" or "info target"; upon return *TEXT has been
628    advanced past "info ").
629
630    If RESULT_LIST is NULL, don't set *RESULT_LIST (but don't otherwise
631    affect the operation).
632
633    This routine does *not* modify the text pointed to by TEXT.
634
635    If IGNORE_HELP_CLASSES is nonzero, ignore any command list elements which
636    are actually help classes rather than commands (i.e. the function field of
637    the struct cmd_list_element is NULL).  */
638
639 struct cmd_list_element *
640 lookup_cmd_1 (text, clist, result_list, ignore_help_classes)
641      char **text;
642      struct cmd_list_element *clist, **result_list;
643      int ignore_help_classes;
644 {
645   char *p, *command;
646   int len, tmp, nfound;
647   struct cmd_list_element *found, *c;
648
649   while (**text == ' ' || **text == '\t')
650     (*text)++;
651
652   /* Treating underscores as part of command words is important
653      so that "set args_foo()" doesn't get interpreted as
654      "set args _foo()".  */
655   for (p = *text;
656        *p && (isalnum (*p) || *p == '-' || *p == '_' ||
657               (tui_version &&
658                (*p == '+' || *p == '<' || *p == '>' || *p == '$')) ||
659               (xdb_commands && (*p == '!' || *p == '/' || *p == '?')));
660        p++)
661     ;
662
663   /* If nothing but whitespace, return 0.  */
664   if (p == *text)
665     return 0;
666
667   len = p - *text;
668
669   /* *text and p now bracket the first command word to lookup (and
670      it's length is len).  We copy this into a local temporary */
671
672
673   command = (char *) alloca (len + 1);
674   for (tmp = 0; tmp < len; tmp++)
675     {
676       char x = (*text)[tmp];
677       command[tmp] = x;
678     }
679   command[len] = '\0';
680
681   /* Look it up.  */
682   found = 0;
683   nfound = 0;
684   found = find_cmd (command, len, clist, ignore_help_classes, &nfound);
685
686   /* 
687      ** We didn't find the command in the entered case, so lower case it
688      ** and search again.
689    */
690   if (!found || nfound == 0)
691     {
692       for (tmp = 0; tmp < len; tmp++)
693         {
694           char x = command[tmp];
695           command[tmp] = isupper (x) ? tolower (x) : x;
696         }
697       found = find_cmd (command, len, clist, ignore_help_classes, &nfound);
698     }
699
700   /* If nothing matches, we have a simple failure.  */
701   if (nfound == 0)
702     return 0;
703
704   if (nfound > 1)
705     {
706       if (result_list != NULL)
707         /* Will be modified in calling routine
708            if we know what the prefix command is.  */
709         *result_list = 0;
710       return (struct cmd_list_element *) -1;    /* Ambiguous.  */
711     }
712
713   /* We've matched something on this list.  Move text pointer forward. */
714
715   *text = p;
716
717   /* If this was an abbreviation, use the base command instead.  */
718
719   if (found->cmd_pointer)
720     found = found->cmd_pointer;
721
722   /* If we found a prefix command, keep looking.  */
723
724   if (found->prefixlist)
725     {
726       c = lookup_cmd_1 (text, *found->prefixlist, result_list,
727                         ignore_help_classes);
728       if (!c)
729         {
730           /* Didn't find anything; this is as far as we got.  */
731           if (result_list != NULL)
732             *result_list = clist;
733           return found;
734         }
735       else if (c == (struct cmd_list_element *) -1)
736         {
737           /* We've gotten this far properly, but the next step
738              is ambiguous.  We need to set the result list to the best
739              we've found (if an inferior hasn't already set it).  */
740           if (result_list != NULL)
741             if (!*result_list)
742               /* This used to say *result_list = *found->prefixlist
743                  If that was correct, need to modify the documentation
744                  at the top of this function to clarify what is supposed
745                  to be going on.  */
746               *result_list = found;
747           return c;
748         }
749       else
750         {
751           /* We matched!  */
752           return c;
753         }
754     }
755   else
756     {
757       if (result_list != NULL)
758         *result_list = clist;
759       return found;
760     }
761 }
762
763 /* All this hair to move the space to the front of cmdtype */
764
765 static void
766 undef_cmd_error (cmdtype, q)
767      char *cmdtype, *q;
768 {
769   error ("Undefined %scommand: \"%s\".  Try \"help%s%.*s\".",
770          cmdtype,
771          q,
772          *cmdtype ? " " : "",
773          strlen (cmdtype) - 1,
774          cmdtype);
775 }
776
777 /* Look up the contents of *LINE as a command in the command list LIST.
778    LIST is a chain of struct cmd_list_element's.
779    If it is found, return the struct cmd_list_element for that command
780    and update *LINE to point after the command name, at the first argument.
781    If not found, call error if ALLOW_UNKNOWN is zero
782    otherwise (or if error returns) return zero.
783    Call error if specified command is ambiguous,
784    unless ALLOW_UNKNOWN is negative.
785    CMDTYPE precedes the word "command" in the error message.
786
787    If INGNORE_HELP_CLASSES is nonzero, ignore any command list
788    elements which are actually help classes rather than commands (i.e.
789    the function field of the struct cmd_list_element is 0).  */
790
791 struct cmd_list_element *
792 lookup_cmd (line, list, cmdtype, allow_unknown, ignore_help_classes)
793      char **line;
794      struct cmd_list_element *list;
795      char *cmdtype;
796      int allow_unknown;
797      int ignore_help_classes;
798 {
799   struct cmd_list_element *last_list = 0;
800   struct cmd_list_element *c =
801   lookup_cmd_1 (line, list, &last_list, ignore_help_classes);
802 #if 0
803   /* This is wrong for complete_command.  */
804   char *ptr = (*line) + strlen (*line) - 1;
805
806   /* Clear off trailing whitespace.  */
807   while (ptr >= *line && (*ptr == ' ' || *ptr == '\t'))
808     ptr--;
809   *(ptr + 1) = '\0';
810 #endif
811
812   if (!c)
813     {
814       if (!allow_unknown)
815         {
816           if (!*line)
817             error ("Lack of needed %scommand", cmdtype);
818           else
819             {
820               char *p = *line, *q;
821
822               while (isalnum (*p) || *p == '-')
823                 p++;
824
825               q = (char *) alloca (p - *line + 1);
826               strncpy (q, *line, p - *line);
827               q[p - *line] = '\0';
828               undef_cmd_error (cmdtype, q);
829             }
830         }
831       else
832         return 0;
833     }
834   else if (c == (struct cmd_list_element *) -1)
835     {
836       /* Ambigous.  Local values should be off prefixlist or called
837          values.  */
838       int local_allow_unknown = (last_list ? last_list->allow_unknown :
839                                  allow_unknown);
840       char *local_cmdtype = last_list ? last_list->prefixname : cmdtype;
841       struct cmd_list_element *local_list =
842       (last_list ? *(last_list->prefixlist) : list);
843
844       if (local_allow_unknown < 0)
845         {
846           if (last_list)
847             return last_list;   /* Found something.  */
848           else
849             return 0;           /* Found nothing.  */
850         }
851       else
852         {
853           /* Report as error.  */
854           int amb_len;
855           char ambbuf[100];
856
857           for (amb_len = 0;
858                ((*line)[amb_len] && (*line)[amb_len] != ' '
859                 && (*line)[amb_len] != '\t');
860                amb_len++)
861             ;
862
863           ambbuf[0] = 0;
864           for (c = local_list; c; c = c->next)
865             if (!strncmp (*line, c->name, amb_len))
866               {
867                 if (strlen (ambbuf) + strlen (c->name) + 6 < (int) sizeof ambbuf)
868                   {
869                     if (strlen (ambbuf))
870                       strcat (ambbuf, ", ");
871                     strcat (ambbuf, c->name);
872                   }
873                 else
874                   {
875                     strcat (ambbuf, "..");
876                     break;
877                   }
878               }
879           error ("Ambiguous %scommand \"%s\": %s.", local_cmdtype,
880                  *line, ambbuf);
881           return 0;             /* lint */
882         }
883     }
884   else
885     {
886       /* We've got something.  It may still not be what the caller
887          wants (if this command *needs* a subcommand).  */
888       while (**line == ' ' || **line == '\t')
889         (*line)++;
890
891       if (c->prefixlist && **line && !c->allow_unknown)
892         undef_cmd_error (c->prefixname, *line);
893
894       /* Seems to be what he wants.  Return it.  */
895       return c;
896     }
897   return 0;
898 }
899
900 #if 0
901 /* Look up the contents of *LINE as a command in the command list LIST.
902    LIST is a chain of struct cmd_list_element's.
903    If it is found, return the struct cmd_list_element for that command
904    and update *LINE to point after the command name, at the first argument.
905    If not found, call error if ALLOW_UNKNOWN is zero
906    otherwise (or if error returns) return zero.
907    Call error if specified command is ambiguous,
908    unless ALLOW_UNKNOWN is negative.
909    CMDTYPE precedes the word "command" in the error message.  */
910
911 struct cmd_list_element *
912 lookup_cmd (line, list, cmdtype, allow_unknown)
913      char **line;
914      struct cmd_list_element *list;
915      char *cmdtype;
916      int allow_unknown;
917 {
918   register char *p;
919   register struct cmd_list_element *c, *found;
920   int nfound;
921   char ambbuf[100];
922   char *processed_cmd;
923   int i, cmd_len;
924
925   /* Skip leading whitespace.  */
926
927   while (**line == ' ' || **line == '\t')
928     (*line)++;
929
930   /* Clear out trailing whitespace.  */
931
932   p = *line + strlen (*line);
933   while (p != *line && (p[-1] == ' ' || p[-1] == '\t'))
934     p--;
935   *p = 0;
936
937   /* Find end of command name.  */
938
939   p = *line;
940   while (*p == '-' || isalnum (*p))
941     p++;
942
943   /* Look up the command name.
944      If exact match, keep that.
945      Otherwise, take command abbreviated, if unique.  Note that (in my
946      opinion) a null string does *not* indicate ambiguity; simply the
947      end of the argument.  */
948
949   if (p == *line)
950     {
951       if (!allow_unknown)
952         error ("Lack of needed %scommand", cmdtype);
953       return 0;
954     }
955
956   /* Copy over to a local buffer, converting to lowercase on the way.
957      This is in case the command being parsed is a subcommand which
958      doesn't match anything, and that's ok.  We want the original
959      untouched for the routine of the original command.  */
960
961   processed_cmd = (char *) alloca (p - *line + 1);
962   for (cmd_len = 0; cmd_len < p - *line; cmd_len++)
963     {
964       char x = (*line)[cmd_len];
965       if (isupper (x))
966         processed_cmd[cmd_len] = tolower (x);
967       else
968         processed_cmd[cmd_len] = x;
969     }
970   processed_cmd[cmd_len] = '\0';
971
972   /* Check all possibilities in the current command list.  */
973   found = 0;
974   nfound = 0;
975   for (c = list; c; c = c->next)
976     {
977       if (!strncmp (processed_cmd, c->name, cmd_len))
978         {
979           found = c;
980           nfound++;
981           if (c->name[cmd_len] == 0)
982             {
983               nfound = 1;
984               break;
985             }
986         }
987     }
988
989   /* Report error for undefined command name.  */
990
991   if (nfound != 1)
992     {
993       if (nfound > 1 && allow_unknown >= 0)
994         {
995           ambbuf[0] = 0;
996           for (c = list; c; c = c->next)
997             if (!strncmp (processed_cmd, c->name, cmd_len))
998               {
999                 if (strlen (ambbuf) + strlen (c->name) + 6 < sizeof ambbuf)
1000                   {
1001                     if (strlen (ambbuf))
1002                       strcat (ambbuf, ", ");
1003                     strcat (ambbuf, c->name);
1004                   }
1005                 else
1006                   {
1007                     strcat (ambbuf, "..");
1008                     break;
1009                   }
1010               }
1011           error ("Ambiguous %scommand \"%s\": %s.", cmdtype,
1012                  processed_cmd, ambbuf);
1013         }
1014       else if (!allow_unknown)
1015         error ("Undefined %scommand: \"%s\".", cmdtype, processed_cmd);
1016       return 0;
1017     }
1018
1019   /* Skip whitespace before the argument.  */
1020
1021   while (*p == ' ' || *p == '\t')
1022     p++;
1023   *line = p;
1024
1025   if (found->prefixlist && *p)
1026     {
1027       c = lookup_cmd (line, *found->prefixlist, found->prefixname,
1028                       found->allow_unknown);
1029       if (c)
1030         return c;
1031     }
1032
1033   return found;
1034 }
1035 #endif
1036
1037 /* Helper function for SYMBOL_COMPLETION_FUNCTION.  */
1038
1039 /* Return a vector of char pointers which point to the different
1040    possible completions in LIST of TEXT.  
1041
1042    WORD points in the same buffer as TEXT, and completions should be
1043    returned relative to this position.  For example, suppose TEXT is "foo"
1044    and we want to complete to "foobar".  If WORD is "oo", return
1045    "oobar"; if WORD is "baz/foo", return "baz/foobar".  */
1046
1047 char **
1048 complete_on_cmdlist (list, text, word)
1049      struct cmd_list_element *list;
1050      char *text;
1051      char *word;
1052 {
1053   struct cmd_list_element *ptr;
1054   char **matchlist;
1055   int sizeof_matchlist;
1056   int matches;
1057   int textlen = strlen (text);
1058
1059   sizeof_matchlist = 10;
1060   matchlist = (char **) xmalloc (sizeof_matchlist * sizeof (char *));
1061   matches = 0;
1062
1063   for (ptr = list; ptr; ptr = ptr->next)
1064     if (!strncmp (ptr->name, text, textlen)
1065         && !ptr->abbrev_flag
1066         && (ptr->function.cfunc
1067             || ptr->prefixlist))
1068       {
1069         if (matches == sizeof_matchlist)
1070           {
1071             sizeof_matchlist *= 2;
1072             matchlist = (char **) xrealloc ((char *) matchlist,
1073                                             (sizeof_matchlist
1074                                              * sizeof (char *)));
1075           }
1076
1077         matchlist[matches] = (char *)
1078           xmalloc (strlen (word) + strlen (ptr->name) + 1);
1079         if (word == text)
1080           strcpy (matchlist[matches], ptr->name);
1081         else if (word > text)
1082           {
1083             /* Return some portion of ptr->name.  */
1084             strcpy (matchlist[matches], ptr->name + (word - text));
1085           }
1086         else
1087           {
1088             /* Return some of text plus ptr->name.  */
1089             strncpy (matchlist[matches], word, text - word);
1090             matchlist[matches][text - word] = '\0';
1091             strcat (matchlist[matches], ptr->name);
1092           }
1093         ++matches;
1094       }
1095
1096   if (matches == 0)
1097     {
1098       free ((PTR) matchlist);
1099       matchlist = 0;
1100     }
1101   else
1102     {
1103       matchlist = (char **) xrealloc ((char *) matchlist, ((matches + 1)
1104                                                         * sizeof (char *)));
1105       matchlist[matches] = (char *) 0;
1106     }
1107
1108   return matchlist;
1109 }
1110
1111 /* Helper function for SYMBOL_COMPLETION_FUNCTION.  */
1112
1113 /* Return a vector of char pointers which point to the different
1114    possible completions in CMD of TEXT.  
1115
1116    WORD points in the same buffer as TEXT, and completions should be
1117    returned relative to this position.  For example, suppose TEXT is "foo"
1118    and we want to complete to "foobar".  If WORD is "oo", return
1119    "oobar"; if WORD is "baz/foo", return "baz/foobar".  */
1120
1121 char **
1122 complete_on_enum (enumlist, text, word)
1123      char **enumlist;
1124      char *text;
1125      char *word;
1126 {
1127   char **matchlist;
1128   int sizeof_matchlist;
1129   int matches;
1130   int textlen = strlen (text);
1131   int i;
1132   char *name;
1133
1134   sizeof_matchlist = 10;
1135   matchlist = (char **) xmalloc (sizeof_matchlist * sizeof (char *));
1136   matches = 0;
1137
1138   for (i = 0; (name = enumlist[i]) != NULL; i++)
1139     if (strncmp (name, text, textlen) == 0)
1140       {
1141         if (matches == sizeof_matchlist)
1142           {
1143             sizeof_matchlist *= 2;
1144             matchlist = (char **) xrealloc ((char *) matchlist,
1145                                             (sizeof_matchlist
1146                                              * sizeof (char *)));
1147           }
1148
1149         matchlist[matches] = (char *)
1150           xmalloc (strlen (word) + strlen (name) + 1);
1151         if (word == text)
1152           strcpy (matchlist[matches], name);
1153         else if (word > text)
1154           {
1155             /* Return some portion of name.  */
1156             strcpy (matchlist[matches], name + (word - text));
1157           }
1158         else
1159           {
1160             /* Return some of text plus name.  */
1161             strncpy (matchlist[matches], word, text - word);
1162             matchlist[matches][text - word] = '\0';
1163             strcat (matchlist[matches], name);
1164           }
1165         ++matches;
1166       }
1167
1168   if (matches == 0)
1169     {
1170       free ((PTR) matchlist);
1171       matchlist = 0;
1172     }
1173   else
1174     {
1175       matchlist = (char **) xrealloc ((char *) matchlist, ((matches + 1)
1176                                                         * sizeof (char *)));
1177       matchlist[matches] = (char *) 0;
1178     }
1179
1180   return matchlist;
1181 }
1182
1183 static int
1184 parse_binary_operation (arg)
1185      char *arg;
1186 {
1187   int length;
1188
1189   if (!arg || !*arg)
1190     return 1;
1191
1192   length = strlen (arg);
1193
1194   while (arg[length - 1] == ' ' || arg[length - 1] == '\t')
1195     length--;
1196
1197   if (!strncmp (arg, "on", length)
1198       || !strncmp (arg, "1", length)
1199       || !strncmp (arg, "yes", length))
1200     return 1;
1201   else if (!strncmp (arg, "off", length)
1202            || !strncmp (arg, "0", length)
1203            || !strncmp (arg, "no", length))
1204     return 0;
1205   else
1206     {
1207       error ("\"on\" or \"off\" expected.");
1208       return 0;
1209     }
1210 }
1211
1212 /* Do a "set" or "show" command.  ARG is NULL if no argument, or the text
1213    of the argument, and FROM_TTY is nonzero if this command is being entered
1214    directly by the user (i.e. these are just like any other
1215    command).  C is the command list element for the command.  */
1216 void
1217 do_setshow_command (arg, from_tty, c)
1218      char *arg;
1219      int from_tty;
1220      struct cmd_list_element *c;
1221 {
1222   if (c->type == set_cmd)
1223     {
1224       switch (c->var_type)
1225         {
1226         case var_string:
1227           {
1228             char *new;
1229             char *p;
1230             char *q;
1231             int ch;
1232
1233             if (arg == NULL)
1234               arg = "";
1235             new = (char *) xmalloc (strlen (arg) + 2);
1236             p = arg;
1237             q = new;
1238             while ((ch = *p++) != '\000')
1239               {
1240                 if (ch == '\\')
1241                   {
1242                     /* \ at end of argument is used after spaces
1243                        so they won't be lost.  */
1244                     /* This is obsolete now that we no longer strip
1245                        trailing whitespace and actually, the backslash
1246                        didn't get here in my test, readline or
1247                        something did something funky with a backslash
1248                        right before a newline.  */
1249                     if (*p == 0)
1250                       break;
1251                     ch = parse_escape (&p);
1252                     if (ch == 0)
1253                       break;    /* C loses */
1254                     else if (ch > 0)
1255                       *q++ = ch;
1256                   }
1257                 else
1258                   *q++ = ch;
1259               }
1260 #if 0
1261             if (*(p - 1) != '\\')
1262               *q++ = ' ';
1263 #endif
1264             *q++ = '\0';
1265             new = (char *) xrealloc (new, q - new);
1266             if (*(char **) c->var != NULL)
1267               free (*(char **) c->var);
1268             *(char **) c->var = new;
1269           }
1270           break;
1271         case var_string_noescape:
1272           if (arg == NULL)
1273             arg = "";
1274           if (*(char **) c->var != NULL)
1275             free (*(char **) c->var);
1276           *(char **) c->var = savestring (arg, strlen (arg));
1277           break;
1278         case var_filename:
1279           if (arg == NULL)
1280             error_no_arg ("filename to set it to.");
1281           if (*(char **) c->var != NULL)
1282             free (*(char **) c->var);
1283           *(char **) c->var = tilde_expand (arg);
1284           break;
1285         case var_boolean:
1286           *(int *) c->var = parse_binary_operation (arg);
1287           break;
1288         case var_uinteger:
1289           if (arg == NULL)
1290             error_no_arg ("integer to set it to.");
1291           *(unsigned int *) c->var = parse_and_eval_address (arg);
1292           if (*(unsigned int *) c->var == 0)
1293             *(unsigned int *) c->var = UINT_MAX;
1294           break;
1295         case var_integer:
1296           {
1297             unsigned int val;
1298             if (arg == NULL)
1299               error_no_arg ("integer to set it to.");
1300             val = parse_and_eval_address (arg);
1301             if (val == 0)
1302               *(int *) c->var = INT_MAX;
1303             else if (val >= INT_MAX)
1304               error ("integer %u out of range", val);
1305             else
1306               *(int *) c->var = val;
1307             break;
1308           }
1309         case var_zinteger:
1310           if (arg == NULL)
1311             error_no_arg ("integer to set it to.");
1312           *(int *) c->var = parse_and_eval_address (arg);
1313           break;
1314         case var_enum:
1315           {
1316             int i;
1317             int len;
1318             int nmatches;
1319             char *match = NULL;
1320             char *p;
1321
1322             /* if no argument was supplied, print an informative error message */
1323             if (arg == NULL)
1324               {
1325                 char msg[1024];
1326                 strcpy (msg, "Requires an argument. Valid arguments are ");
1327                 for (i = 0; c->enums[i]; i++)
1328                   {
1329                     if (i != 0)
1330                       strcat (msg, ", ");
1331                     strcat (msg, c->enums[i]);
1332                   }
1333                 strcat (msg, ".");
1334                 error (msg);
1335               }
1336
1337             p = strchr (arg, ' ');
1338
1339             if (p)
1340               len = p - arg;
1341             else
1342               len = strlen (arg);
1343
1344             nmatches = 0;
1345             for (i = 0; c->enums[i]; i++)
1346               if (strncmp (arg, c->enums[i], len) == 0)
1347                 {
1348                   match = c->enums[i];
1349                   nmatches++;
1350                 }
1351
1352             if (nmatches <= 0)
1353               error ("Undefined item: \"%s\".", arg);
1354
1355             if (nmatches > 1)
1356               error ("Ambiguous item \"%s\".", arg);
1357
1358             *(char **) c->var = match;
1359           }
1360           break;
1361         default:
1362           error ("gdb internal error: bad var_type in do_setshow_command");
1363         }
1364     }
1365   else if (c->type == show_cmd)
1366     {
1367 #ifdef UI_OUT
1368       struct cleanup *old_chain;
1369       struct ui_stream *stb;
1370       int quote;
1371
1372       stb = ui_out_stream_new (uiout);
1373       old_chain = make_cleanup ((make_cleanup_func) ui_out_stream_delete, stb);
1374 #endif /* UI_OUT */
1375
1376       /* Print doc minus "show" at start.  */
1377       print_doc_line (gdb_stdout, c->doc + 5);
1378
1379 #ifdef UI_OUT
1380       ui_out_text (uiout, " is ");
1381       ui_out_wrap_hint (uiout, "    ");
1382       quote = 0;
1383       switch (c->var_type)
1384         {
1385         case var_string:
1386           {
1387             unsigned char *p;
1388
1389             if (*(unsigned char **) c->var)
1390               fputstr_filtered (*(unsigned char **) c->var, '"', stb->stream);
1391             quote = 1;
1392           }
1393           break;
1394         case var_string_noescape:
1395         case var_filename:
1396         case var_enum:
1397           if (*(char **) c->var)
1398             fputs_filtered (*(char **) c->var, stb->stream);
1399           quote = 1;
1400           break;
1401         case var_boolean:
1402           fputs_filtered (*(int *) c->var ? "on" : "off", stb->stream);
1403           break;
1404         case var_uinteger:
1405           if (*(unsigned int *) c->var == UINT_MAX)
1406             {
1407               fputs_filtered ("unlimited", stb->stream);
1408               break;
1409             }
1410           /* else fall through */
1411         case var_zinteger:
1412           fprintf_filtered (stb->stream, "%u", *(unsigned int *) c->var);
1413           break;
1414         case var_integer:
1415           if (*(int *) c->var == INT_MAX)
1416             {
1417               fputs_filtered ("unlimited", stb->stream);
1418             }
1419           else
1420             fprintf_filtered (stb->stream, "%d", *(int *) c->var);
1421           break;
1422
1423         default:
1424           error ("gdb internal error: bad var_type in do_setshow_command");
1425         }
1426       if (quote)
1427         ui_out_text (uiout, "\"");
1428       ui_out_field_stream (uiout, "value", stb);
1429       if (quote)
1430         ui_out_text (uiout, "\"");
1431       ui_out_text (uiout, ".\n");
1432       do_cleanups (old_chain);
1433 #else
1434       fputs_filtered (" is ", gdb_stdout);
1435       wrap_here ("    ");
1436       switch (c->var_type)
1437         {
1438         case var_string:
1439           {
1440             fputs_filtered ("\"", gdb_stdout);
1441             if (*(unsigned char **) c->var)
1442               fputstr_filtered (*(unsigned char **) c->var, '"', gdb_stdout);
1443             fputs_filtered ("\"", gdb_stdout);
1444           }
1445           break;
1446         case var_string_noescape:
1447         case var_filename:
1448         case var_enum:
1449           fputs_filtered ("\"", gdb_stdout);
1450           if (*(char **) c->var)
1451             fputs_filtered (*(char **) c->var, gdb_stdout);
1452           fputs_filtered ("\"", gdb_stdout);
1453           break;
1454         case var_boolean:
1455           fputs_filtered (*(int *) c->var ? "on" : "off", gdb_stdout);
1456           break;
1457         case var_uinteger:
1458           if (*(unsigned int *) c->var == UINT_MAX)
1459             {
1460               fputs_filtered ("unlimited", gdb_stdout);
1461               break;
1462             }
1463           /* else fall through */
1464         case var_zinteger:
1465           fprintf_filtered (gdb_stdout, "%u", *(unsigned int *) c->var);
1466           break;
1467         case var_integer:
1468           if (*(int *) c->var == INT_MAX)
1469             {
1470               fputs_filtered ("unlimited", gdb_stdout);
1471             }
1472           else
1473             fprintf_filtered (gdb_stdout, "%d", *(int *) c->var);
1474           break;
1475
1476         default:
1477           error ("gdb internal error: bad var_type in do_setshow_command");
1478         }
1479       fputs_filtered (".\n", gdb_stdout);
1480 #endif
1481     }
1482   else
1483     error ("gdb internal error: bad cmd_type in do_setshow_command");
1484   (*c->function.sfunc) (NULL, from_tty, c);
1485   if (c->type == set_cmd && set_hook)
1486     set_hook (c);
1487 }
1488
1489 /* Show all the settings in a list of show commands.  */
1490
1491 void
1492 cmd_show_list (list, from_tty, prefix)
1493      struct cmd_list_element *list;
1494      int from_tty;
1495      char *prefix;
1496 {
1497 #ifdef UI_OUT
1498   ui_out_list_begin (uiout, "showlist");
1499 #endif
1500   for (; list != NULL; list = list->next)
1501     {
1502       /* If we find a prefix, run its list, prefixing our output by its
1503          prefix (with "show " skipped).  */
1504 #ifdef UI_OUT
1505       if (list->prefixlist && !list->abbrev_flag)
1506         {
1507           ui_out_list_begin (uiout, "optionlist");
1508           ui_out_field_string (uiout, "prefix", list->prefixname + 5);
1509           cmd_show_list (*list->prefixlist, from_tty, list->prefixname + 5);
1510           ui_out_list_end (uiout);
1511         }
1512       if (list->type == show_cmd)
1513         {
1514           ui_out_list_begin (uiout, "option");
1515           ui_out_text (uiout, prefix);
1516           ui_out_field_string (uiout, "name", list->name);
1517           ui_out_text (uiout, ":  ");
1518           do_setshow_command ((char *) NULL, from_tty, list);
1519           ui_out_list_end (uiout);
1520         }
1521 #else
1522       if (list->prefixlist && !list->abbrev_flag)
1523         cmd_show_list (*list->prefixlist, from_tty, list->prefixname + 5);
1524       if (list->type == show_cmd)
1525         {
1526           fputs_filtered (prefix, gdb_stdout);
1527           fputs_filtered (list->name, gdb_stdout);
1528           fputs_filtered (":  ", gdb_stdout);
1529           do_setshow_command ((char *) NULL, from_tty, list);
1530         }
1531 #endif
1532     }
1533 #ifdef UI_OUT
1534   ui_out_list_end (uiout);
1535 #endif
1536 }
1537
1538 /* ARGSUSED */
1539 static void
1540 shell_escape (arg, from_tty)
1541      char *arg;
1542      int from_tty;
1543 {
1544 #ifdef CANT_FORK
1545   /* If ARG is NULL, they want an inferior shell, but `system' just
1546      reports if the shell is available when passed a NULL arg.  */
1547   int rc = system (arg ? arg : "");
1548
1549   if (!arg)
1550     arg = "inferior shell";
1551
1552   if (rc == -1)
1553     {
1554       fprintf_unfiltered (gdb_stderr, "Cannot execute %s: %s\n", arg,
1555                           safe_strerror (errno));
1556       gdb_flush (gdb_stderr);
1557     }
1558   else if (rc)
1559     {
1560       fprintf_unfiltered (gdb_stderr, "%s exited with status %d\n", arg, rc);
1561       gdb_flush (gdb_stderr);
1562     }
1563 #ifdef __DJGPP__
1564   /* Make sure to return to the directory GDB thinks it is, in case the
1565      shell command we just ran changed it.  */
1566   chdir (current_directory);
1567 #endif
1568 #else /* Can fork.  */
1569   int rc, status, pid;
1570   char *p, *user_shell;
1571
1572   if ((user_shell = (char *) getenv ("SHELL")) == NULL)
1573     user_shell = "/bin/sh";
1574
1575   /* Get the name of the shell for arg0 */
1576   if ((p = strrchr (user_shell, '/')) == NULL)
1577     p = user_shell;
1578   else
1579     p++;                        /* Get past '/' */
1580
1581   if ((pid = fork ()) == 0)
1582     {
1583       if (!arg)
1584         execl (user_shell, p, 0);
1585       else
1586         execl (user_shell, p, "-c", arg, 0);
1587
1588       fprintf_unfiltered (gdb_stderr, "Cannot execute %s: %s\n", user_shell,
1589                           safe_strerror (errno));
1590       gdb_flush (gdb_stderr);
1591       _exit (0177);
1592     }
1593
1594   if (pid != -1)
1595     while ((rc = wait (&status)) != pid && rc != -1)
1596       ;
1597   else
1598     error ("Fork failed");
1599 #endif /* Can fork.  */
1600 }
1601
1602 static void
1603 make_command (arg, from_tty)
1604      char *arg;
1605      int from_tty;
1606 {
1607   char *p;
1608
1609   if (arg == 0)
1610     p = "make";
1611   else
1612     {
1613       p = xmalloc (sizeof ("make ") + strlen (arg));
1614       strcpy (p, "make ");
1615       strcpy (p + sizeof ("make ") - 1, arg);
1616     }
1617
1618   shell_escape (p, from_tty);
1619 }
1620
1621 static void
1622 show_user_1 (c, stream)
1623      struct cmd_list_element *c;
1624      struct ui_file *stream;
1625 {
1626   register struct command_line *cmdlines;
1627
1628   cmdlines = c->user_commands;
1629   if (!cmdlines)
1630     return;
1631   fputs_filtered ("User command ", stream);
1632   fputs_filtered (c->name, stream);
1633   fputs_filtered (":\n", stream);
1634
1635 #ifdef UI_OUT
1636   print_command_lines (uiout, cmdlines, 1);
1637   fputs_filtered ("\n", stream);
1638 #else
1639   while (cmdlines)
1640     {
1641       print_command_line (cmdlines, 4, stream);
1642       cmdlines = cmdlines->next;
1643     }
1644   fputs_filtered ("\n", stream);
1645 #endif
1646 }
1647
1648 /* ARGSUSED */
1649 static void
1650 show_user (args, from_tty)
1651      char *args;
1652      int from_tty;
1653 {
1654   struct cmd_list_element *c;
1655   extern struct cmd_list_element *cmdlist;
1656
1657   if (args)
1658     {
1659       c = lookup_cmd (&args, cmdlist, "", 0, 1);
1660       if (c->class != class_user)
1661         error ("Not a user command.");
1662       show_user_1 (c, gdb_stdout);
1663     }
1664   else
1665     {
1666       for (c = cmdlist; c; c = c->next)
1667         {
1668           if (c->class == class_user)
1669             show_user_1 (c, gdb_stdout);
1670         }
1671     }
1672 }
1673
1674 void
1675 _initialize_command ()
1676 {
1677   add_com ("shell", class_support, shell_escape,
1678            "Execute the rest of the line as a shell command.  \n\
1679 With no arguments, run an inferior shell.");
1680
1681   if (xdb_commands)
1682     add_com_alias ("!", "shell", class_support, 0);
1683
1684   add_com ("make", class_support, make_command,
1685        "Run the ``make'' program using the rest of the line as arguments.");
1686   add_cmd ("user", no_class, show_user,
1687            "Show definitions of user defined commands.\n\
1688 Argument is the name of the user defined command.\n\
1689 With no argument, show definitions of all user defined commands.", &showlist);
1690 }