diff --git a/core/builtin.py b/core/builtin.py index 01e8cacaaa..c4faca4a22 100755 --- a/core/builtin.py +++ b/core/builtin.py @@ -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,6 +516,7 @@ 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 @@ -520,8 +524,28 @@ def Popd(argv, dir_stack): 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 diff --git a/spec/builtins2.test.sh b/spec/builtins2.test.sh index a894bfe37a..8f2c4b535d 100644 --- a/spec/builtins2.test.sh +++ b/spec/builtins2.test.sh @@ -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"