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