-
Notifications
You must be signed in to change notification settings - Fork 0
/
.kshdir
185 lines (165 loc) · 3.31 KB
/
.kshdir
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
#! /bin/ksh
#
# .kshdir - ksh Push/Pop directory package
#
#ident "@(#)HOME:.kshdir 37.1 21/03/23 11:43:07 (woods)"
# Internal Variables:
# _dir_stack - array of strings (inverted stack)
# _dir_stack_top - stack pointer
#
# Internal Functions:
# _dir_to_tilde
#
# External Functions:
# pushd, popd, showd, dirs
unset _dir_stack_top _dir_stack
unset -f pushd popd showd dirs
typeset -i10 _dir_stack_top
_dir_stack[0]=""
_dir_stack_top=0
#
#
function _dir_to_tilde
{
if [ $# -ne 1 ]; then
echo "kshdir: internal error" >&2
echo "Usage: $0 directory" >&2
return 2
fi
case $1 in
$HOME)
echo "~"
;;
/*)
echo "$1"
;;
*)
echo "~/$1"
;;
esac
}
#
#
function popd
{
typeset -i10 pop_size pop_pointer
if [ $# -gt 1 ]; then
echo "Usage: $0 [number]" >&2
unset pop_size pop_pointer
return 2
fi
if [ $# -eq 1 ]; then
if [ "${1##[0-9]*}" = "" -o "${1##-[0-9]*}" = "" ]; then
pop_size=$1
else
echo "Usage: $0 [number]" >&2
unset pop_size pop_pointer
return 2
fi
else
pop_size=-1
fi
if [ $pop_size -lt 0 ]; then
((pop_pointer=-pop_size))
else
((pop_pointer=_dir_stack_top-pop_size))
fi
if [ $_dir_stack_top -lt "$pop_pointer" ]; then
echo "$0: The directory stack does not have $pop_size elements." >&2
else
((_dir_stack_top=pop_pointer))
fi
cd ${_dir_stack[$_dir_stack_top]}
unset pop_size pop_pointer
}
#
#
function pushd
{
if [ $# -ne 1 ]; then
echo "Usage: $0 directory" >&2
return 2
fi
_dir_stack[_dir_stack_top]="$(/bin/pwd)"
((_dir_stack_top=_dir_stack_top+1))
cd $1
}
#
#
function showd
{
typeset -i10 neg_count count pos_count
neg_count=0
count=1
pos_count=$_dir_stack_top+1
echo "$pos_count $neg_count $(_dir_to_tilde $(/bin/pwd))"
while [ $count -lt $_dir_stack_top+1 ]; do
((pos_count=pos_count-1))
((neg_count=neg_count-1))
echo "$pos_count $neg_count ${_dir_stack[_dir_stack_top-count]}"
((count=count+1))
done
unset neg_count count pos_count
}
# to be compatible with csh (esp. for emacs-19 shell mode)
#
function dirs
{
typeset -i10 count
count=1
echo "$(_dir_to_tilde $(/bin/pwd))\c"
while [ $count -lt $_dir_stack_top+1 ]; do
echo " $(_dir_to_tilde ${_dir_stack[_dir_stack_top-count]})\c"
((count=count+1))
done
echo ""
unset count
}
# unused (i.e. it's broke)
#
## function sd
## {
## if [ $# -ne 1 ]; then
## showd
## return 0
## fi
## if [ "${1##[0-9]*}" = "" -o "${1##-[0-9]*}" = "" ]; then
## switch_index=$1
## else
## echo "$0: argument must be numeric." >&2
## showd
## return 1
## fi
## if [ $switch_index -eq 0 ]; then
## cd
## unset switch_index
## return 0
## fi
## if [ $switch_index -lt 1 ]; then
## ((switch_index=-switch_index))
## else
## ((switch_index=_dir_stack_top - switch_index + 1))
## fi
## if [ $_dir_stack_top -lt $switch_index -o $switch_index -lt 1 ]; then
## echo "$0: The directory stack does not have $switch_index elements." >&2
## fi
## cd ${_dir_stack[_dir_stack_top-switch_index]}
## unset switch_index
## }
# unused (i.e. it's broke)
#
## function sd_init
## {
## _dir_stack[0]=""
## _dir_stack_top=0
## sd_init_list="$HOME/*"
##
## for dir_path in $std_init_list ; do
## _dir_stack[_dir_stack_top]="$dir_path"
## ((_dir_stack_top=_dir_stack_top+1))
## done
## unset sd_init_list dir_path
## }
##
## typeset -fx popd pushd showd sd sd_init
typeset -fx popd pushd showd