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