2 * "$Id: ipp.c 7948 2008-09-17 00:04:12Z mike $"
4 * IPP backend for CUPS.
6 * Copyright 2007-2011 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 * new_request() - Create a new print creation or validation request.
25 * password_cb() - Disable the password prompt for
26 * cupsDoFileRequest().
27 * report_attr() - Report an IPP attribute value.
28 * report_printer_state() - Report the printer state.
29 * sigterm_handler() - Handle 'terminate' signals that stop the backend.
33 * Include necessary headers.
36 #include "backend-private.h"
37 #include <sys/types.h>
46 typedef struct _cups_monitor_s
/**** Monitoring data ****/
48 const char *uri
, /* Printer URI */
49 *hostname
, /* Hostname */
51 *resource
; /* Resource path */
52 int port
, /* Port number */
53 version
, /* IPP version */
54 job_id
; /* Job ID for submitted job */
55 http_encryption_t encryption
; /* Use encryption? */
56 ipp_jstate_t job_state
; /* Current job state */
57 ipp_pstate_t printer_state
; /* Current printer state */
65 static const char *auth_info_required
= "none";
66 /* New auth-info-required value */
67 static const char * const jattrs
[] = /* Job attributes we want */
69 "job-media-sheets-completed",
73 static int job_canceled
= 0; /* Job cancelled? */
74 static char *password
= NULL
; /* Password for device URI */
75 static int password_tries
= 0; /* Password tries */
76 static const char * const pattrs
[] = /* Printer attributes we want */
80 "document-format-supported",
88 "media-col-supported",
90 "printer-alert-description",
91 "printer-is-accepting-jobs",
93 "printer-state-message",
94 "printer-state-reasons",
102 static void cancel_job(http_t
*http
, const char *uri
, int id
,
103 const char *resource
, const char *user
,
105 static ipp_pstate_t
check_printer_state(http_t
*http
, const char *uri
,
106 const char *resource
,
107 const char *user
, int version
,
110 static void compress_files(int num_files
, char **files
);
111 #endif /* HAVE_LIBZ */
112 static void *monitor_printer(_cups_monitor_t
*monitor
);
113 static ipp_t
*new_request(ipp_op_t op
, int version
, const char *uri
,
114 const char *user
, const char *title
,
115 int num_options
, cups_option_t
*options
,
116 const char *compression
, int copies
,
117 const char *format
, _pwg_t
*pwg
,
118 ipp_attribute_t
*media_col_sup
);
119 static const char *password_cb(const char *);
120 static void report_attr(ipp_attribute_t
*attr
);
121 static int report_printer_state(ipp_t
*ipp
, int job_id
);
122 static void sigterm_handler(int sig
);
126 * 'main()' - Send a file to the printer or server.
130 * printer-uri job-id user title copies options [file]
133 int /* O - Exit status */
134 main(int argc
, /* I - Number of command-line args */
135 char *argv
[]) /* I - Command-line arguments */
137 int i
; /* Looping var */
138 int send_options
; /* Send job options? */
139 int num_options
; /* Number of printer options */
140 cups_option_t
*options
; /* Printer options */
141 const char *device_uri
; /* Device URI */
142 char scheme
[255], /* Scheme in URI */
143 hostname
[1024], /* Hostname */
144 username
[255], /* Username info */
145 resource
[1024], /* Resource info (printer name) */
146 addrname
[256], /* Address name */
147 *optptr
, /* Pointer to URI options */
148 *name
, /* Name of option */
149 *value
, /* Value of option */
150 sep
; /* Separator character */
151 int snmp_fd
, /* SNMP socket */
152 start_count
, /* Page count via SNMP at start */
153 page_count
, /* Page count via SNMP */
154 have_supplies
; /* Printer supports supply levels? */
155 int num_files
; /* Number of files to print */
156 char **files
; /* Files to print */
157 int port
; /* Port number (not used) */
158 char uri
[HTTP_MAX_URI
]; /* Updated URI without user/pass */
159 http_status_t http_status
; /* Status of HTTP request */
160 ipp_status_t ipp_status
; /* Status of IPP request */
161 http_t
*http
; /* HTTP connection */
162 ipp_t
*request
, /* IPP request */
163 *response
, /* IPP response */
164 *supported
; /* get-printer-attributes response */
165 time_t start_time
; /* Time of first connect */
166 int contimeout
; /* Connection timeout */
167 int delay
; /* Delay for retries... */
168 const char *compression
; /* Compression mode */
169 int waitjob
, /* Wait for job complete? */
170 waitprinter
; /* Wait for printer ready? */
171 _cups_monitor_t monitor
; /* Monitoring data */
172 ipp_attribute_t
*job_id_attr
; /* job-id attribute */
173 int job_id
; /* job-id value */
174 ipp_attribute_t
*job_sheets
; /* job-media-sheets-completed */
175 ipp_attribute_t
*job_state
; /* job-state */
176 ipp_attribute_t
*copies_sup
; /* copies-supported */
177 ipp_attribute_t
*cups_version
; /* cups-version */
178 ipp_attribute_t
*format_sup
; /* document-format-supported */
179 ipp_attribute_t
*media_col_sup
; /* media-col-supported */
180 ipp_attribute_t
*operations_sup
; /* operations-supported */
181 ipp_attribute_t
*printer_state
; /* printer-state attribute */
182 ipp_attribute_t
*printer_accepting
; /* printer-is-accepting-jobs */
183 int validate_job
; /* Does printer support Validate-Job? */
184 int copies
, /* Number of copies for job */
185 copies_remaining
; /* Number of copies remaining */
186 const char *content_type
, /* CONTENT_TYPE environment variable */
187 *final_content_type
, /* FINAL_CONTENT_TYPE environment var */
188 *document_format
; /* document-format value */
189 int fd
; /* File descriptor */
190 off_t bytes
; /* Bytes copied */
191 char buffer
[16384]; /* Copy buffer */
192 #if defined(HAVE_SIGACTION) && !defined(HAVE_SIGSET)
193 struct sigaction action
; /* Actions for POSIX signals */
194 #endif /* HAVE_SIGACTION && !HAVE_SIGSET */
195 int version
; /* IPP version */
196 ppd_file_t
*ppd
; /* PPD file */
197 _pwg_t
*pwg
; /* PWG<->PPD mapping data */
201 * Make sure status messages are not buffered...
204 setbuf(stderr
, NULL
);
207 * Ignore SIGPIPE and catch SIGTERM signals...
211 sigset(SIGPIPE
, SIG_IGN
);
212 sigset(SIGTERM
, sigterm_handler
);
213 #elif defined(HAVE_SIGACTION)
214 memset(&action
, 0, sizeof(action
));
215 action
.sa_handler
= SIG_IGN
;
216 sigaction(SIGPIPE
, &action
, NULL
);
218 sigemptyset(&action
.sa_mask
);
219 sigaddset(&action
.sa_mask
, SIGTERM
);
220 action
.sa_handler
= sigterm_handler
;
221 sigaction(SIGTERM
, &action
, NULL
);
223 signal(SIGPIPE
, SIG_IGN
);
224 signal(SIGTERM
, sigterm_handler
);
225 #endif /* HAVE_SIGSET */
228 * Check command-line...
235 if ((s
= strrchr(argv
[0], '/')) != NULL
)
240 printf("network %s \"Unknown\" \"%s (%s)\"\n",
241 s
, _cupsLangString(cupsLangDefault(),
242 _("Internet Printing Protocol")), s
);
243 return (CUPS_BACKEND_OK
);
247 _cupsLangPrintf(stderr
,
248 _("Usage: %s job-id user title copies options [file]"),
250 return (CUPS_BACKEND_STOP
);
254 * Get the (final) content type...
257 if ((content_type
= getenv("CONTENT_TYPE")) == NULL
)
258 content_type
= "application/octet-stream";
260 if ((final_content_type
= getenv("FINAL_CONTENT_TYPE")) == NULL
)
262 final_content_type
= content_type
;
264 if (!strncmp(final_content_type
, "printer/", 8))
265 final_content_type
= "application/vnd.cups-raw";
269 * Extract the hostname and printer name from the URI...
272 while ((device_uri
= cupsBackendDeviceURI(argv
)) == NULL
)
274 _cupsLangPrintFilter(stderr
, "INFO", _("Unable to locate printer."));
277 if (getenv("CLASS") != NULL
)
278 return (CUPS_BACKEND_FAILED
);
281 httpSeparateURI(HTTP_URI_CODING_ALL
, device_uri
, scheme
, sizeof(scheme
),
282 username
, sizeof(username
), hostname
, sizeof(hostname
), &port
,
283 resource
, sizeof(resource
));
286 port
= IPP_PORT
; /* Default to port 631 */
288 if (!strcmp(scheme
, "https"))
289 cupsSetEncryption(HTTP_ENCRYPT_ALWAYS
);
291 cupsSetEncryption(HTTP_ENCRYPT_IF_REQUESTED
);
294 * See if there are any options...
301 contimeout
= 7 * 24 * 60 * 60;
303 if ((optptr
= strchr(resource
, '?')) != NULL
)
306 * Yup, terminate the device name string and move to the first
307 * character of the optptr...
313 * Then parse the optptr...
324 while (*optptr
&& *optptr
!= '=' && *optptr
!= '+' && *optptr
!= '&')
327 if ((sep
= *optptr
) != '\0')
338 while (*optptr
&& *optptr
!= '+' && *optptr
!= '&')
348 * Process the option...
351 if (!strcasecmp(name
, "waitjob"))
354 * Wait for job completion?
357 waitjob
= !strcasecmp(value
, "on") ||
358 !strcasecmp(value
, "yes") ||
359 !strcasecmp(value
, "true");
361 else if (!strcasecmp(name
, "waitprinter"))
364 * Wait for printer idle?
367 waitprinter
= !strcasecmp(value
, "on") ||
368 !strcasecmp(value
, "yes") ||
369 !strcasecmp(value
, "true");
371 else if (!strcasecmp(name
, "encryption"))
374 * Enable/disable encryption?
377 if (!strcasecmp(value
, "always"))
378 cupsSetEncryption(HTTP_ENCRYPT_ALWAYS
);
379 else if (!strcasecmp(value
, "required"))
380 cupsSetEncryption(HTTP_ENCRYPT_REQUIRED
);
381 else if (!strcasecmp(value
, "never"))
382 cupsSetEncryption(HTTP_ENCRYPT_NEVER
);
383 else if (!strcasecmp(value
, "ifrequested"))
384 cupsSetEncryption(HTTP_ENCRYPT_IF_REQUESTED
);
387 _cupsLangPrintFilter(stderr
, "ERROR",
388 _("Unknown encryption option value: \"%s\"."),
392 else if (!strcasecmp(name
, "version"))
394 if (!strcmp(value
, "1.0"))
396 else if (!strcmp(value
, "1.1"))
398 else if (!strcmp(value
, "2.0"))
400 else if (!strcmp(value
, "2.1"))
402 else if (!strcmp(value
, "2.2"))
406 _cupsLangPrintFilter(stderr
, "ERROR",
407 _("Unknown version option value: \"%s\"."),
412 else if (!strcasecmp(name
, "compression"))
414 if (!strcasecmp(value
, "true") || !strcasecmp(value
, "yes") ||
415 !strcasecmp(value
, "on") || !strcasecmp(value
, "gzip"))
416 compression
= "gzip";
418 #endif /* HAVE_LIBZ */
419 else if (!strcasecmp(name
, "contimeout"))
422 * Set the connection timeout...
426 contimeout
= atoi(value
);
434 _cupsLangPrintFilter(stderr
, "ERROR",
435 _("Unknown option \"%s\" with value \"%s\"."),
442 * If we have 7 arguments, print the file named on the command-line.
443 * Otherwise, copy stdin to a temporary file and print the temporary
450 send_options
= !strcasecmp(final_content_type
, "application/pdf") ||
451 !strcasecmp(final_content_type
, "application/vnd.cups-pdf") ||
452 !strncasecmp(final_content_type
, "image/", 6);
454 fputs("DEBUG: Sending stdin for job...\n", stderr
);
459 * Point to the files on the command-line...
462 num_files
= argc
- 6;
468 compress_files(num_files
, files
);
469 #endif /* HAVE_LIBZ */
471 fprintf(stderr
, "DEBUG: %d files to send in job...\n", num_files
);
475 * Set the authentication info, if any...
478 cupsSetPasswordCB(password_cb
);
483 * Use authenticaion information in the device URI...
486 if ((password
= strchr(username
, ':')) != NULL
)
489 cupsSetUser(username
);
494 * Try loading authentication information from the environment.
497 const char *ptr
= getenv("AUTH_USERNAME");
502 password
= getenv("AUTH_PASSWORD");
506 * Try connecting to the remote server...
510 start_time
= time(NULL
);
512 fputs("STATE: +connecting-to-device\n", stderr
);
516 fprintf(stderr
, "DEBUG: Connecting to %s:%d\n", hostname
, port
);
517 _cupsLangPrintFilter(stderr
, "INFO", _("Connecting to printer."));
519 if ((http
= httpConnectEncrypt(hostname
, port
,
520 cupsEncryption())) == NULL
)
522 #if 0 /* These need to go in here someplace when we see HTTP_PKI_ERROR or IPP_PKI_ERROR */
523 fputs("STATE: +cups-certificate-error\n", stderr
);
524 fputs("STATE: -cups-certificate-error\n", stderr
);
527 int error
= errno
; /* Connection error */
532 if (getenv("CLASS") != NULL
)
535 * If the CLASS environment variable is set, the job was submitted
536 * to a class and not to a specific queue. In this case, we want
537 * to abort immediately so that the job can be requeued on the next
538 * available printer in the class.
541 _cupsLangPrintFilter(stderr
, "INFO",
542 _("Unable to contact printer, queuing on next "
543 "printer in class."));
546 * Sleep 5 seconds to keep the job from requeuing too rapidly...
551 return (CUPS_BACKEND_FAILED
);
554 fprintf(stderr
, "DEBUG: Connection error: %s\n", strerror(errno
));
556 if (errno
== ECONNREFUSED
|| errno
== EHOSTDOWN
||
557 errno
== EHOSTUNREACH
)
559 if (contimeout
&& (time(NULL
) - start_time
) > contimeout
)
561 _cupsLangPrintFilter(stderr
, "ERROR",
562 _("The printer is not responding."));
563 return (CUPS_BACKEND_FAILED
);
569 _cupsLangPrintFilter(stderr
, "WARNING",
570 _("Network printer \"%s\" may not exist or "
571 "is unavailable at this time."),
576 _cupsLangPrintFilter(stderr
, "WARNING",
577 _("Network printer \"%s\" is unreachable at "
578 "this time."), hostname
);
583 _cupsLangPrintFilter(stderr
, "WARNING",
584 _("Network printer \"%s\" is busy."),
596 _cupsLangPrintFilter(stderr
, "ERROR",
597 _("Unable to locate network printer \"%s\"."),
599 return (CUPS_BACKEND_STOP
);
603 _cupsLangPrintFilter(stderr
, "ERROR",
604 _("Network printer \"%s\" is not responding."),
613 while (http
== NULL
);
615 if (job_canceled
|| !http
)
616 return (CUPS_BACKEND_FAILED
);
618 fputs("STATE: -connecting-to-device\n", stderr
);
619 _cupsLangPrintFilter(stderr
, "INFO", _("Connected to printer."));
622 if (http
->hostaddr
->addr
.sa_family
== AF_INET6
)
623 fprintf(stderr
, "DEBUG: Connected to [%s]:%d (IPv6)...\n",
624 httpAddrString(http
->hostaddr
, addrname
, sizeof(addrname
)),
625 ntohs(http
->hostaddr
->ipv6
.sin6_port
));
627 #endif /* AF_INET6 */
628 if (http
->hostaddr
->addr
.sa_family
== AF_INET
)
629 fprintf(stderr
, "DEBUG: Connected to %s:%d (IPv4)...\n",
630 httpAddrString(http
->hostaddr
, addrname
, sizeof(addrname
)),
631 ntohs(http
->hostaddr
->ipv4
.sin_port
));
634 * See if the printer supports SNMP...
637 if ((snmp_fd
= _cupsSNMPOpen(http
->hostaddr
->addr
.sa_family
)) >= 0)
638 have_supplies
= !backendSNMPSupplies(snmp_fd
, http
->hostaddr
, &start_count
,
641 have_supplies
= start_count
= 0;
644 * Build a URI for the printer and fill the standard IPP attributes for
645 * an IPP_PRINT_FILE request. We can't use the URI in argv[0] because it
646 * might contain username:password information...
649 httpAssembleURI(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), scheme
, NULL
, hostname
,
653 * First validate the destination and see if the device supports multiple
660 media_col_sup
= NULL
;
662 operations_sup
= NULL
;
668 * Check for side-channel requests...
671 backendCheckSideChannel(snmp_fd
, http
->hostaddr
);
674 * Build the IPP request...
677 request
= ippNewRequest(IPP_GET_PRINTER_ATTRIBUTES
);
678 request
->request
.op
.version
[0] = version
/ 10;
679 request
->request
.op
.version
[1] = version
% 10;
681 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
684 ippAddStrings(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
685 "requested-attributes", sizeof(pattrs
) / sizeof(pattrs
[0]),
692 fputs("DEBUG: Getting supported attributes...\n", stderr
);
694 if (http
->version
< HTTP_1_1
)
696 fprintf(stderr
, "DEBUG: Printer responded with HTTP version %d.%d.\n",
697 http
->version
/ 100, http
->version
% 100);
699 _cupsLangPrintFilter(stderr
, "ERROR",
700 _("Unable to print: the printer does not conform to "
701 "the IPP standard."));
702 exit(CUPS_BACKEND_STOP
);
705 supported
= cupsDoRequest(http
, request
, resource
);
706 ipp_status
= cupsLastError();
708 if (ipp_status
> IPP_OK_CONFLICT
)
710 fprintf(stderr
, "DEBUG: Get-Printer-Attributes returned %s.\n",
711 ippErrorString(ipp_status
));
713 if (ipp_status
== IPP_PRINTER_BUSY
||
714 ipp_status
== IPP_SERVICE_UNAVAILABLE
)
716 if (contimeout
&& (time(NULL
) - start_time
) > contimeout
)
718 _cupsLangPrintFilter(stderr
, "ERROR",
719 _("The printer is not responding."));
720 return (CUPS_BACKEND_FAILED
);
723 _cupsLangPrintFilter(stderr
, "WARNING",
724 _("Network host \"%s\" is busy; will retry in %d "
725 "seconds."), hostname
, delay
);
727 report_printer_state(supported
, 0);
734 else if ((ipp_status
== IPP_BAD_REQUEST
||
735 ipp_status
== IPP_VERSION_NOT_SUPPORTED
) && version
> 10)
738 * Switch to IPP/1.1 or IPP/1.0...
743 _cupsLangPrintFilter(stderr
, "INFO",
744 _("Printer does not support IPP/%d.%d, trying "
745 "IPP/%s."), version
/ 10, version
% 10, "1.1");
750 _cupsLangPrintFilter(stderr
, "INFO",
751 _("Printer does not support IPP/%d.%d, trying "
752 "IPP/%s."), version
/ 10, version
% 10, "1.0");
758 else if (ipp_status
== IPP_NOT_FOUND
)
760 _cupsLangPrintFilter(stderr
, "ERROR",
761 _("The printer URI is incorrect or no longer "
765 ippDelete(supported
);
767 return (CUPS_BACKEND_STOP
);
769 else if (ipp_status
== IPP_NOT_AUTHORIZED
|| ipp_status
== IPP_FORBIDDEN
)
771 if (!strncmp(httpGetField(http
, HTTP_FIELD_WWW_AUTHENTICATE
),
773 auth_info_required
= "negotiate";
775 fprintf(stderr
, "ATTR: auth-info-required=%s\n", auth_info_required
);
776 return (CUPS_BACKEND_AUTH_REQUIRED
);
780 _cupsLangPrintFilter(stderr
, "ERROR",
781 _("Unable to get printer status: %s"),
782 cupsLastErrorString());
787 ippDelete(supported
);
793 * Check for supported attributes...
796 if ((copies_sup
= ippFindAttribute(supported
, "copies-supported",
797 IPP_TAG_RANGE
)) != NULL
)
800 * Has the "copies-supported" attribute - does it have an upper
804 fprintf(stderr
, "DEBUG: copies-supported=%d-%d\n",
805 copies_sup
->values
[0].range
.lower
,
806 copies_sup
->values
[0].range
.upper
);
808 if (copies_sup
->values
[0].range
.upper
<= 1)
809 copies_sup
= NULL
; /* No */
812 cups_version
= ippFindAttribute(supported
, "cups-version", IPP_TAG_TEXT
);
814 if ((format_sup
= ippFindAttribute(supported
, "document-format-supported",
815 IPP_TAG_MIMETYPE
)) != NULL
)
817 fprintf(stderr
, "DEBUG: document-format-supported (%d values)\n",
818 format_sup
->num_values
);
819 for (i
= 0; i
< format_sup
->num_values
; i
++)
820 fprintf(stderr
, "DEBUG: [%d] = \"%s\"\n", i
,
821 format_sup
->values
[i
].string
.text
);
824 if ((media_col_sup
= ippFindAttribute(supported
, "media-col-supported",
825 IPP_TAG_KEYWORD
)) != NULL
)
827 fprintf(stderr
, "DEBUG: media-col-supported (%d values)\n",
828 media_col_sup
->num_values
);
829 for (i
= 0; i
< media_col_sup
->num_values
; i
++)
830 fprintf(stderr
, "DEBUG: [%d] = \"%s\"\n", i
,
831 media_col_sup
->values
[i
].string
.text
);
834 if ((operations_sup
= ippFindAttribute(supported
, "operations-supported",
835 IPP_TAG_ENUM
)) != NULL
)
837 for (i
= 0; i
< operations_sup
->num_values
; i
++)
838 if (operations_sup
->values
[i
].integer
== IPP_VALIDATE_JOB
)
846 _cupsLangPrintFilter(stderr
, "WARNING",
847 _("This printer does not conform to the IPP "
848 "standard and may not work."));
849 fputs("DEBUG: operations-supported does not list Validate-Job.\n",
855 _cupsLangPrintFilter(stderr
, "WARNING",
856 _("This printer does not conform to the IPP "
857 "standard and may not work."));
858 fputs("DEBUG: operations-supported not returned in "
859 "Get-Printer-Attributes request.\n", stderr
);
862 report_printer_state(supported
, 0);
864 while (ipp_status
> IPP_OK_CONFLICT
);
867 * See if the printer is accepting jobs and is not stopped; if either
868 * condition is true and we are printing to a class, requeue the job...
871 if (getenv("CLASS") != NULL
)
873 printer_state
= ippFindAttribute(supported
, "printer-state",
875 printer_accepting
= ippFindAttribute(supported
, "printer-is-accepting-jobs",
878 if (printer_state
== NULL
||
879 (printer_state
->values
[0].integer
> IPP_PRINTER_PROCESSING
&&
881 printer_accepting
== NULL
||
882 !printer_accepting
->values
[0].boolean
)
885 * If the CLASS environment variable is set, the job was submitted
886 * to a class and not to a specific queue. In this case, we want
887 * to abort immediately so that the job can be requeued on the next
888 * available printer in the class.
891 _cupsLangPrintFilter(stderr
, "INFO",
892 _("Unable to contact printer, queuing on next "
893 "printer in class."));
895 ippDelete(supported
);
899 * Sleep 5 seconds to keep the job from requeuing too rapidly...
904 return (CUPS_BACKEND_FAILED
);
909 * See if the printer supports multiple copies...
912 copies
= atoi(argv
[4]);
914 if (copies_sup
|| argc
< 7)
916 copies_remaining
= 1;
922 copies_remaining
= copies
;
925 * Prepare remaining printing options...
933 num_options
= cupsParseOptions(argv
[5], 0, &options
);
935 if (!cups_version
&& media_col_sup
)
938 * Load the PPD file and generate PWG attribute mapping information...
941 ppd
= ppdOpenFile(getenv("PPD"));
942 pwg
= _pwgCreateWithPPD(ppd
);
950 document_format
= NULL
;
952 if (format_sup
!= NULL
)
954 for (i
= 0; i
< format_sup
->num_values
; i
++)
955 if (!strcasecmp(final_content_type
, format_sup
->values
[i
].string
.text
))
957 document_format
= final_content_type
;
963 * Start monitoring the printer in the background...
967 monitor
.hostname
= hostname
;
968 monitor
.user
= argv
[2];
969 monitor
.resource
= resource
;
971 monitor
.version
= version
;
973 monitor
.encryption
= cupsEncryption();
974 monitor
.job_state
= IPP_JOB_PENDING
;
975 monitor
.printer_state
= IPP_PRINTER_IDLE
;
977 _cupsThreadCreate((_cups_thread_func_t
)monitor_printer
, &monitor
);
980 * Validate access to the printer...
983 while (!job_canceled
)
985 request
= new_request(IPP_VALIDATE_JOB
, version
, uri
, argv
[2], argv
[3],
986 num_options
, options
, compression
,
987 copies_sup
? copies
: 1, document_format
, pwg
,
990 ippDelete(cupsDoRequest(http
, request
, resource
));
992 ipp_status
= cupsLastError();
994 if (ipp_status
> IPP_OK_CONFLICT
)
999 if (ipp_status
== IPP_SERVICE_UNAVAILABLE
||
1000 ipp_status
== IPP_PRINTER_BUSY
)
1002 _cupsLangPrintFilter(stderr
, "INFO",
1003 _("Printer busy; will retry in 10 seconds."));
1009 * Update auth-info-required as needed...
1012 _cupsLangPrintFilter(stderr
, "ERROR", "%s", cupsLastErrorString());
1014 if (ipp_status
== IPP_NOT_AUTHORIZED
|| ipp_status
== IPP_FORBIDDEN
)
1016 fprintf(stderr
, "DEBUG: WWW-Authenticate=\"%s\"\n",
1017 httpGetField(http
, HTTP_FIELD_WWW_AUTHENTICATE
));
1020 * Normal authentication goes through the password callback, which sets
1021 * auth_info_required to "username,password". Kerberos goes directly
1022 * through GSSAPI, so look for Negotiate in the WWW-Authenticate header
1023 * here and set auth_info_required as needed...
1026 if (!strncmp(httpGetField(http
, HTTP_FIELD_WWW_AUTHENTICATE
),
1028 auth_info_required
= "negotiate";
1039 * Then issue the print-job request...
1044 while (!job_canceled
&& copies_remaining
> 0)
1047 * Check for side-channel requests...
1050 backendCheckSideChannel(snmp_fd
, http
->hostaddr
);
1053 * Build the IPP job creation request...
1059 request
= new_request(num_files
> 1 ? IPP_CREATE_JOB
: IPP_PRINT_JOB
,
1060 version
, uri
, argv
[2], argv
[3], num_options
, options
,
1061 compression
, copies_sup
? copies
: 1, document_format
,
1062 pwg
, media_col_sup
);
1069 response
= cupsDoRequest(http
, request
, resource
);
1072 fputs("DEBUG: Sending file using chunking...\n", stderr
);
1073 http_status
= cupsSendRequest(http
, request
, resource
, 0);
1074 if (http_status
== HTTP_CONTINUE
&& request
->state
== IPP_DATA
)
1077 fd
= open(files
[0], O_RDONLY
);
1081 while ((bytes
= read(fd
, buffer
, sizeof(buffer
))) > 0)
1083 fprintf(stderr
, "DEBUG: Read %d bytes...\n", (int)bytes
);
1085 if (cupsWriteRequestData(http
, buffer
, bytes
) != HTTP_CONTINUE
)
1090 * Check for side-channel requests...
1093 backendCheckSideChannel(snmp_fd
, http
->hostaddr
);
1101 response
= cupsGetResponse(http
, resource
);
1105 ipp_status
= cupsLastError();
1107 if (ipp_status
> IPP_OK_CONFLICT
)
1114 if (ipp_status
== IPP_SERVICE_UNAVAILABLE
||
1115 ipp_status
== IPP_PRINTER_BUSY
)
1117 _cupsLangPrintFilter(stderr
, "INFO",
1118 _("Printer busy; will retry in 10 seconds."));
1124 * Update auth-info-required as needed...
1127 _cupsLangPrintFilter(stderr
, "ERROR",
1128 _("Print file was not accepted: %s"),
1129 cupsLastErrorString());
1131 if (ipp_status
== IPP_NOT_AUTHORIZED
|| ipp_status
== IPP_FORBIDDEN
)
1133 fprintf(stderr
, "DEBUG: WWW-Authenticate=\"%s\"\n",
1134 httpGetField(http
, HTTP_FIELD_WWW_AUTHENTICATE
));
1137 * Normal authentication goes through the password callback, which sets
1138 * auth_info_required to "username,password". Kerberos goes directly
1139 * through GSSAPI, so look for Negotiate in the WWW-Authenticate header
1140 * here and set auth_info_required as needed...
1143 if (!strncmp(httpGetField(http
, HTTP_FIELD_WWW_AUTHENTICATE
),
1145 auth_info_required
= "negotiate";
1149 else if ((job_id_attr
= ippFindAttribute(response
, "job-id",
1150 IPP_TAG_INTEGER
)) == NULL
)
1152 _cupsLangPrintFilter(stderr
, "INFO",
1153 _("Print file accepted - job ID unknown."));
1158 monitor
.job_id
= job_id
= job_id_attr
->values
[0].integer
;
1159 _cupsLangPrintFilter(stderr
, "INFO",
1160 _("Print file accepted - job ID %d."), job_id
);
1163 ippDelete(response
);
1168 if (job_id
&& num_files
> 1)
1170 for (i
= 0; i
< num_files
; i
++)
1173 * Check for side-channel requests...
1176 backendCheckSideChannel(snmp_fd
, http
->hostaddr
);
1179 * Send the next file in the job...
1182 request
= ippNewRequest(IPP_SEND_DOCUMENT
);
1183 request
->request
.op
.version
[0] = version
/ 10;
1184 request
->request
.op
.version
[1] = version
% 10;
1186 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
1189 ippAddInteger(request
, IPP_TAG_OPERATION
, IPP_TAG_INTEGER
, "job-id",
1193 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
1194 "requesting-user-name", NULL
, argv
[2]);
1196 if ((i
+ 1) == num_files
)
1197 ippAddBoolean(request
, IPP_TAG_OPERATION
, "last-document", 1);
1199 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_MIMETYPE
,
1200 "document-format", NULL
, content_type
);
1202 fprintf(stderr
, "DEBUG: Sending file %d using chunking...\n", i
+ 1);
1203 http_status
= cupsSendRequest(http
, request
, resource
, 0);
1204 if (http_status
== HTTP_CONTINUE
&& request
->state
== IPP_DATA
&&
1205 (fd
= open(files
[i
], O_RDONLY
)) >= 0)
1207 while ((bytes
= read(fd
, buffer
, sizeof(buffer
))) > 0)
1209 if (cupsWriteRequestData(http
, buffer
, bytes
) != HTTP_CONTINUE
)
1214 * Check for side-channel requests...
1217 backendCheckSideChannel(snmp_fd
, http
->hostaddr
);
1224 ippDelete(cupsGetResponse(http
, resource
));
1227 if (cupsLastError() > IPP_OK_CONFLICT
)
1229 ipp_status
= cupsLastError();
1231 _cupsLangPrintFilter(stderr
, "ERROR",
1232 _("Unable to add file to job: %s"),
1233 cupsLastErrorString());
1239 if (ipp_status
<= IPP_OK_CONFLICT
&& argc
> 6)
1241 fprintf(stderr
, "PAGE: 1 %d\n", copies_sup
? atoi(argv
[4]) : 1);
1242 copies_remaining
--;
1244 else if (ipp_status
== IPP_SERVICE_UNAVAILABLE
||
1245 ipp_status
== IPP_PRINTER_BUSY
)
1248 copies_remaining
--;
1251 * Wait for the job to complete...
1254 if (!job_id
|| !waitjob
)
1257 _cupsLangPrintFilter(stderr
, "INFO", _("Waiting for job to complete."));
1259 for (delay
= 1; !job_canceled
;)
1262 * Check for side-channel requests...
1265 backendCheckSideChannel(snmp_fd
, http
->hostaddr
);
1268 * Build an IPP_GET_JOB_ATTRIBUTES request...
1271 request
= ippNewRequest(IPP_GET_JOB_ATTRIBUTES
);
1272 request
->request
.op
.version
[0] = version
/ 10;
1273 request
->request
.op
.version
[1] = version
% 10;
1275 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
1278 ippAddInteger(request
, IPP_TAG_OPERATION
, IPP_TAG_INTEGER
, "job-id",
1282 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
1283 "requesting-user-name", NULL
, argv
[2]);
1285 ippAddStrings(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
1286 "requested-attributes", sizeof(jattrs
) / sizeof(jattrs
[0]),
1293 response
= cupsDoRequest(http
, request
, resource
);
1294 ipp_status
= cupsLastError();
1296 if (ipp_status
== IPP_NOT_FOUND
)
1299 * Job has gone away and/or the server has no job history...
1302 ippDelete(response
);
1304 ipp_status
= IPP_OK
;
1308 if (ipp_status
> IPP_OK_CONFLICT
)
1310 if (ipp_status
!= IPP_SERVICE_UNAVAILABLE
&&
1311 ipp_status
!= IPP_PRINTER_BUSY
)
1313 ippDelete(response
);
1315 _cupsLangPrintFilter(stderr
, "ERROR",
1316 _("Unable to get job attributes: %s"),
1317 cupsLastErrorString());
1324 if ((job_state
= ippFindAttribute(response
, "job-state",
1325 IPP_TAG_ENUM
)) != NULL
)
1328 * Stop polling if the job is finished or pending-held...
1331 if (job_state
->values
[0].integer
> IPP_JOB_STOPPED
)
1333 if ((job_sheets
= ippFindAttribute(response
,
1334 "job-media-sheets-completed",
1335 IPP_TAG_INTEGER
)) != NULL
)
1336 fprintf(stderr
, "PAGE: total %d\n",
1337 job_sheets
->values
[0].integer
);
1339 ippDelete(response
);
1346 * If the printer does not return a job-state attribute, it does not
1347 * conform to the IPP specification - break out immediately and fail
1351 fputs("DEBUG: No job-state available from printer - stopping queue.\n",
1353 ipp_status
= IPP_INTERNAL_ERROR
;
1358 ippDelete(response
);
1362 * Check the printer state and report it if necessary...
1365 check_printer_state(http
, uri
, resource
, argv
[2], version
, job_id
);
1369 * Wait 1-10 seconds before polling again...
1381 * Cancel the job as needed...
1384 if (job_canceled
&& job_id
)
1385 cancel_job(http
, uri
, job_id
, resource
, argv
[2], version
);
1388 * Check the printer state and report it if necessary...
1391 check_printer_state(http
, uri
, resource
, argv
[2], version
, job_id
);
1394 * Collect the final page count as needed...
1397 if (have_supplies
&&
1398 !backendSNMPSupplies(snmp_fd
, http
->hostaddr
, &page_count
, NULL
) &&
1399 page_count
> start_count
)
1400 fprintf(stderr
, "PAGE: total %d\n", page_count
- start_count
);
1404 * See if we used Kerberos at all...
1408 auth_info_required
= "negotiate";
1409 #endif /* HAVE_GSSAPI */
1417 cupsFreeOptions(num_options
, options
);
1422 ippDelete(supported
);
1425 * Remove the temporary file(s) if necessary...
1431 for (i
= 0; i
< num_files
; i
++)
1434 #endif /* HAVE_LIBZ */
1437 * Return the queue status...
1440 fprintf(stderr
, "ATTR: auth-info-required=%s\n", auth_info_required
);
1442 if (ipp_status
== IPP_NOT_AUTHORIZED
|| ipp_status
== IPP_FORBIDDEN
)
1443 return (CUPS_BACKEND_AUTH_REQUIRED
);
1444 else if (ipp_status
== IPP_INTERNAL_ERROR
)
1445 return (CUPS_BACKEND_STOP
);
1446 else if (ipp_status
> IPP_OK_CONFLICT
)
1447 return (CUPS_BACKEND_FAILED
);
1450 _cupsLangPrintFilter(stderr
, "INFO", _("Ready to print."));
1451 return (CUPS_BACKEND_OK
);
1457 * 'cancel_job()' - Cancel a print job.
1461 cancel_job(http_t
*http
, /* I - HTTP connection */
1462 const char *uri
, /* I - printer-uri */
1463 int id
, /* I - job-id */
1464 const char *resource
, /* I - Resource path */
1465 const char *user
, /* I - requesting-user-name */
1466 int version
) /* I - IPP version */
1468 ipp_t
*request
; /* Cancel-Job request */
1471 _cupsLangPrintFilter(stderr
, "INFO", _("Canceling print job."));
1473 request
= ippNewRequest(IPP_CANCEL_JOB
);
1474 request
->request
.op
.version
[0] = version
/ 10;
1475 request
->request
.op
.version
[1] = version
% 10;
1477 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
1479 ippAddInteger(request
, IPP_TAG_OPERATION
, IPP_TAG_INTEGER
, "job-id", id
);
1481 if (user
&& user
[0])
1482 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
1483 "requesting-user-name", NULL
, user
);
1489 ippDelete(cupsDoRequest(http
, request
, resource
));
1491 if (cupsLastError() > IPP_OK_CONFLICT
)
1492 _cupsLangPrintFilter(stderr
, "ERROR", _("Unable to cancel job: %s"),
1493 cupsLastErrorString());
1498 * 'check_printer_state()' - Check the printer state.
1501 static ipp_pstate_t
/* O - Current printer-state */
1502 check_printer_state(
1503 http_t
*http
, /* I - HTTP connection */
1504 const char *uri
, /* I - Printer URI */
1505 const char *resource
, /* I - Resource path */
1506 const char *user
, /* I - Username, if any */
1507 int version
, /* I - IPP version */
1510 ipp_t
*request
, /* IPP request */
1511 *response
; /* IPP response */
1512 ipp_attribute_t
*attr
; /* Attribute in response */
1513 ipp_pstate_t printer_state
= IPP_PRINTER_STOPPED
;
1514 /* Current printer-state */
1518 * Send a Get-Printer-Attributes request and log the results...
1521 request
= ippNewRequest(IPP_GET_PRINTER_ATTRIBUTES
);
1522 request
->request
.op
.version
[0] = version
/ 10;
1523 request
->request
.op
.version
[1] = version
% 10;
1525 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
1528 if (user
&& user
[0])
1529 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
1530 "requesting-user-name", NULL
, user
);
1532 ippAddStrings(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
1533 "requested-attributes",
1534 (int)(sizeof(pattrs
) / sizeof(pattrs
[0])), NULL
, pattrs
);
1536 if ((response
= cupsDoRequest(http
, request
, resource
)) != NULL
)
1538 report_printer_state(response
, job_id
);
1540 if ((attr
= ippFindAttribute(response
, "printer-state",
1541 IPP_TAG_ENUM
)) != NULL
)
1542 printer_state
= (ipp_pstate_t
)attr
->values
[0].integer
;
1544 ippDelete(response
);
1548 * Return the printer-state value...
1551 return (printer_state
);
1557 * 'compress_files()' - Compress print files...
1561 compress_files(int num_files
, /* I - Number of files */
1562 char **files
) /* I - Files */
1564 int i
, /* Looping var */
1565 fd
; /* Temporary file descriptor */
1566 ssize_t bytes
; /* Bytes read/written */
1567 size_t total
; /* Total bytes read */
1568 cups_file_t
*in
, /* Input file */
1569 *out
; /* Output file */
1570 struct stat outinfo
; /* Output file information */
1571 char filename
[1024], /* Temporary filename */
1572 buffer
[32768]; /* Copy buffer */
1575 fprintf(stderr
, "DEBUG: Compressing %d job files...\n", num_files
);
1576 for (i
= 0; i
< num_files
; i
++)
1578 if ((fd
= cupsTempFd(filename
, sizeof(filename
))) < 0)
1580 _cupsLangPrintError("ERROR", _("Unable to create compressed print file"));
1581 exit(CUPS_BACKEND_FAILED
);
1584 if ((out
= cupsFileOpenFd(fd
, "w9")) == NULL
)
1586 _cupsLangPrintError("ERROR", _("Unable to open compressed print file"));
1587 exit(CUPS_BACKEND_FAILED
);
1590 if ((in
= cupsFileOpen(files
[i
], "r")) == NULL
)
1592 _cupsLangPrintError("ERROR", _("Unable to open print file"));
1594 exit(CUPS_BACKEND_FAILED
);
1598 while ((bytes
= cupsFileRead(in
, buffer
, sizeof(buffer
))) > 0)
1599 if (cupsFileWrite(out
, buffer
, bytes
) < bytes
)
1601 _cupsLangPrintError("ERROR",
1602 _("Unable to generate compressed print file"));
1605 exit(CUPS_BACKEND_FAILED
);
1613 files
[i
] = strdup(filename
);
1615 if (!stat(filename
, &outinfo
))
1617 "DEBUG: File %d compressed to %.1f%% of original size, "
1618 CUPS_LLFMT
" bytes...\n",
1619 i
+ 1, 100.0 * outinfo
.st_size
/ total
,
1620 CUPS_LLCAST outinfo
.st_size
);
1623 #endif /* HAVE_LIBZ */
1627 * 'monitor_printer()' - Monitor the printer state...
1630 static void * /* O - Thread exit code */
1632 _cups_monitor_t
*monitor
) /* I - Monitoring data */
1634 http_t
*http
; /* Connection to printer */
1635 ipp_t
*request
, /* IPP request */
1636 *response
; /* IPP response */
1637 ipp_attribute_t
*attr
; /* Attribute in response */
1638 int delay
, /* Current delay */
1639 prev_delay
, /* Previous delay */
1640 temp_delay
; /* Temporary delay value */
1644 * Make a copy of the printer connection...
1647 http
= _httpCreate(monitor
->hostname
, monitor
->port
, monitor
->encryption
,
1649 cupsSetPasswordCB(password_cb
);
1652 * Loop until the job is canceled, aborted, or completed.
1658 while (monitor
->job_state
< IPP_JOB_CANCELED
&& !job_canceled
)
1661 * Reconnect to the printer...
1664 if (!httpReconnect(http
))
1667 * Connected, so check on the printer state...
1670 monitor
->printer_state
= check_printer_state(http
, monitor
->uri
,
1676 if (monitor
->job_id
> 0)
1679 * Check the status of the job itself...
1682 request
= ippNewRequest(IPP_GET_JOB_ATTRIBUTES
);
1683 request
->request
.op
.version
[0] = monitor
->version
/ 10;
1684 request
->request
.op
.version
[1] = monitor
->version
% 10;
1686 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
1687 NULL
, monitor
->uri
);
1688 ippAddInteger(request
, IPP_TAG_OPERATION
, IPP_TAG_INTEGER
, "job-id",
1691 if (monitor
->user
&& monitor
->user
[0])
1692 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
1693 "requesting-user-name", NULL
, monitor
->user
);
1695 ippAddStrings(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
1696 "requested-attributes",
1697 (int)(sizeof(jattrs
) / sizeof(jattrs
[0])), NULL
, jattrs
);
1703 response
= cupsDoRequest(http
, request
, monitor
->resource
);
1705 if ((attr
= ippFindAttribute(response
, "job-state",
1706 IPP_TAG_ENUM
)) != NULL
)
1707 monitor
->job_state
= (ipp_jstate_t
)attr
->values
[0].integer
;
1709 monitor
->job_state
= IPP_JOB_COMPLETED
;
1711 ippDelete(response
);
1715 * Disconnect from the printer - we'll reconnect on the next poll...
1718 _httpDisconnect(http
);
1722 * Sleep for N seconds, and then update the next sleep time using a
1723 * Fibonacci series (1 1 2 3 5 8)...
1729 delay
= (delay
+ prev_delay
) % 12;
1730 prev_delay
= delay
< temp_delay
? 0 : temp_delay
;
1734 * Cleanup and return...
1744 * 'new_request()' - Create a new print creation or validation request.
1747 static ipp_t
* /* O - Request data */
1749 ipp_op_t op
, /* I - IPP operation code */
1750 int version
, /* I - IPP version number */
1751 const char *uri
, /* I - printer-uri value */
1752 const char *user
, /* I - requesting-user-name value */
1753 const char *title
, /* I - job-name value */
1754 int num_options
, /* I - Number of options to send */
1755 cups_option_t
*options
, /* I - Options to send */
1756 const char *compression
, /* I - compression value or NULL */
1757 int copies
, /* I - copies value or 0 */
1758 const char *format
, /* I - documet-format value or NULL */
1759 _pwg_t
*pwg
, /* I - PWG<->PPD mapping data */
1760 ipp_attribute_t
*media_col_sup
) /* I - media-col-supported values */
1762 int i
; /* Looping var */
1763 ipp_t
*request
; /* Request data */
1764 const char *keyword
; /* PWG keyword */
1765 _pwg_size_t
*size
; /* PWG media size */
1766 ipp_t
*media_col
, /* media-col value */
1767 *media_size
; /* media-size value */
1768 const char *media_source
, /* media-source value */
1769 *media_type
; /* media-type value */
1773 * Create the IPP request...
1776 request
= ippNewRequest(op
);
1777 request
->request
.op
.version
[0] = version
/ 10;
1778 request
->request
.op
.version
[1] = version
% 10;
1780 fprintf(stderr
, "DEBUG: %s IPP/%d.%d\n",
1781 ippOpString(request
->request
.op
.operation_id
),
1782 request
->request
.op
.version
[0],
1783 request
->request
.op
.version
[1]);
1786 * Add standard attributes...
1789 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
1791 fprintf(stderr
, "DEBUG: printer-uri=\"%s\"\n", uri
);
1795 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
1796 "requesting-user-name", NULL
, user
);
1797 fprintf(stderr
, "DEBUG: requesting-user-name=\"%s\"\n", user
);
1800 if (title
&& *title
)
1802 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "job-name", NULL
,
1804 fprintf(stderr
, "DEBUG: job-name=\"%s\"\n", title
);
1809 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_MIMETYPE
,
1810 "document-format", NULL
, format
);
1811 fprintf(stderr
, "DEBUG: document-format=\"%s\"\n", format
);
1817 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
1818 "compression", NULL
, compression
);
1819 fprintf(stderr
, "DEBUG: compression=\"%s\"\n", compression
);
1821 #endif /* HAVE_LIBZ */
1824 * Handle options on the command-line...
1827 if (num_options
> 0)
1832 * Send standard IPP attributes...
1835 if ((keyword
= cupsGetOption("PageSize", num_options
, options
)) == NULL
)
1836 keyword
= cupsGetOption("media", num_options
, options
);
1838 if ((size
= _pwgGetSize(pwg
, keyword
)) != NULL
)
1841 * Add a media-col value...
1844 media_size
= ippNew();
1845 ippAddInteger(media_size
, IPP_TAG_ZERO
, IPP_TAG_INTEGER
,
1846 "x-dimension", size
->width
);
1847 ippAddInteger(media_size
, IPP_TAG_ZERO
, IPP_TAG_INTEGER
,
1848 "y-dimension", size
->length
);
1850 media_col
= ippNew();
1851 ippAddCollection(media_col
, IPP_TAG_ZERO
, "media-size", media_size
);
1853 media_source
= _pwgGetSource(pwg
, cupsGetOption("InputSlot",
1856 media_type
= _pwgGetType(pwg
, cupsGetOption("MediaType",
1857 num_options
, options
));
1859 for (i
= 0; i
< media_col_sup
->num_values
; i
++)
1861 if (!strcmp(media_col_sup
->values
[i
].string
.text
,
1862 "media-left-margin"))
1863 ippAddInteger(media_col
, IPP_TAG_ZERO
, IPP_TAG_INTEGER
,
1864 "media-left-margin", size
->left
);
1865 else if (!strcmp(media_col_sup
->values
[i
].string
.text
,
1866 "media-bottom-margin"))
1867 ippAddInteger(media_col
, IPP_TAG_ZERO
, IPP_TAG_INTEGER
,
1868 "media-bottom-margin", size
->left
);
1869 else if (!strcmp(media_col_sup
->values
[i
].string
.text
,
1870 "media-right-margin"))
1871 ippAddInteger(media_col
, IPP_TAG_ZERO
, IPP_TAG_INTEGER
,
1872 "media-right-margin", size
->left
);
1873 else if (!strcmp(media_col_sup
->values
[i
].string
.text
,
1874 "media-top-margin"))
1875 ippAddInteger(media_col
, IPP_TAG_ZERO
, IPP_TAG_INTEGER
,
1876 "media-top-margin", size
->left
);
1877 else if (!strcmp(media_col_sup
->values
[i
].string
.text
,
1878 "media-source") && media_source
)
1879 ippAddString(media_col
, IPP_TAG_ZERO
, IPP_TAG_KEYWORD
,
1880 "media-source", NULL
, media_source
);
1881 else if (!strcmp(media_col_sup
->values
[i
].string
.text
,
1882 "media-type") && media_type
)
1883 ippAddString(media_col
, IPP_TAG_ZERO
, IPP_TAG_KEYWORD
,
1884 "media-type", NULL
, media_type
);
1887 ippAddCollection(request
, IPP_TAG_JOB
, "media-col", media_col
);
1890 if ((keyword
= cupsGetOption("output-bin", num_options
,
1892 keyword
= _pwgGetBin(pwg
, cupsGetOption("OutputBin", num_options
,
1896 ippAddString(request
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
, "output-bin",
1899 if ((keyword
= cupsGetOption("output-mode", num_options
,
1901 ippAddString(request
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
, "output-mode",
1903 else if ((keyword
= cupsGetOption("ColorModel", num_options
,
1906 if (!strcasecmp(keyword
, "Gray"))
1907 ippAddString(request
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
, "output-mode",
1908 NULL
, "monochrome");
1910 ippAddString(request
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
, "output-mode",
1914 if ((keyword
= cupsGetOption("print-quality", num_options
,
1916 ippAddInteger(request
, IPP_TAG_JOB
, IPP_TAG_ENUM
, "print-quality",
1918 else if ((keyword
= cupsGetOption("cupsPrintQuality", num_options
,
1921 if (!strcasecmp(keyword
, "draft"))
1922 ippAddInteger(request
, IPP_TAG_JOB
, IPP_TAG_ENUM
, "print-quality",
1924 else if (!strcasecmp(keyword
, "normal"))
1925 ippAddInteger(request
, IPP_TAG_JOB
, IPP_TAG_ENUM
, "print-quality",
1926 IPP_QUALITY_NORMAL
);
1927 else if (!strcasecmp(keyword
, "high"))
1928 ippAddInteger(request
, IPP_TAG_JOB
, IPP_TAG_ENUM
, "print-quality",
1932 if ((keyword
= cupsGetOption("sides", num_options
, options
)) != NULL
)
1933 ippAddString(request
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
, "sides",
1935 else if (pwg
->sides_option
&&
1936 (keyword
= cupsGetOption(pwg
->sides_option
, num_options
,
1939 if (!strcasecmp(keyword
, pwg
->sides_1sided
))
1940 ippAddString(request
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
, "sides",
1942 else if (!strcasecmp(keyword
, pwg
->sides_2sided_long
))
1943 ippAddString(request
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
, "sides",
1944 NULL
, "two-sided-long-edge");
1945 if (!strcasecmp(keyword
, pwg
->sides_2sided_short
))
1946 ippAddString(request
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
, "sides",
1947 NULL
, "two-sided-short-edge");
1953 * When talking to another CUPS server, send all options...
1956 cupsEncodeOptions(request
, num_options
, options
);
1960 ippAddInteger(request
, IPP_TAG_JOB
, IPP_TAG_INTEGER
, "copies", copies
);
1968 * 'password_cb()' - Disable the password prompt for cupsDoFileRequest().
1971 static const char * /* O - Password */
1972 password_cb(const char *prompt
) /* I - Prompt (not used) */
1977 * Remember that we need to authenticate...
1980 auth_info_required
= "username,password";
1982 if (password
&& *password
&& password_tries
< 3)
1991 * Give up after 3 tries or if we don't have a password to begin with...
2000 * 'report_attr()' - Report an IPP attribute value.
2004 report_attr(ipp_attribute_t
*attr
) /* I - Attribute */
2006 int i
; /* Looping var */
2007 char value
[1024], /* Value string */
2008 *valptr
, /* Pointer into value string */
2009 *attrptr
; /* Pointer into attribute value */
2013 * Convert the attribute values into quoted strings...
2016 for (i
= 0, valptr
= value
;
2017 i
< attr
->num_values
&& valptr
< (value
+ sizeof(value
) - 10);
2023 switch (attr
->value_tag
)
2025 case IPP_TAG_INTEGER
:
2027 snprintf(valptr
, sizeof(value
) - (valptr
- value
), "%d",
2028 attr
->values
[i
].integer
);
2029 valptr
+= strlen(valptr
);
2034 case IPP_TAG_KEYWORD
:
2036 for (attrptr
= attr
->values
[i
].string
.text
;
2037 *attrptr
&& valptr
< (value
+ sizeof(value
) - 10);
2040 if (*attrptr
== '\\' || *attrptr
== '\"')
2043 *valptr
++ = *attrptr
;
2050 * Unsupported value type...
2060 * Tell the scheduler about the new values...
2063 fprintf(stderr
, "ATTR: %s=%s\n", attr
->name
, value
);
2068 * 'report_printer_state()' - Report the printer state.
2071 static int /* O - Number of reasons shown */
2072 report_printer_state(ipp_t
*ipp
, /* I - IPP response */
2073 int job_id
) /* I - Current job ID */
2075 int i
; /* Looping var */
2076 int count
; /* Count of reasons shown... */
2077 ipp_attribute_t
*pa
, /* printer-alert */
2078 *pam
, /* printer-alert-message */
2079 *psm
, /* printer-state-message */
2080 *reasons
, /* printer-state-reasons */
2081 *marker
; /* marker-* attributes */
2082 const char *reason
; /* Current reason */
2083 const char *prefix
; /* Prefix for STATE: line */
2084 char value
[1024], /* State/message string */
2085 *valptr
; /* Pointer into string */
2089 * Report alerts and messages...
2092 if ((pa
= ippFindAttribute(ipp
, "printer-alert", IPP_TAG_TEXT
)) != NULL
)
2095 if ((pam
= ippFindAttribute(ipp
, "printer-alert-message",
2096 IPP_TAG_TEXT
)) != NULL
)
2099 if ((psm
= ippFindAttribute(ipp
, "printer-state-message",
2100 IPP_TAG_TEXT
)) != NULL
)
2102 char *ptr
; /* Pointer into message */
2105 strlcpy(value
, "INFO: ", sizeof(value
));
2106 for (ptr
= psm
->values
[0].string
.text
, valptr
= value
+ 6;
2107 *ptr
&& valptr
< (value
+ sizeof(value
) - 6);
2110 if (*ptr
< ' ' && *ptr
> 0 && *ptr
!= '\t')
2113 * Substitute "<XX>" for the control character; sprintf is safe because
2114 * we always leave 6 chars free at the end...
2117 sprintf(valptr
, "<%02X>", *ptr
);
2127 fputs(value
, stderr
);
2131 * Now report printer-state-reasons, filtering out some of the reasons we never
2135 if ((reasons
= ippFindAttribute(ipp
, "printer-state-reasons",
2136 IPP_TAG_KEYWORD
)) == NULL
)
2142 for (i
= 0, count
= 0, valptr
= value
; i
< reasons
->num_values
; i
++)
2144 reason
= reasons
->values
[i
].string
.text
;
2146 if (strcmp(reason
, "paused") &&
2147 strcmp(reason
, "com.apple.print.recoverable-warning"))
2149 strlcpy(valptr
, prefix
, sizeof(value
) - (valptr
- value
) - 1);
2150 valptr
+= strlen(valptr
);
2151 strlcpy(valptr
, reason
, sizeof(value
) - (valptr
- value
) - 1);
2152 valptr
+= strlen(valptr
);
2162 fputs(value
, stderr
);
2166 * Relay the current marker-* attribute values...
2169 if ((marker
= ippFindAttribute(ipp
, "marker-colors", IPP_TAG_NAME
)) != NULL
)
2170 report_attr(marker
);
2171 if ((marker
= ippFindAttribute(ipp
, "marker-high-levels",
2172 IPP_TAG_INTEGER
)) != NULL
)
2173 report_attr(marker
);
2174 if ((marker
= ippFindAttribute(ipp
, "marker-levels",
2175 IPP_TAG_INTEGER
)) != NULL
)
2176 report_attr(marker
);
2177 if ((marker
= ippFindAttribute(ipp
, "marker-low-levels",
2178 IPP_TAG_INTEGER
)) != NULL
)
2179 report_attr(marker
);
2180 if ((marker
= ippFindAttribute(ipp
, "marker-message", IPP_TAG_TEXT
)) != NULL
)
2181 report_attr(marker
);
2182 if ((marker
= ippFindAttribute(ipp
, "marker-names", IPP_TAG_NAME
)) != NULL
)
2183 report_attr(marker
);
2184 if ((marker
= ippFindAttribute(ipp
, "marker-types", IPP_TAG_KEYWORD
)) != NULL
)
2185 report_attr(marker
);
2192 * 'sigterm_handler()' - Handle 'terminate' signals that stop the backend.
2196 sigterm_handler(int sig
) /* I - Signal */
2198 (void)sig
; /* remove compiler warnings... */
2203 * Flag that the job should be cancelled...
2215 * End of "$Id: ipp.c 7948 2008-09-17 00:04:12Z mike $".