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