OSDN Git Service

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