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