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