]>
git.ipfire.org Git - thirdparty/cups.git/blob - backend/parallel.c
2 * "$Id: parallel.c 5194 2006-02-27 20:57:07Z 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...
207 if ((fd
= open(resource
, O_WRONLY
| O_EXCL
)) == -1)
209 if (getenv("CLASS") != NULL
)
212 * If the CLASS environment variable is set, the job was submitted
213 * to a class and not to a specific queue. In this case, we want
214 * to abort immediately so that the job can be requeued on the next
215 * available printer in the class.
218 fputs("INFO: Unable to open parallel port, queuing on next printer in class...\n",
222 * Sleep 5 seconds to keep the job from requeuing too rapidly...
227 return (CUPS_BACKEND_FAILED
);
232 fputs("INFO: Parallel port busy; will retry in 30 seconds...\n", stderr
);
235 else if (errno
== ENXIO
|| errno
== EIO
|| errno
== ENOENT
)
237 fputs("INFO: Printer not connected; will retry in 30 seconds...\n", stderr
);
242 fprintf(stderr
, "ERROR: Unable to open parallel port device file \"%s\": %s\n",
243 resource
, strerror(errno
));
244 return (CUPS_BACKEND_FAILED
);
251 * Set any options provided...
254 tcgetattr(fd
, &opts
);
256 opts
.c_lflag
&= ~(ICANON
| ECHO
| ISIG
); /* Raw mode */
258 /**** No options supported yet ****/
260 tcsetattr(fd
, TCSANOW
, &opts
);
263 * Check printer status...
268 #if defined(__linux) && defined(LP_POUTPA)
270 * Show the printer status before we send the file...
273 while (!ioctl(fd
, LPGETSTATUS
, &status
))
275 fprintf(stderr
, "DEBUG: LPGETSTATUS returned a port status of %02X...\n", status
);
277 if (status
& LP_POUTPA
)
279 fputs("WARNING: Media tray empty!\n", stderr
);
280 fputs("STATUS: +media-tray-empty-error\n", stderr
);
285 if (!(status
& LP_PERRORP
))
286 fputs("WARNING: Printer fault!\n", stderr
);
287 else if (!(status
& LP_PSELECD
))
288 fputs("WARNING: Printer off-line.\n", stderr
);
294 #endif /* __linux && LP_POUTPA */
297 * Now that we are "connected" to the port, ignore SIGTERM so that we
298 * can finish out any page data the driver sends (e.g. to eject the
299 * current page... Only ignore SIGTERM if we are printing data from
300 * stdin (otherwise you can't cancel raw jobs...)
305 #ifdef HAVE_SIGSET /* Use System V signals over POSIX to avoid bugs */
306 sigset(SIGTERM
, SIG_IGN
);
307 #elif defined(HAVE_SIGACTION)
308 memset(&action
, 0, sizeof(action
));
310 sigemptyset(&action
.sa_mask
);
311 action
.sa_handler
= SIG_IGN
;
312 sigaction(SIGTERM
, &action
, NULL
);
314 signal(SIGTERM
, SIG_IGN
);
315 #endif /* HAVE_SIGSET */
319 * Finally, send the print file...
330 fputs("PAGE: 1 1\n", stderr
);
331 lseek(fp
, 0, SEEK_SET
);
335 while ((nbytes
= read(fp
, buffer
, sizeof(buffer
))) > 0)
338 * Write the print data to the printer...
347 * See if we are ready to read or write...
357 while (select(fd
+ 1, &input
, &output
, NULL
, NULL
) < 0);
359 if (FD_ISSET(fd
, &input
))
362 * Read backchannel data...
365 if ((rbytes
= read(fd
, resource
, sizeof(resource
))) > 0)
367 fprintf(stderr
, "DEBUG: Received %d bytes of back-channel data!\n",
369 cupsBackChannelWrite(resource
, rbytes
, 1.0);
373 if (FD_ISSET(fd
, &output
))
376 * Write print data...
379 if ((wbytes
= write(fd
, bufptr
, nbytes
)) < 0)
381 wbytes
= write(fd
, bufptr
, nbytes
);
386 * Check for retryable errors...
392 fputs("ERROR: Out of paper!\n", stderr
);
393 fputs("STATUS: +media-tray-empty-error\n", stderr
);
395 else if (errno
!= EAGAIN
&& errno
!= EINTR
)
397 perror("ERROR: Unable to send print file to printer");
404 * Update count and pointer...
409 fputs("STATUS: -media-tray-empty-error\n", stderr
);
423 fprintf(stderr
, "INFO: Sending print file, %lu bytes...\n",
424 (unsigned long)tbytes
);
429 * Close the socket connection and input file and return...
436 return (wbytes
< 0 ? CUPS_BACKEND_FAILED
: CUPS_BACKEND_OK
);
441 * 'list_devices()' - List all parallel devices.
447 #if defined(__hpux) || defined(__sgi) || defined(__sun)
448 static char *funky_hex
= "0123456789abcdefghijklmnopqrstuvwxyz";
449 /* Funky hex numbering used for some devices */
450 #endif /* __hpux || __sgi || __sun */
453 int i
; /* Looping var */
454 int fd
; /* File descriptor */
455 char device
[255], /* Device filename */
456 basedevice
[255], /* Base device filename for ports */
457 device_id
[1024], /* Device ID string */
458 make_model
[1024]; /* Make and model */
461 if (!access("/dev/parallel/", 0))
462 strcpy(basedevice
, "/dev/parallel/");
463 else if (!access("/dev/printers/", 0))
464 strcpy(basedevice
, "/dev/printers/");
465 else if (!access("/dev/par0", 0))
466 strcpy(basedevice
, "/dev/par");
468 strcpy(basedevice
, "/dev/lp");
470 for (i
= 0; i
< 4; i
++)
473 * Open the port, if available...
476 sprintf(device
, "%s%d", basedevice
, i
);
477 if ((fd
= open(device
, O_RDWR
| O_EXCL
)) < 0)
478 fd
= open(device
, O_WRONLY
);
483 * Now grab the IEEE 1284 device ID string...
486 if (!get_device_id(fd
, device_id
, sizeof(device_id
),
487 make_model
, sizeof(make_model
),
489 printf("direct parallel:%s \"%s\" \"%s LPT #%d\" \"%s\"\n", device
,
490 make_model
, make_model
, i
+ 1, device_id
);
492 printf("direct parallel:%s \"Unknown\" \"LPT #%d\"\n", device
, i
+ 1);
498 int i
, j
, n
; /* Looping vars */
499 char device
[255]; /* Device filename */
500 inventory_t
*inv
; /* Hardware inventory info */
504 * IRIX maintains a hardware inventory of most devices...
509 while ((inv
= getinvent()) != NULL
)
511 if (inv
->inv_class
== INV_PARALLEL
&&
512 (inv
->inv_type
== INV_ONBOARD_PLP
||
513 inv
->inv_type
== INV_EPP_ECP_PLP
))
516 * Standard parallel port...
519 puts("direct parallel:/dev/plp \"Unknown\" \"Onboard Parallel Port\"");
521 else if (inv
->inv_class
== INV_PARALLEL
&&
522 inv
->inv_type
== INV_EPC_PLP
)
525 * EPC parallel port...
528 printf("direct parallel:/dev/plp%d \"Unknown\" \"Integral EPC parallel port, Ebus slot %d\"\n",
529 inv
->inv_controller
, inv
->inv_controller
);
536 * Central Data makes serial and parallel "servers" that can be
537 * connected in a number of ways. Look for ports...
540 for (i
= 0; i
< 10; i
++)
541 for (j
= 0; j
< 8; j
++)
542 for (n
= 0; n
< 32; n
++)
544 if (i
== 8) /* EtherLite */
545 sprintf(device
, "/dev/lpn%d%c", j
, funky_hex
[n
]);
546 else if (i
== 9) /* PCI */
547 sprintf(device
, "/dev/lpp%d%c", j
, funky_hex
[n
]);
549 sprintf(device
, "/dev/lp%d%d%c", i
, j
, funky_hex
[n
]);
551 if (access(device
, 0) == 0)
554 printf("direct parallel:%s \"Unknown\" \"Central Data EtherLite Parallel Port, ID %d, port %d\"\n",
557 printf("direct parallel:%s \"Unknown\" \"Central Data PCI Parallel Port, ID %d, port %d\"\n",
560 printf("direct parallel:%s \"Unknown\" \"Central Data SCSI Parallel Port, logical bus %d, ID %d, port %d\"\n",
565 int i
, j
, n
; /* Looping vars */
566 char device
[255]; /* Device filename */
570 * Standard parallel ports...
573 for (i
= 0; i
< 10; i
++)
575 sprintf(device
, "/dev/ecpp%d", i
);
576 if (access(device
, 0) == 0)
577 printf("direct parallel:%s \"Unknown\" \"Sun IEEE-1284 Parallel Port #%d\"\n",
581 for (i
= 0; i
< 10; i
++)
583 sprintf(device
, "/dev/bpp%d", i
);
584 if (access(device
, 0) == 0)
585 printf("direct parallel:%s \"Unknown\" \"Sun Standard Parallel Port #%d\"\n",
589 for (i
= 0; i
< 3; i
++)
591 sprintf(device
, "/dev/lp%d", i
);
593 if (access(device
, 0) == 0)
594 printf("direct parallel:%s \"Unknown\" \"PC Parallel Port #%d\"\n",
599 * MAGMA parallel ports...
602 for (i
= 0; i
< 40; i
++)
604 sprintf(device
, "/dev/pm%02d", i
);
605 if (access(device
, 0) == 0)
606 printf("direct parallel:%s \"Unknown\" \"MAGMA Parallel Board #%d Port #%d\"\n",
607 device
, (i
/ 10) + 1, (i
% 10) + 1);
611 * Central Data parallel ports...
614 for (i
= 0; i
< 9; i
++)
615 for (j
= 0; j
< 8; j
++)
616 for (n
= 0; n
< 32; n
++)
618 if (i
== 8) /* EtherLite */
619 sprintf(device
, "/dev/sts/lpN%d%c", j
, funky_hex
[n
]);
621 sprintf(device
, "/dev/sts/lp%c%d%c", i
+ 'C', j
,
624 if (access(device
, 0) == 0)
627 printf("direct parallel:%s \"Unknown\" \"Central Data EtherLite Parallel Port, ID %d, port %d\"\n",
630 printf("direct parallel:%s \"Unknown\" \"Central Data SCSI Parallel Port, logical bus %d, ID %d, port %d\"\n",
634 #elif defined(__hpux)
635 int i
, j
, n
; /* Looping vars */
636 char device
[255]; /* Device filename */
640 * Standard parallel ports...
643 if (access("/dev/rlp", 0) == 0)
644 puts("direct parallel:/dev/rlp \"Unknown\" \"Standard Parallel Port (/dev/rlp)\"");
646 for (i
= 0; i
< 7; i
++)
647 for (j
= 0; j
< 7; j
++)
649 sprintf(device
, "/dev/c%dt%dd0_lp", i
, j
);
650 if (access(device
, 0) == 0)
651 printf("direct parallel:%s \"Unknown\" \"Parallel Port #%d,%d\"\n",
656 * Central Data parallel ports...
659 for (i
= 0; i
< 9; i
++)
660 for (j
= 0; j
< 8; j
++)
661 for (n
= 0; n
< 32; n
++)
663 if (i
== 8) /* EtherLite */
664 sprintf(device
, "/dev/lpN%d%c", j
, funky_hex
[n
]);
666 sprintf(device
, "/dev/lp%c%d%c", i
+ 'C', j
,
669 if (access(device
, 0) == 0)
672 printf("direct parallel:%s \"Unknown\" \"Central Data EtherLite Parallel Port, ID %d, port %d\"\n",
675 printf("direct parallel:%s \"Unknown\" \"Central Data SCSI Parallel Port, logical bus %d, ID %d, port %d\"\n",
679 #elif defined(__osf__)
680 int i
; /* Looping var */
681 int fd
; /* File descriptor */
682 char device
[255]; /* Device filename */
685 for (i
= 0; i
< 3; i
++)
687 sprintf(device
, "/dev/lp%d", i
);
688 if ((fd
= open(device
, O_WRONLY
)) >= 0)
691 printf("direct parallel:%s \"Unknown\" \"Parallel Port #%d\"\n", device
, i
+ 1);
694 #elif defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__) || defined(__DragonFly__)
695 int i
; /* Looping var */
696 int fd
; /* File descriptor */
697 char device
[255]; /* Device filename */
700 for (i
= 0; i
< 3; i
++)
702 sprintf(device
, "/dev/lpt%d", i
);
703 if ((fd
= open(device
, O_WRONLY
)) >= 0)
706 printf("direct parallel:%s \"Unknown\" \"Parallel Port #%d (interrupt-driven)\"\n", device
, i
+ 1);
709 sprintf(device
, "/dev/lpa%d", i
);
710 if ((fd
= open(device
, O_WRONLY
)) >= 0)
713 printf("direct parallel:%s \"Unknown\" \"Parallel Port #%d (polled)\"\n", device
, i
+ 1);
717 int i
; /* Looping var */
718 int fd
; /* File descriptor */
719 char device
[255]; /* Device filename */
722 for (i
= 0; i
< 8; i
++)
724 sprintf(device
, "/dev/lp%d", i
);
725 if ((fd
= open(device
, O_WRONLY
)) >= 0)
728 printf("direct parallel:%s \"Unknown\" \"Parallel Port #%d\"\n", device
, i
+ 1);
736 * End of "$Id: parallel.c 5194 2006-02-27 20:57:07Z mike $".