Imported from ../bash-4.0-rc1.tar.gz.
[platform/upstream/bash.git] / builtins / source.def
index 895e98b..72627db 100644 (file)
 This file is source.def, from which is created source.c.
 It implements the builtins "." and  "source" 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 source.c
 
 $BUILTIN source
 $FUNCTION source_builtin
-$SHORT_DOC source filename
-Read and execute commands from FILENAME and return.  The pathnames
-in $PATH are used to find the directory containing FILENAME.
+$SHORT_DOC source filename [arguments]
+Execute commands from a file in the current shell.
+
+Read and execute commands from FILENAME in the current shell.  The
+entries in $PATH are used to find the directory containing FILENAME.
+If any ARGUMENTS are supplied, they become the positional parameters
+when FILENAME is executed.
+
+Exit Status:
+Returns the status of the last command executed in FILENAME; fails if
+FILENAME cannot be read.
 $END
+
 $BUILTIN .
 $DOCNAME dot
 $FUNCTION source_builtin
-$SHORT_DOC . filename
-Read and execute commands from FILENAME and return.  The pathnames
-in $PATH are used to find the directory containing FILENAME.
+$SHORT_DOC . filename [arguments]
+Execute commands from a file in the current shell.
+
+Read and execute commands from FILENAME in the current shell.  The
+entries in $PATH are used to find the directory containing FILENAME.
+If any ARGUMENTS are supplied, they become the positional parameters
+when FILENAME is executed.
+
+Exit Status:
+Returns the status of the last command executed in FILENAME; fails if
+FILENAME cannot be read.
 $END
-/* source.c - Implements the `.' and `source' builtins. */
 
-#include <sys/types.h>
-#include <sys/file.h>
+#include <config.h>
+
+#include "../bashtypes.h"
+#include "posixstat.h"
+#include "filecntl.h"
+#if ! defined(_MINIX) && defined (HAVE_SYS_FILE_H)
+#  include <sys/file.h>
+#endif
 #include <errno.h>
 
-#if defined (HAVE_STRING_H)
-#  include <string.h>
-#else /* !HAVE_STRING_H */
-#  include <strings.h>
-#endif /* !HAVE_STRING_H */
+#if defined (HAVE_UNISTD_H)
+#  include <unistd.h>
+#endif
+
+#include "../bashansi.h"
+#include "../bashintl.h"
 
 #include "../shell.h"
-#include "../posixstat.h"
-#include "../filecntl.h"
-#include "../execute_cmd.h"
+#include "../flags.h"
+#include "../findcmd.h"
+#include "common.h"
+#include "bashgetopt.h"
+#include "../trap.h"
 
-/* Not all systems declare ERRNO in errno.h... and some systems #define it! */
 #if !defined (errno)
 extern int errno;
 #endif /* !errno */
 
-/* Variables used here but defined in other files. */
-extern int return_catch_flag, return_catch_value;
-extern jmp_buf return_catch;
 extern int posixly_correct;
-extern int interactive, interactive_shell, last_command_exit_value;
 
-/* How many `levels' of sourced files we have. */
-int sourcelevel = 0;
+static void maybe_pop_dollar_vars __P((void));
+
+/* If non-zero, `.' uses $PATH to look up the script to be sourced. */
+int source_uses_path = 1;
+
+/* If non-zero, `.' looks in the current directory if the filename argument
+   is not found in the $PATH. */
+int source_searches_cwd = 1;
 
 /* If this . script is supplied arguments, we save the dollar vars and
    replace them with the script arguments for the duration of the script's
    execution.  If the script does not change the dollar vars, we restore
-   what we saved.  If the dollar vars are changed in the script, we leave
-   the new values alone and free the saved values. */
+   what we saved.  If the dollar vars are changed in the script, and we are
+   not executing a shell function, we leave the new values alone and free
+   the saved values. */
 static void
 maybe_pop_dollar_vars ()
 {
-  if (dollar_vars_changed ())
-    {
-      dispose_saved_dollar_vars ();
-      set_dollar_vars_unchanged ();
-    }
+  if (variable_context == 0 && (dollar_vars_changed () & ARGS_SETBLTIN))
+    dispose_saved_dollar_vars ();
   else
     pop_dollar_vars ();
+  if (debugging_mode)
+    pop_args ();       /* restore BASH_ARGC and BASH_ARGV */
+  set_dollar_vars_unchanged ();
 }
 
 /* Read and execute commands from the file passed as argument.  Guess what.
    This cannot be done in a subshell, since things like variable assignments
    take place in there.  So, I open the file, place it into a large string,
    close the file, and then execute the string. */
