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