From 6ee9b758c5481e0950b48b15ede49b5bebb4b90c Mon Sep 17 00:00:00 2001
Date: Sat, 30 Apr 2016 06:55:57 -0400
Subject: [PATCH 1/1] Properly reset options for run of #!-less script

The following options that previously leaked are now properly reset:
autocd checkjobs complete_fullquote direxpand dirspell errexit errtrace
expand_aliases extdebug extquote failglob force_fignore functrace
globasciiranges globstar gnu_errfmt huponexit inherit_errexit lastpipe
no_empty_cmd_completion nocaseglob nocasematch posix progcomp xpg_echo

The following #defines are now respected:
DIRCOMPLETE_EXPAND_DEFAULT DEFAULT_ECHO_TO_XPG STRICT_POSIX
GLOBASCII_DEFAULT EXTGLOB_DEFAULT
---
 builtins/set.def   | 17 ++++++++++--
 builtins/shopt.def | 81 ++++++++++++++++++++++++++++++++++++++++++++----------
 flags.c            | 58 +++++++++++++++++++++++++-------------
 3 files changed, 120 insertions(+), 36 deletions(-)

diff --git a/builtins/set.def b/builtins/set.def
index 829c659..8c91eff 100644
--- a/builtins/set.def
+++ b/builtins/set.def
@@ -623,10 +623,23 @@ initialize_shell_options (no_shellopts)
 void
 reset_shell_options ()
 {
+  /* Options in o_options with variables defined here */
 #if defined (HISTORY)
-  remember_on_history = enable_history_list = 1;
-#endif
+  enable_history_list = 1;
+#endif  /* HISTORY */
   ignoreeof = 0;
+#if defined (HISTORY)
+  dont_save_function_defs = 0;
+#endif  /* HISTORY */
+  pipefail_opt = 0;
+#if defined (STRICT_POSIX)
+  posixly_correct = 1;
+#else
+  posixly_correct = 0;
+#endif  /* STRICT_POSIX */
+
+  /* Other variables defined here */
+  remember_on_history = enable_history_list;
 }
 
 /* Set some flags from the word values in the input list.  If LIST is empty,
diff --git a/builtins/shopt.def b/builtins/shopt.def
index c4a75ea..cfea40e 100644
--- a/builtins/shopt.def
+++ b/builtins/shopt.def
@@ -308,30 +308,81 @@ shopt_builtin (list)
 void
 reset_shopt_options ()
 {
-  allow_null_glob_expansion = glob_dot_filenames = 0;
-  cdable_vars = mail_warning = 0;
-  no_exit_on_failed_exec = print_shift_error = 0;
-  check_hashed_filenames = cdspelling = expand_aliases = 0;
-
-  source_uses_path = promptvars = 1;
-
+  autocd = 0;
+  cdable_vars = 0;
+  cdspelling = 0;
+  check_hashed_filenames = CHECKHASH_DEFAULT;
+#if defined (JOB_CONTROL)
+  check_jobs_at_exit = 0;
+#endif
   check_window_size = CHECKWINSIZE_DEFAULT;
-
+#if defined (HISTORY)
+  command_oriented_history = 1;
+#endif
+  /* don't care about compat */
+#if defined (READLINE)
+  complete_fullquote = 1;
+#if DIRCOMPLETE_EXPAND_DEFAULT
+  dircomplete_expand = 1;
+#else
+  dircomplete_expand = 0;
+#endif
+  dircomplete_spelling = 0;
+#endif /* READLINE */
+  glob_dot_filenames = 0;
+  no_exit_on_failed_exec = 0;
+  expand_aliases = 0;
+#if defined (DEBUGGER)
+  debugging_mode = 0;
+#endif
 #if defined (EXTENDED_GLOB)
-  extended_glob = 0;
+  extended_glob = EXTGLOB_DEFAULT;
 #endif
-
+  extended_quote = 1;
+  fail_glob_expansion = 0;
+#if defined (READLINE)
+  force_fignore = 1;
+#endif
+  glob_asciirange = GLOBASCII_DEFAULT;
+  glob_star = 0;
+  gnu_error_format = 0;
 #if defined (HISTORY)
