Imported from ../bash-2.05.tar.gz.
[platform/upstream/bash.git] / shell.c
diff --git a/shell.c b/shell.c
index e4f649c..91395d7 100644 (file)
--- a/shell.c
+++ b/shell.c
@@ -1,30 +1,22 @@
-/* shell.c -- GNU's idea of the POSIX shell specification.
+/* shell.c -- GNU's idea of the POSIX shell specification. */
 
-   This file is part of Bash, the Bourne Again SHell.  Bash is free
-   software; no one can prevent you from reading the source code, or
-   giving it to someone else.  This file is copyrighted under the GNU
-   General Public License, which can be found in the file called
-   COPYING.
+/* Copyright (C) 1987,1991 Free Software Foundation, Inc.
 
-   Copyright (C) 1988, 1991 Free Software Foundation, Inc.
+   This file is part of GNU Bash, the Bourne Again SHell.
 
-   This file is part of GNU Bash.
+   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 2, or (at your option)
+   any later version.
 
    Bash is distributed in the hope that it will be useful, but WITHOUT
-   ANY WARRANTY.  No author or distributor accepts responsibility to
-   anyone for the consequences of using it or for whether it serves
-   any particular purpose or works at all, unless he says so in
-   writing.  Refer to the GNU Emacs General Public License for full
-   details.
+   ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+   or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public
+   License for more details.
 
-   Everyone is granted permission to copy, modify and redistribute
-   Bash, but only under the conditions described in the GNU General
-   Public License.  A copy of this license is supposed to have been
-   given to you along with GNU Emacs so you can know your rights and
-   responsibilities.  It should be in a file named COPYING.
-
-   Among other things, the copyright notice and this notice must be
-   preserved on all copies.
+   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, 59 Temple Place, Suite 330, Boston, MA 02111 USA.
 
   Birthdate:
   Sunday, January 10th, 1988.
 #include <tilde/tilde.h>
 #include <glob/fnmatch.h>
 
+#if defined (__OPENNT)
+#  include <opennt/opennt.h>
+#endif
+
 #if !defined (HAVE_GETPW_DECLS)
 extern struct passwd *getpwuid ();
 #endif /* !HAVE_GETPW_DECLS */
@@ -81,8 +77,13 @@ extern struct passwd *getpwuid ();
 extern int errno;
 #endif
 
+#if defined (NO_MAIN_ENV_ARG)
+extern char **environ; /* used if no third argument to main() */
+#endif
+
 extern char *dist_version, *release_status;
 extern int patch_level, build_version;
+extern int shell_level;
 extern int subshell_environment;
 extern int last_command_exit_value;
 extern int line_number;
@@ -110,7 +111,7 @@ char *current_host_name = (char *)NULL;
    Specifically:
    0 = not login shell.
    1 = login shell from getty (or equivalent fake out)
-  -1 = login shell from "-login" flag.
+  -1 = login shell from "--login" flag.
   -2 = both from getty, and from flag.
  */
 int login_shell = 0;
@@ -201,6 +202,7 @@ struct {
   { "dump-po-strings", Int, &dump_po_strings, (char **)0x0 },
   { "dump-strings", Int, &dump_translatable_strings, (char **)0x0 },
   { "help", Int, &want_initial_help, (char **)0x0 },
+  { "init-file", Charp, (int *)0x0, &bashrc_file },
   { "login", Int, &make_login_shell, (char **)0x0 },
   { "noediting", Int, &no_line_editing, (char **)0x0 },
   { "noprofile", Int, &no_profile, (char **)0x0 },
@@ -230,8 +232,11 @@ char **subshell_envp;
 int default_buffered_input = -1;
 #endif
 
-static int read_from_stdin;            /* -s flag supplied */
-static int want_pending_command;       /* -c flag supplied */
+/* The following two variables are not static so they can show up in $-. */
+int read_from_stdin;           /* -s flag supplied */
+int want_pending_command;      /* -c flag supplied */
+
+static int shell_reinitialized = 0;
 static char *local_pending_command;
 
 static FILE *default_input;
@@ -255,7 +260,7 @@ static void shell_reinitialize ();
 
 static void show_shell_usage ();
 
-#ifdef __CYGWIN32__
+#ifdef __CYGWIN__
 static void
 _cygwin32_check_tmp ()
 {
@@ -269,17 +274,30 @@ _cygwin32_check_tmp ()
        internal_warning ("/tmp must be a valid directory name");
     }
 }
