Imported from ../bash-4.0-rc1.tar.gz.
[platform/upstream/bash.git] / builtins / command.def
index 6e6e5f6..77f9126 100644 (file)
@@ -1,40 +1,50 @@
 This file is command.def, from which is created command.c.
 It implements the builtin "command" in Bash.
 
-Copyright (C) 1987, 1989, 1991 Free Software Foundation, Inc.
+Copyright (C) 1987-2009 Free Software Foundation, Inc.
 
 This file is part of GNU Bash, the Bourne Again SHell.
 
-Bash is free software; you can redistribute it and/or modify it under
-the terms of the GNU General Public License as published by the Free
-Software Foundation; either version 1, or (at your option) any later
-version.
+Bash is free software: you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation, either version 3 of the License, or
+(at your option) any later version.
 
-Bash is distributed in the hope that it will be useful, but WITHOUT ANY
-WARRANTY; without even the implied warranty of MERCHANTABILITY or
-FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
-for more details.
+Bash is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+GNU General Public License for more details.
 
-You should have received a copy of the GNU General Public License along
-with Bash; see the file COPYING.  If not, write to the Free Software
-Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
+You should have received a copy of the GNU General Public License
+along with Bash.  If not, see <http://www.gnu.org/licenses/>.
 
 $PRODUCES command.c
 
 $BUILTIN command
 $FUNCTION command_builtin
 $SHORT_DOC command [-pVv] command [arg ...]
-Runs COMMAND with ARGS ignoring shell functions.  If you have a shell
-function called `ls', and you wish to call the command `ls', you can
-say "command ls".  If the -p option is given, a default value is used
-for PATH that is guaranteed to find all of the standard utilities.  If
-the -V or -v option is given, a string is printed describing COMMAND.
-The -V option produces a more verbose description.
+Execute a simple command or display information about commands.
+
+Runs COMMAND with ARGS suppressing  shell function lookup, or display
+information about the specified COMMANDs.  Can be used to invoke commands
+on disk when a function with the same name exists.
+
+Options:
+  -p   use a default value for PATH that is guaranteed to find all of
+       the standard utilities
+  -v   print a description of COMMAND similar to the `type' builtin
+  -V   print a more verbose description of each COMMAND
+
+Exit Status:
+Returns exit status of COMMAND, or failure if COMMAND is not found.
 $END
 
 #include <config.h>
 
 #if defined (HAVE_UNISTD_H)
+#  ifdef _MINIX
+#    include <sys/types.h>
+#  endif
 #  include <unistd.h>
 #endif
 
@@ -46,10 +56,14 @@ $END
 #include "bashgetopt.h"
 #include "common.h"
 
+#if defined (_CS_PATH) && defined (HAVE_CONFSTR) && !HAVE_DECL_CONFSTR
+extern size_t confstr __P((int, char *, size_t));
+#endif
+
 extern int subshell_environment;
 
-static void restore_path ();
-static char *get_standard_path ();
+static void restore_path __P((char *));
+static char *get_standard_path __P((void));
 
 /* Run the commands mentioned in LIST without paying attention to shell
    functions. */
@@ -71,10 +85,10 @@ command_builtin (list)
          use_standard_path = 1;
          break;
        case 'V':
-         verbose = 2;
+         verbose = CDESC_SHORTDESC|CDESC_ABSPATH;      /* look in common.h for constants */
          break;
        case 'v':
-         verbose = 4;
+         verbose = CDESC_REUSABLE;     /* ditto */
          break;
        default:
          builtin_usage ();
@@ -92,10 +106,10 @@ command_builtin (list)
 
       for (any_found = 0; list; list = list->next)
        {
-         found = describe_command (list->word->word, verbose, 0);
+         found = describe_command (list->word->word, verbose);
 
-         if (found == 0)
-           builtin_error ("%s: not found", list->word->word);
+         if (found == 0 && verbose != CDESC_REUSABLE)
+           sh_notfound (list->word->word);
 
          any_found += found;
        }
@@ -105,7 +119,7 @@ command_builtin (list)
 #if defined (RESTRICTED_SHELL)
   if (use_standard_path && restricted)
     {
-      builtin_error ("restricted: cannot use -p");
+      sh_restricted ("-p");
       return (EXECUTION_FAILURE);
     }
 #endif
@@ -124,15 +138,17 @@ command_builtin (list)
       add_unwind_protect ((Function *)restore_path, old_path);
 
       standard_path = get_standard_path ();
-      bind_variable ("PATH", standard_path ? standard_path : "");
+      bind_variable ("PATH", standard_path ? standard_path : "", 0);
       FREE (standard_path);
     }
 
+#define COMMAND_BUILTIN_FLAGS (CMD_NO_FUNCTIONS | CMD_INHIBIT_EXPANSION | CMD_COMMAND_BUILTIN)
+
   command = make_bare_simple_command ();
   command->value.Simple->words = (WORD_LIST *)copy_word_list (list);
   command->value.Simple->redirects = (REDIRECT *)NULL;
-  command->flags |= (CMD_NO_FUNCTIONS | CMD_INHIBIT_EXPANSION);
-  command->value.Simple->flags |= (CMD_NO_FUNCTIONS | CMD_INHIBIT_EXPANSION);
+  command->flags |= COMMAND_BUILTIN_FLAGS;
+  command->value.Simple->flags |= COMMAND_BUILTIN_FLAGS;
 #if 0
   /* This breaks for things like ( cd /tmp ; command z ababa ; echo next )
      or $(command echo a ; command echo b;) or even
@@ -161,7 +177,7 @@ restore_path (var)
 {
   if (var)
     {
-      bind_variable ("PATH", var);
+      bind_variable ("PATH", var, 0);
       free (var);
     }
   else
@@ -179,11 +195,16 @@ get_standard_path ()
   size_t len;
 
   len = (size_t)confstr (_CS_PATH, (char *)NULL, (size_t)0);
-  p = xmalloc ((int)len + 2);
-  *p = '\0';
-  confstr (_CS_PATH, p, len);
-  return (p);
-#else /* !_CSPATH || !HAVE_CONFSTR  */
+  if (len > 0)
+    {
+      p = (char *)xmalloc (len + 2);
+      *p = '\0';
+      confstr (_CS_PATH, p, len);
+      return (p);
+    }
+  else
+    return (savestring (STANDARD_UTILS_PATH));
+#else /* !_CS_PATH || !HAVE_CONFSTR  */
 #  if defined (CS_PATH)
   return (savestring (CS_PATH));
 #  else