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