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