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