4 * Printing utilities for CUPS.
6 * Copyright 2007-2015 by Apple Inc.
7 * Copyright 1997-2006 by Easy Software Products.
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 * which should have been included with this file. If this file is
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 * Include necessary headers...
22 #include "cups-private.h"
25 #if defined(WIN32) || defined(__EMX__)
29 #endif /* WIN32 || __EMX__ */
36 static int cups_get_printer_uri(http_t
*http
, const char *name
,
37 char *host
, int hostsize
, int *port
,
38 char *resource
, int resourcesize
,
43 * 'cupsCancelJob()' - Cancel a print job on the default server.
45 * Pass @code CUPS_JOBID_ALL@ to cancel all jobs or @code CUPS_JOBID_CURRENT@
46 * to cancel the current job on the named destination.
48 * Use the @link cupsLastError@ and @link cupsLastErrorString@ functions to get
49 * the cause of any failure.
52 int /* O - 1 on success, 0 on failure */
53 cupsCancelJob(const char *name
, /* I - Name of printer or class */
54 int job_id
) /* I - Job ID, @code CUPS_JOBID_CURRENT@ for the current job, or @code CUPS_JOBID_ALL@ for all jobs */
56 return (cupsCancelJob2(CUPS_HTTP_DEFAULT
, name
, job_id
, 0)
57 < IPP_STATUS_REDIRECTION_OTHER_SITE
);
62 * 'cupsCancelJob2()' - Cancel or purge a print job.
64 * Canceled jobs remain in the job history while purged jobs are removed
65 * from the job history.
67 * Pass @code CUPS_JOBID_ALL@ to cancel all jobs or @code CUPS_JOBID_CURRENT@
68 * to cancel the current job on the named destination.
70 * Use the @link cupsLastError@ and @link cupsLastErrorString@ functions to get
71 * the cause of any failure.
73 * @since CUPS 1.4/OS X 10.6@
76 ipp_status_t
/* O - IPP status */
77 cupsCancelJob2(http_t
*http
, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
78 const char *name
, /* I - Name of printer or class */
79 int job_id
, /* I - Job ID, @code CUPS_JOBID_CURRENT@ for the current job, or @code CUPS_JOBID_ALL@ for all jobs */
80 int purge
) /* I - 1 to purge, 0 to cancel */
82 char uri
[HTTP_MAX_URI
]; /* Job/printer URI */
83 ipp_t
*request
; /* IPP request */
87 * Range check input...
90 if (job_id
< -1 || (!name
&& job_id
== 0))
92 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, strerror(EINVAL
), 0);
97 * Connect to the default server as needed...
101 if ((http
= _cupsConnect()) == NULL
)
102 return (IPP_STATUS_ERROR_SERVICE_UNAVAILABLE
);
105 * Build an IPP_CANCEL_JOB or IPP_PURGE_JOBS request, which requires the following
109 * attributes-natural-language
110 * job-uri or printer-uri + job-id
111 * requesting-user-name
112 * [purge-job] or [purge-jobs]
115 request
= ippNewRequest(job_id
< 0 ? IPP_OP_PURGE_JOBS
: IPP_OP_CANCEL_JOB
);
119 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
120 "localhost", ippPort(), "/printers/%s", name
);
122 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri", NULL
,
124 ippAddInteger(request
, IPP_TAG_OPERATION
, IPP_TAG_INTEGER
, "job-id",
129 snprintf(uri
, sizeof(uri
), "ipp://localhost/jobs/%d", job_id
);
131 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "job-uri", NULL
, uri
);
134 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "requesting-user-name",
137 if (purge
&& job_id
>= 0)
138 ippAddBoolean(request
, IPP_TAG_OPERATION
, "purge-job", 1);
139 else if (!purge
&& job_id
< 0)
140 ippAddBoolean(request
, IPP_TAG_OPERATION
, "purge-jobs", 0);
146 ippDelete(cupsDoRequest(http
, request
, "/jobs/"));
148 return (cupsLastError());
153 * 'cupsCreateJob()' - Create an empty job for streaming.
155 * Use this function when you want to stream print data using the
156 * @link cupsStartDocument@, @link cupsWriteRequestData@, and
157 * @link cupsFinishDocument@ functions. If you have one or more files to
158 * print, use the @link cupsPrintFile2@ or @link cupsPrintFiles2@ function
161 * @since CUPS 1.4/OS X 10.6@
164 int /* O - Job ID or 0 on error */
166 http_t
*http
, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
167 const char *name
, /* I - Destination name */
168 const char *title
, /* I - Title of job */
169 int num_options
, /* I - Number of options */
170 cups_option_t
*options
) /* I - Options */
172 char printer_uri
[1024], /* Printer URI */
173 resource
[1024]; /* Printer resource */
174 ipp_t
*request
, /* Create-Job request */
175 *response
; /* Create-Job response */
176 ipp_attribute_t
*attr
; /* job-id attribute */
177 int job_id
= 0; /* job-id value */
180 DEBUG_printf(("cupsCreateJob(http=%p, name=\"%s\", title=\"%s\", "
181 "num_options=%d, options=%p)",
182 http
, name
, title
, num_options
, options
));
185 * Range check input...
190 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, strerror(EINVAL
), 0);
195 * Build a Create-Job request...
198 if ((request
= ippNewRequest(IPP_OP_CREATE_JOB
)) == NULL
)
200 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, strerror(ENOMEM
), 0);
204 httpAssembleURIf(HTTP_URI_CODING_ALL
, printer_uri
, sizeof(printer_uri
), "ipp",
205 NULL
, "localhost", ippPort(), "/printers/%s", name
);
206 snprintf(resource
, sizeof(resource
), "/printers/%s", name
);
208 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
210 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "requesting-user-name",
213 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "job-name", NULL
,
215 cupsEncodeOptions2(request
, num_options
, options
, IPP_TAG_OPERATION
);
216 cupsEncodeOptions2(request
, num_options
, options
, IPP_TAG_JOB
);
217 cupsEncodeOptions2(request
, num_options
, options
, IPP_TAG_SUBSCRIPTION
);
220 * Send the request and get the job-id...
223 response
= cupsDoRequest(http
, request
, resource
);
225 if ((attr
= ippFindAttribute(response
, "job-id", IPP_TAG_INTEGER
)) != NULL
)
226 job_id
= attr
->values
[0].integer
;
239 * 'cupsFinishDocument()' - Finish sending a document.
241 * The document must have been started using @link cupsStartDocument@.
243 * @since CUPS 1.4/OS X 10.6@
246 ipp_status_t
/* O - Status of document submission */
247 cupsFinishDocument(http_t
*http
, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
248 const char *name
) /* I - Destination name */
250 char resource
[1024]; /* Printer resource */
253 snprintf(resource
, sizeof(resource
), "/printers/%s", name
);
255 ippDelete(cupsGetResponse(http
, resource
));
257 return (cupsLastError());
262 * 'cupsFreeJobs()' - Free memory used by job data.
266 cupsFreeJobs(int num_jobs
, /* I - Number of jobs */
267 cups_job_t
*jobs
) /* I - Jobs */
269 int i
; /* Looping var */
270 cups_job_t
*job
; /* Current job */
273 if (num_jobs
<= 0 || !jobs
)
276 for (i
= num_jobs
, job
= jobs
; i
> 0; i
--, job
++)
278 _cupsStrFree(job
->dest
);
279 _cupsStrFree(job
->user
);
280 _cupsStrFree(job
->format
);
281 _cupsStrFree(job
->title
);
289 * 'cupsGetClasses()' - Get a list of printer classes from the default server.
291 * This function is deprecated and no longer returns a list of printer
292 * classes - use @link cupsGetDests@ instead.
297 int /* O - Number of classes */
298 cupsGetClasses(char ***classes
) /* O - Classes */
308 * 'cupsGetDefault()' - Get the default printer or class for the default server.
310 * This function returns the default printer or class as defined by
311 * the LPDEST or PRINTER environment variables. If these environment
312 * variables are not set, the server default destination is returned.
313 * Applications should use the @link cupsGetDests@ and @link cupsGetDest@
314 * functions to get the user-defined default printer, as this function does
315 * not support the lpoptions-defined default printer.
318 const char * /* O - Default printer or @code NULL@ */
322 * Return the default printer...
325 return (cupsGetDefault2(CUPS_HTTP_DEFAULT
));
330 * 'cupsGetDefault2()' - Get the default printer or class for the specified server.
332 * This function returns the default printer or class as defined by
333 * the LPDEST or PRINTER environment variables. If these environment
334 * variables are not set, the server default destination is returned.
335 * Applications should use the @link cupsGetDests@ and @link cupsGetDest@
336 * functions to get the user-defined default printer, as this function does
337 * not support the lpoptions-defined default printer.
339 * @since CUPS 1.1.21/OS X 10.4@
342 const char * /* O - Default printer or @code NULL@ */
343 cupsGetDefault2(http_t
*http
) /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
345 ipp_t
*request
, /* IPP Request */
346 *response
; /* IPP Response */
347 ipp_attribute_t
*attr
; /* Current attribute */
348 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals */
352 * See if we have a user default printer set...
355 if (_cupsUserDefault(cg
->def_printer
, sizeof(cg
->def_printer
)))
356 return (cg
->def_printer
);
359 * Connect to the server as needed...
363 if ((http
= _cupsConnect()) == NULL
)
367 * Build a CUPS_GET_DEFAULT request, which requires the following
371 * attributes-natural-language
374 request
= ippNewRequest(IPP_OP_CUPS_GET_DEFAULT
);
377 * Do the request and get back a response...
380 if ((response
= cupsDoRequest(http
, request
, "/")) != NULL
)
382 if ((attr
= ippFindAttribute(response
, "printer-name",
383 IPP_TAG_NAME
)) != NULL
)
385 strlcpy(cg
->def_printer
, attr
->values
[0].string
.text
,
386 sizeof(cg
->def_printer
));
388 return (cg
->def_printer
);
399 * 'cupsGetJobs()' - Get the jobs from the default server.
401 * A "whichjobs" value of @code CUPS_WHICHJOBS_ALL@ returns all jobs regardless
402 * of state, while @code CUPS_WHICHJOBS_ACTIVE@ returns jobs that are
403 * pending, processing, or held and @code CUPS_WHICHJOBS_COMPLETED@ returns
404 * jobs that are stopped, canceled, aborted, or completed.
407 int /* O - Number of jobs */
408 cupsGetJobs(cups_job_t
**jobs
, /* O - Job data */
409 const char *name
, /* I - @code NULL@ = all destinations, otherwise show jobs for named destination */
410 int myjobs
, /* I - 0 = all users, 1 = mine */
411 int whichjobs
) /* I - @code CUPS_WHICHJOBS_ALL@, @code CUPS_WHICHJOBS_ACTIVE@, or @code CUPS_WHICHJOBS_COMPLETED@ */
417 return (cupsGetJobs2(CUPS_HTTP_DEFAULT
, jobs
, name
, myjobs
, whichjobs
));
423 * 'cupsGetJobs2()' - Get the jobs from the specified server.
425 * A "whichjobs" value of @code CUPS_WHICHJOBS_ALL@ returns all jobs regardless
426 * of state, while @code CUPS_WHICHJOBS_ACTIVE@ returns jobs that are
427 * pending, processing, or held and @code CUPS_WHICHJOBS_COMPLETED@ returns
428 * jobs that are stopped, canceled, aborted, or completed.
430 * @since CUPS 1.1.21/OS X 10.4@
433 int /* O - Number of jobs */
434 cupsGetJobs2(http_t
*http
, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
435 cups_job_t
**jobs
, /* O - Job data */
436 const char *name
, /* I - @code NULL@ = all destinations, otherwise show jobs for named destination */
437 int myjobs
, /* I - 0 = all users, 1 = mine */
438 int whichjobs
) /* I - @code CUPS_WHICHJOBS_ALL@, @code CUPS_WHICHJOBS_ACTIVE@, or @code CUPS_WHICHJOBS_COMPLETED@ */
440 int n
; /* Number of jobs */
441 ipp_t
*request
, /* IPP Request */
442 *response
; /* IPP Response */
443 ipp_attribute_t
*attr
; /* Current attribute */
444 cups_job_t
*temp
; /* Temporary pointer */
446 priority
, /* job-priority */
447 size
; /* job-k-octets */
448 ipp_jstate_t state
; /* job-state */
449 time_t completed_time
, /* time-at-completed */
450 creation_time
, /* time-at-creation */
451 processing_time
; /* time-at-processing */
452 const char *dest
, /* job-printer-uri */
453 *format
, /* document-format */
454 *title
, /* job-name */
455 *user
; /* job-originating-user-name */
456 char uri
[HTTP_MAX_URI
]; /* URI for jobs */
457 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals */
458 static const char * const attrs
[] = /* Requested attributes */
464 "job-originating-user-name",
475 * Range check input...
480 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, strerror(EINVAL
), 0);
486 * Get the right URI...
491 if (httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
492 "localhost", 0, "/printers/%s",
493 name
) < HTTP_URI_STATUS_OK
)
495 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
,
496 _("Unable to create printer-uri"), 1);
502 strlcpy(uri
, "ipp://localhost/", sizeof(uri
));
505 if ((http
= _cupsConnect()) == NULL
)
509 * Build an IPP_GET_JOBS request, which requires the following
513 * attributes-natural-language
515 * requesting-user-name
518 * requested-attributes
521 request
= ippNewRequest(IPP_OP_GET_JOBS
);
523 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
,
524 "printer-uri", NULL
, uri
);
526 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
527 "requesting-user-name", NULL
, cupsUser());
530 ippAddBoolean(request
, IPP_TAG_OPERATION
, "my-jobs", 1);
532 if (whichjobs
== CUPS_WHICHJOBS_COMPLETED
)
533 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
534 "which-jobs", NULL
, "completed");
535 else if (whichjobs
== CUPS_WHICHJOBS_ALL
)
536 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
537 "which-jobs", NULL
, "all");
539 ippAddStrings(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
540 "requested-attributes", sizeof(attrs
) / sizeof(attrs
[0]),
544 * Do the request and get back a response...
550 if ((response
= cupsDoRequest(http
, request
, "/")) != NULL
)
552 for (attr
= response
->attrs
; attr
; attr
= attr
->next
)
555 * Skip leading attributes until we hit a job...
558 while (attr
&& attr
->group_tag
!= IPP_TAG_JOB
)
565 * Pull the needed attributes from this job...
571 state
= IPP_JSTATE_PENDING
;
574 format
= "application/octet-stream";
580 while (attr
&& attr
->group_tag
== IPP_TAG_JOB
)
582 if (!strcmp(attr
->name
, "job-id") &&
583 attr
->value_tag
== IPP_TAG_INTEGER
)
584 id
= attr
->values
[0].integer
;
585 else if (!strcmp(attr
->name
, "job-state") &&
586 attr
->value_tag
== IPP_TAG_ENUM
)
587 state
= (ipp_jstate_t
)attr
->values
[0].integer
;
588 else if (!strcmp(attr
->name
, "job-priority") &&
589 attr
->value_tag
== IPP_TAG_INTEGER
)
590 priority
= attr
->values
[0].integer
;
591 else if (!strcmp(attr
->name
, "job-k-octets") &&
592 attr
->value_tag
== IPP_TAG_INTEGER
)
593 size
= attr
->values
[0].integer
;
594 else if (!strcmp(attr
->name
, "time-at-completed") &&
595 attr
->value_tag
== IPP_TAG_INTEGER
)
596 completed_time
= attr
->values
[0].integer
;
597 else if (!strcmp(attr
->name
, "time-at-creation") &&
598 attr
->value_tag
== IPP_TAG_INTEGER
)
599 creation_time
= attr
->values
[0].integer
;
600 else if (!strcmp(attr
->name
, "time-at-processing") &&
601 attr
->value_tag
== IPP_TAG_INTEGER
)
602 processing_time
= attr
->values
[0].integer
;
603 else if (!strcmp(attr
->name
, "job-printer-uri") &&
604 attr
->value_tag
== IPP_TAG_URI
)
606 if ((dest
= strrchr(attr
->values
[0].string
.text
, '/')) != NULL
)
609 else if (!strcmp(attr
->name
, "job-originating-user-name") &&
610 attr
->value_tag
== IPP_TAG_NAME
)
611 user
= attr
->values
[0].string
.text
;
612 else if (!strcmp(attr
->name
, "document-format") &&
613 attr
->value_tag
== IPP_TAG_MIMETYPE
)
614 format
= attr
->values
[0].string
.text
;
615 else if (!strcmp(attr
->name
, "job-name") &&
616 (attr
->value_tag
== IPP_TAG_TEXT
||
617 attr
->value_tag
== IPP_TAG_NAME
))
618 title
= attr
->values
[0].string
.text
;
624 * See if we have everything needed...
636 * Allocate memory for the job...
640 temp
= malloc(sizeof(cups_job_t
));
642 temp
= realloc(*jobs
, sizeof(cups_job_t
) * (size_t)(n
+ 1));
650 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, NULL
, 0);
652 cupsFreeJobs(n
, *jobs
);
665 * Copy the data over...
668 temp
->dest
= _cupsStrAlloc(dest
);
669 temp
->user
= _cupsStrAlloc(user
);
670 temp
->format
= _cupsStrAlloc(format
);
671 temp
->title
= _cupsStrAlloc(title
);
673 temp
->priority
= priority
;
676 temp
->completed_time
= completed_time
;
677 temp
->creation_time
= creation_time
;
678 temp
->processing_time
= processing_time
;
687 if (n
== 0 && cg
->last_error
>= IPP_STATUS_ERROR_BAD_REQUEST
)
695 * 'cupsGetPPD()' - Get the PPD file for a printer on the default server.
697 * For classes, @code cupsGetPPD@ returns the PPD file for the first printer
700 * The returned filename is stored in a static buffer and is overwritten with
701 * each call to @code cupsGetPPD@ or @link cupsGetPPD2@. The caller "owns" the
702 * file that is created and must @code unlink@ the returned filename.
705 const char * /* O - Filename for PPD file */
706 cupsGetPPD(const char *name
) /* I - Destination name */
708 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals */
709 time_t modtime
= 0; /* Modification time */
713 * Return the PPD file...
716 cg
->ppd_filename
[0] = '\0';
718 if (cupsGetPPD3(CUPS_HTTP_DEFAULT
, name
, &modtime
, cg
->ppd_filename
,
719 sizeof(cg
->ppd_filename
)) == HTTP_STATUS_OK
)
720 return (cg
->ppd_filename
);
727 * 'cupsGetPPD2()' - Get the PPD file for a printer from the specified server.
729 * For classes, @code cupsGetPPD2@ returns the PPD file for the first printer
732 * The returned filename is stored in a static buffer and is overwritten with
733 * each call to @link cupsGetPPD@ or @code cupsGetPPD2@. The caller "owns" the
734 * file that is created and must @code unlink@ the returned filename.
736 * @since CUPS 1.1.21/OS X 10.4@
739 const char * /* O - Filename for PPD file */
740 cupsGetPPD2(http_t
*http
, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
741 const char *name
) /* I - Destination name */
743 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals */
744 time_t modtime
= 0; /* Modification time */
747 cg
->ppd_filename
[0] = '\0';
749 if (cupsGetPPD3(http
, name
, &modtime
, cg
->ppd_filename
,
750 sizeof(cg
->ppd_filename
)) == HTTP_STATUS_OK
)
751 return (cg
->ppd_filename
);
758 * 'cupsGetPPD3()' - Get the PPD file for a printer on the specified
759 * server if it has changed.
761 * The "modtime" parameter contains the modification time of any
762 * locally-cached content and is updated with the time from the PPD file on
765 * The "buffer" parameter contains the local PPD filename. If it contains
766 * the empty string, a new temporary file is created, otherwise the existing
767 * file will be overwritten as needed. The caller "owns" the file that is
768 * created and must @code unlink@ the returned filename.
770 * On success, @code HTTP_STATUS_OK@ is returned for a new PPD file and
771 * @code HTTP_STATUS_NOT_MODIFIED@ if the existing PPD file is up-to-date. Any other
772 * status is an error.
774 * For classes, @code cupsGetPPD3@ returns the PPD file for the first printer
777 * @since CUPS 1.4/OS X 10.6@
780 http_status_t
/* O - HTTP status */
781 cupsGetPPD3(http_t
*http
, /* I - HTTP connection or @code CUPS_HTTP_DEFAULT@ */
782 const char *name
, /* I - Destination name */
783 time_t *modtime
, /* IO - Modification time */
784 char *buffer
, /* I - Filename buffer */
785 size_t bufsize
) /* I - Size of filename buffer */
787 int http_port
; /* Port number */
788 char http_hostname
[HTTP_MAX_HOST
];
789 /* Hostname associated with connection */
790 http_t
*http2
; /* Alternate HTTP connection */
791 int fd
; /* PPD file */
792 char localhost
[HTTP_MAX_URI
],/* Local hostname */
793 hostname
[HTTP_MAX_URI
], /* Hostname */
794 resource
[HTTP_MAX_URI
]; /* Resource name */
795 int port
; /* Port number */
796 http_status_t status
; /* HTTP status from server */
797 char tempfile
[1024] = ""; /* Temporary filename */
798 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals */
802 * Range check input...
805 DEBUG_printf(("cupsGetPPD3(http=%p, name=\"%s\", modtime=%p(%d), buffer=%p, "
806 "bufsize=%d)", http
, name
, modtime
,
807 modtime
? (int)*modtime
: 0, buffer
, (int)bufsize
));
811 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, _("No printer name"), 1);
812 return (HTTP_STATUS_NOT_ACCEPTABLE
);
817 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, _("No modification time"), 1);
818 return (HTTP_STATUS_NOT_ACCEPTABLE
);
821 if (!buffer
|| bufsize
<= 1)
823 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, _("Bad filename buffer"), 1);
824 return (HTTP_STATUS_NOT_ACCEPTABLE
);
829 * See if the PPD file is available locally...
833 httpGetHostname(http
, hostname
, sizeof(hostname
));
836 strlcpy(hostname
, cupsServer(), sizeof(hostname
));
837 if (hostname
[0] == '/')
838 strlcpy(hostname
, "localhost", sizeof(hostname
));
841 if (!_cups_strcasecmp(hostname
, "localhost"))
843 char ppdname
[1024]; /* PPD filename */
844 struct stat ppdinfo
; /* PPD file information */
847 snprintf(ppdname
, sizeof(ppdname
), "%s/ppd/%s.ppd", cg
->cups_serverroot
,
849 if (!stat(ppdname
, &ppdinfo
) && !access(ppdname
, R_OK
))
852 * OK, the file exists and is readable, use it!
859 if (symlink(ppdname
, buffer
) && errno
!= EEXIST
)
861 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, NULL
, 0);
863 return (HTTP_STATUS_SERVER_ERROR
);
868 int tries
; /* Number of tries */
869 const char *tmpdir
; /* TMPDIR environment variable */
870 struct timeval curtime
; /* Current time */
873 * Previously we put root temporary files in the default CUPS temporary
874 * directory under /var/spool/cups. However, since the scheduler cleans
875 * out temporary files there and runs independently of the user apps, we
876 * don't want to use it unless specifically told to by cupsd.
879 if ((tmpdir
= getenv("TMPDIR")) == NULL
)
881 tmpdir
= "/private/tmp"; /* /tmp is a symlink to /private/tmp */
884 # endif /* __APPLE__ */
887 * Make the temporary name using the specified directory...
895 * Get the current time of day...
898 gettimeofday(&curtime
, NULL
);
901 * Format a string using the hex time values...
904 snprintf(buffer
, bufsize
, "%s/%08lx%05lx", tmpdir
,
905 (unsigned long)curtime
.tv_sec
,
906 (unsigned long)curtime
.tv_usec
);
909 * Try to make a symlink...
912 if (!symlink(ppdname
, buffer
))
917 while (tries
< 1000);
921 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, NULL
, 0);
923 return (HTTP_STATUS_SERVER_ERROR
);
927 if (*modtime
>= ppdinfo
.st_mtime
)
928 return (HTTP_STATUS_NOT_MODIFIED
);
931 *modtime
= ppdinfo
.st_mtime
;
932 return (HTTP_STATUS_OK
);
939 * Try finding a printer URI for this printer...
943 if ((http
= _cupsConnect()) == NULL
)
944 return (HTTP_STATUS_SERVICE_UNAVAILABLE
);
946 if (!cups_get_printer_uri(http
, name
, hostname
, sizeof(hostname
), &port
,
947 resource
, sizeof(resource
), 0))
948 return (HTTP_STATUS_NOT_FOUND
);
950 DEBUG_printf(("2cupsGetPPD3: Printer hostname=\"%s\", port=%d", hostname
,
953 if (cupsServer()[0] == '/' && !_cups_strcasecmp(hostname
, "localhost") && port
== ippPort())
956 * Redirect localhost to domain socket...
959 strlcpy(hostname
, cupsServer(), sizeof(hostname
));
962 DEBUG_printf(("2cupsGetPPD3: Redirecting to \"%s\".", hostname
));
966 * Remap local hostname to localhost...
969 httpGetHostname(NULL
, localhost
, sizeof(localhost
));
971 DEBUG_printf(("2cupsGetPPD3: Local hostname=\"%s\"", localhost
));
973 if (!_cups_strcasecmp(localhost
, hostname
))
974 strlcpy(hostname
, "localhost", sizeof(hostname
));
977 * Get the hostname and port number we are connected to...
980 httpGetHostname(http
, http_hostname
, sizeof(http_hostname
));
981 http_port
= httpAddrPort(http
->hostaddr
);
983 DEBUG_printf(("2cupsGetPPD3: Connection hostname=\"%s\", port=%d",
984 http_hostname
, http_port
));
987 * Reconnect to the correct server as needed...
990 if (!_cups_strcasecmp(http_hostname
, hostname
) && port
== http_port
)
992 else if ((http2
= httpConnect2(hostname
, port
, NULL
, AF_UNSPEC
,
993 cupsEncryption(), 1, 30000, NULL
)) == NULL
)
995 DEBUG_puts("1cupsGetPPD3: Unable to connect to server");
997 return (HTTP_STATUS_SERVICE_UNAVAILABLE
);
1001 * Get a temp file...
1005 fd
= open(buffer
, O_CREAT
| O_TRUNC
| O_WRONLY
, 0600);
1007 fd
= cupsTempFd(tempfile
, sizeof(tempfile
));
1012 * Can't open file; close the server connection and return NULL...
1015 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, NULL
, 0);
1020 return (HTTP_STATUS_SERVER_ERROR
);
1024 * And send a request to the HTTP server...
1027 strlcat(resource
, ".ppd", sizeof(resource
));
1030 httpSetField(http2
, HTTP_FIELD_IF_MODIFIED_SINCE
,
1031 httpGetDateString(*modtime
));
1033 status
= cupsGetFd(http2
, resource
, fd
);
1038 * See if we actually got the file or an error...
1041 if (status
== HTTP_STATUS_OK
)
1043 *modtime
= httpGetDateTime(httpGetField(http2
, HTTP_FIELD_DATE
));
1046 strlcpy(buffer
, tempfile
, bufsize
);
1048 else if (status
!= HTTP_STATUS_NOT_MODIFIED
)
1050 _cupsSetHTTPError(status
);
1054 else if (tempfile
[0])
1057 else if (tempfile
[0])
1064 * Return the PPD file...
1067 DEBUG_printf(("1cupsGetPPD3: Returning status %d", status
));
1074 * 'cupsGetPrinters()' - Get a list of printers from the default server.
1076 * This function is deprecated and no longer returns a list of printers - use
1077 * @link cupsGetDests@ instead.
1082 int /* O - Number of printers */
1083 cupsGetPrinters(char ***printers
) /* O - Printers */
1093 * 'cupsGetServerPPD()' - Get an available PPD file from the server.
1095 * This function returns the named PPD file from the server. The
1096 * list of available PPDs is provided by the IPP @code CUPS_GET_PPDS@
1099 * You must remove (unlink) the PPD file when you are finished with
1100 * it. The PPD filename is stored in a static location that will be
1101 * overwritten on the next call to @link cupsGetPPD@, @link cupsGetPPD2@,
1102 * or @link cupsGetServerPPD@.
1104 * @since CUPS 1.3/OS X 10.5@
1107 char * /* O - Name of PPD file or @code NULL@ on error */
1108 cupsGetServerPPD(http_t
*http
, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
1109 const char *name
) /* I - Name of PPD file ("ppd-name") */
1111 int fd
; /* PPD file descriptor */
1112 ipp_t
*request
; /* IPP request */
1113 _cups_globals_t
*cg
= _cupsGlobals();
1114 /* Pointer to library globals */
1118 * Range check input...
1123 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, _("No PPD name"), 1);
1129 if ((http
= _cupsConnect()) == NULL
)
1133 * Get a temp file...
1136 if ((fd
= cupsTempFd(cg
->ppd_filename
, sizeof(cg
->ppd_filename
))) < 0)
1139 * Can't open file; close the server connection and return NULL...
1142 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, NULL
, 0);
1148 * Get the PPD file...
1151 request
= ippNewRequest(IPP_OP_CUPS_GET_PPD
);
1152 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "ppd-name", NULL
,
1155 ippDelete(cupsDoIORequest(http
, request
, "/", -1, fd
));
1159 if (cupsLastError() != IPP_STATUS_OK
)
1161 unlink(cg
->ppd_filename
);
1165 return (cg
->ppd_filename
);
1170 * 'cupsPrintFile()' - Print a file to a printer or class on the default server.
1173 int /* O - Job ID or 0 on error */
1174 cupsPrintFile(const char *name
, /* I - Destination name */
1175 const char *filename
, /* I - File to print */
1176 const char *title
, /* I - Title of job */
1177 int num_options
,/* I - Number of options */
1178 cups_option_t
*options
) /* I - Options */
1180 DEBUG_printf(("cupsPrintFile(name=\"%s\", filename=\"%s\", "
1181 "title=\"%s\", num_options=%d, options=%p)",
1182 name
, filename
, title
, num_options
, options
));
1184 return (cupsPrintFiles2(CUPS_HTTP_DEFAULT
, name
, 1, &filename
, title
,
1185 num_options
, options
));
1190 * 'cupsPrintFile2()' - Print a file to a printer or class on the specified
1193 * @since CUPS 1.1.21/OS X 10.4@
1196 int /* O - Job ID or 0 on error */
1198 http_t
*http
, /* I - Connection to server */
1199 const char *name
, /* I - Destination name */
1200 const char *filename
, /* I - File to print */
1201 const char *title
, /* I - Title of job */
1202 int num_options
, /* I - Number of options */
1203 cups_option_t
*options
) /* I - Options */
1205 DEBUG_printf(("cupsPrintFile2(http=%p, name=\"%s\", filename=\"%s\", "
1206 "title=\"%s\", num_options=%d, options=%p)",
1207 http
, name
, filename
, title
, num_options
, options
));
1209 return (cupsPrintFiles2(http
, name
, 1, &filename
, title
, num_options
,
1215 * 'cupsPrintFiles()' - Print one or more files to a printer or class on the
1219 int /* O - Job ID or 0 on error */
1221 const char *name
, /* I - Destination name */
1222 int num_files
, /* I - Number of files */
1223 const char **files
, /* I - File(s) to print */
1224 const char *title
, /* I - Title of job */
1225 int num_options
, /* I - Number of options */
1226 cups_option_t
*options
) /* I - Options */
1228 DEBUG_printf(("cupsPrintFiles(name=\"%s\", num_files=%d, "
1229 "files=%p, title=\"%s\", num_options=%d, options=%p)",
1230 name
, num_files
, files
, title
, num_options
, options
));
1233 * Print the file(s)...
1236 return (cupsPrintFiles2(CUPS_HTTP_DEFAULT
, name
, num_files
, files
, title
,
1237 num_options
, options
));
1242 * 'cupsPrintFiles2()' - Print one or more files to a printer or class on the
1245 * @since CUPS 1.1.21/OS X 10.4@
1248 int /* O - Job ID or 0 on error */
1250 http_t
*http
, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
1251 const char *name
, /* I - Destination name */
1252 int num_files
, /* I - Number of files */
1253 const char **files
, /* I - File(s) to print */
1254 const char *title
, /* I - Title of job */
1255 int num_options
, /* I - Number of options */
1256 cups_option_t
*options
) /* I - Options */
1258 int i
; /* Looping var */
1259 int job_id
; /* New job ID */
1260 const char *docname
; /* Basename of current filename */
1261 const char *format
; /* Document format */
1262 cups_file_t
*fp
; /* Current file */
1263 char buffer
[8192]; /* Copy buffer */
1264 ssize_t bytes
; /* Bytes in buffer */
1265 http_status_t status
; /* Status of write */
1266 _cups_globals_t
*cg
= _cupsGlobals(); /* Global data */
1267 ipp_status_t cancel_status
; /* Status code to preserve */
1268 char *cancel_message
; /* Error message to preserve */
1271 DEBUG_printf(("cupsPrintFiles2(http=%p, name=\"%s\", num_files=%d, "
1272 "files=%p, title=\"%s\", num_options=%d, options=%p)",
1273 http
, name
, num_files
, files
, title
, num_options
, options
));
1276 * Range check input...
1279 if (!name
|| num_files
< 1 || !files
)
1281 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, strerror(EINVAL
), 0);
1287 * Create the print job...
1290 if ((job_id
= cupsCreateJob(http
, name
, title
, num_options
, options
)) == 0)
1294 * Send each of the files...
1297 if (cupsGetOption("raw", num_options
, options
))
1298 format
= CUPS_FORMAT_RAW
;
1299 else if ((format
= cupsGetOption("document-format", num_options
,
1301 format
= CUPS_FORMAT_AUTO
;
1303 for (i
= 0; i
< num_files
; i
++)
1306 * Start the next file...
1309 if ((docname
= strrchr(files
[i
], '/')) != NULL
)
1314 if ((fp
= cupsFileOpen(files
[i
], "rb")) == NULL
)
1317 * Unable to open print file, cancel the job and return...
1320 _cupsSetError(IPP_STATUS_ERROR_DOCUMENT_ACCESS
, NULL
, 0);
1324 status
= cupsStartDocument(http
, name
, job_id
, docname
, format
,
1325 i
== (num_files
- 1));
1327 while (status
== HTTP_STATUS_CONTINUE
&&
1328 (bytes
= cupsFileRead(fp
, buffer
, sizeof(buffer
))) > 0)
1329 status
= cupsWriteRequestData(http
, buffer
, (size_t)bytes
);
1333 if (status
!= HTTP_STATUS_CONTINUE
|| cupsFinishDocument(http
, name
) != IPP_STATUS_OK
)
1336 * Unable to queue, cancel the job and return...
1346 * If we get here, something happened while sending the print job so we need
1347 * to cancel the job without setting the last error (since we need to preserve
1348 * the current error...
1353 cancel_status
= cg
->last_error
;
1354 cancel_message
= cg
->last_status_message
?
1355 _cupsStrRetain(cg
->last_status_message
) : NULL
;
1357 cupsCancelJob2(http
, name
, job_id
, 0);
1359 cg
->last_error
= cancel_status
;
1360 cg
->last_status_message
= cancel_message
;
1367 * 'cupsStartDocument()' - Add a document to a job created with cupsCreateJob().
1369 * Use @link cupsWriteRequestData@ to write data for the document and
1370 * @link cupsFinishDocument@ to finish the document and get the submission status.
1372 * The MIME type constants @code CUPS_FORMAT_AUTO@, @code CUPS_FORMAT_PDF@,
1373 * @code CUPS_FORMAT_POSTSCRIPT@, @code CUPS_FORMAT_RAW@, and
1374 * @code CUPS_FORMAT_TEXT@ are provided for the "format" argument, although
1375 * any supported MIME type string can be supplied.
1377 * @since CUPS 1.4/OS X 10.6@
1380 http_status_t
/* O - HTTP status of request */
1382 http_t
*http
, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
1383 const char *name
, /* I - Destination name */
1384 int job_id
, /* I - Job ID from @link cupsCreateJob@ */
1385 const char *docname
, /* I - Name of document */
1386 const char *format
, /* I - MIME type or @code CUPS_FORMAT_foo@ */
1387 int last_document
) /* I - 1 for last document in job, 0 otherwise */
1389 char resource
[1024], /* Resource for destinatio */
1390 printer_uri
[1024]; /* Printer URI */
1391 ipp_t
*request
; /* Send-Document request */
1392 http_status_t status
; /* HTTP status */
1396 * Create a Send-Document request...
1399 if ((request
= ippNewRequest(IPP_OP_SEND_DOCUMENT
)) == NULL
)
1401 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, strerror(ENOMEM
), 0);
1402 return (HTTP_STATUS_ERROR
);
1405 httpAssembleURIf(HTTP_URI_CODING_ALL
, printer_uri
, sizeof(printer_uri
), "ipp",
1406 NULL
, "localhost", ippPort(), "/printers/%s", name
);
1407 snprintf(resource
, sizeof(resource
), "/printers/%s", name
);
1409 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
1411 ippAddInteger(request
, IPP_TAG_OPERATION
, IPP_TAG_INTEGER
, "job-id", job_id
);
1412 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "requesting-user-name",
1415 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "document-name",
1418 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_MIMETYPE
,
1419 "document-format", NULL
, format
);
1420 ippAddBoolean(request
, IPP_TAG_OPERATION
, "last-document", (char)last_document
);
1423 * Send and delete the request, then return the status...
1426 status
= cupsSendRequest(http
, request
, resource
, CUPS_LENGTH_VARIABLE
);
1435 * 'cups_get_printer_uri()' - Get the printer-uri-supported attribute for the
1436 * first printer in a class.
1439 static int /* O - 1 on success, 0 on failure */
1440 cups_get_printer_uri(
1441 http_t
*http
, /* I - Connection to server */
1442 const char *name
, /* I - Name of printer or class */
1443 char *host
, /* I - Hostname buffer */
1444 int hostsize
, /* I - Size of hostname buffer */
1445 int *port
, /* O - Port number */
1446 char *resource
, /* I - Resource buffer */
1447 int resourcesize
, /* I - Size of resource buffer */
1448 int depth
) /* I - Depth of query */
1450 int i
; /* Looping var */
1451 int http_port
; /* Port number */
1452 http_t
*http2
; /* Alternate HTTP connection */
1453 ipp_t
*request
, /* IPP request */
1454 *response
; /* IPP response */
1455 ipp_attribute_t
*attr
; /* Current attribute */
1456 char uri
[HTTP_MAX_URI
], /* printer-uri attribute */
1457 scheme
[HTTP_MAX_URI
], /* Scheme name */
1458 username
[HTTP_MAX_URI
], /* Username:password */
1459 classname
[255], /* Temporary class name */
1460 http_hostname
[HTTP_MAX_HOST
];
1461 /* Hostname associated with connection */
1462 static const char * const requested_attrs
[] =
1463 { /* Requested attributes */
1466 "printer-uri-supported",
1471 DEBUG_printf(("4cups_get_printer_uri(http=%p, name=\"%s\", host=%p, hostsize=%d, resource=%p, resourcesize=%d, depth=%d)", http
, name
, host
, hostsize
, resource
, resourcesize
, depth
));
1474 * Setup the printer URI...
1477 if (httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
, "localhost", 0, "/printers/%s", name
) < HTTP_URI_STATUS_OK
)
1479 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, _("Unable to create printer-uri"), 1);
1487 DEBUG_printf(("5cups_get_printer_uri: printer-uri=\"%s\"", uri
));
1490 * Get the hostname and port number we are connected to...
1493 httpGetHostname(http
, http_hostname
, sizeof(http_hostname
));
1494 http_port
= httpAddrPort(http
->hostaddr
);
1496 DEBUG_printf(("5cups_get_printer_uri: http_hostname=\"%s\"", http_hostname
));
1499 * Build an IPP_GET_PRINTER_ATTRIBUTES request, which requires the following
1502 * attributes-charset
1503 * attributes-natural-language
1505 * requested-attributes
1508 request
= ippNewRequest(IPP_OP_GET_PRINTER_ATTRIBUTES
);
1510 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri", NULL
, uri
);
1512 ippAddStrings(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
, "requested-attributes", sizeof(requested_attrs
) / sizeof(requested_attrs
[0]), NULL
, requested_attrs
);
1515 * Do the request and get back a response...
1518 snprintf(resource
, (size_t)resourcesize
, "/printers/%s", name
);
1520 if ((response
= cupsDoRequest(http
, request
, resource
)) != NULL
)
1522 const char *device_uri
= NULL
; /* device-uri value */
1524 if ((attr
= ippFindAttribute(response
, "device-uri", IPP_TAG_URI
)) != NULL
)
1526 device_uri
= attr
->values
[0].string
.text
;
1527 DEBUG_printf(("5cups_get_printer_uri: device-uri=\"%s\"", device_uri
));
1531 (!strncmp(device_uri
, "ipp://", 6) ||
1532 !strncmp(device_uri
, "ipps://", 7) ||
1533 ((strstr(device_uri
, "._ipp.") != NULL
||
1534 strstr(device_uri
, "._ipps.") != NULL
) &&
1535 !strcmp(device_uri
+ strlen(device_uri
) - 5, "/cups"))))
1538 * Statically-configured shared printer.
1541 httpSeparateURI(HTTP_URI_CODING_ALL
, _httpResolveURI(device_uri
, uri
, sizeof(uri
), _HTTP_RESOLVE_DEFAULT
, NULL
, NULL
), scheme
, sizeof(scheme
), username
, sizeof(username
), host
, hostsize
, port
, resource
, resourcesize
);
1542 ippDelete(response
);
1544 DEBUG_printf(("5cups_get_printer_uri: Resolved to host=\"%s\", port=%d, resource=\"%s\"", host
, *port
, resource
));
1547 else if ((attr
= ippFindAttribute(response
, "member-uris", IPP_TAG_URI
)) != NULL
)
1550 * Get the first actual printer name in the class...
1553 DEBUG_printf(("5cups_get_printer_uri: Got member-uris with %d values.", ippGetCount(attr
)));
1555 for (i
= 0; i
< attr
->num_values
; i
++)
1557 DEBUG_printf(("5cups_get_printer_uri: member-uris[%d]=\"%s\"", i
, ippGetString(attr
, i
, NULL
)));
1559 httpSeparateURI(HTTP_URI_CODING_ALL
, attr
->values
[i
].string
.text
, scheme
, sizeof(scheme
), username
, sizeof(username
), host
, hostsize
, port
, resource
, resourcesize
);
1560 if (!strncmp(resource
, "/printers/", 10))
1566 ippDelete(response
);
1568 DEBUG_printf(("5cups_get_printer_uri: Found printer member with host=\"%s\", port=%d, resource=\"%s\"", host
, *port
, resource
));
1574 * No printers in this class - try recursively looking for a printer,
1575 * but not more than 3 levels deep...
1580 for (i
= 0; i
< attr
->num_values
; i
++)
1582 httpSeparateURI(HTTP_URI_CODING_ALL
, attr
->values
[i
].string
.text
,
1583 scheme
, sizeof(scheme
), username
, sizeof(username
),
1584 host
, hostsize
, port
, resource
, resourcesize
);
1585 if (!strncmp(resource
, "/classes/", 9))
1588 * Found a class! Connect to the right server...
1591 if (!_cups_strcasecmp(http_hostname
, host
) && *port
== http_port
)
1593 else if ((http2
= httpConnect2(host
, *port
, NULL
, AF_UNSPEC
, cupsEncryption(), 1, 30000, NULL
)) == NULL
)
1595 DEBUG_puts("8cups_get_printer_uri: Unable to connect to server");
1601 * Look up printers on that server...
1604 strlcpy(classname
, resource
+ 9, sizeof(classname
));
1606 cups_get_printer_uri(http2
, classname
, host
, hostsize
, port
,
1607 resource
, resourcesize
, depth
+ 1);
1610 * Close the connection as needed...
1622 else if ((attr
= ippFindAttribute(response
, "printer-uri-supported", IPP_TAG_URI
)) != NULL
)
1624 httpSeparateURI(HTTP_URI_CODING_ALL
, _httpResolveURI(attr
->values
[0].string
.text
, uri
, sizeof(uri
), _HTTP_RESOLVE_DEFAULT
, NULL
, NULL
), scheme
, sizeof(scheme
), username
, sizeof(username
), host
, hostsize
, port
, resource
, resourcesize
);
1625 ippDelete(response
);
1627 DEBUG_printf(("5cups_get_printer_uri: Resolved to host=\"%s\", port=%d, resource=\"%s\"", host
, *port
, resource
));
1629 if (!strncmp(resource
, "/classes/", 9))
1631 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, _("No printer-uri found for class"), 1);
1636 DEBUG_puts("5cups_get_printer_uri: Not returning class.");
1643 ippDelete(response
);
1646 if (cupsLastError() != IPP_STATUS_ERROR_NOT_FOUND
)
1647 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, _("No printer-uri found"), 1);
1652 DEBUG_puts("5cups_get_printer_uri: Printer URI not found.");