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