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