Permalink
Browse files

Improve error message when the shell can't execute a command.

Also, the exit code is now 126 when permission is denied, as specified
by POSIX.

Also: tiny tweaks to 'alias'.  Realized that we need to parse
incrementally.
  • Loading branch information...
Andy Chu
Andy Chu committed Sep 1, 2018
1 parent b4ea3e8 commit 1f9d6f6eec9ee1db130aa74c2e838403ed48f4d2
Showing with 36 additions and 12 deletions.
  1. +2 −3 core/builtin.py
  2. +14 −6 core/process.py
  3. +5 −1 osh/cmd_parse.py
  4. +14 −0 spec/command_.test.sh
  5. +1 −2 test/spec.sh
View
@@ -999,7 +999,6 @@ def Alias(argv, aliases):
for arg in argv:
parts = arg.split('=', 1)
log(' p %s', parts)
if len(parts) == 1:
name = parts[0]
alias_exp = aliases.get(name)
@@ -1011,8 +1010,8 @@ def Alias(argv, aliases):
name, alias_exp = parts
aliases[name] = alias_exp
print(argv)
print(aliases)
#print(argv)
#log('AFTER ALIAS %s', aliases)
return 0
View
@@ -323,14 +323,22 @@ def ExecExternalProgram(argv, environ):
try:
os.execvpe(argv[0], argv, environ)
except OSError as e:
log('Unexpected error in execvpe(%r, %r, ...): %s', argv[0], argv, e)
util.error('%r: %s', argv[0], os.strerror(e.errno))
# POSIX mentions 126 and 127 for two specific errors. The rest are
# unspecified.
#
# http://pubs.opengroup.org/onlinepubs/9699919799.2016edition/utilities/V3_chap02.html#tag_18_08_02
# TODO:
# - Make this error look better.
# - What about permission errors?
if e.errno == errno.EACCES:
status = 126
elif e.errno == errno.ENOENT:
status = 127 # e.g. command not found should be 127.
else:
# dash uses 2, but we use that for parse errors. This seems to be
# consistent with mksh and zsh.
status = 127
# Command not found means 127.
sys.exit(127)
sys.exit(status)
# no return
View
@@ -125,7 +125,10 @@ def __init__(self, w_parser, lexer, line_reader, arena, aliases=None):
self.lexer = lexer # for fast lookahead to (, for function defs
self.line_reader = line_reader # for here docs
self.arena = arena # for adding here doc spans
self.aliases = aliases or {} # aliases to expand at parse time
if aliases is None:
self.aliases = {}
else:
self.aliases = aliases # aliases to expand at parse time
self.Reset()
@@ -1201,6 +1204,7 @@ def ParseCommand(self):
ok, word_str, quoted = word.StaticEval(self.cur_word)
if ok and not quoted:
alias_exp = self.aliases.get(word_str)
#log('AT PARSE TIME %s', self.aliases)
if alias_exp is not None:
log('expand %s -> %s', word_str, alias_exp)
View
@@ -6,3 +6,17 @@
{ which ls; }
## stdout: /bin/ls
#### Permission denied
touch $TMP/text-file
$TMP/text-file
## status: 126
#### Not a dir
$TMP/not-a-dir/text-file
## status: 127
#### Name too long
./0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789
## status: 127
## OK dash status: 2
## OK bash status: 126
View
@@ -371,8 +371,7 @@ command-sub() {
}
command_() {
sh-spec spec/command_.test.sh \
${REF_SHELLS[@]} $OSH_LIST "$@"
sh-spec spec/command_.test.sh ${REF_SHELLS[@]} $ZSH $OSH_LIST "$@"
}
pipeline() {

0 comments on commit 1f9d6f6

Please sign in to comment.