1#!/bin/mksh
2# $MirOS: src/usr.bin/spell/spell.ksh,v 1.5 2009/10/19 20:43:50 tg Exp $
3# $OpenBSD: spell.ksh,v 1.9 2005/07/06 07:08:05 jmc Exp $
4#
5# Copyright (c) 2001, 2003 Todd C. Miller <Todd.Miller@courtesan.com>
6#
7# Permission to use, copy, modify, and distribute this software for any
8# purpose with or without fee is hereby granted, provided that the above
9# copyright notice and this permission notice appear in all copies.
10#
11# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18#
19# Sponsored in part by the Defense Advanced Research Projects
20# Agency (DARPA) and Air Force Research Laboratory, Air Force
21# Materiel Command, USAF, under agreement number F39502-99-1-0512.
22#
23SPELLPROG=/usr/libexec/spellprog
24DICT=/usr/share/dict/words
25LOCAL_DICT=/usr/local/share/dict/words
26STOP=/usr/share/dict/stop
27LOCAL_STOP=/usr/local/share/dict/stop
28AMERICAN=/usr/share/dict/american
29BRITISH=/usr/share/dict/british
30LANG=$AMERICAN
31STOP_LANG=$BRITISH
32EXTRA=
33FLAGS=
34DEROFF="deroff -w"
35HISTFILE=
36TMP=`mktemp /tmp/spell.XXXXXXXX` || exit 1
37VTMP=
38USAGE="usage: spell [-biltvx] [-d list] [-h spellhist] [-m a | e | l | m | s]\n\t[-s stop] [+extra_list] [file ...]"
39
40OPTIND=1		# force getopts to reset itself
41
42trap "rm -f $TMP $VTMP; exit 0" 0 1 2 15
43
44# Use local word/stop lists if they exist
45if [ -f $LOCAL_DICT ]; then
46	DICT="$DICT $LOCAL_DICT"
47fi
48if [ -f $LOCAL_STOP ]; then
49	STOP="$STOP $LOCAL_STOP"
50fi
51
52while getopts "biltvxd:h:m:s:" c; do
53	case $c in
54	b)	LANG=$BRITISH
55		STOP_LANG=$AMERICAN
56		FLAGS[${#FLAGS[@]}]="-b"
57		;;
58	i)	DEROFF="$DEROFF -i"
59		;;
60	l)	DEROFF="delatex"
61		;;
62	m)	DEROFF="$DEROFF -m $OPTARG"
63		;;
64	t)	DEROFF="detex"
65		;;
66	v)	VTMP=`mktemp /tmp/spell.XXXXXXXX` || {
67			rm -f ${TMP}
68			exit 1
69		}
70		FLAGS[${#FLAGS[@]}]="-v"
71		FLAGS[${#FLAGS[@]}]="-o"
72		FLAGS[${#FLAGS[@]}]="$VTMP"
73		;;
74	x)	FLAGS[${#FLAGS[@]}]="-x"
75		;;
76	d)	DICT="$OPTARG"
77		LANG=
78		;;
79	s)	STOP="$OPTARG"
80		STOP_LANG=
81		LOCAL_STOP=
82		;;
83	h)	HISTFILE="$OPTARG"
84		;;
85	*)	echo "$USAGE" 1>&2
86		exit 1
87		;;
88	esac
89done
90shift $(( $OPTIND - 1 ))
91
92while test $# -ne 0; do
93	case "$1" in
94		+*)	EXTRA="$EXTRA ${1#+}"
95			shift
96			;;
97		*)	break
98			;;
99	esac
100done
101
102# Any parameters left are files to be checked, pass them to deroff
103DEROFF="$DEROFF $@"
104
105if [ -n "$HISTFILE" ]; then
106	$DEROFF | sort -u | $SPELLPROG -o $TMP $STOP $STOP_LANG | \
107	    $SPELLPROG ${FLAGS[*]} $DICT $LANG $EXTRA | sort -u -k1f - $TMP | \
108	    tee -a $HISTFILE
109	who -m >> $HISTFILE
110else
111	$DEROFF | sort -u | $SPELLPROG -o $TMP $STOP $STOP_LANG | \
112	    $SPELLPROG ${FLAGS[*]} $DICT $LANG $EXTRA | sort -u -k1f - $TMP
113fi
114
115if [ -n "$VTMP" ]; then
116	sort -u -k2f -k1 $VTMP
117fi
118
119exit 0
120