-#endif /* __CYGWIN32__ */
+#endif /* __CYGWIN__ */
 
+#if defined (NO_MAIN_ENV_ARG)
+/* systems without third argument to main() */
+int
+main (argc, argv)
+     int argc;
+     char **argv;
+#else /* !NO_MAIN_ENV_ARG */
 int
 main (argc, argv, env)
      int argc;
      char **argv, **env;
+#endif /* !NO_MAIN_ENV_ARG */
 {
   register int i;
-  int code, saverst;
+  int code, saverst, old_errexit_flag;
   volatile int locally_skip_execution;
   volatile int arg_index, top_level_arg_index;
+#ifdef __OPENNT
+  char **env;
+
+  env = environ;
+#endif /* __OPENNT */
 
   /* Catch early SIGINTs. */
   code = setjmp (top_level);
@@ -288,9 +306,9 @@ main (argc, argv, env)
 
   check_dev_tty ();
 
-#ifdef __CYGWIN32__
+#ifdef __CYGWIN__
   _cygwin32_check_tmp ();
-#endif
+#endif /* __CYGWIN__ */
 
   /* Wait forever if we are debugging a login shell. */
   while (debugging_login_shell);
@@ -299,8 +317,8 @@ main (argc, argv, env)
 
   running_setuid = uidget ();
 
-  posixly_correct = (getenv ("POSIXLY_CORRECT") != (char *)NULL) ||
-                   (getenv ("POSIX_PEDANTIC") != (char *)NULL);
+  if (getenv ("POSIXLY_CORRECT") || getenv ("POSIX_PEDANTIC"))
+    posixly_correct = 1;
 
 #if defined (USE_GNU_MALLOC_LIBRARY)
   mcheck (programming_error, (void (*) ())0);
@@ -314,6 +332,8 @@ main (argc, argv, env)
       sourced_env = 0;
     }
 
+  shell_reinitialized = 0;
+
   /* Initialize `local' variables for all `invocations' of main (). */
   arg_index = 1;
   local_pending_command = (char *)NULL;
@@ -436,11 +456,8 @@ main (argc, argv, env)
      other Posix.2 things. */
   if (posixly_correct)
     {
-      posix_initialize (posixly_correct);
-#if defined (READLINE)
-      if (interactive_shell)
-       posix_readline_initialize (posixly_correct);
-#endif
+      bind_variable ("POSIXLY_CORRECT", "y");
+      sv_strict_posix ("POSIXLY_CORRECT");
     }
 
   /* From here on in, the shell must be a normal functioning shell.
@@ -461,6 +478,7 @@ main (argc, argv, env)
     }
 
   top_level_arg_index = arg_index;
+  old_errexit_flag = exit_immediately_on_error;
 
   /* Give this shell a place to longjmp to before executing the
      startup files.  This allows users to press C-c to abort the
@@ -476,6 +494,9 @@ main (argc, argv, env)
          /* Reset job control, since run_startup_files turned it off. */
          set_job_control (interactive_shell);
 #endif
+         /* Reset value of `set -e', since it's turned off before running
+            the startup files. */
+         exit_immediately_on_error += old_errexit_flag;
          locally_skip_execution++;
        }
     }
@@ -488,7 +509,8 @@ main (argc, argv, env)
     {
       makunbound ("PS1", shell_variables);
       makunbound ("PS2", shell_variables);
-      interactive = expand_aliases = 0;
+      interactive = 0;
+      expand_aliases = posixly_correct;
     }
   else
     {
@@ -497,23 +519,32 @@ main (argc, argv, env)
     }
 
 #if defined (RESTRICTED_SHELL)
+  /* Set restricted_shell based on whether the basename of $0 indicates that
+     the shell should be restricted or if the `-r' option was supplied at
+     startup. */
+  restricted_shell = shell_is_restricted (shell_name);
+
   /* If the `-r' option is supplied at invocation, make sure that the shell
      is not in restricted mode when running the startup files. */
-    saverst = restricted;
-    restricted = 0;
+  saverst = restricted;
+  restricted = 0;
 #endif
 
