xref: /illumos-gate/usr/src/cmd/lp/model/uri (revision 53ac4dca)
17c478bd9Sstevel@tonic-gate#!/bin/ksh
27c478bd9Sstevel@tonic-gate#
37c478bd9Sstevel@tonic-gate# CDDL HEADER START
47c478bd9Sstevel@tonic-gate#
57c478bd9Sstevel@tonic-gate# The contents of this file are subject to the terms of the
6*53ac4dcaSjacobs# Common Development and Distribution License (the "License").
7*53ac4dcaSjacobs# You may not use this file except in compliance with the License.
87c478bd9Sstevel@tonic-gate#
97c478bd9Sstevel@tonic-gate# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate# or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate# See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate# and limitations under the License.
137c478bd9Sstevel@tonic-gate#
147c478bd9Sstevel@tonic-gate# When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate# If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate# fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate# information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate#
207c478bd9Sstevel@tonic-gate# CDDL HEADER END
217c478bd9Sstevel@tonic-gate#
227c478bd9Sstevel@tonic-gate#
23*53ac4dcaSjacobs# Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate# Use is subject to license terms.
257c478bd9Sstevel@tonic-gate#
267c478bd9Sstevel@tonic-gate# ident	"%Z%%M%	%I%	%E% SMI"
277c478bd9Sstevel@tonic-gate#
287c478bd9Sstevel@tonic-gate#	printer interface script for printers with a URI instead of
297c478bd9Sstevel@tonic-gate#	device name.
307c478bd9Sstevel@tonic-gate#
317c478bd9Sstevel@tonic-gate# The existence of a "PPD" environment variable in the calling environment
327c478bd9Sstevel@tonic-gate# indicates that Foomatic is to be used for filtering all job data as it is
337c478bd9Sstevel@tonic-gate# streamed to the output device (printer).
347c478bd9Sstevel@tonic-gate#
357c478bd9Sstevel@tonic-gate# The contents of a "DEVICE_URI" environment variable in the calling
367c478bd9Sstevel@tonic-gate# environment indicates the method and endpoint used in communicating with
377c478bd9Sstevel@tonic-gate# the output device (printer).  If no DEVICE_URI is present or the value
387c478bd9Sstevel@tonic-gate# contains a missing or unknown scheme, the URI scheme is assumed to be
397c478bd9Sstevel@tonic-gate# "file" and output streaming will be handled accordingly.
407c478bd9Sstevel@tonic-gate
417c478bd9Sstevel@tonic-gateexport PATH=/bin:/usr/bin:/usr/lib/lp/bin:/usr/sfw/bin
427c478bd9Sstevel@tonic-gate
437c478bd9Sstevel@tonic-gateTAG="uri-interface"
447c478bd9Sstevel@tonic-gate
457c478bd9Sstevel@tonic-gate
467c478bd9Sstevel@tonic-gate# Re-arrange fds for later use
477c478bd9Sstevel@tonic-gateexec 5>&2 2>/dev/null 3>&1
487c478bd9Sstevel@tonic-gate
497c478bd9Sstevel@tonic-gate#
507c478bd9Sstevel@tonic-gate# Exit Codes:
517c478bd9Sstevel@tonic-gate#
527c478bd9Sstevel@tonic-gateEXIT_OK=0
537c478bd9Sstevel@tonic-gateEXIT_FATAL=1
547c478bd9Sstevel@tonic-gateEXIT_TERM=128
557c478bd9Sstevel@tonic-gateEXIT_RETRY=129
567c478bd9Sstevel@tonic-gate
577c478bd9Sstevel@tonic-gatefail() {	# exit-code "message"
58*53ac4dcaSjacobs	logger -p lpr.error -t ${TAG} "${2}"
597c478bd9Sstevel@tonic-gate	echo ${2} >&5
607c478bd9Sstevel@tonic-gate	exit ${1}
617c478bd9Sstevel@tonic-gate}
627c478bd9Sstevel@tonic-gate
637c478bd9Sstevel@tonic-gate# signal handling
647c478bd9Sstevel@tonic-gate#   EXIT   0  - normal exit
657c478bd9Sstevel@tonic-gate#   HUP    1  - the output stream disconnected
667c478bd9Sstevel@tonic-gate#   INT    2  - the output stream interupted us
677c478bd9Sstevel@tonic-gate#   QUIT   3  - the output stream interupted us
687c478bd9Sstevel@tonic-gate#   TERM  15  - we have been cancelled or shutdown
697c478bd9Sstevel@tonic-gate
707c478bd9Sstevel@tonic-gatecatch_exit() {
717c478bd9Sstevel@tonic-gate	exit $exit_code
727c478bd9Sstevel@tonic-gate}
737c478bd9Sstevel@tonic-gate
747c478bd9Sstevel@tonic-gatecatch_disconnect() {
757c478bd9Sstevel@tonic-gate	fail ${EXIT_RETRY} "connection to the printer dropped; off-line?"
767c478bd9Sstevel@tonic-gate}
777c478bd9Sstevel@tonic-gate
787c478bd9Sstevel@tonic-gatecatch_interrupt() {
797c478bd9Sstevel@tonic-gate	fail ${EXIT_RETRY} "interrupt from the printer; baud-rate issues?"
807c478bd9Sstevel@tonic-gate}
817c478bd9Sstevel@tonic-gate
827c478bd9Sstevel@tonic-gatecatch_cancellation() {
837c478bd9Sstevel@tonic-gate	fail ${EXIT_RETRY} "job cancelled"
847c478bd9Sstevel@tonic-gate}
857c478bd9Sstevel@tonic-gate
867c478bd9Sstevel@tonic-gatetrap 'catch_disconnect()' HUP
877c478bd9Sstevel@tonic-gatetrap 'catch_interrupt()' INT QUIT
887c478bd9Sstevel@tonic-gatetrap 'catch_cancellation()' TERM
897c478bd9Sstevel@tonic-gate
907c478bd9Sstevel@tonic-gateparse_uri() {	# scheme://[[user[:password]@]host[:port]]/path
917c478bd9Sstevel@tonic-gate	URI_SCHEME=$(expr "$1" : "\(.*\)://.*")
927c478bd9Sstevel@tonic-gate}
937c478bd9Sstevel@tonic-gate
947c478bd9Sstevel@tonic-gateparse() {
957c478bd9Sstevel@tonic-gate	echo "$(expr \"$1\" : \"^[^=]*=\(.*\)\")"
967c478bd9Sstevel@tonic-gate}
977c478bd9Sstevel@tonic-gate
987c478bd9Sstevel@tonic-gate#
997c478bd9Sstevel@tonic-gate#	Generate an ASCII burst page and pass it to the printer
1007c478bd9Sstevel@tonic-gate# This may be much faster than the PostScript(TM) burst page
1017c478bd9Sstevel@tonic-gate#
1027c478bd9Sstevel@tonic-gateascii_burst_page() {
1037c478bd9Sstevel@tonic-gate	cat <<EOF
1047c478bd9Sstevel@tonic-gate	${title}
1057c478bd9Sstevel@tonic-gate	Request: ${request_id}
1067c478bd9Sstevel@tonic-gate	User: ${user}
1077c478bd9Sstevel@tonic-gate	Printer: ${printer}
1087c478bd9Sstevel@tonic-gate	Time: $(date)
1097c478bd9Sstevel@tonic-gate	Copies: ${copies}
1107c478bd9Sstevel@tonic-gateEOF
1117c478bd9Sstevel@tonic-gate	tput ff
1127c478bd9Sstevel@tonic-gate}
1137c478bd9Sstevel@tonic-gate
1147c478bd9Sstevel@tonic-gate#
1157c478bd9Sstevel@tonic-gate#	Generate a PostScript(TM) burst page (this assumes an 8.5x11 page size)
1167c478bd9Sstevel@tonic-gate#
1177c478bd9Sstevel@tonic-gatepostscript_burst_page() {
1187c478bd9Sstevel@tonic-gate	cat <<-EOF
1197c478bd9Sstevel@tonic-gate	%!ps
1207c478bd9Sstevel@tonic-gate	/PrintLine { exch findfont exch scalefont setfont moveto show } def
1217c478bd9Sstevel@tonic-gate	newpath 4 setlinewidth 1 setlinejoin
1227c478bd9Sstevel@tonic-gate	15 760 moveto 595 760 lineto 595 585 lineto 15 585 lineto closepath
1237c478bd9Sstevel@tonic-gate	gsave .75 setgray fill grestore
1247c478bd9Sstevel@tonic-gate	0 setgray stroke
1257c478bd9Sstevel@tonic-gate	(${user}) 30 730 /Times-Bold 24 PrintLine
1267c478bd9Sstevel@tonic-gate	(${request_id}) 415 730 /Times-Bold 24 PrintLine
1277c478bd9Sstevel@tonic-gate	(${printer}) 30 600 /Times-Bold 16 PrintLine
1287c478bd9Sstevel@tonic-gate	($(date)) 350 600 /Times-Roman 16 PrintLine
1297c478bd9Sstevel@tonic-gate	(${title}) 100 660 /Times-Bold 36 PrintLine
1307c478bd9Sstevel@tonic-gate	(Copies: ${copies}) 30 25 /Times-Roman 16 PrintLine
1317c478bd9Sstevel@tonic-gate	showpage
1327c478bd9Sstevel@tonic-gate	EOF
1337c478bd9Sstevel@tonic-gate}
1347c478bd9Sstevel@tonic-gate
1357c478bd9Sstevel@tonic-gatelogger -p lpr.debug -t ${TAG} "$0 $*"
1367c478bd9Sstevel@tonic-gate
1377c478bd9Sstevel@tonic-gate#
1387c478bd9Sstevel@tonic-gate# Detemine if we were called correctly
1397c478bd9Sstevel@tonic-gate#
1407c478bd9Sstevel@tonic-gateif [[ $# -lt 5 ]] ; then
1417c478bd9Sstevel@tonic-gate	fail ${EXIT_FATAL} "wrong number of arguments to interface script"
1427c478bd9Sstevel@tonic-gatefi
1437c478bd9Sstevel@tonic-gate
1447c478bd9Sstevel@tonic-gate
1457c478bd9Sstevel@tonic-gateprinter=$(basename $0)
1467c478bd9Sstevel@tonic-gaterequest_id=$1
1477c478bd9Sstevel@tonic-gateuser=$2
1487c478bd9Sstevel@tonic-gatetitle=$3
1497c478bd9Sstevel@tonic-gatecopies=$4
1507c478bd9Sstevel@tonic-gateoptions=$5
1517c478bd9Sstevel@tonic-gate
1527c478bd9Sstevel@tonic-gateshift 5
1537c478bd9Sstevel@tonic-gatefiles="$*"
1547c478bd9Sstevel@tonic-gate
1557c478bd9Sstevel@tonic-gateburst_page="postscript_burst_page"
1567c478bd9Sstevel@tonic-gate
1577c478bd9Sstevel@tonic-gatefor i in ${options}
1587c478bd9Sstevel@tonic-gatedo
1597c478bd9Sstevel@tonic-gate	case "${i}" in
1607c478bd9Sstevel@tonic-gate
1617c478bd9Sstevel@tonic-gate	nobanner )
1627c478bd9Sstevel@tonic-gate		burst_page=""
1637c478bd9Sstevel@tonic-gate		;;
1647c478bd9Sstevel@tonic-gate
1657c478bd9Sstevel@tonic-gate	nofilebreak )
1667c478bd9Sstevel@tonic-gate		nofilebreak="yes"
1677c478bd9Sstevel@tonic-gate		;;
1687c478bd9Sstevel@tonic-gate
1697c478bd9Sstevel@tonic-gate	burst-page-type=* )
1707c478bd9Sstevel@tonic-gate		burst_page="$(parse ${i})_burst_page"
1717c478bd9Sstevel@tonic-gate		;;
1727c478bd9Sstevel@tonic-gate
1737c478bd9Sstevel@tonic-gate	* )
1747c478bd9Sstevel@tonic-gate		logger -p lpr.error -t ${TAG} \
1757c478bd9Sstevel@tonic-gate			"unrecognized \"-o ${i}\" option, ignored" 1>&2
1767c478bd9Sstevel@tonic-gate		;;
1777c478bd9Sstevel@tonic-gate	esac
1787c478bd9Sstevel@tonic-gatedone
1797c478bd9Sstevel@tonic-gate
1807c478bd9Sstevel@tonic-gate
1817c478bd9Sstevel@tonic-gate#
1827c478bd9Sstevel@tonic-gate# Procss the DEVICE_URI if we have one
1837c478bd9Sstevel@tonic-gate#
1847c478bd9Sstevel@tonic-gateif [[ -n "${DEVICE_URI}" ]] ; then
1857c478bd9Sstevel@tonic-gate	parse_uri ${DEVICE_URI}		# split up the URI
1867c478bd9Sstevel@tonic-gate
1877c478bd9Sstevel@tonic-gate	URI_SCHEME=${URI_SCHEME:-file}	# if there is no scheme, assume "file"
1887c478bd9Sstevel@tonic-gate
1897c478bd9Sstevel@tonic-gate	case "${URI_SCHEME}" in
1907c478bd9Sstevel@tonic-gate	file|usb|ecpp|serial|parallel)
1917c478bd9Sstevel@tonic-gate		IO_HANDLER="lp.cat"
1927c478bd9Sstevel@tonic-gate		IO_HANDLER_ARGS=""
1937c478bd9Sstevel@tonic-gate		;;
1947c478bd9Sstevel@tonic-gate	smb)
1957c478bd9Sstevel@tonic-gate		IO_HANDLER="smbspool"
1967c478bd9Sstevel@tonic-gate		IO_HANDLER_ARGS="${request_id} ${user} \"${title}\" 1
1977c478bd9Sstevel@tonic-gate				 \"${options}\""
1987c478bd9Sstevel@tonic-gate		;;
199*53ac4dcaSjacobs	tcp|socket)
200*53ac4dcaSjacobs		URI_HOST=$(expr "${DEVICE_URI}" : ".*://\([^:/]*\)")
201*53ac4dcaSjacobs		URI_PORT=$(expr "${DEVICE_URI}" : ".*://.*:\([^/]*\)")
202*53ac4dcaSjacobs		if [[ -z "${URI_HOST}" ]] ; then
203*53ac4dcaSjacobs			fail ${EXIT_FATAL} "invalid device-uri: ${DEVICE_URI}, reset with lpadmin -v"
204*53ac4dcaSjacobs		fi
205*53ac4dcaSjacobs		URI_PORT=${URI_PORT:-"9100"}
206*53ac4dcaSjacobs		IO_HANDLER="telnet"
207*53ac4dcaSjacobs		IO_HANDLER_ARGS="-c -E ${URI_HOST} ${URI_PORT}"
208*53ac4dcaSjacobs		;;
209*53ac4dcaSjacobs	lpd|ipp)
210*53ac4dcaSjacobs		IO_HANDLER="lp"
211*53ac4dcaSjacobs		IO_HANDLER_ARGS="-s -d ${DEVICE_URI}"
212*53ac4dcaSjacobs		;;
2137c478bd9Sstevel@tonic-gate	*)
2147c478bd9Sstevel@tonic-gate		IO_HANDLER=${URI_SCHEME}
2157c478bd9Sstevel@tonic-gate		IO_HANDLER_ARGS=""
2167c478bd9Sstevel@tonic-gate		;;
2177c478bd9Sstevel@tonic-gate	esac
2187c478bd9Sstevel@tonic-gatefi
2197c478bd9Sstevel@tonic-gateIO_HANDLER=${IO_HANDLER:-"lp.cat"} # if IO_HANDLER is still unset, use lp.cat
2207c478bd9Sstevel@tonic-gate
2217c478bd9Sstevel@tonic-gate# determine if the IO handler is available for us to use when communicating with
2227c478bd9Sstevel@tonic-gate# the output device (printer.)
2237c478bd9Sstevel@tonic-gatewhence ${IO_HANDLER} >/dev/null
2247c478bd9Sstevel@tonic-gateif [[ $? -ne 0 ]] ; then
2257c478bd9Sstevel@tonic-gate	fail ${ERR_FATAL} \
2267c478bd9Sstevel@tonic-gate		"Interface script unable to locate IO handler: ${IO_HANDLER}"
2277c478bd9Sstevel@tonic-gatefi
2287c478bd9Sstevel@tonic-gate
2297c478bd9Sstevel@tonic-gate#  There is a PPD file specified, so use foomatic
2307c478bd9Sstevel@tonic-gateif [[ -n "${PPD}" ]] ; then
2317c478bd9Sstevel@tonic-gate	FILTER_CHAIN="| foomatic-rip"
2327c478bd9Sstevel@tonic-gatefi
2337c478bd9Sstevel@tonic-gate
2347c478bd9Sstevel@tonic-gate#
2357c478bd9Sstevel@tonic-gate# Start processing the job here
2367c478bd9Sstevel@tonic-gate#
2377c478bd9Sstevel@tonic-gateset | logger -p lpr.debug -t "${TAG}"
2387c478bd9Sstevel@tonic-gate
2397c478bd9Sstevel@tonic-gate(
2407c478bd9Sstevel@tonic-gate	if [[ -n "${burst_page}" ]] ; then
2417c478bd9Sstevel@tonic-gate		eval "${burst_page} ${FILTER_CHAIN}"
2427c478bd9Sstevel@tonic-gate	fi
2437c478bd9Sstevel@tonic-gate	while [[ $copies -gt 0 ]] ; do
2447c478bd9Sstevel@tonic-gate		for file in ${files} ; do
2457c478bd9Sstevel@tonic-gate			if [[ -r "${file}" ]] ; then
2467c478bd9Sstevel@tonic-gate				eval "cat ${file} ${FILTER_CHAIN}"
2477c478bd9Sstevel@tonic-gate			fi
2487c478bd9Sstevel@tonic-gate		done
2497c478bd9Sstevel@tonic-gate		copies=$(( copies - 1 ))
2507c478bd9Sstevel@tonic-gate	done
2517c478bd9Sstevel@tonic-gate) | ${IO_HANDLER} ${IO_HANDLER_ARGS}
2527c478bd9Sstevel@tonic-gate
2537c478bd9Sstevel@tonic-gateexit ${EXIT_OK}
254