xref: /onnv-gate/usr/src/lib/libshell/common/fun/dirs (revision 4887:feebf9260c2e)
1*4887Schin#
2*4887Schin# DIRECTORY MANIPULATION FUNCTIONS, REPLACES CD
3*4887Schin#
4*4887Schin# Uses global parameters _push_max _push_top _push_stack
5*4887Schininteger _push_max=${CDSTACK-32} _push_top=${CDSTACK-32}
6*4887Schinunalias cd
7*4887Schinalias cd=_cd
8*4887Schin# Display directory stack -- $HOME displayed as ~
9*4887Schinfunction dirs
10*4887Schin{
11*4887Schin    typeset dir="${PWD#$HOME/}"
12*4887Schin    case $dir in
13*4887Schin    $HOME)
14*4887Schin        dir=\~
15*4887Schin        ;;
16*4887Schin    /*) ;;
17*4887Schin    *)  dir=\~/$dir
18*4887Schin    esac
19*4887Schin    PS3=
20*4887Schin    select i in "$dir" "${_push_stack[@]}"
21*4887Schin    do	:
22*4887Schin    done < /dev/null
23*4887Schin}
24*4887Schin
25*4887Schin# Change directory and put directory on front of stack
26*4887Schinfunction _cd
27*4887Schin{
28*4887Schin    typeset dir=
29*4887Schin    integer n=0 type=4
30*4887Schin    case $1 in
31*4887Schin    -|-1|2) # \cd -
32*4887Schin        n=_push_top type=1
33*4887Schin        ;;
34*4887Schin    -[1-9]*([0-9])) # \cd -n
35*4887Schin        n=_push_top+${1#-}-1 type=2
36*4887Schin        ;;
37*4887Schin    1)  # keep present directory
38*4887Schin        print -r - "$PWD"
39*4887Schin	return
40*4887Schin        ;;
41*4887Schin    [1-9]*([0-9])) # \cd n
42*4887Schin        n=_push_top+${1}-2 type=2
43*4887Schin        ;;
44*4887Schin    *)  if    ((_push_top <= 0))
45*4887Schin        then  type=3 n=_push_max
46*4887Schin        fi
47*4887Schin    esac
48*4887Schin    if    ((type<3))
49*4887Schin    then  if    ((n >= _push_max+1))
50*4887Schin          then  print -u2 cd: Directory stack not that deep.
51*4887Schin                return 1
52*4887Schin          else  dir=${_push_stack[n]}
53*4887Schin          fi
54*4887Schin    fi
55*4887Schin    case $dir in
56*4887Schin    \~*)   dir=$HOME${dir#\~}
57*4887Schin    esac
58*4887Schin    \cd "${dir:-$@}" >| /dev/null || return 1
59*4887Schin    dir=${OLDPWD#$HOME/}
60*4887Schin    case $TERM in
61*4887Schin    630)
62*4887Schin	    print "\033[?${#PWD};2v$PWD\c"
63*4887Schin	    ;;
64*4887Schin    esac
65*4887Schin    case $dir in
66*4887Schin    $HOME)
67*4887Schin        dir=\~
68*4887Schin        ;;
69*4887Schin    /*) ;;
70*4887Schin    *)  dir=\~/$dir
71*4887Schin    esac
72*4887Schin    case $type in
73*4887Schin    1)  # swap first two elements
74*4887Schin        _push_stack[_push_top]=$dir
75*4887Schin        ;;
76*4887Schin    2|3)  # put $dir on top and shift down by one until top
77*4887Schin        integer i=_push_top
78*4887Schin        for dir in "$dir" "${_push_stack[@]}"
79*4887Schin        do  ((i > n)) && break
80*4887Schin            _push_stack[i]=$dir
81*4887Schin            i=i+1
82*4887Schin        done
83*4887Schin        ;;
84*4887Schin    4)  # push name
85*4887Schin        _push_stack[_push_top=_push_top-1]=$dir
86*4887Schin        ;;
87*4887Schin    esac
88*4887Schin    print -r - "$PWD"
89*4887Schin}
90*4887Schin
91*4887Schin# Menu driven change directory command
92*4887Schinfunction mcd
93*4887Schin{
94*4887Schin    typeset dir="${PWD#$HOME/}"
95*4887Schin    case $dir in
96*4887Schin    $HOME)
97*4887Schin        dir=\~
98*4887Schin        ;;
99*4887Schin    /*) ;;
100*4887Schin    *)  dir=\~/$dir
101*4887Schin    esac
102*4887Schin    PS3='Select by number or enter a name: '
103*4887Schin    select dir in "$dir" "${_push_stack[@]}"
104*4887Schin    do  if    _cd $REPLY
105*4887Schin        then  return
106*4887Schin        fi
107*4887Schin    done
108*4887Schin}
109