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