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