Imported from ../bash-3.1.tar.gz.
[platform/upstream/bash.git] / builtins / exec.def
index c14af75..0818a25 100644 (file)
@@ -1,13 +1,13 @@
 This file is exec.def, from which is created exec.c.
 It implements the builtin "exec" in Bash.
 
-Copyright (C) 1987, 1989, 1991 Free Software Foundation, Inc.
+Copyright (C) 1987-2003 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
+Software Foundation; either version 2, or (at your option) any later
 version.
 
 Bash is distributed in the hope that it will be useful, but WITHOUT ANY
@@ -17,7 +17,7 @@ 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.
+Foundation, 59 Temple Place, Suite 330, Boston, MA 02111 USA.
 
 $PRODUCES exec.c
 
@@ -36,8 +36,8 @@ $END
 
 #include <config.h>
 
-#include <sys/types.h>
-#include "../posixstat.h"
+#include "../bashtypes.h"
+#include "posixstat.h"
 #include <signal.h>
 #include <errno.h>
 
@@ -46,9 +46,11 @@ $END
 #endif
 
 #include "../bashansi.h"
+#include "../bashintl.h"
 
 #include "../shell.h"
 #include "../execute_cmd.h"
+#include "../findcmd.h"
 #if defined (JOB_CONTROL)
 #  include "../jobs.h"
 #endif
@@ -65,7 +67,7 @@ $END
 extern int errno;
 #endif /* !errno */
 
-extern int interactive, subshell_environment;
+extern int subshell_environment;
 extern REDIRECT *redirection_undo_list;
 
 int no_exit_on_failed_exec;
@@ -78,7 +80,7 @@ mkdashname (name)
 {
   char *ret;
 
-  ret = xmalloc (2 + strlen (name));
+  ret = (char *)xmalloc (2 + strlen (name));
   ret[0] = '-';
   strcpy (ret + 1, name);
   return ret;
@@ -90,7 +92,7 @@ exec_builtin (list)
 {
   int exit_value = EXECUTION_FAILURE;
   int cleanenv, login, opt;
-  char *argv0, *command, **args, **env, *newname;
+  char *argv0, *command, **args, **env, *newname, *com2;
 
   cleanenv = login = 0;
   argv0 = (char *)NULL;
@@ -126,24 +128,30 @@ exec_builtin (list)
 #if defined (RESTRICTED_SHELL)
   if (restricted)
     {
-      builtin_error ("restricted");
+      sh_restricted ((char *)NULL);
       return (EXECUTION_FAILURE);
     }
 #endif /* RESTRICTED_SHELL */
 
-  args = word_list_to_argv (list, 1, 0, (int *)NULL);
+  args = strvec_from_word_list (list, 1, 0, (int *)NULL);
 
   /* A command with a slash anywhere in its name is not looked up in $PATH. */
   command = absolute_program (args[0]) ? args[0] : search_for_command (args[0]);
 
   if (command == 0)
     {
-      builtin_error ("%s: not found", args[0]);
+      sh_notfound (args[0]);
       exit_value = EX_NOTFOUND;        /* As per Posix.2, 3.14.6 */
       goto failed_exec;
     }
 
-  command = full_pathname (command);
+  com2 = full_pathname (command);
+  if (com2)
+    {
+      if (command != args[0])
+       free (command);
+      command = com2;
+    }
 
   if (argv0)
     {
@@ -172,7 +180,8 @@ exec_builtin (list)
     }
 
 #if defined (HISTORY)
-  maybe_save_shell_history ();
+  if (interactive_shell && subshell_environment == 0)
+    maybe_save_shell_history ();
 #endif /* HISTORY */
 
   restore_original_signals ();
@@ -183,29 +192,37 @@ exec_builtin (list)
 #endif /* JOB_CONTROL */
 
   shell_execve (command, args, env);
+
+  /* We have to set this to NULL because shell_execve has called realloc()
+     to stuff more items at the front of the array, which may have caused
+     the memory to be freed by realloc().  We don't want to free it twice. */
+  args = (char **)NULL;
   if (cleanenv == 0)
     adjust_shell_level (1);
 
   if (executable_file (command) == 0)
     {
-      builtin_error ("%s: cannot execute: %s", command, strerror (errno));
+      builtin_error (_("%s: cannot execute: %s"), command, strerror (errno));
       exit_value = EX_NOEXEC;  /* As per Posix.2, 3.14.6 */
     }
   else
     file_error (command);
 
 failed_exec:
-  if (command)
-    free (command);
+  FREE (command);
 
   if (subshell_environment || (interactive == 0 && no_exit_on_failed_exec == 0))
     exit_shell (exit_value);
 
+  if (args)
+    strvec_dispose (args);
+
   initialize_traps ();
-  reinitialize_signals ();
+  initialize_signals (1);
 
 #if defined (JOB_CONTROL)
-  restart_job_control ();
+  if (interactive_shell || job_control)
+    restart_job_control ();
 #endif /* JOB_CONTROL */
 
   return (exit_value);