xref: /titanic_52/usr/src/cmd/lp/model/lp.tell.c (revision f928ce67ef743c33ea27c573c9c7e2d4a4833cbd)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * 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*f928ce67Sceastha  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
287c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
297c478bd9Sstevel@tonic-gate 
30*f928ce67Sceastha #pragma ident	"%Z%%M%	%I%	%E% SMI"
317c478bd9Sstevel@tonic-gate 
327c478bd9Sstevel@tonic-gate #include "signal.h"
337c478bd9Sstevel@tonic-gate #include "stdio.h"
347c478bd9Sstevel@tonic-gate #include "errno.h"
357c478bd9Sstevel@tonic-gate 
367c478bd9Sstevel@tonic-gate #include "lp.h"
377c478bd9Sstevel@tonic-gate #include "msgs.h"
387c478bd9Sstevel@tonic-gate #include "string.h"
397c478bd9Sstevel@tonic-gate 
407c478bd9Sstevel@tonic-gate void			startup(),
417c478bd9Sstevel@tonic-gate 			cleanup(),
427c478bd9Sstevel@tonic-gate 			done();
437c478bd9Sstevel@tonic-gate 
447c478bd9Sstevel@tonic-gate extern char		*getenv(),
457c478bd9Sstevel@tonic-gate 			*malloc(),
467c478bd9Sstevel@tonic-gate 			*realloc();
477c478bd9Sstevel@tonic-gate 
487c478bd9Sstevel@tonic-gate extern long		atol();
497c478bd9Sstevel@tonic-gate 
507c478bd9Sstevel@tonic-gate extern int		atoi();
517c478bd9Sstevel@tonic-gate 
527c478bd9Sstevel@tonic-gate static void		wakeup();
537c478bd9Sstevel@tonic-gate extern char    *optarg;
547c478bd9Sstevel@tonic-gate extern int     optind, opterr, optopt;
557c478bd9Sstevel@tonic-gate int optsw;
567c478bd9Sstevel@tonic-gate 
577c478bd9Sstevel@tonic-gate #define PREFIX_STRING "%%["
587c478bd9Sstevel@tonic-gate #define SUFFIX_STRING "]%%"
597c478bd9Sstevel@tonic-gate #define PRINTER_ERROR_STRING "PrinterError:"
607c478bd9Sstevel@tonic-gate #define STATUS_STRING "status:"
617c478bd9Sstevel@tonic-gate #define JOB_STRING "job:"
627c478bd9Sstevel@tonic-gate #define STATUS_OK_STRING "ready and printing"
637c478bd9Sstevel@tonic-gate #define PAPER_CHANGED_STRING "paper changed:"
647c478bd9Sstevel@tonic-gate 
657c478bd9Sstevel@tonic-gate /*
667c478bd9Sstevel@tonic-gate  * Some common postscript printer fault messages.
677c478bd9Sstevel@tonic-gate  * These strings are here so that they get l10ned and then lpstat will
687c478bd9Sstevel@tonic-gate  * be able to display them in the users language.
697c478bd9Sstevel@tonic-gate  * This seemed like a good place for them, since lp.tell knows about
707c478bd9Sstevel@tonic-gate  * postscript msgs.
717c478bd9Sstevel@tonic-gate  */
727c478bd9Sstevel@tonic-gate 
737c478bd9Sstevel@tonic-gate char *ps_m1 = "unable to print: out of media (paper)";
747c478bd9Sstevel@tonic-gate char *ps_m2 = "out of media (paper)";
757c478bd9Sstevel@tonic-gate char *ps_m3 = "unable to print: tray not (properly) installed";
767c478bd9Sstevel@tonic-gate char *ps_m4 = "tray not (properly) installed";
777c478bd9Sstevel@tonic-gate char *ps_m5 = "unable to print: paper out for the selected tray";
787c478bd9Sstevel@tonic-gate char *ps_m6 = "paper out for the selected tray";
797c478bd9Sstevel@tonic-gate char *ps_m7 = "unable to print: cartridge life expiring";
807c478bd9Sstevel@tonic-gate char *ps_m8 = "cartridge life expiring";
817c478bd9Sstevel@tonic-gate char *ps_m9 = "unable to print: printer cover not locked";
827c478bd9Sstevel@tonic-gate char *ps_m10 = "printer cover not locked";
837c478bd9Sstevel@tonic-gate char *ps_m11 = "unable to print: media (paper) jam in exit path";
847c478bd9Sstevel@tonic-gate char *ps_m12 = "media (paper) jam in exit path";
857c478bd9Sstevel@tonic-gate char *ps_m13 = "unable to print: media (paper) jam in feed path";
867c478bd9Sstevel@tonic-gate char *ps_m14 = "media (paper) jam in feed path";
877c478bd9Sstevel@tonic-gate char *ps_m15 = "unable to print: drum assembly almost expended";
887c478bd9Sstevel@tonic-gate char *ps_m16 = "drum assembly almost expended";
897c478bd9Sstevel@tonic-gate char *ps_m17 = "unable to print: toner cartridge almost expended";
907c478bd9Sstevel@tonic-gate char *ps_m18 = "toner cartridge almost expended";
917c478bd9Sstevel@tonic-gate char *ps_m19 = "unable to print: drum assembly not (properly) installed";
927c478bd9Sstevel@tonic-gate char *ps_m20 = "drum assembly not (properly) installed";
937c478bd9Sstevel@tonic-gate char *ps_m21 = "unable to print: toner cartridge not (properly) installed";
947c478bd9Sstevel@tonic-gate char *ps_m22 = "toner cartridge not (properly) installed";
957c478bd9Sstevel@tonic-gate char *ps_m23 = "unable to print: drum assembly requires replacement";
967c478bd9Sstevel@tonic-gate char *ps_m24 = "drum assembly requires replacement";
977c478bd9Sstevel@tonic-gate char *ps_m25 = "unable to print: toner cartridge requires replacement";
987c478bd9Sstevel@tonic-gate char *ps_m26 = "toner cartridge requires replacement";
997c478bd9Sstevel@tonic-gate char *ps_m27 = "unable to print: fuser warming up";
1007c478bd9Sstevel@tonic-gate char *ps_m28 = "fuser warming up";
1017c478bd9Sstevel@tonic-gate char *ps_m29 = "unable to print: printer not responding";
1027c478bd9Sstevel@tonic-gate char *ps_m30 = "printer not responding";
1037c478bd9Sstevel@tonic-gate char *ps_m31 = "unable to print: fuser pausing";
1047c478bd9Sstevel@tonic-gate char *ps_m32 = "fuser pausing";
1057c478bd9Sstevel@tonic-gate char *ps_m33 = "unable to print: printer turned off";
1067c478bd9Sstevel@tonic-gate char *ps_m34 = "printer turned off";
1077c478bd9Sstevel@tonic-gate char *ps_m35 = "unable to print: printer warming up";
1087c478bd9Sstevel@tonic-gate char *ps_m36 = "printer warming up";
1097c478bd9Sstevel@tonic-gate char *ps_m37 = "unable to print: interlock open";
1107c478bd9Sstevel@tonic-gate char *ps_m38 = "interlock open";
1117c478bd9Sstevel@tonic-gate char *ps_m39 = "unable to print: selected tray out";
1127c478bd9Sstevel@tonic-gate char *ps_m40 = "selected tray out";
1137c478bd9Sstevel@tonic-gate char *ps_m41 = "unable to print: paper out for the manual tray";
1147c478bd9Sstevel@tonic-gate char *ps_m42 = "paper out for the manual tray";
1157c478bd9Sstevel@tonic-gate char *ps_m43 = "unable to print: paper exit jam";
1167c478bd9Sstevel@tonic-gate char *ps_m44 = "paper exit jam";
1177c478bd9Sstevel@tonic-gate char *ps_m45 = "unable to print: paper misfeed jam";
1187c478bd9Sstevel@tonic-gate char *ps_m46 = "paper misfeed jam";
1197c478bd9Sstevel@tonic-gate char *ps_m47 = "unable to print: paper jam between registration & heat rollers";
1207c478bd9Sstevel@tonic-gate char *ps_m48 = "paper jam between registration & heat rollers";
1217c478bd9Sstevel@tonic-gate char *ps_m49 = "unable to print: paper jam at registration roller";
1227c478bd9Sstevel@tonic-gate char *ps_m50 = "paper jam at registration roller";
1237c478bd9Sstevel@tonic-gate char *ps_m51 = "unable to print: no cartridge";
1247c478bd9Sstevel@tonic-gate char *ps_m52 = "no cartridge";
1257c478bd9Sstevel@tonic-gate char *ps_m53 = "unable to print: cartridge out";
1267c478bd9Sstevel@tonic-gate char *ps_m54 = "cartridge out";
1277c478bd9Sstevel@tonic-gate 
1287c478bd9Sstevel@tonic-gate /**
1297c478bd9Sstevel@tonic-gate  ** main()
1307c478bd9Sstevel@tonic-gate  **/
1317c478bd9Sstevel@tonic-gate 
132*f928ce67Sceastha int
133*f928ce67Sceastha main(int argc, char *argv[])
1347c478bd9Sstevel@tonic-gate {
1357c478bd9Sstevel@tonic-gate 	char			*alert_text,
1367c478bd9Sstevel@tonic-gate 				buf[BUFSIZ],
1377c478bd9Sstevel@tonic-gate 				msgbuf[MSGMAX],
1387c478bd9Sstevel@tonic-gate 				*bufPtr,
1397c478bd9Sstevel@tonic-gate 				*printer,
1407c478bd9Sstevel@tonic-gate 				*s_key;
1417c478bd9Sstevel@tonic-gate 
1427c478bd9Sstevel@tonic-gate 	char *printerErrorString = NULL;
1437c478bd9Sstevel@tonic-gate 	char *statusString = NULL;
1447c478bd9Sstevel@tonic-gate 	char *paperChangedString = NULL;
1457c478bd9Sstevel@tonic-gate 	char *suffixString = NULL;
1467c478bd9Sstevel@tonic-gate 	char *jobString = NULL;
1477c478bd9Sstevel@tonic-gate 	char *prefixString = NULL;
1487c478bd9Sstevel@tonic-gate 	char *statusOkString = NULL;
1497c478bd9Sstevel@tonic-gate 	int			mtype,
1507c478bd9Sstevel@tonic-gate                doStdOut,
1517c478bd9Sstevel@tonic-gate 					doDebug,
1527c478bd9Sstevel@tonic-gate 					first,
1537c478bd9Sstevel@tonic-gate 				oldalarm;
1547c478bd9Sstevel@tonic-gate 
1557c478bd9Sstevel@tonic-gate 
1567c478bd9Sstevel@tonic-gate 	short			status;
1577c478bd9Sstevel@tonic-gate 
1587c478bd9Sstevel@tonic-gate 	long			key,clearKey;
1597c478bd9Sstevel@tonic-gate 	char *ptr1,*ptr2,*ptr3,*ptr4,*ptr5;
1607c478bd9Sstevel@tonic-gate 	int trayNum = 0;
1617c478bd9Sstevel@tonic-gate 	int mode = 0;
1627c478bd9Sstevel@tonic-gate 	int pagesPrinted = 0;
1637c478bd9Sstevel@tonic-gate 	char *paperType = NULL;
1647c478bd9Sstevel@tonic-gate 	short mesgRetType;
1657c478bd9Sstevel@tonic-gate 	int useLaserWriterMessages;
1667c478bd9Sstevel@tonic-gate 	int pLen,sLen,peLen,jLen,pcLen ;
1677c478bd9Sstevel@tonic-gate 
1687c478bd9Sstevel@tonic-gate 	void			(*oldsignal)();
1697c478bd9Sstevel@tonic-gate 
1707c478bd9Sstevel@tonic-gate 
1717c478bd9Sstevel@tonic-gate 	/*
1727c478bd9Sstevel@tonic-gate 	 * Run immune from typical interruptions, so that
1737c478bd9Sstevel@tonic-gate 	 * we stand a chance to get the fault message.
1747c478bd9Sstevel@tonic-gate 	 * EOF (or startup error) is the only way out.
1757c478bd9Sstevel@tonic-gate 	 */
1767c478bd9Sstevel@tonic-gate 	signal (SIGHUP, SIG_IGN);
1777c478bd9Sstevel@tonic-gate 	signal (SIGINT, SIG_IGN);
1787c478bd9Sstevel@tonic-gate 	signal (SIGQUIT, SIG_IGN);
1797c478bd9Sstevel@tonic-gate 	signal (SIGTERM, SIG_IGN);
1807c478bd9Sstevel@tonic-gate 
1817c478bd9Sstevel@tonic-gate 	/*
1827c478bd9Sstevel@tonic-gate 	 *  Do we have a key?
1837c478bd9Sstevel@tonic-gate 	 */
1847c478bd9Sstevel@tonic-gate 	if (
1857c478bd9Sstevel@tonic-gate 		argc < 2
1867c478bd9Sstevel@tonic-gate 	     || !(s_key = getenv("SPOOLER_KEY"))
1877c478bd9Sstevel@tonic-gate 	     || !*s_key
1887c478bd9Sstevel@tonic-gate 	     || (key = atol(s_key)) <= 0
1897c478bd9Sstevel@tonic-gate 	) {
1907c478bd9Sstevel@tonic-gate 		printf( "Usage: lptell [-lodk] [-X String]  printer\n");
1917c478bd9Sstevel@tonic-gate 		printf("Options (where X is P,S,e,s, O or c )\n");
1927c478bd9Sstevel@tonic-gate 		printf("   environment variable SPOOLER_KEY: must be defined and > 0\n");
1937c478bd9Sstevel@tonic-gate 		printf("   printer: name of printer to give status for.\n");
1947c478bd9Sstevel@tonic-gate 		printf("   -l: expect laser writer type messages (NeWSprint does)\n");
1957c478bd9Sstevel@tonic-gate 		printf("   -o: send input to stdout\n");
1967c478bd9Sstevel@tonic-gate 		printf("   -d: send additional debugging output to stdout\n");
1977c478bd9Sstevel@tonic-gate 		printf("   -P String: string for prefix, default: '%%%%['\n");
1987c478bd9Sstevel@tonic-gate 		printf("   -S String: string for suffix, default: ']%%%%'\n");
1997c478bd9Sstevel@tonic-gate 		printf("   -e String: string to detect printer error,\n");
2007c478bd9Sstevel@tonic-gate 		printf("       default: 'PrinterError:', send S_SEND_FAULT to lpsched\n");
2017c478bd9Sstevel@tonic-gate 		printf(
2027c478bd9Sstevel@tonic-gate 		"   -c String: string to detect paper change in context of printer error,\n");
2037c478bd9Sstevel@tonic-gate 		printf("       default: 'paper changed:', send S_PAPER_CHANGED to lpsched\n");
2047c478bd9Sstevel@tonic-gate 		printf("   -s String: string to detect printer ok status, \n");
2057c478bd9Sstevel@tonic-gate 		printf("       default: 'status:', send S_CLEAR_FAULT to lpsched\n");
2067c478bd9Sstevel@tonic-gate 		printf("       -k: do not use the key for making status ok\n");
2077c478bd9Sstevel@tonic-gate 		printf("   -O String: string sent as status message to lpsched,\n");
2087c478bd9Sstevel@tonic-gate 		printf("                  default: 'ready and printing:'\n");
2097c478bd9Sstevel@tonic-gate 		exit (90);
2107c478bd9Sstevel@tonic-gate 		  }
2117c478bd9Sstevel@tonic-gate 
2127c478bd9Sstevel@tonic-gate 
2137c478bd9Sstevel@tonic-gate 	doStdOut = 0;
2147c478bd9Sstevel@tonic-gate 	doDebug = 0;
2157c478bd9Sstevel@tonic-gate 	useLaserWriterMessages = 0;
2167c478bd9Sstevel@tonic-gate 	clearKey = key;
2177c478bd9Sstevel@tonic-gate 
2187c478bd9Sstevel@tonic-gate 	prefixString = PREFIX_STRING; pLen = strlen(prefixString);
2197c478bd9Sstevel@tonic-gate 	suffixString = SUFFIX_STRING;
2207c478bd9Sstevel@tonic-gate 	printerErrorString = PRINTER_ERROR_STRING;
2217c478bd9Sstevel@tonic-gate 	  peLen = strlen(printerErrorString);
2227c478bd9Sstevel@tonic-gate 	statusString = STATUS_STRING; sLen = strlen(statusString);
2237c478bd9Sstevel@tonic-gate 	jobString = JOB_STRING; jLen = strlen(jobString);
2247c478bd9Sstevel@tonic-gate 	paperChangedString = PAPER_CHANGED_STRING;
2257c478bd9Sstevel@tonic-gate 	  pcLen = strlen(paperChangedString);
2267c478bd9Sstevel@tonic-gate 	statusOkString = STATUS_OK_STRING;
2277c478bd9Sstevel@tonic-gate 
2287c478bd9Sstevel@tonic-gate    while ((optsw = getopt(argc, argv, "le:s:c:okdO:S:P:")) != EOF) {
2297c478bd9Sstevel@tonic-gate 		switch ( optsw ) {
2307c478bd9Sstevel@tonic-gate 			case 'l':
2317c478bd9Sstevel@tonic-gate 				useLaserWriterMessages = 1;
2327c478bd9Sstevel@tonic-gate 				break;
2337c478bd9Sstevel@tonic-gate 			case 'P':
2347c478bd9Sstevel@tonic-gate 				prefixString = (optarg  ? strdup(optarg) : NULL);
2357c478bd9Sstevel@tonic-gate 				pLen = strlen(prefixString );
2367c478bd9Sstevel@tonic-gate 				break;
2377c478bd9Sstevel@tonic-gate 			case 'S':
2387c478bd9Sstevel@tonic-gate 				suffixString = (optarg  ? strdup(optarg) : NULL);
2397c478bd9Sstevel@tonic-gate 				break;
2407c478bd9Sstevel@tonic-gate 			case 'e':
2417c478bd9Sstevel@tonic-gate 				printerErrorString = (optarg  ? strdup(optarg) : NULL);
2427c478bd9Sstevel@tonic-gate 				peLen = strlen(printerErrorString);
2437c478bd9Sstevel@tonic-gate 				break;
2447c478bd9Sstevel@tonic-gate 			case 's':
2457c478bd9Sstevel@tonic-gate 				statusString = (optarg  ? strdup(optarg) : NULL);
2467c478bd9Sstevel@tonic-gate 				sLen = strlen(statusString);
2477c478bd9Sstevel@tonic-gate 				break;
2487c478bd9Sstevel@tonic-gate 			case 'O':
2497c478bd9Sstevel@tonic-gate 				statusOkString = (optarg  ? strdup(optarg) : NULL);
2507c478bd9Sstevel@tonic-gate 				break;
2517c478bd9Sstevel@tonic-gate 			case 'c':
2527c478bd9Sstevel@tonic-gate 				paperChangedString = (optarg  ? strdup(optarg) : NULL);
2537c478bd9Sstevel@tonic-gate 				pcLen = strlen(paperChangedString );
2547c478bd9Sstevel@tonic-gate 				break;
2557c478bd9Sstevel@tonic-gate 			case 'k':
2567c478bd9Sstevel@tonic-gate 				clearKey = -1;
2577c478bd9Sstevel@tonic-gate 				break;
2587c478bd9Sstevel@tonic-gate 			case 'o':
2597c478bd9Sstevel@tonic-gate 				doStdOut = 1;
2607c478bd9Sstevel@tonic-gate 				break;
2617c478bd9Sstevel@tonic-gate 			case 'd':
2627c478bd9Sstevel@tonic-gate 				doDebug = 1;
2637c478bd9Sstevel@tonic-gate 				break;
2647c478bd9Sstevel@tonic-gate 			}
2657c478bd9Sstevel@tonic-gate 		}
2667c478bd9Sstevel@tonic-gate 	/*
2677c478bd9Sstevel@tonic-gate 	 * Which printer is this? Do we have a key?
2687c478bd9Sstevel@tonic-gate 	 */
2697c478bd9Sstevel@tonic-gate 	if (
2707c478bd9Sstevel@tonic-gate 	     !(printer = argv[optind])
2717c478bd9Sstevel@tonic-gate 	     || !*printer
2727c478bd9Sstevel@tonic-gate 	) {
2737c478bd9Sstevel@tonic-gate 		exit (90);
2747c478bd9Sstevel@tonic-gate 		  }
2757c478bd9Sstevel@tonic-gate 	if (doDebug) {
2767c478bd9Sstevel@tonic-gate 		printf( "start lp.tell for %s key %d mode %s %s\n",
2777c478bd9Sstevel@tonic-gate 				printer,key,(useLaserWriterMessages ? "LW" : "standard"),
2787c478bd9Sstevel@tonic-gate 				(doStdOut ? "doStdOut" : "no output"));
2797c478bd9Sstevel@tonic-gate 		printf( "prefix (%s) suffix (%s) printerError (%s)\n",
2807c478bd9Sstevel@tonic-gate 			  prefixString,suffixString,printerErrorString);
2817c478bd9Sstevel@tonic-gate 		printf( "paper_changed (%s) status (%s) key %d \n",
2827c478bd9Sstevel@tonic-gate 			paperChangedString,statusString , clearKey);
2837c478bd9Sstevel@tonic-gate 		fflush(stdout);
2847c478bd9Sstevel@tonic-gate 	   }
2857c478bd9Sstevel@tonic-gate 	/*
2867c478bd9Sstevel@tonic-gate 	 * Wait for a message on the standard input. When a single line
2877c478bd9Sstevel@tonic-gate 	 * comes in, take a couple of more seconds to get any other lines
2887c478bd9Sstevel@tonic-gate 	 * that may be ready, then send them to the Spooler.
2897c478bd9Sstevel@tonic-gate 	 */
2907c478bd9Sstevel@tonic-gate 	while (fgets(buf, BUFSIZ, stdin)) {
2917c478bd9Sstevel@tonic-gate 		if (useLaserWriterMessages) {
2927c478bd9Sstevel@tonic-gate 			/* NeWSprint style processing (which simulates the LaserWriter
2937c478bd9Sstevel@tonic-gate 			 *There are four types of messages:
2947c478bd9Sstevel@tonic-gate 			 * 	1) fault messages: printer error message from handler
2957c478bd9Sstevel@tonic-gate 			 * 	2) clear fault messages: printer ok messages from handler
2967c478bd9Sstevel@tonic-gate 			 * 	3) paper changed messages: printer handler detected paper change
2977c478bd9Sstevel@tonic-gate 			 * 	4) server messages: xnews problems
2987c478bd9Sstevel@tonic-gate 			 */
2997c478bd9Sstevel@tonic-gate 			bufPtr = buf;
3007c478bd9Sstevel@tonic-gate 			if (strncmp(prefixString, bufPtr, pLen) == 0) {
3017c478bd9Sstevel@tonic-gate 				bufPtr += pLen;
3027c478bd9Sstevel@tonic-gate 				while (*bufPtr == ' ')
3037c478bd9Sstevel@tonic-gate 					bufPtr++;
3047c478bd9Sstevel@tonic-gate 
3057c478bd9Sstevel@tonic-gate 				if (strncmp(printerErrorString, bufPtr,
3067c478bd9Sstevel@tonic-gate 						peLen) == 0) {
3077c478bd9Sstevel@tonic-gate 					bufPtr += peLen;
3087c478bd9Sstevel@tonic-gate 					while (*bufPtr == ' ')
3097c478bd9Sstevel@tonic-gate 						bufPtr++;
3107c478bd9Sstevel@tonic-gate 
3117c478bd9Sstevel@tonic-gate 					if ((strncmp(bufPtr,paperChangedString,pcLen) == 0) &&
3127c478bd9Sstevel@tonic-gate 							 (ptr1 = bufPtr +pcLen) &&
3137c478bd9Sstevel@tonic-gate 							 (ptr2 = strchr(ptr1+1,':')) &&
3147c478bd9Sstevel@tonic-gate 							 (ptr3 = strchr(ptr2+1,':')) &&
3157c478bd9Sstevel@tonic-gate 							 (ptr4 = strchr(ptr3+1,':')) &&
3167c478bd9Sstevel@tonic-gate 							 (ptr5 = strchr(ptr4+1,'\n'))) {
3177c478bd9Sstevel@tonic-gate 						if (doStdOut) printf("%s",buf);
3187c478bd9Sstevel@tonic-gate 						*ptr2 =0;
3197c478bd9Sstevel@tonic-gate 						*ptr3= 0;
3207c478bd9Sstevel@tonic-gate 						*ptr4= 0;
3217c478bd9Sstevel@tonic-gate 						*ptr5= 0;
3227c478bd9Sstevel@tonic-gate 						trayNum = atoi(ptr1+1);
3237c478bd9Sstevel@tonic-gate 						paperType = ptr2+1;
3247c478bd9Sstevel@tonic-gate 						mode = atoi(ptr3+1);
3257c478bd9Sstevel@tonic-gate 						pagesPrinted = atoi(ptr4+1);
3267c478bd9Sstevel@tonic-gate 						if (doDebug) {
3277c478bd9Sstevel@tonic-gate 							 printf("Paper changed: %s tray %d paper %s md %d pages %d\n",
3287c478bd9Sstevel@tonic-gate 								printer,trayNum,paperType,mode,pagesPrinted);
3297c478bd9Sstevel@tonic-gate 							}
3307c478bd9Sstevel@tonic-gate 						startup ();
3317c478bd9Sstevel@tonic-gate 						mesgRetType = R_PAPER_CHANGED;
3327c478bd9Sstevel@tonic-gate 						(void)putmessage ( msgbuf, S_PAPER_CHANGED, printer, trayNum,
3337c478bd9Sstevel@tonic-gate 							paperType, mode, pagesPrinted);
3347c478bd9Sstevel@tonic-gate 					} else {
3357c478bd9Sstevel@tonic-gate 						if (doStdOut)  printf("%s",buf);
3367c478bd9Sstevel@tonic-gate 						if (ptr1 = strstr(bufPtr,suffixString))  *ptr1 = 0;
3377c478bd9Sstevel@tonic-gate 						if ( doDebug ) {
3387c478bd9Sstevel@tonic-gate 							printf("Send fault: %s key %d (%s)\n",printer,key,bufPtr);
3397c478bd9Sstevel@tonic-gate 						}
3407c478bd9Sstevel@tonic-gate 						mesgRetType = R_SEND_FAULT;
3417c478bd9Sstevel@tonic-gate 						startup ();
3427c478bd9Sstevel@tonic-gate 						(void)putmessage (msgbuf,S_SEND_FAULT,printer,key,bufPtr);
3437c478bd9Sstevel@tonic-gate 					}
3447c478bd9Sstevel@tonic-gate 				} else if ((first = (strncmp(statusString,bufPtr,sLen) == 0)) ||
3457c478bd9Sstevel@tonic-gate 							 (strncmp(jobString,bufPtr,jLen) == 0)) {
3467c478bd9Sstevel@tonic-gate 					bufPtr += (first ? sLen : jLen);
3477c478bd9Sstevel@tonic-gate 					if (doStdOut)  printf("%s",buf);
3487c478bd9Sstevel@tonic-gate 					if (ptr1 = strstr(bufPtr,suffixString))  *ptr1 = 0;
3497c478bd9Sstevel@tonic-gate 					if ( doDebug ) {
3507c478bd9Sstevel@tonic-gate 						printf("Clear fault: %s key %d (%s)\n",printer, clearKey,
3517c478bd9Sstevel@tonic-gate 							  bufPtr);
3527c478bd9Sstevel@tonic-gate 						}
3537c478bd9Sstevel@tonic-gate 					mesgRetType = R_CLEAR_FAULT;
3547c478bd9Sstevel@tonic-gate 					startup ();
3557c478bd9Sstevel@tonic-gate 					(void)putmessage( msgbuf,S_CLEAR_FAULT,printer,clearKey,
3567c478bd9Sstevel@tonic-gate 						statusOkString);
3577c478bd9Sstevel@tonic-gate 				} else {
3587c478bd9Sstevel@tonic-gate 					if (doStdOut)  printf("%s",buf);
3597c478bd9Sstevel@tonic-gate 					if (ptr1 = strstr(bufPtr,suffixString))  *ptr1 = 0;
3607c478bd9Sstevel@tonic-gate 					if ( doDebug ) {
3617c478bd9Sstevel@tonic-gate 						printf("Server error: %s key %d (%s)\n",printer,key,
3627c478bd9Sstevel@tonic-gate 							 buf);
3637c478bd9Sstevel@tonic-gate 					}
3647c478bd9Sstevel@tonic-gate 					mesgRetType = 0;
3657c478bd9Sstevel@tonic-gate 				}
3667c478bd9Sstevel@tonic-gate 			} else {
3677c478bd9Sstevel@tonic-gate 				if (doStdOut) printf("%s",buf);
3687c478bd9Sstevel@tonic-gate 				if (ptr1 = strstr(bufPtr,suffixString))
3697c478bd9Sstevel@tonic-gate 					*ptr1 = 0;
3707c478bd9Sstevel@tonic-gate 				if (doDebug) {
3717c478bd9Sstevel@tonic-gate 					printf("Server error: %s key %d (%s)\n",
3727c478bd9Sstevel@tonic-gate 						printer, key, buf);
3737c478bd9Sstevel@tonic-gate 				}
3747c478bd9Sstevel@tonic-gate 				mesgRetType = 0;
3757c478bd9Sstevel@tonic-gate 			}
3767c478bd9Sstevel@tonic-gate 		} else {	/* not generic PostScript style messages */
3777c478bd9Sstevel@tonic-gate 			oldsignal = signal(SIGALRM, wakeup);
3787c478bd9Sstevel@tonic-gate 			oldalarm = alarm(2);
3797c478bd9Sstevel@tonic-gate 
3807c478bd9Sstevel@tonic-gate 			alert_text = 0;
3817c478bd9Sstevel@tonic-gate 			do {
3827c478bd9Sstevel@tonic-gate 				if (alert_text)
3837c478bd9Sstevel@tonic-gate 					alert_text = realloc(alert_text,
3847c478bd9Sstevel@tonic-gate 						strlen(alert_text)+strlen(buf)+1
3857c478bd9Sstevel@tonic-gate 					);
3867c478bd9Sstevel@tonic-gate 				else {
3877c478bd9Sstevel@tonic-gate 					alert_text = malloc(strlen(buf) + 1);
3887c478bd9Sstevel@tonic-gate 					alert_text[0] = 0;
3897c478bd9Sstevel@tonic-gate 				}
3907c478bd9Sstevel@tonic-gate 				strcat (alert_text, buf);
3917c478bd9Sstevel@tonic-gate 
3927c478bd9Sstevel@tonic-gate 			} while (fgets(buf, BUFSIZ, stdin));
3937c478bd9Sstevel@tonic-gate 
3947c478bd9Sstevel@tonic-gate 			alarm (oldalarm);
3957c478bd9Sstevel@tonic-gate 			signal (SIGALRM, oldsignal);
3967c478bd9Sstevel@tonic-gate 
3977c478bd9Sstevel@tonic-gate 			if (doStdOut) {
3987c478bd9Sstevel@tonic-gate 				if ( doDebug ) {
3997c478bd9Sstevel@tonic-gate 					printf("Send generic fault: %s key %d (%s)\n",printer,key,
4007c478bd9Sstevel@tonic-gate 						alert_text);
4017c478bd9Sstevel@tonic-gate 					}
4027c478bd9Sstevel@tonic-gate 				else {
4037c478bd9Sstevel@tonic-gate 					printf("%s\n",alert_text);
4047c478bd9Sstevel@tonic-gate 					}
4057c478bd9Sstevel@tonic-gate 				}
4067c478bd9Sstevel@tonic-gate 			if (strcmp(alert_text, "printer ok\n") == 0) {
4077c478bd9Sstevel@tonic-gate 				mesgRetType = R_CLEAR_FAULT;
4087c478bd9Sstevel@tonic-gate 				startup ();
4097c478bd9Sstevel@tonic-gate 				(void)putmessage(msgbuf, S_CLEAR_FAULT, printer,
4107c478bd9Sstevel@tonic-gate 						clearKey, statusOkString);
4117c478bd9Sstevel@tonic-gate 			} else {
4127c478bd9Sstevel@tonic-gate          			mesgRetType = R_SEND_FAULT;
4137c478bd9Sstevel@tonic-gate 				startup ();
4147c478bd9Sstevel@tonic-gate 				(void)putmessage(msgbuf, S_SEND_FAULT, printer,
4157c478bd9Sstevel@tonic-gate 						key, alert_text);
4167c478bd9Sstevel@tonic-gate 			}
4177c478bd9Sstevel@tonic-gate 		}
4187c478bd9Sstevel@tonic-gate 
4197c478bd9Sstevel@tonic-gate 		if (mesgRetType) {
4207c478bd9Sstevel@tonic-gate 			if (msend(msgbuf) == -1)
4217c478bd9Sstevel@tonic-gate 				done (91);
4227c478bd9Sstevel@tonic-gate 			if (mrecv(msgbuf, sizeof(msgbuf)) == -1)
4237c478bd9Sstevel@tonic-gate 				done (92);
4247c478bd9Sstevel@tonic-gate 			mtype = getmessage(msgbuf, mesgRetType, &status);
4257c478bd9Sstevel@tonic-gate 			/*
4267c478bd9Sstevel@tonic-gate 			 * check for R_CLEAR_FAULT here and 3 lines below
4277c478bd9Sstevel@tonic-gate 			 * because older lpsched doesn't pass S_CLEAR_FAULT
4287c478bd9Sstevel@tonic-gate 			 */
4297c478bd9Sstevel@tonic-gate 			if ((mtype != mesgRetType) &&
4307c478bd9Sstevel@tonic-gate 			    (mesgRetType != R_CLEAR_FAULT))
4317c478bd9Sstevel@tonic-gate 				done (93);
4327c478bd9Sstevel@tonic-gate 
4337c478bd9Sstevel@tonic-gate 			if ((status != MOK) && (mesgRetType != R_CLEAR_FAULT))
4347c478bd9Sstevel@tonic-gate 				done (94);
4357c478bd9Sstevel@tonic-gate 		}
4367c478bd9Sstevel@tonic-gate 
4377c478bd9Sstevel@tonic-gate 	}
4387c478bd9Sstevel@tonic-gate 	done (0);
439*f928ce67Sceastha 
440*f928ce67Sceastha 	return (0);
4417c478bd9Sstevel@tonic-gate }
4427c478bd9Sstevel@tonic-gate 
4437c478bd9Sstevel@tonic-gate /**
4447c478bd9Sstevel@tonic-gate  ** startup() - OPEN MESSAGE QUEUE TO SPOOLER
4457c478bd9Sstevel@tonic-gate  ** cleanup() - CLOSE THE MESSAGE QUEUE TO THE SPOOLER
4467c478bd9Sstevel@tonic-gate  **/
4477c478bd9Sstevel@tonic-gate 
4487c478bd9Sstevel@tonic-gate static int		have_contacted_spooler	= 0;
4497c478bd9Sstevel@tonic-gate 
4507c478bd9Sstevel@tonic-gate void			startup ()
4517c478bd9Sstevel@tonic-gate {
4527c478bd9Sstevel@tonic-gate 	void			catch();
4537c478bd9Sstevel@tonic-gate 
4547c478bd9Sstevel@tonic-gate 	/*
4557c478bd9Sstevel@tonic-gate 	 * Open a message queue to the Spooler.
4567c478bd9Sstevel@tonic-gate 	 * An error is deadly.
4577c478bd9Sstevel@tonic-gate 	 */
4587c478bd9Sstevel@tonic-gate 	if (!have_contacted_spooler) {
4597c478bd9Sstevel@tonic-gate 		if (mopen() == -1) {
4607c478bd9Sstevel@tonic-gate 
4617c478bd9Sstevel@tonic-gate 			switch (errno) {
4627c478bd9Sstevel@tonic-gate 			case ENOMEM:
4637c478bd9Sstevel@tonic-gate 			case ENOSPC:
4647c478bd9Sstevel@tonic-gate 				break;
4657c478bd9Sstevel@tonic-gate 			default:
4667c478bd9Sstevel@tonic-gate 				break;
4677c478bd9Sstevel@tonic-gate 			}
4687c478bd9Sstevel@tonic-gate 
4697c478bd9Sstevel@tonic-gate 			exit (1);
4707c478bd9Sstevel@tonic-gate 		}
4717c478bd9Sstevel@tonic-gate 		have_contacted_spooler = 1;
4727c478bd9Sstevel@tonic-gate 	}
4737c478bd9Sstevel@tonic-gate 	return;
4747c478bd9Sstevel@tonic-gate }
4757c478bd9Sstevel@tonic-gate 
4767c478bd9Sstevel@tonic-gate void			cleanup ()
4777c478bd9Sstevel@tonic-gate {
4787c478bd9Sstevel@tonic-gate 	if (have_contacted_spooler)
4797c478bd9Sstevel@tonic-gate 		mclose ();
4807c478bd9Sstevel@tonic-gate 	return;
4817c478bd9Sstevel@tonic-gate }
4827c478bd9Sstevel@tonic-gate 
4837c478bd9Sstevel@tonic-gate /**
4847c478bd9Sstevel@tonic-gate  ** wakeup() - TRAP ALARM
4857c478bd9Sstevel@tonic-gate  **/
4867c478bd9Sstevel@tonic-gate 
4877c478bd9Sstevel@tonic-gate static void		wakeup ()
4887c478bd9Sstevel@tonic-gate {
4897c478bd9Sstevel@tonic-gate 	return;
4907c478bd9Sstevel@tonic-gate }
4917c478bd9Sstevel@tonic-gate 
4927c478bd9Sstevel@tonic-gate /**
4937c478bd9Sstevel@tonic-gate  ** done() - CLEANUP AND EXIT
4947c478bd9Sstevel@tonic-gate  **/
4957c478bd9Sstevel@tonic-gate 
4967c478bd9Sstevel@tonic-gate void			done (ec)
4977c478bd9Sstevel@tonic-gate 	int			ec;
4987c478bd9Sstevel@tonic-gate {
4997c478bd9Sstevel@tonic-gate 	cleanup ();
5007c478bd9Sstevel@tonic-gate 	exit (ec);
5017c478bd9Sstevel@tonic-gate }
502