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