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