xref: /plan9/sys/src/cmd/gs/lib/unix-lpr.sh (revision 593dc095aefb2a85c828727bbfa9da139a49bdf4)
1#!/bin/sh
2# $Id: unix-lpr.sh,v 1.5 2004/08/04 00:55:46 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
19# This definition is changed on install to match the
20# executable name set in the makefile
21GS_EXECUTABLE=gs
22
23PBMPLUSPATH=/usr/local/bin
24PSFILTERPATH=/usr/local/lib/ghostscript
25LOCALPATH=/usr/local/bin
26X11HOME=/usr/X11R6
27
28PATH=/bin:/usr/bin:/usr/ucb:/usr/etc
29PATH=${PATH}\:${LOCALPATH}\:${PBMPLUSPATH}\:${PSFILTERPATH}
30LD_LIBRARY_PATH=${X11HOME}/lib
31
32export PATH LD_LIBRARY_PATH acctfile host user
33
34user= host= acctfile=/dev/null
35
36#
37# Redirect stdout to stderr (for the logfile) and open a new channel
38# connected to stdout for the raw data. This enables us to keep the
39# raw data separate from programmed postscript output and error messages.
40#
41exec 3>&1 1>&2
42
43#
44# Get username and hostname from filter parameters
45#
46while [ $# != 0 ]
47do  case "$1" in
48  -n)	user=$2 ; shift ;;
49  -h)	host=$2 ; shift ;;
50  -*)	;;
51  *)	acctfile=$1 ;;
52  esac
53  shift
54done
55
56#
57# Get the filter, printer device and queue type (direct/indirect)
58#
59filter=`basename $0`
60device=`dirname $0`
61type=`dirname ${device}`
62device=`basename ${device}`
63fdevname=$device
64type=`basename ${type}`
65
66#
67# Find the bpp and number of colors, if specified
68#
69
70colorspec="`echo ${device} | sed 's/.*\.[0-9][0-9]*\.\([0-9][0-9]*\)$/\1/'`"
71if test "$colorspec" = "${device}"
72then
73    colorspec=""
74else
75    device=`basename ${device} .$colorspec`
76    colorspec="-dColors=$colorspec"
77fi
78
79bpp="`echo ${device} | sed 's/.*\.\([0-9][0-9]*\)$/\1/'`"
80if test "$bpp" = "${device}"
81then
82    bpp=1
83else
84    device=`basename ${device} .$bpp`
85fi
86
87#
88# Information for the logfile
89#
90lock=`dirname ${acctfile}`/lock
91cf=`sed -n '$p' ${lock}`
92job=`sed -n 's/^J//p' ${cf}`
93
94echo "gsbanner: ${host}:${user}  Job: ${job}  Date: `date`"
95echo "gsif: ${host}:${user} ${fdevname} start - `date`"
96
97#
98# Set the direct or indirect output destinations
99#
100#gspipe=/tmp/gspipe.$$
101#mknod ${gspipe} p
102
103case "${type}" in
104  direct)
105		gsoutput="cat 1>&3" ;;
106#		cat ${gspipe} 1>&3 & ;;
107  indirect)
108		gsoutput="lpr -P${device}.raw" ;;
109#		cat ${gspipe} | lpr -P${device}.raw & ;;
110esac
111
112(
113#
114# Any setup required may be done here (eg. setting gamma for colour printing)
115#
116#echo "{0.333 exp} dup dup currenttransfer setcolortransfer"
117
118#
119# The input data is filtered here, before being passed on to Ghostscript
120#
121case "${filter}" in
122  gsif)	  cat ;;
123  gsnf)	  psdit ;;
124  gstf)	  pscat ;;
125  gsgf)	  psplot ;;
126  gsvf)	  rasttopnm | pnmtops ;;
127  gsdf)	  dvi2ps -sqlw ;;
128  gscf|gsrf) echo "${filter}: filter not available" 1>&2 ; exit 0 ;;
129esac
130
131#
132# This is the postlude which does the accounting
133#
134echo "\
135(acctfile) getenv
136  { currentdevice /PageCount gsgetdeviceprop dup cvi 0 gt
137    { exch (a) file /acctfile exch def
138      /string 20 string def
139      string cvs dup length dup
140      4 lt
141        { 4 exch sub
142          { acctfile ( ) writestring } repeat
143        } { pop } ifelse
144      acctfile exch writestring
145      acctfile (.00 ) writestring
146      acctfile (host) getenv
147        { string cvs } { (NOHOST) } ifelse writestring
148      acctfile (:) writestring
149      acctfile (user) getenv
150        { string cvs } { (NOUSER) } ifelse writestring
151      acctfile (\n) writestring
152      acctfile closefile
153    } { pop } ifelse
154  } if
155quit"
156) | $GS_EXECUTABLE -q -dNOPAUSE -sDEVICE=${device} \
157		-dBitsPerPixel=${bpp} $colorspec \
158		-sOutputFile=\|"${gsoutput}" -
159#		-sOutputFile=${gspipe} -
160
161rm -f ${gspipe}
162#
163# End the logfile entry
164#
165echo "gsif: end - `date`"
166
167