xref: /titanic_50/usr/src/cmd/lp/model/uri (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1*7c478bd9Sstevel@tonic-gate#!/bin/ksh
2*7c478bd9Sstevel@tonic-gate#
3*7c478bd9Sstevel@tonic-gate# CDDL HEADER START
4*7c478bd9Sstevel@tonic-gate#
5*7c478bd9Sstevel@tonic-gate# The contents of this file are subject to the terms of the
6*7c478bd9Sstevel@tonic-gate# Common Development and Distribution License, Version 1.0 only
7*7c478bd9Sstevel@tonic-gate# (the "License").  You may not use this file except in compliance
8*7c478bd9Sstevel@tonic-gate# with the License.
9*7c478bd9Sstevel@tonic-gate#
10*7c478bd9Sstevel@tonic-gate# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
11*7c478bd9Sstevel@tonic-gate# or http://www.opensolaris.org/os/licensing.
12*7c478bd9Sstevel@tonic-gate# See the License for the specific language governing permissions
13*7c478bd9Sstevel@tonic-gate# and limitations under the License.
14*7c478bd9Sstevel@tonic-gate#
15*7c478bd9Sstevel@tonic-gate# When distributing Covered Code, include this CDDL HEADER in each
16*7c478bd9Sstevel@tonic-gate# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
17*7c478bd9Sstevel@tonic-gate# If applicable, add the following below this CDDL HEADER, with the
18*7c478bd9Sstevel@tonic-gate# fields enclosed by brackets "[]" replaced with your own identifying
19*7c478bd9Sstevel@tonic-gate# information: Portions Copyright [yyyy] [name of copyright owner]
20*7c478bd9Sstevel@tonic-gate#
21*7c478bd9Sstevel@tonic-gate# CDDL HEADER END
22*7c478bd9Sstevel@tonic-gate#
23*7c478bd9Sstevel@tonic-gate#
24*7c478bd9Sstevel@tonic-gate# Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
25*7c478bd9Sstevel@tonic-gate# Use is subject to license terms.
26*7c478bd9Sstevel@tonic-gate#
27*7c478bd9Sstevel@tonic-gate# ident	"%Z%%M%	%I%	%E% SMI"
28*7c478bd9Sstevel@tonic-gate#
29*7c478bd9Sstevel@tonic-gate#	printer interface script for printers with a URI instead of
30*7c478bd9Sstevel@tonic-gate#	device name.
31*7c478bd9Sstevel@tonic-gate#
32*7c478bd9Sstevel@tonic-gate# The existence of a "PPD" environment variable in the calling environment
33*7c478bd9Sstevel@tonic-gate# indicates that Foomatic is to be used for filtering all job data as it is
34*7c478bd9Sstevel@tonic-gate# streamed to the output device (printer).
35*7c478bd9Sstevel@tonic-gate#
36*7c478bd9Sstevel@tonic-gate# The contents of a "DEVICE_URI" environment variable in the calling
37*7c478bd9Sstevel@tonic-gate# environment indicates the method and endpoint used in communicating with
38*7c478bd9Sstevel@tonic-gate# the output device (printer).  If no DEVICE_URI is present or the value
39*7c478bd9Sstevel@tonic-gate# contains a missing or unknown scheme, the URI scheme is assumed to be
40*7c478bd9Sstevel@tonic-gate# "file" and output streaming will be handled accordingly.
41*7c478bd9Sstevel@tonic-gate
42*7c478bd9Sstevel@tonic-gateexport PATH=/bin:/usr/bin:/usr/lib/lp/bin:/usr/sfw/bin
43*7c478bd9Sstevel@tonic-gate
44*7c478bd9Sstevel@tonic-gateTAG="uri-interface"
45*7c478bd9Sstevel@tonic-gate
46*7c478bd9Sstevel@tonic-gate
47*7c478bd9Sstevel@tonic-gate# Re-arrange fds for later use
48*7c478bd9Sstevel@tonic-gateexec 5>&2 2>/dev/null 3>&1
49*7c478bd9Sstevel@tonic-gate
50*7c478bd9Sstevel@tonic-gate#
51*7c478bd9Sstevel@tonic-gate# Exit Codes:
52*7c478bd9Sstevel@tonic-gate#
53*7c478bd9Sstevel@tonic-gateEXIT_OK=0
54*7c478bd9Sstevel@tonic-gateEXIT_FATAL=1
55*7c478bd9Sstevel@tonic-gateEXIT_TERM=128
56*7c478bd9Sstevel@tonic-gateEXIT_RETRY=129
57*7c478bd9Sstevel@tonic-gate
58*7c478bd9Sstevel@tonic-gatefail() {	# exit-code "message"
59*7c478bd9Sstevel@tonic-gate	echo ${2} >&5
60*7c478bd9Sstevel@tonic-gate	exit ${1}
61*7c478bd9Sstevel@tonic-gate}
62*7c478bd9Sstevel@tonic-gate
63*7c478bd9Sstevel@tonic-gate# signal handling
64*7c478bd9Sstevel@tonic-gate#   EXIT   0  - normal exit
65*7c478bd9Sstevel@tonic-gate#   HUP    1  - the output stream disconnected
66*7c478bd9Sstevel@tonic-gate#   INT    2  - the output stream interupted us
67*7c478bd9Sstevel@tonic-gate#   QUIT   3  - the output stream interupted us
68*7c478bd9Sstevel@tonic-gate#   TERM  15  - we have been cancelled or shutdown
69*7c478bd9Sstevel@tonic-gate
70*7c478bd9Sstevel@tonic-gatecatch_exit() {
71*7c478bd9Sstevel@tonic-gate	exit $exit_code
72*7c478bd9Sstevel@tonic-gate}
73*7c478bd9Sstevel@tonic-gate
74*7c478bd9Sstevel@tonic-gatecatch_disconnect() {
75*7c478bd9Sstevel@tonic-gate	fail ${EXIT_RETRY} "connection to the printer dropped; off-line?"
76*7c478bd9Sstevel@tonic-gate}
77*7c478bd9Sstevel@tonic-gate
78*7c478bd9Sstevel@tonic-gatecatch_interrupt() {
79*7c478bd9Sstevel@tonic-gate	fail ${EXIT_RETRY} "interrupt from the printer; baud-rate issues?"
80*7c478bd9Sstevel@tonic-gate}
81*7c478bd9Sstevel@tonic-gate
82*7c478bd9Sstevel@tonic-gatecatch_cancellation() {
83*7c478bd9Sstevel@tonic-gate	fail ${EXIT_RETRY} "job cancelled"
84*7c478bd9Sstevel@tonic-gate}
85*7c478bd9Sstevel@tonic-gate
86*7c478bd9Sstevel@tonic-gatetrap 'catch_disconnect()' HUP
87*7c478bd9Sstevel@tonic-gatetrap 'catch_interrupt()' INT QUIT
88*7c478bd9Sstevel@tonic-gatetrap 'catch_cancellation()' TERM
89*7c478bd9Sstevel@tonic-gate
90*7c478bd9Sstevel@tonic-gateparse_uri() {	# scheme://[[user[:password]@]host[:port]]/path
91*7c478bd9Sstevel@tonic-gate	URI_SCHEME=$(expr "$1" : "\(.*\)://.*")
92*7c478bd9Sstevel@tonic-gate}
93*7c478bd9Sstevel@tonic-gate
94*7c478bd9Sstevel@tonic-gateparse() {
95*7c478bd9Sstevel@tonic-gate	echo "$(expr \"$1\" : \"^[^=]*=\(.*\)\")"
96*7c478bd9Sstevel@tonic-gate}
97*7c478bd9Sstevel@tonic-gate
98*7c478bd9Sstevel@tonic-gate#
99*7c478bd9Sstevel@tonic-gate#	Generate an ASCII burst page and pass it to the printer
100*7c478bd9Sstevel@tonic-gate# This may be much faster than the PostScript(TM) burst page
101*7c478bd9Sstevel@tonic-gate#
102*7c478bd9Sstevel@tonic-gateascii_burst_page() {
103*7c478bd9Sstevel@tonic-gate	cat <<EOF
104*7c478bd9Sstevel@tonic-gate	${title}
105*7c478bd9Sstevel@tonic-gate	Request: ${request_id}
106*7c478bd9Sstevel@tonic-gate	User: ${user}
107*7c478bd9Sstevel@tonic-gate	Printer: ${printer}
108*7c478bd9Sstevel@tonic-gate	Time: $(date)
109*7c478bd9Sstevel@tonic-gate	Copies: ${copies}
110*7c478bd9Sstevel@tonic-gateEOF
111*7c478bd9Sstevel@tonic-gate	tput ff
112*7c478bd9Sstevel@tonic-gate}
113*7c478bd9Sstevel@tonic-gate
114*7c478bd9Sstevel@tonic-gate#
115*7c478bd9Sstevel@tonic-gate#	Generate a PostScript(TM) burst page (this assumes an 8.5x11 page size)
116*7c478bd9Sstevel@tonic-gate#
117*7c478bd9Sstevel@tonic-gatepostscript_burst_page() {
118*7c478bd9Sstevel@tonic-gate	cat <<-EOF
119*7c478bd9Sstevel@tonic-gate	%!ps
120*7c478bd9Sstevel@tonic-gate	/PrintLine { exch findfont exch scalefont setfont moveto show } def
121*7c478bd9Sstevel@tonic-gate	newpath 4 setlinewidth 1 setlinejoin
122*7c478bd9Sstevel@tonic-gate	15 760 moveto 595 760 lineto 595 585 lineto 15 585 lineto closepath
123*7c478bd9Sstevel@tonic-gate	gsave .75 setgray fill grestore
124*7c478bd9Sstevel@tonic-gate	0 setgray stroke
125*7c478bd9Sstevel@tonic-gate	(${user}) 30 730 /Times-Bold 24 PrintLine
126*7c478bd9Sstevel@tonic-gate	(${request_id}) 415 730 /Times-Bold 24 PrintLine
127*7c478bd9Sstevel@tonic-gate	(${printer}) 30 600 /Times-Bold 16 PrintLine
128*7c478bd9Sstevel@tonic-gate	($(date)) 350 600 /Times-Roman 16 PrintLine
129*7c478bd9Sstevel@tonic-gate	(${title}) 100 660 /Times-Bold 36 PrintLine
130*7c478bd9Sstevel@tonic-gate	(Copies: ${copies}) 30 25 /Times-Roman 16 PrintLine
131*7c478bd9Sstevel@tonic-gate	showpage
132*7c478bd9Sstevel@tonic-gate	EOF
133*7c478bd9Sstevel@tonic-gate}
134*7c478bd9Sstevel@tonic-gate
135*7c478bd9Sstevel@tonic-gatelogger -p lpr.debug -t ${TAG} "$0 $*"
136*7c478bd9Sstevel@tonic-gate
137*7c478bd9Sstevel@tonic-gate#
138*7c478bd9Sstevel@tonic-gate# Detemine if we were called correctly
139*7c478bd9Sstevel@tonic-gate#
140*7c478bd9Sstevel@tonic-gateif [[ $# -lt 5 ]] ; then
141*7c478bd9Sstevel@tonic-gate	fail ${EXIT_FATAL} "wrong number of arguments to interface script"
142*7c478bd9Sstevel@tonic-gatefi
143*7c478bd9Sstevel@tonic-gate
144*7c478bd9Sstevel@tonic-gate
145*7c478bd9Sstevel@tonic-gateprinter=$(basename $0)
146*7c478bd9Sstevel@tonic-gaterequest_id=$1
147*7c478bd9Sstevel@tonic-gateuser=$2
148*7c478bd9Sstevel@tonic-gatetitle=$3
149*7c478bd9Sstevel@tonic-gatecopies=$4
150*7c478bd9Sstevel@tonic-gateoptions=$5
151*7c478bd9Sstevel@tonic-gate
152*7c478bd9Sstevel@tonic-gateshift 5
153*7c478bd9Sstevel@tonic-gatefiles="$*"
154*7c478bd9Sstevel@tonic-gate
155*7c478bd9Sstevel@tonic-gateburst_page="postscript_burst_page"
156*7c478bd9Sstevel@tonic-gate
157*7c478bd9Sstevel@tonic-gatefor i in ${options}
158*7c478bd9Sstevel@tonic-gatedo
159*7c478bd9Sstevel@tonic-gate	case "${i}" in
160*7c478bd9Sstevel@tonic-gate
161*7c478bd9Sstevel@tonic-gate	nobanner )
162*7c478bd9Sstevel@tonic-gate		burst_page=""
163*7c478bd9Sstevel@tonic-gate		;;
164*7c478bd9Sstevel@tonic-gate
165*7c478bd9Sstevel@tonic-gate	nofilebreak )
166*7c478bd9Sstevel@tonic-gate		nofilebreak="yes"
167*7c478bd9Sstevel@tonic-gate		;;
168*7c478bd9Sstevel@tonic-gate
169*7c478bd9Sstevel@tonic-gate	burst-page-type=* )
170*7c478bd9Sstevel@tonic-gate		burst_page="$(parse ${i})_burst_page"
171*7c478bd9Sstevel@tonic-gate		;;
172*7c478bd9Sstevel@tonic-gate
173*7c478bd9Sstevel@tonic-gate	* )
174*7c478bd9Sstevel@tonic-gate		logger -p lpr.error -t ${TAG} \
175*7c478bd9Sstevel@tonic-gate			"unrecognized \"-o ${i}\" option, ignored" 1>&2
176*7c478bd9Sstevel@tonic-gate		;;
177*7c478bd9Sstevel@tonic-gate	esac
178*7c478bd9Sstevel@tonic-gatedone
179*7c478bd9Sstevel@tonic-gate
180*7c478bd9Sstevel@tonic-gate
181*7c478bd9Sstevel@tonic-gate#
182*7c478bd9Sstevel@tonic-gate# Procss the DEVICE_URI if we have one
183*7c478bd9Sstevel@tonic-gate#
184*7c478bd9Sstevel@tonic-gateif [[ -n "${DEVICE_URI}" ]] ; then
185*7c478bd9Sstevel@tonic-gate	parse_uri ${DEVICE_URI}		# split up the URI
186*7c478bd9Sstevel@tonic-gate
187*7c478bd9Sstevel@tonic-gate	URI_SCHEME=${URI_SCHEME:-file}	# if there is no scheme, assume "file"
188*7c478bd9Sstevel@tonic-gate
189*7c478bd9Sstevel@tonic-gate	case "${URI_SCHEME}" in
190*7c478bd9Sstevel@tonic-gate	file|usb|ecpp|serial|parallel)
191*7c478bd9Sstevel@tonic-gate		IO_HANDLER="lp.cat"
192*7c478bd9Sstevel@tonic-gate		IO_HANDLER_ARGS=""
193*7c478bd9Sstevel@tonic-gate		;;
194*7c478bd9Sstevel@tonic-gate	smb)
195*7c478bd9Sstevel@tonic-gate		IO_HANDLER="smbspool"
196*7c478bd9Sstevel@tonic-gate		IO_HANDLER_ARGS="${request_id} ${user} \"${title}\" 1
197*7c478bd9Sstevel@tonic-gate				 \"${options}\""
198*7c478bd9Sstevel@tonic-gate		;;
199*7c478bd9Sstevel@tonic-gate	*)
200*7c478bd9Sstevel@tonic-gate		IO_HANDLER=${URI_SCHEME}
201*7c478bd9Sstevel@tonic-gate		IO_HANDLER_ARGS=""
202*7c478bd9Sstevel@tonic-gate		;;
203*7c478bd9Sstevel@tonic-gate	esac
204*7c478bd9Sstevel@tonic-gatefi
205*7c478bd9Sstevel@tonic-gateIO_HANDLER=${IO_HANDLER:-"lp.cat"} # if IO_HANDLER is still unset, use lp.cat
206*7c478bd9Sstevel@tonic-gate
207*7c478bd9Sstevel@tonic-gate# determine if the IO handler is available for us to use when communicating with
208*7c478bd9Sstevel@tonic-gate# the output device (printer.)
209*7c478bd9Sstevel@tonic-gatewhence ${IO_HANDLER} >/dev/null
210*7c478bd9Sstevel@tonic-gateif [[ $? -ne 0 ]] ; then
211*7c478bd9Sstevel@tonic-gate	fail ${ERR_FATAL} \
212*7c478bd9Sstevel@tonic-gate		"Interface script unable to locate IO handler: ${IO_HANDLER}"
213*7c478bd9Sstevel@tonic-gatefi
214*7c478bd9Sstevel@tonic-gate
215*7c478bd9Sstevel@tonic-gate#  There is a PPD file specified, so use foomatic
216*7c478bd9Sstevel@tonic-gateif [[ -n "${PPD}" ]] ; then
217*7c478bd9Sstevel@tonic-gate	FILTER_CHAIN="| foomatic-rip"
218*7c478bd9Sstevel@tonic-gatefi
219*7c478bd9Sstevel@tonic-gate
220*7c478bd9Sstevel@tonic-gate#
221*7c478bd9Sstevel@tonic-gate# Start processing the job here
222*7c478bd9Sstevel@tonic-gate#
223*7c478bd9Sstevel@tonic-gateset | logger -p lpr.debug -t "${TAG}"
224*7c478bd9Sstevel@tonic-gate
225*7c478bd9Sstevel@tonic-gate(
226*7c478bd9Sstevel@tonic-gate	if [[ -n "${burst_page}" ]] ; then
227*7c478bd9Sstevel@tonic-gate		eval "${burst_page} ${FILTER_CHAIN}"
228*7c478bd9Sstevel@tonic-gate	fi
229*7c478bd9Sstevel@tonic-gate	while [[ $copies -gt 0 ]] ; do
230*7c478bd9Sstevel@tonic-gate		for file in ${files} ; do
231*7c478bd9Sstevel@tonic-gate			if [[ -r "${file}" ]] ; then
232*7c478bd9Sstevel@tonic-gate				eval "cat ${file} ${FILTER_CHAIN}"
233*7c478bd9Sstevel@tonic-gate			fi
234*7c478bd9Sstevel@tonic-gate		done
235*7c478bd9Sstevel@tonic-gate		copies=$(( copies - 1 ))
236*7c478bd9Sstevel@tonic-gate	done
237*7c478bd9Sstevel@tonic-gate) | ${IO_HANDLER} ${IO_HANDLER_ARGS}
238*7c478bd9Sstevel@tonic-gate
239*7c478bd9Sstevel@tonic-gateexit ${EXIT_OK}
240