[comp.lang.postscript] a printcap entry for DRAFT output

cplai@daisy.UUCP (Chung-Pang Lai) (12/20/88)

I based on Spencer Thomas's psdraft and rewrote it into a filter that
is suitable for printcap use.  I also modified Ken Yap's changes so
that \ ( and ) are handled correctly.  I designed this to be used by
the output filter of lpd directly, hence I took out the -s option.
You can put back the -s option so that the string "DRAFT" is variable.

This filter can be used on command line as well as indirectly via lpr.
IF you don't have root access, you still can use it on the command line.
At the end of this, another filter "cover" is used to produce a cover
page.  If your environment don't need such cover page simply remove
the invocation at the last line of the script "draft".

I've created a printcap entry called psd as follow:

psd|psdraft|PostScript printer for drafts:\
	:sh:\
	:lp=/dev/pp0:\
	:sd=/usr/spool/pp0d:\
	:if=/etc/postscript/draft:\
	:lf=/usr/adm/pp0-errs:

The :lp: :sd: :lf: values are site-dependent, set it up to suit yourself.
The :if=: field specifies the use of this filter, change the path
name to fit your local setup.  The last line of the script "draft"
also refer to /etc/postscript directory, change it also for your
local setup.

Usage:
    any PS program | draft | lpr -Pps

If the printcap is set up, you can output via this filter by:
	lpr -Ppsd file.ps ....


Note: two filter script files follows:
=== cut ==== /etc/postscript/draft ==== cut ===
#! /bin/sh
#
# Put the word "DRAFT" (or a specified word) on each page of a postscript
# document.
#
# Usage:
# 	cat file ... | draft | lpr -Pps
#
# Author: Spencer W. Thomas
#	  Computer Science Dept.
#	  University of Utah
#	  (Now spencer@crim.eecs.umich.edu)
# modified by cplai for printcap use, added cover page

#
# Insert header after first line that does not begin %% or %!
#

trap "rm -f /tmp/psd$$.*" 0 1 2 15

# edit string for different printer names 
printerstr=psd

filterstr=`basename $0`
draftstring=DRAFT
loginstr=UNKNOWN
hoststr=UNKNOWN
# process input parameter from lpr's "if" filter
while [ "x$1" != "x" ] ; 
do
	# need many \ to escape several layers of shells, sed and postscript
	if [ "x$1" = "x-n" ] ; then
		loginstr=`echo $2 | sed \
		-e 's/\\\\/\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\/g' \
		-e 's/[()]/\\\\\\\\\\\\\\\\&/g' \
		-e 's:/:\\\\/:g'`
		shift
	elif [ "x$1" = "x-h" ] ; then
		hoststr=`echo $2 | sed \
		-e 's/\\\\/\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\/g' \
		-e 's/[()]/\\\\\\\\\\\\\\\\&/g' \
		-e 's:/:\\\\/:g'`
		shift
	fi
	shift
done

# Create sed script file
sed -e "s/(DRAFT)/($draftstring)/" <<'EOF' >/tmp/psd$$.sed
1,/^[^%]/{ 
s/^%/%/
s/^$//
t skip
i\
% Prelude to show a draft string on every page.\
(DRAFT)\
/DRAFTDICT 10 dict def\
DRAFTDICT begin\
/DRAFTSTRING exch def\
/bd /Helvetica-Bold findfont def\
/od bd maxlength 1 add dict def\
bd {exch dup /FID ne {exch od 3 1 roll put} {pop pop} ifelse} forall\
od /FontName /Outline0 put od /PaintType 2 put od /StrokeWidth 0 put\
/Outline0 od definefont pop\
/DRAFT { gsave\
	 initmatrix\
	/Outline0 findfont setfont\
	DRAFTSTRING dup stringwidth pop 8.875 exch div dup 72 mul dup scale\
	52.3 rotate 2.5 exch div -.35 translate\
	0 0 moveto show\
	grestore } def\
/oldshow /showpage load def\
/oldcopy /copypage load def\
end\
/showpage { DRAFTDICT begin DRAFT oldshow end } def\
/copypage { DRAFTDICT begin DRAFT oldcopy end } def\
% End of draft prelude
: skip
}
EOF

