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