Permalink
Browse files

Implement the 'dirs' builtin (#55)

Add error checking to the 'pushd' and 'popd' builtins.
  • Loading branch information...
timetoplatypus authored and andychu committed Dec 9, 2017
1 parent e081a57 commit adf1b569aafd22ef2094bad9206837340badf820
Showing with 55 additions and 3 deletions.
  1. +27 −3 core/builtin.py
  2. +28 −0 spec/builtins2.test.sh
View
@@ -482,10 +482,12 @@ def Cd(argv, mem):
state.SetGlobalString(mem, 'PWD', dest_dir)
return 0
def PrintDirStack(dir_stack):
if len(dir_stack) > 0:
sys.stdout.write(' '.join(dir_stack) + '\n');
def Pushd(argv, dir_stack):
num_args = len(argv)
if num_args <= 0:
util.error('pushd: no other directory')
return 1
@@ -499,8 +501,9 @@ def Pushd(argv, dir_stack):
except OSError as e:
util.error("pushd: %r: %s", dest_dir, os.strerror(e.errno))
return 1
dir_stack.append(os.getcwd())
PrintDirStack(dir_stack)
return 0
@@ -513,15 +516,36 @@ def Popd(argv, dir_stack):
try:
os.chdir(dest_dir)
PrintDirStack(dir_stack)
except OSError as e:
util.error("popd: %r: %s", dest_dir, os.strerror(e.errno))
return 1
return 0
DIRS_SPEC = _Register('dirs')
DIRS_SPEC.ShortFlag('-c')
DIRS_SPEC.ShortFlag('-l')
DIRS_SPEC.ShortFlag('-p')
DIRS_SPEC.ShortFlag('-v')
def Dirs(argv, dir_stack):
print(dir_stack)
arg, i = DIRS_SPEC.Parse(argv)
if arg.l:
util.warn('*** dirs -l not implemented ***')
# Following `bash` behavior for order of operations
if arg.c:
del dir_stack[:]
elif arg.v:
for i, entry in enumerate(dir_stack):
print('%2d %s' % (i, entry))
elif arg.p:
for entry in dir_stack:
print(entry)
else:
PrintDirStack(dir_stack)
return 0
View
@@ -24,3 +24,31 @@ echo status=$?
# BUG bash stdout-json: "echo\nmyfunc\nfor\nstatus=0\n"
# BUG dash stdout-json: "echo\nstatus=0\n"
# OK mksh stdout-json: "echo\nmyfunc\nstatus=1\n"
### dirs builtin
dirs
# stdout-json: ""
# status: 0
### dirs -c
pushd /
dirs
dirs -c
dirs
# stdout-json: "/\n/\n"
# status: 0
### dirs -v
pushd /
dirs -v
pushd /
dirs -v
# stdout-json: "/\n 0 /\n/ /\n 0 /\n 1 /\n"
# status: 0
### dirs -p
pushd /
dirs -p
pushd /
dirs -p
# stdout-json: "/\n/\n/ /\n/\n/\n"

0 comments on commit adf1b56

Please sign in to comment.