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