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