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