xref: /titanic_41/usr/src/cmd/vgrind/vgrind.ksh (revision 6185db853e024a486ff8837e6784dd290d866112)
1#! /usr/bin/ksh
2#
3# vgrind
4# Copyright (c) 1999-2000 by Sun Microsystems, Inc.
5# All rights reserved.
6#
7# ident	"%Z%%M%	%I%	%E% SMI"
8#
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.
12#
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
16#
17
18
19# Definitions the user can override
20
21troff=${TROFF:-/usr/bin/troff}
22vfontedpr=${VFONTEDPR:-/usr/lib/vfontedpr}
23macros=${TMAC_VGRIND:-/usr/share/lib/tmac/tmac.vgrind}
24lp=${LP:-/usr/bin/lp}
25
26# Internal processing of options
27
28dpost=/usr/lib/lp/postscript/dpost
29
30args=""
31dpostopts="-e 2"
32files=""
33lpopts=""
34troffopts="-t"
35
36filter=0
37uselp=1
38usedpost=1
39stdoutisatty=0
40
41pspec=0
42tspec=0
43twospec=0
44
45printer=""
46
47if [ -t 1 ] ; then
48  stdoutisatty=1
49fi
50
51# Process command line options
52
53while 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
58      ;;
59    "?")
60      /usr/bin/printf "`/usr/bin/gettext TEXT_DOMAIN 'vgrind: bad option %s'`\n" "-$OPTARG" >&2
61      exit 1
62      ;;
63    2)
64      dpostopts="$dpostopts -p l"
65      usedpost=1
66      args="$args -2"
67      twospec=1
68      ;;
69    d)
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
74      fi
75      ;;
76    f)
77      filter=1
78      args="$args -f"
79      ;;
80    h)
81      args="$args -h '$OPTARG'"
82      ;;
83    l)
84      args="$args -l$OPTARG"
85      ;;
86    n)
87      args="$args -$opt"
88      ;;
89    o)
90      troffopts="$troffopts -o$OPTARG"
91      ;;
92    P)
93      uselp=1
94      usedpost=1
95      printer=$OPTARG
96      pspec=1
97      ;;
98    s)
99      args="$args -s$OPTARG"
100      ;;
101    T)
102      troffopts="$troffopts -T$OPTARG"
103      ;;
104    t)
105      uselp=0
106      usedpost=0
107      tspec=1
108      ;;
109    W)
110      # Do nothing with this switch
111      ;;
112    w)
113      args="$args -t"
114      ;;
115    x)
116      args="$args -x"
117      ;;
118    *)
119      troffopts="$troffopts -$opt"
120      ;;
121  esac
122
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
126  fi
127done
128
129shift $((OPTIND - 1))
130
131for x in "$@" ; do
132  args="$args '$x'"
133  files="$files '$x'"
134done
135
136shift $#
137
138if [ $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
141fi
142
143if [ $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
146fi
147
148if [ $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
151fi
152
153# Do some reasoning about whether to print
154
155if [ $uselp -eq 1 ] ; then
156  # If we want to print
157
158  if [ -z "$printer" ] ; then
159    # If "-P" was not specified
160
161    defaultprinter=`LC_ALL=C /usr/bin/lpstat -d | /usr/bin/sed -e "s/no system default destination//" 2>/dev/null`
162
163    if [ -n "$defaultprinter" ] ; then
164      defaultprinter=`echo $defaultprinter | \
165        /usr/bin/sed -e "s/system default destination: //" 2>/dev/null`
166    fi
167
168    if [ $stdoutisatty -eq 1 ] ; then
169      # If stdout is not re-directed
170
171      if [ -z "$defaultprinter" ] ; then
172        uselp=0
173      else
174        printer=$defaultprinter
175      fi
176    else
177      # stdout is redirected - assume it is for further processing of
178      # postscript output.
179
180      uselp=0
181    fi
182  fi
183fi
184
185if [ $uselp -eq 1 ] ; then
186  case $(/usr/bin/basename $lp) in
187   lp)
188     lpopts="$lpopts -d$printer"
189     ;;
190   lpr)
191     lpopts="$lpopts -P$printer"
192     ;;
193   *)
194     /usr/bin/printf "`/usr/bin/gettext TEXT_DOMAIN 'vgrind: unknown print program %s'`\n" $lp >&2
195     exit 1
196     ;;
197  esac
198fi
199
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).
203
204if [ $filter -eq 1 ] ; then
205  eval "$vfontedpr $args | /usr/bin/cat $macros -"
206else
207  cmd="$vfontedpr $args"
208
209  if [ -r index ] ; then
210    # Removes any entries from the index that come from the files we are
211    # processing.
212
213    echo > nindex
214
215    for i in "$files" ; do
216      echo "? $i ?d" | /usr/bin/sed -e "s:/:\\\/:g" -e "s:?:/:g" >> nindex
217    done
218
219    /usr/bin/sed -f nindex index > xindex
220
221    # Now process the input.
222    # (! [$filter -eq 1])
223
224    trap "rm -f xindex nindex; exit 1" INT QUIT
225
226    cmd="$cmd | $troff -rx1 $troffopts -i $macros - 2>> xindex"
227
228    if [ $usedpost -eq 1 ] ; then
229      cmd="$cmd | $dpost $dpostopts"
230    fi
231
232    if [ $uselp -eq 1 ] ; then
233      cmd="$cmd | $lp $lpopts"
234    fi
235
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 ])
242
243    cmd="$cmd | $troff -i $troffopts $macros -"
244
245    if [ $usedpost -eq 1 ] ; then
246      cmd="$cmd | $dpost $dpostopts"
247    fi
248
249    if [ $uselp -eq 1 ] ; then
250        cmd="$cmd | $lp $lpopts"
251    fi
252
253    eval $cmd
254  fi
255fi
256