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