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