[comp.sources.misc] v06i052: dirstack for MKS Toolkit Shell

allbery@uunet.UU.NET (Brandon S. Allbery - comp.sources.misc) (03/07/89)

Posting-number: Volume 6, Issue 52
Submitted-by: keithe@tekgvs.labs.tek.com (Keith Ericson)
Archive-name: dirstack.mks

[Please shar your postings, people.  ++bsa]

Herewith are

	1. dirstack.ksh

	2. color.ksh, and

	3. color.hlp

that I use with my MKS system.  Dirstack does what it sez.  Color.??? are
used to set up the prompt string and give me the colors I want even if some
other program comes along and changes 'em on me.  You can eliminate the
"dynamic prompt string" and eliminate the need for color.???, or retain the
dynamic prompt string, but eliminate the ${PSC} from the definition of PS1
(near the end of the dirstack.ksh file) if you want.

	Have Fun...
kEITH


Keith Ericson  at TekLabs (resident factious factotum)
Tektronix, PO 500, MS 58-383   Beaverton OR 97077    (503)627-6042
UUCP:	uunet!tektronix!tekgvs!keithe		   |Note:
		or				   |  These addresses
	uunet!tektronix!tekgvs.labs.tek.com!keithe |  may be hazardous
ARPA:	keithe%tekgvs.LABS.TEK.COM@RELAY.CS.NET	   |  to your mailer's
CSNet:	keithe@tekgvs.LABS.TEK.COM		   |  health!!
"Of course, you only live one life, and you make all your mistakes,
   and learn what not to do, and that's the end of you." - R.P. Feynman
