xref: /titanic_52/usr/src/cmd/vgrind/vgrind.ksh (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1*7c478bd9Sstevel@tonic-gate#! /usr/bin/ksh
2*7c478bd9Sstevel@tonic-gate#
3*7c478bd9Sstevel@tonic-gate# vgrind
4*7c478bd9Sstevel@tonic-gate# Copyright (c) 1999-2000 by Sun Microsystems, Inc.
5*7c478bd9Sstevel@tonic-gate# All rights reserved.
6*7c478bd9Sstevel@tonic-gate#
7*7c478bd9Sstevel@tonic-gate# ident	"%Z%%M%	%I%	%E% SMI"
8*7c478bd9Sstevel@tonic-gate#
9*7c478bd9Sstevel@tonic-gate# Copyright (c) 1980 Regents of the University of California.
10*7c478bd9Sstevel@tonic-gate# All rights reserved.  The Berkeley software License Agreement
11*7c478bd9Sstevel@tonic-gate# specifies the terms and conditions for redistribution.
12*7c478bd9Sstevel@tonic-gate#
13*7c478bd9Sstevel@tonic-gate#       This is a rewrite in ksh of the command originally written in
14*7c478bd9Sstevel@tonic-gate#       csh whose last incarnation was:
15*7c478bd9Sstevel@tonic-gate#       vgrind.csh 1.16 96/10/14 SMI; from UCB 5.3 (Berkeley) 11/13/85
16*7c478bd9Sstevel@tonic-gate#
17*7c478bd9Sstevel@tonic-gate
18*7c478bd9Sstevel@tonic-gate
19*7c478bd9Sstevel@tonic-gate# Definitions the user can override
20*7c478bd9Sstevel@tonic-gate
21*7c478bd9Sstevel@tonic-gatetroff=${TROFF:-/usr/bin/troff}
22*7c478bd9Sstevel@tonic-gatevfontedpr=${VFONTEDPR:-/usr/lib/vfontedpr}
23*7c478bd9Sstevel@tonic-gatemacros=${TMAC_VGRIND:-/usr/share/lib/tmac/tmac.vgrind}
24*7c478bd9Sstevel@tonic-gatelp=${LP:-/usr/bin/lp}
25*7c478bd9Sstevel@tonic-gate
26*7c478bd9Sstevel@tonic-gate# Internal processing of options
27*7c478bd9Sstevel@tonic-gate
28*7c478bd9Sstevel@tonic-gatedpost=/usr/lib/lp/postscript/dpost
29*7c478bd9Sstevel@tonic-gate
30*7c478bd9Sstevel@tonic-gateargs=""
31*7c478bd9Sstevel@tonic-gatedpostopts="-e 2"
32*7c478bd9Sstevel@tonic-gatefiles=""
33*7c478bd9Sstevel@tonic-gatelpopts=""
34*7c478bd9Sstevel@tonic-gatetroffopts="-t"
35*7c478bd9Sstevel@tonic-gate
36*7c478bd9Sstevel@tonic-gatefilter=0
37*7c478bd9Sstevel@tonic-gateuselp=1
38*7c478bd9Sstevel@tonic-gateusedpost=1
39*7c478bd9Sstevel@tonic-gatestdoutisatty=0
40*7c478bd9Sstevel@tonic-gate
41*7c478bd9Sstevel@tonic-gatepspec=0
42*7c478bd9Sstevel@tonic-gatetspec=0
43*7c478bd9Sstevel@tonic-gatetwospec=0
44*7c478bd9Sstevel@tonic-gate
45*7c478bd9Sstevel@tonic-gateprinter=""
46*7c478bd9Sstevel@tonic-gate
47*7c478bd9Sstevel@tonic-gateif [ -t 1 ] ; then
48*7c478bd9Sstevel@tonic-gate  stdoutisatty=1
49*7c478bd9Sstevel@tonic-gatefi
50*7c478bd9Sstevel@tonic-gate
51*7c478bd9Sstevel@tonic-gate# Process command line options
52*7c478bd9Sstevel@tonic-gate
53*7c478bd9Sstevel@tonic-gatewhile getopts ":2d:fh:l:no:P:s:tT:wWx" opt ; do
54*7c478bd9Sstevel@tonic-gate  case "$opt" in
55*7c478bd9Sstevel@tonic-gate    +*)
56*7c478bd9Sstevel@tonic-gate      /usr/bin/printf "`/usr/bin/gettext TEXT_DOMAIN 'vgrind: bad option %s'`\n" "+$opt" >&2
57*7c478bd9Sstevel@tonic-gate      exit 1
58*7c478bd9Sstevel@tonic-gate      ;;
59*7c478bd9Sstevel@tonic-gate    "?")
60*7c478bd9Sstevel@tonic-gate      /usr/bin/printf "`/usr/bin/gettext TEXT_DOMAIN 'vgrind: bad option %s'`\n" "-$OPTARG" >&2
61*7c478bd9Sstevel@tonic-gate      exit 1
62*7c478bd9Sstevel@tonic-gate      ;;
63*7c478bd9Sstevel@tonic-gate    2)
64*7c478bd9Sstevel@tonic-gate      dpostopts="$dpostopts -p l"
65*7c478bd9Sstevel@tonic-gate      usedpost=1
66*7c478bd9Sstevel@tonic-gate      args="$args -2"
67*7c478bd9Sstevel@tonic-gate      twospec=1
68*7c478bd9Sstevel@tonic-gate      ;;
69*7c478bd9Sstevel@tonic-gate    d)
70*7c478bd9Sstevel@tonic-gate      args="$args -d $OPTARG"
71*7c478bd9Sstevel@tonic-gate      if ! [ -r $OPTARG ] ; then
72*7c478bd9Sstevel@tonic-gate        /usr/bin/printf "`/usr/bin/gettext TEXT_DOMAIN 'vgrind: -%s %s: file not readable'`\n" "$opt" "$OPTARG" >&2
73*7c478bd9Sstevel@tonic-gate        exit 1
74*7c478bd9Sstevel@tonic-gate      fi
75*7c478bd9Sstevel@tonic-gate      ;;
76*7c478bd9Sstevel@tonic-gate    f)
77*7c478bd9Sstevel@tonic-gate      filter=1
78*7c478bd9Sstevel@tonic-gate      args="$args -f"
79*7c478bd9Sstevel@tonic-gate      ;;
80*7c478bd9Sstevel@tonic-gate    h)
81*7c478bd9Sstevel@tonic-gate      args="$args -h '$OPTARG'"
82*7c478bd9Sstevel@tonic-gate      ;;
83*7c478bd9Sstevel@tonic-gate    l)
84*7c478bd9Sstevel@tonic-gate      args="$args -l$OPTARG"
85*7c478bd9Sstevel@tonic-gate      ;;
86*7c478bd9Sstevel@tonic-gate    n)
87*7c478bd9Sstevel@tonic-gate      args="$args -$opt"
88*7c478bd9Sstevel@tonic-gate      ;;
89*7c478bd9Sstevel@tonic-gate    o)
90*7c478bd9Sstevel@tonic-gate      troffopts="$troffopts -o$OPTARG"
91*7c478bd9Sstevel@tonic-gate      ;;
92*7c478bd9Sstevel@tonic-gate    P)
93*7c478bd9Sstevel@tonic-gate      uselp=1
94*7c478bd9Sstevel@tonic-gate      usedpost=1
95*7c478bd9Sstevel@tonic-gate      printer=$OPTARG
96*7c478bd9Sstevel@tonic-gate      pspec=1
97*7c478bd9Sstevel@tonic-gate      ;;
98*7c478bd9Sstevel@tonic-gate    s)
99*7c478bd9Sstevel@tonic-gate      args="$args -s$OPTARG"
100*7c478bd9Sstevel@tonic-gate      ;;
101*7c478bd9Sstevel@tonic-gate    T)
102*7c478bd9Sstevel@tonic-gate      troffopts="$troffopts -T$OPTARG"
103*7c478bd9Sstevel@tonic-gate      ;;
104*7c478bd9Sstevel@tonic-gate    t)
105*7c478bd9Sstevel@tonic-gate      uselp=0
106*7c478bd9Sstevel@tonic-gate      usedpost=0
107*7c478bd9Sstevel@tonic-gate      tspec=1
108*7c478bd9Sstevel@tonic-gate      ;;
109*7c478bd9Sstevel@tonic-gate    W)
110*7c478bd9Sstevel@tonic-gate      # Do nothing with this switch
111*7c478bd9Sstevel@tonic-gate      ;;
112*7c478bd9Sstevel@tonic-gate    w)
113*7c478bd9Sstevel@tonic-gate      args="$args -t"
114*7c478bd9Sstevel@tonic-gate      ;;
115*7c478bd9Sstevel@tonic-gate    x)
116*7c478bd9Sstevel@tonic-gate      args="$args -x"
117*7c478bd9Sstevel@tonic-gate      ;;
118*7c478bd9Sstevel@tonic-gate    *)
119*7c478bd9Sstevel@tonic-gate      troffopts="$troffopts -$opt"
120*7c478bd9Sstevel@tonic-gate      ;;
121*7c478bd9Sstevel@tonic-gate  esac
122*7c478bd9Sstevel@tonic-gate
123*7c478bd9Sstevel@tonic-gate  if [ "$opt" = ":" ] ; then
124*7c478bd9Sstevel@tonic-gate    /usr/bin/printf "`/usr/bin/gettext TEXT_DOMAIN 'vgrind: missing argument to option %s'` \n" "-$OPTARG" >&2
125*7c478bd9Sstevel@tonic-gate    exit 1
126*7c478bd9Sstevel@tonic-gate  fi
127*7c478bd9Sstevel@tonic-gatedone
128*7c478bd9Sstevel@tonic-gate
129*7c478bd9Sstevel@tonic-gateshift $((OPTIND - 1))
130*7c478bd9Sstevel@tonic-gate
131*7c478bd9Sstevel@tonic-gatefor x in "$@" ; do
132*7c478bd9Sstevel@tonic-gate  args="$args '$x'"
133*7c478bd9Sstevel@tonic-gate  files="$files '$x'"
134*7c478bd9Sstevel@tonic-gatedone
135*7c478bd9Sstevel@tonic-gate
136*7c478bd9Sstevel@tonic-gateshift $#
137*7c478bd9Sstevel@tonic-gate
138*7c478bd9Sstevel@tonic-gateif [ $filter -eq 1 -a \( $twospec -eq 1 -o $pspec -eq 1 \) ] ; then
139*7c478bd9Sstevel@tonic-gate  /usr/bin/printf "`/usr/bin/gettext TEXT_DOMAIN 'vgrind: option -f is incompatible with -2 and -P'`\n" >&2
140*7c478bd9Sstevel@tonic-gate  exit 1
141*7c478bd9Sstevel@tonic-gatefi
142*7c478bd9Sstevel@tonic-gate
143*7c478bd9Sstevel@tonic-gateif [ $filter -eq 1 -a $tspec -eq 1 ] ; then
144*7c478bd9Sstevel@tonic-gate  /usr/bin/printf "`/usr/bin/gettext TEXT_DOMAIN 'vgrind: option -f is incompatible with -t'`\n" >&2
145*7c478bd9Sstevel@tonic-gate  exit 1
146*7c478bd9Sstevel@tonic-gatefi
147*7c478bd9Sstevel@tonic-gate
148*7c478bd9Sstevel@tonic-gateif [ $tspec -eq 1 -a \( $twospec -eq 1 -o $pspec -eq 1 \) ] ; then
149*7c478bd9Sstevel@tonic-gate  /usr/bin/printf "`/usr/bin/gettext TEXT_DOMAIN 'vgrind: option -t is incompatible with -2 and -P'`\n" >&2
150*7c478bd9Sstevel@tonic-gate  exit 1
151*7c478bd9Sstevel@tonic-gatefi
152*7c478bd9Sstevel@tonic-gate
153*7c478bd9Sstevel@tonic-gate# Do some reasoning about whether to print
154*7c478bd9Sstevel@tonic-gate
155*7c478bd9Sstevel@tonic-gateif [ $uselp -eq 1 ] ; then
156*7c478bd9Sstevel@tonic-gate  # If we want to print
157*7c478bd9Sstevel@tonic-gate
158*7c478bd9Sstevel@tonic-gate  if [ -z "$printer" ] ; then
159*7c478bd9Sstevel@tonic-gate    # If "-P" was not specified
160*7c478bd9Sstevel@tonic-gate
161*7c478bd9Sstevel@tonic-gate    defaultprinter=`LC_ALL=C /usr/bin/lpstat -d | /usr/bin/sed -e "s/no system default destination//" 2>/dev/null`
162*7c478bd9Sstevel@tonic-gate
163*7c478bd9Sstevel@tonic-gate    if [ -n "$defaultprinter" ] ; then
164*7c478bd9Sstevel@tonic-gate      defaultprinter=`echo $defaultprinter | \
165*7c478bd9Sstevel@tonic-gate        /usr/bin/sed -e "s/system default destination: //" 2>/dev/null`
166*7c478bd9Sstevel@tonic-gate    fi
167*7c478bd9Sstevel@tonic-gate
168*7c478bd9Sstevel@tonic-gate    if [ $stdoutisatty -eq 1 ] ; then
169*7c478bd9Sstevel@tonic-gate      # If stdout is not re-directed
170*7c478bd9Sstevel@tonic-gate
171*7c478bd9Sstevel@tonic-gate      if [ -z "$defaultprinter" ] ; then
172*7c478bd9Sstevel@tonic-gate        uselp=0
173*7c478bd9Sstevel@tonic-gate      else
174*7c478bd9Sstevel@tonic-gate        printer=$defaultprinter
175*7c478bd9Sstevel@tonic-gate      fi
176*7c478bd9Sstevel@tonic-gate    else
177*7c478bd9Sstevel@tonic-gate      # stdout is redirected - assume it is for further processing of
178*7c478bd9Sstevel@tonic-gate      # postscript output.
179*7c478bd9Sstevel@tonic-gate
180*7c478bd9Sstevel@tonic-gate      uselp=0
181*7c478bd9Sstevel@tonic-gate    fi
182*7c478bd9Sstevel@tonic-gate  fi
183*7c478bd9Sstevel@tonic-gatefi
184*7c478bd9Sstevel@tonic-gate
185*7c478bd9Sstevel@tonic-gateif [ $uselp -eq 1 ] ; then
186*7c478bd9Sstevel@tonic-gate  case $(/usr/bin/basename $lp) in
187*7c478bd9Sstevel@tonic-gate   lp)
188*7c478bd9Sstevel@tonic-gate     lpopts="$lpopts -d$printer"
189*7c478bd9Sstevel@tonic-gate     ;;
190*7c478bd9Sstevel@tonic-gate   lpr)
191*7c478bd9Sstevel@tonic-gate     lpopts="$lpopts -P$printer"
192*7c478bd9Sstevel@tonic-gate     ;;
193*7c478bd9Sstevel@tonic-gate   *)
194*7c478bd9Sstevel@tonic-gate     /usr/bin/printf "`/usr/bin/gettext TEXT_DOMAIN 'vgrind: unknown print program %s'`\n" $lp >&2
195*7c478bd9Sstevel@tonic-gate     exit 1
196*7c478bd9Sstevel@tonic-gate     ;;
197*7c478bd9Sstevel@tonic-gate  esac
198*7c478bd9Sstevel@tonic-gatefi
199*7c478bd9Sstevel@tonic-gate
200*7c478bd9Sstevel@tonic-gate# Implementation note:  In the following, we use "eval" to execute the
201*7c478bd9Sstevel@tonic-gate# command in order to preserve spaces which may appear in the -h option
202*7c478bd9Sstevel@tonic-gate# argument (and possibly in file names).
203*7c478bd9Sstevel@tonic-gate
204*7c478bd9Sstevel@tonic-gateif [ $filter -eq 1 ] ; then
205*7c478bd9Sstevel@tonic-gate  eval "$vfontedpr $args | /usr/bin/cat $macros -"
206*7c478bd9Sstevel@tonic-gateelse
207*7c478bd9Sstevel@tonic-gate  cmd="$vfontedpr $args"
208*7c478bd9Sstevel@tonic-gate
209*7c478bd9Sstevel@tonic-gate  if [ -r index ] ; then
210*7c478bd9Sstevel@tonic-gate    # Removes any entries from the index that come from the files we are
211*7c478bd9Sstevel@tonic-gate    # processing.
212*7c478bd9Sstevel@tonic-gate
213*7c478bd9Sstevel@tonic-gate    echo > nindex
214*7c478bd9Sstevel@tonic-gate
215*7c478bd9Sstevel@tonic-gate    for i in "$files" ; do
216*7c478bd9Sstevel@tonic-gate      echo "? $i ?d" | /usr/bin/sed -e "s:/:\\\/:g" -e "s:?:/:g" >> nindex
217*7c478bd9Sstevel@tonic-gate    done
218*7c478bd9Sstevel@tonic-gate
219*7c478bd9Sstevel@tonic-gate    /usr/bin/sed -f nindex index > xindex
220*7c478bd9Sstevel@tonic-gate
221*7c478bd9Sstevel@tonic-gate    # Now process the input.
222*7c478bd9Sstevel@tonic-gate    # (! [$filter -eq 1])
223*7c478bd9Sstevel@tonic-gate
224*7c478bd9Sstevel@tonic-gate    trap "rm -f xindex nindex; exit 1" INT QUIT
225*7c478bd9Sstevel@tonic-gate
226*7c478bd9Sstevel@tonic-gate    cmd="$cmd | $troff -rx1 $troffopts -i $macros - 2>> xindex"
227*7c478bd9Sstevel@tonic-gate
228*7c478bd9Sstevel@tonic-gate    if [ $usedpost -eq 1 ] ; then
229*7c478bd9Sstevel@tonic-gate      cmd="$cmd | $dpost $dpostopts"
230*7c478bd9Sstevel@tonic-gate    fi
231*7c478bd9Sstevel@tonic-gate
232*7c478bd9Sstevel@tonic-gate    if [ $uselp -eq 1 ] ; then
233*7c478bd9Sstevel@tonic-gate      cmd="$cmd | $lp $lpopts"
234*7c478bd9Sstevel@tonic-gate    fi
235*7c478bd9Sstevel@tonic-gate
236*7c478bd9Sstevel@tonic-gate    eval $cmd
237*7c478bd9Sstevel@tonic-gate    trap - INT QUIT
238*7c478bd9Sstevel@tonic-gate    /usr/bin/sort -dfu +0 -2 xindex > index
239*7c478bd9Sstevel@tonic-gate    /usr/bin/rm nindex xindex
240*7c478bd9Sstevel@tonic-gate  else
241*7c478bd9Sstevel@tonic-gate    # (! [ -r index ])
242*7c478bd9Sstevel@tonic-gate
243*7c478bd9Sstevel@tonic-gate    cmd="$cmd | $troff -i $troffopts $macros -"
244*7c478bd9Sstevel@tonic-gate
245*7c478bd9Sstevel@tonic-gate    if [ $usedpost -eq 1 ] ; then
246*7c478bd9Sstevel@tonic-gate      cmd="$cmd | $dpost $dpostopts"
247*7c478bd9Sstevel@tonic-gate    fi
248*7c478bd9Sstevel@tonic-gate
249*7c478bd9Sstevel@tonic-gate    if [ $uselp -eq 1 ] ; then
250*7c478bd9Sstevel@tonic-gate        cmd="$cmd | $lp $lpopts"
251*7c478bd9Sstevel@tonic-gate    fi
252*7c478bd9Sstevel@tonic-gate
253*7c478bd9Sstevel@tonic-gate    eval $cmd
254*7c478bd9Sstevel@tonic-gate  fi
255*7c478bd9Sstevel@tonic-gatefi
256