]>
Commit | Line | Data |
---|---|---|
726f6388 JA |
1 | This file is command.def, from which is created command.c. |
2 | It implements the builtin "command" in Bash. | |
3 | ||
a0c0a00f | 4 | Copyright (C) 1987-2015 Free Software Foundation, Inc. |
726f6388 JA |
5 | |
6 | This file is part of GNU Bash, the Bourne Again SHell. | |
7 | ||
3185942a JA |
8 | Bash is free software: you can redistribute it and/or modify |
9 | it under the terms of the GNU General Public License as published by | |
10 | the Free Software Foundation, either version 3 of the License, or | |
11 | (at your option) any later version. | |
726f6388 | 12 | |
3185942a JA |
13 | Bash is distributed in the hope that it will be useful, |
14 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 | GNU General Public License for more details. | |
726f6388 | 17 | |
3185942a JA |
18 | You should have received a copy of the GNU General Public License |
19 | along with Bash. If not, see <http://www.gnu.org/licenses/>. | |
726f6388 JA |
20 | |
21 | $PRODUCES command.c | |
22 | ||
23 | $BUILTIN command | |
24 | $FUNCTION command_builtin | |
ccc6cda3 | 25 | $SHORT_DOC command [-pVv] command [arg ...] |
3185942a JA |
26 | Execute a simple command or display information about commands. |
27 | ||
28 | Runs COMMAND with ARGS suppressing shell function lookup, or display | |
29 | information about the specified COMMANDs. Can be used to invoke commands | |
30 | on disk when a function with the same name exists. | |
31 | ||
32 | Options: | |
a0c0a00f CR |
33 | -p use a default value for PATH that is guaranteed to find all of |
34 | the standard utilities | |
35 | -v print a description of COMMAND similar to the `type' builtin | |
36 | -V print a more verbose description of each COMMAND | |
3185942a JA |
37 | |
38 | Exit Status: | |
39 | Returns exit status of COMMAND, or failure if COMMAND is not found. | |
726f6388 JA |
40 | $END |
41 | ||
ccc6cda3 JA |
42 | #include <config.h> |
43 | ||
44 | #if defined (HAVE_UNISTD_H) | |
cce855bc JA |
45 | # ifdef _MINIX |
46 | # include <sys/types.h> | |
47 | # endif | |
ccc6cda3 JA |
48 | # include <unistd.h> |
49 | #endif | |
50 | ||
51 | #include "../bashansi.h" | |
726f6388 JA |
52 | |
53 | #include "../shell.h" | |
ccc6cda3 JA |
54 | #include "../execute_cmd.h" |
55 | #include "../flags.h" | |
726f6388 | 56 | #include "bashgetopt.h" |
ccc6cda3 | 57 | #include "common.h" |
726f6388 | 58 | |
f73dda09 JA |
59 | #if defined (_CS_PATH) && defined (HAVE_CONFSTR) && !HAVE_DECL_CONFSTR |
60 | extern size_t confstr __P((int, char *, size_t)); | |
61 | #endif | |
62 | ||
726f6388 JA |
63 | extern int subshell_environment; |
64 | ||
f73dda09 | 65 | static void restore_path __P((char *)); |
726f6388 JA |
66 | |
67 | /* Run the commands mentioned in LIST without paying attention to shell | |
68 | functions. */ | |
69 | int | |
70 | command_builtin (list) | |
71 | WORD_LIST *list; | |
72 | { | |
ccc6cda3 JA |
73 | int result, verbose, use_standard_path, opt; |
74 | char *old_path, *standard_path; | |
75 | COMMAND *command; | |
76 | ||
77 | verbose = use_standard_path = 0; | |
726f6388 JA |
78 | reset_internal_getopt (); |
79 | while ((opt = internal_getopt (list, "pvV")) != -1) | |
80 | { | |
81 | switch (opt) | |
82 | { | |
83 | case 'p': | |
a0c0a00f | 84 | use_standard_path = CDESC_STDPATH; |
726f6388 JA |
85 | break; |
86 | case 'V': | |
95732b49 | 87 | verbose = CDESC_SHORTDESC|CDESC_ABSPATH; /* look in common.h for constants */ |
726f6388 JA |
88 | break; |
89 | case 'v': | |
7117c2d2 | 90 | verbose = CDESC_REUSABLE; /* ditto */ |
726f6388 | 91 | break; |
a0c0a00f | 92 | CASE_HELPOPT; |
726f6388 | 93 | default: |
ccc6cda3 | 94 | builtin_usage (); |
726f6388 JA |
95 | return (EX_USAGE); |
96 | } | |
97 | } | |
98 | list = loptend; | |
99 | ||
ccc6cda3 | 100 | if (list == 0) |
726f6388 JA |
101 | return (EXECUTION_SUCCESS); |
102 | ||
ccc6cda3 JA |
103 | #if defined (RESTRICTED_SHELL) |
104 | if (use_standard_path && restricted) | |
105 | { | |
7117c2d2 | 106 | sh_restricted ("-p"); |
ccc6cda3 JA |
107 | return (EXECUTION_FAILURE); |
108 | } | |
109 | #endif | |
110 | ||
0001803f CR |
111 | if (verbose) |
112 | { | |
113 | int found, any_found; | |
114 | ||
115 | for (any_found = 0; list; list = list->next) | |
116 | { | |
a0c0a00f | 117 | found = describe_command (list->word->word, verbose|use_standard_path); |
0001803f CR |
118 | |
119 | if (found == 0 && verbose != CDESC_REUSABLE) | |
120 | sh_notfound (list->word->word); | |
121 | ||
122 | any_found += found; | |
123 | } | |
124 | ||
0001803f CR |
125 | return (any_found ? EXECUTION_SUCCESS : EXECUTION_FAILURE); |
126 | } | |
127 | ||
a0c0a00f CR |
128 | begin_unwind_frame ("command_builtin"); |
129 | ||
130 | #define COMMAND_BUILTIN_FLAGS (CMD_NO_FUNCTIONS | CMD_INHIBIT_EXPANSION | CMD_COMMAND_BUILTIN | (use_standard_path ? CMD_STDPATH : 0)) | |
f73dda09 | 131 | |
0001803f CR |
132 | /* We don't want this to be reparsed (consider command echo 'foo &'), so |
133 | just make a simple_command structure and call execute_command with it. */ | |
ccc6cda3 JA |
134 | command = make_bare_simple_command (); |
135 | command->value.Simple->words = (WORD_LIST *)copy_word_list (list); | |
136 | command->value.Simple->redirects = (REDIRECT *)NULL; | |
f73dda09 JA |
137 | command->flags |= COMMAND_BUILTIN_FLAGS; |
138 | command->value.Simple->flags |= COMMAND_BUILTIN_FLAGS; | |
a0c0a00f | 139 | |
ccc6cda3 JA |
140 | add_unwind_protect ((char *)dispose_command, command); |
141 | result = execute_command (command); | |
726f6388 JA |
142 | |
143 | run_unwind_frame ("command_builtin"); | |
144 | ||
145 | return (result); | |
146 | } | |
147 | ||
148 | /* Restore the value of the $PATH variable after replacing it when | |
149 | executing `command -p'. */ | |
150 | static void | |
151 | restore_path (var) | |
152 | char *var; | |
153 | { | |
d166f048 JA |
154 | if (var) |
155 | { | |
95732b49 | 156 | bind_variable ("PATH", var, 0); |
d166f048 JA |
157 | free (var); |
158 | } | |
159 | else | |
160 | unbind_variable ("PATH"); | |
0001803f CR |
161 | |
162 | stupidly_hack_special_variables ("PATH"); | |
726f6388 | 163 | } |