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