* command.c (add_set_cmd, no_just_help_class_command): Change
[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., 675 Mass Ave, Cambridge, MA 02139, USA.  */
17
18 #include "defs.h"
19 #include "gdbcmd.h"
20 #include "symtab.h"
21 #include "value.h"
22 #include <ctype.h>
23 #include <string.h>
24
25 /* Prototypes for local functions */
26
27 static void
28 undef_cmd_error PARAMS ((char *, char *));
29
30 static void
31 show_user PARAMS ((char *, int));
32
33 static void
34 show_user_1 PARAMS ((struct cmd_list_element *, GDB_FILE *));
35
36 static void
37 make_command PARAMS ((char *, int));
38
39 static void
40 shell_escape PARAMS ((char *, int));
41
42 static int
43 parse_binary_operation PARAMS ((char *));
44
45 static void
46 print_doc_line PARAMS ((GDB_FILE *, char *));
47
48 /* Add element named NAME.
49    CLASS is the top level category into which commands are broken down
50    for "help" purposes.
51    FUN should be the function to execute the command;
52    it will get a character string as argument, with leading
53    and trailing blanks already eliminated.
54
55    DOC is a documentation string for the command.
56    Its first line should be a complete sentence.
57    It should start with ? for a command that is an abbreviation
58    or with * for a command that most users don't need to know about.
59
60    Add this command to command list *LIST.  */
61
62 struct cmd_list_element *
63 add_cmd (name, class, fun, doc, list)
64      char *name;
65      enum command_class class;
66      void (*fun) PARAMS ((char *, int));
67      char *doc;
68      struct cmd_list_element **list;
69 {
70   register struct cmd_list_element *c
71     = (struct cmd_list_element *) xmalloc (sizeof (struct cmd_list_element));
72
73   delete_cmd (name, list);
74   c->next = *list;
75   c->name = name;
76   c->class = class;
77   c->function.cfunc = fun;
78   c->doc = doc;
79   c->prefixlist = 0;
80   c->prefixname = (char *)NULL;
81   c->allow_unknown = 0;
82   c->hook = 0;
83   c->hookee = 0;
84   c->cmd_pointer = 0;
85   c->abbrev_flag = 0;
86   c->type = not_set_cmd;
87   c->completer = make_symbol_completion_list;
88   c->var = 0;
89   c->var_type = var_boolean;
90   c->user_commands = 0;
91   *list = c;
92   return c;
93 }
94
95 /* Same as above, except that the abbrev_flag is set. */
96
97 #if 0   /* Currently unused */
98
99 struct cmd_list_element *
100 add_abbrev_cmd (name, class, fun, doc, list)
101      char *name;
102      enum command_class class;
103      void (*fun) PARAMS ((char *, int));
104      char *doc;
105      struct cmd_list_element **list;
106 {
107   register struct cmd_list_element *c
108     = add_cmd (name, class, fun, doc, list);
109
110   c->abbrev_flag = 1;
111   return c;
112 }
113
114 #endif
115
116 struct cmd_list_element *
117 add_alias_cmd (name, oldname, class, abbrev_flag, list)
118      char *name;
119      char *oldname;
120      enum command_class class;
121      int abbrev_flag;
122      struct cmd_list_element **list;
123 {
124   /* Must do this since lookup_cmd tries to side-effect its first arg */
125   char *copied_name;
126   register struct cmd_list_element *old;
127   register struct cmd_list_element *c;
128   copied_name = (char *) alloca (strlen (oldname) + 1);
129   strcpy (copied_name, oldname);
130   old  = lookup_cmd (&copied_name, *list, "", 1, 1);
131
132   if (old == 0)
133     {
134       delete_cmd (name, list);
135       return 0;
136     }
137
138   c = add_cmd (name, class, old->function.cfunc, old->doc, list);
139   c->prefixlist = old->prefixlist;
140   c->prefixname = old->prefixname;
141   c->allow_unknown = old->allow_unknown;
142   c->abbrev_flag = abbrev_flag;
143   c->cmd_pointer = old;
144   return c;
145 }
146
147 /* Like add_cmd but adds an element for a command prefix:
148    a name that should be followed by a subcommand to be looked up
149    in another command list.  PREFIXLIST should be the address
150    of the variable containing that list.  */
151
152 struct cmd_list_element *
153 add_prefix_cmd (name, class, fun, doc, prefixlist, prefixname,
154                 allow_unknown, list)
155      char *name;
156      enum command_class class;
157      void (*fun) PARAMS ((char *, int));
158      char *doc;
159      struct cmd_list_element **prefixlist;
160      char *prefixname;
161      int allow_unknown;
162      struct cmd_list_element **list;
163 {
164   register struct cmd_list_element *c = add_cmd (name, class, fun, doc, list);
165   c->prefixlist = prefixlist;
166   c->prefixname = prefixname;
167   c->allow_unknown = allow_unknown;
168   return c;
169 }
170
171 /* Like add_prefix_cmd but sets the abbrev_flag on the new command. */
172    
173 struct cmd_list_element *
174 add_abbrev_prefix_cmd (name, class, fun, doc, prefixlist, prefixname,
175                        allow_unknown, list)
176      char *name;
177      enum command_class class;
178      void (*fun) PARAMS ((char *, int));
179      char *doc;
180      struct cmd_list_element **prefixlist;
181      char *prefixname;
182      int allow_unknown;
183      struct cmd_list_element **list;
184 {
185   register struct cmd_list_element *c = add_cmd (name, class, fun, doc, list);
186   c->prefixlist = prefixlist;
187   c->prefixname = prefixname;
188   c->allow_unknown = allow_unknown;
189   c->abbrev_flag = 1;
190   return c;
191 }
192
193 /* ARGSUSED */
194 void
195 not_just_help_class_command (args, from_tty, c)
196      char *args;
197      int from_tty;
198      struct cmd_list_element *c;
199 {
200 }
201
202 /* Add element named NAME to command list LIST (the list for set
203    or some sublist thereof).
204    CLASS is as in add_cmd.
205    VAR_TYPE is the kind of thing we are setting.
206    VAR is address of the variable being controlled by this command.
207    DOC is the documentation string.  */
208
209 struct cmd_list_element *
210 add_set_cmd (name, class, var_type, var, doc, list)
211      char *name;
212      enum command_class class;
213      var_types var_type;
214      char *var;
215      char *doc;
216      struct cmd_list_element **list;
217 {
218   struct cmd_list_element *c
219     = add_cmd (name, class, NO_FUNCTION, doc, list);
220
221   c->type = set_cmd;
222   c->var_type = var_type;
223   c->var = var;
224   /* This needs to be something besides NO_FUNCTION so that this isn't
225      treated as a help class.  */
226   c->function.sfunc = not_just_help_class_command;
227   return c;
228 }
229
230 /* Where SETCMD has already been added, add the corresponding show
231    command to LIST and return a pointer to it.  */
232 struct cmd_list_element *
233 add_show_from_set (setcmd, list)
234      struct cmd_list_element *setcmd;
235      struct cmd_list_element **list;
236 {
237   struct cmd_list_element *showcmd =
238     (struct cmd_list_element *) xmalloc (sizeof (struct cmd_list_element));
239
240   memcpy (showcmd, setcmd, sizeof (struct cmd_list_element));
241   delete_cmd (showcmd->name, list);
242   showcmd->type = show_cmd;
243   
244   /* Replace "set " at start of docstring with "show ".  */
245   if (setcmd->doc[0] == 'S' && setcmd->doc[1] == 'e'
246       && setcmd->doc[2] == 't' && setcmd->doc[3] == ' ')
247     showcmd->doc = concat ("Show ", setcmd->doc + 4, NULL);
248   else
249     fprintf_unfiltered (gdb_stderr, "GDB internal error: Bad docstring for set command\n");
250   
251   showcmd->next = *list;
252   *list = showcmd;
253   return showcmd;
254 }
255
256 /* Remove the command named NAME from the command list.  */
257
258 void
259 delete_cmd (name, list)
260      char *name;
261      struct cmd_list_element **list;
262 {
263   register struct cmd_list_element *c;
264   struct cmd_list_element *p;
265
266   while (*list && STREQ ((*list)->name, name))
267     {
268       if ((*list)->hookee)
269         (*list)->hookee->hook = 0;      /* Hook slips out of its mouth */
270       p = (*list)->next;
271       free ((PTR)*list);
272       *list = p;
273     }
274
275   if (*list)
276     for (c = *list; c->next;)
277       {
278         if (STREQ (c->next->name, name))
279           {
280             if (c->next->hookee)
281               c->next->hookee->hook = 0;  /* hooked cmd gets away.  */
282             p = c->next->next;
283             free ((PTR)c->next);
284             c->next = p;
285           }
286         else
287           c = c->next;
288       }
289 }
290
291 /* This command really has to deal with two things:
292  *     1) I want documentation on *this string* (usually called by
293  * "help commandname").
294  *     2) I want documentation on *this list* (usually called by
295  * giving a command that requires subcommands.  Also called by saying
296  * just "help".)
297  *
298  *   I am going to split this into two seperate comamnds, help_cmd and
299  * help_list. 
300  */
301
302 void
303 help_cmd (command, stream)
304      char *command;
305      GDB_FILE *stream;
306 {
307   struct cmd_list_element *c;
308   extern struct cmd_list_element *cmdlist;
309
310   if (!command)
311     {
312       help_list (cmdlist, "", all_classes, stream);
313       return;
314     }
315
316   c = lookup_cmd (&command, cmdlist, "", 0, 0);
317
318   if (c == 0)
319     return;
320
321   /* There are three cases here.
322      If c->prefixlist is nonzero, we have a prefix command.
323      Print its documentation, then list its subcommands.
324      
325      If c->function is nonzero, we really have a command.
326      Print its documentation and return.
327      
328      If c->function is zero, we have a class name.
329      Print its documentation (as if it were a command)
330      and then set class to the number of this class
331      so that the commands in the class will be listed.  */
332
333   fputs_filtered (c->doc, stream);
334   fputs_filtered ("\n", stream);
335
336   if (c->prefixlist == 0 && c->function.cfunc != NULL)
337     return;
338   fprintf_filtered (stream, "\n");
339
340   /* If this is a prefix command, print it's subcommands */
341   if (c->prefixlist)
342     help_list (*c->prefixlist, c->prefixname, all_commands, stream);
343
344   /* If this is a class name, print all of the commands in the class */
345   if (c->function.cfunc == NULL)
346     help_list (cmdlist, "", c->class, stream);
347
348   if (c->hook)
349     fprintf_filtered (stream, "\nThis command has a hook defined: %s\n",
350                       c->hook->name);
351 }
352
353 /*
354  * Get a specific kind of help on a command list.
355  *
356  * LIST is the list.
357  * CMDTYPE is the prefix to use in the title string.
358  * CLASS is the class with which to list the nodes of this list (see
359  * documentation for help_cmd_list below),  As usual, ALL_COMMANDS for
360  * everything, ALL_CLASSES for just classes, and non-negative for only things
361  * in a specific class.
362  * and STREAM is the output stream on which to print things.
363  * If you call this routine with a class >= 0, it recurses.
364  */
365 void
366 help_list (list, cmdtype, class, stream)
367      struct cmd_list_element *list;
368      char *cmdtype;
369      enum command_class class;
370      GDB_FILE *stream;
371 {
372   int len;
373   char *cmdtype1, *cmdtype2;
374   
375   /* If CMDTYPE is "foo ", CMDTYPE1 gets " foo" and CMDTYPE2 gets "foo sub"  */
376   len = strlen (cmdtype);
377   cmdtype1 = (char *) alloca (len + 1);
378   cmdtype1[0] = 0;
379   cmdtype2 = (char *) alloca (len + 4);
380   cmdtype2[0] = 0;
381   if (len)
382     {
383       cmdtype1[0] = ' ';
384       strncpy (cmdtype1 + 1, cmdtype, len - 1);
385       cmdtype1[len] = 0;
386       strncpy (cmdtype2, cmdtype, len - 1);
387       strcpy (cmdtype2 + len - 1, " sub");
388     }
389
390   if (class == all_classes)
391     fprintf_filtered (stream, "List of classes of %scommands:\n\n", cmdtype2);
392   else
393     fprintf_filtered (stream, "List of %scommands:\n\n", cmdtype2);
394
395   help_cmd_list (list, class, cmdtype, (int)class >= 0, stream);
396
397   if (class == all_classes)
398     fprintf_filtered (stream, "\n\
399 Type \"help%s\" followed by a class name for a list of commands in that class.",
400              cmdtype1);
401
402   fprintf_filtered (stream, "\n\
403 Type \"help%s\" followed by %scommand name for full documentation.\n\
404 Command name abbreviations are allowed if unambiguous.\n",
405            cmdtype1, cmdtype2);
406 }
407      
408 /* Print only the first line of STR on STREAM.  */
409 static void
410 print_doc_line (stream, str)
411      GDB_FILE *stream;
412      char *str;
413 {
414   static char *line_buffer = 0;
415   static int line_size;
416   register char *p;
417
418   if (!line_buffer)
419     {
420       line_size = 80;
421       line_buffer = (char *) xmalloc (line_size);
422     }
423
424   p = str;
425   while (*p && *p != '\n' && *p != '.' && *p != ',')
426     p++;
427   if (p - str > line_size - 1)
428     {
429       line_size = p - str + 1;
430       free ((PTR)line_buffer);
431       line_buffer = (char *) xmalloc (line_size);
432     }
433   strncpy (line_buffer, str, p - str);
434   line_buffer[p - str] = '\0';
435   if (islower (line_buffer[0]))
436     line_buffer[0] = toupper (line_buffer[0]);
437   fputs_filtered (line_buffer, stream);
438 }
439
440 /*
441  * Implement a help command on command list LIST.
442  * RECURSE should be non-zero if this should be done recursively on
443  * all sublists of LIST.
444  * PREFIX is the prefix to print before each command name.
445  * STREAM is the stream upon which the output should be written.
446  * CLASS should be:
447  *      A non-negative class number to list only commands in that
448  * class.
449  *      ALL_COMMANDS to list all commands in list.
450  *      ALL_CLASSES  to list all classes in list.
451  *
452  *   Note that RECURSE will be active on *all* sublists, not just the
453  * ones selected by the criteria above (ie. the selection mechanism
454  * is at the low level, not the high-level).
455  */
456 void
457 help_cmd_list (list, class, prefix, recurse, stream)
458      struct cmd_list_element *list;
459      enum command_class class;
460      char *prefix;
461      int recurse;
462      GDB_FILE *stream;
463 {
464   register struct cmd_list_element *c;
465
466   for (c = list; c; c = c->next)
467     {
468       if (c->abbrev_flag == 0 &&
469           (class == all_commands
470           || (class == all_classes && c->function.cfunc == NULL)
471           || (class == c->class && c->function.cfunc != NULL)))
472         {
473           fprintf_filtered (stream, "%s%s -- ", prefix, c->name);
474           print_doc_line (stream, c->doc);
475           fputs_filtered ("\n", stream);
476         }
477       if (recurse
478           && c->prefixlist != 0
479           && c->abbrev_flag == 0)
480         help_cmd_list (*c->prefixlist, class, c->prefixname, 1, stream);
481     }
482 }
483 \f
484 /* This routine takes a line of TEXT and a CLIST in which to start the
485    lookup.  When it returns it will have incremented the text pointer past
486    the section of text it matched, set *RESULT_LIST to point to the list in
487    which the last word was matched, and will return a pointer to the cmd
488    list element which the text matches.  It will return NULL if no match at
489    all was possible.  It will return -1 (cast appropriately, ick) if ambigous
490    matches are possible; in this case *RESULT_LIST will be set to point to
491    the list in which there are ambiguous choices (and *TEXT will be set to
492    the ambiguous text string).
493
494    If the located command was an abbreviation, this routine returns the base
495    command of the abbreviation.
496
497    It does no error reporting whatsoever; control will always return
498    to the superior routine.
499
500    In the case of an ambiguous return (-1), *RESULT_LIST will be set to point
501    at the prefix_command (ie. the best match) *or* (special case) will be NULL
502    if no prefix command was ever found.  For example, in the case of "info a",
503    "info" matches without ambiguity, but "a" could be "args" or "address", so
504    *RESULT_LIST is set to the cmd_list_element for "info".  So in this case
505    RESULT_LIST should not be interpeted as a pointer to the beginning of a
506    list; it simply points to a specific command.  In the case of an ambiguous
507    return *TEXT is advanced past the last non-ambiguous prefix (e.g.
508    "info t" can be "info types" or "info target"; upon return *TEXT has been
509    advanced past "info ").
510
511    If RESULT_LIST is NULL, don't set *RESULT_LIST (but don't otherwise
512    affect the operation).
513
514    This routine does *not* modify the text pointed to by TEXT.
515    
516    If IGNORE_HELP_CLASSES is nonzero, ignore any command list elements which
517    are actually help classes rather than commands (i.e. the function field of
518    the struct cmd_list_element is NULL).  */
519
520 struct cmd_list_element *
521 lookup_cmd_1 (text, clist, result_list, ignore_help_classes)
522      char **text;
523      struct cmd_list_element *clist, **result_list;
524      int ignore_help_classes;
525 {
526   char *p, *command;
527   int len, tmp, nfound;
528   struct cmd_list_element *found, *c;
529
530   while (**text == ' ' || **text == '\t')
531     (*text)++;
532
533   /* Treating underscores as part of command words is important
534      so that "set args_foo()" doesn't get interpreted as
535      "set args _foo()".  */
536   for (p = *text;
537        *p && (isalnum(*p) || *p == '-' || *p == '_');
538        p++)
539     ;
540
541   /* If nothing but whitespace, return 0.  */
542   if (p == *text)
543     return 0;
544   
545   len = p - *text;
546
547   /* *text and p now bracket the first command word to lookup (and
548      it's length is len).  We copy this into a local temporary,
549      converting to lower case as we go.  */
550
551   command = (char *) alloca (len + 1);
552   for (tmp = 0; tmp < len; tmp++)
553     {
554       char x = (*text)[tmp];
555       command[tmp] = isupper(x) ? tolower(x) : x;
556     }
557   command[len] = '\0';
558
559   /* Look it up.  */
560   found = 0;
561   nfound = 0;
562   for (c = clist; c; c = c->next)
563     if (!strncmp (command, c->name, len)
564         && (!ignore_help_classes || c->function.cfunc))
565       {
566         found = c;
567         nfound++;
568         if (c->name[len] == '\0')
569           {
570             nfound = 1;
571             break;
572           }
573       }
574
575   /* If nothing matches, we have a simple failure.  */
576   if (nfound == 0)
577     return 0;
578
579   if (nfound > 1)
580     {
581       if (result_list != NULL)
582         /* Will be modified in calling routine
583            if we know what the prefix command is.  */
584         *result_list = 0;               
585       return (struct cmd_list_element *) -1; /* Ambiguous.  */
586     }
587
588   /* We've matched something on this list.  Move text pointer forward. */
589
590   *text = p;
591
592   /* If this was an abbreviation, use the base command instead.  */
593
594   if (found->cmd_pointer)
595     found = found->cmd_pointer;
596
597   /* If we found a prefix command, keep looking.  */
598
599   if (found->prefixlist)
600     {
601       c = lookup_cmd_1 (text, *found->prefixlist, result_list,
602                         ignore_help_classes);
603       if (!c)
604         {
605           /* Didn't find anything; this is as far as we got.  */
606           if (result_list != NULL)
607             *result_list = clist;
608           return found;
609         }
610       else if (c == (struct cmd_list_element *) -1)
611         {
612           /* We've gotten this far properley, but the next step
613              is ambiguous.  We need to set the result list to the best
614              we've found (if an inferior hasn't already set it).  */
615           if (result_list != NULL)
616             if (!*result_list)
617               /* This used to say *result_list = *found->prefixlist
618                  If that was correct, need to modify the documentation
619                  at the top of this function to clarify what is supposed
620                  to be going on.  */
621               *result_list = found;
622           return c;
623         }
624       else
625         {
626           /* We matched!  */
627           return c;
628         }
629     }
630   else
631     {
632       if (result_list != NULL)
633         *result_list = clist;
634       return found;
635     }
636 }
637
638 /* All this hair to move the space to the front of cmdtype */
639
640 static void
641 undef_cmd_error (cmdtype, q)
642      char *cmdtype, *q;
643 {
644   error ("Undefined %scommand: \"%s\".  Try \"help%s%.*s\".",
645     cmdtype,
646     q,
647     *cmdtype? " ": "",
648     strlen(cmdtype)-1,
649     cmdtype);
650 }
651
652 /* Look up the contents of *LINE as a command in the command list LIST.
653    LIST is a chain of struct cmd_list_element's.
654    If it is found, return the struct cmd_list_element for that command
655    and update *LINE to point after the command name, at the first argument.
656    If not found, call error if ALLOW_UNKNOWN is zero
657    otherwise (or if error returns) return zero.
658    Call error if specified command is ambiguous,
659    unless ALLOW_UNKNOWN is negative.
660    CMDTYPE precedes the word "command" in the error message.
661
662    If INGNORE_HELP_CLASSES is nonzero, ignore any command list
663    elements which are actually help classes rather than commands (i.e.
664    the function field of the struct cmd_list_element is 0).  */
665
666 struct cmd_list_element *
667 lookup_cmd (line, list, cmdtype, allow_unknown, ignore_help_classes)
668      char **line;
669      struct cmd_list_element *list;
670      char *cmdtype;
671      int allow_unknown;
672      int ignore_help_classes;
673 {
674   struct cmd_list_element *last_list = 0;
675   struct cmd_list_element *c =
676     lookup_cmd_1 (line, list, &last_list, ignore_help_classes);
677   char *ptr = (*line) + strlen (*line) - 1;
678
679   /* Clear off trailing whitespace.  */
680   while (ptr >= *line && (*ptr == ' ' || *ptr == '\t'))
681     ptr--;
682   *(ptr + 1) = '\0';
683   
684   if (!c)
685     {
686       if (!allow_unknown)
687         {
688           if (!*line)
689             error ("Lack of needed %scommand", cmdtype);
690           else
691             {
692               char *p = *line, *q;
693
694               while (isalnum(*p) || *p == '-')
695                 p++;
696
697               q = (char *) alloca (p - *line + 1);
698               strncpy (q, *line, p - *line);
699               q[p-*line] = '\0';
700               undef_cmd_error (cmdtype, q);
701             }
702         }
703       else
704         return 0;
705     }
706   else if (c == (struct cmd_list_element *) -1)
707     {
708       /* Ambigous.  Local values should be off prefixlist or called
709          values.  */
710       int local_allow_unknown = (last_list ? last_list->allow_unknown :
711                                  allow_unknown);
712       char *local_cmdtype = last_list ? last_list->prefixname : cmdtype;
713       struct cmd_list_element *local_list =
714         (last_list ? *(last_list->prefixlist) : list);
715       
716       if (local_allow_unknown < 0)
717         {
718           if (last_list)
719             return last_list;   /* Found something.  */
720           else
721             return 0;           /* Found nothing.  */
722         }
723       else
724         {
725           /* Report as error.  */
726           int amb_len;
727           char ambbuf[100];
728
729           for (amb_len = 0;
730                ((*line)[amb_len] && (*line)[amb_len] != ' '
731                 && (*line)[amb_len] != '\t');
732                amb_len++)
733             ;
734           
735           ambbuf[0] = 0;
736           for (c = local_list; c; c = c->next)
737             if (!strncmp (*line, c->name, amb_len))
738               {
739                 if (strlen (ambbuf) + strlen (c->name) + 6 < (int)sizeof ambbuf)
740                   {
741                     if (strlen (ambbuf))
742                       strcat (ambbuf, ", ");
743                     strcat (ambbuf, c->name);
744                   }
745                 else
746                   {
747                     strcat (ambbuf, "..");
748                     break;
749                   }
750               }
751           error ("Ambiguous %scommand \"%s\": %s.", local_cmdtype,
752                  *line, ambbuf);
753           return 0;             /* lint */
754         }
755     }
756   else
757     {
758       /* We've got something.  It may still not be what the caller
759          wants (if this command *needs* a subcommand).  */
760       while (**line == ' ' || **line == '\t')
761         (*line)++;
762
763       if (c->prefixlist && **line && !c->allow_unknown)
764         undef_cmd_error (c->prefixname, *line);
765
766       /* Seems to be what he wants.  Return it.  */
767       return c;
768     }
769   return 0;
770 }
771         
772 #if 0
773 /* Look up the contents of *LINE as a command in the command list LIST.
774    LIST is a chain of struct cmd_list_element's.
775    If it is found, return the struct cmd_list_element for that command
776    and update *LINE to point after the command name, at the first argument.
777    If not found, call error if ALLOW_UNKNOWN is zero
778    otherwise (or if error returns) return zero.
779    Call error if specified command is ambiguous,
780    unless ALLOW_UNKNOWN is negative.
781    CMDTYPE precedes the word "command" in the error message.  */
782
783 struct cmd_list_element *
784 lookup_cmd (line, list, cmdtype, allow_unknown)
785      char **line;
786      struct cmd_list_element *list;
787      char *cmdtype;
788      int allow_unknown;
789 {
790   register char *p;
791   register struct cmd_list_element *c, *found;
792   int nfound;
793   char ambbuf[100];
794   char *processed_cmd;
795   int i, cmd_len;
796
797   /* Skip leading whitespace.  */
798
799   while (**line == ' ' || **line == '\t')
800     (*line)++;
801
802   /* Clear out trailing whitespace.  */
803
804   p = *line + strlen (*line);
805   while (p != *line && (p[-1] == ' ' || p[-1] == '\t'))
806     p--;
807   *p = 0;
808
809   /* Find end of command name.  */
810
811   p = *line;
812   while (*p == '-' || isalnum(*p))
813     p++;
814
815   /* Look up the command name.
816      If exact match, keep that.
817      Otherwise, take command abbreviated, if unique.  Note that (in my
818      opinion) a null string does *not* indicate ambiguity; simply the
819      end of the argument.  */
820
821   if (p == *line)
822     {
823       if (!allow_unknown)
824         error ("Lack of needed %scommand", cmdtype);
825       return 0;
826     }
827   
828   /* Copy over to a local buffer, converting to lowercase on the way.
829      This is in case the command being parsed is a subcommand which
830      doesn't match anything, and that's ok.  We want the original
831      untouched for the routine of the original command.  */
832   
833   processed_cmd = (char *) alloca (p - *line + 1);
834   for (cmd_len = 0; cmd_len < p - *line; cmd_len++)
835     {
836       char x = (*line)[cmd_len];
837       if (isupper(x))
838         processed_cmd[cmd_len] = tolower(x);
839       else
840         processed_cmd[cmd_len] = x;
841     }
842   processed_cmd[cmd_len] = '\0';
843
844   /* Check all possibilities in the current command list.  */
845   found = 0;
846   nfound = 0;
847   for (c = list; c; c = c->next)
848     {
849       if (!strncmp (processed_cmd, c->name, cmd_len))
850         {
851           found = c;
852           nfound++;
853           if (c->name[cmd_len] == 0)
854             {
855               nfound = 1;
856               break;
857             }
858         }
859     }
860
861   /* Report error for undefined command name.  */
862
863   if (nfound != 1)
864     {
865       if (nfound > 1 && allow_unknown >= 0)
866         {
867           ambbuf[0] = 0;
868           for (c = list; c; c = c->next)
869             if (!strncmp (processed_cmd, c->name, cmd_len))
870               {
871                 if (strlen (ambbuf) + strlen (c->name) + 6 < sizeof ambbuf)
872                   {
873                     if (strlen (ambbuf))
874                       strcat (ambbuf, ", ");
875                     strcat (ambbuf, c->name);
876                   }
877                 else
878                   {
879                     strcat (ambbuf, "..");
880                     break;
881                   }
882               }
883           error ("Ambiguous %scommand \"%s\": %s.", cmdtype,
884                  processed_cmd, ambbuf);
885         }
886       else if (!allow_unknown)
887         error ("Undefined %scommand: \"%s\".", cmdtype, processed_cmd);
888       return 0;
889     }
890
891   /* Skip whitespace before the argument.  */
892
893   while (*p == ' ' || *p == '\t') p++;
894   *line = p;
895
896   if (found->prefixlist && *p)
897     {
898       c = lookup_cmd (line, *found->prefixlist, found->prefixname,
899                       found->allow_unknown);
900       if (c)
901         return c;
902     }
903
904   return found;
905 }
906 #endif
907
908 /* Helper function for SYMBOL_COMPLETION_FUNCTION.  */
909
910 /* Return a vector of char pointers which point to the different
911    possible completions in LIST of TEXT.  
912
913    WORD points in the same buffer as TEXT, and completions should be
914    returned relative to this position.  For example, suppose TEXT is "foo"
915    and we want to complete to "foobar".  If WORD is "oo", return
916    "oobar"; if WORD is "baz/foo", return "baz/foobar".  */
917
918 char **
919 complete_on_cmdlist (list, text, word)
920      struct cmd_list_element *list;
921      char *text;
922      char *word;
923 {
924   struct cmd_list_element *ptr;
925   char **matchlist;
926   int sizeof_matchlist;
927   int matches;
928   int textlen = strlen (text);
929
930   sizeof_matchlist = 10;
931   matchlist = (char **) xmalloc (sizeof_matchlist * sizeof (char *));
932   matches = 0;
933
934   for (ptr = list; ptr; ptr = ptr->next)
935     if (!strncmp (ptr->name, text, textlen)
936         && !ptr->abbrev_flag
937         && (ptr->function.cfunc
938             || ptr->prefixlist))
939       {
940         if (matches == sizeof_matchlist)
941           {
942             sizeof_matchlist *= 2;
943             matchlist = (char **) xrealloc ((char *)matchlist,
944                                             (sizeof_matchlist
945                                              * sizeof (char *)));
946           }
947
948         matchlist[matches] = (char *) 
949           xmalloc (strlen (word) + strlen (ptr->name) + 1);
950         if (word == text)
951           strcpy (matchlist[matches], ptr->name);
952         else if (word > text)
953           {
954             /* Return some portion of ptr->name.  */
955             strcpy (matchlist[matches], ptr->name + (word - text));
956           }
957         else
958           {
959             /* Return some of text plus ptr->name.  */
960             strncpy (matchlist[matches], word, text - word);
961             matchlist[matches][text - word] = '\0';
962             strcat (matchlist[matches], ptr->name);
963           }
964         ++matches;
965       }
966
967   if (matches == 0)
968     {
969       free ((PTR)matchlist);
970       matchlist = 0;
971     }
972   else
973     {
974       matchlist = (char **) xrealloc ((char *)matchlist, ((matches + 1)
975                                                 * sizeof (char *)));
976       matchlist[matches] = (char *) 0;
977     }
978
979   return matchlist;
980 }
981
982 static int
983 parse_binary_operation (arg)
984      char *arg;
985 {
986   int length;
987
988   if (!arg || !*arg)
989     return 1;
990
991   length = strlen (arg);
992
993   while (arg[length - 1] == ' ' || arg[length - 1] == '\t')
994     length--;
995
996   if (!strncmp (arg, "on", length)
997       || !strncmp (arg, "1", length)
998       || !strncmp (arg, "yes", length))
999     return 1;
1000   else
1001     if (!strncmp (arg, "off", length)
1002         || !strncmp (arg, "0", length)
1003         || !strncmp (arg, "no", length))
1004       return 0;
1005     else 
1006       {
1007         error ("\"on\" or \"off\" expected.");
1008         return 0;
1009       }
1010 }
1011
1012 /* Do a "set" or "show" command.  ARG is NULL if no argument, or the text
1013    of the argument, and FROM_TTY is nonzero if this command is being entered
1014    directly by the user (i.e. these are just like any other
1015    command).  C is the command list element for the command.  */
1016 void
1017 do_setshow_command (arg, from_tty, c)
1018      char *arg;
1019      int from_tty;
1020      struct cmd_list_element *c;
1021 {
1022   if (c->type == set_cmd)
1023     {
1024       switch (c->var_type)
1025         {
1026         case var_string:
1027           {
1028             char *new;
1029             char *p;
1030             char *q;
1031             int ch;
1032             
1033             if (arg == NULL)
1034               arg = "";
1035             new = (char *) xmalloc (strlen (arg) + 2);
1036             p = arg; q = new;
1037             while ((ch = *p++) != '\000')
1038               {
1039                 if (ch == '\\')
1040                   {
1041                     /* \ at end of argument is used after spaces
1042                        so they won't be lost.  */
1043                     if (*p == 0)
1044                       break;
1045                     ch = parse_escape (&p);
1046                     if (ch == 0)
1047                       break; /* C loses */
1048                     else if (ch > 0)
1049                       *q++ = ch;
1050                   }
1051                 else
1052                   *q++ = ch;
1053               }
1054             if (*(p - 1) != '\\')
1055               *q++ = ' ';
1056             *q++ = '\0';
1057             new = (char *) xrealloc (new, q - new);
1058             if (*(char **)c->var != NULL)
1059               free (*(char **)c->var);
1060             *(char **) c->var = new;
1061           }
1062           break;
1063         case var_string_noescape:
1064           if (arg == NULL)
1065             arg = "";
1066           if (*(char **)c->var != NULL)
1067             free (*(char **)c->var);
1068           *(char **) c->var = savestring (arg, strlen (arg));
1069           break;
1070         case var_filename:
1071           if (arg == NULL)
1072             error_no_arg ("filename to set it to.");
1073           if (*(char **)c->var != NULL)
1074             free (*(char **)c->var);
1075           *(char **)c->var = tilde_expand (arg);
1076           break;
1077         case var_boolean:
1078           *(int *) c->var = parse_binary_operation (arg);
1079           break;
1080         case var_uinteger:
1081           if (arg == NULL)
1082             error_no_arg ("integer to set it to.");
1083           *(unsigned int *) c->var = parse_and_eval_address (arg);
1084           if (*(unsigned int *) c->var == 0)
1085             *(unsigned int *) c->var = UINT_MAX;
1086           break;
1087         case var_integer:
1088           {
1089             unsigned int val;
1090             if (arg == NULL)
1091               error_no_arg ("integer to set it to.");
1092             val = parse_and_eval_address (arg);
1093             if (val == 0)
1094               *(int *) c->var = INT_MAX;
1095             else if (val >= INT_MAX)
1096               error ("integer %u out of range", val);
1097             else
1098               *(int *) c->var = val;
1099             break;
1100           }
1101         case var_zinteger:
1102           if (arg == NULL)
1103             error_no_arg ("integer to set it to.");
1104           *(int *) c->var = parse_and_eval_address (arg);
1105           break;
1106         default:
1107           error ("gdb internal error: bad var_type in do_setshow_command");
1108         }
1109     }
1110   else if (c->type == show_cmd)
1111     {
1112       /* Print doc minus "show" at start.  */
1113       print_doc_line (gdb_stdout, c->doc + 5);
1114       
1115       fputs_filtered (" is ", gdb_stdout);
1116       wrap_here ("    ");
1117       switch (c->var_type)
1118         {
1119       case var_string:
1120         {
1121           unsigned char *p;
1122           fputs_filtered ("\"", gdb_stdout);
1123           for (p = *(unsigned char **) c->var; *p != '\0'; p++)
1124             gdb_printchar (*p, gdb_stdout, '"');
1125           fputs_filtered ("\"", gdb_stdout);
1126         }
1127         break;
1128       case var_string_noescape:
1129       case var_filename:
1130         fputs_filtered ("\"", gdb_stdout);
1131         fputs_filtered (*(char **) c->var, gdb_stdout);
1132         fputs_filtered ("\"", gdb_stdout);
1133         break;
1134       case var_boolean:
1135         fputs_filtered (*(int *) c->var ? "on" : "off", gdb_stdout);
1136         break;
1137       case var_uinteger:
1138         if (*(unsigned int *) c->var == UINT_MAX) {
1139           fputs_filtered ("unlimited", gdb_stdout);
1140           break;
1141         }
1142         /* else fall through */
1143       case var_zinteger:
1144         fprintf_filtered (gdb_stdout, "%u", *(unsigned int *) c->var);
1145         break;
1146       case var_integer:
1147         if (*(int *) c->var == INT_MAX)
1148           {
1149             fputs_filtered ("unlimited", gdb_stdout);
1150           }
1151         else
1152           fprintf_filtered (gdb_stdout, "%d", *(int *) c->var);
1153         break;
1154             
1155       default:
1156         error ("gdb internal error: bad var_type in do_setshow_command");
1157       }
1158       fputs_filtered (".\n", gdb_stdout);
1159     }
1160   else
1161     error ("gdb internal error: bad cmd_type in do_setshow_command");
1162   (*c->function.sfunc) (NULL, from_tty, c);
1163 }
1164
1165 /* Show all the settings in a list of show commands.  */
1166
1167 void
1168 cmd_show_list (list, from_tty, prefix)
1169      struct cmd_list_element *list;
1170      int from_tty;
1171      char *prefix;
1172 {
1173   for (; list != NULL; list = list->next) {
1174     /* If we find a prefix, run its list, prefixing our output by its
1175        prefix (with "show " skipped).  */
1176     if (list->prefixlist && !list->abbrev_flag)
1177       cmd_show_list (*list->prefixlist, from_tty, list->prefixname + 5);
1178     if (list->type == show_cmd)
1179       {
1180         fputs_filtered (prefix, gdb_stdout);
1181         fputs_filtered (list->name, gdb_stdout);
1182         fputs_filtered (":  ", gdb_stdout);
1183         do_setshow_command ((char *)NULL, from_tty, list);
1184       }
1185   }
1186 }
1187
1188 /* ARGSUSED */
1189 static void
1190 shell_escape (arg, from_tty)
1191      char *arg;
1192      int from_tty;
1193 {
1194 #ifdef CANT_FORK
1195   /* FIXME: what about errors (I don't know how GO32 system() handles
1196      them)?  */
1197   system (arg);
1198 #else /* Can fork.  */
1199   int rc, status, pid;
1200   char *p, *user_shell;
1201
1202   if ((user_shell = (char *) getenv ("SHELL")) == NULL)
1203     user_shell = "/bin/sh";
1204
1205   /* Get the name of the shell for arg0 */
1206   if ((p = strrchr (user_shell, '/')) == NULL)
1207     p = user_shell;
1208   else
1209     p++;                        /* Get past '/' */
1210
1211   if ((pid = fork()) == 0)
1212     {
1213       if (!arg)
1214         execl (user_shell, p, 0);
1215       else
1216         execl (user_shell, p, "-c", arg, 0);
1217
1218       fprintf_unfiltered (gdb_stderr, "Cannot execute %s: %s\n", user_shell,
1219                           safe_strerror (errno));
1220       gdb_flush (gdb_stderr);
1221       _exit (0177);
1222     }
1223
1224   if (pid != -1)
1225     while ((rc = wait (&status)) != pid && rc != -1)
1226       ;
1227   else
1228     error ("Fork failed");
1229 #endif /* Can fork.  */
1230 }
1231
1232 static void
1233 make_command (arg, from_tty)
1234      char *arg;
1235      int from_tty;
1236 {
1237   char *p;
1238
1239   if (arg == 0)
1240     p = "make";
1241   else
1242     {
1243       p = xmalloc (sizeof("make ") + strlen(arg));
1244       strcpy (p, "make ");
1245       strcpy (p + sizeof("make ")-1, arg);
1246     }
1247   
1248   shell_escape (p, from_tty);
1249 }
1250
1251 static void
1252 show_user_1 (c, stream)
1253      struct cmd_list_element *c;
1254      GDB_FILE *stream;
1255 {
1256   register struct command_line *cmdlines;
1257
1258   cmdlines = c->user_commands;
1259   if (!cmdlines)
1260     return;
1261   fputs_filtered ("User command ", stream);
1262   fputs_filtered (c->name, stream);
1263   fputs_filtered (":\n", stream);
1264   while (cmdlines)
1265     {
1266       fputs_filtered (cmdlines->line, stream); 
1267       fputs_filtered ("\n", stream); 
1268       cmdlines = cmdlines->next;
1269     }
1270   fputs_filtered ("\n", stream);
1271 }
1272
1273 /* ARGSUSED */
1274 static void
1275 show_user (args, from_tty)
1276      char *args;
1277      int from_tty;
1278 {
1279   struct cmd_list_element *c;
1280   extern struct cmd_list_element *cmdlist;
1281
1282   if (args)
1283     {
1284       c = lookup_cmd (&args, cmdlist, "", 0, 1);
1285       if (c->class != class_user)
1286         error ("Not a user command.");
1287       show_user_1 (c, gdb_stdout);
1288     }
1289   else
1290     {
1291       for (c = cmdlist; c; c = c->next)
1292         {
1293           if (c->class == class_user)
1294             show_user_1 (c, gdb_stdout);
1295         }
1296     }
1297 }
1298
1299 void
1300 _initialize_command ()
1301 {
1302   add_com ("shell", class_support, shell_escape,
1303            "Execute the rest of the line as a shell command.  \n\
1304 With no arguments, run an inferior shell.");
1305   add_com ("make", class_support, make_command,
1306            "Run the ``make'' program using the rest of the line as arguments.");
1307   add_cmd ("user", no_class, show_user, 
1308            "Show definitions of user defined commands.\n\
1309 Argument is the name of the user defined command.\n\
1310 With no argument, show definitions of all user defined commands.", &showlist);
1311 }