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