8322 nl: misleading-indentation
[unleashed/tickless.git] / usr / src / cmd / vgrind / vgrind.ksh
blob6444554c83d560aa0412385b23779505fc7ff46a
1 #! /usr/bin/ksh
3 # vgrind
4 # Copyright (c) 1999-2000 by Sun Microsystems, Inc.
5 # All rights reserved.
7 # ident "%Z%%M% %I% %E% SMI"
9 # Copyright (c) 1980 Regents of the University of California.
10 # All rights reserved. The Berkeley software License Agreement
11 # specifies the terms and conditions for redistribution.
13 # This is a rewrite in ksh of the command originally written in
14 # csh whose last incarnation was:
15 # vgrind.csh 1.16 96/10/14 SMI; from UCB 5.3 (Berkeley) 11/13/85
19 # Definitions the user can override
21 troff=${TROFF:-/usr/bin/troff}
22 vfontedpr=${VFONTEDPR:-/usr/lib/vfontedpr}
23 macros=${TMAC_VGRIND:-/usr/share/lib/tmac/tmac.vgrind}
24 lp=${LP:-/usr/bin/lp}
26 # Internal processing of options
28 dpost=/usr/lib/lp/postscript/dpost
30 args=""
31 dpostopts="-e 2"
32 files=""
33 lpopts=""
34 troffopts="-t"
36 filter=0
37 uselp=1
38 usedpost=1
39 stdoutisatty=0
41 pspec=0
42 tspec=0
43 twospec=0
45 printer=""
47 if [ -t 1 ] ; then
48 stdoutisatty=1
51 # Process command line options
53 while getopts ":2d:fh:l:no:P:s:tT:wWx" opt ; do
54 case "$opt" in
55 +*)
56 /usr/bin/printf "`/usr/bin/gettext TEXT_DOMAIN 'vgrind: bad option %s'`\n" "+$opt" >&2
57 exit 1
59 "?")
60 /usr/bin/printf "`/usr/bin/gettext TEXT_DOMAIN 'vgrind: bad option %s'`\n" "-$OPTARG" >&2
61 exit 1
64 dpostopts="$dpostopts -p l"
65 usedpost=1
66 args="$args -2"
67 twospec=1
70 args="$args -d $OPTARG"
71 if ! [ -r $OPTARG ] ; then
72 /usr/bin/printf "`/usr/bin/gettext TEXT_DOMAIN 'vgrind: -%s %s: file not readable'`\n" "$opt" "$OPTARG" >&2
73 exit 1
77 filter=1
78 args="$args -f"
81 args="$args -h '$OPTARG'"
84 args="$args -l$OPTARG"
87 args="$args -$opt"
90 troffopts="$troffopts -o$OPTARG"
93 uselp=1
94 usedpost=1
95 printer=$OPTARG
96 pspec=1
99 args="$args -s$OPTARG"
102 troffopts="$troffopts -T$OPTARG"
105 uselp=0
106 usedpost=0
107 tspec=1
110 # Do nothing with this switch
113 args="$args -t"
116 args="$args -x"
119 troffopts="$troffopts -$opt"
121 esac
123 if [ "$opt" = ":" ] ; then
124 /usr/bin/printf "`/usr/bin/gettext TEXT_DOMAIN 'vgrind: missing argument to option %s'` \n" "-$OPTARG" >&2
125 exit 1
127 done
129 shift $((OPTIND - 1))
131 for x in "$@" ; do
132 args="$args '$x'"
133 files="$files '$x'"
134 done
136 shift $#
138 if [ $filter -eq 1 -a \( $twospec -eq 1 -o $pspec -eq 1 \) ] ; then
139 /usr/bin/printf "`/usr/bin/gettext TEXT_DOMAIN 'vgrind: option -f is incompatible with -2 and -P'`\n" >&2
140 exit 1
143 if [ $filter -eq 1 -a $tspec -eq 1 ] ; then
144 /usr/bin/printf "`/usr/bin/gettext TEXT_DOMAIN 'vgrind: option -f is incompatible with -t'`\n" >&2
145 exit 1
148 if [ $tspec -eq 1 -a \( $twospec -eq 1 -o $pspec -eq 1 \) ] ; then
149 /usr/bin/printf "`/usr/bin/gettext TEXT_DOMAIN 'vgrind: option -t is incompatible with -2 and -P'`\n" >&2
150 exit 1
153 # Do some reasoning about whether to print
155 if [ $uselp -eq 1 ] ; then
156 # If we want to print
158 if [ -z "$printer" ] ; then
159 # If "-P" was not specified
161 defaultprinter=`LC_ALL=C /usr/bin/lpstat -d | /usr/bin/sed -e "s/no system default destination//" 2>/dev/null`
163 if [ -n "$defaultprinter" ] ; then
164 defaultprinter=`echo $defaultprinter | \
165 /usr/bin/sed -e "s/system default destination: //" 2>/dev/null`
168 if [ $stdoutisatty -eq 1 ] ; then
169 # If stdout is not re-directed
171 if [ -z "$defaultprinter" ] ; then
172 uselp=0
173 else
174 printer=$defaultprinter
176 else
177 # stdout is redirected - assume it is for further processing of
178 # postscript output.
180 uselp=0
185 if [ $uselp -eq 1 ] ; then
186 case $(/usr/bin/basename $lp) in
188 lpopts="$lpopts -d$printer"
190 lpr)
191 lpopts="$lpopts -P$printer"
194 /usr/bin/printf "`/usr/bin/gettext TEXT_DOMAIN 'vgrind: unknown print program %s'`\n" $lp >&2
195 exit 1
197 esac
200 # Implementation note: In the following, we use "eval" to execute the
201 # command in order to preserve spaces which may appear in the -h option
202 # argument (and possibly in file names).
204 if [ $filter -eq 1 ] ; then
205 eval "$vfontedpr $args | /usr/bin/cat $macros -"
206 else
207 cmd="$vfontedpr $args"
209 if [ -r index ] ; then
210 # Removes any entries from the index that come from the files we are
211 # processing.
213 echo > nindex
215 for i in "$files" ; do
216 echo "? $i ?d" | /usr/bin/sed -e "s:/:\\\/:g" -e "s:?:/:g" >> nindex
217 done
219 /usr/bin/sed -f nindex index > xindex
221 # Now process the input.
222 # (! [$filter -eq 1])
224 trap "rm -f xindex nindex; exit 1" INT QUIT
226 cmd="$cmd | $troff -rx1 $troffopts -i $macros - 2>> xindex"
228 if [ $usedpost -eq 1 ] ; then
229 cmd="$cmd | $dpost $dpostopts"
232 if [ $uselp -eq 1 ] ; then
233 cmd="$cmd | $lp $lpopts"
236 eval $cmd
237 trap - INT QUIT
238 /usr/bin/sort -dfu +0 -2 xindex > index
239 /usr/bin/rm nindex xindex
240 else
241 # (! [ -r index ])
243 cmd="$cmd | $troff -i $troffopts $macros -"
245 if [ $usedpost -eq 1 ] ; then
246 cmd="$cmd | $dpost $dpostopts"
249 if [ $uselp -eq 1 ] ; then
250 cmd="$cmd | $lp $lpopts"
253 eval $cmd