[alt.sources] cd, dirs, popd, pushd, pwd for VMS

rogers@sud509.ed.ray.com (Andrew Rogers) (09/28/90)

Today is (hopefully) the last day I will ever have to use a VMS system.  As
a parting gift to those less fortunate, here are my VMS implementations for
the csh commands cd (change directory), dirs (print directory stack), popd
(pop directory stack), pushd (push directory onto stack and 'cd' there) and
pwd (print working directory).  Some call others by symbolic name, so it
is best to define all five as global symbols.  Also, the magic to highlight
the current directory is highly VT-XXX specific and may not work with other
terminals.

Andrew

-------------------------------- cut here --------------------------------
#! /bin/sh
# This is a shell archive, meaning:
# 1. Remove everything above the #! /bin/sh line.
# 2. Save the resulting text in a file.
# 3. Execute the file with /bin/sh (not csh) to create:
#	 cd.com
#	 dirs.com
#	 popd.com
#	 pushd.com
#	 pwd.com
# This archive created: Fri Sep 28 11:00:30 EDT 1990
export PATH; PATH=/bin:/usr/bin:$PATH
if test -f 'cd.com'
then
	echo shar: will not over-write existing file 'cd.com'
else
cat << \SHAR_EOF > 'cd.com'
$! cd.com - simulate Un*x 'cd' command
$
$ if p1 .eqs. "" then p1 = f$logical("sys$login")
$
$! translate disk:[dir1.dir2.dirN] to disk:[dir1.dir2.-]dirN.dir
$
$ dir = f$parse(p1,,,"DIRECTORY") - "[" - "]"
$l1:
$  n = f$locate(".", dir)
$  if n .ge. f$length(dir) then goto l2
$  dir = f$extract(n + 1, 999, dir)
$  goto l1
$l2:
$ dir = p1 - "]" + ".-]" + dir + ".dir"
$
$ if f$search(dir) .nes. "" then goto OK		! look for directory
$ if f$trnlog(p1 - ":") .nes. "" then goto OK		! try logical name
$  write sys$output "Directory ", p1, " does not exist"
$  exit
$
$OK:
$ set def 'p1
$ if f$type(pwd) .eqs. "" then pwd :== show default
$ pwd
SHAR_EOF
fi
if test -f 'dirs.com'
then
	echo shar: will not over-write existing file 'dirs.com'
else
cat << \SHAR_EOF > 'dirs.com'
$! dirs.com - print directory stack (current directory bold)
$
$ if f$type(dir_stack) .eqs. "" then dir_stack :== ""
$ esc[0,8] = %D27
$ write sys$output esc,"[1m",f$environment("default"),esc,"[22m ",dir_stack
SHAR_EOF
fi
if test -f 'popd.com'
then
	echo shar: will not over-write existing file 'popd.com'
else
cat << \SHAR_EOF > 'popd.com'
$! popd.com - pop one or more directories from stack
$
$ if f$type(dir_stack) .eqs. "" then dir_stack :== ""
$ if f$type(p1) .nes. "INTEGER" then p1 = 1	! default # to pop: 1
$ if p1 .lt. 0 then p1 = - p1
$ if dir_stack .nes. "" then goto loop
$   write sys$output "Directory stack empty"
$   exit
$loop:
$   newdir = f$element(0," ",dir_stack)
$   dir_stack == dir_stack - newdir - " "
$   p1 = p1 - 1
$   if p1 .gt. 0 .and. dir_stack .nes. "" then goto loop
$ set def 'newdir
$ if f$type(dirs) .eqs. "" then dirs :== show default
$ dirs
SHAR_EOF
fi
if test -f 'pushd.com'
then
	echo shar: will not over-write existing file 'pushd.com'
else
cat << \SHAR_EOF > 'pushd.com'
$! pushd.com - push directory onto stack
$
$ if f$type(dir_stack) .eqs. "" then dir_stack :== ""
$ if p1 .eqs. "" then p1 = f$logical("sys$login")
$
$! check that new directory exists (derived from cd.com)
$
$ dir = f$parse(p1,,,"DIRECTORY") - "[" - "]"
$l1:
$  n = f$locate(".", dir)
$  if n .ge. f$length(dir) then goto l2
$  dir = f$extract(n + 1, 999, dir)
$  goto l1
$l2:
$ dir = p1 - "]" + ".-]" + dir + ".dir"
$
$ if f$search(dir) .nes. "" then goto OK		! look for directory
$ if f$trnlog(p1 - ":") .nes. "" then goto OK		! try logical name
$  write sys$output "Directory ", p1, " does not exist"
$  exit
$OK:
$
$ here = f$environment("default")		! push directory on stack
$ dir_stack == here + " " + dir_stack
$ set def 'p1					! change to directory
$ if f$type(dirs) .eqs. "" then dirs :== show default
$ dirs
SHAR_EOF
fi
if test -f 'pwd.com'
then
	echo shar: will not over-write existing file 'pwd.com'
else
cat << \SHAR_EOF > 'pwd.com'
$! pwd.com - print current directory (bold)
$
$ esc[0,8] = %D27
$ write sys$output esc,"[1m",f$environment("default"),esc,"[22m"
SHAR_EOF
fi
exit 0
#	End of shell archive