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