diff options
author | Jason Downs <downsj@cvs.openbsd.org> | 1996-08-14 08:05:27 +0000 |
---|---|---|
committer | Jason Downs <downsj@cvs.openbsd.org> | 1996-08-14 08:05:27 +0000 |
commit | c66bb679d79d844029859810085e570f4f4d67ee (patch) | |
tree | f94e76262c4e3ac77eb927b67f21b198a4ec95e4 /bin/ksh/tests/cdhist.t | |
parent | 52a7e18144267fb93e80ff7051945e20832f5a67 (diff) |
Add these to the repository, but don't do anything with them; they
need perl (at least for now).
Diffstat (limited to 'bin/ksh/tests/cdhist.t')
-rw-r--r-- | bin/ksh/tests/cdhist.t | 160 |
1 files changed, 160 insertions, 0 deletions
diff --git a/bin/ksh/tests/cdhist.t b/bin/ksh/tests/cdhist.t new file mode 100644 index 00000000000..720f7d60bcc --- /dev/null +++ b/bin/ksh/tests/cdhist.t @@ -0,0 +1,160 @@ +name: cd-history +description: + Test someone's CD history package (uses arrays) +stdin: + # go to known place before doing anything + cd / + + alias cd=_cd + function _cd + { + typeset -i cdlen i + typeset t + + if [ $# -eq 0 ] + then + set -- $HOME + fi + + if [ "$CDHISTFILE" -a -r "$CDHISTFILE" ] # if directory history exists + then + typeset CDHIST + i=-1 + while read -r t # read directory history file + do + CDHIST[i=i+1]=$t + done <$CDHISTFILE + fi + + if [ "${CDHIST[0]}" != "$PWD" -a "$PWD" != "" ] + then + _cdins # insert $PWD into cd history + fi + + cdlen=${#CDHIST[*]} # number of elements in history + + case "$@" in + -) # cd to new dir + if [ "$OLDPWD" = "" ] && ((cdlen>1)) + then + 'print' ${CDHIST[1]} + 'cd' ${CDHIST[1]} + _pwd + else + 'cd' $@ + _pwd + fi + ;; + -l) # print directory list + typeset -R3 num + ((i=cdlen)) + while (((i=i-1)>=0)) + do + num=$i + 'print' "$num ${CDHIST[i]}" + done + return + ;; + -[0-9]|-[0-9][0-9]) # cd to dir in list + if (((i=${1#-})<cdlen)) + then + 'print' ${CDHIST[i]} + 'cd' ${CDHIST[i]} + _pwd + else + 'cd' $@ + _pwd + fi + ;; + -*) # cd to matched dir in list + t=${1#-} + i=1 + while ((i<cdlen)) + do + case ${CDHIST[i]} in + *$t*) + 'print' ${CDHIST[i]} + 'cd' ${CDHIST[i]} + _pwd + break + ;; + esac + ((i=i+1)) + done + if ((i>=cdlen)) + then + 'cd' $@ + _pwd + fi + ;; + *) # cd to new dir + 'cd' $@ + _pwd + ;; + esac + + _cdins # insert $PWD into cd history + + if [ "$CDHISTFILE" ] + then + cdlen=${#CDHIST[*]} # number of elements in history + + i=0 + while ((i<cdlen)) + do + 'print' -r ${CDHIST[i]} # update directory history + ((i=i+1)) + done >$CDHISTFILE + fi + } + + function _cdins # insert $PWD into cd history + { # meant to be called only by _cd + typeset -i i + + ((i=0)) + while ((i<${#CDHIST[*]})) # see if dir is already in list + do + if [ "${CDHIST[$i]}" = "$PWD" ] + then + break + fi + ((i=i+1)) + done + + if ((i>22)) # limit max size of list + then + i=22 + fi + + while (((i=i-1)>=0)) # bump old dirs in list + do + CDHIST[i+1]=${CDHIST[i]} + done + + CDHIST[0]=$PWD # insert new directory in list + } + + + function _pwd + { + if [ -n "$ECD" ] + then + pwd 1>&6 + fi + } + # Start of test + cd /tmp + cd /bin + cd /etc + cd - + cd -2 + cd -l +expected-stdout: + /bin + /tmp + 3 / + 2 /etc + 1 /bin + 0 /tmp +--- |