No specific user configuration
[platform/upstream/bash.git] / flags.c
diff --git a/flags.c b/flags.c
index 8d2f7f5..eaec9aa 100644 (file)
--- a/flags.c
+++ b/flags.c
@@ -1,25 +1,24 @@
 /* flags.c -- Everything about flags except the `set' command.  That
    is in builtins.c */
 
-/* Copyright (C) 1987,1989 Free Software Foundation, Inc.
+/* Copyright (C) 1987-2009 Free Software Foundation, Inc.
 
-This file is part of GNU Bash, the Bourne Again SHell.
+   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 2, 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, 59 Temple Place, Suite 330, Boston, MA 02111 USA. */
+   You should have received a copy of the GNU General Public License
+   along with Bash.  If not, see <http://www.gnu.org/licenses/>.
+*/
 
-/* Flags hacking. */
 #include "config.h"
 #if defined (HAVE_UNISTD_H)
 #  include <unistd.h>
@@ -28,20 +27,27 @@ Foundation, 59 Temple Place, Suite 330, Boston, MA 02111 USA. */
 #include "shell.h"
 #include "flags.h"
 
+#if defined (BANG_HISTORY)
+#  include "bashhist.h"
+#endif
+
 #if defined (JOB_CONTROL)
-extern int set_job_control ();
+extern int set_job_control __P((int));
 #endif
 
 #if defined (RESTRICTED_SHELL)
 extern char *shell_name;
 #endif
 
+extern int shell_initialized;
+extern int builtin_ignoring_errexit;
+
 /* -c, -s invocation options -- not really flags, but they show up in $- */
 extern int want_pending_command, read_from_stdin;
 
 /* **************************************************************** */
 /*                                                                 */
-/*                     The Standard Sh Flags.                      */
+/*                     The Standard sh Flags.                      */
 /*                                                                 */
 /* **************************************************************** */
 
@@ -54,7 +60,9 @@ int mark_modified_vars = 0;
 int asynchronous_notification = 0;
 
 /* Non-zero means exit immediately if a command exits with a non-zero
-   exit status. */
+   exit status.  The first is what controls set -e; the second is what
+   bash uses internally. */
+int errexit_flag = 0;
 int exit_immediately_on_error = 0;
 
 /* Non-zero means disable filename globbing. */
@@ -67,7 +75,7 @@ int place_keywords_in_env = 0;
 
 /* Non-zero means read commands, but don't execute them.  This is useful
    for debugging shell scripts that should do something hairy and possibly
-   desctructive. */
+   destructive. */
 int read_but_dont_execute = 0;
 
 /* Non-zero means end of file is after one command. */
@@ -121,7 +129,11 @@ int hashing_enabled = 1;
 #if defined (BANG_HISTORY)
 /* Non-zero means that we are doing history expansion.  The default.
    This means !22 gets the 22nd line of history. */
+#  if defined (STRICT_POSIX)
+int history_expansion = 0;
+#  else
 int history_expansion = 1;
+#  endif
 #endif /* BANG_HISTORY */
 
 /* Non-zero means that we allow comments to appear in interactive commands. */
@@ -147,19 +159,30 @@ int privileged_mode = 0;
 int brace_expansion = 1;
 #endif
 
+/* Non-zero means that shell functions inherit the DEBUG trap. */
+int function_trace_mode = 0;
+
+/* Non-zero means that shell functions inherit the ERR trap. */
+int error_trace_mode = 0;
+
+/* Non-zero means that the rightmost non-zero exit status in a pipeline
+   is the exit status of the entire pipeline.  If each processes exits
+   with a 0 status, the status of the pipeline is 0. */
+int pipefail_opt = 0;
+
 /* **************************************************************** */
 /*                                                                 */
 /*                     The Flags ALIST.                            */
 /*                                                                 */
 /* **************************************************************** */
 
