Skip to content

Commit

Permalink
Tweaks to run bash_completion
Browse files Browse the repository at this point in the history
- Implement shopt -q
- no-op stub for shopt -s hostcomplete (it's on by default anyway)

Also:

- iterate over args in 'complete' builtin
  • Loading branch information
Andy Chu committed Sep 22, 2018
1 parent b22f756 commit dde0c6e
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 23 deletions.
12 changes: 12 additions & 0 deletions core/builtin.py
Original file line number Diff line number Diff line change
Expand Up @@ -774,6 +774,7 @@ def Set(argv, exec_opts, mem):
SHOPT_SPEC.ShortFlag('-u') # unset
SHOPT_SPEC.ShortFlag('-o') # use 'set -o' up names
SHOPT_SPEC.ShortFlag('-p') # print
SHOPT_SPEC.ShortFlag('-q') # query option settings


def Shopt(argv, exec_opts):
Expand All @@ -786,6 +787,17 @@ def Shopt(argv, exec_opts):
exec_opts.ShowShoptOptions(argv[i:])
return 0

if arg.q: # query values
status = 0
for opt_name in argv[i:]:
if not hasattr(exec_opts, opt_name): # Invalid option
status = 1 # bash does this, 2 would be better
break
if not getattr(exec_opts, opt_name):
status = 1
break
return status

b = None
if arg.s:
b = True
Expand Down
44 changes: 22 additions & 22 deletions core/comp_builtins.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,33 +69,33 @@ def Complete(argv, ex, funcs, comp_lookup):
arg_r = args.Reader(argv)
arg = COMPLETE_SPEC.Parse(arg_r)
# TODO: process arg.opt_changes
log('arg %s', arg)
#log('arg %s', arg)

commands = arg_r.Rest()
if not commands:
raise args.UsageError('missing required commands')
command = commands[0] # TODO: loop over commands

# NOTE: bash doesn't actually check the name until completion time, but
# obviously it's better to check here.
if arg.F:
func_name = arg.F
func = funcs.get(func_name)
if func is None:
print('Function %r not found' % func_name)
return 1

chain = completion.ShellFuncAction(ex, func)
comp_lookup.RegisterName(command, chain)

# TODO: Some feedback would be nice?
# Should we show an error like this? Or maybe a warning? Maybe
# comp_lookup has readline_mod?
# util.error('Oil was not built with readline/completion.')
else:
pass

return 0
for command in commands:
# NOTE: bash doesn't actually check the name until completion time, but
# obviously it's better to check here.
if arg.F:
func_name = arg.F
func = funcs.get(func_name)
if func is None:
print('Function %r not found' % func_name)
return 1

chain = completion.ShellFuncAction(ex, func)
comp_lookup.RegisterName(command, chain)

# TODO: Some feedback would be nice?
# Should we show an error like this? Or maybe a warning? Maybe
# comp_lookup has readline_mod?
# util.error('Oil was not built with readline/completion.')
else:
pass

return 0


COMPGEN_SPEC = args.FlagsAndOptions() # for -o and -A
Expand Down
3 changes: 2 additions & 1 deletion core/state.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ def __init__(self, mem, readline):
self.expand_aliases = False
self.extglob = False # No-op for bash compatibility.
self.progcomp = False # ditto
self.hostcomplete = False # ditto, for words with '@'

#
# OSH-specific options that are not yet implemented.
Expand Down Expand Up @@ -260,7 +261,7 @@ def SetOption(self, opt_name, b):
self.mem.InternalSetGlobal('SHELLOPTS', new_val)

SHOPT_OPTIONS = ('nullglob', 'failglob', 'expand_aliases', 'extglob',
'progcomp')
'progcomp', 'hostcomplete')

def SetShoptOption(self, opt_name, b):
""" For shopt -s/-u. """
Expand Down
42 changes: 42 additions & 0 deletions spec/sh-options.test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -242,3 +242,45 @@ set -o | grep set
#### set without args lists variables
set | grep PWD
## status: 0

#### shopt -q
shopt -q nullglob
echo nullglob=$?

# set it
shopt -s nullglob

shopt -q nullglob
echo nullglob=$?

shopt -q nullglob failglob
echo nullglob,failglob=$?

# set it
shopt -s failglob
shopt -q nullglob failglob
echo nullglob,failglob=$?

## STDOUT:
nullglob=1
nullglob=0
nullglob,failglob=1
nullglob,failglob=0
## END
## N-I dash/mksh STDOUT:
nullglob=127
nullglob=127
nullglob,failglob=127
nullglob,failglob=127
## END

#### shopt -q invalid
shopt -q invalidZZ
echo invalidZZ=$?
## STDOUT:
invalidZZ=1
## END
## N-I dash/mksh STDOUT:
invalidZZ=127
## END

0 comments on commit dde0c6e

Please sign in to comment.