Permalink
Browse files

Implement the pwd builtin (#111)

With spec tests.
  • Loading branch information...
BatmanAoD authored and andychu committed May 18, 2018
1 parent 179be3f commit c5a61b5b9ef244f337ae00b3ff90e748073996aa
Showing with 45 additions and 6 deletions.
  1. +20 −0 core/builtin.py
  2. +9 −6 core/cmd_exec.py
  3. +1 −0 core/runtime.asdl
  4. +15 −0 spec/builtins.test.sh
View
@@ -83,6 +83,7 @@
"pushd": builtin_e.PUSHD,
"popd": builtin_e.POPD,
"dirs": builtin_e.DIRS,
"pwd": builtin_e.PWD,
"source": builtin_e.SOURCE, # note that . alias is special
@@ -636,6 +637,25 @@ def Dirs(argv, home_dir, dir_stack):
return 0
PWD_SPEC = _Register('pwd')
PWD_SPEC.ShortFlag('-L')
PWD_SPEC.ShortFlag('-P')
def Pwd(argv, mem):
arg, i = PWD_SPEC.Parse(argv)
pwd = mem.GetVar('PWD').s
# `-L` is the default behavior; no need to check it
# TODO: ensure that if multiple flags are provided, the *last* one overrides
# the others
if arg.P:
pwd = os.path.realpath(pwd)
print(pwd)
return 0
EXPORT_SPEC = _Register('export')
EXPORT_SPEC.ShortFlag('-n')
View
@@ -119,7 +119,7 @@ def __init__(self, mem, fd_state, status_lines, funcs, completion,
"""
self.mem = mem
self.fd_state = fd_state
self.status_lines = status_lines
self.status_lines = status_lines
# function space is different than var space. Not hierarchical.
self.funcs = funcs
self.completion = completion
@@ -317,6 +317,9 @@ def _RunBuiltin(self, builtin_id, argv):
elif builtin_id == builtin_e.DIRS:
status = builtin.Dirs(argv, self.mem.GetVar('HOME'), self.dir_stack)
elif builtin_id == builtin_e.PWD:
status = builtin.Pwd(argv, self.mem)
elif builtin_id in (builtin_e.SOURCE, builtin_e.DOT):
status = self._Source(argv)
@@ -396,7 +399,7 @@ def _CheckStatus(self, status, node, argv0=None):
elif node.tag == command_e.Assignment:
span_id = self._SpanIdForAssignment(node)
raise util.ErrExitFailure(
'[%d] assignment exited with status %d', os.getpid(),
'[%d] assignment exited with status %d', os.getpid(),
status, span_id=span_id, status=status)
else:
@@ -863,7 +866,7 @@ def _Dispatch(self, node, fork_external):
self.loop_level == 0):
ok = False
msg = 'Invalid control flow at top level'
if ok:
raise _ControlFlow(tok, arg)
@@ -1129,7 +1132,7 @@ def _Execute(self, node, fork_external=True):
check_errexit = True
if redirects is None: # evaluation error
status = 1
status = 1
elif redirects:
if self.fd_state.Push(redirects, self.waiter):
@@ -1411,7 +1414,7 @@ def RunFunc(self, func_node, argv):
class Tracer(object):
"""A tracer for this process.
TODO: Connect it somehow to tracers for other processes. So you can make an
HTML report offline.
@@ -1421,7 +1424,7 @@ class Tracer(object):
- argv and span ID of the SimpleCommand that corresponds to that
- then print line number using arena
- set -x doesn't print line numbers! OH but you can do that with
PS4=$LINENO
PS4=$LINENO
"""
def __init__(self, exec_opts, mem, word_ev):
"""
View
@@ -67,6 +67,7 @@ module runtime
| TEST | BRACKET | GETOPTS
| COMMAND | TYPE | HELP
| DECLARE | TYPESET
| PWD
-- word_eval.py: SliceParts is for ${a-} and ${a+}, Error is for ${a?}, and
-- SliceAndAssign is for ${a=}.
View
@@ -28,6 +28,21 @@ echo "old: $OLDPWD"
cd -
# stdout-json: "old: /\n/\n"
### pwd
cd /
pwd
# stdout: /
### pwd -P
mkdir -p $TMP/symtarg
ln -s $TMP/symtarg $TMP/symlink
cd $TMP/symlink
basename $(pwd -P)
cd $TMP
rmdir $TMP/symtarg
rm $TMP/symlink
# stdout: symtarg
### cd with no arguments
HOME=$TMP/home
mkdir -p $HOME

0 comments on commit c5a61b5

Please sign in to comment.