+  /* The startup files are run with `set -e' temporarily disabled. */
   if (locally_skip_execution == 0 && running_setuid == 0)
-    run_startup_files ();
+    {
+      old_errexit_flag = exit_immediately_on_error;
+      exit_immediately_on_error = 0;
+
+      run_startup_files ();
+      exit_immediately_on_error += old_errexit_flag;
+    }
 
   /* If we are invoked as `sh', turn on Posix mode. */
   if (act_like_sh)
     {
-      posix_initialize (posixly_correct = 1);
-#if defined (READLINE)
-      if (interactive_shell)
-        posix_readline_initialize (posixly_correct);
-#endif
+      bind_variable ("POSIXLY_CORRECT", "y");
+      sv_strict_posix ("POSIXLY_CORRECT");
     }
 
 #if defined (RESTRICTED_SHELL)
@@ -521,7 +552,8 @@ main (argc, argv, env)
      means that `bash -r' or `set -r' invoked from a startup file will
      turn on the restrictions after the startup files are executed. */
   restricted = saverst || restricted;
-  maybe_make_restricted (shell_name);
+  if (shell_reinitialized == 0)
+    maybe_make_restricted (shell_name);
 #endif /* RESTRICTED_SHELL */
 
   if (wordexp_only)
@@ -534,7 +566,6 @@ main (argc, argv, env)
   if (local_pending_command)
     {
       arg_index = bind_args (argv, arg_index, argc, 0);
-
       startup_state = 2;
 #if defined (ONESHOT)
       run_one_command (local_pending_command);
@@ -565,7 +596,6 @@ main (argc, argv, env)
 
   /* Bind remaining args to $1 ... $n */
   arg_index = bind_args (argv, arg_index, argc, 1);
-
   /* Do the things that should be done only for interactive shells. */
   if (interactive_shell)
     {
@@ -749,14 +779,26 @@ exit_shell (s)
     hangup_all_jobs ();
 
   /* If this shell is interactive, terminate all stopped jobs and
-     restore the original terminal process group. */
-  end_job_control ();
+     restore the original terminal process group.  Don't do this if we're
+     in a subshell and calling exit_shell after, for example, a failed
+     word expansion. */
+  if (subshell_environment == 0)
+    end_job_control ();
 #endif /* JOB_CONTROL */
 
   /* Always return the exit status of the last command to our parent. */
   exit (s);
 }
 
+/* A wrapper for exit that (optionally) can do other things, like malloc
+   statistics tracing. */
+void
+sh_exit (s)
+     int s;
+{
+  exit (s);
+}
+
 /* Source the bash startup files.  If POSIXLY_CORRECT is non-zero, we obey
    the Posix.2 startup file rules:  $ENV is expanded, and if the file it
    names exists, that file is sourced.  The Posix.2 rules are in effect
@@ -772,16 +814,16 @@ exit_shell (s)
 
    then:
 
-         COMMAND           EXECUTE BASHRC
-         --------------------------------
-         bash -c foo           NO
-         bash foo              NO
-         foo                   NO
-         rsh machine ls                YES (for rsh, which calls `bash -c')
-         rsh machine foo       YES (for shell started by rsh) NO (for foo!)
-         echo ls | bash                NO
-         login                 NO
-         bash                  YES
+        COMMAND            EXECUTE BASHRC
+        --------------------------------
+        bash -c foo            NO
+        bash foo               NO
+        foo                    NO
+        rsh machine ls         YES (for rsh, which calls `bash -c')
+        rsh machine foo        YES (for shell started by rsh) NO (for foo!)
+        echo ls | bash         NO
+        login                  NO
+        bash                   YES
 */
 
 static void
@@ -813,30 +855,24 @@ run_startup_files ()
   int old_job_control;
 #endif
   int sourced_login, run_by_ssh;
-  SHELL_VAR *sshvar;
 
   /* get the rshd/sshd case out of the way first. */
   if (interactive_shell == 0 && no_rc == 0 && login_shell == 0 &&
       act_like_sh == 0 && local_pending_command)
     {
-      /* Find out if we were invoked by ssh.  If so, set RUN_BY_SSH to 1. */
-      sshvar = find_variable ("SSH_CLIENT");
-      if (sshvar)
-       {
-         run_by_ssh = 1;
-         /* Now that we've tested the variable, we need to unexport it. */
-         sshvar->attributes &= ~att_exported;
-         array_needs_making = 1;
-       }
-      else
-       run_by_ssh = 0;
+      run_by_ssh = find_variable ("SSH_CLIENT") != (SHELL_VAR *)0;
+      run_by_ssh |= find_variable ("SSH2_CLIENT") != (SHELL_VAR *)0;
 
       /* If we were run by sshd or we think we were run by rshd, execute
-        ~/.bashrc. */
-      if (run_by_ssh || isnetconn (fileno (stdin)))
+        ~/.bashrc if we are a top-level shell. */
+      if ((run_by_ssh || isnetconn (fileno (stdin))) && shell_level < 2)
        {
 #ifdef SYS_BASHRC
+#  if defined (__OPENNT)
+         maybe_execute_file (_prefixInstallPath(SYS_BASHRC, NULL, 0), 1);
+#  else
          maybe_execute_file (SYS_BASHRC, 1);
+#  endif
 #endif
          maybe_execute_file (bashrc_file, 1);
          return;
@@ -850,8 +886,15 @@ run_startup_files ()
 
   sourced_login = 0;
 
+  /* A shell begun with the --login flag that is not in posix mode runs
+     the login shell startup files, no matter whether or not it is
+     interactive.  If NON_INTERACTIVE_LOGIN_SHELLS is defined, run the
+     startup files if argv[0][0] == '-' as well. */
 #if defined (NON_INTERACTIVE_LOGIN_SHELLS)
-  if (login_shell)
+  if (login_shell && posixly_correct == 0)
+#else
+  if (login_shell < 0 && posixly_correct == 0)
+#endif
     {
       /* We don't execute .bashrc for login shells. */
       no_rc++;
@@ -871,7 +914,6 @@ run_startup_files ()
 
       sourced_login = 1;
     }
-#endif /* NON_INTERACTIVE_LOGIN_SHELLS */
 
   /* A non-interactive shell not named `sh' and not in posix mode reads and
      executes commands from $BASH_ENV.  If `su' starts a shell with `-c cmd'
@@ -911,19 +953,23 @@ run_startup_files ()
       if (act_like_sh == 0 && no_rc == 0)
        {
 #ifdef SYS_BASHRC
+#  if defined (__OPENNT)
+         maybe_execute_file (_prefixInstallPath(SYS_BASHRC, NULL, 0), 1);
+#  else
          maybe_execute_file (SYS_BASHRC, 1);
+#  endif
 #endif
-          maybe_execute_file (bashrc_file, 1);
+         maybe_execute_file (bashrc_file, 1);
        }
       /* sh */
       else if (act_like_sh && privileged_mode == 0 && sourced_env++ == 0)
-        execute_env_file (get_string_value ("ENV"));
+       execute_env_file (get_string_value ("ENV"));
     }
   else         /* bash --posix, sh --posix */
     {
       /* bash and sh */
       if (interactive_shell && privileged_mode == 0 && sourced_env++ == 0)
-        execute_env_file (get_string_value ("ENV"));
+       execute_env_file (get_string_value ("ENV"));
     }
 
 #if defined (JOB_CONTROL)
@@ -932,10 +978,26 @@ run_startup_files ()
 }
 
 #if defined (RESTRICTED_SHELL)
+/* Return 1 if the shell should be a restricted one based on NAME or the
+   value of `restricted'.  Don't actually do anything, just return a
+   boolean value. */
+int
+shell_is_restricted (name)
+     char *name;
+{
+  char *temp;
+
+  if (restricted)
+    return 1;
+  temp = base_pathname (name);
+  return (STREQ (temp, RESTRICTED_SHELL_NAME));
+}
+
 /* Perhaps make this shell a `restricted' one, based on NAME.  If the
    basename of NAME is "rbash", then this shell is restricted.  The
    name of the restricted shell is a configurable option, see config.h.
-   In a restricted shell, PATH and SHELL are read-only and non-unsettable.
+   In a restricted shell, PATH, SHELL, ENV, and BASH_ENV are read-only
+   and non-unsettable.
    Do this also if `restricted' is already set to 1; maybe the shell was
    started with -r. */
 int
@@ -949,7 +1011,9 @@ maybe_make_restricted (name)
     {
       set_var_read_only ("PATH");
       set_var_read_only ("SHELL");
-      restricted++;
+      set_var_read_only ("ENV");
+      set_var_read_only ("BASH_ENV");
+      restricted = 1;
     }
   return (restricted);
 }
