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