]> git.ipfire.org Git - thirdparty/cups.git/blob - backend/ipp.c
Mirror 1.1.x changes.
[thirdparty/cups.git] / backend / ipp.c
1 /*
2 * "$Id: ipp.c,v 1.38.2.19 2003/01/23 19:25:29 mike Exp $"
3 *
4 * IPP backend for the Common UNIX Printing System (CUPS).
5 *
6 * Copyright 1997-2003 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-3111 USA
19 *
20 * Voice: (301) 373-9603
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 printer or server.
29 * password_cb() - Disable the password prompt for
30 * cupsDoFileRequest().
31 * report_printer_state() - Report the printer state.
32 */
33
34 /*
35 * Include necessary headers.
36 */
37
38 #include <stdio.h>
39 #include <stdlib.h>
40 #include <errno.h>
41 #include <sys/types.h>
42 #include <sys/stat.h>
43 #include <cups/cups.h>
44 #include <cups/language.h>
45 #include <cups/string.h>
46 #include <signal.h>
47
48
49 /*
50 * Local functions...
51 */
52
53 const char *password_cb(const char *);
54 int report_printer_state(ipp_t *ipp);
55
56 #ifdef __APPLE__
57 int run_pictwps_filter(char **argv, char *filename, int length);
58 #endif /* __APPLE__ */
59
60
61 /*
62 * Local globals...
63 */
64
65 char *password = NULL;
66
67
68 /*
69 * 'main()' - Send a file to the printer or server.
70 *
71 * Usage:
72 *
73 * printer-uri job-id user title copies options [file]
74 */
75
76 int /* O - Exit status */
77 main(int argc, /* I - Number of command-line arguments (6 or 7) */
78 char *argv[]) /* I - Command-line arguments */
79 {
80 int i; /* Looping var */
81 int num_options; /* Number of printer options */
82 cups_option_t *options; /* Printer options */
83 char method[255], /* Method in URI */
84 hostname[1024], /* Hostname */
85 username[255], /* Username info */
86 resource[1024], /* Resource info (printer name) */
87 filename[1024]; /* File to print */
88 int port; /* Port number (not used) */
89 char uri[HTTP_MAX_URI];/* Updated URI without user/pass */
90 ipp_status_t ipp_status; /* Status of IPP request */
91 http_t *http; /* HTTP connection */
92 ipp_t *request, /* IPP request */
93 *response, /* IPP response */
94 *supported; /* get-printer-attributes response */
95 ipp_attribute_t *job_id_attr; /* job-id attribute */
96 int job_id; /* job-id value */
97 ipp_attribute_t *job_state; /* job-state attribute */
98 ipp_attribute_t *copies_sup; /* copies-supported attribute */
99 ipp_attribute_t *charset_sup; /* charset-supported attribute */
100 ipp_attribute_t *format_sup; /* document-format-supported attribute */
101 ipp_attribute_t *printer_state;
102 /* printer-state attribute */
103 ipp_attribute_t *printer_accepting;
104 /* printer-is-accepting-jobs attribute */
105 const char *charset; /* Character set to use */
106 cups_lang_t *language; /* Default language */
107 int copies; /* Number of copies remaining */
108 const char *content_type; /* CONTENT_TYPE environment variable */
109 #if defined(HAVE_SIGACTION) && !defined(HAVE_SIGSET)
110 struct sigaction action; /* Actions for POSIX signals */
111 #endif /* HAVE_SIGACTION && !HAVE_SIGSET */
112 int version; /* IPP version */
113 int reasons; /* Number of printer-state-reasons shown */
114
115
116 /*
117 * Make sure status messages are not buffered...
118 */
119
120 setbuf(stderr, NULL);
121
122 /*
123 * Ignore SIGPIPE signals...
124 */
125
126 #ifdef HAVE_SIGSET
127 sigset(SIGPIPE, SIG_IGN);
128 #elif defined(HAVE_SIGACTION)
129 memset(&action, 0, sizeof(action));
130 action.sa_handler = SIG_IGN;
131 sigaction(SIGPIPE, &action, NULL);
132 #else
133 signal(SIGPIPE, SIG_IGN);
134 #endif /* HAVE_SIGSET */
135
136 /*
137 * Check command-line...
138 */
139
140 if (argc == 1)
141 {
142 char *s;
143
144 if ((s = strrchr(argv[0], '/')) != NULL)
145 s ++;
146 else
147 s = argv[0];
148
149 printf("network %s \"Unknown\" \"Internet Printing Protocol (%s)\"\n", s, s);
150 return (0);
151 }
152 else if (argc < 6 || argc > 7)
153 {
154 fprintf(stderr, "Usage: %s job-id user title copies options [file]\n",
155 argv[0]);
156 return (1);
157 }
158
159 /*
160 * Get the content type...
161 */
162
163 if (argc > 6)
164 content_type = getenv("CONTENT_TYPE");
165 else
166 content_type = "application/vnd.cups-raw";
167
168 if (content_type == NULL)
169 content_type = "application/octet-stream";
170
171 /*
172 * If we have 7 arguments, print the file named on the command-line.
173 * Otherwise, copy stdin to a temporary file and print the temporary
174 * file.
175 */
176
177 if (argc == 6)
178 {
179 /*
180 * Copy stdin to a temporary file...
181 */
182
183 int fd; /* Temporary file */
184 char buffer[8192]; /* Buffer for copying */
185 int bytes; /* Number of bytes read */
186
187
188 if ((fd = cupsTempFd(filename, sizeof(filename))) < 0)
189 {
190 perror("ERROR: unable to create temporary file");
191 return (1);
192 }
193
194 while ((bytes = fread(buffer, 1, sizeof(buffer), stdin)) > 0)
195 if (write(fd, buffer, bytes) < bytes)
196 {
197 perror("ERROR: unable to write to temporary file");
198 close(fd);
199 unlink(filename);
200 return (1);
201 }
202
203 close(fd);
204 }
205 #ifdef __APPLE__
206 else if (strcasecmp(content_type, "application/pictwps") == 0)
207 {
208 /*
209 * Convert to PostScript...
210 */
211
212 if (run_pictwps_filter(argv, filename, sizeof(filename)))
213 return (1);
214
215 /*
216 * Change the MIME type to application/vnd.cups-postscript...
217 */
218
219 content_type = "application/vnd.cups-postscript";
220 }
221 #endif /* __APPLE__ */
222 else
223 strlcpy(filename, argv[6], sizeof(filename));
224
225 /*
226 * Extract the hostname and printer name from the URI...
227 */
228
229 httpSeparate(argv[0], method, username, hostname, &port, resource);
230
231 /*
232 * Set the authentication info, if any...
233 */
234
235 cupsSetPasswordCB(password_cb);
236
237 if (username[0])
238 {
239 if ((password = strchr(username, ':')) != NULL)
240 *password++ = '\0';
241
242 cupsSetUser(username);
243 }
244
245 /*
246 * Try connecting to the remote server...
247 */
248
249 do
250 {
251 fprintf(stderr, "INFO: Connecting to %s on port %d...\n", hostname, port);
252
253 if ((http = httpConnectEncrypt(hostname, port, cupsEncryption())) == NULL)
254 {
255 if (getenv("CLASS") != NULL)
256 {
257 /*
258 * If the CLASS environment variable is set, the job was submitted
259 * to a class and not to a specific queue. In this case, we want
260 * to abort immediately so that the job can be requeued on the next
261 * available printer in the class.
262 */
263
264 fprintf(stderr, "INFO: Unable to queue job on %s, queuing on next printer in class...\n",
265 hostname);
266
267 if (argc == 6 || strcmp(filename, argv[6]))
268 unlink(filename);
269
270 /*
271 * Sleep 5 seconds to keep the job from requeuing too rapidly...
272 */
273
274 sleep(5);
275
276 return (1);
277 }
278
279 if (errno == ECONNREFUSED || errno == EHOSTDOWN ||
280 errno == EHOSTUNREACH)
281 {
282 fprintf(stderr, "INFO: Network host \'%s\' is busy; will retry in 30 seconds...",
283 hostname);
284 sleep(30);
285 }
286 else if (h_errno)
287 {
288 fprintf(stderr, "INFO: Unable to lookup host \'%s\' - %s\n",
289 hostname, hstrerror(h_errno));
290 sleep(30);
291 }
292 else
293 {
294 perror("ERROR: Unable to connect to IPP host");
295 sleep(30);
296 }
297 }
298 }
299 while (http == NULL);
300
301 fprintf(stderr, "INFO: Connected to %s...\n", hostname);
302
303 /*
304 * Build a URI for the printer and fill the standard IPP attributes for
305 * an IPP_PRINT_FILE request. We can't use the URI in argv[0] because it
306 * might contain username:password information...
307 */
308
309 snprintf(uri, sizeof(uri), "%s://%s:%d%s", method, hostname, port, resource);
310
311 /*
312 * First validate the destination and see if the device supports multiple
313 * copies. We have to do this because some IPP servers (e.g. HP JetDirect)
314 * don't support the copies attribute...
315 */
316
317 language = cupsLangDefault();
318 charset_sup = NULL;
319 copies_sup = NULL;
320 format_sup = NULL;
321 version = 1;
322 supported = NULL;
323
324 do
325 {
326 /*
327 * Build the IPP request...
328 */
329
330 request = ippNew();
331 request->request.op.version[1] = version;
332 request->request.op.operation_id = IPP_GET_PRINTER_ATTRIBUTES;
333 request->request.op.request_id = 1;
334
335 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
336 "attributes-charset", NULL, "utf-8");
337
338 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
339 "attributes-natural-language", NULL,
340 language != NULL ? language->language : "en");
341
342 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri",
343 NULL, uri);
344
345 /*
346 * Do the request...
347 */
348
349 fputs("DEBUG: Getting supported attributes...\n", stderr);
350
351 if ((supported = cupsDoRequest(http, request, resource)) == NULL)
352 ipp_status = cupsLastError();
353 else
354 ipp_status = supported->request.status.status_code;
355
356 if (ipp_status > IPP_OK_CONFLICT)
357 {
358 if (ipp_status == IPP_PRINTER_BUSY ||
359 ipp_status == IPP_SERVICE_UNAVAILABLE)
360 {
361 fputs("INFO: Printer busy; will retry in 10 seconds...\n", stderr);
362 report_printer_state(supported);
363 sleep(10);
364 }
365 else if ((ipp_status == IPP_BAD_REQUEST ||
366 ipp_status == IPP_VERSION_NOT_SUPPORTED) && version == 1)
367 {
368 /*
369 * Switch to IPP/1.0...
370 */
371
372 fputs("INFO: Printer does not support IPP/1.1, trying IPP/1.0...\n", stderr);
373 version = 0;
374 httpReconnect(http);
375 }
376 else
377 fprintf(stderr, "ERROR: Unable to get printer status (%s)!\n",
378 ippErrorString(ipp_status));
379
380 if (supported)
381 ippDelete(supported);
382
383 continue;
384 }
385 else if ((copies_sup = ippFindAttribute(supported, "copies-supported",
386 IPP_TAG_RANGE)) != NULL)
387 {
388 /*
389 * Has the "copies-supported" attribute - does it have an upper
390 * bound > 1?
391 */
392
393 if (copies_sup->values[0].range.upper <= 1)
394 copies_sup = NULL; /* No */
395 }
396
397 charset_sup = ippFindAttribute(supported, "charset-supported",
398 IPP_TAG_CHARSET);
399 format_sup = ippFindAttribute(supported, "document-format-supported",
400 IPP_TAG_MIMETYPE);
401
402 if (format_sup)
403 {
404 fprintf(stderr, "DEBUG: document-format-supported (%d values)\n",
405 format_sup->num_values);
406 for (i = 0; i < format_sup->num_values; i ++)
407 fprintf(stderr, "DEBUG: [%d] = \"%s\"\n", i,
408 format_sup->values[i].string.text);
409 }
410
411 report_printer_state(supported);
412 }
413 while (ipp_status > IPP_OK_CONFLICT);
414
415 /*
416 * See if the printer is accepting jobs and is not stopped; if either
417 * condition is true and we are printing to a class, requeue the job...
418 */
419
420 if (getenv("CLASS") != NULL)
421 {
422 printer_state = ippFindAttribute(supported, "printer-state",
423 IPP_TAG_ENUM);
424 printer_accepting = ippFindAttribute(supported, "printer-is-accepting-jobs",
425 IPP_TAG_BOOLEAN);
426
427 if (printer_state == NULL ||
428 printer_state->values[0].integer > IPP_PRINTER_PROCESSING ||
429 printer_accepting == NULL ||
430 !printer_accepting->values[0].boolean)
431 {
432 /*
433 * If the CLASS environment variable is set, the job was submitted
434 * to a class and not to a specific queue. In this case, we want
435 * to abort immediately so that the job can be requeued on the next
436 * available printer in the class.
437 */
438
439 fprintf(stderr, "INFO: Unable to queue job on %s, queuing on next printer in class...\n",
440 hostname);
441
442 ippDelete(supported);
443 httpClose(http);
444
445 if (argc == 6 || strcmp(filename, argv[6]))
446 unlink(filename);
447
448 /*
449 * Sleep 5 seconds to keep the job from requeuing too rapidly...
450 */
451
452 sleep(5);
453
454 return (1);
455 }
456 }
457
458 /*
459 * Now that we are "connected" to the port, ignore SIGTERM so that we
460 * can finish out any page data the driver sends (e.g. to eject the
461 * current page... Only ignore SIGTERM if we are printing data from
462 * stdin (otherwise you can't cancel raw jobs...)
463 */
464
465 if (argc < 7)
466 {
467 #ifdef HAVE_SIGSET /* Use System V signals over POSIX to avoid bugs */
468 sigset(SIGTERM, SIG_IGN);
469 #elif defined(HAVE_SIGACTION)
470 memset(&action, 0, sizeof(action));
471
472 sigemptyset(&action.sa_mask);
473 action.sa_handler = SIG_IGN;
474 sigaction(SIGTERM, &action, NULL);
475 #else
476 signal(SIGTERM, SIG_IGN);
477 #endif /* HAVE_SIGSET */
478 }
479
480 /*
481 * See if the printer supports multiple copies...
482 */
483
484 if (copies_sup || argc < 7)
485 copies = 1;
486 else
487 copies = atoi(argv[4]);
488
489 /*
490 * Figure out the character set to use...
491 */
492
493 charset = language ? cupsLangEncoding(language) : "us-ascii";
494
495 if (charset_sup)
496 {
497 /*
498 * See if IPP server supports the requested character set...
499 */
500
501 for (i = 0; i < charset_sup->num_values; i ++)
502 if (strcasecmp(charset, charset_sup->values[i].string.text) == 0)
503 break;
504
505 /*
506 * If not, choose us-ascii or utf-8...
507 */
508
509 if (i >= charset_sup->num_values)
510 {
511 /*
512 * See if us-ascii is supported...
513 */
514
515 for (i = 0; i < charset_sup->num_values; i ++)
516 if (strcasecmp("us-ascii", charset_sup->values[i].string.text) == 0)
517 break;
518
519 if (i < charset_sup->num_values)
520 charset = "us-ascii";
521 else
522 charset = "utf-8";
523 }
524 }
525
526 /*
527 * Then issue the print-job request...
528 */
529
530 reasons = 0;
531
532 while (copies > 0)
533 {
534 /*
535 * Build the IPP request...
536 */
537
538 request = ippNew();
539 request->request.op.version[1] = version;
540 request->request.op.operation_id = IPP_PRINT_JOB;
541 request->request.op.request_id = 1;
542
543 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
544 "attributes-charset", NULL, charset);
545
546 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
547 "attributes-natural-language", NULL,
548 language != NULL ? language->language : "en");
549
550 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri",
551 NULL, uri);
552
553 fprintf(stderr, "DEBUG: printer-uri = \"%s\"\n", uri);
554
555 if (argv[2][0])
556 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "requesting-user-name",
557 NULL, argv[2]);
558
559 fprintf(stderr, "DEBUG: requesting-user-name = \"%s\"\n", argv[2]);
560
561 if (argv[3][0])
562 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "job-name", NULL,
563 argv[3]);
564
565 fprintf(stderr, "DEBUG: job-name = \"%s\"\n", argv[3]);
566
567 /*
568 * Handle options on the command-line...
569 */
570
571 options = NULL;
572 num_options = cupsParseOptions(argv[5], 0, &options);
573
574 if (content_type != NULL && format_sup != NULL)
575 {
576 for (i = 0; i < format_sup->num_values; i ++)
577 if (strcasecmp(content_type, format_sup->values[i].string.text) == 0)
578 break;
579
580 if (i < format_sup->num_values)
581 num_options = cupsAddOption("document-format", content_type,
582 num_options, &options);
583 }
584
585 if (copies_sup)
586 {
587 /*
588 * Only send options if the destination printer supports the copies
589 * attribute. This is a hack for the HP JetDirect implementation of
590 * IPP, which does not accept extension attributes and incorrectly
591 * reports a client-error-bad-request error instead of the
592 * successful-ok-unsupported-attributes status. In short, at least
593 * some HP implementations of IPP are non-compliant.
594 */
595
596 cupsEncodeOptions(request, num_options, options);
597 ippAddInteger(request, IPP_TAG_JOB, IPP_TAG_INTEGER, "copies",
598 atoi(argv[4]));
599 }
600
601 cupsFreeOptions(num_options, options);
602
603 /*
604 * If copies aren't supported, then we are likely dealing with an HP
605 * JetDirect. The HP IPP implementation seems to close the connection
606 * after every request (that is, it does *not* implement HTTP Keep-
607 * Alive, which is REQUIRED by HTTP/1.1...
608 */
609
610 if (!copies_sup)
611 httpReconnect(http);
612
613 /*
614 * Do the request...
615 */
616
617 if ((response = cupsDoFileRequest(http, request, resource, filename)) == NULL)
618 ipp_status = cupsLastError();
619 else
620 ipp_status = response->request.status.status_code;
621
622 if (ipp_status > IPP_OK_CONFLICT)
623 {
624 job_id = 0;
625
626 if (ipp_status == IPP_SERVICE_UNAVAILABLE ||
627 ipp_status == IPP_PRINTER_BUSY)
628 {
629 fputs("INFO: Printer is busy; retrying print job...\n", stderr);
630 sleep(10);
631 }
632 else
633 fprintf(stderr, "ERROR: Print file was not accepted (%s)!\n",
634 ippErrorString(ipp_status));
635 }
636 else if ((job_id_attr = ippFindAttribute(response, "job-id",
637 IPP_TAG_INTEGER)) == NULL)
638 {
639 fputs("INFO: Print file accepted - job ID unknown.\n", stderr);
640 job_id = 0;
641 }
642 else
643 {
644 job_id = job_id_attr->values[0].integer;
645 fprintf(stderr, "INFO: Print file accepted - job ID %d.\n", job_id);
646 }
647
648 if (response)
649 ippDelete(response);
650
651 if (ipp_status <= IPP_OK_CONFLICT && argc > 6)
652 {
653 fprintf(stderr, "PAGE: 1 %d\n", copies_sup ? atoi(argv[4]) : 1);
654 copies --;
655 }
656 else if (ipp_status != IPP_SERVICE_UNAVAILABLE &&
657 ipp_status != IPP_PRINTER_BUSY)
658 break;
659
660 /*
661 * Wait for the job to complete...
662 */
663
664 if (!job_id)
665 continue;
666
667 fputs("INFO: Waiting for job to complete...\n", stderr);
668
669 for (;;)
670 {
671 /*
672 * Build an IPP_GET_JOB_ATTRIBUTES request...
673 */
674
675 request = ippNew();
676 request->request.op.version[1] = version;
677 request->request.op.operation_id = IPP_GET_JOB_ATTRIBUTES;
678 request->request.op.request_id = 1;
679
680 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
681 "attributes-charset", NULL, charset);
682
683 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
684 "attributes-natural-language", NULL,
685 language != NULL ? language->language : "en");
686
687 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri",
688 NULL, uri);
689
690 ippAddInteger(request, IPP_TAG_OPERATION, IPP_TAG_INTEGER, "job-id",
691 job_id);
692
693 if (argv[2][0])
694 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "requesting-user-name",
695 NULL, argv[2]);
696
697 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_KEYWORD,
698 "requested-attributes", NULL, "job-state");
699
700 /*
701 * Do the request...
702 */
703
704 if (!copies_sup)
705 httpReconnect(http);
706
707 if ((response = cupsDoRequest(http, request, resource)) == NULL)
708 ipp_status = cupsLastError();
709 else
710 ipp_status = response->request.status.status_code;
711
712 if (ipp_status == IPP_NOT_FOUND)
713 {
714 /*
715 * Job has gone away and/or the server has no job history...
716 */
717
718 ippDelete(response);
719
720 ipp_status = IPP_OK;
721 break;
722 }
723
724 if (ipp_status > IPP_OK_CONFLICT)
725 {
726 if (ipp_status != IPP_SERVICE_UNAVAILABLE &&
727 ipp_status != IPP_PRINTER_BUSY)
728 {
729 if (response)
730 ippDelete(response);
731
732 fprintf(stderr, "ERROR: Unable to get job %d attributes (%s)!\n",
733 job_id, ippErrorString(ipp_status));
734 break;
735 }
736 }
737 else if ((job_state = ippFindAttribute(response, "job-state", IPP_TAG_ENUM)) != NULL)
738 {
739 /*
740 * Stop polling if the job is finished or pending-held...
741 */
742
743 if (job_state->values[0].integer > IPP_JOB_PROCESSING ||
744 job_state->values[0].integer == IPP_JOB_HELD)
745 {
746 ippDelete(response);
747 break;
748 }
749 }
750
751 if (response)
752 ippDelete(response);
753
754 /*
755 * Now check on the printer state...
756 */
757
758 request = ippNew();
759 request->request.op.version[1] = version;
760 request->request.op.operation_id = IPP_GET_PRINTER_ATTRIBUTES;
761 request->request.op.request_id = 1;
762
763 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
764 "attributes-charset", NULL, charset);
765
766 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
767 "attributes-natural-language", NULL,
768 language != NULL ? language->language : "en");
769
770 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri",
771 NULL, uri);
772
773 if (argv[2][0])
774 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "requesting-user-name",
775 NULL, argv[2]);
776
777 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_KEYWORD,
778 "requested-attributes", NULL, "printer-state-reasons");
779
780 /*
781 * Do the request...
782 */
783
784 if (!copies_sup)
785 httpReconnect(http);
786
787 if ((response = cupsDoRequest(http, request, resource)) != NULL)
788 {
789 reasons = report_printer_state(response);
790 ippDelete(response);
791 }
792
793 /*
794 * Wait 10 seconds before polling again...
795 */
796
797 sleep(10);
798 }
799 }
800
801 /*
802 * Free memory...
803 */
804
805 httpClose(http);
806
807 if (supported)
808 ippDelete(supported);
809
810 /*
811 * Close and remove the temporary file if necessary...
812 */
813
814 if (argc == 6 || strcmp(filename, argv[6]))
815 unlink(filename);
816
817 /*
818 * Return the queue status...
819 */
820
821 if (ipp_status <= IPP_OK_CONFLICT && reasons == 0)
822 fputs("INFO: Ready to print.\n", stderr);
823
824 return (ipp_status > IPP_OK_CONFLICT);
825 }
826
827
828 /*
829 * 'password_cb()' - Disable the password prompt for cupsDoFileRequest().
830 */
831
832 const char * /* O - Password */
833 password_cb(const char *prompt) /* I - Prompt (not used) */
834 {
835 (void)prompt;
836
837 return (password);
838 }
839
840
841 /*
842 * 'report_printer_state()' - Report the printer state.
843 */
844
845 int /* O - Number of reasons shown */
846 report_printer_state(ipp_t *ipp) /* I - IPP response */
847 {
848 int i; /* Looping var */
849 int count; /* Count of reasons shown... */
850 ipp_attribute_t *reasons; /* printer-state-reasons */
851 const char *message; /* Message to show */
852 char unknown[1024]; /* Unknown message string */
853
854
855 if ((reasons = ippFindAttribute(ipp, "printer-state-reasons",
856 IPP_TAG_KEYWORD)) == NULL)
857 return (0);
858
859 for (i = 0, count = 0; i < reasons->num_values; i ++)
860 {
861 message = NULL;
862
863 if (strncmp(reasons->values[i].string.text, "media-needed", 12) == 0)
864 message = "Media tray needs to be filled.";
865 else if (strncmp(reasons->values[i].string.text, "media-jam", 9) == 0)
866 message = "Media jam!";
867 else if (strncmp(reasons->values[i].string.text, "moving-to-paused", 16) == 0 ||
868 strncmp(reasons->values[i].string.text, "paused", 6) == 0 ||
869 strncmp(reasons->values[i].string.text, "shutdown", 8) == 0)
870 message = "Printer off-line.";
871 else if (strncmp(reasons->values[i].string.text, "toner-low", 9) == 0)
872 message = "Toner low.";
873 else if (strncmp(reasons->values[i].string.text, "toner-empty", 11) == 0)
874 message = "Out of toner!";
875 else if (strncmp(reasons->values[i].string.text, "cover-open", 10) == 0)
876 message = "Cover open.";
877 else if (strncmp(reasons->values[i].string.text, "interlock-open", 14) == 0)
878 message = "Interlock open.";
879 else if (strncmp(reasons->values[i].string.text, "door-open", 9) == 0)
880 message = "Door open.";
881 else if (strncmp(reasons->values[i].string.text, "input-tray-missing", 18) == 0)
882 message = "Media tray missing!";
883 else if (strncmp(reasons->values[i].string.text, "media-low", 9) == 0)
884 message = "Media tray almost empty.";
885 else if (strncmp(reasons->values[i].string.text, "media-empty", 11) == 0)
886 message = "Media tray empty!";
887 else if (strncmp(reasons->values[i].string.text, "output-tray-missing", 19) == 0)
888 message = "Output tray missing!";
889 else if (strncmp(reasons->values[i].string.text, "output-area-almost-full", 23) == 0)
890 message = "Output bin almost full.";
891 else if (strncmp(reasons->values[i].string.text, "output-area-full", 16) == 0)
892 message = "Output bin full!";
893 else if (strncmp(reasons->values[i].string.text, "marker-supply-low", 17) == 0)
894 message = "Ink/toner almost empty.";
895 else if (strncmp(reasons->values[i].string.text, "marker-supply-empty", 19) == 0)
896 message = "Ink/toner empty!";
897 else if (strncmp(reasons->values[i].string.text, "marker-waste-almost-full", 24) == 0)
898 message = "Ink/toner waste bin almost full.";
899 else if (strncmp(reasons->values[i].string.text, "marker-waste-full", 17) == 0)
900 message = "Ink/toner waste bin full!";
901 else if (strncmp(reasons->values[i].string.text, "fuser-over-temp", 15) == 0)
902 message = "Fuser temperature high!";
903 else if (strncmp(reasons->values[i].string.text, "fuser-under-temp", 16) == 0)
904 message = "Fuser temperature low!";
905 else if (strncmp(reasons->values[i].string.text, "opc-near-eol", 12) == 0)
906 message = "OPC almost at end-of-life.";
907 else if (strncmp(reasons->values[i].string.text, "opc-life-over", 13) == 0)
908 message = "OPC at end-of-life!";
909 else if (strncmp(reasons->values[i].string.text, "developer-low", 13) == 0)
910 message = "Developer almost empty.";
911 else if (strncmp(reasons->values[i].string.text, "developer-empty", 15) == 0)
912 message = "Developer empty!";
913 else if (strstr(reasons->values[i].string.text, "error") != NULL)
914 {
915 message = unknown;
916
917 snprintf(unknown, sizeof(unknown), "Unknown printer error (%s)!",
918 reasons->values[i].string.text);
919 }
920
921 if (message)
922 {
923 count ++;
924 if (strstr(reasons->values[i].string.text, "error"))
925 fprintf(stderr, "ERROR: %s\n", message);
926 else if (strstr(reasons->values[i].string.text, "warning"))
927 fprintf(stderr, "WARNING: %s\n", message);
928 else
929 fprintf(stderr, "INFO: %s\n", message);
930 }
931 }
932
933 return (count);
934 }
935
936
937 #ifdef __APPLE__
938 /*
939 * 'run_pictwps_filter()' - Convert PICT files to PostScript when printing
940 * remotely.
941 *
942 * This step is required because the PICT format is not documented and
943 * subject to change, so developing a filter for other OS's is infeasible.
944 * Also, fonts required by the PICT file need to be embedded on the
945 * client side (which has the fonts), so we run the filter to get a
946 * PostScript file for printing...
947 */
948
949 int /* O - Exit status of filter */
950 run_pictwps_filter(char **argv, /* I - Command-line arguments */
951 char *filename, /* I - Filename buffer */
952 int length) /* I - Size of filename buffer */
953 {
954 struct stat fileinfo; /* Print file information */
955 const char *ppdfile; /* PPD file for destination printer */
956 int pid; /* Child process ID */
957 int fd; /* Temporary file descriptor */
958 int status; /* Exit status of filter */
959 const char *printer; /* PRINTER env var */
960 static char ppdenv[1024]; /* PPD environment variable */
961
962
963 /*
964 * First get the PPD file for the printer...
965 */
966
967 printer = getenv("PRINTER");
968 if (!printer)
969 {
970 fputs("ERROR: PRINTER environment variable not defined!\n", stderr);
971 return (-1);
972 }
973
974 if ((ppdfile = cupsGetPPD(printer)) == NULL)
975 {
976 fprintf(stderr, "ERROR: Unable to get PPD file for printer \"%s\" - %s.\n",
977 printer, ippErrorString(cupsLastError()));
978 return (-1);
979 }
980
981 snprintf(ppdenv, sizeof(ppdenv), "PPD=%s", ppdfile);
982 putenv(ppdenv);
983
984 /*
985 * Then create a temporary file for printing...
986 */
987
988 if ((fd = cupsTempFd(filename, length)) < 0)
989 {
990 fprintf(stderr, "ERROR: Unable to create temporary file - %s.\n",
991 printer, strerror(errno));
992 return (-1);
993 }
994
995 /*
996 * Get the owner of the spool file - it is owned by the user we want to run
997 * as...
998 */
999
1000 stat(argv[6], &fileinfo);
1001
1002 /*
1003 * Finally, run the filter to convert the file...
1004 */
1005
1006 if ((pid = fork()) == 0)
1007 {
1008 /*
1009 * Child process for pictwpstops... Redirect output of pictwpstops to a
1010 * file...
1011 */
1012
1013 close(1);
1014 dup(fd);
1015 close(fd);
1016
1017 if (!getuid())
1018 {
1019 /*
1020 * Change to an unpriviledged user...
1021 */
1022
1023 setgid(fileinfo.st_gid);
1024 setuid(fileinfo.st_uid);
1025 }
1026
1027 execlp("pictwpstops", printer, argv[1], argv[2], argv[3], argv[4], argv[5],
1028 argv[6], NULL);
1029 perror("ERROR: Unable to exec pictwpstops");
1030 return (errno);
1031 }
1032 else if (pid < 0)
1033 {
1034 /*
1035 * Error!
1036 */
1037
1038 perror("ERROR: Unable to fork pictwpstops");
1039 close(fd);
1040 unlink(filename);
1041 return (-1);
1042 }
1043
1044 /*
1045 * Now wait for the filter to complete...
1046 */
1047
1048 if (wait(&status) < 0)
1049 {
1050 perror("ERROR: Unable to wait for pictwpstops");
1051 close(fd);
1052 unlink(filename);
1053 return (-1);
1054 }
1055
1056 close(fd);
1057
1058 if (status)
1059 {
1060 if (status >= 256)
1061 fprintf(stderr, "ERROR: pictwpstops exited with status %d!\n",
1062 status / 256);
1063 else
1064 fprintf(stderr, "ERROR: pictwpstops exited on signal %d!\n",
1065 status);
1066
1067 unlink(filename);
1068 return (status);
1069 }
1070
1071 /*
1072 * Return with no errors..
1073 */
1074
1075 return (0);
1076 }
1077 #endif /* __APPLE__ */
1078
1079
1080 /*
1081 * End of "$Id: ipp.c,v 1.38.2.19 2003/01/23 19:25:29 mike Exp $".
1082 */