4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License, Version 1.0 only
6 * (the "License"). You may not use this file except in compliance
9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 * or http://www.opensolaris.org/os/licensing.
11 * See the License for the specific language governing permissions
12 * and limitations under the License.
14 * When distributing Covered Code, include this CDDL HEADER in each
15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 * If applicable, add the following below this CDDL HEADER, with the
17 * fields enclosed by brackets "[]" replaced with your own identifying
18 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
28 /* All Rights Reserved */
30 #pragma ident "%Z%%M% %I% %E% SMI"
44 extern char *getenv(),
54 extern int optind
, opterr
, optopt
;
57 #define PREFIX_STRING "%%["
58 #define SUFFIX_STRING "]%%"
59 #define PRINTER_ERROR_STRING "PrinterError:"
60 #define STATUS_STRING "status:"
61 #define JOB_STRING "job:"
62 #define STATUS_OK_STRING "ready and printing"
63 #define PAPER_CHANGED_STRING "paper changed:"
66 * Some common postscript printer fault messages.
67 * These strings are here so that they get l10ned and then lpstat will
68 * be able to display them in the users language.
69 * This seemed like a good place for them, since lp.tell knows about
73 char *ps_m1
= "unable to print: out of media (paper)";
74 char *ps_m2
= "out of media (paper)";
75 char *ps_m3
= "unable to print: tray not (properly) installed";
76 char *ps_m4
= "tray not (properly) installed";
77 char *ps_m5
= "unable to print: paper out for the selected tray";
78 char *ps_m6
= "paper out for the selected tray";
79 char *ps_m7
= "unable to print: cartridge life expiring";
80 char *ps_m8
= "cartridge life expiring";
81 char *ps_m9
= "unable to print: printer cover not locked";
82 char *ps_m10
= "printer cover not locked";
83 char *ps_m11
= "unable to print: media (paper) jam in exit path";
84 char *ps_m12
= "media (paper) jam in exit path";
85 char *ps_m13
= "unable to print: media (paper) jam in feed path";
86 char *ps_m14
= "media (paper) jam in feed path";
87 char *ps_m15
= "unable to print: drum assembly almost expended";
88 char *ps_m16
= "drum assembly almost expended";
89 char *ps_m17
= "unable to print: toner cartridge almost expended";
90 char *ps_m18
= "toner cartridge almost expended";
91 char *ps_m19
= "unable to print: drum assembly not (properly) installed";
92 char *ps_m20
= "drum assembly not (properly) installed";
93 char *ps_m21
= "unable to print: toner cartridge not (properly) installed";
94 char *ps_m22
= "toner cartridge not (properly) installed";
95 char *ps_m23
= "unable to print: drum assembly requires replacement";
96 char *ps_m24
= "drum assembly requires replacement";
97 char *ps_m25
= "unable to print: toner cartridge requires replacement";
98 char *ps_m26
= "toner cartridge requires replacement";
99 char *ps_m27
= "unable to print: fuser warming up";
100 char *ps_m28
= "fuser warming up";
101 char *ps_m29
= "unable to print: printer not responding";
102 char *ps_m30
= "printer not responding";
103 char *ps_m31
= "unable to print: fuser pausing";
104 char *ps_m32
= "fuser pausing";
105 char *ps_m33
= "unable to print: printer turned off";
106 char *ps_m34
= "printer turned off";
107 char *ps_m35
= "unable to print: printer warming up";
108 char *ps_m36
= "printer warming up";
109 char *ps_m37
= "unable to print: interlock open";
110 char *ps_m38
= "interlock open";
111 char *ps_m39
= "unable to print: selected tray out";
112 char *ps_m40
= "selected tray out";
113 char *ps_m41
= "unable to print: paper out for the manual tray";
114 char *ps_m42
= "paper out for the manual tray";
115 char *ps_m43
= "unable to print: paper exit jam";
116 char *ps_m44
= "paper exit jam";
117 char *ps_m45
= "unable to print: paper misfeed jam";
118 char *ps_m46
= "paper misfeed jam";
119 char *ps_m47
= "unable to print: paper jam between registration & heat rollers";
120 char *ps_m48
= "paper jam between registration & heat rollers";
121 char *ps_m49
= "unable to print: paper jam at registration roller";
122 char *ps_m50
= "paper jam at registration roller";
123 char *ps_m51
= "unable to print: no cartridge";
124 char *ps_m52
= "no cartridge";
125 char *ps_m53
= "unable to print: cartridge out";
126 char *ps_m54
= "cartridge out";
133 main(int argc
, char *argv
[])
142 char *printerErrorString
= NULL
;
143 char *statusString
= NULL
;
144 char *paperChangedString
= NULL
;
145 char *suffixString
= NULL
;
146 char *jobString
= NULL
;
147 char *prefixString
= NULL
;
148 char *statusOkString
= NULL
;
159 char *ptr1
,*ptr2
,*ptr3
,*ptr4
,*ptr5
;
162 int pagesPrinted
= 0;
163 char *paperType
= NULL
;
165 int useLaserWriterMessages
;
166 int pLen
,sLen
,peLen
,jLen
,pcLen
;
172 * Run immune from typical interruptions, so that
173 * we stand a chance to get the fault message.
174 * EOF (or startup error) is the only way out.
176 signal (SIGHUP
, SIG_IGN
);
177 signal (SIGINT
, SIG_IGN
);
178 signal (SIGQUIT
, SIG_IGN
);
179 signal (SIGTERM
, SIG_IGN
);
186 || !(s_key
= getenv("SPOOLER_KEY"))
188 || (key
= atol(s_key
)) <= 0
190 printf( "Usage: lptell [-lodk] [-X String] printer\n");
191 printf("Options (where X is P,S,e,s, O or c )\n");
192 printf(" environment variable SPOOLER_KEY: must be defined and > 0\n");
193 printf(" printer: name of printer to give status for.\n");
194 printf(" -l: expect laser writer type messages (NeWSprint does)\n");
195 printf(" -o: send input to stdout\n");
196 printf(" -d: send additional debugging output to stdout\n");
197 printf(" -P String: string for prefix, default: '%%%%['\n");
198 printf(" -S String: string for suffix, default: ']%%%%'\n");
199 printf(" -e String: string to detect printer error,\n");
200 printf(" default: 'PrinterError:', send S_SEND_FAULT to lpsched\n");
202 " -c String: string to detect paper change in context of printer error,\n");
203 printf(" default: 'paper changed:', send S_PAPER_CHANGED to lpsched\n");
204 printf(" -s String: string to detect printer ok status, \n");
205 printf(" default: 'status:', send S_CLEAR_FAULT to lpsched\n");
206 printf(" -k: do not use the key for making status ok\n");
207 printf(" -O String: string sent as status message to lpsched,\n");
208 printf(" default: 'ready and printing:'\n");
215 useLaserWriterMessages
= 0;
218 prefixString
= PREFIX_STRING
; pLen
= strlen(prefixString
);
219 suffixString
= SUFFIX_STRING
;
220 printerErrorString
= PRINTER_ERROR_STRING
;
221 peLen
= strlen(printerErrorString
);
222 statusString
= STATUS_STRING
; sLen
= strlen(statusString
);
223 jobString
= JOB_STRING
; jLen
= strlen(jobString
);
224 paperChangedString
= PAPER_CHANGED_STRING
;
225 pcLen
= strlen(paperChangedString
);
226 statusOkString
= STATUS_OK_STRING
;
228 while ((optsw
= getopt(argc
, argv
, "le:s:c:okdO:S:P:")) != EOF
) {
231 useLaserWriterMessages
= 1;
234 prefixString
= (optarg
? strdup(optarg
) : NULL
);
235 pLen
= strlen(prefixString
);
238 suffixString
= (optarg
? strdup(optarg
) : NULL
);
241 printerErrorString
= (optarg
? strdup(optarg
) : NULL
);
242 peLen
= strlen(printerErrorString
);
245 statusString
= (optarg
? strdup(optarg
) : NULL
);
246 sLen
= strlen(statusString
);
249 statusOkString
= (optarg
? strdup(optarg
) : NULL
);
252 paperChangedString
= (optarg
? strdup(optarg
) : NULL
);
253 pcLen
= strlen(paperChangedString
);
267 * Which printer is this? Do we have a key?
270 !(printer
= argv
[optind
])
276 printf( "start lp.tell for %s key %d mode %s %s\n",
277 printer
,key
,(useLaserWriterMessages
? "LW" : "standard"),
278 (doStdOut
? "doStdOut" : "no output"));
279 printf( "prefix (%s) suffix (%s) printerError (%s)\n",
280 prefixString
,suffixString
,printerErrorString
);
281 printf( "paper_changed (%s) status (%s) key %d \n",
282 paperChangedString
,statusString
, clearKey
);
286 * Wait for a message on the standard input. When a single line
287 * comes in, take a couple of more seconds to get any other lines
288 * that may be ready, then send them to the Spooler.
290 while (fgets(buf
, BUFSIZ
, stdin
)) {
291 if (useLaserWriterMessages
) {
292 /* NeWSprint style processing (which simulates the LaserWriter
293 *There are four types of messages:
294 * 1) fault messages: printer error message from handler
295 * 2) clear fault messages: printer ok messages from handler
296 * 3) paper changed messages: printer handler detected paper change
297 * 4) server messages: xnews problems
300 if (strncmp(prefixString
, bufPtr
, pLen
) == 0) {
302 while (*bufPtr
== ' ')
305 if (strncmp(printerErrorString
, bufPtr
,
308 while (*bufPtr
== ' ')
311 if ((strncmp(bufPtr
,paperChangedString
,pcLen
) == 0) &&
312 (ptr1
= bufPtr
+pcLen
) &&
313 (ptr2
= strchr(ptr1
+1,':')) &&
314 (ptr3
= strchr(ptr2
+1,':')) &&
315 (ptr4
= strchr(ptr3
+1,':')) &&
316 (ptr5
= strchr(ptr4
+1,'\n'))) {
317 if (doStdOut
) printf("%s",buf
);
322 trayNum
= atoi(ptr1
+1);
325 pagesPrinted
= atoi(ptr4
+1);
327 printf("Paper changed: %s tray %d paper %s md %d pages %d\n",
328 printer
,trayNum
,paperType
,mode
,pagesPrinted
);
331 mesgRetType
= R_PAPER_CHANGED
;
332 (void)putmessage ( msgbuf
, S_PAPER_CHANGED
, printer
, trayNum
,
333 paperType
, mode
, pagesPrinted
);
335 if (doStdOut
) printf("%s",buf
);
336 if (ptr1
= strstr(bufPtr
,suffixString
)) *ptr1
= 0;
338 printf("Send fault: %s key %d (%s)\n",printer
,key
,bufPtr
);
340 mesgRetType
= R_SEND_FAULT
;
342 (void)putmessage (msgbuf
,S_SEND_FAULT
,printer
,key
,bufPtr
);
344 } else if ((first
= (strncmp(statusString
,bufPtr
,sLen
) == 0)) ||
345 (strncmp(jobString
,bufPtr
,jLen
) == 0)) {
346 bufPtr
+= (first
? sLen
: jLen
);
347 if (doStdOut
) printf("%s",buf
);
348 if (ptr1
= strstr(bufPtr
,suffixString
)) *ptr1
= 0;
350 printf("Clear fault: %s key %d (%s)\n",printer
, clearKey
,
353 mesgRetType
= R_CLEAR_FAULT
;
355 (void)putmessage( msgbuf
,S_CLEAR_FAULT
,printer
,clearKey
,
358 if (doStdOut
) printf("%s",buf
);
359 if (ptr1
= strstr(bufPtr
,suffixString
)) *ptr1
= 0;
361 printf("Server error: %s key %d (%s)\n",printer
,key
,
367 if (doStdOut
) printf("%s",buf
);
368 if (ptr1
= strstr(bufPtr
,suffixString
))
371 printf("Server error: %s key %d (%s)\n",
376 } else { /* not generic PostScript style messages */
377 oldsignal
= signal(SIGALRM
, wakeup
);
383 alert_text
= realloc(alert_text
,
384 strlen(alert_text
)+strlen(buf
)+1
387 alert_text
= malloc(strlen(buf
) + 1);
390 strcat (alert_text
, buf
);
392 } while (fgets(buf
, BUFSIZ
, stdin
));
395 signal (SIGALRM
, oldsignal
);
399 printf("Send generic fault: %s key %d (%s)\n",printer
,key
,
403 printf("%s\n",alert_text
);
406 if (strcmp(alert_text
, "printer ok\n") == 0) {
407 mesgRetType
= R_CLEAR_FAULT
;
409 (void)putmessage(msgbuf
, S_CLEAR_FAULT
, printer
,
410 clearKey
, statusOkString
);
412 mesgRetType
= R_SEND_FAULT
;
414 (void)putmessage(msgbuf
, S_SEND_FAULT
, printer
,
420 if (msend(msgbuf
) == -1)
422 if (mrecv(msgbuf
, sizeof(msgbuf
)) == -1)
424 mtype
= getmessage(msgbuf
, mesgRetType
, &status
);
426 * check for R_CLEAR_FAULT here and 3 lines below
427 * because older lpsched doesn't pass S_CLEAR_FAULT
429 if ((mtype
!= mesgRetType
) &&
430 (mesgRetType
!= R_CLEAR_FAULT
))
433 if ((status
!= MOK
) && (mesgRetType
!= R_CLEAR_FAULT
))
444 ** startup() - OPEN MESSAGE QUEUE TO SPOOLER
445 ** cleanup() - CLOSE THE MESSAGE QUEUE TO THE SPOOLER
448 static int have_contacted_spooler
= 0;
455 * Open a message queue to the Spooler.
456 * An error is deadly.
458 if (!have_contacted_spooler
) {
471 have_contacted_spooler
= 1;
478 if (have_contacted_spooler
)
484 ** wakeup() - TRAP ALARM
487 static void wakeup ()
493 ** done() - CLEANUP AND EXIT