]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/cli/cli-decode.c
gdb: add context getter/setter to cmd_list_element
[thirdparty/binutils-gdb.git] / gdb / cli / cli-decode.c
CommitLineData
c906108c 1/* Handle lists of commands, their decoding and documentation, for GDB.
8926118c 2
3666a048 3 Copyright (C) 1986-2021 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,
7170dadf
TT
574 struct cmd_list_element **show_list,
575 void *context)
1b295c3d 576{
af7f8f52
SM
577 set_show_commands commands
578 = add_setshow_cmd_full (name, theclass, var_enum, var,
579 set_doc, show_doc, help_doc,
580 set_func, show_func,
581 set_list, show_list);
582 commands.set->enums = enumlist;
cdb27c12 583
0f8e2034
SM
584 commands.set->set_context (context);
585 commands.show->set_context (context);
7170dadf 586
af7f8f52 587 return commands;
1b295c3d
AC
588}
589
9d0faba9 590/* See cli-decode.h. */
5b9afe8a
YQ
591const char * const auto_boolean_enums[] = { "on", "off", "auto", NULL };
592
e9e68a56
AC
593/* Add an auto-boolean command named NAME to both the set and show
594 command list lists. CLASS is as in add_cmd. VAR is address of the
595 variable which will contain the value. DOC is the documentation
596 string. FUNC is the corresponding callback. */
af7f8f52
SM
597
598set_show_commands
6f937416 599add_setshow_auto_boolean_cmd (const char *name,
fe978cb0 600 enum command_class theclass,
e9e68a56 601 enum auto_boolean *var,
3b64bf98 602 const char *set_doc, const char *show_doc,
335cca0d 603 const char *help_doc,
eb4c3f4a 604 cmd_const_sfunc_ftype *set_func,
08546159 605 show_value_ftype *show_func,
e9e68a56
AC
606 struct cmd_list_element **set_list,
607 struct cmd_list_element **show_list)
97c3646f 608{
af7f8f52
SM
609 set_show_commands commands
610 = add_setshow_cmd_full (name, theclass, var_auto_boolean, var,
611 set_doc, show_doc, help_doc,
612 set_func, show_func,
613 set_list, show_list);
cdb27c12 614
af7f8f52
SM
615 commands.set->enums = auto_boolean_enums;
616
617 return commands;
97c3646f
AC
618}
619
9d0faba9
PA
620/* See cli-decode.h. */
621const char * const boolean_enums[] = { "on", "off", NULL };
622
e707bbc2
AC
623/* Add element named NAME to both the set and show command LISTs (the
624 list for set/show or some sublist thereof). CLASS is as in
625 add_cmd. VAR is address of the variable which will contain the
2daf894e
PA
626 value. SET_DOC and SHOW_DOC are the documentation strings.
627 Returns the new command element. */
628
af7f8f52 629set_show_commands
491144b5 630add_setshow_boolean_cmd (const char *name, enum command_class theclass, bool *var,
3b64bf98 631 const char *set_doc, const char *show_doc,
335cca0d 632 const char *help_doc,
eb4c3f4a 633 cmd_const_sfunc_ftype *set_func,
08546159 634 show_value_ftype *show_func,
e707bbc2
AC
635 struct cmd_list_element **set_list,
636 struct cmd_list_element **show_list)
f3796e26 637{
af7f8f52
SM
638 set_show_commands commands
639 = add_setshow_cmd_full (name, theclass, var_boolean, var,
640 set_doc, show_doc, help_doc,
641 set_func, show_func,
642 set_list, show_list);
cdb27c12 643
af7f8f52 644 commands.set->enums = boolean_enums;
2daf894e 645
af7f8f52 646 return commands;
f3796e26
AC
647}
648
b3f42336
AC
649/* Add element named NAME to both the set and show command LISTs (the
650 list for set/show or some sublist thereof). */
af7f8f52
SM
651
652set_show_commands
fe978cb0 653add_setshow_filename_cmd (const char *name, enum command_class theclass,
b3f42336
AC
654 char **var,
655 const char *set_doc, const char *show_doc,
335cca0d 656 const char *help_doc,
eb4c3f4a 657 cmd_const_sfunc_ftype *set_func,
08546159 658 show_value_ftype *show_func,
b3f42336
AC
659 struct cmd_list_element **set_list,
660 struct cmd_list_element **show_list)
661{
af7f8f52
SM
662 set_show_commands commands
663 = add_setshow_cmd_full (name, theclass, var_filename, var,
664 set_doc, show_doc, help_doc,
665 set_func, show_func,
666 set_list, show_list);
cdb27c12 667
af7f8f52
SM
668 set_cmd_completer (commands.set, filename_completer);
669
670 return commands;
b3f42336
AC
671}
672
673/* Add element named NAME to both the set and show command LISTs (the
5efd5804 674 list for set/show or some sublist thereof). */
af7f8f52
SM
675
676set_show_commands
fe978cb0 677add_setshow_string_cmd (const char *name, enum command_class theclass,
6df3bc68
MS
678 char **var,
679 const char *set_doc, const char *show_doc,
680 const char *help_doc,
eb4c3f4a 681 cmd_const_sfunc_ftype *set_func,
6df3bc68
MS
682 show_value_ftype *show_func,
683 struct cmd_list_element **set_list,
684 struct cmd_list_element **show_list)
b3f42336 685{
af7f8f52
SM
686 set_show_commands commands
687 = add_setshow_cmd_full (name, theclass, var_string, var,
688 set_doc, show_doc, help_doc,
689 set_func, show_func,
690 set_list, show_list);
597bf39d
PA
691
692 /* Disable the default symbol completer. */
af7f8f52
SM
693 set_cmd_completer (commands.set, nullptr);
694
695 return commands;
b3f42336
AC
696}
697
26c41df3 698/* Add element named NAME to both the set and show command LISTs (the
5efd5804 699 list for set/show or some sublist thereof). */
af7f8f52
SM
700
701set_show_commands
fe978cb0 702add_setshow_string_noescape_cmd (const char *name, enum command_class theclass,
26c41df3
AC
703 char **var,
704 const char *set_doc, const char *show_doc,
705 const char *help_doc,
eb4c3f4a 706 cmd_const_sfunc_ftype *set_func,
26c41df3
AC
707 show_value_ftype *show_func,
708 struct cmd_list_element **set_list,
709 struct cmd_list_element **show_list)
710{
af7f8f52
SM
711 set_show_commands commands
712 = add_setshow_cmd_full (name, theclass, var_string_noescape, var,
713 set_doc, show_doc, help_doc,
714 set_func, show_func,
715 set_list, show_list);
597bf39d
PA
716
717 /* Disable the default symbol completer. */
af7f8f52 718 set_cmd_completer (commands.set, nullptr);
597bf39d 719
af7f8f52 720 return commands;
26c41df3
AC
721}
722
b4b4ac0b
AC
723/* Add element named NAME to both the set and show command LISTs (the
724 list for set/show or some sublist thereof). */
af7f8f52
SM
725
726set_show_commands
fe978cb0 727add_setshow_optional_filename_cmd (const char *name, enum command_class theclass,
b4b4ac0b
AC
728 char **var,
729 const char *set_doc, const char *show_doc,
730 const char *help_doc,
eb4c3f4a 731 cmd_const_sfunc_ftype *set_func,
b4b4ac0b
AC
732 show_value_ftype *show_func,
733 struct cmd_list_element **set_list,
734 struct cmd_list_element **show_list)
735{
af7f8f52
SM
736 set_show_commands commands
737 = add_setshow_cmd_full (name, theclass, var_optional_filename, var,
738 set_doc, show_doc, help_doc,
739 set_func, show_func,
740 set_list, show_list);
7f6a6314 741
af7f8f52 742 set_cmd_completer (commands.set, filename_completer);
7f6a6314 743
af7f8f52 744 return commands;
b4b4ac0b
AC
745}
746
f81d1120
PA
747/* Completes on literal "unlimited". Used by integer commands that
748 support a special "unlimited" value. */
749
eb3ff9a5 750static void
f81d1120 751integer_unlimited_completer (struct cmd_list_element *ignore,
eb3ff9a5 752 completion_tracker &tracker,
f81d1120
PA
753 const char *text, const char *word)
754{
755 static const char * const keywords[] =
756 {
757 "unlimited",
758 NULL,
759 };
760
eb3ff9a5 761 complete_on_enum (tracker, keywords, text, word);
f81d1120
PA
762}
763
c0d88b1b
AC
764/* Add element named NAME to both the set and show command LISTs (the
765 list for set/show or some sublist thereof). CLASS is as in
766 add_cmd. VAR is address of the variable which will contain the
6fc1c773
YQ
767 value. SET_DOC and SHOW_DOC are the documentation strings. This
768 function is only used in Python API. Please don't use it elsewhere. */
af7f8f52
SM
769
770set_show_commands
fe978cb0 771add_setshow_integer_cmd (const char *name, enum command_class theclass,
47b667de 772 int *var,
6df3bc68
MS
773 const char *set_doc, const char *show_doc,
774 const char *help_doc,
eb4c3f4a 775 cmd_const_sfunc_ftype *set_func,
6df3bc68
MS
776 show_value_ftype *show_func,
777 struct cmd_list_element **set_list,
778 struct cmd_list_element **show_list)
c0d88b1b 779{
af7f8f52
SM
780 set_show_commands commands
781 = add_setshow_cmd_full (name, theclass, var_integer, var,
782 set_doc, show_doc, help_doc,
783 set_func, show_func,
784 set_list, show_list);
f81d1120 785
af7f8f52 786 set_cmd_completer (commands.set, integer_unlimited_completer);
f81d1120 787
af7f8f52 788 return commands;
c0d88b1b
AC
789}
790
25d29d70
AC
791/* Add element named NAME to both the set and show command LISTs (the
792 list for set/show or some sublist thereof). CLASS is as in
793 add_cmd. VAR is address of the variable which will contain the
5efd5804 794 value. SET_DOC and SHOW_DOC are the documentation strings. */
af7f8f52
SM
795
796set_show_commands
fe978cb0 797add_setshow_uinteger_cmd (const char *name, enum command_class theclass,
3b64bf98
AC
798 unsigned int *var,
799 const char *set_doc, const char *show_doc,
335cca0d 800 const char *help_doc,
eb4c3f4a 801 cmd_const_sfunc_ftype *set_func,
08546159 802 show_value_ftype *show_func,
25d29d70
AC
803 struct cmd_list_element **set_list,
804 struct cmd_list_element **show_list)
805{
af7f8f52
SM
806 set_show_commands commands
807 = add_setshow_cmd_full (name, theclass, var_uinteger, var,
808 set_doc, show_doc, help_doc,
809 set_func, show_func,
810 set_list, show_list);
f81d1120 811
af7f8f52 812 set_cmd_completer (commands.set, integer_unlimited_completer);
f81d1120 813
af7f8f52 814 return commands;
25d29d70
AC
815}
816
15170568
AC
817/* Add element named NAME to both the set and show command LISTs (the
818 list for set/show or some sublist thereof). CLASS is as in
819 add_cmd. VAR is address of the variable which will contain the
5efd5804 820 value. SET_DOC and SHOW_DOC are the documentation strings. */
af7f8f52
SM
821
822set_show_commands
fe978cb0 823add_setshow_zinteger_cmd (const char *name, enum command_class theclass,
3b64bf98
AC
824 int *var,
825 const char *set_doc, const char *show_doc,
335cca0d 826 const char *help_doc,
eb4c3f4a 827 cmd_const_sfunc_ftype *set_func,
08546159 828 show_value_ftype *show_func,
15170568
AC
829 struct cmd_list_element **set_list,
830 struct cmd_list_element **show_list)
831{
af7f8f52
SM
832 return add_setshow_cmd_full (name, theclass, var_zinteger, var,
833 set_doc, show_doc, help_doc,
834 set_func, show_func,
835 set_list, show_list);
15170568
AC
836}
837
af7f8f52 838set_show_commands
6f937416 839add_setshow_zuinteger_unlimited_cmd (const char *name,
fe978cb0 840 enum command_class theclass,
b69b1fb1 841 int *var,
6fc1c773
YQ
842 const char *set_doc,
843 const char *show_doc,
844 const char *help_doc,
eb4c3f4a 845 cmd_const_sfunc_ftype *set_func,
6fc1c773
YQ
846 show_value_ftype *show_func,
847 struct cmd_list_element **set_list,
848 struct cmd_list_element **show_list)
849{
af7f8f52
SM
850 set_show_commands commands
851 = add_setshow_cmd_full (name, theclass, var_zuinteger_unlimited, var,
852 set_doc, show_doc, help_doc,
853 set_func, show_func,
854 set_list, show_list);
f81d1120 855
af7f8f52 856 set_cmd_completer (commands.set, integer_unlimited_completer);
f81d1120 857
af7f8f52 858 return commands;
6fc1c773
YQ
859}
860
1e8fb976
PA
861/* Add element named NAME to both the set and show command LISTs (the
862 list for set/show or some sublist thereof). CLASS is as in
863 add_cmd. VAR is address of the variable which will contain the
5efd5804 864 value. SET_DOC and SHOW_DOC are the documentation strings. */
af7f8f52
SM
865
866set_show_commands
fe978cb0 867add_setshow_zuinteger_cmd (const char *name, enum command_class theclass,
1e8fb976
PA
868 unsigned int *var,
869 const char *set_doc, const char *show_doc,
870 const char *help_doc,
eb4c3f4a 871 cmd_const_sfunc_ftype *set_func,
1e8fb976
PA
872 show_value_ftype *show_func,
873 struct cmd_list_element **set_list,
874 struct cmd_list_element **show_list)
875{
af7f8f52
SM
876 return add_setshow_cmd_full (name, theclass, var_zuinteger, var,
877 set_doc, show_doc, help_doc,
878 set_func, show_func,
879 set_list, show_list);
1e8fb976
PA
880}
881
b05dcbb7
TT
882/* Remove the command named NAME from the command list. Return the
883 list commands which were aliased to the deleted command. If the
fad6eecd
TT
884 command had no aliases, return NULL. The various *HOOKs are set to
885 the pre- and post-hook commands for the deleted command. If the
886 command does not have a hook, the corresponding out parameter is
887 set to NULL. */
c906108c 888
b05dcbb7 889static struct cmd_list_element *
6f937416 890delete_cmd (const char *name, struct cmd_list_element **list,
fad6eecd
TT
891 struct cmd_list_element **prehook,
892 struct cmd_list_element **prehookee,
893 struct cmd_list_element **posthook,
894 struct cmd_list_element **posthookee)
c906108c 895{
b05dcbb7
TT
896 struct cmd_list_element *iter;
897 struct cmd_list_element **previous_chain_ptr;
898 struct cmd_list_element *aliases = NULL;
c906108c 899
fad6eecd
TT
900 *prehook = NULL;
901 *prehookee = NULL;
902 *posthook = NULL;
903 *posthookee = NULL;
b05dcbb7
TT
904 previous_chain_ptr = list;
905
906 for (iter = *previous_chain_ptr; iter; iter = *previous_chain_ptr)
c906108c 907 {
b05dcbb7
TT
908 if (strcmp (iter->name, name) == 0)
909 {
d8906c6f 910 if (iter->destroyer)
0f8e2034
SM
911 iter->destroyer (iter, iter->context ());
912
b05dcbb7
TT
913 if (iter->hookee_pre)
914 iter->hookee_pre->hook_pre = 0;
fad6eecd
TT
915 *prehook = iter->hook_pre;
916 *prehookee = iter->hookee_pre;
b05dcbb7
TT
917 if (iter->hookee_post)
918 iter->hookee_post->hook_post = 0;
fad6eecd
TT
919 *posthook = iter->hook_post;
920 *posthookee = iter->hookee_post;
b05dcbb7
TT
921
922 /* Update the link. */
923 *previous_chain_ptr = iter->next;
924
925 aliases = iter->aliases;
926
927 /* If this command was an alias, remove it from the list of
928 aliases. */
1be99b11 929 if (iter->is_alias ())
b05dcbb7 930 {
99858724 931 struct cmd_list_element **prevp = &iter->alias_target->aliases;
b05dcbb7
TT
932 struct cmd_list_element *a = *prevp;
933
934 while (a != iter)
935 {
936 prevp = &a->alias_chain;
937 a = *prevp;
938 }
939 *prevp = iter->alias_chain;
940 }
941
e2fc72e2 942 delete iter;
b05dcbb7
TT
943
944 /* We won't see another command with the same name. */
945 break;
946 }
947 else
948 previous_chain_ptr = &iter->next;
c906108c
SS
949 }
950
b05dcbb7 951 return aliases;
c906108c 952}
d318976c 953\f
ebcd3b23 954/* Shorthands to the commands above. */
d318976c
FN
955
956/* Add an element to the list of info subcommands. */
957
958struct cmd_list_element *
1d12d88f 959add_info (const char *name, cmd_const_cfunc_ftype *fun, const char *doc)
d318976c 960{
f3575e08 961 return add_cmd (name, class_info, fun, doc, &infolist);
d318976c
FN
962}
963
964/* Add an alias to the list of info subcommands. */
965
e0f25bd9
SM
966cmd_list_element *
967add_info_alias (const char *name, cmd_list_element *target, int abbrev_flag)
d318976c 968{
e0f25bd9 969 return add_alias_cmd (name, target, class_run, abbrev_flag, &infolist);
d318976c
FN
970}
971
972/* Add an element to the list of commands. */
973
974struct cmd_list_element *
0b39b52e
TT
975add_com (const char *name, enum command_class theclass,
976 cmd_const_cfunc_ftype *fun,
1947513d 977 const char *doc)
d318976c 978{
fe978cb0 979 return add_cmd (name, theclass, fun, doc, &cmdlist);
d318976c
FN
980}
981
57b4f16e
PW
982/* Add an alias or abbreviation command to the list of commands.
983 For aliases predefined by GDB (such as bt), THECLASS must be
984 different of class_alias, as class_alias is used to identify
985 user defined aliases. */
d318976c 986
3947f654
SM
987cmd_list_element *
988add_com_alias (const char *name, cmd_list_element *target,
99858724 989 command_class theclass, int abbrev_flag)
d318976c 990{
3947f654 991 return add_alias_cmd (name, target, theclass, abbrev_flag, &cmdlist);
d318976c 992}
4034d0ff
AT
993
994/* Add an element with a suppress notification to the list of commands. */
995
996struct cmd_list_element *
997add_com_suppress_notification (const char *name, enum command_class theclass,
1ee870c5 998 cmd_const_cfunc_ftype *fun, const char *doc,
4034d0ff
AT
999 int *suppress_notification)
1000{
f67ffa6a
AB
1001 return add_cmd_suppress_notification (name, theclass, fun, doc,
1002 &cmdlist, suppress_notification);
4034d0ff
AT
1003}
1004
3b3aaacb
PW
1005/* Print the prefix of C followed by name of C in title style. */
1006
1007static void
1008fput_command_name_styled (struct cmd_list_element *c, struct ui_file *stream)
1009{
2f822da5
MB
1010 std::string prefixname
1011 = c->prefix == nullptr ? "" : c->prefix->prefixname ();
3b3aaacb 1012
2f822da5
MB
1013 fprintf_styled (stream, title_style.style (), "%s%s",
1014 prefixname.c_str (), c->name);
3b3aaacb
PW
1015}
1016
cf00cd6f
PW
1017/* Print the definition of alias C using title style for alias
1018 and aliased command. */
1019
1020static void
1021fput_alias_definition_styled (struct cmd_list_element *c,
1022 struct ui_file *stream)
1023{
1be99b11 1024 gdb_assert (c->is_alias ());
cf00cd6f
PW
1025 fputs_filtered (" alias ", stream);
1026 fput_command_name_styled (c, stream);
1027 fprintf_filtered (stream, " = ");
99858724 1028 fput_command_name_styled (c->alias_target, stream);
cf00cd6f
PW
1029 fprintf_filtered (stream, " %s\n", c->default_args.c_str ());
1030}
1031
1032/* Print the definition of the aliases of CMD that have default args. */
1033
1034static void
1035fput_aliases_definition_styled (struct cmd_list_element *cmd,
1036 struct ui_file *stream)
1037{
1038 if (cmd->aliases != nullptr)
1039 {
1040 for (cmd_list_element *iter = cmd->aliases;
1041 iter;
1042 iter = iter->alias_chain)
1043 {
1044 if (!iter->default_args.empty ())
1045 fput_alias_definition_styled (iter, stream);
1046 }
1047 }
1048}
1049
1050
3b3aaacb
PW
1051/* If C has one or more aliases, style print the name of C and
1052 the name of its aliases, separated by commas.
1053 If ALWAYS_FPUT_C_NAME, print the name of C even if it has no aliases.
1054 If one or more names are printed, POSTFIX is printed after the last name.
1055*/
1056
1057static void
1058fput_command_names_styled (struct cmd_list_element *c,
1059 bool always_fput_c_name, const char *postfix,
1060 struct ui_file *stream)
1061{
1062 if (always_fput_c_name || c->aliases != nullptr)
1063 fput_command_name_styled (c, stream);
1064 if (c->aliases != nullptr)
1065 {
1066 for (cmd_list_element *iter = c->aliases; iter; iter = iter->alias_chain)
1067 {
1068 fputs_filtered (", ", stream);
1069 wrap_here (" ");
1070 fput_command_name_styled (iter, stream);
1071 }
1072 }
1073 if (always_fput_c_name || c->aliases != nullptr)
1074 fputs_filtered (postfix, stream);
1075}
1076
66d8c862
PW
1077/* If VERBOSE, print the full help for command C and highlight the
1078 documentation parts matching HIGHLIGHT,
1079 otherwise print only one-line help for command C. */
1080
1081static void
1082print_doc_of_command (struct cmd_list_element *c, const char *prefix,
1083 bool verbose, compiled_regex &highlight,
1084 struct ui_file *stream)
1085{
1086 /* When printing the full documentation, add a line to separate
1087 this documentation from the previous command help, in the likely
1088 case that apropos finds several commands. */
1089 if (verbose)
1090 fputs_filtered ("\n", stream);
1091
cf00cd6f
PW
1092 fput_command_names_styled (c, true,
1093 verbose ? "" : " -- ", stream);
66d8c862 1094 if (verbose)
cf00cd6f
PW
1095 {
1096 fputs_filtered ("\n", stream);
1097 fput_aliases_definition_styled (c, stream);
1098 fputs_highlighted (c->doc, highlight, stream);
1099 fputs_filtered ("\n", stream);
1100 }
66d8c862 1101 else
cf00cd6f
PW
1102 {
1103 print_doc_line (stream, c->doc, false);
1104 fputs_filtered ("\n", stream);
1105 fput_aliases_definition_styled (c, stream);
1106 }
66d8c862
PW
1107}
1108
6837a0a2
DB
1109/* Recursively walk the commandlist structures, and print out the
1110 documentation of commands that match our regex in either their
1111 name, or their documentation.
66d8c862
PW
1112 If VERBOSE, prints the complete documentation and highlight the
1113 documentation parts matching REGEX, otherwise prints only
1114 the first line.
6837a0a2 1115*/
66d8c862
PW
1116void
1117apropos_cmd (struct ui_file *stream,
ebcd3b23 1118 struct cmd_list_element *commandlist,
66d8c862 1119 bool verbose, compiled_regex &regex, const char *prefix)
6837a0a2 1120{
d5b5ac79 1121 struct cmd_list_element *c;
4a98be19 1122 int returnvalue;
cdb27c12 1123
ebcd3b23 1124 /* Walk through the commands. */
6837a0a2
DB
1125 for (c=commandlist;c;c=c->next)
1126 {
1be99b11 1127 if (c->is_alias ())
7c05caf7
PW
1128 {
1129 /* Command aliases/abbreviations are skipped to ensure we print the
1130 doc of a command only once, when encountering the aliased
1131 command. */
1132 continue;
1133 }
1134
ebcd3b23 1135 returnvalue = -1; /* Needed to avoid double printing. */
6837a0a2
DB
1136 if (c->name != NULL)
1137 {
2d7cc5c7
PA
1138 size_t name_len = strlen (c->name);
1139
ebcd3b23 1140 /* Try to match against the name. */
2d7cc5c7 1141 returnvalue = regex.search (c->name, name_len, 0, name_len, NULL);
6837a0a2 1142 if (returnvalue >= 0)
66d8c862 1143 print_doc_of_command (c, prefix, verbose, regex, stream);
7c05caf7
PW
1144
1145 /* Try to match against the name of the aliases. */
1146 for (cmd_list_element *iter = c->aliases;
1147 returnvalue < 0 && iter;
1148 iter = iter->alias_chain)
1149 {
1150 name_len = strlen (iter->name);
1151 returnvalue = regex.search (iter->name, name_len, 0, name_len, NULL);
1152 if (returnvalue >= 0)
1153 print_doc_of_command (c, prefix, verbose, regex, stream);
1154 }
6837a0a2 1155 }
4a98be19 1156 if (c->doc != NULL && returnvalue < 0)
6837a0a2 1157 {
2d7cc5c7
PA
1158 size_t doc_len = strlen (c->doc);
1159
ebcd3b23 1160 /* Try to match against documentation. */
2d7cc5c7 1161 if (regex.search (c->doc, doc_len, 0, doc_len, NULL) >= 0)
66d8c862 1162 print_doc_of_command (c, prefix, verbose, regex, stream);
6837a0a2 1163 }
7c05caf7 1164 /* Check if this command has subcommands. */
3d0b3564 1165 if (c->is_prefix ())
6837a0a2
DB
1166 {
1167 /* Recursively call ourselves on the subcommand list,
ebcd3b23 1168 passing the right prefix in. */
14b42fc4 1169 apropos_cmd (stream, *c->subcommands, verbose, regex,
2f822da5 1170 c->prefixname ().c_str ());
6837a0a2
DB
1171 }
1172 }
1173}
c906108c
SS
1174
1175/* This command really has to deal with two things:
ebcd3b23
MS
1176 1) I want documentation on *this string* (usually called by
1177 "help commandname").
1178
1179 2) I want documentation on *this list* (usually called by giving a
1180 command that requires subcommands. Also called by saying just
1181 "help".)
1182
30baf67b 1183 I am going to split this into two separate commands, help_cmd and
ebcd3b23 1184 help_list. */
c906108c
SS
1185
1186void
64669f3b 1187help_cmd (const char *command, struct ui_file *stream)
c906108c 1188{
7c05caf7 1189 struct cmd_list_element *c, *alias, *prefix_cmd, *c_cmd;
c906108c
SS
1190
1191 if (!command)
1192 {
1193 help_list (cmdlist, "", all_classes, stream);
1194 return;
1195 }
1196
49a5a3a3
GM
1197 if (strcmp (command, "all") == 0)
1198 {
1199 help_all (stream);
1200 return;
1201 }
1202
7c05caf7 1203 const char *orig_command = command;
cf00cd6f 1204 c = lookup_cmd (&command, cmdlist, "", NULL, 0, 0);
c906108c
SS
1205
1206 if (c == 0)
1207 return;
1208
7c05caf7
PW
1209 lookup_cmd_composition (orig_command, &alias, &prefix_cmd, &c_cmd);
1210
c906108c 1211 /* There are three cases here.
14b42fc4 1212 If c->subcommands is nonzero, we have a prefix command.
c906108c 1213 Print its documentation, then list its subcommands.
c5aa993b 1214
9f60d481
AC
1215 If c->func is non NULL, we really have a command. Print its
1216 documentation and return.
c5aa993b 1217
9f60d481
AC
1218 If c->func is NULL, we have a class name. Print its
1219 documentation (as if it were a command) and then set class to the
1220 number of this class so that the commands in the class will be
1221 listed. */
c906108c 1222
7c05caf7
PW
1223 /* If the user asked 'help somecommand' and there is no alias,
1224 the false indicates to not output the (single) command name. */
1225 fput_command_names_styled (c, false, "\n", stream);
cf00cd6f 1226 fput_aliases_definition_styled (c, stream);
c906108c
SS
1227 fputs_filtered (c->doc, stream);
1228 fputs_filtered ("\n", stream);
1229
034dce7a 1230 if (!c->is_prefix () && !c->is_command_class_help ())
c906108c 1231 return;
3d0b3564 1232
c906108c
SS
1233 fprintf_filtered (stream, "\n");
1234
ebcd3b23 1235 /* If this is a prefix command, print it's subcommands. */
3d0b3564 1236 if (c->is_prefix ())
14b42fc4 1237 help_list (*c->subcommands, c->prefixname ().c_str (),
2f822da5 1238 all_commands, stream);
c906108c 1239
ebcd3b23 1240 /* If this is a class name, print all of the commands in the class. */
034dce7a 1241 if (c->is_command_class_help ())
fe978cb0 1242 help_list (cmdlist, "", c->theclass, stream);
c906108c 1243
73bc900d
FN
1244 if (c->hook_pre || c->hook_post)
1245 fprintf_filtered (stream,
dda83cd7 1246 "\nThis command has a hook (or hooks) defined:\n");
73bc900d
FN
1247
1248 if (c->hook_pre)
327529e9 1249 fprintf_filtered (stream,
dda83cd7
SM
1250 "\tThis command is run after : %s (pre hook)\n",
1251 c->hook_pre->name);
73bc900d 1252 if (c->hook_post)
327529e9 1253 fprintf_filtered (stream,
dda83cd7
SM
1254 "\tThis command is run before : %s (post hook)\n",
1255 c->hook_post->name);
c906108c
SS
1256}
1257
1258/*
1259 * Get a specific kind of help on a command list.
1260 *
1261 * LIST is the list.
1262 * CMDTYPE is the prefix to use in the title string.
1263 * CLASS is the class with which to list the nodes of this list (see
1264 * documentation for help_cmd_list below), As usual, ALL_COMMANDS for
1265 * everything, ALL_CLASSES for just classes, and non-negative for only things
1266 * in a specific class.
1267 * and STREAM is the output stream on which to print things.
1268 * If you call this routine with a class >= 0, it recurses.
1269 */
1270void
64e61d29 1271help_list (struct cmd_list_element *list, const char *cmdtype,
fe978cb0 1272 enum command_class theclass, struct ui_file *stream)
c906108c
SS
1273{
1274 int len;
1275 char *cmdtype1, *cmdtype2;
c5aa993b 1276
ebcd3b23
MS
1277 /* If CMDTYPE is "foo ", CMDTYPE1 gets " foo" and CMDTYPE2 gets "foo sub".
1278 */
c906108c
SS
1279 len = strlen (cmdtype);
1280 cmdtype1 = (char *) alloca (len + 1);
1281 cmdtype1[0] = 0;
1282 cmdtype2 = (char *) alloca (len + 4);
1283 cmdtype2[0] = 0;
1284 if (len)
1285 {
1286 cmdtype1[0] = ' ';
29f9a567 1287 memcpy (cmdtype1 + 1, cmdtype, len - 1);
c906108c 1288 cmdtype1[len] = 0;
29f9a567 1289 memcpy (cmdtype2, cmdtype, len - 1);
c906108c
SS
1290 strcpy (cmdtype2 + len - 1, " sub");
1291 }
1292
fe978cb0 1293 if (theclass == all_classes)
c906108c
SS
1294 fprintf_filtered (stream, "List of classes of %scommands:\n\n", cmdtype2);
1295 else
1296 fprintf_filtered (stream, "List of %scommands:\n\n", cmdtype2);
1297
3b3aaacb 1298 help_cmd_list (list, theclass, theclass >= 0, stream);
c906108c 1299
fe978cb0 1300 if (theclass == all_classes)
de74f71f
MS
1301 {
1302 fprintf_filtered (stream, "\n\
1303Type \"help%s\" followed by a class name for a list of commands in ",
1304 cmdtype1);
1305 wrap_here ("");
1306 fprintf_filtered (stream, "that class.");
6e381ba0
VP
1307
1308 fprintf_filtered (stream, "\n\
1309Type \"help all\" for the list of all commands.");
de74f71f 1310 }
c906108c 1311
de74f71f 1312 fprintf_filtered (stream, "\nType \"help%s\" followed by %scommand name ",
c5aa993b 1313 cmdtype1, cmdtype2);
de74f71f
MS
1314 wrap_here ("");
1315 fputs_filtered ("for ", stream);
1316 wrap_here ("");
1317 fputs_filtered ("full ", stream);
1318 wrap_here ("");
1319 fputs_filtered ("documentation.\n", stream);
6e381ba0 1320 fputs_filtered ("Type \"apropos word\" to search "
ebcd3b23 1321 "for commands related to \"word\".\n", stream);
66d8c862
PW
1322 fputs_filtered ("Type \"apropos -v word\" for full documentation", stream);
1323 wrap_here ("");
1324 fputs_filtered (" of commands related to \"word\".\n", stream);
de74f71f
MS
1325 fputs_filtered ("Command name abbreviations are allowed if unambiguous.\n",
1326 stream);
c906108c 1327}
c5aa993b 1328
49a5a3a3 1329static void
c85871a3 1330help_all (struct ui_file *stream)
49a5a3a3
GM
1331{
1332 struct cmd_list_element *c;
6e381ba0 1333 int seen_unclassified = 0;
49a5a3a3
GM
1334
1335 for (c = cmdlist; c; c = c->next)
1336 {
1337 if (c->abbrev_flag)
dda83cd7 1338 continue;
ebcd3b23
MS
1339 /* If this is a class name, print all of the commands in the
1340 class. */
6e381ba0 1341
034dce7a 1342 if (c->is_command_class_help ())
6e381ba0
VP
1343 {
1344 fprintf_filtered (stream, "\nCommand class: %s\n\n", c->name);
3b3aaacb 1345 help_cmd_list (cmdlist, c->theclass, true, stream);
6e381ba0 1346 }
49a5a3a3 1347 }
6e381ba0
VP
1348
1349 /* While it's expected that all commands are in some class,
1350 as a safety measure, we'll print commands outside of any
1351 class at the end. */
1352
1353 for (c = cmdlist; c; c = c->next)
1354 {
1355 if (c->abbrev_flag)
dda83cd7 1356 continue;
6e381ba0 1357
fe978cb0 1358 if (c->theclass == no_class)
6e381ba0
VP
1359 {
1360 if (!seen_unclassified)
1361 {
1362 fprintf_filtered (stream, "\nUnclassified commands\n\n");
1363 seen_unclassified = 1;
1364 }
cf00cd6f 1365 print_help_for_command (c, true, stream);
6e381ba0
VP
1366 }
1367 }
1368
49a5a3a3
GM
1369}
1370
590042fc
PW
1371/* See cli-decode.h. */
1372
d318976c 1373void
590042fc
PW
1374print_doc_line (struct ui_file *stream, const char *str,
1375 bool for_value_prefix)
c906108c
SS
1376{
1377 static char *line_buffer = 0;
1378 static int line_size;
1947513d 1379 const char *p;
c906108c
SS
1380
1381 if (!line_buffer)
1382 {
1383 line_size = 80;
1384 line_buffer = (char *) xmalloc (line_size);
1385 }
1386
590042fc 1387 /* Searches for the first end of line or the end of STR. */
c906108c 1388 p = str;
590042fc 1389 while (*p && *p != '\n')
c906108c
SS
1390 p++;
1391 if (p - str > line_size - 1)
1392 {
1393 line_size = p - str + 1;
b8c9b27d 1394 xfree (line_buffer);
c906108c
SS
1395 line_buffer = (char *) xmalloc (line_size);
1396 }
1397 strncpy (line_buffer, str, p - str);
590042fc
PW
1398 if (for_value_prefix)
1399 {
1400 if (islower (line_buffer[0]))
1401 line_buffer[0] = toupper (line_buffer[0]);
1402 gdb_assert (p > str);
1403 if (line_buffer[p - str - 1] == '.')
1404 line_buffer[p - str - 1] = '\0';
1405 else
1406 line_buffer[p - str] = '\0';
1407 }
1408 else
1409 line_buffer[p - str] = '\0';
b6201d44 1410 fputs_filtered (line_buffer, stream);
c906108c
SS
1411}
1412
6e381ba0
VP
1413/* Print one-line help for command C.
1414 If RECURSE is non-zero, also print one-line descriptions
ebcd3b23 1415 of all prefixed subcommands. */
6e381ba0 1416static void
3b3aaacb
PW
1417print_help_for_command (struct cmd_list_element *c,
1418 bool recurse, struct ui_file *stream)
6e381ba0 1419{
3b3aaacb 1420 fput_command_names_styled (c, true, " -- ", stream);
590042fc 1421 print_doc_line (stream, c->doc, false);
6e381ba0 1422 fputs_filtered ("\n", stream);
cf00cd6f
PW
1423 if (!c->default_args.empty ())
1424 fput_alias_definition_styled (c, stream);
1425 fput_aliases_definition_styled (c, stream);
66d8c862 1426
6e381ba0 1427 if (recurse
3d0b3564 1428 && c->is_prefix ()
6e381ba0
VP
1429 && c->abbrev_flag == 0)
1430 /* Subcommands of a prefix command typically have 'all_commands'
1431 as class. If we pass CLASS to recursive invocation,
ebcd3b23 1432 most often we won't see anything. */
14b42fc4 1433 help_cmd_list (*c->subcommands, all_commands, true, stream);
6e381ba0
VP
1434}
1435
c906108c
SS
1436/*
1437 * Implement a help command on command list LIST.
1438 * RECURSE should be non-zero if this should be done recursively on
1439 * all sublists of LIST.
c906108c 1440 * STREAM is the stream upon which the output should be written.
2aa08bd1 1441 * THECLASS should be:
c5aa993b 1442 * A non-negative class number to list only commands in that
c5aa993b
JM
1443 * ALL_COMMANDS to list all commands in list.
1444 * ALL_CLASSES to list all classes in list.
c906108c 1445 *
3b3aaacb
PW
1446 * Note that aliases are only shown when THECLASS is class_alias.
1447 * In the other cases, the aliases will be shown together with their
1448 * aliased command.
1449 *
c906108c
SS
1450 * Note that RECURSE will be active on *all* sublists, not just the
1451 * ones selected by the criteria above (ie. the selection mechanism
1452 * is at the low level, not the high-level).
1453 */
3b3aaacb
PW
1454
1455static void
fe978cb0 1456help_cmd_list (struct cmd_list_element *list, enum command_class theclass,
3b3aaacb 1457 bool recurse, struct ui_file *stream)
c906108c 1458{
d5b5ac79 1459 struct cmd_list_element *c;
c906108c
SS
1460
1461 for (c = list; c; c = c->next)
2aa08bd1 1462 {
3b3aaacb 1463 if (c->abbrev_flag == 1 || c->cmd_deprecated)
c906108c 1464 {
3b3aaacb
PW
1465 /* Do not show abbreviations or deprecated commands. */
1466 continue;
c906108c 1467 }
3b3aaacb 1468
1be99b11 1469 if (c->is_alias () && theclass != class_alias)
3b3aaacb
PW
1470 {
1471 /* Do not show an alias, unless specifically showing the
1472 list of aliases: for all other classes, an alias is
1473 shown (if needed) together with its aliased command. */
1474 continue;
1475 }
1476
1477 if (theclass == all_commands
034dce7a
SM
1478 || (theclass == all_classes && c->is_command_class_help ())
1479 || (theclass == c->theclass && !c->is_command_class_help ()))
3b3aaacb
PW
1480 {
1481 /* show C when
dda83cd7 1482 - showing all commands
3b3aaacb
PW
1483 - showing all classes and C is a help class
1484 - showing commands of THECLASS and C is not the help class */
1485
1486 /* If we show the class_alias and C is an alias, do not recurse,
1487 as this would show the (possibly very long) not very useful
1488 list of sub-commands of the aliased command. */
1489 print_help_for_command
1490 (c,
1be99b11 1491 recurse && (theclass != class_alias || !c->is_alias ()),
3b3aaacb
PW
1492 stream);
1493 continue;
1494 }
1495
1496 if (recurse
1497 && (theclass == class_user || theclass == class_alias)
3d0b3564 1498 && c->is_prefix ())
3b3aaacb
PW
1499 {
1500 /* User-defined commands or aliases may be subcommands. */
14b42fc4 1501 help_cmd_list (*c->subcommands, theclass, recurse, stream);
3b3aaacb
PW
1502 continue;
1503 }
1504
1505 /* Do not show C or recurse on C, e.g. because C does not belong to
1506 THECLASS or because C is a help class. */
c906108c
SS
1507 }
1508}
c906108c 1509\f
c5aa993b 1510
c906108c
SS
1511/* Search the input clist for 'command'. Return the command if
1512 found (or NULL if not), and return the number of commands
ebcd3b23 1513 found in nfound. */
c906108c
SS
1514
1515static struct cmd_list_element *
6f937416 1516find_cmd (const char *command, int len, struct cmd_list_element *clist,
fba45db2 1517 int ignore_help_classes, int *nfound)
c906108c
SS
1518{
1519 struct cmd_list_element *found, *c;
1520
b03e6ad9 1521 found = NULL;
c906108c
SS
1522 *nfound = 0;
1523 for (c = clist; c; c = c->next)
1524 if (!strncmp (command, c->name, len)
034dce7a 1525 && (!ignore_help_classes || !c->is_command_class_help ()))
c906108c 1526 {
c5aa993b
JM
1527 found = c;
1528 (*nfound)++;
1529 if (c->name[len] == '\0')
1530 {
1531 *nfound = 1;
1532 break;
1533 }
c906108c
SS
1534 }
1535 return found;
1536}
1537
604c4576
JG
1538/* Return the length of command name in TEXT. */
1539
1540int
3386243e
AS
1541find_command_name_length (const char *text)
1542{
1543 const char *p = text;
1544
1545 /* Treating underscores as part of command words is important
1546 so that "set args_foo()" doesn't get interpreted as
1547 "set args _foo()". */
ebcd3b23
MS
1548 /* Some characters are only used for TUI specific commands.
1549 However, they are always allowed for the sake of consistency.
1550
ebcd3b23
MS
1551 Note that this is larger than the character set allowed when
1552 creating user-defined commands. */
1553
947d3946 1554 /* Recognize the single character commands so that, e.g., "!ls"
ed59ded5 1555 works as expected. */
947d3946 1556 if (*p == '!' || *p == '|')
ed59ded5
DE
1557 return 1;
1558
be09caf1 1559 while (valid_cmd_char_p (*p)
3386243e 1560 /* Characters used by TUI specific commands. */
4f45d445 1561 || *p == '+' || *p == '<' || *p == '>' || *p == '$')
3386243e
AS
1562 p++;
1563
1564 return p - text;
1565}
1566
be09caf1
PW
1567/* See command.h. */
1568
1569bool
1570valid_cmd_char_p (int c)
1571{
1572 /* Alas "42" is a legitimate user-defined command.
1573 In the interests of not breaking anything we preserve that. */
1574
1575 return isalnum (c) || c == '-' || c == '_' || c == '.';
1576}
1577
1578/* See command.h. */
5a56e9c5 1579
7f008c9e 1580bool
5a56e9c5
DE
1581valid_user_defined_cmd_name_p (const char *name)
1582{
1583 const char *p;
1584
1585 if (*name == '\0')
7f008c9e 1586 return false;
5a56e9c5 1587
5a56e9c5
DE
1588 for (p = name; *p != '\0'; ++p)
1589 {
be09caf1 1590 if (valid_cmd_char_p (*p))
5a56e9c5
DE
1591 ; /* Ok. */
1592 else
7f008c9e 1593 return false;
5a56e9c5
DE
1594 }
1595
7f008c9e 1596 return true;
5a56e9c5
DE
1597}
1598
1536146f 1599/* See command.h. */
c906108c
SS
1600
1601struct cmd_list_element *
6f937416 1602lookup_cmd_1 (const char **text, struct cmd_list_element *clist,
cf00cd6f 1603 struct cmd_list_element **result_list, std::string *default_args,
1536146f 1604 int ignore_help_classes, bool lookup_for_completion_p)
c906108c 1605{
3386243e 1606 char *command;
798a7429 1607 int len, nfound;
c906108c 1608 struct cmd_list_element *found, *c;
cf00cd6f 1609 bool found_alias = false;
6f937416 1610 const char *line = *text;
c906108c
SS
1611
1612 while (**text == ' ' || **text == '\t')
1613 (*text)++;
1614
3386243e
AS
1615 /* Identify the name of the command. */
1616 len = find_command_name_length (*text);
c906108c
SS
1617
1618 /* If nothing but whitespace, return 0. */
3386243e 1619 if (len == 0)
c906108c 1620 return 0;
c5aa993b 1621
c906108c 1622 /* *text and p now bracket the first command word to lookup (and
ebcd3b23 1623 it's length is len). We copy this into a local temporary. */
c906108c
SS
1624
1625
1626 command = (char *) alloca (len + 1);
22ad7fee 1627 memcpy (command, *text, len);
c906108c
SS
1628 command[len] = '\0';
1629
1630 /* Look it up. */
1631 found = 0;
1632 nfound = 0;
c5aa993b 1633 found = find_cmd (command, len, clist, ignore_help_classes, &nfound);
c906108c 1634
c906108c
SS
1635 /* If nothing matches, we have a simple failure. */
1636 if (nfound == 0)
1637 return 0;
1638
1639 if (nfound > 1)
1640 {
cf00cd6f 1641 if (result_list != nullptr)
c906108c
SS
1642 /* Will be modified in calling routine
1643 if we know what the prefix command is. */
c5aa993b 1644 *result_list = 0;
cf00cd6f
PW
1645 if (default_args != nullptr)
1646 *default_args = std::string ();
1427fe5e 1647 return CMD_LIST_AMBIGUOUS; /* Ambiguous. */
c906108c
SS
1648 }
1649
ebcd3b23 1650 /* We've matched something on this list. Move text pointer forward. */
c906108c 1651
3386243e 1652 *text += len;
c906108c 1653
1be99b11 1654 if (found->is_alias ())
56382845 1655 {
ebcd3b23
MS
1656 /* We drop the alias (abbreviation) in favor of the command it
1657 is pointing to. If the alias is deprecated, though, we need to
56382845
FN
1658 warn the user about it before we drop it. Note that while we
1659 are warning about the alias, we may also warn about the command
1660 itself and we will adjust the appropriate DEPRECATED_WARN_USER
ebcd3b23 1661 flags. */
cf00cd6f 1662
1536146f 1663 if (found->deprecated_warn_user && !lookup_for_completion_p)
9ef6d4a1 1664 deprecated_cmd_warning (line, clist);
cf00cd6f 1665
44c77c32 1666
cf00cd6f
PW
1667 /* Return the default_args of the alias, not the default_args
1668 of the command it is pointing to. */
1669 if (default_args != nullptr)
1670 *default_args = found->default_args;
99858724 1671 found = found->alias_target;
cf00cd6f 1672 found_alias = true;
56382845 1673 }
c906108c
SS
1674 /* If we found a prefix command, keep looking. */
1675
3d0b3564 1676 if (found->is_prefix ())
c906108c 1677 {
14b42fc4 1678 c = lookup_cmd_1 (text, *found->subcommands, result_list, default_args,
1536146f 1679 ignore_help_classes, lookup_for_completion_p);
c906108c
SS
1680 if (!c)
1681 {
1682 /* Didn't find anything; this is as far as we got. */
cf00cd6f 1683 if (result_list != nullptr)
c906108c 1684 *result_list = clist;
cf00cd6f
PW
1685 if (!found_alias && default_args != nullptr)
1686 *default_args = found->default_args;
c906108c
SS
1687 return found;
1688 }
1427fe5e 1689 else if (c == CMD_LIST_AMBIGUOUS)
c906108c 1690 {
ebcd3b23
MS
1691 /* We've gotten this far properly, but the next step is
1692 ambiguous. We need to set the result list to the best
c906108c 1693 we've found (if an inferior hasn't already set it). */
cf00cd6f 1694 if (result_list != nullptr)
c906108c 1695 if (!*result_list)
14b42fc4 1696 /* This used to say *result_list = *found->subcommands.
dda83cd7
SM
1697 If that was correct, need to modify the documentation
1698 at the top of this function to clarify what is
1699 supposed to be going on. */
c906108c 1700 *result_list = found;
cf00cd6f
PW
1701 /* For ambiguous commands, do not return any default_args args. */
1702 if (default_args != nullptr)
1703 *default_args = std::string ();
c906108c
SS
1704 return c;
1705 }
1706 else
1707 {
1708 /* We matched! */
1709 return c;
1710 }
1711 }
1712 else
1713 {
cf00cd6f 1714 if (result_list != nullptr)
c906108c 1715 *result_list = clist;
cf00cd6f
PW
1716 if (!found_alias && default_args != nullptr)
1717 *default_args = found->default_args;
c906108c
SS
1718 return found;
1719 }
1720}
1721
1722/* All this hair to move the space to the front of cmdtype */
1723
1724static void
6f937416 1725undef_cmd_error (const char *cmdtype, const char *q)
c906108c 1726{
8a3fe4f8 1727 error (_("Undefined %scommand: \"%s\". Try \"help%s%.*s\"."),
c5aa993b
JM
1728 cmdtype,
1729 q,
1730 *cmdtype ? " " : "",
823ca731 1731 (int) strlen (cmdtype) - 1,
c5aa993b 1732 cmdtype);
c906108c
SS
1733}
1734
1735/* Look up the contents of *LINE as a command in the command list LIST.
1736 LIST is a chain of struct cmd_list_element's.
cf00cd6f
PW
1737 If it is found, return the struct cmd_list_element for that command,
1738 update *LINE to point after the command name, at the first argument
1739 and update *DEFAULT_ARGS (if DEFAULT_ARGS is not null) to the default
1740 args to prepend to the user provided args when running the command.
1741 Note that if the found cmd_list_element is found via an alias,
1742 the default args of the alias are returned.
1743
c906108c
SS
1744 If not found, call error if ALLOW_UNKNOWN is zero
1745 otherwise (or if error returns) return zero.
1746 Call error if specified command is ambiguous,
1747 unless ALLOW_UNKNOWN is negative.
1748 CMDTYPE precedes the word "command" in the error message.
1749
30baf67b 1750 If IGNORE_HELP_CLASSES is nonzero, ignore any command list
c906108c
SS
1751 elements which are actually help classes rather than commands (i.e.
1752 the function field of the struct cmd_list_element is 0). */
1753
1754struct cmd_list_element *
a121b7c1
PA
1755lookup_cmd (const char **line, struct cmd_list_element *list,
1756 const char *cmdtype,
cf00cd6f 1757 std::string *default_args,
fba45db2 1758 int allow_unknown, int ignore_help_classes)
c906108c
SS
1759{
1760 struct cmd_list_element *last_list = 0;
3cebf8d8 1761 struct cmd_list_element *c;
c64601c7
FN
1762
1763 /* Note: Do not remove trailing whitespace here because this
1764 would be wrong for complete_command. Jim Kingdon */
c5aa993b 1765
3cebf8d8
MS
1766 if (!*line)
1767 error (_("Lack of needed %scommand"), cmdtype);
1768
cf00cd6f 1769 c = lookup_cmd_1 (line, list, &last_list, default_args, ignore_help_classes);
3cebf8d8 1770
c906108c
SS
1771 if (!c)
1772 {
1773 if (!allow_unknown)
1774 {
3cebf8d8
MS
1775 char *q;
1776 int len = find_command_name_length (*line);
c906108c 1777
3cebf8d8
MS
1778 q = (char *) alloca (len + 1);
1779 strncpy (q, *line, len);
1780 q[len] = '\0';
1781 undef_cmd_error (cmdtype, q);
c906108c
SS
1782 }
1783 else
1784 return 0;
1785 }
1427fe5e 1786 else if (c == CMD_LIST_AMBIGUOUS)
c906108c 1787 {
14b42fc4 1788 /* Ambigous. Local values should be off subcommands or called
dda83cd7 1789 values. */
c906108c
SS
1790 int local_allow_unknown = (last_list ? last_list->allow_unknown :
1791 allow_unknown);
2f822da5
MB
1792 std::string local_cmdtype
1793 = last_list ? last_list->prefixname () : cmdtype;
c906108c 1794 struct cmd_list_element *local_list =
14b42fc4 1795 (last_list ? *(last_list->subcommands) : list);
c5aa993b 1796
c906108c
SS
1797 if (local_allow_unknown < 0)
1798 {
1799 if (last_list)
1800 return last_list; /* Found something. */
1801 else
1802 return 0; /* Found nothing. */
1803 }
1804 else
1805 {
1806 /* Report as error. */
1807 int amb_len;
1808 char ambbuf[100];
1809
1810 for (amb_len = 0;
1811 ((*line)[amb_len] && (*line)[amb_len] != ' '
1812 && (*line)[amb_len] != '\t');
1813 amb_len++)
1814 ;
c5aa993b 1815
c906108c
SS
1816 ambbuf[0] = 0;
1817 for (c = local_list; c; c = c->next)
1818 if (!strncmp (*line, c->name, amb_len))
1819 {
9a2b4c1b
MS
1820 if (strlen (ambbuf) + strlen (c->name) + 6
1821 < (int) sizeof ambbuf)
c906108c
SS
1822 {
1823 if (strlen (ambbuf))
1824 strcat (ambbuf, ", ");
1825 strcat (ambbuf, c->name);
1826 }
1827 else
1828 {
1829 strcat (ambbuf, "..");
1830 break;
1831 }
1832 }
2f822da5
MB
1833 error (_("Ambiguous %scommand \"%s\": %s."),
1834 local_cmdtype.c_str (), *line, ambbuf);
c906108c
SS
1835 }
1836 }
1837 else
1838 {
bf9e4d0c 1839 if (c->type == set_cmd && **line != '\0' && !isspace (**line))
dda83cd7 1840 error (_("Argument must be preceded by space."));
bf9e4d0c 1841
c906108c 1842 /* We've got something. It may still not be what the caller
dda83cd7 1843 wants (if this command *needs* a subcommand). */
c906108c
SS
1844 while (**line == ' ' || **line == '\t')
1845 (*line)++;
1846
3d0b3564 1847 if (c->is_prefix () && **line && !c->allow_unknown)
2f822da5 1848 undef_cmd_error (c->prefixname ().c_str (), *line);
c906108c
SS
1849
1850 /* Seems to be what he wants. Return it. */
1851 return c;
1852 }
1853 return 0;
1854}
c5aa993b 1855
a9b49cbc
MB
1856/* See command.h. */
1857
1858struct cmd_list_element *
1859lookup_cmd_exact (const char *name,
1860 struct cmd_list_element *list,
1861 bool ignore_help_classes)
1862{
1863 const char *tem = name;
1864 struct cmd_list_element *cmd = lookup_cmd (&tem, list, "", NULL, -1,
1865 ignore_help_classes);
1866 if (cmd != nullptr && strcmp (name, cmd->name) != 0)
1867 cmd = nullptr;
1868 return cmd;
1869}
1870
6f937416 1871/* We are here presumably because an alias or command in TEXT is
ebcd3b23 1872 deprecated and a warning message should be generated. This
6f937416 1873 function decodes TEXT and potentially generates a warning message
ebcd3b23 1874 as outlined below.
56382845
FN
1875
1876 Example for 'set endian big' which has a fictitious alias 'seb'.
1877
6f937416 1878 If alias wasn't used in TEXT, and the command is deprecated:
56382845
FN
1879 "warning: 'set endian big' is deprecated."
1880
1881 If alias was used, and only the alias is deprecated:
1882 "warning: 'seb' an alias for the command 'set endian big' is deprecated."
1883
ebcd3b23
MS
1884 If alias was used and command is deprecated (regardless of whether
1885 the alias itself is deprecated:
56382845
FN
1886
1887 "warning: 'set endian big' (seb) is deprecated."
1888
1889 After the message has been sent, clear the appropriate flags in the
1890 command and/or the alias so the user is no longer bothered.
1891
1892*/
1893void
9ef6d4a1 1894deprecated_cmd_warning (const char *text, struct cmd_list_element *list)
56382845 1895{
44c77c32 1896 struct cmd_list_element *alias = nullptr;
44c77c32 1897 struct cmd_list_element *cmd = nullptr;
edefbb7c 1898
19c659f1
AB
1899 /* Return if text doesn't evaluate to a command. We place this lookup
1900 within its own scope so that the PREFIX_CMD local is not visible
1901 later in this function. The value returned in PREFIX_CMD is based on
1902 the prefix found in TEXT, and is our case this prefix can be missing
1903 in some situations (when LIST is not the global CMDLIST).
1904
1905 It is better for our purposes to use the prefix commands directly from
1906 the ALIAS and CMD results. */
1907 {
1908 struct cmd_list_element *prefix_cmd = nullptr;
1909 if (!lookup_cmd_composition_1 (text, &alias, &prefix_cmd, &cmd, list))
1910 return;
1911 }
56382845 1912
44c77c32
AB
1913 /* Return if nothing is deprecated. */
1914 if (!((alias != nullptr ? alias->deprecated_warn_user : 0)
1915 || cmd->deprecated_warn_user))
56382845 1916 return;
56382845 1917
44c77c32
AB
1918 /* Join command prefix (if any) and the command name. */
1919 std::string tmp_cmd_str;
19c659f1 1920 if (cmd->prefix != nullptr)
2f822da5 1921 tmp_cmd_str += cmd->prefix->prefixname ();
44c77c32 1922 tmp_cmd_str += std::string (cmd->name);
56382845 1923
44c77c32
AB
1924 /* Display the appropriate first line, this warns that the thing the user
1925 entered is deprecated. */
1926 if (alias != nullptr)
56382845 1927 {
19c659f1
AB
1928 /* Join the alias prefix (if any) and the alias name. */
1929 std::string tmp_alias_str;
1930 if (alias->prefix != nullptr)
2f822da5 1931 tmp_alias_str += alias->prefix->prefixname ();
19c659f1
AB
1932 tmp_alias_str += std::string (alias->name);
1933
44c77c32 1934 if (cmd->cmd_deprecated)
0e5ad442
TT
1935 printf_filtered (_("Warning: command '%ps' (%ps) is deprecated.\n"),
1936 styled_string (title_style.style (),
1937 tmp_cmd_str.c_str ()),
1938 styled_string (title_style.style (),
1939 tmp_alias_str.c_str ()));
56382845 1940 else
0e5ad442 1941 printf_filtered (_("Warning: '%ps', an alias for the command '%ps', "
44c77c32 1942 "is deprecated.\n"),
0e5ad442
TT
1943 styled_string (title_style.style (),
1944 tmp_alias_str.c_str ()),
1945 styled_string (title_style.style (),
1946 tmp_cmd_str.c_str ()));
56382845 1947 }
44c77c32 1948 else
0e5ad442
TT
1949 printf_filtered (_("Warning: command '%ps' is deprecated.\n"),
1950 styled_string (title_style.style (),
1951 tmp_cmd_str.c_str ()));
44c77c32
AB
1952
1953 /* Now display a second line indicating what the user should use instead.
1954 If it is only the alias that is deprecated, we want to indicate the
1955 new alias, otherwise we'll indicate the new command. */
1956 const char *replacement;
1957 if (alias != nullptr && !cmd->cmd_deprecated)
1958 replacement = alias->replacement;
1959 else
1960 replacement = cmd->replacement;
1961 if (replacement != nullptr)
0e5ad442
TT
1962 printf_filtered (_("Use '%ps'.\n\n"),
1963 styled_string (title_style.style (),
1964 replacement));
44c77c32
AB
1965 else
1966 printf_filtered (_("No alternative known.\n\n"));
56382845 1967
ebcd3b23 1968 /* We've warned you, now we'll keep quiet. */
44c77c32 1969 if (alias != nullptr)
1f2bdf09 1970 alias->deprecated_warn_user = 0;
1f2bdf09 1971 cmd->deprecated_warn_user = 0;
56382845
FN
1972}
1973
9ef6d4a1 1974/* Look up the contents of TEXT as a command in the command list CUR_LIST.
56382845 1975 Return 1 on success, 0 on failure.
bc3609fd
PW
1976
1977 If TEXT refers to an alias, *ALIAS will point to that alias.
1978
1979 If TEXT is a subcommand (i.e. one that is preceded by a prefix
1980 command) set *PREFIX_CMD.
1981
1982 Set *CMD to point to the command TEXT indicates.
1983
1984 If any of *ALIAS, *PREFIX_CMD, or *CMD cannot be determined or do not
56382845 1985 exist, they are NULL when we return.
bc3609fd 1986
56382845 1987*/
9ef6d4a1
AB
1988
1989static int
1990lookup_cmd_composition_1 (const char *text,
1991 struct cmd_list_element **alias,
1992 struct cmd_list_element **prefix_cmd,
1993 struct cmd_list_element **cmd,
1994 struct cmd_list_element *cur_list)
56382845 1995{
19c659f1
AB
1996 *alias = nullptr;
1997 *prefix_cmd = cur_list->prefix;
1998 *cmd = nullptr;
bc3609fd 1999
0605465f
PW
2000 text = skip_spaces (text);
2001
19c659f1
AB
2002 /* Go through as many command lists as we need to, to find the command
2003 TEXT refers to. */
56382845 2004 while (1)
bc3609fd 2005 {
3386243e 2006 /* Identify the name of the command. */
19c659f1 2007 int len = find_command_name_length (text);
bc3609fd 2008
56382845 2009 /* If nothing but whitespace, return. */
3386243e
AS
2010 if (len == 0)
2011 return 0;
bc3609fd
PW
2012
2013 /* TEXT is the start of the first command word to lookup (and
0605465f 2014 it's length is LEN). We copy this into a local temporary. */
19c659f1 2015 std::string command (text, len);
bc3609fd 2016
56382845 2017 /* Look it up. */
19c659f1
AB
2018 int nfound = 0;
2019 *cmd = find_cmd (command.c_str (), len, cur_list, 1, &nfound);
bc3609fd 2020
19c659f1
AB
2021 /* We only handle the case where a single command was found. */
2022 if (*cmd == CMD_LIST_AMBIGUOUS || *cmd == nullptr)
2023 return 0;
56382845 2024 else
cdb27c12 2025 {
1be99b11 2026 if ((*cmd)->is_alias ())
cdb27c12 2027 {
19c659f1
AB
2028 /* If the command was actually an alias, we note that an
2029 alias was used (by assigning *ALIAS) and we set *CMD. */
cdb27c12 2030 *alias = *cmd;
99858724 2031 *cmd = (*cmd)->alias_target;
cdb27c12 2032 }
cdb27c12 2033 }
0605465f
PW
2034
2035 text += len;
2036 text = skip_spaces (text);
2037
3d0b3564 2038 if ((*cmd)->is_prefix () && *text != '\0')
19c659f1 2039 {
14b42fc4 2040 cur_list = *(*cmd)->subcommands;
19c659f1
AB
2041 *prefix_cmd = *cmd;
2042 }
56382845 2043 else
cdb27c12 2044 return 1;
56382845
FN
2045 }
2046}
2047
9ef6d4a1
AB
2048/* Look up the contents of TEXT as a command in the command list 'cmdlist'.
2049 Return 1 on success, 0 on failure.
2050
2051 If TEXT refers to an alias, *ALIAS will point to that alias.
2052
2053 If TEXT is a subcommand (i.e. one that is preceded by a prefix
2054 command) set *PREFIX_CMD.
2055
2056 Set *CMD to point to the command TEXT indicates.
2057
2058 If any of *ALIAS, *PREFIX_CMD, or *CMD cannot be determined or do not
2059 exist, they are NULL when we return.
2060
2061*/
2062
2063int
2064lookup_cmd_composition (const char *text,
2065 struct cmd_list_element **alias,
2066 struct cmd_list_element **prefix_cmd,
2067 struct cmd_list_element **cmd)
2068{
2069 return lookup_cmd_composition_1 (text, alias, prefix_cmd, cmd, cmdlist);
2070}
2071
c906108c
SS
2072/* Helper function for SYMBOL_COMPLETION_FUNCTION. */
2073
2074/* Return a vector of char pointers which point to the different
ebcd3b23 2075 possible completions in LIST of TEXT.
c906108c
SS
2076
2077 WORD points in the same buffer as TEXT, and completions should be
ebcd3b23
MS
2078 returned relative to this position. For example, suppose TEXT is
2079 "foo" and we want to complete to "foobar". If WORD is "oo", return
c906108c
SS
2080 "oobar"; if WORD is "baz/foo", return "baz/foobar". */
2081
eb3ff9a5 2082void
6f937416 2083complete_on_cmdlist (struct cmd_list_element *list,
eb3ff9a5 2084 completion_tracker &tracker,
6f937416 2085 const char *text, const char *word,
ace21957 2086 int ignore_help_classes)
c906108c
SS
2087{
2088 struct cmd_list_element *ptr;
c906108c 2089 int textlen = strlen (text);
3f172e24
TT
2090 int pass;
2091 int saw_deprecated_match = 0;
c906108c 2092
3f172e24
TT
2093 /* We do one or two passes. In the first pass, we skip deprecated
2094 commands. If we see no matching commands in the first pass, and
2095 if we did happen to see a matching deprecated command, we do
2096 another loop to collect those. */
eb3ff9a5 2097 for (pass = 0; pass < 2; ++pass)
3f172e24 2098 {
eb3ff9a5
PA
2099 bool got_matches = false;
2100
3f172e24
TT
2101 for (ptr = list; ptr; ptr = ptr->next)
2102 if (!strncmp (ptr->name, text, textlen)
2103 && !ptr->abbrev_flag
034dce7a 2104 && (!ignore_help_classes || !ptr->is_command_class_help ()
3d0b3564 2105 || ptr->is_prefix ()))
c906108c 2106 {
3f172e24
TT
2107 if (pass == 0)
2108 {
1f2bdf09 2109 if (ptr->cmd_deprecated)
3f172e24
TT
2110 {
2111 saw_deprecated_match = 1;
2112 continue;
2113 }
2114 }
c906108c 2115
60a20c19
PA
2116 tracker.add_completion
2117 (make_completion_match_str (ptr->name, text, word));
eb3ff9a5 2118 got_matches = true;
c906108c 2119 }
eb3ff9a5
PA
2120
2121 if (got_matches)
2122 break;
2123
3f172e24
TT
2124 /* If we saw no matching deprecated commands in the first pass,
2125 just bail out. */
2126 if (!saw_deprecated_match)
2127 break;
2128 }
c906108c
SS
2129}
2130
2131/* Helper function for SYMBOL_COMPLETION_FUNCTION. */
2132
eb3ff9a5 2133/* Add the different possible completions in ENUMLIST of TEXT.
c906108c
SS
2134
2135 WORD points in the same buffer as TEXT, and completions should be
2136 returned relative to this position. For example, suppose TEXT is "foo"
2137 and we want to complete to "foobar". If WORD is "oo", return
2138 "oobar"; if WORD is "baz/foo", return "baz/foobar". */
2139
eb3ff9a5
PA
2140void
2141complete_on_enum (completion_tracker &tracker,
2142 const char *const *enumlist,
6f937416 2143 const char *text, const char *word)
c906108c 2144{
c906108c
SS
2145 int textlen = strlen (text);
2146 int i;
53904c9e 2147 const char *name;
c906108c 2148
c906108c
SS
2149 for (i = 0; (name = enumlist[i]) != NULL; i++)
2150 if (strncmp (name, text, textlen) == 0)
60a20c19 2151 tracker.add_completion (make_completion_match_str (name, text, word));
c906108c
SS
2152}
2153
ebcd3b23 2154/* Call the command function. */
f436dd25 2155void
95a6b0a1 2156cmd_func (struct cmd_list_element *cmd, const char *args, int from_tty)
f436dd25 2157{
034dce7a 2158 if (!cmd->is_command_class_help ())
4034d0ff 2159 {
156d9eab 2160 gdb::optional<scoped_restore_tmpl<int>> restore_suppress;
4034d0ff
AT
2161
2162 if (cmd->suppress_notification != NULL)
156d9eab 2163 restore_suppress.emplace (cmd->suppress_notification, 1);
4034d0ff
AT
2164
2165 (*cmd->func) (cmd, args, from_tty);
4034d0ff 2166 }
f436dd25 2167 else
8a3fe4f8 2168 error (_("Invalid command"));
f436dd25 2169}
a9f116cb
GKB
2170
2171int
2172cli_user_command_p (struct cmd_list_element *cmd)
2173{
fe978cb0 2174 return (cmd->theclass == class_user
5fed81ff 2175 && (cmd->func == do_const_cfunc || cmd->func == do_sfunc));
a9f116cb 2176}