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