===========================================================================
#! /bin/sh
# This file was wrapped with "dummyshar".  "sh" this file to extract.
# Contents:  DIRSTACK.KSH COLOR.KSH COLOR.HLP
echo extracting 'DIRSTACK.KSH'
if test -f 'DIRSTACK.KSH' -a -z "$1"; then echo Not overwriting 'DIRSTACK.KSH'; else
sed 's/^X//' << \EOF > 'DIRSTACK.KSH'
Xprint "Setting up the directory stack mechanism..."
X# directory stack routines.
X
X# Bottom of stack is 0 and top of stack (TOS) is at ddepth.
X
X# Original for BSD ksh by David C. Stewart, Tektronix Inc., 7/31/85
X# Modified for MKS Toolkit ksh by Keith Ericson, Tektronix Inc., 8/27/87
X#
X#	Note: for MKS, don't use the shorthand notation "[" for "test"
X#	especially if you're using PC-NFS and ever do a 'cd' while the
X#	current directory is a remotely-mounted filesystem.  For some
X#	reason the "[" version of "test" takes abominably longer to
X#	execute than does good ol' "test."  I've informed Gerry Wheeler
X#	at Mortice Kern Systems about this but never heard if it had been
X#	fixed.  - kde 1989Feb13
X
X#
X# This is released to the public domain and is yours to uses as you see
X#	fit. It might be nice to give credit where credit is due, however...
X
X#
XHOST=`uname -n`
X# Strip off all trailing blanks from the HOST string
XHOST=${HOST%% }
X
Xtypeset dstack[0]=$HOME
Xtypeset -i ddepth=0
Xtypeset -i index
X#
X# dirs prints out the contents of the directory stack
X#
Xfunction dirs {
X	index=$ddepth
X	while test $index -ge 0 
X	do	print -n ${dstack[index]} ''
X		(( index=index-1 ))
X	done
X	echo
X}
X#
X#pushd pushes entries onto the stack, or swaps TOS with the indexed-value
X#	e.g., pushd +2 swaps TOS with the 3rd entry.
X#	With no arguments pushd exchanges the top two dirstack elements and
X#	changes to the new directory.
X#
Xfunction pushd {
X	case $1 in
X	+[0-9]|+[0-9][0-9])
X		index=${1#+}
X		tmp=${dstack[ddepth-index]}
X		dstack[ddepth-index]=${dstack[ddepth]}
X		dstack[$ddepth]=$tmp
X		'cd' ${dstack[ddepth]}
X		dirs
X		unset tmp
X		;;
X	+*)	echo 'usage: pushd [dir-name | +n]'
X		;;
X	'')	if test $ddepth -eq 0 
X		then	echo directory stack is empty
X		else	dtemp=${dstack[ddepth-1]}
X			dstack[ddepth-1]=${dstack[ddepth]}
X			dstack[$ddepth]=$dtemp
X			'cd' ${dstack[ddepth]}
X			dirs
X		fi
X		;;
X	*)	'cd' $1
X		if	test $? -eq 0 
X		then	 dstack[$ddepth+1]=$PWD
X			(( ddepth=ddepth+1 ))
X			dirs
X		fi
X		;;
X	esac
Xkcd .
X}
X#
X# Popd effectively pops the TOS entry off the stack and changes to the
X#	directory named by the 'new' TOS.  If a (numeric) argument is
X#	given then that element is removed and deeper elements bubbled
X#	up in the stack.
X#
Xfunction popd {
X	if test $ddepth -eq 0 
X	then	echo directory stack is empty
X	else	case $1 in
X			'')	;;
X			+[0-9]|+[0-9][0-9])
X				(( index=$ddepth-${1#+} ))
X				if	test $index -lt 0 
X				then 	echo popd: bad argument value
X					return
X				else	while	test $index -lt $ddepth 
X					do	dstack[$index]=${dstack[index+1]}
X						(( index=index+1 ))
X					done
X				fi
X				;;
X			*)	echo usage: popd [+n]
X				return
X				;;
X		esac
X		(( ddepth=ddepth-1 ))
X		'cd' ${dstack[ddepth]}
X		dirs
X	fi
Xkcd .
X}
X
X#
X# The replacement directory changer function...
X#
X
Xprint "Setting up dynamic prompt string"
X
Xfunction kcd {
X	if	test $1 
X	then
X		'cd' $1
X		dstack[$ddepth]=$PWD
X	else
X		'cd'
X		dstack[$ddepth]=$HOME
X	fi
X#
X# Now we set up the Prompt String to tell us (1) what machine we're
X#	talking to (if you use rlogin to multiple machine this can help
X#	retain your sanity) (maybe) and (2) which directory you're in.
X#
X#	the "if..then..else" replaces your home-directory-string with a
X#	"~" (tilde) to reduce the length of the prompt when you're in
X#	familiar territory.  Replace "c:/usr/keithe" with the appropriate
X#	string in the two occurrances below.
X#
X	if	test "${PWD}" = "${PWD#c:/usr/keithe}" 
X	then	TILDE=${PWD}
X	else	TILDE="~"${PWD#c:/usr/keithe}
X	fi
X# ${PSC} is the "Prompt String Color" string set up by
X#	/usr/bin/color.ksh
XPS1="${PSC}[${HOST}]${TILDE}> "
X}
Xalias cd='kcd'
Xalias pd='pushd .'
Xalias sd='pushd'	# 'Cuz I can never remember that 'pushd' swaps the top
X			#  two stack elements: 'sd' means "swap directories"
Xkcd .
X#
EOF
chars=`wc -c < 'DIRSTACK.KSH'`
if test $chars !=     3877; then echo 'DIRSTACK.KSH' is $chars characters, should be     3877 characters!; fi
fi
echo extracting 'COLOR.KSH'
if test -f 'COLOR.KSH' -a -z "$1"; then echo Not overwriting 'COLOR.KSH'; else
sed 's/^X//' << \EOF > 'COLOR.KSH'
XCOLOR=
Xfor i in $@
Xdo
X	case $1 in
X		HELP|help)	cat c:/usr/bin/COLOR.HLP
X				exit ;;
X		NORMAL|normal)	COLOR=${COLOR}'[0m'  ;;
X		BRIGHT|bright)	COLOR=${COLOR}'[1m'  ;;
X		DIM|dim)	COLOR=${COLOR}'[2m'  ;;
X		REVERSE|reverse)	COLOR=${COLOR}'[7m' ;;
X		BLACK|black)	COLOR=${COLOR}'[30m' ;;
X		RED|red)	COLOR=${COLOR}'[31m' ;;
X		GREEN|green)	COLOR=${COLOR}'[32m' ;;
X		YELLOW|yellow)	COLOR=${COLOR}'[33m' ;;
X		BLUE|blue)	COLOR=${COLOR}'[34m' ;;
X		MAGENTA|magenta)	COLOR=${COLOR}'[35m' ;;
X		CYAN|cyan)	COLOR=${COLOR}'[36m' ;;
X		WHITE|white)	COLOR=${COLOR}'[37m' ;;
X		BBLACK|bblack)	COLOR=${COLOR}'[40m' ;;
X		BRED|bred)	COLOR=${COLOR}'[41m' ;;
X		BGREEN|bgreen)	COLOR=${COLOR}'[42m' ;;
X		BYELLOW|byellow)	COLOR=${COLOR}'[43m' ;;
X		BBLUE|bblue)	COLOR=${COLOR}'[44m' ;;
X		BMAGENTA|bmagenta)	COLOR=${COLOR}'[45m' ;;
X		BCYAN|bcyan)	COLOR=${COLOR}'[46m' ;;
X		BWHITE|bwhite)	COLOR=${COLOR}'[47m' ;;
X		UNDER|under)	COLOR=${COLOR}'[4m'   ;;
X		NOUNDER|nounder)	COLOR=${COLOR}'[24m' ;;
X		BLINK|blink)	COLOR=${COLOR}'[5m'   ;;
X		NOBLINK|noblink)	COLOR=${COLOR}'[25m' ;;
X		BOLD|bold)	COLOR=${COLOR}'[1m'  ;;
X		NOBOLD|nobold)	COLOR=${COLOR}'[22m' ;;
X		INVISIBLE|invisible)	COLOR=${COLOR}'[8m' ;;
X	esac
X	shift
Xdone
X# let dirstack's "kcd" function know what happened here: export
X#	the part of the prompt string that sets the colors
Xtypeset -x PSC="${COLOR}"
Xecho "${COLOR}"
Xcd .
EOF
chars=`wc -c < 'COLOR.KSH'`
if test $chars !=     1392; then echo 'COLOR.KSH' is $chars characters, should be     1392 characters!; fi
fi
#! /bin/sh
# This file was wrapped with "dummyshar".  "sh" this file to extract.
# Contents:  COLOR.HLP
echo extracting 'COLOR.HLP'
if test -f 'COLOR.HLP' -a -z "$1"; then echo Not overwriting 'COLOR.HLP'; else
sed 's/^X//' << \EOF > 'COLOR.HLP'
XDocumentation for COLOR.KSH   [This file must be named COLOR.HLP]
X
X  COLOR.KSH may be used to set foreground and background colors on
X  color monitors and set bold (high intensity), normal or reverse on
X  any type of monitor.
X
X======================================================================
X  Examples: COLOR NORMAL GREEN BOLD
X            COLOR YELLOW BBLUE NORMAL BCYAN WHITE RED YELLOW BBLACK
X            COLOR COLOR NORMAL GREEN
X            COLOR MONO NORMAL BOLD
X======================================================================
XCOLOR.HLP FILE - THIS IS HELP FOR THE COLOR.KSH COMMAND FILE.
X.
X. ANSI.SYS, CONFIG.SYS (with DEVICE=ANSI.SYS) and MODE.COM must be on
X.  the disk when the PC is booted.
X. Type COLOR and then your choice of character, intensity, and/or
X.  background color(s) from the following list of parameters:
X. =====================================================================
X.  ----------------------- GENERAL PARAMETERS -------------------------
X. 1. normal (resets everything)       4. [no]under ([re]sets underline)
X. 2. [no]bold (high intensity off/on) 5. [no]blink ([re]sets blink mode)
X. 3. reverse (reverse video)          6. invisible (guess what...)
X. =====================================================================
X. ---------------------- FOR COLOR DISPLAYS ONLY ----------------------
X. ---- FORGROUND COLORS ---------    ------ BACKGROUND COLORS ---------
X. 1. black        5. blue            1. bblack          5. bblue
X. 2. red          6. magenta         2. bred            6. bmagenta
X. 3. green        7. cyan            3. bgreen          7. bcyan
X. 4. yellow       8. white           4. byellow         8. bwhite
X. =====================================================================
EOF
chars=`wc -c < 'COLOR.HLP'`
if test $chars !=     1781; then echo 'COLOR.HLP' is $chars characters, should be     1781 characters!; fi
fi
exit 0