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