-  literal_history = force_append_history = 0;
-  command_oriented_history = 1;
+  force_append_history = 0;
 #endif
-
 #if defined (READLINE)
-  hist_verify = history_reediting = 0;
+  history_reediting = 0;
+  hist_verify = 0;
   perform_hostname_completion = 1;
 #endif
-
+  hup_on_exit = 0;
+  inherit_errexit = 0;
+  interactive_comments = 1;
+  lastpipe_opt = 0;
+#if defined (HISTORY)
+  literal_history = 0;
+#endif
   shopt_login_shell = login_shell;
+  mail_warning = 0;
+#if defined (READLINE)
+  no_empty_command_completion = 0;
+#endif
+  glob_ignore_case = 0;
+  match_ignore_case = 0;
+  allow_null_glob_expansion = 0;
+#if defined (PROGRAMMABLE_COMPLETION)
+  prog_completion_enabled = 1;
+#endif
+  promptvars = 1;
+#if defined (RESTRICTED_SHELL)
+  restricted_shell = 0;
+#endif
+  print_shift_error = 0;
+  source_uses_path = 1;
+#if defined (DEFAULT_ECHO_TO_XPG) || defined (STRICT_POSIX)
+  xpg_echo = 0;
+#else
+  xpg_echo = 0;
+#endif
 }
 
 static int
diff --git a/flags.c b/flags.c
index 5704741..cbcc14c 100644
--- a/flags.c
+++ b/flags.c
@@ -356,29 +356,49 @@ set_current_flags (bitmap)
 void
 reset_shell_flags ()
 {
-  mark_modified_vars = exit_immediately_on_error = disallow_filename_globbing = 0;
-  place_keywords_in_env = read_but_dont_execute = just_one_command = 0;
-  noclobber = unbound_vars_is_error = echo_input_at_read = verbose_flag = 0;
-  echo_command_at_execute = jobs_m_flag = forced_interactive = 0;
-  no_symbolic_links = no_invisible_vars = privileged_mode = pipefail_opt = 0;
-
-  hashing_enabled = interactive_comments = 1;
-
+  /* flags from flags_alist */
+  mark_modified_vars = 0;
 #if defined (JOB_CONTROL)
   asynchronous_notification = 0;
-#endif
-
-#if defined (BANG_HISTORY)
-  history_expansion = 1;
-#endif
-
-#if defined (BRACE_EXPANSION)
-  brace_expansion = 1;
-#endif
-
+#endif /* JOB_CONTROL */
+  errexit_flag = 0;
+  disallow_filename_globbing = 0;
+  hashing_enabled = 1;
+  forced_interactive = 0;
+  place_keywords_in_env = 0;
+#if defined (JOB_CONTROL)
+  jobs_m_flag = 0;
+#endif /* JOB_CONTROL */
+  read_but_dont_execute = 0;
+  privileged_mode = 0;
 #if defined (RESTRICTED_SHELL)
   restricted = 0;
-#endif
+#endif /* RESTRICTED_SHELL */
+  just_one_command = 0;
+  unbound_vars_is_error = 0;
+  verbose_flag = 0;
+  echo_command_at_execute = 0;
+#if defined (BRACE_EXPANSION)
+  brace_expansion = 1;
+#endif /* BRACE_EXPANSION */
+  noclobber = 0;
+  error_trace_mode = 0;
+#if defined (BANG_HISTORY)
+#  if defined (STRICT_POSIX)
+  history_expansion = 0;
+#  else
+  history_expansion = 1;
+#  endif /* STRICT_POSIX */
+#endif /* BANG_HISTORY */
+  no_invisible_vars = 0;
+  no_symbolic_links = 0;
+#if defined (DEBUGGER)
+  function_trace_mode = 0;
+#endif /* DEBUGGER */
+
+  /* other variables set here */
+  exit_immediately_on_error = 0;
+  echo_input_at_read = 0;
 }
 
 void
-- 
2.8.1

