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