@@ -1011,7 +1075,7 @@ run_wordexp (words)
        case DISCARD:
          return last_command_exit_value = 1;
        default:
-         programming_error ("run_wordexp: bad jump: code %d", code);
+         command_error ("run_wordexp", CMDERR_BADJUMP, code, 0);
        }
     }
 
@@ -1020,14 +1084,14 @@ run_wordexp (words)
     {
       with_input_from_string (words, "--wordexp");
       if (parse_command () != 0)
-        return (126);
+       return (126);
       if (global_command == 0)
        {
          printf ("0\n0\n");
          return (0);
        }
       if (global_command->type != cm_simple)
-        return (126);
+       return (126);
       wl = global_command->value.Simple->words;
       result = wl ? expand_words_no_vars (wl) : (WORD_LIST *)0;
     }
@@ -1084,7 +1148,7 @@ run_one_command (command)
        case DISCARD:
          return last_command_exit_value = 1;
        default:
-         programming_error ("run_one_command: bad jump: code %d", code);
+         command_error ("run_one_command", CMDERR_BADJUMP, code, 0);
        }
     }
    return (parse_and_execute (savestring (command), "-c", SEVAL_NOHIST));
@@ -1114,7 +1178,7 @@ bind_args (argv, arg_start, arg_end, start_index)
          remember_args (args->next, 1);
        }
       else                     /* bind to $1...$n for shell script */
