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