A Directory Heap for "ksh"
AndrieDA
daa at druxh.UUCP
Sat Oct 13 02:23:47 AEST 1984
# Ok, how about a "heap" implementation for saving directories.
# The functions "pu" and "po" ("push" and "pop") implement a
# simple stack mechanism on top of the heap.
#
################################################################
#
# Directory heap commands:
# pd - print directory
# sd [ <dir> ] - save directory [default=cur_dir]
# dd [ <num> ] - delete directory
# gd [ <num> ] - go to a saved directory
# pu <dir> - save current directory and goto <dir>
# po - go to last saved directory and remove
# it from the heap.
#
################################################################
#
PS3='Select One ==> '
export PS3
dh_index=0
export dh_index
#----------------------------------
# print_dir_heap - print the heap
#----------------------------------
pd() { pr_index=1
print - "PWD = $PWD"
while [ $pr_index -le $dh_index ]
do
print - "$pr_index) ${dir_heap_p[$pr_index]}"
((pr_index=pr_index+1))
done
}
#----------------------------------
# save_dir - save a directory in the heap
#----------------------------------
sd() { if [ "$1" ] # true if argument passed to sd
then
arg=$1
else
arg=$PWD
fi
if test -d $arg # true if the directory exists
then
cd $arg
((dh_index=dh_index+1))
dir_heap_u[dh_index]=$PWD
basen=`substring -L '*/' $PWD`
basen=$basen"................"
trunc=`substring -l '????????????????' $basen`
basen=`substring $basen $trunc`
REL_PATH=${PWD#$HOME}
if [ "$REL_PATH" != "$PWD" ]
then
dir_heap_p[dh_index]="$basen~$REL_PATH"
else
dir_heap_p[dh_index]="$basen$PWD"
fi
cd $OLDPWD
else
print - "sd: "$1 "not a directory - not saved"
fi
pd
}
#----------------------------------
# go_dir - go to a directory in the heap
#----------------------------------
gd() { if [ "$1" ] # true if argument passed to gd
then
if [ $1 -le $dh_index ] # in bounds?
then
cd ${dir_heap_u[$1]}
print - ${dir_heap_u[$1]}
return
else
print - "gd: out of range"
return
fi
else
print - " "
select DIR in ${dir_heap_p[*]} "Forget it"
do
if [ ! "$DIR" ]
then
print - "gd: out of range"
return
elif [ "$DIR" = "Forget it" ]
then
return
else
cd ${dir_heap_u[$REPLY]}
print - $PWD
return
fi
done
fi
}
#----------------------------------
# del_dir - delete a directory from heap
#----------------------------------
dd() { if [ "$1" ] # true if argument passed to gd
then
if [ "$1" = "*" ]
then
while [ $dh_index -ne 0 ]
do
dir_heap_u[$dh_index]=
dir_heap_p[$dh_index]=
((dh_index=dh_index-1))
done
print - "all entries deleted"
return
elif [ "$1" -le $dh_index ] # in bounds?
then
index=$1
print - "entry" ${dir_heap_p[index]} "deleted"
while [ $index -lt $dh_index ]
do
dir_heap_u[index]=${dir_heap_u[index+1]}
dir_heap_p[index]=${dir_heap_p[index+1]}
((index=index+1))
done
dir_heap_u[dh_index]=
dir_heap_p[dh_index]=
((dh_index=dh_index-1))
return
else
print - "dd: out of range"
return
fi
else
print - " "
select DIR in ${dir_heap_p[*]} "Forget it"
do
if [ ! "$DIR" ]
then
print - "dd: out of range"
return
elif [ "$DIR" = "Forget it" ]
then
return
else
index=$REPLY
while [ $index -lt $dh_index ]
do
dir_heap_u[index]=${dir_heap_u[index+1]}
dir_heap_p[index]=${dir_heap_p[index+1]}
((index=index+1))
done
dir_heap_u[dh_index]=
dir_heap_p[dh_index]=
((dh_index=dh_index-1))
pd
return
fi
done
fi
}
#----------------------------------
# pu - push current directory and go elsewhere
# po - pop from current directory back to previous
#----------------------------------
pu() {
if [ $# -gt 1 ]
then
print - 'pu: ERROR: arg count'
return
fi
if [ $# -lt 1 ]
then
dh_i=$dh_index
sd > /dev/null
gd $dh_i > /dev/null
dd $dh_i > /dev/null
pd
else
cd $1
if [ $? -eq 0 ]
then
sd $OLDPWD
fi
fi
}
po() {
if [ $# -gt 0 ]
then
print - 'po: ERROR: arg count'
return
fi
gd $dh_index > /dev/null
dd $dh_index > /dev/null
pd
}
#----------------------------------
# end of directory operations
#----------------------------------
Donald A. Andrie
AT&T Information Systems - Denver
ihnp4!druxh!daa
More information about the Comp.sources.unix
mailing list