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