* configure.in: Move termcap determination a later in the file to catch
[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}
65b07ddc 562
dd3b648e 563\f
65b07ddc
DT
564/* Search the input clist for 'command'. Return the command if
565 found (or NULL if not), and return the number of commands
566 found in nfound */
567
568static struct cmd_list_element *
569find_cmd(command, len, clist, ignore_help_classes, nfound)
570 char *command;
571 struct cmd_list_element *clist;
572 int ignore_help_classes;
573 int *nfound;
574{
575 struct cmd_list_element *found, *c;
576
577 found = (struct cmd_list_element *)NULL;
578 *nfound = 0;
579 for (c = clist; c; c = c->next)
580 if (!strncmp (command, c->name, len)
581 && (!ignore_help_classes || c->function.cfunc))
582 {
583 found = c;
584 (*nfound)++;
585 if (c->name[len] == '\0')
586 {
587 *nfound = 1;
588 break;
589 }
590 }
591 return found;
592}
593
311592ff
FF
594/* This routine takes a line of TEXT and a CLIST in which to start the
595 lookup. When it returns it will have incremented the text pointer past
596 the section of text it matched, set *RESULT_LIST to point to the list in
597 which the last word was matched, and will return a pointer to the cmd
598 list element which the text matches. It will return NULL if no match at
599 all was possible. It will return -1 (cast appropriately, ick) if ambigous
600 matches are possible; in this case *RESULT_LIST will be set to point to
601 the list in which there are ambiguous choices (and *TEXT will be set to
602 the ambiguous text string).
dd3b648e 603
a65841d7
JG
604 If the located command was an abbreviation, this routine returns the base
605 command of the abbreviation.
606
dd3b648e
RP
607 It does no error reporting whatsoever; control will always return
608 to the superior routine.
609
311592ff
FF
610 In the case of an ambiguous return (-1), *RESULT_LIST will be set to point
611 at the prefix_command (ie. the best match) *or* (special case) will be NULL
612 if no prefix command was ever found. For example, in the case of "info a",
613 "info" matches without ambiguity, but "a" could be "args" or "address", so
614 *RESULT_LIST is set to the cmd_list_element for "info". So in this case
615 RESULT_LIST should not be interpeted as a pointer to the beginning of a
a32ebcfd
JK
616 list; it simply points to a specific command. In the case of an ambiguous
617 return *TEXT is advanced past the last non-ambiguous prefix (e.g.
618 "info t" can be "info types" or "info target"; upon return *TEXT has been
619 advanced past "info ").
dd3b648e
RP
620
621 If RESULT_LIST is NULL, don't set *RESULT_LIST (but don't otherwise
622 affect the operation).
623
624 This routine does *not* modify the text pointed to by TEXT.
625
311592ff
FF
626 If IGNORE_HELP_CLASSES is nonzero, ignore any command list elements which
627 are actually help classes rather than commands (i.e. the function field of
628 the struct cmd_list_element is NULL). */
dd3b648e
RP
629
630struct cmd_list_element *
631lookup_cmd_1 (text, clist, result_list, ignore_help_classes)
632 char **text;
633 struct cmd_list_element *clist, **result_list;
634 int ignore_help_classes;
635{
636 char *p, *command;
637 int len, tmp, nfound;
638 struct cmd_list_element *found, *c;
639
640 while (**text == ' ' || **text == '\t')
641 (*text)++;
642
643 /* Treating underscores as part of command words is important
644 so that "set args_foo()" doesn't get interpreted as
645 "set args _foo()". */
646 for (p = *text;
65b07ddc
DT
647 *p && (isalnum(*p) || *p == '-' || *p == '_' ||
648 (tui_version &&
649 (*p == '+' || *p == '<' || *p == '>' || *p == '$')) ||
650 (xdb_commands && (*p == '!' || *p == '/' || *p == '?')));
dd3b648e
RP
651 p++)
652 ;
653
654 /* If nothing but whitespace, return 0. */
655 if (p == *text)
656 return 0;
657
658 len = p - *text;
659
660 /* *text and p now bracket the first command word to lookup (and
65b07ddc
DT
661 it's length is len). We copy this into a local temporary */
662
dd3b648e
RP
663
664 command = (char *) alloca (len + 1);
665 for (tmp = 0; tmp < len; tmp++)
666 {
667 char x = (*text)[tmp];
65b07ddc 668 command[tmp] = x;
dd3b648e
RP
669 }
670 command[len] = '\0';
671
672 /* Look it up. */
673 found = 0;
674 nfound = 0;
65b07ddc
DT
675 found = find_cmd(command, len, clist, ignore_help_classes, &nfound);
676
677 /*
678 ** We didn't find the command in the entered case, so lower case it
679 ** and search again.
680 */
681 if (!found || nfound == 0)
682 {
683 for (tmp = 0; tmp < len; tmp++)
684 {
685 char x = command[tmp];
686 command[tmp] = isupper(x) ? tolower(x) : x;
687 }
688 found = find_cmd(command, len, clist, ignore_help_classes, &nfound);
689 }
dd3b648e
RP
690
691 /* If nothing matches, we have a simple failure. */
692 if (nfound == 0)
693 return 0;
694
695 if (nfound > 1)
696 {
697 if (result_list != NULL)
698 /* Will be modified in calling routine
699 if we know what the prefix command is. */
700 *result_list = 0;
701 return (struct cmd_list_element *) -1; /* Ambiguous. */
702 }
703
704 /* We've matched something on this list. Move text pointer forward. */
705
706 *text = p;
a65841d7
JG
707
708 /* If this was an abbreviation, use the base command instead. */
709
710 if (found->cmd_pointer)
711 found = found->cmd_pointer;
712
713 /* If we found a prefix command, keep looking. */
714
dd3b648e
RP
715 if (found->prefixlist)
716 {
717 c = lookup_cmd_1 (text, *found->prefixlist, result_list,
718 ignore_help_classes);
719 if (!c)
720 {
721 /* Didn't find anything; this is as far as we got. */
722 if (result_list != NULL)
723 *result_list = clist;
724 return found;
725 }
726 else if (c == (struct cmd_list_element *) -1)
727 {
36ad9bd5 728 /* We've gotten this far properly, but the next step
dd3b648e
RP
729 is ambiguous. We need to set the result list to the best
730 we've found (if an inferior hasn't already set it). */
731 if (result_list != NULL)
732 if (!*result_list)
733 /* This used to say *result_list = *found->prefixlist
734 If that was correct, need to modify the documentation
735 at the top of this function to clarify what is supposed
736 to be going on. */
737 *result_list = found;
738 return c;
739 }
740 else
741 {
742 /* We matched! */
743 return c;
744 }
745 }
746 else
747 {
748 if (result_list != NULL)
749 *result_list = clist;
750 return found;
751 }
752}
753
81066208
JG
754/* All this hair to move the space to the front of cmdtype */
755
1ab3bf1b 756static void
81066208
JG
757undef_cmd_error (cmdtype, q)
758 char *cmdtype, *q;
759{
760 error ("Undefined %scommand: \"%s\". Try \"help%s%.*s\".",
761 cmdtype,
762 q,
763 *cmdtype? " ": "",
764 strlen(cmdtype)-1,
765 cmdtype);
766}
767
dd3b648e
RP
768/* Look up the contents of *LINE as a command in the command list LIST.
769 LIST is a chain of struct cmd_list_element's.
770 If it is found, return the struct cmd_list_element for that command
771 and update *LINE to point after the command name, at the first argument.
772 If not found, call error if ALLOW_UNKNOWN is zero
773 otherwise (or if error returns) return zero.
774 Call error if specified command is ambiguous,
775 unless ALLOW_UNKNOWN is negative.
776 CMDTYPE precedes the word "command" in the error message.
777
778 If INGNORE_HELP_CLASSES is nonzero, ignore any command list
779 elements which are actually help classes rather than commands (i.e.
780 the function field of the struct cmd_list_element is 0). */
781
782struct cmd_list_element *
783lookup_cmd (line, list, cmdtype, allow_unknown, ignore_help_classes)
784 char **line;
785 struct cmd_list_element *list;
786 char *cmdtype;
787 int allow_unknown;
788 int ignore_help_classes;
789{
790 struct cmd_list_element *last_list = 0;
791 struct cmd_list_element *c =
792 lookup_cmd_1 (line, list, &last_list, ignore_help_classes);
09973223
JK
793#if 0
794 /* This is wrong for complete_command. */
dd3b648e
RP
795 char *ptr = (*line) + strlen (*line) - 1;
796
797 /* Clear off trailing whitespace. */
798 while (ptr >= *line && (*ptr == ' ' || *ptr == '\t'))
799 ptr--;
800 *(ptr + 1) = '\0';
09973223 801#endif
dd3b648e
RP
802
803 if (!c)
804 {
805 if (!allow_unknown)
806 {
807 if (!*line)
808 error ("Lack of needed %scommand", cmdtype);
809 else
810 {
811 char *p = *line, *q;
812
813 while (isalnum(*p) || *p == '-')
814 p++;
815
816 q = (char *) alloca (p - *line + 1);
817 strncpy (q, *line, p - *line);
36ad9bd5 818 q[p - *line] = '\0';
81066208 819 undef_cmd_error (cmdtype, q);
dd3b648e
RP
820 }
821 }
822 else
823 return 0;
824 }
825 else if (c == (struct cmd_list_element *) -1)
826 {
827 /* Ambigous. Local values should be off prefixlist or called
828 values. */
829 int local_allow_unknown = (last_list ? last_list->allow_unknown :
830 allow_unknown);
831 char *local_cmdtype = last_list ? last_list->prefixname : cmdtype;
832 struct cmd_list_element *local_list =
833 (last_list ? *(last_list->prefixlist) : list);
834
835 if (local_allow_unknown < 0)
836 {
837 if (last_list)
838 return last_list; /* Found something. */
839 else
840 return 0; /* Found nothing. */
841 }
842 else
843 {
844 /* Report as error. */
845 int amb_len;
846 char ambbuf[100];
847
848 for (amb_len = 0;
849 ((*line)[amb_len] && (*line)[amb_len] != ' '
850 && (*line)[amb_len] != '\t');
851 amb_len++)
852 ;
853
854 ambbuf[0] = 0;
855 for (c = local_list; c; c = c->next)
856 if (!strncmp (*line, c->name, amb_len))
857 {
858 if (strlen (ambbuf) + strlen (c->name) + 6 < (int)sizeof ambbuf)
859 {
860 if (strlen (ambbuf))
861 strcat (ambbuf, ", ");
862 strcat (ambbuf, c->name);
863 }
864 else
865 {
866 strcat (ambbuf, "..");
867 break;
868 }
869 }
870 error ("Ambiguous %scommand \"%s\": %s.", local_cmdtype,
871 *line, ambbuf);
872 return 0; /* lint */
873 }
874 }
875 else
876 {
877 /* We've got something. It may still not be what the caller
878 wants (if this command *needs* a subcommand). */
879 while (**line == ' ' || **line == '\t')
880 (*line)++;
881
882 if (c->prefixlist && **line && !c->allow_unknown)
81066208 883 undef_cmd_error (c->prefixname, *line);
dd3b648e
RP
884
885 /* Seems to be what he wants. Return it. */
886 return c;
887 }
888 return 0;
889}
890
891#if 0
892/* Look up the contents of *LINE as a command in the command list LIST.
893 LIST is a chain of struct cmd_list_element's.
894 If it is found, return the struct cmd_list_element for that command
895 and update *LINE to point after the command name, at the first argument.
896 If not found, call error if ALLOW_UNKNOWN is zero
897 otherwise (or if error returns) return zero.
898 Call error if specified command is ambiguous,
899 unless ALLOW_UNKNOWN is negative.
900 CMDTYPE precedes the word "command" in the error message. */
901
902struct cmd_list_element *
903lookup_cmd (line, list, cmdtype, allow_unknown)
904 char **line;
905 struct cmd_list_element *list;
906 char *cmdtype;
907 int allow_unknown;
908{
909 register char *p;
910 register struct cmd_list_element *c, *found;
911 int nfound;
912 char ambbuf[100];
913 char *processed_cmd;
914 int i, cmd_len;
915
916 /* Skip leading whitespace. */
917
918 while (**line == ' ' || **line == '\t')
919 (*line)++;
920
921 /* Clear out trailing whitespace. */
922
923 p = *line + strlen (*line);
924 while (p != *line && (p[-1] == ' ' || p[-1] == '\t'))
925 p--;
926 *p = 0;
927
928 /* Find end of command name. */
929
930 p = *line;
5c71cf23 931 while (*p == '-' || isalnum(*p))
dd3b648e
RP
932 p++;
933
934 /* Look up the command name.
935 If exact match, keep that.
936 Otherwise, take command abbreviated, if unique. Note that (in my
937 opinion) a null string does *not* indicate ambiguity; simply the
938 end of the argument. */
939
940 if (p == *line)
941 {
942 if (!allow_unknown)
943 error ("Lack of needed %scommand", cmdtype);
944 return 0;
945 }
946
947 /* Copy over to a local buffer, converting to lowercase on the way.
948 This is in case the command being parsed is a subcommand which
949 doesn't match anything, and that's ok. We want the original
950 untouched for the routine of the original command. */
951
952 processed_cmd = (char *) alloca (p - *line + 1);
953 for (cmd_len = 0; cmd_len < p - *line; cmd_len++)
954 {
955 char x = (*line)[cmd_len];
5c71cf23
PB
956 if (isupper(x))
957 processed_cmd[cmd_len] = tolower(x);
dd3b648e
RP
958 else
959 processed_cmd[cmd_len] = x;
960 }
961 processed_cmd[cmd_len] = '\0';
962
963 /* Check all possibilities in the current command list. */
964 found = 0;
965 nfound = 0;
966 for (c = list; c; c = c->next)
967 {
968 if (!strncmp (processed_cmd, c->name, cmd_len))
969 {
970 found = c;
971 nfound++;
972 if (c->name[cmd_len] == 0)
973 {
974 nfound = 1;
975 break;
976 }
977 }
978 }
979
980 /* Report error for undefined command name. */
981
982 if (nfound != 1)
983 {
984 if (nfound > 1 && allow_unknown >= 0)
985 {
986 ambbuf[0] = 0;
987 for (c = list; c; c = c->next)
988 if (!strncmp (processed_cmd, c->name, cmd_len))
989 {
990 if (strlen (ambbuf) + strlen (c->name) + 6 < sizeof ambbuf)
991 {
992 if (strlen (ambbuf))
993 strcat (ambbuf, ", ");
994 strcat (ambbuf, c->name);
995 }
996 else
997 {
998 strcat (ambbuf, "..");
999 break;
1000 }
1001 }
1002 error ("Ambiguous %scommand \"%s\": %s.", cmdtype,
1003 processed_cmd, ambbuf);
1004 }
1005 else if (!allow_unknown)
1006 error ("Undefined %scommand: \"%s\".", cmdtype, processed_cmd);
1007 return 0;
1008 }
1009
1010 /* Skip whitespace before the argument. */
1011
1012 while (*p == ' ' || *p == '\t') p++;
1013 *line = p;
1014
1015 if (found->prefixlist && *p)
1016 {
1017 c = lookup_cmd (line, *found->prefixlist, found->prefixname,
1018 found->allow_unknown);
1019 if (c)
1020 return c;
1021 }
1022
1023 return found;
1024}
1025#endif
1026
1027/* Helper function for SYMBOL_COMPLETION_FUNCTION. */
1028
1029/* Return a vector of char pointers which point to the different
a32ebcfd
JK
1030 possible completions in LIST of TEXT.
1031
1032 WORD points in the same buffer as TEXT, and completions should be
1033 returned relative to this position. For example, suppose TEXT is "foo"
1034 and we want to complete to "foobar". If WORD is "oo", return
1035 "oobar"; if WORD is "baz/foo", return "baz/foobar". */
dd3b648e
RP
1036
1037char **
a32ebcfd 1038complete_on_cmdlist (list, text, word)
dd3b648e
RP
1039 struct cmd_list_element *list;
1040 char *text;
a32ebcfd 1041 char *word;
dd3b648e
RP
1042{
1043 struct cmd_list_element *ptr;
1044 char **matchlist;
1045 int sizeof_matchlist;
1046 int matches;
1047 int textlen = strlen (text);
1048
1049 sizeof_matchlist = 10;
1050 matchlist = (char **) xmalloc (sizeof_matchlist * sizeof (char *));
1051 matches = 0;
1052
1053 for (ptr = list; ptr; ptr = ptr->next)
1054 if (!strncmp (ptr->name, text, textlen)
1055 && !ptr->abbrev_flag
1ab3bf1b 1056 && (ptr->function.cfunc
dd3b648e
RP
1057 || ptr->prefixlist))
1058 {
1059 if (matches == sizeof_matchlist)
1060 {
1061 sizeof_matchlist *= 2;
1062 matchlist = (char **) xrealloc ((char *)matchlist,
1063 (sizeof_matchlist
1064 * sizeof (char *)));
1065 }
1066
1067 matchlist[matches] = (char *)
a32ebcfd
JK
1068 xmalloc (strlen (word) + strlen (ptr->name) + 1);
1069 if (word == text)
1070 strcpy (matchlist[matches], ptr->name);
1071 else if (word > text)
1072 {
1073 /* Return some portion of ptr->name. */
1074 strcpy (matchlist[matches], ptr->name + (word - text));
1075 }
1076 else
1077 {
1078 /* Return some of text plus ptr->name. */
1079 strncpy (matchlist[matches], word, text - word);
1080 matchlist[matches][text - word] = '\0';
1081 strcat (matchlist[matches], ptr->name);
1082 }
1083 ++matches;
dd3b648e
RP
1084 }
1085
1086 if (matches == 0)
1087 {
be772100 1088 free ((PTR)matchlist);
dd3b648e
RP
1089 matchlist = 0;
1090 }
1091 else
1092 {
1093 matchlist = (char **) xrealloc ((char *)matchlist, ((matches + 1)
1094 * sizeof (char *)));
1095 matchlist[matches] = (char *) 0;
1096 }
1097
1098 return matchlist;
1099}
1100
236274b9
SG
1101/* Helper function for SYMBOL_COMPLETION_FUNCTION. */
1102
1103/* Return a vector of char pointers which point to the different
1104 possible completions in CMD of TEXT.
1105
1106 WORD points in the same buffer as TEXT, and completions should be
1107 returned relative to this position. For example, suppose TEXT is "foo"
1108 and we want to complete to "foobar". If WORD is "oo", return
1109 "oobar"; if WORD is "baz/foo", return "baz/foobar". */
1110
1111char **
1112complete_on_enum (enumlist, text, word)
1113 char **enumlist;
1114 char *text;
1115 char *word;
1116{
1117 char **matchlist;
1118 int sizeof_matchlist;
1119 int matches;
1120 int textlen = strlen (text);
1121 int i;
1122 char *name;
1123
1124 sizeof_matchlist = 10;
1125 matchlist = (char **) xmalloc (sizeof_matchlist * sizeof (char *));
1126 matches = 0;
1127
b52cac6b 1128 for (i = 0; (name = enumlist[i]) != NULL; i++)
236274b9
SG
1129 if (strncmp (name, text, textlen) == 0)
1130 {
1131 if (matches == sizeof_matchlist)
1132 {
1133 sizeof_matchlist *= 2;
1134 matchlist = (char **) xrealloc ((char *)matchlist,
1135 (sizeof_matchlist
1136 * sizeof (char *)));
1137 }
1138
1139 matchlist[matches] = (char *)
1140 xmalloc (strlen (word) + strlen (name) + 1);
1141 if (word == text)
1142 strcpy (matchlist[matches], name);
1143 else if (word > text)
1144 {
1145 /* Return some portion of name. */
1146 strcpy (matchlist[matches], name + (word - text));
1147 }
1148 else
1149 {
1150 /* Return some of text plus name. */
1151 strncpy (matchlist[matches], word, text - word);
1152 matchlist[matches][text - word] = '\0';
1153 strcat (matchlist[matches], name);
1154 }
1155 ++matches;
1156 }
1157
1158 if (matches == 0)
1159 {
1160 free ((PTR)matchlist);
1161 matchlist = 0;
1162 }
1163 else
1164 {
1165 matchlist = (char **) xrealloc ((char *)matchlist, ((matches + 1)
1166 * sizeof (char *)));
1167 matchlist[matches] = (char *) 0;
1168 }
1169
1170 return matchlist;
1171}
1172
dd3b648e
RP
1173static int
1174parse_binary_operation (arg)
1175 char *arg;
1176{
1177 int length;
1178
1179 if (!arg || !*arg)
1180 return 1;
1181
1182 length = strlen (arg);
1183
1184 while (arg[length - 1] == ' ' || arg[length - 1] == '\t')
1185 length--;
1186
1187 if (!strncmp (arg, "on", length)
1188 || !strncmp (arg, "1", length)
1189 || !strncmp (arg, "yes", length))
1190 return 1;
1191 else
1192 if (!strncmp (arg, "off", length)
1193 || !strncmp (arg, "0", length)
1194 || !strncmp (arg, "no", length))
1195 return 0;
1196 else
1197 {
1198 error ("\"on\" or \"off\" expected.");
1199 return 0;
1200 }
1201}
1202
1203/* Do a "set" or "show" command. ARG is NULL if no argument, or the text
1204 of the argument, and FROM_TTY is nonzero if this command is being entered
1205 directly by the user (i.e. these are just like any other
1206 command). C is the command list element for the command. */
1207void
1208do_setshow_command (arg, from_tty, c)
1209 char *arg;
1210 int from_tty;
1211 struct cmd_list_element *c;
1212{
1213 if (c->type == set_cmd)
1214 {
1215 switch (c->var_type)
1216 {
1217 case var_string:
1218 {
1219 char *new;
1220 char *p;
1221 char *q;
1222 int ch;
1223
1224 if (arg == NULL)
1225 arg = "";
1226 new = (char *) xmalloc (strlen (arg) + 2);
1227 p = arg; q = new;
1ab3bf1b 1228 while ((ch = *p++) != '\000')
dd3b648e
RP
1229 {
1230 if (ch == '\\')
1231 {
1232 /* \ at end of argument is used after spaces
1233 so they won't be lost. */
72e35288
JK
1234 /* This is obsolete now that we no longer strip
1235 trailing whitespace and actually, the backslash
1236 didn't get here in my test, readline or
1237 something did something funky with a backslash
1238 right before a newline. */
dd3b648e
RP
1239 if (*p == 0)
1240 break;
1241 ch = parse_escape (&p);
1242 if (ch == 0)
1243 break; /* C loses */
1244 else if (ch > 0)
1245 *q++ = ch;
1246 }
1247 else
1248 *q++ = ch;
1249 }
72e35288 1250#if 0
dd3b648e
RP
1251 if (*(p - 1) != '\\')
1252 *q++ = ' ';
72e35288 1253#endif
dd3b648e
RP
1254 *q++ = '\0';
1255 new = (char *) xrealloc (new, q - new);
1256 if (*(char **)c->var != NULL)
1257 free (*(char **)c->var);
1258 *(char **) c->var = new;
1259 }
1260 break;
1261 case var_string_noescape:
1262 if (arg == NULL)
1263 arg = "";
1264 if (*(char **)c->var != NULL)
1265 free (*(char **)c->var);
1266 *(char **) c->var = savestring (arg, strlen (arg));
1267 break;
1268 case var_filename:
1269 if (arg == NULL)
1270 error_no_arg ("filename to set it to.");
1271 if (*(char **)c->var != NULL)
1272 free (*(char **)c->var);
1273 *(char **)c->var = tilde_expand (arg);
1274 break;
1275 case var_boolean:
1276 *(int *) c->var = parse_binary_operation (arg);
1277 break;
1278 case var_uinteger:
1279 if (arg == NULL)
1280 error_no_arg ("integer to set it to.");
4966c17c
JG
1281 *(unsigned int *) c->var = parse_and_eval_address (arg);
1282 if (*(unsigned int *) c->var == 0)
1283 *(unsigned int *) c->var = UINT_MAX;
dd3b648e 1284 break;
359a097f
JK
1285 case var_integer:
1286 {
1287 unsigned int val;
1288 if (arg == NULL)
1289 error_no_arg ("integer to set it to.");
1290 val = parse_and_eval_address (arg);
1291 if (val == 0)
1292 *(int *) c->var = INT_MAX;
1293 else if (val >= INT_MAX)
1294 error ("integer %u out of range", val);
1295 else
1296 *(int *) c->var = val;
1297 break;
1298 }
dd3b648e
RP
1299 case var_zinteger:
1300 if (arg == NULL)
1301 error_no_arg ("integer to set it to.");
1302 *(int *) c->var = parse_and_eval_address (arg);
1303 break;
236274b9
SG
1304 case var_enum:
1305 {
1306 int i;
1307 int len;
1308 int nmatches;
efaf2b58 1309 char *match = NULL;
236274b9
SG
1310 char *p;
1311
1d7a3fd7
MH
1312 /* if no argument was supplied, print an informative error message */
1313 if (arg == NULL)
1314 {
1315 char msg[1024];
1316 strcpy (msg, "Requires an argument. Valid arguments are ");
1317 for (i = 0; c->enums[i]; i++)
1318 {
1319 if (i != 0)
1320 strcat (msg, ", ");
1321 strcat (msg, c->enums[i]);
1322 }
1323 strcat (msg, ".");
1324 error (msg);
1325 }
236274b9 1326
1d7a3fd7
MH
1327 p = strchr (arg, ' ');
1328
236274b9
SG
1329 if (p)
1330 len = p - arg;
1331 else
1332 len = strlen (arg);
1333
1334 nmatches = 0;
1335 for (i = 0; c->enums[i]; i++)
1336 if (strncmp (arg, c->enums[i], len) == 0)
1337 {
1338 match = c->enums[i];
1339 nmatches++;
1340 }
1341
1342 if (nmatches <= 0)
1343 error ("Undefined item: \"%s\".", arg);
1344
1345 if (nmatches > 1)
1346 error ("Ambiguous item \"%s\".", arg);
1347
1348 *(char **)c->var = match;
1349 }
1350 break;
dd3b648e
RP
1351 default:
1352 error ("gdb internal error: bad var_type in do_setshow_command");
1353 }
1354 }
1355 else if (c->type == show_cmd)
1356 {
1357 /* Print doc minus "show" at start. */
199b2450 1358 print_doc_line (gdb_stdout, c->doc + 5);
dd3b648e 1359
199b2450 1360 fputs_filtered (" is ", gdb_stdout);
dd3b648e
RP
1361 wrap_here (" ");
1362 switch (c->var_type)
1363 {
1364 case var_string:
1365 {
1366 unsigned char *p;
76420d46 1367
199b2450 1368 fputs_filtered ("\"", gdb_stdout);
76420d46
SG
1369 if (*(unsigned char **)c->var)
1370 for (p = *(unsigned char **) c->var; *p != '\0'; p++)
1371 gdb_printchar (*p, gdb_stdout, '"');
199b2450 1372 fputs_filtered ("\"", gdb_stdout);
dd3b648e
RP
1373 }
1374 break;
1375 case var_string_noescape:
1376 case var_filename:
236274b9 1377 case var_enum:
199b2450 1378 fputs_filtered ("\"", gdb_stdout);
76420d46
SG
1379 if (*(char **)c->var)
1380 fputs_filtered (*(char **) c->var, gdb_stdout);
199b2450 1381 fputs_filtered ("\"", gdb_stdout);
dd3b648e
RP
1382 break;
1383 case var_boolean:
199b2450 1384 fputs_filtered (*(int *) c->var ? "on" : "off", gdb_stdout);
dd3b648e
RP
1385 break;
1386 case var_uinteger:
1387 if (*(unsigned int *) c->var == UINT_MAX) {
199b2450 1388 fputs_filtered ("unlimited", gdb_stdout);
dd3b648e
RP
1389 break;
1390 }
1391 /* else fall through */
1392 case var_zinteger:
199b2450 1393 fprintf_filtered (gdb_stdout, "%u", *(unsigned int *) c->var);
dd3b648e 1394 break;
359a097f
JK
1395 case var_integer:
1396 if (*(int *) c->var == INT_MAX)
1397 {
199b2450 1398 fputs_filtered ("unlimited", gdb_stdout);
359a097f
JK
1399 }
1400 else
199b2450 1401 fprintf_filtered (gdb_stdout, "%d", *(int *) c->var);
359a097f
JK
1402 break;
1403
dd3b648e
RP
1404 default:
1405 error ("gdb internal error: bad var_type in do_setshow_command");
1406 }
199b2450 1407 fputs_filtered (".\n", gdb_stdout);
dd3b648e
RP
1408 }
1409 else
1410 error ("gdb internal error: bad cmd_type in do_setshow_command");
1ab3bf1b 1411 (*c->function.sfunc) (NULL, from_tty, c);
dd3b648e
RP
1412}
1413
1414/* Show all the settings in a list of show commands. */
1415
1416void
1417cmd_show_list (list, from_tty, prefix)
1418 struct cmd_list_element *list;
1419 int from_tty;
1420 char *prefix;
1421{
1422 for (; list != NULL; list = list->next) {
1423 /* If we find a prefix, run its list, prefixing our output by its
1424 prefix (with "show " skipped). */
1425 if (list->prefixlist && !list->abbrev_flag)
1426 cmd_show_list (*list->prefixlist, from_tty, list->prefixname + 5);
1427 if (list->type == show_cmd)
1428 {
199b2450
TL
1429 fputs_filtered (prefix, gdb_stdout);
1430 fputs_filtered (list->name, gdb_stdout);
1431 fputs_filtered (": ", gdb_stdout);
dd3b648e
RP
1432 do_setshow_command ((char *)NULL, from_tty, list);
1433 }
1434 }
1435}
1436
e1ce8aa5 1437/* ARGSUSED */
dd3b648e
RP
1438static void
1439shell_escape (arg, from_tty)
1440 char *arg;
1441 int from_tty;
1442{
87237c52
JK
1443#ifdef CANT_FORK
1444 /* FIXME: what about errors (I don't know how GO32 system() handles
1445 them)? */
1446 system (arg);
1447#else /* Can fork. */
dd3b648e
RP
1448 int rc, status, pid;
1449 char *p, *user_shell;
dd3b648e
RP
1450
1451 if ((user_shell = (char *) getenv ("SHELL")) == NULL)
1452 user_shell = "/bin/sh";
1453
1454 /* Get the name of the shell for arg0 */
1ab3bf1b 1455 if ((p = strrchr (user_shell, '/')) == NULL)
dd3b648e
RP
1456 p = user_shell;
1457 else
1458 p++; /* Get past '/' */
1459
1460 if ((pid = fork()) == 0)
1461 {
1462 if (!arg)
1463 execl (user_shell, p, 0);
1464 else
1465 execl (user_shell, p, "-c", arg, 0);
1466
5b3591ab
JK
1467 fprintf_unfiltered (gdb_stderr, "Cannot execute %s: %s\n", user_shell,
1468 safe_strerror (errno));
1469 gdb_flush (gdb_stderr);
1470 _exit (0177);
dd3b648e
RP
1471 }
1472
1473 if (pid != -1)
1474 while ((rc = wait (&status)) != pid && rc != -1)
1475 ;
1476 else
1477 error ("Fork failed");
87237c52 1478#endif /* Can fork. */
dd3b648e
RP
1479}
1480
1481static void
1482make_command (arg, from_tty)
1483 char *arg;
1484 int from_tty;
1485{
1486 char *p;
1487
1488 if (arg == 0)
1489 p = "make";
1490 else
1491 {
1492 p = xmalloc (sizeof("make ") + strlen(arg));
1493 strcpy (p, "make ");
1494 strcpy (p + sizeof("make ")-1, arg);
1495 }
1496
1497 shell_escape (p, from_tty);
1498}
1499
3f2e006b 1500static void
4369a140 1501show_user_1 (c, stream)
3f2e006b 1502 struct cmd_list_element *c;
199b2450 1503 GDB_FILE *stream;
3f2e006b
JG
1504{
1505 register struct command_line *cmdlines;
1506
1507 cmdlines = c->user_commands;
1508 if (!cmdlines)
1509 return;
3021c40d
JG
1510 fputs_filtered ("User command ", stream);
1511 fputs_filtered (c->name, stream);
1512 fputs_filtered (":\n", stream);
59528079 1513
3f2e006b
JG
1514 while (cmdlines)
1515 {
59528079 1516 print_command_line (cmdlines, 4);
3f2e006b
JG
1517 cmdlines = cmdlines->next;
1518 }
1519 fputs_filtered ("\n", stream);
1520}
1521
1522/* ARGSUSED */
1523static void
4369a140 1524show_user (args, from_tty)
3f2e006b
JG
1525 char *args;
1526 int from_tty;
1527{
1528 struct cmd_list_element *c;
1529 extern struct cmd_list_element *cmdlist;
1530
1531 if (args)
1532 {
1533 c = lookup_cmd (&args, cmdlist, "", 0, 1);
1534 if (c->class != class_user)
1535 error ("Not a user command.");
199b2450 1536 show_user_1 (c, gdb_stdout);
3f2e006b
JG
1537 }
1538 else
1539 {
1540 for (c = cmdlist; c; c = c->next)
1541 {
1542 if (c->class == class_user)
199b2450 1543 show_user_1 (c, gdb_stdout);
3f2e006b
JG
1544 }
1545 }
1546}
1547
dd3b648e
RP
1548void
1549_initialize_command ()
1550{
1551 add_com ("shell", class_support, shell_escape,
1552 "Execute the rest of the line as a shell command. \n\
1553With no arguments, run an inferior shell.");
65b07ddc
DT
1554
1555 if (xdb_commands)
1556 add_com_alias("!", "shell", class_support, 0);
1557
dd3b648e
RP
1558 add_com ("make", class_support, make_command,
1559 "Run the ``make'' program using the rest of the line as arguments.");
4369a140
JG
1560 add_cmd ("user", no_class, show_user,
1561 "Show definitions of user defined commands.\n\
3f2e006b 1562Argument is the name of the user defined command.\n\
4369a140 1563With no argument, show definitions of all user defined commands.", &showlist);
dd3b648e 1564}
This page took 0.414716 seconds and 4 git commands to generate.