xref: /titanic_44/usr/src/cmd/spell/spell.sh (revision 32e0ab73531b6e6e8957e9ecdbbd42603865f2d0)
1#!/usr/bin/ksh
2#
3# CDDL HEADER START
4#
5# The contents of this file are subject to the terms of the
6# Common Development and Distribution License (the "License").
7# You may not use this file except in compliance with the License.
8#
9# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10# or http://www.opensolaris.org/os/licensing.
11# See the License for the specific language governing permissions
12# and limitations under the License.
13#
14# When distributing Covered Code, include this CDDL HEADER in each
15# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16# If applicable, add the following below this CDDL HEADER, with the
17# fields enclosed by brackets "[]" replaced with your own identifying
18# information: Portions Copyright [yyyy] [name of copyright owner]
19#
20# CDDL HEADER END
21#
22#	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T
23#	  All Rights Reserved
24
25# Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
26# Use is subject to license terms.
27
28#	spell program
29# B_SPELL flags, D_SPELL dictionary, F_SPELL input files, H_SPELL history,
30# S_SPELL stop, V_SPELL data for -v
31# L_SPELL sed script, I_SPELL -i option to deroff
32PATH=/usr/lib/spell:/usr/bin:$PATH
33
34SPELLPROG=/usr/lib/spell/spellprog
35
36H_SPELL=${H_SPELL:-/var/adm/spellhist}
37V_SPELL=/dev/null
38F_SPELL=
39FT_SPELL=
40B_SPELL=
41L_SPELL="/usr/bin/sed -e \"/^[.'].*[.'][ 	]*nx[ 	]*\/usr\/lib/d\" -e \"/^[.'].*[.'][ 	]*so[ 	]*\/usr\/lib/d\" -e \"/^[.'][ 	]*so[ 	]*\/usr\/lib/d\" -e \"/^[.'][ 	]*nx[ 	]*\/usr\/lib/d\" "
42
43LOCAL=
44
45# mktmpdir - Create a private (mode 0700) temporary directory inside of /tmp
46# for this process's temporary files.  We set up a trap to remove the
47# directory on exit (trap 0), and also on SIGHUP, SIGINT, SIGQUIT, and
48# SIGTERM.
49#
50mktmpdir() {
51	tmpdir=/tmp/spell.$$
52	trap "/usr/bin/rm -rf $tmpdir; exit" 0 1 2 13 15
53	/usr/bin/mkdir -m 700 $tmpdir || exit 1
54}
55
56mktmpdir
57
58# figure out whether or not we can use deroff
59if [ -x /usr/bin/deroff ]
60then
61	DEROFF="deroff \$I_SPELL"
62else
63	DEROFF="cat"
64fi
65
66# Filter out + arguments that are incorrectly handled by getopts
67set -A args xxx "$@"
68while [ x${args[$OPTIND]#+} = x${args[$OPTIND]} ] && getopts ablvxi A
69do
70	case $A in
71	v)	if [ -r /bin/pdp11 ] && /bin/pdp11
72		then	gettext "spell: -v option not supported on pdp11\n" 1>&2
73			EXIT_SPELL="exit 1"
74		else	B_SPELL="$B_SPELL -v"
75			V_SPELL=$tmpdir/spell.$$
76		fi ;;
77	b) 	D_SPELL=${LB_SPELL:-/usr/lib/spell/hlistb}
78		B_SPELL="$B_SPELL -b" ;;
79	x)	B_SPELL="$B_SPELL -x" ;;
80	l)	L_SPELL="cat" ;;
81	i)	I_SPELL="-i" ;;
82	?)	gettext "Usage: spell [-bvxli] [+local_file] [files...]\n" 1>&2
83		exit 1;;
84	esac
85done
86shift $(($OPTIND - 1))
87
88for A in $*
89do
90	case $A in
91	+*)	if [ "$FIRSTPLUS" = "+" ]
92			then	gettext "spell: multiple + options in spell, all but the last are ignored" 1>&2
93		fi;
94		FIRSTPLUS="$FIRSTPLUS"+
95		if  LOCAL=`expr $A : '+\(.*\)' 2>/dev/null`;
96		then if test ! -r $LOCAL;
97			then printf "`gettext 'spell: Cannot read %s'`\n" "$LOCAL" 1>&2; EXIT_SPELL="exit 1";
98		     fi
99		else gettext "spell: Cannot identify local spell file\n" 1>&2; EXIT_SPELL="exit 1";
100		fi ;;
101	*)	FT_SPELL="$FT_SPELL $A"
102		if [ -r $A ]; then
103			F_SPELL="$F_SPELL $A"
104		else
105			printf "`gettext 'spell: Cannot read file %s'`\n" "$A" 1>&2
106		fi
107	esac
108done
109${EXIT_SPELL:-:}
110
111if [ "x$FT_SPELL" != "x$F_SPELL" ] && [ "x$F_SPELL" = "x" ]; then
112	exit 1
113fi
114
115(cat $F_SPELL; printf "\n";) | eval $L_SPELL |\
116 eval $DEROFF |\
117 LC_ALL=C /usr/bin/tr -cs "[A-Z][a-z][0-9]\'\&\.\,\;\?\:" "[\012*]" |\
118 /usr/bin/sed '1,$s/^[^A-Za-z0-9]*//' | /usr/bin/sed '1,$s/[^A-Za-z0-9]*$//' |\
119 /usr/bin/sed -n "/[A-Za-z]/p" | /usr/bin/sort -u +0 |\
120 $SPELLPROG ${S_SPELL:-/usr/lib/spell/hstop} 1 |\
121 $SPELLPROG $B_SPELL ${D_SPELL:-/usr/lib/spell/hlista} $V_SPELL |\
122 comm -23 - ${LOCAL:-/dev/null} |\
123 tee -a $H_SPELL
124/usr/bin/who am i >>$H_SPELL 2>/dev/null
125case $V_SPELL in
126/dev/null)
127	exit
128esac
129/usr/bin/sed '/^\./d' $V_SPELL | /usr/bin/sort -u +1f +0
130