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