Files removed as of the 1999-08-23 snapshot.
[deliverable/binutils-gdb.git] / gdb / command.c
CommitLineData
c906108c
SS
1/* Handle lists of commands, their decoding and documentation, for GDB.
2 Copyright 1986, 1989, 1990, 1991, 1998 Free Software Foundation, Inc.
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"
25#ifdef HAVE_UNISTD_H
26#include <unistd.h>
27#endif
28
29#ifdef HAVE_WAIT_H
c5aa993b 30#include <wait.h>
c906108c 31#else
c5aa993b
JM
32#ifdef HAVE_SYS_WAIT_H
33#include <sys/wait.h>
34#endif
c906108c
SS
35#endif
36
37#include "wait.h"
38
53a5351d
JM
39/* FIXME: this should be auto-configured! */
40#ifdef __MSDOS__
41# define CANT_FORK
42#endif
43
c906108c
SS
44/* Prototypes for local functions */
45
46static void undef_cmd_error PARAMS ((char *, char *));
47
48static void show_user PARAMS ((char *, int));
49
50static void show_user_1 PARAMS ((struct cmd_list_element *, GDB_FILE *));
51
52static void make_command PARAMS ((char *, int));
53
54static void shell_escape PARAMS ((char *, int));
55
56static int parse_binary_operation PARAMS ((char *));
57
58static void print_doc_line PARAMS ((GDB_FILE *, char *));
59
392a587b
JM
60static struct cmd_list_element *find_cmd PARAMS ((char *command,
61 int len,
c5aa993b 62 struct cmd_list_element * clist,
392a587b
JM
63 int ignore_help_classes,
64 int *nfound));
65
c906108c
SS
66void _initialize_command PARAMS ((void));
67
68/* Add element named NAME.
69 CLASS is the top level category into which commands are broken down
70 for "help" purposes.
71 FUN should be the function to execute the command;
72 it will get a character string as argument, with leading
73 and trailing blanks already eliminated.
74
75 DOC is a documentation string for the command.
76 Its first line should be a complete sentence.
77 It should start with ? for a command that is an abbreviation
78 or with * for a command that most users don't need to know about.
79
80 Add this command to command list *LIST.
81
82 Returns a pointer to the added command (not necessarily the head
83 of *LIST). */
84
85struct cmd_list_element *
86add_cmd (name, class, fun, doc, list)
87 char *name;
88 enum command_class class;
89 void (*fun) PARAMS ((char *, int));
90 char *doc;
91 struct cmd_list_element **list;
92{
93 register struct cmd_list_element *c
c5aa993b 94 = (struct cmd_list_element *) xmalloc (sizeof (struct cmd_list_element));
c906108c
SS
95 struct cmd_list_element *p;
96
97 delete_cmd (name, list);
98
99 if (*list == NULL || STRCMP ((*list)->name, name) >= 0)
100 {
101 c->next = *list;
102 *list = c;
103 }
104 else
105 {
106 p = *list;
107 while (p->next && STRCMP (p->next->name, name) <= 0)
c5aa993b
JM
108 {
109 p = p->next;
110 }
c906108c
SS
111 c->next = p->next;
112 p->next = c;
113 }
114
115 c->name = name;
116 c->class = class;
117 c->function.cfunc = fun;
118 c->doc = doc;
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
136/* Same as above, except that the abbrev_flag is set. */
137
c5aa993b 138#if 0 /* Currently unused */
c906108c
SS
139
140struct cmd_list_element *
141add_abbrev_cmd (name, class, fun, doc, list)
142 char *name;
143 enum command_class class;
144 void (*fun) PARAMS ((char *, int));
145 char *doc;
146 struct cmd_list_element **list;
147{
148 register struct cmd_list_element *c
c5aa993b 149 = add_cmd (name, class, fun, doc, list);
c906108c
SS
150
151 c->abbrev_flag = 1;
152 return c;
153}
154
155#endif
156
157struct cmd_list_element *
158add_alias_cmd (name, oldname, class, abbrev_flag, list)
159 char *name;
160 char *oldname;
161 enum command_class class;
162 int abbrev_flag;
163 struct cmd_list_element **list;
164{
165 /* Must do this since lookup_cmd tries to side-effect its first arg */
166 char *copied_name;
167 register struct cmd_list_element *old;
168 register struct cmd_list_element *c;
169 copied_name = (char *) alloca (strlen (oldname) + 1);
170 strcpy (copied_name, oldname);
c5aa993b 171 old = lookup_cmd (&copied_name, *list, "", 1, 1);
c906108c
SS
172
173 if (old == 0)
174 {
175 delete_cmd (name, list);
176 return 0;
177 }
178
179 c = add_cmd (name, class, old->function.cfunc, old->doc, list);
180 c->prefixlist = old->prefixlist;
181 c->prefixname = old->prefixname;
182 c->allow_unknown = old->allow_unknown;
183 c->abbrev_flag = abbrev_flag;
184 c->cmd_pointer = old;
185 return c;
186}
187
188/* Like add_cmd but adds an element for a command prefix:
189 a name that should be followed by a subcommand to be looked up
190 in another command list. PREFIXLIST should be the address
191 of the variable containing that list. */
192
193struct cmd_list_element *
194add_prefix_cmd (name, class, fun, doc, prefixlist, prefixname,
195 allow_unknown, list)
196 char *name;
197 enum command_class class;
198 void (*fun) PARAMS ((char *, int));
199 char *doc;
200 struct cmd_list_element **prefixlist;
201 char *prefixname;
202 int allow_unknown;
203 struct cmd_list_element **list;
204{
205 register struct cmd_list_element *c = add_cmd (name, class, fun, doc, list);
206 c->prefixlist = prefixlist;
207 c->prefixname = prefixname;
208 c->allow_unknown = allow_unknown;
209 return c;
210}
211
212/* Like add_prefix_cmd but sets the abbrev_flag on the new command. */
c5aa993b 213
c906108c
SS
214struct cmd_list_element *
215add_abbrev_prefix_cmd (name, class, fun, doc, prefixlist, prefixname,
216 allow_unknown, list)
217 char *name;
218 enum command_class class;
219 void (*fun) PARAMS ((char *, int));
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 c->abbrev_flag = 1;
231 return c;
232}
233
234/* This is an empty "cfunc". */
235void
236not_just_help_class_command (args, from_tty)
237 char *args;
238 int from_tty;
239{
240}
241
242/* This is an empty "sfunc". */
243static void empty_sfunc PARAMS ((char *, int, struct cmd_list_element *));
244
245static void
246empty_sfunc (args, from_tty, c)
247 char *args;
248 int from_tty;
249 struct cmd_list_element *c;
250{
251}
252
253/* Add element named NAME to command list LIST (the list for set
254 or some sublist thereof).
255 CLASS is as in add_cmd.
256 VAR_TYPE is the kind of thing we are setting.
257 VAR is address of the variable being controlled by this command.
258 DOC is the documentation string. */
259
260struct cmd_list_element *
261add_set_cmd (name, class, var_type, var, doc, list)
262 char *name;
263 enum command_class class;
264 var_types var_type;
265 char *var;
266 char *doc;
267 struct cmd_list_element **list;
268{
269 struct cmd_list_element *c
c5aa993b 270 = add_cmd (name, class, NO_FUNCTION, doc, list);
c906108c
SS
271
272 c->type = set_cmd;
273 c->var_type = var_type;
274 c->var = var;
275 /* This needs to be something besides NO_FUNCTION so that this isn't
276 treated as a help class. */
277 c->function.sfunc = empty_sfunc;
278 return c;
279}
280
281/* Add element named NAME to command list LIST (the list for set
282 or some sublist thereof).
283 CLASS is as in add_cmd.
284 ENUMLIST is a list of strings which may follow NAME.
285 VAR is address of the variable which will contain the matching string
c5aa993b 286 (from ENUMLIST).
c906108c
SS
287 DOC is the documentation string. */
288
289struct cmd_list_element *
290add_set_enum_cmd (name, class, enumlist, var, doc, list)
291 char *name;
292 enum command_class class;
293 char *enumlist[];
294 char *var;
295 char *doc;
296 struct cmd_list_element **list;
297{
298 struct cmd_list_element *c
c5aa993b 299 = add_set_cmd (name, class, var_enum, var, doc, list);
c906108c
SS
300 c->enums = enumlist;
301
302 return c;
303}
304
305/* Where SETCMD has already been added, add the corresponding show
306 command to LIST and return a pointer to the added command (not
307 necessarily the head of LIST). */
308struct cmd_list_element *
309add_show_from_set (setcmd, list)
310 struct cmd_list_element *setcmd;
311 struct cmd_list_element **list;
312{
313 struct cmd_list_element *showcmd =
c5aa993b 314 (struct cmd_list_element *) xmalloc (sizeof (struct cmd_list_element));
c906108c
SS
315 struct cmd_list_element *p;
316
317 memcpy (showcmd, setcmd, sizeof (struct cmd_list_element));
318 delete_cmd (showcmd->name, list);
319 showcmd->type = show_cmd;
c5aa993b 320
c906108c
SS
321 /* Replace "set " at start of docstring with "show ". */
322 if (setcmd->doc[0] == 'S' && setcmd->doc[1] == 'e'
323 && setcmd->doc[2] == 't' && setcmd->doc[3] == ' ')
324 showcmd->doc = concat ("Show ", setcmd->doc + 4, NULL);
325 else
326 fprintf_unfiltered (gdb_stderr, "GDB internal error: Bad docstring for set command\n");
c5aa993b
JM
327
328 if (*list == NULL || STRCMP ((*list)->name, showcmd->name) >= 0)
c906108c
SS
329 {
330 showcmd->next = *list;
331 *list = showcmd;
332 }
333 else
334 {
335 p = *list;
336 while (p->next && STRCMP (p->next->name, showcmd->name) <= 0)
c5aa993b
JM
337 {
338 p = p->next;
339 }
c906108c
SS
340 showcmd->next = p->next;
341 p->next = showcmd;
342 }
343
344 return showcmd;
345}
346
347/* Remove the command named NAME from the command list. */
348
349void
350delete_cmd (name, list)
351 char *name;
352 struct cmd_list_element **list;
353{
354 register struct cmd_list_element *c;
355 struct cmd_list_element *p;
356
357 while (*list && STREQ ((*list)->name, name))
358 {
359 if ((*list)->hookee)
360 (*list)->hookee->hook = 0; /* Hook slips out of its mouth */
361 p = (*list)->next;
c5aa993b 362 free ((PTR) * list);
c906108c
SS
363 *list = p;
364 }
365
366 if (*list)
367 for (c = *list; c->next;)
368 {
369 if (STREQ (c->next->name, name))
370 {
371 if (c->next->hookee)
c5aa993b 372 c->next->hookee->hook = 0; /* hooked cmd gets away. */
c906108c 373 p = c->next->next;
c5aa993b 374 free ((PTR) c->next);
c906108c
SS
375 c->next = p;
376 }
377 else
378 c = c->next;
379 }
380}
381
382/* This command really has to deal with two things:
383 * 1) I want documentation on *this string* (usually called by
384 * "help commandname").
385 * 2) I want documentation on *this list* (usually called by
386 * giving a command that requires subcommands. Also called by saying
387 * just "help".)
388 *
389 * I am going to split this into two seperate comamnds, help_cmd and
390 * help_list.
391 */
392
393void
394help_cmd (command, stream)
395 char *command;
396 GDB_FILE *stream;
397{
398 struct cmd_list_element *c;
399 extern struct cmd_list_element *cmdlist;
400
401 if (!command)
402 {
403 help_list (cmdlist, "", all_classes, stream);
404 return;
405 }
406
407 c = lookup_cmd (&command, cmdlist, "", 0, 0);
408
409 if (c == 0)
410 return;
411
412 /* There are three cases here.
413 If c->prefixlist is nonzero, we have a prefix command.
414 Print its documentation, then list its subcommands.
c5aa993b 415
c906108c
SS
416 If c->function is nonzero, we really have a command.
417 Print its documentation and return.
c5aa993b 418
c906108c
SS
419 If c->function is zero, we have a class name.
420 Print its documentation (as if it were a command)
421 and then set class to the number of this class
422 so that the commands in the class will be listed. */
423
424 fputs_filtered (c->doc, stream);
425 fputs_filtered ("\n", stream);
426
427 if (c->prefixlist == 0 && c->function.cfunc != NULL)
428 return;
429 fprintf_filtered (stream, "\n");
430
431 /* If this is a prefix command, print it's subcommands */
432 if (c->prefixlist)
433 help_list (*c->prefixlist, c->prefixname, all_commands, stream);
434
435 /* If this is a class name, print all of the commands in the class */
436 if (c->function.cfunc == NULL)
437 help_list (cmdlist, "", c->class, stream);
438
439 if (c->hook)
440 fprintf_filtered (stream, "\nThis command has a hook defined: %s\n",
441 c->hook->name);
442}
443
444/*
445 * Get a specific kind of help on a command list.
446 *
447 * LIST is the list.
448 * CMDTYPE is the prefix to use in the title string.
449 * CLASS is the class with which to list the nodes of this list (see
450 * documentation for help_cmd_list below), As usual, ALL_COMMANDS for
451 * everything, ALL_CLASSES for just classes, and non-negative for only things
452 * in a specific class.
453 * and STREAM is the output stream on which to print things.
454 * If you call this routine with a class >= 0, it recurses.
455 */
456void
457help_list (list, cmdtype, class, stream)
458 struct cmd_list_element *list;
459 char *cmdtype;
460 enum command_class class;
461 GDB_FILE *stream;
462{
463 int len;
464 char *cmdtype1, *cmdtype2;
c5aa993b 465
c906108c
SS
466 /* If CMDTYPE is "foo ", CMDTYPE1 gets " foo" and CMDTYPE2 gets "foo sub" */
467 len = strlen (cmdtype);
468 cmdtype1 = (char *) alloca (len + 1);
469 cmdtype1[0] = 0;
470 cmdtype2 = (char *) alloca (len + 4);
471 cmdtype2[0] = 0;
472 if (len)
473 {
474 cmdtype1[0] = ' ';
475 strncpy (cmdtype1 + 1, cmdtype, len - 1);
476 cmdtype1[len] = 0;
477 strncpy (cmdtype2, cmdtype, len - 1);
478 strcpy (cmdtype2 + len - 1, " sub");
479 }
480
481 if (class == all_classes)
482 fprintf_filtered (stream, "List of classes of %scommands:\n\n", cmdtype2);
483 else
484 fprintf_filtered (stream, "List of %scommands:\n\n", cmdtype2);
485
c5aa993b 486 help_cmd_list (list, class, cmdtype, (int) class >= 0, stream);
c906108c
SS
487
488 if (class == all_classes)
489 fprintf_filtered (stream, "\n\
490Type \"help%s\" followed by a class name for a list of commands in that class.",
c5aa993b 491 cmdtype1);
c906108c
SS
492
493 fprintf_filtered (stream, "\n\
494Type \"help%s\" followed by %scommand name for full documentation.\n\
495Command name abbreviations are allowed if unambiguous.\n",
c5aa993b 496 cmdtype1, cmdtype2);
c906108c 497}
c5aa993b 498
c906108c
SS
499/* Print only the first line of STR on STREAM. */
500static void
501print_doc_line (stream, str)
502 GDB_FILE *stream;
503 char *str;
504{
505 static char *line_buffer = 0;
506 static int line_size;
507 register char *p;
508
509 if (!line_buffer)
510 {
511 line_size = 80;
512 line_buffer = (char *) xmalloc (line_size);
513 }
514
515 p = str;
516 while (*p && *p != '\n' && *p != '.' && *p != ',')
517 p++;
518 if (p - str > line_size - 1)
519 {
520 line_size = p - str + 1;
c5aa993b 521 free ((PTR) line_buffer);
c906108c
SS
522 line_buffer = (char *) xmalloc (line_size);
523 }
524 strncpy (line_buffer, str, p - str);
525 line_buffer[p - str] = '\0';
526 if (islower (line_buffer[0]))
527 line_buffer[0] = toupper (line_buffer[0]);
528 fputs_filtered (line_buffer, stream);
529}
530
531/*
532 * Implement a help command on command list LIST.
533 * RECURSE should be non-zero if this should be done recursively on
534 * all sublists of LIST.
535 * PREFIX is the prefix to print before each command name.
536 * STREAM is the stream upon which the output should be written.
537 * CLASS should be:
c5aa993b 538 * A non-negative class number to list only commands in that
c906108c 539 * class.
c5aa993b
JM
540 * ALL_COMMANDS to list all commands in list.
541 * ALL_CLASSES to list all classes in list.
c906108c
SS
542 *
543 * Note that RECURSE will be active on *all* sublists, not just the
544 * ones selected by the criteria above (ie. the selection mechanism
545 * is at the low level, not the high-level).
546 */
547void
548help_cmd_list (list, class, prefix, recurse, stream)
549 struct cmd_list_element *list;
550 enum command_class class;
551 char *prefix;
552 int recurse;
553 GDB_FILE *stream;
554{
555 register struct cmd_list_element *c;
556
557 for (c = list; c; c = c->next)
558 {
559 if (c->abbrev_flag == 0 &&
560 (class == all_commands
c5aa993b
JM
561 || (class == all_classes && c->function.cfunc == NULL)
562 || (class == c->class && c->function.cfunc != NULL)))
c906108c
SS
563 {
564 fprintf_filtered (stream, "%s%s -- ", prefix, c->name);
565 print_doc_line (stream, c->doc);
566 fputs_filtered ("\n", stream);
567 }
568 if (recurse
569 && c->prefixlist != 0
570 && c->abbrev_flag == 0)
571 help_cmd_list (*c->prefixlist, class, c->prefixname, 1, stream);
572 }
573}
c906108c 574\f
c5aa993b 575
c906108c
SS
576/* Search the input clist for 'command'. Return the command if
577 found (or NULL if not), and return the number of commands
578 found in nfound */
579
580static struct cmd_list_element *
c5aa993b 581find_cmd (command, len, clist, ignore_help_classes, nfound)
c906108c 582 char *command;
392a587b 583 int len;
c906108c
SS
584 struct cmd_list_element *clist;
585 int ignore_help_classes;
586 int *nfound;
587{
588 struct cmd_list_element *found, *c;
589
c5aa993b 590 found = (struct cmd_list_element *) NULL;
c906108c
SS
591 *nfound = 0;
592 for (c = clist; c; c = c->next)
593 if (!strncmp (command, c->name, len)
c5aa993b 594 && (!ignore_help_classes || c->function.cfunc))
c906108c 595 {
c5aa993b
JM
596 found = c;
597 (*nfound)++;
598 if (c->name[len] == '\0')
599 {
600 *nfound = 1;
601 break;
602 }
c906108c
SS
603 }
604 return found;
605}
606
607/* This routine takes a line of TEXT and a CLIST in which to start the
608 lookup. When it returns it will have incremented the text pointer past
609 the section of text it matched, set *RESULT_LIST to point to the list in
610 which the last word was matched, and will return a pointer to the cmd
611 list element which the text matches. It will return NULL if no match at
612 all was possible. It will return -1 (cast appropriately, ick) if ambigous
613 matches are possible; in this case *RESULT_LIST will be set to point to
614 the list in which there are ambiguous choices (and *TEXT will be set to
615 the ambiguous text string).
616
617 If the located command was an abbreviation, this routine returns the base
618 command of the abbreviation.
619
620 It does no error reporting whatsoever; control will always return
621 to the superior routine.
622
623 In the case of an ambiguous return (-1), *RESULT_LIST will be set to point
624 at the prefix_command (ie. the best match) *or* (special case) will be NULL
625 if no prefix command was ever found. For example, in the case of "info a",
626 "info" matches without ambiguity, but "a" could be "args" or "address", so
627 *RESULT_LIST is set to the cmd_list_element for "info". So in this case
628 RESULT_LIST should not be interpeted as a pointer to the beginning of a
629 list; it simply points to a specific command. In the case of an ambiguous
630 return *TEXT is advanced past the last non-ambiguous prefix (e.g.
631 "info t" can be "info types" or "info target"; upon return *TEXT has been
632 advanced past "info ").
633
634 If RESULT_LIST is NULL, don't set *RESULT_LIST (but don't otherwise
635 affect the operation).
636
637 This routine does *not* modify the text pointed to by TEXT.
c5aa993b 638
c906108c
SS
639 If IGNORE_HELP_CLASSES is nonzero, ignore any command list elements which
640 are actually help classes rather than commands (i.e. the function field of
641 the struct cmd_list_element is NULL). */
642
643struct cmd_list_element *
644lookup_cmd_1 (text, clist, result_list, ignore_help_classes)
645 char **text;
646 struct cmd_list_element *clist, **result_list;
647 int ignore_help_classes;
648{
649 char *p, *command;
650 int len, tmp, nfound;
651 struct cmd_list_element *found, *c;
652
653 while (**text == ' ' || **text == '\t')
654 (*text)++;
655
656 /* Treating underscores as part of command words is important
657 so that "set args_foo()" doesn't get interpreted as
658 "set args _foo()". */
659 for (p = *text;
c5aa993b 660 *p && (isalnum (*p) || *p == '-' || *p == '_' ||
c906108c
SS
661 (tui_version &&
662 (*p == '+' || *p == '<' || *p == '>' || *p == '$')) ||
663 (xdb_commands && (*p == '!' || *p == '/' || *p == '?')));
664 p++)
665 ;
666
667 /* If nothing but whitespace, return 0. */
668 if (p == *text)
669 return 0;
c5aa993b 670
c906108c
SS
671 len = p - *text;
672
673 /* *text and p now bracket the first command word to lookup (and
674 it's length is len). We copy this into a local temporary */
675
676
677 command = (char *) alloca (len + 1);
678 for (tmp = 0; tmp < len; tmp++)
679 {
680 char x = (*text)[tmp];
681 command[tmp] = x;
682 }
683 command[len] = '\0';
684
685 /* Look it up. */
686 found = 0;
687 nfound = 0;
c5aa993b 688 found = find_cmd (command, len, clist, ignore_help_classes, &nfound);
c906108c
SS
689
690 /*
c5aa993b
JM
691 ** We didn't find the command in the entered case, so lower case it
692 ** and search again.
693 */
c906108c
SS
694 if (!found || nfound == 0)
695 {
696 for (tmp = 0; tmp < len; tmp++)
c5aa993b
JM
697 {
698 char x = command[tmp];
699 command[tmp] = isupper (x) ? tolower (x) : x;
700 }
701 found = find_cmd (command, len, clist, ignore_help_classes, &nfound);
c906108c
SS
702 }
703
704 /* If nothing matches, we have a simple failure. */
705 if (nfound == 0)
706 return 0;
707
708 if (nfound > 1)
709 {
710 if (result_list != NULL)
711 /* Will be modified in calling routine
712 if we know what the prefix command is. */
c5aa993b
JM
713 *result_list = 0;
714 return (struct cmd_list_element *) -1; /* Ambiguous. */
c906108c
SS
715 }
716
717 /* We've matched something on this list. Move text pointer forward. */
718
719 *text = p;
720
721 /* If this was an abbreviation, use the base command instead. */
722
723 if (found->cmd_pointer)
724 found = found->cmd_pointer;
725
726 /* If we found a prefix command, keep looking. */
727
728 if (found->prefixlist)
729 {
730 c = lookup_cmd_1 (text, *found->prefixlist, result_list,
731 ignore_help_classes);
732 if (!c)
733 {
734 /* Didn't find anything; this is as far as we got. */
735 if (result_list != NULL)
736 *result_list = clist;
737 return found;
738 }
739 else if (c == (struct cmd_list_element *) -1)
740 {
741 /* We've gotten this far properly, but the next step
742 is ambiguous. We need to set the result list to the best
743 we've found (if an inferior hasn't already set it). */
744 if (result_list != NULL)
745 if (!*result_list)
746 /* This used to say *result_list = *found->prefixlist
c5aa993b
JM
747 If that was correct, need to modify the documentation
748 at the top of this function to clarify what is supposed
749 to be going on. */
c906108c
SS
750 *result_list = found;
751 return c;
752 }
753 else
754 {
755 /* We matched! */
756 return c;
757 }
758 }
759 else
760 {
761 if (result_list != NULL)
762 *result_list = clist;
763 return found;
764 }
765}
766
767/* All this hair to move the space to the front of cmdtype */
768
769static void
770undef_cmd_error (cmdtype, q)
771 char *cmdtype, *q;
772{
773 error ("Undefined %scommand: \"%s\". Try \"help%s%.*s\".",
c5aa993b
JM
774 cmdtype,
775 q,
776 *cmdtype ? " " : "",
777 strlen (cmdtype) - 1,
778 cmdtype);
c906108c
SS
779}
780
781/* Look up the contents of *LINE as a command in the command list LIST.
782 LIST is a chain of struct cmd_list_element's.
783 If it is found, return the struct cmd_list_element for that command
784 and update *LINE to point after the command name, at the first argument.
785 If not found, call error if ALLOW_UNKNOWN is zero
786 otherwise (or if error returns) return zero.
787 Call error if specified command is ambiguous,
788 unless ALLOW_UNKNOWN is negative.
789 CMDTYPE precedes the word "command" in the error message.
790
791 If INGNORE_HELP_CLASSES is nonzero, ignore any command list
792 elements which are actually help classes rather than commands (i.e.
793 the function field of the struct cmd_list_element is 0). */
794
795struct cmd_list_element *
796lookup_cmd (line, list, cmdtype, allow_unknown, ignore_help_classes)
797 char **line;
798 struct cmd_list_element *list;
799 char *cmdtype;
800 int allow_unknown;
801 int ignore_help_classes;
802{
803 struct cmd_list_element *last_list = 0;
804 struct cmd_list_element *c =
c5aa993b 805 lookup_cmd_1 (line, list, &last_list, ignore_help_classes);
c906108c
SS
806#if 0
807 /* This is wrong for complete_command. */
808 char *ptr = (*line) + strlen (*line) - 1;
809
810 /* Clear off trailing whitespace. */
811 while (ptr >= *line && (*ptr == ' ' || *ptr == '\t'))
812 ptr--;
813 *(ptr + 1) = '\0';
814#endif
c5aa993b 815
c906108c
SS
816 if (!c)
817 {
818 if (!allow_unknown)
819 {
820 if (!*line)
821 error ("Lack of needed %scommand", cmdtype);
822 else
823 {
824 char *p = *line, *q;
825
c5aa993b 826 while (isalnum (*p) || *p == '-')
c906108c
SS
827 p++;
828
829 q = (char *) alloca (p - *line + 1);
830 strncpy (q, *line, p - *line);
831 q[p - *line] = '\0';
832 undef_cmd_error (cmdtype, q);
833 }
834 }
835 else
836 return 0;
837 }
838 else if (c == (struct cmd_list_element *) -1)
839 {
840 /* Ambigous. Local values should be off prefixlist or called
c5aa993b 841 values. */
c906108c
SS
842 int local_allow_unknown = (last_list ? last_list->allow_unknown :
843 allow_unknown);
844 char *local_cmdtype = last_list ? last_list->prefixname : cmdtype;
845 struct cmd_list_element *local_list =
c5aa993b
JM
846 (last_list ? *(last_list->prefixlist) : list);
847
c906108c
SS
848 if (local_allow_unknown < 0)
849 {
850 if (last_list)
851 return last_list; /* Found something. */
852 else
853 return 0; /* Found nothing. */
854 }
855 else
856 {
857 /* Report as error. */
858 int amb_len;
859 char ambbuf[100];
860
861 for (amb_len = 0;
862 ((*line)[amb_len] && (*line)[amb_len] != ' '
863 && (*line)[amb_len] != '\t');
864 amb_len++)
865 ;
c5aa993b 866
c906108c
SS
867 ambbuf[0] = 0;
868 for (c = local_list; c; c = c->next)
869 if (!strncmp (*line, c->name, amb_len))
870 {
c5aa993b 871 if (strlen (ambbuf) + strlen (c->name) + 6 < (int) sizeof ambbuf)
c906108c
SS
872 {
873 if (strlen (ambbuf))
874 strcat (ambbuf, ", ");
875 strcat (ambbuf, c->name);
876 }
877 else
878 {
879 strcat (ambbuf, "..");
880 break;
881 }
882 }
883 error ("Ambiguous %scommand \"%s\": %s.", local_cmdtype,
884 *line, ambbuf);
885 return 0; /* lint */
886 }
887 }
888 else
889 {
890 /* We've got something. It may still not be what the caller
891 wants (if this command *needs* a subcommand). */
892 while (**line == ' ' || **line == '\t')
893 (*line)++;
894
895 if (c->prefixlist && **line && !c->allow_unknown)
896 undef_cmd_error (c->prefixname, *line);
897
898 /* Seems to be what he wants. Return it. */
899 return c;
900 }
901 return 0;
902}
c5aa993b 903
c906108c
SS
904#if 0
905/* Look up the contents of *LINE as a command in the command list LIST.
906 LIST is a chain of struct cmd_list_element's.
907 If it is found, return the struct cmd_list_element for that command
908 and update *LINE to point after the command name, at the first argument.
909 If not found, call error if ALLOW_UNKNOWN is zero
910 otherwise (or if error returns) return zero.
911 Call error if specified command is ambiguous,
912 unless ALLOW_UNKNOWN is negative.
913 CMDTYPE precedes the word "command" in the error message. */
914
915struct cmd_list_element *
916lookup_cmd (line, list, cmdtype, allow_unknown)
917 char **line;
918 struct cmd_list_element *list;
919 char *cmdtype;
920 int allow_unknown;
921{
922 register char *p;
923 register struct cmd_list_element *c, *found;
924 int nfound;
925 char ambbuf[100];
926 char *processed_cmd;
927 int i, cmd_len;
928
929 /* Skip leading whitespace. */
930
931 while (**line == ' ' || **line == '\t')
932 (*line)++;
933
934 /* Clear out trailing whitespace. */
935
936 p = *line + strlen (*line);
937 while (p != *line && (p[-1] == ' ' || p[-1] == '\t'))
938 p--;
939 *p = 0;
940
941 /* Find end of command name. */
942
943 p = *line;
c5aa993b 944 while (*p == '-' || isalnum (*p))
c906108c
SS
945 p++;
946
947 /* Look up the command name.
948 If exact match, keep that.
949 Otherwise, take command abbreviated, if unique. Note that (in my
950 opinion) a null string does *not* indicate ambiguity; simply the
951 end of the argument. */
952
953 if (p == *line)
954 {
955 if (!allow_unknown)
956 error ("Lack of needed %scommand", cmdtype);
957 return 0;
958 }
c5aa993b 959
c906108c
SS
960 /* Copy over to a local buffer, converting to lowercase on the way.
961 This is in case the command being parsed is a subcommand which
962 doesn't match anything, and that's ok. We want the original
963 untouched for the routine of the original command. */
c5aa993b 964
c906108c
SS
965 processed_cmd = (char *) alloca (p - *line + 1);
966 for (cmd_len = 0; cmd_len < p - *line; cmd_len++)
967 {
968 char x = (*line)[cmd_len];
c5aa993b
JM
969 if (isupper (x))
970 processed_cmd[cmd_len] = tolower (x);
c906108c
SS
971 else
972 processed_cmd[cmd_len] = x;
973 }
974 processed_cmd[cmd_len] = '\0';
975
976 /* Check all possibilities in the current command list. */
977 found = 0;
978 nfound = 0;
979 for (c = list; c; c = c->next)
980 {
981 if (!strncmp (processed_cmd, c->name, cmd_len))
982 {
983 found = c;
984 nfound++;
985 if (c->name[cmd_len] == 0)
986 {
987 nfound = 1;
988 break;
989 }
990 }
991 }
992
993 /* Report error for undefined command name. */
994
995 if (nfound != 1)
996 {
997 if (nfound > 1 && allow_unknown >= 0)
998 {
999 ambbuf[0] = 0;
1000 for (c = list; c; c = c->next)
1001 if (!strncmp (processed_cmd, c->name, cmd_len))
1002 {
1003 if (strlen (ambbuf) + strlen (c->name) + 6 < sizeof ambbuf)
1004 {
1005 if (strlen (ambbuf))
1006 strcat (ambbuf, ", ");
1007 strcat (ambbuf, c->name);
1008 }
1009 else
1010 {
1011 strcat (ambbuf, "..");
1012 break;
1013 }
1014 }
1015 error ("Ambiguous %scommand \"%s\": %s.", cmdtype,
1016 processed_cmd, ambbuf);
1017 }
1018 else if (!allow_unknown)
1019 error ("Undefined %scommand: \"%s\".", cmdtype, processed_cmd);
1020 return 0;
1021 }
1022
1023 /* Skip whitespace before the argument. */
1024
c5aa993b
JM
1025 while (*p == ' ' || *p == '\t')
1026 p++;
c906108c
SS
1027 *line = p;
1028
1029 if (found->prefixlist && *p)
1030 {
1031 c = lookup_cmd (line, *found->prefixlist, found->prefixname,
1032 found->allow_unknown);
1033 if (c)
1034 return c;
1035 }
1036
1037 return found;
1038}
1039#endif
1040
1041/* Helper function for SYMBOL_COMPLETION_FUNCTION. */
1042
1043/* Return a vector of char pointers which point to the different
1044 possible completions in LIST of TEXT.
1045
1046 WORD points in the same buffer as TEXT, and completions should be
1047 returned relative to this position. For example, suppose TEXT is "foo"
1048 and we want to complete to "foobar". If WORD is "oo", return
1049 "oobar"; if WORD is "baz/foo", return "baz/foobar". */
1050
1051char **
1052complete_on_cmdlist (list, text, word)
1053 struct cmd_list_element *list;
1054 char *text;
1055 char *word;
1056{
1057 struct cmd_list_element *ptr;
1058 char **matchlist;
1059 int sizeof_matchlist;
1060 int matches;
1061 int textlen = strlen (text);
1062
1063 sizeof_matchlist = 10;
1064 matchlist = (char **) xmalloc (sizeof_matchlist * sizeof (char *));
1065 matches = 0;
1066
1067 for (ptr = list; ptr; ptr = ptr->next)
1068 if (!strncmp (ptr->name, text, textlen)
1069 && !ptr->abbrev_flag
1070 && (ptr->function.cfunc
1071 || ptr->prefixlist))
1072 {
1073 if (matches == sizeof_matchlist)
1074 {
1075 sizeof_matchlist *= 2;
c5aa993b 1076 matchlist = (char **) xrealloc ((char *) matchlist,
c906108c
SS
1077 (sizeof_matchlist
1078 * sizeof (char *)));
1079 }
1080
c5aa993b 1081 matchlist[matches] = (char *)
c906108c
SS
1082 xmalloc (strlen (word) + strlen (ptr->name) + 1);
1083 if (word == text)
1084 strcpy (matchlist[matches], ptr->name);
1085 else if (word > text)
1086 {
1087 /* Return some portion of ptr->name. */
1088 strcpy (matchlist[matches], ptr->name + (word - text));
1089 }
1090 else
1091 {
1092 /* Return some of text plus ptr->name. */
1093 strncpy (matchlist[matches], word, text - word);
1094 matchlist[matches][text - word] = '\0';
1095 strcat (matchlist[matches], ptr->name);
1096 }
1097 ++matches;
1098 }
1099
1100 if (matches == 0)
1101 {
c5aa993b 1102 free ((PTR) matchlist);
c906108c
SS
1103 matchlist = 0;
1104 }
1105 else
1106 {
c5aa993b
JM
1107 matchlist = (char **) xrealloc ((char *) matchlist, ((matches + 1)
1108 * sizeof (char *)));
c906108c
SS
1109 matchlist[matches] = (char *) 0;
1110 }
1111
1112 return matchlist;
1113}
1114
1115/* Helper function for SYMBOL_COMPLETION_FUNCTION. */
1116
1117/* Return a vector of char pointers which point to the different
1118 possible completions in CMD of TEXT.
1119
1120 WORD points in the same buffer as TEXT, and completions should be
1121 returned relative to this position. For example, suppose TEXT is "foo"
1122 and we want to complete to "foobar". If WORD is "oo", return
1123 "oobar"; if WORD is "baz/foo", return "baz/foobar". */
1124
1125char **
1126complete_on_enum (enumlist, text, word)
1127 char **enumlist;
1128 char *text;
1129 char *word;
1130{
1131 char **matchlist;
1132 int sizeof_matchlist;
1133 int matches;
1134 int textlen = strlen (text);
1135 int i;
1136 char *name;
1137
1138 sizeof_matchlist = 10;
1139 matchlist = (char **) xmalloc (sizeof_matchlist * sizeof (char *));
1140 matches = 0;
1141
1142 for (i = 0; (name = enumlist[i]) != NULL; i++)
1143 if (strncmp (name, text, textlen) == 0)
1144 {
1145 if (matches == sizeof_matchlist)
1146 {
1147 sizeof_matchlist *= 2;
c5aa993b 1148 matchlist = (char **) xrealloc ((char *) matchlist,
c906108c
SS
1149 (sizeof_matchlist
1150 * sizeof (char *)));
1151 }
1152
c5aa993b 1153 matchlist[matches] = (char *)
c906108c
SS
1154 xmalloc (strlen (word) + strlen (name) + 1);
1155 if (word == text)
1156 strcpy (matchlist[matches], name);
1157 else if (word > text)
1158 {
1159 /* Return some portion of name. */
1160 strcpy (matchlist[matches], name + (word - text));
1161 }
1162 else
1163 {
1164 /* Return some of text plus name. */
1165 strncpy (matchlist[matches], word, text - word);
1166 matchlist[matches][text - word] = '\0';
1167 strcat (matchlist[matches], name);
1168 }
1169 ++matches;
1170 }
1171
1172 if (matches == 0)
1173 {
c5aa993b 1174 free ((PTR) matchlist);
c906108c
SS
1175 matchlist = 0;
1176 }
1177 else
1178 {
c5aa993b
JM
1179 matchlist = (char **) xrealloc ((char *) matchlist, ((matches + 1)
1180 * sizeof (char *)));
c906108c
SS
1181 matchlist[matches] = (char *) 0;
1182 }
1183
1184 return matchlist;
1185}
1186
1187static int
1188parse_binary_operation (arg)
1189 char *arg;
1190{
1191 int length;
1192
1193 if (!arg || !*arg)
1194 return 1;
1195
1196 length = strlen (arg);
1197
1198 while (arg[length - 1] == ' ' || arg[length - 1] == '\t')
1199 length--;
1200
1201 if (!strncmp (arg, "on", length)
1202 || !strncmp (arg, "1", length)
1203 || !strncmp (arg, "yes", length))
1204 return 1;
c5aa993b
JM
1205 else if (!strncmp (arg, "off", length)
1206 || !strncmp (arg, "0", length)
1207 || !strncmp (arg, "no", length))
1208 return 0;
c906108c 1209 else
c5aa993b
JM
1210 {
1211 error ("\"on\" or \"off\" expected.");
c906108c 1212 return 0;
c5aa993b 1213 }
c906108c
SS
1214}
1215
1216/* Do a "set" or "show" command. ARG is NULL if no argument, or the text
1217 of the argument, and FROM_TTY is nonzero if this command is being entered
1218 directly by the user (i.e. these are just like any other
1219 command). C is the command list element for the command. */
1220void
1221do_setshow_command (arg, from_tty, c)
1222 char *arg;
1223 int from_tty;
1224 struct cmd_list_element *c;
1225{
1226 if (c->type == set_cmd)
1227 {
1228 switch (c->var_type)
1229 {
1230 case var_string:
1231 {
1232 char *new;
1233 char *p;
1234 char *q;
1235 int ch;
c5aa993b 1236
c906108c
SS
1237 if (arg == NULL)
1238 arg = "";
1239 new = (char *) xmalloc (strlen (arg) + 2);
c5aa993b
JM
1240 p = arg;
1241 q = new;
c906108c
SS
1242 while ((ch = *p++) != '\000')
1243 {
1244 if (ch == '\\')
1245 {
1246 /* \ at end of argument is used after spaces
1247 so they won't be lost. */
1248 /* This is obsolete now that we no longer strip
1249 trailing whitespace and actually, the backslash
1250 didn't get here in my test, readline or
1251 something did something funky with a backslash
1252 right before a newline. */
1253 if (*p == 0)
1254 break;
1255 ch = parse_escape (&p);
1256 if (ch == 0)
c5aa993b 1257 break; /* C loses */
c906108c
SS
1258 else if (ch > 0)
1259 *q++ = ch;
1260 }
1261 else
1262 *q++ = ch;
1263 }
1264#if 0
1265 if (*(p - 1) != '\\')
1266 *q++ = ' ';
1267#endif
1268 *q++ = '\0';
1269 new = (char *) xrealloc (new, q - new);
c5aa993b
JM
1270 if (*(char **) c->var != NULL)
1271 free (*(char **) c->var);
c906108c
SS
1272 *(char **) c->var = new;
1273 }
1274 break;
1275 case var_string_noescape:
1276 if (arg == NULL)
1277 arg = "";
c5aa993b
JM
1278 if (*(char **) c->var != NULL)
1279 free (*(char **) c->var);
c906108c
SS
1280 *(char **) c->var = savestring (arg, strlen (arg));
1281 break;
1282 case var_filename:
1283 if (arg == NULL)
1284 error_no_arg ("filename to set it to.");
c5aa993b
JM
1285 if (*(char **) c->var != NULL)
1286 free (*(char **) c->var);
1287 *(char **) c->var = tilde_expand (arg);
c906108c
SS
1288 break;
1289 case var_boolean:
1290 *(int *) c->var = parse_binary_operation (arg);
1291 break;
1292 case var_uinteger:
1293 if (arg == NULL)
1294 error_no_arg ("integer to set it to.");
1295 *(unsigned int *) c->var = parse_and_eval_address (arg);
1296 if (*(unsigned int *) c->var == 0)
1297 *(unsigned int *) c->var = UINT_MAX;
1298 break;
1299 case var_integer:
1300 {
1301 unsigned int val;
1302 if (arg == NULL)
1303 error_no_arg ("integer to set it to.");
1304 val = parse_and_eval_address (arg);
1305 if (val == 0)
1306 *(int *) c->var = INT_MAX;
1307 else if (val >= INT_MAX)
1308 error ("integer %u out of range", val);
1309 else
1310 *(int *) c->var = val;
1311 break;
1312 }
1313 case var_zinteger:
1314 if (arg == NULL)
1315 error_no_arg ("integer to set it to.");
1316 *(int *) c->var = parse_and_eval_address (arg);
1317 break;
1318 case var_enum:
1319 {
1320 int i;
1321 int len;
1322 int nmatches;
1323 char *match = NULL;
1324 char *p;
1325
1326 /* if no argument was supplied, print an informative error message */
1327 if (arg == NULL)
1328 {
1329 char msg[1024];
1330 strcpy (msg, "Requires an argument. Valid arguments are ");
1331 for (i = 0; c->enums[i]; i++)
1332 {
1333 if (i != 0)
1334 strcat (msg, ", ");
1335 strcat (msg, c->enums[i]);
1336 }
1337 strcat (msg, ".");
1338 error (msg);
1339 }
1340
1341 p = strchr (arg, ' ');
c5aa993b 1342
c906108c
SS
1343 if (p)
1344 len = p - arg;
1345 else
1346 len = strlen (arg);
1347
1348 nmatches = 0;
1349 for (i = 0; c->enums[i]; i++)
1350 if (strncmp (arg, c->enums[i], len) == 0)
1351 {
1352 match = c->enums[i];
1353 nmatches++;
1354 }
1355
1356 if (nmatches <= 0)
1357 error ("Undefined item: \"%s\".", arg);
1358
1359 if (nmatches > 1)
1360 error ("Ambiguous item \"%s\".", arg);
1361
c5aa993b 1362 *(char **) c->var = match;
c906108c
SS
1363 }
1364 break;
1365 default:
1366 error ("gdb internal error: bad var_type in do_setshow_command");
1367 }
1368 }
1369 else if (c->type == show_cmd)
1370 {
1371 /* Print doc minus "show" at start. */
1372 print_doc_line (gdb_stdout, c->doc + 5);
c5aa993b 1373
c906108c
SS
1374 fputs_filtered (" is ", gdb_stdout);
1375 wrap_here (" ");
1376 switch (c->var_type)
1377 {
c5aa993b
JM
1378 case var_string:
1379 {
1380 unsigned char *p;
c906108c 1381
c5aa993b
JM
1382 fputs_filtered ("\"", gdb_stdout);
1383 if (*(unsigned char **) c->var)
43e526b9 1384 fputstr_filtered (*(unsigned char **) c->var, '"', gdb_stdout);
c5aa993b
JM
1385 fputs_filtered ("\"", gdb_stdout);
1386 }
1387 break;
1388 case var_string_noescape:
1389 case var_filename:
1390 case var_enum:
c906108c 1391 fputs_filtered ("\"", gdb_stdout);
c5aa993b
JM
1392 if (*(char **) c->var)
1393 fputs_filtered (*(char **) c->var, gdb_stdout);
c906108c 1394 fputs_filtered ("\"", gdb_stdout);
c906108c 1395 break;
c5aa993b
JM
1396 case var_boolean:
1397 fputs_filtered (*(int *) c->var ? "on" : "off", gdb_stdout);
1398 break;
1399 case var_uinteger:
1400 if (*(unsigned int *) c->var == UINT_MAX)
1401 {
1402 fputs_filtered ("unlimited", gdb_stdout);
1403 break;
1404 }
1405 /* else fall through */
1406 case var_zinteger:
1407 fprintf_filtered (gdb_stdout, "%u", *(unsigned int *) c->var);
1408 break;
1409 case var_integer:
1410 if (*(int *) c->var == INT_MAX)
1411 {
1412 fputs_filtered ("unlimited", gdb_stdout);
1413 }
1414 else
1415 fprintf_filtered (gdb_stdout, "%d", *(int *) c->var);
1416 break;
1417
1418 default:
1419 error ("gdb internal error: bad var_type in do_setshow_command");
c906108c 1420 }
c906108c
SS
1421 fputs_filtered (".\n", gdb_stdout);
1422 }
1423 else
1424 error ("gdb internal error: bad cmd_type in do_setshow_command");
1425 (*c->function.sfunc) (NULL, from_tty, c);
96baa820
JM
1426 if (c->type == set_cmd && set_hook)
1427 set_hook (c);
c906108c
SS
1428}
1429
1430/* Show all the settings in a list of show commands. */
1431
1432void
1433cmd_show_list (list, from_tty, prefix)
1434 struct cmd_list_element *list;
1435 int from_tty;
1436 char *prefix;
1437{
c5aa993b
JM
1438 for (; list != NULL; list = list->next)
1439 {
1440 /* If we find a prefix, run its list, prefixing our output by its
1441 prefix (with "show " skipped). */
1442 if (list->prefixlist && !list->abbrev_flag)
1443 cmd_show_list (*list->prefixlist, from_tty, list->prefixname + 5);
1444 if (list->type == show_cmd)
1445 {
1446 fputs_filtered (prefix, gdb_stdout);
1447 fputs_filtered (list->name, gdb_stdout);
1448 fputs_filtered (": ", gdb_stdout);
1449 do_setshow_command ((char *) NULL, from_tty, list);
1450 }
1451 }
c906108c
SS
1452}
1453
1454/* ARGSUSED */
1455static void
1456shell_escape (arg, from_tty)
1457 char *arg;
1458 int from_tty;
1459{
1460#ifdef CANT_FORK
53a5351d
JM
1461 /* If ARG is NULL, they want an inferior shell, but `system' just
1462 reports if the shell is available when passed a NULL arg. */
1463 int rc = system (arg ? arg : "");
1464
1465 if (!arg)
1466 arg = "inferior shell";
1467
1468 if (rc == -1)
1469 {
1470 fprintf_unfiltered (gdb_stderr, "Cannot execute %s: %s\n", arg,
1471 safe_strerror (errno));
1472 gdb_flush (gdb_stderr);
1473 }
1474 else if (rc)
1475 {
1476 fprintf_unfiltered (gdb_stderr, "%s exited with status %d\n", arg, rc);
1477 gdb_flush (gdb_stderr);
1478 }
1479#ifdef __DJGPP__
1480 /* Make sure to return to the directory GDB thinks it is, in case the
1481 shell command we just ran changed it. */
1482 chdir (current_directory);
1483#endif
c906108c
SS
1484#else /* Can fork. */
1485 int rc, status, pid;
1486 char *p, *user_shell;
1487
1488 if ((user_shell = (char *) getenv ("SHELL")) == NULL)
1489 user_shell = "/bin/sh";
1490
1491 /* Get the name of the shell for arg0 */
1492 if ((p = strrchr (user_shell, '/')) == NULL)
1493 p = user_shell;
1494 else
1495 p++; /* Get past '/' */
1496
c5aa993b 1497 if ((pid = fork ()) == 0)
c906108c
SS
1498 {
1499 if (!arg)
1500 execl (user_shell, p, 0);
1501 else
1502 execl (user_shell, p, "-c", arg, 0);
1503
1504 fprintf_unfiltered (gdb_stderr, "Cannot execute %s: %s\n", user_shell,
1505 safe_strerror (errno));
1506 gdb_flush (gdb_stderr);
1507 _exit (0177);
1508 }
1509
1510 if (pid != -1)
1511 while ((rc = wait (&status)) != pid && rc != -1)
1512 ;
1513 else
1514 error ("Fork failed");
1515#endif /* Can fork. */
1516}
1517
1518static void
1519make_command (arg, from_tty)
1520 char *arg;
1521 int from_tty;
1522{
1523 char *p;
1524
1525 if (arg == 0)
1526 p = "make";
1527 else
1528 {
c5aa993b 1529 p = xmalloc (sizeof ("make ") + strlen (arg));
c906108c 1530 strcpy (p, "make ");
c5aa993b 1531 strcpy (p + sizeof ("make ") - 1, arg);
c906108c 1532 }
c5aa993b 1533
c906108c
SS
1534 shell_escape (p, from_tty);
1535}
1536
1537static void
1538show_user_1 (c, stream)
1539 struct cmd_list_element *c;
1540 GDB_FILE *stream;
1541{
1542 register struct command_line *cmdlines;
1543
1544 cmdlines = c->user_commands;
1545 if (!cmdlines)
1546 return;
1547 fputs_filtered ("User command ", stream);
1548 fputs_filtered (c->name, stream);
1549 fputs_filtered (":\n", stream);
1550
1551 while (cmdlines)
1552 {
9e086581 1553 print_command_line (cmdlines, 4, stream);
c906108c
SS
1554 cmdlines = cmdlines->next;
1555 }
1556 fputs_filtered ("\n", stream);
1557}
1558
1559/* ARGSUSED */
1560static void
1561show_user (args, from_tty)
1562 char *args;
1563 int from_tty;
1564{
1565 struct cmd_list_element *c;
1566 extern struct cmd_list_element *cmdlist;
1567
1568 if (args)
1569 {
1570 c = lookup_cmd (&args, cmdlist, "", 0, 1);
1571 if (c->class != class_user)
1572 error ("Not a user command.");
1573 show_user_1 (c, gdb_stdout);
1574 }
1575 else
1576 {
1577 for (c = cmdlist; c; c = c->next)
1578 {
1579 if (c->class == class_user)
1580 show_user_1 (c, gdb_stdout);
1581 }
1582 }
1583}
1584
1585void
1586_initialize_command ()
1587{
1588 add_com ("shell", class_support, shell_escape,
1589 "Execute the rest of the line as a shell command. \n\
1590With no arguments, run an inferior shell.");
1591
1592 if (xdb_commands)
c5aa993b 1593 add_com_alias ("!", "shell", class_support, 0);
c906108c
SS
1594
1595 add_com ("make", class_support, make_command,
c5aa993b
JM
1596 "Run the ``make'' program using the rest of the line as arguments.");
1597 add_cmd ("user", no_class, show_user,
c906108c
SS
1598 "Show definitions of user defined commands.\n\
1599Argument is the name of the user defined command.\n\
1600With no argument, show definitions of all user defined commands.", &showlist);
1601}
This page took 0.092558 seconds and 4 git commands to generate.