keithe@tekgvs.TEK.COM (Keith Ericson) (08/27/87)
We have an implementation of directory-stack scheme that runs as a BSD
systems ksh script; it takes a little modification to work with the MKS
ksh. Here it is, with extensions to print the current path as the prompt
string. It's not real fast, but how fast does a directory-stack manipulation
scheme have to be, anyway? :-) But you can _easily_ modify it to do whatever
you want it to...
Use the command
sed 's/^V^I^V^I*#.*//^V^J/^$/d'
on this file to clean it up a bit (discard explanatory comments and blank
lines). (^V = control-V; ^I = control-I (= tab); ^J = control-J)
(^$ = shift-6 shift-4) (Ain't ASCII fun?)
(This is all kind of assuming that you're set up with sh.exe as the command
interpreter. At least, that's how I have my machine set up. It looks like a
(single-user, single-tasking) UNIX environment, complete with login: and
Password: prompts. (MKS does not include password-based file protection,
however).)
Not a shar file.
======C=U=T===M=E=====H=U=R=T===M=E=====I=T===F=E=E=L=S===S=O===G=O=O=D=!=!=====
# directory stack routines.
# Bottom of stack is 0 and top of stack (TOS) is at ddepth.
# Original for BSD ksh by David C. Stewart, Tektronix Inc., 7/31/85
# Modified for MKS Toolkit ksh by Keith Ericson, Tektronix Inc., 8/27/87
# This is yours to uses as you see fit. It might be nice to give credit
# where credit is due, however...
typeset dstack[0]=$HOME # dstack is a string array of dir-names
typeset -i ddepth=0 # ddepth marks the current depth of the
# dir-name string-array
typeset -i index # an oft-used counter
function dirs { # print out the directory stack, with
index=$ddepth # TOS on the left
while [ $index -ge 0 ]
do
print -n ${dstack[index]} ''
(( index=index-1 ))
done
echo
}
function pushd { # manipulate the directory stack:
if [ $1 ] # pushd with no argument swaps the
then # top two stack elements (and "cd's"
if [ $1 != ${1#+} ] # to the new TOS name.
then # "pushd dir-name" places that name
integer index=${1#+}
tmp=${dstack[ddepth-index]}
dstack[ddepth-index]=${dstack[ddepth]}
dstack[$ddepth]=$tmp
'cd' ${dstack[ddepth]}
dirs # on the top of the stack and "cd's"
unset tmp # to it.
else #
'cd' $1 # "pushd +n" exchanges the TOS
dstack[$ddepth+1]=$PWD # with element n.
(( ddepth=ddepth+1 ))
dirs
fi
else
if [ $ddepth -eq 0 ]
then
echo directory stack is empty
else
dtemp=${dstack[ddepth-1]}
dstack[ddepth-1]=${dstack[ddepth]}
dstack[$ddepth]=$dtemp
'cd' ${dstack[ddepth]}
dirs
fi
fi
kcd .
}
function popd { # popd with no argument discards TOS
if [ $ddepth -eq 0 ] # and "cd's" to the new top-of-
then # stack dir-name.
echo directory stack is empty
else # popd +n discards the nth entry
if [ $1 ] # from the stack (shifting higher
then if [ $1 != ${1#+} ]
then # elements down to fill the gap).
integer index=$1
while [ $index -lt $ddepth ]
do dstack[$index]=${dstack[index+1]}
(( index=index+1 ))
done
else echo pushd: +n argument only
return
fi
fi
(( ddepth=ddepth-1 ))
'cd' ${dstack[ddepth]}
dirs
fi
kcd .
}
HOST=`uname -n` # HOST is defined for the prompt string.
# I include it because I use so many
# different machines that I'll lose
# track if I don't... - kde
function kcd { # kcd is the new cd function. It takes
if [ $1 ] # care of updating the prompt-string
then # to display the hostname followed by
'cd' $1 # the current path name. Two versions
dstack[$ddepth]=$PWD # are selectable:
else #
'cd' # 1) //machine/dir1/dir2/..
dstack[$ddepth]=$HOME #
fi # 2) machine:/dir1/dir2...
PS1="//${HOST}${PWD#?:}> " #
# PS1="${HOST}:${PWD#?:}> " # Take your pick... (below, also)
} #
# (The ${PWD#?:} construct eliminates
# the drive-designator from the
# prompt-string.)
#
alias cd='kcd'
alias sd='pushd' # shorthand to swap top entries of
# dirstack and chdir to new TOS
alias rd='pushd +1 ; pushd +2 ' # rot dirstack (ala Forth 'rot' word)
PS1="//${HOST}${PWD#?:}> "
# PS1="${HOST}:${PWD#?:}> "
#
# end of directory stack routines
#