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