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