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