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