2 * "$Id: ipp.c 7948 2008-09-17 00:04:12Z mike $"
4 * IPP backend for CUPS.
6 * Copyright 2007-2010 by Apple Inc.
7 * Copyright 1997-2007 by Easy Software Products, all rights reserved.
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/".
15 * This file is subject to the Apple OS-Developed Software exception.
19 * main() - Send a file to the printer or server.
20 * cancel_job() - Cancel a print job.
21 * check_printer_state() - Check the printer state.
22 * compress_files() - Compress print files...
23 * monitor_printer() - Monitor the printer state...
24 * password_cb() - Disable the password prompt for
25 * cupsDoFileRequest().
26 * report_attr() - Report an IPP attribute value.
27 * report_printer_state() - Report the printer state.
28 * sigterm_handler() - Handle 'terminate' signals that stop the backend.
32 * Include necessary headers.
35 #include "backend-private.h"
36 #include <sys/types.h>
45 typedef struct _cups_monitor_s
/**** Monitoring data ****/
47 const char *uri
, /* Printer URI */
48 *hostname
, /* Hostname */
50 *resource
; /* Resource path */
51 int port
, /* Port number */
52 version
, /* IPP version */
53 job_id
; /* Job ID for submitted job */
54 http_encryption_t encryption
; /* Use encryption? */
55 ipp_jstate_t job_state
; /* Current job state */
56 ipp_pstate_t printer_state
; /* Current printer state */
64 static const char *auth_info_required
= "none";
65 /* New auth-info-required value */
66 static const char * const jattrs
[] = /* Job attributes we want */
68 "job-media-sheets-completed",
72 static int job_canceled
= 0; /* Job cancelled? */
73 static char *password
= NULL
; /* Password for device URI */
74 static int password_tries
= 0; /* Password tries */
75 static const char * const pattrs
[] = /* Printer attributes we want */
79 "document-format-supported",
87 "media-col-supported",
89 "printer-alert-description",
90 "printer-is-accepting-jobs",
92 "printer-state-message",
93 "printer-state-reasons",
101 static void cancel_job(http_t
*http
, const char *uri
, int id
,
102 const char *resource
, const char *user
,
104 static ipp_pstate_t
check_printer_state(http_t
*http
, const char *uri
,
105 const char *resource
,
106 const char *user
, int version
,
109 static void compress_files(int num_files
, char **files
);
110 #endif /* HAVE_LIBZ */
111 static void *monitor_printer(_cups_monitor_t
*monitor
);
112 static const char *password_cb(const char *);
113 static void report_attr(ipp_attribute_t
*attr
);
114 static int report_printer_state(ipp_t
*ipp
, int job_id
);
115 static void sigterm_handler(int sig
);
119 * 'main()' - Send a file to the printer or server.
123 * printer-uri job-id user title copies options [file]
126 int /* O - Exit status */
127 main(int argc
, /* I - Number of command-line args */
128 char *argv
[]) /* I - Command-line arguments */
130 int i
; /* Looping var */
131 int send_options
; /* Send job options? */
132 int num_options
; /* Number of printer options */
133 cups_option_t
*options
; /* Printer options */
134 const char *device_uri
; /* Device URI */
135 char scheme
[255], /* Scheme in URI */
136 hostname
[1024], /* Hostname */
137 username
[255], /* Username info */
138 resource
[1024], /* Resource info (printer name) */
139 addrname
[256], /* Address name */
140 *optptr
, /* Pointer to URI options */
141 *name
, /* Name of option */
142 *value
, /* Value of option */
143 sep
; /* Separator character */
144 int snmp_fd
, /* SNMP socket */
145 start_count
, /* Page count via SNMP at start */
146 page_count
, /* Page count via SNMP */
147 have_supplies
; /* Printer supports supply levels? */
148 int num_files
; /* Number of files to print */
149 char **files
; /* Files to print */
150 int port
; /* Port number (not used) */
151 char uri
[HTTP_MAX_URI
]; /* Updated URI without user/pass */
152 http_status_t http_status
; /* Status of HTTP request */
153 ipp_status_t ipp_status
; /* Status of IPP request */
154 http_t
*http
; /* HTTP connection */
155 ipp_t
*request
, /* IPP request */
156 *response
, /* IPP response */
157 *supported
; /* get-printer-attributes response */
158 time_t start_time
; /* Time of first connect */
159 int contimeout
; /* Connection timeout */
160 int delay
; /* Delay for retries... */
161 int compression
, /* Do compression of the job data? */
162 waitjob
, /* Wait for job complete? */
163 waitprinter
; /* Wait for printer ready? */
164 _cups_monitor_t monitor
; /* Monitoring data */
165 ipp_attribute_t
*job_id_attr
; /* job-id attribute */
166 int job_id
; /* job-id value */
167 ipp_attribute_t
*job_sheets
; /* job-media-sheets-completed */
168 ipp_attribute_t
*job_state
; /* job-state */
169 ipp_attribute_t
*copies_sup
; /* copies-supported */
170 ipp_attribute_t
*cups_version
; /* cups-version */
171 ipp_attribute_t
*format_sup
; /* document-format-supported */
172 ipp_attribute_t
*media_col_sup
; /* media-col-supported */
173 ipp_attribute_t
*printer_state
; /* printer-state attribute */
174 ipp_attribute_t
*printer_accepting
; /* printer-is-accepting-jobs */
175 int copies
, /* Number of copies for job */
176 copies_remaining
; /* Number of copies remaining */
177 const char *content_type
, /* CONTENT_TYPE environment variable */
178 *final_content_type
; /* FINAL_CONTENT_TYPE environment var */
179 int fd
; /* File descriptor */
180 off_t bytes
; /* Bytes copied */
181 char buffer
[16384]; /* Copy buffer */
182 #if defined(HAVE_SIGACTION) && !defined(HAVE_SIGSET)
183 struct sigaction action
; /* Actions for POSIX signals */
184 #endif /* HAVE_SIGACTION && !HAVE_SIGSET */
185 int version
; /* IPP version */
189 * Make sure status messages are not buffered...
192 setbuf(stderr
, NULL
);
195 * Ignore SIGPIPE and catch SIGTERM signals...
199 sigset(SIGPIPE
, SIG_IGN
);
200 sigset(SIGTERM
, sigterm_handler
);
201 #elif defined(HAVE_SIGACTION)
202 memset(&action
, 0, sizeof(action
));
203 action
.sa_handler
= SIG_IGN
;
204 sigaction(SIGPIPE
, &action
, NULL
);
206 sigemptyset(&action
.sa_mask
);
207 sigaddset(&action
.sa_mask
, SIGTERM
);
208 action
.sa_handler
= sigterm_handler
;
209 sigaction(SIGTERM
, &action
, NULL
);
211 signal(SIGPIPE
, SIG_IGN
);
212 signal(SIGTERM
, sigterm_handler
);
213 #endif /* HAVE_SIGSET */
216 * Check command-line...
223 if ((s
= strrchr(argv
[0], '/')) != NULL
)
228 printf("network %s \"Unknown\" \"%s (%s)\"\n",
229 s
, _cupsLangString(cupsLangDefault(),
230 _("Internet Printing Protocol")), s
);
231 return (CUPS_BACKEND_OK
);
235 _cupsLangPrintf(stderr
,
236 _("Usage: %s job-id user title copies options [file]"),
238 return (CUPS_BACKEND_STOP
);
242 * Get the (final) content type...
245 if ((content_type
= getenv("CONTENT_TYPE")) == NULL
)
246 content_type
= "application/octet-stream";
248 if ((final_content_type
= getenv("FINAL_CONTENT_TYPE")) == NULL
)
250 final_content_type
= content_type
;
252 if (!strncmp(final_content_type
, "printer/", 8))
253 final_content_type
= "application/vnd.cups-raw";
257 * Extract the hostname and printer name from the URI...
260 if ((device_uri
= cupsBackendDeviceURI(argv
)) == NULL
)
261 return (CUPS_BACKEND_FAILED
);
263 httpSeparateURI(HTTP_URI_CODING_ALL
, device_uri
, scheme
, sizeof(scheme
),
264 username
, sizeof(username
), hostname
, sizeof(hostname
), &port
,
265 resource
, sizeof(resource
));
268 port
= IPP_PORT
; /* Default to port 631 */
270 if (!strcmp(scheme
, "https"))
271 cupsSetEncryption(HTTP_ENCRYPT_ALWAYS
);
273 cupsSetEncryption(HTTP_ENCRYPT_IF_REQUESTED
);
276 * See if there are any options...
283 contimeout
= 7 * 24 * 60 * 60;
285 if ((optptr
= strchr(resource
, '?')) != NULL
)
288 * Yup, terminate the device name string and move to the first
289 * character of the optptr...
295 * Then parse the optptr...
306 while (*optptr
&& *optptr
!= '=' && *optptr
!= '+' && *optptr
!= '&')
309 if ((sep
= *optptr
) != '\0')
320 while (*optptr
&& *optptr
!= '+' && *optptr
!= '&')
330 * Process the option...
333 if (!strcasecmp(name
, "waitjob"))
336 * Wait for job completion?
339 waitjob
= !strcasecmp(value
, "on") ||
340 !strcasecmp(value
, "yes") ||
341 !strcasecmp(value
, "true");
343 else if (!strcasecmp(name
, "waitprinter"))
346 * Wait for printer idle?
349 waitprinter
= !strcasecmp(value
, "on") ||
350 !strcasecmp(value
, "yes") ||
351 !strcasecmp(value
, "true");
353 else if (!strcasecmp(name
, "encryption"))
356 * Enable/disable encryption?
359 if (!strcasecmp(value
, "always"))
360 cupsSetEncryption(HTTP_ENCRYPT_ALWAYS
);
361 else if (!strcasecmp(value
, "required"))
362 cupsSetEncryption(HTTP_ENCRYPT_REQUIRED
);
363 else if (!strcasecmp(value
, "never"))
364 cupsSetEncryption(HTTP_ENCRYPT_NEVER
);
365 else if (!strcasecmp(value
, "ifrequested"))
366 cupsSetEncryption(HTTP_ENCRYPT_IF_REQUESTED
);
369 _cupsLangPrintFilter(stderr
, "ERROR",
370 _("Unknown encryption option value: \"%s\"."),
374 else if (!strcasecmp(name
, "version"))
376 if (!strcmp(value
, "1.0"))
378 else if (!strcmp(value
, "1.1"))
380 else if (!strcmp(value
, "2.0"))
382 else if (!strcmp(value
, "2.1"))
384 else if (!strcmp(value
, "2.2"))
388 _cupsLangPrintFilter(stderr
, "ERROR",
389 _("Unknown version option value: \"%s\"."),
394 else if (!strcasecmp(name
, "compression"))
396 compression
= !strcasecmp(value
, "true") ||
397 !strcasecmp(value
, "yes") ||
398 !strcasecmp(value
, "on") ||
399 !strcasecmp(value
, "gzip");
401 #endif /* HAVE_LIBZ */
402 else if (!strcasecmp(name
, "contimeout"))
405 * Set the connection timeout...
409 contimeout
= atoi(value
);
417 _cupsLangPrintFilter(stderr
, "ERROR",
418 _("Unknown option \"%s\" with value \"%s\"."),
425 * If we have 7 arguments, print the file named on the command-line.
426 * Otherwise, copy stdin to a temporary file and print the temporary
433 send_options
= !strcasecmp(final_content_type
, "application/pdf") ||
434 !strcasecmp(final_content_type
, "application/vnd.cups-pdf") ||
435 !strcasecmp(final_content_type
, "image/jpeg") ||
436 !strcasecmp(final_content_type
, "image/png");
438 fputs("DEBUG: Sending stdin for job...\n", stderr
);
443 * Point to the files on the command-line...
446 num_files
= argc
- 6;
452 compress_files(num_files
, files
);
453 #endif /* HAVE_LIBZ */
455 fprintf(stderr
, "DEBUG: %d files to send in job...\n", num_files
);
459 * Set the authentication info, if any...
462 cupsSetPasswordCB(password_cb
);
467 * Use authenticaion information in the device URI...
470 if ((password
= strchr(username
, ':')) != NULL
)
473 cupsSetUser(username
);
478 * Try loading authentication information from the environment.
481 const char *ptr
= getenv("AUTH_USERNAME");
486 password
= getenv("AUTH_PASSWORD");
490 * Try connecting to the remote server...
494 start_time
= time(NULL
);
496 fputs("STATE: +connecting-to-device\n", stderr
);
500 fprintf(stderr
, "DEBUG: Connecting to %s:%d\n", hostname
, port
);
501 _cupsLangPrintFilter(stderr
, "INFO", _("Connecting to printer."));
503 if ((http
= httpConnectEncrypt(hostname
, port
, cupsEncryption())) == NULL
)
505 #if 0 /* These need to go in here someplace when we see HTTP_PKI_ERROR or IPP_PKI_ERROR */
506 fputs("STATE: +cups-certificate-error\n", stderr
);
507 fputs("STATE: -cups-certificate-error\n", stderr
);
510 int error
= errno
; /* Connection error */
515 if (getenv("CLASS") != NULL
)
518 * If the CLASS environment variable is set, the job was submitted
519 * to a class and not to a specific queue. In this case, we want
520 * to abort immediately so that the job can be requeued on the next
521 * available printer in the class.
524 _cupsLangPrintFilter(stderr
, "INFO",
525 _("Unable to contact printer, queuing on next "
526 "printer in class."));
529 * Sleep 5 seconds to keep the job from requeuing too rapidly...
534 return (CUPS_BACKEND_FAILED
);
537 fprintf(stderr
, "DEBUG: Connection error: %s\n", strerror(errno
));
539 if (errno
== ECONNREFUSED
|| errno
== EHOSTDOWN
||
540 errno
== EHOSTUNREACH
)
542 if (contimeout
&& (time(NULL
) - start_time
) > contimeout
)
544 _cupsLangPrintFilter(stderr
, "ERROR",
545 _("The printer is not responding."));
546 return (CUPS_BACKEND_FAILED
);
552 _cupsLangPrintFilter(stderr
, "WARNING",
553 _("Network printer \"%s\" may not exist or "
554 "is unavailable at this time."),
559 _cupsLangPrintFilter(stderr
, "WARNING",
560 _("Network printer \"%s\" is unreachable at "
561 "this time."), hostname
);
566 _cupsLangPrintFilter(stderr
, "WARNING",
567 _("Network printer \"%s\" is busy."),
579 _cupsLangPrintFilter(stderr
, "ERROR",
580 _("Unable to locate network printer \"%s\"."),
582 return (CUPS_BACKEND_STOP
);
586 _cupsLangPrintFilter(stderr
, "ERROR",
587 _("Network printer \"%s\" is not responding."),
596 while (http
== NULL
);
598 if (job_canceled
|| !http
)
599 return (CUPS_BACKEND_FAILED
);
601 fputs("STATE: -connecting-to-device\n", stderr
);
602 _cupsLangPrintFilter(stderr
, "INFO", _("Connected to printer."));
605 if (http
->hostaddr
->addr
.sa_family
== AF_INET6
)
606 fprintf(stderr
, "DEBUG: Connected to [%s]:%d (IPv6)...\n",
607 httpAddrString(http
->hostaddr
, addrname
, sizeof(addrname
)),
608 ntohs(http
->hostaddr
->ipv6
.sin6_port
));
610 #endif /* AF_INET6 */
611 if (http
->hostaddr
->addr
.sa_family
== AF_INET
)
612 fprintf(stderr
, "DEBUG: Connected to %s:%d (IPv4)...\n",
613 httpAddrString(http
->hostaddr
, addrname
, sizeof(addrname
)),
614 ntohs(http
->hostaddr
->ipv4
.sin_port
));
617 * See if the printer supports SNMP...
620 if ((snmp_fd
= _cupsSNMPOpen(http
->hostaddr
->addr
.sa_family
)) >= 0)
621 have_supplies
= !backendSNMPSupplies(snmp_fd
, http
->hostaddr
, &start_count
,
624 have_supplies
= start_count
= 0;
627 * Build a URI for the printer and fill the standard IPP attributes for
628 * an IPP_PRINT_FILE request. We can't use the URI in argv[0] because it
629 * might contain username:password information...
632 httpAssembleURI(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), scheme
, NULL
, hostname
,
636 * First validate the destination and see if the device supports multiple
643 media_col_sup
= NULL
;
649 * Check for side-channel requests...
652 backendCheckSideChannel(snmp_fd
, http
->hostaddr
);
655 * Build the IPP request...
658 request
= ippNewRequest(IPP_GET_PRINTER_ATTRIBUTES
);
659 request
->request
.op
.version
[0] = version
/ 10;
660 request
->request
.op
.version
[1] = version
% 10;
662 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
665 ippAddStrings(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
666 "requested-attributes", sizeof(pattrs
) / sizeof(pattrs
[0]),
673 fputs("DEBUG: Getting supported attributes...\n", stderr
);
675 if (http
->version
< HTTP_1_1
)
677 fprintf(stderr
, "DEBUG: Printer responded with HTTP version %d.%d.\n",
678 http
->version
/ 100, http
->version
% 100);
680 _cupsLangPrintFilter(stderr
, "ERROR",
681 _("Unable to print: the printer does not conform to "
682 "the IPP standard."));
683 exit(CUPS_BACKEND_STOP
);
686 supported
= cupsDoRequest(http
, request
, resource
);
687 ipp_status
= cupsLastError();
689 if (ipp_status
> IPP_OK_CONFLICT
)
691 fprintf(stderr
, "DEBUG: Get-Printer-Attributes returned %s.\n",
692 ippErrorString(ipp_status
));
694 if (ipp_status
== IPP_PRINTER_BUSY
||
695 ipp_status
== IPP_SERVICE_UNAVAILABLE
)
697 if (contimeout
&& (time(NULL
) - start_time
) > contimeout
)
699 _cupsLangPrintFilter(stderr
, "ERROR",
700 _("The printer is not responding."));
701 return (CUPS_BACKEND_FAILED
);
704 _cupsLangPrintFilter(stderr
, "WARNING",
705 _("Network host \"%s\" is busy; will retry in %d "
706 "seconds."), hostname
, delay
);
708 report_printer_state(supported
, 0);
715 else if ((ipp_status
== IPP_BAD_REQUEST
||
716 ipp_status
== IPP_VERSION_NOT_SUPPORTED
) && version
> 10)
719 * Switch to IPP/1.1 or IPP/1.0...
724 _cupsLangPrintFilter(stderr
, "INFO",
725 _("Printer does not support IPP/%d.%d, trying "
726 "IPP/%s."), version
/ 10, version
% 10, "1.1");
731 _cupsLangPrintFilter(stderr
, "INFO",
732 _("Printer does not support IPP/%d.%d, trying "
733 "IPP/%s."), version
/ 10, version
% 10, "1.0");
739 else if (ipp_status
== IPP_NOT_FOUND
)
741 _cupsLangPrintFilter(stderr
, "ERROR",
742 _("The printer URI is incorrect or no longer "
746 ippDelete(supported
);
748 return (CUPS_BACKEND_STOP
);
750 else if (ipp_status
== IPP_NOT_AUTHORIZED
|| ipp_status
== IPP_FORBIDDEN
)
752 if (!strncmp(httpGetField(http
, HTTP_FIELD_WWW_AUTHENTICATE
),
754 auth_info_required
= "negotiate";
756 fprintf(stderr
, "ATTR: auth-info-required=%s\n", auth_info_required
);
757 return (CUPS_BACKEND_AUTH_REQUIRED
);
761 _cupsLangPrintFilter(stderr
, "ERROR",
762 _("Unable to get printer status: %s"),
763 cupsLastErrorString());
768 ippDelete(supported
);
774 * Check for supported attributes...
777 if ((copies_sup
= ippFindAttribute(supported
, "copies-supported",
778 IPP_TAG_RANGE
)) != NULL
)
781 * Has the "copies-supported" attribute - does it have an upper
785 fprintf(stderr
, "DEBUG: copies-supported=%d-%d\n",
786 copies_sup
->values
[0].range
.lower
,
787 copies_sup
->values
[0].range
.upper
);
789 if (copies_sup
->values
[0].range
.upper
<= 1)
790 copies_sup
= NULL
; /* No */
793 cups_version
= ippFindAttribute(supported
, "cups-version", IPP_TAG_TEXT
);
795 if ((format_sup
= ippFindAttribute(supported
, "document-format-supported",
796 IPP_TAG_MIMETYPE
)) != NULL
)
798 fprintf(stderr
, "DEBUG: document-format-supported (%d values)\n",
799 format_sup
->num_values
);
800 for (i
= 0; i
< format_sup
->num_values
; i
++)
801 fprintf(stderr
, "DEBUG: [%d] = \"%s\"\n", i
,
802 format_sup
->values
[i
].string
.text
);
805 if ((media_col_sup
= ippFindAttribute(supported
, "media-col-supported",
806 IPP_TAG_KEYWORD
)) != NULL
)
808 fprintf(stderr
, "DEBUG: media-col-supported (%d values)\n",
809 media_col_sup
->num_values
);
810 for (i
= 0; i
< media_col_sup
->num_values
; i
++)
811 fprintf(stderr
, "DEBUG: [%d] = \"%s\"\n", i
,
812 media_col_sup
->values
[i
].string
.text
);
815 report_printer_state(supported
, 0);
817 while (ipp_status
> IPP_OK_CONFLICT
);
820 * See if the printer is accepting jobs and is not stopped; if either
821 * condition is true and we are printing to a class, requeue the job...
824 if (getenv("CLASS") != NULL
)
826 printer_state
= ippFindAttribute(supported
, "printer-state",
828 printer_accepting
= ippFindAttribute(supported
, "printer-is-accepting-jobs",
831 if (printer_state
== NULL
||
832 (printer_state
->values
[0].integer
> IPP_PRINTER_PROCESSING
&&
834 printer_accepting
== NULL
||
835 !printer_accepting
->values
[0].boolean
)
838 * If the CLASS environment variable is set, the job was submitted
839 * to a class and not to a specific queue. In this case, we want
840 * to abort immediately so that the job can be requeued on the next
841 * available printer in the class.
844 _cupsLangPrintFilter(stderr
, "INFO",
845 _("Unable to contact printer, queuing on next "
846 "printer in class."));
848 ippDelete(supported
);
852 * Sleep 5 seconds to keep the job from requeuing too rapidly...
857 return (CUPS_BACKEND_FAILED
);
862 * See if the printer supports multiple copies...
865 copies
= atoi(argv
[4]);
867 if (copies_sup
|| argc
< 7)
869 copies_remaining
= 1;
875 copies_remaining
= copies
;
878 * Start monitoring the printer in the background...
882 monitor
.hostname
= hostname
;
883 monitor
.user
= argv
[2];
884 monitor
.resource
= resource
;
886 monitor
.version
= version
;
888 monitor
.encryption
= cupsEncryption();
889 monitor
.job_state
= IPP_JOB_PENDING
;
890 monitor
.printer_state
= IPP_PRINTER_IDLE
;
892 _cupsThreadCreate((_cups_thread_func_t
)monitor_printer
, &monitor
);
895 * Then issue the print-job request...
900 while (copies_remaining
> 0)
903 * Check for side-channel requests...
906 backendCheckSideChannel(snmp_fd
, http
->hostaddr
);
909 * Build the IPP request...
916 request
= ippNewRequest(IPP_CREATE_JOB
);
918 request
= ippNewRequest(IPP_PRINT_JOB
);
920 request
->request
.op
.version
[0] = version
/ 10;
921 request
->request
.op
.version
[1] = version
% 10;
923 fprintf(stderr
, "DEBUG: %s IPP/%d.%d\n",
924 ippOpString(request
->request
.op
.operation_id
),
925 request
->request
.op
.version
[0],
926 request
->request
.op
.version
[1]);
928 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
930 fprintf(stderr
, "DEBUG: printer-uri=\"%s\"\n", uri
);
934 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
935 "requesting-user-name", NULL
, argv
[2]);
936 fprintf(stderr
, "DEBUG: requesting-user-name=\"%s\"\n", argv
[2]);
941 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "job-name", NULL
,
943 fprintf(stderr
, "DEBUG: job-name=\"%s\"\n", argv
[3]);
948 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
949 "compression", NULL
, "gzip");
950 #endif /* HAVE_LIBZ */
953 * Handle options on the command-line...
957 num_options
= cupsParseOptions(argv
[5], 0, &options
);
959 if (format_sup
!= NULL
)
961 for (i
= 0; i
< format_sup
->num_values
; i
++)
962 if (!strcasecmp(final_content_type
, format_sup
->values
[i
].string
.text
))
965 if (i
< format_sup
->num_values
)
966 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_MIMETYPE
,
967 "document-format", NULL
, final_content_type
);
972 if (cups_version
|| !media_col_sup
)
975 * When talking to another CUPS server, send all options...
978 cupsEncodeOptions(request
, num_options
, options
);
983 * Otherwise send standard IPP attributes...
986 char cachefile
[1024];/* Printer PWG cache file */
987 const char *cups_cachedir
; /* Location of cache file */
988 _pwg_t
*pwg
; /* PWG mapping data */
989 const char *keyword
; /* PWG keyword */
990 _pwg_size_t
*size
; /* PWG media size */
993 if ((cups_cachedir
= getenv("CUPS_CACHEDIR")) == NULL
)
994 cups_cachedir
= CUPS_CACHEDIR
;
996 snprintf(cachefile
, sizeof(cachefile
), "%s/%s.pwg", cups_cachedir
,
999 if ((keyword
= cupsGetOption("PageSize", num_options
, options
)) == NULL
)
1000 keyword
= cupsGetOption("media", num_options
, options
);
1002 pwg
= _pwgCreateWithFile(cachefile
);
1003 size
= _pwgGetSize(pwg
, keyword
);
1005 if (media_col_sup
&& size
)
1007 ipp_t
*media_col
, /* media-col value */
1008 *media_size
; /* media-size value */
1009 const char *media_source
, /* media-source value */
1010 *media_type
; /* media-type value */
1012 media_size
= ippNew();
1013 ippAddInteger(media_size
, IPP_TAG_ZERO
, IPP_TAG_INTEGER
,
1014 "x-dimension", size
->width
);
1015 ippAddInteger(media_size
, IPP_TAG_ZERO
, IPP_TAG_INTEGER
,
1016 "y-dimension", size
->length
);
1018 media_col
= ippNew();
1019 ippAddCollection(media_col
, IPP_TAG_ZERO
, "media-size", media_size
);
1021 media_source
= _pwgGetSource(pwg
, cupsGetOption("InputSlot",
1024 media_type
= _pwgGetType(pwg
, cupsGetOption("MediaType",
1025 num_options
, options
));
1027 for (i
= 0; i
< media_col_sup
->num_values
; i
++)
1029 if (!strcmp(media_col_sup
->values
[i
].string
.text
,
1030 "media-left-margin"))
1031 ippAddInteger(media_col
, IPP_TAG_ZERO
, IPP_TAG_INTEGER
,
1032 "media-left-margin", size
->left
);
1033 else if (!strcmp(media_col_sup
->values
[i
].string
.text
,
1034 "media-bottom-margin"))
1035 ippAddInteger(media_col
, IPP_TAG_ZERO
, IPP_TAG_INTEGER
,
1036 "media-bottom-margin", size
->left
);
1037 else if (!strcmp(media_col_sup
->values
[i
].string
.text
,
1038 "media-right-margin"))
1039 ippAddInteger(media_col
, IPP_TAG_ZERO
, IPP_TAG_INTEGER
,
1040 "media-right-margin", size
->left
);
1041 else if (!strcmp(media_col_sup
->values
[i
].string
.text
,
1042 "media-top-margin"))
1043 ippAddInteger(media_col
, IPP_TAG_ZERO
, IPP_TAG_INTEGER
,
1044 "media-top-margin", size
->left
);
1045 else if (!strcmp(media_col_sup
->values
[i
].string
.text
,
1046 "media-source") && media_source
)
1047 ippAddString(media_col
, IPP_TAG_ZERO
, IPP_TAG_KEYWORD
,
1048 "media-source", NULL
, media_source
);
1049 else if (!strcmp(media_col_sup
->values
[i
].string
.text
,
1050 "media-type") && media_type
)
1051 ippAddString(media_col
, IPP_TAG_ZERO
, IPP_TAG_KEYWORD
,
1052 "media-type", NULL
, media_type
);
1055 ippAddCollection(request
, IPP_TAG_JOB
, "media-col", media_col
);
1058 ippAddString(request
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
, "media",
1059 NULL
, size
->map
.pwg
);
1062 _pwg_media_t
*found
; /* PWG media */
1065 if ((found
= _pwgMediaForPPD(keyword
)) != NULL
)
1066 keyword
= found
->pwg
;
1067 else if ((found
= _pwgMediaForLegacy(keyword
)) != NULL
)
1068 keyword
= found
->pwg
;
1070 ippAddString(request
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
, "media",
1074 if ((keyword
= cupsGetOption("output-bin", num_options
,
1076 keyword
= _pwgGetBin(pwg
, cupsGetOption("OutputBin", num_options
,
1080 ippAddString(request
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
, "output-bin",
1083 if ((keyword
= cupsGetOption("output-mode", num_options
,
1085 ippAddString(request
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
, "output-mode",
1087 else if ((keyword
= cupsGetOption("ColorModel", num_options
,
1090 if (!strcasecmp(keyword
, "Gray"))
1091 ippAddString(request
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
, "output-mode",
1092 NULL
, "monochrome");
1093 else if (!strcasecmp(keyword
, "Color"))
1094 ippAddString(request
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
, "output-mode",
1098 if ((keyword
= cupsGetOption("print-quality", num_options
,
1100 ippAddInteger(request
, IPP_TAG_JOB
, IPP_TAG_ENUM
, "print-quality",
1102 else if ((keyword
= cupsGetOption("cupsPrintQuality", num_options
,
1105 if (!strcasecmp(keyword
, "draft"))
1106 ippAddInteger(request
, IPP_TAG_JOB
, IPP_TAG_ENUM
, "print-quality",
1108 else if (!strcasecmp(keyword
, "normal"))
1109 ippAddInteger(request
, IPP_TAG_JOB
, IPP_TAG_ENUM
, "print-quality",
1110 IPP_QUALITY_NORMAL
);
1111 else if (!strcasecmp(keyword
, "high"))
1112 ippAddInteger(request
, IPP_TAG_JOB
, IPP_TAG_ENUM
, "print-quality",
1116 if ((keyword
= cupsGetOption("sides", num_options
, options
)) != NULL
)
1117 ippAddString(request
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
, "sides",
1119 else if ((keyword
= cupsGetOption("Duplex", num_options
,
1122 if (!strcasecmp(keyword
, "None"))
1123 ippAddString(request
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
, "sides",
1125 else if (!strcasecmp(keyword
, "DuplexNoTumble"))
1126 ippAddString(request
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
, "sides",
1127 NULL
, "two-sided-long-edge");
1128 if (!strcasecmp(keyword
, "DuplexTumble"))
1129 ippAddString(request
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
, "sides",
1130 NULL
, "two-sided-short-edge");
1136 if (copies_sup
&& copies
> 1)
1137 ippAddInteger(request
, IPP_TAG_JOB
, IPP_TAG_INTEGER
, "copies", copies
);
1140 cupsFreeOptions(num_options
, options
);
1147 response
= cupsDoRequest(http
, request
, resource
);
1150 fputs("DEBUG: Sending file using chunking...\n", stderr
);
1151 http_status
= cupsSendRequest(http
, request
, resource
, 0);
1152 if (http_status
== HTTP_CONTINUE
&& request
->state
== IPP_DATA
)
1155 fd
= open(files
[0], O_RDONLY
);
1159 while ((bytes
= read(fd
, buffer
, sizeof(buffer
))) > 0)
1161 fprintf(stderr
, "DEBUG: Read %d bytes...\n", (int)bytes
);
1163 if (cupsWriteRequestData(http
, buffer
, bytes
) != HTTP_CONTINUE
)
1168 * Check for side-channel requests...
1171 backendCheckSideChannel(snmp_fd
, http
->hostaddr
);
1179 response
= cupsGetResponse(http
, resource
);
1183 ipp_status
= cupsLastError();
1185 if (ipp_status
> IPP_OK_CONFLICT
)
1192 if (ipp_status
== IPP_SERVICE_UNAVAILABLE
||
1193 ipp_status
== IPP_PRINTER_BUSY
)
1195 _cupsLangPrintFilter(stderr
, "INFO",
1196 _("Printer busy; will retry in 10 seconds."));
1202 * Update auth-info-required as needed...
1205 _cupsLangPrintFilter(stderr
, "ERROR",
1206 _("Print file was not accepted: %s"),
1207 cupsLastErrorString());
1209 if (ipp_status
== IPP_NOT_AUTHORIZED
|| ipp_status
== IPP_FORBIDDEN
)
1211 fprintf(stderr
, "DEBUG: WWW-Authenticate=\"%s\"\n",
1212 httpGetField(http
, HTTP_FIELD_WWW_AUTHENTICATE
));
1215 * Normal authentication goes through the password callback, which sets
1216 * auth_info_required to "username,password". Kerberos goes directly
1217 * through GSSAPI, so look for Negotiate in the WWW-Authenticate header
1218 * here and set auth_info_required as needed...
1221 if (!strncmp(httpGetField(http
, HTTP_FIELD_WWW_AUTHENTICATE
),
1223 auth_info_required
= "negotiate";
1227 else if ((job_id_attr
= ippFindAttribute(response
, "job-id",
1228 IPP_TAG_INTEGER
)) == NULL
)
1230 _cupsLangPrintFilter(stderr
, "INFO",
1231 _("Print file accepted - job ID unknown."));
1236 monitor
.job_id
= job_id
= job_id_attr
->values
[0].integer
;
1237 _cupsLangPrintFilter(stderr
, "INFO",
1238 _("Print file accepted - job ID %d."), job_id
);
1241 ippDelete(response
);
1246 if (job_id
&& num_files
> 1)
1248 for (i
= 0; i
< num_files
; i
++)
1251 * Check for side-channel requests...
1254 backendCheckSideChannel(snmp_fd
, http
->hostaddr
);
1257 * Send the next file in the job...
1260 request
= ippNewRequest(IPP_SEND_DOCUMENT
);
1261 request
->request
.op
.version
[0] = version
/ 10;
1262 request
->request
.op
.version
[1] = version
% 10;
1264 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
1267 ippAddInteger(request
, IPP_TAG_OPERATION
, IPP_TAG_INTEGER
, "job-id",
1271 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
1272 "requesting-user-name", NULL
, argv
[2]);
1274 if ((i
+ 1) == num_files
)
1275 ippAddBoolean(request
, IPP_TAG_OPERATION
, "last-document", 1);
1277 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_MIMETYPE
,
1278 "document-format", NULL
, content_type
);
1280 fprintf(stderr
, "DEBUG: Sending file %d using chunking...\n", i
+ 1);
1281 http_status
= cupsSendRequest(http
, request
, resource
, 0);
1282 if (http_status
== HTTP_CONTINUE
&& request
->state
== IPP_DATA
&&
1283 (fd
= open(files
[i
], O_RDONLY
)) >= 0)
1285 while ((bytes
= read(fd
, buffer
, sizeof(buffer
))) > 0)
1287 if (cupsWriteRequestData(http
, buffer
, bytes
) != HTTP_CONTINUE
)
1292 * Check for side-channel requests...
1295 backendCheckSideChannel(snmp_fd
, http
->hostaddr
);
1302 ippDelete(cupsGetResponse(http
, resource
));
1305 if (cupsLastError() > IPP_OK_CONFLICT
)
1307 ipp_status
= cupsLastError();
1309 _cupsLangPrintFilter(stderr
, "ERROR",
1310 _("Unable to add file to job: %s"),
1311 cupsLastErrorString());
1317 if (ipp_status
<= IPP_OK_CONFLICT
&& argc
> 6)
1319 fprintf(stderr
, "PAGE: 1 %d\n", copies_sup
? atoi(argv
[4]) : 1);
1320 copies_remaining
--;
1322 else if (ipp_status
== IPP_SERVICE_UNAVAILABLE
||
1323 ipp_status
== IPP_PRINTER_BUSY
)
1326 copies_remaining
--;
1329 * Wait for the job to complete...
1332 if (!job_id
|| !waitjob
)
1335 _cupsLangPrintFilter(stderr
, "INFO", _("Waiting for job to complete."));
1337 for (delay
= 1; !job_canceled
;)
1340 * Check for side-channel requests...
1343 backendCheckSideChannel(snmp_fd
, http
->hostaddr
);
1346 * Build an IPP_GET_JOB_ATTRIBUTES request...
1349 request
= ippNewRequest(IPP_GET_JOB_ATTRIBUTES
);
1350 request
->request
.op
.version
[0] = version
/ 10;
1351 request
->request
.op
.version
[1] = version
% 10;
1353 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
1356 ippAddInteger(request
, IPP_TAG_OPERATION
, IPP_TAG_INTEGER
, "job-id",
1360 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
1361 "requesting-user-name", NULL
, argv
[2]);
1363 ippAddStrings(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
1364 "requested-attributes", sizeof(jattrs
) / sizeof(jattrs
[0]),
1371 response
= cupsDoRequest(http
, request
, resource
);
1372 ipp_status
= cupsLastError();
1374 if (ipp_status
== IPP_NOT_FOUND
)
1377 * Job has gone away and/or the server has no job history...
1380 ippDelete(response
);
1382 ipp_status
= IPP_OK
;
1386 if (ipp_status
> IPP_OK_CONFLICT
)
1388 if (ipp_status
!= IPP_SERVICE_UNAVAILABLE
&&
1389 ipp_status
!= IPP_PRINTER_BUSY
)
1391 ippDelete(response
);
1393 _cupsLangPrintFilter(stderr
, "ERROR",
1394 _("Unable to get job attributes: %s"),
1395 cupsLastErrorString());
1402 if ((job_state
= ippFindAttribute(response
, "job-state",
1403 IPP_TAG_ENUM
)) != NULL
)
1406 * Stop polling if the job is finished or pending-held...
1409 if (job_state
->values
[0].integer
> IPP_JOB_STOPPED
)
1411 if ((job_sheets
= ippFindAttribute(response
,
1412 "job-media-sheets-completed",
1413 IPP_TAG_INTEGER
)) != NULL
)
1414 fprintf(stderr
, "PAGE: total %d\n",
1415 job_sheets
->values
[0].integer
);
1417 ippDelete(response
);
1424 * If the printer does not return a job-state attribute, it does not
1425 * conform to the IPP specification - break out immediately and fail
1429 fputs("DEBUG: No job-state available from printer - stopping queue.\n",
1431 ipp_status
= IPP_INTERNAL_ERROR
;
1436 ippDelete(response
);
1440 * Check the printer state and report it if necessary...
1443 check_printer_state(http
, uri
, resource
, argv
[2], version
, job_id
);
1447 * Wait 1-10 seconds before polling again...
1459 * Cancel the job as needed...
1462 if (job_canceled
&& job_id
)
1463 cancel_job(http
, uri
, job_id
, resource
, argv
[2], version
);
1466 * Check the printer state and report it if necessary...
1469 check_printer_state(http
, uri
, resource
, argv
[2], version
, job_id
);
1472 * Collect the final page count as needed...
1475 if (have_supplies
&&
1476 !backendSNMPSupplies(snmp_fd
, http
->hostaddr
, &page_count
, NULL
) &&
1477 page_count
> start_count
)
1478 fprintf(stderr
, "PAGE: total %d\n", page_count
- start_count
);
1482 * See if we used Kerberos at all...
1486 auth_info_required
= "negotiate";
1487 #endif /* HAVE_GSSAPI */
1495 ippDelete(supported
);
1498 * Remove the temporary file(s) if necessary...
1504 for (i
= 0; i
< num_files
; i
++)
1507 #endif /* HAVE_LIBZ */
1510 * Return the queue status...
1513 fprintf(stderr
, "ATTR: auth-info-required=%s\n", auth_info_required
);
1515 if (ipp_status
== IPP_NOT_AUTHORIZED
|| ipp_status
== IPP_FORBIDDEN
)
1516 return (CUPS_BACKEND_AUTH_REQUIRED
);
1517 else if (ipp_status
== IPP_INTERNAL_ERROR
)
1518 return (CUPS_BACKEND_STOP
);
1519 else if (ipp_status
> IPP_OK_CONFLICT
)
1520 return (CUPS_BACKEND_FAILED
);
1523 _cupsLangPrintFilter(stderr
, "INFO", _("Ready to print."));
1524 return (CUPS_BACKEND_OK
);
1530 * 'cancel_job()' - Cancel a print job.
1534 cancel_job(http_t
*http
, /* I - HTTP connection */
1535 const char *uri
, /* I - printer-uri */
1536 int id
, /* I - job-id */
1537 const char *resource
, /* I - Resource path */
1538 const char *user
, /* I - requesting-user-name */
1539 int version
) /* I - IPP version */
1541 ipp_t
*request
; /* Cancel-Job request */
1544 _cupsLangPrintFilter(stderr
, "INFO", _("Canceling print job."));
1546 request
= ippNewRequest(IPP_CANCEL_JOB
);
1547 request
->request
.op
.version
[0] = version
/ 10;
1548 request
->request
.op
.version
[1] = version
% 10;
1550 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
1552 ippAddInteger(request
, IPP_TAG_OPERATION
, IPP_TAG_INTEGER
, "job-id", id
);
1554 if (user
&& user
[0])
1555 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
1556 "requesting-user-name", NULL
, user
);
1562 ippDelete(cupsDoRequest(http
, request
, resource
));
1564 if (cupsLastError() > IPP_OK_CONFLICT
)
1565 _cupsLangPrintFilter(stderr
, "ERROR", _("Unable to cancel job: %s"),
1566 cupsLastErrorString());
1571 * 'check_printer_state()' - Check the printer state.
1574 static ipp_pstate_t
/* O - Current printer-state */
1575 check_printer_state(
1576 http_t
*http
, /* I - HTTP connection */
1577 const char *uri
, /* I - Printer URI */
1578 const char *resource
, /* I - Resource path */
1579 const char *user
, /* I - Username, if any */
1580 int version
, /* I - IPP version */
1583 ipp_t
*request
, /* IPP request */
1584 *response
; /* IPP response */
1585 ipp_attribute_t
*attr
; /* Attribute in response */
1586 ipp_pstate_t printer_state
= IPP_PRINTER_STOPPED
;
1587 /* Current printer-state */
1591 * Send a Get-Printer-Attributes request and log the results...
1594 request
= ippNewRequest(IPP_GET_PRINTER_ATTRIBUTES
);
1595 request
->request
.op
.version
[0] = version
/ 10;
1596 request
->request
.op
.version
[1] = version
% 10;
1598 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
1601 if (user
&& user
[0])
1602 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
1603 "requesting-user-name", NULL
, user
);
1605 ippAddStrings(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
1606 "requested-attributes",
1607 (int)(sizeof(pattrs
) / sizeof(pattrs
[0])), NULL
, pattrs
);
1609 if ((response
= cupsDoRequest(http
, request
, resource
)) != NULL
)
1611 report_printer_state(response
, job_id
);
1613 if ((attr
= ippFindAttribute(response
, "printer-state",
1614 IPP_TAG_ENUM
)) != NULL
)
1615 printer_state
= (ipp_pstate_t
)attr
->values
[0].integer
;
1617 ippDelete(response
);
1621 * Return the printer-state value...
1624 return (printer_state
);
1630 * 'compress_files()' - Compress print files...
1634 compress_files(int num_files
, /* I - Number of files */
1635 char **files
) /* I - Files */
1637 int i
, /* Looping var */
1638 fd
; /* Temporary file descriptor */
1639 ssize_t bytes
; /* Bytes read/written */
1640 size_t total
; /* Total bytes read */
1641 cups_file_t
*in
, /* Input file */
1642 *out
; /* Output file */
1643 struct stat outinfo
; /* Output file information */
1644 char filename
[1024], /* Temporary filename */
1645 buffer
[32768]; /* Copy buffer */
1648 fprintf(stderr
, "DEBUG: Compressing %d job files...\n", num_files
);
1649 for (i
= 0; i
< num_files
; i
++)
1651 if ((fd
= cupsTempFd(filename
, sizeof(filename
))) < 0)
1653 _cupsLangPrintError("ERROR", _("Unable to create compressed print file"));
1654 exit(CUPS_BACKEND_FAILED
);
1657 if ((out
= cupsFileOpenFd(fd
, "w9")) == NULL
)
1659 _cupsLangPrintError("ERROR", _("Unable to open compressed print file"));
1660 exit(CUPS_BACKEND_FAILED
);
1663 if ((in
= cupsFileOpen(files
[i
], "r")) == NULL
)
1665 _cupsLangPrintError("ERROR", _("Unable to open print file"));
1667 exit(CUPS_BACKEND_FAILED
);
1671 while ((bytes
= cupsFileRead(in
, buffer
, sizeof(buffer
))) > 0)
1672 if (cupsFileWrite(out
, buffer
, bytes
) < bytes
)
1674 _cupsLangPrintError("ERROR",
1675 _("Unable to generate compressed print file"));
1678 exit(CUPS_BACKEND_FAILED
);
1686 files
[i
] = strdup(filename
);
1688 if (!stat(filename
, &outinfo
))
1690 "DEBUG: File %d compressed to %.1f%% of original size, "
1691 CUPS_LLFMT
" bytes...\n",
1692 i
+ 1, 100.0 * outinfo
.st_size
/ total
,
1693 CUPS_LLCAST outinfo
.st_size
);
1696 #endif /* HAVE_LIBZ */
1700 * 'monitor_printer()' - Monitor the printer state...
1703 static void * /* O - Thread exit code */
1705 _cups_monitor_t
*monitor
) /* I - Monitoring data */
1707 http_t
*http
; /* Connection to printer */
1708 ipp_t
*request
, /* IPP request */
1709 *response
; /* IPP response */
1710 ipp_attribute_t
*attr
; /* Attribute in response */
1711 int delay
, /* Current delay */
1712 prev_delay
, /* Previous delay */
1713 temp_delay
; /* Temporary delay value */
1717 * Make a copy of the printer connection...
1720 http
= _httpCreate(monitor
->hostname
, monitor
->port
, monitor
->encryption
,
1722 cupsSetPasswordCB(password_cb
);
1725 * Loop until the job is canceled, aborted, or completed.
1731 while (monitor
->job_state
< IPP_JOB_CANCELED
&& !job_canceled
)
1734 * Reconnect to the printer...
1737 if (!httpReconnect(http
))
1740 * Connected, so check on the printer state...
1743 monitor
->printer_state
= check_printer_state(http
, monitor
->uri
,
1749 if (monitor
->job_id
> 0)
1752 * Check the status of the job itself...
1755 request
= ippNewRequest(IPP_GET_JOB_ATTRIBUTES
);
1756 request
->request
.op
.version
[0] = monitor
->version
/ 10;
1757 request
->request
.op
.version
[1] = monitor
->version
% 10;
1759 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
1760 NULL
, monitor
->uri
);
1761 ippAddInteger(request
, IPP_TAG_OPERATION
, IPP_TAG_INTEGER
, "job-id",
1764 if (monitor
->user
&& monitor
->user
[0])
1765 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
1766 "requesting-user-name", NULL
, monitor
->user
);
1768 ippAddStrings(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
1769 "requested-attributes",
1770 (int)(sizeof(jattrs
) / sizeof(jattrs
[0])), NULL
, jattrs
);
1776 response
= cupsDoRequest(http
, request
, monitor
->resource
);
1778 if ((attr
= ippFindAttribute(response
, "job-state",
1779 IPP_TAG_ENUM
)) != NULL
)
1780 monitor
->job_state
= (ipp_jstate_t
)attr
->values
[0].integer
;
1782 monitor
->job_state
= IPP_JOB_COMPLETED
;
1784 ippDelete(response
);
1788 * Disconnect from the printer - we'll reconnect on the next poll...
1791 _httpDisconnect(http
);
1795 * Sleep for N seconds, and then update the next sleep time using a
1796 * Fibonacci series (1 1 2 3 5 8)...
1802 delay
= (delay
+ prev_delay
) % 12;
1803 prev_delay
= delay
< temp_delay
? 0 : temp_delay
;
1807 * Cleanup and return...
1817 * 'password_cb()' - Disable the password prompt for cupsDoFileRequest().
1820 static const char * /* O - Password */
1821 password_cb(const char *prompt
) /* I - Prompt (not used) */
1826 * Remember that we need to authenticate...
1829 auth_info_required
= "username,password";
1831 if (password
&& *password
&& password_tries
< 3)
1840 * Give up after 3 tries or if we don't have a password to begin with...
1849 * 'report_attr()' - Report an IPP attribute value.
1853 report_attr(ipp_attribute_t
*attr
) /* I - Attribute */
1855 int i
; /* Looping var */
1856 char value
[1024], /* Value string */
1857 *valptr
, /* Pointer into value string */
1858 *attrptr
; /* Pointer into attribute value */
1862 * Convert the attribute values into quoted strings...
1865 for (i
= 0, valptr
= value
;
1866 i
< attr
->num_values
&& valptr
< (value
+ sizeof(value
) - 10);
1872 switch (attr
->value_tag
)
1874 case IPP_TAG_INTEGER
:
1876 snprintf(valptr
, sizeof(value
) - (valptr
- value
), "%d",
1877 attr
->values
[i
].integer
);
1878 valptr
+= strlen(valptr
);
1883 case IPP_TAG_KEYWORD
:
1885 for (attrptr
= attr
->values
[i
].string
.text
;
1886 *attrptr
&& valptr
< (value
+ sizeof(value
) - 10);
1889 if (*attrptr
== '\\' || *attrptr
== '\"')
1892 *valptr
++ = *attrptr
;
1899 * Unsupported value type...
1909 * Tell the scheduler about the new values...
1912 fprintf(stderr
, "ATTR: %s=%s\n", attr
->name
, value
);
1917 * 'report_printer_state()' - Report the printer state.
1920 static int /* O - Number of reasons shown */
1921 report_printer_state(ipp_t
*ipp
, /* I - IPP response */
1922 int job_id
) /* I - Current job ID */
1924 int i
; /* Looping var */
1925 int count
; /* Count of reasons shown... */
1926 ipp_attribute_t
*pa
, /* printer-alert */
1927 *pam
, /* printer-alert-message */
1928 *psm
, /* printer-state-message */
1929 *reasons
, /* printer-state-reasons */
1930 *marker
; /* marker-* attributes */
1931 const char *reason
; /* Current reason */
1932 const char *prefix
; /* Prefix for STATE: line */
1933 char value
[1024], /* State/message string */
1934 *valptr
; /* Pointer into string */
1938 * Report alerts and messages...
1941 if ((pa
= ippFindAttribute(ipp
, "printer-alert", IPP_TAG_TEXT
)) != NULL
)
1944 if ((pam
= ippFindAttribute(ipp
, "printer-alert-message",
1945 IPP_TAG_TEXT
)) != NULL
)
1948 if ((psm
= ippFindAttribute(ipp
, "printer-state-message",
1949 IPP_TAG_TEXT
)) != NULL
)
1951 char *ptr
; /* Pointer into message */
1954 strlcpy(value
, "INFO: ", sizeof(value
));
1955 for (ptr
= psm
->values
[0].string
.text
, valptr
= value
+ 6;
1956 *ptr
&& valptr
< (value
+ sizeof(value
) - 6);
1959 if (*ptr
< ' ' && *ptr
> 0 && *ptr
!= '\t')
1962 * Substitute "<XX>" for the control character; sprintf is safe because
1963 * we always leave 6 chars free at the end...
1966 sprintf(valptr
, "<%02X>", *ptr
);
1976 fputs(value
, stderr
);
1980 * Now report printer-state-reasons, filtering out some of the reasons we never
1984 if ((reasons
= ippFindAttribute(ipp
, "printer-state-reasons",
1985 IPP_TAG_KEYWORD
)) == NULL
)
1991 for (i
= 0, count
= 0, valptr
= value
; i
< reasons
->num_values
; i
++)
1993 reason
= reasons
->values
[i
].string
.text
;
1995 if (strcmp(reason
, "paused") &&
1996 strcmp(reason
, "com.apple.print.recoverable-warning"))
1998 strlcpy(valptr
, prefix
, sizeof(value
) - (valptr
- value
) - 1);
1999 valptr
+= strlen(valptr
);
2000 strlcpy(valptr
, reason
, sizeof(value
) - (valptr
- value
) - 1);
2001 valptr
+= strlen(valptr
);
2011 fputs(value
, stderr
);
2015 * Relay the current marker-* attribute values...
2018 if ((marker
= ippFindAttribute(ipp
, "marker-colors", IPP_TAG_NAME
)) != NULL
)
2019 report_attr(marker
);
2020 if ((marker
= ippFindAttribute(ipp
, "marker-high-levels",
2021 IPP_TAG_INTEGER
)) != NULL
)
2022 report_attr(marker
);
2023 if ((marker
= ippFindAttribute(ipp
, "marker-levels",
2024 IPP_TAG_INTEGER
)) != NULL
)
2025 report_attr(marker
);
2026 if ((marker
= ippFindAttribute(ipp
, "marker-low-levels",
2027 IPP_TAG_INTEGER
)) != NULL
)
2028 report_attr(marker
);
2029 if ((marker
= ippFindAttribute(ipp
, "marker-message", IPP_TAG_TEXT
)) != NULL
)
2030 report_attr(marker
);
2031 if ((marker
= ippFindAttribute(ipp
, "marker-names", IPP_TAG_NAME
)) != NULL
)
2032 report_attr(marker
);
2033 if ((marker
= ippFindAttribute(ipp
, "marker-types", IPP_TAG_KEYWORD
)) != NULL
)
2034 report_attr(marker
);
2041 * 'sigterm_handler()' - Handle 'terminate' signals that stop the backend.
2045 sigterm_handler(int sig
) /* I - Signal */
2047 (void)sig
; /* remove compiler warnings... */
2052 * Flag that the job should be cancelled...
2064 * End of "$Id: ipp.c 7948 2008-09-17 00:04:12Z mike $".