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