xref: /titanic_44/usr/src/cmd/lp/model/lp.cat.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate  * Copyright 1989-2002 Sun Microsystems, Inc.  All rights reserved.
24*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
25*7c478bd9Sstevel@tonic-gate  */
26*7c478bd9Sstevel@tonic-gate 
27*7c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
28*7c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
29*7c478bd9Sstevel@tonic-gate 
30*7c478bd9Sstevel@tonic-gate 
31*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
32*7c478bd9Sstevel@tonic-gate 
33*7c478bd9Sstevel@tonic-gate #include "stdio.h"
34*7c478bd9Sstevel@tonic-gate #include "termio.h"
35*7c478bd9Sstevel@tonic-gate #include "sys/types.h"
36*7c478bd9Sstevel@tonic-gate #include "errno.h"
37*7c478bd9Sstevel@tonic-gate #include "signal.h"
38*7c478bd9Sstevel@tonic-gate #include "sys/times.h"
39*7c478bd9Sstevel@tonic-gate #include "string.h"
40*7c478bd9Sstevel@tonic-gate #include "limits.h"
41*7c478bd9Sstevel@tonic-gate #include <sys/prnio.h>
42*7c478bd9Sstevel@tonic-gate 
43*7c478bd9Sstevel@tonic-gate #include "lp.h"
44*7c478bd9Sstevel@tonic-gate 
45*7c478bd9Sstevel@tonic-gate #include <locale.h>
46*7c478bd9Sstevel@tonic-gate 
47*7c478bd9Sstevel@tonic-gate /**
48*7c478bd9Sstevel@tonic-gate  **	Begin Sun Additions for Parallel ports
49*7c478bd9Sstevel@tonic-gate  **/
50*7c478bd9Sstevel@tonic-gate 
51*7c478bd9Sstevel@tonic-gate extern char *_sys_errlist[];
52*7c478bd9Sstevel@tonic-gate #include <string.h>
53*7c478bd9Sstevel@tonic-gate #include <stdarg.h>
54*7c478bd9Sstevel@tonic-gate #include <signal.h>
55*7c478bd9Sstevel@tonic-gate #include <unistd.h>
56*7c478bd9Sstevel@tonic-gate #include <sys/types.h>
57*7c478bd9Sstevel@tonic-gate #include <sys/ioccom.h>
58*7c478bd9Sstevel@tonic-gate #include <sys/ioctl.h>
59*7c478bd9Sstevel@tonic-gate 
60*7c478bd9Sstevel@tonic-gate #include <sys/bpp_io.h>
61*7c478bd9Sstevel@tonic-gate #include <sys/ecppsys.h>
62*7c478bd9Sstevel@tonic-gate #include <stropts.h>
63*7c478bd9Sstevel@tonic-gate 
64*7c478bd9Sstevel@tonic-gate /*
65*7c478bd9Sstevel@tonic-gate  * the parameter structure for the parallel port
66*7c478bd9Sstevel@tonic-gate  */
67*7c478bd9Sstevel@tonic-gate struct ppc_params_t {
68*7c478bd9Sstevel@tonic-gate 	int		flags;		/* same as above */
69*7c478bd9Sstevel@tonic-gate 	int		state;		/* status of the printer interface */
70*7c478bd9Sstevel@tonic-gate 	int		strobe_w;	/* strobe width, in uS */
71*7c478bd9Sstevel@tonic-gate 	int		data_setup;	/* data setup time, in uS */
72*7c478bd9Sstevel@tonic-gate 	int		ack_timeout;	/* ACK timeout, in secs */
73*7c478bd9Sstevel@tonic-gate 	int		error_timeout;	/* PAPER OUT, etc... timeout, in secs */
74*7c478bd9Sstevel@tonic-gate 	int		busy_timeout;	/* BUSY timeout, in seconds */
75*7c478bd9Sstevel@tonic-gate };
76*7c478bd9Sstevel@tonic-gate 
77*7c478bd9Sstevel@tonic-gate 
78*7c478bd9Sstevel@tonic-gate 
79*7c478bd9Sstevel@tonic-gate static void printer_info(char *fmt, ...);
80*7c478bd9Sstevel@tonic-gate 
81*7c478bd9Sstevel@tonic-gate /*	These are the routines avaliable to others for use 	*/
82*7c478bd9Sstevel@tonic-gate int is_a_parallel_bpp(int);
83*7c478bd9Sstevel@tonic-gate int bpp_state(int);
84*7c478bd9Sstevel@tonic-gate int parallel_comm(int, int());
85*7c478bd9Sstevel@tonic-gate int get_ecpp_status(int fd);
86*7c478bd9Sstevel@tonic-gate int is_a_prnio(int);
87*7c478bd9Sstevel@tonic-gate int prnio_state(int);
88*7c478bd9Sstevel@tonic-gate 
89*7c478bd9Sstevel@tonic-gate #define PRINTER_ERROR_PAPER_OUT		1
90*7c478bd9Sstevel@tonic-gate #define PRINTER_ERROR_OFFLINE		2
91*7c478bd9Sstevel@tonic-gate #define PRINTER_ERROR_BUSY		3
92*7c478bd9Sstevel@tonic-gate #define PRINTER_ERROR_ERROR		4
93*7c478bd9Sstevel@tonic-gate #define PRINTER_ERROR_CABLE_POWER	5
94*7c478bd9Sstevel@tonic-gate #define PRINTER_ERROR_UNKNOWN		6
95*7c478bd9Sstevel@tonic-gate #define PRINTER_ERROR_TIMEOUT		7
96*7c478bd9Sstevel@tonic-gate #define	PRINTER_IO_ERROR		129
97*7c478bd9Sstevel@tonic-gate 
98*7c478bd9Sstevel@tonic-gate 
99*7c478bd9Sstevel@tonic-gate /****************************************************************************/
100*7c478bd9Sstevel@tonic-gate 
101*7c478bd9Sstevel@tonic-gate /**
102*7c478bd9Sstevel@tonic-gate  *	for BPP PARALLEL interfaces
103*7c478bd9Sstevel@tonic-gate  **/
104*7c478bd9Sstevel@tonic-gate 
105*7c478bd9Sstevel@tonic-gate int is_a_parallel_bpp(int fd)
106*7c478bd9Sstevel@tonic-gate {
107*7c478bd9Sstevel@tonic-gate 	if (ioctl(fd, BPPIOC_TESTIO) == 0 || errno == EIO)
108*7c478bd9Sstevel@tonic-gate 		return(1);
109*7c478bd9Sstevel@tonic-gate 	return(0);
110*7c478bd9Sstevel@tonic-gate }
111*7c478bd9Sstevel@tonic-gate 
112*7c478bd9Sstevel@tonic-gate 
113*7c478bd9Sstevel@tonic-gate #if defined(DEBUG) && defined(NOTDEF)
114*7c478bd9Sstevel@tonic-gate char *BppState(int state)
115*7c478bd9Sstevel@tonic-gate {
116*7c478bd9Sstevel@tonic-gate 	static char buf[BUFSIZ];
117*7c478bd9Sstevel@tonic-gate 
118*7c478bd9Sstevel@tonic-gate 	memset(buf, 0, sizeof(buf));
119*7c478bd9Sstevel@tonic-gate 	sprintf(buf, "State (0x%.4x) - (%s%s%s%s)\n", state,
120*7c478bd9Sstevel@tonic-gate 		((state & BPP_SLCT_ERR) ?  "offline " : ""),
121*7c478bd9Sstevel@tonic-gate 		((state & BPP_BUSY_ERR) ?  "busy " : ""),
122*7c478bd9Sstevel@tonic-gate 		((state & BPP_PE_ERR) ?  "paper " : ""),
123*7c478bd9Sstevel@tonic-gate 		((state & BPP_ERR_ERR) ?  "error " : ""));
124*7c478bd9Sstevel@tonic-gate 
125*7c478bd9Sstevel@tonic-gate 	return(buf);
126*7c478bd9Sstevel@tonic-gate }
127*7c478bd9Sstevel@tonic-gate #endif
128*7c478bd9Sstevel@tonic-gate 
129*7c478bd9Sstevel@tonic-gate int bpp_state(int fd)
130*7c478bd9Sstevel@tonic-gate {
131*7c478bd9Sstevel@tonic-gate 	if (ioctl(fd, BPPIOC_TESTIO)) {
132*7c478bd9Sstevel@tonic-gate 		struct bpp_error_status  bpp_stat;
133*7c478bd9Sstevel@tonic-gate 		int state;
134*7c478bd9Sstevel@tonic-gate 
135*7c478bd9Sstevel@tonic-gate 		if (ioctl(fd, BPPIOC_GETERR, &bpp_stat) < 0)
136*7c478bd9Sstevel@tonic-gate 			exit(PRINTER_IO_ERROR);
137*7c478bd9Sstevel@tonic-gate 		state = bpp_stat.pin_status;
138*7c478bd9Sstevel@tonic-gate 
139*7c478bd9Sstevel@tonic-gate #if defined(DEBUG) && defined(NOTDEF)
140*7c478bd9Sstevel@tonic-gate 		logit("%s", BppState(state));
141*7c478bd9Sstevel@tonic-gate #endif
142*7c478bd9Sstevel@tonic-gate 
143*7c478bd9Sstevel@tonic-gate 		if (state == (BPP_PE_ERR | BPP_ERR_ERR | BPP_SLCT_ERR)) {
144*7c478bd9Sstevel@tonic-gate 			/* paper is out */
145*7c478bd9Sstevel@tonic-gate 			return(PRINTER_ERROR_PAPER_OUT);
146*7c478bd9Sstevel@tonic-gate 		} else if (state & BPP_BUSY_ERR) {
147*7c478bd9Sstevel@tonic-gate 			/* printer is busy */
148*7c478bd9Sstevel@tonic-gate 			return(PRINTER_ERROR_BUSY);
149*7c478bd9Sstevel@tonic-gate 		} else if (state & BPP_SLCT_ERR) {
150*7c478bd9Sstevel@tonic-gate 			/* printer is offline */
151*7c478bd9Sstevel@tonic-gate 			return(PRINTER_ERROR_OFFLINE);
152*7c478bd9Sstevel@tonic-gate 		} else if (state & BPP_ERR_ERR) {
153*7c478bd9Sstevel@tonic-gate 			/* printer is errored */
154*7c478bd9Sstevel@tonic-gate 			return(PRINTER_ERROR_ERROR);
155*7c478bd9Sstevel@tonic-gate 		} else if (state == BPP_PE_ERR) {
156*7c478bd9Sstevel@tonic-gate 			/* printer is off/unplugged */
157*7c478bd9Sstevel@tonic-gate 			return(PRINTER_ERROR_CABLE_POWER);
158*7c478bd9Sstevel@tonic-gate 		} else if (state) {
159*7c478bd9Sstevel@tonic-gate 			return(PRINTER_ERROR_UNKNOWN);
160*7c478bd9Sstevel@tonic-gate 		} else
161*7c478bd9Sstevel@tonic-gate 			return(0);
162*7c478bd9Sstevel@tonic-gate 	}
163*7c478bd9Sstevel@tonic-gate 	return(0);
164*7c478bd9Sstevel@tonic-gate }
165*7c478bd9Sstevel@tonic-gate 
166*7c478bd9Sstevel@tonic-gate /*
167*7c478bd9Sstevel@tonic-gate  * For ecpp parallel port
168*7c478bd9Sstevel@tonic-gate  */
169*7c478bd9Sstevel@tonic-gate 
170*7c478bd9Sstevel@tonic-gate int
171*7c478bd9Sstevel@tonic-gate get_ecpp_status(int fd)
172*7c478bd9Sstevel@tonic-gate {
173*7c478bd9Sstevel@tonic-gate 	int state;
174*7c478bd9Sstevel@tonic-gate 	struct ecpp_transfer_parms transfer_parms;
175*7c478bd9Sstevel@tonic-gate 
176*7c478bd9Sstevel@tonic-gate 
177*7c478bd9Sstevel@tonic-gate 	if (ioctl(fd, ECPPIOC_GETPARMS, &transfer_parms) == -1) {
178*7c478bd9Sstevel@tonic-gate 		return(-1);
179*7c478bd9Sstevel@tonic-gate 	}
180*7c478bd9Sstevel@tonic-gate 
181*7c478bd9Sstevel@tonic-gate 	state = transfer_parms.mode;
182*7c478bd9Sstevel@tonic-gate 	/*
183*7c478bd9Sstevel@tonic-gate 	 * We don't know what all printers will return in
184*7c478bd9Sstevel@tonic-gate 	 * nibble mode, therefore if we support nibble mode we will
185*7c478bd9Sstevel@tonic-gate 	 * force the printer to be in CENTRONICS mode.
186*7c478bd9Sstevel@tonic-gate 	 */
187*7c478bd9Sstevel@tonic-gate 	if (state != ECPP_CENTRONICS) {
188*7c478bd9Sstevel@tonic-gate 		transfer_parms.mode = ECPP_CENTRONICS;
189*7c478bd9Sstevel@tonic-gate 		if (ioctl(fd, ECPPIOC_SETPARMS, &transfer_parms) == -1) {
190*7c478bd9Sstevel@tonic-gate 			return(-1);
191*7c478bd9Sstevel@tonic-gate 		} else {
192*7c478bd9Sstevel@tonic-gate 			state = ECPP_CENTRONICS;
193*7c478bd9Sstevel@tonic-gate 		}
194*7c478bd9Sstevel@tonic-gate 	}
195*7c478bd9Sstevel@tonic-gate 
196*7c478bd9Sstevel@tonic-gate 
197*7c478bd9Sstevel@tonic-gate 	return(state);
198*7c478bd9Sstevel@tonic-gate }
199*7c478bd9Sstevel@tonic-gate 
200*7c478bd9Sstevel@tonic-gate /**
201*7c478bd9Sstevel@tonic-gate  * For prnio(7I) - generic printer interface
202*7c478bd9Sstevel@tonic-gate  **/
203*7c478bd9Sstevel@tonic-gate int is_a_prnio(int fd)
204*7c478bd9Sstevel@tonic-gate {
205*7c478bd9Sstevel@tonic-gate 	uint_t	cap;
206*7c478bd9Sstevel@tonic-gate 
207*7c478bd9Sstevel@tonic-gate 	/* check if device supports prnio */
208*7c478bd9Sstevel@tonic-gate 	if (ioctl(fd, PRNIOC_GET_IFCAP, &cap) == -1) {
209*7c478bd9Sstevel@tonic-gate 		return (0);
210*7c478bd9Sstevel@tonic-gate 	}
211*7c478bd9Sstevel@tonic-gate 	/* we will use 1284 status if available */
212*7c478bd9Sstevel@tonic-gate 	if ((cap & PRN_1284_STATUS) == 0) {
213*7c478bd9Sstevel@tonic-gate 		/* some devices may only support 1284 status in unidir. mode */
214*7c478bd9Sstevel@tonic-gate 		if (cap & PRN_BIDI) {
215*7c478bd9Sstevel@tonic-gate 			cap &= ~PRN_BIDI;
216*7c478bd9Sstevel@tonic-gate 			(void) ioctl(fd, PRNIOC_SET_IFCAP, &cap);
217*7c478bd9Sstevel@tonic-gate 		}
218*7c478bd9Sstevel@tonic-gate 	}
219*7c478bd9Sstevel@tonic-gate 	return (1);
220*7c478bd9Sstevel@tonic-gate }
221*7c478bd9Sstevel@tonic-gate 
222*7c478bd9Sstevel@tonic-gate int prnio_state(int fd)
223*7c478bd9Sstevel@tonic-gate {
224*7c478bd9Sstevel@tonic-gate 	uint_t	status;
225*7c478bd9Sstevel@tonic-gate 	uchar_t	pins;
226*7c478bd9Sstevel@tonic-gate 
227*7c478bd9Sstevel@tonic-gate 	if ((ioctl(fd, PRNIOC_GET_STATUS, &status) == 0) &&
228*7c478bd9Sstevel@tonic-gate 	    (status & PRN_READY)) {
229*7c478bd9Sstevel@tonic-gate 		return(0);
230*7c478bd9Sstevel@tonic-gate 	}
231*7c478bd9Sstevel@tonic-gate 
232*7c478bd9Sstevel@tonic-gate 	if (ioctl(fd, PRNIOC_GET_1284_STATUS, &pins) != 0) {
233*7c478bd9Sstevel@tonic-gate 		return(PRINTER_ERROR_UNKNOWN);
234*7c478bd9Sstevel@tonic-gate 	}
235*7c478bd9Sstevel@tonic-gate 
236*7c478bd9Sstevel@tonic-gate 	if ((pins & ~PRN_1284_BUSY) == PRN_1284_PE) {
237*7c478bd9Sstevel@tonic-gate 		/* paper is out */
238*7c478bd9Sstevel@tonic-gate 		return(PRINTER_ERROR_PAPER_OUT);
239*7c478bd9Sstevel@tonic-gate 	} else if (pins == (PRN_1284_PE | PRN_1284_SELECT |
240*7c478bd9Sstevel@tonic-gate 				PRN_1284_NOFAULT | PRN_1284_BUSY)) {
241*7c478bd9Sstevel@tonic-gate 		/* printer is off/unplugged */
242*7c478bd9Sstevel@tonic-gate 		return(PRINTER_ERROR_CABLE_POWER);
243*7c478bd9Sstevel@tonic-gate 	} else if ((pins & PRN_1284_SELECT) == 0) {
244*7c478bd9Sstevel@tonic-gate 		/* printer is offline */
245*7c478bd9Sstevel@tonic-gate 		return(PRINTER_ERROR_OFFLINE);
246*7c478bd9Sstevel@tonic-gate 	} else if ((pins & PRN_1284_NOFAULT) == 0) {
247*7c478bd9Sstevel@tonic-gate 		/* printer is errored */
248*7c478bd9Sstevel@tonic-gate 		return(PRINTER_ERROR_ERROR);
249*7c478bd9Sstevel@tonic-gate 	} else if (pins & PRN_1284_PE) {
250*7c478bd9Sstevel@tonic-gate 		/* paper is out */
251*7c478bd9Sstevel@tonic-gate 		return(PRINTER_ERROR_PAPER_OUT);
252*7c478bd9Sstevel@tonic-gate 	} else if (pins ^ (PRN_1284_SELECT | PRN_1284_NOFAULT)) {
253*7c478bd9Sstevel@tonic-gate 		return(PRINTER_ERROR_UNKNOWN);
254*7c478bd9Sstevel@tonic-gate 	}
255*7c478bd9Sstevel@tonic-gate 
256*7c478bd9Sstevel@tonic-gate 	return(0);
257*7c478bd9Sstevel@tonic-gate }
258*7c478bd9Sstevel@tonic-gate 
259*7c478bd9Sstevel@tonic-gate /**
260*7c478bd9Sstevel@tonic-gate  *	Common routines
261*7c478bd9Sstevel@tonic-gate  **/
262*7c478bd9Sstevel@tonic-gate 
263*7c478bd9Sstevel@tonic-gate /*ARGSUSED0*/
264*7c478bd9Sstevel@tonic-gate static void
265*7c478bd9Sstevel@tonic-gate ByeByeParallel(int sig)
266*7c478bd9Sstevel@tonic-gate {
267*7c478bd9Sstevel@tonic-gate 	/* try to shove out the EOT */
268*7c478bd9Sstevel@tonic-gate 	(void) write(1, "\004", 1);
269*7c478bd9Sstevel@tonic-gate 	exit(0);
270*7c478bd9Sstevel@tonic-gate }
271*7c478bd9Sstevel@tonic-gate 
272*7c478bd9Sstevel@tonic-gate 
273*7c478bd9Sstevel@tonic-gate /*ARGSUSED0*/
274*7c478bd9Sstevel@tonic-gate static void
275*7c478bd9Sstevel@tonic-gate printer_info(char *fmt, ...)
276*7c478bd9Sstevel@tonic-gate {
277*7c478bd9Sstevel@tonic-gate 	char mesg[BUFSIZ];
278*7c478bd9Sstevel@tonic-gate 	va_list ap;
279*7c478bd9Sstevel@tonic-gate 
280*7c478bd9Sstevel@tonic-gate 	va_start(ap, fmt);
281*7c478bd9Sstevel@tonic-gate 	vsprintf(mesg, fmt, ap);
282*7c478bd9Sstevel@tonic-gate 	va_end(ap);
283*7c478bd9Sstevel@tonic-gate /*
284*7c478bd9Sstevel@tonic-gate 	fprintf(stderr,
285*7c478bd9Sstevel@tonic-gate 		"%%%%[ PrinterError: %s; source: parallel ]%%%%\n",
286*7c478bd9Sstevel@tonic-gate 		mesg);
287*7c478bd9Sstevel@tonic-gate */
288*7c478bd9Sstevel@tonic-gate 	fprintf(stderr, "%s\n", mesg);
289*7c478bd9Sstevel@tonic-gate 	fflush(stderr);
290*7c478bd9Sstevel@tonic-gate 	fsync(2);
291*7c478bd9Sstevel@tonic-gate 
292*7c478bd9Sstevel@tonic-gate }
293*7c478bd9Sstevel@tonic-gate 
294*7c478bd9Sstevel@tonic-gate static void
295*7c478bd9Sstevel@tonic-gate printer_error(int error)
296*7c478bd9Sstevel@tonic-gate {
297*7c478bd9Sstevel@tonic-gate 	switch (error) {
298*7c478bd9Sstevel@tonic-gate 		case -1:
299*7c478bd9Sstevel@tonic-gate 			printer_info("ioctl(): %s", _sys_errlist[errno]);
300*7c478bd9Sstevel@tonic-gate 			break;
301*7c478bd9Sstevel@tonic-gate 		case PRINTER_ERROR_PAPER_OUT:
302*7c478bd9Sstevel@tonic-gate 			printer_info("out of paper");
303*7c478bd9Sstevel@tonic-gate 			break;
304*7c478bd9Sstevel@tonic-gate 		case PRINTER_ERROR_OFFLINE:
305*7c478bd9Sstevel@tonic-gate 			printer_info("offline");
306*7c478bd9Sstevel@tonic-gate 			break;
307*7c478bd9Sstevel@tonic-gate 		case PRINTER_ERROR_BUSY:
308*7c478bd9Sstevel@tonic-gate 			printer_info("busy");
309*7c478bd9Sstevel@tonic-gate 			break;
310*7c478bd9Sstevel@tonic-gate 		case PRINTER_ERROR_ERROR:
311*7c478bd9Sstevel@tonic-gate 			printer_info("printer error");
312*7c478bd9Sstevel@tonic-gate 			break;
313*7c478bd9Sstevel@tonic-gate 		case PRINTER_ERROR_CABLE_POWER:
314*7c478bd9Sstevel@tonic-gate 			printer_info("printer powered off or disconnected");
315*7c478bd9Sstevel@tonic-gate 			break;
316*7c478bd9Sstevel@tonic-gate 		case PRINTER_ERROR_UNKNOWN:
317*7c478bd9Sstevel@tonic-gate 			printer_info("unknown error");
318*7c478bd9Sstevel@tonic-gate 			break;
319*7c478bd9Sstevel@tonic-gate 		case PRINTER_ERROR_TIMEOUT:
320*7c478bd9Sstevel@tonic-gate 			printer_info("communications timeout");
321*7c478bd9Sstevel@tonic-gate 			break;
322*7c478bd9Sstevel@tonic-gate 		default:
323*7c478bd9Sstevel@tonic-gate 			printer_info("get_status() failed");
324*7c478bd9Sstevel@tonic-gate 	}
325*7c478bd9Sstevel@tonic-gate }
326*7c478bd9Sstevel@tonic-gate 
327*7c478bd9Sstevel@tonic-gate 
328*7c478bd9Sstevel@tonic-gate static void
329*7c478bd9Sstevel@tonic-gate wait_state(int fd, int get_state())
330*7c478bd9Sstevel@tonic-gate {
331*7c478bd9Sstevel@tonic-gate 	int state;
332*7c478bd9Sstevel@tonic-gate 	int was_faulted = 0;
333*7c478bd9Sstevel@tonic-gate 
334*7c478bd9Sstevel@tonic-gate 	while (state = get_state(fd)) {
335*7c478bd9Sstevel@tonic-gate 		was_faulted=1;
336*7c478bd9Sstevel@tonic-gate 		printer_error(state);
337*7c478bd9Sstevel@tonic-gate 		sleep(15);
338*7c478bd9Sstevel@tonic-gate 	}
339*7c478bd9Sstevel@tonic-gate 
340*7c478bd9Sstevel@tonic-gate 	if (was_faulted) {
341*7c478bd9Sstevel@tonic-gate 		fprintf(stderr, "printer ok\n");
342*7c478bd9Sstevel@tonic-gate 		fflush(stderr);
343*7c478bd9Sstevel@tonic-gate 		fsync(2);
344*7c478bd9Sstevel@tonic-gate 	}
345*7c478bd9Sstevel@tonic-gate }
346*7c478bd9Sstevel@tonic-gate 
347*7c478bd9Sstevel@tonic-gate /**
348*7c478bd9Sstevel@tonic-gate  **  end of Sun Additions for parallel port
349*7c478bd9Sstevel@tonic-gate  **/
350*7c478bd9Sstevel@tonic-gate #define	IDENTICAL(A,B)	(A.st_dev==B.st_dev && A.st_ino==B.st_ino)
351*7c478bd9Sstevel@tonic-gate #define ISBLK(A)	((A.st_mode & S_IFMT) == S_IFBLK)
352*7c478bd9Sstevel@tonic-gate #define ISCHR(A)	((A.st_mode & S_IFMT) == S_IFCHR)
353*7c478bd9Sstevel@tonic-gate 
354*7c478bd9Sstevel@tonic-gate #define E_SUCCESS	0
355*7c478bd9Sstevel@tonic-gate #define E_BAD_INPUT	1
356*7c478bd9Sstevel@tonic-gate #define E_BAD_OUTPUT	2
357*7c478bd9Sstevel@tonic-gate #define E_BAD_TERM	3
358*7c478bd9Sstevel@tonic-gate #define E_IDENTICAL	4
359*7c478bd9Sstevel@tonic-gate #define	E_WRITE_FAILED	5
360*7c478bd9Sstevel@tonic-gate #define	E_TIMEOUT	6
361*7c478bd9Sstevel@tonic-gate #define E_HANGUP	7
362*7c478bd9Sstevel@tonic-gate #define E_INTERRUPT	8
363*7c478bd9Sstevel@tonic-gate 
364*7c478bd9Sstevel@tonic-gate #define SAFETY_FACTOR	2.0
365*7c478bd9Sstevel@tonic-gate #define R(F)		(int)((F) + .5)
366*7c478bd9Sstevel@tonic-gate #define DELAY(N,D)	R(SAFETY_FACTOR * ((N) / (double)(D)))
367*7c478bd9Sstevel@tonic-gate 
368*7c478bd9Sstevel@tonic-gate extern int		sys_nerr;
369*7c478bd9Sstevel@tonic-gate 
370*7c478bd9Sstevel@tonic-gate extern char		*sys_errlist[],
371*7c478bd9Sstevel@tonic-gate 			*getenv();
372*7c478bd9Sstevel@tonic-gate 
373*7c478bd9Sstevel@tonic-gate extern int		atoi();
374*7c478bd9Sstevel@tonic-gate 
375*7c478bd9Sstevel@tonic-gate char			buffer[BUFSIZ];
376*7c478bd9Sstevel@tonic-gate 
377*7c478bd9Sstevel@tonic-gate void			sighup(),
378*7c478bd9Sstevel@tonic-gate 			sigint(),
379*7c478bd9Sstevel@tonic-gate 			sigquit(),
380*7c478bd9Sstevel@tonic-gate 			sigpipe(),
381*7c478bd9Sstevel@tonic-gate 			sigalrm(),
382*7c478bd9Sstevel@tonic-gate 			sigterm();
383*7c478bd9Sstevel@tonic-gate 
384*7c478bd9Sstevel@tonic-gate #if	defined(baudrate)
385*7c478bd9Sstevel@tonic-gate # undef	baudrate
386*7c478bd9Sstevel@tonic-gate #endif
387*7c478bd9Sstevel@tonic-gate 
388*7c478bd9Sstevel@tonic-gate int			baudrate();
389*7c478bd9Sstevel@tonic-gate 
390*7c478bd9Sstevel@tonic-gate 
391*7c478bd9Sstevel@tonic-gate int nop(int fd) { return (0); }
392*7c478bd9Sstevel@tonic-gate int bpp_state(int);
393*7c478bd9Sstevel@tonic-gate 
394*7c478bd9Sstevel@tonic-gate 
395*7c478bd9Sstevel@tonic-gate /**
396*7c478bd9Sstevel@tonic-gate  ** main()
397*7c478bd9Sstevel@tonic-gate  **/
398*7c478bd9Sstevel@tonic-gate 
399*7c478bd9Sstevel@tonic-gate int			main (argc, argv)
400*7c478bd9Sstevel@tonic-gate 	int			argc;
401*7c478bd9Sstevel@tonic-gate 	char			*argv[];
402*7c478bd9Sstevel@tonic-gate {
403*7c478bd9Sstevel@tonic-gate 	register int		nin,
404*7c478bd9Sstevel@tonic-gate 				nout,
405*7c478bd9Sstevel@tonic-gate 				effective_rate,
406*7c478bd9Sstevel@tonic-gate 				max_delay	= 0,
407*7c478bd9Sstevel@tonic-gate 				n;
408*7c478bd9Sstevel@tonic-gate 
409*7c478bd9Sstevel@tonic-gate 	int			report_rate;
410*7c478bd9Sstevel@tonic-gate 
411*7c478bd9Sstevel@tonic-gate 	short			print_rate;
412*7c478bd9Sstevel@tonic-gate 
413*7c478bd9Sstevel@tonic-gate 	struct stat		in,
414*7c478bd9Sstevel@tonic-gate 				out;
415*7c478bd9Sstevel@tonic-gate 
416*7c478bd9Sstevel@tonic-gate 	struct tms		tms;
417*7c478bd9Sstevel@tonic-gate 
418*7c478bd9Sstevel@tonic-gate 	long			epoch_start,
419*7c478bd9Sstevel@tonic-gate 				epoch_end;
420*7c478bd9Sstevel@tonic-gate 
421*7c478bd9Sstevel@tonic-gate 	char			*TERM;
422*7c478bd9Sstevel@tonic-gate 
423*7c478bd9Sstevel@tonic-gate 	int			(*func)(int fd);
424*7c478bd9Sstevel@tonic-gate 
425*7c478bd9Sstevel@tonic-gate 	/*
426*7c478bd9Sstevel@tonic-gate 	 * The Spooler can hit us with SIGTERM for three reasons:
427*7c478bd9Sstevel@tonic-gate 	 *
428*7c478bd9Sstevel@tonic-gate 	 *	- the user's job has been canceled
429*7c478bd9Sstevel@tonic-gate 	 *	- the printer has been disabled while we were printing
430*7c478bd9Sstevel@tonic-gate 	 *	- the Spooler heard that the printer has a fault,
431*7c478bd9Sstevel@tonic-gate 	 *	  and the fault recovery is wait or beginning
432*7c478bd9Sstevel@tonic-gate 	 *
433*7c478bd9Sstevel@tonic-gate 	 * We should exit cleanly for the first two cases,
434*7c478bd9Sstevel@tonic-gate 	 * but we have to be careful with the last. If it was THIS
435*7c478bd9Sstevel@tonic-gate 	 * PROGRAM that told the Spooler about the fault, we must
436*7c478bd9Sstevel@tonic-gate 	 * exit consistently.
437*7c478bd9Sstevel@tonic-gate 	 *
438*7c478bd9Sstevel@tonic-gate 	 * The method of avoiding any problem is to turn off the
439*7c478bd9Sstevel@tonic-gate 	 * trapping of SIGTERM before telling the Spooler about
440*7c478bd9Sstevel@tonic-gate 	 * the fault.
441*7c478bd9Sstevel@tonic-gate 	 *
442*7c478bd9Sstevel@tonic-gate 	 * Faults that we can detect:
443*7c478bd9Sstevel@tonic-gate 	 *	- hangup (drop of carrier)
444*7c478bd9Sstevel@tonic-gate 	 *	- interrupt (printer sent a break or quit character)
445*7c478bd9Sstevel@tonic-gate 	 *	- SIGPIPE (output port is a FIFO, and was closed early)
446*7c478bd9Sstevel@tonic-gate 	 *	- failed or incomplete write()
447*7c478bd9Sstevel@tonic-gate 	 *	- excess delay in write() (handled with SIGALRM later)
448*7c478bd9Sstevel@tonic-gate 	 *
449*7c478bd9Sstevel@tonic-gate 	 * Pseudo-faults (errors in use):
450*7c478bd9Sstevel@tonic-gate 	 *	- No input/output, or strange input/output
451*7c478bd9Sstevel@tonic-gate 	 *	- Input/output identical
452*7c478bd9Sstevel@tonic-gate 	 *	- No TERM defined or trouble reading Terminfo database
453*7c478bd9Sstevel@tonic-gate 	 */
454*7c478bd9Sstevel@tonic-gate 	signal (SIGTERM, sigterm);
455*7c478bd9Sstevel@tonic-gate 	signal (SIGHUP, sighup);
456*7c478bd9Sstevel@tonic-gate 	signal (SIGINT, sigint);
457*7c478bd9Sstevel@tonic-gate 	signal (SIGQUIT, sigint);
458*7c478bd9Sstevel@tonic-gate 	signal (SIGPIPE, sigpipe);
459*7c478bd9Sstevel@tonic-gate 
460*7c478bd9Sstevel@tonic-gate 
461*7c478bd9Sstevel@tonic-gate 	if (argc > 1 && STREQU(argv[1], "-r")) {
462*7c478bd9Sstevel@tonic-gate 		report_rate = 1;
463*7c478bd9Sstevel@tonic-gate 		argc--;
464*7c478bd9Sstevel@tonic-gate 		argv++;
465*7c478bd9Sstevel@tonic-gate 	} else
466*7c478bd9Sstevel@tonic-gate 		report_rate = 0;
467*7c478bd9Sstevel@tonic-gate 
468*7c478bd9Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
469*7c478bd9Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)
470*7c478bd9Sstevel@tonic-gate #define TEXT_DOMAIN "SYS_TEST"
471*7c478bd9Sstevel@tonic-gate #endif
472*7c478bd9Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
473*7c478bd9Sstevel@tonic-gate 
474*7c478bd9Sstevel@tonic-gate 	/*
475*7c478bd9Sstevel@tonic-gate 	 * Stat the standard output to be sure it is defined.
476*7c478bd9Sstevel@tonic-gate 	 */
477*7c478bd9Sstevel@tonic-gate 	if (fstat(1, &out) < 0) {
478*7c478bd9Sstevel@tonic-gate 		signal (SIGTERM, SIG_IGN);
479*7c478bd9Sstevel@tonic-gate 		fprintf (
480*7c478bd9Sstevel@tonic-gate 			stderr,
481*7c478bd9Sstevel@tonic-gate 		gettext("Can't stat output (%s);\nincorrect use of lp.cat!\n"),
482*7c478bd9Sstevel@tonic-gate 			PERROR
483*7c478bd9Sstevel@tonic-gate 		);
484*7c478bd9Sstevel@tonic-gate 		exit (E_BAD_OUTPUT);
485*7c478bd9Sstevel@tonic-gate 	}
486*7c478bd9Sstevel@tonic-gate 
487*7c478bd9Sstevel@tonic-gate 	/*
488*7c478bd9Sstevel@tonic-gate 	 * Stat the standard input to be sure it is defined.
489*7c478bd9Sstevel@tonic-gate 	 */
490*7c478bd9Sstevel@tonic-gate 	if (fstat(0, &in) < 0) {
491*7c478bd9Sstevel@tonic-gate 		signal (SIGTERM, SIG_IGN);
492*7c478bd9Sstevel@tonic-gate 		fprintf (
493*7c478bd9Sstevel@tonic-gate 			stderr,
494*7c478bd9Sstevel@tonic-gate 		gettext("Can't stat input (%s);\nincorrect use of lp.cat!\n"),
495*7c478bd9Sstevel@tonic-gate 			PERROR
496*7c478bd9Sstevel@tonic-gate 		);
497*7c478bd9Sstevel@tonic-gate 		exit (E_BAD_INPUT);
498*7c478bd9Sstevel@tonic-gate 	}
499*7c478bd9Sstevel@tonic-gate 
500*7c478bd9Sstevel@tonic-gate 	/*
501*7c478bd9Sstevel@tonic-gate 	 * If the standard output is not a character special file or a
502*7c478bd9Sstevel@tonic-gate 	 * block special file, make sure it is not identical to the
503*7c478bd9Sstevel@tonic-gate 	 * standard input.
504*7c478bd9Sstevel@tonic-gate 	 *
505*7c478bd9Sstevel@tonic-gate 	 * If we are an ecpp parallel port in centronics mode treat
506*7c478bd9Sstevel@tonic-gate 	 * ourselves as a bpp compatible device.
507*7c478bd9Sstevel@tonic-gate 	 */
508*7c478bd9Sstevel@tonic-gate 
509*7c478bd9Sstevel@tonic-gate 	if (is_a_prnio(1)) {
510*7c478bd9Sstevel@tonic-gate 		func = prnio_state;
511*7c478bd9Sstevel@tonic-gate 	} else if (is_a_parallel_bpp(1) ||
512*7c478bd9Sstevel@tonic-gate 		    (get_ecpp_status(1) == ECPP_CENTRONICS)) {
513*7c478bd9Sstevel@tonic-gate 		func = bpp_state;
514*7c478bd9Sstevel@tonic-gate         } else if (isatty(1)) {
515*7c478bd9Sstevel@tonic-gate 		/* serial connection (probably) - continue as usual */
516*7c478bd9Sstevel@tonic-gate 		func = nop;
517*7c478bd9Sstevel@tonic-gate         } else {
518*7c478bd9Sstevel@tonic-gate 		func = nop;
519*7c478bd9Sstevel@tonic-gate         }
520*7c478bd9Sstevel@tonic-gate 
521*7c478bd9Sstevel@tonic-gate 	if (!ISCHR(out) && !ISBLK(out) && IDENTICAL(out, in)) {
522*7c478bd9Sstevel@tonic-gate 		signal (SIGTERM, SIG_IGN);
523*7c478bd9Sstevel@tonic-gate 		fprintf (
524*7c478bd9Sstevel@tonic-gate 			stderr,
525*7c478bd9Sstevel@tonic-gate 	gettext("Input and output are identical; incorrect use of lp.cat!\n")
526*7c478bd9Sstevel@tonic-gate 		);
527*7c478bd9Sstevel@tonic-gate 		exit (E_IDENTICAL);
528*7c478bd9Sstevel@tonic-gate 	}
529*7c478bd9Sstevel@tonic-gate 
530*7c478bd9Sstevel@tonic-gate 	/*
531*7c478bd9Sstevel@tonic-gate 	 * The effective data transfer rate is the lesser
532*7c478bd9Sstevel@tonic-gate 	 * of the transmission rate and print rate. If an
533*7c478bd9Sstevel@tonic-gate 	 * argument was passed to us, it should be a data
534*7c478bd9Sstevel@tonic-gate 	 * rate and it may be lower still.
535*7c478bd9Sstevel@tonic-gate 	 * Based on the effective data transfer rate,
536*7c478bd9Sstevel@tonic-gate 	 * we can predict the maximum delay we should experience.
537*7c478bd9Sstevel@tonic-gate 	 * But there are other factors that could introduce
538*7c478bd9Sstevel@tonic-gate 	 * delay, so let's be generous; after all, we'd rather
539*7c478bd9Sstevel@tonic-gate 	 * err in favor of waiting too long to detect a fault
540*7c478bd9Sstevel@tonic-gate 	 * than err too often on false alarms.
541*7c478bd9Sstevel@tonic-gate 	 */
542*7c478bd9Sstevel@tonic-gate 
543*7c478bd9Sstevel@tonic-gate 	if (
544*7c478bd9Sstevel@tonic-gate 		!(TERM = getenv("TERM"))
545*7c478bd9Sstevel@tonic-gate 	     || !*TERM
546*7c478bd9Sstevel@tonic-gate 	) {
547*7c478bd9Sstevel@tonic-gate 		signal (SIGTERM, SIG_IGN);
548*7c478bd9Sstevel@tonic-gate 		fprintf (
549*7c478bd9Sstevel@tonic-gate 			stderr,
550*7c478bd9Sstevel@tonic-gate 	gettext("No TERM variable defined! Trouble with the Spooler!\n")
551*7c478bd9Sstevel@tonic-gate 		);
552*7c478bd9Sstevel@tonic-gate 		exit (E_BAD_TERM);
553*7c478bd9Sstevel@tonic-gate 	}
554*7c478bd9Sstevel@tonic-gate 	if (
555*7c478bd9Sstevel@tonic-gate 		!STREQU(TERM, NAME_UNKNOWN)
556*7c478bd9Sstevel@tonic-gate 	     && tidbit(TERM, "cps", &print_rate) == -1
557*7c478bd9Sstevel@tonic-gate 	) {
558*7c478bd9Sstevel@tonic-gate 		signal (SIGTERM, SIG_IGN);
559*7c478bd9Sstevel@tonic-gate 		fprintf (
560*7c478bd9Sstevel@tonic-gate 			stderr,
561*7c478bd9Sstevel@tonic-gate gettext("Trouble identifying printer type \"%s\"; check the Terminfo database.\n"),
562*7c478bd9Sstevel@tonic-gate 			TERM
563*7c478bd9Sstevel@tonic-gate 		);
564*7c478bd9Sstevel@tonic-gate 		exit (E_BAD_TERM);
565*7c478bd9Sstevel@tonic-gate 	}
566*7c478bd9Sstevel@tonic-gate 	if (STREQU(TERM, NAME_UNKNOWN))
567*7c478bd9Sstevel@tonic-gate 		print_rate = -1;
568*7c478bd9Sstevel@tonic-gate 
569*7c478bd9Sstevel@tonic-gate 	effective_rate = baudrate() / 10; /* okay for most bauds */
570*7c478bd9Sstevel@tonic-gate 	if (print_rate != -1 && print_rate < effective_rate)
571*7c478bd9Sstevel@tonic-gate 		effective_rate = print_rate;
572*7c478bd9Sstevel@tonic-gate 	if (argc > 1 && (n = atoi(argv[1])) >= 0 && n < effective_rate)
573*7c478bd9Sstevel@tonic-gate 		effective_rate = n;	  /* 0 means infinite delay */
574*7c478bd9Sstevel@tonic-gate 	if (effective_rate)
575*7c478bd9Sstevel@tonic-gate 		max_delay = DELAY(BUFSIZ, effective_rate);
576*7c478bd9Sstevel@tonic-gate 
577*7c478bd9Sstevel@tonic-gate 	/*
578*7c478bd9Sstevel@tonic-gate 	 * We'll use the "alarm()" system call to keep us from
579*7c478bd9Sstevel@tonic-gate 	 * waiting too long to write to a printer in trouble.
580*7c478bd9Sstevel@tonic-gate 	 */
581*7c478bd9Sstevel@tonic-gate 	if (max_delay)
582*7c478bd9Sstevel@tonic-gate 		signal (SIGALRM, sigalrm);
583*7c478bd9Sstevel@tonic-gate 
584*7c478bd9Sstevel@tonic-gate 	/*
585*7c478bd9Sstevel@tonic-gate 	 * While not end of standard input, copy blocks to
586*7c478bd9Sstevel@tonic-gate 	 * standard output.
587*7c478bd9Sstevel@tonic-gate 	 */
588*7c478bd9Sstevel@tonic-gate 	while ((nin = read(0, buffer, BUFSIZ)) > 0) {
589*7c478bd9Sstevel@tonic-gate 		char *ptr = buffer;
590*7c478bd9Sstevel@tonic-gate 
591*7c478bd9Sstevel@tonic-gate 		/*
592*7c478bd9Sstevel@tonic-gate 		 * We should be safe from incomplete writes to a full
593*7c478bd9Sstevel@tonic-gate 		 * pipe, as long as the size of the buffer we write is
594*7c478bd9Sstevel@tonic-gate 		 * a even divisor of the pipe buffer limit. As long as
595*7c478bd9Sstevel@tonic-gate 		 * we read from files or pipes (not communication devices)
596*7c478bd9Sstevel@tonic-gate 		 * this should be true for all but the last buffer. The
597*7c478bd9Sstevel@tonic-gate 		 * last will be smaller, and won't straddle the pipe max
598*7c478bd9Sstevel@tonic-gate 		 * limit (think about it).
599*7c478bd9Sstevel@tonic-gate 		 */
600*7c478bd9Sstevel@tonic-gate #if	PIPE_BUF < BUFSIZ || (PIPE_MAX % BUFSIZ)
601*7c478bd9Sstevel@tonic-gate 		this_wont_compile;
602*7c478bd9Sstevel@tonic-gate #endif
603*7c478bd9Sstevel@tonic-gate 		if (report_rate)
604*7c478bd9Sstevel@tonic-gate 			epoch_start = times(&tms);
605*7c478bd9Sstevel@tonic-gate 		do {
606*7c478bd9Sstevel@tonic-gate 			wait_state(1, func);
607*7c478bd9Sstevel@tonic-gate 
608*7c478bd9Sstevel@tonic-gate 			if (max_delay)
609*7c478bd9Sstevel@tonic-gate 				alarm (max_delay);
610*7c478bd9Sstevel@tonic-gate 			nout = write(1, ptr, nin);
611*7c478bd9Sstevel@tonic-gate 			alarm(0);
612*7c478bd9Sstevel@tonic-gate 			if (nout < 0) {
613*7c478bd9Sstevel@tonic-gate 				fprintf (
614*7c478bd9Sstevel@tonic-gate 					stderr,
615*7c478bd9Sstevel@tonic-gate 	gettext("Write failed (%s);\nperhaps the printer has gone off-line.\n"),
616*7c478bd9Sstevel@tonic-gate 					PERROR
617*7c478bd9Sstevel@tonic-gate 				);
618*7c478bd9Sstevel@tonic-gate 				fflush(stderr);
619*7c478bd9Sstevel@tonic-gate 				if (errno != EINTR)
620*7c478bd9Sstevel@tonic-gate 				/* I/O error on device, get lpcshed to retry */
621*7c478bd9Sstevel@tonic-gate 					exit(PRINTER_IO_ERROR);
622*7c478bd9Sstevel@tonic-gate 				else /* wait for printer to come back online */
623*7c478bd9Sstevel@tonic-gate 					sleep(15);
624*7c478bd9Sstevel@tonic-gate 			} else {
625*7c478bd9Sstevel@tonic-gate 				nin -= nout;
626*7c478bd9Sstevel@tonic-gate 				ptr += nout;
627*7c478bd9Sstevel@tonic-gate 			}
628*7c478bd9Sstevel@tonic-gate 		} while (nin > 0);
629*7c478bd9Sstevel@tonic-gate 
630*7c478bd9Sstevel@tonic-gate 		if (max_delay)
631*7c478bd9Sstevel@tonic-gate 			alarm (0);
632*7c478bd9Sstevel@tonic-gate 		else if (report_rate) {
633*7c478bd9Sstevel@tonic-gate 			epoch_end = times(&tms);
634*7c478bd9Sstevel@tonic-gate 			if (epoch_end - epoch_start > 0)
635*7c478bd9Sstevel@tonic-gate 				fprintf (
636*7c478bd9Sstevel@tonic-gate 					stderr,
637*7c478bd9Sstevel@tonic-gate 					"%d CPS\n",
638*7c478bd9Sstevel@tonic-gate 		R((100 * BUFSIZ) / (double)(epoch_end - epoch_start))
639*7c478bd9Sstevel@tonic-gate 				);
640*7c478bd9Sstevel@tonic-gate 		}
641*7c478bd9Sstevel@tonic-gate 
642*7c478bd9Sstevel@tonic-gate 	}
643*7c478bd9Sstevel@tonic-gate 
644*7c478bd9Sstevel@tonic-gate 	exit (E_SUCCESS);
645*7c478bd9Sstevel@tonic-gate 	/*NOTREACHED*/
646*7c478bd9Sstevel@tonic-gate }
647*7c478bd9Sstevel@tonic-gate 
648*7c478bd9Sstevel@tonic-gate /**
649*7c478bd9Sstevel@tonic-gate  ** sighup() - CATCH A HANGUP (LOSS OF CARRIER)
650*7c478bd9Sstevel@tonic-gate  **/
651*7c478bd9Sstevel@tonic-gate 
652*7c478bd9Sstevel@tonic-gate void			sighup ()
653*7c478bd9Sstevel@tonic-gate {
654*7c478bd9Sstevel@tonic-gate 	signal (SIGTERM, SIG_IGN);
655*7c478bd9Sstevel@tonic-gate 	signal (SIGHUP, SIG_IGN);
656*7c478bd9Sstevel@tonic-gate 	fprintf (stderr, gettext(HANGUP_FAULT_LPCAT));
657*7c478bd9Sstevel@tonic-gate 	exit (E_HANGUP);
658*7c478bd9Sstevel@tonic-gate }
659*7c478bd9Sstevel@tonic-gate 
660*7c478bd9Sstevel@tonic-gate /**
661*7c478bd9Sstevel@tonic-gate  ** sigint() - CATCH AN INTERRUPT
662*7c478bd9Sstevel@tonic-gate  **/
663*7c478bd9Sstevel@tonic-gate 
664*7c478bd9Sstevel@tonic-gate void			sigint ()
665*7c478bd9Sstevel@tonic-gate {
666*7c478bd9Sstevel@tonic-gate 	signal (SIGTERM, SIG_IGN);
667*7c478bd9Sstevel@tonic-gate 	signal (SIGINT, SIG_IGN);
668*7c478bd9Sstevel@tonic-gate 	fprintf (stderr, gettext(INTERRUPT_FAULT));
669*7c478bd9Sstevel@tonic-gate 	exit (E_INTERRUPT);
670*7c478bd9Sstevel@tonic-gate }
671*7c478bd9Sstevel@tonic-gate 
672*7c478bd9Sstevel@tonic-gate /**
673*7c478bd9Sstevel@tonic-gate  ** sigpipe() - CATCH EARLY CLOSE OF PIPE
674*7c478bd9Sstevel@tonic-gate  **/
675*7c478bd9Sstevel@tonic-gate 
676*7c478bd9Sstevel@tonic-gate void			sigpipe ()
677*7c478bd9Sstevel@tonic-gate {
678*7c478bd9Sstevel@tonic-gate 	signal (SIGTERM, SIG_IGN);
679*7c478bd9Sstevel@tonic-gate 	signal (SIGPIPE, SIG_IGN);
680*7c478bd9Sstevel@tonic-gate 	fprintf (stderr, gettext(PIPE_FAULT));
681*7c478bd9Sstevel@tonic-gate 	exit (E_INTERRUPT);
682*7c478bd9Sstevel@tonic-gate }
683*7c478bd9Sstevel@tonic-gate 
684*7c478bd9Sstevel@tonic-gate /**
685*7c478bd9Sstevel@tonic-gate  ** sigalrm() - CATCH AN ALARM
686*7c478bd9Sstevel@tonic-gate  **/
687*7c478bd9Sstevel@tonic-gate 
688*7c478bd9Sstevel@tonic-gate void			sigalrm ()
689*7c478bd9Sstevel@tonic-gate {
690*7c478bd9Sstevel@tonic-gate 	signal (SIGTERM, SIG_IGN);
691*7c478bd9Sstevel@tonic-gate 	fprintf (
692*7c478bd9Sstevel@tonic-gate 		stderr,
693*7c478bd9Sstevel@tonic-gate 	gettext("Excessive write delay; perhaps the printer has gone off-line.\n")
694*7c478bd9Sstevel@tonic-gate 	);
695*7c478bd9Sstevel@tonic-gate 	exit (E_TIMEOUT);
696*7c478bd9Sstevel@tonic-gate }
697*7c478bd9Sstevel@tonic-gate 
698*7c478bd9Sstevel@tonic-gate /**
699*7c478bd9Sstevel@tonic-gate  ** sigterm() - CATCH A TERMINATION SIGNAL
700*7c478bd9Sstevel@tonic-gate  **/
701*7c478bd9Sstevel@tonic-gate 
702*7c478bd9Sstevel@tonic-gate void			sigterm ()
703*7c478bd9Sstevel@tonic-gate {
704*7c478bd9Sstevel@tonic-gate 	signal (SIGTERM, SIG_IGN);
705*7c478bd9Sstevel@tonic-gate 	/*
706*7c478bd9Sstevel@tonic-gate 	 * try to flush the output queue in the case of ecpp port.
707*7c478bd9Sstevel@tonic-gate 	 * ignore the return code as this may not be the ecpp.
708*7c478bd9Sstevel@tonic-gate 	 */
709*7c478bd9Sstevel@tonic-gate 	ioctl(1, I_FLUSH, FLUSHW);
710*7c478bd9Sstevel@tonic-gate 	exit (E_SUCCESS);
711*7c478bd9Sstevel@tonic-gate }
712*7c478bd9Sstevel@tonic-gate 
713*7c478bd9Sstevel@tonic-gate /**
714*7c478bd9Sstevel@tonic-gate  ** baudrate() - RETURN BAUD RATE OF OUTPUT LINE
715*7c478bd9Sstevel@tonic-gate  **/
716*7c478bd9Sstevel@tonic-gate 
717*7c478bd9Sstevel@tonic-gate static int		baud_convert[] =
718*7c478bd9Sstevel@tonic-gate {
719*7c478bd9Sstevel@tonic-gate 	0, 50, 75, 110, 135, 150, 200, 300, 600, 1200,
720*7c478bd9Sstevel@tonic-gate 	1800, 2400, 4800, 9600, 19200, 38400, 57600,
721*7c478bd9Sstevel@tonic-gate 	76800, 115200, 153600, 230400, 307200, 460800
722*7c478bd9Sstevel@tonic-gate };
723*7c478bd9Sstevel@tonic-gate 
724*7c478bd9Sstevel@tonic-gate int			baudrate ()
725*7c478bd9Sstevel@tonic-gate {
726*7c478bd9Sstevel@tonic-gate 	struct termio		tm;
727*7c478bd9Sstevel@tonic-gate 	struct termios		tms;
728*7c478bd9Sstevel@tonic-gate 	int			speed;
729*7c478bd9Sstevel@tonic-gate 
730*7c478bd9Sstevel@tonic-gate 	if (ioctl(1, TCGETS, &tms) < 0) {
731*7c478bd9Sstevel@tonic-gate 		if (ioctl(1, TCGETA, &tm) < 0)
732*7c478bd9Sstevel@tonic-gate 			return (1200);
733*7c478bd9Sstevel@tonic-gate 		else
734*7c478bd9Sstevel@tonic-gate 			speed = tm.c_cflag&CBAUD;
735*7c478bd9Sstevel@tonic-gate 	} else
736*7c478bd9Sstevel@tonic-gate 		speed = cfgetospeed(&tms);
737*7c478bd9Sstevel@tonic-gate 
738*7c478bd9Sstevel@tonic-gate 	return (speed ? baud_convert[speed] : 1200);
739*7c478bd9Sstevel@tonic-gate }
740