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