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