1#!/bin/sh
2#
3# Unix lpr filter. The default setup sends output directly to a pipe,
4# which requires the Ghostscript process to fork, and thus may cause
5# small systems to run out of memory/swap space. An alternative strategy,
6# based on a suggestion by Andy Fyfe (andy@cs.caltech.edu), uses a named
7# pipe for output, which avoids the fork and can thus save a lot of memory.
8#
9# Unfortunately this approach can cause problems when a print job is aborted,
10# as the abort can cause one of the processes to die, leaving the process
11# at the other end of the pipe hanging forever.
12#
13# Because of this, the named pipe method has not been made the default,
14# but it may be restored by commenting out the lines referring to
15# 'gsoutput' and uncommenting the lines referring to 'gspipe'.
16#
17
18# This definition is changed on install to match the
19# executable name set in the makefile
20GS_EXECUTABLE=gs
21
22PBMPLUSPATH=/usr/local/bin
23PSFILTERPATH=/usr/local/lib/ghostscript
24LOCALPATH=/usr/local/bin
25X11HOME=/usr/X11R6
26
27PATH=/bin:/usr/bin:/usr/ucb:/usr/etc
28PATH=${PATH}\:${LOCALPATH}\:${PBMPLUSPATH}\:${PSFILTERPATH}
29LD_LIBRARY_PATH=${X11HOME}/lib
30
31export PATH LD_LIBRARY_PATH acctfile host user
32
33user= host= acctfile=/dev/null
34
35#
36# Redirect stdout to stderr (for the logfile) and open a new channel
37# connected to stdout for the raw data. This enables us to keep the
38# raw data separate from programmed postscript output and error messages.
39#
40exec 3>&1 1>&2
41
42#
43# Get username and hostname from filter parameters
44#
45while [ $# != 0 ]
46do  case "$1" in
47  -n)	user=$2 ; shift ;;
48  -h)	host=$2 ; shift ;;
49  -*)	;;
50  *)	acctfile=$1 ;;
51  esac
52  shift
53done
54
55#
56# Get the filter, printer device and queue type (direct/indirect)
57#
58filter=`basename $0`
59device=`dirname $0`
60type=`dirname ${device}`
61device=`basename ${device}`
62fdevname=$device
63type=`basename ${type}`
64
65#
66# Find the bpp and number of colors, if specified
67#
68
69colorspec="`echo ${device} | sed 's/.*\.[0-9][0-9]*\.\([0-9][0-9]*\)$/\1/'`"
70if test "$colorspec" = "${device}"
71then
72    colorspec=""
73else
74    device=`basename ${device} .$colorspec`
75    colorspec="-dColors=$colorspec"
76fi
77
78bpp="`echo ${device} | sed 's/.*\.\([0-9][0-9]*\)$/\1/'`"
79if test "$bpp" = "${device}"
80then
81    bpp=1
82else
83    device=`basename ${device} .$bpp`
84fi
85
86#
87# Information for the logfile
88#
89lock=`dirname ${acctfile}`/lock
90cf=`sed -n '$p' ${lock}`
91job=`sed -n 's/^J//p' ${cf}`
92
93echo "gsbanner: ${host}:${user}  Job: ${job}  Date: `date`"
94echo "gsif: ${host}:${user} ${fdevname} start - `date`"
95
96#
97# Set the direct or indirect output destinations
98#
99#gspipe=/tmp/gspipe.$$
100#mknod ${gspipe} p
101
102case "${type}" in
103  direct)
104		gsoutput="cat 1>&3" ;;
105#		cat ${gspipe} 1>&3 & ;;
106  indirect)
107		gsoutput="lpr -P${device}.raw" ;;
108#		cat ${gspipe} | lpr -P${device}.raw & ;;
109esac
110
111(
112#
113# Any setup required may be done here (eg. setting gamma for colour printing)
114#
115#echo "{0.333 exp} dup dup currenttransfer setcolortransfer"
116
117#
118# The input data is filtered here, before being passed on to Ghostscript
119#
120case "${filter}" in
121  gsif)	  cat ;;
122  gsnf)	  psdit ;;
123  gstf)	  pscat ;;
124  gsgf)	  psplot ;;
125  gsvf)	  rasttopnm | pnmtops ;;
126  gsdf)	  dvi2ps -sqlw ;;
127  gscf|gsrf) echo "${filter}: filter not available" 1>&2 ; exit 0 ;;
128esac
129
130#
131# This is the postlude which does the accounting
132#
133echo "\
134(acctfile) getenv
135  { currentdevice /PageCount gsgetdeviceprop dup cvi 0 gt
136    { exch (a) file /acctfile exch def
137      /string 20 string def
138      string cvs dup length dup
139      4 lt
140        { 4 exch sub
141          { acctfile ( ) writestring } repeat
142        } { pop } ifelse
143      acctfile exch writestring
144      acctfile (.00 ) writestring
145      acctfile (host) getenv
146        { string cvs } { (NOHOST) } ifelse writestring
147      acctfile (:) writestring
148      acctfile (user) getenv
149        { string cvs } { (NOUSER) } ifelse writestring
150      acctfile (\n) writestring
151      acctfile closefile
152    } { pop } ifelse
153  } if
154quit"
155) | $GS_EXECUTABLE -q -P- -dSAFER -dNOPAUSE -sDEVICE=${device} \
156		-dBitsPerPixel=${bpp} $colorspec \
157		-sOutputFile=\|"${gsoutput}" -
158#		-sOutputFile=${gspipe} -
159
160rm -f ${gspipe}
161#
162# End the logfile entry
163#
164echo "gsif: end - `date`"
165
166