diff options
Diffstat (limited to 'src/cmd/ksh93/fun')
-rwxr-xr-x | src/cmd/ksh93/fun/dirs | 108 | ||||
-rwxr-xr-x | src/cmd/ksh93/fun/popd | 111 | ||||
-rwxr-xr-x | src/cmd/ksh93/fun/pushd | 111 |
3 files changed, 330 insertions, 0 deletions
diff --git a/src/cmd/ksh93/fun/dirs b/src/cmd/ksh93/fun/dirs new file mode 100755 index 0000000..0329970 --- /dev/null +++ b/src/cmd/ksh93/fun/dirs @@ -0,0 +1,108 @@ +# +# DIRECTORY MANIPULATION FUNCTIONS, REPLACES CD +# +# Uses global parameters _push_max _push_top _push_stack +integer _push_max=${CDSTACK-32} _push_top=${CDSTACK-32} +unalias cd +alias cd=_cd +# Display directory stack -- $HOME displayed as ~ +function dirs +{ + typeset dir="${PWD#$HOME/}" + case $dir in + $HOME) + dir=\~ + ;; + /*) ;; + *) dir=\~/$dir + esac + PS3= + select i in "$dir" "${_push_stack[@]}" + do : + done < /dev/null +} + +# Change directory and put directory on front of stack +function _cd +{ + typeset dir= + integer n=0 type=4 + case $1 in + -|-1|2) # \cd - + n=_push_top type=1 + ;; + -[1-9]*([0-9])) # \cd -n + n=_push_top+${1#-}-1 type=2 + ;; + 1) # keep present directory + print -r - "$PWD" + return + ;; + [1-9]*([0-9])) # \cd n + n=_push_top+${1}-2 type=2 + ;; + *) if ((_push_top <= 0)) + then type=3 n=_push_max + fi + esac + if ((type<3)) + then if ((n >= _push_max+1)) + then print -u2 cd: Directory stack not that deep. + return 1 + else dir=${_push_stack[n]} + fi + fi + case $dir in + \~*) dir=$HOME${dir#\~} + esac + \cd "${dir:-$@}" >| /dev/null || return 1 + dir=${OLDPWD#$HOME/} + case $TERM in + 630) + print "\033[?${#PWD};2v$PWD\c" + ;; + esac + case $dir in + $HOME) + dir=\~ + ;; + /*) ;; + *) dir=\~/$dir + esac + case $type in + 1) # swap first two elements + _push_stack[_push_top]=$dir + ;; + 2|3) # put $dir on top and shift down by one until top + integer i=_push_top + for dir in "$dir" "${_push_stack[@]}" + do ((i > n)) && break + _push_stack[i]=$dir + i=i+1 + done + ;; + 4) # push name + _push_stack[_push_top=_push_top-1]=$dir + ;; + esac + print -r - "$PWD" +} + +# Menu driven change directory command +function mcd +{ + typeset dir="${PWD#$HOME/}" + case $dir in + $HOME) + dir=\~ + ;; + /*) ;; + *) dir=\~/$dir + esac + PS3='Select by number or enter a name: ' + select dir in "$dir" "${_push_stack[@]}" + do if _cd $REPLY + then return + fi + done +} diff --git a/src/cmd/ksh93/fun/popd b/src/cmd/ksh93/fun/popd new file mode 100755 index 0000000..1bc9346 --- /dev/null +++ b/src/cmd/ksh93/fun/popd @@ -0,0 +1,111 @@ +# +# DIRECTORY MANIPULATION FUNCTIONS PUSHD, POPD AND DIRS +# +# Uses global parameters _push_max _push_top _push_stack +integer _push_max=100 _push_top=100 +# Display directory stack -- $HOME displayed as ~ +function dirs +{ + typeset dir="${PWD#$HOME/}" + case $dir in + $HOME) + dir=\~ + ;; + /*) ;; + *) dir=\~/$dir + esac + print -r - "$dir ${_push_stack[@]}" +} + +# Change directory and put directory on front of stack +function pushd +{ + typeset dir= type=0 + integer i + case $1 in + "") # pushd + if ((_push_top >= _push_max)) + then print pushd: No other directory. + return 1 + fi + type=1 dir=${_push_stack[_push_top]} + ;; + +[1-9]|+[1-9][0-9]) # pushd +n + integer i=_push_top$1-1 + if ((i >= _push_max)) + then print pushd: Directory stack not that deep. + return 1 + fi + type=2 dir=${_push_stack[i]} + ;; + *) if ((_push_top <= 0)) + then print pushd: Directory stack overflow. + return 1 + fi + esac + case $dir in + \~*) dir=$HOME${dir#\~} + esac + cd "${dir:-$1}" > /dev/null || return 1 + dir=${OLDPWD#$HOME/} + case $dir in + $HOME) + dir=\~ + ;; + /*) ;; + *) dir=\~/$dir + esac + case $type in + 0) # pushd name + _push_stack[_push_top=_push_top-1]=$dir + ;; + 1) # pushd + _push_stack[_push_top]=$dir + ;; + 2) # push +n + type=${1#+} i=_push_top-1 + set -- "${_push_stack[@]}" "$dir" "${_push_stack[@]}" + shift $type + for dir + do (((i=i+1) < _push_max)) || break + _push_stack[i]=$dir + done + esac + dirs +} + +# Pops the top directory +function popd +{ + typeset dir + if ((_push_top >= _push_max)) + then print popd: Nothing to pop. + return 1 + fi + case $1 in + "") + dir=${_push_stack[_push_top]} + case $dir in + \~*) dir=$HOME${dir#\~} + esac + cd "$dir" || return 1 + ;; + +[1-9]|+[1-9][0-9]) + typeset savedir + integer i=_push_top$1-1 + if ((i >= _push_max)) + then print pushd: Directory stack not that deep. + return 1 + fi + while ((i > _push_top)) + do _push_stack[i]=${_push_stack[i-1]} + i=i-1 + done + ;; + *) print pushd: Bad directory. + return 1 + esac + unset '_push_stack[_push_top]' + _push_top=_push_top+1 + dirs +} diff --git a/src/cmd/ksh93/fun/pushd b/src/cmd/ksh93/fun/pushd new file mode 100755 index 0000000..1bc9346 --- /dev/null +++ b/src/cmd/ksh93/fun/pushd @@ -0,0 +1,111 @@ +# +# DIRECTORY MANIPULATION FUNCTIONS PUSHD, POPD AND DIRS +# +# Uses global parameters _push_max _push_top _push_stack +integer _push_max=100 _push_top=100 +# Display directory stack -- $HOME displayed as ~ +function dirs +{ + typeset dir="${PWD#$HOME/}" + case $dir in + $HOME) + dir=\~ + ;; + /*) ;; + *) dir=\~/$dir + esac + print -r - "$dir ${_push_stack[@]}" +} + +# Change directory and put directory on front of stack +function pushd +{ + typeset dir= type=0 + integer i + case $1 in + "") # pushd + if ((_push_top >= _push_max)) + then print pushd: No other directory. + return 1 + fi + type=1 dir=${_push_stack[_push_top]} + ;; + +[1-9]|+[1-9][0-9]) # pushd +n + integer i=_push_top$1-1 + if ((i >= _push_max)) + then print pushd: Directory stack not that deep. + return 1 + fi + type=2 dir=${_push_stack[i]} + ;; + *) if ((_push_top <= 0)) + then print pushd: Directory stack overflow. + return 1 + fi + esac + case $dir in + \~*) dir=$HOME${dir#\~} + esac + cd "${dir:-$1}" > /dev/null || return 1 + dir=${OLDPWD#$HOME/} + case $dir in + $HOME) + dir=\~ + ;; + /*) ;; + *) dir=\~/$dir + esac + case $type in + 0) # pushd name + _push_stack[_push_top=_push_top-1]=$dir + ;; + 1) # pushd + _push_stack[_push_top]=$dir + ;; + 2) # push +n + type=${1#+} i=_push_top-1 + set -- "${_push_stack[@]}" "$dir" "${_push_stack[@]}" + shift $type + for dir + do (((i=i+1) < _push_max)) || break + _push_stack[i]=$dir + done + esac + dirs +} + +# Pops the top directory +function popd +{ + typeset dir + if ((_push_top >= _push_max)) + then print popd: Nothing to pop. + return 1 + fi + case $1 in + "") + dir=${_push_stack[_push_top]} + case $dir in + \~*) dir=$HOME${dir#\~} + esac + cd "$dir" || return 1 + ;; + +[1-9]|+[1-9][0-9]) + typeset savedir + integer i=_push_top$1-1 + if ((i >= _push_max)) + then print pushd: Directory stack not that deep. + return 1 + fi + while ((i > _push_top)) + do _push_stack[i]=${_push_stack[i-1]} + i=i-1 + done + ;; + *) print pushd: Bad directory. + return 1 + esac + unset '_push_stack[_push_top]' + _push_top=_push_top+1 + dirs +} |