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