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