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