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