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