]>
git.ipfire.org Git - thirdparty/cups.git/blob - backend/parallel.c
1f559ffaf958d7052a347fa88f2eab7a683b3235
2 * "$Id: parallel.c 5099 2006-02-13 02:46:10Z mike $"
4 * Parallel port backend for the Common UNIX Printing System (CUPS).
6 * Copyright 1997-2006 by Easy Software Products, all rights reserved.
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
15 * Attn: CUPS Licensing Information
16 * Easy Software Products
17 * 44141 Airport View Drive, Suite 204
18 * Hollywood, Maryland 20636 USA
20 * Voice: (301) 373-9600
21 * EMail: cups-info@cups.org
22 * WWW: http://www.cups.org
24 * This file is subject to the Apple OS-Developed Software exception.
28 * main() - Send a file to the specified parallel port.
29 * list_devices() - List all parallel devices.
33 * Include necessary headers.
36 #include <cups/backend.h>
37 #include <cups/cups.h>
41 #include <cups/string.h>
43 #include <sys/select.h>
52 # include <sys/socket.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 */
71 void list_devices(void);
75 * 'main()' - Send a file to the specified parallel port.
79 * printer-uri job-id user title copies options [file]
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 */
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 */
109 unsigned int status
; /* Port status (off-line, out-of-paper, etc.) */
114 * Make sure status messages are not buffered...
117 setbuf(stderr
, NULL
);
120 * Ignore SIGPIPE signals...
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
);
130 signal(SIGPIPE
, SIG_IGN
);
131 #endif /* HAVE_SIGSET */
134 * Check command-line...
140 return (CUPS_BACKEND_OK
);
142 else if (argc
< 6 || argc
> 7)
144 fputs("Usage: parallel job-id user title copies options [file]\n", stderr
);
145 return (CUPS_BACKEND_FAILED
);
149 * If we have 7 arguments, print the file named on the command-line.
150 * Otherwise, send stdin instead...
161 * Try to open the print file...
164 if ((fp
= open(argv
[6], O_RDONLY
)) < 0)
166 perror("ERROR: unable to open print file");
167 return (CUPS_BACKEND_FAILED
);
170 copies
= atoi(argv
[4]);
174 * Extract the device name and options from the URI...
177 httpSeparateURI(HTTP_URI_CODING_ALL
, cupsBackendDeviceURI(argv
),
178 method
, sizeof(method
), username
, sizeof(username
),
179 hostname
, sizeof(hostname
), &port
,
180 resource
, sizeof(resource
));
183 * See if there are any options...
186 if ((options
= strchr(resource
, '?')) != NULL
)
189 * Yup, terminate the device name string and move to the first
190 * character of the options...
197 * Open the parallel port device...
202 if ((fd
= open(resource
, O_WRONLY
| O_EXCL
)) == -1)
204 if (getenv("CLASS") != NULL
)
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.
213 fputs("INFO: Unable to open parallel port, queuing on next printer in class...\n",
217 * Sleep 5 seconds to keep the job from requeuing too rapidly...
222 return (CUPS_BACKEND_FAILED
);
227 fputs("INFO: Parallel port busy; will retry in 30 seconds...\n", stderr
);
230 else if (errno
== ENXIO
|| errno
== EIO
|| errno
== ENOENT
)
232 fputs("INFO: Printer not connected; will retry in 30 seconds...\n", stderr
);
237 fprintf(stderr
, "ERROR: Unable to open parallel port device file \"%s\": %s\n",
238 resource
, strerror(errno
));
239 return (CUPS_BACKEND_FAILED
);
246 * Set any options provided...
249 tcgetattr(fd
, &opts
);
251 opts
.c_lflag
&= ~(ICANON
| ECHO
| ISIG
); /* Raw mode */
253 /**** No options supported yet ****/
255 tcsetattr(fd
, TCSANOW
, &opts
);
258 * Check printer status...
263 #if defined(__linux) && defined(LP_POUTPA)
265 * Show the printer status before we send the file...
268 while (!ioctl(fd
, LPGETSTATUS
, &status
))
270 fprintf(stderr
, "DEBUG: LPGETSTATUS returned a port status of %02X...\n", status
);
272 if (status
& LP_POUTPA
)
274 fputs("WARNING: Media tray empty!\n", stderr
);
275 fputs("STATUS: +media-tray-empty-error\n", stderr
);
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
);
289 #endif /* __linux && LP_POUTPA */
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...)
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
));
305 sigemptyset(&action
.sa_mask
);
306 action
.sa_handler
= SIG_IGN
;
307 sigaction(SIGTERM
, &action
, NULL
);
309 signal(SIGTERM
, SIG_IGN
);
310 #endif /* HAVE_SIGSET */
314 * Finally, send the print file...
325 fputs("PAGE: 1 1\n", stderr
);
326 lseek(fp
, 0, SEEK_SET
);
330 while ((nbytes
= read(fp
, buffer
, sizeof(buffer
))) > 0)
333 * Write the print data to the printer...
342 * See if we are ready to read or write...
352 while (select(fd
+ 1, &input
, &output
, NULL
, NULL
) < 0);
354 if (FD_ISSET(fd
, &input
))
357 * Read backchannel data...
360 if ((rbytes
= read(fd
, resource
, sizeof(resource
))) > 0)
362 fprintf(stderr
, "DEBUG: Received %d bytes of back-channel data!\n",
364 cupsBackChannelWrite(resource
, rbytes
, 1.0);
368 if (FD_ISSET(fd
, &output
))
371 * Write print data...
374 if ((wbytes
= write(fd
, bufptr
, nbytes
)) < 0)
376 wbytes
= write(fd
, bufptr
, nbytes
);
381 * Check for retryable errors...
387 fputs("ERROR: Out of paper!\n", stderr
);
388 fputs("STATUS: +media-tray-empty-error\n", stderr
);
390 else if (errno
!= EAGAIN
&& errno
!= EINTR
)
392 perror("ERROR: Unable to send print file to printer");
399 * Update count and pointer...
404 fputs("STATUS: -media-tray-empty-error\n", stderr
);
418 fprintf(stderr
, "INFO: Sending print file, %lu bytes...\n",
419 (unsigned long)tbytes
);
424 * Close the socket connection and input file and return...
431 return (wbytes
< 0 ? CUPS_BACKEND_FAILED
: CUPS_BACKEND_OK
);
436 * 'list_devices()' - List all parallel devices.
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 */
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 */
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");
463 strcpy(basedevice
, "/dev/lp");
465 for (i
= 0; i
< 4; i
++)
468 * Open the port, if available...
471 sprintf(device
, "%s%d", basedevice
, i
);
472 if ((fd
= open(device
, O_RDWR
| O_EXCL
)) < 0)
473 fd
= open(device
, O_WRONLY
);
478 * Now grab the IEEE 1284 device ID string...
481 if (!get_device_id(fd
, device_id
, sizeof(device_id
),
482 make_model
, sizeof(make_model
),
484 printf("direct parallel:%s \"%s\" \"%s LPT #%d\" \"%s\"\n", device
,
485 make_model
, make_model
, i
+ 1, device_id
);
487 printf("direct parallel:%s \"Unknown\" \"LPT #%d\"\n", device
, i
+ 1);
493 int i
, j
, n
; /* Looping vars */
494 char device
[255]; /* Device filename */
495 inventory_t
*inv
; /* Hardware inventory info */
499 * IRIX maintains a hardware inventory of most devices...
504 while ((inv
= getinvent()) != NULL
)
506 if (inv
->inv_class
== INV_PARALLEL
&&
507 (inv
->inv_type
== INV_ONBOARD_PLP
||
508 inv
->inv_type
== INV_EPP_ECP_PLP
))
511 * Standard parallel port...
514 puts("direct parallel:/dev/plp \"Unknown\" \"Onboard Parallel Port\"");
516 else if (inv
->inv_class
== INV_PARALLEL
&&
517 inv
->inv_type
== INV_EPC_PLP
)
520 * EPC parallel port...
523 printf("direct parallel:/dev/plp%d \"Unknown\" \"Integral EPC parallel port, Ebus slot %d\"\n",
524 inv
->inv_controller
, inv
->inv_controller
);
531 * Central Data makes serial and parallel "servers" that can be
532 * connected in a number of ways. Look for ports...
535 for (i
= 0; i
< 10; i
++)
536 for (j
= 0; j
< 8; j
++)
537 for (n
= 0; n
< 32; n
++)
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
]);
544 sprintf(device
, "/dev/lp%d%d%c", i
, j
, funky_hex
[n
]);
546 if (access(device
, 0) == 0)
549 printf("direct parallel:%s \"Unknown\" \"Central Data EtherLite Parallel Port, ID %d, port %d\"\n",
552 printf("direct parallel:%s \"Unknown\" \"Central Data PCI Parallel Port, ID %d, port %d\"\n",
555 printf("direct parallel:%s \"Unknown\" \"Central Data SCSI Parallel Port, logical bus %d, ID %d, port %d\"\n",
560 int i
, j
, n
; /* Looping vars */
561 char device
[255]; /* Device filename */
565 * Standard parallel ports...
568 for (i
= 0; i
< 10; i
++)
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",
576 for (i
= 0; i
< 10; i
++)
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",
584 for (i
= 0; i
< 3; i
++)
586 sprintf(device
, "/dev/lp%d", i
);
588 if (access(device
, 0) == 0)
589 printf("direct parallel:%s \"Unknown\" \"PC Parallel Port #%d\"\n",
594 * MAGMA parallel ports...
597 for (i
= 0; i
< 40; i
++)
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);
606 * Central Data parallel ports...
609 for (i
= 0; i
< 9; i
++)
610 for (j
= 0; j
< 8; j
++)
611 for (n
= 0; n
< 32; n
++)
613 if (i
== 8) /* EtherLite */
614 sprintf(device
, "/dev/sts/lpN%d%c", j
, funky_hex
[n
]);
616 sprintf(device
, "/dev/sts/lp%c%d%c", i
+ 'C', j
,
619 if (access(device
, 0) == 0)
622 printf("direct parallel:%s \"Unknown\" \"Central Data EtherLite Parallel Port, ID %d, port %d\"\n",
625 printf("direct parallel:%s \"Unknown\" \"Central Data SCSI Parallel Port, logical bus %d, ID %d, port %d\"\n",
629 #elif defined(__hpux)
630 int i
, j
, n
; /* Looping vars */
631 char device
[255]; /* Device filename */
635 * Standard parallel ports...
638 if (access("/dev/rlp", 0) == 0)
639 puts("direct parallel:/dev/rlp \"Unknown\" \"Standard Parallel Port (/dev/rlp)\"");
641 for (i
= 0; i
< 7; i
++)
642 for (j
= 0; j
< 7; j
++)
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",
651 * Central Data parallel ports...
654 for (i
= 0; i
< 9; i
++)
655 for (j
= 0; j
< 8; j
++)
656 for (n
= 0; n
< 32; n
++)
658 if (i
== 8) /* EtherLite */
659 sprintf(device
, "/dev/lpN%d%c", j
, funky_hex
[n
]);
661 sprintf(device
, "/dev/lp%c%d%c", i
+ 'C', j
,
664 if (access(device
, 0) == 0)
667 printf("direct parallel:%s \"Unknown\" \"Central Data EtherLite Parallel Port, ID %d, port %d\"\n",
670 printf("direct parallel:%s \"Unknown\" \"Central Data SCSI Parallel Port, logical bus %d, ID %d, port %d\"\n",
674 #elif defined(__osf__)
675 int i
; /* Looping var */
676 int fd
; /* File descriptor */
677 char device
[255]; /* Device filename */
680 for (i
= 0; i
< 3; i
++)
682 sprintf(device
, "/dev/lp%d", i
);
683 if ((fd
= open(device
, O_WRONLY
)) >= 0)
686 printf("direct parallel:%s \"Unknown\" \"Parallel Port #%d\"\n", device
, i
+ 1);
689 #elif defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__)
690 int i
; /* Looping var */
691 int fd
; /* File descriptor */
692 char device
[255]; /* Device filename */
695 for (i
= 0; i
< 3; i
++)
697 sprintf(device
, "/dev/lpt%d", i
);
698 if ((fd
= open(device
, O_WRONLY
)) >= 0)
701 printf("direct parallel:%s \"Unknown\" \"Parallel Port #%d (interrupt-driven)\"\n", device
, i
+ 1);
704 sprintf(device
, "/dev/lpa%d", i
);
705 if ((fd
= open(device
, O_WRONLY
)) >= 0)
708 printf("direct parallel:%s \"Unknown\" \"Parallel Port #%d (polled)\"\n", device
, i
+ 1);
712 int i
; /* Looping var */
713 int fd
; /* File descriptor */
714 char device
[255]; /* Device filename */
717 for (i
= 0; i
< 8; i
++)
719 sprintf(device
, "/dev/lp%d", i
);
720 if ((fd
= open(device
, O_WRONLY
)) >= 0)
723 printf("direct parallel:%s \"Unknown\" \"Parallel Port #%d\"\n", device
, i
+ 1);
731 * End of "$Id: parallel.c 5099 2006-02-13 02:46:10Z mike $".