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