Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

shopt -p and -o compatibility with bash #2013

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions builtin/pure_osh.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ def Run(self, cmd_val):
# 'set -o' shows options. This is actually used by autoconf-generated
# scripts!
if arg.show_options:
self.exec_opts.ShowOptions([])
self.exec_opts.ShowOptions([], False)
return 0

# Note: set -o nullglob is not valid. The 'shopt' builtin is preferred in
Expand All @@ -193,12 +193,12 @@ def __init__(self, mutable_opts, cmd_ev):
self.mutable_opts = mutable_opts
self.cmd_ev = cmd_ev

def _PrintOptions(self, use_set_opts, opt_names):
# type: (bool, List[str]) -> None
def _PrintOptions(self, use_set_opts, as_command, opt_names):
# type: (bool, bool, List[str]) -> None
if use_set_opts:
self.mutable_opts.ShowOptions(opt_names)
self.mutable_opts.ShowOptions(opt_names, as_command)
else:
self.mutable_opts.ShowShoptOptions(opt_names)
self.mutable_opts.ShowShoptOptions(opt_names, as_command)

def Run(self, cmd_val):
# type: (cmd_value.Argv) -> int
Expand All @@ -223,10 +223,10 @@ def Run(self, cmd_val):
elif arg.u:
b = False
elif arg.p: # explicit -p
self._PrintOptions(arg.o, opt_names)
self._PrintOptions(arg.o, arg.p, opt_names)
return 0
else: # otherwise -p is implicit
self._PrintOptions(arg.o, opt_names)
self._PrintOptions(arg.o, arg.p, opt_names)
return 0

# shopt --set x { my-block }
Expand Down
5 changes: 3 additions & 2 deletions core/shell.py
Original file line number Diff line number Diff line change
Expand Up @@ -357,8 +357,9 @@ def Main(
state.InitMem(mem, environ, version_str)

if attrs.show_options: # special case: sh -o
mutable_opts.ShowOptions([])
return 0
mutable_opts.ShowOptions([], False)
# removed return as sh -o does not return
# return 0

# Set these BEFORE processing flags, so they can be overridden.
if lang == 'ysh':
Expand Down
21 changes: 13 additions & 8 deletions core/state.py
Original file line number Diff line number Diff line change
Expand Up @@ -670,9 +670,9 @@ def SetAnyOption(self, opt_name, b):

self._SetArrayByNum(opt_num, b)

def ShowOptions(self, opt_names):
# type: (List[str]) -> None
"""For 'set -o' and 'shopt -p -o'."""
def ShowOptions(self, opt_names, as_command):
# type: (List[str], bool) -> None
"""For 'set -o' and 'shopt -p -o' and 'shopt -o'."""
# TODO: Maybe sort them differently?

if len(opt_names) == 0: # if none, supplied, show all
Expand All @@ -681,10 +681,13 @@ def ShowOptions(self, opt_names):
for opt_name in opt_names:
opt_num = _SetOptionNum(opt_name)
b = self.Get(opt_num)
print('set %so %s' % ('-' if b else '+', opt_name))
if as_command:
print('set %so %s' % ('-' if b else '+', opt_name))
else:
print('%-16s\t%s' % (opt_name, 'on' if b else 'off'))

def ShowShoptOptions(self, opt_names):
# type: (List[str]) -> None
def ShowShoptOptions(self, opt_names, as_command):
# type: (List[str], bool) -> None
"""For 'shopt -p'."""

# Respect option groups.
Expand Down Expand Up @@ -712,8 +715,10 @@ def ShowShoptOptions(self, opt_names):

for opt_num in opt_nums:
b = self.Get(opt_num)
print('shopt -%s %s' %
('s' if b else 'u', consts.OptionName(opt_num)))
if as_command:
print('shopt -%s %s' % ('s' if b else 'u', consts.OptionName(opt_num)))
else:
print('%-20s\t%s' % (consts.OptionName(opt_num), 'on' if b else 'off', ))


class _ArgFrame(object):
Expand Down
24 changes: 24 additions & 0 deletions spec/builtin-shopt-bash.test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
## compare_shells: bash
## oils_failures_allowed: 0

# builtin-shopt-bash.test.sh

#### shopt
shopt | grep inherit_errexit | tr -d ' '
## stdout-json: "inherit_errexit\toff\n"

#### shopt -p
shopt -p | grep inherit_errexit
## STDOUT:
shopt -u inherit_errexit
## END

#### shopt -o
shopt -o | grep errexit | tr -d ' '
## stdout-json: "errexit\toff\n"

#### shopt -p -o
shopt -p -o | grep errexit
## STDOUT:
set +o errexit
## END
4 changes: 4 additions & 0 deletions test/spec.sh
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,10 @@ builtin-trap-bash() {
run-file builtin-trap-bash "$@"
}

builtin-shopt-bash() {
run-file builtin-shopt-bash "$@"
}

# Bash implements type -t, but no other shell does. For Nix.
# zsh/mksh/dash don't have the 'help' builtin.
builtin-bash() {
Expand Down
Loading