xref: /illumos-gate/usr/src/cmd/print/scripts/conv_lpd (revision 7c478bd9)
1*7c478bd9Sstevel@tonic-gate#!/bin/sh
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 (c) 1994, 1995, 1996 by Sun Microsystems, Inc.
25*7c478bd9Sstevel@tonic-gate# All Rights Reserved
26*7c478bd9Sstevel@tonic-gate#
27*7c478bd9Sstevel@tonic-gate# ident	"%Z%%M%	%I%	%E% SMI"
28*7c478bd9Sstevel@tonic-gate#
29*7c478bd9Sstevel@tonic-gate#  Printcap <-> Printers.conf conversion utility...
30*7c478bd9Sstevel@tonic-gate#
31*7c478bd9Sstevel@tonic-gate#	Usage: conv_lpd [ -c (printers|printcap) ] [ -n ] (file)
32*7c478bd9Sstevel@tonic-gate#
33*7c478bd9Sstevel@tonic-gate
34*7c478bd9Sstevel@tonic-gateTEXTDOMAIN="SUNW_OST_OSCMD"
35*7c478bd9Sstevel@tonic-gateexport TEXTDOMAIN
36*7c478bd9Sstevel@tonic-gate
37*7c478bd9Sstevel@tonic-gateUSAGE=`gettext "Usage: conv_lpd [ -c (printers|printcap) ] [ -n ] (file)\n"`
38*7c478bd9Sstevel@tonic-gate
39*7c478bd9Sstevel@tonic-gatePATH=/usr/bin:/bin:/usr/sbin export PATH
40*7c478bd9Sstevel@tonic-gateconversion="printers"
41*7c478bd9Sstevel@tonic-gatenamelist=0
42*7c478bd9Sstevel@tonic-gate
43*7c478bd9Sstevel@tonic-gateumask 022
44*7c478bd9Sstevel@tonic-gate
45*7c478bd9Sstevel@tonic-gatefor i in $*
46*7c478bd9Sstevel@tonic-gatedo
47*7c478bd9Sstevel@tonic-gate	case $1 in
48*7c478bd9Sstevel@tonic-gate		-c*)
49*7c478bd9Sstevel@tonic-gate			conversion=$2;
50*7c478bd9Sstevel@tonic-gate			shift ; shift ;
51*7c478bd9Sstevel@tonic-gate		;;
52*7c478bd9Sstevel@tonic-gate		-n*)
53*7c478bd9Sstevel@tonic-gate			namelist=1;
54*7c478bd9Sstevel@tonic-gate			shift ;
55*7c478bd9Sstevel@tonic-gate		;;
56*7c478bd9Sstevel@tonic-gate		*)
57*7c478bd9Sstevel@tonic-gate			break ;
58*7c478bd9Sstevel@tonic-gate		;;
59*7c478bd9Sstevel@tonic-gate	esac
60*7c478bd9Sstevel@tonic-gatedone
61*7c478bd9Sstevel@tonic-gate
62*7c478bd9Sstevel@tonic-gateTMPF1=/tmp/tinput1.$$
63*7c478bd9Sstevel@tonic-gateTMPF2=/tmp/tinput2.$$
64*7c478bd9Sstevel@tonic-gateFILE=/tmp/input.$$
65*7c478bd9Sstevel@tonic-gate
66*7c478bd9Sstevel@tonic-gate# Any remaining arg is the "file" specification.  It is a required arg.
67*7c478bd9Sstevel@tonic-gateif [ -z "$1" ]; then
68*7c478bd9Sstevel@tonic-gate	echo $USAGE
69*7c478bd9Sstevel@tonic-gate	exit 1
70*7c478bd9Sstevel@tonic-gateelse
71*7c478bd9Sstevel@tonic-gate	cp $1 $TMPF1
72*7c478bd9Sstevel@tonic-gatefi
73*7c478bd9Sstevel@tonic-gate
74*7c478bd9Sstevel@tonic-gateecho  >>$TMPF1
75*7c478bd9Sstevel@tonic-gateecho "_done" >>$TMPF1
76*7c478bd9Sstevel@tonic-gate
77*7c478bd9Sstevel@tonic-gate#
78*7c478bd9Sstevel@tonic-gate# First, strip all continuation characters, leaving one, single line
79*7c478bd9Sstevel@tonic-gate# for each printer entry.
80*7c478bd9Sstevel@tonic-gate#
81*7c478bd9Sstevel@tonic-gate	CONV_FIX=/usr/lib/print/conv_fix
82*7c478bd9Sstevel@tonic-gate
83*7c478bd9Sstevel@tonic-gate	if [ -f $CONV_FIX ]; then
84*7c478bd9Sstevel@tonic-gate		$CONV_FIX -f $TMPF1 -o $TMPF2
85*7c478bd9Sstevel@tonic-gate		if [ $? != 0 ]; then
86*7c478bd9Sstevel@tonic-gate			echo "$0:"
87*7c478bd9Sstevel@tonic-gate			gettext "Fatal Error: $CONV_FIX failed.\n"
88*7c478bd9Sstevel@tonic-gate			gettext "Please contact your Sun support representative.\n"
89*7c478bd9Sstevel@tonic-gate			exit 1
90*7c478bd9Sstevel@tonic-gate		fi
91*7c478bd9Sstevel@tonic-gate	else
92*7c478bd9Sstevel@tonic-gate		gettext "$0: Fatal: Cannot locate $CONV_FIX binary.\n"
93*7c478bd9Sstevel@tonic-gate		gettext "Please contact your Sun support representative.\n"
94*7c478bd9Sstevel@tonic-gate		exit 1
95*7c478bd9Sstevel@tonic-gate	fi
96*7c478bd9Sstevel@tonic-gate
97*7c478bd9Sstevel@tonic-gate#
98*7c478bd9Sstevel@tonic-gate# Continuation characters are now stripped.  Continue processing.
99*7c478bd9Sstevel@tonic-gate#
100*7c478bd9Sstevel@tonic-gate	/bin/sed -e "s/:[ 	]*:/:/g" $TMPF2 > $FILE
101*7c478bd9Sstevel@tonic-gate
102*7c478bd9Sstevel@tonic-gate#
103*7c478bd9Sstevel@tonic-gate# Empty colons ":[ <TAB>]*:" are now stripped.  Continue processing.
104*7c478bd9Sstevel@tonic-gate#
105*7c478bd9Sstevel@tonic-gate
106*7c478bd9Sstevel@tonic-gatenawk '
107*7c478bd9Sstevel@tonic-gateBEGIN {
108*7c478bd9Sstevel@tonic-gate	"uname -n" | getline ;
109*7c478bd9Sstevel@tonic-gate	host = $0 ;
110*7c478bd9Sstevel@tonic-gate	found = 0 ;
111*7c478bd9Sstevel@tonic-gate	local_pr = 0;
112*7c478bd9Sstevel@tonic-gate}
113*7c478bd9Sstevel@tonic-gate
114*7c478bd9Sstevel@tonic-gate{
115*7c478bd9Sstevel@tonic-gate        FS=":"; OFS=":" ;
116*7c478bd9Sstevel@tonic-gate        if ($0 !~ /^#/)
117*7c478bd9Sstevel@tonic-gate         {
118*7c478bd9Sstevel@tonic-gate                        if ($0 ~ /^[_a-zA-Z0-9_]/) {     # New entry
119*7c478bd9Sstevel@tonic-gate				if ( found != 0 ) {
120*7c478bd9Sstevel@tonic-gate				    if ( "'$namelist'" == 1 )
121*7c478bd9Sstevel@tonic-gate					printer = names ;
122*7c478bd9Sstevel@tonic-gate				    else
123*7c478bd9Sstevel@tonic-gate					printer = name[1] ;
124*7c478bd9Sstevel@tonic-gate
125*7c478bd9Sstevel@tonic-gate				    if ( "'$conversion'" == "printers" ) {
126*7c478bd9Sstevel@tonic-gate					printf "\n%s:", names ;
127*7c478bd9Sstevel@tonic-gate					for (key in values) {
128*7c478bd9Sstevel@tonic-gate						if ((key != "rp") &&
129*7c478bd9Sstevel@tonic-gate						    (key != "rm")) {
130*7c478bd9Sstevel@tonic-gate						printf "\\\n\t:%s=%s:",
131*7c478bd9Sstevel@tonic-gate							key, values[key] ;
132*7c478bd9Sstevel@tonic-gate						delete values[key];
133*7c478bd9Sstevel@tonic-gate						}
134*7c478bd9Sstevel@tonic-gate					}
135*7c478bd9Sstevel@tonic-gate					if (values["rm"] != "") {
136*7c478bd9Sstevel@tonic-gate						printf "\\\n\t:bsdaddr=%s,%s:", \
137*7c478bd9Sstevel@tonic-gate							values["rm"], \
138*7c478bd9Sstevel@tonic-gate							values["rp"] ;
139*7c478bd9Sstevel@tonic-gate						if (values["rm"] == host) local_pr++;
140*7c478bd9Sstevel@tonic-gate					} else {
141*7c478bd9Sstevel@tonic-gate						printf "\\\n\t:bsdaddr=%s,%s:", \
142*7c478bd9Sstevel@tonic-gate							host, printer ;
143*7c478bd9Sstevel@tonic-gate						local_pr++;
144*7c478bd9Sstevel@tonic-gate					}
145*7c478bd9Sstevel@tonic-gate					delete values["rp"];
146*7c478bd9Sstevel@tonic-gate					delete values["rm"];
147*7c478bd9Sstevel@tonic-gate				    } else {
148*7c478bd9Sstevel@tonic-gate					printf "\n%s:", names ;
149*7c478bd9Sstevel@tonic-gate					for (key in values) {
150*7c478bd9Sstevel@tonic-gate						if (key == "bsdaddr") {
151*7c478bd9Sstevel@tonic-gate							split(values[key],
152*7c478bd9Sstevel@tonic-gate								pair, ",");
153*7c478bd9Sstevel@tonic-gate							printf "\\\n\t:%s=%s:", \
154*7c478bd9Sstevel@tonic-gate								"rm", pair[1] ;
155*7c478bd9Sstevel@tonic-gate							if (pair[2] == "")
156*7c478bd9Sstevel@tonic-gate								pair[2] = printer;
157*7c478bd9Sstevel@tonic-gate							printf "\\\n\t:%s=%s:", \
158*7c478bd9Sstevel@tonic-gate								"rp", pair[2] ;
159*7c478bd9Sstevel@tonic-gate
160*7c478bd9Sstevel@tonic-gate						} else if ((key == "br") || \
161*7c478bd9Sstevel@tonic-gate							   (key == "fc") || \
162*7c478bd9Sstevel@tonic-gate							   (key == "fs") || \
163*7c478bd9Sstevel@tonic-gate							   (key == "mc") || \
164*7c478bd9Sstevel@tonic-gate							   (key == "mx") || \
165*7c478bd9Sstevel@tonic-gate							   (key == "pc") || \
166*7c478bd9Sstevel@tonic-gate							   (key == "pl") || \
167*7c478bd9Sstevel@tonic-gate							   (key == "pw") || \
168*7c478bd9Sstevel@tonic-gate							   (key == "px") || \
169*7c478bd9Sstevel@tonic-gate							   (key == "py") || \
170*7c478bd9Sstevel@tonic-gate							   (key == "xc") || \
171*7c478bd9Sstevel@tonic-gate							   (key == "xs"))
172*7c478bd9Sstevel@tonic-gate						   	printf "\\\n\t:%s#%s:", \
173*7c478bd9Sstevel@tonic-gate								key, \
174*7c478bd9Sstevel@tonic-gate								values[key] ;
175*7c478bd9Sstevel@tonic-gate						else if (values[key] == "true")
176*7c478bd9Sstevel@tonic-gate							printf "\\\n\t:%s:", \
177*7c478bd9Sstevel@tonic-gate								key ;
178*7c478bd9Sstevel@tonic-gate						else if (values[key] != "false")
179*7c478bd9Sstevel@tonic-gate						   	printf "\\\n\t:%s=%s:", \
180*7c478bd9Sstevel@tonic-gate								key, \
181*7c478bd9Sstevel@tonic-gate								values[key] ;
182*7c478bd9Sstevel@tonic-gate						delete values[key];
183*7c478bd9Sstevel@tonic-gate					}
184*7c478bd9Sstevel@tonic-gate				    }
185*7c478bd9Sstevel@tonic-gate				}
186*7c478bd9Sstevel@tonic-gate				split( $1, name, "|");
187*7c478bd9Sstevel@tonic-gate				names = $1 ;
188*7c478bd9Sstevel@tonic-gate				found++;
189*7c478bd9Sstevel@tonic-gate			}
190*7c478bd9Sstevel@tonic-gate
191*7c478bd9Sstevel@tonic-gate			for ( i = 1 ; i <= NF ; i++ ) {
192*7c478bd9Sstevel@tonic-gate				if (($i == names) || ($i == "") || \
193*7c478bd9Sstevel@tonic-gate			  	    ($i == "\t"))
194*7c478bd9Sstevel@tonic-gate					continue ;
195*7c478bd9Sstevel@tonic-gate				if ((split( $i, pair, "=" ) != 2) && \
196*7c478bd9Sstevel@tonic-gate				    (split( $i, pair, "\#") != 2))
197*7c478bd9Sstevel@tonic-gate					pair[2] = "true";
198*7c478bd9Sstevel@tonic-gate
199*7c478bd9Sstevel@tonic-gate				if (pair[1] != "" && pair[1] != "	")
200*7c478bd9Sstevel@tonic-gate					values[pair[1]] = pair[2] ;
201*7c478bd9Sstevel@tonic-gate			}
202*7c478bd9Sstevel@tonic-gate        }
203*7c478bd9Sstevel@tonic-gate}
204*7c478bd9Sstevel@tonic-gateEND {
205*7c478bd9Sstevel@tonic-gate	 printf "\n" ;
206*7c478bd9Sstevel@tonic-gate	 if (local_pr != 0)
207*7c478bd9Sstevel@tonic-gate		 printf  "One or more printers are local, you may want to run lpadmin to configure LP server side operation\n" | "cat 1>&2" ;
208*7c478bd9Sstevel@tonic-gate
209*7c478bd9Sstevel@tonic-gate}' ${FILE}
210*7c478bd9Sstevel@tonic-gate
211*7c478bd9Sstevel@tonic-gate/bin/rm -f ${TMPF1} ${TMP2} ${FILE}
212*7c478bd9Sstevel@tonic-gate
213*7c478bd9Sstevel@tonic-gateexit 0
214