* gdb.base/sizeof.c: include <stdio.h>.
[deliverable/binutils-gdb.git] / gdb / command.c
CommitLineData
c906108c 1/* Handle lists of commands, their decoding and documentation, for GDB.
d9fcf2fb 2 Copyright 1986, 1989, 1990, 1991, 1998, 2000 Free Software Foundation, Inc.
c906108c 3
c5aa993b
JM
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
c906108c 8
c5aa993b
JM
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
c906108c 13
c5aa993b
JM
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place - Suite 330,
17 Boston, MA 02111-1307, USA. */
c906108c
SS
18
19#include "defs.h"
20#include "gdbcmd.h"
21#include "symtab.h"
22#include "value.h"
23#include <ctype.h>
24#include "gdb_string.h"
8b93c638
JM
25#ifdef UI_OUT
26#include "ui-out.h"
27#endif
c906108c 28
03f2053f 29#include "gdb_wait.h"
6837a0a2 30#include "gnu-regex.h"
53a5351d
JM
31/* FIXME: this should be auto-configured! */
32#ifdef __MSDOS__
33# define CANT_FORK
34#endif
35
c906108c
SS
36/* Prototypes for local functions */
37
a14ed312 38static void undef_cmd_error (char *, char *);
c906108c 39
a14ed312 40static void show_user (char *, int);
c906108c 41
d9fcf2fb 42static void show_user_1 (struct cmd_list_element *, struct ui_file *);
c906108c 43
a14ed312 44static void make_command (char *, int);
c906108c 45
a14ed312 46static void shell_escape (char *, int);
c906108c 47
a14ed312 48static int parse_binary_operation (char *);
c906108c 49
d9fcf2fb 50static void print_doc_line (struct ui_file *, char *);
c906108c 51
a14ed312
KB
52static struct cmd_list_element *find_cmd (char *command,
53 int len,
54 struct cmd_list_element *clist,
55 int ignore_help_classes,
56 int *nfound);
6837a0a2
DB
57static void apropos_cmd_helper (struct ui_file *, struct cmd_list_element *,
58 struct re_pattern_buffer *, char *);
59
c85871a3
AC
60static void help_all (struct ui_file *stream);
61
6837a0a2 62void apropos_command (char *, int);
392a587b 63
a14ed312 64void _initialize_command (void);
c906108c
SS
65
66/* Add element named NAME.
67 CLASS is the top level category into which commands are broken down
68 for "help" purposes.
69 FUN should be the function to execute the command;
70 it will get a character string as argument, with leading
71 and trailing blanks already eliminated.
72
73 DOC is a documentation string for the command.
74 Its first line should be a complete sentence.
75 It should start with ? for a command that is an abbreviation
76 or with * for a command that most users don't need to know about.
77
78 Add this command to command list *LIST.
79
80 Returns a pointer to the added command (not necessarily the head
81 of *LIST). */
82
83struct cmd_list_element *
84add_cmd (name, class, fun, doc, list)
85 char *name;
86 enum command_class class;
507f3c78 87 void (*fun) (char *, int);
c906108c
SS
88 char *doc;
89 struct cmd_list_element **list;
90{
91 register struct cmd_list_element *c
c5aa993b 92 = (struct cmd_list_element *) xmalloc (sizeof (struct cmd_list_element));
c906108c
SS
93 struct cmd_list_element *p;
94
95 delete_cmd (name, list);
96
97 if (*list == NULL || STRCMP ((*list)->name, name) >= 0)
98 {
99 c->next = *list;
100 *list = c;
101 }
102 else
103 {
104 p = *list;
105 while (p->next && STRCMP (p->next->name, name) <= 0)
c5aa993b
JM
106 {
107 p = p->next;
108 }
c906108c
SS
109 c->next = p->next;
110 p->next = c;
111 }
112
113 c->name = name;
114 c->class = class;
115 c->function.cfunc = fun;
116 c->doc = doc;
56382845
FN
117 c->flags = 0;
118 c->replacement = NULL;
c906108c
SS
119 c->hook = NULL;
120 c->prefixlist = NULL;
121 c->prefixname = NULL;
122 c->allow_unknown = 0;
123 c->abbrev_flag = 0;
124 c->completer = make_symbol_completion_list;
125 c->type = not_set_cmd;
126 c->var = NULL;
127 c->var_type = var_boolean;
128 c->enums = NULL;
129 c->user_commands = NULL;
130 c->hookee = NULL;
131 c->cmd_pointer = NULL;
132
133 return c;
134}
135
56382845
FN
136
137/* Deprecates a command CMD.
138 REPLACEMENT is the name of the command which should be used in place
139 of this command, or NULL if no such command exists.
140
141 This function does not check to see if command REPLACEMENT exists
142 since gdb may not have gotten around to adding REPLACEMENT when this
143 function is called.
144
145 Returns a pointer to the deprecated command. */
146
147struct cmd_list_element *
fba45db2 148deprecate_cmd (struct cmd_list_element *cmd, char *replacement)
56382845
FN
149{
150 cmd->flags |= (CMD_DEPRECATED | DEPRECATED_WARN_USER);
151
152 if (replacement != NULL)
153 cmd->replacement = replacement;
154 else
155 cmd->replacement = NULL;
156
157 return cmd;
158}
159
160
c906108c
SS
161/* Same as above, except that the abbrev_flag is set. */
162
c5aa993b 163#if 0 /* Currently unused */
c906108c
SS
164
165struct cmd_list_element *
166add_abbrev_cmd (name, class, fun, doc, list)
167 char *name;
168 enum command_class class;
507f3c78 169 void (*fun) (char *, int);
c906108c
SS
170 char *doc;
171 struct cmd_list_element **list;
172{
173 register struct cmd_list_element *c
c5aa993b 174 = add_cmd (name, class, fun, doc, list);
c906108c
SS
175
176 c->abbrev_flag = 1;
177 return c;
178}
179
180#endif
181
182struct cmd_list_element *
fba45db2
KB
183add_alias_cmd (char *name, char *oldname, enum command_class class,
184 int abbrev_flag, struct cmd_list_element **list)
c906108c
SS
185{
186 /* Must do this since lookup_cmd tries to side-effect its first arg */
187 char *copied_name;
188 register struct cmd_list_element *old;
189 register struct cmd_list_element *c;
190 copied_name = (char *) alloca (strlen (oldname) + 1);
191 strcpy (copied_name, oldname);
c5aa993b 192 old = lookup_cmd (&copied_name, *list, "", 1, 1);
c906108c
SS
193
194 if (old == 0)
195 {
196 delete_cmd (name, list);
197 return 0;
198 }
199
200 c = add_cmd (name, class, old->function.cfunc, old->doc, list);
201 c->prefixlist = old->prefixlist;
202 c->prefixname = old->prefixname;
203 c->allow_unknown = old->allow_unknown;
204 c->abbrev_flag = abbrev_flag;
205 c->cmd_pointer = old;
206 return c;
207}
208
209/* Like add_cmd but adds an element for a command prefix:
210 a name that should be followed by a subcommand to be looked up
211 in another command list. PREFIXLIST should be the address
212 of the variable containing that list. */
213
214struct cmd_list_element *
215add_prefix_cmd (name, class, fun, doc, prefixlist, prefixname,
216 allow_unknown, list)
217 char *name;
218 enum command_class class;
507f3c78 219 void (*fun) (char *, int);
c906108c
SS
220 char *doc;
221 struct cmd_list_element **prefixlist;
222 char *prefixname;
223 int allow_unknown;
224 struct cmd_list_element **list;
225{
226 register struct cmd_list_element *c = add_cmd (name, class, fun, doc, list);
227 c->prefixlist = prefixlist;
228 c->prefixname = prefixname;
229 c->allow_unknown = allow_unknown;
230 return c;
231}
232
233/* Like add_prefix_cmd but sets the abbrev_flag on the new command. */
c5aa993b 234
c906108c
SS
235struct cmd_list_element *
236add_abbrev_prefix_cmd (name, class, fun, doc, prefixlist, prefixname,
237 allow_unknown, list)
238 char *name;
239 enum command_class class;
507f3c78 240 void (*fun) (char *, int);
c906108c
SS
241 char *doc;
242 struct cmd_list_element **prefixlist;
243 char *prefixname;
244 int allow_unknown;
245 struct cmd_list_element **list;
246{
247 register struct cmd_list_element *c = add_cmd (name, class, fun, doc, list);
248 c->prefixlist = prefixlist;
249 c->prefixname = prefixname;
250 c->allow_unknown = allow_unknown;
251 c->abbrev_flag = 1;
252 return c;
253}
254
255/* This is an empty "cfunc". */
256void
fba45db2 257not_just_help_class_command (char *args, int from_tty)
c906108c
SS
258{
259}
260
261/* This is an empty "sfunc". */
a14ed312 262static void empty_sfunc (char *, int, struct cmd_list_element *);
c906108c
SS
263
264static void
fba45db2 265empty_sfunc (char *args, int from_tty, struct cmd_list_element *c)
c906108c
SS
266{
267}
268
269/* Add element named NAME to command list LIST (the list for set
270 or some sublist thereof).
271 CLASS is as in add_cmd.
272 VAR_TYPE is the kind of thing we are setting.
273 VAR is address of the variable being controlled by this command.
274 DOC is the documentation string. */
275
276struct cmd_list_element *
1ed2a135
AC
277add_set_cmd (char *name,
278 enum command_class class,
279 var_types var_type,
280 void *var,
281 char *doc,
282 struct cmd_list_element **list)
c906108c
SS
283{
284 struct cmd_list_element *c
c5aa993b 285 = add_cmd (name, class, NO_FUNCTION, doc, list);
c906108c
SS
286
287 c->type = set_cmd;
288 c->var_type = var_type;
289 c->var = var;
290 /* This needs to be something besides NO_FUNCTION so that this isn't
291 treated as a help class. */
292 c->function.sfunc = empty_sfunc;
293 return c;
294}
295
296/* Add element named NAME to command list LIST (the list for set
297 or some sublist thereof).
298 CLASS is as in add_cmd.
299 ENUMLIST is a list of strings which may follow NAME.
300 VAR is address of the variable which will contain the matching string
c5aa993b 301 (from ENUMLIST).
c906108c
SS
302 DOC is the documentation string. */
303
304struct cmd_list_element *
1ed2a135
AC
305add_set_enum_cmd (char *name,
306 enum command_class class,
53904c9e
AC
307 const char *enumlist[],
308 const char **var,
1ed2a135
AC
309 char *doc,
310 struct cmd_list_element **list)
c906108c
SS
311{
312 struct cmd_list_element *c
c5aa993b 313 = add_set_cmd (name, class, var_enum, var, doc, list);
c906108c
SS
314 c->enums = enumlist;
315
316 return c;
317}
318
97c3646f
AC
319/* Add element named NAME to command list LIST (the list for set
320 or some sublist thereof).
321 CLASS is as in add_cmd.
322 VAR is address of the variable which will contain the value.
323 DOC is the documentation string. */
324struct cmd_list_element *
325add_set_auto_boolean_cmd (char *name,
326 enum command_class class,
327 enum cmd_auto_boolean *var,
328 char *doc,
329 struct cmd_list_element **list)
330{
331 static const char *auto_boolean_enums[] = { "on", "off", "auto", NULL };
332 struct cmd_list_element *c;
333 c = add_set_cmd (name, class, var_auto_boolean, var, doc, list);
334 c->enums = auto_boolean_enums;
335 return c;
336}
337
c906108c
SS
338/* Where SETCMD has already been added, add the corresponding show
339 command to LIST and return a pointer to the added command (not
340 necessarily the head of LIST). */
341struct cmd_list_element *
fba45db2
KB
342add_show_from_set (struct cmd_list_element *setcmd,
343 struct cmd_list_element **list)
c906108c
SS
344{
345 struct cmd_list_element *showcmd =
c5aa993b 346 (struct cmd_list_element *) xmalloc (sizeof (struct cmd_list_element));
c906108c
SS
347 struct cmd_list_element *p;
348
349 memcpy (showcmd, setcmd, sizeof (struct cmd_list_element));
350 delete_cmd (showcmd->name, list);
351 showcmd->type = show_cmd;
c5aa993b 352
c906108c
SS
353 /* Replace "set " at start of docstring with "show ". */
354 if (setcmd->doc[0] == 'S' && setcmd->doc[1] == 'e'
355 && setcmd->doc[2] == 't' && setcmd->doc[3] == ' ')
356 showcmd->doc = concat ("Show ", setcmd->doc + 4, NULL);
357 else
358 fprintf_unfiltered (gdb_stderr, "GDB internal error: Bad docstring for set command\n");
c5aa993b
JM
359
360 if (*list == NULL || STRCMP ((*list)->name, showcmd->name) >= 0)
c906108c
SS
361 {
362 showcmd->next = *list;
363 *list = showcmd;
364 }
365 else
366 {
367 p = *list;
368 while (p->next && STRCMP (p->next->name, showcmd->name) <= 0)
c5aa993b
JM
369 {
370 p = p->next;
371 }
c906108c
SS
372 showcmd->next = p->next;
373 p->next = showcmd;
374 }
375
376 return showcmd;
377}
378
379/* Remove the command named NAME from the command list. */
380
381void
fba45db2 382delete_cmd (char *name, struct cmd_list_element **list)
c906108c
SS
383{
384 register struct cmd_list_element *c;
385 struct cmd_list_element *p;
386
387 while (*list && STREQ ((*list)->name, name))
388 {
389 if ((*list)->hookee)
390 (*list)->hookee->hook = 0; /* Hook slips out of its mouth */
391 p = (*list)->next;
c5aa993b 392 free ((PTR) * list);
c906108c
SS
393 *list = p;
394 }
395
396 if (*list)
397 for (c = *list; c->next;)
398 {
399 if (STREQ (c->next->name, name))
400 {
401 if (c->next->hookee)
c5aa993b 402 c->next->hookee->hook = 0; /* hooked cmd gets away. */
c906108c 403 p = c->next->next;
c5aa993b 404 free ((PTR) c->next);
c906108c
SS
405 c->next = p;
406 }
407 else
408 c = c->next;
409 }
410}
6837a0a2
DB
411/* Recursively walk the commandlist structures, and print out the
412 documentation of commands that match our regex in either their
413 name, or their documentation.
414*/
415static void
416apropos_cmd_helper (struct ui_file *stream, struct cmd_list_element *commandlist,
417 struct re_pattern_buffer *regex, char *prefix)
418{
419 register struct cmd_list_element *c;
420 int returnvalue=1; /*Needed to avoid double printing*/
421 /* Walk through the commands */
422 for (c=commandlist;c;c=c->next)
423 {
424 if (c->name != NULL)
425 {
426 /* Try to match against the name*/
427 returnvalue=re_search(regex,c->name,strlen(c->name),0,strlen(c->name),NULL);
428 if (returnvalue >= 0)
429 {
430 /* Stolen from help_cmd_list. We don't directly use
431 * help_cmd_list because it doesn't let us print out
432 * single commands
433 */
434 fprintf_filtered (stream, "%s%s -- ", prefix, c->name);
435 print_doc_line (stream, c->doc);
436 fputs_filtered ("\n", stream);
437 returnvalue=0; /*Set this so we don't print it again.*/
438 }
439 }
440 if (c->doc != NULL && returnvalue != 0)
441 {
442 /* Try to match against documentation */
443 if (re_search(regex,c->doc,strlen(c->doc),0,strlen(c->doc),NULL) >=0)
444 {
445 /* Stolen from help_cmd_list. We don't directly use
446 * help_cmd_list because it doesn't let us print out
447 * single commands
448 */
449 fprintf_filtered (stream, "%s%s -- ", prefix, c->name);
450 print_doc_line (stream, c->doc);
451 fputs_filtered ("\n", stream);
452 }
453 }
454 /* Check if this command has subcommands */
455 if (c->prefixlist != NULL)
456 {
457 /* Recursively call ourselves on the subcommand list,
458 passing the right prefix in.
459 */
460 apropos_cmd_helper(stream,*c->prefixlist,regex,c->prefixname);
461 }
462 }
463}
464/* Search through names of commands and documentations for a certain
465 regular expression.
466*/
467void
468apropos_command (char *searchstr, int from_tty)
469{
470 extern struct cmd_list_element *cmdlist; /*This is the main command list*/
471 regex_t pattern;
472 char *pattern_fastmap;
473 char errorbuffer[512];
474 pattern_fastmap=calloc(256,sizeof(char));
475 if (searchstr == NULL)
476 error("REGEXP string is empty");
477
478 if (regcomp(&pattern,searchstr,REG_ICASE) == 0)
479 {
480 pattern.fastmap=pattern_fastmap;
481 re_compile_fastmap(&pattern);
482 apropos_cmd_helper(gdb_stdout,cmdlist,&pattern,"");
483 }
484 else
485 {
486 regerror(regcomp(&pattern,searchstr,REG_ICASE),NULL,errorbuffer,512);
487 error("Error in regular expression:%s",errorbuffer);
488 }
489 free(pattern_fastmap);
490}
491
c906108c
SS
492
493/* This command really has to deal with two things:
494 * 1) I want documentation on *this string* (usually called by
495 * "help commandname").
496 * 2) I want documentation on *this list* (usually called by
497 * giving a command that requires subcommands. Also called by saying
498 * just "help".)
499 *
500 * I am going to split this into two seperate comamnds, help_cmd and
501 * help_list.
502 */
503
504void
fba45db2 505help_cmd (char *command, struct ui_file *stream)
c906108c
SS
506{
507 struct cmd_list_element *c;
508 extern struct cmd_list_element *cmdlist;
509
510 if (!command)
511 {
512 help_list (cmdlist, "", all_classes, stream);
513 return;
514 }
515
49a5a3a3
GM
516 if (strcmp (command, "all") == 0)
517 {
518 help_all (stream);
519 return;
520 }
521
c906108c
SS
522 c = lookup_cmd (&command, cmdlist, "", 0, 0);
523
524 if (c == 0)
525 return;
526
527 /* There are three cases here.
528 If c->prefixlist is nonzero, we have a prefix command.
529 Print its documentation, then list its subcommands.
c5aa993b 530
c906108c
SS
531 If c->function is nonzero, we really have a command.
532 Print its documentation and return.
c5aa993b 533
c906108c
SS
534 If c->function is zero, we have a class name.
535 Print its documentation (as if it were a command)
536 and then set class to the number of this class
537 so that the commands in the class will be listed. */
538
539 fputs_filtered (c->doc, stream);
540 fputs_filtered ("\n", stream);
541
542 if (c->prefixlist == 0 && c->function.cfunc != NULL)
543 return;
544 fprintf_filtered (stream, "\n");
545
546 /* If this is a prefix command, print it's subcommands */
547 if (c->prefixlist)
548 help_list (*c->prefixlist, c->prefixname, all_commands, stream);
549
550 /* If this is a class name, print all of the commands in the class */
551 if (c->function.cfunc == NULL)
552 help_list (cmdlist, "", c->class, stream);
553
554 if (c->hook)
555 fprintf_filtered (stream, "\nThis command has a hook defined: %s\n",
556 c->hook->name);
557}
558
559/*
560 * Get a specific kind of help on a command list.
561 *
562 * LIST is the list.
563 * CMDTYPE is the prefix to use in the title string.
564 * CLASS is the class with which to list the nodes of this list (see
565 * documentation for help_cmd_list below), As usual, ALL_COMMANDS for
566 * everything, ALL_CLASSES for just classes, and non-negative for only things
567 * in a specific class.
568 * and STREAM is the output stream on which to print things.
569 * If you call this routine with a class >= 0, it recurses.
570 */
571void
fba45db2
KB
572help_list (struct cmd_list_element *list, char *cmdtype,
573 enum command_class class, struct ui_file *stream)
c906108c
SS
574{
575 int len;
576 char *cmdtype1, *cmdtype2;
c5aa993b 577
c906108c
SS
578 /* If CMDTYPE is "foo ", CMDTYPE1 gets " foo" and CMDTYPE2 gets "foo sub" */
579 len = strlen (cmdtype);
580 cmdtype1 = (char *) alloca (len + 1);
581 cmdtype1[0] = 0;
582 cmdtype2 = (char *) alloca (len + 4);
583 cmdtype2[0] = 0;
584 if (len)
585 {
586 cmdtype1[0] = ' ';
587 strncpy (cmdtype1 + 1, cmdtype, len - 1);
588 cmdtype1[len] = 0;
589 strncpy (cmdtype2, cmdtype, len - 1);
590 strcpy (cmdtype2 + len - 1, " sub");
591 }
592
593 if (class == all_classes)
594 fprintf_filtered (stream, "List of classes of %scommands:\n\n", cmdtype2);
595 else
596 fprintf_filtered (stream, "List of %scommands:\n\n", cmdtype2);
597
c5aa993b 598 help_cmd_list (list, class, cmdtype, (int) class >= 0, stream);
c906108c
SS
599
600 if (class == all_classes)
601 fprintf_filtered (stream, "\n\
602Type \"help%s\" followed by a class name for a list of commands in that class.",
c5aa993b 603 cmdtype1);
c906108c
SS
604
605 fprintf_filtered (stream, "\n\
606Type \"help%s\" followed by %scommand name for full documentation.\n\
607Command name abbreviations are allowed if unambiguous.\n",
c5aa993b 608 cmdtype1, cmdtype2);
c906108c 609}
c5aa993b 610
49a5a3a3 611static void
c85871a3 612help_all (struct ui_file *stream)
49a5a3a3
GM
613{
614 struct cmd_list_element *c;
615 extern struct cmd_list_element *cmdlist;
616
617 for (c = cmdlist; c; c = c->next)
618 {
619 if (c->abbrev_flag)
620 continue;
621 /* If this is a prefix command, print it's subcommands */
622 if (c->prefixlist)
623 help_cmd_list (*c->prefixlist, all_commands, c->prefixname, 0, stream);
624
625 /* If this is a class name, print all of the commands in the class */
626 else if (c->function.cfunc == NULL)
627 help_cmd_list (cmdlist, c->class, "", 0, stream);
628 }
629}
630
c906108c
SS
631/* Print only the first line of STR on STREAM. */
632static void
fba45db2 633print_doc_line (struct ui_file *stream, char *str)
c906108c
SS
634{
635 static char *line_buffer = 0;
636 static int line_size;
637 register char *p;
638
639 if (!line_buffer)
640 {
641 line_size = 80;
642 line_buffer = (char *) xmalloc (line_size);
643 }
644
645 p = str;
646 while (*p && *p != '\n' && *p != '.' && *p != ',')
647 p++;
648 if (p - str > line_size - 1)
649 {
650 line_size = p - str + 1;
c5aa993b 651 free ((PTR) line_buffer);
c906108c
SS
652 line_buffer = (char *) xmalloc (line_size);
653 }
654 strncpy (line_buffer, str, p - str);
655 line_buffer[p - str] = '\0';
656 if (islower (line_buffer[0]))
657 line_buffer[0] = toupper (line_buffer[0]);
8b93c638
JM
658#ifdef UI_OUT
659 ui_out_text (uiout, line_buffer);
660#else
c906108c 661 fputs_filtered (line_buffer, stream);
8b93c638 662#endif
c906108c
SS
663}
664
665/*
666 * Implement a help command on command list LIST.
667 * RECURSE should be non-zero if this should be done recursively on
668 * all sublists of LIST.
669 * PREFIX is the prefix to print before each command name.
670 * STREAM is the stream upon which the output should be written.
671 * CLASS should be:
c5aa993b 672 * A non-negative class number to list only commands in that
c906108c 673 * class.
c5aa993b
JM
674 * ALL_COMMANDS to list all commands in list.
675 * ALL_CLASSES to list all classes in list.
c906108c
SS
676 *
677 * Note that RECURSE will be active on *all* sublists, not just the
678 * ones selected by the criteria above (ie. the selection mechanism
679 * is at the low level, not the high-level).
680 */
681void
fba45db2
KB
682help_cmd_list (struct cmd_list_element *list, enum command_class class,
683 char *prefix, int recurse, struct ui_file *stream)
c906108c
SS
684{
685 register struct cmd_list_element *c;
686
687 for (c = list; c; c = c->next)
688 {
689 if (c->abbrev_flag == 0 &&
690 (class == all_commands
c5aa993b
JM
691 || (class == all_classes && c->function.cfunc == NULL)
692 || (class == c->class && c->function.cfunc != NULL)))
c906108c
SS
693 {
694 fprintf_filtered (stream, "%s%s -- ", prefix, c->name);
695 print_doc_line (stream, c->doc);
696 fputs_filtered ("\n", stream);
697 }
698 if (recurse
699 && c->prefixlist != 0
700 && c->abbrev_flag == 0)
701 help_cmd_list (*c->prefixlist, class, c->prefixname, 1, stream);
702 }
703}
c906108c 704\f
c5aa993b 705
c906108c
SS
706/* Search the input clist for 'command'. Return the command if
707 found (or NULL if not), and return the number of commands
708 found in nfound */
709
710static struct cmd_list_element *
fba45db2
KB
711find_cmd (char *command, int len, struct cmd_list_element *clist,
712 int ignore_help_classes, int *nfound)
c906108c
SS
713{
714 struct cmd_list_element *found, *c;
715
c5aa993b 716 found = (struct cmd_list_element *) NULL;
c906108c
SS
717 *nfound = 0;
718 for (c = clist; c; c = c->next)
719 if (!strncmp (command, c->name, len)
c5aa993b 720 && (!ignore_help_classes || c->function.cfunc))
c906108c 721 {
c5aa993b
JM
722 found = c;
723 (*nfound)++;
724 if (c->name[len] == '\0')
725 {
726 *nfound = 1;
727 break;
728 }
c906108c
SS
729 }
730 return found;
731}
732
733/* This routine takes a line of TEXT and a CLIST in which to start the
734 lookup. When it returns it will have incremented the text pointer past
735 the section of text it matched, set *RESULT_LIST to point to the list in
736 which the last word was matched, and will return a pointer to the cmd
737 list element which the text matches. It will return NULL if no match at
738 all was possible. It will return -1 (cast appropriately, ick) if ambigous
739 matches are possible; in this case *RESULT_LIST will be set to point to
740 the list in which there are ambiguous choices (and *TEXT will be set to
741 the ambiguous text string).
742
743 If the located command was an abbreviation, this routine returns the base
744 command of the abbreviation.
745
746 It does no error reporting whatsoever; control will always return
747 to the superior routine.
748
749 In the case of an ambiguous return (-1), *RESULT_LIST will be set to point
750 at the prefix_command (ie. the best match) *or* (special case) will be NULL
751 if no prefix command was ever found. For example, in the case of "info a",
752 "info" matches without ambiguity, but "a" could be "args" or "address", so
753 *RESULT_LIST is set to the cmd_list_element for "info". So in this case
754 RESULT_LIST should not be interpeted as a pointer to the beginning of a
755 list; it simply points to a specific command. In the case of an ambiguous
756 return *TEXT is advanced past the last non-ambiguous prefix (e.g.
757 "info t" can be "info types" or "info target"; upon return *TEXT has been
758 advanced past "info ").
759
760 If RESULT_LIST is NULL, don't set *RESULT_LIST (but don't otherwise
761 affect the operation).
762
763 This routine does *not* modify the text pointed to by TEXT.
c5aa993b 764
c906108c
SS
765 If IGNORE_HELP_CLASSES is nonzero, ignore any command list elements which
766 are actually help classes rather than commands (i.e. the function field of
767 the struct cmd_list_element is NULL). */
768
769struct cmd_list_element *
fba45db2
KB
770lookup_cmd_1 (char **text, struct cmd_list_element *clist,
771 struct cmd_list_element **result_list, int ignore_help_classes)
c906108c
SS
772{
773 char *p, *command;
774 int len, tmp, nfound;
775 struct cmd_list_element *found, *c;
56382845 776 char *line = *text;
c906108c
SS
777
778 while (**text == ' ' || **text == '\t')
779 (*text)++;
780
781 /* Treating underscores as part of command words is important
782 so that "set args_foo()" doesn't get interpreted as
783 "set args _foo()". */
784 for (p = *text;
c5aa993b 785 *p && (isalnum (*p) || *p == '-' || *p == '_' ||
c906108c
SS
786 (tui_version &&
787 (*p == '+' || *p == '<' || *p == '>' || *p == '$')) ||
788 (xdb_commands && (*p == '!' || *p == '/' || *p == '?')));
789 p++)
790 ;
791
792 /* If nothing but whitespace, return 0. */
793 if (p == *text)
794 return 0;
c5aa993b 795
c906108c
SS
796 len = p - *text;
797
798 /* *text and p now bracket the first command word to lookup (and
799 it's length is len). We copy this into a local temporary */
800
801
802 command = (char *) alloca (len + 1);
803 for (tmp = 0; tmp < len; tmp++)
804 {
805 char x = (*text)[tmp];
806 command[tmp] = x;
807 }
808 command[len] = '\0';
809
810 /* Look it up. */
811 found = 0;
812 nfound = 0;
c5aa993b 813 found = find_cmd (command, len, clist, ignore_help_classes, &nfound);
c906108c
SS
814
815 /*
c5aa993b
JM
816 ** We didn't find the command in the entered case, so lower case it
817 ** and search again.
818 */
c906108c
SS
819 if (!found || nfound == 0)
820 {
821 for (tmp = 0; tmp < len; tmp++)
c5aa993b
JM
822 {
823 char x = command[tmp];
824 command[tmp] = isupper (x) ? tolower (x) : x;
825 }
826 found = find_cmd (command, len, clist, ignore_help_classes, &nfound);
c906108c
SS
827 }
828
829 /* If nothing matches, we have a simple failure. */
830 if (nfound == 0)
831 return 0;
832
833 if (nfound > 1)
834 {
835 if (result_list != NULL)
836 /* Will be modified in calling routine
837 if we know what the prefix command is. */
c5aa993b
JM
838 *result_list = 0;
839 return (struct cmd_list_element *) -1; /* Ambiguous. */
c906108c
SS
840 }
841
842 /* We've matched something on this list. Move text pointer forward. */
843
844 *text = p;
845
c906108c 846 if (found->cmd_pointer)
56382845
FN
847 {
848 /* We drop the alias (abbreviation) in favor of the command it is
849 pointing to. If the alias is deprecated, though, we need to
850 warn the user about it before we drop it. Note that while we
851 are warning about the alias, we may also warn about the command
852 itself and we will adjust the appropriate DEPRECATED_WARN_USER
853 flags */
854
855 if (found->flags & DEPRECATED_WARN_USER)
856 deprecated_cmd_warning (&line);
857 found = found->cmd_pointer;
858 }
c906108c
SS
859 /* If we found a prefix command, keep looking. */
860
861 if (found->prefixlist)
862 {
863 c = lookup_cmd_1 (text, *found->prefixlist, result_list,
864 ignore_help_classes);
865 if (!c)
866 {
867 /* Didn't find anything; this is as far as we got. */
868 if (result_list != NULL)
869 *result_list = clist;
870 return found;
871 }
872 else if (c == (struct cmd_list_element *) -1)
873 {
874 /* We've gotten this far properly, but the next step
875 is ambiguous. We need to set the result list to the best
876 we've found (if an inferior hasn't already set it). */
877 if (result_list != NULL)
878 if (!*result_list)
879 /* This used to say *result_list = *found->prefixlist
c5aa993b
JM
880 If that was correct, need to modify the documentation
881 at the top of this function to clarify what is supposed
882 to be going on. */
c906108c
SS
883 *result_list = found;
884 return c;
885 }
886 else
887 {
888 /* We matched! */
889 return c;
890 }
891 }
892 else
893 {
894 if (result_list != NULL)
895 *result_list = clist;
896 return found;
897 }
898}
899
900/* All this hair to move the space to the front of cmdtype */
901
902static void
fba45db2 903undef_cmd_error (char *cmdtype, char *q)
c906108c
SS
904{
905 error ("Undefined %scommand: \"%s\". Try \"help%s%.*s\".",
c5aa993b
JM
906 cmdtype,
907 q,
908 *cmdtype ? " " : "",
909 strlen (cmdtype) - 1,
910 cmdtype);
c906108c
SS
911}
912
913/* Look up the contents of *LINE as a command in the command list LIST.
914 LIST is a chain of struct cmd_list_element's.
915 If it is found, return the struct cmd_list_element for that command
916 and update *LINE to point after the command name, at the first argument.
917 If not found, call error if ALLOW_UNKNOWN is zero
918 otherwise (or if error returns) return zero.
919 Call error if specified command is ambiguous,
920 unless ALLOW_UNKNOWN is negative.
921 CMDTYPE precedes the word "command" in the error message.
922
923 If INGNORE_HELP_CLASSES is nonzero, ignore any command list
924 elements which are actually help classes rather than commands (i.e.
925 the function field of the struct cmd_list_element is 0). */
926
927struct cmd_list_element *
fba45db2
KB
928lookup_cmd (char **line, struct cmd_list_element *list, char *cmdtype,
929 int allow_unknown, int ignore_help_classes)
c906108c
SS
930{
931 struct cmd_list_element *last_list = 0;
932 struct cmd_list_element *c =
c5aa993b 933 lookup_cmd_1 (line, list, &last_list, ignore_help_classes);
c906108c
SS
934#if 0
935 /* This is wrong for complete_command. */
936 char *ptr = (*line) + strlen (*line) - 1;
937
938 /* Clear off trailing whitespace. */
939 while (ptr >= *line && (*ptr == ' ' || *ptr == '\t'))
940 ptr--;
941 *(ptr + 1) = '\0';
942#endif
c5aa993b 943
c906108c
SS
944 if (!c)
945 {
946 if (!allow_unknown)
947 {
948 if (!*line)
949 error ("Lack of needed %scommand", cmdtype);
950 else
951 {
952 char *p = *line, *q;
953
c5aa993b 954 while (isalnum (*p) || *p == '-')
c906108c
SS
955 p++;
956
957 q = (char *) alloca (p - *line + 1);
958 strncpy (q, *line, p - *line);
959 q[p - *line] = '\0';
960 undef_cmd_error (cmdtype, q);
961 }
962 }
963 else
964 return 0;
965 }
966 else if (c == (struct cmd_list_element *) -1)
967 {
968 /* Ambigous. Local values should be off prefixlist or called
c5aa993b 969 values. */
c906108c
SS
970 int local_allow_unknown = (last_list ? last_list->allow_unknown :
971 allow_unknown);
972 char *local_cmdtype = last_list ? last_list->prefixname : cmdtype;
973 struct cmd_list_element *local_list =
c5aa993b
JM
974 (last_list ? *(last_list->prefixlist) : list);
975
c906108c
SS
976 if (local_allow_unknown < 0)
977 {
978 if (last_list)
979 return last_list; /* Found something. */
980 else
981 return 0; /* Found nothing. */
982 }
983 else
984 {
985 /* Report as error. */
986 int amb_len;
987 char ambbuf[100];
988
989 for (amb_len = 0;
990 ((*line)[amb_len] && (*line)[amb_len] != ' '
991 && (*line)[amb_len] != '\t');
992 amb_len++)
993 ;
c5aa993b 994
c906108c
SS
995 ambbuf[0] = 0;
996 for (c = local_list; c; c = c->next)
997 if (!strncmp (*line, c->name, amb_len))
998 {
c5aa993b 999 if (strlen (ambbuf) + strlen (c->name) + 6 < (int) sizeof ambbuf)
c906108c
SS
1000 {
1001 if (strlen (ambbuf))
1002 strcat (ambbuf, ", ");
1003 strcat (ambbuf, c->name);
1004 }
1005 else
1006 {
1007 strcat (ambbuf, "..");
1008 break;
1009 }
1010 }
1011 error ("Ambiguous %scommand \"%s\": %s.", local_cmdtype,
1012 *line, ambbuf);
1013 return 0; /* lint */
1014 }
1015 }
1016 else
1017 {
1018 /* We've got something. It may still not be what the caller
1019 wants (if this command *needs* a subcommand). */
1020 while (**line == ' ' || **line == '\t')
1021 (*line)++;
1022
1023 if (c->prefixlist && **line && !c->allow_unknown)
1024 undef_cmd_error (c->prefixname, *line);
1025
1026 /* Seems to be what he wants. Return it. */
1027 return c;
1028 }
1029 return 0;
1030}
c5aa993b 1031
56382845
FN
1032/* We are here presumably because an alias or command in *TEXT is
1033 deprecated and a warning message should be generated. This function
1034 decodes *TEXT and potentially generates a warning message as outlined
1035 below.
1036
1037 Example for 'set endian big' which has a fictitious alias 'seb'.
1038
1039 If alias wasn't used in *TEXT, and the command is deprecated:
1040 "warning: 'set endian big' is deprecated."
1041
1042 If alias was used, and only the alias is deprecated:
1043 "warning: 'seb' an alias for the command 'set endian big' is deprecated."
1044
1045 If alias was used and command is deprecated (regardless of whether the
1046 alias itself is deprecated:
1047
1048 "warning: 'set endian big' (seb) is deprecated."
1049
1050 After the message has been sent, clear the appropriate flags in the
1051 command and/or the alias so the user is no longer bothered.
1052
1053*/
1054void
1055deprecated_cmd_warning (char **text)
1056{
1057 struct cmd_list_element *alias = NULL;
1058 struct cmd_list_element *prefix_cmd = NULL;
1059 struct cmd_list_element *cmd = NULL;
1060 struct cmd_list_element *c;
1061 char *type;
1062
1063 if (!lookup_cmd_composition (*text, &alias, &prefix_cmd, &cmd))
1064 /* return if text doesn't evaluate to a command */
1065 return;
1066
1067 if (!((alias ? (alias->flags & DEPRECATED_WARN_USER) : 0)
1068 || (cmd->flags & DEPRECATED_WARN_USER) ) )
1069 /* return if nothing is deprecated */
1070 return;
1071
1072 printf_filtered ("Warning:");
1073
1074 if (alias && !(cmd->flags & CMD_DEPRECATED))
1075 printf_filtered (" '%s', an alias for the", alias->name);
1076
1077 printf_filtered (" command '");
1078
1079 if (prefix_cmd)
1080 printf_filtered ("%s", prefix_cmd->prefixname);
1081
1082 printf_filtered ("%s", cmd->name);
1083
1084 if (alias && (cmd->flags & CMD_DEPRECATED))
1085 printf_filtered ("' (%s) is deprecated.\n", alias->name);
1086 else
1087 printf_filtered ("' is deprecated.\n");
1088
1089
1090 /* if it is only the alias that is deprecated, we want to indicate the
1091 new alias, otherwise we'll indicate the new command */
1092
1093 if (alias && !(cmd->flags & CMD_DEPRECATED))
1094 {
1095 if (alias->replacement)
1096 printf_filtered ("Use '%s'.\n\n", alias->replacement);
1097 else
1098 printf_filtered ("No alternative known.\n\n");
1099 }
1100 else
1101 {
1102 if (cmd->replacement)
1103 printf_filtered ("Use '%s'.\n\n", cmd->replacement);
1104 else
1105 printf_filtered ("No alternative known.\n\n");
1106 }
1107
1108 /* We've warned you, now we'll keep quiet */
1109 if (alias)
1110 alias->flags &= ~DEPRECATED_WARN_USER;
1111
1112 cmd->flags &= ~DEPRECATED_WARN_USER;
1113}
1114
1115
1116
1117/* Look up the contents of LINE as a command in the command list 'cmdlist'.
1118 Return 1 on success, 0 on failure.
1119
1120 If LINE refers to an alias, *alias will point to that alias.
1121
1122 If LINE is a postfix command (i.e. one that is preceeded by a prefix
1123 command) set *prefix_cmd.
1124
1125 Set *cmd to point to the command LINE indicates.
1126
1127 If any of *alias, *prefix_cmd, or *cmd cannot be determined or do not
1128 exist, they are NULL when we return.
1129
1130*/
1131int
1132lookup_cmd_composition (char *text,
1133 struct cmd_list_element **alias,
1134 struct cmd_list_element **prefix_cmd,
1135 struct cmd_list_element **cmd)
1136{
1137 char *p, *command;
1138 int len, tmp, nfound;
1139 struct cmd_list_element *cur_list;
1140 struct cmd_list_element *prev_cmd;
1141 *alias = NULL;
1142 *prefix_cmd = NULL;
1143 *cmd = NULL;
1144
1145 cur_list = cmdlist;
1146
1147 while (1)
1148 {
1149 /* Go through as many command lists as we need to
1150 to find the command TEXT refers to. */
1151
1152 prev_cmd = *cmd;
1153
1154 while (*text == ' ' || *text == '\t')
1155 (text)++;
1156
1157 /* Treating underscores as part of command words is important
1158 so that "set args_foo()" doesn't get interpreted as
1159 "set args _foo()". */
1160 for (p = text;
1161 *p && (isalnum (*p) || *p == '-' || *p == '_' ||
1162 (tui_version &&
1163 (*p == '+' || *p == '<' || *p == '>' || *p == '$')) ||
1164 (xdb_commands && (*p == '!' || *p == '/' || *p == '?')));
1165 p++)
1166 ;
1167
1168 /* If nothing but whitespace, return. */
1169 if (p == text)
1170 return 0;
1171
1172 len = p - text;
1173
1174 /* text and p now bracket the first command word to lookup (and
1175 it's length is len). We copy this into a local temporary */
1176
1177 command = (char *) alloca (len + 1);
1178 for (tmp = 0; tmp < len; tmp++)
1179 {
1180 char x = text[tmp];
1181 command[tmp] = x;
1182 }
1183 command[len] = '\0';
1184
1185 /* Look it up. */
1186 *cmd = 0;
1187 nfound = 0;
1188 *cmd = find_cmd (command, len, cur_list, 1, &nfound);
1189
1190 /* We didn't find the command in the entered case, so lower case it
1191 and search again.
1192 */
1193 if (!*cmd || nfound == 0)
1194 {
1195 for (tmp = 0; tmp < len; tmp++)
1196 {
1197 char x = command[tmp];
1198 command[tmp] = isupper (x) ? tolower (x) : x;
1199 }
1200 *cmd = find_cmd (command, len, cur_list, 1, &nfound);
1201 }
1202
1203 if (*cmd == (struct cmd_list_element *) -1)
1204 {
1205 return 0; /* ambiguous */
1206 }
1207
1208 if (*cmd == NULL)
1209 return 0; /* nothing found */
1210 else
1211 {
1212 if ((*cmd)->cmd_pointer)
1213 {
1214 /* cmd was actually an alias, we note that an alias was used
1215 (by assigning *alais) and we set *cmd.
1216 */
1217 *alias = *cmd;
1218 *cmd = (*cmd)->cmd_pointer;
1219 }
1220 *prefix_cmd = prev_cmd;
1221 }
1222 if ((*cmd)->prefixlist)
1223 cur_list = *(*cmd)->prefixlist;
1224 else
1225 return 1;
1226
1227 text = p;
1228 }
1229}
1230
1231
1232
1233
c906108c
SS
1234#if 0
1235/* Look up the contents of *LINE as a command in the command list LIST.
1236 LIST is a chain of struct cmd_list_element's.
1237 If it is found, return the struct cmd_list_element for that command
1238 and update *LINE to point after the command name, at the first argument.
1239 If not found, call error if ALLOW_UNKNOWN is zero
1240 otherwise (or if error returns) return zero.
1241 Call error if specified command is ambiguous,
1242 unless ALLOW_UNKNOWN is negative.
1243 CMDTYPE precedes the word "command" in the error message. */
1244
1245struct cmd_list_element *
fba45db2
KB
1246lookup_cmd (char **line, struct cmd_list_element *list, char *cmdtype,
1247 int allow_unknown)
c906108c
SS
1248{
1249 register char *p;
1250 register struct cmd_list_element *c, *found;
1251 int nfound;
1252 char ambbuf[100];
1253 char *processed_cmd;
1254 int i, cmd_len;
1255
1256 /* Skip leading whitespace. */
1257
1258 while (**line == ' ' || **line == '\t')
1259 (*line)++;
1260
1261 /* Clear out trailing whitespace. */
1262
1263 p = *line + strlen (*line);
1264 while (p != *line && (p[-1] == ' ' || p[-1] == '\t'))
1265 p--;
1266 *p = 0;
1267
1268 /* Find end of command name. */
1269
1270 p = *line;
c5aa993b 1271 while (*p == '-' || isalnum (*p))
c906108c
SS
1272 p++;
1273
1274 /* Look up the command name.
1275 If exact match, keep that.
1276 Otherwise, take command abbreviated, if unique. Note that (in my
1277 opinion) a null string does *not* indicate ambiguity; simply the
1278 end of the argument. */
1279
1280 if (p == *line)
1281 {
1282 if (!allow_unknown)
1283 error ("Lack of needed %scommand", cmdtype);
1284 return 0;
1285 }
c5aa993b 1286
c906108c
SS
1287 /* Copy over to a local buffer, converting to lowercase on the way.
1288 This is in case the command being parsed is a subcommand which
1289 doesn't match anything, and that's ok. We want the original
1290 untouched for the routine of the original command. */
c5aa993b 1291
c906108c
SS
1292 processed_cmd = (char *) alloca (p - *line + 1);
1293 for (cmd_len = 0; cmd_len < p - *line; cmd_len++)
1294 {
1295 char x = (*line)[cmd_len];
c5aa993b
JM
1296 if (isupper (x))
1297 processed_cmd[cmd_len] = tolower (x);
c906108c
SS
1298 else
1299 processed_cmd[cmd_len] = x;
1300 }
1301 processed_cmd[cmd_len] = '\0';
1302
1303 /* Check all possibilities in the current command list. */
1304 found = 0;
1305 nfound = 0;
1306 for (c = list; c; c = c->next)
1307 {
1308 if (!strncmp (processed_cmd, c->name, cmd_len))
1309 {
1310 found = c;
1311 nfound++;
1312 if (c->name[cmd_len] == 0)
1313 {
1314 nfound = 1;
1315 break;
1316 }
1317 }
1318 }
1319
1320 /* Report error for undefined command name. */
1321
1322 if (nfound != 1)
1323 {
1324 if (nfound > 1 && allow_unknown >= 0)
1325 {
1326 ambbuf[0] = 0;
1327 for (c = list; c; c = c->next)
1328 if (!strncmp (processed_cmd, c->name, cmd_len))
1329 {
1330 if (strlen (ambbuf) + strlen (c->name) + 6 < sizeof ambbuf)
1331 {
1332 if (strlen (ambbuf))
1333 strcat (ambbuf, ", ");
1334 strcat (ambbuf, c->name);
1335 }
1336 else
1337 {
1338 strcat (ambbuf, "..");
1339 break;
1340 }
1341 }
1342 error ("Ambiguous %scommand \"%s\": %s.", cmdtype,
1343 processed_cmd, ambbuf);
1344 }
1345 else if (!allow_unknown)
1346 error ("Undefined %scommand: \"%s\".", cmdtype, processed_cmd);
1347 return 0;
1348 }
1349
1350 /* Skip whitespace before the argument. */
1351
c5aa993b
JM
1352 while (*p == ' ' || *p == '\t')
1353 p++;
c906108c
SS
1354 *line = p;
1355
1356 if (found->prefixlist && *p)
1357 {
1358 c = lookup_cmd (line, *found->prefixlist, found->prefixname,
1359 found->allow_unknown);
1360 if (c)
1361 return c;
1362 }
1363
1364 return found;
1365}
1366#endif
1367
1368/* Helper function for SYMBOL_COMPLETION_FUNCTION. */
1369
1370/* Return a vector of char pointers which point to the different
1371 possible completions in LIST of TEXT.
1372
1373 WORD points in the same buffer as TEXT, and completions should be
1374 returned relative to this position. For example, suppose TEXT is "foo"
1375 and we want to complete to "foobar". If WORD is "oo", return
1376 "oobar"; if WORD is "baz/foo", return "baz/foobar". */
1377
1378char **
fba45db2 1379complete_on_cmdlist (struct cmd_list_element *list, char *text, char *word)
c906108c
SS
1380{
1381 struct cmd_list_element *ptr;
1382 char **matchlist;
1383 int sizeof_matchlist;
1384 int matches;
1385 int textlen = strlen (text);
1386
1387 sizeof_matchlist = 10;
1388 matchlist = (char **) xmalloc (sizeof_matchlist * sizeof (char *));
1389 matches = 0;
1390
1391 for (ptr = list; ptr; ptr = ptr->next)
1392 if (!strncmp (ptr->name, text, textlen)
1393 && !ptr->abbrev_flag
1394 && (ptr->function.cfunc
1395 || ptr->prefixlist))
1396 {
1397 if (matches == sizeof_matchlist)
1398 {
1399 sizeof_matchlist *= 2;
c5aa993b 1400 matchlist = (char **) xrealloc ((char *) matchlist,
c906108c
SS
1401 (sizeof_matchlist
1402 * sizeof (char *)));
1403 }
1404
c5aa993b 1405 matchlist[matches] = (char *)
c906108c
SS
1406 xmalloc (strlen (word) + strlen (ptr->name) + 1);
1407 if (word == text)
1408 strcpy (matchlist[matches], ptr->name);
1409 else if (word > text)
1410 {
1411 /* Return some portion of ptr->name. */
1412 strcpy (matchlist[matches], ptr->name + (word - text));
1413 }
1414 else
1415 {
1416 /* Return some of text plus ptr->name. */
1417 strncpy (matchlist[matches], word, text - word);
1418 matchlist[matches][text - word] = '\0';
1419 strcat (matchlist[matches], ptr->name);
1420 }
1421 ++matches;
1422 }
1423
1424 if (matches == 0)
1425 {
c5aa993b 1426 free ((PTR) matchlist);
c906108c
SS
1427 matchlist = 0;
1428 }
1429 else
1430 {
c5aa993b
JM
1431 matchlist = (char **) xrealloc ((char *) matchlist, ((matches + 1)
1432 * sizeof (char *)));
c906108c
SS
1433 matchlist[matches] = (char *) 0;
1434 }
1435
1436 return matchlist;
1437}
1438
1439/* Helper function for SYMBOL_COMPLETION_FUNCTION. */
1440
1441/* Return a vector of char pointers which point to the different
1442 possible completions in CMD of TEXT.
1443
1444 WORD points in the same buffer as TEXT, and completions should be
1445 returned relative to this position. For example, suppose TEXT is "foo"
1446 and we want to complete to "foobar". If WORD is "oo", return
1447 "oobar"; if WORD is "baz/foo", return "baz/foobar". */
1448
1449char **
53904c9e
AC
1450complete_on_enum (const char *enumlist[],
1451 char *text,
1452 char *word)
c906108c
SS
1453{
1454 char **matchlist;
1455 int sizeof_matchlist;
1456 int matches;
1457 int textlen = strlen (text);
1458 int i;
53904c9e 1459 const char *name;
c906108c
SS
1460
1461 sizeof_matchlist = 10;
1462 matchlist = (char **) xmalloc (sizeof_matchlist * sizeof (char *));
1463 matches = 0;
1464
1465 for (i = 0; (name = enumlist[i]) != NULL; i++)
1466 if (strncmp (name, text, textlen) == 0)
1467 {
1468 if (matches == sizeof_matchlist)
1469 {
1470 sizeof_matchlist *= 2;
c5aa993b 1471 matchlist = (char **) xrealloc ((char *) matchlist,
c906108c
SS
1472 (sizeof_matchlist
1473 * sizeof (char *)));
1474 }
1475
c5aa993b 1476 matchlist[matches] = (char *)
c906108c
SS
1477 xmalloc (strlen (word) + strlen (name) + 1);
1478 if (word == text)
1479 strcpy (matchlist[matches], name);
1480 else if (word > text)
1481 {
1482 /* Return some portion of name. */
1483 strcpy (matchlist[matches], name + (word - text));
1484 }
1485 else
1486 {
1487 /* Return some of text plus name. */
1488 strncpy (matchlist[matches], word, text - word);
1489 matchlist[matches][text - word] = '\0';
1490 strcat (matchlist[matches], name);
1491 }
1492 ++matches;
1493 }
1494
1495 if (matches == 0)
1496 {
c5aa993b 1497 free ((PTR) matchlist);
c906108c
SS
1498 matchlist = 0;
1499 }
1500 else
1501 {
c5aa993b
JM
1502 matchlist = (char **) xrealloc ((char *) matchlist, ((matches + 1)
1503 * sizeof (char *)));
c906108c
SS
1504 matchlist[matches] = (char *) 0;
1505 }
1506
1507 return matchlist;
1508}
1509
97c3646f
AC
1510static enum cmd_auto_boolean
1511parse_auto_binary_operation (const char *arg)
1512{
1513 if (arg != NULL && *arg != '\0')
1514 {
1515 int length = strlen (arg);
1516 while (isspace (arg[length - 1]) && length > 0)
1517 length--;
1518 if (strncmp (arg, "on", length) == 0
1519 || strncmp (arg, "1", length) == 0
1520 || strncmp (arg, "yes", length) == 0
1521 || strncmp (arg, "enable", length) == 0)
1522 return CMD_AUTO_BOOLEAN_TRUE;
1523 else if (strncmp (arg, "off", length) == 0
1524 || strncmp (arg, "0", length) == 0
1525 || strncmp (arg, "no", length) == 0
1526 || strncmp (arg, "disable", length) == 0)
1527 return CMD_AUTO_BOOLEAN_FALSE;
1528 else if (strncmp (arg, "auto", length) == 0
1529 || (strncmp (arg, "-1", length) == 0 && length > 1))
1530 return CMD_AUTO_BOOLEAN_AUTO;
1531 }
1532 error ("\"on\", \"off\" or \"auto\" expected.");
1533 return CMD_AUTO_BOOLEAN_AUTO; /* pacify GCC */
1534}
1535
c906108c 1536static int
fba45db2 1537parse_binary_operation (char *arg)
c906108c
SS
1538{
1539 int length;
1540
1541 if (!arg || !*arg)
1542 return 1;
1543
1544 length = strlen (arg);
1545
1546 while (arg[length - 1] == ' ' || arg[length - 1] == '\t')
1547 length--;
1548
97c3646f
AC
1549 if (strncmp (arg, "on", length) == 0
1550 || strncmp (arg, "1", length) == 0
1551 || strncmp (arg, "yes", length) == 0
1552 || strncmp (arg, "enable", length) == 0)
c906108c 1553 return 1;
97c3646f
AC
1554 else if (strncmp (arg, "off", length) == 0
1555 || strncmp (arg, "0", length) == 0
1556 || strncmp (arg, "no", length) == 0
1557 || strncmp (arg, "disable", length) == 0)
c5aa993b 1558 return 0;
c906108c 1559 else
c5aa993b
JM
1560 {
1561 error ("\"on\" or \"off\" expected.");
c906108c 1562 return 0;
c5aa993b 1563 }
c906108c
SS
1564}
1565
1566/* Do a "set" or "show" command. ARG is NULL if no argument, or the text
1567 of the argument, and FROM_TTY is nonzero if this command is being entered
1568 directly by the user (i.e. these are just like any other
1569 command). C is the command list element for the command. */
1570void
fba45db2 1571do_setshow_command (char *arg, int from_tty, struct cmd_list_element *c)
c906108c
SS
1572{
1573 if (c->type == set_cmd)
1574 {
1575 switch (c->var_type)
1576 {
1577 case var_string:
1578 {
1579 char *new;
1580 char *p;
1581 char *q;
1582 int ch;
c5aa993b 1583
c906108c
SS
1584 if (arg == NULL)
1585 arg = "";
1586 new = (char *) xmalloc (strlen (arg) + 2);
c5aa993b
JM
1587 p = arg;
1588 q = new;
c906108c
SS
1589 while ((ch = *p++) != '\000')
1590 {
1591 if (ch == '\\')
1592 {
1593 /* \ at end of argument is used after spaces
1594 so they won't be lost. */
1595 /* This is obsolete now that we no longer strip
1596 trailing whitespace and actually, the backslash
1597 didn't get here in my test, readline or
1598 something did something funky with a backslash
1599 right before a newline. */
1600 if (*p == 0)
1601 break;
1602 ch = parse_escape (&p);
1603 if (ch == 0)
c5aa993b 1604 break; /* C loses */
c906108c
SS
1605 else if (ch > 0)
1606 *q++ = ch;
1607 }
1608 else
1609 *q++ = ch;
1610 }
1611#if 0
1612 if (*(p - 1) != '\\')
1613 *q++ = ' ';
1614#endif
1615 *q++ = '\0';
1616 new = (char *) xrealloc (new, q - new);
c5aa993b
JM
1617 if (*(char **) c->var != NULL)
1618 free (*(char **) c->var);
c906108c
SS
1619 *(char **) c->var = new;
1620 }
1621 break;
1622 case var_string_noescape:
1623 if (arg == NULL)
1624 arg = "";
c5aa993b
JM
1625 if (*(char **) c->var != NULL)
1626 free (*(char **) c->var);
c906108c
SS
1627 *(char **) c->var = savestring (arg, strlen (arg));
1628 break;
1629 case var_filename:
1630 if (arg == NULL)
1631 error_no_arg ("filename to set it to.");
c5aa993b
JM
1632 if (*(char **) c->var != NULL)
1633 free (*(char **) c->var);
1634 *(char **) c->var = tilde_expand (arg);
c906108c
SS
1635 break;
1636 case var_boolean:
1637 *(int *) c->var = parse_binary_operation (arg);
1638 break;
97c3646f
AC
1639 case var_auto_boolean:
1640 *(enum cmd_auto_boolean *) c->var = parse_auto_binary_operation (arg);
1641 break;
c906108c
SS
1642 case var_uinteger:
1643 if (arg == NULL)
1644 error_no_arg ("integer to set it to.");
1645 *(unsigned int *) c->var = parse_and_eval_address (arg);
1646 if (*(unsigned int *) c->var == 0)
1647 *(unsigned int *) c->var = UINT_MAX;
1648 break;
1649 case var_integer:
1650 {
1651 unsigned int val;
1652 if (arg == NULL)
1653 error_no_arg ("integer to set it to.");
1654 val = parse_and_eval_address (arg);
1655 if (val == 0)
1656 *(int *) c->var = INT_MAX;
1657 else if (val >= INT_MAX)
1658 error ("integer %u out of range", val);
1659 else
1660 *(int *) c->var = val;
1661 break;
1662 }
1663 case var_zinteger:
1664 if (arg == NULL)
1665 error_no_arg ("integer to set it to.");
1666 *(int *) c->var = parse_and_eval_address (arg);
1667 break;
1668 case var_enum:
1669 {
1670 int i;
1671 int len;
1672 int nmatches;
53904c9e 1673 const char *match = NULL;
c906108c
SS
1674 char *p;
1675
1676 /* if no argument was supplied, print an informative error message */
1677 if (arg == NULL)
1678 {
1679 char msg[1024];
1680 strcpy (msg, "Requires an argument. Valid arguments are ");
1681 for (i = 0; c->enums[i]; i++)
1682 {
1683 if (i != 0)
1684 strcat (msg, ", ");
1685 strcat (msg, c->enums[i]);
1686 }
1687 strcat (msg, ".");
1688 error (msg);
1689 }
1690
1691 p = strchr (arg, ' ');
c5aa993b 1692
c906108c
SS
1693 if (p)
1694 len = p - arg;
1695 else
1696 len = strlen (arg);
1697
1698 nmatches = 0;
1699 for (i = 0; c->enums[i]; i++)
1700 if (strncmp (arg, c->enums[i], len) == 0)
1701 {
aa891095
AC
1702 if (c->enums[i][len] == '\0')
1703 {
1704 match = c->enums[i];
1705 nmatches = 1;
1706 break; /* exact match. */
1707 }
1708 else
1709 {
1710 match = c->enums[i];
1711 nmatches++;
1712 }
c906108c
SS
1713 }
1714
1715 if (nmatches <= 0)
1716 error ("Undefined item: \"%s\".", arg);
1717
1718 if (nmatches > 1)
1719 error ("Ambiguous item \"%s\".", arg);
1720
53904c9e 1721 *(const char **) c->var = match;
c906108c
SS
1722 }
1723 break;
1724 default:
1725 error ("gdb internal error: bad var_type in do_setshow_command");
1726 }
1727 }
1728 else if (c->type == show_cmd)
1729 {
8b93c638
JM
1730#ifdef UI_OUT
1731 struct cleanup *old_chain;
1732 struct ui_stream *stb;
1733 int quote;
1734
1735 stb = ui_out_stream_new (uiout);
b02eeafb 1736 old_chain = make_cleanup_ui_out_stream_delete (stb);
8b93c638
JM
1737#endif /* UI_OUT */
1738
c906108c
SS
1739 /* Print doc minus "show" at start. */
1740 print_doc_line (gdb_stdout, c->doc + 5);
c5aa993b 1741
8b93c638
JM
1742#ifdef UI_OUT
1743 ui_out_text (uiout, " is ");
1744 ui_out_wrap_hint (uiout, " ");
1745 quote = 0;
1746 switch (c->var_type)
1747 {
1748 case var_string:
1749 {
1750 unsigned char *p;
1751
1752 if (*(unsigned char **) c->var)
1753 fputstr_filtered (*(unsigned char **) c->var, '"', stb->stream);
1754 quote = 1;
1755 }
1756 break;
1757 case var_string_noescape:
1758 case var_filename:
1759 case var_enum:
1760 if (*(char **) c->var)
1761 fputs_filtered (*(char **) c->var, stb->stream);
1762 quote = 1;
1763 break;
1764 case var_boolean:
1765 fputs_filtered (*(int *) c->var ? "on" : "off", stb->stream);
1766 break;
97c3646f 1767 case var_auto_boolean:
d5b61b59 1768 switch (*(enum cmd_auto_boolean*) c->var)
97c3646f
AC
1769 {
1770 case CMD_AUTO_BOOLEAN_TRUE:
1771 fputs_filtered ("on", stb->stream);
1772 break;
1773 case CMD_AUTO_BOOLEAN_FALSE:
1774 fputs_filtered ("off", stb->stream);
1775 break;
1776 case CMD_AUTO_BOOLEAN_AUTO:
1777 fputs_filtered ("auto", stb->stream);
1778 break;
1779 default:
1780 internal_error ("do_setshow_command: invalid var_auto_boolean");
1781 break;
1782 }
1783 break;
8b93c638
JM
1784 case var_uinteger:
1785 if (*(unsigned int *) c->var == UINT_MAX)
1786 {
1787 fputs_filtered ("unlimited", stb->stream);
1788 break;
1789 }
1790 /* else fall through */
1791 case var_zinteger:
1792 fprintf_filtered (stb->stream, "%u", *(unsigned int *) c->var);
1793 break;
1794 case var_integer:
1795 if (*(int *) c->var == INT_MAX)
1796 {
1797 fputs_filtered ("unlimited", stb->stream);
1798 }
1799 else
1800 fprintf_filtered (stb->stream, "%d", *(int *) c->var);
1801 break;
1802
1803 default:
1804 error ("gdb internal error: bad var_type in do_setshow_command");
1805 }
1806 if (quote)
1807 ui_out_text (uiout, "\"");
1808 ui_out_field_stream (uiout, "value", stb);
1809 if (quote)
1810 ui_out_text (uiout, "\"");
1811 ui_out_text (uiout, ".\n");
1812 do_cleanups (old_chain);
1813#else
c906108c
SS
1814 fputs_filtered (" is ", gdb_stdout);
1815 wrap_here (" ");
1816 switch (c->var_type)
1817 {
c5aa993b
JM
1818 case var_string:
1819 {
c5aa993b
JM
1820 fputs_filtered ("\"", gdb_stdout);
1821 if (*(unsigned char **) c->var)
43e526b9 1822 fputstr_filtered (*(unsigned char **) c->var, '"', gdb_stdout);
c5aa993b
JM
1823 fputs_filtered ("\"", gdb_stdout);
1824 }
1825 break;
1826 case var_string_noescape:
1827 case var_filename:
1828 case var_enum:
c906108c 1829 fputs_filtered ("\"", gdb_stdout);
c5aa993b
JM
1830 if (*(char **) c->var)
1831 fputs_filtered (*(char **) c->var, gdb_stdout);
c906108c 1832 fputs_filtered ("\"", gdb_stdout);
c906108c 1833 break;
c5aa993b
JM
1834 case var_boolean:
1835 fputs_filtered (*(int *) c->var ? "on" : "off", gdb_stdout);
1836 break;
97c3646f
AC
1837 case var_auto_boolean:
1838 switch (*(enum cmd_auto_boolean*) c->var)
1839 {
1840 case CMD_AUTO_BOOLEAN_TRUE:
1841 fputs_filtered ("on", gdb_stdout);
1842 break;
1843 case CMD_AUTO_BOOLEAN_FALSE:
1844 fputs_filtered ("off", gdb_stdout);
1845 break;
1846 case CMD_AUTO_BOOLEAN_AUTO:
1847 fputs_filtered ("auto", gdb_stdout);
1848 break;
1849 default:
1850 internal_error ("do_setshow_command: invalid var_auto_boolean");
1851 break;
1852 }
1853 break;
c5aa993b
JM
1854 case var_uinteger:
1855 if (*(unsigned int *) c->var == UINT_MAX)
1856 {
1857 fputs_filtered ("unlimited", gdb_stdout);
1858 break;
1859 }
1860 /* else fall through */
1861 case var_zinteger:
1862 fprintf_filtered (gdb_stdout, "%u", *(unsigned int *) c->var);
1863 break;
1864 case var_integer:
1865 if (*(int *) c->var == INT_MAX)
1866 {
1867 fputs_filtered ("unlimited", gdb_stdout);
1868 }
1869 else
1870 fprintf_filtered (gdb_stdout, "%d", *(int *) c->var);
1871 break;
1872
1873 default:
1874 error ("gdb internal error: bad var_type in do_setshow_command");
c906108c 1875 }
c906108c 1876 fputs_filtered (".\n", gdb_stdout);
8b93c638 1877#endif
c906108c
SS
1878 }
1879 else
1880 error ("gdb internal error: bad cmd_type in do_setshow_command");
1881 (*c->function.sfunc) (NULL, from_tty, c);
96baa820
JM
1882 if (c->type == set_cmd && set_hook)
1883 set_hook (c);
c906108c
SS
1884}
1885
1886/* Show all the settings in a list of show commands. */
1887
1888void
fba45db2 1889cmd_show_list (struct cmd_list_element *list, int from_tty, char *prefix)
c906108c 1890{
8b93c638
JM
1891#ifdef UI_OUT
1892 ui_out_list_begin (uiout, "showlist");
1893#endif
c5aa993b
JM
1894 for (; list != NULL; list = list->next)
1895 {
1896 /* If we find a prefix, run its list, prefixing our output by its
1897 prefix (with "show " skipped). */
8b93c638
JM
1898#ifdef UI_OUT
1899 if (list->prefixlist && !list->abbrev_flag)
1900 {
1901 ui_out_list_begin (uiout, "optionlist");
1902 ui_out_field_string (uiout, "prefix", list->prefixname + 5);
1903 cmd_show_list (*list->prefixlist, from_tty, list->prefixname + 5);
1904 ui_out_list_end (uiout);
1905 }
1906 if (list->type == show_cmd)
1907 {
1908 ui_out_list_begin (uiout, "option");
1909 ui_out_text (uiout, prefix);
1910 ui_out_field_string (uiout, "name", list->name);
1911 ui_out_text (uiout, ": ");
1912 do_setshow_command ((char *) NULL, from_tty, list);
1913 ui_out_list_end (uiout);
1914 }
1915#else
c5aa993b
JM
1916 if (list->prefixlist && !list->abbrev_flag)
1917 cmd_show_list (*list->prefixlist, from_tty, list->prefixname + 5);
1918 if (list->type == show_cmd)
1919 {
1920 fputs_filtered (prefix, gdb_stdout);
1921 fputs_filtered (list->name, gdb_stdout);
1922 fputs_filtered (": ", gdb_stdout);
1923 do_setshow_command ((char *) NULL, from_tty, list);
1924 }
8b93c638 1925#endif
c5aa993b 1926 }
8b93c638
JM
1927#ifdef UI_OUT
1928 ui_out_list_end (uiout);
1929#endif
c906108c
SS
1930}
1931
1932/* ARGSUSED */
1933static void
fba45db2 1934shell_escape (char *arg, int from_tty)
c906108c
SS
1935{
1936#ifdef CANT_FORK
53a5351d
JM
1937 /* If ARG is NULL, they want an inferior shell, but `system' just
1938 reports if the shell is available when passed a NULL arg. */
1939 int rc = system (arg ? arg : "");
1940
1941 if (!arg)
1942 arg = "inferior shell";
1943
1944 if (rc == -1)
1945 {
1946 fprintf_unfiltered (gdb_stderr, "Cannot execute %s: %s\n", arg,
1947 safe_strerror (errno));
1948 gdb_flush (gdb_stderr);
1949 }
1950 else if (rc)
1951 {
1952 fprintf_unfiltered (gdb_stderr, "%s exited with status %d\n", arg, rc);
1953 gdb_flush (gdb_stderr);
1954 }
1955#ifdef __DJGPP__
1956 /* Make sure to return to the directory GDB thinks it is, in case the
1957 shell command we just ran changed it. */
1958 chdir (current_directory);
1959#endif
c906108c
SS
1960#else /* Can fork. */
1961 int rc, status, pid;
1962 char *p, *user_shell;
1963
1964 if ((user_shell = (char *) getenv ("SHELL")) == NULL)
1965 user_shell = "/bin/sh";
1966
1967 /* Get the name of the shell for arg0 */
1968 if ((p = strrchr (user_shell, '/')) == NULL)
1969 p = user_shell;
1970 else
1971 p++; /* Get past '/' */
1972
c5aa993b 1973 if ((pid = fork ()) == 0)
c906108c
SS
1974 {
1975 if (!arg)
1976 execl (user_shell, p, 0);
1977 else
1978 execl (user_shell, p, "-c", arg, 0);
1979
1980 fprintf_unfiltered (gdb_stderr, "Cannot execute %s: %s\n", user_shell,
1981 safe_strerror (errno));
1982 gdb_flush (gdb_stderr);
1983 _exit (0177);
1984 }
1985
1986 if (pid != -1)
1987 while ((rc = wait (&status)) != pid && rc != -1)
1988 ;
1989 else
1990 error ("Fork failed");
1991#endif /* Can fork. */
1992}
1993
1994static void
fba45db2 1995make_command (char *arg, int from_tty)
c906108c
SS
1996{
1997 char *p;
1998
1999 if (arg == 0)
2000 p = "make";
2001 else
2002 {
c5aa993b 2003 p = xmalloc (sizeof ("make ") + strlen (arg));
c906108c 2004 strcpy (p, "make ");
c5aa993b 2005 strcpy (p + sizeof ("make ") - 1, arg);
c906108c 2006 }
c5aa993b 2007
c906108c
SS
2008 shell_escape (p, from_tty);
2009}
2010
2011static void
fba45db2 2012show_user_1 (struct cmd_list_element *c, struct ui_file *stream)
c906108c
SS
2013{
2014 register struct command_line *cmdlines;
2015
2016 cmdlines = c->user_commands;
2017 if (!cmdlines)
2018 return;
2019 fputs_filtered ("User command ", stream);
2020 fputs_filtered (c->name, stream);
2021 fputs_filtered (":\n", stream);
2022
8b93c638
JM
2023#ifdef UI_OUT
2024 print_command_lines (uiout, cmdlines, 1);
2025 fputs_filtered ("\n", stream);
2026#else
c906108c
SS
2027 while (cmdlines)
2028 {
9e086581 2029 print_command_line (cmdlines, 4, stream);
c906108c
SS
2030 cmdlines = cmdlines->next;
2031 }
2032 fputs_filtered ("\n", stream);
8b93c638 2033#endif
c906108c
SS
2034}
2035
2036/* ARGSUSED */
2037static void
fba45db2 2038show_user (char *args, int from_tty)
c906108c
SS
2039{
2040 struct cmd_list_element *c;
2041 extern struct cmd_list_element *cmdlist;
2042
2043 if (args)
2044 {
2045 c = lookup_cmd (&args, cmdlist, "", 0, 1);
2046 if (c->class != class_user)
2047 error ("Not a user command.");
2048 show_user_1 (c, gdb_stdout);
2049 }
2050 else
2051 {
2052 for (c = cmdlist; c; c = c->next)
2053 {
2054 if (c->class == class_user)
2055 show_user_1 (c, gdb_stdout);
2056 }
2057 }
2058}
2059
2060void
fba45db2 2061_initialize_command (void)
c906108c
SS
2062{
2063 add_com ("shell", class_support, shell_escape,
2064 "Execute the rest of the line as a shell command. \n\
2065With no arguments, run an inferior shell.");
2066
acd46a93
AC
2067 /* NOTE: cagney/2000-03-20: Being able to enter ``(gdb) !ls'' would
2068 be a really useful feature. Unfortunatly, the below wont do
2069 this. Instead it adds support for the form ``(gdb) ! ls''
2070 (i.e. the space is required). If the ``!'' command below is
2071 added the complains about no ``!'' command would be replaced by
2072 complains about how the ``!'' command is broken :-) */
c906108c 2073 if (xdb_commands)
c5aa993b 2074 add_com_alias ("!", "shell", class_support, 0);
c906108c
SS
2075
2076 add_com ("make", class_support, make_command,
c5aa993b
JM
2077 "Run the ``make'' program using the rest of the line as arguments.");
2078 add_cmd ("user", no_class, show_user,
c906108c
SS
2079 "Show definitions of user defined commands.\n\
2080Argument is the name of the user defined command.\n\
2081With no argument, show definitions of all user defined commands.", &showlist);
6837a0a2 2082 add_com ("apropos", class_support, apropos_command, "Search for commands matching a REGEXP");
c906108c 2083}
This page took 0.200068 seconds and 4 git commands to generate.