]>
git.ipfire.org Git - thirdparty/cups.git/blob - backend/parallel.c
2 * "$Id: parallel.c 5387 2006-04-10 02:51:12Z 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>
46 # include <sys/time.h>
48 # include <sys/select.h>
57 # include <sys/socket.h>
62 # ifndef INV_EPP_ECP_PLP
63 # define INV_EPP_ECP_PLP 6 /* From 6.3/6.4/6.5 sys/invent.h */
64 # define INV_ASO_SERIAL 14 /* serial portion of SGI ASO board */
65 # define INV_IOC3_DMA 16 /* DMA mode IOC3 serial */
66 # define INV_IOC3_PIO 17 /* PIO mode IOC3 serial */
67 # define INV_ISA_DMA 19 /* DMA mode ISA serial -- O2 */
68 # endif /* !INV_EPP_ECP_PLP */
76 void list_devices(void);
80 * 'main()' - Send a file to the specified parallel port.
84 * printer-uri job-id user title copies options [file]
87 int /* O - Exit status */
88 main(int argc
, /* I - Number of command-line arguments (6 or 7) */
89 char *argv
[]) /* I - Command-line arguments */
91 char method
[255], /* Method in URI */
92 hostname
[1024], /* Hostname */
93 username
[255], /* Username info (not used) */
94 resource
[1024], /* Resource info (device and options) */
95 *options
; /* Pointer to options */
96 int port
; /* Port number (not used) */
97 int fp
; /* Print file */
98 int copies
; /* Number of copies to print */
99 int fd
; /* Parallel device */
100 int rbytes
; /* Number of bytes read */
101 int wbytes
; /* Number of bytes written */
102 size_t nbytes
, /* Number of bytes read */
103 tbytes
; /* Total number of bytes written */
104 char buffer
[8192], /* Output buffer */
105 *bufptr
; /* Pointer into buffer */
106 struct termios opts
; /* Parallel port options */
107 fd_set input
, /* Input set for select() */
108 output
; /* Output set for select() */
109 int paperout
; /* Paper out? */
110 #if defined(HAVE_SIGACTION) && !defined(HAVE_SIGSET)
111 struct sigaction action
; /* Actions for POSIX signals */
112 #endif /* HAVE_SIGACTION && !HAVE_SIGSET */
114 unsigned int status
; /* Port status (off-line, out-of-paper, etc.) */
119 * Make sure status messages are not buffered...
122 setbuf(stderr
, NULL
);
125 * Ignore SIGPIPE signals...
129 sigset(SIGPIPE
, SIG_IGN
);
130 #elif defined(HAVE_SIGACTION)
131 memset(&action
, 0, sizeof(action
));
132 action
.sa_handler
= SIG_IGN
;
133 sigaction(SIGPIPE
, &action
, NULL
);
135 signal(SIGPIPE
, SIG_IGN
);
136 #endif /* HAVE_SIGSET */
139 * Check command-line...
145 return (CUPS_BACKEND_OK
);
147 else if (argc
< 6 || argc
> 7)
149 fputs("Usage: parallel job-id user title copies options [file]\n", stderr
);
150 return (CUPS_BACKEND_FAILED
);
154 * If we have 7 arguments, print the file named on the command-line.
155 * Otherwise, send stdin instead...
166 * Try to open the print file...
169 if ((fp
= open(argv
[6], O_RDONLY
)) < 0)
171 perror("ERROR: unable to open print file");
172 return (CUPS_BACKEND_FAILED
);
175 copies
= atoi(argv
[4]);
179 * Extract the device name and options from the URI...
182 httpSeparateURI(HTTP_URI_CODING_ALL
, cupsBackendDeviceURI(argv
),
183 method
, sizeof(method
), username
, sizeof(username
),
184 hostname
, sizeof(hostname
), &port
,
185 resource
, sizeof(resource
));
188 * See if there are any options...
191 if ((options
= strchr(resource
, '?')) != NULL
)
194 * Yup, terminate the device name string and move to the first
195 * character of the options...
202 * Open the parallel port device...
205 fputs("STATE: +connecting-to-device\n", stderr
);
209 if ((fd
= open(resource
, O_WRONLY
| O_EXCL
)) == -1)
211 if (getenv("CLASS") != NULL
)
214 * If the CLASS environment variable is set, the job was submitted
215 * to a class and not to a specific queue. In this case, we want
216 * to abort immediately so that the job can be requeued on the next
217 * available printer in the class.
220 fputs("INFO: Unable to open parallel port, queuing on next printer in class...\n",
224 * Sleep 5 seconds to keep the job from requeuing too rapidly...
229 return (CUPS_BACKEND_FAILED
);
234 fputs("INFO: Parallel port busy; will retry in 30 seconds...\n", stderr
);
237 else if (errno
== ENXIO
|| errno
== EIO
|| errno
== ENOENT
)
239 fputs("INFO: Printer not connected; will retry in 30 seconds...\n", stderr
);
244 fprintf(stderr
, "ERROR: Unable to open parallel port device file \"%s\": %s\n",
245 resource
, strerror(errno
));
246 return (CUPS_BACKEND_FAILED
);
252 fputs("STATE: -connecting-to-device\n", stderr
);
255 * Set any options provided...
258 tcgetattr(fd
, &opts
);
260 opts
.c_lflag
&= ~(ICANON
| ECHO
| ISIG
); /* Raw mode */
262 /**** No options supported yet ****/
264 tcsetattr(fd
, TCSANOW
, &opts
);
267 * Check printer status...
272 #if defined(__linux) && defined(LP_POUTPA)
274 * Show the printer status before we send the file...
277 while (!ioctl(fd
, LPGETSTATUS
, &status
))
279 fprintf(stderr
, "DEBUG: LPGETSTATUS returned a port status of %02X...\n", status
);
281 if (status
& LP_POUTPA
)
283 fputs("WARNING: Media tray empty!\n", stderr
);
284 fputs("STATUS: +media-tray-empty-error\n", stderr
);
289 if (!(status
& LP_PERRORP
))
290 fputs("WARNING: Printer fault!\n", stderr
);
291 else if (!(status
& LP_PSELECD
))
292 fputs("WARNING: Printer off-line.\n", stderr
);
298 #endif /* __linux && LP_POUTPA */
301 * Now that we are "connected" to the port, ignore SIGTERM so that we
302 * can finish out any page data the driver sends (e.g. to eject the
303 * current page... Only ignore SIGTERM if we are printing data from
304 * stdin (otherwise you can't cancel raw jobs...)
309 #ifdef HAVE_SIGSET /* Use System V signals over POSIX to avoid bugs */
310 sigset(SIGTERM
, SIG_IGN
);
311 #elif defined(HAVE_SIGACTION)
312 memset(&action
, 0, sizeof(action
));
314 sigemptyset(&action
.sa_mask
);
315 action
.sa_handler
= SIG_IGN
;
316 sigaction(SIGTERM
, &action
, NULL
);
318 signal(SIGTERM
, SIG_IGN
);
319 #endif /* HAVE_SIGSET */
323 * Finally, send the print file...
334 fputs("PAGE: 1 1\n", stderr
);
335 lseek(fp
, 0, SEEK_SET
);
339 while ((nbytes
= read(fp
, buffer
, sizeof(buffer
))) > 0)
342 * Write the print data to the printer...
351 * See if we are ready to read or write...
361 while (select(fd
+ 1, &input
, &output
, NULL
, NULL
) < 0);
363 if (FD_ISSET(fd
, &input
))
366 * Read backchannel data...
369 if ((rbytes
= read(fd
, resource
, sizeof(resource
))) > 0)
371 fprintf(stderr
, "DEBUG: Received %d bytes of back-channel data!\n",
373 cupsBackChannelWrite(resource
, rbytes
, 1.0);
377 if (FD_ISSET(fd
, &output
))
380 * Write print data...
383 if ((wbytes
= write(fd
, bufptr
, nbytes
)) < 0)
385 wbytes
= write(fd
, bufptr
, nbytes
);
390 * Check for retryable errors...
396 fputs("ERROR: Out of paper!\n", stderr
);
397 fputs("STATUS: +media-tray-empty-error\n", stderr
);
399 else if (errno
!= EAGAIN
&& errno
!= EINTR
)
401 perror("ERROR: Unable to send print file to printer");
408 * Update count and pointer...
413 fputs("STATUS: -media-tray-empty-error\n", stderr
);
427 fprintf(stderr
, "INFO: Sending print file, %lu bytes...\n",
428 (unsigned long)tbytes
);
433 * Close the socket connection and input file and return...
440 return (wbytes
< 0 ? CUPS_BACKEND_FAILED
: CUPS_BACKEND_OK
);
445 * 'list_devices()' - List all parallel devices.
451 #if defined(__hpux) || defined(__sgi) || defined(__sun)
452 static char *funky_hex
= "0123456789abcdefghijklmnopqrstuvwxyz";
453 /* Funky hex numbering used for some devices */
454 #endif /* __hpux || __sgi || __sun */
457 int i
; /* Looping var */
458 int fd
; /* File descriptor */
459 char device
[255], /* Device filename */
460 basedevice
[255], /* Base device filename for ports */
461 device_id
[1024], /* Device ID string */
462 make_model
[1024]; /* Make and model */
465 if (!access("/dev/parallel/", 0))
466 strcpy(basedevice
, "/dev/parallel/");
467 else if (!access("/dev/printers/", 0))
468 strcpy(basedevice
, "/dev/printers/");
470 strcpy(basedevice
, "/dev/lp");
472 for (i
= 0; i
< 4; i
++)
475 * Open the port, if available...
478 sprintf(device
, "%s%d", basedevice
, i
);
479 if ((fd
= open(device
, O_RDWR
| O_EXCL
)) < 0)
480 fd
= open(device
, O_WRONLY
);
485 * Now grab the IEEE 1284 device ID string...
488 if (!get_device_id(fd
, device_id
, sizeof(device_id
),
489 make_model
, sizeof(make_model
),
491 printf("direct parallel:%s \"%s\" \"%s LPT #%d\" \"%s\"\n", device
,
492 make_model
, make_model
, i
+ 1, device_id
);
494 printf("direct parallel:%s \"Unknown\" \"LPT #%d\"\n", device
, i
+ 1);
500 int i
, j
, n
; /* Looping vars */
501 char device
[255]; /* Device filename */
502 inventory_t
*inv
; /* Hardware inventory info */
506 * IRIX maintains a hardware inventory of most devices...
511 while ((inv
= getinvent()) != NULL
)
513 if (inv
->inv_class
== INV_PARALLEL
&&
514 (inv
->inv_type
== INV_ONBOARD_PLP
||
515 inv
->inv_type
== INV_EPP_ECP_PLP
))
518 * Standard parallel port...
521 puts("direct parallel:/dev/plp \"Unknown\" \"Onboard Parallel Port\"");
523 else if (inv
->inv_class
== INV_PARALLEL
&&
524 inv
->inv_type
== INV_EPC_PLP
)
527 * EPC parallel port...
530 printf("direct parallel:/dev/plp%d \"Unknown\" \"Integral EPC parallel port, Ebus slot %d\"\n",
531 inv
->inv_controller
, inv
->inv_controller
);
538 * Central Data makes serial and parallel "servers" that can be
539 * connected in a number of ways. Look for ports...
542 for (i
= 0; i
< 10; i
++)
543 for (j
= 0; j
< 8; j
++)
544 for (n
= 0; n
< 32; n
++)
546 if (i
== 8) /* EtherLite */
547 sprintf(device
, "/dev/lpn%d%c", j
, funky_hex
[n
]);
548 else if (i
== 9) /* PCI */
549 sprintf(device
, "/dev/lpp%d%c", j
, funky_hex
[n
]);
551 sprintf(device
, "/dev/lp%d%d%c", i
, j
, funky_hex
[n
]);
553 if (access(device
, 0) == 0)
556 printf("direct parallel:%s \"Unknown\" \"Central Data EtherLite Parallel Port, ID %d, port %d\"\n",
559 printf("direct parallel:%s \"Unknown\" \"Central Data PCI Parallel Port, ID %d, port %d\"\n",
562 printf("direct parallel:%s \"Unknown\" \"Central Data SCSI Parallel Port, logical bus %d, ID %d, port %d\"\n",
567 int i
, j
, n
; /* Looping vars */
568 char device
[255]; /* Device filename */
572 * Standard parallel ports...
575 for (i
= 0; i
< 10; i
++)
577 sprintf(device
, "/dev/ecpp%d", i
);
578 if (access(device
, 0) == 0)
579 printf("direct parallel:%s \"Unknown\" \"Sun IEEE-1284 Parallel Port #%d\"\n",
583 for (i
= 0; i
< 10; i
++)
585 sprintf(device
, "/dev/bpp%d", i
);
586 if (access(device
, 0) == 0)
587 printf("direct parallel:%s \"Unknown\" \"Sun Standard Parallel Port #%d\"\n",
591 for (i
= 0; i
< 3; i
++)
593 sprintf(device
, "/dev/lp%d", i
);
595 if (access(device
, 0) == 0)
596 printf("direct parallel:%s \"Unknown\" \"PC Parallel Port #%d\"\n",
601 * MAGMA parallel ports...
604 for (i
= 0; i
< 40; i
++)
606 sprintf(device
, "/dev/pm%02d", i
);
607 if (access(device
, 0) == 0)
608 printf("direct parallel:%s \"Unknown\" \"MAGMA Parallel Board #%d Port #%d\"\n",
609 device
, (i
/ 10) + 1, (i
% 10) + 1);
613 * Central Data parallel ports...
616 for (i
= 0; i
< 9; i
++)
617 for (j
= 0; j
< 8; j
++)
618 for (n
= 0; n
< 32; n
++)
620 if (i
== 8) /* EtherLite */
621 sprintf(device
, "/dev/sts/lpN%d%c", j
, funky_hex
[n
]);
623 sprintf(device
, "/dev/sts/lp%c%d%c", i
+ 'C', j
,
626 if (access(device
, 0) == 0)
629 printf("direct parallel:%s \"Unknown\" \"Central Data EtherLite Parallel Port, ID %d, port %d\"\n",
632 printf("direct parallel:%s \"Unknown\" \"Central Data SCSI Parallel Port, logical bus %d, ID %d, port %d\"\n",
636 #elif defined(__hpux)
637 int i
, j
, n
; /* Looping vars */
638 char device
[255]; /* Device filename */
642 * Standard parallel ports...
645 if (access("/dev/rlp", 0) == 0)
646 puts("direct parallel:/dev/rlp \"Unknown\" \"Standard Parallel Port (/dev/rlp)\"");
648 for (i
= 0; i
< 7; i
++)
649 for (j
= 0; j
< 7; j
++)
651 sprintf(device
, "/dev/c%dt%dd0_lp", i
, j
);
652 if (access(device
, 0) == 0)
653 printf("direct parallel:%s \"Unknown\" \"Parallel Port #%d,%d\"\n",
658 * Central Data parallel ports...
661 for (i
= 0; i
< 9; i
++)
662 for (j
= 0; j
< 8; j
++)
663 for (n
= 0; n
< 32; n
++)
665 if (i
== 8) /* EtherLite */
666 sprintf(device
, "/dev/lpN%d%c", j
, funky_hex
[n
]);
668 sprintf(device
, "/dev/lp%c%d%c", i
+ 'C', j
,
671 if (access(device
, 0) == 0)
674 printf("direct parallel:%s \"Unknown\" \"Central Data EtherLite Parallel Port, ID %d, port %d\"\n",
677 printf("direct parallel:%s \"Unknown\" \"Central Data SCSI Parallel Port, logical bus %d, ID %d, port %d\"\n",
681 #elif defined(__osf__)
682 int i
; /* Looping var */
683 int fd
; /* File descriptor */
684 char device
[255]; /* Device filename */
687 for (i
= 0; i
< 3; i
++)
689 sprintf(device
, "/dev/lp%d", i
);
690 if ((fd
= open(device
, O_WRONLY
)) >= 0)
693 printf("direct parallel:%s \"Unknown\" \"Parallel Port #%d\"\n", device
, i
+ 1);
696 #elif defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__) || defined(__DragonFly__)
697 int i
; /* Looping var */
698 int fd
; /* File descriptor */
699 char device
[255]; /* Device filename */
702 for (i
= 0; i
< 3; i
++)
704 sprintf(device
, "/dev/lpt%d", i
);
705 if ((fd
= open(device
, O_WRONLY
)) >= 0)
708 printf("direct parallel:%s \"Unknown\" \"Parallel Port #%d (interrupt-driven)\"\n", device
, i
+ 1);
711 sprintf(device
, "/dev/lpa%d", i
);
712 if ((fd
= open(device
, O_WRONLY
)) >= 0)
715 printf("direct parallel:%s \"Unknown\" \"Parallel Port #%d (polled)\"\n", device
, i
+ 1);
719 int i
; /* Looping var */
720 int fd
; /* File descriptor */
721 char device
[255]; /* Device filename */
724 for (i
= 0; i
< 8; i
++)
726 sprintf(device
, "/dev/lp%d", i
);
727 if ((fd
= open(device
, O_WRONLY
)) >= 0)
730 printf("direct parallel:%s \"Unknown\" \"Parallel Port #%d\"\n", device
, i
+ 1);
738 * End of "$Id: parallel.c 5387 2006-04-10 02:51:12Z mike $".