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