[unix-pc.general] pushd/popd as ksh functions: possible in unix pc ksh?

afc@shibaya.lonestar.org (Augustine Cano) (12/28/90)

Happy new year y'all!

I just typed in the pushd/popd functions found in "The Korn Shell" by Bolsky
and Korn and guess what... they don't work!  Is this because of our ancient
version?  Has anybody attempted this?  Does anybody have a working version
of pushd/popd as ksh functions that work on our antiquated shell?

Alternatively, is a newer version of ksh available for the unix pc? (without
paying the toolchest prices, that is...)

Thanks in advance for any enlightenment..

-- 
Augustine Cano		INTERNET: afc@shibaya.lonestar.org
			UUCP:     ...!{ernest,egsner}!shibaya!afc

thad@cup.portal.com (Thad P Floryan) (12/29/90)

afc@shibaya.lonestar.org (Augustine Cano)
in <1990Dec28.054110.13613@shibaya.lonestar.org> writes:

	I just typed in the pushd/popd functions found in "The Korn Shell" by
	Bolsky and Korn and guess what... they don't work!
	[...]
	Alternatively, is a newer version of ksh available for the unix pc?
	(without paying the toolchest prices, that is...)

What version of ksh did you use?  One way to find out is to set "emacs" mode
(e.g.  set -o emacs) then type a control-V (i.e. ^V) and see what displays.

The version that accompanied the UNIXPC and which accompanied the "fixdisks"
is a brain-damaged version; the version from The STORE (at osu-cis) is the
1986 version for which most things in "The KORN Shell" book will work unless
EXPLICITLY stated otherwise.

The "stock" ksh for the UNIXPC has a version something like "1.2 ..." (don't
know for sure and cannot find out since I deleted it), and the "STORE" version
will display "Version 06/03/86" which IS a fairly recent and decent version.

Since I checked AT&T's "Toolchest" recently (re: curses), I found the source
price of the latest ksh to be $3,000.  Ouch.  Maybe if some 3,000 UNIXPC owners
each contribute $1 and designate ONE person to actual receive the source (all
stuff from the Toolchest is sent via UUCP) who will then distribute the binary
executable ....    (well, we CAN dream)

If our Users' Group secretary ever gets the incorporation papers drawn up,
perhaps we can qualify as an "educational institution" and get some discounts
on this stuff, or sumtin', maybe even get the UNIXPC's SVR2 source so we can
support all UNIXPC owners.  I wouldn't mind doing the work and donating my
time, but none of us can afford the $$$ for a "full" source license.

Sigh.  I couldn't even get the source to curses and terminfo, and finally had
to get a SVR3-compatible version from Aspen Scientific.

Thad Floryan [ thad@cup.portal.com (OR) ..!sun!portal!cup.portal.com!thad ]

bruce@balilly.UUCP (Bruce Lilly) (12/29/90)

In article <1990Dec28.054110.13613@shibaya.lonestar.org> afc@shibaya.lonestar.org (Augustine Cano) writes:
>
>I just typed in the pushd/popd functions found in "The Korn Shell" by Bolsky
>and Korn and guess what... they don't work!  Is this because of our ancient
>version?  Has anybody attempted this?  Does anybody have a working version
>of pushd/popd as ksh functions that work on our antiquated shell?

