Line data Source code
1 : This file is command.def, from which is created command.c. 2 : It implements the builtin "command" in Bash. 3 : 4 : Copyright (C) 1987-2015 Free Software Foundation, Inc. 5 : 6 : This file is part of GNU Bash, the Bourne Again SHell. 7 : 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. 12 : 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. 17 : 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/>. 20 : 21 : $PRODUCES command.c 22 : 23 : $BUILTIN command 24 : $FUNCTION command_builtin 25 : $SHORT_DOC command [-pVv] command [arg ...] 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: 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 37 : 38 : Exit Status: 39 : Returns exit status of COMMAND, or failure if COMMAND is not found. 40 : $END 41 : 42 : #include <config.h> 43 : 44 : #if defined (HAVE_UNISTD_H) 45 : # ifdef _MINIX 46 : # include <sys/types.h> 47 : # endif 48 : # include <unistd.h> 49 : #endif 50 : 51 : #include "../bashansi.h" 52 : 53 : #include "../shell.h" 54 : #include "../execute_cmd.h" 55 : #include "../flags.h" 56 : #include "bashgetopt.h" 57 : #include "common.h" 58 : 59 : #if defined (_CS_PATH) && defined (HAVE_CONFSTR) && !HAVE_DECL_CONFSTR 60 : extern size_t confstr __P((int, char *, size_t)); 61 : #endif 62 : 63 : extern int subshell_environment; 64 : 65 : #if 0 66 : static void restore_path __P((char *)); 67 : #endif 68 : 69 : /* Run the commands mentioned in LIST without paying attention to shell 70 : functions. */ 71 : int 72 66 : command_builtin (list) 73 : WORD_LIST *list; 74 : { 75 66 : int result, verbose, use_standard_path, opt; 76 66 : COMMAND *command; 77 : 78 66 : verbose = use_standard_path = 0; 79 66 : reset_internal_getopt (); 80 66 : while ((opt = internal_getopt (list, "pvV")) != -1) 81 : { 82 0 : switch (opt) 83 : { 84 : case 'p': 85 : use_standard_path = CDESC_STDPATH; 86 : break; 87 0 : case 'V': 88 0 : verbose = CDESC_SHORTDESC|CDESC_ABSPATH; /* look in common.h for constants */ 89 0 : break; 90 0 : case 'v': 91 0 : verbose = CDESC_REUSABLE; /* ditto */ 92 0 : break; 93 0 : CASE_HELPOPT; 94 0 : default: 95 0 : builtin_usage (); 96 0 : return (EX_USAGE); 97 : } 98 : } 99 66 : list = loptend; 100 : 101 66 : if (list == 0) 102 : return (EXECUTION_SUCCESS); 103 : 104 : #if defined (RESTRICTED_SHELL) 105 17 : if (use_standard_path && restricted) 106 : { 107 0 : sh_restricted ("-p"); 108 0 : return (EXECUTION_FAILURE); 109 : } 110 : #endif 111 : 112 17 : if (verbose) 113 : { 114 : int found, any_found; 115 : 116 0 : for (any_found = 0; list; list = list->next) 117 : { 118 0 : found = describe_command (list->word->word, verbose|use_standard_path); 119 : 120 0 : if (found == 0 && verbose != CDESC_REUSABLE) 121 0 : sh_notfound (list->word->word); 122 : 123 0 : any_found += found; 124 : } 125 : 126 0 : return (any_found ? EXECUTION_SUCCESS : EXECUTION_FAILURE); 127 : } 128 : 129 17 : begin_unwind_frame ("command_builtin"); 130 : 131 : #define COMMAND_BUILTIN_FLAGS (CMD_NO_FUNCTIONS | CMD_INHIBIT_EXPANSION | CMD_COMMAND_BUILTIN | (use_standard_path ? CMD_STDPATH : 0)) 132 : 133 : /* We don't want this to be reparsed (consider command echo 'foo &'), so 134 : just make a simple_command structure and call execute_command with it. */ 135 17 : command = make_bare_simple_command (); 136 17 : command->value.Simple->words = (WORD_LIST *)copy_word_list (list); 137 17 : command->value.Simple->redirects = (REDIRECT *)NULL; 138 17 : command->flags |= COMMAND_BUILTIN_FLAGS; 139 17 : command->value.Simple->flags |= COMMAND_BUILTIN_FLAGS; 140 : 141 17 : add_unwind_protect ((char *)dispose_command, command); 142 17 : result = execute_command (command); 143 : 144 16 : run_unwind_frame ("command_builtin"); 145 : 146 16 : return (result); 147 : } 148 : 149 : #if 0 150 : /* Restore the value of the $PATH variable after replacing it when 151 : executing `command -p'. */ 152 : static void 153 : restore_path (var) 154 : char *var; 155 : { 156 : if (var) 157 : { 158 : bind_variable ("PATH", var, 0); 159 : free (var); 160 : } 161 : else 162 : unbind_variable ("PATH"); 163 : 164 : stupidly_hack_special_variables ("PATH"); 165 : } 166 : #endif