sed -f /tmp/psd$$.sed | /etc/postscript/cover -n $loginstr -h $hoststr -p $printerstr -f $filterstr 

=== cut ==== /etc/postscript/cover ==== cut ===
#! /bin/sh
#
# Add cover page to the job
#
# Author: C.P.Lai

#
# Insert header after first line that does not begin %% or %!
#

trap "rm -f /tmp/ps$$.*" 0 1 2 15

# edit string for different printer names
printerstr=ps

filterstr=`basename $0`
loginstr=UNKNOWN
hoststr=UNKNOWN
# process input parameter from lpr's "if" filter
while [ "x$1" != "x" ] ; 
do
	# need many \ to escape several layers of shells, sed and postscript
	if [ "x$1" = "x-n" ] ; then
		loginstr=`echo $2 | sed \
		-e 's/\\\\/\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\/g' \
		-e 's/[()]/\\\\\\\\\\\\\\\\&/g' \
		-e 's:/:\\\\/:g'`
		shift
	elif [ "x$1" = "x-h" ] ; then
		hoststr=`echo $2 | sed \
		-e 's/\\\\/\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\/g' \
		-e 's/[()]/\\\\\\\\\\\\\\\\&/g' \
		-e 's:/:\\\\/:g'`
		shift
	# process input parameter from other filter
	elif [ "x$1" = "x-p" ] ; then
		printerstr=$2
		shift
	elif [ "x$1" = "x-f" ] ; then
		filterstr=$2
		shift
	fi
	shift
done
datestr=`date`

# Create sed script file
sed 	-e "s/(JOBLOGIN)/(user: $loginstr)/" \
	-e "s/(JOBHOST)/(host: $hoststr)/" \
	-e "s/(JOBPRINTER)/(printer name: $printerstr)/" \
	-e "s/(JOBFILTER)/(output filter: $filterstr)/" \
	-e "s/(JOBDATE)/(date: $datestr)/" <<'EOF' >/tmp/ps$$.sed
1,/^[^%]/{ 
s/^%/%/
s/^$//
t skip
i\
% Prelude to print a cover page \
% Written by C.P. Lai\
% CAUTION CAUTION CAUTION\
% This does not conform to Postscript standard, executable code in Prolog\
% Eject a blank color paper as separator\
% Draw a cover page\
(JOBLOGIN) (JOBHOST) (JOBDATE) (JOBPRINTER) (JOBFILTER)\
/COVERDICT 10 dict def\
COVERDICT begin\
/FILTERSTR exch def\
/PRINTERSTR exch def\
/DATESTR exch def\
/HOSTSTR exch def\
/LOGINSTR exch def\
% draw a daisy logo, substitute your company logo here\
gsave\
/wedge {newpath 0 0 moveto 0 1 translate\
	halfangle neg rotate halfangle sin 0 translate\
	0 0 halfangle sin 180 0 arcn closepath } bind def\
/angle 360 7 div def /halfangle angle 2 div def\
306 468 translate 0 0 moveto 144 dup scale 0.02 setlinewidth\
1 1 7 {	pop gsave wedge 0 setgray stroke grestore angle rotate} for\
newpath 0 0 .35 0 360 arc 1 setgray gsave fill grestore 0 setgray stroke\
grestore\
% end logo\
/Helvetica-Bold findfont setfont\
gsave\
72 72 moveto 24 24 scale PRINTERSTR show\
grestore \
gsave\
306 72 moveto 24 24 scale FILTERSTR show\
grestore \
gsave\
72 99 moveto 24 24 scale DATESTR show\
grestore \
gsave\
.8 setgray 72 144 moveto \
HOSTSTR  dup stringwidth pop 6.5 exch div 72 mul dup scale show\
grestore \
gsave \
.9 setgray 72 360 moveto \
LOGINSTR dup stringwidth pop 6.5 exch div 72 mul dup scale show\
grestore\
showpage\
end %COVERDICT\
% End of cover page prelude
: skip
}
EOF

sed -f /tmp/ps$$.sed 
-- 
.signature under construction ...

{cbosgd,fortune,hplabs,seismo!ihnp4,ucbvax!hpda}!nsc!daisy!cplai    C.P. Lai
Daisy Systems Corp, 700B Middlefield Road, Mtn View CA 94039.  (415)960-6961