summaryrefslogtreecommitdiff
path: root/usr/src/cmd/vgrind/vgrind.ksh
blob: 6444554c83d560aa0412385b23779505fc7ff46a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
#! /usr/bin/ksh
#
# vgrind
# Copyright (c) 1999-2000 by Sun Microsystems, Inc.
# All rights reserved.
#
# ident	"%Z%%M%	%I%	%E% SMI"
#
# Copyright (c) 1980 Regents of the University of California.
# All rights reserved.  The Berkeley software License Agreement
# specifies the terms and conditions for redistribution.
#
#       This is a rewrite in ksh of the command originally written in
#       csh whose last incarnation was:
#       vgrind.csh 1.16 96/10/14 SMI; from UCB 5.3 (Berkeley) 11/13/85
#


# Definitions the user can override

troff=${TROFF:-/usr/bin/troff}
vfontedpr=${VFONTEDPR:-/usr/lib/vfontedpr}
macros=${TMAC_VGRIND:-/usr/share/lib/tmac/tmac.vgrind}
lp=${LP:-/usr/bin/lp}

# Internal processing of options

dpost=/usr/lib/lp/postscript/dpost

args=""
dpostopts="-e 2"
files=""
lpopts=""
troffopts="-t"

filter=0
uselp=1
usedpost=1
stdoutisatty=0

pspec=0
tspec=0
twospec=0

printer=""

if [ -t 1 ] ; then
  stdoutisatty=1
fi

# Process command line options

while getopts ":2d:fh:l:no:P:s:tT:wWx" opt ; do
  case "$opt" in
    +*)
      /usr/bin/printf "`/usr/bin/gettext TEXT_DOMAIN 'vgrind: bad option %s'`\n" "+$opt" >&2
      exit 1
      ;;
    "?")
      /usr/bin/printf "`/usr/bin/gettext TEXT_DOMAIN 'vgrind: bad option %s'`\n" "-$OPTARG" >&2
      exit 1
      ;;
    2)
      dpostopts="$dpostopts -p l"
      usedpost=1
      args="$args -2"
      twospec=1
      ;;
    d)
      args="$args -d $OPTARG"
      if ! [ -r $OPTARG ] ; then
        /usr/bin/printf "`/usr/bin/gettext TEXT_DOMAIN 'vgrind: -%s %s: file not readable'`\n" "$opt" "$OPTARG" >&2
        exit 1
      fi
      ;;
    f)
      filter=1
      args="$args -f"
      ;;
    h)
      args="$args -h '$OPTARG'"
      ;;
    l)
      args="$args -l$OPTARG"
      ;;
    n)
      args="$args -$opt"
      ;;
    o)
      troffopts="$troffopts -o$OPTARG"
      ;;
    P)
      uselp=1
      usedpost=1
      printer=$OPTARG
      pspec=1
      ;;
    s)
      args="$args -s$OPTARG"
      ;;
    T)
      troffopts="$troffopts -T$OPTARG"
      ;;
    t)
      uselp=0
      usedpost=0
      tspec=1
      ;;
    W)
      # Do nothing with this switch
      ;;
    w)
      args="$args -t"
      ;;
    x)
      args="$args -x"
      ;;
    *)
      troffopts="$troffopts -$opt"
      ;;
  esac

  if [ "$opt" = ":" ] ; then
    /usr/bin/printf "`/usr/bin/gettext TEXT_DOMAIN 'vgrind: missing argument to option %s'` \n" "-$OPTARG" >&2
    exit 1
  fi
done

shift $((OPTIND - 1))

for x in "$@" ; do
  args="$args '$x'"
  files="$files '$x'"
done

shift $#

if [ $filter -eq 1 -a \( $twospec -eq 1 -o $pspec -eq 1 \) ] ; then
  /usr/bin/printf "`/usr/bin/gettext TEXT_DOMAIN 'vgrind: option -f is incompatible with -2 and -P'`\n" >&2
  exit 1
fi

if [ $filter -eq 1 -a $tspec -eq 1 ] ; then
  /usr/bin/printf "`/usr/bin/gettext TEXT_DOMAIN 'vgrind: option -f is incompatible with -t'`\n" >&2
  exit 1
fi

if [ $tspec -eq 1 -a \( $twospec -eq 1 -o $pspec -eq 1 \) ] ; then
  /usr/bin/printf "`/usr/bin/gettext TEXT_DOMAIN 'vgrind: option -t is incompatible with -2 and -P'`\n" >&2
  exit 1
fi

# Do some reasoning about whether to print

if [ $uselp -eq 1 ] ; then
  # If we want to print

  if [ -z "$printer" ] ; then
    # If "-P" was not specified

    defaultprinter=`LC_ALL=C /usr/bin/lpstat -d | /usr/bin/sed -e "s/no system default destination//" 2>/dev/null`

    if [ -n "$defaultprinter" ] ; then
      defaultprinter=`echo $defaultprinter | \
        /usr/bin/sed -e "s/system default destination: //" 2>/dev/null`
    fi

    if [ $stdoutisatty -eq 1 ] ; then
      # If stdout is not re-directed

      if [ -z "$defaultprinter" ] ; then
        uselp=0
      else
        printer=$defaultprinter
      fi
    else
      # stdout is redirected - assume it is for further processing of
      # postscript output.

      uselp=0
    fi
  fi
fi

if [ $uselp -eq 1 ] ; then
  case $(/usr/bin/basename $lp) in
   lp)
     lpopts="$lpopts -d$printer"
     ;;
   lpr)
     lpopts="$lpopts -P$printer"
     ;;
   *)
     /usr/bin/printf "`/usr/bin/gettext TEXT_DOMAIN 'vgrind: unknown print program %s'`\n" $lp >&2
     exit 1
     ;;
  esac
fi

# Implementation note:  In the following, we use "eval" to execute the
# command in order to preserve spaces which may appear in the -h option
# argument (and possibly in file names).

if [ $filter -eq 1 ] ; then
  eval "$vfontedpr $args | /usr/bin/cat $macros -"
else
  cmd="$vfontedpr $args"

  if [ -r index ] ; then
    # Removes any entries from the index that come from the files we are
    # processing.

    echo > nindex

    for i in "$files" ; do
      echo "? $i ?d" | /usr/bin/sed -e "s:/:\\\/:g" -e "s:?:/:g" >> nindex
    done

    /usr/bin/sed -f nindex index > xindex

    # Now process the input.
    # (! [$filter -eq 1])

    trap "rm -f xindex nindex; exit 1" INT QUIT

    cmd="$cmd | $troff -rx1 $troffopts -i $macros - 2>> xindex"

    if [ $usedpost -eq 1 ] ; then
      cmd="$cmd | $dpost $dpostopts"
    fi

    if [ $uselp -eq 1 ] ; then
      cmd="$cmd | $lp $lpopts"
    fi

    eval $cmd
    trap - INT QUIT
    /usr/bin/sort -dfu +0 -2 xindex > index
    /usr/bin/rm nindex xindex
  else
    # (! [ -r index ])

    cmd="$cmd | $troff -i $troffopts $macros -"

    if [ $usedpost -eq 1 ] ; then
      cmd="$cmd | $dpost $dpostopts"
    fi

    if [ $uselp -eq 1 ] ; then
        cmd="$cmd | $lp $lpopts"
    fi

    eval $cmd
  fi
fi