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