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