-        remember_args (args, 1);
+       remember_args (args, 1);
 
       dispose_words (args);
     }
@@ -1132,7 +1196,7 @@ static int
 open_shell_script (script_name)
      char *script_name;
 {
-  int fd, e;
+  int fd, e, fd_is_tty;
   char *filename, *path_filename;
   unsigned char sample[80];
   int sample_len;
@@ -1166,8 +1230,14 @@ open_shell_script (script_name)
       exit ((e == ENOENT) ? EX_NOTFOUND : EX_NOINPUT);
     }
 
-  /* Only do this with file descriptors we can seek on. */
-  if (lseek (fd, 0L, 1) != -1)
+#ifdef HAVE_DEV_FD
+  fd_is_tty = isatty (fd);
+#else
+  fd_is_tty = 0;
+#endif
+
+  /* Only do this with non-tty file descriptors we can seek on. */
+  if (fd_is_tty == 0 && (lseek (fd, 0L, 1) != -1))
     {
       /* Check to see if the `file' in `bash file' is a binary file
         according to the same tests done by execute_simple_command (),
@@ -1178,7 +1248,7 @@ open_shell_script (script_name)
          e = errno;
          if ((fstat (fd, &sb) == 0) && S_ISDIR (sb.st_mode))
            internal_error ("%s: is a directory", filename);
-          else
+         else
            {
              errno = e;
              file_error (filename);
@@ -1194,23 +1264,19 @@ open_shell_script (script_name)
       lseek (fd, 0L, 0);
     }
 
-#if defined (BUFFERED_INPUT)
-  default_buffered_input = fd;
-#  if 0
-  /* This is never executed. */
-  if (default_buffered_input == -1)
-    {
-      file_error (filename);
-      exit (EX_NOTFOUND);
-    }
-#  endif
-  SET_CLOSE_ON_EXEC (default_buffered_input);
-#else /* !BUFFERED_INPUT */
   /* Open the script.  But try to move the file descriptor to a randomly
      large one, in the hopes that any descriptors used by the script will
-      not match with ours. */
+     not match with ours. */
   fd = move_to_high_fd (fd, 0, -1);
 
+#if defined (__CYGWIN__) && defined (O_TEXT)
+  setmode (fd, O_TEXT);
+#endif
+
+#if defined (BUFFERED_INPUT)
+  default_buffered_input = fd;
+  SET_CLOSE_ON_EXEC (default_buffered_input);
+#else /* !BUFFERED_INPUT */
   default_input = fdopen (fd, "r");
 
   if (default_input == 0)
@@ -1224,13 +1290,10 @@ open_shell_script (script_name)
     SET_CLOSE_ON_EXEC (fileno (default_input));
 #endif /* !BUFFERED_INPUT */
 
-  if (interactive_shell == 0 || isatty (fd) == 0)
-    /* XXX - does this really need to be called again here? */
-    init_noninteractive ();
-  else
+  /* Just about the only way for this code to be executed is if something
+     like `bash -i /dev/stdin' is executed. */
+  if (interactive_shell && fd_is_tty)
     {
-      /* I don't believe that this code is ever executed, even in
-        the presence of /dev/fd. */
       dup2 (fd, 0);
       close (fd);
       fd = 0;
@@ -1241,6 +1304,7 @@ open_shell_script (script_name)
       default_input = stdin;
 #endif
     }
+
   free (filename);
   return (fd);
 }
