]> git.ipfire.org Git - thirdparty/cups.git/blob - backend/parallel.c
Load cups into easysw/current.
[thirdparty/cups.git] / backend / parallel.c
1 /*
2 * "$Id: parallel.c 5099 2006-02-13 02:46:10Z mike $"
3 *
4 * Parallel port backend for the Common UNIX Printing System (CUPS).
5 *
6 * Copyright 1997-2006 by Easy Software Products, all rights reserved.
7 *
8 * These coded instructions, statements, and computer programs are the
9 * property of Easy Software Products and are protected by Federal
10 * copyright law. Distribution and use rights are outlined in the file
11 * "LICENSE" which should have been included with this file. If this
12 * file is missing or damaged please contact Easy Software Products
13 * at:
14 *
15 * Attn: CUPS Licensing Information
16 * Easy Software Products
17 * 44141 Airport View Drive, Suite 204
18 * Hollywood, Maryland 20636 USA
19 *
20 * Voice: (301) 373-9600
21 * EMail: cups-info@cups.org
22 * WWW: http://www.cups.org
23 *
24 * This file is subject to the Apple OS-Developed Software exception.
25 *
26 * Contents:
27 *
28 * main() - Send a file to the specified parallel port.
29 * list_devices() - List all parallel devices.
30 */
31
32 /*
33 * Include necessary headers.
34 */
35
36 #include <cups/backend.h>
37 #include <cups/cups.h>
38 #include <stdio.h>
39 #include <stdlib.h>
40 #include <errno.h>
41 #include <cups/string.h>
42 #include <signal.h>
43 #include <sys/select.h>
44 #include "ieee1284.c"
45
46 #ifdef WIN32
47 # include <io.h>
48 #else
49 # include <unistd.h>
50 # include <fcntl.h>
51 # include <termios.h>
52 # include <sys/socket.h>
53 #endif /* WIN32 */
54
55 #ifdef __sgi
56 # include <invent.h>
57 # ifndef INV_EPP_ECP_PLP
58 # define INV_EPP_ECP_PLP 6 /* From 6.3/6.4/6.5 sys/invent.h */
59 # define INV_ASO_SERIAL 14 /* serial portion of SGI ASO board */
60 # define INV_IOC3_DMA 16 /* DMA mode IOC3 serial */
61 # define INV_IOC3_PIO 17 /* PIO mode IOC3 serial */
62 # define INV_ISA_DMA 19 /* DMA mode ISA serial -- O2 */
63 # endif /* !INV_EPP_ECP_PLP */
64 #endif /* __sgi */
65
66
67 /*
68 * Local functions...
69 */
70
71 void list_devices(void);
72
73
74 /*
75 * 'main()' - Send a file to the specified parallel port.
76 *
77 * Usage:
78 *
79 * printer-uri job-id user title copies options [file]
80 */
81
82 int /* O - Exit status */
83 main(int argc, /* I - Number of command-line arguments (6 or 7) */
84 char *argv[]) /* I - Command-line arguments */
85 {
86 char method[255], /* Method in URI */
87 hostname[1024], /* Hostname */
88 username[255], /* Username info (not used) */
89 resource[1024], /* Resource info (device and options) */
90 *options; /* Pointer to options */
91 int port; /* Port number (not used) */
92 int fp; /* Print file */
93 int copies; /* Number of copies to print */
94 int fd; /* Parallel device */
95 int rbytes; /* Number of bytes read */
96 int wbytes; /* Number of bytes written */
97 size_t nbytes, /* Number of bytes read */
98 tbytes; /* Total number of bytes written */
99 char buffer[8192], /* Output buffer */
100 *bufptr; /* Pointer into buffer */
101 struct termios opts; /* Parallel port options */
102 fd_set input, /* Input set for select() */
103 output; /* Output set for select() */
104 int paperout; /* Paper out? */
105 #if defined(HAVE_SIGACTION) && !defined(HAVE_SIGSET)
106 struct sigaction action; /* Actions for POSIX signals */
107 #endif /* HAVE_SIGACTION && !HAVE_SIGSET */
108 #ifdef __linux
109 unsigned int status; /* Port status (off-line, out-of-paper, etc.) */
110 #endif /* __linux */
111
112
113 /*
114 * Make sure status messages are not buffered...
115 */
116
117 setbuf(stderr, NULL);
118
119 /*
120 * Ignore SIGPIPE signals...
121 */
122
123 #ifdef HAVE_SIGSET
124 sigset(SIGPIPE, SIG_IGN);
125 #elif defined(HAVE_SIGACTION)
126 memset(&action, 0, sizeof(action));
127 action.sa_handler = SIG_IGN;
128 sigaction(SIGPIPE, &action, NULL);
129 #else
130 signal(SIGPIPE, SIG_IGN);
131 #endif /* HAVE_SIGSET */
132
133 /*
134 * Check command-line...
135 */
136
137 if (argc == 1)
138 {
139 list_devices();
140 return (CUPS_BACKEND_OK);
141 }
142 else if (argc < 6 || argc > 7)
143 {
144 fputs("Usage: parallel job-id user title copies options [file]\n", stderr);
145 return (CUPS_BACKEND_FAILED);
146 }
147
148 /*
149 * If we have 7 arguments, print the file named on the command-line.
150 * Otherwise, send stdin instead...
151 */
152
153 if (argc == 6)
154 {
155 fp = 0;
156 copies = 1;
157 }
158 else
159 {
160 /*
161 * Try to open the print file...
162 */
163
164 if ((fp = open(argv[6], O_RDONLY)) < 0)
165 {
166 perror("ERROR: unable to open print file");
167 return (CUPS_BACKEND_FAILED);
168 }
169
170 copies = atoi(argv[4]);
171 }
172
173 /*
174 * Extract the device name and options from the URI...
175 */
176
177 httpSeparateURI(HTTP_URI_CODING_ALL, cupsBackendDeviceURI(argv),
178 method, sizeof(method), username, sizeof(username),
179 hostname, sizeof(hostname), &port,
180 resource, sizeof(resource));
181
182 /*
183 * See if there are any options...
184 */
185
186 if ((options = strchr(resource, '?')) != NULL)
187 {
188 /*
189 * Yup, terminate the device name string and move to the first
190 * character of the options...
191 */
192
193 *options++ = '\0';
194 }
195
196 /*
197 * Open the parallel port device...
198 */
199
200 do
201 {
202 if ((fd = open(resource, O_WRONLY | O_EXCL)) == -1)
203 {
204 if (getenv("CLASS") != NULL)
205 {
206 /*
207 * If the CLASS environment variable is set, the job was submitted
208 * to a class and not to a specific queue. In this case, we want
209 * to abort immediately so that the job can be requeued on the next
210 * available printer in the class.
211 */
212
213 fputs("INFO: Unable to open parallel port, queuing on next printer in class...\n",
214 stderr);
215
216 /*
217 * Sleep 5 seconds to keep the job from requeuing too rapidly...
218 */
219
220 sleep(5);
221
222 return (CUPS_BACKEND_FAILED);
223 }
224
225 if (errno == EBUSY)
226 {
227 fputs("INFO: Parallel port busy; will retry in 30 seconds...\n", stderr);
228 sleep(30);
229 }
230 else if (errno == ENXIO || errno == EIO || errno == ENOENT)
231 {
232 fputs("INFO: Printer not connected; will retry in 30 seconds...\n", stderr);
233 sleep(30);
234 }
235 else
236 {
237 fprintf(stderr, "ERROR: Unable to open parallel port device file \"%s\": %s\n",
238 resource, strerror(errno));
239 return (CUPS_BACKEND_FAILED);
240 }
241 }
242 }
243 while (fd < 0);
244
245 /*
246 * Set any options provided...
247 */
248
249 tcgetattr(fd, &opts);
250
251 opts.c_lflag &= ~(ICANON | ECHO | ISIG); /* Raw mode */
252
253 /**** No options supported yet ****/
254
255 tcsetattr(fd, TCSANOW, &opts);
256
257 /*
258 * Check printer status...
259 */
260
261 paperout = 0;
262
263 #if defined(__linux) && defined(LP_POUTPA)
264 /*
265 * Show the printer status before we send the file...
266 */
267
268 while (!ioctl(fd, LPGETSTATUS, &status))
269 {
270 fprintf(stderr, "DEBUG: LPGETSTATUS returned a port status of %02X...\n", status);
271
272 if (status & LP_POUTPA)
273 {
274 fputs("WARNING: Media tray empty!\n", stderr);
275 fputs("STATUS: +media-tray-empty-error\n", stderr);
276
277 paperout = 1;
278 }
279
280 if (!(status & LP_PERRORP))
281 fputs("WARNING: Printer fault!\n", stderr);
282 else if (!(status & LP_PSELECD))
283 fputs("WARNING: Printer off-line.\n", stderr);
284 else
285 break;
286
287 sleep(5);
288 }
289 #endif /* __linux && LP_POUTPA */
290
291 /*
292 * Now that we are "connected" to the port, ignore SIGTERM so that we
293 * can finish out any page data the driver sends (e.g. to eject the
294 * current page... Only ignore SIGTERM if we are printing data from
295 * stdin (otherwise you can't cancel raw jobs...)
296 */
297
298 if (argc < 7)
299 {
300 #ifdef HAVE_SIGSET /* Use System V signals over POSIX to avoid bugs */
301 sigset(SIGTERM, SIG_IGN);
302 #elif defined(HAVE_SIGACTION)
303 memset(&action, 0, sizeof(action));
304
305 sigemptyset(&action.sa_mask);
306 action.sa_handler = SIG_IGN;
307 sigaction(SIGTERM, &action, NULL);
308 #else
309 signal(SIGTERM, SIG_IGN);
310 #endif /* HAVE_SIGSET */
311 }
312
313 /*
314 * Finally, send the print file...
315 */
316
317 wbytes = 0;
318
319 while (copies > 0)
320 {
321 copies --;
322
323 if (fp != 0)
324 {
325 fputs("PAGE: 1 1\n", stderr);
326 lseek(fp, 0, SEEK_SET);
327 }
328
329 tbytes = 0;
330 while ((nbytes = read(fp, buffer, sizeof(buffer))) > 0)
331 {
332 /*
333 * Write the print data to the printer...
334 */
335
336 tbytes += nbytes;
337 bufptr = buffer;
338
339 while (nbytes > 0)
340 {
341 /*
342 * See if we are ready to read or write...
343 */
344
345 do
346 {
347 FD_ZERO(&input);
348 FD_SET(fd, &input);
349 FD_ZERO(&output);
350 FD_SET(fd, &output);
351 }
352 while (select(fd + 1, &input, &output, NULL, NULL) < 0);
353
354 if (FD_ISSET(fd, &input))
355 {
356 /*
357 * Read backchannel data...
358 */
359
360 if ((rbytes = read(fd, resource, sizeof(resource))) > 0)
361 {
362 fprintf(stderr, "DEBUG: Received %d bytes of back-channel data!\n",
363 rbytes);
364 cupsBackChannelWrite(resource, rbytes, 1.0);
365 }
366 }
367
368 if (FD_ISSET(fd, &output))
369 {
370 /*
371 * Write print data...
372 */
373
374 if ((wbytes = write(fd, bufptr, nbytes)) < 0)
375 if (errno == ENOTTY)
376 wbytes = write(fd, bufptr, nbytes);
377
378 if (wbytes < 0)
379 {
380 /*
381 * Check for retryable errors...
382 */
383
384 if (errno == ENOSPC)
385 {
386 paperout = 1;
387 fputs("ERROR: Out of paper!\n", stderr);
388 fputs("STATUS: +media-tray-empty-error\n", stderr);
389 }
390 else if (errno != EAGAIN && errno != EINTR)
391 {
392 perror("ERROR: Unable to send print file to printer");
393 break;
394 }
395 }
396 else
397 {
398 /*
399 * Update count and pointer...
400 */
401
402 if (paperout)
403 {
404 fputs("STATUS: -media-tray-empty-error\n", stderr);
405 paperout = 0;
406 }
407
408 nbytes -= wbytes;
409 bufptr += wbytes;
410 }
411 }
412 }
413
414 if (wbytes < 0)
415 break;
416
417 if (argc > 6)
418 fprintf(stderr, "INFO: Sending print file, %lu bytes...\n",
419 (unsigned long)tbytes);
420 }
421 }
422
423 /*
424 * Close the socket connection and input file and return...
425 */
426
427 close(fd);
428 if (fp != 0)
429 close(fp);
430
431 return (wbytes < 0 ? CUPS_BACKEND_FAILED : CUPS_BACKEND_OK);
432 }
433
434
435 /*
436 * 'list_devices()' - List all parallel devices.
437 */
438
439 void
440 list_devices(void)
441 {
442 #if defined(__hpux) || defined(__sgi) || defined(__sun)
443 static char *funky_hex = "0123456789abcdefghijklmnopqrstuvwxyz";
444 /* Funky hex numbering used for some devices */
445 #endif /* __hpux || __sgi || __sun */
446
447 #ifdef __linux
448 int i; /* Looping var */
449 int fd; /* File descriptor */
450 char device[255], /* Device filename */
451 basedevice[255], /* Base device filename for ports */
452 device_id[1024], /* Device ID string */
453 make_model[1024]; /* Make and model */
454
455
456 if (!access("/dev/parallel/", 0))
457 strcpy(basedevice, "/dev/parallel/");
458 else if (!access("/dev/printers/", 0))
459 strcpy(basedevice, "/dev/printers/");
460 else if (!access("/dev/par0", 0))
461 strcpy(basedevice, "/dev/par");
462 else
463 strcpy(basedevice, "/dev/lp");
464
465 for (i = 0; i < 4; i ++)
466 {
467 /*
468 * Open the port, if available...
469 */
470
471 sprintf(device, "%s%d", basedevice, i);
472 if ((fd = open(device, O_RDWR | O_EXCL)) < 0)
473 fd = open(device, O_WRONLY);
474
475 if (fd >= 0)
476 {
477 /*
478 * Now grab the IEEE 1284 device ID string...
479 */
480
481 if (!get_device_id(fd, device_id, sizeof(device_id),
482 make_model, sizeof(make_model),
483 NULL, NULL, 0))
484 printf("direct parallel:%s \"%s\" \"%s LPT #%d\" \"%s\"\n", device,
485 make_model, make_model, i + 1, device_id);
486 else
487 printf("direct parallel:%s \"Unknown\" \"LPT #%d\"\n", device, i + 1);
488
489 close(fd);
490 }
491 }
492 #elif defined(__sgi)
493 int i, j, n; /* Looping vars */
494 char device[255]; /* Device filename */
495 inventory_t *inv; /* Hardware inventory info */
496
497
498 /*
499 * IRIX maintains a hardware inventory of most devices...
500 */
501
502 setinvent();
503
504 while ((inv = getinvent()) != NULL)
505 {
506 if (inv->inv_class == INV_PARALLEL &&
507 (inv->inv_type == INV_ONBOARD_PLP ||
508 inv->inv_type == INV_EPP_ECP_PLP))
509 {
510 /*
511 * Standard parallel port...
512 */
513
514 puts("direct parallel:/dev/plp \"Unknown\" \"Onboard Parallel Port\"");
515 }
516 else if (inv->inv_class == INV_PARALLEL &&
517 inv->inv_type == INV_EPC_PLP)
518 {
519 /*
520 * EPC parallel port...
521 */
522
523 printf("direct parallel:/dev/plp%d \"Unknown\" \"Integral EPC parallel port, Ebus slot %d\"\n",
524 inv->inv_controller, inv->inv_controller);
525 }
526 }
527
528 endinvent();
529
530 /*
531 * Central Data makes serial and parallel "servers" that can be
532 * connected in a number of ways. Look for ports...
533 */
534
535 for (i = 0; i < 10; i ++)
536 for (j = 0; j < 8; j ++)
537 for (n = 0; n < 32; n ++)
538 {
539 if (i == 8) /* EtherLite */
540 sprintf(device, "/dev/lpn%d%c", j, funky_hex[n]);
541 else if (i == 9) /* PCI */
542 sprintf(device, "/dev/lpp%d%c", j, funky_hex[n]);
543 else /* SCSI */
544 sprintf(device, "/dev/lp%d%d%c", i, j, funky_hex[n]);
545
546 if (access(device, 0) == 0)
547 {
548 if (i == 8)
549 printf("direct parallel:%s \"Unknown\" \"Central Data EtherLite Parallel Port, ID %d, port %d\"\n",
550 device, j, n);
551 else if (i == 9)
552 printf("direct parallel:%s \"Unknown\" \"Central Data PCI Parallel Port, ID %d, port %d\"\n",
553 device, j, n);
554 else
555 printf("direct parallel:%s \"Unknown\" \"Central Data SCSI Parallel Port, logical bus %d, ID %d, port %d\"\n",
556 device, i, j, n);
557 }
558 }
559 #elif defined(__sun)
560 int i, j, n; /* Looping vars */
561 char device[255]; /* Device filename */
562
563
564 /*
565 * Standard parallel ports...
566 */
567
568 for (i = 0; i < 10; i ++)
569 {
570 sprintf(device, "/dev/ecpp%d", i);
571 if (access(device, 0) == 0)
572 printf("direct parallel:%s \"Unknown\" \"Sun IEEE-1284 Parallel Port #%d\"\n",
573 device, i + 1);
574 }
575
576 for (i = 0; i < 10; i ++)
577 {
578 sprintf(device, "/dev/bpp%d", i);
579 if (access(device, 0) == 0)
580 printf("direct parallel:%s \"Unknown\" \"Sun Standard Parallel Port #%d\"\n",
581 device, i + 1);
582 }
583
584 for (i = 0; i < 3; i ++)
585 {
586 sprintf(device, "/dev/lp%d", i);
587
588 if (access(device, 0) == 0)
589 printf("direct parallel:%s \"Unknown\" \"PC Parallel Port #%d\"\n",
590 device, i + 1);
591 }
592
593 /*
594 * MAGMA parallel ports...
595 */
596
597 for (i = 0; i < 40; i ++)
598 {
599 sprintf(device, "/dev/pm%02d", i);
600 if (access(device, 0) == 0)
601 printf("direct parallel:%s \"Unknown\" \"MAGMA Parallel Board #%d Port #%d\"\n",
602 device, (i / 10) + 1, (i % 10) + 1);
603 }
604
605 /*
606 * Central Data parallel ports...
607 */
608
609 for (i = 0; i < 9; i ++)
610 for (j = 0; j < 8; j ++)
611 for (n = 0; n < 32; n ++)
612 {
613 if (i == 8) /* EtherLite */
614 sprintf(device, "/dev/sts/lpN%d%c", j, funky_hex[n]);
615 else
616 sprintf(device, "/dev/sts/lp%c%d%c", i + 'C', j,
617 funky_hex[n]);
618
619 if (access(device, 0) == 0)
620 {
621 if (i == 8)
622 printf("direct parallel:%s \"Unknown\" \"Central Data EtherLite Parallel Port, ID %d, port %d\"\n",
623 device, j, n);
624 else
625 printf("direct parallel:%s \"Unknown\" \"Central Data SCSI Parallel Port, logical bus %d, ID %d, port %d\"\n",
626 device, i, j, n);
627 }
628 }
629 #elif defined(__hpux)
630 int i, j, n; /* Looping vars */
631 char device[255]; /* Device filename */
632
633
634 /*
635 * Standard parallel ports...
636 */
637
638 if (access("/dev/rlp", 0) == 0)
639 puts("direct parallel:/dev/rlp \"Unknown\" \"Standard Parallel Port (/dev/rlp)\"");
640
641 for (i = 0; i < 7; i ++)
642 for (j = 0; j < 7; j ++)
643 {
644 sprintf(device, "/dev/c%dt%dd0_lp", i, j);
645 if (access(device, 0) == 0)
646 printf("direct parallel:%s \"Unknown\" \"Parallel Port #%d,%d\"\n",
647 device, i, j);
648 }
649
650 /*
651 * Central Data parallel ports...
652 */
653
654 for (i = 0; i < 9; i ++)
655 for (j = 0; j < 8; j ++)
656 for (n = 0; n < 32; n ++)
657 {
658 if (i == 8) /* EtherLite */
659 sprintf(device, "/dev/lpN%d%c", j, funky_hex[n]);
660 else
661 sprintf(device, "/dev/lp%c%d%c", i + 'C', j,
662 funky_hex[n]);
663
664 if (access(device, 0) == 0)
665 {
666 if (i == 8)
667 printf("direct parallel:%s \"Unknown\" \"Central Data EtherLite Parallel Port, ID %d, port %d\"\n",
668 device, j, n);
669 else
670 printf("direct parallel:%s \"Unknown\" \"Central Data SCSI Parallel Port, logical bus %d, ID %d, port %d\"\n",
671 device, i, j, n);
672 }
673 }
674 #elif defined(__osf__)
675 int i; /* Looping var */
676 int fd; /* File descriptor */
677 char device[255]; /* Device filename */
678
679
680 for (i = 0; i < 3; i ++)
681 {
682 sprintf(device, "/dev/lp%d", i);
683 if ((fd = open(device, O_WRONLY)) >= 0)
684 {
685 close(fd);
686 printf("direct parallel:%s \"Unknown\" \"Parallel Port #%d\"\n", device, i + 1);
687 }
688 }
689 #elif defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__)
690 int i; /* Looping var */
691 int fd; /* File descriptor */
692 char device[255]; /* Device filename */
693
694
695 for (i = 0; i < 3; i ++)
696 {
697 sprintf(device, "/dev/lpt%d", i);
698 if ((fd = open(device, O_WRONLY)) >= 0)
699 {
700 close(fd);
701 printf("direct parallel:%s \"Unknown\" \"Parallel Port #%d (interrupt-driven)\"\n", device, i + 1);
702 }
703
704 sprintf(device, "/dev/lpa%d", i);
705 if ((fd = open(device, O_WRONLY)) >= 0)
706 {
707 close(fd);
708 printf("direct parallel:%s \"Unknown\" \"Parallel Port #%d (polled)\"\n", device, i + 1);
709 }
710 }
711 #elif defined(_AIX)
712 int i; /* Looping var */
713 int fd; /* File descriptor */
714 char device[255]; /* Device filename */
715
716
717 for (i = 0; i < 8; i ++)
718 {
719 sprintf(device, "/dev/lp%d", i);
720 if ((fd = open(device, O_WRONLY)) >= 0)
721 {
722 close(fd);
723 printf("direct parallel:%s \"Unknown\" \"Parallel Port #%d\"\n", device, i + 1);
724 }
725 }
726 #endif
727 }
728
729
730 /*
731 * End of "$Id: parallel.c 5099 2006-02-13 02:46:10Z mike $".
732 */