]>
git.ipfire.org Git - thirdparty/cups.git/blob - backend/parallel.c
2 * "$Id: parallel.c 4906 2006-01-10 20:53:28Z 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(argv
[0], method
, sizeof(method
), username
, sizeof(username
),
178 hostname
, sizeof(hostname
), &port
,
179 resource
, sizeof(resource
));
182 * See if there are any options...
185 if ((options
= strchr(resource
, '?')) != NULL
)
188 * Yup, terminate the device name string and move to the first
189 * character of the options...
196 * Open the parallel port device...
201 if ((fd
= open(resource
, O_WRONLY
| O_EXCL
)) == -1)
203 if (getenv("CLASS") != NULL
)
206 * If the CLASS environment variable is set, the job was submitted
207 * to a class and not to a specific queue. In this case, we want
208 * to abort immediately so that the job can be requeued on the next
209 * available printer in the class.
212 fputs("INFO: Unable to open parallel port, queuing on next printer in class...\n",
216 * Sleep 5 seconds to keep the job from requeuing too rapidly...
221 return (CUPS_BACKEND_FAILED
);
226 fputs("INFO: Parallel port busy; will retry in 30 seconds...\n", stderr
);
229 else if (errno
== ENXIO
|| errno
== EIO
|| errno
== ENOENT
)
231 fputs("INFO: Printer not connected; will retry in 30 seconds...\n", stderr
);
236 fprintf(stderr
, "ERROR: Unable to open parallel port device file \"%s\": %s\n",
237 resource
, strerror(errno
));
238 return (CUPS_BACKEND_FAILED
);
245 * Set any options provided...
248 tcgetattr(fd
, &opts
);
250 opts
.c_lflag
&= ~(ICANON
| ECHO
| ISIG
); /* Raw mode */
252 /**** No options supported yet ****/
254 tcsetattr(fd
, TCSANOW
, &opts
);
257 * Check printer status...
262 #if defined(__linux) && defined(LP_POUTPA)
264 * Show the printer status before we send the file...
267 while (!ioctl(fd
, LPGETSTATUS
, &status
))
269 fprintf(stderr
, "DEBUG: LPGETSTATUS returned a port status of %02X...\n", status
);
271 if (status
& LP_POUTPA
)
273 fputs("WARNING: Media tray empty!\n", stderr
);
274 fputs("STATUS: +media-tray-empty-error\n", stderr
);
279 if (!(status
& LP_PERRORP
))
280 fputs("WARNING: Printer fault!\n", stderr
);
281 else if (!(status
& LP_PSELECD
))
282 fputs("WARNING: Printer off-line.\n", stderr
);
288 #endif /* __linux && LP_POUTPA */
291 * Now that we are "connected" to the port, ignore SIGTERM so that we
292 * can finish out any page data the driver sends (e.g. to eject the
293 * current page... Only ignore SIGTERM if we are printing data from
294 * stdin (otherwise you can't cancel raw jobs...)
299 #ifdef HAVE_SIGSET /* Use System V signals over POSIX to avoid bugs */
300 sigset(SIGTERM
, SIG_IGN
);
301 #elif defined(HAVE_SIGACTION)
302 memset(&action
, 0, sizeof(action
));
304 sigemptyset(&action
.sa_mask
);
305 action
.sa_handler
= SIG_IGN
;
306 sigaction(SIGTERM
, &action
, NULL
);
308 signal(SIGTERM
, SIG_IGN
);
309 #endif /* HAVE_SIGSET */
313 * Finally, send the print file...
324 fputs("PAGE: 1 1\n", stderr
);
325 lseek(fp
, 0, SEEK_SET
);
329 while ((nbytes
= read(fp
, buffer
, sizeof(buffer
))) > 0)
332 * Write the print data to the printer...
341 * See if we are ready to read or write...
351 while (select(fd
+ 1, &input
, &output
, NULL
, NULL
) < 0);
353 if (FD_ISSET(fd
, &input
))
356 * Read backchannel data...
359 if ((rbytes
= read(fd
, resource
, sizeof(resource
))) > 0)
361 fprintf(stderr
, "DEBUG: Received %d bytes of back-channel data!\n",
363 cupsBackchannelWrite(resource
, rbytes
, 1.0);
367 if (FD_ISSET(fd
, &output
))
370 * Write print data...
373 if ((wbytes
= write(fd
, bufptr
, nbytes
)) < 0)
375 wbytes
= write(fd
, bufptr
, nbytes
);
380 * Check for retryable errors...
386 fputs("ERROR: Out of paper!\n", stderr
);
387 fputs("STATUS: +media-tray-empty-error\n", stderr
);
389 else if (errno
!= EAGAIN
&& errno
!= EINTR
)
391 perror("ERROR: Unable to send print file to printer");
398 * Update count and pointer...
403 fputs("STATUS: -media-tray-empty-error\n", stderr
);
417 fprintf(stderr
, "INFO: Sending print file, %lu bytes...\n",
418 (unsigned long)tbytes
);
423 * Close the socket connection and input file and return...
430 return (wbytes
< 0 ? CUPS_BACKEND_FAILED
: CUPS_BACKEND_OK
);
435 * 'list_devices()' - List all parallel devices.
441 #if defined(__hpux) || defined(__sgi) || defined(__sun)
442 static char *funky_hex
= "0123456789abcdefghijklmnopqrstuvwxyz";
443 /* Funky hex numbering used for some devices */
444 #endif /* __hpux || __sgi || __sun */
447 int i
; /* Looping var */
448 int fd
; /* File descriptor */
449 char device
[255], /* Device filename */
450 basedevice
[255], /* Base device filename for ports */
451 device_id
[1024], /* Device ID string */
452 make_model
[1024]; /* Make and model */
455 if (!access("/dev/parallel/", 0))
456 strcpy(basedevice
, "/dev/parallel/");
457 else if (!access("/dev/printers/", 0))
458 strcpy(basedevice
, "/dev/printers/");
459 else if (!access("/dev/par0", 0))
460 strcpy(basedevice
, "/dev/par");
462 strcpy(basedevice
, "/dev/lp");
464 for (i
= 0; i
< 4; i
++)
467 * Open the port, if available...
470 sprintf(device
, "%s%d", basedevice
, i
);
471 if ((fd
= open(device
, O_RDWR
| O_EXCL
)) < 0)
472 fd
= open(device
, O_WRONLY
);
477 * Now grab the IEEE 1284 device ID string...
480 if (!get_device_id(fd
, device_id
, sizeof(device_id
),
481 make_model
, sizeof(make_model
),
483 printf("direct parallel:%s \"%s\" \"%s LPT #%d\" \"%s\"\n", device
,
484 make_model
, make_model
, i
+ 1, device_id
);
486 printf("direct parallel:%s \"Unknown\" \"LPT #%d\"\n", device
, i
+ 1);
492 int i
, j
, n
; /* Looping vars */
493 char device
[255]; /* Device filename */
494 inventory_t
*inv
; /* Hardware inventory info */
498 * IRIX maintains a hardware inventory of most devices...
503 while ((inv
= getinvent()) != NULL
)
505 if (inv
->inv_class
== INV_PARALLEL
&&
506 (inv
->inv_type
== INV_ONBOARD_PLP
||
507 inv
->inv_type
== INV_EPP_ECP_PLP
))
510 * Standard parallel port...
513 puts("direct parallel:/dev/plp \"Unknown\" \"Onboard Parallel Port\"");
515 else if (inv
->inv_class
== INV_PARALLEL
&&
516 inv
->inv_type
== INV_EPC_PLP
)
519 * EPC parallel port...
522 printf("direct parallel:/dev/plp%d \"Unknown\" \"Integral EPC parallel port, Ebus slot %d\"\n",
523 inv
->inv_controller
, inv
->inv_controller
);
530 * Central Data makes serial and parallel "servers" that can be
531 * connected in a number of ways. Look for ports...
534 for (i
= 0; i
< 10; i
++)
535 for (j
= 0; j
< 8; j
++)
536 for (n
= 0; n
< 32; n
++)
538 if (i
== 8) /* EtherLite */
539 sprintf(device
, "/dev/lpn%d%c", j
, funky_hex
[n
]);
540 else if (i
== 9) /* PCI */
541 sprintf(device
, "/dev/lpp%d%c", j
, funky_hex
[n
]);
543 sprintf(device
, "/dev/lp%d%d%c", i
, j
, funky_hex
[n
]);
545 if (access(device
, 0) == 0)
548 printf("direct parallel:%s \"Unknown\" \"Central Data EtherLite Parallel Port, ID %d, port %d\"\n",
551 printf("direct parallel:%s \"Unknown\" \"Central Data PCI Parallel Port, ID %d, port %d\"\n",
554 printf("direct parallel:%s \"Unknown\" \"Central Data SCSI Parallel Port, logical bus %d, ID %d, port %d\"\n",
559 int i
, j
, n
; /* Looping vars */
560 char device
[255]; /* Device filename */
564 * Standard parallel ports...
567 for (i
= 0; i
< 10; i
++)
569 sprintf(device
, "/dev/ecpp%d", i
);
570 if (access(device
, 0) == 0)
571 printf("direct parallel:%s \"Unknown\" \"Sun IEEE-1284 Parallel Port #%d\"\n",
575 for (i
= 0; i
< 10; i
++)
577 sprintf(device
, "/dev/bpp%d", i
);
578 if (access(device
, 0) == 0)
579 printf("direct parallel:%s \"Unknown\" \"Sun Standard Parallel Port #%d\"\n",
583 for (i
= 0; i
< 3; i
++)
585 sprintf(device
, "/dev/lp%d", i
);
587 if (access(device
, 0) == 0)
588 printf("direct parallel:%s \"Unknown\" \"PC Parallel Port #%d\"\n",
593 * MAGMA parallel ports...
596 for (i
= 0; i
< 40; i
++)
598 sprintf(device
, "/dev/pm%02d", i
);
599 if (access(device
, 0) == 0)
600 printf("direct parallel:%s \"Unknown\" \"MAGMA Parallel Board #%d Port #%d\"\n",
601 device
, (i
/ 10) + 1, (i
% 10) + 1);
605 * Central Data parallel ports...
608 for (i
= 0; i
< 9; i
++)
609 for (j
= 0; j
< 8; j
++)
610 for (n
= 0; n
< 32; n
++)
612 if (i
== 8) /* EtherLite */
613 sprintf(device
, "/dev/sts/lpN%d%c", j
, funky_hex
[n
]);
615 sprintf(device
, "/dev/sts/lp%c%d%c", i
+ 'C', j
,
618 if (access(device
, 0) == 0)
621 printf("direct parallel:%s \"Unknown\" \"Central Data EtherLite Parallel Port, ID %d, port %d\"\n",
624 printf("direct parallel:%s \"Unknown\" \"Central Data SCSI Parallel Port, logical bus %d, ID %d, port %d\"\n",
628 #elif defined(__hpux)
629 int i
, j
, n
; /* Looping vars */
630 char device
[255]; /* Device filename */
634 * Standard parallel ports...
637 if (access("/dev/rlp", 0) == 0)
638 puts("direct parallel:/dev/rlp \"Unknown\" \"Standard Parallel Port (/dev/rlp)\"");
640 for (i
= 0; i
< 7; i
++)
641 for (j
= 0; j
< 7; j
++)
643 sprintf(device
, "/dev/c%dt%dd0_lp", i
, j
);
644 if (access(device
, 0) == 0)
645 printf("direct parallel:%s \"Unknown\" \"Parallel Port #%d,%d\"\n",
650 * Central Data parallel ports...
653 for (i
= 0; i
< 9; i
++)
654 for (j
= 0; j
< 8; j
++)
655 for (n
= 0; n
< 32; n
++)
657 if (i
== 8) /* EtherLite */
658 sprintf(device
, "/dev/lpN%d%c", j
, funky_hex
[n
]);
660 sprintf(device
, "/dev/lp%c%d%c", i
+ 'C', j
,
663 if (access(device
, 0) == 0)
666 printf("direct parallel:%s \"Unknown\" \"Central Data EtherLite Parallel Port, ID %d, port %d\"\n",
669 printf("direct parallel:%s \"Unknown\" \"Central Data SCSI Parallel Port, logical bus %d, ID %d, port %d\"\n",
673 #elif defined(__osf__)
674 int i
; /* Looping var */
675 int fd
; /* File descriptor */
676 char device
[255]; /* Device filename */
679 for (i
= 0; i
< 3; i
++)
681 sprintf(device
, "/dev/lp%d", i
);
682 if ((fd
= open(device
, O_WRONLY
)) >= 0)
685 printf("direct parallel:%s \"Unknown\" \"Parallel Port #%d\"\n", device
, i
+ 1);
688 #elif defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__)
689 int i
; /* Looping var */
690 int fd
; /* File descriptor */
691 char device
[255]; /* Device filename */
694 for (i
= 0; i
< 3; i
++)
696 sprintf(device
, "/dev/lpt%d", i
);
697 if ((fd
= open(device
, O_WRONLY
)) >= 0)
700 printf("direct parallel:%s \"Unknown\" \"Parallel Port #%d (interrupt-driven)\"\n", device
, i
+ 1);
703 sprintf(device
, "/dev/lpa%d", i
);
704 if ((fd
= open(device
, O_WRONLY
)) >= 0)
707 printf("direct parallel:%s \"Unknown\" \"Parallel Port #%d (polled)\"\n", device
, i
+ 1);
711 int i
; /* Looping var */
712 int fd
; /* File descriptor */
713 char device
[255]; /* Device filename */
716 for (i
= 0; i
< 8; i
++)
718 sprintf(device
, "/dev/lp%d", i
);
719 if ((fd
= open(device
, O_WRONLY
)) >= 0)
722 printf("direct parallel:%s \"Unknown\" \"Parallel Port #%d\"\n", device
, i
+ 1);
730 * End of "$Id: parallel.c 4906 2006-01-10 20:53:28Z mike $".