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