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