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