3821fafb186f1bbb924ef5ad28de2ebb07902508
[external/binutils.git] / gdb / cli / cli-decode.c
1 /* Handle lists of commands, their decoding and documentation, for GDB.
2
3    Copyright 1986, 1989, 1990, 1991, 1998, 2000, 2001, 2002 Free
4    Software Foundation, Inc.
5
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 2 of the License, or
9    (at your option) any later version.
10
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15
16    You should have received a copy of the GNU General Public License
17    along with this program; if not, write to the Free Software
18    Foundation, Inc., 59 Temple Place - Suite 330,
19    Boston, MA 02111-1307, USA.  */
20
21 #include "defs.h"
22 #include "symtab.h"
23 #include <ctype.h>
24 #include "gdb_regex.h"
25
26 #include "ui-out.h"
27
28 #include "cli/cli-cmds.h"
29 #include "cli/cli-decode.h"
30
31 /* Prototypes for local functions */
32
33 static void undef_cmd_error (char *, char *);
34
35 static struct cmd_list_element *find_cmd (char *command,
36                                           int len,
37                                           struct cmd_list_element *clist,
38                                           int ignore_help_classes,
39                                           int *nfound);
40
41 static void help_all (struct ui_file *stream);
42 \f
43 /* Add element named NAME.
44    CLASS is the top level category into which commands are broken down
45    for "help" purposes.
46    FUN should be the function to execute the command;
47    it will get a character string as argument, with leading
48    and trailing blanks already eliminated.
49
50    DOC is a documentation string for the command.
51    Its first line should be a complete sentence.
52    It should start with ? for a command that is an abbreviation
53    or with * for a command that most users don't need to know about.
54
55    Add this command to command list *LIST.  
56
57    Returns a pointer to the added command (not necessarily the head 
58    of *LIST). */
59
60 struct cmd_list_element *
61 add_cmd (char *name, enum command_class class, void (*fun) (char *, int),
62          char *doc, struct cmd_list_element **list)
63 {
64   register struct cmd_list_element *c
65   = (struct cmd_list_element *) xmalloc (sizeof (struct cmd_list_element));
66   struct cmd_list_element *p;
67
68   delete_cmd (name, list);
69
70   if (*list == NULL || strcmp ((*list)->name, name) >= 0)
71     {
72       c->next = *list;
73       *list = c;
74     }
75   else
76     {
77       p = *list;
78       while (p->next && strcmp (p->next->name, name) <= 0)
79         {
80           p = p->next;
81         }
82       c->next = p->next;
83       p->next = c;
84     }
85
86   c->name = name;
87   c->class = class;
88   c->function.cfunc = fun;
89   c->doc = doc;
90   c->flags = 0;
91   c->replacement = NULL;
92   c->pre_show_hook = NULL;
93   c->hook_pre  = NULL;
94   c->hook_post = NULL;
95   c->hook_in = 0;
96   c->prefixlist = NULL;
97   c->prefixname = NULL;
98   c->allow_unknown = 0;
99   c->abbrev_flag = 0;
100   c->completer = make_symbol_completion_list;
101   c->type = not_set_cmd;
102   c->var = NULL;
103   c->var_type = var_boolean;
104   c->enums = NULL;
105   c->user_commands = NULL;
106   c->hookee_pre = NULL;
107   c->hookee_post = NULL;
108   c->cmd_pointer = NULL;
109
110   return c;
111 }
112
113 /* Same as above, except that the abbrev_flag is set. */
114 /* Note: Doesn't seem to be used anywhere currently. */
115
116 struct cmd_list_element *
117 add_abbrev_cmd (char *name, enum command_class class, void (*fun) (char *, int),
118                 char *doc, struct cmd_list_element **list)
119 {
120   register struct cmd_list_element *c
121   = add_cmd (name, class, fun, doc, list);
122
123   c->abbrev_flag = 1;
124   return c;
125 }
126
127 /* Deprecates a command CMD.
128    REPLACEMENT is the name of the command which should be used in place
129    of this command, or NULL if no such command exists.
130
131    This function does not check to see if command REPLACEMENT exists
132    since gdb may not have gotten around to adding REPLACEMENT when this
133    function is called.
134
135    Returns a pointer to the deprecated command.  */
136
137 struct cmd_list_element *
138 deprecate_cmd (struct cmd_list_element *cmd, char *replacement)
139 {
140   cmd->flags |= (CMD_DEPRECATED | DEPRECATED_WARN_USER);
141
142   if (replacement != NULL)
143     cmd->replacement = replacement;
144   else
145     cmd->replacement = NULL;
146
147   return cmd;
148 }
149
150 struct cmd_list_element *
151 add_alias_cmd (char *name, char *oldname, enum command_class class,
152                int abbrev_flag, struct cmd_list_element **list)
153 {
154   /* Must do this since lookup_cmd tries to side-effect its first arg */
155   char *copied_name;
156   register struct cmd_list_element *old;
157   register struct cmd_list_element *c;
158   copied_name = (char *) alloca (strlen (oldname) + 1);
159   strcpy (copied_name, oldname);
160   old = lookup_cmd (&copied_name, *list, "", 1, 1);
161
162   if (old == 0)
163     {
164       delete_cmd (name, list);
165       return 0;
166     }
167
168   c = add_cmd (name, class, old->function.cfunc, old->doc, list);
169   c->prefixlist = old->prefixlist;
170   c->prefixname = old->prefixname;
171   c->allow_unknown = old->allow_unknown;
172   c->abbrev_flag = abbrev_flag;
173   c->cmd_pointer = old;
174   return c;
175 }
176
177 /* Like add_cmd but adds an element for a command prefix:
178    a name that should be followed by a subcommand to be looked up
179    in another command list.  PREFIXLIST should be the address
180    of the variable containing that list.  */
181
182 struct cmd_list_element *
183 add_prefix_cmd (char *name, enum command_class class, void (*fun) (char *, int),
184                 char *doc, struct cmd_list_element **prefixlist,
185                 char *prefixname, int allow_unknown,
186                 struct cmd_list_element **list)
187 {
188   register struct cmd_list_element *c = add_cmd (name, class, fun, doc, list);
189   c->prefixlist = prefixlist;
190   c->prefixname = prefixname;
191   c->allow_unknown = allow_unknown;
192   return c;
193 }
194
195 /* Like add_prefix_cmd but sets the abbrev_flag on the new command. */
196
197 struct cmd_list_element *
198 add_abbrev_prefix_cmd (char *name, enum command_class class,
199                        void (*fun) (char *, int), char *doc,
200                        struct cmd_list_element **prefixlist, char *prefixname,
201                        int allow_unknown, struct cmd_list_element **list)
202 {
203   register struct cmd_list_element *c = add_cmd (name, class, fun, doc, list);
204   c->prefixlist = prefixlist;
205   c->prefixname = prefixname;
206   c->allow_unknown = allow_unknown;
207   c->abbrev_flag = 1;
208   return c;
209 }
210
211 /* This is an empty "cfunc".  */
212 void
213 not_just_help_class_command (char *args, int from_tty)
214 {
215 }
216
217 /* This is an empty "sfunc".  */
218 static void empty_sfunc (char *, int, struct cmd_list_element *);
219
220 static void
221 empty_sfunc (char *args, int from_tty, struct cmd_list_element *c)
222 {
223 }
224
225 /* Add element named NAME to command list LIST (the list for set
226    or some sublist thereof).
227    CLASS is as in add_cmd.
228    VAR_TYPE is the kind of thing we are setting.
229    VAR is address of the variable being controlled by this command.
230    DOC is the documentation string.  */
231
232 struct cmd_list_element *
233 add_set_cmd (char *name,
234              enum command_class class,
235              var_types var_type,
236              void *var,
237              char *doc,
238              struct cmd_list_element **list)
239 {
240   struct cmd_list_element *c = add_cmd (name, class, NULL, doc, list);
241
242   c->type = set_cmd;
243   c->var_type = var_type;
244   c->var = var;
245   /* This needs to be something besides NULL so that this isn't
246      treated as a help class.  */
247   c->function.sfunc = empty_sfunc;
248   return c;
249 }
250
251 /* Add element named NAME to command list LIST (the list for set
252    or some sublist thereof).
253    CLASS is as in add_cmd.
254    ENUMLIST is a list of strings which may follow NAME.
255    VAR is address of the variable which will contain the matching string
256    (from ENUMLIST).
257    DOC is the documentation string.  */
258
259 struct cmd_list_element *
260 add_set_enum_cmd (char *name,
261                   enum command_class class,
262                   const char *enumlist[],
263                   const char **var,
264                   char *doc,
265                   struct cmd_list_element **list)
266 {
267   struct cmd_list_element *c
268   = add_set_cmd (name, class, var_enum, var, doc, list);
269   c->enums = enumlist;
270
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    VAR is address of the variable which will contain the value.
278    DOC is the documentation string.  */
279 struct cmd_list_element *
280 add_set_auto_boolean_cmd (char *name,
281                           enum command_class class,
282                           enum cmd_auto_boolean *var,
283                           char *doc,
284                           struct cmd_list_element **list)
285 {
286   static const char *auto_boolean_enums[] = { "on", "off", "auto", NULL };
287   struct cmd_list_element *c;
288   c = add_set_cmd (name, class, var_auto_boolean, var, doc, list);
289   c->enums = auto_boolean_enums;
290   return c;
291 }
292
293 /* Add element named NAME to command list LIST (the list for set
294    or some sublist thereof).
295    CLASS is as in add_cmd.
296    VAR is address of the variable which will contain the value.
297    DOC is the documentation string.  */
298 struct cmd_list_element *
299 add_set_boolean_cmd (char *name,
300                      enum command_class class,
301                      int *var,
302                      char *doc,
303                      struct cmd_list_element **list)
304 {
305   static const char *boolean_enums[] = { "on", "off", NULL };
306   struct cmd_list_element *c;
307   c = add_set_cmd (name, class, var_boolean, var, doc, list);
308   c->enums = boolean_enums;
309   return c;
310 }
311
312 /* Where SETCMD has already been added, add the corresponding show
313    command to LIST and return a pointer to the added command (not 
314    necessarily the head of LIST).  */
315 struct cmd_list_element *
316 add_show_from_set (struct cmd_list_element *setcmd,
317                    struct cmd_list_element **list)
318 {
319   struct cmd_list_element *showcmd =
320   (struct cmd_list_element *) xmalloc (sizeof (struct cmd_list_element));
321   struct cmd_list_element *p;
322
323   memcpy (showcmd, setcmd, sizeof (struct cmd_list_element));
324   delete_cmd (showcmd->name, list);
325   showcmd->type = show_cmd;
326
327   /* Replace "set " at start of docstring with "show ".  */
328   if (setcmd->doc[0] == 'S' && setcmd->doc[1] == 'e'
329       && setcmd->doc[2] == 't' && setcmd->doc[3] == ' ')
330     showcmd->doc = concat ("Show ", setcmd->doc + 4, NULL);
331   else
332     fprintf_unfiltered (gdb_stderr, "GDB internal error: Bad docstring for set command\n");
333
334   if (*list == NULL || strcmp ((*list)->name, showcmd->name) >= 0)
335     {
336       showcmd->next = *list;
337       *list = showcmd;
338     }
339   else
340     {
341       p = *list;
342       while (p->next && strcmp (p->next->name, showcmd->name) <= 0)
343         {
344           p = p->next;
345         }
346       showcmd->next = p->next;
347       p->next = showcmd;
348     }
349
350   return showcmd;
351 }
352
353 /* Remove the command named NAME from the command list.  */
354
355 void
356 delete_cmd (char *name, struct cmd_list_element **list)
357 {
358   register struct cmd_list_element *c;
359   struct cmd_list_element *p;
360
361   while (*list && STREQ ((*list)->name, name))
362     {
363       if ((*list)->hookee_pre)
364       (*list)->hookee_pre->hook_pre = 0;   /* Hook slips out of its mouth */
365       if ((*list)->hookee_post)
366       (*list)->hookee_post->hook_post = 0; /* Hook slips out of its bottom  */
367       p = (*list)->next;
368       xfree (* list);
369       *list = p;
370     }
371
372   if (*list)
373     for (c = *list; c->next;)
374       {
375         if (STREQ (c->next->name, name))
376           {
377           if (c->next->hookee_pre)
378             c->next->hookee_pre->hook_pre = 0; /* hooked cmd gets away.  */
379           if (c->next->hookee_post)
380             c->next->hookee_post->hook_post = 0; /* remove post hook */
381                                                /* :( no fishing metaphore */
382             p = c->next->next;
383             xfree (c->next);
384             c->next = p;
385           }
386         else
387           c = c->next;
388       }
389 }
390 \f
391 /* Shorthands to the commands above. */
392
393 /* Add an element to the list of info subcommands.  */
394
395 struct cmd_list_element *
396 add_info (char *name, void (*fun) (char *, int), char *doc)
397 {
398   return add_cmd (name, no_class, fun, doc, &infolist);
399 }
400
401 /* Add an alias to the list of info subcommands.  */
402
403 struct cmd_list_element *
404 add_info_alias (char *name, char *oldname, int abbrev_flag)
405 {
406   return add_alias_cmd (name, oldname, 0, abbrev_flag, &infolist);
407 }
408
409 /* Add an element to the list of commands.  */
410
411 struct cmd_list_element *
412 add_com (char *name, enum command_class class, void (*fun) (char *, int),
413          char *doc)
414 {
415   return add_cmd (name, class, fun, doc, &cmdlist);
416 }
417
418 /* Add an alias or abbreviation command to the list of commands.  */
419
420 struct cmd_list_element *
421 add_com_alias (char *name, char *oldname, enum command_class class,
422                int abbrev_flag)
423 {
424   return add_alias_cmd (name, oldname, class, abbrev_flag, &cmdlist);
425 }
426 \f
427 /* Recursively walk the commandlist structures, and print out the
428    documentation of commands that match our regex in either their
429    name, or their documentation.
430 */
431 void 
432 apropos_cmd (struct ui_file *stream, struct cmd_list_element *commandlist,
433                          struct re_pattern_buffer *regex, char *prefix)
434 {
435   register struct cmd_list_element *c;
436   int returnvalue=1; /*Needed to avoid double printing*/
437   /* Walk through the commands */
438   for (c=commandlist;c;c=c->next)
439     {
440       if (c->name != NULL)
441         {
442           /* Try to match against the name*/
443           returnvalue=re_search(regex,c->name,strlen(c->name),0,strlen(c->name),NULL);
444           if (returnvalue >= 0)
445             {
446               /* Stolen from help_cmd_list. We don't directly use
447                * help_cmd_list because it doesn't let us print out
448                * single commands
449                */
450               fprintf_filtered (stream, "%s%s -- ", prefix, c->name);
451               print_doc_line (stream, c->doc);
452               fputs_filtered ("\n", stream);
453               returnvalue=0; /*Set this so we don't print it again.*/
454             }
455         }
456       if (c->doc != NULL && returnvalue != 0)
457         {
458           /* Try to match against documentation */
459           if (re_search(regex,c->doc,strlen(c->doc),0,strlen(c->doc),NULL) >=0)
460             {
461               /* Stolen from help_cmd_list. We don't directly use
462                * help_cmd_list because it doesn't let us print out
463                * single commands
464                */
465               fprintf_filtered (stream, "%s%s -- ", prefix, c->name);
466               print_doc_line (stream, c->doc);
467               fputs_filtered ("\n", stream);
468             }
469         }
470       /* Check if this command has subcommands */
471       if (c->prefixlist != NULL)
472         {
473           /* Recursively call ourselves on the subcommand list,
474              passing the right prefix in.
475           */
476           apropos_cmd (stream,*c->prefixlist,regex,c->prefixname);
477         }
478     }
479 }
480
481 /* This command really has to deal with two things:
482  *     1) I want documentation on *this string* (usually called by
483  * "help commandname").
484  *     2) I want documentation on *this list* (usually called by
485  * giving a command that requires subcommands.  Also called by saying
486  * just "help".)
487  *
488  *   I am going to split this into two seperate comamnds, help_cmd and
489  * help_list. 
490  */
491
492 void
493 help_cmd (char *command, struct ui_file *stream)
494 {
495   struct cmd_list_element *c;
496   extern struct cmd_list_element *cmdlist;
497
498   if (!command)
499     {
500       help_list (cmdlist, "", all_classes, stream);
501       return;
502     }
503
504   if (strcmp (command, "all") == 0)
505     {
506       help_all (stream);
507       return;
508     }
509
510   c = lookup_cmd (&command, cmdlist, "", 0, 0);
511
512   if (c == 0)
513     return;
514
515   /* There are three cases here.
516      If c->prefixlist is nonzero, we have a prefix command.
517      Print its documentation, then list its subcommands.
518
519      If c->function is nonzero, we really have a command.
520      Print its documentation and return.
521
522      If c->function is zero, we have a class name.
523      Print its documentation (as if it were a command)
524      and then set class to the number of this class
525      so that the commands in the class will be listed.  */
526
527   fputs_filtered (c->doc, stream);
528   fputs_filtered ("\n", stream);
529
530   if (c->prefixlist == 0 && c->function.cfunc != NULL)
531     return;
532   fprintf_filtered (stream, "\n");
533
534   /* If this is a prefix command, print it's subcommands */
535   if (c->prefixlist)
536     help_list (*c->prefixlist, c->prefixname, all_commands, stream);
537
538   /* If this is a class name, print all of the commands in the class */
539   if (c->function.cfunc == NULL)
540     help_list (cmdlist, "", c->class, stream);
541
542   if (c->hook_pre || c->hook_post)
543     fprintf_filtered (stream,
544                       "\nThis command has a hook (or hooks) defined:\n");
545
546   if (c->hook_pre)
547     fprintf_filtered (stream, 
548                       "\tThis command is run after  : %s (pre hook)\n",
549                     c->hook_pre->name);
550   if (c->hook_post)
551     fprintf_filtered (stream, 
552                       "\tThis command is run before : %s (post hook)\n",
553                     c->hook_post->name);
554 }
555
556 /*
557  * Get a specific kind of help on a command list.
558  *
559  * LIST is the list.
560  * CMDTYPE is the prefix to use in the title string.
561  * CLASS is the class with which to list the nodes of this list (see
562  * documentation for help_cmd_list below),  As usual, ALL_COMMANDS for
563  * everything, ALL_CLASSES for just classes, and non-negative for only things
564  * in a specific class.
565  * and STREAM is the output stream on which to print things.
566  * If you call this routine with a class >= 0, it recurses.
567  */
568 void
569 help_list (struct cmd_list_element *list, char *cmdtype,
570            enum command_class class, struct ui_file *stream)
571 {
572   int len;
573   char *cmdtype1, *cmdtype2;
574
575   /* If CMDTYPE is "foo ", CMDTYPE1 gets " foo" and CMDTYPE2 gets "foo sub"  */
576   len = strlen (cmdtype);
577   cmdtype1 = (char *) alloca (len + 1);
578   cmdtype1[0] = 0;
579   cmdtype2 = (char *) alloca (len + 4);
580   cmdtype2[0] = 0;
581   if (len)
582     {
583       cmdtype1[0] = ' ';
584       strncpy (cmdtype1 + 1, cmdtype, len - 1);
585       cmdtype1[len] = 0;
586       strncpy (cmdtype2, cmdtype, len - 1);
587       strcpy (cmdtype2 + len - 1, " sub");
588     }
589
590   if (class == all_classes)
591     fprintf_filtered (stream, "List of classes of %scommands:\n\n", cmdtype2);
592   else
593     fprintf_filtered (stream, "List of %scommands:\n\n", cmdtype2);
594
595   help_cmd_list (list, class, cmdtype, (int) class >= 0, stream);
596
597   if (class == all_classes)
598     fprintf_filtered (stream, "\n\
599 Type \"help%s\" followed by a class name for a list of commands in that class.",
600                       cmdtype1);
601
602   fprintf_filtered (stream, "\n\
603 Type \"help%s\" followed by %scommand name for full documentation.\n\
604 Command name abbreviations are allowed if unambiguous.\n",
605                     cmdtype1, cmdtype2);
606 }
607
608 static void
609 help_all (struct ui_file *stream)
610 {
611   struct cmd_list_element *c;
612   extern struct cmd_list_element *cmdlist;
613
614   for (c = cmdlist; c; c = c->next)
615     {
616       if (c->abbrev_flag)
617         continue;
618       /* If this is a prefix command, print it's subcommands */
619       if (c->prefixlist)
620         help_cmd_list (*c->prefixlist, all_commands, c->prefixname, 0, stream);
621     
622       /* If this is a class name, print all of the commands in the class */
623       else if (c->function.cfunc == NULL)
624         help_cmd_list (cmdlist, c->class, "", 0, stream);
625     }
626 }
627
628 /* Print only the first line of STR on STREAM.  */
629 void
630 print_doc_line (struct ui_file *stream, char *str)
631 {
632   static char *line_buffer = 0;
633   static int line_size;
634   register char *p;
635
636   if (!line_buffer)
637     {
638       line_size = 80;
639       line_buffer = (char *) xmalloc (line_size);
640     }
641
642   p = str;
643   while (*p && *p != '\n' && *p != '.' && *p != ',')
644     p++;
645   if (p - str > line_size - 1)
646     {
647       line_size = p - str + 1;
648       xfree (line_buffer);
649       line_buffer = (char *) xmalloc (line_size);
650     }
651   strncpy (line_buffer, str, p - str);
652   line_buffer[p - str] = '\0';
653   if (islower (line_buffer[0]))
654     line_buffer[0] = toupper (line_buffer[0]);
655   ui_out_text (uiout, line_buffer);
656 }
657
658 /*
659  * Implement a help command on command list LIST.
660  * RECURSE should be non-zero if this should be done recursively on
661  * all sublists of LIST.
662  * PREFIX is the prefix to print before each command name.
663  * STREAM is the stream upon which the output should be written.
664  * CLASS should be:
665  *      A non-negative class number to list only commands in that
666  * class.
667  *      ALL_COMMANDS to list all commands in list.
668  *      ALL_CLASSES  to list all classes in list.
669  *
670  *   Note that RECURSE will be active on *all* sublists, not just the
671  * ones selected by the criteria above (ie. the selection mechanism
672  * is at the low level, not the high-level).
673  */
674 void
675 help_cmd_list (struct cmd_list_element *list, enum command_class class,
676                char *prefix, int recurse, struct ui_file *stream)
677 {
678   register struct cmd_list_element *c;
679
680   for (c = list; c; c = c->next)
681     {
682       if (c->abbrev_flag == 0 &&
683           (class == all_commands
684            || (class == all_classes && c->function.cfunc == NULL)
685            || (class == c->class && c->function.cfunc != NULL)))
686         {
687           fprintf_filtered (stream, "%s%s -- ", prefix, c->name);
688           print_doc_line (stream, c->doc);
689           fputs_filtered ("\n", stream);
690         }
691       if (recurse
692           && c->prefixlist != 0
693           && c->abbrev_flag == 0)
694         help_cmd_list (*c->prefixlist, class, c->prefixname, 1, stream);
695     }
696 }
697 \f
698
699 /* Search the input clist for 'command'.  Return the command if
700    found (or NULL if not), and return the number of commands
701    found in nfound */
702
703 static struct cmd_list_element *
704 find_cmd (char *command, int len, struct cmd_list_element *clist,
705           int ignore_help_classes, int *nfound)
706 {
707   struct cmd_list_element *found, *c;
708
709   found = (struct cmd_list_element *) NULL;
710   *nfound = 0;
711   for (c = clist; c; c = c->next)
712     if (!strncmp (command, c->name, len)
713         && (!ignore_help_classes || c->function.cfunc))
714       {
715         found = c;
716         (*nfound)++;
717         if (c->name[len] == '\0')
718           {
719             *nfound = 1;
720             break;
721           }
722       }
723   return found;
724 }
725
726 /* This routine takes a line of TEXT and a CLIST in which to start the
727    lookup.  When it returns it will have incremented the text pointer past
728    the section of text it matched, set *RESULT_LIST to point to the list in
729    which the last word was matched, and will return a pointer to the cmd
730    list element which the text matches.  It will return NULL if no match at
731    all was possible.  It will return -1 (cast appropriately, ick) if ambigous
732    matches are possible; in this case *RESULT_LIST will be set to point to
733    the list in which there are ambiguous choices (and *TEXT will be set to
734    the ambiguous text string).
735
736    If the located command was an abbreviation, this routine returns the base
737    command of the abbreviation.
738
739    It does no error reporting whatsoever; control will always return
740    to the superior routine.
741
742    In the case of an ambiguous return (-1), *RESULT_LIST will be set to point
743    at the prefix_command (ie. the best match) *or* (special case) will be NULL
744    if no prefix command was ever found.  For example, in the case of "info a",
745    "info" matches without ambiguity, but "a" could be "args" or "address", so
746    *RESULT_LIST is set to the cmd_list_element for "info".  So in this case
747    RESULT_LIST should not be interpeted as a pointer to the beginning of a
748    list; it simply points to a specific command.  In the case of an ambiguous
749    return *TEXT is advanced past the last non-ambiguous prefix (e.g.
750    "info t" can be "info types" or "info target"; upon return *TEXT has been
751    advanced past "info ").
752
753    If RESULT_LIST is NULL, don't set *RESULT_LIST (but don't otherwise
754    affect the operation).
755
756    This routine does *not* modify the text pointed to by TEXT.
757
758    If IGNORE_HELP_CLASSES is nonzero, ignore any command list elements which
759    are actually help classes rather than commands (i.e. the function field of
760    the struct cmd_list_element is NULL).  */
761
762 struct cmd_list_element *
763 lookup_cmd_1 (char **text, struct cmd_list_element *clist,
764               struct cmd_list_element **result_list, int ignore_help_classes)
765 {
766   char *p, *command;
767   int len, tmp, nfound;
768   struct cmd_list_element *found, *c;
769   char *line = *text;
770
771   while (**text == ' ' || **text == '\t')
772     (*text)++;
773
774   /* Treating underscores as part of command words is important
775      so that "set args_foo()" doesn't get interpreted as
776      "set args _foo()".  */
777   for (p = *text;
778        *p && (isalnum (*p) || *p == '-' || *p == '_' ||
779               (tui_version &&
780                (*p == '+' || *p == '<' || *p == '>' || *p == '$')) ||
781               (xdb_commands && (*p == '!' || *p == '/' || *p == '?')));
782        p++)
783     ;
784
785   /* If nothing but whitespace, return 0.  */
786   if (p == *text)
787     return 0;
788
789   len = p - *text;
790
791   /* *text and p now bracket the first command word to lookup (and
792      it's length is len).  We copy this into a local temporary */
793
794
795   command = (char *) alloca (len + 1);
796   for (tmp = 0; tmp < len; tmp++)
797     {
798       char x = (*text)[tmp];
799       command[tmp] = x;
800     }
801   command[len] = '\0';
802
803   /* Look it up.  */
804   found = 0;
805   nfound = 0;
806   found = find_cmd (command, len, clist, ignore_help_classes, &nfound);
807
808   /* 
809      ** We didn't find the command in the entered case, so lower case it
810      ** and search again.
811    */
812   if (!found || nfound == 0)
813     {
814       for (tmp = 0; tmp < len; tmp++)
815         {
816           char x = command[tmp];
817           command[tmp] = isupper (x) ? tolower (x) : x;
818         }
819       found = find_cmd (command, len, clist, ignore_help_classes, &nfound);
820     }
821
822   /* If nothing matches, we have a simple failure.  */
823   if (nfound == 0)
824     return 0;
825
826   if (nfound > 1)
827     {
828       if (result_list != NULL)
829         /* Will be modified in calling routine
830            if we know what the prefix command is.  */
831         *result_list = 0;
832       return (struct cmd_list_element *) -1;    /* Ambiguous.  */
833     }
834
835   /* We've matched something on this list.  Move text pointer forward. */
836
837   *text = p;
838
839   if (found->cmd_pointer)
840     {
841       /* We drop the alias (abbreviation) in favor of the command it is
842        pointing to.  If the alias is deprecated, though, we need to
843        warn the user about it before we drop it.  Note that while we
844        are warning about the alias, we may also warn about the command
845        itself and we will adjust the appropriate DEPRECATED_WARN_USER
846        flags */
847       
848       if (found->flags & DEPRECATED_WARN_USER)
849       deprecated_cmd_warning (&line);
850       found = found->cmd_pointer;
851     }
852   /* If we found a prefix command, keep looking.  */
853
854   if (found->prefixlist)
855     {
856       c = lookup_cmd_1 (text, *found->prefixlist, result_list,
857                         ignore_help_classes);
858       if (!c)
859         {
860           /* Didn't find anything; this is as far as we got.  */
861           if (result_list != NULL)
862             *result_list = clist;
863           return found;
864         }
865       else if (c == (struct cmd_list_element *) -1)
866         {
867           /* We've gotten this far properly, but the next step
868              is ambiguous.  We need to set the result list to the best
869              we've found (if an inferior hasn't already set it).  */
870           if (result_list != NULL)
871             if (!*result_list)
872               /* This used to say *result_list = *found->prefixlist
873                  If that was correct, need to modify the documentation
874                  at the top of this function to clarify what is supposed
875                  to be going on.  */
876               *result_list = found;
877           return c;
878         }
879       else
880         {
881           /* We matched!  */
882           return c;
883         }
884     }
885   else
886     {
887       if (result_list != NULL)
888         *result_list = clist;
889       return found;
890     }
891 }
892
893 /* All this hair to move the space to the front of cmdtype */
894
895 static void
896 undef_cmd_error (char *cmdtype, char *q)
897 {
898   error ("Undefined %scommand: \"%s\".  Try \"help%s%.*s\".",
899          cmdtype,
900          q,
901          *cmdtype ? " " : "",
902          strlen (cmdtype) - 1,
903          cmdtype);
904 }
905
906 /* Look up the contents of *LINE as a command in the command list LIST.
907    LIST is a chain of struct cmd_list_element's.
908    If it is found, return the struct cmd_list_element for that command
909    and update *LINE to point after the command name, at the first argument.
910    If not found, call error if ALLOW_UNKNOWN is zero
911    otherwise (or if error returns) return zero.
912    Call error if specified command is ambiguous,
913    unless ALLOW_UNKNOWN is negative.
914    CMDTYPE precedes the word "command" in the error message.
915
916    If INGNORE_HELP_CLASSES is nonzero, ignore any command list
917    elements which are actually help classes rather than commands (i.e.
918    the function field of the struct cmd_list_element is 0).  */
919
920 struct cmd_list_element *
921 lookup_cmd (char **line, struct cmd_list_element *list, char *cmdtype,
922             int allow_unknown, int ignore_help_classes)
923 {
924   struct cmd_list_element *last_list = 0;
925   struct cmd_list_element *c =
926   lookup_cmd_1 (line, list, &last_list, ignore_help_classes);
927
928   /* Note: Do not remove trailing whitespace here because this
929      would be wrong for complete_command.  Jim Kingdon  */
930
931   if (!c)
932     {
933       if (!allow_unknown)
934         {
935           if (!*line)
936             error ("Lack of needed %scommand", cmdtype);
937           else
938             {
939               char *p = *line, *q;
940
941               while (isalnum (*p) || *p == '-')
942                 p++;
943
944               q = (char *) alloca (p - *line + 1);
945               strncpy (q, *line, p - *line);
946               q[p - *line] = '\0';
947               undef_cmd_error (cmdtype, q);
948             }
949         }
950       else
951         return 0;
952     }
953   else if (c == (struct cmd_list_element *) -1)
954     {
955       /* Ambigous.  Local values should be off prefixlist or called
956          values.  */
957       int local_allow_unknown = (last_list ? last_list->allow_unknown :
958                                  allow_unknown);
959       char *local_cmdtype = last_list ? last_list->prefixname : cmdtype;
960       struct cmd_list_element *local_list =
961       (last_list ? *(last_list->prefixlist) : list);
962
963       if (local_allow_unknown < 0)
964         {
965           if (last_list)
966             return last_list;   /* Found something.  */
967           else
968             return 0;           /* Found nothing.  */
969         }
970       else
971         {
972           /* Report as error.  */
973           int amb_len;
974           char ambbuf[100];
975
976           for (amb_len = 0;
977                ((*line)[amb_len] && (*line)[amb_len] != ' '
978                 && (*line)[amb_len] != '\t');
979                amb_len++)
980             ;
981
982           ambbuf[0] = 0;
983           for (c = local_list; c; c = c->next)
984             if (!strncmp (*line, c->name, amb_len))
985               {
986                 if (strlen (ambbuf) + strlen (c->name) + 6 < (int) sizeof ambbuf)
987                   {
988                     if (strlen (ambbuf))
989                       strcat (ambbuf, ", ");
990                     strcat (ambbuf, c->name);
991                   }
992                 else
993                   {
994                     strcat (ambbuf, "..");
995                     break;
996                   }
997               }
998           error ("Ambiguous %scommand \"%s\": %s.", local_cmdtype,
999                  *line, ambbuf);
1000           return 0;             /* lint */
1001         }
1002     }
1003   else
1004     {
1005       /* We've got something.  It may still not be what the caller
1006          wants (if this command *needs* a subcommand).  */
1007       while (**line == ' ' || **line == '\t')
1008         (*line)++;
1009
1010       if (c->prefixlist && **line && !c->allow_unknown)
1011         undef_cmd_error (c->prefixname, *line);
1012
1013       /* Seems to be what he wants.  Return it.  */
1014       return c;
1015     }
1016   return 0;
1017 }
1018
1019 /* We are here presumably because an alias or command in *TEXT is 
1020    deprecated and a warning message should be generated.  This function
1021    decodes *TEXT and potentially generates a warning message as outlined
1022    below.
1023    
1024    Example for 'set endian big' which has a fictitious alias 'seb'.
1025    
1026    If alias wasn't used in *TEXT, and the command is deprecated:
1027    "warning: 'set endian big' is deprecated." 
1028    
1029    If alias was used, and only the alias is deprecated:
1030    "warning: 'seb' an alias for the command 'set endian big' is deprecated."
1031    
1032    If alias was used and command is deprecated (regardless of whether the
1033    alias itself is deprecated:
1034    
1035    "warning: 'set endian big' (seb) is deprecated."
1036
1037    After the message has been sent, clear the appropriate flags in the
1038    command and/or the alias so the user is no longer bothered.
1039    
1040 */
1041 void
1042 deprecated_cmd_warning (char **text)
1043 {
1044   struct cmd_list_element *alias = NULL;
1045   struct cmd_list_element *prefix_cmd = NULL;
1046   struct cmd_list_element *cmd = NULL;
1047   struct cmd_list_element *c;
1048   char *type;
1049  
1050   if (!lookup_cmd_composition (*text, &alias, &prefix_cmd, &cmd))
1051     /* return if text doesn't evaluate to a command */
1052     return;
1053
1054   if (!((alias ? (alias->flags & DEPRECATED_WARN_USER) : 0)
1055       || (cmd->flags & DEPRECATED_WARN_USER) ) ) 
1056     /* return if nothing is deprecated */
1057     return;
1058   
1059   printf_filtered ("Warning:");
1060   
1061   if (alias && !(cmd->flags & CMD_DEPRECATED))
1062     printf_filtered (" '%s', an alias for the", alias->name);
1063     
1064   printf_filtered (" command '");
1065   
1066   if (prefix_cmd)
1067     printf_filtered ("%s", prefix_cmd->prefixname);
1068   
1069   printf_filtered ("%s", cmd->name);
1070
1071   if (alias && (cmd->flags & CMD_DEPRECATED))
1072     printf_filtered ("' (%s) is deprecated.\n", alias->name);
1073   else
1074     printf_filtered ("' is deprecated.\n"); 
1075   
1076
1077   /* if it is only the alias that is deprecated, we want to indicate the
1078      new alias, otherwise we'll indicate the new command */
1079
1080   if (alias && !(cmd->flags & CMD_DEPRECATED))
1081     {
1082       if (alias->replacement)
1083       printf_filtered ("Use '%s'.\n\n", alias->replacement);
1084       else
1085       printf_filtered ("No alternative known.\n\n");
1086      }  
1087   else
1088     {
1089       if (cmd->replacement)
1090       printf_filtered ("Use '%s'.\n\n", cmd->replacement);
1091       else
1092       printf_filtered ("No alternative known.\n\n");
1093     }
1094
1095   /* We've warned you, now we'll keep quiet */
1096   if (alias)
1097     alias->flags &= ~DEPRECATED_WARN_USER;
1098   
1099   cmd->flags &= ~DEPRECATED_WARN_USER;
1100 }
1101
1102
1103
1104 /* Look up the contents of LINE as a command in the command list 'cmdlist'. 
1105    Return 1 on success, 0 on failure.
1106    
1107    If LINE refers to an alias, *alias will point to that alias.
1108    
1109    If LINE is a postfix command (i.e. one that is preceeded by a prefix
1110    command) set *prefix_cmd.
1111    
1112    Set *cmd to point to the command LINE indicates.
1113    
1114    If any of *alias, *prefix_cmd, or *cmd cannot be determined or do not 
1115    exist, they are NULL when we return.
1116    
1117 */
1118 int
1119 lookup_cmd_composition (char *text,
1120                       struct cmd_list_element **alias,
1121                       struct cmd_list_element **prefix_cmd, 
1122                       struct cmd_list_element **cmd)
1123 {
1124   char *p, *command;
1125   int len, tmp, nfound;
1126   struct cmd_list_element *cur_list;
1127   struct cmd_list_element *prev_cmd;
1128   *alias = NULL;
1129   *prefix_cmd = NULL;
1130   *cmd = NULL;
1131   
1132   cur_list = cmdlist;
1133   
1134   while (1)
1135     { 
1136       /* Go through as many command lists as we need to 
1137        to find the command TEXT refers to. */
1138       
1139       prev_cmd = *cmd;
1140       
1141       while (*text == ' ' || *text == '\t')
1142       (text)++;
1143       
1144       /* Treating underscores as part of command words is important
1145        so that "set args_foo()" doesn't get interpreted as
1146        "set args _foo()".  */
1147       for (p = text;
1148          *p && (isalnum (*p) || *p == '-' || *p == '_' ||
1149                 (tui_version &&
1150                  (*p == '+' || *p == '<' || *p == '>' || *p == '$')) ||
1151                 (xdb_commands && (*p == '!' || *p == '/' || *p == '?')));
1152          p++)
1153       ;
1154       
1155       /* If nothing but whitespace, return.  */
1156       if (p == text)
1157       return 0;
1158       
1159       len = p - text;
1160       
1161       /* text and p now bracket the first command word to lookup (and
1162        it's length is len).  We copy this into a local temporary */
1163       
1164       command = (char *) alloca (len + 1);
1165       for (tmp = 0; tmp < len; tmp++)
1166       {
1167         char x = text[tmp];
1168         command[tmp] = x;
1169       }
1170       command[len] = '\0';
1171       
1172       /* Look it up.  */
1173       *cmd = 0;
1174       nfound = 0;
1175       *cmd = find_cmd (command, len, cur_list, 1, &nfound);
1176       
1177       /* We didn't find the command in the entered case, so lower case it
1178        and search again.
1179       */
1180       if (!*cmd || nfound == 0)
1181       {
1182         for (tmp = 0; tmp < len; tmp++)
1183           {
1184             char x = command[tmp];
1185             command[tmp] = isupper (x) ? tolower (x) : x;
1186           }
1187         *cmd = find_cmd (command, len, cur_list, 1, &nfound);
1188       }
1189       
1190       if (*cmd == (struct cmd_list_element *) -1)
1191       {
1192         return 0;              /* ambiguous */
1193       }
1194       
1195       if (*cmd == NULL)
1196       return 0;                /* nothing found */
1197       else
1198       {
1199         if ((*cmd)->cmd_pointer)
1200           {
1201             /* cmd was actually an alias, we note that an alias was used 
1202                (by assigning *alais) and we set *cmd. 
1203              */
1204             *alias = *cmd;
1205             *cmd = (*cmd)->cmd_pointer;
1206           }
1207         *prefix_cmd = prev_cmd;
1208       }
1209       if ((*cmd)->prefixlist)
1210       cur_list = *(*cmd)->prefixlist;
1211       else
1212       return 1;
1213       
1214       text = p;
1215     }
1216 }
1217
1218 /* Helper function for SYMBOL_COMPLETION_FUNCTION.  */
1219
1220 /* Return a vector of char pointers which point to the different
1221    possible completions in LIST of TEXT.  
1222
1223    WORD points in the same buffer as TEXT, and completions should be
1224    returned relative to this position.  For example, suppose TEXT is "foo"
1225    and we want to complete to "foobar".  If WORD is "oo", return
1226    "oobar"; if WORD is "baz/foo", return "baz/foobar".  */
1227
1228 char **
1229 complete_on_cmdlist (struct cmd_list_element *list, char *text, char *word)
1230 {
1231   struct cmd_list_element *ptr;
1232   char **matchlist;
1233   int sizeof_matchlist;
1234   int matches;
1235   int textlen = strlen (text);
1236
1237   sizeof_matchlist = 10;
1238   matchlist = (char **) xmalloc (sizeof_matchlist * sizeof (char *));
1239   matches = 0;
1240
1241   for (ptr = list; ptr; ptr = ptr->next)
1242     if (!strncmp (ptr->name, text, textlen)
1243         && !ptr->abbrev_flag
1244         && (ptr->function.cfunc
1245             || ptr->prefixlist))
1246       {
1247         if (matches == sizeof_matchlist)
1248           {
1249             sizeof_matchlist *= 2;
1250             matchlist = (char **) xrealloc ((char *) matchlist,
1251                                             (sizeof_matchlist
1252                                              * sizeof (char *)));
1253           }
1254
1255         matchlist[matches] = (char *)
1256           xmalloc (strlen (word) + strlen (ptr->name) + 1);
1257         if (word == text)
1258           strcpy (matchlist[matches], ptr->name);
1259         else if (word > text)
1260           {
1261             /* Return some portion of ptr->name.  */
1262             strcpy (matchlist[matches], ptr->name + (word - text));
1263           }
1264         else
1265           {
1266             /* Return some of text plus ptr->name.  */
1267             strncpy (matchlist[matches], word, text - word);
1268             matchlist[matches][text - word] = '\0';
1269             strcat (matchlist[matches], ptr->name);
1270           }
1271         ++matches;
1272       }
1273
1274   if (matches == 0)
1275     {
1276       xfree (matchlist);
1277       matchlist = 0;
1278     }
1279   else
1280     {
1281       matchlist = (char **) xrealloc ((char *) matchlist, ((matches + 1)
1282                                                         * sizeof (char *)));
1283       matchlist[matches] = (char *) 0;
1284     }
1285
1286   return matchlist;
1287 }
1288
1289 /* Helper function for SYMBOL_COMPLETION_FUNCTION.  */
1290
1291 /* Return a vector of char pointers which point to the different
1292    possible completions in CMD of TEXT.  
1293
1294    WORD points in the same buffer as TEXT, and completions should be
1295    returned relative to this position.  For example, suppose TEXT is "foo"
1296    and we want to complete to "foobar".  If WORD is "oo", return
1297    "oobar"; if WORD is "baz/foo", return "baz/foobar".  */
1298
1299 char **
1300 complete_on_enum (const char *enumlist[],
1301                   char *text,
1302                   char *word)
1303 {
1304   char **matchlist;
1305   int sizeof_matchlist;
1306   int matches;
1307   int textlen = strlen (text);
1308   int i;
1309   const char *name;
1310
1311   sizeof_matchlist = 10;
1312   matchlist = (char **) xmalloc (sizeof_matchlist * sizeof (char *));
1313   matches = 0;
1314
1315   for (i = 0; (name = enumlist[i]) != NULL; i++)
1316     if (strncmp (name, text, textlen) == 0)
1317       {
1318         if (matches == sizeof_matchlist)
1319           {
1320             sizeof_matchlist *= 2;
1321             matchlist = (char **) xrealloc ((char *) matchlist,
1322                                             (sizeof_matchlist
1323                                              * sizeof (char *)));
1324           }
1325
1326         matchlist[matches] = (char *)
1327           xmalloc (strlen (word) + strlen (name) + 1);
1328         if (word == text)
1329           strcpy (matchlist[matches], name);
1330         else if (word > text)
1331           {
1332             /* Return some portion of name.  */
1333             strcpy (matchlist[matches], name + (word - text));
1334           }
1335         else
1336           {
1337             /* Return some of text plus name.  */
1338             strncpy (matchlist[matches], word, text - word);
1339             matchlist[matches][text - word] = '\0';
1340             strcat (matchlist[matches], name);
1341           }
1342         ++matches;
1343       }
1344
1345   if (matches == 0)
1346     {
1347       xfree (matchlist);
1348       matchlist = 0;
1349     }
1350   else
1351     {
1352       matchlist = (char **) xrealloc ((char *) matchlist, ((matches + 1)
1353                                                         * sizeof (char *)));
1354       matchlist[matches] = (char *) 0;
1355     }
1356
1357   return matchlist;
1358 }
1359