* utils.c (prompt_for_continue): Call readline, not gdb_readline.
[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);
687 char *ptr = (*line) + strlen (*line) - 1;
688
689 /* Clear off trailing whitespace. */
690 while (ptr >= *line && (*ptr == ' ' || *ptr == '\t'))
691 ptr--;
692 *(ptr + 1) = '\0';
693
694 if (!c)
695 {
696 if (!allow_unknown)
697 {
698 if (!*line)
699 error ("Lack of needed %scommand", cmdtype);
700 else
701 {
702 char *p = *line, *q;
703
704 while (isalnum(*p) || *p == '-')
705 p++;
706
707 q = (char *) alloca (p - *line + 1);
708 strncpy (q, *line, p - *line);
709 q[p-*line] = '\0';
81066208 710 undef_cmd_error (cmdtype, q);
dd3b648e
RP
711 }
712 }
713 else
714 return 0;
715 }
716 else if (c == (struct cmd_list_element *) -1)
717 {
718 /* Ambigous. Local values should be off prefixlist or called
719 values. */
720 int local_allow_unknown = (last_list ? last_list->allow_unknown :
721 allow_unknown);
722 char *local_cmdtype = last_list ? last_list->prefixname : cmdtype;
723 struct cmd_list_element *local_list =
724 (last_list ? *(last_list->prefixlist) : list);
725
726 if (local_allow_unknown < 0)
727 {
728 if (last_list)
729 return last_list; /* Found something. */
730 else
731 return 0; /* Found nothing. */
732 }
733 else
734 {
735 /* Report as error. */
736 int amb_len;
737 char ambbuf[100];
738
739 for (amb_len = 0;
740 ((*line)[amb_len] && (*line)[amb_len] != ' '
741 && (*line)[amb_len] != '\t');
742 amb_len++)
743 ;
744
745 ambbuf[0] = 0;
746 for (c = local_list; c; c = c->next)
747 if (!strncmp (*line, c->name, amb_len))
748 {
749 if (strlen (ambbuf) + strlen (c->name) + 6 < (int)sizeof ambbuf)
750 {
751 if (strlen (ambbuf))
752 strcat (ambbuf, ", ");
753 strcat (ambbuf, c->name);
754 }
755 else
756 {
757 strcat (ambbuf, "..");
758 break;
759 }
760 }
761 error ("Ambiguous %scommand \"%s\": %s.", local_cmdtype,
762 *line, ambbuf);
763 return 0; /* lint */
764 }
765 }
766 else
767 {
768 /* We've got something. It may still not be what the caller
769 wants (if this command *needs* a subcommand). */
770 while (**line == ' ' || **line == '\t')
771 (*line)++;
772
773 if (c->prefixlist && **line && !c->allow_unknown)
81066208 774 undef_cmd_error (c->prefixname, *line);
dd3b648e
RP
775
776 /* Seems to be what he wants. Return it. */
777 return c;
778 }
779 return 0;
780}
781
782#if 0
783/* Look up the contents of *LINE as a command in the command list LIST.
784 LIST is a chain of struct cmd_list_element's.
785 If it is found, return the struct cmd_list_element for that command
786 and update *LINE to point after the command name, at the first argument.
787 If not found, call error if ALLOW_UNKNOWN is zero
788 otherwise (or if error returns) return zero.
789 Call error if specified command is ambiguous,
790 unless ALLOW_UNKNOWN is negative.
791 CMDTYPE precedes the word "command" in the error message. */
792
793struct cmd_list_element *
794lookup_cmd (line, list, cmdtype, allow_unknown)
795 char **line;
796 struct cmd_list_element *list;
797 char *cmdtype;
798 int allow_unknown;
799{
800 register char *p;
801 register struct cmd_list_element *c, *found;
802 int nfound;
803 char ambbuf[100];
804 char *processed_cmd;
805 int i, cmd_len;
806
807 /* Skip leading whitespace. */
808
809 while (**line == ' ' || **line == '\t')
810 (*line)++;
811
812 /* Clear out trailing whitespace. */
813
814 p = *line + strlen (*line);
815 while (p != *line && (p[-1] == ' ' || p[-1] == '\t'))
816 p--;
817 *p = 0;
818
819 /* Find end of command name. */
820
821 p = *line;
5c71cf23 822 while (*p == '-' || isalnum(*p))
dd3b648e
RP
823 p++;
824
825 /* Look up the command name.
826 If exact match, keep that.
827 Otherwise, take command abbreviated, if unique. Note that (in my
828 opinion) a null string does *not* indicate ambiguity; simply the
829 end of the argument. */
830
831 if (p == *line)
832 {
833 if (!allow_unknown)
834 error ("Lack of needed %scommand", cmdtype);
835 return 0;
836 }
837
838 /* Copy over to a local buffer, converting to lowercase on the way.
839 This is in case the command being parsed is a subcommand which
840 doesn't match anything, and that's ok. We want the original
841 untouched for the routine of the original command. */
842
843 processed_cmd = (char *) alloca (p - *line + 1);
844 for (cmd_len = 0; cmd_len < p - *line; cmd_len++)
845 {
846 char x = (*line)[cmd_len];
5c71cf23
PB
847 if (isupper(x))
848 processed_cmd[cmd_len] = tolower(x);
dd3b648e
RP
849 else
850 processed_cmd[cmd_len] = x;
851 }
852 processed_cmd[cmd_len] = '\0';
853
854 /* Check all possibilities in the current command list. */
855 found = 0;
856 nfound = 0;
857 for (c = list; c; c = c->next)
858 {
859 if (!strncmp (processed_cmd, c->name, cmd_len))
860 {
861 found = c;
862 nfound++;
863 if (c->name[cmd_len] == 0)
864 {
865 nfound = 1;
866 break;
867 }
868 }
869 }
870
871 /* Report error for undefined command name. */
872
873 if (nfound != 1)
874 {
875 if (nfound > 1 && allow_unknown >= 0)
876 {
877 ambbuf[0] = 0;
878 for (c = list; c; c = c->next)
879 if (!strncmp (processed_cmd, c->name, cmd_len))
880 {
881 if (strlen (ambbuf) + strlen (c->name) + 6 < sizeof ambbuf)
882 {
883 if (strlen (ambbuf))
884 strcat (ambbuf, ", ");
885 strcat (ambbuf, c->name);
886 }
887 else
888 {
889 strcat (ambbuf, "..");
890 break;
891 }
892 }
893 error ("Ambiguous %scommand \"%s\": %s.", cmdtype,
894 processed_cmd, ambbuf);
895 }
896 else if (!allow_unknown)
897 error ("Undefined %scommand: \"%s\".", cmdtype, processed_cmd);
898 return 0;
899 }
900
901 /* Skip whitespace before the argument. */
902
903 while (*p == ' ' || *p == '\t') p++;
904 *line = p;
905
906 if (found->prefixlist && *p)
907 {
908 c = lookup_cmd (line, *found->prefixlist, found->prefixname,
909 found->allow_unknown);
910 if (c)
911 return c;
912 }
913
914 return found;
915}
916#endif
917
918/* Helper function for SYMBOL_COMPLETION_FUNCTION. */
919
920/* Return a vector of char pointers which point to the different
a32ebcfd
JK
921 possible completions in LIST of TEXT.
922
923 WORD points in the same buffer as TEXT, and completions should be
924 returned relative to this position. For example, suppose TEXT is "foo"
925 and we want to complete to "foobar". If WORD is "oo", return
926 "oobar"; if WORD is "baz/foo", return "baz/foobar". */
dd3b648e
RP
927
928char **
a32ebcfd 929complete_on_cmdlist (list, text, word)
dd3b648e
RP
930 struct cmd_list_element *list;
931 char *text;
a32ebcfd 932 char *word;
dd3b648e
RP
933{
934 struct cmd_list_element *ptr;
935 char **matchlist;
936 int sizeof_matchlist;
937 int matches;
938 int textlen = strlen (text);
939
940 sizeof_matchlist = 10;
941 matchlist = (char **) xmalloc (sizeof_matchlist * sizeof (char *));
942 matches = 0;
943
944 for (ptr = list; ptr; ptr = ptr->next)
945 if (!strncmp (ptr->name, text, textlen)
946 && !ptr->abbrev_flag
1ab3bf1b 947 && (ptr->function.cfunc
dd3b648e
RP
948 || ptr->prefixlist))
949 {
950 if (matches == sizeof_matchlist)
951 {
952 sizeof_matchlist *= 2;
953 matchlist = (char **) xrealloc ((char *)matchlist,
954 (sizeof_matchlist
955 * sizeof (char *)));
956 }
957
958 matchlist[matches] = (char *)
a32ebcfd
JK
959 xmalloc (strlen (word) + strlen (ptr->name) + 1);
960 if (word == text)
961 strcpy (matchlist[matches], ptr->name);
962 else if (word > text)
963 {
964 /* Return some portion of ptr->name. */
965 strcpy (matchlist[matches], ptr->name + (word - text));
966 }
967 else
968 {
969 /* Return some of text plus ptr->name. */
970 strncpy (matchlist[matches], word, text - word);
971 matchlist[matches][text - word] = '\0';
972 strcat (matchlist[matches], ptr->name);
973 }
974 ++matches;
dd3b648e
RP
975 }
976
977 if (matches == 0)
978 {
be772100 979 free ((PTR)matchlist);
dd3b648e
RP
980 matchlist = 0;
981 }
982 else
983 {
984 matchlist = (char **) xrealloc ((char *)matchlist, ((matches + 1)
985 * sizeof (char *)));
986 matchlist[matches] = (char *) 0;
987 }
988
989 return matchlist;
990}
991
992static int
993parse_binary_operation (arg)
994 char *arg;
995{
996 int length;
997
998 if (!arg || !*arg)
999 return 1;
1000
1001 length = strlen (arg);
1002
1003 while (arg[length - 1] == ' ' || arg[length - 1] == '\t')
1004 length--;
1005
1006 if (!strncmp (arg, "on", length)
1007 || !strncmp (arg, "1", length)
1008 || !strncmp (arg, "yes", length))
1009 return 1;
1010 else
1011 if (!strncmp (arg, "off", length)
1012 || !strncmp (arg, "0", length)
1013 || !strncmp (arg, "no", length))
1014 return 0;
1015 else
1016 {
1017 error ("\"on\" or \"off\" expected.");
1018 return 0;
1019 }
1020}
1021
1022/* Do a "set" or "show" command. ARG is NULL if no argument, or the text
1023 of the argument, and FROM_TTY is nonzero if this command is being entered
1024 directly by the user (i.e. these are just like any other
1025 command). C is the command list element for the command. */
1026void
1027do_setshow_command (arg, from_tty, c)
1028 char *arg;
1029 int from_tty;
1030 struct cmd_list_element *c;
1031{
1032 if (c->type == set_cmd)
1033 {
1034 switch (c->var_type)
1035 {
1036 case var_string:
1037 {
1038 char *new;
1039 char *p;
1040 char *q;
1041 int ch;
1042
1043 if (arg == NULL)
1044 arg = "";
1045 new = (char *) xmalloc (strlen (arg) + 2);
1046 p = arg; q = new;
1ab3bf1b 1047 while ((ch = *p++) != '\000')
dd3b648e
RP
1048 {
1049 if (ch == '\\')
1050 {
1051 /* \ at end of argument is used after spaces
1052 so they won't be lost. */
1053 if (*p == 0)
1054 break;
1055 ch = parse_escape (&p);
1056 if (ch == 0)
1057 break; /* C loses */
1058 else if (ch > 0)
1059 *q++ = ch;
1060 }
1061 else
1062 *q++ = ch;
1063 }
1064 if (*(p - 1) != '\\')
1065 *q++ = ' ';
1066 *q++ = '\0';
1067 new = (char *) xrealloc (new, q - new);
1068 if (*(char **)c->var != NULL)
1069 free (*(char **)c->var);
1070 *(char **) c->var = new;
1071 }
1072 break;
1073 case var_string_noescape:
1074 if (arg == NULL)
1075 arg = "";
1076 if (*(char **)c->var != NULL)
1077 free (*(char **)c->var);
1078 *(char **) c->var = savestring (arg, strlen (arg));
1079 break;
1080 case var_filename:
1081 if (arg == NULL)
1082 error_no_arg ("filename to set it to.");
1083 if (*(char **)c->var != NULL)
1084 free (*(char **)c->var);
1085 *(char **)c->var = tilde_expand (arg);
1086 break;
1087 case var_boolean:
1088 *(int *) c->var = parse_binary_operation (arg);
1089 break;
1090 case var_uinteger:
1091 if (arg == NULL)
1092 error_no_arg ("integer to set it to.");
4966c17c
JG
1093 *(unsigned int *) c->var = parse_and_eval_address (arg);
1094 if (*(unsigned int *) c->var == 0)
1095 *(unsigned int *) c->var = UINT_MAX;
dd3b648e 1096 break;
359a097f
JK
1097 case var_integer:
1098 {
1099 unsigned int val;
1100 if (arg == NULL)
1101 error_no_arg ("integer to set it to.");
1102 val = parse_and_eval_address (arg);
1103 if (val == 0)
1104 *(int *) c->var = INT_MAX;
1105 else if (val >= INT_MAX)
1106 error ("integer %u out of range", val);
1107 else
1108 *(int *) c->var = val;
1109 break;
1110 }
dd3b648e
RP
1111 case var_zinteger:
1112 if (arg == NULL)
1113 error_no_arg ("integer to set it to.");
1114 *(int *) c->var = parse_and_eval_address (arg);
1115 break;
1116 default:
1117 error ("gdb internal error: bad var_type in do_setshow_command");
1118 }
1119 }
1120 else if (c->type == show_cmd)
1121 {
1122 /* Print doc minus "show" at start. */
199b2450 1123 print_doc_line (gdb_stdout, c->doc + 5);
dd3b648e 1124
199b2450 1125 fputs_filtered (" is ", gdb_stdout);
dd3b648e
RP
1126 wrap_here (" ");
1127 switch (c->var_type)
1128 {
1129 case var_string:
1130 {
1131 unsigned char *p;
199b2450 1132 fputs_filtered ("\"", gdb_stdout);
dd3b648e 1133 for (p = *(unsigned char **) c->var; *p != '\0'; p++)
199b2450
TL
1134 gdb_printchar (*p, gdb_stdout, '"');
1135 fputs_filtered ("\"", gdb_stdout);
dd3b648e
RP
1136 }
1137 break;
1138 case var_string_noescape:
1139 case var_filename:
199b2450
TL
1140 fputs_filtered ("\"", gdb_stdout);
1141 fputs_filtered (*(char **) c->var, gdb_stdout);
1142 fputs_filtered ("\"", gdb_stdout);
dd3b648e
RP
1143 break;
1144 case var_boolean:
199b2450 1145 fputs_filtered (*(int *) c->var ? "on" : "off", gdb_stdout);
dd3b648e
RP
1146 break;
1147 case var_uinteger:
1148 if (*(unsigned int *) c->var == UINT_MAX) {
199b2450 1149 fputs_filtered ("unlimited", gdb_stdout);
dd3b648e
RP
1150 break;
1151 }
1152 /* else fall through */
1153 case var_zinteger:
199b2450 1154 fprintf_filtered (gdb_stdout, "%u", *(unsigned int *) c->var);
dd3b648e 1155 break;
359a097f
JK
1156 case var_integer:
1157 if (*(int *) c->var == INT_MAX)
1158 {
199b2450 1159 fputs_filtered ("unlimited", gdb_stdout);
359a097f
JK
1160 }
1161 else
199b2450 1162 fprintf_filtered (gdb_stdout, "%d", *(int *) c->var);
359a097f
JK
1163 break;
1164
dd3b648e
RP
1165 default:
1166 error ("gdb internal error: bad var_type in do_setshow_command");
1167 }
199b2450 1168 fputs_filtered (".\n", gdb_stdout);
dd3b648e
RP
1169 }
1170 else
1171 error ("gdb internal error: bad cmd_type in do_setshow_command");
1ab3bf1b 1172 (*c->function.sfunc) (NULL, from_tty, c);
dd3b648e
RP
1173}
1174
1175/* Show all the settings in a list of show commands. */
1176
1177void
1178cmd_show_list (list, from_tty, prefix)
1179 struct cmd_list_element *list;
1180 int from_tty;
1181 char *prefix;
1182{
1183 for (; list != NULL; list = list->next) {
1184 /* If we find a prefix, run its list, prefixing our output by its
1185 prefix (with "show " skipped). */
1186 if (list->prefixlist && !list->abbrev_flag)
1187 cmd_show_list (*list->prefixlist, from_tty, list->prefixname + 5);
1188 if (list->type == show_cmd)
1189 {
199b2450
TL
1190 fputs_filtered (prefix, gdb_stdout);
1191 fputs_filtered (list->name, gdb_stdout);
1192 fputs_filtered (": ", gdb_stdout);
dd3b648e
RP
1193 do_setshow_command ((char *)NULL, from_tty, list);
1194 }
1195 }
1196}
1197
e1ce8aa5 1198/* ARGSUSED */
dd3b648e
RP
1199static void
1200shell_escape (arg, from_tty)
1201 char *arg;
1202 int from_tty;
1203{
87237c52
JK
1204#ifdef CANT_FORK
1205 /* FIXME: what about errors (I don't know how GO32 system() handles
1206 them)? */
1207 system (arg);
1208#else /* Can fork. */
dd3b648e
RP
1209 int rc, status, pid;
1210 char *p, *user_shell;
dd3b648e
RP
1211
1212 if ((user_shell = (char *) getenv ("SHELL")) == NULL)
1213 user_shell = "/bin/sh";
1214
1215 /* Get the name of the shell for arg0 */
1ab3bf1b 1216 if ((p = strrchr (user_shell, '/')) == NULL)
dd3b648e
RP
1217 p = user_shell;
1218 else
1219 p++; /* Get past '/' */
1220
1221 if ((pid = fork()) == 0)
1222 {
1223 if (!arg)
1224 execl (user_shell, p, 0);
1225 else
1226 execl (user_shell, p, "-c", arg, 0);
1227
5b3591ab
JK
1228 fprintf_unfiltered (gdb_stderr, "Cannot execute %s: %s\n", user_shell,
1229 safe_strerror (errno));
1230 gdb_flush (gdb_stderr);
1231 _exit (0177);
dd3b648e
RP
1232 }
1233
1234 if (pid != -1)
1235 while ((rc = wait (&status)) != pid && rc != -1)
1236 ;
1237 else
1238 error ("Fork failed");
87237c52 1239#endif /* Can fork. */
dd3b648e
RP
1240}
1241
1242static void
1243make_command (arg, from_tty)
1244 char *arg;
1245 int from_tty;
1246{
1247 char *p;
1248
1249 if (arg == 0)
1250 p = "make";
1251 else
1252 {
1253 p = xmalloc (sizeof("make ") + strlen(arg));
1254 strcpy (p, "make ");
1255 strcpy (p + sizeof("make ")-1, arg);
1256 }
1257
1258 shell_escape (p, from_tty);
1259}
1260
3f2e006b 1261static void
4369a140 1262show_user_1 (c, stream)
3f2e006b 1263 struct cmd_list_element *c;
199b2450 1264 GDB_FILE *stream;
3f2e006b
JG
1265{
1266 register struct command_line *cmdlines;
1267
1268 cmdlines = c->user_commands;
1269 if (!cmdlines)
1270 return;
3021c40d
JG
1271 fputs_filtered ("User command ", stream);
1272 fputs_filtered (c->name, stream);
1273 fputs_filtered (":\n", stream);
3f2e006b
JG
1274 while (cmdlines)
1275 {
3021c40d
JG
1276 fputs_filtered (cmdlines->line, stream);
1277 fputs_filtered ("\n", stream);
3f2e006b
JG
1278 cmdlines = cmdlines->next;
1279 }
1280 fputs_filtered ("\n", stream);
1281}
1282
1283/* ARGSUSED */
1284static void
4369a140 1285show_user (args, from_tty)
3f2e006b
JG
1286 char *args;
1287 int from_tty;
1288{
1289 struct cmd_list_element *c;
1290 extern struct cmd_list_element *cmdlist;
1291
1292 if (args)
1293 {
1294 c = lookup_cmd (&args, cmdlist, "", 0, 1);
1295 if (c->class != class_user)
1296 error ("Not a user command.");
199b2450 1297 show_user_1 (c, gdb_stdout);
3f2e006b
JG
1298 }
1299 else
1300 {
1301 for (c = cmdlist; c; c = c->next)
1302 {
1303 if (c->class == class_user)
199b2450 1304 show_user_1 (c, gdb_stdout);
3f2e006b
JG
1305 }
1306 }
1307}
1308
dd3b648e
RP
1309void
1310_initialize_command ()
1311{
1312 add_com ("shell", class_support, shell_escape,
1313 "Execute the rest of the line as a shell command. \n\
1314With no arguments, run an inferior shell.");
dd3b648e
RP
1315 add_com ("make", class_support, make_command,
1316 "Run the ``make'' program using the rest of the line as arguments.");
4369a140
JG
1317 add_cmd ("user", no_class, show_user,
1318 "Show definitions of user defined commands.\n\
3f2e006b 1319Argument is the name of the user defined command.\n\
4369a140 1320With no argument, show definitions of all user defined commands.", &showlist);
dd3b648e 1321}
This page took 0.184765 seconds and 4 git commands to generate.