1 /* Library for reading command lines and decoding commands.
2 Copyright (C) 1986, 1989 Free Software Foundation, Inc.
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 1, or (at your option)
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
23 extern char *xmalloc ();
25 /* Add element named NAME to command list *LIST.
26 FUN should be the function to execute the command;
27 it will get a character string as argument, with leading
28 and trailing blanks already eliminated.
30 DOC is a documentation string for the command.
31 Its first line should be a complete sentence.
32 It should start with ? for a command that is an abbreviation
33 or with * for a command that most users don't need to know about. */
35 struct cmd_list_element
*
36 add_cmd (name
, class, fun
, doc
, list
)
41 struct cmd_list_element
**list
;
43 register struct cmd_list_element
*c
44 = (struct cmd_list_element
*) xmalloc (sizeof (struct cmd_list_element
));
46 delete_cmd (name
, list
);
48 c
->name
= savestring (name
, strlen (name
));
60 /* Same as above, except that the abbrev_flag is set. */
62 struct cmd_list_element
*
63 add_abbrev_cmd (name
, class, fun
, doc
, list
)
68 struct cmd_list_element
**list
;
70 register struct cmd_list_element
*c
71 = (struct cmd_list_element
*) xmalloc (sizeof (struct cmd_list_element
));
73 delete_cmd (name
, list
);
75 c
->name
= savestring (name
, strlen (name
));
87 struct cmd_list_element
*
88 add_alias_cmd (name
, oldname
, class, abbrev_flag
, list
)
93 struct cmd_list_element
**list
;
95 /* Must do this since lookup_cmd tries to side-effect its first arg */
97 register struct cmd_list_element
*old
;
98 register struct cmd_list_element
*c
;
99 copied_name
= (char *) alloca (strlen (oldname
) + 1);
100 strcpy (copied_name
, oldname
);
101 old
= lookup_cmd (&copied_name
, *list
, 0, 1, 1);
105 delete_cmd (name
, list
);
109 c
= add_cmd (name
, class, old
->function
, old
->doc
, list
);
110 c
->prefixlist
= old
->prefixlist
;
111 c
->prefixname
= old
->prefixname
;
112 c
->allow_unknown
= old
->allow_unknown
;
113 c
->abbrev_flag
= abbrev_flag
;
118 /* Like add_cmd but adds an element for a command prefix:
119 a name that should be followed by a subcommand to be looked up
120 in another command list. PREFIXLIST should be the address
121 of the variable containing that list. */
123 struct cmd_list_element
*
124 add_prefix_cmd (name
, class, fun
, doc
, prefixlist
, prefixname
,
130 struct cmd_list_element
**prefixlist
;
133 struct cmd_list_element
**list
;
135 register struct cmd_list_element
*c
= add_cmd (name
, class, fun
, doc
, list
);
136 c
->prefixlist
= prefixlist
;
137 c
->prefixname
= prefixname
;
138 c
->allow_unknown
= allow_unknown
;
142 /* Like add_prefix_cmd butsets the abbrev_flag on the new command. */
144 struct cmd_list_element
*
145 add_abbrev_prefix_cmd (name
, class, fun
, doc
, prefixlist
, prefixname
,
151 struct cmd_list_element
**prefixlist
;
154 struct cmd_list_element
**list
;
156 register struct cmd_list_element
*c
= add_cmd (name
, class, fun
, doc
, list
);
157 c
->prefixlist
= prefixlist
;
158 c
->prefixname
= prefixname
;
159 c
->allow_unknown
= allow_unknown
;
164 /* Remove the command named NAME from the command list. */
167 delete_cmd (name
, list
)
169 struct cmd_list_element
**list
;
171 register struct cmd_list_element
*c
;
173 while (*list
&& !strcmp ((*list
)->name
, name
))
175 *list
= (*list
)->next
;
179 for (c
= *list
; c
->next
;)
181 if (!strcmp (c
->next
->name
, name
))
182 c
->next
= c
->next
->next
;
188 void help_cmd (), help_list (), help_cmd_list ();
190 /* This command really has to deal with two things:
191 * 1) I want documentation on *this string* (usually called by
192 * "help commandname").
193 * 2) I want documentation on *this list* (usually called by
194 * giving a command that requires subcommands. Also called by saying
197 * I am going to split this into two seperate comamnds, help_cmd and
202 help_cmd (command
, stream
)
206 struct cmd_list_element
*c
;
207 extern struct cmd_list_element
*cmdlist
;
211 help_list (cmdlist
, "", -2, stream
);
215 c
= lookup_cmd (&command
, cmdlist
, "", 0, 0);
220 /* There are three cases here.
221 If c->prefixlist is nonzer, we have a prefix command.
222 Print its documentation, then list its subcommands.
224 If c->function is nonzero, we really have a command.
225 Print its documentation and return.
227 If c->function is zero, we have a class name.
228 Print its documentation (as if it were a command)
229 and then set class to he number of this class
230 so that the commands in the class will be listed. */
232 fputs_filtered (c
->doc
, stream
);
233 fputs_filtered ("\n", stream
);
235 if (c
->prefixlist
== 0 && c
->function
!= 0)
237 fprintf_filtered (stream
, "\n");
239 /* If this is a prefix command, print it's subcommands */
241 help_list (*c
->prefixlist
, c
->prefixname
, -1, stream
);
243 /* If this is a class name, print all of the commands in the class */
244 if (c
->function
== 0)
245 help_list (cmdlist
, "", c
->class, stream
);
249 * Get a specific kind of help on a command list.
252 * CMDTYPE is the prefix to use in the title string.
253 * CLASS is the class with which to list the nodes of this list (see
254 * documentation for help_cmd_list below), As usual, -1 for
255 * everything, -2 for just classes, and non-negative for only things
256 * in a specific class.
257 * and STREAM is the output stream on which to print things.
258 * If you call this routine with a class >= 0, it recurses.
261 help_list (list
, cmdtype
, class, stream
)
262 struct cmd_list_element
*list
;
268 char *cmdtype1
, *cmdtype2
;
270 /* If CMDTYPE is "foo ", CMDTYPE1 gets " foo" and CMDTYPE2 gets "foo sub" */
271 len
= strlen (cmdtype
);
272 cmdtype1
= (char *) alloca (len
+ 1);
274 cmdtype2
= (char *) alloca (len
+ 4);
279 strncpy (cmdtype1
+ 1, cmdtype
, len
- 1);
281 strncpy (cmdtype2
, cmdtype
, len
- 1);
282 strcpy (cmdtype2
+ len
- 1, " sub");
286 fprintf_filtered (stream
, "List of classes of %scommands:\n\n", cmdtype2
);
288 fprintf_filtered (stream
, "List of %scommands:\n\n", cmdtype2
);
290 help_cmd_list (list
, class, cmdtype
, (class >= 0), stream
);
293 fprintf_filtered (stream
, "\n\
294 Type \"help%s\" followed by a class name for a list of commands in that class.",
297 fprintf_filtered (stream
, "\n\
298 Type \"help%s\" followed by %scommand name for full documentation.\n\
299 Command name abbreviations are allowed if unambiguous.\n",
305 * Implement a help command on command list LIST.
306 * RECURSE should be non-zero if this should be done recursively on
307 * all sublists of LIST.
308 * PREFIX is the prefix to print before each command name.
309 * STREAM is the stream upon which the output should be written.
311 * A non-negative class number to list only commands in that
313 * -1 to list all commands in list.
314 * -2 to list all classes in list.
316 * Note that RECURSE will be active on *all* sublists, not just the
317 * ones seclected by the criteria above (ie. the selection mechanism
318 * is at the low level, not the high-level).
321 help_cmd_list (list
, class, prefix
, recurse
, stream
)
322 struct cmd_list_element
*list
;
328 register struct cmd_list_element
*c
;
330 static char *line_buffer
= 0;
331 static int line_size
;
336 line_buffer
= (char *) xmalloc (line_size
);
339 for (c
= list
; c
; c
= c
->next
)
341 if (c
->abbrev_flag
== 0 &&
343 || (class == -2 && c
->function
== 0)
344 || (class == c
->class && c
->function
!= 0)))
346 fprintf_filtered (stream
, "%s%s -- ", prefix
, c
->name
);
347 /* Print just the first line */
349 while (*p
&& *p
!= '\n') p
++;
350 if (p
- c
->doc
> line_size
- 1)
352 line_size
= p
- c
->doc
+ 1;
354 line_buffer
= (char *) xmalloc (line_size
);
356 strncpy (line_buffer
, c
->doc
, p
- c
->doc
);
357 line_buffer
[p
- c
->doc
] = '\0';
358 fputs_filtered (line_buffer
, stream
);
359 fputs_filtered ("\n", stream
);
362 && c
->prefixlist
!= 0
363 && c
->abbrev_flag
== 0)
364 help_cmd_list (*c
->prefixlist
, class, c
->prefixname
, 1, stream
);
368 /* This routine takes a line of TEXT and a CLIST in which to
369 start the lookup. When it returns it will have incremented the text
370 pointer past the section of text it matched, set *RESULT_LIST to
371 the list in which the last word was matched, and will return the
372 cmd list element which the text matches. It will return 0 if no
373 match at all was possible. It will return -1 if ambigous matches are
374 possible; in this case *RESULT_LIST will be set to the list in which
375 there are ambiguous choices (and text will be set to the ambiguous
378 It does no error reporting whatsoever; control will always return
379 to the superior routine.
381 In the case of an ambiguous return (-1), *RESULT_LIST will be set to
382 point at the prefix_command (ie. the best match) *or* (special
383 case) will be 0 if no prefix command was ever found. For example,
384 in the case of "info a", "info" matches without ambiguity, but "a"
385 could be "args" or "address", so *RESULT_LIST is set to
386 the cmd_list_element for "info". So in this case
387 result list should not be interpeted as a pointer to the beginning
388 of a list; it simply points to a specific command.
390 This routine does *not* modify the text pointed to by TEXT.
392 If INGNORE_HELP_CLASSES is nonzero, ignore any command list
393 elements which are actually help classes rather than commands (i.e.
394 the function field of the struct cmd_list_element is 0). */
396 struct cmd_list_element
*
397 lookup_cmd_1 (text
, clist
, result_list
, ignore_help_classes
)
399 struct cmd_list_element
*clist
, **result_list
;
400 int ignore_help_classes
;
403 int len
, tmp
, nfound
;
404 struct cmd_list_element
*found
, *c
;
406 while (**text
== ' ' || **text
== '\t')
409 /* Treating underscores as part of command words is important
410 so that "set args_foo()" doesn't get interpreted as
411 "set args _foo()". */
413 *p
&& (isalnum(*p
) || *p
== '-' || *p
== '_');
417 /* If nothing but whitespace, return 0. */
423 /* *text and p now bracket the first command word to lookup (and
424 it's length is len). We copy this into a local temporary,
425 converting to lower case as we go. */
427 command
= (char *) alloca (len
+ 1);
428 for (tmp
= 0; tmp
< len
; tmp
++)
430 char x
= (*text
)[tmp
];
431 command
[tmp
] = (x
>= 'A' && x
<= 'Z') ? x
- 'A' + 'a' : x
;
438 for (c
= clist
; c
; c
= c
->next
)
439 if (!strncmp (command
, c
->name
, len
)
440 && (!ignore_help_classes
|| c
->function
))
444 if (c
->name
[len
] == '\0')
451 /* If nothing matches, we have a simple failure. */
457 *result_list
= 0; /* Will be modified in calling routine
458 if we know what the prefix command is.
460 return (struct cmd_list_element
*) -1; /* Ambiguous. */
463 /* We've matched something on this list. Move text pointer forward. */
466 if (found
->prefixlist
)
468 c
= lookup_cmd_1 (text
, *found
->prefixlist
, result_list
,
469 ignore_help_classes
);
472 /* Didn't find anything; this is as far as we got. */
473 *result_list
= clist
;
476 else if (c
== (struct cmd_list_element
*) -1)
478 /* We've gotten this far properley, but the next step
479 is ambiguous. We need to set the result list to the best
480 we've found (if an inferior hasn't already set it). */
482 /* This used to say *result_list = *found->prefixlist
483 If that was correct, need to modify the documentation
484 at the top of this function to clarify what is supposed
486 *result_list
= found
;
497 *result_list
= clist
;
502 /* Look up the contents of *LINE as a command in the command list LIST.
503 LIST is a chain of struct cmd_list_element's.
504 If it is found, return the struct cmd_list_element for that command
505 and update *LINE to point after the command name, at the first argument.
506 If not found, call error if ALLOW_UNKNOWN is zero
507 otherwise (or if error returns) return zero.
508 Call error if specified command is ambiguous,
509 unless ALLOW_UNKNOWN is negative.
510 CMDTYPE precedes the word "command" in the error message.
512 If INGNORE_HELP_CLASSES is nonzero, ignore any command list
513 elements which are actually help classes rather than commands (i.e.
514 the function field of the struct cmd_list_element is 0). */
516 struct cmd_list_element
*
517 lookup_cmd (line
, list
, cmdtype
, allow_unknown
, ignore_help_classes
)
519 struct cmd_list_element
*list
;
522 int ignore_help_classes
;
524 struct cmd_list_element
*last_list
= 0;
525 struct cmd_list_element
*c
=
526 lookup_cmd_1 (line
, list
, &last_list
, ignore_help_classes
);
527 char *ptr
= (*line
) + strlen (*line
) - 1;
529 /* Clear off trailing whitespace. */
530 while (ptr
>= *line
&& (*ptr
== ' ' || *ptr
== '\t'))
539 error ("Lack of needed %scommand", cmdtype
);
544 while (isalnum(*p
) || *p
== '-')
547 q
= (char *) alloca (p
- *line
+ 1);
548 strncpy (q
, *line
, p
- *line
);
551 error ("Undefined %scommand: \"%s\".", cmdtype
, q
);
557 else if (c
== (struct cmd_list_element
*) -1)
559 /* Ambigous. Local values should be off prefixlist or called
561 int local_allow_unknown
= (last_list
? last_list
->allow_unknown
:
563 char *local_cmdtype
= last_list
? last_list
->prefixname
: cmdtype
;
564 struct cmd_list_element
*local_list
=
565 (last_list
? *(last_list
->prefixlist
) : list
);
567 if (local_allow_unknown
< 0)
570 return last_list
; /* Found something. */
572 return 0; /* Found nothing. */
576 /* Report as error. */
581 ((*line
)[amb_len
] && (*line
)[amb_len
] != ' '
582 && (*line
)[amb_len
] != '\t');
587 for (c
= local_list
; c
; c
= c
->next
)
588 if (!strncmp (*line
, c
->name
, amb_len
))
590 if (strlen (ambbuf
) + strlen (c
->name
) + 6 < sizeof ambbuf
)
593 strcat (ambbuf
, ", ");
594 strcat (ambbuf
, c
->name
);
598 strcat (ambbuf
, "..");
602 error ("Ambiguous %scommand \"%s\": %s.", local_cmdtype
,
608 /* We've got something. It may still not be what the caller
609 wants (if this command *needs* a subcommand). */
610 while (**line
== ' ' || **line
== '\t')
613 if (c
->prefixlist
&& **line
&& !c
->allow_unknown
)
614 error ("Undefined %scommand: \"%s\".", c
->prefixname
, *line
);
616 /* Seems to be what he wants. Return it. */
622 /* Look up the contents of *LINE as a command in the command list LIST.
623 LIST is a chain of struct cmd_list_element's.
624 If it is found, return the struct cmd_list_element for that command
625 and update *LINE to point after the command name, at the first argument.
626 If not found, call error if ALLOW_UNKNOWN is zero
627 otherwise (or if error returns) return zero.
628 Call error if specified command is ambiguous,
629 unless ALLOW_UNKNOWN is negative.
630 CMDTYPE precedes the word "command" in the error message. */
632 struct cmd_list_element
*
633 lookup_cmd (line
, list
, cmdtype
, allow_unknown
)
635 struct cmd_list_element
*list
;
640 register struct cmd_list_element
*c
, *found
;
646 /* Skip leading whitespace. */
648 while (**line
== ' ' || **line
== '\t')
651 /* Clear out trailing whitespace. */
653 p
= *line
+ strlen (*line
);
654 while (p
!= *line
&& (p
[-1] == ' ' || p
[-1] == '\t'))
658 /* Find end of command name. */
662 || (*p
>= 'a' && *p
<= 'z')
663 || (*p
>= 'A' && *p
<= 'Z')
664 || (*p
>= '0' && *p
<= '9'))
667 /* Look up the command name.
668 If exact match, keep that.
669 Otherwise, take command abbreviated, if unique. Note that (in my
670 opinion) a null string does *not* indicate ambiguity; simply the
671 end of the argument. */
676 error ("Lack of needed %scommand", cmdtype
);
680 /* Copy over to a local buffer, converting to lowercase on the way.
681 This is in case the command being parsed is a subcommand which
682 doesn't match anything, and that's ok. We want the original
683 untouched for the routine of the original command. */
685 processed_cmd
= (char *) alloca (p
- *line
+ 1);
686 for (cmd_len
= 0; cmd_len
< p
- *line
; cmd_len
++)
688 char x
= (*line
)[cmd_len
];
689 if (x
>= 'A' && x
<= 'Z')
690 processed_cmd
[cmd_len
] = x
- 'A' + 'a';
692 processed_cmd
[cmd_len
] = x
;
694 processed_cmd
[cmd_len
] = '\0';
696 /* Check all possibilities in the current command list. */
699 for (c
= list
; c
; c
= c
->next
)
701 if (!strncmp (processed_cmd
, c
->name
, cmd_len
))
705 if (c
->name
[cmd_len
] == 0)
713 /* Report error for undefined command name. */
717 if (nfound
> 1 && allow_unknown
>= 0)
720 for (c
= list
; c
; c
= c
->next
)
721 if (!strncmp (processed_cmd
, c
->name
, cmd_len
))
723 if (strlen (ambbuf
) + strlen (c
->name
) + 6 < sizeof ambbuf
)
726 strcat (ambbuf
, ", ");
727 strcat (ambbuf
, c
->name
);
731 strcat (ambbuf
, "..");
735 error ("Ambiguous %scommand \"%s\": %s.", cmdtype
,
736 processed_cmd
, ambbuf
);
738 else if (!allow_unknown
)
739 error ("Undefined %scommand: \"%s\".", cmdtype
, processed_cmd
);
743 /* Skip whitespace before the argument. */
745 while (*p
== ' ' || *p
== '\t') p
++;
748 if (found
->prefixlist
&& *p
)
750 c
= lookup_cmd (line
, *found
->prefixlist
, found
->prefixname
,
751 found
->allow_unknown
);
760 /* Helper function for SYMBOL_COMPLETION_FUNCTION. */
762 /* Return a vector of char pointers which point to the different
763 possible completions in LIST of TEXT. */
766 complete_on_cmdlist (list
, text
)
767 struct cmd_list_element
*list
;
770 struct cmd_list_element
*ptr
;
772 int sizeof_matchlist
;
774 int textlen
= strlen (text
);
776 sizeof_matchlist
= 10;
777 matchlist
= (char **) xmalloc (sizeof_matchlist
* sizeof (char *));
780 for (ptr
= list
; ptr
; ptr
= ptr
->next
)
781 if (!strncmp (ptr
->name
, text
, textlen
)
786 if (matches
== sizeof_matchlist
)
788 sizeof_matchlist
*= 2;
789 matchlist
= (char **) xrealloc (matchlist
,
794 matchlist
[matches
] = (char *)
795 xmalloc (strlen (ptr
->name
) + 1);
796 strcpy (matchlist
[matches
++], ptr
->name
);
806 matchlist
= (char **) xrealloc (matchlist
, ((matches
+ 1)
808 matchlist
[matches
] = (char *) 0;
815 shell_escape (arg
, from_tty
)
820 char *p
, *user_shell
;
821 extern char *rindex ();
823 if ((user_shell
= (char *) getenv ("SHELL")) == NULL
)
824 user_shell
= "/bin/sh";
826 /* Get the name of the shell for arg0 */
827 if ((p
= rindex (user_shell
, '/')) == NULL
)
830 p
++; /* Get past '/' */
832 if ((pid
= fork()) == 0)
835 execl (user_shell
, p
, 0);
837 execl (user_shell
, p
, "-c", arg
, 0);
839 fprintf (stderr
, "Exec of shell failed\n");
844 while ((rc
= wait (&status
)) != pid
&& rc
!= -1)
847 error ("Fork failed");
851 _initialize_command ()
853 add_com ("shell", class_support
, shell_escape
,
854 "Execute the rest of the line as a shell command. \n\
855 With no arguments, run an inferior shell.");
This page took 0.074831 seconds and 5 git commands to generate.