]> Git Repo - binutils.git/blob - gdb/command.c
2000-04-02 Mark Kettenis <[email protected]>
[binutils.git] / gdb / command.c
1 /* Handle lists of commands, their decoding and documentation, for GDB.
2    Copyright 1986, 1989, 1990, 1991, 1998, 2000 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,
17    Boston, MA 02111-1307, USA.  */
18
19 #include "defs.h"
20 #include "gdbcmd.h"
21 #include "symtab.h"
22 #include "value.h"
23 #include <ctype.h>
24 #include "gdb_string.h"
25 #ifdef UI_OUT
26 #include "ui-out.h"
27 #endif
28
29 #include "gdb_wait.h"
30 #include "gnu-regex.h"
31 /* FIXME: this should be auto-configured!  */
32 #ifdef __MSDOS__
33 # define CANT_FORK
34 #endif
35
36 /* Prototypes for local functions */
37
38 static void undef_cmd_error PARAMS ((char *, char *));
39
40 static void show_user PARAMS ((char *, int));
41
42 static void show_user_1 (struct cmd_list_element *, struct ui_file *);
43
44 static void make_command PARAMS ((char *, int));
45
46 static void shell_escape PARAMS ((char *, int));
47
48 static int parse_binary_operation PARAMS ((char *));
49
50 static void print_doc_line (struct ui_file *, char *);
51
52 static struct cmd_list_element *find_cmd PARAMS ((char *command,
53                                                   int len,
54                                             struct cmd_list_element * clist,
55                                                   int ignore_help_classes,
56                                                   int *nfound));
57 static void apropos_cmd_helper (struct ui_file *, struct cmd_list_element *, 
58                                 struct re_pattern_buffer *, char *);
59
60 void apropos_command (char *, int);
61
62 void _initialize_command PARAMS ((void));
63
64 /* Add element named NAME.
65    CLASS is the top level category into which commands are broken down
66    for "help" purposes.
67    FUN should be the function to execute the command;
68    it will get a character string as argument, with leading
69    and trailing blanks already eliminated.
70
71    DOC is a documentation string for the command.
72    Its first line should be a complete sentence.
73    It should start with ? for a command that is an abbreviation
74    or with * for a command that most users don't need to know about.
75
76    Add this command to command list *LIST.  
77
78    Returns a pointer to the added command (not necessarily the head 
79    of *LIST). */
80
81 struct cmd_list_element *
82 add_cmd (name, class, fun, doc, list)
83      char *name;
84      enum command_class class;
85      void (*fun) PARAMS ((char *, int));
86      char *doc;
87      struct cmd_list_element **list;
88 {
89   register struct cmd_list_element *c
90   = (struct cmd_list_element *) xmalloc (sizeof (struct cmd_list_element));
91   struct cmd_list_element *p;
92
93   delete_cmd (name, list);
94
95   if (*list == NULL || STRCMP ((*list)->name, name) >= 0)
96     {
97       c->next = *list;
98       *list = c;
99     }
100   else
101     {
102       p = *list;
103       while (p->next && STRCMP (p->next->name, name) <= 0)
104         {
105           p = p->next;
106         }
107       c->next = p->next;
108       p->next = c;
109     }
110
111   c->name = name;
112   c->class = class;
113   c->function.cfunc = fun;
114   c->doc = doc;
115   c->flags = 0;
116   c->replacement = NULL;
117   c->hook = NULL;
118   c->prefixlist = NULL;
119   c->prefixname = NULL;
120   c->allow_unknown = 0;
121   c->abbrev_flag = 0;
122   c->completer = make_symbol_completion_list;
123   c->type = not_set_cmd;
124   c->var = NULL;
125   c->var_type = var_boolean;
126   c->enums = NULL;
127   c->user_commands = NULL;
128   c->hookee = NULL;
129   c->cmd_pointer = NULL;
130
131   return c;
132 }
133
134
135 /* Deprecates a command CMD.
136    REPLACEMENT is the name of the command which should be used in place
137    of this command, or NULL if no such command exists.
138
139    This function does not check to see if command REPLACEMENT exists
140    since gdb may not have gotten around to adding REPLACEMENT when this
141    function is called.
142
143    Returns a pointer to the deprecated command.  */
144
145 struct cmd_list_element *
146 deprecate_cmd (cmd, replacement)
147      struct cmd_list_element *cmd;
148      char *replacement;
149 {
150   cmd->flags |= (CMD_DEPRECATED | DEPRECATED_WARN_USER);
151
152   if (replacement != NULL)
153     cmd->replacement = replacement;
154   else
155     cmd->replacement = NULL;
156
157   return cmd;
158 }
159
160
161 /* Same as above, except that the abbrev_flag is set. */
162
163 #if 0                           /* Currently unused */
164
165 struct cmd_list_element *
166 add_abbrev_cmd (name, class, fun, doc, list)
167      char *name;
168      enum command_class class;
169      void (*fun) PARAMS ((char *, int));
170      char *doc;
171      struct cmd_list_element **list;
172 {
173   register struct cmd_list_element *c
174   = add_cmd (name, class, fun, doc, list);
175
176   c->abbrev_flag = 1;
177   return c;
178 }
179
180 #endif
181
182 struct cmd_list_element *
183 add_alias_cmd (name, oldname, class, abbrev_flag, list)
184      char *name;
185      char *oldname;
186      enum command_class class;
187      int abbrev_flag;
188      struct cmd_list_element **list;
189 {
190   /* Must do this since lookup_cmd tries to side-effect its first arg */
191   char *copied_name;
192   register struct cmd_list_element *old;
193   register struct cmd_list_element *c;
194   copied_name = (char *) alloca (strlen (oldname) + 1);
195   strcpy (copied_name, oldname);
196   old = lookup_cmd (&copied_name, *list, "", 1, 1);
197
198   if (old == 0)
199     {
200       delete_cmd (name, list);
201       return 0;
202     }
203
204   c = add_cmd (name, class, old->function.cfunc, old->doc, list);
205   c->prefixlist = old->prefixlist;
206   c->prefixname = old->prefixname;
207   c->allow_unknown = old->allow_unknown;
208   c->abbrev_flag = abbrev_flag;
209   c->cmd_pointer = old;
210   return c;
211 }
212
213 /* Like add_cmd but adds an element for a command prefix:
214    a name that should be followed by a subcommand to be looked up
215    in another command list.  PREFIXLIST should be the address
216    of the variable containing that list.  */
217
218 struct cmd_list_element *
219 add_prefix_cmd (name, class, fun, doc, prefixlist, prefixname,
220                 allow_unknown, list)
221      char *name;
222      enum command_class class;
223      void (*fun) PARAMS ((char *, int));
224      char *doc;
225      struct cmd_list_element **prefixlist;
226      char *prefixname;
227      int allow_unknown;
228      struct cmd_list_element **list;
229 {
230   register struct cmd_list_element *c = add_cmd (name, class, fun, doc, list);
231   c->prefixlist = prefixlist;
232   c->prefixname = prefixname;
233   c->allow_unknown = allow_unknown;
234   return c;
235 }
236
237 /* Like add_prefix_cmd but sets the abbrev_flag on the new command. */
238
239 struct cmd_list_element *
240 add_abbrev_prefix_cmd (name, class, fun, doc, prefixlist, prefixname,
241                        allow_unknown, list)
242      char *name;
243      enum command_class class;
244      void (*fun) PARAMS ((char *, int));
245      char *doc;
246      struct cmd_list_element **prefixlist;
247      char *prefixname;
248      int allow_unknown;
249      struct cmd_list_element **list;
250 {
251   register struct cmd_list_element *c = add_cmd (name, class, fun, doc, list);
252   c->prefixlist = prefixlist;
253   c->prefixname = prefixname;
254   c->allow_unknown = allow_unknown;
255   c->abbrev_flag = 1;
256   return c;
257 }
258
259 /* This is an empty "cfunc".  */
260 void
261 not_just_help_class_command (args, from_tty)
262      char *args;
263      int from_tty;
264 {
265 }
266
267 /* This is an empty "sfunc".  */
268 static void empty_sfunc PARAMS ((char *, int, struct cmd_list_element *));
269
270 static void
271 empty_sfunc (args, from_tty, c)
272      char *args;
273      int from_tty;
274      struct cmd_list_element *c;
275 {
276 }
277
278 /* Add element named NAME to command list LIST (the list for set
279    or some sublist thereof).
280    CLASS is as in add_cmd.
281    VAR_TYPE is the kind of thing we are setting.
282    VAR is address of the variable being controlled by this command.
283    DOC is the documentation string.  */
284
285 struct cmd_list_element *
286 add_set_cmd (name, class, var_type, var, doc, list)
287      char *name;
288      enum command_class class;
289      var_types var_type;
290      char *var;
291      char *doc;
292      struct cmd_list_element **list;
293 {
294   struct cmd_list_element *c
295   = add_cmd (name, class, NO_FUNCTION, doc, list);
296
297   c->type = set_cmd;
298   c->var_type = var_type;
299   c->var = var;
300   /* This needs to be something besides NO_FUNCTION so that this isn't
301      treated as a help class.  */
302   c->function.sfunc = empty_sfunc;
303   return c;
304 }
305
306 /* Add element named NAME to command list LIST (the list for set
307    or some sublist thereof).
308    CLASS is as in add_cmd.
309    ENUMLIST is a list of strings which may follow NAME.
310    VAR is address of the variable which will contain the matching string
311    (from ENUMLIST).
312    DOC is the documentation string.  */
313
314 struct cmd_list_element *
315 add_set_enum_cmd (name, class, enumlist, var, doc, list)
316      char *name;
317      enum command_class class;
318      char *enumlist[];
319      char *var;
320      char *doc;
321      struct cmd_list_element **list;
322 {
323   struct cmd_list_element *c
324   = add_set_cmd (name, class, var_enum, var, doc, list);
325   c->enums = enumlist;
326
327   return c;
328 }
329
330 /* Where SETCMD has already been added, add the corresponding show
331    command to LIST and return a pointer to the added command (not 
332    necessarily the head of LIST).  */
333 struct cmd_list_element *
334 add_show_from_set (setcmd, list)
335      struct cmd_list_element *setcmd;
336      struct cmd_list_element **list;
337 {
338   struct cmd_list_element *showcmd =
339   (struct cmd_list_element *) xmalloc (sizeof (struct cmd_list_element));
340   struct cmd_list_element *p;
341
342   memcpy (showcmd, setcmd, sizeof (struct cmd_list_element));
343   delete_cmd (showcmd->name, list);
344   showcmd->type = show_cmd;
345
346   /* Replace "set " at start of docstring with "show ".  */
347   if (setcmd->doc[0] == 'S' && setcmd->doc[1] == 'e'
348       && setcmd->doc[2] == 't' && setcmd->doc[3] == ' ')
349     showcmd->doc = concat ("Show ", setcmd->doc + 4, NULL);
350   else
351     fprintf_unfiltered (gdb_stderr, "GDB internal error: Bad docstring for set command\n");
352
353   if (*list == NULL || STRCMP ((*list)->name, showcmd->name) >= 0)
354     {
355       showcmd->next = *list;
356       *list = showcmd;
357     }
358   else
359     {
360       p = *list;
361       while (p->next && STRCMP (p->next->name, showcmd->name) <= 0)
362         {
363           p = p->next;
364         }
365       showcmd->next = p->next;
366       p->next = showcmd;
367     }
368
369   return showcmd;
370 }
371
372 /* Remove the command named NAME from the command list.  */
373
374 void
375 delete_cmd (name, list)
376      char *name;
377      struct cmd_list_element **list;
378 {
379   register struct cmd_list_element *c;
380   struct cmd_list_element *p;
381
382   while (*list && STREQ ((*list)->name, name))
383     {
384       if ((*list)->hookee)
385         (*list)->hookee->hook = 0;      /* Hook slips out of its mouth */
386       p = (*list)->next;
387       free ((PTR) * list);
388       *list = p;
389     }
390
391   if (*list)
392     for (c = *list; c->next;)
393       {
394         if (STREQ (c->next->name, name))
395           {
396             if (c->next->hookee)
397               c->next->hookee->hook = 0;        /* hooked cmd gets away.  */
398             p = c->next->next;
399             free ((PTR) c->next);
400             c->next = p;
401           }
402         else
403           c = c->next;
404       }
405 }
406 /* Recursively walk the commandlist structures, and print out the
407    documentation of commands that match our regex in either their
408    name, or their documentation.
409 */
410 static void 
411 apropos_cmd_helper (struct ui_file *stream, struct cmd_list_element *commandlist,
412                          struct re_pattern_buffer *regex, char *prefix)
413 {
414   register struct cmd_list_element *c;
415   int returnvalue=1; /*Needed to avoid double printing*/
416   /* Walk through the commands */
417   for (c=commandlist;c;c=c->next)
418     {
419       if (c->name != NULL)
420         {
421           /* Try to match against the name*/
422           returnvalue=re_search(regex,c->name,strlen(c->name),0,strlen(c->name),NULL);
423           if (returnvalue >= 0)
424             {
425               /* Stolen from help_cmd_list. We don't directly use
426                * help_cmd_list because it doesn't let us print out
427                * single commands
428                */
429               fprintf_filtered (stream, "%s%s -- ", prefix, c->name);
430               print_doc_line (stream, c->doc);
431               fputs_filtered ("\n", stream);
432               returnvalue=0; /*Set this so we don't print it again.*/
433             }
434         }
435       if (c->doc != NULL && returnvalue != 0)
436         {
437           /* Try to match against documentation */
438           if (re_search(regex,c->doc,strlen(c->doc),0,strlen(c->doc),NULL) >=0)
439             {
440               /* Stolen from help_cmd_list. We don't directly use
441                * help_cmd_list because it doesn't let us print out
442                * single commands
443                */
444               fprintf_filtered (stream, "%s%s -- ", prefix, c->name);
445               print_doc_line (stream, c->doc);
446               fputs_filtered ("\n", stream);
447             }
448         }
449       /* Check if this command has subcommands */
450       if (c->prefixlist != NULL)
451         {
452           /* Recursively call ourselves on the subcommand list,
453              passing the right prefix in.
454           */
455           apropos_cmd_helper(stream,*c->prefixlist,regex,c->prefixname);
456         }
457     }
458 }
459 /* Search through names of commands and documentations for a certain
460    regular expression.
461 */
462 void 
463 apropos_command (char *searchstr, int from_tty)
464 {
465   extern struct cmd_list_element *cmdlist; /*This is the main command list*/
466   regex_t pattern;
467   char *pattern_fastmap;
468   char errorbuffer[512];
469   pattern_fastmap=calloc(256,sizeof(char));
470   if (searchstr == NULL)
471       error("REGEXP string is empty");
472
473   if (regcomp(&pattern,searchstr,REG_ICASE) == 0)
474     {
475       pattern.fastmap=pattern_fastmap;
476       re_compile_fastmap(&pattern);
477       apropos_cmd_helper(gdb_stdout,cmdlist,&pattern,"");
478     }
479   else
480     {
481       regerror(regcomp(&pattern,searchstr,REG_ICASE),NULL,errorbuffer,512);
482       error("Error in regular expression:%s",errorbuffer);
483     }
484   free(pattern_fastmap);
485 }
486
487
488 /* This command really has to deal with two things:
489  *     1) I want documentation on *this string* (usually called by
490  * "help commandname").
491  *     2) I want documentation on *this list* (usually called by
492  * giving a command that requires subcommands.  Also called by saying
493  * just "help".)
494  *
495  *   I am going to split this into two seperate comamnds, help_cmd and
496  * help_list. 
497  */
498
499 void
500 help_cmd (command, stream)
501      char *command;
502      struct ui_file *stream;
503 {
504   struct cmd_list_element *c;
505   extern struct cmd_list_element *cmdlist;
506
507   if (!command)
508     {
509       help_list (cmdlist, "", all_classes, stream);
510       return;
511     }
512
513   c = lookup_cmd (&command, cmdlist, "", 0, 0);
514
515   if (c == 0)
516     return;
517
518   /* There are three cases here.
519      If c->prefixlist is nonzero, we have a prefix command.
520      Print its documentation, then list its subcommands.
521
522      If c->function is nonzero, we really have a command.
523      Print its documentation and return.
524
525      If c->function is zero, we have a class name.
526      Print its documentation (as if it were a command)
527      and then set class to the number of this class
528      so that the commands in the class will be listed.  */
529
530   fputs_filtered (c->doc, stream);
531   fputs_filtered ("\n", stream);
532
533   if (c->prefixlist == 0 && c->function.cfunc != NULL)
534     return;
535   fprintf_filtered (stream, "\n");
536
537   /* If this is a prefix command, print it's subcommands */
538   if (c->prefixlist)
539     help_list (*c->prefixlist, c->prefixname, all_commands, stream);
540
541   /* If this is a class name, print all of the commands in the class */
542   if (c->function.cfunc == NULL)
543     help_list (cmdlist, "", c->class, stream);
544
545   if (c->hook)
546     fprintf_filtered (stream, "\nThis command has a hook defined: %s\n",
547                       c->hook->name);
548 }
549
550 /*
551  * Get a specific kind of help on a command list.
552  *
553  * LIST is the list.
554  * CMDTYPE is the prefix to use in the title string.
555  * CLASS is the class with which to list the nodes of this list (see
556  * documentation for help_cmd_list below),  As usual, ALL_COMMANDS for
557  * everything, ALL_CLASSES for just classes, and non-negative for only things
558  * in a specific class.
559  * and STREAM is the output stream on which to print things.
560  * If you call this routine with a class >= 0, it recurses.
561  */
562 void
563 help_list (list, cmdtype, class, stream)
564      struct cmd_list_element *list;
565      char *cmdtype;
566      enum command_class class;
567      struct ui_file *stream;
568 {
569   int len;
570   char *cmdtype1, *cmdtype2;
571
572   /* If CMDTYPE is "foo ", CMDTYPE1 gets " foo" and CMDTYPE2 gets "foo sub"  */
573   len = strlen (cmdtype);
574   cmdtype1 = (char *) alloca (len + 1);
575   cmdtype1[0] = 0;
576   cmdtype2 = (char *) alloca (len + 4);
577   cmdtype2[0] = 0;
578   if (len)
579     {
580       cmdtype1[0] = ' ';
581       strncpy (cmdtype1 + 1, cmdtype, len - 1);
582       cmdtype1[len] = 0;
583       strncpy (cmdtype2, cmdtype, len - 1);
584       strcpy (cmdtype2 + len - 1, " sub");
585     }
586
587   if (class == all_classes)
588     fprintf_filtered (stream, "List of classes of %scommands:\n\n", cmdtype2);
589   else
590     fprintf_filtered (stream, "List of %scommands:\n\n", cmdtype2);
591
592   help_cmd_list (list, class, cmdtype, (int) class >= 0, stream);
593
594   if (class == all_classes)
595     fprintf_filtered (stream, "\n\
596 Type \"help%s\" followed by a class name for a list of commands in that class.",
597                       cmdtype1);
598
599   fprintf_filtered (stream, "\n\
600 Type \"help%s\" followed by %scommand name for full documentation.\n\
601 Command name abbreviations are allowed if unambiguous.\n",
602                     cmdtype1, cmdtype2);
603 }
604
605 /* Print only the first line of STR on STREAM.  */
606 static void
607 print_doc_line (stream, str)
608      struct ui_file *stream;
609      char *str;
610 {
611   static char *line_buffer = 0;
612   static int line_size;
613   register char *p;
614
615   if (!line_buffer)
616     {
617       line_size = 80;
618       line_buffer = (char *) xmalloc (line_size);
619     }
620
621   p = str;
622   while (*p && *p != '\n' && *p != '.' && *p != ',')
623     p++;
624   if (p - str > line_size - 1)
625     {
626       line_size = p - str + 1;
627       free ((PTR) line_buffer);
628       line_buffer = (char *) xmalloc (line_size);
629     }
630   strncpy (line_buffer, str, p - str);
631   line_buffer[p - str] = '\0';
632   if (islower (line_buffer[0]))
633     line_buffer[0] = toupper (line_buffer[0]);
634 #ifdef UI_OUT
635   ui_out_text (uiout, line_buffer);
636 #else
637   fputs_filtered (line_buffer, stream);
638 #endif
639 }
640
641 /*
642  * Implement a help command on command list LIST.
643  * RECURSE should be non-zero if this should be done recursively on
644  * all sublists of LIST.
645  * PREFIX is the prefix to print before each command name.
646  * STREAM is the stream upon which the output should be written.
647  * CLASS should be:
648  *      A non-negative class number to list only commands in that
649  * class.
650  *      ALL_COMMANDS to list all commands in list.
651  *      ALL_CLASSES  to list all classes in list.
652  *
653  *   Note that RECURSE will be active on *all* sublists, not just the
654  * ones selected by the criteria above (ie. the selection mechanism
655  * is at the low level, not the high-level).
656  */
657 void
658 help_cmd_list (list, class, prefix, recurse, stream)
659      struct cmd_list_element *list;
660      enum command_class class;
661      char *prefix;
662      int recurse;
663      struct ui_file *stream;
664 {
665   register struct cmd_list_element *c;
666
667   for (c = list; c; c = c->next)
668     {
669       if (c->abbrev_flag == 0 &&
670           (class == all_commands
671            || (class == all_classes && c->function.cfunc == NULL)
672            || (class == c->class && c->function.cfunc != NULL)))
673         {
674           fprintf_filtered (stream, "%s%s -- ", prefix, c->name);
675           print_doc_line (stream, c->doc);
676           fputs_filtered ("\n", stream);
677         }
678       if (recurse
679           && c->prefixlist != 0
680           && c->abbrev_flag == 0)
681         help_cmd_list (*c->prefixlist, class, c->prefixname, 1, stream);
682     }
683 }
684 \f
685
686 /* Search the input clist for 'command'.  Return the command if
687    found (or NULL if not), and return the number of commands
688    found in nfound */
689
690 static struct cmd_list_element *
691 find_cmd (command, len, clist, ignore_help_classes, nfound)
692      char *command;
693      int len;
694      struct cmd_list_element *clist;
695      int ignore_help_classes;
696      int *nfound;
697 {
698   struct cmd_list_element *found, *c;
699
700   found = (struct cmd_list_element *) NULL;
701   *nfound = 0;
702   for (c = clist; c; c = c->next)
703     if (!strncmp (command, c->name, len)
704         && (!ignore_help_classes || c->function.cfunc))
705       {
706         found = c;
707         (*nfound)++;
708         if (c->name[len] == '\0')
709           {
710             *nfound = 1;
711             break;
712           }
713       }
714   return found;
715 }
716
717 /* This routine takes a line of TEXT and a CLIST in which to start the
718    lookup.  When it returns it will have incremented the text pointer past
719    the section of text it matched, set *RESULT_LIST to point to the list in
720    which the last word was matched, and will return a pointer to the cmd
721    list element which the text matches.  It will return NULL if no match at
722    all was possible.  It will return -1 (cast appropriately, ick) if ambigous
723    matches are possible; in this case *RESULT_LIST will be set to point to
724    the list in which there are ambiguous choices (and *TEXT will be set to
725    the ambiguous text string).
726
727    If the located command was an abbreviation, this routine returns the base
728    command of the abbreviation.
729
730    It does no error reporting whatsoever; control will always return
731    to the superior routine.
732
733    In the case of an ambiguous return (-1), *RESULT_LIST will be set to point
734    at the prefix_command (ie. the best match) *or* (special case) will be NULL
735    if no prefix command was ever found.  For example, in the case of "info a",
736    "info" matches without ambiguity, but "a" could be "args" or "address", so
737    *RESULT_LIST is set to the cmd_list_element for "info".  So in this case
738    RESULT_LIST should not be interpeted as a pointer to the beginning of a
739    list; it simply points to a specific command.  In the case of an ambiguous
740    return *TEXT is advanced past the last non-ambiguous prefix (e.g.
741    "info t" can be "info types" or "info target"; upon return *TEXT has been
742    advanced past "info ").
743
744    If RESULT_LIST is NULL, don't set *RESULT_LIST (but don't otherwise
745    affect the operation).
746
747    This routine does *not* modify the text pointed to by TEXT.
748
749    If IGNORE_HELP_CLASSES is nonzero, ignore any command list elements which
750    are actually help classes rather than commands (i.e. the function field of
751    the struct cmd_list_element is NULL).  */
752
753 struct cmd_list_element *
754 lookup_cmd_1 (text, clist, result_list, ignore_help_classes)
755      char **text;
756      struct cmd_list_element *clist, **result_list;
757      int ignore_help_classes;
758 {
759   char *p, *command;
760   int len, tmp, nfound;
761   struct cmd_list_element *found, *c;
762   char *line = *text;
763
764   while (**text == ' ' || **text == '\t')
765     (*text)++;
766
767   /* Treating underscores as part of command words is important
768      so that "set args_foo()" doesn't get interpreted as
769      "set args _foo()".  */
770   for (p = *text;
771        *p && (isalnum (*p) || *p == '-' || *p == '_' ||
772               (tui_version &&
773                (*p == '+' || *p == '<' || *p == '>' || *p == '$')) ||
774               (xdb_commands && (*p == '!' || *p == '/' || *p == '?')));
775        p++)
776     ;
777
778   /* If nothing but whitespace, return 0.  */
779   if (p == *text)
780     return 0;
781
782   len = p - *text;
783
784   /* *text and p now bracket the first command word to lookup (and
785      it's length is len).  We copy this into a local temporary */
786
787
788   command = (char *) alloca (len + 1);
789   for (tmp = 0; tmp < len; tmp++)
790     {
791       char x = (*text)[tmp];
792       command[tmp] = x;
793     }
794   command[len] = '\0';
795
796   /* Look it up.  */
797   found = 0;
798   nfound = 0;
799   found = find_cmd (command, len, clist, ignore_help_classes, &nfound);
800
801   /* 
802      ** We didn't find the command in the entered case, so lower case it
803      ** and search again.
804    */
805   if (!found || nfound == 0)
806     {
807       for (tmp = 0; tmp < len; tmp++)
808         {
809           char x = command[tmp];
810           command[tmp] = isupper (x) ? tolower (x) : x;
811         }
812       found = find_cmd (command, len, clist, ignore_help_classes, &nfound);
813     }
814
815   /* If nothing matches, we have a simple failure.  */
816   if (nfound == 0)
817     return 0;
818
819   if (nfound > 1)
820     {
821       if (result_list != NULL)
822         /* Will be modified in calling routine
823            if we know what the prefix command is.  */
824         *result_list = 0;
825       return (struct cmd_list_element *) -1;    /* Ambiguous.  */
826     }
827
828   /* We've matched something on this list.  Move text pointer forward. */
829
830   *text = p;
831
832   if (found->cmd_pointer)
833     {
834       /* We drop the alias (abbreviation) in favor of the command it is
835        pointing to.  If the alias is deprecated, though, we need to
836        warn the user about it before we drop it.  Note that while we
837        are warning about the alias, we may also warn about the command
838        itself and we will adjust the appropriate DEPRECATED_WARN_USER
839        flags */
840       
841       if (found->flags & DEPRECATED_WARN_USER)
842       deprecated_cmd_warning (&line);
843       found = found->cmd_pointer;
844     }
845   /* If we found a prefix command, keep looking.  */
846
847   if (found->prefixlist)
848     {
849       c = lookup_cmd_1 (text, *found->prefixlist, result_list,
850                         ignore_help_classes);
851       if (!c)
852         {
853           /* Didn't find anything; this is as far as we got.  */
854           if (result_list != NULL)
855             *result_list = clist;
856           return found;
857         }
858       else if (c == (struct cmd_list_element *) -1)
859         {
860           /* We've gotten this far properly, but the next step
861              is ambiguous.  We need to set the result list to the best
862              we've found (if an inferior hasn't already set it).  */
863           if (result_list != NULL)
864             if (!*result_list)
865               /* This used to say *result_list = *found->prefixlist
866                  If that was correct, need to modify the documentation
867                  at the top of this function to clarify what is supposed
868                  to be going on.  */
869               *result_list = found;
870           return c;
871         }
872       else
873         {
874           /* We matched!  */
875           return c;
876         }
877     }
878   else
879     {
880       if (result_list != NULL)
881         *result_list = clist;
882       return found;
883     }
884 }
885
886 /* All this hair to move the space to the front of cmdtype */
887
888 static void
889 undef_cmd_error (cmdtype, q)
890      char *cmdtype, *q;
891 {
892   error ("Undefined %scommand: \"%s\".  Try \"help%s%.*s\".",
893          cmdtype,
894          q,
895          *cmdtype ? " " : "",
896          strlen (cmdtype) - 1,
897          cmdtype);
898 }
899
900 /* Look up the contents of *LINE as a command in the command list LIST.
901    LIST is a chain of struct cmd_list_element's.
902    If it is found, return the struct cmd_list_element for that command
903    and update *LINE to point after the command name, at the first argument.
904    If not found, call error if ALLOW_UNKNOWN is zero
905    otherwise (or if error returns) return zero.
906    Call error if specified command is ambiguous,
907    unless ALLOW_UNKNOWN is negative.
908    CMDTYPE precedes the word "command" in the error message.
909
910    If INGNORE_HELP_CLASSES is nonzero, ignore any command list
911    elements which are actually help classes rather than commands (i.e.
912    the function field of the struct cmd_list_element is 0).  */
913
914 struct cmd_list_element *
915 lookup_cmd (line, list, cmdtype, allow_unknown, ignore_help_classes)
916      char **line;
917      struct cmd_list_element *list;
918      char *cmdtype;
919      int allow_unknown;
920      int ignore_help_classes;
921 {
922   struct cmd_list_element *last_list = 0;
923   struct cmd_list_element *c =
924   lookup_cmd_1 (line, list, &last_list, ignore_help_classes);
925 #if 0
926   /* This is wrong for complete_command.  */
927   char *ptr = (*line) + strlen (*line) - 1;
928
929   /* Clear off trailing whitespace.  */
930   while (ptr >= *line && (*ptr == ' ' || *ptr == '\t'))
931     ptr--;
932   *(ptr + 1) = '\0';
933 #endif
934
935   if (!c)
936     {
937       if (!allow_unknown)
938         {
939           if (!*line)
940             error ("Lack of needed %scommand", cmdtype);
941           else
942             {
943               char *p = *line, *q;
944
945               while (isalnum (*p) || *p == '-')
946                 p++;
947
948               q = (char *) alloca (p - *line + 1);
949               strncpy (q, *line, p - *line);
950               q[p - *line] = '\0';
951               undef_cmd_error (cmdtype, q);
952             }
953         }
954       else
955         return 0;
956     }
957   else if (c == (struct cmd_list_element *) -1)
958     {
959       /* Ambigous.  Local values should be off prefixlist or called
960          values.  */
961       int local_allow_unknown = (last_list ? last_list->allow_unknown :
962                                  allow_unknown);
963       char *local_cmdtype = last_list ? last_list->prefixname : cmdtype;
964       struct cmd_list_element *local_list =
965       (last_list ? *(last_list->prefixlist) : list);
966
967       if (local_allow_unknown < 0)
968         {
969           if (last_list)
970             return last_list;   /* Found something.  */
971           else
972             return 0;           /* Found nothing.  */
973         }
974       else
975         {
976           /* Report as error.  */
977           int amb_len;
978           char ambbuf[100];
979
980           for (amb_len = 0;
981                ((*line)[amb_len] && (*line)[amb_len] != ' '
982                 && (*line)[amb_len] != '\t');
983                amb_len++)
984             ;
985
986           ambbuf[0] = 0;
987           for (c = local_list; c; c = c->next)
988             if (!strncmp (*line, c->name, amb_len))
989               {
990                 if (strlen (ambbuf) + strlen (c->name) + 6 < (int) sizeof ambbuf)
991                   {
992                     if (strlen (ambbuf))
993                       strcat (ambbuf, ", ");
994                     strcat (ambbuf, c->name);
995                   }
996                 else
997                   {
998                     strcat (ambbuf, "..");
999                     break;
1000                   }
1001               }
1002           error ("Ambiguous %scommand \"%s\": %s.", local_cmdtype,
1003                  *line, ambbuf);
1004           return 0;             /* lint */
1005         }
1006     }
1007   else
1008     {
1009       /* We've got something.  It may still not be what the caller
1010          wants (if this command *needs* a subcommand).  */
1011       while (**line == ' ' || **line == '\t')
1012         (*line)++;
1013
1014       if (c->prefixlist && **line && !c->allow_unknown)
1015         undef_cmd_error (c->prefixname, *line);
1016
1017       /* Seems to be what he wants.  Return it.  */
1018       return c;
1019     }
1020   return 0;
1021 }
1022
1023 /* We are here presumably because an alias or command in *TEXT is 
1024    deprecated and a warning message should be generated.  This function
1025    decodes *TEXT and potentially generates a warning message as outlined
1026    below.
1027    
1028    Example for 'set endian big' which has a fictitious alias 'seb'.
1029    
1030    If alias wasn't used in *TEXT, and the command is deprecated:
1031    "warning: 'set endian big' is deprecated." 
1032    
1033    If alias was used, and only the alias is deprecated:
1034    "warning: 'seb' an alias for the command 'set endian big' is deprecated."
1035    
1036    If alias was used and command is deprecated (regardless of whether the
1037    alias itself is deprecated:
1038    
1039    "warning: 'set endian big' (seb) is deprecated."
1040
1041    After the message has been sent, clear the appropriate flags in the
1042    command and/or the alias so the user is no longer bothered.
1043    
1044 */
1045 void
1046 deprecated_cmd_warning (char **text)
1047 {
1048   struct cmd_list_element *alias = NULL;
1049   struct cmd_list_element *prefix_cmd = NULL;
1050   struct cmd_list_element *cmd = NULL;
1051   struct cmd_list_element *c;
1052   char *type;
1053  
1054   if (!lookup_cmd_composition (*text, &alias, &prefix_cmd, &cmd))
1055     /* return if text doesn't evaluate to a command */
1056     return;
1057
1058   if (!((alias ? (alias->flags & DEPRECATED_WARN_USER) : 0)
1059       || (cmd->flags & DEPRECATED_WARN_USER) ) ) 
1060     /* return if nothing is deprecated */
1061     return;
1062   
1063   printf_filtered ("Warning:");
1064   
1065   if (alias && !(cmd->flags & CMD_DEPRECATED))
1066     printf_filtered (" '%s', an alias for the", alias->name);
1067     
1068   printf_filtered (" command '");
1069   
1070   if (prefix_cmd)
1071     printf_filtered ("%s", prefix_cmd->prefixname);
1072   
1073   printf_filtered ("%s", cmd->name);
1074
1075   if (alias && (cmd->flags & CMD_DEPRECATED))
1076     printf_filtered ("' (%s) is deprecated.\n", alias->name);
1077   else
1078     printf_filtered ("' is deprecated.\n"); 
1079   
1080
1081   /* if it is only the alias that is deprecated, we want to indicate the
1082      new alias, otherwise we'll indicate the new command */
1083
1084   if (alias && !(cmd->flags & CMD_DEPRECATED))
1085     {
1086       if (alias->replacement)
1087       printf_filtered ("Use '%s'.\n\n", alias->replacement);
1088       else
1089       printf_filtered ("No alternative known.\n\n");
1090      }  
1091   else
1092     {
1093       if (cmd->replacement)
1094       printf_filtered ("Use '%s'.\n\n", cmd->replacement);
1095       else
1096       printf_filtered ("No alternative known.\n\n");
1097     }
1098
1099   /* We've warned you, now we'll keep quiet */
1100   if (alias)
1101     alias->flags &= ~DEPRECATED_WARN_USER;
1102   
1103   cmd->flags &= ~DEPRECATED_WARN_USER;
1104 }
1105
1106
1107
1108 /* Look up the contents of LINE as a command in the command list 'cmdlist'. 
1109    Return 1 on success, 0 on failure.
1110    
1111    If LINE refers to an alias, *alias will point to that alias.
1112    
1113    If LINE is a postfix command (i.e. one that is preceeded by a prefix
1114    command) set *prefix_cmd.
1115    
1116    Set *cmd to point to the command LINE indicates.
1117    
1118    If any of *alias, *prefix_cmd, or *cmd cannot be determined or do not 
1119    exist, they are NULL when we return.
1120    
1121 */
1122 int
1123 lookup_cmd_composition (char *text,
1124                       struct cmd_list_element **alias,
1125                       struct cmd_list_element **prefix_cmd, 
1126                       struct cmd_list_element **cmd)
1127 {
1128   char *p, *command;
1129   int len, tmp, nfound;
1130   struct cmd_list_element *cur_list;
1131   struct cmd_list_element *prev_cmd;
1132   *alias = NULL;
1133   *prefix_cmd = NULL;
1134   *cmd = NULL;
1135   
1136   cur_list = cmdlist;
1137   
1138   while (1)
1139     { 
1140       /* Go through as many command lists as we need to 
1141        to find the command TEXT refers to. */
1142       
1143       prev_cmd = *cmd;
1144       
1145       while (*text == ' ' || *text == '\t')
1146       (text)++;
1147       
1148       /* Treating underscores as part of command words is important
1149        so that "set args_foo()" doesn't get interpreted as
1150        "set args _foo()".  */
1151       for (p = text;
1152          *p && (isalnum (*p) || *p == '-' || *p == '_' ||
1153                 (tui_version &&
1154                  (*p == '+' || *p == '<' || *p == '>' || *p == '$')) ||
1155                 (xdb_commands && (*p == '!' || *p == '/' || *p == '?')));
1156          p++)
1157       ;
1158       
1159       /* If nothing but whitespace, return.  */
1160       if (p == text)
1161       return 0;
1162       
1163       len = p - text;
1164       
1165       /* text and p now bracket the first command word to lookup (and
1166        it's length is len).  We copy this into a local temporary */
1167       
1168       command = (char *) alloca (len + 1);
1169       for (tmp = 0; tmp < len; tmp++)
1170       {
1171         char x = text[tmp];
1172         command[tmp] = x;
1173       }
1174       command[len] = '\0';
1175       
1176       /* Look it up.  */
1177       *cmd = 0;
1178       nfound = 0;
1179       *cmd = find_cmd (command, len, cur_list, 1, &nfound);
1180       
1181       /* We didn't find the command in the entered case, so lower case it
1182        and search again.
1183       */
1184       if (!*cmd || nfound == 0)
1185       {
1186         for (tmp = 0; tmp < len; tmp++)
1187           {
1188             char x = command[tmp];
1189             command[tmp] = isupper (x) ? tolower (x) : x;
1190           }
1191         *cmd = find_cmd (command, len, cur_list, 1, &nfound);
1192       }
1193       
1194       if (*cmd == (struct cmd_list_element *) -1)
1195       {
1196         return 0;              /* ambiguous */
1197       }
1198       
1199       if (*cmd == NULL)
1200       return 0;                /* nothing found */
1201       else
1202       {
1203         if ((*cmd)->cmd_pointer)
1204           {
1205             /* cmd was actually an alias, we note that an alias was used 
1206                (by assigning *alais) and we set *cmd. 
1207              */
1208             *alias = *cmd;
1209             *cmd = (*cmd)->cmd_pointer;
1210           }
1211         *prefix_cmd = prev_cmd;
1212       }
1213       if ((*cmd)->prefixlist)
1214       cur_list = *(*cmd)->prefixlist;
1215       else
1216       return 1;
1217       
1218       text = p;
1219     }
1220 }
1221
1222
1223
1224
1225 #if 0
1226 /* Look up the contents of *LINE as a command in the command list LIST.
1227    LIST is a chain of struct cmd_list_element's.
1228    If it is found, return the struct cmd_list_element for that command
1229    and update *LINE to point after the command name, at the first argument.
1230    If not found, call error if ALLOW_UNKNOWN is zero
1231    otherwise (or if error returns) return zero.
1232    Call error if specified command is ambiguous,
1233    unless ALLOW_UNKNOWN is negative.
1234    CMDTYPE precedes the word "command" in the error message.  */
1235
1236 struct cmd_list_element *
1237 lookup_cmd (line, list, cmdtype, allow_unknown)
1238      char **line;
1239      struct cmd_list_element *list;
1240      char *cmdtype;
1241      int allow_unknown;
1242 {
1243   register char *p;
1244   register struct cmd_list_element *c, *found;
1245   int nfound;
1246   char ambbuf[100];
1247   char *processed_cmd;
1248   int i, cmd_len;
1249
1250   /* Skip leading whitespace.  */
1251
1252   while (**line == ' ' || **line == '\t')
1253     (*line)++;
1254
1255   /* Clear out trailing whitespace.  */
1256
1257   p = *line + strlen (*line);
1258   while (p != *line && (p[-1] == ' ' || p[-1] == '\t'))
1259     p--;
1260   *p = 0;
1261
1262   /* Find end of command name.  */
1263
1264   p = *line;
1265   while (*p == '-' || isalnum (*p))
1266     p++;
1267
1268   /* Look up the command name.
1269      If exact match, keep that.
1270      Otherwise, take command abbreviated, if unique.  Note that (in my
1271      opinion) a null string does *not* indicate ambiguity; simply the
1272      end of the argument.  */
1273
1274   if (p == *line)
1275     {
1276       if (!allow_unknown)
1277         error ("Lack of needed %scommand", cmdtype);
1278       return 0;
1279     }
1280
1281   /* Copy over to a local buffer, converting to lowercase on the way.
1282      This is in case the command being parsed is a subcommand which
1283      doesn't match anything, and that's ok.  We want the original
1284      untouched for the routine of the original command.  */
1285
1286   processed_cmd = (char *) alloca (p - *line + 1);
1287   for (cmd_len = 0; cmd_len < p - *line; cmd_len++)
1288     {
1289       char x = (*line)[cmd_len];
1290       if (isupper (x))
1291         processed_cmd[cmd_len] = tolower (x);
1292       else
1293         processed_cmd[cmd_len] = x;
1294     }
1295   processed_cmd[cmd_len] = '\0';
1296
1297   /* Check all possibilities in the current command list.  */
1298   found = 0;
1299   nfound = 0;
1300   for (c = list; c; c = c->next)
1301     {
1302       if (!strncmp (processed_cmd, c->name, cmd_len))
1303         {
1304           found = c;
1305           nfound++;
1306           if (c->name[cmd_len] == 0)
1307             {
1308               nfound = 1;
1309               break;
1310             }
1311         }
1312     }
1313
1314   /* Report error for undefined command name.  */
1315
1316   if (nfound != 1)
1317     {
1318       if (nfound > 1 && allow_unknown >= 0)
1319         {
1320           ambbuf[0] = 0;
1321           for (c = list; c; c = c->next)
1322             if (!strncmp (processed_cmd, c->name, cmd_len))
1323               {
1324                 if (strlen (ambbuf) + strlen (c->name) + 6 < sizeof ambbuf)
1325                   {
1326                     if (strlen (ambbuf))
1327                       strcat (ambbuf, ", ");
1328                     strcat (ambbuf, c->name);
1329                   }
1330                 else
1331                   {
1332                     strcat (ambbuf, "..");
1333                     break;
1334                   }
1335               }
1336           error ("Ambiguous %scommand \"%s\": %s.", cmdtype,
1337                  processed_cmd, ambbuf);
1338         }
1339       else if (!allow_unknown)
1340         error ("Undefined %scommand: \"%s\".", cmdtype, processed_cmd);
1341       return 0;
1342     }
1343
1344   /* Skip whitespace before the argument.  */
1345
1346   while (*p == ' ' || *p == '\t')
1347     p++;
1348   *line = p;
1349
1350   if (found->prefixlist && *p)
1351     {
1352       c = lookup_cmd (line, *found->prefixlist, found->prefixname,
1353                       found->allow_unknown);
1354       if (c)
1355         return c;
1356     }
1357
1358   return found;
1359 }
1360 #endif
1361
1362 /* Helper function for SYMBOL_COMPLETION_FUNCTION.  */
1363
1364 /* Return a vector of char pointers which point to the different
1365    possible completions in LIST of TEXT.  
1366
1367    WORD points in the same buffer as TEXT, and completions should be
1368    returned relative to this position.  For example, suppose TEXT is "foo"
1369    and we want to complete to "foobar".  If WORD is "oo", return
1370    "oobar"; if WORD is "baz/foo", return "baz/foobar".  */
1371
1372 char **
1373 complete_on_cmdlist (list, text, word)
1374      struct cmd_list_element *list;
1375      char *text;
1376      char *word;
1377 {
1378   struct cmd_list_element *ptr;
1379   char **matchlist;
1380   int sizeof_matchlist;
1381   int matches;
1382   int textlen = strlen (text);
1383
1384   sizeof_matchlist = 10;
1385   matchlist = (char **) xmalloc (sizeof_matchlist * sizeof (char *));
1386   matches = 0;
1387
1388   for (ptr = list; ptr; ptr = ptr->next)
1389     if (!strncmp (ptr->name, text, textlen)
1390         && !ptr->abbrev_flag
1391         && (ptr->function.cfunc
1392             || ptr->prefixlist))
1393       {
1394         if (matches == sizeof_matchlist)
1395           {
1396             sizeof_matchlist *= 2;
1397             matchlist = (char **) xrealloc ((char *) matchlist,
1398                                             (sizeof_matchlist
1399                                              * sizeof (char *)));
1400           }
1401
1402         matchlist[matches] = (char *)
1403           xmalloc (strlen (word) + strlen (ptr->name) + 1);
1404         if (word == text)
1405           strcpy (matchlist[matches], ptr->name);
1406         else if (word > text)
1407           {
1408             /* Return some portion of ptr->name.  */
1409             strcpy (matchlist[matches], ptr->name + (word - text));
1410           }
1411         else
1412           {
1413             /* Return some of text plus ptr->name.  */
1414             strncpy (matchlist[matches], word, text - word);
1415             matchlist[matches][text - word] = '\0';
1416             strcat (matchlist[matches], ptr->name);
1417           }
1418         ++matches;
1419       }
1420
1421   if (matches == 0)
1422     {
1423       free ((PTR) matchlist);
1424       matchlist = 0;
1425     }
1426   else
1427     {
1428       matchlist = (char **) xrealloc ((char *) matchlist, ((matches + 1)
1429                                                         * sizeof (char *)));
1430       matchlist[matches] = (char *) 0;
1431     }
1432
1433   return matchlist;
1434 }
1435
1436 /* Helper function for SYMBOL_COMPLETION_FUNCTION.  */
1437
1438 /* Return a vector of char pointers which point to the different
1439    possible completions in CMD of TEXT.  
1440
1441    WORD points in the same buffer as TEXT, and completions should be
1442    returned relative to this position.  For example, suppose TEXT is "foo"
1443    and we want to complete to "foobar".  If WORD is "oo", return
1444    "oobar"; if WORD is "baz/foo", return "baz/foobar".  */
1445
1446 char **
1447 complete_on_enum (enumlist, text, word)
1448      char **enumlist;
1449      char *text;
1450      char *word;
1451 {
1452   char **matchlist;
1453   int sizeof_matchlist;
1454   int matches;
1455   int textlen = strlen (text);
1456   int i;
1457   char *name;
1458
1459   sizeof_matchlist = 10;
1460   matchlist = (char **) xmalloc (sizeof_matchlist * sizeof (char *));
1461   matches = 0;
1462
1463   for (i = 0; (name = enumlist[i]) != NULL; i++)
1464     if (strncmp (name, text, textlen) == 0)
1465       {
1466         if (matches == sizeof_matchlist)
1467           {
1468             sizeof_matchlist *= 2;
1469             matchlist = (char **) xrealloc ((char *) matchlist,
1470                                             (sizeof_matchlist
1471                                              * sizeof (char *)));
1472           }
1473
1474         matchlist[matches] = (char *)
1475           xmalloc (strlen (word) + strlen (name) + 1);
1476         if (word == text)
1477           strcpy (matchlist[matches], name);
1478         else if (word > text)
1479           {
1480             /* Return some portion of name.  */
1481             strcpy (matchlist[matches], name + (word - text));
1482           }
1483         else
1484           {
1485             /* Return some of text plus name.  */
1486             strncpy (matchlist[matches], word, text - word);
1487             matchlist[matches][text - word] = '\0';
1488             strcat (matchlist[matches], name);
1489           }
1490         ++matches;
1491       }
1492
1493   if (matches == 0)
1494     {
1495       free ((PTR) matchlist);
1496       matchlist = 0;
1497     }
1498   else
1499     {
1500       matchlist = (char **) xrealloc ((char *) matchlist, ((matches + 1)
1501                                                         * sizeof (char *)));
1502       matchlist[matches] = (char *) 0;
1503     }
1504
1505   return matchlist;
1506 }
1507
1508 static int
1509 parse_binary_operation (arg)
1510      char *arg;
1511 {
1512   int length;
1513
1514   if (!arg || !*arg)
1515     return 1;
1516
1517   length = strlen (arg);
1518
1519   while (arg[length - 1] == ' ' || arg[length - 1] == '\t')
1520     length--;
1521
1522   if (!strncmp (arg, "on", length)
1523       || !strncmp (arg, "1", length)
1524       || !strncmp (arg, "yes", length))
1525     return 1;
1526   else if (!strncmp (arg, "off", length)
1527            || !strncmp (arg, "0", length)
1528            || !strncmp (arg, "no", length))
1529     return 0;
1530   else
1531     {
1532       error ("\"on\" or \"off\" expected.");
1533       return 0;
1534     }
1535 }
1536
1537 /* Do a "set" or "show" command.  ARG is NULL if no argument, or the text
1538    of the argument, and FROM_TTY is nonzero if this command is being entered
1539    directly by the user (i.e. these are just like any other
1540    command).  C is the command list element for the command.  */
1541 void
1542 do_setshow_command (arg, from_tty, c)
1543      char *arg;
1544      int from_tty;
1545      struct cmd_list_element *c;
1546 {
1547   if (c->type == set_cmd)
1548     {
1549       switch (c->var_type)
1550         {
1551         case var_string:
1552           {
1553             char *new;
1554             char *p;
1555             char *q;
1556             int ch;
1557
1558             if (arg == NULL)
1559               arg = "";
1560             new = (char *) xmalloc (strlen (arg) + 2);
1561             p = arg;
1562             q = new;
1563             while ((ch = *p++) != '\000')
1564               {
1565                 if (ch == '\\')
1566                   {
1567                     /* \ at end of argument is used after spaces
1568                        so they won't be lost.  */
1569                     /* This is obsolete now that we no longer strip
1570                        trailing whitespace and actually, the backslash
1571                        didn't get here in my test, readline or
1572                        something did something funky with a backslash
1573                        right before a newline.  */
1574                     if (*p == 0)
1575                       break;
1576                     ch = parse_escape (&p);
1577                     if (ch == 0)
1578                       break;    /* C loses */
1579                     else if (ch > 0)
1580                       *q++ = ch;
1581                   }
1582                 else
1583                   *q++ = ch;
1584               }
1585 #if 0
1586             if (*(p - 1) != '\\')
1587               *q++ = ' ';
1588 #endif
1589             *q++ = '\0';
1590             new = (char *) xrealloc (new, q - new);
1591             if (*(char **) c->var != NULL)
1592               free (*(char **) c->var);
1593             *(char **) c->var = new;
1594           }
1595           break;
1596         case var_string_noescape:
1597           if (arg == NULL)
1598             arg = "";
1599           if (*(char **) c->var != NULL)
1600             free (*(char **) c->var);
1601           *(char **) c->var = savestring (arg, strlen (arg));
1602           break;
1603         case var_filename:
1604           if (arg == NULL)
1605             error_no_arg ("filename to set it to.");
1606           if (*(char **) c->var != NULL)
1607             free (*(char **) c->var);
1608           *(char **) c->var = tilde_expand (arg);
1609           break;
1610         case var_boolean:
1611           *(int *) c->var = parse_binary_operation (arg);
1612           break;
1613         case var_uinteger:
1614           if (arg == NULL)
1615             error_no_arg ("integer to set it to.");
1616           *(unsigned int *) c->var = parse_and_eval_address (arg);
1617           if (*(unsigned int *) c->var == 0)
1618             *(unsigned int *) c->var = UINT_MAX;
1619           break;
1620         case var_integer:
1621           {
1622             unsigned int val;
1623             if (arg == NULL)
1624               error_no_arg ("integer to set it to.");
1625             val = parse_and_eval_address (arg);
1626             if (val == 0)
1627               *(int *) c->var = INT_MAX;
1628             else if (val >= INT_MAX)
1629               error ("integer %u out of range", val);
1630             else
1631               *(int *) c->var = val;
1632             break;
1633           }
1634         case var_zinteger:
1635           if (arg == NULL)
1636             error_no_arg ("integer to set it to.");
1637           *(int *) c->var = parse_and_eval_address (arg);
1638           break;
1639         case var_enum:
1640           {
1641             int i;
1642             int len;
1643             int nmatches;
1644             char *match = NULL;
1645             char *p;
1646
1647             /* if no argument was supplied, print an informative error message */
1648             if (arg == NULL)
1649               {
1650                 char msg[1024];
1651                 strcpy (msg, "Requires an argument. Valid arguments are ");
1652                 for (i = 0; c->enums[i]; i++)
1653                   {
1654                     if (i != 0)
1655                       strcat (msg, ", ");
1656                     strcat (msg, c->enums[i]);
1657                   }
1658                 strcat (msg, ".");
1659                 error (msg);
1660               }
1661
1662             p = strchr (arg, ' ');
1663
1664             if (p)
1665               len = p - arg;
1666             else
1667               len = strlen (arg);
1668
1669             nmatches = 0;
1670             for (i = 0; c->enums[i]; i++)
1671               if (strncmp (arg, c->enums[i], len) == 0)
1672                 {
1673                   match = c->enums[i];
1674                   nmatches++;
1675                 }
1676
1677             if (nmatches <= 0)
1678               error ("Undefined item: \"%s\".", arg);
1679
1680             if (nmatches > 1)
1681               error ("Ambiguous item \"%s\".", arg);
1682
1683             *(char **) c->var = match;
1684           }
1685           break;
1686         default:
1687           error ("gdb internal error: bad var_type in do_setshow_command");
1688         }
1689     }
1690   else if (c->type == show_cmd)
1691     {
1692 #ifdef UI_OUT
1693       struct cleanup *old_chain;
1694       struct ui_stream *stb;
1695       int quote;
1696
1697       stb = ui_out_stream_new (uiout);
1698       old_chain = make_cleanup ((make_cleanup_func) ui_out_stream_delete, stb);
1699 #endif /* UI_OUT */
1700
1701       /* Print doc minus "show" at start.  */
1702       print_doc_line (gdb_stdout, c->doc + 5);
1703
1704 #ifdef UI_OUT
1705       ui_out_text (uiout, " is ");
1706       ui_out_wrap_hint (uiout, "    ");
1707       quote = 0;
1708       switch (c->var_type)
1709         {
1710         case var_string:
1711           {
1712             unsigned char *p;
1713
1714             if (*(unsigned char **) c->var)
1715               fputstr_filtered (*(unsigned char **) c->var, '"', stb->stream);
1716             quote = 1;
1717           }
1718           break;
1719         case var_string_noescape:
1720         case var_filename:
1721         case var_enum:
1722           if (*(char **) c->var)
1723             fputs_filtered (*(char **) c->var, stb->stream);
1724           quote = 1;
1725           break;
1726         case var_boolean:
1727           fputs_filtered (*(int *) c->var ? "on" : "off", stb->stream);
1728           break;
1729         case var_uinteger:
1730           if (*(unsigned int *) c->var == UINT_MAX)
1731             {
1732               fputs_filtered ("unlimited", stb->stream);
1733               break;
1734             }
1735           /* else fall through */
1736         case var_zinteger:
1737           fprintf_filtered (stb->stream, "%u", *(unsigned int *) c->var);
1738           break;
1739         case var_integer:
1740           if (*(int *) c->var == INT_MAX)
1741             {
1742               fputs_filtered ("unlimited", stb->stream);
1743             }
1744           else
1745             fprintf_filtered (stb->stream, "%d", *(int *) c->var);
1746           break;
1747
1748         default:
1749           error ("gdb internal error: bad var_type in do_setshow_command");
1750         }
1751       if (quote)
1752         ui_out_text (uiout, "\"");
1753       ui_out_field_stream (uiout, "value", stb);
1754       if (quote)
1755         ui_out_text (uiout, "\"");
1756       ui_out_text (uiout, ".\n");
1757       do_cleanups (old_chain);
1758 #else
1759       fputs_filtered (" is ", gdb_stdout);
1760       wrap_here ("    ");
1761       switch (c->var_type)
1762         {
1763         case var_string:
1764           {
1765             fputs_filtered ("\"", gdb_stdout);
1766             if (*(unsigned char **) c->var)
1767               fputstr_filtered (*(unsigned char **) c->var, '"', gdb_stdout);
1768             fputs_filtered ("\"", gdb_stdout);
1769           }
1770           break;
1771         case var_string_noescape:
1772         case var_filename:
1773         case var_enum:
1774           fputs_filtered ("\"", gdb_stdout);
1775           if (*(char **) c->var)
1776             fputs_filtered (*(char **) c->var, gdb_stdout);
1777           fputs_filtered ("\"", gdb_stdout);
1778           break;
1779         case var_boolean:
1780           fputs_filtered (*(int *) c->var ? "on" : "off", gdb_stdout);
1781           break;
1782         case var_uinteger:
1783           if (*(unsigned int *) c->var == UINT_MAX)
1784             {
1785               fputs_filtered ("unlimited", gdb_stdout);
1786               break;
1787             }
1788           /* else fall through */
1789         case var_zinteger:
1790           fprintf_filtered (gdb_stdout, "%u", *(unsigned int *) c->var);
1791           break;
1792         case var_integer:
1793           if (*(int *) c->var == INT_MAX)
1794             {
1795               fputs_filtered ("unlimited", gdb_stdout);
1796             }
1797           else
1798             fprintf_filtered (gdb_stdout, "%d", *(int *) c->var);
1799           break;
1800
1801         default:
1802           error ("gdb internal error: bad var_type in do_setshow_command");
1803         }
1804       fputs_filtered (".\n", gdb_stdout);
1805 #endif
1806     }
1807   else
1808     error ("gdb internal error: bad cmd_type in do_setshow_command");
1809   (*c->function.sfunc) (NULL, from_tty, c);
1810   if (c->type == set_cmd && set_hook)
1811     set_hook (c);
1812 }
1813
1814 /* Show all the settings in a list of show commands.  */
1815
1816 void
1817 cmd_show_list (list, from_tty, prefix)
1818      struct cmd_list_element *list;
1819      int from_tty;
1820      char *prefix;
1821 {
1822 #ifdef UI_OUT
1823   ui_out_list_begin (uiout, "showlist");
1824 #endif
1825   for (; list != NULL; list = list->next)
1826     {
1827       /* If we find a prefix, run its list, prefixing our output by its
1828          prefix (with "show " skipped).  */
1829 #ifdef UI_OUT
1830       if (list->prefixlist && !list->abbrev_flag)
1831         {
1832           ui_out_list_begin (uiout, "optionlist");
1833           ui_out_field_string (uiout, "prefix", list->prefixname + 5);
1834           cmd_show_list (*list->prefixlist, from_tty, list->prefixname + 5);
1835           ui_out_list_end (uiout);
1836         }
1837       if (list->type == show_cmd)
1838         {
1839           ui_out_list_begin (uiout, "option");
1840           ui_out_text (uiout, prefix);
1841           ui_out_field_string (uiout, "name", list->name);
1842           ui_out_text (uiout, ":  ");
1843           do_setshow_command ((char *) NULL, from_tty, list);
1844           ui_out_list_end (uiout);
1845         }
1846 #else
1847       if (list->prefixlist && !list->abbrev_flag)
1848         cmd_show_list (*list->prefixlist, from_tty, list->prefixname + 5);
1849       if (list->type == show_cmd)
1850         {
1851           fputs_filtered (prefix, gdb_stdout);
1852           fputs_filtered (list->name, gdb_stdout);
1853           fputs_filtered (":  ", gdb_stdout);
1854           do_setshow_command ((char *) NULL, from_tty, list);
1855         }
1856 #endif
1857     }
1858 #ifdef UI_OUT
1859   ui_out_list_end (uiout);
1860 #endif
1861 }
1862
1863 /* ARGSUSED */
1864 static void
1865 shell_escape (arg, from_tty)
1866      char *arg;
1867      int from_tty;
1868 {
1869 #ifdef CANT_FORK
1870   /* If ARG is NULL, they want an inferior shell, but `system' just
1871      reports if the shell is available when passed a NULL arg.  */
1872   int rc = system (arg ? arg : "");
1873
1874   if (!arg)
1875     arg = "inferior shell";
1876
1877   if (rc == -1)
1878     {
1879       fprintf_unfiltered (gdb_stderr, "Cannot execute %s: %s\n", arg,
1880                           safe_strerror (errno));
1881       gdb_flush (gdb_stderr);
1882     }
1883   else if (rc)
1884     {
1885       fprintf_unfiltered (gdb_stderr, "%s exited with status %d\n", arg, rc);
1886       gdb_flush (gdb_stderr);
1887     }
1888 #ifdef __DJGPP__
1889   /* Make sure to return to the directory GDB thinks it is, in case the
1890      shell command we just ran changed it.  */
1891   chdir (current_directory);
1892 #endif
1893 #else /* Can fork.  */
1894   int rc, status, pid;
1895   char *p, *user_shell;
1896
1897   if ((user_shell = (char *) getenv ("SHELL")) == NULL)
1898     user_shell = "/bin/sh";
1899
1900   /* Get the name of the shell for arg0 */
1901   if ((p = strrchr (user_shell, '/')) == NULL)
1902     p = user_shell;
1903   else
1904     p++;                        /* Get past '/' */
1905
1906   if ((pid = fork ()) == 0)
1907     {
1908       if (!arg)
1909         execl (user_shell, p, 0);
1910       else
1911         execl (user_shell, p, "-c", arg, 0);
1912
1913       fprintf_unfiltered (gdb_stderr, "Cannot execute %s: %s\n", user_shell,
1914                           safe_strerror (errno));
1915       gdb_flush (gdb_stderr);
1916       _exit (0177);
1917     }
1918
1919   if (pid != -1)
1920     while ((rc = wait (&status)) != pid && rc != -1)
1921       ;
1922   else
1923     error ("Fork failed");
1924 #endif /* Can fork.  */
1925 }
1926
1927 static void
1928 make_command (arg, from_tty)
1929      char *arg;
1930      int from_tty;
1931 {
1932   char *p;
1933
1934   if (arg == 0)
1935     p = "make";
1936   else
1937     {
1938       p = xmalloc (sizeof ("make ") + strlen (arg));
1939       strcpy (p, "make ");
1940       strcpy (p + sizeof ("make ") - 1, arg);
1941     }
1942
1943   shell_escape (p, from_tty);
1944 }
1945
1946 static void
1947 show_user_1 (c, stream)
1948      struct cmd_list_element *c;
1949      struct ui_file *stream;
1950 {
1951   register struct command_line *cmdlines;
1952
1953   cmdlines = c->user_commands;
1954   if (!cmdlines)
1955     return;
1956   fputs_filtered ("User command ", stream);
1957   fputs_filtered (c->name, stream);
1958   fputs_filtered (":\n", stream);
1959
1960 #ifdef UI_OUT
1961   print_command_lines (uiout, cmdlines, 1);
1962   fputs_filtered ("\n", stream);
1963 #else
1964   while (cmdlines)
1965     {
1966       print_command_line (cmdlines, 4, stream);
1967       cmdlines = cmdlines->next;
1968     }
1969   fputs_filtered ("\n", stream);
1970 #endif
1971 }
1972
1973 /* ARGSUSED */
1974 static void
1975 show_user (args, from_tty)
1976      char *args;
1977      int from_tty;
1978 {
1979   struct cmd_list_element *c;
1980   extern struct cmd_list_element *cmdlist;
1981
1982   if (args)
1983     {
1984       c = lookup_cmd (&args, cmdlist, "", 0, 1);
1985       if (c->class != class_user)
1986         error ("Not a user command.");
1987       show_user_1 (c, gdb_stdout);
1988     }
1989   else
1990     {
1991       for (c = cmdlist; c; c = c->next)
1992         {
1993           if (c->class == class_user)
1994             show_user_1 (c, gdb_stdout);
1995         }
1996     }
1997 }
1998
1999 void
2000 _initialize_command ()
2001 {
2002   add_com ("shell", class_support, shell_escape,
2003            "Execute the rest of the line as a shell command.  \n\
2004 With no arguments, run an inferior shell.");
2005
2006   /* NOTE: cagney/2000-03-20: Being able to enter ``(gdb) !ls'' would
2007      be a really useful feature.  Unfortunatly, the below wont do
2008      this.  Instead it adds support for the form ``(gdb) ! ls''
2009      (i.e. the space is required).  If the ``!'' command below is
2010      added the complains about no ``!'' command would be replaced by
2011      complains about how the ``!'' command is broken :-) */
2012   if (xdb_commands)
2013     add_com_alias ("!", "shell", class_support, 0);
2014
2015   add_com ("make", class_support, make_command,
2016        "Run the ``make'' program using the rest of the line as arguments.");
2017   add_cmd ("user", no_class, show_user,
2018            "Show definitions of user defined commands.\n\
2019 Argument is the name of the user defined command.\n\
2020 With no argument, show definitions of all user defined commands.", &showlist);
2021   add_com ("apropos", class_support, apropos_command, "Search for commands matching a REGEXP");
2022 }
This page took 0.139452 seconds and 4 git commands to generate.