xref: /illumos-gate/usr/src/cmd/spell/spell.sh (revision 91f79f1d)
17c478bd9Sstevel@tonic-gate#!/usr/bin/ksh
27c478bd9Sstevel@tonic-gate#
37c478bd9Sstevel@tonic-gate# CDDL HEADER START
47c478bd9Sstevel@tonic-gate#
57c478bd9Sstevel@tonic-gate# The contents of this file are subject to the terms of the
6*91f79f1dSVladimir Marek# Common Development and Distribution License (the "License").
7*91f79f1dSVladimir Marek# You may not use this file except in compliance with the License.
87c478bd9Sstevel@tonic-gate#
97c478bd9Sstevel@tonic-gate# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate# or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate# See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate# and limitations under the License.
137c478bd9Sstevel@tonic-gate#
147c478bd9Sstevel@tonic-gate# When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate# If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate# fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate# information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate#
207c478bd9Sstevel@tonic-gate# CDDL HEADER END
217c478bd9Sstevel@tonic-gate#
227c478bd9Sstevel@tonic-gate#	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T
237c478bd9Sstevel@tonic-gate#	  All Rights Reserved
247c478bd9Sstevel@tonic-gate
25*91f79f1dSVladimir Marek# Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
26*91f79f1dSVladimir Marek# Use is subject to license terms.
277c478bd9Sstevel@tonic-gate
287c478bd9Sstevel@tonic-gate#	spell program
297c478bd9Sstevel@tonic-gate# B_SPELL flags, D_SPELL dictionary, F_SPELL input files, H_SPELL history,
307c478bd9Sstevel@tonic-gate# S_SPELL stop, V_SPELL data for -v
317c478bd9Sstevel@tonic-gate# L_SPELL sed script, I_SPELL -i option to deroff
327c478bd9Sstevel@tonic-gatePATH=/usr/lib/spell:/usr/bin:$PATH
337c478bd9Sstevel@tonic-gate
347c478bd9Sstevel@tonic-gateSPELLPROG=/usr/lib/spell/spellprog
357c478bd9Sstevel@tonic-gate
367c478bd9Sstevel@tonic-gateH_SPELL=${H_SPELL:-/var/adm/spellhist}
377c478bd9Sstevel@tonic-gateV_SPELL=/dev/null
387c478bd9Sstevel@tonic-gateF_SPELL=
397c478bd9Sstevel@tonic-gateFT_SPELL=
407c478bd9Sstevel@tonic-gateB_SPELL=
417c478bd9Sstevel@tonic-gateL_SPELL="/usr/bin/sed -e \"/^[.'].*[.'][ 	]*nx[ 	]*\/usr\/lib/d\" -e \"/^[.'].*[.'][ 	]*so[ 	]*\/usr\/lib/d\" -e \"/^[.'][ 	]*so[ 	]*\/usr\/lib/d\" -e \"/^[.'][ 	]*nx[ 	]*\/usr\/lib/d\" "
427c478bd9Sstevel@tonic-gate
437c478bd9Sstevel@tonic-gateLOCAL=
447c478bd9Sstevel@tonic-gate
457c478bd9Sstevel@tonic-gate# mktmpdir - Create a private (mode 0700) temporary directory inside of /tmp
467c478bd9Sstevel@tonic-gate# for this process's temporary files.  We set up a trap to remove the
477c478bd9Sstevel@tonic-gate# directory on exit (trap 0), and also on SIGHUP, SIGINT, SIGQUIT, and
487c478bd9Sstevel@tonic-gate# SIGTERM.
497c478bd9Sstevel@tonic-gate#
507c478bd9Sstevel@tonic-gatemktmpdir() {
517c478bd9Sstevel@tonic-gate	tmpdir=/tmp/spell.$$
527c478bd9Sstevel@tonic-gate	trap "/usr/bin/rm -rf $tmpdir; exit" 0 1 2 13 15
537c478bd9Sstevel@tonic-gate	/usr/bin/mkdir -m 700 $tmpdir || exit 1
547c478bd9Sstevel@tonic-gate}
557c478bd9Sstevel@tonic-gate
567c478bd9Sstevel@tonic-gatemktmpdir
577c478bd9Sstevel@tonic-gate
587c478bd9Sstevel@tonic-gate# figure out whether or not we can use deroff
597c478bd9Sstevel@tonic-gateif [ -x /usr/bin/deroff ]
607c478bd9Sstevel@tonic-gatethen
617c478bd9Sstevel@tonic-gate	DEROFF="deroff \$I_SPELL"
627c478bd9Sstevel@tonic-gateelse
637c478bd9Sstevel@tonic-gate	DEROFF="cat"
647c478bd9Sstevel@tonic-gatefi
657c478bd9Sstevel@tonic-gate
667c478bd9Sstevel@tonic-gate# Filter out + arguments that are incorrectly handled by getopts
677c478bd9Sstevel@tonic-gateset -A args xxx "$@"
687c478bd9Sstevel@tonic-gatewhile [ x${args[$OPTIND]#+} = x${args[$OPTIND]} ] && getopts ablvxi A
697c478bd9Sstevel@tonic-gatedo
707c478bd9Sstevel@tonic-gate	case $A in
717c478bd9Sstevel@tonic-gate	v)	if [ -r /bin/pdp11 ] && /bin/pdp11
727c478bd9Sstevel@tonic-gate		then	gettext "spell: -v option not supported on pdp11\n" 1>&2
737c478bd9Sstevel@tonic-gate			EXIT_SPELL="exit 1"
747c478bd9Sstevel@tonic-gate		else	B_SPELL="$B_SPELL -v"
757c478bd9Sstevel@tonic-gate			V_SPELL=$tmpdir/spell.$$
767c478bd9Sstevel@tonic-gate		fi ;;
777c478bd9Sstevel@tonic-gate	b) 	D_SPELL=${LB_SPELL:-/usr/lib/spell/hlistb}
787c478bd9Sstevel@tonic-gate		B_SPELL="$B_SPELL -b" ;;
797c478bd9Sstevel@tonic-gate	x)	B_SPELL="$B_SPELL -x" ;;
807c478bd9Sstevel@tonic-gate	l)	L_SPELL="cat" ;;
817c478bd9Sstevel@tonic-gate	i)	I_SPELL="-i" ;;
827c478bd9Sstevel@tonic-gate	?)	gettext "Usage: spell [-bvxli] [+local_file] [files...]\n" 1>&2
837c478bd9Sstevel@tonic-gate		exit 1;;
847c478bd9Sstevel@tonic-gate	esac
857c478bd9Sstevel@tonic-gatedone
867c478bd9Sstevel@tonic-gateshift $(($OPTIND - 1))
877c478bd9Sstevel@tonic-gate
887c478bd9Sstevel@tonic-gatefor A in $*
897c478bd9Sstevel@tonic-gatedo
907c478bd9Sstevel@tonic-gate	case $A in
917c478bd9Sstevel@tonic-gate	+*)	if [ "$FIRSTPLUS" = "+" ]
927c478bd9Sstevel@tonic-gate			then	gettext "spell: multiple + options in spell, all but the last are ignored" 1>&2
937c478bd9Sstevel@tonic-gate		fi;
947c478bd9Sstevel@tonic-gate		FIRSTPLUS="$FIRSTPLUS"+
957c478bd9Sstevel@tonic-gate		if  LOCAL=`expr $A : '+\(.*\)' 2>/dev/null`;
967c478bd9Sstevel@tonic-gate		then if test ! -r $LOCAL;
977c478bd9Sstevel@tonic-gate			then printf "`gettext 'spell: Cannot read %s'`\n" "$LOCAL" 1>&2; EXIT_SPELL="exit 1";
987c478bd9Sstevel@tonic-gate		     fi
997c478bd9Sstevel@tonic-gate		else gettext "spell: Cannot identify local spell file\n" 1>&2; EXIT_SPELL="exit 1";
1007c478bd9Sstevel@tonic-gate		fi ;;
1017c478bd9Sstevel@tonic-gate	*)	FT_SPELL="$FT_SPELL $A"
1027c478bd9Sstevel@tonic-gate		if [ -r $A ]; then
1037c478bd9Sstevel@tonic-gate			F_SPELL="$F_SPELL $A"
1047c478bd9Sstevel@tonic-gate		else
1057c478bd9Sstevel@tonic-gate			printf "`gettext 'spell: Cannot read file %s'`\n" "$A" 1>&2
1067c478bd9Sstevel@tonic-gate		fi
1077c478bd9Sstevel@tonic-gate	esac
1087c478bd9Sstevel@tonic-gatedone
1097c478bd9Sstevel@tonic-gate${EXIT_SPELL:-:}
1107c478bd9Sstevel@tonic-gate
1117c478bd9Sstevel@tonic-gateif [ "x$FT_SPELL" != "x$F_SPELL" ] && [ "x$F_SPELL" = "x" ]; then
1127c478bd9Sstevel@tonic-gate	exit 1
1137c478bd9Sstevel@tonic-gatefi
1147c478bd9Sstevel@tonic-gate
1157c478bd9Sstevel@tonic-gate(cat $F_SPELL; printf "\n";) | eval $L_SPELL |\
1167c478bd9Sstevel@tonic-gate eval $DEROFF |\
117*91f79f1dSVladimir Marek LC_ALL=C /usr/bin/tr -cs "[A-Z][a-z][0-9]\'\&\.\,\;\?\:" "[\012*]" |\
1187c478bd9Sstevel@tonic-gate /usr/bin/sed '1,$s/^[^A-Za-z0-9]*//' | /usr/bin/sed '1,$s/[^A-Za-z0-9]*$//' |\
1197c478bd9Sstevel@tonic-gate /usr/bin/sed -n "/[A-Za-z]/p" | /usr/bin/sort -u +0 |\
1207c478bd9Sstevel@tonic-gate $SPELLPROG ${S_SPELL:-/usr/lib/spell/hstop} 1 |\
1217c478bd9Sstevel@tonic-gate $SPELLPROG $B_SPELL ${D_SPELL:-/usr/lib/spell/hlista} $V_SPELL |\
1227c478bd9Sstevel@tonic-gate comm -23 - ${LOCAL:-/dev/null} |\
1237c478bd9Sstevel@tonic-gate tee -a $H_SPELL
1247c478bd9Sstevel@tonic-gate/usr/bin/who am i >>$H_SPELL 2>/dev/null
1257c478bd9Sstevel@tonic-gatecase $V_SPELL in
1267c478bd9Sstevel@tonic-gate/dev/null)
1277c478bd9Sstevel@tonic-gate	exit
1287c478bd9Sstevel@tonic-gateesac
1297c478bd9Sstevel@tonic-gate/usr/bin/sed '/^\./d' $V_SPELL | /usr/bin/sort -u +1f +0
130