In addition to Thad's comments, I can offer the following which ``sort
of'' work (seems to be a minor problem after su'ing).  These are the
``dirs'', ``cd'', and ``mcd'' functions described in the same section as
``pushd'' and ``popd''.  The push and pop stuff is in-line.  I hope this
helps.  I've also included a function ``su'' which allows one to run a
shell as another user in a new window (using windy) -- with this, I only
run a single login window, as I can then run a new window (e.g. as root)
to do other things.

	unalias cd
	alias cd=_cd

	function _cd
	{
		typeset dir=$1
		integer n=0 type=4
		case $1 in
		-|-1|2) # cd -
			n=_push_top type=1
			;;
		-[1-9]|-[1-9][0-9])	# cd -n
			n=_push_top+${1#-}-1 type=2
			;;
		1)	# keep present directory
			print -r - "$PWD"
			return
			;;
		[2-9]|[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))
				then	print cd: Directory stack not that deep.
					return 1
				else	dir=${_push_stack[n]}
				fi
		fi
		case $dir in
		\~)	dir=$HOME
			;;
		\~/*)	dir=$HOME${dir#~}
				;;
		\~*)	dirleft=${dir%%/*}
			dirright=${dir##*/}
			if [ "$dirleft" = "$dirright" ]
			then
				dirright=
			else
				dirright=/$dirright
			fi
			username=${dirleft#~}
			userhome=`grep ^$username: /etc/passwd | cut -d: -f6 | sed -e "s,/$,,"`
			dir=$userhome$dirright
			unset dirright dirleft username userhome
			;;
		esac
		\cd "${dir:-$@}" > /dev/null || return 1
		dir=${OLDPWD#$HOME/}
		case $dir in
		$HOME)
			dir=\~
			;;
		/*)	;;
		*)	dir=\~/$dir;;
		esac
		case $type in
		1)	# swap first two elements
			_push_stack[_push_top=_push_top-1]=$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"
	}

	function dirs
	{
		typeset dir=${PWD#$HOME/}
		case $dir in
		$HOME)
			dir=\~ ;;
		/*)	;;
		*)	dir=\~/$dir;;
		esac
		print -r - "$dir ${_push_stack[@]}"
	}

	function mcd
	{
		typeset dir="${PWD#$HOME/}"
		case $dir in
		~/*)	dir=$HOME${dir#~};;
		esac
		case $dir in
		$HOME)
			dir=\~
			;;
		/*)	;;
		*)	dir=\~/$dir;;
		esac
		OLDPS3=$PS3
		PS3='Select by number or enter a name: '
		select dir in "$dir" "${_push_stack[@]}"
		do	if	_cd $REPLY
			then	return
			fi
		done
		PS3=$OLDPS3
	}

	function su {
		if [ "$1" = "-" ]
		then
			# no problem - .profile will take care of things
			windy -b /bin/su $*
		else
			# need to make sure ksh is used
			# but can't simply force "-" because that will clear environment
			# need to take care of ENV and HISTFILE
			tempENV=$ENV
			tempHISTFILE=$HISTFILE
			tempHISTSIZE=$HISTSIZE
			tempHOME=$HOME
			tempMAIL=$MAIL
			tempMAILPATH=$MAILPATH
			if [ $# = 0 ]
			then
				# no args - root is default
				suuser=root
				suhome=""
			else
				# have some args - $1 must be user
				suuser=$1
				suhome=`grep "^${suuser}:" /etc/passwd | cut -d: -f6 | sed -e "s,/$,,"`
				shift
			fi
			ENV=${suhome}/.kshrc
			HISTFILE=${suhome}/.kshistory
			HISTSIZE=999
			HOME=${suhome:=/}
			unset MAIL
			MAILPATH=$MAILPATH:/usr/mail/$suuser:/usr/spool/uucppublic/$suuser
			windy -b /bin/su ${suuser} -c "exec /bin/ksh $*"
			ENV=$tempENV
			HISTFILE=$tempHISTFILE
			HISTSIZE=$tempHISTSIZE
			HOME=$tempHOME
			MAIL=$tempMAIL
			MAILPATH=$tempMAILPATH
			unset suuser suhome tempENV tempHISTFILE tempHISTSIZE tempHOME tempMAIL tempMAILPATH
		fi
	}

--
	Bruce Lilly		blilly!balilly!bruce@sonyd1.Broadcast.Sony.COM

kak@hico2.UUCP (Kris A. Kugel) (01/04/91)

In article <37339@cup.portal.com>, thad@cup.portal.com (Thad P Floryan) writes:
# afc@shibaya.lonestar.org (Augustine Cano)
# in <1990Dec28.054110.13613@shibaya.lonestar.org> writes:
# . . .
# # Alternatively, is a newer version of ksh available for the unix pc?
# # (without paying the toolchest prices, that is...)
# 
# The version that accompanied the UNIXPC and which accompanied the "fixdisks"
# is a brain-damaged version; the version from The STORE (at osu-cis) is the
# 1986 version for which most things in "The KORN Shell" book will work unless
# EXPLICITLY stated otherwise.

I'm running the 1.2 shell, and I've got a functioning pushd that looks like
it has the same ultimate source as the one in the KORN shell book.
I've forwarded it to Augustine Cano, maybe we'll get a followup report.

# 
# The "stock" ksh for the UNIXPC has a version something like "1.2 ..." (don't
# know for sure and cannot find out since I deleted it), and the "STORE" version
# will display "Version 06/03/86" which IS a fairly recent and decent version.
# 
# Since I checked AT&T's "Toolchest" recently (re: curses), I found the source
# price of the latest ksh to be $3,000.  Ouch.
# . . .
# Thad Floryan [ thad@cup.portal.com (OR) ..!sun!portal!cup.portal.com!thad ]

There's evidently a P D version of the Korn Shell posted recently;
I've seen some "bug reports" for it posted.  Sadly, I don't have it.
And I don't know how good it is.  But it's SOMETHING.

                               Kris A. Kugel
                             ( 908 ) 842-2707
          { uunet | rutgers | att }!westmark!hico2!kak
                         {daver,ditka,zorch}!hico2!kak
                         internet: kak@hico2.westmark.com