1 /* Handle lists of commands, their decoding and documentation, for GDB.
2 
3    Copyright (C) 1986-2021 Free Software Foundation, Inc.
4 
5    This program is free software; you can redistribute it and/or modify
6    it under the terms of the GNU General Public License as published by
7    the Free Software Foundation; either version 3 of the License, or
8    (at your option) any later version.
9 
10    This program is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13    GNU General Public License for more details.
14 
15    You should have received a copy of the GNU General Public License
16    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
17 
18 #include "defs.h"
19 #include "symtab.h"
20 #include <ctype.h>
21 #include "gdb_regex.h"
22 #include "completer.h"
23 #include "ui-out.h"
24 #include "cli/cli-cmds.h"
25 #include "cli/cli-decode.h"
26 #include "cli/cli-style.h"
27 #include "gdbsupport/gdb_optional.h"
28 
29 /* Prototypes for local functions.  */
30 
31 static void undef_cmd_error (const char *, const char *);
32 
33 static struct cmd_list_element *delete_cmd (const char *name,
34 					    struct cmd_list_element **list,
35 					    struct cmd_list_element **prehook,
36 					    struct cmd_list_element **prehookee,
37 					    struct cmd_list_element **posthook,
38 					    struct cmd_list_element **posthookee);
39 
40 static struct cmd_list_element *find_cmd (const char *command,
41 					  int len,
42 					  struct cmd_list_element *clist,
43 					  int ignore_help_classes,
44 					  int *nfound);
45 
46 static void help_cmd_list (struct cmd_list_element *list,
47 			   enum command_class theclass,
48 			   bool recurse,
49 			   struct ui_file *stream);
50 
51 static void help_all (struct ui_file *stream);
52 
53 static int lookup_cmd_composition_1 (const char *text,
54 				     struct cmd_list_element **alias,
55 				     struct cmd_list_element **prefix_cmd,
56 				     struct cmd_list_element **cmd,
57 				     struct cmd_list_element *cur_list);
58 
59 /* Look up a command whose 'subcommands' field is SUBCOMMANDS.  Return the
60    command if found, otherwise return NULL.  */
61 
62 static struct cmd_list_element *
lookup_cmd_with_subcommands(cmd_list_element ** subcommands,cmd_list_element * list)63 lookup_cmd_with_subcommands (cmd_list_element **subcommands,
64 			     cmd_list_element *list)
65 {
66   struct cmd_list_element *p = NULL;
67 
68   for (p = list; p != NULL; p = p->next)
69     {
70       struct cmd_list_element *q;
71 
72       if (!p->is_prefix ())
73 	continue;
74 
75       else if (p->subcommands == subcommands)
76 	{
77 	  /* If we found an alias, we must return the aliased
78 	     command.  */
79 	  return p->is_alias () ? p->alias_target : p;
80 	}
81 
82       q = lookup_cmd_with_subcommands (subcommands, *(p->subcommands));
83       if (q != NULL)
84 	return q;
85     }
86 
87   return NULL;
88 }
89 
90 static void
91 print_help_for_command (struct cmd_list_element *c,
92 			bool recurse, struct ui_file *stream);
93 
94 
95 /* Set the callback function for the specified command.  For each both
96    the commands callback and func() are set.  The latter set to a
97    bounce function (unless cfunc / sfunc is NULL that is).  */
98 
99 static void
do_const_cfunc(struct cmd_list_element * c,const char * args,int from_tty)100 do_const_cfunc (struct cmd_list_element *c, const char *args, int from_tty)
101 {
102   c->function.const_cfunc (args, from_tty);
103 }
104 
105 static void
set_cmd_cfunc(struct cmd_list_element * cmd,cmd_const_cfunc_ftype * cfunc)106 set_cmd_cfunc (struct cmd_list_element *cmd, cmd_const_cfunc_ftype *cfunc)
107 {
108   if (cfunc == NULL)
109     cmd->func = NULL;
110   else
111     cmd->func = do_const_cfunc;
112   cmd->function.const_cfunc = cfunc;
113 }
114 
115 static void
do_sfunc(struct cmd_list_element * c,const char * args,int from_tty)116 do_sfunc (struct cmd_list_element *c, const char *args, int from_tty)
117 {
118   c->function.sfunc (args, from_tty, c);
119 }
120 
121 void
set_cmd_sfunc(struct cmd_list_element * cmd,cmd_const_sfunc_ftype * sfunc)122 set_cmd_sfunc (struct cmd_list_element *cmd, cmd_const_sfunc_ftype *sfunc)
123 {
124   if (sfunc == NULL)
125     cmd->func = NULL;
126   else
127     cmd->func = do_sfunc;
128   cmd->function.sfunc = sfunc;
129 }
130 
131 int
cmd_cfunc_eq(struct cmd_list_element * cmd,cmd_const_cfunc_ftype * cfunc)132 cmd_cfunc_eq (struct cmd_list_element *cmd, cmd_const_cfunc_ftype *cfunc)
133 {
134   return cmd->func == do_const_cfunc && cmd->function.const_cfunc == cfunc;
135 }
136 
137 void
set_cmd_completer(struct cmd_list_element * cmd,completer_ftype * completer)138 set_cmd_completer (struct cmd_list_element *cmd, completer_ftype *completer)
139 {
140   cmd->completer = completer; /* Ok.  */
141 }
142 
143 /* See definition in commands.h.  */
144 
145 void
set_cmd_completer_handle_brkchars(struct cmd_list_element * cmd,completer_handle_brkchars_ftype * func)146 set_cmd_completer_handle_brkchars (struct cmd_list_element *cmd,
147 				   completer_handle_brkchars_ftype *func)
148 {
149   cmd->completer_handle_brkchars = func;
150 }
151 
152 std::string
prefixname()153 cmd_list_element::prefixname () const
154 {
155   if (!this->is_prefix ())
156     /* Not a prefix command.  */
157     return "";
158 
159   std::string prefixname;
160   if (this->prefix != nullptr)
161     prefixname = this->prefix->prefixname ();
162 
163   prefixname += this->name;
164   prefixname += " ";
165 
166   return prefixname;
167 }
168 
169 /* Add element named NAME.
170    Space for NAME and DOC must be allocated by the caller.
171    CLASS is the top level category into which commands are broken down
172    for "help" purposes.
173    FUN should be the function to execute the command;
174    it will get a character string as argument, with leading
175    and trailing blanks already eliminated.
176 
177    DOC is a documentation string for the command.
178    Its first line should be a complete sentence.
179    It should start with ? for a command that is an abbreviation
180    or with * for a command that most users don't need to know about.
181 
182    Add this command to command list *LIST.
183 
184    Returns a pointer to the added command (not necessarily the head
185    of *LIST).  */
186 
187 static struct cmd_list_element *
do_add_cmd(const char * name,enum command_class theclass,const char * doc,struct cmd_list_element ** list)188 do_add_cmd (const char *name, enum command_class theclass,
189 	    const char *doc, struct cmd_list_element **list)
190 {
191   struct cmd_list_element *c = new struct cmd_list_element (name, theclass,
192 							    doc);
193   struct cmd_list_element *p, *iter;
194 
195   /* Turn each alias of the old command into an alias of the new
196      command.  */
197   c->aliases = delete_cmd (name, list, &c->hook_pre, &c->hookee_pre,
198 			   &c->hook_post, &c->hookee_post);
199   for (iter = c->aliases; iter; iter = iter->alias_chain)
200     iter->alias_target = c;
201   if (c->hook_pre)
202     c->hook_pre->hookee_pre = c;
203   if (c->hookee_pre)
204     c->hookee_pre->hook_pre = c;
205   if (c->hook_post)
206     c->hook_post->hookee_post = c;
207   if (c->hookee_post)
208     c->hookee_post->hook_post = c;
209 
210   if (*list == NULL || strcmp ((*list)->name, name) >= 0)
211     {
212       c->next = *list;
213       *list = c;
214     }
215   else
216     {
217       p = *list;
218       while (p->next && strcmp (p->next->name, name) <= 0)
219 	{
220 	  p = p->next;
221 	}
222       c->next = p->next;
223       p->next = c;
224     }
225 
226   /* Search the prefix cmd of C, and assigns it to C->prefix.
227      See also add_prefix_cmd and update_prefix_field_of_prefixed_commands.  */
228   cmd_list_element *prefixcmd = lookup_cmd_with_subcommands (list, cmdlist);
229   c->prefix = prefixcmd;
230 
231 
232   return c;
233 }
234 
235 struct cmd_list_element *
add_cmd(const char * name,enum command_class theclass,const char * doc,struct cmd_list_element ** list)236 add_cmd (const char *name, enum command_class theclass,
237 	 const char *doc, struct cmd_list_element **list)
238 {
239   cmd_list_element *result = do_add_cmd (name, theclass, doc, list);
240   result->func = NULL;
241   result->function.const_cfunc = NULL;
242   return result;
243 }
244 
245 struct cmd_list_element *
add_cmd(const char * name,enum command_class theclass,cmd_const_cfunc_ftype * fun,const char * doc,struct cmd_list_element ** list)246 add_cmd (const char *name, enum command_class theclass,
247 	 cmd_const_cfunc_ftype *fun,
248 	 const char *doc, struct cmd_list_element **list)
249 {
250   cmd_list_element *result = do_add_cmd (name, theclass, doc, list);
251   set_cmd_cfunc (result, fun);
252   return result;
253 }
254 
255 /* Add an element with a suppress notification to the LIST of commands.  */
256 
257 struct cmd_list_element *
add_cmd_suppress_notification(const char * name,enum command_class theclass,cmd_const_cfunc_ftype * fun,const char * doc,struct cmd_list_element ** list,int * suppress_notification)258 add_cmd_suppress_notification (const char *name, enum command_class theclass,
259 			       cmd_const_cfunc_ftype *fun, const char *doc,
260 			       struct cmd_list_element **list,
261 			       int *suppress_notification)
262 {
263   struct cmd_list_element *element;
264 
265   element = add_cmd (name, theclass, fun, doc, list);
266   element->suppress_notification = suppress_notification;
267 
268   return element;
269 }
270 
271 
272 /* Deprecates a command CMD.
273    REPLACEMENT is the name of the command which should be used in
274    place of this command, or NULL if no such command exists.
275 
276    This function does not check to see if command REPLACEMENT exists
277    since gdb may not have gotten around to adding REPLACEMENT when
278    this function is called.
279 
280    Returns a pointer to the deprecated command.  */
281 
282 struct cmd_list_element *
deprecate_cmd(struct cmd_list_element * cmd,const char * replacement)283 deprecate_cmd (struct cmd_list_element *cmd, const char *replacement)
284 {
285   cmd->cmd_deprecated = 1;
286   cmd->deprecated_warn_user = 1;
287 
288   if (replacement != NULL)
289     cmd->replacement = replacement;
290   else
291     cmd->replacement = NULL;
292 
293   return cmd;
294 }
295 
296 struct cmd_list_element *
add_alias_cmd(const char * name,cmd_list_element * target,enum command_class theclass,int abbrev_flag,struct cmd_list_element ** list)297 add_alias_cmd (const char *name, cmd_list_element *target,
298 	       enum command_class theclass, int abbrev_flag,
299 	       struct cmd_list_element **list)
300 {
301   gdb_assert (target != nullptr);
302 
303   struct cmd_list_element *c = add_cmd (name, theclass, target->doc, list);
304 
305   /* If TARGET->DOC can be freed, we should make another copy.  */
306   if (target->doc_allocated)
307     {
308       c->doc = xstrdup (target->doc);
309       c->doc_allocated = 1;
310     }
311   /* NOTE: Both FUNC and all the FUNCTIONs need to be copied.  */
312   c->func = target->func;
313   c->function = target->function;
314   c->subcommands = target->subcommands;
315   c->allow_unknown = target->allow_unknown;
316   c->abbrev_flag = abbrev_flag;
317   c->alias_target = target;
318   c->alias_chain = target->aliases;
319   target->aliases = c;
320 
321   return c;
322 }
323 
324 /* Update the prefix field of all sub-commands of the prefix command C.
325    We must do this when a prefix command is defined as the GDB init sequence
326    does not guarantee that a prefix command is created before its sub-commands.
327    For example, break-catch-sig.c initialization runs before breakpoint.c
328    initialization, but it is breakpoint.c that creates the "catch" command used
329    by the "catch signal" command created by break-catch-sig.c.  */
330 
331 static void
update_prefix_field_of_prefixed_commands(struct cmd_list_element * c)332 update_prefix_field_of_prefixed_commands (struct cmd_list_element *c)
333 {
334   for (cmd_list_element *p = *c->subcommands; p != NULL; p = p->next)
335     {
336       p->prefix = c;
337 
338       /* We must recursively update the prefix field to cover
339 	 e.g.  'info auto-load libthread-db' where the creation
340 	 order was:
341 	   libthread-db
342 	   auto-load
343 	   info
344 	 In such a case, when 'auto-load' was created by do_add_cmd,
345 	 the 'libthread-db' prefix field could not be updated, as the
346 	 'auto-load' command was not yet reachable by
347 	    lookup_cmd_for_subcommands (list, cmdlist)
348 	    that searches from the top level 'cmdlist'.  */
349       if (p->is_prefix ())
350 	update_prefix_field_of_prefixed_commands (p);
351     }
352 }
353 
354 
355 /* Like add_cmd but adds an element for a command prefix: a name that
356    should be followed by a subcommand to be looked up in another
357    command list.  SUBCOMMANDS should be the address of the variable
358    containing that list.  */
359 
360 struct cmd_list_element *
add_prefix_cmd(const char * name,enum command_class theclass,cmd_const_cfunc_ftype * fun,const char * doc,struct cmd_list_element ** subcommands,int allow_unknown,struct cmd_list_element ** list)361 add_prefix_cmd (const char *name, enum command_class theclass,
362 		cmd_const_cfunc_ftype *fun,
363 		const char *doc, struct cmd_list_element **subcommands,
364 		int allow_unknown, struct cmd_list_element **list)
365 {
366   struct cmd_list_element *c = add_cmd (name, theclass, fun, doc, list);
367 
368   c->subcommands = subcommands;
369   c->allow_unknown = allow_unknown;
370 
371   /* Now that prefix command C is defined, we need to set the prefix field
372      of all prefixed commands that were defined before C itself was defined.  */
373   update_prefix_field_of_prefixed_commands (c);
374 
375   return c;
376 }
377 
378 /* A helper function for add_basic_prefix_cmd.  This is a command
379    function that just forwards to help_list.  */
380 
381 static void
do_prefix_cmd(const char * args,int from_tty,struct cmd_list_element * c)382 do_prefix_cmd (const char *args, int from_tty, struct cmd_list_element *c)
383 {
384   /* Look past all aliases.  */
385   while (c->is_alias ())
386     c = c->alias_target;
387 
388   help_list (*c->subcommands, c->prefixname ().c_str (),
389 	     all_commands, gdb_stdout);
390 }
391 
392 /* See command.h.  */
393 
394 struct cmd_list_element *
add_basic_prefix_cmd(const char * name,enum command_class theclass,const char * doc,struct cmd_list_element ** subcommands,int allow_unknown,struct cmd_list_element ** list)395 add_basic_prefix_cmd (const char *name, enum command_class theclass,
396 		      const char *doc, struct cmd_list_element **subcommands,
397 		      int allow_unknown, struct cmd_list_element **list)
398 {
399   struct cmd_list_element *cmd = add_prefix_cmd (name, theclass, nullptr,
400 						 doc, subcommands,
401 						 allow_unknown, list);
402   set_cmd_sfunc (cmd, do_prefix_cmd);
403   return cmd;
404 }
405 
406 /* A helper function for add_show_prefix_cmd.  This is a command
407    function that just forwards to cmd_show_list.  */
408 
409 static void
do_show_prefix_cmd(const char * args,int from_tty,struct cmd_list_element * c)410 do_show_prefix_cmd (const char *args, int from_tty, struct cmd_list_element *c)
411 {
412   cmd_show_list (*c->subcommands, from_tty);
413 }
414 
415 /* See command.h.  */
416 
417 struct cmd_list_element *
add_show_prefix_cmd(const char * name,enum command_class theclass,const char * doc,struct cmd_list_element ** subcommands,int allow_unknown,struct cmd_list_element ** list)418 add_show_prefix_cmd (const char *name, enum command_class theclass,
419 		     const char *doc, struct cmd_list_element **subcommands,
420 		     int allow_unknown, struct cmd_list_element **list)
421 {
422   struct cmd_list_element *cmd = add_prefix_cmd (name, theclass, nullptr,
423 						 doc, subcommands,
424 						 allow_unknown, list);
425   set_cmd_sfunc (cmd, do_show_prefix_cmd);
426   return cmd;
427 }
428 
429 /* Like ADD_PREFIX_CMD but sets the suppress_notification pointer on the
430    new command list element.  */
431 
432 struct cmd_list_element *
add_prefix_cmd_suppress_notification(const char * name,enum command_class theclass,cmd_const_cfunc_ftype * fun,const char * doc,struct cmd_list_element ** subcommands,int allow_unknown,struct cmd_list_element ** list,int * suppress_notification)433 add_prefix_cmd_suppress_notification
434 	       (const char *name, enum command_class theclass,
435 		cmd_const_cfunc_ftype *fun,
436 		const char *doc, struct cmd_list_element **subcommands,
437 		int allow_unknown, struct cmd_list_element **list,
438 		int *suppress_notification)
439 {
440   struct cmd_list_element *element
441     = add_prefix_cmd (name, theclass, fun, doc, subcommands,
442 		      allow_unknown, list);
443   element->suppress_notification = suppress_notification;
444   return element;
445 }
446 
447 /* Like add_prefix_cmd but sets the abbrev_flag on the new command.  */
448 
449 struct cmd_list_element *
add_abbrev_prefix_cmd(const char * name,enum command_class theclass,cmd_const_cfunc_ftype * fun,const char * doc,struct cmd_list_element ** subcommands,int allow_unknown,struct cmd_list_element ** list)450 add_abbrev_prefix_cmd (const char *name, enum command_class theclass,
451 		       cmd_const_cfunc_ftype *fun, const char *doc,
452 		       struct cmd_list_element **subcommands,
453 		       int allow_unknown, struct cmd_list_element **list)
454 {
455   struct cmd_list_element *c = add_cmd (name, theclass, fun, doc, list);
456 
457   c->subcommands = subcommands;
458   c->allow_unknown = allow_unknown;
459   c->abbrev_flag = 1;
460   return c;
461 }
462 
463 /* This is an empty "cfunc".  */
464 void
not_just_help_class_command(const char * args,int from_tty)465 not_just_help_class_command (const char *args, int from_tty)
466 {
467 }
468 
469 /* This is an empty "sfunc".  */
470 
471 static void
empty_sfunc(const char * args,int from_tty,struct cmd_list_element * c)472 empty_sfunc (const char *args, int from_tty, struct cmd_list_element *c)
473 {
474 }
475 
476 /* Add element named NAME to command list LIST (the list for set/show
477    or some sublist thereof).
478    TYPE is set_cmd or show_cmd.
479    CLASS is as in add_cmd.
480    VAR_TYPE is the kind of thing we are setting.
481    VAR is address of the variable being controlled by this command.
482    DOC is the documentation string.  */
483 
484 static struct cmd_list_element *
add_set_or_show_cmd(const char * name,enum cmd_types type,enum command_class theclass,var_types var_type,void * var,const char * doc,struct cmd_list_element ** list)485 add_set_or_show_cmd (const char *name,
486 		     enum cmd_types type,
487 		     enum command_class theclass,
488 		     var_types var_type,
489 		     void *var,
490 		     const char *doc,
491 		     struct cmd_list_element **list)
492 {
493   struct cmd_list_element *c = add_cmd (name, theclass, doc, list);
494 
495   gdb_assert (type == set_cmd || type == show_cmd);
496   c->type = type;
497   c->var_type = var_type;
498   c->var = var;
499   /* This needs to be something besides NULL so that this isn't
500      treated as a help class.  */
501   set_cmd_sfunc (c, empty_sfunc);
502   return c;
503 }
504 
505 /* Add element named NAME to both the command SET_LIST and SHOW_LIST.
506    CLASS is as in add_cmd.  VAR_TYPE is the kind of thing we are
507    setting.  VAR is address of the variable being controlled by this
508    command.  SET_FUNC and SHOW_FUNC are the callback functions (if
509    non-NULL).  SET_DOC, SHOW_DOC and HELP_DOC are the documentation
510    strings.
511 
512    Return the newly created set and show commands.  */
513 
514 static set_show_commands
add_setshow_cmd_full(const char * name,enum command_class theclass,var_types var_type,void * var,const char * set_doc,const char * show_doc,const char * help_doc,cmd_const_sfunc_ftype * set_func,show_value_ftype * show_func,struct cmd_list_element ** set_list,struct cmd_list_element ** show_list)515 add_setshow_cmd_full (const char *name,
516 		      enum command_class theclass,
517 		      var_types var_type, void *var,
518 		      const char *set_doc, const char *show_doc,
519 		      const char *help_doc,
520 		      cmd_const_sfunc_ftype *set_func,
521 		      show_value_ftype *show_func,
522 		      struct cmd_list_element **set_list,
523 		      struct cmd_list_element **show_list)
524 {
525   struct cmd_list_element *set;
526   struct cmd_list_element *show;
527   char *full_set_doc;
528   char *full_show_doc;
529 
530   if (help_doc != NULL)
531     {
532       full_set_doc = xstrprintf ("%s\n%s", set_doc, help_doc);
533       full_show_doc = xstrprintf ("%s\n%s", show_doc, help_doc);
534     }
535   else
536     {
537       full_set_doc = xstrdup (set_doc);
538       full_show_doc = xstrdup (show_doc);
539     }
540   set = add_set_or_show_cmd (name, set_cmd, theclass, var_type, var,
541 			     full_set_doc, set_list);
542   set->doc_allocated = 1;
543 
544   if (set_func != NULL)
545     set_cmd_sfunc (set, set_func);
546 
547   show = add_set_or_show_cmd (name, show_cmd, theclass, var_type, var,
548 			      full_show_doc, show_list);
549   show->doc_allocated = 1;
550   show->show_value_func = show_func;
551   /* Disable the default symbol completer.  Doesn't make much sense
552      for the "show" command to complete on anything.  */
553   set_cmd_completer (show, nullptr);
554 
555   return {set, show};
556 }
557 
558 /* Add element named NAME to command list LIST (the list for set or
559    some sublist thereof).  CLASS is as in add_cmd.  ENUMLIST is a list
560    of strings which may follow NAME.  VAR is address of the variable
561    which will contain the matching string (from ENUMLIST).  */
562 
563 set_show_commands
add_setshow_enum_cmd(const char * name,enum command_class theclass,const char * const * enumlist,const char ** var,const char * set_doc,const char * show_doc,const char * help_doc,cmd_const_sfunc_ftype * set_func,show_value_ftype * show_func,struct cmd_list_element ** set_list,struct cmd_list_element ** show_list)564 add_setshow_enum_cmd (const char *name,
565 		      enum command_class theclass,
566 		      const char *const *enumlist,
567 		      const char **var,
568 		      const char *set_doc,
569 		      const char *show_doc,
570 		      const char *help_doc,
571 		      cmd_const_sfunc_ftype *set_func,
572 		      show_value_ftype *show_func,
573 		      struct cmd_list_element **set_list,
574 		      struct cmd_list_element **show_list)
575 {
576   set_show_commands commands
577     =  add_setshow_cmd_full (name, theclass, var_enum, var,
578 			     set_doc, show_doc, help_doc,
579 			     set_func, show_func,
580 			     set_list, show_list);
581   commands.set->enums = enumlist;
582   return commands;
583 }
584 
585 /* See cli-decode.h.  */
586 const char * const auto_boolean_enums[] = { "on", "off", "auto", NULL };
587 
588 /* Add an auto-boolean command named NAME to both the set and show
589    command list lists.  CLASS is as in add_cmd.  VAR is address of the
590    variable which will contain the value.  DOC is the documentation
591    string.  FUNC is the corresponding callback.  */
592 
593 set_show_commands
add_setshow_auto_boolean_cmd(const char * name,enum command_class theclass,enum auto_boolean * var,const char * set_doc,const char * show_doc,const char * help_doc,cmd_const_sfunc_ftype * set_func,show_value_ftype * show_func,struct cmd_list_element ** set_list,struct cmd_list_element ** show_list)594 add_setshow_auto_boolean_cmd (const char *name,
595 			      enum command_class theclass,
596 			      enum auto_boolean *var,
597 			      const char *set_doc, const char *show_doc,
598 			      const char *help_doc,
599 			      cmd_const_sfunc_ftype *set_func,
600 			      show_value_ftype *show_func,
601 			      struct cmd_list_element **set_list,
602 			      struct cmd_list_element **show_list)
603 {
604   set_show_commands commands
605     = add_setshow_cmd_full (name, theclass, var_auto_boolean, var,
606 			    set_doc, show_doc, help_doc,
607 			    set_func, show_func,
608 			    set_list, show_list);
609 
610   commands.set->enums = auto_boolean_enums;
611 
612   return commands;
613 }
614 
615 /* See cli-decode.h.  */
616 const char * const boolean_enums[] = { "on", "off", NULL };
617 
618 /* Add element named NAME to both the set and show command LISTs (the
619    list for set/show or some sublist thereof).  CLASS is as in
620    add_cmd.  VAR is address of the variable which will contain the
621    value.  SET_DOC and SHOW_DOC are the documentation strings.
622    Returns the new command element.  */
623 
624 set_show_commands
add_setshow_boolean_cmd(const char * name,enum command_class theclass,bool * var,const char * set_doc,const char * show_doc,const char * help_doc,cmd_const_sfunc_ftype * set_func,show_value_ftype * show_func,struct cmd_list_element ** set_list,struct cmd_list_element ** show_list)625 add_setshow_boolean_cmd (const char *name, enum command_class theclass, bool *var,
626 			 const char *set_doc, const char *show_doc,
627 			 const char *help_doc,
628 			 cmd_const_sfunc_ftype *set_func,
629 			 show_value_ftype *show_func,
630 			 struct cmd_list_element **set_list,
631 			 struct cmd_list_element **show_list)
632 {
633   set_show_commands commands
634     = add_setshow_cmd_full (name, theclass, var_boolean, var,
635 			    set_doc, show_doc, help_doc,
636 			    set_func, show_func,
637 			    set_list, show_list);
638 
639   commands.set->enums = boolean_enums;
640 
641   return commands;
642 }
643 
644 /* Add element named NAME to both the set and show command LISTs (the
645    list for set/show or some sublist thereof).  */
646 
647 set_show_commands
add_setshow_filename_cmd(const char * name,enum command_class theclass,char ** var,const char * set_doc,const char * show_doc,const char * help_doc,cmd_const_sfunc_ftype * set_func,show_value_ftype * show_func,struct cmd_list_element ** set_list,struct cmd_list_element ** show_list)648 add_setshow_filename_cmd (const char *name, enum command_class theclass,
649 			  char **var,
650 			  const char *set_doc, const char *show_doc,
651 			  const char *help_doc,
652 			  cmd_const_sfunc_ftype *set_func,
653 			  show_value_ftype *show_func,
654 			  struct cmd_list_element **set_list,
655 			  struct cmd_list_element **show_list)
656 {
657   set_show_commands commands
658     = add_setshow_cmd_full (name, theclass, var_filename, var,
659 			    set_doc, show_doc, help_doc,
660 			    set_func, show_func,
661 			    set_list, show_list);
662 
663   set_cmd_completer (commands.set, filename_completer);
664 
665   return commands;
666 }
667 
668 /* Add element named NAME to both the set and show command LISTs (the
669    list for set/show or some sublist thereof).  */
670 
671 set_show_commands
add_setshow_string_cmd(const char * name,enum command_class theclass,char ** var,const char * set_doc,const char * show_doc,const char * help_doc,cmd_const_sfunc_ftype * set_func,show_value_ftype * show_func,struct cmd_list_element ** set_list,struct cmd_list_element ** show_list)672 add_setshow_string_cmd (const char *name, enum command_class theclass,
673 			char **var,
674 			const char *set_doc, const char *show_doc,
675 			const char *help_doc,
676 			cmd_const_sfunc_ftype *set_func,
677 			show_value_ftype *show_func,
678 			struct cmd_list_element **set_list,
679 			struct cmd_list_element **show_list)
680 {
681   set_show_commands commands
682     = add_setshow_cmd_full (name, theclass, var_string, var,
683 			    set_doc, show_doc, help_doc,
684 			    set_func, show_func,
685 			    set_list, show_list);
686 
687   /* Disable the default symbol completer.  */
688   set_cmd_completer (commands.set, nullptr);
689 
690   return commands;
691 }
692 
693 /* Add element named NAME to both the set and show command LISTs (the
694    list for set/show or some sublist thereof).  */
695 
696 set_show_commands
add_setshow_string_noescape_cmd(const char * name,enum command_class theclass,char ** var,const char * set_doc,const char * show_doc,const char * help_doc,cmd_const_sfunc_ftype * set_func,show_value_ftype * show_func,struct cmd_list_element ** set_list,struct cmd_list_element ** show_list)697 add_setshow_string_noescape_cmd (const char *name, enum command_class theclass,
698 				 char **var,
699 				 const char *set_doc, const char *show_doc,
700 				 const char *help_doc,
701 				 cmd_const_sfunc_ftype *set_func,
702 				 show_value_ftype *show_func,
703 				 struct cmd_list_element **set_list,
704 				 struct cmd_list_element **show_list)
705 {
706   set_show_commands commands
707     = add_setshow_cmd_full (name, theclass, var_string_noescape, var,
708 			    set_doc, show_doc, help_doc,
709 			    set_func, show_func,
710 			    set_list, show_list);
711 
712   /* Disable the default symbol completer.  */
713   set_cmd_completer (commands.set, nullptr);
714 
715   return commands;
716 }
717 
718 /* Add element named NAME to both the set and show command LISTs (the
719    list for set/show or some sublist thereof).  */
720 
721 set_show_commands
add_setshow_optional_filename_cmd(const char * name,enum command_class theclass,char ** var,const char * set_doc,const char * show_doc,const char * help_doc,cmd_const_sfunc_ftype * set_func,show_value_ftype * show_func,struct cmd_list_element ** set_list,struct cmd_list_element ** show_list)722 add_setshow_optional_filename_cmd (const char *name, enum command_class theclass,
723 				   char **var,
724 				   const char *set_doc, const char *show_doc,
725 				   const char *help_doc,
726 				   cmd_const_sfunc_ftype *set_func,
727 				   show_value_ftype *show_func,
728 				   struct cmd_list_element **set_list,
729 				   struct cmd_list_element **show_list)
730 {
731   set_show_commands commands
732     = add_setshow_cmd_full (name, theclass, var_optional_filename, var,
733 			    set_doc, show_doc, help_doc,
734 			    set_func, show_func,
735 			    set_list, show_list);
736 
737   set_cmd_completer (commands.set, filename_completer);
738 
739   return commands;
740 }
741 
742 /* Completes on literal "unlimited".  Used by integer commands that
743    support a special "unlimited" value.  */
744 
745 static void
integer_unlimited_completer(struct cmd_list_element * ignore,completion_tracker & tracker,const char * text,const char * word)746 integer_unlimited_completer (struct cmd_list_element *ignore,
747 			     completion_tracker &tracker,
748 			     const char *text, const char *word)
749 {
750   static const char * const keywords[] =
751     {
752       "unlimited",
753       NULL,
754     };
755 
756   complete_on_enum (tracker, keywords, text, word);
757 }
758 
759 /* Add element named NAME to both the set and show command LISTs (the
760    list for set/show or some sublist thereof).  CLASS is as in
761    add_cmd.  VAR is address of the variable which will contain the
762    value.  SET_DOC and SHOW_DOC are the documentation strings.  This
763    function is only used in Python API.  Please don't use it elsewhere.  */
764 
765 set_show_commands
add_setshow_integer_cmd(const char * name,enum command_class theclass,int * var,const char * set_doc,const char * show_doc,const char * help_doc,cmd_const_sfunc_ftype * set_func,show_value_ftype * show_func,struct cmd_list_element ** set_list,struct cmd_list_element ** show_list)766 add_setshow_integer_cmd (const char *name, enum command_class theclass,
767 			 int *var,
768 			 const char *set_doc, const char *show_doc,
769 			 const char *help_doc,
770 			 cmd_const_sfunc_ftype *set_func,
771 			 show_value_ftype *show_func,
772 			 struct cmd_list_element **set_list,
773 			 struct cmd_list_element **show_list)
774 {
775   set_show_commands commands
776     = add_setshow_cmd_full (name, theclass, var_integer, var,
777 			    set_doc, show_doc, help_doc,
778 			    set_func, show_func,
779 			    set_list, show_list);
780 
781   set_cmd_completer (commands.set, integer_unlimited_completer);
782 
783   return commands;
784 }
785 
786 /* Add element named NAME to both the set and show command LISTs (the
787    list for set/show or some sublist thereof).  CLASS is as in
788    add_cmd.  VAR is address of the variable which will contain the
789    value.  SET_DOC and SHOW_DOC are the documentation strings.  */
790 
791 set_show_commands
add_setshow_uinteger_cmd(const char * name,enum command_class theclass,unsigned int * var,const char * set_doc,const char * show_doc,const char * help_doc,cmd_const_sfunc_ftype * set_func,show_value_ftype * show_func,struct cmd_list_element ** set_list,struct cmd_list_element ** show_list)792 add_setshow_uinteger_cmd (const char *name, enum command_class theclass,
793 			  unsigned int *var,
794 			  const char *set_doc, const char *show_doc,
795 			  const char *help_doc,
796 			  cmd_const_sfunc_ftype *set_func,
797 			  show_value_ftype *show_func,
798 			  struct cmd_list_element **set_list,
799 			  struct cmd_list_element **show_list)
800 {
801   set_show_commands commands
802     = add_setshow_cmd_full (name, theclass, var_uinteger, var,
803 			    set_doc, show_doc, help_doc,
804 			    set_func, show_func,
805 			    set_list, show_list);
806 
807   set_cmd_completer (commands.set, integer_unlimited_completer);
808 
809   return commands;
810 }
811 
812 /* Add element named NAME to both the set and show command LISTs (the
813    list for set/show or some sublist thereof).  CLASS is as in
814    add_cmd.  VAR is address of the variable which will contain the
815    value.  SET_DOC and SHOW_DOC are the documentation strings.  */
816 
817 set_show_commands
add_setshow_zinteger_cmd(const char * name,enum command_class theclass,int * var,const char * set_doc,const char * show_doc,const char * help_doc,cmd_const_sfunc_ftype * set_func,show_value_ftype * show_func,struct cmd_list_element ** set_list,struct cmd_list_element ** show_list)818 add_setshow_zinteger_cmd (const char *name, enum command_class theclass,
819 			  int *var,
820 			  const char *set_doc, const char *show_doc,
821 			  const char *help_doc,
822 			  cmd_const_sfunc_ftype *set_func,
823 			  show_value_ftype *show_func,
824 			  struct cmd_list_element **set_list,
825 			  struct cmd_list_element **show_list)
826 {
827   return add_setshow_cmd_full (name, theclass, var_zinteger, var,
828 			       set_doc, show_doc, help_doc,
829 			       set_func, show_func,
830 			       set_list, show_list);
831 }
832 
833 set_show_commands
add_setshow_zuinteger_unlimited_cmd(const char * name,enum command_class theclass,int * var,const char * set_doc,const char * show_doc,const char * help_doc,cmd_const_sfunc_ftype * set_func,show_value_ftype * show_func,struct cmd_list_element ** set_list,struct cmd_list_element ** show_list)834 add_setshow_zuinteger_unlimited_cmd (const char *name,
835 				     enum command_class theclass,
836 				     int *var,
837 				     const char *set_doc,
838 				     const char *show_doc,
839 				     const char *help_doc,
840 				     cmd_const_sfunc_ftype *set_func,
841 				     show_value_ftype *show_func,
842 				     struct cmd_list_element **set_list,
843 				     struct cmd_list_element **show_list)
844 {
845   set_show_commands commands
846     = add_setshow_cmd_full (name, theclass, var_zuinteger_unlimited, var,
847 			    set_doc, show_doc, help_doc,
848 			    set_func, show_func,
849 			    set_list, show_list);
850 
851   set_cmd_completer (commands.set, integer_unlimited_completer);
852 
853   return commands;
854 }
855 
856 /* Add element named NAME to both the set and show command LISTs (the
857    list for set/show or some sublist thereof).  CLASS is as in
858    add_cmd.  VAR is address of the variable which will contain the
859    value.  SET_DOC and SHOW_DOC are the documentation strings.  */
860 
861 set_show_commands
add_setshow_zuinteger_cmd(const char * name,enum command_class theclass,unsigned int * var,const char * set_doc,const char * show_doc,const char * help_doc,cmd_const_sfunc_ftype * set_func,show_value_ftype * show_func,struct cmd_list_element ** set_list,struct cmd_list_element ** show_list)862 add_setshow_zuinteger_cmd (const char *name, enum command_class theclass,
863 			   unsigned int *var,
864 			   const char *set_doc, const char *show_doc,
865 			   const char *help_doc,
866 			   cmd_const_sfunc_ftype *set_func,
867 			   show_value_ftype *show_func,
868 			   struct cmd_list_element **set_list,
869 			   struct cmd_list_element **show_list)
870 {
871   return add_setshow_cmd_full (name, theclass, var_zuinteger, var,
872 			       set_doc, show_doc, help_doc,
873 			       set_func, show_func,
874 			       set_list, show_list);
875 }
876 
877 /* Remove the command named NAME from the command list.  Return the
878    list commands which were aliased to the deleted command.  If the
879    command had no aliases, return NULL.  The various *HOOKs are set to
880    the pre- and post-hook commands for the deleted command.  If the
881    command does not have a hook, the corresponding out parameter is
882    set to NULL.  */
883 
884 static struct cmd_list_element *
delete_cmd(const char * name,struct cmd_list_element ** list,struct cmd_list_element ** prehook,struct cmd_list_element ** prehookee,struct cmd_list_element ** posthook,struct cmd_list_element ** posthookee)885 delete_cmd (const char *name, struct cmd_list_element **list,
886 	    struct cmd_list_element **prehook,
887 	    struct cmd_list_element **prehookee,
888 	    struct cmd_list_element **posthook,
889 	    struct cmd_list_element **posthookee)
890 {
891   struct cmd_list_element *iter;
892   struct cmd_list_element **previous_chain_ptr;
893   struct cmd_list_element *aliases = NULL;
894 
895   *prehook = NULL;
896   *prehookee = NULL;
897   *posthook = NULL;
898   *posthookee = NULL;
899   previous_chain_ptr = list;
900 
901   for (iter = *previous_chain_ptr; iter; iter = *previous_chain_ptr)
902     {
903       if (strcmp (iter->name, name) == 0)
904 	{
905 	  if (iter->destroyer)
906 	    iter->destroyer (iter, iter->context ());
907 
908 	  if (iter->hookee_pre)
909 	    iter->hookee_pre->hook_pre = 0;
910 	  *prehook = iter->hook_pre;
911 	  *prehookee = iter->hookee_pre;
912 	  if (iter->hookee_post)
913 	    iter->hookee_post->hook_post = 0;
914 	  *posthook = iter->hook_post;
915 	  *posthookee = iter->hookee_post;
916 
917 	  /* Update the link.  */
918 	  *previous_chain_ptr = iter->next;
919 
920 	  aliases = iter->aliases;
921 
922 	  /* If this command was an alias, remove it from the list of
923 	     aliases.  */
924 	  if (iter->is_alias ())
925 	    {
926 	      struct cmd_list_element **prevp = &iter->alias_target->aliases;
927 	      struct cmd_list_element *a = *prevp;
928 
929 	      while (a != iter)
930 		{
931 		  prevp = &a->alias_chain;
932 		  a = *prevp;
933 		}
934 	      *prevp = iter->alias_chain;
935 	    }
936 
937 	  delete iter;
938 
939 	  /* We won't see another command with the same name.  */
940 	  break;
941 	}
942       else
943 	previous_chain_ptr = &iter->next;
944     }
945 
946   return aliases;
947 }
948 
949 /* Shorthands to the commands above.  */
950 
951 /* Add an element to the list of info subcommands.  */
952 
953 struct cmd_list_element *
add_info(const char * name,cmd_const_cfunc_ftype * fun,const char * doc)954 add_info (const char *name, cmd_const_cfunc_ftype *fun, const char *doc)
955 {
956   return add_cmd (name, class_info, fun, doc, &infolist);
957 }
958 
959 /* Add an alias to the list of info subcommands.  */
960 
961 cmd_list_element *
add_info_alias(const char * name,cmd_list_element * target,int abbrev_flag)962 add_info_alias (const char *name, cmd_list_element *target, int abbrev_flag)
963 {
964   return add_alias_cmd (name, target, class_run, abbrev_flag, &infolist);
965 }
966 
967 /* Add an element to the list of commands.  */
968 
969 struct cmd_list_element *
add_com(const char * name,enum command_class theclass,cmd_const_cfunc_ftype * fun,const char * doc)970 add_com (const char *name, enum command_class theclass,
971 	 cmd_const_cfunc_ftype *fun,
972 	 const char *doc)
973 {
974   return add_cmd (name, theclass, fun, doc, &cmdlist);
975 }
976 
977 /* Add an alias or abbreviation command to the list of commands.
978    For aliases predefined by GDB (such as bt), THECLASS must be
979    different of class_alias, as class_alias is used to identify
980    user defined aliases.  */
981 
982 cmd_list_element *
add_com_alias(const char * name,cmd_list_element * target,command_class theclass,int abbrev_flag)983 add_com_alias (const char *name, cmd_list_element *target,
984 	       command_class theclass, int abbrev_flag)
985 {
986   return add_alias_cmd (name, target, theclass, abbrev_flag, &cmdlist);
987 }
988 
989 /* Add an element with a suppress notification to the list of commands.  */
990 
991 struct cmd_list_element *
add_com_suppress_notification(const char * name,enum command_class theclass,cmd_const_cfunc_ftype * fun,const char * doc,int * suppress_notification)992 add_com_suppress_notification (const char *name, enum command_class theclass,
993 			       cmd_const_cfunc_ftype *fun, const char *doc,
994 			       int *suppress_notification)
995 {
996   return add_cmd_suppress_notification (name, theclass, fun, doc,
997 					&cmdlist, suppress_notification);
998 }
999 
1000 /* Print the prefix of C followed by name of C in title style.  */
1001 
1002 static void
fput_command_name_styled(struct cmd_list_element * c,struct ui_file * stream)1003 fput_command_name_styled (struct cmd_list_element *c, struct ui_file *stream)
1004 {
1005   std::string prefixname
1006     = c->prefix == nullptr ? "" : c->prefix->prefixname ();
1007 
1008   fprintf_styled (stream, title_style.style (), "%s%s",
1009 		  prefixname.c_str (), c->name);
1010 }
1011 
1012 /* Print the definition of alias C using title style for alias
1013    and aliased command.  */
1014 
1015 static void
fput_alias_definition_styled(struct cmd_list_element * c,struct ui_file * stream)1016 fput_alias_definition_styled (struct cmd_list_element *c,
1017 			      struct ui_file *stream)
1018 {
1019   gdb_assert (c->is_alias ());
1020   fputs_filtered ("  alias ", stream);
1021   fput_command_name_styled (c, stream);
1022   fprintf_filtered (stream, " = ");
1023   fput_command_name_styled (c->alias_target, stream);
1024   fprintf_filtered (stream, " %s\n", c->default_args.c_str ());
1025 }
1026 
1027 /* Print the definition of the aliases of CMD that have default args.  */
1028 
1029 static void
fput_aliases_definition_styled(struct cmd_list_element * cmd,struct ui_file * stream)1030 fput_aliases_definition_styled (struct cmd_list_element *cmd,
1031 				struct ui_file *stream)
1032 {
1033   if (cmd->aliases != nullptr)
1034     {
1035       for (cmd_list_element *iter = cmd->aliases;
1036 	   iter;
1037 	   iter = iter->alias_chain)
1038 	{
1039 	  if (!iter->default_args.empty ())
1040 	    fput_alias_definition_styled (iter, stream);
1041 	}
1042     }
1043 }
1044 
1045 
1046 /* If C has one or more aliases, style print the name of C and
1047    the name of its aliases, separated by commas.
1048    If ALWAYS_FPUT_C_NAME, print the name of C even if it has no aliases.
1049    If one or more names are printed, POSTFIX is printed after the last name.
1050 */
1051 
1052 static void
fput_command_names_styled(struct cmd_list_element * c,bool always_fput_c_name,const char * postfix,struct ui_file * stream)1053 fput_command_names_styled (struct cmd_list_element *c,
1054 			   bool always_fput_c_name, const char *postfix,
1055 			   struct ui_file *stream)
1056 {
1057   if (always_fput_c_name ||  c->aliases != nullptr)
1058     fput_command_name_styled (c, stream);
1059   if (c->aliases != nullptr)
1060     {
1061       for (cmd_list_element *iter = c->aliases; iter; iter = iter->alias_chain)
1062 	{
1063 	  fputs_filtered (", ", stream);
1064 	  wrap_here ("   ");
1065 	  fput_command_name_styled (iter, stream);
1066 	}
1067     }
1068   if (always_fput_c_name ||  c->aliases != nullptr)
1069     fputs_filtered (postfix, stream);
1070 }
1071 
1072 /* If VERBOSE, print the full help for command C and highlight the
1073    documentation parts matching HIGHLIGHT,
1074    otherwise print only one-line help for command C.  */
1075 
1076 static void
print_doc_of_command(struct cmd_list_element * c,const char * prefix,bool verbose,compiled_regex & highlight,struct ui_file * stream)1077 print_doc_of_command (struct cmd_list_element *c, const char *prefix,
1078 		      bool verbose, compiled_regex &highlight,
1079 		      struct ui_file *stream)
1080 {
1081   /* When printing the full documentation, add a line to separate
1082      this documentation from the previous command help, in the likely
1083      case that apropos finds several commands.  */
1084   if (verbose)
1085     fputs_filtered ("\n", stream);
1086 
1087   fput_command_names_styled (c, true,
1088 			     verbose ? "" : " -- ", stream);
1089   if (verbose)
1090     {
1091       fputs_filtered ("\n", stream);
1092       fput_aliases_definition_styled (c, stream);
1093       fputs_highlighted (c->doc, highlight, stream);
1094       fputs_filtered ("\n", stream);
1095     }
1096   else
1097     {
1098       print_doc_line (stream, c->doc, false);
1099       fputs_filtered ("\n", stream);
1100       fput_aliases_definition_styled (c, stream);
1101     }
1102 }
1103 
1104 /* Recursively walk the commandlist structures, and print out the
1105    documentation of commands that match our regex in either their
1106    name, or their documentation.
1107    If VERBOSE, prints the complete documentation and highlight the
1108    documentation parts matching REGEX, otherwise prints only
1109    the first line.
1110 */
1111 void
apropos_cmd(struct ui_file * stream,struct cmd_list_element * commandlist,bool verbose,compiled_regex & regex,const char * prefix)1112 apropos_cmd (struct ui_file *stream,
1113 	     struct cmd_list_element *commandlist,
1114 	     bool verbose, compiled_regex &regex, const char *prefix)
1115 {
1116   struct cmd_list_element *c;
1117   int returnvalue;
1118 
1119   /* Walk through the commands.  */
1120   for (c=commandlist;c;c=c->next)
1121     {
1122       if (c->is_alias ())
1123 	{
1124 	  /* Command aliases/abbreviations are skipped to ensure we print the
1125 	     doc of a command only once, when encountering the aliased
1126 	     command.  */
1127 	  continue;
1128 	}
1129 
1130       returnvalue = -1; /* Needed to avoid double printing.  */
1131       if (c->name != NULL)
1132 	{
1133 	  size_t name_len = strlen (c->name);
1134 
1135 	  /* Try to match against the name.  */
1136 	  returnvalue = regex.search (c->name, name_len, 0, name_len, NULL);
1137 	  if (returnvalue >= 0)
1138 	    print_doc_of_command (c, prefix, verbose, regex, stream);
1139 
1140 	  /* Try to match against the name of the aliases.  */
1141 	  for (cmd_list_element *iter = c->aliases;
1142 	       returnvalue < 0 && iter;
1143 	       iter = iter->alias_chain)
1144 	    {
1145 	      name_len = strlen (iter->name);
1146 	      returnvalue = regex.search (iter->name, name_len, 0, name_len, NULL);
1147 	      if (returnvalue >= 0)
1148 		print_doc_of_command (c, prefix, verbose, regex, stream);
1149 	    }
1150 	}
1151       if (c->doc != NULL && returnvalue < 0)
1152 	{
1153 	  size_t doc_len = strlen (c->doc);
1154 
1155 	  /* Try to match against documentation.  */
1156 	  if (regex.search (c->doc, doc_len, 0, doc_len, NULL) >= 0)
1157 	    print_doc_of_command (c, prefix, verbose, regex, stream);
1158 	}
1159       /* Check if this command has subcommands.  */
1160       if (c->is_prefix ())
1161 	{
1162 	  /* Recursively call ourselves on the subcommand list,
1163 	     passing the right prefix in.  */
1164 	  apropos_cmd (stream, *c->subcommands, verbose, regex,
1165 		       c->prefixname ().c_str ());
1166 	}
1167     }
1168 }
1169 
1170 /* This command really has to deal with two things:
1171    1) I want documentation on *this string* (usually called by
1172       "help commandname").
1173 
1174    2) I want documentation on *this list* (usually called by giving a
1175       command that requires subcommands.  Also called by saying just
1176       "help".)
1177 
1178    I am going to split this into two separate commands, help_cmd and
1179    help_list.  */
1180 
1181 void
help_cmd(const char * command,struct ui_file * stream)1182 help_cmd (const char *command, struct ui_file *stream)
1183 {
1184   struct cmd_list_element *c, *alias, *prefix_cmd, *c_cmd;
1185 
1186   if (!command)
1187     {
1188       help_list (cmdlist, "", all_classes, stream);
1189       return;
1190     }
1191 
1192   if (strcmp (command, "all") == 0)
1193     {
1194       help_all (stream);
1195       return;
1196     }
1197 
1198   const char *orig_command = command;
1199   c = lookup_cmd (&command, cmdlist, "", NULL, 0, 0);
1200 
1201   if (c == 0)
1202     return;
1203 
1204   lookup_cmd_composition (orig_command, &alias, &prefix_cmd, &c_cmd);
1205 
1206   /* There are three cases here.
1207      If c->subcommands is nonzero, we have a prefix command.
1208      Print its documentation, then list its subcommands.
1209 
1210      If c->func is non NULL, we really have a command.  Print its
1211      documentation and return.
1212 
1213      If c->func is NULL, we have a class name.  Print its
1214      documentation (as if it were a command) and then set class to the
1215      number of this class so that the commands in the class will be
1216      listed.  */
1217 
1218   /* If the user asked 'help somecommand' and there is no alias,
1219      the false indicates to not output the (single) command name.  */
1220   fput_command_names_styled (c, false, "\n", stream);
1221   fput_aliases_definition_styled (c, stream);
1222   fputs_filtered (c->doc, stream);
1223   fputs_filtered ("\n", stream);
1224 
1225   if (!c->is_prefix () && !c->is_command_class_help ())
1226     return;
1227 
1228   fprintf_filtered (stream, "\n");
1229 
1230   /* If this is a prefix command, print it's subcommands.  */
1231   if (c->is_prefix ())
1232     help_list (*c->subcommands, c->prefixname ().c_str (),
1233 	       all_commands, stream);
1234 
1235   /* If this is a class name, print all of the commands in the class.  */
1236   if (c->is_command_class_help ())
1237     help_list (cmdlist, "", c->theclass, stream);
1238 
1239   if (c->hook_pre || c->hook_post)
1240     fprintf_filtered (stream,
1241 		      "\nThis command has a hook (or hooks) defined:\n");
1242 
1243   if (c->hook_pre)
1244     fprintf_filtered (stream,
1245 		      "\tThis command is run after  : %s (pre hook)\n",
1246 		    c->hook_pre->name);
1247   if (c->hook_post)
1248     fprintf_filtered (stream,
1249 		      "\tThis command is run before : %s (post hook)\n",
1250 		    c->hook_post->name);
1251 }
1252 
1253 /*
1254  * Get a specific kind of help on a command list.
1255  *
1256  * LIST is the list.
1257  * CMDTYPE is the prefix to use in the title string.
1258  * CLASS is the class with which to list the nodes of this list (see
1259  * documentation for help_cmd_list below),  As usual, ALL_COMMANDS for
1260  * everything, ALL_CLASSES for just classes, and non-negative for only things
1261  * in a specific class.
1262  * and STREAM is the output stream on which to print things.
1263  * If you call this routine with a class >= 0, it recurses.
1264  */
1265 void
help_list(struct cmd_list_element * list,const char * cmdtype,enum command_class theclass,struct ui_file * stream)1266 help_list (struct cmd_list_element *list, const char *cmdtype,
1267 	   enum command_class theclass, struct ui_file *stream)
1268 {
1269   int len;
1270   char *cmdtype1, *cmdtype2;
1271 
1272   /* If CMDTYPE is "foo ", CMDTYPE1 gets " foo" and CMDTYPE2 gets "foo sub".
1273    */
1274   len = strlen (cmdtype);
1275   cmdtype1 = (char *) alloca (len + 1);
1276   cmdtype1[0] = 0;
1277   cmdtype2 = (char *) alloca (len + 4);
1278   cmdtype2[0] = 0;
1279   if (len)
1280     {
1281       cmdtype1[0] = ' ';
1282       memcpy (cmdtype1 + 1, cmdtype, len - 1);
1283       cmdtype1[len] = 0;
1284       memcpy (cmdtype2, cmdtype, len - 1);
1285       strcpy (cmdtype2 + len - 1, " sub");
1286     }
1287 
1288   if (theclass == all_classes)
1289     fprintf_filtered (stream, "List of classes of %scommands:\n\n", cmdtype2);
1290   else
1291     fprintf_filtered (stream, "List of %scommands:\n\n", cmdtype2);
1292 
1293   help_cmd_list (list, theclass, theclass >= 0, stream);
1294 
1295   if (theclass == all_classes)
1296     {
1297       fprintf_filtered (stream, "\n\
1298 Type \"help%s\" followed by a class name for a list of commands in ",
1299 			cmdtype1);
1300       wrap_here ("");
1301       fprintf_filtered (stream, "that class.");
1302 
1303       fprintf_filtered (stream, "\n\
1304 Type \"help all\" for the list of all commands.");
1305     }
1306 
1307   fprintf_filtered (stream, "\nType \"help%s\" followed by %scommand name ",
1308 		    cmdtype1, cmdtype2);
1309   wrap_here ("");
1310   fputs_filtered ("for ", stream);
1311   wrap_here ("");
1312   fputs_filtered ("full ", stream);
1313   wrap_here ("");
1314   fputs_filtered ("documentation.\n", stream);
1315   fputs_filtered ("Type \"apropos word\" to search "
1316 		  "for commands related to \"word\".\n", stream);
1317   fputs_filtered ("Type \"apropos -v word\" for full documentation", stream);
1318   wrap_here ("");
1319   fputs_filtered (" of commands related to \"word\".\n", stream);
1320   fputs_filtered ("Command name abbreviations are allowed if unambiguous.\n",
1321 		  stream);
1322 }
1323 
1324 static void
help_all(struct ui_file * stream)1325 help_all (struct ui_file *stream)
1326 {
1327   struct cmd_list_element *c;
1328   int seen_unclassified = 0;
1329 
1330   for (c = cmdlist; c; c = c->next)
1331     {
1332       if (c->abbrev_flag)
1333 	continue;
1334       /* If this is a class name, print all of the commands in the
1335 	 class.  */
1336 
1337       if (c->is_command_class_help ())
1338 	{
1339 	  fprintf_filtered (stream, "\nCommand class: %s\n\n", c->name);
1340 	  help_cmd_list (cmdlist, c->theclass, true, stream);
1341 	}
1342     }
1343 
1344   /* While it's expected that all commands are in some class,
1345      as a safety measure, we'll print commands outside of any
1346      class at the end.  */
1347 
1348   for (c = cmdlist; c; c = c->next)
1349     {
1350       if (c->abbrev_flag)
1351 	continue;
1352 
1353       if (c->theclass == no_class)
1354 	{
1355 	  if (!seen_unclassified)
1356 	    {
1357 	      fprintf_filtered (stream, "\nUnclassified commands\n\n");
1358 	      seen_unclassified = 1;
1359 	    }
1360 	  print_help_for_command (c, true, stream);
1361 	}
1362     }
1363 
1364 }
1365 
1366 /* See cli-decode.h.  */
1367 
1368 void
print_doc_line(struct ui_file * stream,const char * str,bool for_value_prefix)1369 print_doc_line (struct ui_file *stream, const char *str,
1370 		bool for_value_prefix)
1371 {
1372   static char *line_buffer = 0;
1373   static int line_size;
1374   const char *p;
1375 
1376   if (!line_buffer)
1377     {
1378       line_size = 80;
1379       line_buffer = (char *) xmalloc (line_size);
1380     }
1381 
1382   /* Searches for the first end of line or the end of STR.  */
1383   p = str;
1384   while (*p && *p != '\n')
1385     p++;
1386   if (p - str > line_size - 1)
1387     {
1388       line_size = p - str + 1;
1389       xfree (line_buffer);
1390       line_buffer = (char *) xmalloc (line_size);
1391     }
1392   strncpy (line_buffer, str, p - str);
1393   if (for_value_prefix)
1394     {
1395       if (islower (line_buffer[0]))
1396 	line_buffer[0] = toupper (line_buffer[0]);
1397       gdb_assert (p > str);
1398       if (line_buffer[p - str - 1] == '.')
1399 	line_buffer[p - str - 1] = '\0';
1400       else
1401 	line_buffer[p - str] = '\0';
1402     }
1403   else
1404     line_buffer[p - str] = '\0';
1405   fputs_filtered (line_buffer, stream);
1406 }
1407 
1408 /* Print one-line help for command C.
1409    If RECURSE is non-zero, also print one-line descriptions
1410    of all prefixed subcommands.  */
1411 static void
print_help_for_command(struct cmd_list_element * c,bool recurse,struct ui_file * stream)1412 print_help_for_command (struct cmd_list_element *c,
1413 			bool recurse, struct ui_file *stream)
1414 {
1415   fput_command_names_styled (c, true, " -- ", stream);
1416   print_doc_line (stream, c->doc, false);
1417   fputs_filtered ("\n", stream);
1418   if (!c->default_args.empty ())
1419     fput_alias_definition_styled (c, stream);
1420   fput_aliases_definition_styled (c, stream);
1421 
1422   if (recurse
1423       && c->is_prefix ()
1424       && c->abbrev_flag == 0)
1425     /* Subcommands of a prefix command typically have 'all_commands'
1426        as class.  If we pass CLASS to recursive invocation,
1427        most often we won't see anything.  */
1428     help_cmd_list (*c->subcommands, all_commands, true, stream);
1429 }
1430 
1431 /*
1432  * Implement a help command on command list LIST.
1433  * RECURSE should be non-zero if this should be done recursively on
1434  * all sublists of LIST.
1435  * STREAM is the stream upon which the output should be written.
1436  * THECLASS should be:
1437  *      A non-negative class number to list only commands in that
1438  *      ALL_COMMANDS to list all commands in list.
1439  *      ALL_CLASSES  to list all classes in list.
1440  *
1441  *   Note that aliases are only shown when THECLASS is class_alias.
1442  *   In the other cases, the aliases will be shown together with their
1443  *   aliased command.
1444  *
1445  *   Note that RECURSE will be active on *all* sublists, not just the
1446  * ones selected by the criteria above (ie. the selection mechanism
1447  * is at the low level, not the high-level).
1448  */
1449 
1450 static void
help_cmd_list(struct cmd_list_element * list,enum command_class theclass,bool recurse,struct ui_file * stream)1451 help_cmd_list (struct cmd_list_element *list, enum command_class theclass,
1452 	       bool recurse, struct ui_file *stream)
1453 {
1454   struct cmd_list_element *c;
1455 
1456   for (c = list; c; c = c->next)
1457     {
1458       if (c->abbrev_flag == 1 || c->cmd_deprecated)
1459 	{
1460 	  /* Do not show abbreviations or deprecated commands.  */
1461 	  continue;
1462 	}
1463 
1464       if (c->is_alias () && theclass != class_alias)
1465 	{
1466 	  /* Do not show an alias, unless specifically showing the
1467 	     list of aliases:  for all other classes, an alias is
1468 	     shown (if needed) together with its aliased command.  */
1469 	  continue;
1470 	}
1471 
1472       if (theclass == all_commands
1473 	  || (theclass == all_classes && c->is_command_class_help ())
1474 	  || (theclass == c->theclass && !c->is_command_class_help ()))
1475 	{
1476 	  /* show C when
1477 	     - showing all commands
1478 	     - showing all classes and C is a help class
1479 	     - showing commands of THECLASS and C is not the help class  */
1480 
1481 	  /* If we show the class_alias and C is an alias, do not recurse,
1482 	     as this would show the (possibly very long) not very useful
1483 	     list of sub-commands of the aliased command.  */
1484 	  print_help_for_command
1485 	    (c,
1486 	     recurse && (theclass != class_alias || !c->is_alias ()),
1487 	     stream);
1488 	  continue;
1489 	}
1490 
1491       if (recurse
1492 	  && (theclass == class_user || theclass == class_alias)
1493 	  && c->is_prefix ())
1494 	{
1495 	  /* User-defined commands or aliases may be subcommands.  */
1496 	  help_cmd_list (*c->subcommands, theclass, recurse, stream);
1497 	  continue;
1498 	}
1499 
1500       /* Do not show C or recurse on C, e.g. because C does not belong to
1501 	 THECLASS or because C is a help class.  */
1502     }
1503 }
1504 
1505 
1506 /* Search the input clist for 'command'.  Return the command if
1507    found (or NULL if not), and return the number of commands
1508    found in nfound.  */
1509 
1510 static struct cmd_list_element *
find_cmd(const char * command,int len,struct cmd_list_element * clist,int ignore_help_classes,int * nfound)1511 find_cmd (const char *command, int len, struct cmd_list_element *clist,
1512 	  int ignore_help_classes, int *nfound)
1513 {
1514   struct cmd_list_element *found, *c;
1515 
1516   found = NULL;
1517   *nfound = 0;
1518   for (c = clist; c; c = c->next)
1519     if (!strncmp (command, c->name, len)
1520 	&& (!ignore_help_classes || !c->is_command_class_help ()))
1521       {
1522 	found = c;
1523 	(*nfound)++;
1524 	if (c->name[len] == '\0')
1525 	  {
1526 	    *nfound = 1;
1527 	    break;
1528 	  }
1529       }
1530   return found;
1531 }
1532 
1533 /* Return the length of command name in TEXT.  */
1534 
1535 int
find_command_name_length(const char * text)1536 find_command_name_length (const char *text)
1537 {
1538   const char *p = text;
1539 
1540   /* Treating underscores as part of command words is important
1541      so that "set args_foo()" doesn't get interpreted as
1542      "set args _foo()".  */
1543   /* Some characters are only used for TUI specific commands.
1544      However, they are always allowed for the sake of consistency.
1545 
1546      Note that this is larger than the character set allowed when
1547      creating user-defined commands.  */
1548 
1549   /* Recognize the single character commands so that, e.g., "!ls"
1550      works as expected.  */
1551   if (*p == '!' || *p == '|')
1552     return 1;
1553 
1554   while (valid_cmd_char_p (*p)
1555 	 /* Characters used by TUI specific commands.  */
1556 	 || *p == '+' || *p == '<' || *p == '>' || *p == '$')
1557     p++;
1558 
1559   return p - text;
1560 }
1561 
1562 /* See command.h.  */
1563 
1564 bool
valid_cmd_char_p(int c)1565 valid_cmd_char_p (int c)
1566 {
1567   /* Alas "42" is a legitimate user-defined command.
1568      In the interests of not breaking anything we preserve that.  */
1569 
1570   return isalnum (c) || c == '-' || c == '_' || c == '.';
1571 }
1572 
1573 /* See command.h.  */
1574 
1575 bool
valid_user_defined_cmd_name_p(const char * name)1576 valid_user_defined_cmd_name_p (const char *name)
1577 {
1578   const char *p;
1579 
1580   if (*name == '\0')
1581     return false;
1582 
1583   for (p = name; *p != '\0'; ++p)
1584     {
1585       if (valid_cmd_char_p (*p))
1586 	; /* Ok.  */
1587       else
1588 	return false;
1589     }
1590 
1591   return true;
1592 }
1593 
1594 /* See command.h.  */
1595 
1596 struct cmd_list_element *
lookup_cmd_1(const char ** text,struct cmd_list_element * clist,struct cmd_list_element ** result_list,std::string * default_args,int ignore_help_classes,bool lookup_for_completion_p)1597 lookup_cmd_1 (const char **text, struct cmd_list_element *clist,
1598 	      struct cmd_list_element **result_list, std::string *default_args,
1599 	      int ignore_help_classes, bool lookup_for_completion_p)
1600 {
1601   char *command;
1602   int len, nfound;
1603   struct cmd_list_element *found, *c;
1604   bool found_alias = false;
1605   const char *line = *text;
1606 
1607   while (**text == ' ' || **text == '\t')
1608     (*text)++;
1609 
1610   /* Identify the name of the command.  */
1611   len = find_command_name_length (*text);
1612 
1613   /* If nothing but whitespace, return 0.  */
1614   if (len == 0)
1615     return 0;
1616 
1617   /* *text and p now bracket the first command word to lookup (and
1618      it's length is len).  We copy this into a local temporary.  */
1619 
1620 
1621   command = (char *) alloca (len + 1);
1622   memcpy (command, *text, len);
1623   command[len] = '\0';
1624 
1625   /* Look it up.  */
1626   found = 0;
1627   nfound = 0;
1628   found = find_cmd (command, len, clist, ignore_help_classes, &nfound);
1629 
1630   /* If nothing matches, we have a simple failure.  */
1631   if (nfound == 0)
1632     return 0;
1633 
1634   if (nfound > 1)
1635     {
1636       if (result_list != nullptr)
1637 	/* Will be modified in calling routine
1638 	   if we know what the prefix command is.  */
1639 	*result_list = 0;
1640       if (default_args != nullptr)
1641 	*default_args = std::string ();
1642       return CMD_LIST_AMBIGUOUS;	/* Ambiguous.  */
1643     }
1644 
1645   /* We've matched something on this list.  Move text pointer forward.  */
1646 
1647   *text += len;
1648 
1649   if (found->is_alias ())
1650     {
1651       /* We drop the alias (abbreviation) in favor of the command it
1652        is pointing to.  If the alias is deprecated, though, we need to
1653        warn the user about it before we drop it.  Note that while we
1654        are warning about the alias, we may also warn about the command
1655        itself and we will adjust the appropriate DEPRECATED_WARN_USER
1656        flags.  */
1657 
1658       if (found->deprecated_warn_user && !lookup_for_completion_p)
1659 	deprecated_cmd_warning (line, clist);
1660 
1661 
1662       /* Return the default_args of the alias, not the default_args
1663 	 of the command it is pointing to.  */
1664       if (default_args != nullptr)
1665 	*default_args = found->default_args;
1666       found = found->alias_target;
1667       found_alias = true;
1668     }
1669   /* If we found a prefix command, keep looking.  */
1670 
1671   if (found->is_prefix ())
1672     {
1673       c = lookup_cmd_1 (text, *found->subcommands, result_list, default_args,
1674 			ignore_help_classes, lookup_for_completion_p);
1675       if (!c)
1676 	{
1677 	  /* Didn't find anything; this is as far as we got.  */
1678 	  if (result_list != nullptr)
1679 	    *result_list = clist;
1680 	  if (!found_alias && default_args != nullptr)
1681 	    *default_args = found->default_args;
1682 	  return found;
1683 	}
1684       else if (c == CMD_LIST_AMBIGUOUS)
1685 	{
1686 	  /* We've gotten this far properly, but the next step is
1687 	     ambiguous.  We need to set the result list to the best
1688 	     we've found (if an inferior hasn't already set it).  */
1689 	  if (result_list != nullptr)
1690 	    if (!*result_list)
1691 	      /* This used to say *result_list = *found->subcommands.
1692 		 If that was correct, need to modify the documentation
1693 		 at the top of this function to clarify what is
1694 		 supposed to be going on.  */
1695 	      *result_list = found;
1696 	  /* For ambiguous commands, do not return any default_args args.  */
1697 	  if (default_args != nullptr)
1698 	    *default_args = std::string ();
1699 	  return c;
1700 	}
1701       else
1702 	{
1703 	  /* We matched!  */
1704 	  return c;
1705 	}
1706     }
1707   else
1708     {
1709       if (result_list != nullptr)
1710 	*result_list = clist;
1711       if (!found_alias && default_args != nullptr)
1712 	*default_args = found->default_args;
1713       return found;
1714     }
1715 }
1716 
1717 /* All this hair to move the space to the front of cmdtype */
1718 
1719 static void
undef_cmd_error(const char * cmdtype,const char * q)1720 undef_cmd_error (const char *cmdtype, const char *q)
1721 {
1722   error (_("Undefined %scommand: \"%s\".  Try \"help%s%.*s\"."),
1723 	 cmdtype,
1724 	 q,
1725 	 *cmdtype ? " " : "",
1726 	 (int) strlen (cmdtype) - 1,
1727 	 cmdtype);
1728 }
1729 
1730 /* Look up the contents of *LINE as a command in the command list LIST.
1731    LIST is a chain of struct cmd_list_element's.
1732    If it is found, return the struct cmd_list_element for that command,
1733    update *LINE to point after the command name, at the first argument
1734    and update *DEFAULT_ARGS (if DEFAULT_ARGS is not null) to the default
1735    args to prepend to the user provided args when running the command.
1736    Note that if the found cmd_list_element is found via an alias,
1737    the default args of the alias are returned.
1738 
1739    If not found, call error if ALLOW_UNKNOWN is zero
1740    otherwise (or if error returns) return zero.
1741    Call error if specified command is ambiguous,
1742    unless ALLOW_UNKNOWN is negative.
1743    CMDTYPE precedes the word "command" in the error message.
1744 
1745    If IGNORE_HELP_CLASSES is nonzero, ignore any command list
1746    elements which are actually help classes rather than commands (i.e.
1747    the function field of the struct cmd_list_element is 0).  */
1748 
1749 struct cmd_list_element *
lookup_cmd(const char ** line,struct cmd_list_element * list,const char * cmdtype,std::string * default_args,int allow_unknown,int ignore_help_classes)1750 lookup_cmd (const char **line, struct cmd_list_element *list,
1751 	    const char *cmdtype,
1752 	    std::string *default_args,
1753 	    int allow_unknown, int ignore_help_classes)
1754 {
1755   struct cmd_list_element *last_list = 0;
1756   struct cmd_list_element *c;
1757 
1758   /* Note: Do not remove trailing whitespace here because this
1759      would be wrong for complete_command.  Jim Kingdon  */
1760 
1761   if (!*line)
1762     error (_("Lack of needed %scommand"), cmdtype);
1763 
1764   c = lookup_cmd_1 (line, list, &last_list, default_args, ignore_help_classes);
1765 
1766   if (!c)
1767     {
1768       if (!allow_unknown)
1769 	{
1770 	  char *q;
1771 	  int len = find_command_name_length (*line);
1772 
1773 	  q = (char *) alloca (len + 1);
1774 	  strncpy (q, *line, len);
1775 	  q[len] = '\0';
1776 	  undef_cmd_error (cmdtype, q);
1777 	}
1778       else
1779 	return 0;
1780     }
1781   else if (c == CMD_LIST_AMBIGUOUS)
1782     {
1783       /* Ambigous.  Local values should be off subcommands or called
1784 	 values.  */
1785       int local_allow_unknown = (last_list ? last_list->allow_unknown :
1786 				 allow_unknown);
1787       std::string local_cmdtype
1788 	= last_list ? last_list->prefixname () : cmdtype;
1789       struct cmd_list_element *local_list =
1790 	(last_list ? *(last_list->subcommands) : list);
1791 
1792       if (local_allow_unknown < 0)
1793 	{
1794 	  if (last_list)
1795 	    return last_list;	/* Found something.  */
1796 	  else
1797 	    return 0;		/* Found nothing.  */
1798 	}
1799       else
1800 	{
1801 	  /* Report as error.  */
1802 	  int amb_len;
1803 	  char ambbuf[100];
1804 
1805 	  for (amb_len = 0;
1806 	       ((*line)[amb_len] && (*line)[amb_len] != ' '
1807 		&& (*line)[amb_len] != '\t');
1808 	       amb_len++)
1809 	    ;
1810 
1811 	  ambbuf[0] = 0;
1812 	  for (c = local_list; c; c = c->next)
1813 	    if (!strncmp (*line, c->name, amb_len))
1814 	      {
1815 		if (strlen (ambbuf) + strlen (c->name) + 6
1816 		    < (int) sizeof ambbuf)
1817 		  {
1818 		    if (strlen (ambbuf))
1819 		      strcat (ambbuf, ", ");
1820 		    strcat (ambbuf, c->name);
1821 		  }
1822 		else
1823 		  {
1824 		    strcat (ambbuf, "..");
1825 		    break;
1826 		  }
1827 	      }
1828 	  error (_("Ambiguous %scommand \"%s\": %s."),
1829 		 local_cmdtype.c_str (), *line, ambbuf);
1830 	}
1831     }
1832   else
1833     {
1834       if (c->type == set_cmd && **line != '\0' && !isspace (**line))
1835 	error (_("Argument must be preceded by space."));
1836 
1837       /* We've got something.  It may still not be what the caller
1838 	 wants (if this command *needs* a subcommand).  */
1839       while (**line == ' ' || **line == '\t')
1840 	(*line)++;
1841 
1842       if (c->is_prefix () && **line && !c->allow_unknown)
1843 	undef_cmd_error (c->prefixname ().c_str (), *line);
1844 
1845       /* Seems to be what he wants.  Return it.  */
1846       return c;
1847     }
1848   return 0;
1849 }
1850 
1851 /* See command.h.  */
1852 
1853 struct cmd_list_element *
lookup_cmd_exact(const char * name,struct cmd_list_element * list,bool ignore_help_classes)1854 lookup_cmd_exact (const char *name,
1855 		  struct cmd_list_element *list,
1856 		  bool ignore_help_classes)
1857 {
1858   const char *tem = name;
1859   struct cmd_list_element *cmd = lookup_cmd (&tem, list, "", NULL, -1,
1860 					     ignore_help_classes);
1861   if (cmd != nullptr && strcmp (name, cmd->name) != 0)
1862     cmd = nullptr;
1863   return cmd;
1864 }
1865 
1866 /* We are here presumably because an alias or command in TEXT is
1867    deprecated and a warning message should be generated.  This
1868    function decodes TEXT and potentially generates a warning message
1869    as outlined below.
1870 
1871    Example for 'set endian big' which has a fictitious alias 'seb'.
1872 
1873    If alias wasn't used in TEXT, and the command is deprecated:
1874    "warning: 'set endian big' is deprecated."
1875 
1876    If alias was used, and only the alias is deprecated:
1877    "warning: 'seb' an alias for the command 'set endian big' is deprecated."
1878 
1879    If alias was used and command is deprecated (regardless of whether
1880    the alias itself is deprecated:
1881 
1882    "warning: 'set endian big' (seb) is deprecated."
1883 
1884    After the message has been sent, clear the appropriate flags in the
1885    command and/or the alias so the user is no longer bothered.
1886 
1887 */
1888 void
deprecated_cmd_warning(const char * text,struct cmd_list_element * list)1889 deprecated_cmd_warning (const char *text, struct cmd_list_element *list)
1890 {
1891   struct cmd_list_element *alias = nullptr;
1892   struct cmd_list_element *cmd = nullptr;
1893 
1894   /* Return if text doesn't evaluate to a command.  We place this lookup
1895      within its own scope so that the PREFIX_CMD local is not visible
1896      later in this function.  The value returned in PREFIX_CMD is based on
1897      the prefix found in TEXT, and is our case this prefix can be missing
1898      in some situations (when LIST is not the global CMDLIST).
1899 
1900      It is better for our purposes to use the prefix commands directly from
1901      the ALIAS and CMD results.  */
1902   {
1903     struct cmd_list_element *prefix_cmd = nullptr;
1904     if (!lookup_cmd_composition_1 (text, &alias, &prefix_cmd, &cmd, list))
1905       return;
1906   }
1907 
1908   /* Return if nothing is deprecated.  */
1909   if (!((alias != nullptr ? alias->deprecated_warn_user : 0)
1910 	|| cmd->deprecated_warn_user))
1911     return;
1912 
1913   /* Join command prefix (if any) and the command name.  */
1914   std::string tmp_cmd_str;
1915   if (cmd->prefix != nullptr)
1916     tmp_cmd_str += cmd->prefix->prefixname ();
1917   tmp_cmd_str += std::string (cmd->name);
1918 
1919   /* Display the appropriate first line, this warns that the thing the user
1920      entered is deprecated.  */
1921   if (alias != nullptr)
1922     {
1923       /* Join the alias prefix (if any) and the alias name.  */
1924       std::string tmp_alias_str;
1925       if (alias->prefix != nullptr)
1926 	tmp_alias_str += alias->prefix->prefixname ();
1927       tmp_alias_str += std::string (alias->name);
1928 
1929       if (cmd->cmd_deprecated)
1930 	printf_filtered (_("Warning: command '%ps' (%ps) is deprecated.\n"),
1931 			 styled_string (title_style.style (),
1932 					tmp_cmd_str.c_str ()),
1933 			 styled_string (title_style.style (),
1934 					tmp_alias_str.c_str ()));
1935       else
1936 	printf_filtered (_("Warning: '%ps', an alias for the command '%ps', "
1937 			   "is deprecated.\n"),
1938 			 styled_string (title_style.style (),
1939 					tmp_alias_str.c_str ()),
1940 			 styled_string (title_style.style (),
1941 					tmp_cmd_str.c_str ()));
1942     }
1943   else
1944     printf_filtered (_("Warning: command '%ps' is deprecated.\n"),
1945 		     styled_string (title_style.style (),
1946 				    tmp_cmd_str.c_str ()));
1947 
1948   /* Now display a second line indicating what the user should use instead.
1949      If it is only the alias that is deprecated, we want to indicate the
1950      new alias, otherwise we'll indicate the new command.  */
1951   const char *replacement;
1952   if (alias != nullptr && !cmd->cmd_deprecated)
1953     replacement = alias->replacement;
1954   else
1955     replacement = cmd->replacement;
1956   if (replacement != nullptr)
1957     printf_filtered (_("Use '%ps'.\n\n"),
1958 		     styled_string (title_style.style (),
1959 				    replacement));
1960   else
1961     printf_filtered (_("No alternative known.\n\n"));
1962 
1963   /* We've warned you, now we'll keep quiet.  */
1964   if (alias != nullptr)
1965     alias->deprecated_warn_user = 0;
1966   cmd->deprecated_warn_user = 0;
1967 }
1968 
1969 /* Look up the contents of TEXT as a command in the command list CUR_LIST.
1970    Return 1 on success, 0 on failure.
1971 
1972    If TEXT refers to an alias, *ALIAS will point to that alias.
1973 
1974    If TEXT is a subcommand (i.e. one that is preceded by a prefix
1975    command) set *PREFIX_CMD.
1976 
1977    Set *CMD to point to the command TEXT indicates.
1978 
1979    If any of *ALIAS, *PREFIX_CMD, or *CMD cannot be determined or do not
1980    exist, they are NULL when we return.
1981 
1982 */
1983 
1984 static int
lookup_cmd_composition_1(const char * text,struct cmd_list_element ** alias,struct cmd_list_element ** prefix_cmd,struct cmd_list_element ** cmd,struct cmd_list_element * cur_list)1985 lookup_cmd_composition_1 (const char *text,
1986 			  struct cmd_list_element **alias,
1987 			  struct cmd_list_element **prefix_cmd,
1988 			  struct cmd_list_element **cmd,
1989 			  struct cmd_list_element *cur_list)
1990 {
1991   *alias = nullptr;
1992   *prefix_cmd = cur_list->prefix;
1993   *cmd = nullptr;
1994 
1995   text = skip_spaces (text);
1996 
1997   /* Go through as many command lists as we need to, to find the command
1998      TEXT refers to.  */
1999   while (1)
2000     {
2001       /* Identify the name of the command.  */
2002       int len = find_command_name_length (text);
2003 
2004       /* If nothing but whitespace, return.  */
2005       if (len == 0)
2006 	return 0;
2007 
2008       /* TEXT is the start of the first command word to lookup (and
2009 	 it's length is LEN).  We copy this into a local temporary.  */
2010       std::string command (text, len);
2011 
2012       /* Look it up.  */
2013       int nfound = 0;
2014       *cmd = find_cmd (command.c_str (), len, cur_list, 1, &nfound);
2015 
2016       /* We only handle the case where a single command was found.  */
2017       if (*cmd == CMD_LIST_AMBIGUOUS || *cmd == nullptr)
2018 	return 0;
2019       else
2020 	{
2021 	  if ((*cmd)->is_alias ())
2022 	    {
2023 	      /* If the command was actually an alias, we note that an
2024 		 alias was used (by assigning *ALIAS) and we set *CMD.  */
2025 	      *alias = *cmd;
2026 	      *cmd = (*cmd)->alias_target;
2027 	    }
2028 	}
2029 
2030       text += len;
2031       text = skip_spaces (text);
2032 
2033       if ((*cmd)->is_prefix () && *text != '\0')
2034 	{
2035 	  cur_list = *(*cmd)->subcommands;
2036 	  *prefix_cmd = *cmd;
2037 	}
2038       else
2039 	return 1;
2040     }
2041 }
2042 
2043 /* Look up the contents of TEXT as a command in the command list 'cmdlist'.
2044    Return 1 on success, 0 on failure.
2045 
2046    If TEXT refers to an alias, *ALIAS will point to that alias.
2047 
2048    If TEXT is a subcommand (i.e. one that is preceded by a prefix
2049    command) set *PREFIX_CMD.
2050 
2051    Set *CMD to point to the command TEXT indicates.
2052 
2053    If any of *ALIAS, *PREFIX_CMD, or *CMD cannot be determined or do not
2054    exist, they are NULL when we return.
2055 
2056 */
2057 
2058 int
lookup_cmd_composition(const char * text,struct cmd_list_element ** alias,struct cmd_list_element ** prefix_cmd,struct cmd_list_element ** cmd)2059 lookup_cmd_composition (const char *text,
2060 			struct cmd_list_element **alias,
2061 			struct cmd_list_element **prefix_cmd,
2062 			struct cmd_list_element **cmd)
2063 {
2064   return lookup_cmd_composition_1 (text, alias, prefix_cmd, cmd, cmdlist);
2065 }
2066 
2067 /* Helper function for SYMBOL_COMPLETION_FUNCTION.  */
2068 
2069 /* Return a vector of char pointers which point to the different
2070    possible completions in LIST of TEXT.
2071 
2072    WORD points in the same buffer as TEXT, and completions should be
2073    returned relative to this position.  For example, suppose TEXT is
2074    "foo" and we want to complete to "foobar".  If WORD is "oo", return
2075    "oobar"; if WORD is "baz/foo", return "baz/foobar".  */
2076 
2077 void
complete_on_cmdlist(struct cmd_list_element * list,completion_tracker & tracker,const char * text,const char * word,int ignore_help_classes)2078 complete_on_cmdlist (struct cmd_list_element *list,
2079 		     completion_tracker &tracker,
2080 		     const char *text, const char *word,
2081 		     int ignore_help_classes)
2082 {
2083   struct cmd_list_element *ptr;
2084   int textlen = strlen (text);
2085   int pass;
2086   int saw_deprecated_match = 0;
2087 
2088   /* We do one or two passes.  In the first pass, we skip deprecated
2089      commands.  If we see no matching commands in the first pass, and
2090      if we did happen to see a matching deprecated command, we do
2091      another loop to collect those.  */
2092   for (pass = 0; pass < 2; ++pass)
2093     {
2094       bool got_matches = false;
2095 
2096       for (ptr = list; ptr; ptr = ptr->next)
2097 	if (!strncmp (ptr->name, text, textlen)
2098 	    && !ptr->abbrev_flag
2099 	    && (!ignore_help_classes || !ptr->is_command_class_help ()
2100 		|| ptr->is_prefix ()))
2101 	  {
2102 	    if (pass == 0)
2103 	      {
2104 		if (ptr->cmd_deprecated)
2105 		  {
2106 		    saw_deprecated_match = 1;
2107 		    continue;
2108 		  }
2109 	      }
2110 
2111 	    tracker.add_completion
2112 	      (make_completion_match_str (ptr->name, text, word));
2113 	    got_matches = true;
2114 	  }
2115 
2116       if (got_matches)
2117 	break;
2118 
2119       /* If we saw no matching deprecated commands in the first pass,
2120 	 just bail out.  */
2121       if (!saw_deprecated_match)
2122 	break;
2123     }
2124 }
2125 
2126 /* Helper function for SYMBOL_COMPLETION_FUNCTION.  */
2127 
2128 /* Add the different possible completions in ENUMLIST of TEXT.
2129 
2130    WORD points in the same buffer as TEXT, and completions should be
2131    returned relative to this position.  For example, suppose TEXT is "foo"
2132    and we want to complete to "foobar".  If WORD is "oo", return
2133    "oobar"; if WORD is "baz/foo", return "baz/foobar".  */
2134 
2135 void
complete_on_enum(completion_tracker & tracker,const char * const * enumlist,const char * text,const char * word)2136 complete_on_enum (completion_tracker &tracker,
2137 		  const char *const *enumlist,
2138 		  const char *text, const char *word)
2139 {
2140   int textlen = strlen (text);
2141   int i;
2142   const char *name;
2143 
2144   for (i = 0; (name = enumlist[i]) != NULL; i++)
2145     if (strncmp (name, text, textlen) == 0)
2146       tracker.add_completion (make_completion_match_str (name, text, word));
2147 }
2148 
2149 /* Call the command function.  */
2150 void
cmd_func(struct cmd_list_element * cmd,const char * args,int from_tty)2151 cmd_func (struct cmd_list_element *cmd, const char *args, int from_tty)
2152 {
2153   if (!cmd->is_command_class_help ())
2154     {
2155       gdb::optional<scoped_restore_tmpl<int>> restore_suppress;
2156 
2157       if (cmd->suppress_notification != NULL)
2158 	restore_suppress.emplace (cmd->suppress_notification, 1);
2159 
2160       (*cmd->func) (cmd, args, from_tty);
2161     }
2162   else
2163     error (_("Invalid command"));
2164 }
2165 
2166 int
cli_user_command_p(struct cmd_list_element * cmd)2167 cli_user_command_p (struct cmd_list_element *cmd)
2168 {
2169   return (cmd->theclass == class_user
2170 	  && (cmd->func == do_const_cfunc || cmd->func == do_sfunc));
2171 }
2172