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