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