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