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