xref: /dragonfly/contrib/gdb-7/gdb/cli/cli-decode.c (revision 10f4bf95)
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 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 
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 
130 /* Add element named NAME.
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 place
214    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 this
218    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 arg */
240   char *copied_name;
241   struct cmd_list_element *old;
242   struct cmd_list_element *c;
243 
244   copied_name = (char *) alloca (strlen (oldname) + 1);
245   strcpy (copied_name, oldname);
246   old = lookup_cmd (&copied_name, *list, "", 1, 1);
247 
248   if (old == 0)
249     {
250       struct cmd_list_element *prehook, *prehookee, *posthook, *posthookee;
251       struct cmd_list_element *aliases = delete_cmd (name, list,
252 						     &prehook, &prehookee,
253 						     &posthook, &posthookee);
254 
255       /* If this happens, it means a programmer error somewhere.  */
256       gdb_assert (!aliases && !prehook && !prehookee
257 		  && !posthook && ! posthookee);
258       return 0;
259     }
260 
261   c = add_cmd (name, class, NULL, old->doc, list);
262   /* NOTE: Both FUNC and all the FUNCTIONs need to be copied.  */
263   c->func = old->func;
264   c->function = old->function;
265   c->prefixlist = old->prefixlist;
266   c->prefixname = old->prefixname;
267   c->allow_unknown = old->allow_unknown;
268   c->abbrev_flag = abbrev_flag;
269   c->cmd_pointer = old;
270   c->alias_chain = old->aliases;
271   old->aliases = c;
272   return c;
273 }
274 
275 /* Like add_cmd but adds an element for a command prefix:
276    a name that should be followed by a subcommand to be looked up
277    in another command list.  PREFIXLIST should be the address
278    of the variable containing that list.  */
279 
280 struct cmd_list_element *
281 add_prefix_cmd (char *name, enum command_class class, 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 *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 
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 
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, struct cmd_list_element *commandlist,
764 	     struct re_pattern_buffer *regex, char *prefix)
765 {
766   struct cmd_list_element *c;
767   int returnvalue;
768 
769   /* Walk through the commands */
770   for (c=commandlist;c;c=c->next)
771     {
772       returnvalue = -1; /*Needed to avoid double printing*/
773       if (c->name != NULL)
774 	{
775 	  /* Try to match against the name*/
776 	  returnvalue = re_search (regex, c->name, strlen(c->name),
777 				   0, strlen (c->name), NULL);
778 	  if (returnvalue >= 0)
779 	    {
780 	      print_help_for_command (c, prefix,
781 				      0 /* don't recurse */, stream);
782 	    }
783 	}
784       if (c->doc != NULL && returnvalue < 0)
785 	{
786 	  /* Try to match against documentation */
787 	  if (re_search(regex,c->doc,strlen(c->doc),0,strlen(c->doc),NULL) >=0)
788 	    {
789 	      print_help_for_command (c, prefix,
790 				      0 /* don't recurse */, stream);
791 	    }
792 	}
793       /* Check if this command has subcommands and is not an abbreviation.
794 	 We skip listing subcommands of abbreviations in order to avoid
795 	 duplicates in the output.
796        */
797       if (c->prefixlist != NULL && !c->abbrev_flag)
798 	{
799 	  /* Recursively call ourselves on the subcommand list,
800 	     passing the right prefix in.
801 	  */
802 	  apropos_cmd (stream,*c->prefixlist,regex,c->prefixname);
803 	}
804     }
805 }
806 
807 /* This command really has to deal with two things:
808  *     1) I want documentation on *this string* (usually called by
809  * "help commandname").
810  *     2) I want documentation on *this list* (usually called by
811  * giving a command that requires subcommands.  Also called by saying
812  * just "help".)
813  *
814  *   I am going to split this into two seperate comamnds, help_cmd and
815  * help_list.
816  */
817 
818 void
819 help_cmd (char *command, struct ui_file *stream)
820 {
821   struct cmd_list_element *c;
822   extern struct cmd_list_element *cmdlist;
823 
824   if (!command)
825     {
826       help_list (cmdlist, "", all_classes, stream);
827       return;
828     }
829 
830   if (strcmp (command, "all") == 0)
831     {
832       help_all (stream);
833       return;
834     }
835 
836   c = lookup_cmd (&command, cmdlist, "", 0, 0);
837 
838   if (c == 0)
839     return;
840 
841   /* There are three cases here.
842      If c->prefixlist is nonzero, we have a prefix command.
843      Print its documentation, then list its subcommands.
844 
845      If c->func is non NULL, we really have a command.  Print its
846      documentation and return.
847 
848      If c->func is NULL, we have a class name.  Print its
849      documentation (as if it were a command) and then set class to the
850      number of this class so that the commands in the class will be
851      listed.  */
852 
853   fputs_filtered (c->doc, stream);
854   fputs_filtered ("\n", stream);
855 
856   if (c->prefixlist == 0 && c->func != NULL)
857     return;
858   fprintf_filtered (stream, "\n");
859 
860   /* If this is a prefix command, print it's subcommands */
861   if (c->prefixlist)
862     help_list (*c->prefixlist, c->prefixname, all_commands, stream);
863 
864   /* If this is a class name, print all of the commands in the class */
865   if (c->func == NULL)
866     help_list (cmdlist, "", c->class, stream);
867 
868   if (c->hook_pre || c->hook_post)
869     fprintf_filtered (stream,
870                       "\nThis command has a hook (or hooks) defined:\n");
871 
872   if (c->hook_pre)
873     fprintf_filtered (stream,
874                       "\tThis command is run after  : %s (pre hook)\n",
875                     c->hook_pre->name);
876   if (c->hook_post)
877     fprintf_filtered (stream,
878                       "\tThis command is run before : %s (post hook)\n",
879                     c->hook_post->name);
880 }
881 
882 /*
883  * Get a specific kind of help on a command list.
884  *
885  * LIST is the list.
886  * CMDTYPE is the prefix to use in the title string.
887  * CLASS is the class with which to list the nodes of this list (see
888  * documentation for help_cmd_list below),  As usual, ALL_COMMANDS for
889  * everything, ALL_CLASSES for just classes, and non-negative for only things
890  * in a specific class.
891  * and STREAM is the output stream on which to print things.
892  * If you call this routine with a class >= 0, it recurses.
893  */
894 void
895 help_list (struct cmd_list_element *list, char *cmdtype,
896 	   enum command_class class, struct ui_file *stream)
897 {
898   int len;
899   char *cmdtype1, *cmdtype2;
900 
901   /* If CMDTYPE is "foo ", CMDTYPE1 gets " foo" and CMDTYPE2 gets "foo sub"  */
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 class */
961 
962       if (c->func == NULL)
963 	{
964 	  fprintf_filtered (stream, "\nCommand class: %s\n\n", c->name);
965 	  help_cmd_list (cmdlist, c->class, "", 1, stream);
966 	}
967     }
968 
969   /* While it's expected that all commands are in some class,
970      as a safety measure, we'll print commands outside of any
971      class at the end.  */
972 
973   for (c = cmdlist; c; c = c->next)
974     {
975       if (c->abbrev_flag)
976         continue;
977 
978       if (c->class == no_class)
979 	{
980 	  if (!seen_unclassified)
981 	    {
982 	      fprintf_filtered (stream, "\nUnclassified commands\n\n");
983 	      seen_unclassified = 1;
984 	    }
985 	  print_help_for_command (c, "", 1, stream);
986 	}
987     }
988 
989 }
990 
991 /* Print only the first line of STR on STREAM.  */
992 void
993 print_doc_line (struct ui_file *stream, char *str)
994 {
995   static char *line_buffer = 0;
996   static int line_size;
997   char *p;
998 
999   if (!line_buffer)
1000     {
1001       line_size = 80;
1002       line_buffer = (char *) xmalloc (line_size);
1003     }
1004 
1005   p = str;
1006   while (*p && *p != '\n' && *p != '.' && *p != ',')
1007     p++;
1008   if (p - str > line_size - 1)
1009     {
1010       line_size = p - str + 1;
1011       xfree (line_buffer);
1012       line_buffer = (char *) xmalloc (line_size);
1013     }
1014   strncpy (line_buffer, str, p - str);
1015   line_buffer[p - str] = '\0';
1016   if (islower (line_buffer[0]))
1017     line_buffer[0] = toupper (line_buffer[0]);
1018   ui_out_text (uiout, line_buffer);
1019 }
1020 
1021 /* Print one-line help for command C.
1022    If RECURSE is non-zero, also print one-line descriptions
1023    of all prefixed subcommands. */
1024 static void
1025 print_help_for_command (struct cmd_list_element *c, char *prefix, int recurse,
1026 			struct ui_file *stream)
1027 {
1028   fprintf_filtered (stream, "%s%s -- ", prefix, c->name);
1029   print_doc_line (stream, c->doc);
1030   fputs_filtered ("\n", stream);
1031 
1032   if (recurse
1033       && c->prefixlist != 0
1034       && c->abbrev_flag == 0)
1035     /* Subcommands of a prefix command typically have 'all_commands'
1036        as class.  If we pass CLASS to recursive invocation,
1037        most often we won't see anything. */
1038     help_cmd_list (*c->prefixlist, all_commands, c->prefixname, 1, stream);
1039 }
1040 
1041 /*
1042  * Implement a help command on command list LIST.
1043  * RECURSE should be non-zero if this should be done recursively on
1044  * all sublists of LIST.
1045  * PREFIX is the prefix to print before each command name.
1046  * STREAM is the stream upon which the output should be written.
1047  * CLASS should be:
1048  *      A non-negative class number to list only commands in that
1049  * class.
1050  *      ALL_COMMANDS to list all commands in list.
1051  *      ALL_CLASSES  to list all classes in list.
1052  *
1053  *   Note that RECURSE will be active on *all* sublists, not just the
1054  * ones selected by the criteria above (ie. the selection mechanism
1055  * is at the low level, not the high-level).
1056  */
1057 void
1058 help_cmd_list (struct cmd_list_element *list, enum command_class class,
1059 	       char *prefix, int recurse, struct ui_file *stream)
1060 {
1061   struct cmd_list_element *c;
1062 
1063   for (c = list; c; c = c->next)
1064     {
1065       if (c->abbrev_flag == 0
1066 	  && (class == all_commands
1067 	      || (class == all_classes && c->func == NULL)
1068 	      || (class == c->class && c->func != NULL)))
1069 	{
1070 	  print_help_for_command (c, prefix, recurse, stream);
1071 	}
1072       else if (c->abbrev_flag == 0 && recurse
1073 	       && class == class_user && c->prefixlist != NULL)
1074 	/* User-defined commands may be subcommands.  */
1075 	help_cmd_list (*c->prefixlist, class, c->prefixname, recurse, stream);
1076     }
1077 }
1078 
1079 
1080 /* Search the input clist for 'command'.  Return the command if
1081    found (or NULL if not), and return the number of commands
1082    found in nfound */
1083 
1084 static struct cmd_list_element *
1085 find_cmd (char *command, int len, struct cmd_list_element *clist,
1086 	  int ignore_help_classes, int *nfound)
1087 {
1088   struct cmd_list_element *found, *c;
1089 
1090   found = (struct cmd_list_element *) NULL;
1091   *nfound = 0;
1092   for (c = clist; c; c = c->next)
1093     if (!strncmp (command, c->name, len)
1094 	&& (!ignore_help_classes || c->func))
1095       {
1096 	found = c;
1097 	(*nfound)++;
1098 	if (c->name[len] == '\0')
1099 	  {
1100 	    *nfound = 1;
1101 	    break;
1102 	  }
1103       }
1104   return found;
1105 }
1106 
1107 static int
1108 find_command_name_length (const char *text)
1109 {
1110   const char *p = text;
1111 
1112   /* Treating underscores as part of command words is important
1113      so that "set args_foo()" doesn't get interpreted as
1114      "set args _foo()".  */
1115   /* Some characters are only used for TUI specific commands. However, they
1116      are always allowed for the sake of consistency.
1117      The XDB compatibility characters are only allowed when using the right
1118      mode because they clash with other GDB commands - specifically '/' is
1119      used as a suffix for print, examine and display.
1120      Note that this is larger than the character set allowed when creating
1121      user-defined commands.  */
1122   while (isalnum (*p) || *p == '-' || *p == '_'
1123 	 /* Characters used by TUI specific commands.  */
1124 	 || *p == '+' || *p == '<' || *p == '>' || *p == '$'
1125 	 /* Characters used for XDB compatibility.  */
1126 	 || (xdb_commands && (*p == '!' || *p == '/' || *p == '?')))
1127     p++;
1128 
1129   return p - text;
1130 }
1131 
1132 /* This routine takes a line of TEXT and a CLIST in which to start the
1133    lookup.  When it returns it will have incremented the text pointer past
1134    the section of text it matched, set *RESULT_LIST to point to the list in
1135    which the last word was matched, and will return a pointer to the cmd
1136    list element which the text matches.  It will return NULL if no match at
1137    all was possible.  It will return -1 (cast appropriately, ick) if ambigous
1138    matches are possible; in this case *RESULT_LIST will be set to point to
1139    the list in which there are ambiguous choices (and *TEXT will be set to
1140    the ambiguous text string).
1141 
1142    If the located command was an abbreviation, this routine returns the base
1143    command of the abbreviation.
1144 
1145    It does no error reporting whatsoever; control will always return
1146    to the superior routine.
1147 
1148    In the case of an ambiguous return (-1), *RESULT_LIST will be set to point
1149    at the prefix_command (ie. the best match) *or* (special case) will be NULL
1150    if no prefix command was ever found.  For example, in the case of "info a",
1151    "info" matches without ambiguity, but "a" could be "args" or "address", so
1152    *RESULT_LIST is set to the cmd_list_element for "info".  So in this case
1153    RESULT_LIST should not be interpeted as a pointer to the beginning of a
1154    list; it simply points to a specific command.  In the case of an ambiguous
1155    return *TEXT is advanced past the last non-ambiguous prefix (e.g.
1156    "info t" can be "info types" or "info target"; upon return *TEXT has been
1157    advanced past "info ").
1158 
1159    If RESULT_LIST is NULL, don't set *RESULT_LIST (but don't otherwise
1160    affect the operation).
1161 
1162    This routine does *not* modify the text pointed to by TEXT.
1163 
1164    If IGNORE_HELP_CLASSES is nonzero, ignore any command list elements which
1165    are actually help classes rather than commands (i.e. the function field of
1166    the struct cmd_list_element is NULL).  */
1167 
1168 struct cmd_list_element *
1169 lookup_cmd_1 (char **text, struct cmd_list_element *clist,
1170 	      struct cmd_list_element **result_list, int ignore_help_classes)
1171 {
1172   char *command;
1173   int len, tmp, nfound;
1174   struct cmd_list_element *found, *c;
1175   char *line = *text;
1176 
1177   while (**text == ' ' || **text == '\t')
1178     (*text)++;
1179 
1180   /* Identify the name of the command.  */
1181   len = find_command_name_length (*text);
1182 
1183   /* If nothing but whitespace, return 0.  */
1184   if (len == 0)
1185     return 0;
1186 
1187   /* *text and p now bracket the first command word to lookup (and
1188      it's length is len).  We copy this into a local temporary */
1189 
1190 
1191   command = (char *) alloca (len + 1);
1192   memcpy (command, *text, len);
1193   command[len] = '\0';
1194 
1195   /* Look it up.  */
1196   found = 0;
1197   nfound = 0;
1198   found = find_cmd (command, len, clist, ignore_help_classes, &nfound);
1199 
1200   /*
1201      ** We didn't find the command in the entered case, so lower case it
1202      ** and search again.
1203    */
1204   if (!found || nfound == 0)
1205     {
1206       for (tmp = 0; tmp < len; tmp++)
1207 	{
1208 	  char x = command[tmp];
1209 
1210 	  command[tmp] = isupper (x) ? tolower (x) : x;
1211 	}
1212       found = find_cmd (command, len, clist, ignore_help_classes, &nfound);
1213     }
1214 
1215   /* If nothing matches, we have a simple failure.  */
1216   if (nfound == 0)
1217     return 0;
1218 
1219   if (nfound > 1)
1220     {
1221       if (result_list != NULL)
1222 	/* Will be modified in calling routine
1223 	   if we know what the prefix command is.  */
1224 	*result_list = 0;
1225       return (struct cmd_list_element *) -1;	/* Ambiguous.  */
1226     }
1227 
1228   /* We've matched something on this list.  Move text pointer forward. */
1229 
1230   *text += len;
1231 
1232   if (found->cmd_pointer)
1233     {
1234       /* We drop the alias (abbreviation) in favor of the command it is
1235        pointing to.  If the alias is deprecated, though, we need to
1236        warn the user about it before we drop it.  Note that while we
1237        are warning about the alias, we may also warn about the command
1238        itself and we will adjust the appropriate DEPRECATED_WARN_USER
1239        flags */
1240 
1241       if (found->flags & DEPRECATED_WARN_USER)
1242 	deprecated_cmd_warning (&line);
1243       found = found->cmd_pointer;
1244     }
1245   /* If we found a prefix command, keep looking.  */
1246 
1247   if (found->prefixlist)
1248     {
1249       c = lookup_cmd_1 (text, *found->prefixlist, result_list,
1250 			ignore_help_classes);
1251       if (!c)
1252 	{
1253 	  /* Didn't find anything; this is as far as we got.  */
1254 	  if (result_list != NULL)
1255 	    *result_list = clist;
1256 	  return found;
1257 	}
1258       else if (c == (struct cmd_list_element *) -1)
1259 	{
1260 	  /* We've gotten this far properly, but the next step
1261 	     is ambiguous.  We need to set the result list to the best
1262 	     we've found (if an inferior hasn't already set it).  */
1263 	  if (result_list != NULL)
1264 	    if (!*result_list)
1265 	      /* This used to say *result_list = *found->prefixlist
1266 	         If that was correct, need to modify the documentation
1267 	         at the top of this function to clarify what is supposed
1268 	         to be going on.  */
1269 	      *result_list = found;
1270 	  return c;
1271 	}
1272       else
1273 	{
1274 	  /* We matched!  */
1275 	  return c;
1276 	}
1277     }
1278   else
1279     {
1280       if (result_list != NULL)
1281 	*result_list = clist;
1282       return found;
1283     }
1284 }
1285 
1286 /* All this hair to move the space to the front of cmdtype */
1287 
1288 static void
1289 undef_cmd_error (char *cmdtype, char *q)
1290 {
1291   error (_("Undefined %scommand: \"%s\".  Try \"help%s%.*s\"."),
1292 	 cmdtype,
1293 	 q,
1294 	 *cmdtype ? " " : "",
1295 	 (int) strlen (cmdtype) - 1,
1296 	 cmdtype);
1297 }
1298 
1299 /* Look up the contents of *LINE as a command in the command list LIST.
1300    LIST is a chain of struct cmd_list_element's.
1301    If it is found, return the struct cmd_list_element for that command
1302    and update *LINE to point after the command name, at the first argument.
1303    If not found, call error if ALLOW_UNKNOWN is zero
1304    otherwise (or if error returns) return zero.
1305    Call error if specified command is ambiguous,
1306    unless ALLOW_UNKNOWN is negative.
1307    CMDTYPE precedes the word "command" in the error message.
1308 
1309    If INGNORE_HELP_CLASSES is nonzero, ignore any command list
1310    elements which are actually help classes rather than commands (i.e.
1311    the function field of the struct cmd_list_element is 0).  */
1312 
1313 struct cmd_list_element *
1314 lookup_cmd (char **line, struct cmd_list_element *list, char *cmdtype,
1315 	    int allow_unknown, int ignore_help_classes)
1316 {
1317   struct cmd_list_element *last_list = 0;
1318   struct cmd_list_element *c;
1319 
1320   /* Note: Do not remove trailing whitespace here because this
1321      would be wrong for complete_command.  Jim Kingdon  */
1322 
1323   if (!*line)
1324     error (_("Lack of needed %scommand"), cmdtype);
1325 
1326   c = lookup_cmd_1 (line, list, &last_list, ignore_help_classes);
1327 
1328   if (!c)
1329     {
1330       if (!allow_unknown)
1331 	{
1332 	  char *q;
1333 	  int len = find_command_name_length (*line);
1334 
1335 	  q = (char *) alloca (len + 1);
1336 	  strncpy (q, *line, len);
1337 	  q[len] = '\0';
1338 	  undef_cmd_error (cmdtype, q);
1339 	}
1340       else
1341 	return 0;
1342     }
1343   else if (c == (struct cmd_list_element *) -1)
1344     {
1345       /* Ambigous.  Local values should be off prefixlist or called
1346          values.  */
1347       int local_allow_unknown = (last_list ? last_list->allow_unknown :
1348 				 allow_unknown);
1349       char *local_cmdtype = last_list ? last_list->prefixname : cmdtype;
1350       struct cmd_list_element *local_list =
1351 	(last_list ? *(last_list->prefixlist) : list);
1352 
1353       if (local_allow_unknown < 0)
1354 	{
1355 	  if (last_list)
1356 	    return last_list;	/* Found something.  */
1357 	  else
1358 	    return 0;		/* Found nothing.  */
1359 	}
1360       else
1361 	{
1362 	  /* Report as error.  */
1363 	  int amb_len;
1364 	  char ambbuf[100];
1365 
1366 	  for (amb_len = 0;
1367 	       ((*line)[amb_len] && (*line)[amb_len] != ' '
1368 		&& (*line)[amb_len] != '\t');
1369 	       amb_len++)
1370 	    ;
1371 
1372 	  ambbuf[0] = 0;
1373 	  for (c = local_list; c; c = c->next)
1374 	    if (!strncmp (*line, c->name, amb_len))
1375 	      {
1376 		if (strlen (ambbuf) + strlen (c->name) + 6 < (int) sizeof ambbuf)
1377 		  {
1378 		    if (strlen (ambbuf))
1379 		      strcat (ambbuf, ", ");
1380 		    strcat (ambbuf, c->name);
1381 		  }
1382 		else
1383 		  {
1384 		    strcat (ambbuf, "..");
1385 		    break;
1386 		  }
1387 	      }
1388 	  error (_("Ambiguous %scommand \"%s\": %s."), local_cmdtype,
1389 		 *line, ambbuf);
1390 	  return 0;		/* lint */
1391 	}
1392     }
1393   else
1394     {
1395       /* We've got something.  It may still not be what the caller
1396          wants (if this command *needs* a subcommand).  */
1397       while (**line == ' ' || **line == '\t')
1398 	(*line)++;
1399 
1400       if (c->prefixlist && **line && !c->allow_unknown)
1401 	undef_cmd_error (c->prefixname, *line);
1402 
1403       /* Seems to be what he wants.  Return it.  */
1404       return c;
1405     }
1406   return 0;
1407 }
1408 
1409 /* We are here presumably because an alias or command in *TEXT is
1410    deprecated and a warning message should be generated.  This function
1411    decodes *TEXT and potentially generates a warning message as outlined
1412    below.
1413 
1414    Example for 'set endian big' which has a fictitious alias 'seb'.
1415 
1416    If alias wasn't used in *TEXT, and the command is deprecated:
1417    "warning: 'set endian big' is deprecated."
1418 
1419    If alias was used, and only the alias is deprecated:
1420    "warning: 'seb' an alias for the command 'set endian big' is deprecated."
1421 
1422    If alias was used and command is deprecated (regardless of whether the
1423    alias itself is deprecated:
1424 
1425    "warning: 'set endian big' (seb) is deprecated."
1426 
1427    After the message has been sent, clear the appropriate flags in the
1428    command and/or the alias so the user is no longer bothered.
1429 
1430 */
1431 void
1432 deprecated_cmd_warning (char **text)
1433 {
1434   struct cmd_list_element *alias = NULL;
1435   struct cmd_list_element *prefix_cmd = NULL;
1436   struct cmd_list_element *cmd = NULL;
1437 
1438   if (!lookup_cmd_composition (*text, &alias, &prefix_cmd, &cmd))
1439     /* return if text doesn't evaluate to a command */
1440     return;
1441 
1442   if (!((alias ? (alias->flags & DEPRECATED_WARN_USER) : 0)
1443       || (cmd->flags & DEPRECATED_WARN_USER) ) )
1444     /* return if nothing is deprecated */
1445     return;
1446 
1447   printf_filtered ("Warning:");
1448 
1449   if (alias && !(cmd->flags & CMD_DEPRECATED))
1450     printf_filtered (" '%s', an alias for the", alias->name);
1451 
1452   printf_filtered (" command '");
1453 
1454   if (prefix_cmd)
1455     printf_filtered ("%s", prefix_cmd->prefixname);
1456 
1457   printf_filtered ("%s", cmd->name);
1458 
1459   if (alias && (cmd->flags & CMD_DEPRECATED))
1460     printf_filtered ("' (%s) is deprecated.\n", alias->name);
1461   else
1462     printf_filtered ("' is deprecated.\n");
1463 
1464 
1465   /* If it is only the alias that is deprecated, we want to indicate the
1466      new alias, otherwise we'll indicate the new command.  */
1467 
1468   if (alias && !(cmd->flags & CMD_DEPRECATED))
1469     {
1470       if (alias->replacement)
1471 	printf_filtered ("Use '%s'.\n\n", alias->replacement);
1472       else
1473 	printf_filtered ("No alternative known.\n\n");
1474      }
1475   else
1476     {
1477       if (cmd->replacement)
1478 	printf_filtered ("Use '%s'.\n\n", cmd->replacement);
1479       else
1480 	printf_filtered ("No alternative known.\n\n");
1481     }
1482 
1483   /* We've warned you, now we'll keep quiet */
1484   if (alias)
1485     alias->flags &= ~DEPRECATED_WARN_USER;
1486 
1487   cmd->flags &= ~DEPRECATED_WARN_USER;
1488 }
1489 
1490 
1491 
1492 /* Look up the contents of LINE as a command in the command list 'cmdlist'.
1493    Return 1 on success, 0 on failure.
1494 
1495    If LINE refers to an alias, *alias will point to that alias.
1496 
1497    If LINE is a postfix command (i.e. one that is preceeded by a prefix
1498    command) set *prefix_cmd.
1499 
1500    Set *cmd to point to the command LINE indicates.
1501 
1502    If any of *alias, *prefix_cmd, or *cmd cannot be determined or do not
1503    exist, they are NULL when we return.
1504 
1505 */
1506 int
1507 lookup_cmd_composition (char *text,
1508                       struct cmd_list_element **alias,
1509                       struct cmd_list_element **prefix_cmd,
1510                       struct cmd_list_element **cmd)
1511 {
1512   char *command;
1513   int len, tmp, nfound;
1514   struct cmd_list_element *cur_list;
1515   struct cmd_list_element *prev_cmd;
1516 
1517   *alias = NULL;
1518   *prefix_cmd = NULL;
1519   *cmd = NULL;
1520 
1521   cur_list = cmdlist;
1522 
1523   while (1)
1524     {
1525       /* Go through as many command lists as we need to
1526 	 to find the command TEXT refers to. */
1527 
1528       prev_cmd = *cmd;
1529 
1530       while (*text == ' ' || *text == '\t')
1531 	(text)++;
1532 
1533       /* Identify the name of the command.  */
1534       len = find_command_name_length (text);
1535 
1536       /* If nothing but whitespace, return.  */
1537       if (len == 0)
1538 	return 0;
1539 
1540       /* Text is the start of the first command word to lookup (and
1541 	 it's length is len).  We copy this into a local temporary.  */
1542 
1543       command = (char *) alloca (len + 1);
1544       memcpy (command, text, len);
1545       command[len] = '\0';
1546 
1547       /* Look it up.  */
1548       *cmd = 0;
1549       nfound = 0;
1550       *cmd = find_cmd (command, len, cur_list, 1, &nfound);
1551 
1552       /* We didn't find the command in the entered case, so lower case it
1553 	 and search again.
1554       */
1555       if (!*cmd || nfound == 0)
1556 	{
1557 	  for (tmp = 0; tmp < len; tmp++)
1558 	    {
1559 	      char x = command[tmp];
1560 
1561 	      command[tmp] = isupper (x) ? tolower (x) : x;
1562 	    }
1563 	  *cmd = find_cmd (command, len, cur_list, 1, &nfound);
1564 	}
1565 
1566       if (*cmd == (struct cmd_list_element *) -1)
1567 	{
1568 	  return 0;              /* ambiguous */
1569 	}
1570 
1571       if (*cmd == NULL)
1572 	return 0;                /* nothing found */
1573       else
1574 	{
1575 	  if ((*cmd)->cmd_pointer)
1576 	    {
1577 	      /* cmd was actually an alias, we note that an alias was used
1578 		 (by assigning *alais) and we set *cmd.  */
1579 	      *alias = *cmd;
1580 	      *cmd = (*cmd)->cmd_pointer;
1581 	    }
1582 	  *prefix_cmd = prev_cmd;
1583 	}
1584       if ((*cmd)->prefixlist)
1585 	cur_list = *(*cmd)->prefixlist;
1586       else
1587 	return 1;
1588 
1589       text += len;
1590     }
1591 }
1592 
1593 /* Helper function for SYMBOL_COMPLETION_FUNCTION.  */
1594 
1595 /* Return a vector of char pointers which point to the different
1596    possible completions in LIST of TEXT.
1597 
1598    WORD points in the same buffer as TEXT, and completions should be
1599    returned relative to this position.  For example, suppose TEXT is "foo"
1600    and we want to complete to "foobar".  If WORD is "oo", return
1601    "oobar"; if WORD is "baz/foo", return "baz/foobar".  */
1602 
1603 char **
1604 complete_on_cmdlist (struct cmd_list_element *list, char *text, char *word)
1605 {
1606   struct cmd_list_element *ptr;
1607   char **matchlist;
1608   int sizeof_matchlist;
1609   int matches;
1610   int textlen = strlen (text);
1611   int pass;
1612   int saw_deprecated_match = 0;
1613 
1614   sizeof_matchlist = 10;
1615   matchlist = (char **) xmalloc (sizeof_matchlist * sizeof (char *));
1616   matches = 0;
1617 
1618   /* We do one or two passes.  In the first pass, we skip deprecated
1619      commands.  If we see no matching commands in the first pass, and
1620      if we did happen to see a matching deprecated command, we do
1621      another loop to collect those.  */
1622   for (pass = 0; matches == 0 && pass < 2; ++pass)
1623     {
1624       for (ptr = list; ptr; ptr = ptr->next)
1625 	if (!strncmp (ptr->name, text, textlen)
1626 	    && !ptr->abbrev_flag
1627 	    && (ptr->func
1628 		|| ptr->prefixlist))
1629 	  {
1630 	    if (pass == 0)
1631 	      {
1632 		if ((ptr->flags & CMD_DEPRECATED) != 0)
1633 		  {
1634 		    saw_deprecated_match = 1;
1635 		    continue;
1636 		  }
1637 	      }
1638 
1639 	    if (matches == sizeof_matchlist)
1640 	      {
1641 		sizeof_matchlist *= 2;
1642 		matchlist = (char **) xrealloc ((char *) matchlist,
1643 						(sizeof_matchlist
1644 						 * sizeof (char *)));
1645 	      }
1646 
1647 	    matchlist[matches] = (char *)
1648 	      xmalloc (strlen (word) + strlen (ptr->name) + 1);
1649 	    if (word == text)
1650 	      strcpy (matchlist[matches], ptr->name);
1651 	    else if (word > text)
1652 	      {
1653 		/* Return some portion of ptr->name.  */
1654 		strcpy (matchlist[matches], ptr->name + (word - text));
1655 	      }
1656 	    else
1657 	      {
1658 		/* Return some of text plus ptr->name.  */
1659 		strncpy (matchlist[matches], word, text - word);
1660 		matchlist[matches][text - word] = '\0';
1661 		strcat (matchlist[matches], ptr->name);
1662 	      }
1663 	    ++matches;
1664 	  }
1665       /* If we saw no matching deprecated commands in the first pass,
1666 	 just bail out.  */
1667       if (!saw_deprecated_match)
1668 	break;
1669     }
1670 
1671   if (matches == 0)
1672     {
1673       xfree (matchlist);
1674       matchlist = 0;
1675     }
1676   else
1677     {
1678       matchlist = (char **) xrealloc ((char *) matchlist, ((matches + 1)
1679 							* sizeof (char *)));
1680       matchlist[matches] = (char *) 0;
1681     }
1682 
1683   return matchlist;
1684 }
1685 
1686 /* Helper function for SYMBOL_COMPLETION_FUNCTION.  */
1687 
1688 /* Return a vector of char pointers which point to the different
1689    possible completions in CMD of TEXT.
1690 
1691    WORD points in the same buffer as TEXT, and completions should be
1692    returned relative to this position.  For example, suppose TEXT is "foo"
1693    and we want to complete to "foobar".  If WORD is "oo", return
1694    "oobar"; if WORD is "baz/foo", return "baz/foobar".  */
1695 
1696 char **
1697 complete_on_enum (const char *enumlist[],
1698 		  char *text,
1699 		  char *word)
1700 {
1701   char **matchlist;
1702   int sizeof_matchlist;
1703   int matches;
1704   int textlen = strlen (text);
1705   int i;
1706   const char *name;
1707 
1708   sizeof_matchlist = 10;
1709   matchlist = (char **) xmalloc (sizeof_matchlist * sizeof (char *));
1710   matches = 0;
1711 
1712   for (i = 0; (name = enumlist[i]) != NULL; i++)
1713     if (strncmp (name, text, textlen) == 0)
1714       {
1715 	if (matches == sizeof_matchlist)
1716 	  {
1717 	    sizeof_matchlist *= 2;
1718 	    matchlist = (char **) xrealloc ((char *) matchlist,
1719 					    (sizeof_matchlist
1720 					     * sizeof (char *)));
1721 	  }
1722 
1723 	matchlist[matches] = (char *)
1724 	  xmalloc (strlen (word) + strlen (name) + 1);
1725 	if (word == text)
1726 	  strcpy (matchlist[matches], name);
1727 	else if (word > text)
1728 	  {
1729 	    /* Return some portion of name.  */
1730 	    strcpy (matchlist[matches], name + (word - text));
1731 	  }
1732 	else
1733 	  {
1734 	    /* Return some of text plus name.  */
1735 	    strncpy (matchlist[matches], word, text - word);
1736 	    matchlist[matches][text - word] = '\0';
1737 	    strcat (matchlist[matches], name);
1738 	  }
1739 	++matches;
1740       }
1741 
1742   if (matches == 0)
1743     {
1744       xfree (matchlist);
1745       matchlist = 0;
1746     }
1747   else
1748     {
1749       matchlist = (char **) xrealloc ((char *) matchlist, ((matches + 1)
1750 							* sizeof (char *)));
1751       matchlist[matches] = (char *) 0;
1752     }
1753 
1754   return matchlist;
1755 }
1756 
1757 
1758 /* check function pointer */
1759 int
1760 cmd_func_p (struct cmd_list_element *cmd)
1761 {
1762   return (cmd->func != NULL);
1763 }
1764 
1765 
1766 /* call the command function */
1767 void
1768 cmd_func (struct cmd_list_element *cmd, char *args, int from_tty)
1769 {
1770   if (cmd_func_p (cmd))
1771     (*cmd->func) (cmd, args, from_tty);
1772   else
1773     error (_("Invalid command"));
1774 }
1775