-struct flags_alist shell_flags[] = {
+const struct flags_alist shell_flags[] = {
   /* Standard sh flags. */
   { 'a', &mark_modified_vars },
 #if defined (JOB_CONTROL)
   { 'b', &asynchronous_notification },
 #endif /* JOB_CONTROL */
-  { 'e', &exit_immediately_on_error },
+  { 'e', &errexit_flag },
   { 'f', &disallow_filename_globbing },
   { 'h', &hashing_enabled },
   { 'i', &forced_interactive },
@@ -176,29 +199,29 @@ struct flags_alist shell_flags[] = {
   { 'u', &unbound_vars_is_error },
   { 'v', &echo_input_at_read },
   { 'x', &echo_command_at_execute },
-  { 'C', &noclobber },
 
   /* New flags that control non-standard things. */
 #if 0
   { 'l', &lexical_scoping },
 #endif
-  { 'I', &no_invisible_vars },
-
-  { 'P', &no_symbolic_links },
-
 #if defined (BRACE_EXPANSION)
   { 'B', &brace_expansion },
 #endif
-
+  { 'C', &noclobber },
+  { 'E', &error_trace_mode },
 #if defined (BANG_HISTORY)
   { 'H', &history_expansion },
 #endif /* BANG_HISTORY */
-
+  { 'I', &no_invisible_vars },
+  { 'P', &no_symbolic_links },
+  { 'T', &function_trace_mode },
   {0, (int *)NULL}
 };
 
 #define NUM_SHELL_FLAGS (sizeof (shell_flags) / sizeof (struct flags_alist))
 
+char optflags[NUM_SHELL_FLAGS+4] = { '+' };
+
 int *
 find_flag (name)
      int name;
@@ -234,37 +257,46 @@ change_flag (flag, on_or_off)
     return (FLAG_ERROR);
 
   old_value = *value;
-
   *value = (on_or_off == FLAG_ON) ? 1 : 0;
 
   /* Special cases for a few flags. */
   switch (flag)
     {
+#if defined (BANG_HISTORY)
+    case 'H':
+      if (on_or_off == FLAG_ON)
+       bash_initialize_history ();
+      break;
+#endif
+
 #if defined (JOB_CONTROL)
     case 'm':
       set_job_control (on_or_off == FLAG_ON);
       break;
 #endif /* JOB_CONTROL */
 
-#if defined (RESTRICTED_SHELL)
-    case 'r':
-      if (on_or_off == FLAG_ON)
-       maybe_make_restricted (shell_name);
+    case 'e':
+      if (builtin_ignoring_errexit == 0)
+       exit_immediately_on_error = errexit_flag;
       break;
-#endif
 
-#if defined (BANG_HISTORY)
-    case 'H':
-      if (on_or_off == FLAG_ON)
-       bash_initialize_history ();
+    case 'n':
+      if (interactive_shell)
+       read_but_dont_execute = 0;
       break;
-#endif
 
     case 'p':
       if (on_or_off == FLAG_OFF)
        disable_priv_mode ();
+      break;
 
+#if defined (RESTRICTED_SHELL)
+    case 'r':
+      if (on_or_off == FLAG_ON && shell_initialized)
+       maybe_make_restricted (shell_name);
       break;
+#endif
+
     }
 
   return (old_value);
@@ -278,7 +310,7 @@ which_set_flags ()
   char *temp;
   int i, string_index;
 
-  temp = xmalloc (1 + NUM_SHELL_FLAGS + read_from_stdin + want_pending_command);
+  temp = (char *)xmalloc (1 + NUM_SHELL_FLAGS + read_from_stdin + want_pending_command);
   for (i = string_index = 0; shell_flags[i].name; i++)
     if (*(shell_flags[i].value))
       temp[string_index++] = shell_flags[i].name;
@@ -299,7 +331,7 @@ reset_shell_flags ()
   place_keywords_in_env = read_but_dont_execute = just_one_command = 0;
   noclobber = unbound_vars_is_error = echo_input_at_read = 0;
   echo_command_at_execute = jobs_m_flag = forced_interactive = 0;
-  no_symbolic_links = no_invisible_vars = privileged_mode = 0;
+  no_symbolic_links = no_invisible_vars = privileged_mode = pipefail_opt = 0;
 
   hashing_enabled = interactive_comments = 1;
 
@@ -319,3 +351,15 @@ reset_shell_flags ()
   restricted = 0;
 #endif
 }
+
+void
+initialize_flags ()
+{
+  register int i;
+
+  for (i = 0; shell_flags[i].name; i++)
+    optflags[i+1] = shell_flags[i].name;
+  optflags[++i] = 'o';
+  optflags[++i] = ';';
+  optflags[i+1] = '\0';
+}