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