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