From 011c1d1c0766c65517ebd495465c99e86edb63ec Mon Sep 17 00:00:00 2001
From: Stephen Hemminger <stephen.hemminger@vyatta.com>
Date: Mon, 11 Oct 2010 14:49:26 -0700
Subject: Update to bash-4.1

---
 builtins/shopt.def | 261 ++++++++++++++++++++++++++++++++++++++++++++---------
 1 file changed, 218 insertions(+), 43 deletions(-)

(limited to 'builtins/shopt.def')

diff --git a/builtins/shopt.def b/builtins/shopt.def
index ae100ac..68c7245 100644
--- a/builtins/shopt.def
+++ b/builtins/shopt.def
@@ -1,37 +1,44 @@
 This file is shopt.def, from which is created shopt.c.
 It implements the Bash `shopt' builtin.
 
-Copyright (C) 1994-2005 Free Software Foundation, Inc.
+Copyright (C) 1994-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 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/>.
 
 $PRODUCES shopt.c
 
 $BUILTIN shopt
 $FUNCTION shopt_builtin
-$SHORT_DOC shopt [-pqsu] [-o long-option] optname [optname...]
-Toggle the values of variables controlling optional behavior.
-The -s flag means to enable (set) each OPTNAME; the -u flag
-unsets each OPTNAME.  The -q flag suppresses output; the exit
-status indicates whether each OPTNAME is set or unset.  The -o
-option restricts the OPTNAMEs to those defined for use with
-`set -o'.  With no options, or with the -p option, a list of all
-settable options is displayed, with an indication of whether or
-not each is set.
+$SHORT_DOC shopt [-pqsu] [-o] [optname ...]
+Set and unset shell options.
+
+Change the setting of each shell option OPTNAME.  Without any option
+arguments, list all shell options with an indication of whether or not each
+is set. 
+
+Options:
+  -o	restrict OPTNAMEs to those defined for use with `set -o'
+  -p	print each shell option with an indication of its status
+  -q	suppress output
+  -s	enable (set) each OPTNAME
+  -u	disable (unset) each OPTNAME
+
+Exit Status:
+Returns success if OPTNAME is enabled; fails if an invalid option is
+given or OPTNAME is disabled.
 $END
 
 #include <config.h>
@@ -45,6 +52,8 @@ $END
 
 #include <stdio.h>
 
+#include "version.h"
+
 #include "../bashintl.h"
 
 #include "../shell.h"
@@ -52,6 +61,10 @@ $END
 #include "common.h"
 #include "bashgetopt.h"
 
+#if defined (HISTORY)
+#  include "../bashhist.h"
+#endif
+
 #define UNSETOPT	0
 #define SETOPT		1
 
@@ -68,20 +81,20 @@ extern int glob_ignore_case, match_ignore_case;
 extern int hup_on_exit;
 extern int xpg_echo;
 extern int gnu_error_format;
+extern int check_jobs_at_exit;
+extern int autocd;
+extern int glob_star;
 
 #if defined (EXTENDED_GLOB)
 extern int extended_glob;
 #endif
 
-#if defined (HISTORY)
-extern int literal_history, command_oriented_history;
-extern int force_append_history;
-#endif
-
 #if defined (READLINE)
 extern int hist_verify, history_reediting, perform_hostname_completion;
 extern int no_empty_command_completion;
 extern int force_fignore;
+extern int dircomplete_spelling;
+
 extern int enable_hostname_completion __P((int));
 #endif
 
@@ -99,27 +112,42 @@ extern int debugging_mode;
 
 static void shopt_error __P((char *));
 
-static int set_shellopts_after_change __P((int));
+static int set_shellopts_after_change __P((char *, int));
+static int shopt_enable_hostname_completion __P((char *, int));
+static int set_compatibility_level __P((char *, int));
 
 #if defined (RESTRICTED_SHELL)
-static int set_restricted_shell __P((int));
+static int set_restricted_shell __P((char *, int));
 #endif
 
 static int shopt_login_shell;
+static int shopt_compat31;
+static int shopt_compat32;
+static int shopt_compat40;
 
-typedef int shopt_set_func_t __P((int));
+typedef int shopt_set_func_t __P((char *, int));
 
 static struct {
   char *name;
   int  *value;
   shopt_set_func_t *set_func;
 } shopt_vars[] = {
+  { "autocd", &autocd, (shopt_set_func_t *)NULL },
   { "cdable_vars", &cdable_vars, (shopt_set_func_t *)NULL },
   { "cdspell", &cdspelling, (shopt_set_func_t *)NULL },
   { "checkhash", &check_hashed_filenames, (shopt_set_func_t *)NULL },
+#if defined (JOB_CONTROL)
+  { "checkjobs", &check_jobs_at_exit, (shopt_set_func_t *)NULL },
+#endif
   { "checkwinsize", &check_window_size, (shopt_set_func_t *)NULL },
 #if defined (HISTORY)
   { "cmdhist", &command_oriented_history, (shopt_set_func_t *)NULL },
+#endif
+  { "compat31", &shopt_compat31, set_compatibility_level },
+  { "compat32", &shopt_compat32, set_compatibility_level },
+  { "compat40", &shopt_compat40, set_compatibility_level },
+#if defined (READLINE)
+  { "dirspell", &dircomplete_spelling, (shopt_set_func_t *)NULL },
 #endif
   { "dotglob", &glob_dot_filenames, (shopt_set_func_t *)NULL },
   { "execfail", &no_exit_on_failed_exec, (shopt_set_func_t *)NULL },
@@ -135,6 +163,7 @@ static struct {
 #if defined (READLINE)
   { "force_fignore", &force_fignore, (shopt_set_func_t *)NULL },
 #endif
+  { "globstar", &glob_star, (shopt_set_func_t *)NULL },
   { "gnu_errfmt", &gnu_error_format, (shopt_set_func_t *)NULL },
 #if defined (HISTORY)
   { "histappend", &force_append_history, (shopt_set_func_t *)NULL },
@@ -142,7 +171,7 @@ static struct {
 #if defined (READLINE)
   { "histreedit", &history_reediting, (shopt_set_func_t *)NULL },
   { "histverify", &hist_verify, (shopt_set_func_t *)NULL },
-  { "hostcomplete", &perform_hostname_completion, enable_hostname_completion },
+  { "hostcomplete", &perform_hostname_completion, shopt_enable_hostname_completion },
 #endif
   { "huponexit", &hup_on_exit, (shopt_set_func_t *)NULL },
   { "interactive_comments", &interactive_comments, set_shellopts_after_change },
@@ -170,8 +199,12 @@ static struct {
   { (char *)0, (int *)0, (shopt_set_func_t *)NULL }
 };
 
-static char *on = "on";
-static char *off = "off";
+#define N_SHOPT_OPTIONS		(sizeof (shopt_vars) / sizeof (shopt_vars[0]))
+
+#define GET_SHOPT_OPTION_VALUE(i)	(*shopt_vars[i].value)
+
+static const char * const on = "on";
+static const char * const off = "off";
 
 static int find_shopt __P((char *));
 static int toggle_shopts __P((int, WORD_LIST *, int));
@@ -313,9 +346,11 @@ toggle_shopts (mode, list, quiet)
 	{
 	  *shopt_vars[ind].value = mode;	/* 1 for set, 0 for unset */
 	  if (shopt_vars[ind].set_func)
-	    (*shopt_vars[ind].set_func) (mode);
+	    (*shopt_vars[ind].set_func) (shopt_vars[ind].name, mode);
 	}
     }
+
+  set_bashopts ();
   return (rval);
 }
 
@@ -348,7 +383,7 @@ list_shopts (list, flags)
 	  if ((flags & QFLAG) == 0)
 	    print_shopt (shopt_vars[i].name, val, flags);
 	}
-      return (EXECUTION_SUCCESS);
+      return (sh_chkwrite (EXECUTION_SUCCESS));
     }
 
   for (l = list, rval = EXECUTION_SUCCESS; l; l = l->next)
@@ -367,7 +402,7 @@ list_shopts (list, flags)
 	print_shopt (l->word->word, val, flags);
     }
 
-  return (rval);
+  return (sh_chkwrite (rval));
 }
 
 static int
@@ -382,7 +417,7 @@ list_some_shopts (mode, flags)
       if (((flags & QFLAG) == 0) && mode == val)
 	print_shopt (shopt_vars[i].name, val, flags);
     }
-  return (EXECUTION_SUCCESS);
+  return (sh_chkwrite (EXECUTION_SUCCESS));
 }
 
 static int
@@ -397,7 +432,7 @@ list_shopt_o_options (list, flags)
     {
       if ((flags & QFLAG) == 0)
 	list_minus_o_opts (-1, (flags & PFLAG));
-      return (EXECUTION_SUCCESS);
+      return (sh_chkwrite (EXECUTION_SUCCESS));
     }
 
   for (l = list, rval = EXECUTION_SUCCESS; l; l = l->next)
@@ -419,7 +454,7 @@ list_shopt_o_options (list, flags)
 	    printf (OPTFMT, l->word->word, val ? on : off);
 	}
     }
-  return (rval);
+  return (sh_chkwrite (rval));
 }
 
 static int
@@ -428,7 +463,7 @@ list_some_o_options (mode, flags)
 {
   if ((flags & QFLAG) == 0)
     list_minus_o_opts (mode, (flags & PFLAG));
-  return (EXECUTION_SUCCESS);
+  return (sh_chkwrite (EXECUTION_SUCCESS));
 }
 
 static int
@@ -452,18 +487,56 @@ set_shopt_o_options (mode, list, quiet)
 /* If we set or unset interactive_comments with shopt, make sure the
    change is reflected in $SHELLOPTS. */
 static int
-set_shellopts_after_change (mode)
+set_shellopts_after_change (option_name, mode)
+     char *option_name;
      int mode;
 {
   set_shellopts ();
   return (0);
 }
 
+static int
+shopt_enable_hostname_completion (option_name, mode)
+     char *option_name;
+     int mode;
+{
+  return (enable_hostname_completion (mode));
+}
+
+static int
+set_compatibility_level (option_name, mode)
+     char *option_name;
+     int mode;
+{
+  /* Need to change logic here as we add more compatibility levels */
+
+  /* First, check option_name so we can turn off other compat options when
+     one is set. */
+  if (mode && option_name[6] == '3' && option_name[7] == '1')
+    shopt_compat32 = shopt_compat40 = 0;
+  else if (mode && option_name[6] == '3' && option_name[7] == '2')
+    shopt_compat31 = shopt_compat40 = 0;
+  else if (mode && option_name[6] == '4' && option_name[7] == '0')
+    shopt_compat31 = shopt_compat32 = 0;
+
+  /* Then set shell_compatibility_level based on what remains */
+  if (shopt_compat31)
+    shell_compatibility_level = 31;
+  else if (shopt_compat32)
+    shell_compatibility_level = 32;
+  else if (shopt_compat40)
+    shell_compatibility_level = 40;
+  else
+    shell_compatibility_level = DEFAULT_COMPAT_LEVEL;
+  return 0;
+}
+
 #if defined (RESTRICTED_SHELL)
 /* Don't allow the value of restricted_shell to be modified. */
 
 static int
-set_restricted_shell (mode)
+set_restricted_shell (option_name, mode)
+     char *option_name;
      int mode;
 {
   static int save_restricted = -1;
@@ -478,7 +551,8 @@ set_restricted_shell (mode)
 
 /* Not static so shell.c can call it to initialize shopt_login_shell */
 int
-set_login_shell (mode)
+set_login_shell (option_name, mode)
+     char *option_name;
      int mode;
 {
   shopt_login_shell = login_shell != 0;
@@ -536,5 +610,106 @@ shopt_listopt (name, reusable)
     }
 
   print_shopt (name, *shopt_vars[i].value, reusable ? PFLAG : 0);
-  return (EXECUTION_SUCCESS);
+  return (sh_chkwrite (EXECUTION_SUCCESS));
+}
+
+void
+set_bashopts ()
+{
+  char *value;
+  char tflag[N_SHOPT_OPTIONS];
+  int vsize, i, vptr, *ip, exported;
+  SHELL_VAR *v;
+
+  for (vsize = i = 0; shopt_vars[i].name; i++)
+    {
+      tflag[i] = 0;
+      if (GET_SHOPT_OPTION_VALUE (i))
+	{
+	  vsize += strlen (shopt_vars[i].name) + 1;
+	  tflag[i] = 1;
+	}
+    }
+
+  value = (char *)xmalloc (vsize + 1);
+
+  for (i = vptr = 0; shopt_vars[i].name; i++)
+    {
+      if (tflag[i])
+	{
+	  strcpy (value + vptr, shopt_vars[i].name);
+	  vptr += strlen (shopt_vars[i].name);
+	  value[vptr++] = ':';
+	}
+    }
+
+  if (vptr)
+    vptr--;			/* cut off trailing colon */
+  value[vptr] = '\0';
+
+  v = find_variable ("BASHOPTS");
+
+  /* Turn off the read-only attribute so we can bind the new value, and
+     note whether or not the variable was exported. */
+  if (v)
+    {
+      VUNSETATTR (v, att_readonly);
+      exported = exported_p (v);
+    }
+  else
+    exported = 0;
+
+  v = bind_variable ("BASHOPTS", value, 0);
+
+  /* Turn the read-only attribute back on, and turn off the export attribute
+     if it was set implicitly by mark_modified_vars and SHELLOPTS was not
+     exported before we bound the new value. */
+  VSETATTR (v, att_readonly);
+  if (mark_modified_vars && exported == 0 && exported_p (v))
+    VUNSETATTR (v, att_exported);
+
+  free (value);
+}
+
+void
+parse_bashopts (value)
+     char *value;
+{
+  char *vname;
+  int vptr, ind;
+
+  vptr = 0;
+  while (vname = extract_colon_unit (value, &vptr))
+    {
+      ind = find_shopt (vname);
+      if (ind >= 0)
+        *shopt_vars[ind].value = 1;
+      free (vname);
+    }
+}
+
+void
+initialize_bashopts (no_bashopts)
+     int no_bashopts;
+{
+  char *temp;
+  SHELL_VAR *var;
+
+  if (no_bashopts == 0)
+    {
+      var = find_variable ("BASHOPTS");
+      /* set up any shell options we may have inherited. */
+      if (var && imported_p (var))
+	{
+	  temp = (array_p (var) || assoc_p (var)) ? (char *)NULL : savestring (value_cell (var));
+	  if (temp)
+	    {
+	      parse_bashopts (temp);
+	      free (temp);
+	    }
+	}
+    }
+
+  /* Set up the $BASHOPTS variable. */
+  set_bashopts ();
 }
-- 
cgit v1.2.3