@@ -1253,10 +1317,10 @@ set_bash_input ()
      no-delay mode. */
 #if defined (BUFFERED_INPUT)
   if (interactive == 0)
-    unset_nodelay_mode (default_buffered_input);
+    sh_unset_nodelay_mode (default_buffered_input);
   else
 #endif /* !BUFFERED_INPUT */
-    unset_nodelay_mode (fileno (stdin));
+    sh_unset_nodelay_mode (fileno (stdin));
 
   /* with_input_from_stdin really means `with_input_from_readline' */
   if (interactive && no_line_editing == 0)
@@ -1392,8 +1456,8 @@ shell_initialize ()
   /* Line buffer output for stderr and stdout. */
   if (shell_initialized == 0)
     {
-      setlinebuf (stderr);
-      setlinebuf (stdout);
+      sh_setlinebuf (stderr);
+      sh_setlinebuf (stdout);
     }
 
   /* Sort the array of shell builtins so that the binary search in
@@ -1498,10 +1562,7 @@ shell_reinitialize ()
   delete_all_variables (shell_variables);
   delete_all_variables (shell_functions);
 
-#if 0
-  /* Pretend the PATH variable has changed. */
-  flush_hashed_filenames ();
-#endif
+  shell_reinitialized = 1;
 }
 
 static void
@@ -1559,7 +1620,7 @@ static int
 isnetconn (fd)
      int fd;
 {
-#if defined (HAVE_GETPEERNAME) && !defined (SVR4_2)
+#if defined (HAVE_GETPEERNAME) && !defined (SVR4_2) && !defined (__BEOS__)
   int rv, l;
   struct sockaddr sa;
 
@@ -1567,7 +1628,7 @@ isnetconn (fd)
   rv = getpeername(fd, &sa, &l);
   /* Solaris 2.5 getpeername() returns EINVAL if the fd is not a socket. */
   return ((rv < 0 && (errno == ENOTSOCK || errno == EINVAL)) ? 0 : 1);
-#else /* !HAVE_GETPEERNAME || SVR4_2 */
+#else /* !HAVE_GETPEERNAME || SVR4_2 || __BEOS__ */
 #  if defined (SVR4) || defined (SVR4_2)
   /* Sockets on SVR4 and SVR4.2 are character special (streams) devices. */
   struct stat sb;
@@ -1582,15 +1643,15 @@ isnetconn (fd)
 #    endif /* S_ISFIFO */
   return (S_ISCHR (sb.st_mode));
 #  else /* !SVR4 && !SVR4_2 */
-#    if defined (S_ISSOCK)
+#    if defined (S_ISSOCK) && !defined (__BEOS__)
   struct stat sb;
 
   if (fstat (fd, &sb) < 0)
     return (0);
   return (S_ISSOCK (sb.st_mode));
-#    else /* !S_ISSOCK */
+#    else /* !S_ISSOCK || __BEOS__ */
   return (0);
-#    endif /* !S_ISSOCK */
+#    endif /* !S_ISSOCK || __BEOS__ */
 #  endif /* !SVR4 && !SVR4_2 */
-#endif /* !HAVE_GETPEERNAME || SVR4_2 */
+#endif /* !HAVE_GETPEERNAME || SVR4_2 || __BEOS__ */
 }