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