* mn10300.igen (OP_F0F4): Need to load contents of register AN0
[platform/upstream/binutils.git] / gdb / command.c
1 /* Handle lists of commands, their decoding and documentation, for GDB.
2    Copyright 1986, 1989, 1990, 1991 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, Boston, MA 02111-1307, USA.  */
17
18 #include "defs.h"
19 #include "gdbcmd.h"
20 #include "symtab.h"
21 #include "value.h"
22 #include "wait.h"
23 #include <ctype.h>
24 #include "gdb_string.h"
25 #ifdef HAVE_UNISTD_H
26 #include <unistd.h>
27 #endif
28
29 #ifdef HAVE_WAIT_H
30 # include <wait.h>
31 #else
32 # ifdef HAVE_SYS_WAIT_H
33 #  include <sys/wait.h>
34 # endif
35 #endif
36
37 /* Prototypes for local functions */
38
39 static void
40 undef_cmd_error PARAMS ((char *, char *));
41
42 static void
43 show_user PARAMS ((char *, int));
44
45 static void
46 show_user_1 PARAMS ((struct cmd_list_element *, GDB_FILE *));
47
48 static void
49 make_command PARAMS ((char *, int));
50
51 static void
52 shell_escape PARAMS ((char *, int));
53
54 static int
55 parse_binary_operation PARAMS ((char *));
56
57 static void
58 print_doc_line PARAMS ((GDB_FILE *, char *));
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      GDB_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      GDB_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      GDB_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   fputs_filtered (line_buffer, stream);
521 }
522
523 /*
524  * Implement a help command on command list LIST.
525  * RECURSE should be non-zero if this should be done recursively on
526  * all sublists of LIST.
527  * PREFIX is the prefix to print before each command name.
528  * STREAM is the stream upon which the output should be written.
529  * CLASS should be:
530  *      A non-negative class number to list only commands in that
531  * class.
532  *      ALL_COMMANDS to list all commands in list.
533  *      ALL_CLASSES  to list all classes in list.
534  *
535  *   Note that RECURSE will be active on *all* sublists, not just the
536  * ones selected by the criteria above (ie. the selection mechanism
537  * is at the low level, not the high-level).
538  */
539 void
540 help_cmd_list (list, class, prefix, recurse, stream)
541      struct cmd_list_element *list;
542      enum command_class class;
543      char *prefix;
544      int recurse;
545      GDB_FILE *stream;
546 {
547   register struct cmd_list_element *c;
548
549   for (c = list; c; c = c->next)
550     {
551       if (c->abbrev_flag == 0 &&
552           (class == all_commands
553           || (class == all_classes && c->function.cfunc == NULL)
554           || (class == c->class && c->function.cfunc != NULL)))
555         {
556           fprintf_filtered (stream, "%s%s -- ", prefix, c->name);
557           print_doc_line (stream, c->doc);
558           fputs_filtered ("\n", stream);
559         }
560       if (recurse
561           && c->prefixlist != 0
562           && c->abbrev_flag == 0)
563         help_cmd_list (*c->prefixlist, class, c->prefixname, 1, stream);
564     }
565 }
566 \f
567 /* This routine takes a line of TEXT and a CLIST in which to start the
568    lookup.  When it returns it will have incremented the text pointer past
569    the section of text it matched, set *RESULT_LIST to point to the list in
570    which the last word was matched, and will return a pointer to the cmd
571    list element which the text matches.  It will return NULL if no match at
572    all was possible.  It will return -1 (cast appropriately, ick) if ambigous
573    matches are possible; in this case *RESULT_LIST will be set to point to
574    the list in which there are ambiguous choices (and *TEXT will be set to
575    the ambiguous text string).
576
577    If the located command was an abbreviation, this routine returns the base
578    command of the abbreviation.
579
580    It does no error reporting whatsoever; control will always return
581    to the superior routine.
582
583    In the case of an ambiguous return (-1), *RESULT_LIST will be set to point
584    at the prefix_command (ie. the best match) *or* (special case) will be NULL
585    if no prefix command was ever found.  For example, in the case of "info a",
586    "info" matches without ambiguity, but "a" could be "args" or "address", so
587    *RESULT_LIST is set to the cmd_list_element for "info".  So in this case
588    RESULT_LIST should not be interpeted as a pointer to the beginning of a
589    list; it simply points to a specific command.  In the case of an ambiguous
590    return *TEXT is advanced past the last non-ambiguous prefix (e.g.
591    "info t" can be "info types" or "info target"; upon return *TEXT has been
592    advanced past "info ").
593
594    If RESULT_LIST is NULL, don't set *RESULT_LIST (but don't otherwise
595    affect the operation).
596
597    This routine does *not* modify the text pointed to by TEXT.
598    
599    If IGNORE_HELP_CLASSES is nonzero, ignore any command list elements which
600    are actually help classes rather than commands (i.e. the function field of
601    the struct cmd_list_element is NULL).  */
602
603 struct cmd_list_element *
604 lookup_cmd_1 (text, clist, result_list, ignore_help_classes)
605      char **text;
606      struct cmd_list_element *clist, **result_list;
607      int ignore_help_classes;
608 {
609   char *p, *command;
610   int len, tmp, nfound;
611   struct cmd_list_element *found, *c;
612
613   while (**text == ' ' || **text == '\t')
614     (*text)++;
615
616   /* Treating underscores as part of command words is important
617      so that "set args_foo()" doesn't get interpreted as
618      "set args _foo()".  */
619   for (p = *text;
620        *p && (isalnum(*p) || *p == '-' || *p == '_');
621        p++)
622     ;
623
624   /* If nothing but whitespace, return 0.  */
625   if (p == *text)
626     return 0;
627   
628   len = p - *text;
629
630   /* *text and p now bracket the first command word to lookup (and
631      it's length is len).  We copy this into a local temporary,
632      converting to lower case as we go.  */
633
634   command = (char *) alloca (len + 1);
635   for (tmp = 0; tmp < len; tmp++)
636     {
637       char x = (*text)[tmp];
638       command[tmp] = isupper(x) ? tolower(x) : x;
639     }
640   command[len] = '\0';
641
642   /* Look it up.  */
643   found = 0;
644   nfound = 0;
645   for (c = clist; c; c = c->next)
646     if (!strncmp (command, c->name, len)
647         && (!ignore_help_classes || c->function.cfunc))
648       {
649         found = c;
650         nfound++;
651         if (c->name[len] == '\0')
652           {
653             nfound = 1;
654             break;
655           }
656       }
657
658   /* If nothing matches, we have a simple failure.  */
659   if (nfound == 0)
660     return 0;
661
662   if (nfound > 1)
663     {
664       if (result_list != NULL)
665         /* Will be modified in calling routine
666            if we know what the prefix command is.  */
667         *result_list = 0;               
668       return (struct cmd_list_element *) -1; /* Ambiguous.  */
669     }
670
671   /* We've matched something on this list.  Move text pointer forward. */
672
673   *text = p;
674
675   /* If this was an abbreviation, use the base command instead.  */
676
677   if (found->cmd_pointer)
678     found = found->cmd_pointer;
679
680   /* If we found a prefix command, keep looking.  */
681
682   if (found->prefixlist)
683     {
684       c = lookup_cmd_1 (text, *found->prefixlist, result_list,
685                         ignore_help_classes);
686       if (!c)
687         {
688           /* Didn't find anything; this is as far as we got.  */
689           if (result_list != NULL)
690             *result_list = clist;
691           return found;
692         }
693       else if (c == (struct cmd_list_element *) -1)
694         {
695           /* We've gotten this far properly, but the next step
696              is ambiguous.  We need to set the result list to the best
697              we've found (if an inferior hasn't already set it).  */
698           if (result_list != NULL)
699             if (!*result_list)
700               /* This used to say *result_list = *found->prefixlist
701                  If that was correct, need to modify the documentation
702                  at the top of this function to clarify what is supposed
703                  to be going on.  */
704               *result_list = found;
705           return c;
706         }
707       else
708         {
709           /* We matched!  */
710           return c;
711         }
712     }
713   else
714     {
715       if (result_list != NULL)
716         *result_list = clist;
717       return found;
718     }
719 }
720
721 /* All this hair to move the space to the front of cmdtype */
722
723 static void
724 undef_cmd_error (cmdtype, q)
725      char *cmdtype, *q;
726 {
727   error ("Undefined %scommand: \"%s\".  Try \"help%s%.*s\".",
728     cmdtype,
729     q,
730     *cmdtype? " ": "",
731     strlen(cmdtype)-1,
732     cmdtype);
733 }
734
735 /* Look up the contents of *LINE as a command in the command list LIST.
736    LIST is a chain of struct cmd_list_element's.
737    If it is found, return the struct cmd_list_element for that command
738    and update *LINE to point after the command name, at the first argument.
739    If not found, call error if ALLOW_UNKNOWN is zero
740    otherwise (or if error returns) return zero.
741    Call error if specified command is ambiguous,
742    unless ALLOW_UNKNOWN is negative.
743    CMDTYPE precedes the word "command" in the error message.
744
745    If INGNORE_HELP_CLASSES is nonzero, ignore any command list
746    elements which are actually help classes rather than commands (i.e.
747    the function field of the struct cmd_list_element is 0).  */
748
749 struct cmd_list_element *
750 lookup_cmd (line, list, cmdtype, allow_unknown, ignore_help_classes)
751      char **line;
752      struct cmd_list_element *list;
753      char *cmdtype;
754      int allow_unknown;
755      int ignore_help_classes;
756 {
757   struct cmd_list_element *last_list = 0;
758   struct cmd_list_element *c =
759     lookup_cmd_1 (line, list, &last_list, ignore_help_classes);
760 #if 0
761   /* This is wrong for complete_command.  */
762   char *ptr = (*line) + strlen (*line) - 1;
763
764   /* Clear off trailing whitespace.  */
765   while (ptr >= *line && (*ptr == ' ' || *ptr == '\t'))
766     ptr--;
767   *(ptr + 1) = '\0';
768 #endif
769   
770   if (!c)
771     {
772       if (!allow_unknown)
773         {
774           if (!*line)
775             error ("Lack of needed %scommand", cmdtype);
776           else
777             {
778               char *p = *line, *q;
779
780               while (isalnum(*p) || *p == '-')
781                 p++;
782
783               q = (char *) alloca (p - *line + 1);
784               strncpy (q, *line, p - *line);
785               q[p - *line] = '\0';
786               undef_cmd_error (cmdtype, q);
787             }
788         }
789       else
790         return 0;
791     }
792   else if (c == (struct cmd_list_element *) -1)
793     {
794       /* Ambigous.  Local values should be off prefixlist or called
795          values.  */
796       int local_allow_unknown = (last_list ? last_list->allow_unknown :
797                                  allow_unknown);
798       char *local_cmdtype = last_list ? last_list->prefixname : cmdtype;
799       struct cmd_list_element *local_list =
800         (last_list ? *(last_list->prefixlist) : list);
801       
802       if (local_allow_unknown < 0)
803         {
804           if (last_list)
805             return last_list;   /* Found something.  */
806           else
807             return 0;           /* Found nothing.  */
808         }
809       else
810         {
811           /* Report as error.  */
812           int amb_len;
813           char ambbuf[100];
814
815           for (amb_len = 0;
816                ((*line)[amb_len] && (*line)[amb_len] != ' '
817                 && (*line)[amb_len] != '\t');
818                amb_len++)
819             ;
820           
821           ambbuf[0] = 0;
822           for (c = local_list; c; c = c->next)
823             if (!strncmp (*line, c->name, amb_len))
824               {
825                 if (strlen (ambbuf) + strlen (c->name) + 6 < (int)sizeof ambbuf)
826                   {
827                     if (strlen (ambbuf))
828                       strcat (ambbuf, ", ");
829                     strcat (ambbuf, c->name);
830                   }
831                 else
832                   {
833                     strcat (ambbuf, "..");
834                     break;
835                   }
836               }
837           error ("Ambiguous %scommand \"%s\": %s.", local_cmdtype,
838                  *line, ambbuf);
839           return 0;             /* lint */
840         }
841     }
842   else
843     {
844       /* We've got something.  It may still not be what the caller
845          wants (if this command *needs* a subcommand).  */
846       while (**line == ' ' || **line == '\t')
847         (*line)++;
848
849       if (c->prefixlist && **line && !c->allow_unknown)
850         undef_cmd_error (c->prefixname, *line);
851
852       /* Seems to be what he wants.  Return it.  */
853       return c;
854     }
855   return 0;
856 }
857         
858 #if 0
859 /* Look up the contents of *LINE as a command in the command list LIST.
860    LIST is a chain of struct cmd_list_element's.
861    If it is found, return the struct cmd_list_element for that command
862    and update *LINE to point after the command name, at the first argument.
863    If not found, call error if ALLOW_UNKNOWN is zero
864    otherwise (or if error returns) return zero.
865    Call error if specified command is ambiguous,
866    unless ALLOW_UNKNOWN is negative.
867    CMDTYPE precedes the word "command" in the error message.  */
868
869 struct cmd_list_element *
870 lookup_cmd (line, list, cmdtype, allow_unknown)
871      char **line;
872      struct cmd_list_element *list;
873      char *cmdtype;
874      int allow_unknown;
875 {
876   register char *p;
877   register struct cmd_list_element *c, *found;
878   int nfound;
879   char ambbuf[100];
880   char *processed_cmd;
881   int i, cmd_len;
882
883   /* Skip leading whitespace.  */
884
885   while (**line == ' ' || **line == '\t')
886     (*line)++;
887
888   /* Clear out trailing whitespace.  */
889
890   p = *line + strlen (*line);
891   while (p != *line && (p[-1] == ' ' || p[-1] == '\t'))
892     p--;
893   *p = 0;
894
895   /* Find end of command name.  */
896
897   p = *line;
898   while (*p == '-' || isalnum(*p))
899     p++;
900
901   /* Look up the command name.
902      If exact match, keep that.
903      Otherwise, take command abbreviated, if unique.  Note that (in my
904      opinion) a null string does *not* indicate ambiguity; simply the
905      end of the argument.  */
906
907   if (p == *line)
908     {
909       if (!allow_unknown)
910         error ("Lack of needed %scommand", cmdtype);
911       return 0;
912     }
913   
914   /* Copy over to a local buffer, converting to lowercase on the way.
915      This is in case the command being parsed is a subcommand which
916      doesn't match anything, and that's ok.  We want the original
917      untouched for the routine of the original command.  */
918   
919   processed_cmd = (char *) alloca (p - *line + 1);
920   for (cmd_len = 0; cmd_len < p - *line; cmd_len++)
921     {
922       char x = (*line)[cmd_len];
923       if (isupper(x))
924         processed_cmd[cmd_len] = tolower(x);
925       else
926         processed_cmd[cmd_len] = x;
927     }
928   processed_cmd[cmd_len] = '\0';
929
930   /* Check all possibilities in the current command list.  */
931   found = 0;
932   nfound = 0;
933   for (c = list; c; c = c->next)
934     {
935       if (!strncmp (processed_cmd, c->name, cmd_len))
936         {
937           found = c;
938           nfound++;
939           if (c->name[cmd_len] == 0)
940             {
941               nfound = 1;
942               break;
943             }
944         }
945     }
946
947   /* Report error for undefined command name.  */
948
949   if (nfound != 1)
950     {
951       if (nfound > 1 && allow_unknown >= 0)
952         {
953           ambbuf[0] = 0;
954           for (c = list; c; c = c->next)
955             if (!strncmp (processed_cmd, c->name, cmd_len))
956               {
957                 if (strlen (ambbuf) + strlen (c->name) + 6 < sizeof ambbuf)
958                   {
959                     if (strlen (ambbuf))
960                       strcat (ambbuf, ", ");
961                     strcat (ambbuf, c->name);
962                   }
963                 else
964                   {
965                     strcat (ambbuf, "..");
966                     break;
967                   }
968               }
969           error ("Ambiguous %scommand \"%s\": %s.", cmdtype,
970                  processed_cmd, ambbuf);
971         }
972       else if (!allow_unknown)
973         error ("Undefined %scommand: \"%s\".", cmdtype, processed_cmd);
974       return 0;
975     }
976
977   /* Skip whitespace before the argument.  */
978
979   while (*p == ' ' || *p == '\t') p++;
980   *line = p;
981
982   if (found->prefixlist && *p)
983     {
984       c = lookup_cmd (line, *found->prefixlist, found->prefixname,
985                       found->allow_unknown);
986       if (c)
987         return c;
988     }
989
990   return found;
991 }
992 #endif
993
994 /* Helper function for SYMBOL_COMPLETION_FUNCTION.  */
995
996 /* Return a vector of char pointers which point to the different
997    possible completions in LIST of TEXT.  
998
999    WORD points in the same buffer as TEXT, and completions should be
1000    returned relative to this position.  For example, suppose TEXT is "foo"
1001    and we want to complete to "foobar".  If WORD is "oo", return
1002    "oobar"; if WORD is "baz/foo", return "baz/foobar".  */
1003
1004 char **
1005 complete_on_cmdlist (list, text, word)
1006      struct cmd_list_element *list;
1007      char *text;
1008      char *word;
1009 {
1010   struct cmd_list_element *ptr;
1011   char **matchlist;
1012   int sizeof_matchlist;
1013   int matches;
1014   int textlen = strlen (text);
1015
1016   sizeof_matchlist = 10;
1017   matchlist = (char **) xmalloc (sizeof_matchlist * sizeof (char *));
1018   matches = 0;
1019
1020   for (ptr = list; ptr; ptr = ptr->next)
1021     if (!strncmp (ptr->name, text, textlen)
1022         && !ptr->abbrev_flag
1023         && (ptr->function.cfunc
1024             || ptr->prefixlist))
1025       {
1026         if (matches == sizeof_matchlist)
1027           {
1028             sizeof_matchlist *= 2;
1029             matchlist = (char **) xrealloc ((char *)matchlist,
1030                                             (sizeof_matchlist
1031                                              * sizeof (char *)));
1032           }
1033
1034         matchlist[matches] = (char *) 
1035           xmalloc (strlen (word) + strlen (ptr->name) + 1);
1036         if (word == text)
1037           strcpy (matchlist[matches], ptr->name);
1038         else if (word > text)
1039           {
1040             /* Return some portion of ptr->name.  */
1041             strcpy (matchlist[matches], ptr->name + (word - text));
1042           }
1043         else
1044           {
1045             /* Return some of text plus ptr->name.  */
1046             strncpy (matchlist[matches], word, text - word);
1047             matchlist[matches][text - word] = '\0';
1048             strcat (matchlist[matches], ptr->name);
1049           }
1050         ++matches;
1051       }
1052
1053   if (matches == 0)
1054     {
1055       free ((PTR)matchlist);
1056       matchlist = 0;
1057     }
1058   else
1059     {
1060       matchlist = (char **) xrealloc ((char *)matchlist, ((matches + 1)
1061                                                 * sizeof (char *)));
1062       matchlist[matches] = (char *) 0;
1063     }
1064
1065   return matchlist;
1066 }
1067
1068 /* Helper function for SYMBOL_COMPLETION_FUNCTION.  */
1069
1070 /* Return a vector of char pointers which point to the different
1071    possible completions in CMD of TEXT.  
1072
1073    WORD points in the same buffer as TEXT, and completions should be
1074    returned relative to this position.  For example, suppose TEXT is "foo"
1075    and we want to complete to "foobar".  If WORD is "oo", return
1076    "oobar"; if WORD is "baz/foo", return "baz/foobar".  */
1077
1078 char **
1079 complete_on_enum (enumlist, text, word)
1080      char **enumlist;
1081      char *text;
1082      char *word;
1083 {
1084   char **matchlist;
1085   int sizeof_matchlist;
1086   int matches;
1087   int textlen = strlen (text);
1088   int i;
1089   char *name;
1090
1091   sizeof_matchlist = 10;
1092   matchlist = (char **) xmalloc (sizeof_matchlist * sizeof (char *));
1093   matches = 0;
1094
1095   for (i = 0; (name = enumlist[i]) != NULL; i++)
1096     if (strncmp (name, text, textlen) == 0)
1097       {
1098         if (matches == sizeof_matchlist)
1099           {
1100             sizeof_matchlist *= 2;
1101             matchlist = (char **) xrealloc ((char *)matchlist,
1102                                             (sizeof_matchlist
1103                                              * sizeof (char *)));
1104           }
1105
1106         matchlist[matches] = (char *) 
1107           xmalloc (strlen (word) + strlen (name) + 1);
1108         if (word == text)
1109           strcpy (matchlist[matches], name);
1110         else if (word > text)
1111           {
1112             /* Return some portion of name.  */
1113             strcpy (matchlist[matches], name + (word - text));
1114           }
1115         else
1116           {
1117             /* Return some of text plus name.  */
1118             strncpy (matchlist[matches], word, text - word);
1119             matchlist[matches][text - word] = '\0';
1120             strcat (matchlist[matches], name);
1121           }
1122         ++matches;
1123       }
1124
1125   if (matches == 0)
1126     {
1127       free ((PTR)matchlist);
1128       matchlist = 0;
1129     }
1130   else
1131     {
1132       matchlist = (char **) xrealloc ((char *)matchlist, ((matches + 1)
1133                                                 * sizeof (char *)));
1134       matchlist[matches] = (char *) 0;
1135     }
1136
1137   return matchlist;
1138 }
1139
1140 static int
1141 parse_binary_operation (arg)
1142      char *arg;
1143 {
1144   int length;
1145
1146   if (!arg || !*arg)
1147     return 1;
1148
1149   length = strlen (arg);
1150
1151   while (arg[length - 1] == ' ' || arg[length - 1] == '\t')
1152     length--;
1153
1154   if (!strncmp (arg, "on", length)
1155       || !strncmp (arg, "1", length)
1156       || !strncmp (arg, "yes", length))
1157     return 1;
1158   else
1159     if (!strncmp (arg, "off", length)
1160         || !strncmp (arg, "0", length)
1161         || !strncmp (arg, "no", length))
1162       return 0;
1163     else 
1164       {
1165         error ("\"on\" or \"off\" expected.");
1166         return 0;
1167       }
1168 }
1169
1170 /* Do a "set" or "show" command.  ARG is NULL if no argument, or the text
1171    of the argument, and FROM_TTY is nonzero if this command is being entered
1172    directly by the user (i.e. these are just like any other
1173    command).  C is the command list element for the command.  */
1174 void
1175 do_setshow_command (arg, from_tty, c)
1176      char *arg;
1177      int from_tty;
1178      struct cmd_list_element *c;
1179 {
1180   if (c->type == set_cmd)
1181     {
1182       switch (c->var_type)
1183         {
1184         case var_string:
1185           {
1186             char *new;
1187             char *p;
1188             char *q;
1189             int ch;
1190             
1191             if (arg == NULL)
1192               arg = "";
1193             new = (char *) xmalloc (strlen (arg) + 2);
1194             p = arg; q = new;
1195             while ((ch = *p++) != '\000')
1196               {
1197                 if (ch == '\\')
1198                   {
1199                     /* \ at end of argument is used after spaces
1200                        so they won't be lost.  */
1201                     /* This is obsolete now that we no longer strip
1202                        trailing whitespace and actually, the backslash
1203                        didn't get here in my test, readline or
1204                        something did something funky with a backslash
1205                        right before a newline.  */
1206                     if (*p == 0)
1207                       break;
1208                     ch = parse_escape (&p);
1209                     if (ch == 0)
1210                       break; /* C loses */
1211                     else if (ch > 0)
1212                       *q++ = ch;
1213                   }
1214                 else
1215                   *q++ = ch;
1216               }
1217 #if 0
1218             if (*(p - 1) != '\\')
1219               *q++ = ' ';
1220 #endif
1221             *q++ = '\0';
1222             new = (char *) xrealloc (new, q - new);
1223             if (*(char **)c->var != NULL)
1224               free (*(char **)c->var);
1225             *(char **) c->var = new;
1226           }
1227           break;
1228         case var_string_noescape:
1229           if (arg == NULL)
1230             arg = "";
1231           if (*(char **)c->var != NULL)
1232             free (*(char **)c->var);
1233           *(char **) c->var = savestring (arg, strlen (arg));
1234           break;
1235         case var_filename:
1236           if (arg == NULL)
1237             error_no_arg ("filename to set it to.");
1238           if (*(char **)c->var != NULL)
1239             free (*(char **)c->var);
1240           *(char **)c->var = tilde_expand (arg);
1241           break;
1242         case var_boolean:
1243           *(int *) c->var = parse_binary_operation (arg);
1244           break;
1245         case var_uinteger:
1246           if (arg == NULL)
1247             error_no_arg ("integer to set it to.");
1248           *(unsigned int *) c->var = parse_and_eval_address (arg);
1249           if (*(unsigned int *) c->var == 0)
1250             *(unsigned int *) c->var = UINT_MAX;
1251           break;
1252         case var_integer:
1253           {
1254             unsigned int val;
1255             if (arg == NULL)
1256               error_no_arg ("integer to set it to.");
1257             val = parse_and_eval_address (arg);
1258             if (val == 0)
1259               *(int *) c->var = INT_MAX;
1260             else if (val >= INT_MAX)
1261               error ("integer %u out of range", val);
1262             else
1263               *(int *) c->var = val;
1264             break;
1265           }
1266         case var_zinteger:
1267           if (arg == NULL)
1268             error_no_arg ("integer to set it to.");
1269           *(int *) c->var = parse_and_eval_address (arg);
1270           break;
1271         case var_enum:
1272           {
1273             int i;
1274             int len;
1275             int nmatches;
1276             char *match = NULL;
1277             char *p;
1278
1279             /* if no argument was supplied, print an informative error message */
1280             if (arg == NULL)
1281               {
1282                 char msg[1024];
1283                 strcpy (msg, "Requires an argument. Valid arguments are ");
1284                 for (i = 0; c->enums[i]; i++)
1285                   {
1286                     if (i != 0)
1287                       strcat (msg, ", ");
1288                     strcat (msg, c->enums[i]);
1289                   }
1290                 strcat (msg, ".");
1291                 error (msg);
1292               }
1293
1294             p = strchr (arg, ' ');
1295             
1296             if (p)
1297               len = p - arg;
1298             else
1299               len = strlen (arg);
1300
1301             nmatches = 0;
1302             for (i = 0; c->enums[i]; i++)
1303               if (strncmp (arg, c->enums[i], len) == 0)
1304                 {
1305                   match = c->enums[i];
1306                   nmatches++;
1307                 }
1308
1309             if (nmatches <= 0)
1310               error ("Undefined item: \"%s\".", arg);
1311
1312             if (nmatches > 1)
1313               error ("Ambiguous item \"%s\".", arg);
1314
1315             *(char **)c->var = match;
1316           }
1317           break;
1318         default:
1319           error ("gdb internal error: bad var_type in do_setshow_command");
1320         }
1321     }
1322   else if (c->type == show_cmd)
1323     {
1324       /* Print doc minus "show" at start.  */
1325       print_doc_line (gdb_stdout, c->doc + 5);
1326       
1327       fputs_filtered (" is ", gdb_stdout);
1328       wrap_here ("    ");
1329       switch (c->var_type)
1330         {
1331       case var_string:
1332         {
1333           unsigned char *p;
1334
1335           fputs_filtered ("\"", gdb_stdout);
1336           if (*(unsigned char **)c->var)
1337             for (p = *(unsigned char **) c->var; *p != '\0'; p++)
1338               gdb_printchar (*p, gdb_stdout, '"');
1339           fputs_filtered ("\"", gdb_stdout);
1340         }
1341         break;
1342       case var_string_noescape:
1343       case var_filename:
1344       case var_enum:
1345         fputs_filtered ("\"", gdb_stdout);
1346         if (*(char **)c->var)
1347           fputs_filtered (*(char **) c->var, gdb_stdout);
1348         fputs_filtered ("\"", gdb_stdout);
1349         break;
1350       case var_boolean:
1351         fputs_filtered (*(int *) c->var ? "on" : "off", gdb_stdout);
1352         break;
1353       case var_uinteger:
1354         if (*(unsigned int *) c->var == UINT_MAX) {
1355           fputs_filtered ("unlimited", gdb_stdout);
1356           break;
1357         }
1358         /* else fall through */
1359       case var_zinteger:
1360         fprintf_filtered (gdb_stdout, "%u", *(unsigned int *) c->var);
1361         break;
1362       case var_integer:
1363         if (*(int *) c->var == INT_MAX)
1364           {
1365             fputs_filtered ("unlimited", gdb_stdout);
1366           }
1367         else
1368           fprintf_filtered (gdb_stdout, "%d", *(int *) c->var);
1369         break;
1370             
1371       default:
1372         error ("gdb internal error: bad var_type in do_setshow_command");
1373       }
1374       fputs_filtered (".\n", gdb_stdout);
1375     }
1376   else
1377     error ("gdb internal error: bad cmd_type in do_setshow_command");
1378   (*c->function.sfunc) (NULL, from_tty, c);
1379 }
1380
1381 /* Show all the settings in a list of show commands.  */
1382
1383 void
1384 cmd_show_list (list, from_tty, prefix)
1385      struct cmd_list_element *list;
1386      int from_tty;
1387      char *prefix;
1388 {
1389   for (; list != NULL; list = list->next) {
1390     /* If we find a prefix, run its list, prefixing our output by its
1391        prefix (with "show " skipped).  */
1392     if (list->prefixlist && !list->abbrev_flag)
1393       cmd_show_list (*list->prefixlist, from_tty, list->prefixname + 5);
1394     if (list->type == show_cmd)
1395       {
1396         fputs_filtered (prefix, gdb_stdout);
1397         fputs_filtered (list->name, gdb_stdout);
1398         fputs_filtered (":  ", gdb_stdout);
1399         do_setshow_command ((char *)NULL, from_tty, list);
1400       }
1401   }
1402 }
1403
1404 /* ARGSUSED */
1405 static void
1406 shell_escape (arg, from_tty)
1407      char *arg;
1408      int from_tty;
1409 {
1410 #ifdef CANT_FORK
1411   /* FIXME: what about errors (I don't know how GO32 system() handles
1412      them)?  */
1413   system (arg);
1414 #else /* Can fork.  */
1415   int rc, status, pid;
1416   char *p, *user_shell;
1417
1418   if ((user_shell = (char *) getenv ("SHELL")) == NULL)
1419     user_shell = "/bin/sh";
1420
1421   /* Get the name of the shell for arg0 */
1422   if ((p = strrchr (user_shell, '/')) == NULL)
1423     p = user_shell;
1424   else
1425     p++;                        /* Get past '/' */
1426
1427   if ((pid = fork()) == 0)
1428     {
1429       if (!arg)
1430         execl (user_shell, p, 0);
1431       else
1432         execl (user_shell, p, "-c", arg, 0);
1433
1434       fprintf_unfiltered (gdb_stderr, "Cannot execute %s: %s\n", user_shell,
1435                           safe_strerror (errno));
1436       gdb_flush (gdb_stderr);
1437       _exit (0177);
1438     }
1439
1440   if (pid != -1)
1441     while ((rc = wait (&status)) != pid && rc != -1)
1442       ;
1443   else
1444     error ("Fork failed");
1445 #endif /* Can fork.  */
1446 }
1447
1448 static void
1449 make_command (arg, from_tty)
1450      char *arg;
1451      int from_tty;
1452 {
1453   char *p;
1454
1455   if (arg == 0)
1456     p = "make";
1457   else
1458     {
1459       p = xmalloc (sizeof("make ") + strlen(arg));
1460       strcpy (p, "make ");
1461       strcpy (p + sizeof("make ")-1, arg);
1462     }
1463   
1464   shell_escape (p, from_tty);
1465 }
1466
1467 static void
1468 show_user_1 (c, stream)
1469      struct cmd_list_element *c;
1470      GDB_FILE *stream;
1471 {
1472   register struct command_line *cmdlines;
1473
1474   cmdlines = c->user_commands;
1475   if (!cmdlines)
1476     return;
1477   fputs_filtered ("User command ", stream);
1478   fputs_filtered (c->name, stream);
1479   fputs_filtered (":\n", stream);
1480
1481   while (cmdlines)
1482     {
1483       print_command_line (cmdlines, 4);
1484       cmdlines = cmdlines->next;
1485     }
1486   fputs_filtered ("\n", stream);
1487 }
1488
1489 /* ARGSUSED */
1490 static void
1491 show_user (args, from_tty)
1492      char *args;
1493      int from_tty;
1494 {
1495   struct cmd_list_element *c;
1496   extern struct cmd_list_element *cmdlist;
1497
1498   if (args)
1499     {
1500       c = lookup_cmd (&args, cmdlist, "", 0, 1);
1501       if (c->class != class_user)
1502         error ("Not a user command.");
1503       show_user_1 (c, gdb_stdout);
1504     }
1505   else
1506     {
1507       for (c = cmdlist; c; c = c->next)
1508         {
1509           if (c->class == class_user)
1510             show_user_1 (c, gdb_stdout);
1511         }
1512     }
1513 }
1514
1515 void
1516 _initialize_command ()
1517 {
1518   add_com ("shell", class_support, shell_escape,
1519            "Execute the rest of the line as a shell command.  \n\
1520 With no arguments, run an inferior shell.");
1521   add_com ("make", class_support, make_command,
1522            "Run the ``make'' program using the rest of the line as arguments.");
1523   add_cmd ("user", no_class, show_user, 
1524            "Show definitions of user defined commands.\n\
1525 Argument is the name of the user defined command.\n\
1526 With no argument, show definitions of all user defined commands.", &showlist);
1527 }