+int
 source_builtin (list)
      WORD_LIST *list;
 {
-  int result, return_val;
+  int result;
+  char *filename, *debug_trap;
 
-  /* Assume the best. */
-  result = EXECUTION_SUCCESS;
+  if (no_options (list))
+    return (EX_USAGE);
+  list = loptend;
 
-  if (list)
+  if (list == 0)
     {
-      char *string, *filename;
-      struct stat finfo;
-      int fd, tt;
-
-      filename = find_path_file (list->word->word);
-      if (!filename)
-       filename = savestring (list->word->word);
-
-      if (((fd = open (filename, O_RDONLY)) < 0) || (fstat (fd, &finfo) < 0))
-       goto file_error_exit;
-
-      string = (char *)xmalloc (1 + (int)finfo.st_size);
-      tt = read (fd, string, finfo.st_size);
-      string[finfo.st_size] = '\0';
-
-      /* Close the open file, preserving the state of errno. */
-      { int temp = errno; close (fd); errno = temp; }
+      builtin_error (_("filename argument required"));
+      builtin_usage ();
+      return (EX_USAGE);
+    }
 
-      if (tt != finfo.st_size)
+#if defined (RESTRICTED_SHELL)
+  if (restricted && strchr (list->word->word, '/'))
+    {
+      sh_restricted (list->word->word);
+      return (EXECUTION_FAILURE);
+    }
+#endif
+
+  filename = (char *)NULL;
+  /* XXX -- should this be absolute_pathname? */
+  if (posixly_correct && strchr (list->word->word, '/'))
+    filename = savestring (list->word->word);
+  else if (absolute_pathname (list->word->word))
+    filename = savestring (list->word->word);
+  else if (source_uses_path)
+    filename = find_path_file (list->word->word);
+  if (filename == 0)
+    {
+      if (source_searches_cwd == 0)
        {
-         free (string);
-
-       file_error_exit:
-         file_error (filename);
-         free (filename);
-
-         /* POSIX shells exit if non-interactive and file error. */
-         if (posixly_correct && !interactive_shell)
-           {
-             last_command_exit_value = 1;
-             longjmp (top_level, EXITPROG);
-           }
-
+         builtin_error (_("%s: file not found"), list->word->word);
          return (EXECUTION_FAILURE);
        }
-
-      if (tt > 80)
-       tt = 80;
-
-      if (check_binary_file ((unsigned char *)string, tt))
-       {
-         free (string);
-         builtin_error ("%s: cannot execute binary file", filename);
-         free (filename);
-         return (EX_BINARY_FILE);
-       }
-
-      begin_unwind_frame ("File Sourcing");
-
-      if (list->next)
-       {
-         push_dollar_vars ();
-         add_unwind_protect ((Function *)maybe_pop_dollar_vars, (char *)NULL);
-         remember_args (list->next, 1);
-       }
-
-      unwind_protect_int (return_catch_flag);
-      unwind_protect_jmp_buf (return_catch);
-      unwind_protect_int (interactive);
-      unwind_protect_int (sourcelevel);
-      add_unwind_protect ((Function *)xfree, filename);
-      interactive = 0;
-      sourcelevel++;
-
-      set_dollar_vars_unchanged ();
-
-      return_catch_flag++;
-      return_val = setjmp (return_catch);
-
-      if (return_val)
-       parse_and_execute_cleanup ();
       else
-       result = parse_and_execute (string, filename, -1);
+       filename = savestring (list->word->word);
+    }
 
-      run_unwind_frame ("File Sourcing");
+  begin_unwind_frame ("source");
+  add_unwind_protect ((Function *)xfree, filename);
 
-      /* If RETURN_VAL is non-zero, then we return the value given
-        to return_builtin (), since that is how we got here. */
-      if (return_val)
-       result = return_catch_value;
+  if (list->next)
+    {
+      push_dollar_vars ();
+      add_unwind_protect ((Function *)maybe_pop_dollar_vars, (char *)NULL);
+      remember_args (list->next, 1);
+      if (debugging_mode)
+       push_args (list->next); /* Update BASH_ARGV and BASH_ARGC */
     }
-  else
+  set_dollar_vars_unchanged ();
+
+  /* Don't inherit the DEBUG trap unless function_trace_mode (overloaded)
+     is set.  XXX - should sourced files inherit the RETURN trap?  Functions
+     don't. */
+  debug_trap = TRAP_STRING (DEBUG_TRAP);
+  if (debug_trap && function_trace_mode == 0)
     {
-      builtin_error ("filename argument required");
-      result = EXECUTION_FAILURE;
+      debug_trap = savestring (debug_trap);
+      add_unwind_protect (xfree, debug_trap);
+      add_unwind_protect (set_debug_trap, debug_trap);
+      restore_default_signal (DEBUG_TRAP);
     }
+
+  result = source_file (filename, (list && list->next));
+
+  run_unwind_frame ("source");
+
   return (result);
 }