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