Current File : //bin/spell
#!/usr/bin/ksh
#
#	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T
#	  All Rights Reserved

# Copyright (c) 1988, 2012, Oracle and/or its affiliates. All rights reserved.

#	spell program
# B_SPELL flags, D_SPELL dictionary, F_SPELL input files, H_SPELL history,
# S_SPELL stop, V_SPELL data for -v
# L_SPELL sed script, I_SPELL -i option to deroff
PATH=/usr/lib/spell:/usr/bin:$PATH

SPELLPROG=/usr/lib/spell/spellprog

# The algorithm for selecting a spell history file:
#
#    if (H_SPELL is set) then
#        if (the file named by H_SPELL exists) then
#            if (the file named by H_SPELL is writable) then
#                use the file named by H_SPELL
#            else
#                produce no spell history
#        else
#            create a writable file as named by H_SPELL
#                        (owned by user with mode 0600) and use it.
#
#    else if ($HOME/.spellhist exists) then
#        if ($HOME/.spellhist is writable) then
#            use $HOME/.spellhist
#        else
#            produce no spell history
#
#    else
#        create a writable file $HOME/.spellhist 
#
H_SPELL=${H_SPELL:-$HOME/.spellhist}
if [ -f $H_SPELL ]
then
	if [ ! -w $H_SPELL ]
	then
		H_SPELL=/dev/null
	fi
else
	(umask 177; touch $H_SPELL)
fi

V_SPELL=/dev/null
F_SPELL=
FT_SPELL=
B_SPELL=
L_SPELL="/usr/bin/sed -e \"/^[.'].*[.'][ 	]*nx[ 	]*\/usr\/lib/d\" -e \"/^[.'].*[.'][ 	]*so[ 	]*\/usr\/lib/d\" -e \"/^[.'][ 	]*so[ 	]*\/usr\/lib/d\" -e \"/^[.'][ 	]*nx[ 	]*\/usr\/lib/d\" "

LOCAL=

# mktmpdir - Create a private (mode 0700) temporary directory inside of /tmp
# for this process's temporary files.  We set up a trap to remove the
# directory on exit (trap 0), and also on SIGHUP, SIGINT, SIGQUIT, and
# SIGTERM.
#
mktmpdir() {
	tmpdir=`/usr/bin/mktemp -d /tmp/spell.XXXXXX`
	if [ -z "$tmpdir" ]; then exit 1; fi
	trap "/usr/bin/rm -rf $tmpdir; exit" 0 1 2 13 15
}

mktmpdir

# figure out whether or not we can use deroff
if [ -x /usr/bin/deroff ]
then
	DEROFF="deroff \$I_SPELL"
else
	DEROFF="cat"
fi

# Filter out + arguments that are incorrectly handled by getopts
set -A args xxx "$@"
while [ x${args[$OPTIND]#+} = x${args[$OPTIND]} ] && getopts ablvxi A
do
	case $A in
	v)	if [ -r /bin/pdp11 ] && /bin/pdp11 
		then	gettext "spell: -v option not supported on pdp11\n" 1>&2
			EXIT_SPELL="exit 1"
		else	B_SPELL="$B_SPELL -v"
			V_SPELL=$tmpdir/spell.$$
		fi ;;
	b) 	D_SPELL=${LB_SPELL:-/usr/lib/spell/hlistb}
		B_SPELL="$B_SPELL -b" ;;
	x)	B_SPELL="$B_SPELL -x" ;;
	l)	L_SPELL="cat" ;;
	i)	I_SPELL="-i" ;;
	?)	gettext "Usage: spell [-bvxli] [+local_file] [files...]\n" 1>&2
		exit 1;;
	esac
done
shift $(($OPTIND - 1))

for A in $*
do
	case $A in
	+*)	if [ "$FIRSTPLUS" = "+" ]
			then	gettext "spell: multiple + options in spell, all but the last are ignored" 1>&2
		fi;
		FIRSTPLUS="$FIRSTPLUS"+
		if  LOCAL=`expr $A : '+\(.*\)' 2>/dev/null`;
		then if test ! -r $LOCAL;
			then printf "`gettext 'spell: Cannot read %s'`\n" "$LOCAL" 1>&2; EXIT_SPELL="exit 1";
		     fi
		else gettext "spell: Cannot identify local spell file\n" 1>&2; EXIT_SPELL="exit 1";
		fi ;;
	*)	FT_SPELL="$FT_SPELL $A"
		if [ -r $A ]; then
			F_SPELL="$F_SPELL $A"
		else
			printf "`gettext 'spell: Cannot read file %s'`\n" "$A" 1>&2
		fi
	esac
done
${EXIT_SPELL:-:}

if [ "x$FT_SPELL" != "x$F_SPELL" ] && [ "x$F_SPELL" = "x" ]; then
	exit 1
fi

(cat $F_SPELL; printf "\n";) | eval $L_SPELL |\
 eval $DEROFF |\
 LC_ALL=C /usr/bin/tr -cs "[A-Z][a-z][0-9]\'\&\.\,\;\?\:" "[\012*]" |\
 /usr/bin/sed '1,$s/^[^A-Za-z0-9]*//' | /usr/bin/sed '1,$s/[^A-Za-z0-9]*$//' |\
 /usr/bin/sed -n "/[A-Za-z]/p" | /usr/bin/sort -u +0 |\
 $SPELLPROG ${S_SPELL:-/usr/lib/spell/hstop} 1 |\
 $SPELLPROG $B_SPELL ${D_SPELL:-/usr/lib/spell/hlista} $V_SPELL |\
 comm -23 - ${LOCAL:-/dev/null} |\
 tee -a $H_SPELL
/usr/bin/who am i >>$H_SPELL 2>/dev/null
case $V_SPELL in
/dev/null)
	exit
esac
/usr/bin/sed '/^\./d' $V_SPELL | /usr/bin/sort -u +1f +0